Passed
Push — drop-deprecated ( db0b1f )
by Michael
27:00
created

SQLSrvStatement::errorCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver\SQLSrv;
6
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\Driver\Statement;
9
use Doctrine\DBAL\Driver\StatementIterator;
10
use Doctrine\DBAL\FetchMode;
11
use Doctrine\DBAL\ParameterType;
12
use IteratorAggregate;
13
use const SQLSRV_ENC_BINARY;
14
use const SQLSRV_FETCH_ASSOC;
15
use const SQLSRV_FETCH_BOTH;
16
use const SQLSRV_FETCH_NUMERIC;
17
use const SQLSRV_PARAM_IN;
18
use function array_key_exists;
19
use function count;
20
use function in_array;
21
use function is_int;
22
use function is_numeric;
23
use function sqlsrv_execute;
24
use function sqlsrv_fetch;
25
use function sqlsrv_fetch_array;
26
use function sqlsrv_fetch_object;
27
use function sqlsrv_get_field;
28
use function sqlsrv_next_result;
29
use function sqlsrv_num_fields;
30
use function SQLSRV_PHPTYPE_STREAM;
31
use function SQLSRV_PHPTYPE_STRING;
32
use function sqlsrv_prepare;
33
use function sqlsrv_rows_affected;
34
use function SQLSRV_SQLTYPE_VARBINARY;
35
use function stripos;
36
37
/**
38
 * SQL Server Statement.
39
 */
40
class SQLSrvStatement implements IteratorAggregate, Statement
41
{
42
    /**
43
     * The SQLSRV Resource.
44
     *
45
     * @var resource
46
     */
47
    private $conn;
48
49
    /**
50
     * The SQL statement to execute.
51
     *
52
     * @var string
53
     */
54
    private $sql;
55
56
    /**
57
     * The SQLSRV statement resource.
58
     *
59
     * @var resource|null
60
     */
61
    private $stmt;
62
63
    /**
64
     * References to the variables bound as statement parameters.
65
     *
66
     * @var mixed
67
     */
68
    private $variables = [];
69
70
    /**
71
     * Bound parameter types.
72
     *
73
     * @var int[]
74
     */
75
    private $types = [];
76
77
    /**
78
     * Translations.
79
     *
80
     * @var int[]
81
     */
82
    private static $fetchMap = [
83
        FetchMode::MIXED       => SQLSRV_FETCH_BOTH,
84
        FetchMode::ASSOCIATIVE => SQLSRV_FETCH_ASSOC,
85
        FetchMode::NUMERIC     => SQLSRV_FETCH_NUMERIC,
86
    ];
87
88
    /**
89
     * The name of the default class to instantiate when fetching class instances.
90
     *
91
     * @var string
92
     */
93
    private $defaultFetchClass = '\stdClass';
94
95
    /**
96
     * The constructor arguments for the default class to instantiate when fetching class instances.
97
     *
98
     * @var mixed[]
99
     */
100
    private $defaultFetchClassCtorArgs = [];
101
102
    /**
103
     * The fetch style.
104
     *
105
     * @var int
106
     */
107
    private $defaultFetchMode = FetchMode::MIXED;
108
109
    /**
110
     * The last insert ID.
111
     *
112
     * @var LastInsertId|null
113
     */
114
    private $lastInsertId;
115
116
    /**
117
     * Indicates whether the statement is in the state when fetching results is possible
118
     *
119
     * @var bool
120
     */
121
    private $result = false;
122
123
    /**
124
     * Append to any INSERT query to retrieve the last insert id.
125
     */
126
    public const LAST_INSERT_ID_SQL = ';SELECT SCOPE_IDENTITY() AS LastInsertId;';
127
128
    /**
129
     * @param resource $conn
130
     * @param string   $sql
131
     */
132 253
    public function __construct($conn, $sql, ?LastInsertId $lastInsertId = null)
133
    {
134 253
        $this->conn = $conn;
135 253
        $this->sql  = $sql;
136
137 253
        if (stripos($sql, 'INSERT INTO ') !== 0) {
138 252
            return;
139
        }
140
141 253
        $this->sql         .= self::LAST_INSERT_ID_SQL;
142 253
        $this->lastInsertId = $lastInsertId;
143 253
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 253
    public function bindValue($param, $value, $type = ParameterType::STRING) : void
149
    {
150 253
        if (! is_numeric($param)) {
151
            throw new SQLSrvException(
152
                'sqlsrv does not support named parameters to queries, use question mark (?) placeholders instead.'
153
            );
154
        }
155
156 253
        $this->variables[$param] = $value;
157 253
        $this->types[$param]     = $type;
158 253
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163 248
    public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) : void
164
    {
165 248
        if (! is_numeric($column)) {
166
            throw new SQLSrvException('sqlsrv does not support named parameters to queries, use question mark (?) placeholders instead.');
167
        }
168
169 248
        $this->variables[$column] =& $variable;
170 248
        $this->types[$column]     = $type;
171
172
        // unset the statement resource if it exists as the new one will need to be bound to the new variable
173 248
        $this->stmt = null;
174 248
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179 120
    public function closeCursor() : void
180
    {
181
        // not having the result means there's nothing to close
182 120
        if ($this->stmt === null || ! $this->result) {
183 45
            return;
184
        }
185
186
        // emulate it by fetching and discarding rows, similarly to what PDO does in this case
187
        // @link http://php.net/manual/en/pdostatement.closecursor.php
188
        // @link https://github.com/php/php-src/blob/php-7.0.11/ext/pdo/pdo_stmt.c#L2075
189
        // deliberately do not consider multiple result sets, since doctrine/dbal doesn't support them
190 120
        while (sqlsrv_fetch($this->stmt)) {
191
        }
192
193 120
        $this->result = false;
194 120
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199 120
    public function columnCount()
200
    {
201 120
        if ($this->stmt === null) {
202
            return 0;
203
        }
204
205 120
        return sqlsrv_num_fields($this->stmt) ?: 0;
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211 253
    public function execute($params = null) : void
212
    {
213 253
        if ($params) {
214 244
            $hasZeroIndex = array_key_exists(0, $params);
215
216 244
            foreach ($params as $key => $val) {
217 244
                if ($hasZeroIndex && is_int($key)) {
218 244
                    $this->bindValue($key + 1, $val);
219
                } else {
220
                    $this->bindValue($key, $val);
221
                }
222
            }
223
        }
224
225 253
        if (! $this->stmt) {
226 253
            $this->stmt = $this->prepare();
227
        }
228
229 253
        if (! sqlsrv_execute($this->stmt)) {
230
            throw SQLSrvException::fromSqlSrvErrors();
231
        }
232
233 253
        if ($this->lastInsertId) {
234 253
            sqlsrv_next_result($this->stmt);
235 253
            sqlsrv_fetch($this->stmt);
236 253
            $this->lastInsertId->setId(sqlsrv_get_field($this->stmt, 0));
237
        }
238
239 253
        $this->result = true;
240 253
    }
241
242
    /**
243
     * Prepares SQL Server statement resource
244
     *
245
     * @return resource
246
     *
247
     * @throws SQLSrvException
248
     */
249 253
    private function prepare()
250
    {
251 253
        $params = [];
252
253 253
        foreach ($this->variables as $column => &$variable) {
254 253
            switch ($this->types[$column]) {
255
                case ParameterType::LARGE_OBJECT:
256 253
                    $params[$column - 1] = [
257 253
                        &$variable,
258
                        SQLSRV_PARAM_IN,
259 253
                        SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY),
260 253
                        SQLSRV_SQLTYPE_VARBINARY('max'),
261
                    ];
262 253
                    break;
263
264
                case ParameterType::BINARY:
265 18
                    $params[$column - 1] = [
266 18
                        &$variable,
267
                        SQLSRV_PARAM_IN,
268 18
                        SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY),
269
                    ];
270 18
                    break;
271
272
                default:
273 253
                    $params[$column - 1] =& $variable;
274 253
                    break;
275
            }
276
        }
277
278 253
        $stmt = sqlsrv_prepare($this->conn, $this->sql, $params);
279
280 253
        if (! $stmt) {
0 ignored issues
show
introduced by
$stmt is of type false|resource, thus it always evaluated to false.
Loading history...
281 146
            throw SQLSrvException::fromSqlSrvErrors();
282
        }
283
284 253
        return $stmt;
285
    }
286
287
    /**
288
     * {@inheritdoc}
289
     */
290 252
    public function setFetchMode($fetchMode, ...$args) : void
291
    {
292 252
        $this->defaultFetchMode = $fetchMode;
293
294 252
        if (isset($args[0])) {
295 154
            $this->defaultFetchClass = $args[0];
296
        }
297
298 252
        if (! isset($args[1])) {
299 252
            return;
300
        }
301
302
        $this->defaultFetchClassCtorArgs = (array) $args[1];
303
    }
304
305
    /**
306
     * {@inheritdoc}
307
     */
308 255
    public function getIterator()
309
    {
310 255
        return new StatementIterator($this);
311
    }
312
313
    /**
314
     * {@inheritdoc}
315
     *
316
     * @throws SQLSrvException
317
     */
318 252
    public function fetch($fetchMode = null, ...$args)
319
    {
320
        // do not try fetching from the statement if it's not expected to contain result
321
        // in order to prevent exceptional situation
322 252
        if ($this->stmt === null || ! $this->result) {
323 48
            return false;
324
        }
325
326 252
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
327
328 252
        if ($fetchMode === FetchMode::COLUMN) {
329 39
            return $this->fetchColumn();
330
        }
331
332 252
        if (isset(self::$fetchMap[$fetchMode])) {
333 252
            return sqlsrv_fetch_array($this->stmt, self::$fetchMap[$fetchMode]) ?: false;
334
        }
335
336 157
        if (in_array($fetchMode, [FetchMode::STANDARD_OBJECT, FetchMode::CUSTOM_OBJECT], true)) {
337 157
            $className = $this->defaultFetchClass;
338 157
            $ctorArgs  = $this->defaultFetchClassCtorArgs;
339
340 157
            if (count($args) > 0) {
341 156
                $className = $args[0];
342 156
                $ctorArgs  = $args[1] ?? [];
343
            }
344
345 157
            return sqlsrv_fetch_object($this->stmt, $className, $ctorArgs) ?: false;
346
        }
347
348
        throw new SQLSrvException('Fetch mode is not supported!');
349
    }
350
351
    /**
352
     * {@inheritdoc}
353
     */
354 252
    public function fetchAll($fetchMode = null, ...$args)
355
    {
356 252
        $rows = [];
357
358 252
        switch ($fetchMode) {
359
            case FetchMode::CUSTOM_OBJECT:
360 156
                while (($row = $this->fetch($fetchMode, ...$args)) !== false) {
361 156
                    $rows[] = $row;
362
                }
363 156
                break;
364
365
            case FetchMode::COLUMN:
366 252
                while (($row = $this->fetchColumn()) !== false) {
367 252
                    $rows[] = $row;
368
                }
369 252
                break;
370
371
            default:
372 242
                while (($row = $this->fetch($fetchMode)) !== false) {
373 242
                    $rows[] = $row;
374
                }
375
        }
376
377 252
        return $rows;
378
    }
379
380
    /**
381
     * {@inheritdoc}
382
     */
383 252
    public function fetchColumn($columnIndex = 0)
384
    {
385 252
        $row = $this->fetch(FetchMode::NUMERIC);
386
387 252
        if ($row === false) {
388 252
            return false;
389
        }
390
391 252
        if (! array_key_exists($columnIndex, $row)) {
0 ignored issues
show
Bug introduced by
It seems like $row can also be of type object; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

391
        if (! array_key_exists($columnIndex, /** @scrutinizer ignore-type */ $row)) {
Loading history...
392 38
            throw DBALException::invalidColumnIndex($columnIndex, count($row));
393
        }
394
395 252
        return $row[$columnIndex];
396
    }
397
398
    /**
399
     * {@inheritdoc}
400
     */
401 253
    public function rowCount() : int
402
    {
403 253
        if ($this->stmt === null) {
404
            return 0;
405
        }
406
407 253
        return sqlsrv_rows_affected($this->stmt) ?: 0;
408
    }
409
}
410