Failed Conditions
Pull Request — 2.10 (#3762)
by Benjamin
09:16
created

AbstractPlatform::getDateAddMinutesExpression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

343
    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...
344
    {
345
        throw DBALException::notSupported('VARCHARs not supported by Platform.');
346
    }
347
348
    /**
349
     * Returns the SQL snippet used to declare a BINARY/VARBINARY column type.
350
     *
351
     * @param int  $length The length of the column.
352
     * @param bool $fixed  Whether the column length is fixed.
353
     *
354
     * @return string
355
     *
356
     * @throws DBALException If not supported on this platform.
357
     */
358
    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

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

810
            $expression .= /** @scrutinizer ignore-type */ $char . ' ';
Loading history...
811
        }
812
813 59257
        if ($mode || $char !== false) {
814 59226
            $expression .= 'FROM ';
815
        }
816
817 59257
        return 'TRIM(' . $expression . $str . ')';
818
    }
819
820
    /**
821
     * Returns the SQL snippet to trim trailing space characters from the expression.
822
     *
823
     * @param string $str Literal string or column name.
824
     *
825
     * @return string
826
     */
827 28016
    public function getRtrimExpression($str)
828
    {
829 28016
        return 'RTRIM(' . $str . ')';
830
    }
831
832
    /**
833
     * Returns the SQL snippet to trim leading space characters from the expression.
834
     *
835
     * @param string $str Literal string or column name.
836
     *
837
     * @return string
838
     */
839 28016
    public function getLtrimExpression($str)
840
    {
841 28016
        return 'LTRIM(' . $str . ')';
842
    }
843
844
    /**
845
     * Returns the SQL snippet to change all characters from the expression to uppercase,
846
     * according to the current character set mapping.
847
     *
848
     * @param string $str Literal string or column name.
849
     *
850
     * @return string
851
     */
852
    public function getUpperExpression($str)
853
    {
854
        return 'UPPER(' . $str . ')';
855
    }
856
857
    /**
858
     * Returns the SQL snippet to change all characters from the expression to lowercase,
859
     * according to the current character set mapping.
860
     *
861
     * @param string $str Literal string or column name.
862
     *
863
     * @return string
864
     */
865
    public function getLowerExpression($str)
866
    {
867
        return 'LOWER(' . $str . ')';
868
    }
869
870
    /**
871
     * Returns the SQL snippet to get the position of the first occurrence of substring $substr in string $str.
872
     *
873
     * @param string    $str      Literal string.
874
     * @param string    $substr   Literal string to find.
875
     * @param int|false $startPos Position to start at, beginning of string by default.
876
     *
877
     * @return string
878
     *
879
     * @throws DBALException If not supported on this platform.
880
     */
881
    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

881
    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

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

1052
    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

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

1310
    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 $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

1310
    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...
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

1310
    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...
1311
    {
1312
        throw DBALException::notSupported(__METHOD__);
1313
    }
1314
1315
    /**
1316
     * Returns the SQL bit AND comparison expression.
1317
     *
1318
     * @param string $value1
1319
     * @param string $value2
1320
     *
1321
     * @return string
1322
     */
1323 63852
    public function getBitAndComparisonExpression($value1, $value2)
1324
    {
1325 63852
        return '(' . $value1 . ' & ' . $value2 . ')';
1326
    }
1327
1328
    /**
1329
     * Returns the SQL bit OR comparison expression.
1330
     *
1331
     * @param string $value1
1332
     * @param string $value2
1333
     *
1334
     * @return string
1335
     */
1336 63852
    public function getBitOrComparisonExpression($value1, $value2)
1337
    {
1338 63852
        return '(' . $value1 . ' | ' . $value2 . ')';
1339
    }
1340
1341
    /**
1342
     * Returns the FOR UPDATE expression.
1343
     *
1344
     * @return string
1345
     */
1346 55275
    public function getForUpdateSQL()
1347
    {
1348 55275
        return 'FOR UPDATE';
1349
    }
1350
1351
    /**
1352
     * Honors that some SQL vendors such as MsSql use table hints for locking instead of the ANSI SQL FOR UPDATE specification.
1353
     *
1354
     * @param string   $fromClause The FROM clause to append the hint for the given lock mode to.
1355
     * @param int|null $lockMode   One of the Doctrine\DBAL\LockMode::* constants. If null is given, nothing will
1356
     *                             be appended to the FROM clause.
1357
     *
1358
     * @return string
1359
     */
1360 55275
    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

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

1739
    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...
1740
    {
1741
        throw DBALException::notSupported(__METHOD__);
1742
    }
1743
1744
    /**
1745
     * Returns the SQL to change a sequence on this platform.
1746
     *
1747
     * @return string
1748
     *
1749
     * @throws DBALException If not supported on this platform.
1750
     */
1751
    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

1751
    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...
1752
    {
1753
        throw DBALException::notSupported(__METHOD__);
1754
    }
1755
1756
    /**
1757
     * Returns the SQL to create a constraint on a table on this platform.
1758
     *
1759
     * @param Table|string $table
1760
     *
1761
     * @return string
1762
     *
1763
     * @throws InvalidArgumentException
1764
     */
1765 58031
    public function getCreateConstraintSQL(Constraint $constraint, $table)
1766
    {
1767 58031
        if ($table instanceof Table) {
1768
            $table = $table->getQuotedName($this);
1769
        }
1770
1771 58031
        $query = 'ALTER TABLE ' . $table . ' ADD CONSTRAINT ' . $constraint->getQuotedName($this);
1772
1773 58031
        $columnList = '(' . implode(', ', $constraint->getQuotedColumns($this)) . ')';
1774
1775 58031
        $referencesClause = '';
1776 58031
        if ($constraint instanceof Index) {
1777 58031
            if ($constraint->isPrimary()) {
1778 58031
                $query .= ' PRIMARY KEY';
1779 57927
            } elseif ($constraint->isUnique()) {
1780 57927
                $query .= ' UNIQUE';
1781
            } else {
1782
                throw new InvalidArgumentException(
1783 58031
                    'Can only create primary or unique constraints, no common indexes with getCreateConstraintSQL().'
1784
                );
1785
            }
1786 57927
        } elseif ($constraint instanceof ForeignKeyConstraint) {
1787 57927
            $query .= ' FOREIGN KEY';
1788
1789 57927
            $referencesClause = ' REFERENCES ' . $constraint->getQuotedForeignTableName($this) .
1790 57927
                ' (' . implode(', ', $constraint->getQuotedForeignColumns($this)) . ')';
1791
        }
1792 58031
        $query .= ' ' . $columnList . $referencesClause;
1793
1794 58031
        return $query;
1795
    }
1796
1797
    /**
1798
     * Returns the SQL to create an index on a table on this platform.
1799
     *
1800
     * @param Table|string $table The name of the table on which the index is to be created.
1801
     *
1802
     * @return string
1803
     *
1804
     * @throws InvalidArgumentException
1805
     */
1806 62946
    public function getCreateIndexSQL(Index $index, $table)
1807
    {
1808 62946
        if ($table instanceof Table) {
1809 61533
            $table = $table->getQuotedName($this);
1810
        }
1811 62946
        $name    = $index->getQuotedName($this);
1812 62946
        $columns = $index->getColumns();
1813
1814 62946
        if (count($columns) === 0) {
1815
            throw new InvalidArgumentException("Incomplete definition. 'columns' required.");
1816
        }
1817
1818 62946
        if ($index->isPrimary()) {
1819 56184
            return $this->getCreatePrimaryKeySQL($index, $table);
1820
        }
1821
1822 62870
        $query  = 'CREATE ' . $this->getCreateIndexSQLFlags($index) . 'INDEX ' . $name . ' ON ' . $table;
1823 62870
        $query .= ' (' . $this->getIndexFieldDeclarationListSQL($index) . ')' . $this->getPartialIndexSQL($index);
1824
1825 62870
        return $query;
1826
    }
1827
1828
    /**
1829
     * Adds condition for partial index.
1830
     *
1831
     * @return string
1832
     */
1833 64482
    protected function getPartialIndexSQL(Index $index)
1834
    {
1835 64482
        if ($this->supportsPartialIndexes() && $index->hasOption('where')) {
1836 48175
            return ' WHERE ' . $index->getOption('where');
1837
        }
1838
1839 64462
        return '';
1840
    }
1841
1842
    /**
1843
     * Adds additional flags for index generation.
1844
     *
1845
     * @return string
1846
     */
1847 61042
    protected function getCreateIndexSQLFlags(Index $index)
1848
    {
1849 61042
        return $index->isUnique() ? 'UNIQUE ' : '';
1850
    }
1851
1852
    /**
1853
     * Returns the SQL to create an unnamed primary key constraint.
1854
     *
1855
     * @param Table|string $table
1856
     *
1857
     * @return string
1858
     */
1859 59018
    public function getCreatePrimaryKeySQL(Index $index, $table)
1860
    {
1861 59018
        if ($table instanceof Table) {
1862
            $table = $table->getQuotedName($this);
1863
        }
1864
1865 59018
        return 'ALTER TABLE ' . $table . ' ADD PRIMARY KEY (' . $this->getIndexFieldDeclarationListSQL($index) . ')';
1866
    }
1867
1868
    /**
1869
     * Returns the SQL to create a named schema.
1870
     *
1871
     * @param string $schemaName
1872
     *
1873
     * @return string
1874
     *
1875
     * @throws DBALException If not supported on this platform.
1876
     */
1877 57340
    public function getCreateSchemaSQL($schemaName)
0 ignored issues
show
Unused Code introduced by
The parameter $schemaName 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

1877
    public function getCreateSchemaSQL(/** @scrutinizer ignore-unused */ $schemaName)

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...
1878
    {
1879 57340
        throw DBALException::notSupported(__METHOD__);
1880
    }
1881
1882
    /**
1883
     * Quotes a string so that it can be safely used as a table or column name,
1884
     * even if it is a reserved word of the platform. This also detects identifier
1885
     * chains separated by dot and quotes them independently.
1886
     *
1887
     * NOTE: Just because you CAN use quoted identifiers doesn't mean
1888
     * you SHOULD use them. In general, they end up causing way more
1889
     * problems than they solve.
1890
     *
1891
     * @param string $str The identifier name to be quoted.
1892
     *
1893
     * @return string The quoted identifier string.
1894
     */
1895 66121
    public function quoteIdentifier($str)
1896
    {
1897 66121
        if (strpos($str, '.') !== false) {
1898 59185
            $parts = array_map([$this, 'quoteSingleIdentifier'], explode('.', $str));
1899
1900 59185
            return implode('.', $parts);
1901
        }
1902
1903 66121
        return $this->quoteSingleIdentifier($str);
1904
    }
1905
1906
    /**
1907
     * Quotes a single identifier (no dot chain separation).
1908
     *
1909
     * @param string $str The identifier name to be quoted.
1910
     *
1911
     * @return string The quoted identifier string.
1912
     */
1913 66429
    public function quoteSingleIdentifier($str)
1914
    {
1915 66429
        $c = $this->getIdentifierQuoteCharacter();
1916
1917 66429
        return $c . str_replace($c, $c . $c, $str) . $c;
1918
    }
1919
1920
    /**
1921
     * Returns the SQL to create a new foreign key.
1922
     *
1923
     * @param ForeignKeyConstraint $foreignKey The foreign key constraint.
1924
     * @param Table|string         $table      The name of the table on which the foreign key is to be created.
1925
     *
1926
     * @return string
1927
     */
1928 63668
    public function getCreateForeignKeySQL(ForeignKeyConstraint $foreignKey, $table)
1929
    {
1930 63668
        if ($table instanceof Table) {
1931 55977
            $table = $table->getQuotedName($this);
1932
        }
1933
1934 63668
        return 'ALTER TABLE ' . $table . ' ADD ' . $this->getForeignKeyDeclarationSQL($foreignKey);
1935
    }
1936
1937
    /**
1938
     * Gets the SQL statements for altering an existing table.
1939
     *
1940
     * This method returns an array of SQL statements, since some platforms need several statements.
1941
     *
1942
     * @return string[]
1943
     *
1944
     * @throws DBALException If not supported on this platform.
1945
     */
1946
    public function getAlterTableSQL(TableDiff $diff)
1947
    {
1948
        throw DBALException::notSupported(__METHOD__);
1949
    }
1950
1951
    /**
1952
     * @param mixed[] $columnSql
1953
     *
1954
     * @return bool
1955
     */
1956 62792
    protected function onSchemaAlterTableAddColumn(Column $column, TableDiff $diff, &$columnSql)
1957
    {
1958 62792
        if ($this->_eventManager === null) {
1959 58145
            return false;
1960
        }
1961
1962 62472
        if (! $this->_eventManager->hasListeners(Events::onSchemaAlterTableAddColumn)) {
1963 62400
            return false;
1964
        }
1965
1966 57797
        $eventArgs = new SchemaAlterTableAddColumnEventArgs($column, $diff, $this);
1967 57797
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTableAddColumn, $eventArgs);
1968
1969 57797
        $columnSql = array_merge($columnSql, $eventArgs->getSql());
1970
1971 57797
        return $eventArgs->isDefaultPrevented();
1972
    }
1973
1974
    /**
1975
     * @param string[] $columnSql
1976
     *
1977
     * @return bool
1978
     */
1979 61740
    protected function onSchemaAlterTableRemoveColumn(Column $column, TableDiff $diff, &$columnSql)
1980
    {
1981 61740
        if ($this->_eventManager === null) {
1982 58053
            return false;
1983
        }
1984
1985 61512
        if (! $this->_eventManager->hasListeners(Events::onSchemaAlterTableRemoveColumn)) {
1986 61440
            return false;
1987
        }
1988
1989 57797
        $eventArgs = new SchemaAlterTableRemoveColumnEventArgs($column, $diff, $this);
1990 57797
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTableRemoveColumn, $eventArgs);
1991
1992 57797
        $columnSql = array_merge($columnSql, $eventArgs->getSql());
1993
1994 57797
        return $eventArgs->isDefaultPrevented();
1995
    }
1996
1997
    /**
1998
     * @param string[] $columnSql
1999
     *
2000
     * @return bool
2001
     */
2002 62908
    protected function onSchemaAlterTableChangeColumn(ColumnDiff $columnDiff, TableDiff $diff, &$columnSql)
2003
    {
2004 62908
        if ($this->_eventManager === null) {
2005 59414
            return false;
2006
        }
2007
2008 62244
        if (! $this->_eventManager->hasListeners(Events::onSchemaAlterTableChangeColumn)) {
2009 62172
            return false;
2010
        }
2011
2012 57797
        $eventArgs = new SchemaAlterTableChangeColumnEventArgs($columnDiff, $diff, $this);
2013 57797
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTableChangeColumn, $eventArgs);
2014
2015 57797
        $columnSql = array_merge($columnSql, $eventArgs->getSql());
2016
2017 57797
        return $eventArgs->isDefaultPrevented();
2018
    }
2019
2020
    /**
2021
     * @param string   $oldColumnName
2022
     * @param string[] $columnSql
2023
     *
2024
     * @return bool
2025
     */
2026 61637
    protected function onSchemaAlterTableRenameColumn($oldColumnName, Column $column, TableDiff $diff, &$columnSql)
2027
    {
2028 61637
        if ($this->_eventManager === null) {
2029 57332
            return false;
2030
        }
2031
2032 61405
        if (! $this->_eventManager->hasListeners(Events::onSchemaAlterTableRenameColumn)) {
2033 61333
            return false;
2034
        }
2035
2036 57797
        $eventArgs = new SchemaAlterTableRenameColumnEventArgs($oldColumnName, $column, $diff, $this);
2037 57797
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTableRenameColumn, $eventArgs);
2038
2039 57797
        $columnSql = array_merge($columnSql, $eventArgs->getSql());
2040
2041 57797
        return $eventArgs->isDefaultPrevented();
2042
    }
2043
2044
    /**
2045
     * @param string[] $sql
2046
     *
2047
     * @return bool
2048
     */
2049 64176
    protected function onSchemaAlterTable(TableDiff $diff, &$sql)
2050
    {
2051 64176
        if ($this->_eventManager === null) {
2052 60090
            return false;
2053
        }
2054
2055 62836
        if (! $this->_eventManager->hasListeners(Events::onSchemaAlterTable)) {
2056 62764
            return false;
2057
        }
2058
2059 57797
        $eventArgs = new SchemaAlterTableEventArgs($diff, $this);
2060 57797
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTable, $eventArgs);
2061
2062 57797
        $sql = array_merge($sql, $eventArgs->getSql());
2063
2064 57797
        return $eventArgs->isDefaultPrevented();
2065
    }
2066
2067
    /**
2068
     * @return string[]
2069
     */
2070 63874
    protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
2071
    {
2072 63874
        $tableName = $diff->getName($this)->getQuotedName($this);
2073
2074 63874
        $sql = [];
2075 63874
        if ($this->supportsForeignKeyConstraints()) {
2076 63874
            foreach ($diff->removedForeignKeys as $foreignKey) {
2077 61085
                $sql[] = $this->getDropForeignKeySQL($foreignKey, $tableName);
2078
            }
2079 63874
            foreach ($diff->changedForeignKeys as $foreignKey) {
2080 56905
                $sql[] = $this->getDropForeignKeySQL($foreignKey, $tableName);
2081
            }
2082
        }
2083
2084 63874
        foreach ($diff->removedIndexes as $index) {
2085 61552
            $sql[] = $this->getDropIndexSQL($index, $tableName);
2086
        }
2087 63874
        foreach ($diff->changedIndexes as $index) {
2088 61094
            $sql[] = $this->getDropIndexSQL($index, $tableName);
2089
        }
2090
2091 63874
        return $sql;
2092
    }
2093
2094
    /**
2095
     * @return string[]
2096
     */
2097 63874
    protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff)
2098
    {
2099 63874
        $sql     = [];
2100 63874
        $newName = $diff->getNewName();
2101
2102 63874
        if ($newName !== false) {
2103 57965
            $tableName = $newName->getQuotedName($this);
2104
        } else {
2105 63734
            $tableName = $diff->getName($this)->getQuotedName($this);
2106
        }
2107
2108 63874
        if ($this->supportsForeignKeyConstraints()) {
2109 63874
            foreach ($diff->addedForeignKeys as $foreignKey) {
2110 61168
                $sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName);
2111
            }
2112
2113 63874
            foreach ($diff->changedForeignKeys as $foreignKey) {
2114 56905
                $sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName);
2115
            }
2116
        }
2117
2118 63874
        foreach ($diff->addedIndexes as $index) {
2119 61056
            $sql[] = $this->getCreateIndexSQL($index, $tableName);
2120
        }
2121
2122 63874
        foreach ($diff->changedIndexes as $index) {
2123 61094
            $sql[] = $this->getCreateIndexSQL($index, $tableName);
2124
        }
2125
2126 63874
        foreach ($diff->renamedIndexes as $oldIndexName => $index) {
2127 61454
            $oldIndexName = new Identifier($oldIndexName);
2128 61454
            $sql          = array_merge(
2129 61454
                $sql,
2130 61454
                $this->getRenameIndexSQL($oldIndexName->getQuotedName($this), $index, $tableName)
2131
            );
2132
        }
2133
2134 63874
        return $sql;
2135
    }
2136
2137
    /**
2138
     * Returns the SQL for renaming an index on a table.
2139
     *
2140
     * @param string $oldIndexName The name of the index to rename from.
2141
     * @param Index  $index        The definition of the index to rename to.
2142
     * @param string $tableName    The table to rename the given index on.
2143
     *
2144
     * @return string[] The sequence of SQL statements for renaming the given index.
2145
     */
2146 57741
    protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
2147
    {
2148
        return [
2149 57741
            $this->getDropIndexSQL($oldIndexName, $tableName),
2150 57741
            $this->getCreateIndexSQL($index, $tableName),
2151
        ];
2152
    }
2153
2154
    /**
2155
     * Common code for alter table statement generation that updates the changed Index and Foreign Key definitions.
2156
     *
2157
     * @deprecated
2158
     *
2159
     * @return string[]
2160
     */
2161
    protected function _getAlterTableIndexForeignKeySQL(TableDiff $diff)
2162
    {
2163
        return array_merge($this->getPreAlterTableIndexForeignKeySQL($diff), $this->getPostAlterTableIndexForeignKeySQL($diff));
2164
    }
2165
2166
    /**
2167
     * Gets declaration of a number of fields in bulk.
2168
     *
2169
     * @param mixed[][] $fields A multidimensional associative array.
2170
     *                          The first dimension determines the field name, while the second
2171
     *                          dimension is keyed with the name of the properties
2172
     *                          of the field being declared as array indexes. Currently, the types
2173
     *                          of supported field properties are as follows:
2174
     *
2175
     *      length
2176
     *          Integer value that determines the maximum length of the text
2177
     *          field. If this argument is missing the field should be
2178
     *          declared to have the longest length allowed by the DBMS.
2179
     *
2180
     *      default
2181
     *          Text value to be used as default for this field.
2182
     *
2183
     *      notnull
2184
     *          Boolean flag that indicates whether this field is constrained
2185
     *          to not be set to null.
2186
     *      charset
2187
     *          Text value with the default CHARACTER SET for this field.
2188
     *      collation
2189
     *          Text value with the default COLLATION for this field.
2190
     *      unique
2191
     *          unique constraint
2192
     *
2193
     * @return string
2194
     */
2195 66436
    public function getColumnDeclarationListSQL(array $fields)
2196
    {
2197 66436
        $queryFields = [];
2198
2199 66436
        foreach ($fields as $fieldName => $field) {
2200 66436
            $queryFields[] = $this->getColumnDeclarationSQL($fieldName, $field);
2201
        }
2202
2203 66436
        return implode(', ', $queryFields);
2204
    }
2205
2206
    /**
2207
     * Obtains DBMS specific SQL code portion needed to declare a generic type
2208
     * field to be used in statements like CREATE TABLE.
2209
     *
2210
     * @param string  $name  The name the field to be declared.
2211
     * @param mixed[] $field An associative array with the name of the properties
2212
     *                       of the field being declared as array indexes. Currently, the types
2213
     *                       of supported field properties are as follows:
2214
     *
2215
     *      length
2216
     *          Integer value that determines the maximum length of the text
2217
     *          field. If this argument is missing the field should be
2218
     *          declared to have the longest length allowed by the DBMS.
2219
     *
2220
     *      default
2221
     *          Text value to be used as default for this field.
2222
     *
2223
     *      notnull
2224
     *          Boolean flag that indicates whether this field is constrained
2225
     *          to not be set to null.
2226
     *      charset
2227
     *          Text value with the default CHARACTER SET for this field.
2228
     *      collation
2229
     *          Text value with the default COLLATION for this field.
2230
     *      unique
2231
     *          unique constraint
2232
     *      check
2233
     *          column check constraint
2234
     *      columnDefinition
2235
     *          a string that defines the complete column
2236
     *
2237
     * @return string DBMS specific SQL code portion that should be used to declare the column.
2238
     */
2239 66052
    public function getColumnDeclarationSQL($name, array $field)
2240
    {
2241 66052
        if (isset($field['columnDefinition'])) {
2242 60186
            $columnDef = $this->getCustomTypeDeclarationSQL($field);
2243
        } else {
2244 65996
            $default = $this->getDefaultValueDeclarationSQL($field);
2245
2246 65996
            $charset = isset($field['charset']) && $field['charset'] ?
2247 65996
                ' ' . $this->getColumnCharsetDeclarationSQL($field['charset']) : '';
2248
2249 65996
            $collation = isset($field['collation']) && $field['collation'] ?
2250 65996
                ' ' . $this->getColumnCollationDeclarationSQL($field['collation']) : '';
2251
2252 65996
            $notnull = isset($field['notnull']) && $field['notnull'] ? ' NOT NULL' : '';
2253
2254 65996
            $unique = isset($field['unique']) && $field['unique'] ?
2255 65996
                ' ' . $this->getUniqueFieldDeclarationSQL() : '';
2256
2257 65996
            $check = isset($field['check']) && $field['check'] ?
2258 65996
                ' ' . $field['check'] : '';
2259
2260 65996
            $typeDecl  = $field['type']->getSQLDeclaration($field, $this);
2261 65996
            $columnDef = $typeDecl . $charset . $default . $notnull . $unique . $check . $collation;
2262
2263 65996
            if ($this->supportsInlineColumnComments() && isset($field['comment']) && $field['comment'] !== '') {
2264 59986
                $columnDef .= ' ' . $this->getInlineColumnCommentSQL($field['comment']);
2265
            }
2266
        }
2267
2268 66052
        return $name . ' ' . $columnDef;
2269
    }
2270
2271
    /**
2272
     * Returns the SQL snippet that declares a floating point column of arbitrary precision.
2273
     *
2274
     * @param mixed[] $columnDef
2275
     *
2276
     * @return string
2277
     */
2278 62480
    public function getDecimalTypeDeclarationSQL(array $columnDef)
2279
    {
2280 62480
        $columnDef['precision'] = ! isset($columnDef['precision']) || empty($columnDef['precision'])
2281 62480
            ? 10 : $columnDef['precision'];
2282 62480
        $columnDef['scale']     = ! isset($columnDef['scale']) || empty($columnDef['scale'])
2283 62480
            ? 0 : $columnDef['scale'];
2284
2285 62480
        return 'NUMERIC(' . $columnDef['precision'] . ', ' . $columnDef['scale'] . ')';
2286
    }
2287
2288
    /**
2289
     * Obtains DBMS specific SQL code portion needed to set a default value
2290
     * declaration to be used in statements like CREATE TABLE.
2291
     *
2292
     * @param mixed[] $field The field definition array.
2293
     *
2294
     * @return string DBMS specific SQL code portion needed to set a default value.
2295
     */
2296 67004
    public function getDefaultValueDeclarationSQL($field)
2297
    {
2298 67004
        if (! isset($field['default'])) {
2299 66504
            return empty($field['notnull']) ? ' DEFAULT NULL' : '';
2300
        }
2301
2302 63589
        $default = $field['default'];
2303
2304 63589
        if (! isset($field['type'])) {
2305 14448
            return " DEFAULT '" . $default . "'";
2306
        }
2307
2308 63541
        $type = $field['type'];
2309
2310 63541
        if ($type instanceof Types\PhpIntegerMappingType) {
2311 62618
            return ' DEFAULT ' . $default;
2312
        }
2313
2314 63425
        if ($type instanceof Types\PhpDateTimeMappingType && $default === $this->getCurrentTimestampSQL()) {
2315 63043
            return ' DEFAULT ' . $this->getCurrentTimestampSQL();
2316
        }
2317
2318 63017
        if ($type instanceof Types\TimeType && $default === $this->getCurrentTimeSQL()) {
2319 32584
            return ' DEFAULT ' . $this->getCurrentTimeSQL();
2320
        }
2321
2322 63017
        if ($type instanceof Types\DateType && $default === $this->getCurrentDateSQL()) {
2323 60293
            return ' DEFAULT ' . $this->getCurrentDateSQL();
2324
        }
2325
2326 62745
        if ($type instanceof Types\BooleanType) {
2327 62533
            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

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

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

2915
    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...
2916
    {
2917
        throw DBALException::notSupported(__METHOD__);
2918
    }
2919
2920
    /**
2921
     * @param string $sequenceName
2922
     *
2923
     * @return string
2924
     *
2925
     * @throws DBALException If not supported on this platform.
2926
     */
2927
    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

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

3502
    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...
3503
    {
3504 60375
        $tableIdentifier = new Identifier($tableName);
3505
3506 60375
        return 'TRUNCATE ' . $tableIdentifier->getQuotedName($this);
3507
    }
3508
3509
    /**
3510
     * This is for test reasons, many vendors have special requirements for dummy statements.
3511
     *
3512
     * @return string
3513
     */
3514 64579
    public function getDummySelectSQL()
3515
    {
3516 64579
        $expression = func_num_args() > 0 ? func_get_arg(0) : '1';
3517
3518 64579
        return sprintf('SELECT %s', $expression);
3519
    }
3520
3521
    /**
3522
     * Returns the SQL to create a new savepoint.
3523
     *
3524
     * @param string $savepoint
3525
     *
3526
     * @return string
3527
     */
3528 64629
    public function createSavePoint($savepoint)
3529
    {
3530 64629
        return 'SAVEPOINT ' . $savepoint;
3531
    }
3532
3533
    /**
3534
     * Returns the SQL to release a savepoint.
3535
     *
3536
     * @param string $savepoint
3537
     *
3538
     * @return string
3539
     */
3540 64625
    public function releaseSavePoint($savepoint)
3541
    {
3542 64625
        return 'RELEASE SAVEPOINT ' . $savepoint;
3543
    }
3544
3545
    /**
3546
     * Returns the SQL to rollback a savepoint.
3547
     *
3548
     * @param string $savepoint
3549
     *
3550
     * @return string
3551
     */
3552 64629
    public function rollbackSavePoint($savepoint)
3553
    {
3554 64629
        return 'ROLLBACK TO SAVEPOINT ' . $savepoint;
3555
    }
3556
3557
    /**
3558
     * Returns the keyword list instance of this platform.
3559
     *
3560
     * @return KeywordList
3561
     *
3562
     * @throws DBALException If no keyword list is specified.
3563
     */
3564 69312
    final public function getReservedKeywordsList()
3565
    {
3566
        // Check for an existing instantiation of the keywords class.
3567 69312
        if ($this->_keywords) {
3568 69044
            return $this->_keywords;
3569
        }
3570
3571 68662
        $class    = $this->getReservedKeywordsClass();
3572 68662
        $keywords = new $class();
3573 68662
        if (! $keywords instanceof KeywordList) {
3574
            throw DBALException::notSupported(__METHOD__);
3575
        }
3576
3577
        // Store the instance so it doesn't need to be generated on every request.
3578 68662
        $this->_keywords = $keywords;
3579
3580 68662
        return $keywords;
3581
    }
3582
3583
    /**
3584
     * Returns the class name of the reserved keywords list.
3585
     *
3586
     * @return string
3587
     *
3588
     * @throws DBALException If not supported on this platform.
3589
     */
3590
    protected function getReservedKeywordsClass()
3591
    {
3592
        throw DBALException::notSupported(__METHOD__);
3593
    }
3594
3595
    /**
3596
     * Quotes a literal string.
3597
     * This method is NOT meant to fix SQL injections!
3598
     * It is only meant to escape this platform's string literal
3599
     * quote character inside the given literal string.
3600
     *
3601
     * @param string $str The literal string to be quoted.
3602
     *
3603
     * @return string The quoted literal string.
3604
     */
3605 64782
    public function quoteStringLiteral($str)
3606
    {
3607 64782
        $c = $this->getStringLiteralQuoteCharacter();
3608
3609 64782
        return $c . str_replace($c, $c . $c, $str) . $c;
3610
    }
3611
3612
    /**
3613
     * Gets the character used for string literal quoting.
3614
     *
3615
     * @return string
3616
     */
3617 64854
    public function getStringLiteralQuoteCharacter()
3618
    {
3619 64854
        return "'";
3620
    }
3621
3622
    /**
3623
     * Escapes metacharacters in a string intended to be used with a LIKE
3624
     * operator.
3625
     *
3626
     * @param string $inputString a literal, unquoted string
3627
     * @param string $escapeChar  should be reused by the caller in the LIKE
3628
     *                            expression.
3629
     */
3630 63555
    final public function escapeStringForLike(string $inputString, string $escapeChar) : string
3631
    {
3632 63555
        return preg_replace(
3633 63555
            '~([' . preg_quote($this->getLikeWildcardCharacters() . $escapeChar, '~') . '])~u',
3634 63555
            addcslashes($escapeChar, '\\') . '$1',
3635 63555
            $inputString
3636
        );
3637
    }
3638
3639 63555
    protected function getLikeWildcardCharacters() : string
3640
    {
3641 63555
        return '%_';
3642
    }
3643
}
3644