Completed
Pull Request — 3.0.x (#3070)
by Sergei
63:18
created

SQLAnywhereStatement::rowCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\DBAL\Driver\SQLAnywhere;
4
5
use Doctrine\DBAL\Driver\Statement;
6
use Doctrine\DBAL\Driver\StatementIterator;
7
use Doctrine\DBAL\FetchMode;
8
use Doctrine\DBAL\ParameterType;
9
use IteratorAggregate;
10
use const SASQL_BOTH;
0 ignored issues
show
Bug introduced by
The constant SASQL_BOTH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
11
use function array_key_exists;
12
use function assert;
13
use function is_int;
14
use function is_resource;
15
use function sasql_fetch_array;
0 ignored issues
show
introduced by
The function sasql_fetch_array was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
16
use function sasql_fetch_assoc;
0 ignored issues
show
introduced by
The function sasql_fetch_assoc was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
17
use function sasql_fetch_row;
0 ignored issues
show
introduced by
The function sasql_fetch_row was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
18
use function sasql_prepare;
0 ignored issues
show
introduced by
The function sasql_prepare was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
19
use function sasql_stmt_affected_rows;
0 ignored issues
show
introduced by
The function sasql_stmt_affected_rows was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
20
use function sasql_stmt_bind_param_ex;
0 ignored issues
show
introduced by
The function sasql_stmt_bind_param_ex was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
21
use function sasql_stmt_errno;
0 ignored issues
show
introduced by
The function sasql_stmt_errno was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
22
use function sasql_stmt_error;
0 ignored issues
show
introduced by
The function sasql_stmt_error was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
23
use function sasql_stmt_execute;
0 ignored issues
show
introduced by
The function sasql_stmt_execute was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
24
use function sasql_stmt_field_count;
0 ignored issues
show
introduced by
The function sasql_stmt_field_count was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
25
use function sasql_stmt_reset;
0 ignored issues
show
introduced by
The function sasql_stmt_reset was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
26
use function sasql_stmt_result_metadata;
0 ignored issues
show
introduced by
The function sasql_stmt_result_metadata was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
27
28
/**
29
 * SAP SQL Anywhere implementation of the Statement interface.
30
 */
31
class SQLAnywhereStatement implements IteratorAggregate, Statement
32
{
33
    /** @var resource The connection resource. */
34
    private $conn;
35
36
    /** @var int Default fetch mode to use. */
37
    private $defaultFetchMode = FetchMode::MIXED;
38
39
    /** @var resource|null The result set resource to fetch. */
40
    private $result;
41
42
    /** @var resource The prepared SQL statement to execute. */
43
    private $stmt;
44
45
    /** @var mixed[] The references to bound parameter values. */
46
    private $boundValues = [];
47
48
    /**
49
     * Prepares given statement for given connection.
50
     *
51
     * @param resource $conn The connection resource to use.
52
     * @param string   $sql  The SQL statement to prepare.
53
     *
54
     * @throws SQLAnywhereException
55
     */
56
    public function __construct($conn, $sql)
57
    {
58
        if (! is_resource($conn)) {
59
            throw new SQLAnywhereException('Invalid SQL Anywhere connection resource: ' . $conn);
60
        }
61
62
        $this->conn = $conn;
63
        $this->stmt = sasql_prepare($conn, $sql);
0 ignored issues
show
Bug introduced by
The function sasql_prepare was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        $this->stmt = /** @scrutinizer ignore-call */ sasql_prepare($conn, $sql);
Loading history...
64
65
        if (! is_resource($this->stmt)) {
66
            throw SQLAnywhereException::fromSQLAnywhereError($conn);
67
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     *
73
     * @throws SQLAnywhereException
74
     */
75
    public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null)
76
    {
77
        assert(is_int($column));
78
79
        switch ($type) {
80
            case ParameterType::INTEGER:
81
            case ParameterType::BOOLEAN:
82
                $type = 'i';
83
                break;
84
85
            case ParameterType::LARGE_OBJECT:
86
                $type = 'b';
87
                break;
88
89
            case ParameterType::NULL:
90
            case ParameterType::STRING:
91
            case ParameterType::BINARY:
92
                $type = 's';
93
                break;
94
95
            default:
96
                throw new SQLAnywhereException('Unknown type: ' . $type);
97
        }
98
99
        $this->boundValues[$column] =& $variable;
100
101
        if (! sasql_stmt_bind_param_ex($this->stmt, $column - 1, $variable, $type, $variable === null)) {
0 ignored issues
show
Bug introduced by
The function sasql_stmt_bind_param_ex was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
        if (! /** @scrutinizer ignore-call */ sasql_stmt_bind_param_ex($this->stmt, $column - 1, $variable, $type, $variable === null)) {
Loading history...
102
            throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt);
103
        }
104
105
        return true;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function bindValue($param, $value, $type = ParameterType::STRING)
112
    {
113
        return $this->bindParam($param, $value, $type);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     *
119
     * @throws SQLAnywhereException
120
     */
121
    public function closeCursor()
122
    {
123
        if (! sasql_stmt_reset($this->stmt)) {
0 ignored issues
show
Bug introduced by
The function sasql_stmt_reset was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

123
        if (! /** @scrutinizer ignore-call */ sasql_stmt_reset($this->stmt)) {
Loading history...
124
            throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt);
125
        }
126
127
        return true;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function columnCount()
134
    {
135
        return sasql_stmt_field_count($this->stmt);
0 ignored issues
show
Bug introduced by
The function sasql_stmt_field_count was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
        return /** @scrutinizer ignore-call */ sasql_stmt_field_count($this->stmt);
Loading history...
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function errorCode()
142
    {
143
        return sasql_stmt_errno($this->stmt);
0 ignored issues
show
Bug introduced by
The function sasql_stmt_errno was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

143
        return /** @scrutinizer ignore-call */ sasql_stmt_errno($this->stmt);
Loading history...
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function errorInfo()
150
    {
151
        return sasql_stmt_error($this->stmt);
0 ignored issues
show
Bug introduced by
The function sasql_stmt_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

151
        return /** @scrutinizer ignore-call */ sasql_stmt_error($this->stmt);
Loading history...
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     *
157
     * @throws SQLAnywhereException
158
     */
159
    public function execute($params = null)
160
    {
161
        if ($params !== null) {
162
            $hasZeroIndex = array_key_exists(0, $params);
163
164
            foreach ($params as $key => $val) {
165
                if ($hasZeroIndex && is_int($key)) {
166
                    $this->bindValue($key + 1, $val);
167
                } else {
168
                    $this->bindValue($key, $val);
169
                }
170
            }
171
        }
172
173
        if (! sasql_stmt_execute($this->stmt)) {
0 ignored issues
show
Bug introduced by
The function sasql_stmt_execute was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

173
        if (! /** @scrutinizer ignore-call */ sasql_stmt_execute($this->stmt)) {
Loading history...
174
            throw SQLAnywhereException::fromSQLAnywhereError($this->conn, $this->stmt);
175
        }
176
177
        $this->result = sasql_stmt_result_metadata($this->stmt);
0 ignored issues
show
Bug introduced by
The function sasql_stmt_result_metadata was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

177
        $this->result = /** @scrutinizer ignore-call */ sasql_stmt_result_metadata($this->stmt);
Loading history...
178
179
        return true;
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     *
185
     * @throws SQLAnywhereException
186
     */
187
    public function fetch($fetchMode = null)
188
    {
189
        if (! is_resource($this->result)) {
190
            return false;
191
        }
192
193
        $fetchMode = $fetchMode ?? $this->defaultFetchMode;
194
195
        switch ($fetchMode) {
196
            case FetchMode::COLUMN:
197
                return $this->fetchColumn();
198
199
            case FetchMode::ASSOCIATIVE:
200
                return sasql_fetch_assoc($this->result);
0 ignored issues
show
Bug introduced by
The function sasql_fetch_assoc was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

200
                return /** @scrutinizer ignore-call */ sasql_fetch_assoc($this->result);
Loading history...
201
202
            case FetchMode::MIXED:
203
                return sasql_fetch_array($this->result, SASQL_BOTH);
0 ignored issues
show
Bug introduced by
The function sasql_fetch_array was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

203
                return /** @scrutinizer ignore-call */ sasql_fetch_array($this->result, SASQL_BOTH);
Loading history...
Bug introduced by
The constant SASQL_BOTH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
204
205
            case FetchMode::NUMERIC:
206
                return sasql_fetch_row($this->result);
0 ignored issues
show
Bug introduced by
The function sasql_fetch_row was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

206
                return /** @scrutinizer ignore-call */ sasql_fetch_row($this->result);
Loading history...
207
208
            default:
209
                throw new SQLAnywhereException('Fetch mode is not supported: ' . $fetchMode);
210
        }
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216
    public function fetchAll($fetchMode = null)
217
    {
218
        $rows = [];
219
220
        switch ($fetchMode) {
221
            case FetchMode::COLUMN:
222
                while (($row = $this->fetchColumn()) !== false) {
223
                    $rows[] = $row;
224
                }
225
                break;
226
227
            default:
228
                while (($row = $this->fetch($fetchMode)) !== false) {
229
                    $rows[] = $row;
230
                }
231
        }
232
233
        return $rows;
234
    }
235
236
    /**
237
     * {@inheritdoc}
238
     */
239
    public function fetchColumn()
240
    {
241
        $row = $this->fetch(FetchMode::NUMERIC);
242
243
        if ($row === false) {
244
            return false;
245
        }
246
247
        return $row[0] ?? null;
248
    }
249
250
    /**
251
     * {@inheritdoc}
252
     */
253
    public function getIterator()
254
    {
255
        return new StatementIterator($this);
256
    }
257
258
    /**
259
     * {@inheritdoc}
260
     */
261
    public function rowCount() : int
262
    {
263
        return sasql_stmt_affected_rows($this->stmt);
0 ignored issues
show
Bug introduced by
The function sasql_stmt_affected_rows was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

263
        return /** @scrutinizer ignore-call */ sasql_stmt_affected_rows($this->stmt);
Loading history...
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     */
269
    public function setFetchMode($fetchMode)
270
    {
271
        $this->defaultFetchMode = $fetchMode;
272
273
        return true;
274
    }
275
}
276