Passed
Pull Request — 2.10.x (#3936)
by Asmir
05:07
created

AbstractPlatform::getDropIndexSQL()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 7.7656

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 17
ccs 3
cts 4
cp 0.75
rs 8.8333
cc 7
nc 7
nop 2
crap 7.7656
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 67655
    public function __construct()
154
    {
155 67655
    }
156
157
    /**
158
     * Sets the EventManager used by the Platform.
159
     *
160 60839
     * @return void
161
     */
162 60839
    public function setEventManager(EventManager $eventManager)
163 60839
    {
164
        $this->_eventManager = $eventManager;
165
    }
166
167
    /**
168
     * Gets the EventManager used by the Platform.
169
     *
170 58230
     * @return EventManager
171
     */
172 58230
    public function getEventManager()
173
    {
174
        return $this->_eventManager;
175
    }
176
177
    /**
178
     * Returns the SQL snippet that declares a boolean column.
179
     *
180
     * @param mixed[] $columnDef
181
     *
182
     * @return string
183
     */
184
    abstract public function getBooleanTypeDeclarationSQL(array $columnDef);
185
186
    /**
187
     * Returns the SQL snippet that declares a 4 byte integer column.
188
     *
189
     * @param mixed[] $columnDef
190
     *
191
     * @return string
192
     */
193
    abstract public function getIntegerTypeDeclarationSQL(array $columnDef);
194
195
    /**
196
     * Returns the SQL snippet that declares an 8 byte integer column.
197
     *
198
     * @param mixed[] $columnDef
199
     *
200
     * @return string
201
     */
202
    abstract public function getBigIntTypeDeclarationSQL(array $columnDef);
203
204
    /**
205
     * Returns the SQL snippet that declares a 2 byte integer column.
206
     *
207
     * @param mixed[] $columnDef
208
     *
209
     * @return string
210
     */
211
    abstract public function getSmallIntTypeDeclarationSQL(array $columnDef);
212
213
    /**
214
     * Returns the SQL snippet that declares common properties of an integer column.
215
     *
216
     * @param mixed[] $columnDef
217
     *
218
     * @return string
219
     */
220
    abstract protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef);
221
222
    /**
223
     * Lazy load Doctrine Type Mappings.
224
     *
225
     * @return void
226
     */
227
    abstract protected function initializeDoctrineTypeMappings();
228
229
    /**
230
     * Initializes Doctrine Type Mappings with the platform defaults
231
     * and with all additional type mappings.
232
     *
233 58308
     * @return void
234
     */
235 58308
    private function initializeAllDoctrineTypeMappings()
236
    {
237 58308
        $this->initializeDoctrineTypeMappings();
238 58308
239 30122
        foreach (Type::getTypesMap() as $typeName => $className) {
240
            foreach (Type::getType($typeName)->getMappedDatabaseTypes($this) as $dbType) {
241
                $this->doctrineTypeMapping[$dbType] = $typeName;
242 58308
            }
243
        }
244
    }
245
246
    /**
247
     * Returns the SQL snippet used to declare a VARCHAR column type.
248
     *
249
     * @param mixed[] $field
250
     *
251 60263
     * @return string
252
     */
253 60263
    public function getVarcharTypeDeclarationSQL(array $field)
254 56812
    {
255
        if (! isset($field['length'])) {
256
            $field['length'] = $this->getVarcharDefaultLength();
257 60263
        }
258
259 60263
        $fixed = $field['fixed'] ?? false;
260 58370
261 60263
        $maxLength = $fixed
262
            ? $this->getCharMaxLength()
263 60263
            : $this->getVarcharMaxLength();
264
265
        if ($field['length'] > $maxLength) {
266
            return $this->getClobTypeDeclarationSQL($field);
267 60263
        }
268
269
        return $this->getVarcharTypeDeclarationSQLSnippet($field['length'], $fixed);
270
    }
271
272
    /**
273
     * Returns the SQL snippet used to declare a BINARY/VARBINARY column type.
274
     *
275
     * @param mixed[] $field The column definition.
276
     *
277 56993
     * @return string
278
     */
279 56993
    public function getBinaryTypeDeclarationSQL(array $field)
280 56937
    {
281
        if (! isset($field['length'])) {
282
            $field['length'] = $this->getBinaryDefaultLength();
283 56993
        }
284
285 56993
        $fixed = $field['fixed'] ?? false;
286
287 56993
        $maxLength = $this->getBinaryMaxLength();
288 54817
289 54557
        if ($field['length'] > $maxLength) {
290 24
            if ($maxLength > 0) {
291 54557
                @trigger_error(sprintf(
292 54557
                    'Binary field length %d is greater than supported by the platform (%d). Reduce the field length or use a BLOB field instead.',
293 54557
                    $field['length'],
294
                    $maxLength
295
                ), E_USER_DEPRECATED);
296 54817
            }
297
298
            return $this->getBlobTypeDeclarationSQL($field);
299 56969
        }
300
301
        return $this->getBinaryTypeDeclarationSQLSnippet($field['length'], $fixed);
302
    }
303
304
    /**
305
     * Returns the SQL snippet to declare a GUID/UUID field.
306
     *
307
     * By default this maps directly to a CHAR(36) and only maps to more
308
     * special datatypes when the underlying databases support this datatype.
309
     *
310
     * @param mixed[] $field
311
     *
312 56064
     * @return string
313
     */
314 56064
    public function getGuidTypeDeclarationSQL(array $field)
315 56064
    {
316
        $field['length'] = 36;
317 56064
        $field['fixed']  = true;
318
319
        return $this->getVarcharTypeDeclarationSQL($field);
320
    }
321
322
    /**
323
     * Returns the SQL snippet to declare a JSON field.
324
     *
325
     * By default this maps directly to a CLOB and only maps to more
326
     * special datatypes when the underlying databases support this datatype.
327
     *
328
     * @param mixed[] $field
329
     *
330 54418
     * @return string
331
     */
332 54418
    public function getJsonTypeDeclarationSQL(array $field)
333
    {
334
        return $this->getClobTypeDeclarationSQL($field);
335
    }
336
337
    /**
338
     * @param int  $length
339
     * @param bool $fixed
340
     *
341
     * @return string
342
     *
343
     * @throws DBALException If not supported on this platform.
344
     */
345
    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

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

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

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

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

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

1056
    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

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

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

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

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

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

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

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

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

1751
    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...
1752
    {
1753
        throw DBALException::notSupported(__METHOD__);
1754
    }
1755
1756
    /**
1757
     * Returns the SQL to change a sequence on this platform.
1758
     *
1759
     * @return string
1760
     *
1761
     * @throws DBALException If not supported on this platform.
1762
     */
1763
    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

1763
    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...
1764
    {
1765 53762
        throw DBALException::notSupported(__METHOD__);
1766
    }
1767 53762
1768
    /**
1769
     * Returns the SQL to create a constraint on a table on this platform.
1770
     *
1771 53762
     * @param Table|string $table
1772
     *
1773 53762
     * @return string
1774
     *
1775 53762
     * @throws InvalidArgumentException
1776 53762
     */
1777 53762
    public function getCreateConstraintSQL(Constraint $constraint, $table)
1778 53762
    {
1779 53708
        if ($table instanceof Table) {
1780 53708
            $table = $table->getQuotedName($this);
1781
        }
1782
1783 53762
        $query = 'ALTER TABLE ' . $table . ' ADD CONSTRAINT ' . $constraint->getQuotedName($this);
1784
1785
        $columnList = '(' . implode(', ', $constraint->getQuotedColumns($this)) . ')';
1786 53708
1787 53708
        $referencesClause = '';
1788
        if ($constraint instanceof Index) {
1789 53708
            if ($constraint->isPrimary()) {
1790 53708
                $query .= ' PRIMARY KEY';
1791
            } elseif ($constraint->isUnique()) {
1792 53762
                $query .= ' UNIQUE';
1793
            } else {
1794 53762
                throw new InvalidArgumentException(
1795
                    'Can only create primary or unique constraints, no common indexes with getCreateConstraintSQL().'
1796
                );
1797
            }
1798
        } elseif ($constraint instanceof ForeignKeyConstraint) {
1799
            $query .= ' FOREIGN KEY';
1800
1801
            $referencesClause = ' REFERENCES ' . $constraint->getQuotedForeignTableName($this) .
1802
                ' (' . implode(', ', $constraint->getQuotedForeignColumns($this)) . ')';
1803
        }
1804
        $query .= ' ' . $columnList . $referencesClause;
1805
1806 58072
        return $query;
1807
    }
1808 58072
1809 57058
    /**
1810
     * Returns the SQL to create an index on a table on this platform.
1811 58072
     *
1812 58072
     * @param Table|string $table The name of the table on which the index is to be created.
1813
     *
1814 58072
     * @return string
1815
     *
1816
     * @throws InvalidArgumentException
1817
     */
1818 58072
    public function getCreateIndexSQL(Index $index, $table)
1819 52068
    {
1820
        if ($table instanceof Table) {
1821
            $table = $table->getQuotedName($this);
1822 58034
        }
1823 58034
        $name    = $index->getQuotedName($this);
1824
        $columns = $index->getColumns();
1825 58034
1826
        if (count($columns) === 0) {
1827
            throw new InvalidArgumentException("Incomplete definition. 'columns' required.");
1828
        }
1829
1830
        if ($index->isPrimary()) {
1831
            return $this->getCreatePrimaryKeySQL($index, $table);
1832
        }
1833 59192
1834
        $query  = 'CREATE ' . $this->getCreateIndexSQLFlags($index) . 'INDEX ' . $name . ' ON ' . $table;
1835 59192
        $query .= ' (' . $this->getIndexFieldDeclarationListSQL($index) . ')' . $this->getPartialIndexSQL($index);
1836 45105
1837
        return $query;
1838
    }
1839 59182
1840
    /**
1841
     * Adds condition for partial index.
1842
     *
1843
     * @return string
1844
     */
1845
    protected function getPartialIndexSQL(Index $index)
1846
    {
1847 56526
        if ($this->supportsPartialIndexes() && $index->hasOption('where')) {
1848
            return ' WHERE ' . $index->getOption('where');
1849 56526
        }
1850
1851
        return '';
1852
    }
1853
1854
    /**
1855
     * Adds additional flags for index generation.
1856
     *
1857
     * @return string
1858
     */
1859 54705
    protected function getCreateIndexSQLFlags(Index $index)
1860
    {
1861 54705
        return $index->isUnique() ? 'UNIQUE ' : '';
1862
    }
1863
1864
    /**
1865 54705
     * Returns the SQL to create an unnamed primary key constraint.
1866
     *
1867
     * @param Table|string $table
1868
     *
1869
     * @return string
1870
     */
1871
    public function getCreatePrimaryKeySQL(Index $index, $table)
1872
    {
1873
        if ($table instanceof Table) {
1874
            $table = $table->getQuotedName($this);
1875
        }
1876
1877 53150
        if ($index->getName() !=='primary' && $this->supportsNamedPrimaryConstraints()) {
1878
            return $this->getCreateConstraintSQL($index, $table);
1879 53150
        }
1880
1881
        return 'ALTER TABLE ' . $table . ' ADD PRIMARY KEY (' . $this->getIndexFieldDeclarationListSQL($index) . ')';
1882
    }
1883
1884
    /**
1885
     * Returns the SQL to create a named schema.
1886
     *
1887
     * @param string $schemaName
1888
     *
1889
     * @return string
1890
     *
1891
     * @throws DBALException If not supported on this platform.
1892
     */
1893
    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

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

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

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

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

2931
    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...
2932
    {
2933
        throw DBALException::notSupported(__METHOD__);
2934
    }
2935
2936
    /**
2937
     * @param string $sequenceName
2938
     *
2939
     * @return string
2940
     *
2941 44001
     * @throws DBALException If not supported on this platform.
2942
     */
2943 44001
    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

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

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