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($values, $streams) = $this->separateLongData(); |
178
|
132 |
|
if (! $this->_stmt->bind_param($this->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 |
243
|
|
|
*/ |
244
|
132 |
|
private function separateLongData() |
245
|
|
|
{ |
246
|
132 |
|
$streams = $otherData = []; |
247
|
|
|
|
248
|
132 |
|
foreach ($this->_bindedValues as $parameter => $value) { |
249
|
132 |
|
if ($this->types[$parameter - 1] !== static::$_paramTypeMap[ParameterType::LARGE_OBJECT]) { |
250
|
126 |
|
$otherData[$parameter] = $value; |
251
|
126 |
|
continue; |
252
|
|
|
} |
253
|
|
|
|
254
|
21 |
|
if (is_resource($value)) { |
255
|
9 |
|
if (get_resource_type($value) !== 'stream') { |
256
|
|
|
throw new InvalidArgumentException('Resources passed with the LARGE_OBJECT parameter type must be stream resources.'); |
257
|
|
|
} |
258
|
9 |
|
$streams[$parameter] = $value; |
259
|
9 |
|
$otherData[$parameter] = null; |
260
|
|
|
} else { |
261
|
15 |
|
$this->types[$parameter - 1] = static::$_paramTypeMap[ParameterType::STRING]; |
262
|
21 |
|
$otherData[$parameter] = $value; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
132 |
|
return [$otherData, $streams]; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Handle $this->_longData after regular query parameters have been bound |
271
|
|
|
* |
272
|
|
|
* @throws MysqliException |
273
|
|
|
*/ |
274
|
132 |
|
private function sendLongData($streams) |
275
|
|
|
{ |
276
|
132 |
|
foreach ($streams as $paramNr => $stream) { |
277
|
9 |
|
while (! feof($stream)) { |
278
|
9 |
|
$chunk = fread($stream, 8192); |
279
|
|
|
|
280
|
9 |
|
if ($chunk === false) { |
281
|
|
|
throw new MysqliException("Failed reading the stream resource for parameter offset ${paramNr}."); |
282
|
|
|
} |
283
|
|
|
|
284
|
9 |
|
if (! $this->_stmt->send_long_data($paramNr - 1, $chunk)) { |
285
|
|
|
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Binds a array of values to bound parameters. |
293
|
|
|
* |
294
|
|
|
* @param array $values |
295
|
|
|
* |
296
|
|
|
* @return bool |
297
|
|
|
*/ |
298
|
|
|
private function _bindValues($values) |
299
|
|
|
{ |
300
|
264 |
|
$params = []; |
301
|
264 |
|
$types = str_repeat('s', count($values)); |
302
|
|
|
|
303
|
264 |
|
foreach ($values as &$v) { |
304
|
264 |
|
$params[] =& $v; |
305
|
|
|
} |
306
|
|
|
|
307
|
264 |
|
return $this->_stmt->bind_param($types, ...$params); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @return mixed[]|false |
312
|
|
|
*/ |
313
|
|
|
private function _fetch() |
314
|
|
|
{ |
315
|
654 |
|
$ret = $this->_stmt->fetch(); |
316
|
|
|
|
317
|
654 |
|
if (true === $ret) { |
318
|
621 |
|
$values = []; |
319
|
621 |
|
foreach ($this->_rowBindedValues as $v) { |
320
|
621 |
|
$values[] = $v; |
321
|
|
|
} |
322
|
|
|
|
323
|
621 |
|
return $values; |
324
|
|
|
} |
325
|
|
|
|
326
|
381 |
|
return $ret; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* {@inheritdoc} |
331
|
|
|
*/ |
332
|
|
|
public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) |
333
|
|
|
{ |
334
|
|
|
// do not try fetching from the statement if it's not expected to contain result |
335
|
|
|
// in order to prevent exceptional situation |
336
|
681 |
|
if (!$this->result) { |
337
|
27 |
|
return false; |
338
|
|
|
} |
339
|
|
|
|
340
|
654 |
|
$fetchMode = $fetchMode ?: $this->_defaultFetchMode; |
341
|
|
|
|
342
|
654 |
|
if ($fetchMode === FetchMode::COLUMN) { |
343
|
3 |
|
return $this->fetchColumn(); |
344
|
|
|
} |
345
|
|
|
|
346
|
654 |
|
$values = $this->_fetch(); |
347
|
654 |
|
if (null === $values) { |
|
|
|
|
348
|
381 |
|
return false; |
349
|
|
|
} |
350
|
|
|
|
351
|
621 |
|
if (false === $values) { |
352
|
|
|
throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
switch ($fetchMode) { |
356
|
621 |
|
case FetchMode::NUMERIC: |
357
|
177 |
|
return $values; |
358
|
|
|
|
359
|
450 |
|
case FetchMode::ASSOCIATIVE: |
360
|
441 |
|
return array_combine($this->_columnNames, $values); |
|
|
|
|
361
|
|
|
|
362
|
9 |
|
case FetchMode::MIXED: |
363
|
6 |
|
$ret = array_combine($this->_columnNames, $values); |
364
|
6 |
|
$ret += $values; |
365
|
|
|
|
366
|
6 |
|
return $ret; |
367
|
|
|
|
368
|
3 |
|
case FetchMode::STANDARD_OBJECT: |
369
|
3 |
|
$assoc = array_combine($this->_columnNames, $values); |
370
|
3 |
|
$ret = new \stdClass(); |
371
|
|
|
|
372
|
3 |
|
foreach ($assoc as $column => $value) { |
373
|
3 |
|
$ret->$column = $value; |
374
|
|
|
} |
375
|
|
|
|
376
|
3 |
|
return $ret; |
377
|
|
|
|
378
|
|
|
default: |
379
|
|
|
throw new MysqliException("Unknown fetch type '{$fetchMode}'"); |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* {@inheritdoc} |
385
|
|
|
*/ |
386
|
|
|
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) |
387
|
|
|
{ |
388
|
336 |
|
$fetchMode = $fetchMode ?: $this->_defaultFetchMode; |
389
|
|
|
|
390
|
336 |
|
$rows = []; |
391
|
|
|
|
392
|
336 |
|
if ($fetchMode === FetchMode::COLUMN) { |
393
|
27 |
|
while (($row = $this->fetchColumn()) !== false) { |
394
|
27 |
|
$rows[] = $row; |
395
|
|
|
} |
396
|
|
|
} else { |
397
|
309 |
|
while (($row = $this->fetch($fetchMode)) !== false) { |
398
|
288 |
|
$rows[] = $row; |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
402
|
336 |
|
return $rows; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* {@inheritdoc} |
407
|
|
|
*/ |
408
|
|
|
public function fetchColumn($columnIndex = 0) |
409
|
|
|
{ |
410
|
177 |
|
$row = $this->fetch(FetchMode::NUMERIC); |
411
|
|
|
|
412
|
177 |
|
if (false === $row) { |
413
|
45 |
|
return false; |
414
|
|
|
} |
415
|
|
|
|
416
|
159 |
|
return $row[$columnIndex] ?? null; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* {@inheritdoc} |
421
|
|
|
*/ |
422
|
|
|
public function errorCode() |
423
|
|
|
{ |
424
|
|
|
return $this->_stmt->errno; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* {@inheritdoc} |
429
|
|
|
*/ |
430
|
|
|
public function errorInfo() |
431
|
|
|
{ |
432
|
|
|
return $this->_stmt->error; |
|
|
|
|
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* {@inheritdoc} |
437
|
|
|
*/ |
438
|
|
|
public function closeCursor() |
439
|
|
|
{ |
440
|
60 |
|
$this->_stmt->free_result(); |
441
|
60 |
|
$this->result = false; |
442
|
|
|
|
443
|
60 |
|
return true; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* {@inheritdoc} |
448
|
|
|
*/ |
449
|
|
|
public function rowCount() |
450
|
|
|
{ |
451
|
285 |
|
if (false === $this->_columnNames) { |
452
|
285 |
|
return $this->_stmt->affected_rows; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
return $this->_stmt->num_rows; |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* {@inheritdoc} |
460
|
|
|
*/ |
461
|
|
|
public function columnCount() |
462
|
|
|
{ |
463
|
12 |
|
return $this->_stmt->field_count; |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* {@inheritdoc} |
468
|
|
|
*/ |
469
|
|
|
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) |
470
|
|
|
{ |
471
|
714 |
|
$this->_defaultFetchMode = $fetchMode; |
472
|
|
|
|
473
|
714 |
|
return true; |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* {@inheritdoc} |
478
|
|
|
*/ |
479
|
|
|
public function getIterator() |
480
|
|
|
{ |
481
|
41 |
|
return new StatementIterator($this); |
482
|
|
|
} |
483
|
|
|
} |
484
|
|
|
|