1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Doctrine\DBAL\Driver\SQLSrv; |
||
6 | |||
7 | use Doctrine\DBAL\Driver\Statement; |
||
8 | use Doctrine\DBAL\Driver\StatementIterator; |
||
9 | use Doctrine\DBAL\Exception\InvalidColumnIndex; |
||
10 | use Doctrine\DBAL\FetchMode; |
||
11 | use Doctrine\DBAL\ParameterType; |
||
12 | use IteratorAggregate; |
||
13 | use function array_key_exists; |
||
14 | use function assert; |
||
15 | use function count; |
||
16 | use function in_array; |
||
17 | use function is_int; |
||
18 | use function sqlsrv_execute; |
||
19 | use function sqlsrv_fetch; |
||
20 | use function sqlsrv_fetch_array; |
||
21 | use function sqlsrv_fetch_object; |
||
22 | use function sqlsrv_get_field; |
||
23 | use function sqlsrv_next_result; |
||
24 | use function sqlsrv_num_fields; |
||
25 | use function SQLSRV_PHPTYPE_STREAM; |
||
26 | use function SQLSRV_PHPTYPE_STRING; |
||
27 | use function sqlsrv_prepare; |
||
28 | use function sqlsrv_rows_affected; |
||
29 | use function SQLSRV_SQLTYPE_VARBINARY; |
||
30 | use function stripos; |
||
31 | use const SQLSRV_ENC_BINARY; |
||
32 | use const SQLSRV_FETCH_ASSOC; |
||
33 | use const SQLSRV_FETCH_BOTH; |
||
34 | use const SQLSRV_FETCH_NUMERIC; |
||
35 | use const SQLSRV_PARAM_IN; |
||
36 | |||
37 | /** |
||
38 | * SQL Server Statement. |
||
39 | */ |
||
40 | final 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 | private const LAST_INSERT_ID_SQL = ';SELECT SCOPE_IDENTITY() AS LastInsertId;'; |
||
127 | |||
128 | /** |
||
129 | * @param resource $conn |
||
130 | */ |
||
131 | public function __construct($conn, string $sql, ?LastInsertId $lastInsertId = null) |
||
132 | { |
||
133 | $this->conn = $conn; |
||
134 | $this->sql = $sql; |
||
135 | |||
136 | if (stripos($sql, 'INSERT INTO ') !== 0) { |
||
137 | return; |
||
138 | } |
||
139 | |||
140 | $this->sql .= self::LAST_INSERT_ID_SQL; |
||
141 | $this->lastInsertId = $lastInsertId; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | public function bindValue($param, $value, int $type = ParameterType::STRING) : void |
||
148 | { |
||
149 | assert(is_int($param)); |
||
150 | |||
151 | $this->variables[$param] = $value; |
||
152 | $this->types[$param] = $type; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | public function bindParam($param, &$variable, int $type = ParameterType::STRING, ?int $length = null) : void |
||
159 | { |
||
160 | assert(is_int($param)); |
||
161 | |||
162 | $this->variables[$param] =& $variable; |
||
163 | $this->types[$param] = $type; |
||
164 | |||
165 | // unset the statement resource if it exists as the new one will need to be bound to the new variable |
||
166 | $this->stmt = null; |
||
167 | } |
||
168 | |||
169 | public function closeCursor() : void |
||
170 | { |
||
171 | // not having the result means there's nothing to close |
||
172 | if ($this->stmt === null || ! $this->result) { |
||
173 | return; |
||
174 | } |
||
175 | |||
176 | // emulate it by fetching and discarding rows, similarly to what PDO does in this case |
||
177 | // @link http://php.net/manual/en/pdostatement.closecursor.php |
||
178 | // @link https://github.com/php/php-src/blob/php-7.0.11/ext/pdo/pdo_stmt.c#L2075 |
||
179 | // deliberately do not consider multiple result sets, since doctrine/dbal doesn't support them |
||
180 | while (sqlsrv_fetch($this->stmt) !== false) { |
||
181 | } |
||
182 | |||
183 | $this->result = false; |
||
184 | } |
||
185 | |||
186 | public function columnCount() : int |
||
187 | { |
||
188 | if ($this->stmt === null) { |
||
189 | return 0; |
||
190 | } |
||
191 | |||
192 | return sqlsrv_num_fields($this->stmt) ?: 0; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | */ |
||
198 | public function execute(?array $params = null) : void |
||
199 | { |
||
200 | if ($params) { |
||
201 | foreach ($params as $key => $val) { |
||
202 | if (is_int($key)) { |
||
203 | $this->bindValue($key + 1, $val); |
||
204 | } else { |
||
205 | $this->bindValue($key, $val); |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | |||
210 | if (! $this->stmt) { |
||
211 | $this->stmt = $this->prepare(); |
||
212 | } |
||
213 | |||
214 | if (! sqlsrv_execute($this->stmt)) { |
||
215 | throw SQLSrvException::fromSqlSrvErrors(); |
||
216 | } |
||
217 | |||
218 | if ($this->lastInsertId) { |
||
219 | sqlsrv_next_result($this->stmt); |
||
220 | sqlsrv_fetch($this->stmt); |
||
221 | $this->lastInsertId->setId(sqlsrv_get_field($this->stmt, 0)); |
||
222 | } |
||
223 | |||
224 | $this->result = true; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | public function setFetchMode(int $fetchMode, ...$args) : void |
||
231 | { |
||
232 | $this->defaultFetchMode = $fetchMode; |
||
233 | |||
234 | if (isset($args[0])) { |
||
235 | $this->defaultFetchClass = $args[0]; |
||
236 | } |
||
237 | |||
238 | if (! isset($args[1])) { |
||
239 | return; |
||
240 | } |
||
241 | |||
242 | $this->defaultFetchClassCtorArgs = (array) $args[1]; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | public function getIterator() |
||
249 | { |
||
250 | return new StatementIterator($this); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * {@inheritdoc} |
||
255 | * |
||
256 | * @throws SQLSrvException |
||
257 | */ |
||
258 | public function fetch(?int $fetchMode = null, ...$args) |
||
259 | { |
||
260 | // do not try fetching from the statement if it's not expected to contain result |
||
261 | // in order to prevent exceptional situation |
||
262 | if ($this->stmt === null || ! $this->result) { |
||
263 | return false; |
||
264 | } |
||
265 | |||
266 | $fetchMode = $fetchMode ?: $this->defaultFetchMode; |
||
267 | |||
268 | if ($fetchMode === FetchMode::COLUMN) { |
||
269 | return $this->fetchColumn(); |
||
270 | } |
||
271 | |||
272 | if (isset(self::$fetchMap[$fetchMode])) { |
||
273 | return sqlsrv_fetch_array($this->stmt, self::$fetchMap[$fetchMode]) ?: false; |
||
274 | } |
||
275 | |||
276 | if (in_array($fetchMode, [FetchMode::STANDARD_OBJECT, FetchMode::CUSTOM_OBJECT], true)) { |
||
277 | $className = $this->defaultFetchClass; |
||
278 | $ctorArgs = $this->defaultFetchClassCtorArgs; |
||
279 | |||
280 | if (count($args) > 0) { |
||
281 | $className = $args[0]; |
||
282 | $ctorArgs = $args[1] ?? []; |
||
283 | } |
||
284 | |||
285 | return sqlsrv_fetch_object($this->stmt, $className, $ctorArgs) ?: false; |
||
286 | } |
||
287 | |||
288 | throw new SQLSrvException('Fetch mode is not supported.'); |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * {@inheritdoc} |
||
293 | */ |
||
294 | public function fetchAll(?int $fetchMode = null, ...$args) : array |
||
295 | { |
||
296 | $rows = []; |
||
297 | |||
298 | switch ($fetchMode) { |
||
299 | case FetchMode::CUSTOM_OBJECT: |
||
300 | while (($row = $this->fetch($fetchMode, ...$args)) !== false) { |
||
301 | $rows[] = $row; |
||
302 | } |
||
303 | |||
304 | break; |
||
305 | |||
306 | case FetchMode::COLUMN: |
||
307 | while (($row = $this->fetchColumn()) !== false) { |
||
308 | $rows[] = $row; |
||
309 | } |
||
310 | |||
311 | break; |
||
312 | |||
313 | default: |
||
314 | while (($row = $this->fetch($fetchMode)) !== false) { |
||
315 | $rows[] = $row; |
||
316 | } |
||
317 | } |
||
318 | |||
319 | return $rows; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * {@inheritdoc} |
||
324 | */ |
||
325 | public function fetchColumn(int $columnIndex = 0) |
||
326 | { |
||
327 | $row = $this->fetch(FetchMode::NUMERIC); |
||
328 | |||
329 | if ($row === false) { |
||
330 | return false; |
||
331 | } |
||
332 | |||
333 | if (! array_key_exists($columnIndex, $row)) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
334 | throw InvalidColumnIndex::new($columnIndex, count($row)); |
||
335 | } |
||
336 | |||
337 | return $row[$columnIndex]; |
||
338 | } |
||
339 | |||
340 | public function rowCount() : int |
||
341 | { |
||
342 | if ($this->stmt === null) { |
||
343 | return 0; |
||
344 | } |
||
345 | |||
346 | return sqlsrv_rows_affected($this->stmt) ?: 0; |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Prepares SQL Server statement resource |
||
351 | * |
||
352 | * @return resource |
||
353 | * |
||
354 | * @throws SQLSrvException |
||
355 | */ |
||
356 | private function prepare() |
||
357 | { |
||
358 | $params = []; |
||
359 | |||
360 | foreach ($this->variables as $column => &$variable) { |
||
361 | switch ($this->types[$column]) { |
||
362 | case ParameterType::LARGE_OBJECT: |
||
363 | $params[$column - 1] = [ |
||
364 | &$variable, |
||
365 | SQLSRV_PARAM_IN, |
||
366 | SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY), |
||
367 | SQLSRV_SQLTYPE_VARBINARY('max'), |
||
368 | ]; |
||
369 | break; |
||
370 | |||
371 | case ParameterType::BINARY: |
||
372 | $params[$column - 1] = [ |
||
373 | &$variable, |
||
374 | SQLSRV_PARAM_IN, |
||
375 | SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY), |
||
376 | ]; |
||
377 | break; |
||
378 | |||
379 | default: |
||
380 | $params[$column - 1] =& $variable; |
||
381 | break; |
||
382 | } |
||
383 | } |
||
384 | |||
385 | $stmt = sqlsrv_prepare($this->conn, $this->sql, $params); |
||
386 | |||
387 | if (! $stmt) { |
||
388 | throw SQLSrvException::fromSqlSrvErrors(); |
||
389 | } |
||
390 | |||
391 | return $stmt; |
||
392 | } |
||
393 | } |
||
394 |