Failed Conditions
Pull Request — 3.0.x (#3980)
by Guilherme
06:55
created

AbstractPlatform::getSequencePrefix()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 5
nop 2
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\DBAL\Platforms;
4
5
use Doctrine\Common\EventManager;
6
use Doctrine\DBAL\DBALException;
7
use Doctrine\DBAL\Event\SchemaAlterTableAddColumnEventArgs;
8
use Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs;
9
use Doctrine\DBAL\Event\SchemaAlterTableEventArgs;
10
use Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs;
11
use Doctrine\DBAL\Event\SchemaAlterTableRenameColumnEventArgs;
12
use Doctrine\DBAL\Event\SchemaCreateTableColumnEventArgs;
13
use Doctrine\DBAL\Event\SchemaCreateTableEventArgs;
14
use Doctrine\DBAL\Event\SchemaDropTableEventArgs;
15
use Doctrine\DBAL\Events;
16
use Doctrine\DBAL\Platforms\Keywords\KeywordList;
17
use Doctrine\DBAL\Schema\Column;
18
use Doctrine\DBAL\Schema\ColumnDiff;
19
use Doctrine\DBAL\Schema\Constraint;
20
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
21
use Doctrine\DBAL\Schema\Identifier;
22
use Doctrine\DBAL\Schema\Index;
23
use Doctrine\DBAL\Schema\Sequence;
24
use Doctrine\DBAL\Schema\Table;
25
use Doctrine\DBAL\Schema\TableDiff;
26
use Doctrine\DBAL\Schema\UniqueConstraint;
27
use Doctrine\DBAL\TransactionIsolationLevel;
28
use Doctrine\DBAL\Types;
29
use Doctrine\DBAL\Types\Type;
30
use InvalidArgumentException;
31
use UnexpectedValueException;
32
use const E_USER_DEPRECATED;
33
use function addcslashes;
34
use function array_map;
35
use function array_merge;
36
use function array_unique;
37
use function array_values;
38
use function assert;
39
use function count;
40
use function explode;
41
use function func_get_arg;
42
use function func_get_args;
43
use function func_num_args;
44
use function implode;
45
use function in_array;
46
use function is_array;
47
use function is_bool;
48
use function is_int;
49
use function is_string;
50
use function preg_quote;
51
use function preg_replace;
52
use function sprintf;
53
use function str_replace;
54
use function strlen;
55
use function strpos;
56
use function strtolower;
57
use function strtoupper;
58
use function trigger_error;
59
60
/**
61
 * Base class for all DatabasePlatforms. The DatabasePlatforms are the central
62
 * point of abstraction of platform-specific behaviors, features and SQL dialects.
63
 * They are a passive source of information.
64
 *
65
 * @todo Remove any unnecessary methods.
66
 */
67
abstract class AbstractPlatform
68
{
69
    public const CREATE_INDEXES = 1;
70
71
    public const CREATE_FOREIGNKEYS = 2;
72
73
    /**
74
     * @deprecated Use DateIntervalUnit::INTERVAL_UNIT_SECOND.
75
     */
76
    public const DATE_INTERVAL_UNIT_SECOND = DateIntervalUnit::SECOND;
77
78
    /**
79
     * @deprecated Use DateIntervalUnit::MINUTE.
80
     */
81
    public const DATE_INTERVAL_UNIT_MINUTE = DateIntervalUnit::MINUTE;
82
83
    /**
84
     * @deprecated Use DateIntervalUnit::HOUR.
85
     */
86
    public const DATE_INTERVAL_UNIT_HOUR = DateIntervalUnit::HOUR;
87
88
    /**
89
     * @deprecated Use DateIntervalUnit::DAY.
90
     */
91
    public const DATE_INTERVAL_UNIT_DAY = DateIntervalUnit::DAY;
92
93
    /**
94
     * @deprecated Use DateIntervalUnit::WEEK.
95
     */
96
    public const DATE_INTERVAL_UNIT_WEEK = DateIntervalUnit::WEEK;
97
98
    /**
99
     * @deprecated Use DateIntervalUnit::MONTH.
100
     */
101
    public const DATE_INTERVAL_UNIT_MONTH = DateIntervalUnit::MONTH;
102
103
    /**
104
     * @deprecated Use DateIntervalUnit::QUARTER.
105
     */
106
    public const DATE_INTERVAL_UNIT_QUARTER = DateIntervalUnit::QUARTER;
107
108
    /**
109
     * @deprecated Use DateIntervalUnit::QUARTER.
110
     */
111
    public const DATE_INTERVAL_UNIT_YEAR = DateIntervalUnit::YEAR;
112
113
    /**
114
     * @deprecated Use TrimMode::UNSPECIFIED.
115
     */
116
    public const TRIM_UNSPECIFIED = TrimMode::UNSPECIFIED;
117
118
    /**
119
     * @deprecated Use TrimMode::LEADING.
120
     */
121
    public const TRIM_LEADING = TrimMode::LEADING;
122
123
    /**
124
     * @deprecated Use TrimMode::TRAILING.
125
     */
126
    public const TRIM_TRAILING = TrimMode::TRAILING;
127
128
    /**
129
     * @deprecated Use TrimMode::BOTH.
130
     */
131
    public const TRIM_BOTH = TrimMode::BOTH;
132
133
    /** @var string[]|null */
134
    protected $doctrineTypeMapping = null;
135
136
    /**
137
     * Contains a list of all columns that should generate parseable column comments for type-detection
138
     * in reverse engineering scenarios.
139
     *
140
     * @var string[]|null
141
     */
142
    protected $doctrineTypeComments = null;
143
144
    /** @var EventManager|null */
145
    protected $_eventManager;
146
147
    /**
148
     * Holds the KeywordList instance for the current platform.
149
     *
150
     * @var KeywordList|null
151
     */
152
    protected $_keywords;
153
154 44963
    public function __construct()
155
    {
156 44963
    }
157
158
    /**
159
     * Sets the EventManager used by the Platform.
160
     *
161
     * @return void
162
     */
163 1332
    public function setEventManager(EventManager $eventManager)
164
    {
165 1332
        $this->_eventManager = $eventManager;
166 1332
    }
167
168
    /**
169
     * Gets the EventManager used by the Platform.
170
     *
171
     * @return EventManager|null
172
     */
173 1794
    public function getEventManager()
174
    {
175 1794
        return $this->_eventManager;
176
    }
177
178
    /**
179
     * Returns the SQL snippet that declares a boolean column.
180
     *
181
     * @param mixed[] $columnDef
182
     *
183
     * @return string
184
     */
185
    abstract public function getBooleanTypeDeclarationSQL(array $columnDef);
186
187
    /**
188
     * Returns the SQL snippet that declares a 4 byte integer column.
189
     *
190
     * @param mixed[] $columnDef
191
     *
192
     * @return string
193
     */
194
    abstract public function getIntegerTypeDeclarationSQL(array $columnDef);
195
196
    /**
197
     * Returns the SQL snippet that declares an 8 byte integer column.
198
     *
199
     * @param mixed[] $columnDef
200
     *
201
     * @return string
202
     */
203
    abstract public function getBigIntTypeDeclarationSQL(array $columnDef);
204
205
    /**
206
     * Returns the SQL snippet that declares a 2 byte integer column.
207
     *
208
     * @param mixed[] $columnDef
209
     *
210
     * @return string
211
     */
212
    abstract public function getSmallIntTypeDeclarationSQL(array $columnDef);
213
214
    /**
215
     * Returns the SQL snippet that declares common properties of an integer column.
216
     *
217
     * @param mixed[] $columnDef
218
     *
219
     * @return string
220
     */
221
    abstract protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef);
222
223
    /**
224
     * Lazy load Doctrine Type Mappings.
225
     *
226
     * @return void
227
     */
228
    abstract protected function initializeDoctrineTypeMappings();
229
230
    /**
231
     * Initializes Doctrine Type Mappings with the platform defaults
232
     * and with all additional type mappings.
233
     *
234
     * @return void
235
     */
236 1325
    private function initializeAllDoctrineTypeMappings()
237
    {
238 1325
        $this->initializeDoctrineTypeMappings();
239
240 1325
        foreach (Type::getTypesMap() as $typeName => $className) {
241 1325
            foreach (Type::getType($typeName)->getMappedDatabaseTypes($this) as $dbType) {
242 798
                $this->doctrineTypeMapping[$dbType] = $typeName;
243
            }
244
        }
245 1325
    }
246
247
    /**
248
     * Returns the SQL snippet used to declare a VARCHAR column type.
249
     *
250
     * @param mixed[] $field
251
     *
252
     * @return string
253
     */
254 5368
    public function getVarcharTypeDeclarationSQL(array $field)
255
    {
256 5368
        if (! isset($field['length'])) {
257 1026
            $field['length'] = $this->getVarcharDefaultLength();
258
        }
259
260 5368
        $fixed = $field['fixed'] ?? false;
261
262 5368
        $maxLength = $fixed
263 796
            ? $this->getCharMaxLength()
264 5368
            : $this->getVarcharMaxLength();
265
266 5368
        if ($field['length'] > $maxLength) {
267
            return $this->getClobTypeDeclarationSQL($field);
268
        }
269
270 5368
        return $this->getVarcharTypeDeclarationSQLSnippet($field['length'], $fixed);
271
    }
272
273
    /**
274
     * Returns the SQL snippet used to declare a BINARY/VARBINARY column type.
275
     *
276
     * @param mixed[] $field The column definition.
277
     *
278
     * @return string
279
     */
280 437
    public function getBinaryTypeDeclarationSQL(array $field)
281
    {
282 437
        if (! isset($field['length'])) {
283 253
            $field['length'] = $this->getBinaryDefaultLength();
284
        }
285
286 437
        $fixed = $field['fixed'] ?? false;
287
288 437
        $maxLength = $this->getBinaryMaxLength();
289
290 437
        if ($field['length'] > $maxLength) {
291 237
            if ($maxLength > 0) {
292 161
                @trigger_error(sprintf(
293 14
                    'Binary field length %d is greater than supported by the platform (%d). Reduce the field length or use a BLOB field instead.',
294 161
                    $field['length'],
295 161
                    $maxLength
296 161
                ), E_USER_DEPRECATED);
297
            }
298
299 237
            return $this->getBlobTypeDeclarationSQL($field);
300
        }
301
302 269
        return $this->getBinaryTypeDeclarationSQLSnippet($field['length'], $fixed);
303
    }
304
305
    /**
306
     * Returns the SQL snippet to declare a GUID/UUID field.
307
     *
308
     * By default this maps directly to a CHAR(36) and only maps to more
309
     * special datatypes when the underlying databases support this datatype.
310
     *
311
     * @param mixed[] $field
312
     *
313
     * @return string
314
     */
315 152
    public function getGuidTypeDeclarationSQL(array $field)
316
    {
317 152
        $field['length'] = 36;
318 152
        $field['fixed']  = true;
319
320 152
        return $this->getVarcharTypeDeclarationSQL($field);
321
    }
322
323
    /**
324
     * Returns the SQL snippet to declare a JSON field.
325
     *
326
     * By default this maps directly to a CLOB and only maps to more
327
     * special datatypes when the underlying databases support this datatype.
328
     *
329
     * @param mixed[] $field
330
     *
331
     * @return string
332
     */
333 278
    public function getJsonTypeDeclarationSQL(array $field)
334
    {
335 278
        return $this->getClobTypeDeclarationSQL($field);
336
    }
337
338
    /**
339
     * @param int|false $length
340
     * @param bool      $fixed
341
     *
342
     * @return string
343
     *
344
     * @throws DBALException If not supported on this platform.
345
     */
346
    protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
0 ignored issues
show
Unused Code introduced by
The parameter $fixed is not used and could be removed. ( Ignorable by Annotation )

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

346
    protected function getVarcharTypeDeclarationSQLSnippet($length, /** @scrutinizer ignore-unused */ $fixed)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
347
    {
348
        throw DBALException::notSupported('VARCHARs not supported by Platform.');
349
    }
350
351
    /**
352
     * Returns the SQL snippet used to declare a BINARY/VARBINARY column type.
353
     *
354
     * @param int|false $length The length of the column.
355
     * @param bool      $fixed  Whether the column length is fixed.
356
     *
357
     * @return string
358
     *
359
     * @throws DBALException If not supported on this platform.
360
     */
361
    protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed)
0 ignored issues
show
Unused Code introduced by
The parameter $fixed is not used and could be removed. ( Ignorable by Annotation )

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

361
    protected function getBinaryTypeDeclarationSQLSnippet($length, /** @scrutinizer ignore-unused */ $fixed)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
362
    {
363
        throw DBALException::notSupported('BINARY/VARBINARY column types are not supported by this platform.');
364
    }
365
366
    /**
367
     * Returns the SQL snippet used to declare a CLOB column type.
368
     *
369
     * @param mixed[] $field
370
     *
371
     * @return string
372
     */
373
    abstract public function getClobTypeDeclarationSQL(array $field);
374
375
    /**
376
     * Returns the SQL Snippet used to declare a BLOB column type.
377
     *
378
     * @param mixed[] $field
379
     *
380
     * @return string
381
     */
382
    abstract public function getBlobTypeDeclarationSQL(array $field);
383
384
    /**
385
     * Gets the name of the platform.
386
     *
387
     * @return string
388
     */
389
    abstract public function getName();
390
391
    /**
392
     * Registers a doctrine type to be used in conjunction with a column type of this platform.
393
     *
394
     * @param string $dbType
395
     * @param string $doctrineType
396
     *
397
     * @return void
398
     *
399
     * @throws DBALException If the type is not found.
400
     */
401 718
    public function registerDoctrineTypeMapping($dbType, $doctrineType)
402
    {
403 718
        if ($this->doctrineTypeMapping === null) {
404 713
            $this->initializeAllDoctrineTypeMappings();
405
        }
406
407 718
        if (! Types\Type::hasType($doctrineType)) {
408 230
            throw DBALException::typeNotFound($doctrineType);
409
        }
410
411 488
        $dbType                             = strtolower($dbType);
412 488
        $this->doctrineTypeMapping[$dbType] = $doctrineType;
413
414 488
        $doctrineType = Type::getType($doctrineType);
415
416 488
        if (! $doctrineType->requiresSQLCommentHint($this)) {
417 258
            return;
418
        }
419
420 230
        $this->markDoctrineTypeCommented($doctrineType);
421 230
    }
422
423
    /**
424
     * Gets the Doctrine type that is mapped for the given database column type.
425
     *
426
     * @param string $dbType
427
     *
428
     * @return string
429
     *
430
     * @throws DBALException
431
     */
432 2449
    public function getDoctrineTypeMapping($dbType)
433
    {
434 2449
        if ($this->doctrineTypeMapping === null) {
435 313
            $this->initializeAllDoctrineTypeMappings();
436
        }
437
438 2449
        $dbType = strtolower($dbType);
439
440 2449
        if (! isset($this->doctrineTypeMapping[$dbType])) {
441 230
            throw new DBALException('Unknown database type ' . $dbType . ' requested, ' . static::class . ' may not support it.');
442
        }
443
444 2219
        return $this->doctrineTypeMapping[$dbType];
445
    }
446
447
    /**
448
     * Checks if a database type is currently supported by this platform.
449
     *
450
     * @param string $dbType
451
     *
452
     * @return bool
453
     */
454 342
    public function hasDoctrineTypeMappingFor($dbType)
455
    {
456 342
        if ($this->doctrineTypeMapping === null) {
457 299
            $this->initializeAllDoctrineTypeMappings();
458
        }
459
460 342
        $dbType = strtolower($dbType);
461
462 342
        return isset($this->doctrineTypeMapping[$dbType]);
463
    }
464
465
    /**
466
     * Initializes the Doctrine Type comments instance variable for in_array() checks.
467
     *
468
     * @return void
469
     */
470 11662
    protected function initializeCommentedDoctrineTypes()
471
    {
472 11662
        $this->doctrineTypeComments = [];
473
474 11662
        foreach (Type::getTypesMap() as $typeName => $className) {
475 11662
            $type = Type::getType($typeName);
476
477 11662
            if (! $type->requiresSQLCommentHint($this)) {
478 11662
                continue;
479
            }
480
481 11662
            $this->doctrineTypeComments[] = $typeName;
482
        }
483 11662
    }
484
485
    /**
486
     * Is it necessary for the platform to add a parsable type comment to allow reverse engineering the given type?
487
     *
488
     * @return bool
489
     */
490 15248
    public function isCommentedDoctrineType(Type $doctrineType)
491
    {
492 15248
        if ($this->doctrineTypeComments === null) {
493 11432
            $this->initializeCommentedDoctrineTypes();
494
        }
495
496 15248
        assert(is_array($this->doctrineTypeComments));
497
498 15248
        return in_array($doctrineType->getName(), $this->doctrineTypeComments, true);
499
    }
500
501
    /**
502
     * Marks this type as to be commented in ALTER TABLE and CREATE TABLE statements.
503
     *
504
     * @param string|Type $doctrineType
505
     *
506
     * @return void
507
     */
508 230
    public function markDoctrineTypeCommented($doctrineType)
509
    {
510 230
        if ($this->doctrineTypeComments === null) {
511 230
            $this->initializeCommentedDoctrineTypes();
512
        }
513
514 230
        assert(is_array($this->doctrineTypeComments));
515
516 230
        $this->doctrineTypeComments[] = $doctrineType instanceof Type ? $doctrineType->getName() : $doctrineType;
517 230
    }
518
519
    /**
520
     * Gets the comment to append to a column comment that helps parsing this type in reverse engineering.
521
     *
522
     * @return string
523
     */
524 752
    public function getDoctrineTypeComment(Type $doctrineType)
525
    {
526 752
        return '(DC2Type:' . $doctrineType->getName() . ')';
527
    }
528
529
    /**
530
     * Gets the comment of a passed column modified by potential doctrine type comment hints.
531
     *
532
     * @return string|null
533
     */
534 9291
    protected function getColumnComment(Column $column)
535
    {
536 9291
        $comment = $column->getComment();
537
538 9291
        if ($this->isCommentedDoctrineType($column->getType())) {
539 752
            $comment .= $this->getDoctrineTypeComment($column->getType());
540
        }
541
542 9291
        return $comment;
543
    }
544
545
    /**
546
     * Gets the character used for identifier quoting.
547
     *
548
     * @return string
549
     */
550 4198
    public function getIdentifierQuoteCharacter()
551
    {
552 4198
        return '"';
553
    }
554
555
    /**
556
     * Gets the string portion that starts an SQL comment.
557
     *
558
     * @return string
559
     */
560
    public function getSqlCommentStartString()
561
    {
562
        return '--';
563
    }
564
565
    /**
566
     * Gets the string portion that ends an SQL comment.
567
     *
568
     * @return string
569
     */
570
    public function getSqlCommentEndString()
571
    {
572
        return "\n";
573
    }
574
575
    /**
576
     * Gets the maximum length of a char field.
577
     */
578 704
    public function getCharMaxLength() : int
579
    {
580 704
        return $this->getVarcharMaxLength();
581
    }
582
583
    /**
584
     * Gets the maximum length of a varchar field.
585
     *
586
     * @return int
587
     */
588 1810
    public function getVarcharMaxLength()
589
    {
590 1810
        return 4000;
591
    }
592
593
    /**
594
     * Gets the default length of a varchar field.
595
     *
596
     * @return int
597
     */
598 957
    public function getVarcharDefaultLength()
599
    {
600 957
        return 255;
601
    }
602
603
    /**
604
     * Gets the maximum length of a binary field.
605
     *
606
     * @return int
607
     */
608
    public function getBinaryMaxLength()
609
    {
610
        return 4000;
611
    }
612
613
    /**
614
     * Gets the default length of a binary field.
615
     *
616
     * @return int
617
     */
618 246
    public function getBinaryDefaultLength()
619
    {
620 246
        return 255;
621
    }
622
623
    /**
624
     * Gets all SQL wildcard characters of the platform.
625
     *
626
     * @return string[]
627
     */
628
    public function getWildcards()
629
    {
630
        return ['%', '_'];
631
    }
632
633
    /**
634
     * Returns the regular expression operator.
635
     *
636
     * @return string
637
     *
638
     * @throws DBALException If not supported on this platform.
639
     */
640 46
    public function getRegexpExpression()
641
    {
642 46
        throw DBALException::notSupported(__METHOD__);
643
    }
644
645
    /**
646
     * Returns the global unique identifier expression.
647
     *
648
     * @deprecated Use application-generated UUIDs instead
649
     *
650
     * @return string
651
     *
652
     * @throws DBALException If not supported on this platform.
653
     */
654
    public function getGuidExpression()
655
    {
656
        throw DBALException::notSupported(__METHOD__);
657
    }
658
659
    /**
660
     * Returns the SQL snippet to get the average value of a column.
661
     *
662
     * @param string $column The column to use.
663
     *
664
     * @return string Generated SQL including an AVG aggregate function.
665
     */
666
    public function getAvgExpression($column)
667
    {
668
        return 'AVG(' . $column . ')';
669
    }
670
671
    /**
672
     * Returns the SQL snippet to get the number of rows (without a NULL value) of a column.
673
     *
674
     * If a '*' is used instead of a column the number of selected rows is returned.
675
     *
676
     * @param string|int $column The column to use.
677
     *
678
     * @return string Generated SQL including a COUNT aggregate function.
679
     */
680
    public function getCountExpression($column)
681
    {
682
        return 'COUNT(' . $column . ')';
683
    }
684
685
    /**
686
     * Returns the SQL snippet to get the highest value of a column.
687
     *
688
     * @param string $column The column to use.
689
     *
690
     * @return string Generated SQL including a MAX aggregate function.
691
     */
692
    public function getMaxExpression($column)
693
    {
694
        return 'MAX(' . $column . ')';
695
    }
696
697
    /**
698
     * Returns the SQL snippet to get the lowest value of a column.
699
     *
700
     * @param string $column The column to use.
701
     *
702
     * @return string Generated SQL including a MIN aggregate function.
703
     */
704
    public function getMinExpression($column)
705
    {
706
        return 'MIN(' . $column . ')';
707
    }
708
709
    /**
710
     * Returns the SQL snippet to get the total sum of a column.
711
     *
712
     * @param string $column The column to use.
713
     *
714
     * @return string Generated SQL including a SUM aggregate function.
715
     */
716
    public function getSumExpression($column)
717
    {
718
        return 'SUM(' . $column . ')';
719
    }
720
721
    // scalar functions
722
723
    /**
724
     * Returns the SQL snippet to get the md5 sum of a field.
725
     *
726
     * Note: Not SQL92, but common functionality.
727
     *
728
     * @param string $column
729
     *
730
     * @return string
731
     */
732
    public function getMd5Expression($column)
733
    {
734
        return 'MD5(' . $column . ')';
735
    }
736
737
    /**
738
     * Returns the SQL snippet to get the length of a text field.
739
     *
740
     * @param string $column
741
     *
742
     * @return string
743
     */
744
    public function getLengthExpression($column)
745
    {
746
        return 'LENGTH(' . $column . ')';
747
    }
748
749
    /**
750
     * Returns the SQL snippet to get the squared value of a column.
751
     *
752
     * @param string $column The column to use.
753
     *
754
     * @return string Generated SQL including an SQRT aggregate function.
755
     */
756
    public function getSqrtExpression($column)
757
    {
758
        return 'SQRT(' . $column . ')';
759
    }
760
761
    /**
762
     * Returns the SQL snippet to round a numeric field to the number of decimals specified.
763
     *
764
     * @param string $column
765
     * @param int    $decimals
766
     *
767
     * @return string
768
     */
769
    public function getRoundExpression($column, $decimals = 0)
770
    {
771
        return 'ROUND(' . $column . ', ' . $decimals . ')';
772
    }
773
774
    /**
775
     * Returns the SQL snippet to get the remainder of the division operation $expression1 / $expression2.
776
     *
777
     * @param string $expression1
778
     * @param string $expression2
779
     *
780
     * @return string
781
     */
782
    public function getModExpression($expression1, $expression2)
783
    {
784
        return 'MOD(' . $expression1 . ', ' . $expression2 . ')';
785
    }
786
787
    /**
788
     * Returns the SQL snippet to trim a string.
789
     *
790
     * @param string      $str  The expression to apply the trim to.
791
     * @param int         $mode The position of the trim (leading/trailing/both).
792
     * @param string|bool $char The char to trim, has to be quoted already. Defaults to space.
793
     *
794
     * @return string
795
     */
796 756
    public function getTrimExpression($str, $mode = TrimMode::UNSPECIFIED, $char = false)
797
    {
798 756
        $expression = '';
799
800 756
        switch ($mode) {
801
            case TrimMode::LEADING:
802 189
                $expression = 'LEADING ';
803 189
                break;
804
805
            case TrimMode::TRAILING:
806 189
                $expression = 'TRAILING ';
807 189
                break;
808
809
            case TrimMode::BOTH:
810 189
                $expression = 'BOTH ';
811 189
                break;
812
        }
813
814 756
        if ($char !== false) {
815 588
            $expression .= $char . ' ';
0 ignored issues
show
Bug introduced by
Are you sure $char of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

815
            $expression .= /** @scrutinizer ignore-type */ $char . ' ';
Loading history...
816
        }
817
818 756
        if ($mode !== TrimMode::UNSPECIFIED || $char !== false) {
819 714
            $expression .= 'FROM ';
820
        }
821
822 756
        return 'TRIM(' . $expression . $str . ')';
823
    }
824
825
    /**
826
     * Returns the SQL snippet to trim trailing space characters from the expression.
827
     *
828
     * @param string $str Literal string or column name.
829
     *
830
     * @return string
831
     */
832 23
    public function getRtrimExpression($str)
833
    {
834 23
        return 'RTRIM(' . $str . ')';
835
    }
836
837
    /**
838
     * Returns the SQL snippet to trim leading space characters from the expression.
839
     *
840
     * @param string $str Literal string or column name.
841
     *
842
     * @return string
843
     */
844 23
    public function getLtrimExpression($str)
845
    {
846 23
        return 'LTRIM(' . $str . ')';
847
    }
848
849
    /**
850
     * Returns the SQL snippet to change all characters from the expression to uppercase,
851
     * according to the current character set mapping.
852
     *
853
     * @param string $str Literal string or column name.
854
     *
855
     * @return string
856
     */
857
    public function getUpperExpression($str)
858
    {
859
        return 'UPPER(' . $str . ')';
860
    }
861
862
    /**
863
     * Returns the SQL snippet to change all characters from the expression to lowercase,
864
     * according to the current character set mapping.
865
     *
866
     * @param string $str Literal string or column name.
867
     *
868
     * @return string
869
     */
870
    public function getLowerExpression($str)
871
    {
872
        return 'LOWER(' . $str . ')';
873
    }
874
875
    /**
876
     * Returns the SQL snippet to get the position of the first occurrence of substring $substr in string $str.
877
     *
878
     * @param string    $str      Literal string.
879
     * @param string    $substr   Literal string to find.
880
     * @param int|false $startPos Position to start at, beginning of string by default.
881
     *
882
     * @return string
883
     *
884
     * @throws DBALException If not supported on this platform.
885
     */
886
    public function getLocateExpression($str, $substr, $startPos = false)
0 ignored issues
show
Unused Code introduced by
The parameter $substr is not used and could be removed. ( Ignorable by Annotation )

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

886
    public function getLocateExpression($str, /** @scrutinizer ignore-unused */ $substr, $startPos = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $startPos is not used and could be removed. ( Ignorable by Annotation )

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

886
    public function getLocateExpression($str, $substr, /** @scrutinizer ignore-unused */ $startPos = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
887
    {
888
        throw DBALException::notSupported(__METHOD__);
889
    }
890
891
    /**
892
     * Returns the SQL snippet to get the current system date.
893
     *
894
     * @return string
895
     */
896
    public function getNowExpression()
897
    {
898
        return 'NOW()';
899
    }
900
901
    /**
902
     * Returns a SQL snippet to get a substring inside an SQL statement.
903
     *
904
     * Note: Not SQL92, but common functionality.
905
     *
906
     * SQLite only supports the 2 parameter variant of this function.
907
     *
908
     * @param string   $value  An sql string literal or column name/alias.
909
     * @param int      $from   Where to start the substring portion.
910
     * @param int|null $length The substring portion length.
911
     *
912
     * @return string
913
     */
914
    public function getSubstringExpression($value, $from, $length = null)
915
    {
916
        if ($length === null) {
917
            return 'SUBSTRING(' . $value . ' FROM ' . $from . ')';
918
        }
919
920
        return 'SUBSTRING(' . $value . ' FROM ' . $from . ' FOR ' . $length . ')';
921
    }
922
923
    /**
924
     * Returns a SQL snippet to concatenate the given expressions.
925
     *
926
     * Accepts an arbitrary number of string parameters. Each parameter must contain an expression.
927
     *
928
     * @return string
929
     */
930 69
    public function getConcatExpression()
931
    {
932 69
        return implode(' || ', func_get_args());
933
    }
934
935
    /**
936
     * Returns the SQL for a logical not.
937
     *
938
     * Example:
939
     * <code>
940
     * $q = new Doctrine_Query();
941
     * $e = $q->expr;
942
     * $q->select('*')->from('table')
943
     *   ->where($e->eq('id', $e->not('null'));
944
     * </code>
945
     *
946
     * @param string $expression
947
     *
948
     * @return string The logical expression.
949
     */
950
    public function getNotExpression($expression)
951
    {
952
        return 'NOT(' . $expression . ')';
953
    }
954
955
    /**
956
     * Returns the SQL that checks if an expression is null.
957
     *
958
     * @param string $expression The expression that should be compared to null.
959
     *
960
     * @return string The logical expression.
961
     */
962 92
    public function getIsNullExpression($expression)
963
    {
964 92
        return $expression . ' IS NULL';
965
    }
966
967
    /**
968
     * Returns the SQL that checks if an expression is not null.
969
     *
970
     * @param string $expression The expression that should be compared to null.
971
     *
972
     * @return string The logical expression.
973
     */
974
    public function getIsNotNullExpression($expression)
975
    {
976
        return $expression . ' IS NOT NULL';
977
    }
978
979
    /**
980
     * Returns the SQL that checks if an expression evaluates to a value between two values.
981
     *
982
     * The parameter $expression is checked if it is between $value1 and $value2.
983
     *
984
     * Note: There is a slight difference in the way BETWEEN works on some databases.
985
     * http://www.w3schools.com/sql/sql_between.asp. If you want complete database
986
     * independence you should avoid using between().
987
     *
988
     * @param string $expression The value to compare to.
989
     * @param string $value1     The lower value to compare with.
990
     * @param string $value2     The higher value to compare with.
991
     *
992
     * @return string The logical expression.
993
     */
994
    public function getBetweenExpression($expression, $value1, $value2)
995
    {
996
        return $expression . ' BETWEEN ' . $value1 . ' AND ' . $value2;
997
    }
998
999
    /**
1000
     * Returns the SQL to get the arccosine of a value.
1001
     *
1002
     * @param string $value
1003
     *
1004
     * @return string
1005
     */
1006
    public function getAcosExpression($value)
1007
    {
1008
        return 'ACOS(' . $value . ')';
1009
    }
1010
1011
    /**
1012
     * Returns the SQL to get the sine of a value.
1013
     *
1014
     * @param string $value
1015
     *
1016
     * @return string
1017
     */
1018
    public function getSinExpression($value)
1019
    {
1020
        return 'SIN(' . $value . ')';
1021
    }
1022
1023
    /**
1024
     * Returns the SQL to get the PI value.
1025
     *
1026
     * @return string
1027
     */
1028
    public function getPiExpression()
1029
    {
1030
        return 'PI()';
1031
    }
1032
1033
    /**
1034
     * Returns the SQL to get the cosine of a value.
1035
     *
1036
     * @param string $value
1037
     *
1038
     * @return string
1039
     */
1040
    public function getCosExpression($value)
1041
    {
1042
        return 'COS(' . $value . ')';
1043
    }
1044
1045
    /**
1046
     * Returns the SQL to calculate the difference in days between the two passed dates.
1047
     *
1048
     * Computes diff = date1 - date2.
1049
     *
1050
     * @param string $date1
1051
     * @param string $date2
1052
     *
1053
     * @return string
1054
     *
1055
     * @throws DBALException If not supported on this platform.
1056
     */
1057
    public function getDateDiffExpression($date1, $date2)
0 ignored issues
show
Unused Code introduced by
The parameter $date1 is not used and could be removed. ( Ignorable by Annotation )

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

1057
    public function getDateDiffExpression(/** @scrutinizer ignore-unused */ $date1, $date2)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $date2 is not used and could be removed. ( Ignorable by Annotation )

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

1057
    public function getDateDiffExpression($date1, /** @scrutinizer ignore-unused */ $date2)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1058
    {
1059
        throw DBALException::notSupported(__METHOD__);
1060
    }
1061
1062
    /**
1063
     * Returns the SQL to add the number of given seconds to a date.
1064
     *
1065
     * @param string $date
1066
     * @param int    $seconds
1067
     *
1068
     * @return string
1069
     *
1070
     * @throws DBALException If not supported on this platform.
1071
     */
1072 69
    public function getDateAddSecondsExpression($date, $seconds)
1073
    {
1074 69
        return $this->getDateArithmeticIntervalExpression($date, '+', $seconds, DateIntervalUnit::SECOND);
1075
    }
1076
1077
    /**
1078
     * Returns the SQL to subtract the number of given seconds from a date.
1079
     *
1080
     * @param string $date
1081
     * @param int    $seconds
1082
     *
1083
     * @return string
1084
     *
1085
     * @throws DBALException If not supported on this platform.
1086
     */
1087 69
    public function getDateSubSecondsExpression($date, $seconds)
1088
    {
1089 69
        return $this->getDateArithmeticIntervalExpression($date, '-', $seconds, DateIntervalUnit::SECOND);
1090
    }
1091
1092
    /**
1093
     * Returns the SQL to add the number of given minutes to a date.
1094
     *
1095
     * @param string $date
1096
     * @param int    $minutes
1097
     *
1098
     * @return string
1099
     *
1100
     * @throws DBALException If not supported on this platform.
1101
     */
1102 69
    public function getDateAddMinutesExpression($date, $minutes)
1103
    {
1104 69
        return $this->getDateArithmeticIntervalExpression($date, '+', $minutes, DateIntervalUnit::MINUTE);
1105
    }
1106
1107
    /**
1108
     * Returns the SQL to subtract the number of given minutes from a date.
1109
     *
1110
     * @param string $date
1111
     * @param int    $minutes
1112
     *
1113
     * @return string
1114
     *
1115
     * @throws DBALException If not supported on this platform.
1116
     */
1117 69
    public function getDateSubMinutesExpression($date, $minutes)
1118
    {
1119 69
        return $this->getDateArithmeticIntervalExpression($date, '-', $minutes, DateIntervalUnit::MINUTE);
1120
    }
1121
1122
    /**
1123
     * Returns the SQL to add the number of given hours to a date.
1124
     *
1125
     * @param string $date
1126
     * @param int    $hours
1127
     *
1128
     * @return string
1129
     *
1130
     * @throws DBALException If not supported on this platform.
1131
     */
1132 69
    public function getDateAddHourExpression($date, $hours)
1133
    {
1134 69
        return $this->getDateArithmeticIntervalExpression($date, '+', $hours, DateIntervalUnit::HOUR);
1135
    }
1136
1137
    /**
1138
     * Returns the SQL to subtract the number of given hours to a date.
1139
     *
1140
     * @param string $date
1141
     * @param int    $hours
1142
     *
1143
     * @return string
1144
     *
1145
     * @throws DBALException If not supported on this platform.
1146
     */
1147 69
    public function getDateSubHourExpression($date, $hours)
1148
    {
1149 69
        return $this->getDateArithmeticIntervalExpression($date, '-', $hours, DateIntervalUnit::HOUR);
1150
    }
1151
1152
    /**
1153
     * Returns the SQL to add the number of given days to a date.
1154
     *
1155
     * @param string $date
1156
     * @param int    $days
1157
     *
1158
     * @return string
1159
     *
1160
     * @throws DBALException If not supported on this platform.
1161
     */
1162 115
    public function getDateAddDaysExpression($date, $days)
1163
    {
1164 115
        return $this->getDateArithmeticIntervalExpression($date, '+', $days, DateIntervalUnit::DAY);
1165
    }
1166
1167
    /**
1168
     * Returns the SQL to subtract the number of given days to a date.
1169
     *
1170
     * @param string $date
1171
     * @param int    $days
1172
     *
1173
     * @return string
1174
     *
1175
     * @throws DBALException If not supported on this platform.
1176
     */
1177 71
    public function getDateSubDaysExpression($date, $days)
1178
    {
1179 71
        return $this->getDateArithmeticIntervalExpression($date, '-', $days, DateIntervalUnit::DAY);
1180
    }
1181
1182
    /**
1183
     * Returns the SQL to add the number of given weeks to a date.
1184
     *
1185
     * @param string $date
1186
     * @param int    $weeks
1187
     *
1188
     * @return string
1189
     *
1190
     * @throws DBALException If not supported on this platform.
1191
     */
1192 69
    public function getDateAddWeeksExpression($date, $weeks)
1193
    {
1194 69
        return $this->getDateArithmeticIntervalExpression($date, '+', $weeks, DateIntervalUnit::WEEK);
1195
    }
1196
1197
    /**
1198
     * Returns the SQL to subtract the number of given weeks from a date.
1199
     *
1200
     * @param string $date
1201
     * @param int    $weeks
1202
     *
1203
     * @return string
1204
     *
1205
     * @throws DBALException If not supported on this platform.
1206
     */
1207 69
    public function getDateSubWeeksExpression($date, $weeks)
1208
    {
1209 69
        return $this->getDateArithmeticIntervalExpression($date, '-', $weeks, DateIntervalUnit::WEEK);
1210
    }
1211
1212
    /**
1213
     * Returns the SQL to add the number of given months to a date.
1214
     *
1215
     * @param string $date
1216
     * @param int    $months
1217
     *
1218
     * @return string
1219
     *
1220
     * @throws DBALException If not supported on this platform.
1221
     */
1222 69
    public function getDateAddMonthExpression($date, $months)
1223
    {
1224 69
        return $this->getDateArithmeticIntervalExpression($date, '+', $months, DateIntervalUnit::MONTH);
1225
    }
1226
1227
    /**
1228
     * Returns the SQL to subtract the number of given months to a date.
1229
     *
1230
     * @param string $date
1231
     * @param int    $months
1232
     *
1233
     * @return string
1234
     *
1235
     * @throws DBALException If not supported on this platform.
1236
     */
1237 69
    public function getDateSubMonthExpression($date, $months)
1238
    {
1239 69
        return $this->getDateArithmeticIntervalExpression($date, '-', $months, DateIntervalUnit::MONTH);
1240
    }
1241
1242
    /**
1243
     * Returns the SQL to add the number of given quarters to a date.
1244
     *
1245
     * @param string $date
1246
     * @param int    $quarters
1247
     *
1248
     * @return string
1249
     *
1250
     * @throws DBALException If not supported on this platform.
1251
     */
1252 69
    public function getDateAddQuartersExpression($date, $quarters)
1253
    {
1254 69
        return $this->getDateArithmeticIntervalExpression($date, '+', $quarters, DateIntervalUnit::QUARTER);
1255
    }
1256
1257
    /**
1258
     * Returns the SQL to subtract the number of given quarters from a date.
1259
     *
1260
     * @param string $date
1261
     * @param int    $quarters
1262
     *
1263
     * @return string
1264
     *
1265
     * @throws DBALException If not supported on this platform.
1266
     */
1267 69
    public function getDateSubQuartersExpression($date, $quarters)
1268
    {
1269 69
        return $this->getDateArithmeticIntervalExpression($date, '-', $quarters, DateIntervalUnit::QUARTER);
1270
    }
1271
1272
    /**
1273
     * Returns the SQL to add the number of given years to a date.
1274
     *
1275
     * @param string $date
1276
     * @param int    $years
1277
     *
1278
     * @return string
1279
     *
1280
     * @throws DBALException If not supported on this platform.
1281
     */
1282 69
    public function getDateAddYearsExpression($date, $years)
1283
    {
1284 69
        return $this->getDateArithmeticIntervalExpression($date, '+', $years, DateIntervalUnit::YEAR);
1285
    }
1286
1287
    /**
1288
     * Returns the SQL to subtract the number of given years from a date.
1289
     *
1290
     * @param string $date
1291
     * @param int    $years
1292
     *
1293
     * @return string
1294
     *
1295
     * @throws DBALException If not supported on this platform.
1296
     */
1297 69
    public function getDateSubYearsExpression($date, $years)
1298
    {
1299 69
        return $this->getDateArithmeticIntervalExpression($date, '-', $years, DateIntervalUnit::YEAR);
1300
    }
1301
1302
    /**
1303
     * Returns the SQL for a date arithmetic expression.
1304
     *
1305
     * @param string $date     The column or literal representing a date to perform the arithmetic operation on.
1306
     * @param string $operator The arithmetic operator (+ or -).
1307
     * @param int    $interval The interval that shall be calculated into the date.
1308
     * @param string $unit     The unit of the interval that shall be calculated into the date.
1309
     *                         One of the DATE_INTERVAL_UNIT_* constants.
1310
     *
1311
     * @return string
1312
     *
1313
     * @throws DBALException If not supported on this platform.
1314
     */
1315
    protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
0 ignored issues
show
Unused Code introduced by
The parameter $unit is not used and could be removed. ( Ignorable by Annotation )

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

1315
    protected function getDateArithmeticIntervalExpression($date, $operator, $interval, /** @scrutinizer ignore-unused */ $unit)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $interval is not used and could be removed. ( Ignorable by Annotation )

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

1315
    protected function getDateArithmeticIntervalExpression($date, $operator, /** @scrutinizer ignore-unused */ $interval, $unit)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $operator is not used and could be removed. ( Ignorable by Annotation )

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

1315
    protected function getDateArithmeticIntervalExpression($date, /** @scrutinizer ignore-unused */ $operator, $interval, $unit)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1316
    {
1317
        throw DBALException::notSupported(__METHOD__);
1318
    }
1319
1320
    /**
1321
     * Returns the SQL bit AND comparison expression.
1322
     *
1323
     * @param string $value1
1324
     * @param string $value2
1325
     *
1326
     * @return string
1327
     */
1328 205
    public function getBitAndComparisonExpression($value1, $value2)
1329
    {
1330 205
        return '(' . $value1 . ' & ' . $value2 . ')';
1331
    }
1332
1333
    /**
1334
     * Returns the SQL bit OR comparison expression.
1335
     *
1336
     * @param string $value1
1337
     * @param string $value2
1338
     *
1339
     * @return string
1340
     */
1341 205
    public function getBitOrComparisonExpression($value1, $value2)
1342
    {
1343 205
        return '(' . $value1 . ' | ' . $value2 . ')';
1344
    }
1345
1346
    /**
1347
     * Returns the FOR UPDATE expression.
1348
     *
1349
     * @return string
1350
     */
1351 42
    public function getForUpdateSQL()
1352
    {
1353 42
        return 'FOR UPDATE';
1354
    }
1355
1356
    /**
1357
     * Honors that some SQL vendors such as MsSql use table hints for locking instead of the ANSI SQL FOR UPDATE specification.
1358
     *
1359
     * @param string   $fromClause The FROM clause to append the hint for the given lock mode to.
1360
     * @param int|null $lockMode   One of the Doctrine\DBAL\LockMode::* constants. If null is given, nothing will
1361
     *                             be appended to the FROM clause.
1362
     *
1363
     * @return string
1364
     */
1365 42
    public function appendLockHint($fromClause, $lockMode)
0 ignored issues
show
Unused Code introduced by
The parameter $lockMode is not used and could be removed. ( Ignorable by Annotation )

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

1365
    public function appendLockHint($fromClause, /** @scrutinizer ignore-unused */ $lockMode)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1366
    {
1367 42
        return $fromClause;
1368
    }
1369
1370
    /**
1371
     * Returns the SQL snippet to append to any SELECT statement which locks rows in shared read lock.
1372
     *
1373
     * This defaults to the ANSI SQL "FOR UPDATE", which is an exclusive lock (Write). Some database
1374
     * vendors allow to lighten this constraint up to be a real read lock.
1375
     *
1376
     * @return string
1377
     */
1378
    public function getReadLockSQL()
1379
    {
1380
        return $this->getForUpdateSQL();
1381
    }
1382
1383
    /**
1384
     * Returns the SQL snippet to append to any SELECT statement which obtains an exclusive lock on the rows.
1385
     *
1386
     * The semantics of this lock mode should equal the SELECT .. FOR UPDATE of the ANSI SQL standard.
1387
     *
1388
     * @return string
1389
     */
1390 42
    public function getWriteLockSQL()
1391
    {
1392 42
        return $this->getForUpdateSQL();
1393
    }
1394
1395
    /**
1396
     * Returns the SQL snippet to drop an existing database.
1397
     *
1398
     * @param string $database The name of the database that should be dropped.
1399
     *
1400
     * @return string
1401
     */
1402 56
    public function getDropDatabaseSQL($database)
1403
    {
1404 56
        return 'DROP DATABASE ' . $database;
1405
    }
1406
1407
    /**
1408
     * Returns the SQL snippet to drop an existing table.
1409
     *
1410
     * @param Table|string $table
1411
     *
1412
     * @return string
1413
     *
1414
     * @throws InvalidArgumentException
1415
     */
1416 3949
    public function getDropTableSQL($table)
1417
    {
1418 3949
        $tableArg = $table;
1419
1420 3949
        if ($table instanceof Table) {
1421 348
            $table = $table->getQuotedName($this);
1422
        }
1423
1424 3949
        if (! is_string($table)) {
0 ignored issues
show
introduced by
The condition is_string($table) is always true.
Loading history...
1425
            throw new InvalidArgumentException('getDropTableSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
1426
        }
1427
1428 3949
        if ($this->_eventManager !== null && $this->_eventManager->hasListeners(Events::onSchemaDropTable)) {
1429 230
            $eventArgs = new SchemaDropTableEventArgs($tableArg, $this);
1430 230
            $this->_eventManager->dispatchEvent(Events::onSchemaDropTable, $eventArgs);
1431
1432 230
            if ($eventArgs->isDefaultPrevented()) {
1433
                $sql = $eventArgs->getSql();
1434
1435
                if ($sql === null) {
1436
                    throw new UnexpectedValueException('Default implementation of DROP TABLE was overridden with NULL');
1437
                }
1438
1439
                return $sql;
1440
            }
1441
        }
1442
1443 3949
        return 'DROP TABLE ' . $table;
1444
    }
1445
1446
    /**
1447
     * Returns the SQL to safely drop a temporary table WITHOUT implicitly committing an open transaction.
1448
     *
1449
     * @param Table|string $table
1450
     *
1451
     * @return string
1452
     */
1453 14
    public function getDropTemporaryTableSQL($table)
1454
    {
1455 14
        return $this->getDropTableSQL($table);
1456
    }
1457
1458
    /**
1459
     * Returns the SQL to drop an index from a table.
1460
     *
1461
     * @param Index|string $index
1462
     * @param Table|string $table
1463
     *
1464
     * @return string
1465
     *
1466
     * @throws InvalidArgumentException
1467
     */
1468 140
    public function getDropIndexSQL($index, $table = null)
1469
    {
1470 140
        if ($index instanceof Index) {
1471 131
            $index = $index->getQuotedName($this);
1472 9
        } elseif (! is_string($index)) {
0 ignored issues
show
introduced by
The condition is_string($index) is always true.
Loading history...
1473
            throw new InvalidArgumentException('AbstractPlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
1474
        }
1475
1476 140
        return 'DROP INDEX ' . $index;
1477
    }
1478
1479
    /**
1480
     * Returns the SQL to drop a constraint.
1481
     *
1482
     * @param Constraint|string $constraint
1483
     * @param Table|string      $table
1484
     *
1485
     * @return string
1486
     */
1487 680
    public function getDropConstraintSQL($constraint, $table)
1488
    {
1489 680
        if (! $constraint instanceof Constraint) {
1490 583
            $constraint = new Identifier($constraint);
1491
        }
1492
1493 680
        if (! $table instanceof Table) {
1494 680
            $table = new Identifier($table);
1495
        }
1496
1497 680
        $constraint = $constraint->getQuotedName($this);
1498 680
        $table      = $table->getQuotedName($this);
1499
1500 680
        return 'ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $constraint;
1501
    }
1502
1503
    /**
1504
     * Returns the SQL to drop a foreign key.
1505
     *
1506
     * @param ForeignKeyConstraint|string $foreignKey
1507
     * @param Table|string                $table
1508
     *
1509
     * @return string
1510
     */
1511 367
    public function getDropForeignKeySQL($foreignKey, $table)
1512
    {
1513 367
        if (! $foreignKey instanceof ForeignKeyConstraint) {
1514 115
            $foreignKey = new Identifier($foreignKey);
1515
        }
1516
1517 367
        if (! $table instanceof Table) {
1518 367
            $table = new Identifier($table);
1519
        }
1520
1521 367
        $foreignKey = $foreignKey->getQuotedName($this);
1522 367
        $table      = $table->getQuotedName($this);
1523
1524 367
        return 'ALTER TABLE ' . $table . ' DROP FOREIGN KEY ' . $foreignKey;
1525
    }
1526
1527
    /**
1528
     * Returns the SQL statement(s) to create a table with the specified name, columns and constraints
1529
     * on this platform.
1530
     *
1531
     * @param int $createFlags
1532
     *
1533
     * @return string[] The sequence of SQL statements.
1534
     *
1535
     * @throws DBALException
1536
     * @throws InvalidArgumentException
1537
     */
1538 7598
    public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDEXES)
1539
    {
1540 7598
        if (! is_int($createFlags)) {
0 ignored issues
show
introduced by
The condition is_int($createFlags) is always true.
Loading history...
1541
            throw new InvalidArgumentException('Second argument of AbstractPlatform::getCreateTableSQL() has to be integer.');
1542
        }
1543
1544 7598
        if (count($table->getColumns()) === 0) {
1545 230
            throw DBALException::noColumnsSpecifiedForTable($table->getName());
1546
        }
1547
1548 7368
        $tableName                    = $table->getQuotedName($this);
1549 7368
        $options                      = $table->getOptions();
1550 7368
        $options['uniqueConstraints'] = [];
1551 7368
        $options['indexes']           = [];
1552 7368
        $options['primary']           = [];
1553
1554 7368
        if (($createFlags & self::CREATE_INDEXES) > 0) {
1555 7092
            foreach ($table->getIndexes() as $index) {
1556
                /** @var $index Index */
1557 4751
                if (! $index->isPrimary()) {
1558 1461
                    $options['indexes'][$index->getQuotedName($this)] = $index;
1559
1560 1461
                    continue;
1561
                }
1562
1563 3731
                $options['primary']       = $index->getQuotedColumns($this);
1564 3731
                $options['primary_index'] = $index;
1565
            }
1566
1567 7092
            foreach ($table->getUniqueConstraints() as $uniqueConstraint) {
1568
                $options['uniqueConstraints'][$uniqueConstraint->getQuotedName($this)] = $uniqueConstraint;
1569
            }
1570
        }
1571
1572 7368
        if (($createFlags & self::CREATE_FOREIGNKEYS) > 0) {
1573 4549
            $options['foreignKeys'] = [];
1574
1575 4549
            foreach ($table->getForeignKeys() as $fkConstraint) {
1576 634
                $options['foreignKeys'][] = $fkConstraint;
1577
            }
1578
        }
1579
1580 7368
        $columnSql = [];
1581 7368
        $columns   = [];
1582
1583 7368
        foreach ($table->getColumns() as $column) {
1584 7368
            if ($this->_eventManager !== null && $this->_eventManager->hasListeners(Events::onSchemaCreateTableColumn)) {
1585 230
                $eventArgs = new SchemaCreateTableColumnEventArgs($column, $table, $this);
1586
1587 230
                $this->_eventManager->dispatchEvent(Events::onSchemaCreateTableColumn, $eventArgs);
1588
1589 230
                $columnSql = array_merge($columnSql, $eventArgs->getSql());
1590
1591 230
                if ($eventArgs->isDefaultPrevented()) {
1592
                    continue;
1593
                }
1594
            }
1595
1596 7368
            $columnData            = $column->toArray();
1597 7368
            $columnData['name']    = $column->getQuotedName($this);
1598 7368
            $columnData['version'] = $column->hasPlatformOption('version') ? $column->getPlatformOption('version') : false;
1599 7368
            $columnData['comment'] = $this->getColumnComment($column);
1600
1601 7368
            if ($columnData['type'] instanceof Types\StringType && $columnData['length'] === null) {
1602 2673
                $columnData['length'] = 255;
1603
            }
1604
1605 7368
            if (in_array($column->getName(), $options['primary'], true)) {
1606 3455
                $columnData['primary'] = true;
1607
            }
1608
1609 7368
            $columns[$columnData['name']] = $columnData;
1610
        }
1611
1612 7368
        if ($this->_eventManager !== null && $this->_eventManager->hasListeners(Events::onSchemaCreateTable)) {
1613 230
            $eventArgs = new SchemaCreateTableEventArgs($table, $columns, $options, $this);
1614
1615 230
            $this->_eventManager->dispatchEvent(Events::onSchemaCreateTable, $eventArgs);
1616
1617 230
            if ($eventArgs->isDefaultPrevented()) {
1618
                return array_merge($eventArgs->getSql(), $columnSql);
1619
            }
1620
        }
1621
1622 7368
        $sql = $this->_getCreateTableSQL($tableName, $columns, $options);
1623
1624 7368
        if ($this->supportsCommentOnStatement()) {
1625 2622
            if ($table->hasOption('comment')) {
1626 53
                $sql[] = $this->getCommentOnTableSQL($tableName, $table->getOption('comment'));
1627
            }
1628
1629 2622
            foreach ($table->getColumns() as $column) {
1630 2622
                $comment = $this->getColumnComment($column);
1631
1632 2622
                if ($comment === null || $comment === '') {
1633 2465
                    continue;
1634
                }
1635
1636 449
                $sql[] = $this->getCommentOnColumnSQL($tableName, $column->getQuotedName($this), $comment);
1637
            }
1638
        }
1639
1640 7368
        return array_merge($sql, $columnSql);
1641
    }
1642
1643 53
    protected function getCommentOnTableSQL(string $tableName, ?string $comment) : string
1644
    {
1645 53
        $tableName = new Identifier($tableName);
1646
1647 53
        return sprintf(
1648 6
            'COMMENT ON TABLE %s IS %s',
1649 53
            $tableName->getQuotedName($this),
1650 53
            $this->quoteStringLiteral((string) $comment)
1651
        );
1652
    }
1653
1654
    /**
1655
     * @param string      $tableName
1656
     * @param string      $columnName
1657
     * @param string|null $comment
1658
     *
1659
     * @return string
1660
     */
1661 708
    public function getCommentOnColumnSQL($tableName, $columnName, $comment)
1662
    {
1663 708
        $tableName  = new Identifier($tableName);
1664 708
        $columnName = new Identifier($columnName);
1665
1666 708
        return sprintf(
1667 120
            'COMMENT ON COLUMN %s.%s IS %s',
1668 708
            $tableName->getQuotedName($this),
1669 708
            $columnName->getQuotedName($this),
1670 708
            $this->quoteStringLiteral((string) $comment)
1671
        );
1672
    }
1673
1674
    /**
1675
     * Returns the SQL to create inline comment on a column.
1676
     *
1677
     * @param string $comment
1678
     *
1679
     * @return string
1680
     *
1681
     * @throws DBALException If not supported on this platform.
1682
     */
1683 1223
    public function getInlineColumnCommentSQL($comment)
1684
    {
1685 1223
        if (! $this->supportsInlineColumnComments()) {
1686 138
            throw DBALException::notSupported(__METHOD__);
1687
        }
1688
1689 1085
        return 'COMMENT ' . $this->quoteStringLiteral($comment);
1690
    }
1691
1692
    /**
1693
     * Returns the SQL used to create a table.
1694
     *
1695
     * @param string    $name
1696
     * @param mixed[][] $columns
1697
     * @param mixed[]   $options
1698
     *
1699
     * @return string[]
1700
     */
1701 821
    protected function _getCreateTableSQL($name, array $columns, array $options = [])
1702
    {
1703 821
        $columnListSql = $this->getColumnDeclarationListSQL($columns);
1704
1705 821
        if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
1706
            foreach ($options['uniqueConstraints'] as $constraintName => $definition) {
1707
                $columnListSql .= ', ' . $this->getUniqueConstraintDeclarationSQL($constraintName, $definition);
1708
            }
1709
        }
1710
1711 821
        if (isset($options['primary']) && ! empty($options['primary'])) {
1712 416
            $columnListSql .= ', PRIMARY KEY(' . implode(', ', array_unique(array_values($options['primary']))) . ')';
1713
        }
1714
1715 821
        if (isset($options['indexes']) && ! empty($options['indexes'])) {
1716
            foreach ($options['indexes'] as $index => $definition) {
1717
                $columnListSql .= ', ' . $this->getIndexDeclarationSQL($index, $definition);
1718
            }
1719
        }
1720
1721 821
        $query = 'CREATE TABLE ' . $name . ' (' . $columnListSql;
1722 821
        $check = $this->getCheckDeclarationSQL($columns);
1723
1724 821
        if (! empty($check)) {
1725 23
            $query .= ', ' . $check;
1726
        }
1727
1728 821
        $query .= ')';
1729
1730 821
        $sql = [$query];
1731
1732 821
        if (isset($options['foreignKeys'])) {
1733 343
            foreach ((array) $options['foreignKeys'] as $definition) {
1734 87
                $sql[] = $this->getCreateForeignKeySQL($definition, $name);
1735
            }
1736
        }
1737
1738 821
        return $sql;
1739
    }
1740
1741
    /**
1742
     * @return string
1743
     */
1744 42
    public function getCreateTemporaryTableSnippetSQL()
1745
    {
1746 42
        return 'CREATE TEMPORARY TABLE';
1747
    }
1748
1749
    /**
1750
     * Returns the SQL to create a sequence on this platform.
1751
     *
1752
     * @return string
1753
     *
1754
     * @throws DBALException If not supported on this platform.
1755
     */
1756
    public function getCreateSequenceSQL(Sequence $sequence)
0 ignored issues
show
Unused Code introduced by
The parameter $sequence is not used and could be removed. ( Ignorable by Annotation )

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

1756
    public function getCreateSequenceSQL(/** @scrutinizer ignore-unused */ Sequence $sequence)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1757
    {
1758
        throw DBALException::notSupported(__METHOD__);
1759
    }
1760
1761
    /**
1762
     * Returns the SQL to change a sequence on this platform.
1763
     *
1764
     * @return string
1765
     *
1766
     * @throws DBALException If not supported on this platform.
1767
     */
1768
    public function getAlterSequenceSQL(Sequence $sequence)
0 ignored issues
show
Unused Code introduced by
The parameter $sequence is not used and could be removed. ( Ignorable by Annotation )

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

1768
    public function getAlterSequenceSQL(/** @scrutinizer ignore-unused */ Sequence $sequence)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1769
    {
1770
        throw DBALException::notSupported(__METHOD__);
1771
    }
1772
1773
    /**
1774
     * Returns the SQL to create a constraint on a table on this platform.
1775
     *
1776
     * @param Table|string $table
1777
     *
1778
     * @return string
1779
     *
1780
     * @throws InvalidArgumentException
1781
     */
1782 301
    public function getCreateConstraintSQL(Constraint $constraint, $table)
1783
    {
1784 301
        if ($table instanceof Table) {
1785
            $table = $table->getQuotedName($this);
1786
        }
1787
1788 301
        $query = 'ALTER TABLE ' . $table . ' ADD CONSTRAINT ' . $constraint->getQuotedName($this);
1789
1790 301
        $columnList = '(' . implode(', ', $constraint->getQuotedColumns($this)) . ')';
1791
1792 301
        $referencesClause = '';
1793 301
        if ($constraint instanceof Index) {
1794 301
            if ($constraint->isPrimary()) {
1795 301
                $query .= ' PRIMARY KEY';
1796 184
            } elseif ($constraint->isUnique()) {
1797 184
                $query .= ' UNIQUE';
1798
            } else {
1799
                throw new InvalidArgumentException(
1800 301
                    'Can only create primary or unique constraints, no common indexes with getCreateConstraintSQL().'
1801
                );
1802
            }
1803 184
        } elseif ($constraint instanceof ForeignKeyConstraint) {
1804 184
            $query .= ' FOREIGN KEY';
1805
1806 184
            $referencesClause = ' REFERENCES ' . $constraint->getQuotedForeignTableName($this) .
1807 184
                ' (' . implode(', ', $constraint->getQuotedForeignColumns($this)) . ')';
1808
        }
1809 301
        $query .= ' ' . $columnList . $referencesClause;
1810
1811 301
        return $query;
1812
    }
1813
1814
    /**
1815
     * Returns the SQL to create an index on a table on this platform.
1816
     *
1817
     * @param Table|string $table The name of the table on which the index is to be created.
1818
     *
1819
     * @return string
1820
     *
1821
     * @throws InvalidArgumentException
1822
     */
1823 2301
    public function getCreateIndexSQL(Index $index, $table)
1824
    {
1825 2301
        if ($table instanceof Table) {
1826 23
            $table = $table->getQuotedName($this);
1827
        }
1828 2301
        $name    = $index->getQuotedName($this);
1829 2301
        $columns = $index->getColumns();
1830
1831 2301
        if (count($columns) === 0) {
1832
            throw new InvalidArgumentException("Incomplete definition. 'columns' required.");
1833
        }
1834
1835 2301
        if ($index->isPrimary()) {
1836 414
            return $this->getCreatePrimaryKeySQL($index, $table);
1837
        }
1838
1839 1910
        $query  = 'CREATE ' . $this->getCreateIndexSQLFlags($index) . 'INDEX ' . $name . ' ON ' . $table;
1840 1910
        $query .= ' (' . $this->getIndexFieldDeclarationListSQL($index) . ')' . $this->getPartialIndexSQL($index);
1841
1842 1910
        return $query;
1843
    }
1844
1845
    /**
1846
     * Adds condition for partial index.
1847
     *
1848
     * @return string
1849
     */
1850 2770
    protected function getPartialIndexSQL(Index $index)
1851
    {
1852 2770
        if ($this->supportsPartialIndexes() && $index->hasOption('where')) {
1853 51
            return ' WHERE ' . $index->getOption('where');
1854
        }
1855
1856 2719
        return '';
1857
    }
1858
1859
    /**
1860
     * Adds additional flags for index generation.
1861
     *
1862
     * @return string
1863
     */
1864 1078
    protected function getCreateIndexSQLFlags(Index $index)
1865
    {
1866 1078
        return $index->isUnique() ? 'UNIQUE ' : '';
1867
    }
1868
1869
    /**
1870
     * Returns the SQL to create an unnamed primary key constraint.
1871
     *
1872
     * @param Table|string $table
1873
     *
1874
     * @return string
1875
     */
1876 391
    public function getCreatePrimaryKeySQL(Index $index, $table)
1877
    {
1878 391
        if ($table instanceof Table) {
1879
            $table = $table->getQuotedName($this);
1880
        }
1881
1882 391
        return 'ALTER TABLE ' . $table . ' ADD PRIMARY KEY (' . $this->getIndexFieldDeclarationListSQL($index) . ')';
1883
    }
1884
1885
    /**
1886
     * Returns the SQL to create a named schema.
1887
     *
1888
     * @param string $schemaName
1889
     *
1890
     * @return string
1891
     *
1892
     * @throws DBALException If not supported on this platform.
1893
     */
1894 161
    public function getCreateSchemaSQL($schemaName)
1895
    {
1896 161
        throw DBALException::notSupported(__METHOD__);
1897
    }
1898
1899
    /**
1900
     * Quotes a string so that it can be safely used as a table or column name,
1901
     * even if it is a reserved word of the platform. This also detects identifier
1902
     * chains separated by dot and quotes them independently.
1903
     *
1904
     * NOTE: Just because you CAN use quoted identifiers doesn't mean
1905
     * you SHOULD use them. In general, they end up causing way more
1906
     * problems than they solve.
1907
     *
1908
     * @param string $str The identifier name to be quoted.
1909
     *
1910
     * @return string The quoted identifier string.
1911
     */
1912 5281
    public function quoteIdentifier($str)
1913
    {
1914 5281
        if (strpos($str, '.') !== false) {
1915 260
            $parts = array_map([$this, 'quoteSingleIdentifier'], explode('.', $str));
1916
1917 260
            return implode('.', $parts);
1918
        }
1919
1920 5281
        return $this->quoteSingleIdentifier($str);
1921
    }
1922
1923
    /**
1924
     * Quotes a single identifier (no dot chain separation).
1925
     *
1926
     * @param string $str The identifier name to be quoted.
1927
     *
1928
     * @return string The quoted identifier string.
1929
     */
1930 9153
    public function quoteSingleIdentifier($str)
1931
    {
1932 9153
        $c = $this->getIdentifierQuoteCharacter();
1933
1934 9153
        return $c . str_replace($c, $c . $c, $str) . $c;
1935
    }
1936
1937
    /**
1938
     * Returns the SQL to create a new foreign key.
1939
     *
1940
     * @param ForeignKeyConstraint $foreignKey The foreign key constraint.
1941
     * @param Table|string         $table      The name of the table on which the foreign key is to be created.
1942
     *
1943
     * @return string
1944
     */
1945 1316
    public function getCreateForeignKeySQL(ForeignKeyConstraint $foreignKey, $table)
1946
    {
1947 1316
        if ($table instanceof Table) {
1948 23
            $table = $table->getQuotedName($this);
1949
        }
1950
1951 1316
        return 'ALTER TABLE ' . $table . ' ADD ' . $this->getForeignKeyDeclarationSQL($foreignKey);
1952
    }
1953
1954
    /**
1955
     * Gets the SQL statements for altering an existing table.
1956
     *
1957
     * This method returns an array of SQL statements, since some platforms need several statements.
1958
     *
1959
     * @return string[]
1960
     *
1961
     * @throws DBALException If not supported on this platform.
1962
     */
1963
    public function getAlterTableSQL(TableDiff $diff)
1964
    {
1965
        throw DBALException::notSupported(__METHOD__);
1966
    }
1967
1968
    /**
1969
     * @param mixed[] $columnSql
1970
     *
1971
     * @return bool
1972
     */
1973 1279
    protected function onSchemaAlterTableAddColumn(Column $column, TableDiff $diff, &$columnSql)
1974
    {
1975 1279
        if ($this->_eventManager === null) {
1976 1012
            return false;
1977
        }
1978
1979 267
        if (! $this->_eventManager->hasListeners(Events::onSchemaAlterTableAddColumn)) {
1980 37
            return false;
1981
        }
1982
1983 230
        $eventArgs = new SchemaAlterTableAddColumnEventArgs($column, $diff, $this);
1984 230
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTableAddColumn, $eventArgs);
1985
1986 230
        $columnSql = array_merge($columnSql, $eventArgs->getSql());
1987
1988 230
        return $eventArgs->isDefaultPrevented();
1989
    }
1990
1991
    /**
1992
     * @param string[] $columnSql
1993
     *
1994
     * @return bool
1995
     */
1996 897
    protected function onSchemaAlterTableRemoveColumn(Column $column, TableDiff $diff, &$columnSql)
1997
    {
1998 897
        if ($this->_eventManager === null) {
1999 644
            return false;
2000
        }
2001
2002 253
        if (! $this->_eventManager->hasListeners(Events::onSchemaAlterTableRemoveColumn)) {
2003 23
            return false;
2004
        }
2005
2006 230
        $eventArgs = new SchemaAlterTableRemoveColumnEventArgs($column, $diff, $this);
2007 230
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTableRemoveColumn, $eventArgs);
2008
2009 230
        $columnSql = array_merge($columnSql, $eventArgs->getSql());
2010
2011 230
        return $eventArgs->isDefaultPrevented();
2012
    }
2013
2014
    /**
2015
     * @param string[] $columnSql
2016
     *
2017
     * @return bool
2018
     */
2019 2613
    protected function onSchemaAlterTableChangeColumn(ColumnDiff $columnDiff, TableDiff $diff, &$columnSql)
2020
    {
2021 2613
        if ($this->_eventManager === null) {
2022 2047
            return false;
2023
        }
2024
2025 566
        if (! $this->_eventManager->hasListeners(Events::onSchemaAlterTableChangeColumn)) {
2026 336
            return false;
2027
        }
2028
2029 230
        $eventArgs = new SchemaAlterTableChangeColumnEventArgs($columnDiff, $diff, $this);
2030 230
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTableChangeColumn, $eventArgs);
2031
2032 230
        $columnSql = array_merge($columnSql, $eventArgs->getSql());
2033
2034 230
        return $eventArgs->isDefaultPrevented();
2035
    }
2036
2037
    /**
2038
     * @param string   $oldColumnName
2039
     * @param string[] $columnSql
2040
     *
2041
     * @return bool
2042
     */
2043 987
    protected function onSchemaAlterTableRenameColumn($oldColumnName, Column $column, TableDiff $diff, &$columnSql)
2044
    {
2045 987
        if ($this->_eventManager === null) {
2046 736
            return false;
2047
        }
2048
2049 251
        if (! $this->_eventManager->hasListeners(Events::onSchemaAlterTableRenameColumn)) {
2050 21
            return false;
2051
        }
2052
2053 230
        $eventArgs = new SchemaAlterTableRenameColumnEventArgs($oldColumnName, $column, $diff, $this);
2054 230
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTableRenameColumn, $eventArgs);
2055
2056 230
        $columnSql = array_merge($columnSql, $eventArgs->getSql());
2057
2058 230
        return $eventArgs->isDefaultPrevented();
2059
    }
2060
2061
    /**
2062
     * @param string[] $sql
2063
     *
2064
     * @return bool
2065
     */
2066 5232
    protected function onSchemaAlterTable(TableDiff $diff, &$sql)
2067
    {
2068 5232
        if ($this->_eventManager === null) {
2069 4531
            return false;
2070
        }
2071
2072 701
        if (! $this->_eventManager->hasListeners(Events::onSchemaAlterTable)) {
2073 471
            return false;
2074
        }
2075
2076 230
        $eventArgs = new SchemaAlterTableEventArgs($diff, $this);
2077 230
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTable, $eventArgs);
2078
2079 230
        $sql = array_merge($sql, $eventArgs->getSql());
2080
2081 230
        return $eventArgs->isDefaultPrevented();
2082
    }
2083
2084
    /**
2085
     * @return string[]
2086
     */
2087 4895
    protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
2088
    {
2089 4895
        $tableName = $diff->getName($this)->getQuotedName($this);
2090
2091 4895
        $sql = [];
2092 4895
        if ($this->supportsForeignKeyConstraints()) {
2093 4895
            foreach ($diff->removedForeignKeys as $foreignKey) {
2094 343
                $sql[] = $this->getDropForeignKeySQL($foreignKey, $tableName);
2095
            }
2096 4895
            foreach ($diff->changedForeignKeys as $foreignKey) {
2097 276
                $sql[] = $this->getDropForeignKeySQL($foreignKey, $tableName);
2098
            }
2099
        }
2100
2101 4895
        foreach ($diff->removedIndexes as $index) {
2102 233
            $sql[] = $this->getDropIndexSQL($index, $tableName);
2103
        }
2104 4895
        foreach ($diff->changedIndexes as $index) {
2105 410
            $sql[] = $this->getDropIndexSQL($index, $tableName);
2106
        }
2107
2108 4895
        return $sql;
2109
    }
2110
2111
    /**
2112
     * @return string[]
2113
     */
2114 4895
    protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff)
2115
    {
2116 4895
        $sql     = [];
2117 4895
        $newName = $diff->getNewName();
2118
2119 4895
        if ($newName !== false) {
2120 416
            $tableName = $newName->getQuotedName($this);
2121
        } else {
2122 4479
            $tableName = $diff->getName($this)->getQuotedName($this);
2123
        }
2124
2125 4895
        if ($this->supportsForeignKeyConstraints()) {
2126 4895
            foreach ($diff->addedForeignKeys as $foreignKey) {
2127 318
                $sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName);
2128
            }
2129
2130 4895
            foreach ($diff->changedForeignKeys as $foreignKey) {
2131 276
                $sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName);
2132
            }
2133
        }
2134
2135 4895
        foreach ($diff->addedIndexes as $index) {
2136 90
            $sql[] = $this->getCreateIndexSQL($index, $tableName);
2137
        }
2138
2139 4895
        foreach ($diff->changedIndexes as $index) {
2140 410
            $sql[] = $this->getCreateIndexSQL($index, $tableName);
2141
        }
2142
2143 4895
        foreach ($diff->renamedIndexes as $oldIndexName => $index) {
2144 1077
            $oldIndexName = new Identifier($oldIndexName);
2145 1077
            $sql          = array_merge(
2146 1077
                $sql,
2147 1077
                $this->getRenameIndexSQL($oldIndexName->getQuotedName($this), $index, $tableName)
2148
            );
2149
        }
2150
2151 4895
        return $sql;
2152
    }
2153
2154
    /**
2155
     * Returns the SQL for renaming an index on a table.
2156
     *
2157
     * @param string $oldIndexName The name of the index to rename from.
2158
     * @param Index  $index        The definition of the index to rename to.
2159
     * @param string $tableName    The table to rename the given index on.
2160
     *
2161
     * @return string[] The sequence of SQL statements for renaming the given index.
2162
     */
2163 246
    protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
2164
    {
2165
        return [
2166 246
            $this->getDropIndexSQL($oldIndexName, $tableName),
2167 246
            $this->getCreateIndexSQL($index, $tableName),
2168
        ];
2169
    }
2170
2171
    /**
2172
     * Common code for alter table statement generation that updates the changed Index and Foreign Key definitions.
2173
     *
2174
     * @deprecated
2175
     *
2176
     * @return string[]
2177
     */
2178
    protected function _getAlterTableIndexForeignKeySQL(TableDiff $diff)
2179
    {
2180
        return array_merge($this->getPreAlterTableIndexForeignKeySQL($diff), $this->getPostAlterTableIndexForeignKeySQL($diff));
2181
    }
2182
2183
    /**
2184
     * Gets declaration of a number of fields in bulk.
2185
     *
2186
     * @param mixed[][] $fields A multidimensional associative array.
2187
     *                          The first dimension determines the field name, while the second
2188
     *                          dimension is keyed with the name of the properties
2189
     *                          of the field being declared as array indexes. Currently, the types
2190
     *                          of supported field properties are as follows:
2191
     *
2192
     *      length
2193
     *          Integer value that determines the maximum length of the text
2194
     *          field. If this argument is missing the field should be
2195
     *          declared to have the longest length allowed by the DBMS.
2196
     *
2197
     *      default
2198
     *          Text value to be used as default for this field.
2199
     *
2200
     *      notnull
2201
     *          Boolean flag that indicates whether this field is constrained
2202
     *          to not be set to null.
2203
     *      charset
2204
     *          Text value with the default CHARACTER SET for this field.
2205
     *      collation
2206
     *          Text value with the default COLLATION for this field.
2207
     *      unique
2208
     *          unique constraint
2209
     *
2210
     * @return string
2211
     */
2212 7368
    public function getColumnDeclarationListSQL(array $fields)
2213
    {
2214 7368
        $queryFields = [];
2215
2216 7368
        foreach ($fields as $fieldName => $field) {
2217 7368
            $queryFields[] = $this->getColumnDeclarationSQL($fieldName, $field);
2218
        }
2219
2220 7368
        return implode(', ', $queryFields);
2221
    }
2222
2223
    /**
2224
     * Obtains DBMS specific SQL code portion needed to declare a generic type
2225
     * field to be used in statements like CREATE TABLE.
2226
     *
2227
     * @param string  $name  The name the field to be declared.
2228
     * @param mixed[] $field An associative array with the name of the properties
2229
     *                       of the field being declared as array indexes. Currently, the types
2230
     *                       of supported field properties are as follows:
2231
     *
2232
     *      length
2233
     *          Integer value that determines the maximum length of the text
2234
     *          field. If this argument is missing the field should be
2235
     *          declared to have the longest length allowed by the DBMS.
2236
     *
2237
     *      default
2238
     *          Text value to be used as default for this field.
2239
     *
2240
     *      notnull
2241
     *          Boolean flag that indicates whether this field is constrained
2242
     *          to not be set to null.
2243
     *      charset
2244
     *          Text value with the default CHARACTER SET for this field.
2245
     *      collation
2246
     *          Text value with the default COLLATION for this field.
2247
     *      unique
2248
     *          unique constraint
2249
     *      check
2250
     *          column check constraint
2251
     *      columnDefinition
2252
     *          a string that defines the complete column
2253
     *
2254
     * @return string DBMS specific SQL code portion that should be used to declare the column.
2255
     */
2256 8102
    public function getColumnDeclarationSQL($name, array $field)
2257
    {
2258 8102
        if (isset($field['columnDefinition'])) {
2259 200
            $columnDef = $this->getCustomTypeDeclarationSQL($field);
2260
        } else {
2261 7918
            $default = $this->getDefaultValueDeclarationSQL($field);
2262
2263 7918
            $charset = ! empty($field['charset']) ?
2264 7918
                ' ' . $this->getColumnCharsetDeclarationSQL($field['charset']) : '';
2265
2266 7918
            $collation = ! empty($field['collation']) ?
2267 7918
                ' ' . $this->getColumnCollationDeclarationSQL($field['collation']) : '';
2268
2269 7918
            $notnull = ! empty($field['notnull']) ? ' NOT NULL' : '';
2270
2271 7918
            $unique = ! empty($field['unique']) ?
2272 7918
                ' ' . $this->getUniqueFieldDeclarationSQL() : '';
2273
2274 7918
            $check = ! empty($field['check']) ? ' ' . $field['check'] : '';
2275
2276 7918
            $typeDecl  = $field['type']->getSQLDeclaration($field, $this);
2277 7918
            $columnDef = $typeDecl . $charset . $default . $notnull . $unique . $check . $collation;
2278
2279 7918
            if ($this->supportsInlineColumnComments() && isset($field['comment']) && $field['comment'] !== '') {
2280 1013
                $columnDef .= ' ' . $this->getInlineColumnCommentSQL($field['comment']);
2281
            }
2282
        }
2283
2284 8102
        return $name . ' ' . $columnDef;
2285
    }
2286
2287
    /**
2288
     * Returns the SQL snippet that declares a floating point column of arbitrary precision.
2289
     *
2290
     * @param mixed[] $columnDef
2291
     *
2292
     * @return string
2293
     */
2294 1919
    public function getDecimalTypeDeclarationSQL(array $columnDef)
2295
    {
2296 1919
        $columnDef['precision'] = ! isset($columnDef['precision']) || empty($columnDef['precision'])
2297 1919
            ? 10 : $columnDef['precision'];
2298 1919
        $columnDef['scale']     = ! isset($columnDef['scale']) || empty($columnDef['scale'])
2299 1919
            ? 0 : $columnDef['scale'];
2300
2301 1919
        return 'NUMERIC(' . $columnDef['precision'] . ', ' . $columnDef['scale'] . ')';
2302
    }
2303
2304
    /**
2305
     * Obtains DBMS specific SQL code portion needed to set a default value
2306
     * declaration to be used in statements like CREATE TABLE.
2307
     *
2308
     * @param mixed[] $field The field definition array.
2309
     *
2310
     * @return string DBMS specific SQL code portion needed to set a default value.
2311
     */
2312 9632
    public function getDefaultValueDeclarationSQL($field)
2313
    {
2314 9632
        if (! isset($field['default'])) {
2315 8144
            return empty($field['notnull']) ? ' DEFAULT NULL' : '';
2316
        }
2317
2318 1961
        $default = $field['default'];
2319
2320 1961
        if (! isset($field['type'])) {
2321 92
            return " DEFAULT '" . $default . "'";
2322
        }
2323
2324 1869
        $type = $field['type'];
2325
2326 1869
        if ($type instanceof Types\PhpIntegerMappingType) {
2327 537
            return ' DEFAULT ' . $default;
2328
        }
2329
2330 1420
        if ($type instanceof Types\PhpDateTimeMappingType && $default === $this->getCurrentTimestampSQL()) {
2331 285
            return ' DEFAULT ' . $this->getCurrentTimestampSQL();
2332
        }
2333
2334 1153
        if ($type instanceof Types\TimeType && $default === $this->getCurrentTimeSQL()) {
2335 11
            return ' DEFAULT ' . $this->getCurrentTimeSQL();
2336
        }
2337
2338 1146
        if ($type instanceof Types\DateType && $default === $this->getCurrentDateSQL()) {
2339 243
            return ' DEFAULT ' . $this->getCurrentDateSQL();
2340
        }
2341
2342 903
        if ($type instanceof Types\BooleanType) {
2343 258
            return " DEFAULT '" . $this->convertBooleans($default) . "'";
0 ignored issues
show
Bug introduced by
Are you sure $this->convertBooleans($default) of type array|integer|mixed can be used in concatenation? ( Ignorable by Annotation )

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

2343
            return " DEFAULT '" . /** @scrutinizer ignore-type */ $this->convertBooleans($default) . "'";
Loading history...
2344
        }
2345
2346 875
        return ' DEFAULT ' . $this->quoteStringLiteral($default);
2347
    }
2348
2349
    /**
2350
     * Obtains DBMS specific SQL code portion needed to set a CHECK constraint
2351
     * declaration to be used in statements like CREATE TABLE.
2352
     *
2353
     * @param string[]|mixed[][] $definition The check definition.
2354
     *
2355
     * @return string DBMS specific SQL code portion needed to set a CHECK constraint.
2356
     */
2357 1488
    public function getCheckDeclarationSQL(array $definition)
2358
    {
2359 1488
        $constraints = [];
2360 1488
        foreach ($definition as $field => $def) {
2361 1488
            if (is_string($def)) {
2362
                $constraints[] = 'CHECK (' . $def . ')';
2363
            } else {
2364 1488
                if (isset($def['min'])) {
2365 46
                    $constraints[] = 'CHECK (' . $field . ' >= ' . $def['min'] . ')';
2366
                }
2367
2368 1488
                if (isset($def['max'])) {
2369 46
                    $constraints[] = 'CHECK (' . $field . ' <= ' . $def['max'] . ')';
2370
                }
2371
            }
2372
        }
2373
2374 1488
        return implode(', ', $constraints);
2375
    }
2376
2377
    /**
2378
     * Obtains DBMS specific SQL code portion needed to set a unique
2379
     * constraint declaration to be used in statements like CREATE TABLE.
2380
     *
2381
     * @param string           $name       The name of the unique constraint.
2382
     * @param UniqueConstraint $constraint The unique constraint definition.
2383
     *
2384
     * @return string DBMS specific SQL code portion needed to set a constraint.
2385
     *
2386
     * @throws InvalidArgumentException
2387
     */
2388 506
    public function getUniqueConstraintDeclarationSQL($name, UniqueConstraint $constraint)
2389
    {
2390 506
        $columns = $constraint->getQuotedColumns($this);
2391 506
        $name    = new Identifier($name);
2392
2393 506
        if (count($columns) === 0) {
2394 23
            throw new InvalidArgumentException("Incomplete definition. 'columns' required.");
2395
        }
2396
2397 483
        $constraintFlags = array_merge(['UNIQUE'], array_map('strtoupper', $constraint->getFlags()));
2398 483
        $constraintName  = $name->getQuotedName($this);
2399 483
        $columnListNames = $this->getIndexFieldDeclarationListSQL($columns);
2400
2401 483
        return sprintf('CONSTRAINT %s %s (%s)', $constraintName, implode(' ', $constraintFlags), $columnListNames);
2402
    }
2403
2404
    /**
2405
     * Obtains DBMS specific SQL code portion needed to set an index
2406
     * declaration to be used in statements like CREATE TABLE.
2407
     *
2408
     * @param string $name  The name of the index.
2409
     * @param Index  $index The index definition.
2410
     *
2411
     * @return string DBMS specific SQL code portion needed to set an index.
2412
     *
2413
     * @throws InvalidArgumentException
2414
     */
2415 1080
    public function getIndexDeclarationSQL($name, Index $index)
2416
    {
2417 1080
        $columns = $index->getColumns();
2418 1080
        $name    = new Identifier($name);
2419
2420 1080
        if (count($columns) === 0) {
2421
            throw new InvalidArgumentException("Incomplete definition. 'columns' required.");
2422
        }
2423
2424 1080
        return $this->getCreateIndexSQLFlags($index) . 'INDEX ' . $name->getQuotedName($this)
2425 1080
            . ' (' . $this->getIndexFieldDeclarationListSQL($index) . ')' . $this->getPartialIndexSQL($index);
2426
    }
2427
2428
    /**
2429
     * Obtains SQL code portion needed to create a custom column,
2430
     * e.g. when a field has the "columnDefinition" keyword.
2431
     * Only "AUTOINCREMENT" and "PRIMARY KEY" are added if appropriate.
2432
     *
2433
     * @param mixed[] $columnDef
2434
     *
2435
     * @return string
2436
     */
2437 246
    public function getCustomTypeDeclarationSQL(array $columnDef)
2438
    {
2439 246
        return $columnDef['columnDefinition'];
2440
    }
2441
2442
    /**
2443
     * Obtains DBMS specific SQL code portion needed to set an index
2444
     * declaration to be used in statements like CREATE TABLE.
2445
     *
2446
     * @param mixed[]|Index $columnsOrIndex array declaration is deprecated, prefer passing Index to this method
2447
     */
2448 3736
    public function getIndexFieldDeclarationListSQL($columnsOrIndex) : string
2449
    {
2450 3736
        if ($columnsOrIndex instanceof Index) {
2451 3276
            return implode(', ', $columnsOrIndex->getQuotedColumns($this));
2452
        }
2453
2454 713
        if (! is_array($columnsOrIndex)) {
0 ignored issues
show
introduced by
The condition is_array($columnsOrIndex) is always true.
Loading history...
2455
            throw new InvalidArgumentException('Fields argument should be an Index or array.');
2456
        }
2457
2458 713
        $ret = [];
2459
2460 713
        foreach ($columnsOrIndex as $column => $definition) {
2461 713
            if (is_array($definition)) {
2462
                $ret[] = $column;
2463
            } else {
2464 713
                $ret[] = $definition;
2465
            }
2466
        }
2467
2468 713
        return implode(', ', $ret);
2469
    }
2470
2471
    /**
2472
     * Returns the required SQL string that fits between CREATE ... TABLE
2473
     * to create the table as a temporary table.
2474
     *
2475
     * Should be overridden in driver classes to return the correct string for the
2476
     * specific database type.
2477
     *
2478
     * The default is to return the string "TEMPORARY" - this will result in a
2479
     * SQL error for any database that does not support temporary tables, or that
2480
     * requires a different SQL command from "CREATE TEMPORARY TABLE".
2481
     *
2482
     * @return string The string required to be placed between "CREATE" and "TABLE"
2483
     *                to generate a temporary table, if possible.
2484
     */
2485
    public function getTemporaryTableSQL()
2486
    {
2487
        return 'TEMPORARY';
2488
    }
2489
2490
    /**
2491
     * Some vendors require temporary table names to be qualified specially.
2492
     *
2493
     * @param string $tableName
2494
     *
2495
     * @return string
2496
     */
2497 38
    public function getTemporaryTableName($tableName)
2498
    {
2499 38
        return $tableName;
2500
    }
2501
2502
    /**
2503
     * Obtain DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
2504
     * of a field declaration to be used in statements like CREATE TABLE.
2505
     *
2506
     * @return string DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
2507
     *                of a field declaration.
2508
     */
2509 1617
    public function getForeignKeyDeclarationSQL(ForeignKeyConstraint $foreignKey)
2510
    {
2511 1617
        $sql  = $this->getForeignKeyBaseDeclarationSQL($foreignKey);
2512 1548
        $sql .= $this->getAdvancedForeignKeyOptionsSQL($foreignKey);
2513
2514 1548
        return $sql;
2515
    }
2516
2517
    /**
2518
     * Returns the FOREIGN KEY query section dealing with non-standard options
2519
     * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
2520
     *
2521
     * @param ForeignKeyConstraint $foreignKey The foreign key definition.
2522
     *
2523
     * @return string
2524
     */
2525 1432
    public function getAdvancedForeignKeyOptionsSQL(ForeignKeyConstraint $foreignKey)
2526
    {
2527 1432
        $query = '';
2528 1432
        if ($this->supportsForeignKeyOnUpdate() && $foreignKey->hasOption('onUpdate')) {
2529 23
            $query .= ' ON UPDATE ' . $this->getForeignKeyReferentialActionSQL($foreignKey->getOption('onUpdate'));
2530
        }
2531 1432
        if ($foreignKey->hasOption('onDelete')) {
2532 88
            $query .= ' ON DELETE ' . $this->getForeignKeyReferentialActionSQL($foreignKey->getOption('onDelete'));
2533
        }
2534
2535 1432
        return $query;
2536
    }
2537
2538
    /**
2539
     * Returns the given referential action in uppercase if valid, otherwise throws an exception.
2540
     *
2541
     * @param string $action The foreign key referential action.
2542
     *
2543
     * @return string
2544
     *
2545
     * @throws InvalidArgumentException If unknown referential action given.
2546
     */
2547 1491
    public function getForeignKeyReferentialActionSQL($action)
2548
    {
2549 1491
        $upper = strtoupper($action);
2550 128
        switch ($upper) {
2551 1491
            case 'CASCADE':
2552 1017
            case 'SET NULL':
2553 787
            case 'NO ACTION':
2554 603
            case 'RESTRICT':
2555 419
            case 'SET DEFAULT':
2556 1284
                return $upper;
2557
            default:
2558 207
                throw new InvalidArgumentException('Invalid foreign key action: ' . $upper);
2559
        }
2560
    }
2561
2562
    /**
2563
     * Obtains DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
2564
     * of a field declaration to be used in statements like CREATE TABLE.
2565
     *
2566
     * @return string
2567
     *
2568
     * @throws InvalidArgumentException
2569
     */
2570 1387
    public function getForeignKeyBaseDeclarationSQL(ForeignKeyConstraint $foreignKey)
2571
    {
2572 1387
        $sql = '';
2573 1387
        if (strlen($foreignKey->getName()) > 0) {
2574 1180
            $sql .= 'CONSTRAINT ' . $foreignKey->getQuotedName($this) . ' ';
2575
        }
2576 1387
        $sql .= 'FOREIGN KEY (';
2577
2578 1387
        if (count($foreignKey->getLocalColumns()) === 0) {
2579
            throw new InvalidArgumentException("Incomplete definition. 'local' required.");
2580
        }
2581 1387
        if (count($foreignKey->getForeignColumns()) === 0) {
2582
            throw new InvalidArgumentException("Incomplete definition. 'foreign' required.");
2583
        }
2584 1387
        if (strlen($foreignKey->getForeignTableName()) === 0) {
2585
            throw new InvalidArgumentException("Incomplete definition. 'foreignTable' required.");
2586
        }
2587
2588 1387
        return $sql . implode(', ', $foreignKey->getQuotedLocalColumns($this))
2589 1387
            . ') REFERENCES '
2590 1387
            . $foreignKey->getQuotedForeignTableName($this) . ' ('
2591 1387
            . implode(', ', $foreignKey->getQuotedForeignColumns($this)) . ')';
2592
    }
2593
2594
    /**
2595
     * Obtains DBMS specific SQL code portion needed to set the UNIQUE constraint
2596
     * of a field declaration to be used in statements like CREATE TABLE.
2597
     *
2598
     * @return string DBMS specific SQL code portion needed to set the UNIQUE constraint
2599
     *                of a field declaration.
2600
     */
2601
    public function getUniqueFieldDeclarationSQL()
2602
    {
2603
        return 'UNIQUE';
2604
    }
2605
2606
    /**
2607
     * Obtains DBMS specific SQL code portion needed to set the CHARACTER SET
2608
     * of a field declaration to be used in statements like CREATE TABLE.
2609
     *
2610
     * @param string $charset The name of the charset.
2611
     *
2612
     * @return string DBMS specific SQL code portion needed to set the CHARACTER SET
2613
     *                of a field declaration.
2614
     */
2615
    public function getColumnCharsetDeclarationSQL($charset)
0 ignored issues
show
Unused Code introduced by
The parameter $charset is not used and could be removed. ( Ignorable by Annotation )

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

2615
    public function getColumnCharsetDeclarationSQL(/** @scrutinizer ignore-unused */ $charset)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
2616
    {
2617
        return '';
2618
    }
2619
2620
    /**
2621
     * Obtains DBMS specific SQL code portion needed to set the COLLATION
2622
     * of a field declaration to be used in statements like CREATE TABLE.
2623
     *
2624
     * @param string $collation The name of the collation.
2625
     *
2626
     * @return string DBMS specific SQL code portion needed to set the COLLATION
2627
     *                of a field declaration.
2628
     */
2629 94
    public function getColumnCollationDeclarationSQL($collation)
2630
    {
2631 94
        return $this->supportsColumnCollation() ? 'COLLATE ' . $collation : '';
2632
    }
2633
2634
    /**
2635
     * Whether the platform prefers sequences for ID generation.
2636
     * Subclasses should override this method to return TRUE if they prefer sequences.
2637
     *
2638
     * @return bool
2639
     */
2640 46
    public function prefersSequences()
2641
    {
2642 46
        return false;
2643
    }
2644
2645
    /**
2646
     * Whether the platform prefers identity columns (eg. autoincrement) for ID generation.
2647
     * Subclasses should override this method to return TRUE if they prefer identity columns.
2648
     *
2649
     * @return bool
2650
     */
2651 69
    public function prefersIdentityColumns()
2652
    {
2653 69
        return false;
2654
    }
2655
2656
    /**
2657
     * Some platforms need the boolean values to be converted.
2658
     *
2659
     * The default conversion in this implementation converts to integers (false => 0, true => 1).
2660
     *
2661
     * Note: if the input is not a boolean the original input might be returned.
2662
     *
2663
     * There are two contexts when converting booleans: Literals and Prepared Statements.
2664
     * This method should handle the literal case
2665
     *
2666
     * @param mixed $item A boolean or an array of them.
2667
     *
2668
     * @return mixed A boolean database value or an array of them.
2669
     */
2670 330
    public function convertBooleans($item)
2671
    {
2672 330
        if (is_array($item)) {
2673
            foreach ($item as $k => $value) {
2674
                if (! is_bool($value)) {
2675
                    continue;
2676
                }
2677
2678
                $item[$k] = (int) $value;
2679
            }
2680 330
        } elseif (is_bool($item)) {
2681 307
            $item = (int) $item;
2682
        }
2683
2684 330
        return $item;
2685
    }
2686
2687
    /**
2688
     * Some platforms have boolean literals that needs to be correctly converted
2689
     *
2690
     * The default conversion tries to convert value into bool "(bool)$item"
2691
     *
2692
     * @param mixed $item
2693
     *
2694
     * @return bool|null
2695
     */
2696 506
    public function convertFromBoolean($item)
2697
    {
2698 506
        return $item === null ? null: (bool) $item;
2699
    }
2700
2701
    /**
2702
     * This method should handle the prepared statements case. When there is no
2703
     * distinction, it's OK to use the same method.
2704
     *
2705
     * Note: if the input is not a boolean the original input might be returned.
2706
     *
2707
     * @param mixed $item A boolean or an array of them.
2708
     *
2709
     * @return mixed A boolean database value or an array of them.
2710
     */
2711 105
    public function convertBooleansToDatabaseValue($item)
2712
    {
2713 105
        return $this->convertBooleans($item);
2714
    }
2715
2716
    /**
2717
     * Returns the SQL specific for the platform to get the current date.
2718
     *
2719
     * @return string
2720
     */
2721 219
    public function getCurrentDateSQL()
2722
    {
2723 219
        return 'CURRENT_DATE';
2724
    }
2725
2726
    /**
2727
     * Returns the SQL specific for the platform to get the current time.
2728
     *
2729
     * @return string
2730
     */
2731 19
    public function getCurrentTimeSQL()
2732
    {
2733 19
        return 'CURRENT_TIME';
2734
    }
2735
2736
    /**
2737
     * Returns the SQL specific for the platform to get the current timestamp
2738
     *
2739
     * @return string
2740
     */
2741 274
    public function getCurrentTimestampSQL()
2742
    {
2743 274
        return 'CURRENT_TIMESTAMP';
2744
    }
2745
2746
    /**
2747
     * Returns the SQL for a given transaction isolation level Connection constant.
2748
     *
2749
     * @param int $level
2750
     *
2751
     * @return string
2752
     *
2753
     * @throws InvalidArgumentException
2754
     */
2755 138
    protected function _getTransactionIsolationLevelSQL($level)
2756
    {
2757 12
        switch ($level) {
2758 126
            case TransactionIsolationLevel::READ_UNCOMMITTED:
2759 138
                return 'READ UNCOMMITTED';
2760 126
            case TransactionIsolationLevel::READ_COMMITTED:
2761 138
                return 'READ COMMITTED';
2762 126
            case TransactionIsolationLevel::REPEATABLE_READ:
2763 138
                return 'REPEATABLE READ';
2764 126
            case TransactionIsolationLevel::SERIALIZABLE:
2765 138
                return 'SERIALIZABLE';
2766
            default:
2767
                throw new InvalidArgumentException('Invalid isolation level:' . $level);
2768
        }
2769
    }
2770
2771
    /**
2772
     * @return string
2773
     *
2774
     * @throws DBALException If not supported on this platform.
2775
     */
2776 2
    public function getListDatabasesSQL()
2777
    {
2778 2
        throw DBALException::notSupported(__METHOD__);
2779
    }
2780
2781
    /**
2782
     * Returns the SQL statement for retrieving the namespaces defined in the database.
2783
     *
2784
     * @return string
2785
     *
2786
     * @throws DBALException If not supported on this platform.
2787
     */
2788
    public function getListNamespacesSQL()
2789
    {
2790
        throw DBALException::notSupported(__METHOD__);
2791
    }
2792
2793
    /**
2794
     * @param string $database
2795
     *
2796
     * @return string
2797
     *
2798
     * @throws DBALException If not supported on this platform.
2799
     */
2800
    public function getListSequencesSQL($database)
2801
    {
2802
        throw DBALException::notSupported(__METHOD__);
2803
    }
2804
2805
    /**
2806
     * @param string $table
2807
     *
2808
     * @return string
2809
     *
2810
     * @throws DBALException If not supported on this platform.
2811
     */
2812
    public function getListTableConstraintsSQL($table)
2813
    {
2814
        throw DBALException::notSupported(__METHOD__);
2815
    }
2816
2817
    /**
2818
     * @param string      $table
2819
     * @param string|null $database
2820
     *
2821
     * @return string
2822
     *
2823
     * @throws DBALException If not supported on this platform.
2824
     */
2825
    public function getListTableColumnsSQL($table, $database = null)
2826
    {
2827
        throw DBALException::notSupported(__METHOD__);
2828
    }
2829
2830
    /**
2831
     * @return string
2832
     *
2833
     * @throws DBALException If not supported on this platform.
2834
     */
2835
    public function getListTablesSQL()
2836
    {
2837
        throw DBALException::notSupported(__METHOD__);
2838
    }
2839
2840
    /**
2841
     * @return string
2842
     *
2843
     * @throws DBALException If not supported on this platform.
2844
     */
2845
    public function getListUsersSQL()
2846
    {
2847
        throw DBALException::notSupported(__METHOD__);
2848
    }
2849
2850
    /**
2851
     * Returns the SQL to list all views of a database or user.
2852
     *
2853
     * @param string $database
2854
     *
2855
     * @return string
2856
     *
2857
     * @throws DBALException If not supported on this platform.
2858
     */
2859
    public function getListViewsSQL($database)
2860
    {
2861
        throw DBALException::notSupported(__METHOD__);
2862
    }
2863
2864
    /**
2865
     * Returns the list of indexes for the current database.
2866
     *
2867
     * The current database parameter is optional but will always be passed
2868
     * when using the SchemaManager API and is the database the given table is in.
2869
     *
2870
     * Attention: Some platforms only support currentDatabase when they
2871
     * are connected with that database. Cross-database information schema
2872
     * requests may be impossible.
2873
     *
2874
     * @param string $table
2875
     * @param string $currentDatabase
2876
     *
2877
     * @return string
2878
     *
2879
     * @throws DBALException If not supported on this platform.
2880
     */
2881
    public function getListTableIndexesSQL($table, $currentDatabase = null)
0 ignored issues
show
Unused Code introduced by
The parameter $currentDatabase is not used and could be removed. ( Ignorable by Annotation )

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

2881
    public function getListTableIndexesSQL($table, /** @scrutinizer ignore-unused */ $currentDatabase = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
2882
    {
2883
        throw DBALException::notSupported(__METHOD__);
2884
    }
2885
2886
    /**
2887
     * @param string $table
2888
     *
2889
     * @return string
2890
     *
2891
     * @throws DBALException If not supported on this platform.
2892
     */
2893
    public function getListTableForeignKeysSQL($table)
2894
    {
2895
        throw DBALException::notSupported(__METHOD__);
2896
    }
2897
2898
    /**
2899
     * @param string $name
2900
     * @param string $sql
2901
     *
2902
     * @return string
2903
     *
2904
     * @throws DBALException If not supported on this platform.
2905
     */
2906
    public function getCreateViewSQL($name, $sql)
2907
    {
2908
        throw DBALException::notSupported(__METHOD__);
2909
    }
2910
2911
    /**
2912
     * @param string $name
2913
     *
2914
     * @return string
2915
     *
2916
     * @throws DBALException If not supported on this platform.
2917
     */
2918
    public function getDropViewSQL($name)
2919
    {
2920
        throw DBALException::notSupported(__METHOD__);
2921
    }
2922
2923
    /**
2924
     * Returns the SQL snippet to drop an existing sequence.
2925
     *
2926
     * @param Sequence|string $sequence
2927
     *
2928
     * @return string
2929
     *
2930
     * @throws DBALException If not supported on this platform.
2931
     */
2932
    public function getDropSequenceSQL($sequence)
0 ignored issues
show
Unused Code introduced by
The parameter $sequence is not used and could be removed. ( Ignorable by Annotation )

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

2932
    public function getDropSequenceSQL(/** @scrutinizer ignore-unused */ $sequence)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
2933
    {
2934
        throw DBALException::notSupported(__METHOD__);
2935
    }
2936
2937
    /**
2938
     * @param string $sequenceName
2939
     *
2940
     * @return string
2941
     *
2942
     * @throws DBALException If not supported on this platform.
2943
     */
2944
    public function getSequenceNextValSQL($sequenceName)
0 ignored issues
show
Unused Code introduced by
The parameter $sequenceName is not used and could be removed. ( Ignorable by Annotation )

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

2944
    public function getSequenceNextValSQL(/** @scrutinizer ignore-unused */ $sequenceName)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
2945
    {
2946
        throw DBALException::notSupported(__METHOD__);
2947
    }
2948
2949
    /**
2950
     * Returns the SQL to create a new database.
2951
     *
2952
     * @param string $database The name of the database that should be created.
2953
     *
2954
     * @return string
2955
     *
2956
     * @throws DBALException If not supported on this platform.
2957
     */
2958 23
    public function getCreateDatabaseSQL($database)
2959
    {
2960 23
        throw DBALException::notSupported(__METHOD__);
2961
    }
2962
2963
    /**
2964
     * Returns the SQL to set the transaction isolation level.
2965
     *
2966
     * @param int $level
2967
     *
2968
     * @return string
2969
     *
2970
     * @throws DBALException If not supported on this platform.
2971
     */
2972
    public function getSetTransactionIsolationSQL($level)
2973
    {
2974
        throw DBALException::notSupported(__METHOD__);
2975
    }
2976
2977
    /**
2978
     * Obtains DBMS specific SQL to be used to create datetime fields in
2979
     * statements like CREATE TABLE.
2980
     *
2981
     * @param mixed[] $fieldDeclaration
2982
     *
2983
     * @return string
2984
     *
2985
     * @throws DBALException If not supported on this platform.
2986
     */
2987
    public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
2988
    {
2989
        throw DBALException::notSupported(__METHOD__);
2990
    }
2991
2992
    /**
2993
     * Obtains DBMS specific SQL to be used to create datetime with timezone offset fields.
2994
     *
2995
     * @param mixed[] $fieldDeclaration
2996
     *
2997
     * @return string
2998
     */
2999 240
    public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
3000
    {
3001 240
        return $this->getDateTimeTypeDeclarationSQL($fieldDeclaration);
3002
    }
3003
3004
    /**
3005
     * Obtains DBMS specific SQL to be used to create date fields in statements
3006
     * like CREATE TABLE.
3007
     *
3008
     * @param mixed[] $fieldDeclaration
3009
     *
3010
     * @return string
3011
     *
3012
     * @throws DBALException If not supported on this platform.
3013
     */
3014
    public function getDateTypeDeclarationSQL(array $fieldDeclaration)
3015
    {
3016
        throw DBALException::notSupported(__METHOD__);
3017
    }
3018
3019
    /**
3020
     * Obtains DBMS specific SQL to be used to create time fields in statements
3021
     * like CREATE TABLE.
3022
     *
3023
     * @param mixed[] $fieldDeclaration
3024
     *
3025
     * @return string
3026
     *
3027
     * @throws DBALException If not supported on this platform.
3028
     */
3029
    public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
3030
    {
3031
        throw DBALException::notSupported(__METHOD__);
3032
    }
3033
3034
    /**
3035
     * @param mixed[] $fieldDeclaration
3036
     *
3037
     * @return string
3038
     */
3039 1106
    public function getFloatDeclarationSQL(array $fieldDeclaration)
3040
    {
3041 1106
        return 'DOUBLE PRECISION';
3042
    }
3043
3044
    /**
3045
     * Gets the default transaction isolation level of the platform.
3046
     *
3047
     * @see TransactionIsolationLevel
3048
     *
3049
     * @return int The default isolation level.
3050
     */
3051
    public function getDefaultTransactionIsolationLevel()
3052
    {
3053
        return TransactionIsolationLevel::READ_COMMITTED;
3054
    }
3055
3056
    /* supports*() methods */
3057
3058
    /**
3059
     * Whether the platform supports sequences.
3060
     *
3061
     * @return bool
3062
     */
3063 113
    public function supportsSequences()
3064
    {
3065 113
        return false;
3066
    }
3067
3068
    /**
3069
     * Whether the platform supports identity columns.
3070
     *
3071
     * Identity columns are columns that receive an auto-generated value from the
3072
     * database on insert of a row.
3073
     *
3074
     * @return bool
3075
     */
3076 27
    public function supportsIdentityColumns()
3077
    {
3078 27
        return false;
3079
    }
3080
3081
    /**
3082
     * Whether the platform emulates identity columns through sequences.
3083
     *
3084
     * Some platforms that do not support identity columns natively
3085
     * but support sequences can emulate identity columns by using
3086
     * sequences.
3087
     *
3088
     * @return bool
3089
     */
3090 177
    public function usesSequenceEmulatedIdentityColumns()
3091
    {
3092 177
        return false;
3093
    }
3094
3095
    /**
3096
     * Gets the sequence name prefix based on table information.
3097
     */
3098 460
    public function getSequencePrefix(string $tableName, ?string $schemaName = null) : string
3099
    {
3100 460
        if ($schemaName === null) {
3101 230
            return $tableName;
3102
        }
3103
3104
        // Prepend the schema name to the table name if there is one
3105 230
        return ! $this->supportsSchemas() && $this->canEmulateSchemas()
3106 23
            ? $schemaName . '__' . $tableName
3107 230
            : $schemaName . '.' . $tableName;
3108
    }
3109
3110
    /**
3111
     * Returns the name of the sequence for a particular identity column in a particular table.
3112
     *
3113
     * @see usesSequenceEmulatedIdentityColumns
3114
     *
3115
     * @param string $tableName  The name of the table to return the sequence name for.
3116
     * @param string $columnName The name of the identity column in the table to return the sequence name for.
3117
     *
3118
     * @return string
3119
     *
3120
     * @throws DBALException If not supported on this platform.
3121
     */
3122 161
    public function getIdentitySequenceName($tableName, $columnName)
3123
    {
3124 161
        throw DBALException::notSupported(__METHOD__);
3125
    }
3126
3127
    /**
3128
     * Whether the platform supports indexes.
3129
     *
3130
     * @return bool
3131
     */
3132 23
    public function supportsIndexes()
3133
    {
3134 23
        return true;
3135
    }
3136
3137
    /**
3138
     * Whether the platform supports partial indexes.
3139
     *
3140
     * @return bool
3141
     */
3142 2353
    public function supportsPartialIndexes()
3143
    {
3144 2353
        return false;
3145
    }
3146
3147
    /**
3148
     * Whether the platform supports indexes with column length definitions.
3149
     */
3150 3424
    public function supportsColumnLengthIndexes() : bool
3151
    {
3152 3424
        return false;
3153
    }
3154
3155
    /**
3156
     * Whether the platform supports altering tables.
3157
     *
3158
     * @return bool
3159
     */
3160 46
    public function supportsAlterTable()
3161
    {
3162 46
        return true;
3163
    }
3164
3165
    /**
3166
     * Whether the platform supports transactions.
3167
     *
3168
     * @return bool
3169
     */
3170 23
    public function supportsTransactions()
3171
    {
3172 23
        return true;
3173
    }
3174
3175
    /**
3176
     * Whether the platform supports savepoints.
3177
     *
3178
     * @return bool
3179
     */
3180 253
    public function supportsSavepoints()
3181
    {
3182 253
        return true;
3183
    }
3184
3185
    /**
3186
     * Whether the platform supports releasing savepoints.
3187
     *
3188
     * @return bool
3189
     */
3190 44
    public function supportsReleaseSavepoints()
3191
    {
3192 44
        return $this->supportsSavepoints();
3193
    }
3194
3195
    /**
3196
     * Whether the platform supports primary key constraints.
3197
     *
3198
     * @return bool
3199
     */
3200 23
    public function supportsPrimaryConstraints()
3201
    {
3202 23
        return true;
3203
    }
3204
3205
    /**
3206
     * Whether the platform supports foreign key constraints.
3207
     *
3208
     * @return bool
3209
     */
3210 6954
    public function supportsForeignKeyConstraints()
3211
    {
3212 6954
        return true;
3213
    }
3214
3215
    /**
3216
     * Whether this platform supports onUpdate in foreign key constraints.
3217
     *
3218
     * @return bool
3219
     */
3220 1455
    public function supportsForeignKeyOnUpdate()
3221
    {
3222 1455
        return $this->supportsForeignKeyConstraints();
3223
    }
3224
3225
    /**
3226
     * Whether the platform supports database schemas.
3227
     *
3228
     * @return bool
3229
     */
3230 345
    public function supportsSchemas()
3231
    {
3232 345
        return false;
3233
    }
3234
3235
    /**
3236
     * Whether this platform can emulate schemas.
3237
     *
3238
     * Platforms that either support or emulate schemas don't automatically
3239
     * filter a schema for the namespaced elements in {@link
3240
     * AbstractManager#createSchema}.
3241
     *
3242
     * @return bool
3243
     */
3244 161
    public function canEmulateSchemas()
3245
    {
3246 161
        return false;
3247
    }
3248
3249
    /**
3250
     * Returns the default schema name.
3251
     *
3252
     * @return string
3253
     *
3254
     * @throws DBALException If not supported on this platform.
3255
     */
3256
    public function getDefaultSchemaName()
3257
    {
3258
        throw DBALException::notSupported(__METHOD__);
3259
    }
3260
3261
    /**
3262
     * Whether this platform supports create database.
3263
     *
3264
     * Some databases don't allow to create and drop databases at all or only with certain tools.
3265
     *
3266
     * @return bool
3267
     */
3268 77
    public function supportsCreateDropDatabase()
3269
    {
3270 77
        return true;
3271
    }
3272
3273
    /**
3274
     * Whether the platform supports getting the affected rows of a recent update/delete type query.
3275
     *
3276
     * @return bool
3277
     */
3278 23
    public function supportsGettingAffectedRows()
3279
    {
3280 23
        return true;
3281
    }
3282
3283
    /**
3284
     * Whether this platform support to add inline column comments as postfix.
3285
     *
3286
     * @return bool
3287
     */
3288 2678
    public function supportsInlineColumnComments()
3289
    {
3290 2678
        return false;
3291
    }
3292
3293
    /**
3294
     * Whether this platform support the proprietary syntax "COMMENT ON asset".
3295
     *
3296
     * @return bool
3297
     */
3298 4861
    public function supportsCommentOnStatement()
3299
    {
3300 4861
        return false;
3301
    }
3302
3303
    /**
3304
     * Does this platform have native guid type.
3305
     *
3306
     * @return bool
3307
     */
3308 6925
    public function hasNativeGuidType()
3309
    {
3310 6925
        return false;
3311
    }
3312
3313
    /**
3314
     * Does this platform have native JSON type.
3315
     *
3316
     * @return bool
3317
     */
3318 8050
    public function hasNativeJsonType()
3319
    {
3320 8050
        return false;
3321
    }
3322
3323
    /**
3324
     * @deprecated
3325
     *
3326
     * @return string
3327
     *
3328
     * @todo Remove in 3.0
3329
     */
3330
    public function getIdentityColumnNullInsertSQL()
3331
    {
3332
        return '';
3333
    }
3334
3335
    /**
3336
     * Whether this platform supports views.
3337
     *
3338
     * @return bool
3339
     */
3340 23
    public function supportsViews()
3341
    {
3342 23
        return true;
3343
    }
3344
3345
    /**
3346
     * Does this platform support column collation?
3347
     *
3348
     * @return bool
3349
     */
3350
    public function supportsColumnCollation()
3351
    {
3352
        return false;
3353
    }
3354
3355
    /**
3356
     * Gets the format string, as accepted by the date() function, that describes
3357
     * the format of a stored datetime value of this platform.
3358
     *
3359
     * @return string The format string.
3360
     */
3361 414
    public function getDateTimeFormatString()
3362
    {
3363 414
        return 'Y-m-d H:i:s';
3364
    }
3365
3366
    /**
3367
     * Gets the format string, as accepted by the date() function, that describes
3368
     * the format of a stored datetime with timezone value of this platform.
3369
     *
3370
     * @return string The format string.
3371
     */
3372 154
    public function getDateTimeTzFormatString()
3373
    {
3374 154
        return 'Y-m-d H:i:s';
3375
    }
3376
3377
    /**
3378
     * Gets the format string, as accepted by the date() function, that describes
3379
     * the format of a stored date value of this platform.
3380
     *
3381
     * @return string The format string.
3382
     */
3383 159
    public function getDateFormatString()
3384
    {
3385 159
        return 'Y-m-d';
3386
    }
3387
3388
    /**
3389
     * Gets the format string, as accepted by the date() function, that describes
3390
     * the format of a stored time value of this platform.
3391
     *
3392
     * @return string The format string.
3393
     */
3394 136
    public function getTimeFormatString()
3395
    {
3396 136
        return 'H:i:s';
3397
    }
3398
3399
    /**
3400
     * Adds an driver-specific LIMIT clause to the query.
3401
     *
3402
     * @param string   $query
3403
     * @param int|null $limit
3404
     * @param int|null $offset
3405
     *
3406
     * @return string
3407
     *
3408
     * @throws DBALException
3409
     */
3410 1633
    final public function modifyLimitQuery($query, $limit, $offset = null)
3411
    {
3412 1633
        if ($limit !== null) {
3413 1242
            $limit = (int) $limit;
3414
        }
3415
3416 1633
        $offset = (int) $offset;
3417
3418 1633
        if ($offset < 0) {
3419
            throw new DBALException(sprintf(
3420
                'Offset must be a positive integer or zero, %d given',
3421
                $offset
3422
            ));
3423
        }
3424
3425 1633
        if ($offset > 0 && ! $this->supportsLimitOffset()) {
3426
            throw new DBALException(sprintf(
3427
                'Platform %s does not support offset values in limit queries.',
3428
                $this->getName()
3429
            ));
3430
        }
3431
3432 1633
        return $this->doModifyLimitQuery($query, $limit, $offset);
3433
    }
3434
3435
    /**
3436
     * Adds an platform-specific LIMIT clause to the query.
3437
     *
3438
     * @param string   $query
3439
     * @param int|null $limit
3440
     * @param int|null $offset
3441
     *
3442
     * @return string
3443
     */
3444 263
    protected function doModifyLimitQuery($query, $limit, $offset)
3445
    {
3446 263
        if ($limit !== null) {
3447 187
            $query .= ' LIMIT ' . $limit;
3448
        }
3449
3450 263
        if ($offset > 0) {
3451 49
            $query .= ' OFFSET ' . $offset;
3452
        }
3453
3454 263
        return $query;
3455
    }
3456
3457
    /**
3458
     * Whether the database platform support offsets in modify limit clauses.
3459
     *
3460
     * @return bool
3461
     */
3462 368
    public function supportsLimitOffset()
3463
    {
3464 368
        return true;
3465
    }
3466
3467
    /**
3468
     * Gets the character casing of a column in an SQL result set of this platform.
3469
     *
3470
     * @param string $column The column name for which to get the correct character casing.
3471
     *
3472
     * @return string The column name in the character casing used in SQL result sets.
3473
     */
3474
    public function getSQLResultCasing($column)
3475
    {
3476
        return $column;
3477
    }
3478
3479
    /**
3480
     * Makes any fixes to a name of a schema element (table, sequence, ...) that are required
3481
     * by restrictions of the platform, like a maximum length.
3482
     *
3483
     * @param string $schemaElementName
3484
     *
3485
     * @return string
3486
     */
3487
    public function fixSchemaElementName($schemaElementName)
3488
    {
3489
        return $schemaElementName;
3490
    }
3491
3492
    /**
3493
     * Maximum length of any given database identifier, like tables or column names.
3494
     *
3495
     * @return int
3496
     */
3497 383
    public function getMaxIdentifierLength()
3498
    {
3499 383
        return 63;
3500
    }
3501
3502
    /**
3503
     * Returns the insert SQL for an empty insert statement.
3504
     *
3505
     * @param string $tableName
3506
     * @param string $identifierColumnName
3507
     *
3508
     * @return string
3509
     */
3510 18
    public function getEmptyIdentityInsertSQL($tableName, $identifierColumnName)
3511
    {
3512 18
        return 'INSERT INTO ' . $tableName . ' (' . $identifierColumnName . ') VALUES (null)';
3513
    }
3514
3515
    /**
3516
     * Generates a Truncate Table SQL statement for a given table.
3517
     *
3518
     * Cascade is not supported on many platforms but would optionally cascade the truncate by
3519
     * following the foreign keys.
3520
     *
3521
     * @param string $tableName
3522
     * @param bool   $cascade
3523
     *
3524
     * @return string
3525
     */
3526 195
    public function getTruncateTableSQL($tableName, $cascade = false)
0 ignored issues
show
Unused Code introduced by
The parameter $cascade is not used and could be removed. ( Ignorable by Annotation )

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

3526
    public function getTruncateTableSQL($tableName, /** @scrutinizer ignore-unused */ $cascade = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
3527
    {
3528 195
        $tableIdentifier = new Identifier($tableName);
3529
3530 195
        return 'TRUNCATE ' . $tableIdentifier->getQuotedName($this);
3531
    }
3532
3533
    /**
3534
     * This is for test reasons, many vendors have special requirements for dummy statements.
3535
     *
3536
     * @return string
3537
     */
3538 183
    public function getDummySelectSQL()
3539
    {
3540 183
        $expression = func_num_args() > 0 ? func_get_arg(0) : '1';
3541
3542 183
        return sprintf('SELECT %s', $expression);
3543
    }
3544
3545
    /**
3546
     * Returns the SQL to create a new savepoint.
3547
     *
3548
     * @param string $savepoint
3549
     *
3550
     * @return string
3551
     */
3552 23
    public function createSavePoint($savepoint)
3553
    {
3554 23
        return 'SAVEPOINT ' . $savepoint;
3555
    }
3556
3557
    /**
3558
     * Returns the SQL to release a savepoint.
3559
     *
3560
     * @param string $savepoint
3561
     *
3562
     * @return string
3563
     */
3564 21
    public function releaseSavePoint($savepoint)
3565
    {
3566 21
        return 'RELEASE SAVEPOINT ' . $savepoint;
3567
    }
3568
3569
    /**
3570
     * Returns the SQL to rollback a savepoint.
3571
     *
3572
     * @param string $savepoint
3573
     *
3574
     * @return string
3575
     */
3576 23
    public function rollbackSavePoint($savepoint)
3577
    {
3578 23
        return 'ROLLBACK TO SAVEPOINT ' . $savepoint;
3579
    }
3580
3581
    /**
3582
     * Returns the keyword list instance of this platform.
3583
     *
3584
     * @return KeywordList
3585
     *
3586
     * @throws DBALException If no keyword list is specified.
3587
     */
3588 16751
    final public function getReservedKeywordsList()
3589
    {
3590
        // Check for an existing instantiation of the keywords class.
3591 16751
        if ($this->_keywords !== null) {
3592 15946
            return $this->_keywords;
3593
        }
3594
3595 12513
        $class    = $this->getReservedKeywordsClass();
3596 12513
        $keywords = new $class();
3597 12513
        if (! $keywords instanceof KeywordList) {
3598
            throw DBALException::notSupported(__METHOD__);
3599
        }
3600
3601
        // Store the instance so it doesn't need to be generated on every request.
3602 12513
        $this->_keywords = $keywords;
3603
3604 12513
        return $keywords;
3605
    }
3606
3607
    /**
3608
     * Returns the class name of the reserved keywords list.
3609
     *
3610
     * @return string
3611
     *
3612
     * @throws DBALException If not supported on this platform.
3613
     */
3614
    protected function getReservedKeywordsClass()
3615
    {
3616
        throw DBALException::notSupported(__METHOD__);
3617
    }
3618
3619
    /**
3620
     * Quotes a literal string.
3621
     * This method is NOT meant to fix SQL injections!
3622
     * It is only meant to escape this platform's string literal
3623
     * quote character inside the given literal string.
3624
     *
3625
     * @param string $str The literal string to be quoted.
3626
     *
3627
     * @return string The quoted literal string.
3628
     */
3629 6946
    public function quoteStringLiteral($str)
3630
    {
3631 6946
        $c = $this->getStringLiteralQuoteCharacter();
3632
3633 6946
        return $c . str_replace($c, $c . $c, $str) . $c;
3634
    }
3635
3636
    /**
3637
     * Gets the character used for string literal quoting.
3638
     *
3639
     * @return string
3640
     */
3641 7176
    public function getStringLiteralQuoteCharacter()
3642
    {
3643 7176
        return "'";
3644
    }
3645
3646
    /**
3647
     * Escapes metacharacters in a string intended to be used with a LIKE
3648
     * operator.
3649
     *
3650
     * @param string $inputString a literal, unquoted string
3651
     * @param string $escapeChar  should be reused by the caller in the LIKE
3652
     *                            expression.
3653
     */
3654 253
    final public function escapeStringForLike(string $inputString, string $escapeChar) : string
3655
    {
3656 253
        return preg_replace(
3657 253
            '~([' . preg_quote($this->getLikeWildcardCharacters() . $escapeChar, '~') . '])~u',
3658 253
            addcslashes($escapeChar, '\\') . '$1',
3659 253
            $inputString
3660
        );
3661
    }
3662
3663 253
    protected function getLikeWildcardCharacters() : string
3664
    {
3665 253
        return '%_';
3666
    }
3667
}
3668