-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Statement.php
815 lines (719 loc) · 22.5 KB
/
Statement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
<?php
namespace Doctrine\DBAL;
use Doctrine\DBAL\Abstraction\Result;
use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Exception\NoKeyValue;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Result as BaseResult;
use Doctrine\DBAL\Types\Type;
use Doctrine\Deprecations\Deprecation;
use IteratorAggregate;
use PDO;
use PDOStatement;
use Throwable;
use Traversable;
use function array_shift;
use function is_array;
use function is_string;
/**
* A thin wrapper around a Doctrine\DBAL\Driver\Statement that adds support
* for logging, DBAL mapping types, etc.
*/
class Statement implements IteratorAggregate, DriverStatement, Result
{
/**
* The SQL statement.
*
* @var string
*/
protected $sql;
/**
* The bound parameters.
*
* @var mixed[]
*/
protected $params = [];
/**
* The parameter types.
*
* @var int[]|string[]
*/
protected $types = [];
/**
* The underlying driver statement.
*
* @var \Doctrine\DBAL\Driver\Statement
*/
protected $stmt;
/**
* The underlying database platform.
*
* @var AbstractPlatform
*/
protected $platform;
/**
* The connection this statement is bound to and executed on.
*
* @var Connection
*/
protected $conn;
/**
* Creates a new <tt>Statement</tt> for the given SQL and <tt>Connection</tt>.
*
* @internal The statement can be only instantiated by {@link Connection}.
*
* @param string $sql The SQL of the statement.
* @param Connection $conn The connection on which the statement should be executed.
*/
public function __construct($sql, Connection $conn)
{
$this->sql = $sql;
$this->stmt = $conn->getWrappedConnection()->prepare($sql);
$this->conn = $conn;
$this->platform = $conn->getDatabasePlatform();
}
/**
* Binds a parameter value to the statement.
*
* The value can optionally be bound with a PDO binding type or a DBAL mapping type.
* If bound with a DBAL mapping type, the binding type is derived from the mapping
* type and the value undergoes the conversion routines of the mapping type before
* being bound.
*
* @param string|int $param The name or position of the parameter.
* @param mixed $value The value of the parameter.
* @param mixed $type Either a PDO binding type or a DBAL mapping type name or instance.
*
* @return bool TRUE on success, FALSE on failure.
*/
public function bindValue($param, $value, $type = ParameterType::STRING)
{
$this->params[$param] = $value;
$this->types[$param] = $type;
if ($type !== null) {
if (is_string($type)) {
$type = Type::getType($type);
}
if ($type instanceof Type) {
$value = $type->convertToDatabaseValue($value, $this->platform);
$bindingType = $type->getBindingType();
} else {
$bindingType = $type;
}
return $this->stmt->bindValue($param, $value, $bindingType);
}
return $this->stmt->bindValue($param, $value);
}
/**
* Binds a parameter to a value by reference.
*
* Binding a parameter by reference does not support DBAL mapping types.
*
* @param string|int $param The name or position of the parameter.
* @param mixed $variable The reference to the variable to bind.
* @param int $type The PDO binding type.
* @param int|null $length Must be specified when using an OUT bind
* so that PHP allocates enough memory to hold the returned value.
*
* @return bool TRUE on success, FALSE on failure.
*/
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null)
{
$this->params[$param] = $variable;
$this->types[$param] = $type;
if ($this->stmt instanceof PDOStatement) {
$length = $length ?? 0;
}
return $this->stmt->bindParam($param, $variable, $type, $length);
}
/**
* Executes the statement with the currently bound parameters.
*
* @deprecated Statement::execute() is deprecated, use Statement::executeQuery() or executeStatement() instead
*
* @param mixed[]|null $params
*
* @return bool TRUE on success, FALSE on failure.
*
* @throws Exception
*/
public function execute($params = null)
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4580',
'Statement::execute() is deprecated, use Statement::executeQuery() or Statement::executeStatement() instead'
);
if (is_array($params)) {
$this->params = $params;
}
$logger = $this->conn->getConfiguration()->getSQLLogger();
if ($logger) {
$logger->startQuery($this->sql, $this->params, $this->types);
}
try {
$stmt = $this->stmt->execute($params);
} catch (Throwable $ex) {
if ($logger) {
$logger->stopQuery();
}
$this->conn->handleExceptionDuringQuery($ex, $this->sql, $this->params, $this->types);
}
if ($logger) {
$logger->stopQuery();
}
return $stmt;
}
/**
* Executes the statement with the currently bound parameters and return result.
*
* @param mixed[] $params
*
* @throws Exception
*/
public function executeQuery(array $params = []): BaseResult
{
if ($params === []) {
$params = null; // Workaround as long execute() exists and used internally.
}
$this->execute($params);
return new ForwardCompatibility\Result($this);
}
/**
* Executes the statement with the currently bound parameters and return affected rows.
*
* @param mixed[] $params
*
* @throws Exception
*/
public function executeStatement(array $params = []): int
{
if ($params === []) {
$params = null; // Workaround as long execute() exists and used internally.
}
$this->execute($params);
return $this->rowCount();
}
/**
* Closes the cursor, freeing the database resources used by this statement.
*
* @deprecated Use Result::free() instead.
*
* @return bool TRUE on success, FALSE on failure.
*/
public function closeCursor()
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4049',
'Statement::closeCursor() is deprecated, use Result::free() instead.'
);
return $this->stmt->closeCursor();
}
/**
* Returns the number of columns in the result set.
*
* @return int
*/
public function columnCount()
{
return $this->stmt->columnCount();
}
/**
* Fetches the SQLSTATE associated with the last operation on the statement.
*
* @deprecated The error information is available via exceptions.
*
* @return string|int|bool
*/
public function errorCode()
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/3507',
'Connection::errorCode() is deprecated, use getCode() or getSQLState() on Exception instead.'
);
return $this->stmt->errorCode();
}
/**
* {@inheritDoc}
*
* @deprecated The error information is available via exceptions.
*/
public function errorInfo()
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/3507',
'Connection::errorInfo() is deprecated, use getCode() or getSQLState() on Exception instead.'
);
return $this->stmt->errorInfo();
}
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4019',
'Statement::setFetchMode() is deprecated, use explicit Result::fetch*() APIs instead.'
);
if ($arg2 === null) {
return $this->stmt->setFetchMode($fetchMode);
}
if ($arg3 === null) {
return $this->stmt->setFetchMode($fetchMode, $arg2);
}
return $this->stmt->setFetchMode($fetchMode, $arg2, $arg3);
}
/**
* Required by interface IteratorAggregate.
*
* @deprecated Use iterateNumeric(), iterateAssociative() or iterateColumn() instead.
*
* {@inheritdoc}
*/
public function getIterator()
{
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4019',
'Statement::getIterator() is deprecated, use Result::iterateNumeric(), iterateAssociative() ' .
'or iterateColumn() instead.'
);
return $this->stmt;
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchNumeric(), fetchAssociative() or fetchOne() instead.
*/
public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4019',
'Statement::fetch() is deprecated, use Result::fetchNumeric(), fetchAssociative() or fetchOne() instead.'
);
return $this->stmt->fetch($fetchMode);
}
/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4019',
'Statement::fetchAll() is deprecated, use Result::fetchAllNumeric(), fetchAllAssociative() or ' .
'fetchFirstColumn() instead.'
);
if ($ctorArgs !== null) {
return $this->stmt->fetchAll($fetchMode, $fetchArgument, $ctorArgs);
}
if ($fetchArgument !== null) {
return $this->stmt->fetchAll($fetchMode, $fetchArgument);
}
return $this->stmt->fetchAll($fetchMode);
}
/**
* {@inheritDoc}
*
* @deprecated Use fetchOne() instead.
*/
public function fetchColumn($columnIndex = 0)
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/4019',
'Statement::fetchColumn() is deprecated, use Result::fetchOne() instead.'
);
return $this->stmt->fetchColumn($columnIndex);
}
/**
* {@inheritdoc}
*
* @deprecated Use Result::fetchNumeric() instead
*
* @throws Exception
*/
public function fetchNumeric()
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4554',
'Statement::%s() is deprecated, use Result::%s() instead.',
__FUNCTION__,
__FUNCTION__
);
try {
if ($this->stmt instanceof Result) {
return $this->stmt->fetchNumeric();
}
return $this->stmt->fetch(FetchMode::NUMERIC);
} catch (Exception $e) {
$this->conn->handleDriverException($e);
}
}
/**
* {@inheritdoc}
*
* @deprecated Use Result::fetchAssociative() instead
*
* @throws Exception
*/
public function fetchAssociative()
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4554',
'Statement::%s() is deprecated, use Result::%s() instead.',
__FUNCTION__,
__FUNCTION__
);
try {
if ($this->stmt instanceof Result) {
return $this->stmt->fetchAssociative();
}
return $this->stmt->fetch(FetchMode::ASSOCIATIVE);
} catch (Exception $e) {
$this->conn->handleDriverException($e);
}
}
/**
* {@inheritDoc}
*
* @deprecated Use Result::fetchOne() instead
*
* @throws Exception
*/
public function fetchOne()
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4554',
'Statement::%s() is deprecated, use Result::%s() instead.',
__FUNCTION__,
__FUNCTION__
);
try {
if ($this->stmt instanceof Result) {
return $this->stmt->fetchOne();
}
return $this->stmt->fetch(FetchMode::COLUMN);
} catch (Exception $e) {
$this->conn->handleDriverException($e);
}
}
/**
* {@inheritdoc}
*
* @deprecated Use Result::fetchAllNumeric() instead
*
* @throws Exception
*/
public function fetchAllNumeric(): array
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4554',
'Statement::%s() is deprecated, use Result::%s() instead.',
__FUNCTION__,
__FUNCTION__
);
try {
if ($this->stmt instanceof Result) {
return $this->stmt->fetchAllNumeric();
}
return $this->stmt->fetchAll(FetchMode::NUMERIC);
} catch (Exception $e) {
$this->conn->handleDriverException($e);
}
}
/**
* {@inheritdoc}
*
* @deprecated Use Result::fetchAllAssociative() instead
*
* @throws Exception
*/
public function fetchAllAssociative(): array
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4554',
'Statement::%s() is deprecated, use Result::%s() instead.',
__FUNCTION__,
__FUNCTION__
);
try {
if ($this->stmt instanceof Result) {
return $this->stmt->fetchAllAssociative();
}
return $this->stmt->fetchAll(FetchMode::ASSOCIATIVE);
} catch (Exception $e) {
$this->conn->handleDriverException($e);
}
}
/**
* Returns an associative array with the keys mapped to the first column and the values mapped to the second column.
*
* The result must contain at least two columns.
*
* @deprecated Use Result::fetchAllKeyValue() instead
*
* @return array<mixed,mixed>
*
* @throws Exception
*/
public function fetchAllKeyValue(): array
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4554',
'Statement::%s() is deprecated, use Result::%s() instead.',
__FUNCTION__,
__FUNCTION__
);
$this->ensureHasKeyValue();
$data = [];
foreach ($this->fetchAllNumeric() as [$key, $value]) {
$data[$key] = $value;
}
return $data;
}
/**
* Returns an associative array with the keys mapped to the first column and the values being
* an associative array representing the rest of the columns and their values.
*
* @deprecated Use Result::fetchAllAssociativeIndexed() instead
*
* @return array<mixed,array<string,mixed>>
*
* @throws Exception
*/
public function fetchAllAssociativeIndexed(): array
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4554',
'Statement::%s() is deprecated, use Result::%s() instead.',
__FUNCTION__,
__FUNCTION__
);
$data = [];
foreach ($this->fetchAll(FetchMode::ASSOCIATIVE) as $row) {
$data[array_shift($row)] = $row;
}
return $data;
}
/**
* {@inheritdoc}
*
* @deprecated Use Result::fetchFirstColumn() instead
*
* @throws Exception
*/
public function fetchFirstColumn(): array
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4554',
'Statement::%s() is deprecated, use Result::%s() instead.',
__FUNCTION__,
__FUNCTION__
);
try {
if ($this->stmt instanceof Result) {
return $this->stmt->fetchFirstColumn();
}
return $this->stmt->fetchAll(FetchMode::COLUMN);
} catch (Exception $e) {
$this->conn->handleDriverException($e);
}
}
/**
* {@inheritDoc}
*
* @deprecated Use Result::iterateNumeric() instead
*
* @return Traversable<int,array<int,mixed>>
*
* @throws Exception
*/
public function iterateNumeric(): Traversable
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4554',
'Statement::%s() is deprecated, use Result::%s() instead.',
__FUNCTION__,
__FUNCTION__
);
try {
if ($this->stmt instanceof Result) {
while (($row = $this->stmt->fetchNumeric()) !== false) {
yield $row;
}
} else {
while (($row = $this->stmt->fetch(FetchMode::NUMERIC)) !== false) {
yield $row;
}
}
} catch (Exception $e) {
$this->conn->handleDriverException($e);
}
}
/**
* {@inheritDoc}
*
* @deprecated Use Result::iterateAssociative() instead
*
* @return Traversable<int,array<string,mixed>>
*
* @throws Exception
*/
public function iterateAssociative(): Traversable
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4554',
'Statement::%s() is deprecated, use Result::%s() instead.',
__FUNCTION__,
__FUNCTION__
);
try {
if ($this->stmt instanceof Result) {
while (($row = $this->stmt->fetchAssociative()) !== false) {
yield $row;
}
} else {
while (($row = $this->stmt->fetch(FetchMode::ASSOCIATIVE)) !== false) {
yield $row;
}
}
} catch (Exception $e) {
$this->conn->handleDriverException($e);
}
}
/**
* Returns an iterator over the result set with the keys mapped to the first column
* and the values mapped to the second column.
*
* The result must contain at least two columns.
*
* @deprecated Use Result::iterateKeyValue() instead
*
* @return Traversable<mixed,mixed>
*
* @throws Exception
*/
public function iterateKeyValue(): Traversable
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4554',
'Statement::%s() is deprecated, use Result::%s() instead.',
__FUNCTION__,
__FUNCTION__
);
$this->ensureHasKeyValue();
foreach ($this->iterateNumeric() as [$key, $value]) {
yield $key => $value;
}
}
/**
* Returns an iterator over the result set with the keys mapped to the first column and the values being
* an associative array representing the rest of the columns and their values.
*
* @deprecated Use Result::iterateAssociativeIndexed() instead
*
* @return Traversable<mixed,array<string,mixed>>
*
* @throws Exception
*/
public function iterateAssociativeIndexed(): Traversable
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4554',
'Statement::%s() is deprecated, use Result::%s() instead.',
__FUNCTION__,
__FUNCTION__
);
while (($row = $this->stmt->fetch(FetchMode::ASSOCIATIVE)) !== false) {
yield array_shift($row) => $row;
}
}
/**
* {@inheritDoc}
*
* @deprecated Use Result::iterateColumn() instead
*
* @return Traversable<int,mixed>
*
* @throws Exception
*/
public function iterateColumn(): Traversable
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/4554',
'Statement::%s() is deprecated, use Result::%s() instead.',
__FUNCTION__,
__FUNCTION__
);
try {
if ($this->stmt instanceof Result) {
while (($value = $this->stmt->fetchOne()) !== false) {
yield $value;
}
} else {
while (($value = $this->stmt->fetch(FetchMode::COLUMN)) !== false) {
yield $value;
}
}
} catch (Exception $e) {
$this->conn->handleDriverException($e);
}
}
/**
* Returns the number of rows affected by the last execution of this statement.
*
* @return int The number of affected rows.
*/
public function rowCount()
{
return $this->stmt->rowCount();
}
public function free(): void
{
if ($this->stmt instanceof Result) {
$this->stmt->free();
return;
}
$this->stmt->closeCursor();
}
/**
* Gets the wrapped driver statement.
*
* @return \Doctrine\DBAL\Driver\Statement
*/
public function getWrappedStatement()
{
return $this->stmt;
}
private function ensureHasKeyValue(): void
{
$columnCount = $this->columnCount();
if ($columnCount < 2) {
throw NoKeyValue::fromColumnCount($columnCount);
}
}
}