1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Driver\Mysqli; |
21
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Driver\Statement; |
23
|
|
|
use Doctrine\DBAL\Driver\StatementIterator; |
24
|
|
|
use Doctrine\DBAL\Exception\InvalidArgumentException; |
25
|
|
|
use Doctrine\DBAL\FetchMode; |
26
|
|
|
use Doctrine\DBAL\ParameterType; |
27
|
|
|
use function array_combine; |
28
|
|
|
use function array_fill; |
29
|
|
|
use function count; |
30
|
|
|
use function feof; |
31
|
|
|
use function fread; |
32
|
|
|
use function get_resource_type; |
33
|
|
|
use function is_resource; |
34
|
|
|
use function str_repeat; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @author Kim Hemsø Rasmussen <[email protected]> |
38
|
|
|
*/ |
39
|
|
|
class MysqliStatement implements \IteratorAggregate, Statement |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected static $_paramTypeMap = [ |
45
|
|
|
ParameterType::STRING => 's', |
46
|
|
|
ParameterType::BINARY => 's', |
47
|
|
|
ParameterType::BOOLEAN => 'i', |
48
|
|
|
ParameterType::NULL => 's', |
49
|
|
|
ParameterType::INTEGER => 'i', |
50
|
|
|
ParameterType::LARGE_OBJECT => 'b', |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var \mysqli |
55
|
|
|
*/ |
56
|
|
|
protected $_conn; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var \mysqli_stmt |
60
|
|
|
*/ |
61
|
|
|
protected $_stmt; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var null|boolean|array |
65
|
|
|
*/ |
66
|
|
|
protected $_columnNames; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var null|array |
70
|
|
|
*/ |
71
|
|
|
protected $_rowBindedValues; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
protected $_bindedValues; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var string |
80
|
|
|
*/ |
81
|
|
|
protected $types; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Contains ref values for bindValue(). |
85
|
|
|
* |
86
|
|
|
* @var array |
87
|
|
|
*/ |
88
|
|
|
protected $_values = []; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var int |
92
|
|
|
*/ |
93
|
|
|
protected $_defaultFetchMode = FetchMode::MIXED; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Indicates whether the statement is in the state when fetching results is possible |
97
|
|
|
* |
98
|
|
|
* @var bool |
99
|
|
|
*/ |
100
|
|
|
private $result = false; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param \mysqli $conn |
104
|
|
|
* @param string $prepareString |
105
|
|
|
* |
106
|
|
|
* @throws \Doctrine\DBAL\Driver\Mysqli\MysqliException |
107
|
|
|
*/ |
108
|
774 |
|
public function __construct(\mysqli $conn, $prepareString) |
109
|
|
|
{ |
110
|
774 |
|
$this->_conn = $conn; |
111
|
774 |
|
$this->_stmt = $conn->prepare($prepareString); |
112
|
774 |
|
if (false === $this->_stmt) { |
|
|
|
|
113
|
12 |
|
throw new MysqliException($this->_conn->error, $this->_conn->sqlstate, $this->_conn->errno); |
114
|
|
|
} |
115
|
|
|
|
116
|
762 |
|
$paramCount = $this->_stmt->param_count; |
117
|
762 |
|
if (0 < $paramCount) { |
118
|
375 |
|
$this->types = str_repeat('s', $paramCount); |
119
|
375 |
|
$this->_bindedValues = array_fill(1, $paramCount, null); |
120
|
|
|
} |
121
|
762 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
24 |
|
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) |
127
|
|
|
{ |
128
|
24 |
|
if (null === $type) { |
129
|
|
|
$type = 's'; |
130
|
|
|
} else { |
131
|
24 |
|
if (isset(self::$_paramTypeMap[$type])) { |
132
|
24 |
|
$type = self::$_paramTypeMap[$type]; |
133
|
|
|
} else { |
134
|
|
|
throw new MysqliException("Unknown type: '{$type}'"); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
24 |
|
$this->_bindedValues[$column] =& $variable; |
139
|
24 |
|
$this->types[$column - 1] = $type; |
140
|
|
|
|
141
|
24 |
|
return true; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
108 |
|
public function bindValue($param, $value, $type = ParameterType::STRING) |
148
|
|
|
{ |
149
|
108 |
|
if (null === $type) { |
150
|
|
|
$type = 's'; |
151
|
|
|
} else { |
152
|
108 |
|
if (isset(self::$_paramTypeMap[$type])) { |
153
|
108 |
|
$type = self::$_paramTypeMap[$type]; |
154
|
|
|
} else { |
155
|
|
|
throw new MysqliException("Unknown type: '{$type}'"); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
108 |
|
$this->_values[$param] = $value; |
160
|
108 |
|
$this->_bindedValues[$param] =& $this->_values[$param]; |
161
|
108 |
|
$this->types[$param - 1] = $type; |
162
|
|
|
|
163
|
108 |
|
return true; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
738 |
|
public function execute($params = null) |
170
|
|
|
{ |
171
|
738 |
|
if (null !== $this->_bindedValues) { |
172
|
375 |
|
if (null !== $params) { |
173
|
264 |
|
if ( ! $this->_bindValues($params)) { |
174
|
264 |
|
throw new MysqliException($this->_stmt->error, $this->_stmt->errno); |
175
|
|
|
} |
176
|
|
|
} else { |
177
|
132 |
|
list($types, $values, $streams) = $this->separateBoundValues(); |
178
|
132 |
|
if (! $this->_stmt->bind_param($types, ...$values)) { |
|
|
|
|
179
|
|
|
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); |
180
|
|
|
} |
181
|
132 |
|
$this->sendLongData($streams); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
738 |
|
if ( ! $this->_stmt->execute()) { |
186
|
18 |
|
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); |
187
|
|
|
} |
188
|
|
|
|
189
|
735 |
|
if (null === $this->_columnNames) { |
190
|
735 |
|
$meta = $this->_stmt->result_metadata(); |
191
|
735 |
|
if (false !== $meta) { |
192
|
678 |
|
$columnNames = []; |
193
|
678 |
|
foreach ($meta->fetch_fields() as $col) { |
194
|
678 |
|
$columnNames[] = $col->name; |
195
|
|
|
} |
196
|
678 |
|
$meta->free(); |
197
|
|
|
|
198
|
678 |
|
$this->_columnNames = $columnNames; |
199
|
|
|
} else { |
200
|
295 |
|
$this->_columnNames = false; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
735 |
|
if (false !== $this->_columnNames) { |
205
|
|
|
// Store result of every execution which has it. Otherwise it will be impossible |
206
|
|
|
// to execute a new statement in case if the previous one has non-fetched rows |
207
|
|
|
// @link http://dev.mysql.com/doc/refman/5.7/en/commands-out-of-sync.html |
208
|
678 |
|
$this->_stmt->store_result(); |
209
|
|
|
|
210
|
|
|
// Bind row values _after_ storing the result. Otherwise, if mysqli is compiled with libmysql, |
211
|
|
|
// it will have to allocate as much memory as it may be needed for the given column type |
212
|
|
|
// (e.g. for a LONGBLOB field it's 4 gigabytes) |
213
|
|
|
// @link https://bugs.php.net/bug.php?id=51386#1270673122 |
214
|
|
|
// |
215
|
|
|
// Make sure that the values are bound after each execution. Otherwise, if closeCursor() has been |
216
|
|
|
// previously called on the statement, the values are unbound making the statement unusable. |
217
|
|
|
// |
218
|
|
|
// It's also important that row values are bound after _each_ call to store_result(). Otherwise, |
219
|
|
|
// if mysqli is compiled with libmysql, subsequently fetched string values will get truncated |
220
|
|
|
// to the length of the ones fetched during the previous execution. |
221
|
678 |
|
$this->_rowBindedValues = array_fill(0, count($this->_columnNames), null); |
|
|
|
|
222
|
|
|
|
223
|
678 |
|
$refs = []; |
224
|
678 |
|
foreach ($this->_rowBindedValues as $key => &$value) { |
225
|
678 |
|
$refs[$key] =& $value; |
226
|
|
|
} |
227
|
|
|
|
228
|
678 |
|
if (! $this->_stmt->bind_result(...$refs)) { |
229
|
|
|
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
735 |
|
$this->result = true; |
234
|
|
|
|
235
|
735 |
|
return true; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Split $this->_bindedValues into those values that need to be sent using mysqli::send_long_data() |
240
|
|
|
* and those that can be bound the usual way. |
241
|
|
|
* |
242
|
|
|
* @return array<int, array<int|string, mixed>|string> |
243
|
|
|
*/ |
244
|
132 |
|
private function separateBoundValues() |
245
|
|
|
{ |
246
|
132 |
|
$streams = $values = []; |
247
|
132 |
|
$types = $this->types; |
248
|
|
|
|
249
|
132 |
|
foreach ($this->_bindedValues as $parameter => $value) { |
250
|
132 |
|
if (! isset($types[$parameter - 1])) { |
251
|
|
|
$types[$parameter - 1] = static::$_paramTypeMap[ParameterType::STRING]; |
252
|
|
|
} |
253
|
|
|
|
254
|
132 |
|
if ($types[$parameter - 1] === static::$_paramTypeMap[ParameterType::LARGE_OBJECT]) { |
255
|
21 |
|
if (is_resource($value)) { |
256
|
9 |
|
if (get_resource_type($value) !== 'stream') { |
257
|
|
|
throw new InvalidArgumentException('Resources passed with the LARGE_OBJECT parameter type must be stream resources.'); |
258
|
|
|
} |
259
|
9 |
|
$streams[$parameter] = $value; |
260
|
9 |
|
$values[$parameter] = null; |
261
|
9 |
|
continue; |
262
|
|
|
} else { |
263
|
15 |
|
$types[$parameter - 1] = static::$_paramTypeMap[ParameterType::STRING]; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
129 |
|
$values[$parameter] = $value; |
268
|
|
|
} |
269
|
|
|
|
270
|
132 |
|
return [$types, $values, $streams]; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Handle $this->_longData after regular query parameters have been bound |
275
|
|
|
* |
276
|
|
|
* @throws MysqliException |
277
|
|
|
*/ |
278
|
132 |
|
private function sendLongData($streams) |
279
|
|
|
{ |
280
|
132 |
|
foreach ($streams as $paramNr => $stream) { |
281
|
9 |
|
while (! feof($stream)) { |
282
|
9 |
|
$chunk = fread($stream, 8192); |
283
|
|
|
|
284
|
9 |
|
if ($chunk === false) { |
285
|
|
|
throw new MysqliException("Failed reading the stream resource for parameter offset ${paramNr}."); |
286
|
|
|
} |
287
|
|
|
|
288
|
9 |
|
if (! $this->_stmt->send_long_data($paramNr - 1, $chunk)) { |
289
|
|
|
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Binds a array of values to bound parameters. |
297
|
|
|
* |
298
|
|
|
* @param array $values |
299
|
|
|
* |
300
|
|
|
* @return bool |
301
|
|
|
*/ |
302
|
|
|
private function _bindValues($values) |
303
|
|
|
{ |
304
|
264 |
|
$params = []; |
305
|
264 |
|
$types = str_repeat('s', count($values)); |
306
|
|
|
|
307
|
264 |
|
foreach ($values as &$v) { |
308
|
264 |
|
$params[] =& $v; |
309
|
|
|
} |
310
|
|
|
|
311
|
264 |
|
return $this->_stmt->bind_param($types, ...$params); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @return mixed[]|false |
316
|
|
|
*/ |
317
|
|
|
private function _fetch() |
318
|
|
|
{ |
319
|
654 |
|
$ret = $this->_stmt->fetch(); |
320
|
|
|
|
321
|
654 |
|
if (true === $ret) { |
322
|
621 |
|
$values = []; |
323
|
621 |
|
foreach ($this->_rowBindedValues as $v) { |
324
|
621 |
|
$values[] = $v; |
325
|
|
|
} |
326
|
|
|
|
327
|
621 |
|
return $values; |
328
|
|
|
} |
329
|
|
|
|
330
|
381 |
|
return $ret; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* {@inheritdoc} |
335
|
|
|
*/ |
336
|
|
|
public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) |
337
|
|
|
{ |
338
|
|
|
// do not try fetching from the statement if it's not expected to contain result |
339
|
|
|
// in order to prevent exceptional situation |
340
|
681 |
|
if (!$this->result) { |
341
|
27 |
|
return false; |
342
|
|
|
} |
343
|
|
|
|
344
|
654 |
|
$fetchMode = $fetchMode ?: $this->_defaultFetchMode; |
345
|
|
|
|
346
|
654 |
|
if ($fetchMode === FetchMode::COLUMN) { |
347
|
3 |
|
return $this->fetchColumn(); |
348
|
|
|
} |
349
|
|
|
|
350
|
654 |
|
$values = $this->_fetch(); |
351
|
654 |
|
if (null === $values) { |
|
|
|
|
352
|
381 |
|
return false; |
353
|
|
|
} |
354
|
|
|
|
355
|
621 |
|
if (false === $values) { |
356
|
|
|
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
switch ($fetchMode) { |
360
|
621 |
|
case FetchMode::NUMERIC: |
361
|
177 |
|
return $values; |
362
|
|
|
|
363
|
450 |
|
case FetchMode::ASSOCIATIVE: |
364
|
441 |
|
return array_combine($this->_columnNames, $values); |
|
|
|
|
365
|
|
|
|
366
|
9 |
|
case FetchMode::MIXED: |
367
|
6 |
|
$ret = array_combine($this->_columnNames, $values); |
368
|
6 |
|
$ret += $values; |
369
|
|
|
|
370
|
6 |
|
return $ret; |
371
|
|
|
|
372
|
3 |
|
case FetchMode::STANDARD_OBJECT: |
373
|
3 |
|
$assoc = array_combine($this->_columnNames, $values); |
374
|
3 |
|
$ret = new \stdClass(); |
375
|
|
|
|
376
|
3 |
|
foreach ($assoc as $column => $value) { |
377
|
3 |
|
$ret->$column = $value; |
378
|
|
|
} |
379
|
|
|
|
380
|
3 |
|
return $ret; |
381
|
|
|
|
382
|
|
|
default: |
383
|
|
|
throw new MysqliException("Unknown fetch type '{$fetchMode}'"); |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* {@inheritdoc} |
389
|
|
|
*/ |
390
|
|
|
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) |
391
|
|
|
{ |
392
|
336 |
|
$fetchMode = $fetchMode ?: $this->_defaultFetchMode; |
393
|
|
|
|
394
|
336 |
|
$rows = []; |
395
|
|
|
|
396
|
336 |
|
if ($fetchMode === FetchMode::COLUMN) { |
397
|
27 |
|
while (($row = $this->fetchColumn()) !== false) { |
398
|
27 |
|
$rows[] = $row; |
399
|
|
|
} |
400
|
|
|
} else { |
401
|
309 |
|
while (($row = $this->fetch($fetchMode)) !== false) { |
402
|
288 |
|
$rows[] = $row; |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
|
406
|
336 |
|
return $rows; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* {@inheritdoc} |
411
|
|
|
*/ |
412
|
|
|
public function fetchColumn($columnIndex = 0) |
413
|
|
|
{ |
414
|
177 |
|
$row = $this->fetch(FetchMode::NUMERIC); |
415
|
|
|
|
416
|
177 |
|
if (false === $row) { |
417
|
45 |
|
return false; |
418
|
|
|
} |
419
|
|
|
|
420
|
159 |
|
return $row[$columnIndex] ?? null; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* {@inheritdoc} |
425
|
|
|
*/ |
426
|
|
|
public function errorCode() |
427
|
|
|
{ |
428
|
|
|
return $this->_stmt->errno; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* {@inheritdoc} |
433
|
|
|
*/ |
434
|
|
|
public function errorInfo() |
435
|
|
|
{ |
436
|
|
|
return $this->_stmt->error; |
|
|
|
|
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* {@inheritdoc} |
441
|
|
|
*/ |
442
|
|
|
public function closeCursor() |
443
|
|
|
{ |
444
|
60 |
|
$this->_stmt->free_result(); |
445
|
60 |
|
$this->result = false; |
446
|
|
|
|
447
|
60 |
|
return true; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* {@inheritdoc} |
452
|
|
|
*/ |
453
|
|
|
public function rowCount() |
454
|
|
|
{ |
455
|
285 |
|
if (false === $this->_columnNames) { |
456
|
285 |
|
return $this->_stmt->affected_rows; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
return $this->_stmt->num_rows; |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* {@inheritdoc} |
464
|
|
|
*/ |
465
|
|
|
public function columnCount() |
466
|
|
|
{ |
467
|
12 |
|
return $this->_stmt->field_count; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* {@inheritdoc} |
472
|
|
|
*/ |
473
|
|
|
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) |
474
|
|
|
{ |
475
|
714 |
|
$this->_defaultFetchMode = $fetchMode; |
476
|
|
|
|
477
|
714 |
|
return true; |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* {@inheritdoc} |
482
|
|
|
*/ |
483
|
|
|
public function getIterator() |
484
|
|
|
{ |
485
|
41 |
|
return new StatementIterator($this); |
486
|
|
|
} |
487
|
|
|
} |
488
|
|
|
|