Failed Conditions
Push — exceptions ( 7b5f2f...d1ce0d )
by Michael
24:32
created

AbstractPlatform::getCharMaxLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

363
    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...
364
    {
365
        throw NotSupported::new('VARCHARs not supported by Platform.');
366
    }
367
368
    /**
369
     * Returns the SQL snippet used to declare a BINARY/VARBINARY column type.
370
     *
371
     * @param int  $length The length of the column.
372
     * @param bool $fixed  Whether the column length is fixed.
373
     *
374
     * @return string
375
     *
376
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
377
     */
378
    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

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

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

897
    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

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

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

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

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

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

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

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

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

1326
    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...
1327
    {
1328
        throw NotSupported::new(__METHOD__);
1329
    }
1330
1331
    /**
1332
     * Returns the SQL bit AND comparison expression.
1333
     *
1334
     * @param string $value1
1335
     * @param string $value2
1336
     *
1337
     * @return string
1338
     */
1339 176
    public function getBitAndComparisonExpression($value1, $value2)
1340
    {
1341 176
        return '(' . $value1 . ' & ' . $value2 . ')';
1342
    }
1343
1344
    /**
1345
     * Returns the SQL bit OR comparison expression.
1346
     *
1347
     * @param string $value1
1348
     * @param string $value2
1349
     *
1350
     * @return string
1351
     */
1352 176
    public function getBitOrComparisonExpression($value1, $value2)
1353
    {
1354 176
        return '(' . $value1 . ' | ' . $value2 . ')';
1355
    }
1356
1357
    /**
1358
     * Returns the FOR UPDATE expression.
1359
     *
1360
     * @return string
1361
     */
1362 26
    public function getForUpdateSQL()
1363
    {
1364 26
        return 'FOR UPDATE';
1365
    }
1366
1367
    /**
1368
     * Honors that some SQL vendors such as MsSql use table hints for locking instead of the ANSI SQL FOR UPDATE specification.
1369
     *
1370
     * @param string   $fromClause The FROM clause to append the hint for the given lock mode to.
1371
     * @param int|null $lockMode   One of the Doctrine\DBAL\LockMode::* constants. If null is given, nothing will
1372
     *                             be appended to the FROM clause.
1373
     *
1374
     * @return string
1375
     */
1376 26
    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

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

1740
    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...
1741
    {
1742
        throw NotSupported::new(__METHOD__);
1743
    }
1744
1745
    /**
1746
     * Returns the SQL to change a sequence on this platform.
1747
     *
1748
     * @param \Doctrine\DBAL\Schema\Sequence $sequence
1749
     *
1750
     * @return string
1751
     *
1752
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
1753
     */
1754
    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

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

1871
        return 'ALTER TABLE ' . /** @scrutinizer ignore-type */ $table . ' ADD PRIMARY KEY (' . $this->getIndexFieldDeclarationListSQL($index->getQuotedColumns($this)) . ')';
Loading history...
1872
    }
1873
1874
    /**
1875
     * Returns the SQL to create a named schema.
1876
     *
1877
     * @param string $schemaName
1878
     *
1879
     * @return string
1880
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
1881
     */
1882 112
    public function getCreateSchemaSQL($schemaName)
1883
    {
1884 112
        throw NotSupported::new(__METHOD__);
1885
    }
1886
1887
    /**
1888
     * Quotes a string so that it can be safely used as a table or column name,
1889
     * even if it is a reserved word of the platform. This also detects identifier
1890
     * chains separated by dot and quotes them independently.
1891
     *
1892
     * NOTE: Just because you CAN use quoted identifiers doesn't mean
1893
     * you SHOULD use them. In general, they end up causing way more
1894
     * problems than they solve.
1895
     *
1896
     * @param string $str The identifier name to be quoted.
1897
     *
1898
     * @return string The quoted identifier string.
1899
     */
1900 4363
    public function quoteIdentifier($str)
1901
    {
1902 4363
        if (strpos($str, ".") !== false) {
1903 216
            $parts = array_map([$this, "quoteSingleIdentifier"], explode(".", $str));
1904
1905 216
            return implode(".", $parts);
1906
        }
1907
1908 4363
        return $this->quoteSingleIdentifier($str);
1909
    }
1910
1911
    /**
1912
     * Quotes a single identifier (no dot chain separation).
1913
     *
1914
     * @param string $str The identifier name to be quoted.
1915
     *
1916
     * @return string The quoted identifier string.
1917
     */
1918 4313
    public function quoteSingleIdentifier($str)
1919
    {
1920 4313
        $c = $this->getIdentifierQuoteCharacter();
1921
1922 4313
        return $c . str_replace($c, $c.$c, $str) . $c;
1923
    }
1924
1925
    /**
1926
     * Returns the SQL to create a new foreign key.
1927
     *
1928
     * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey The foreign key constraint.
1929
     * @param \Doctrine\DBAL\Schema\Table|string         $table      The name of the table on which the foreign key is to be created.
1930
     *
1931
     * @return string
1932
     */
1933 1043
    public function getCreateForeignKeySQL(ForeignKeyConstraint $foreignKey, $table)
1934
    {
1935 1043
        if ($table instanceof Table) {
1936 16
            $table = $table->getQuotedName($this);
1937
        }
1938
1939 1043
        $query = 'ALTER TABLE ' . $table . ' ADD ' . $this->getForeignKeyDeclarationSQL($foreignKey);
1940
1941 1043
        return $query;
1942
    }
1943
1944
    /**
1945
     * Gets the SQL statements for altering an existing table.
1946
     *
1947
     * This method returns an array of SQL statements, since some platforms need several statements.
1948
     *
1949
     * @param \Doctrine\DBAL\Schema\TableDiff $diff
1950
     *
1951
     * @return array
1952
     *
1953
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
1954
     */
1955
    public function getAlterTableSQL(TableDiff $diff)
1956
    {
1957
        throw NotSupported::new(__METHOD__);
1958
    }
1959
1960
    /**
1961
     * @param \Doctrine\DBAL\Schema\Column    $column
1962
     * @param \Doctrine\DBAL\Schema\TableDiff $diff
1963
     * @param array                           $columnSql
1964
     *
1965
     * @return bool
1966
     */
1967 1108
    protected function onSchemaAlterTableAddColumn(Column $column, TableDiff $diff, &$columnSql)
1968
    {
1969 1108
        if (null === $this->_eventManager) {
1970 896
            return false;
1971
        }
1972
1973 212
        if ( ! $this->_eventManager->hasListeners(Events::onSchemaAlterTableAddColumn)) {
1974 20
            return false;
1975
        }
1976
1977 192
        $eventArgs = new SchemaAlterTableAddColumnEventArgs($column, $diff, $this);
1978 192
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTableAddColumn, $eventArgs);
1979
1980 192
        $columnSql = array_merge($columnSql, $eventArgs->getSql());
1981
1982 192
        return $eventArgs->isDefaultPrevented();
1983
    }
1984
1985
    /**
1986
     * @param \Doctrine\DBAL\Schema\Column    $column
1987
     * @param \Doctrine\DBAL\Schema\TableDiff $diff
1988
     * @param array                           $columnSql
1989
     *
1990
     * @return bool
1991
     */
1992 822
    protected function onSchemaAlterTableRemoveColumn(Column $column, TableDiff $diff, &$columnSql)
1993
    {
1994 822
        if (null === $this->_eventManager) {
1995 608
            return false;
1996
        }
1997
1998 214
        if ( ! $this->_eventManager->hasListeners(Events::onSchemaAlterTableRemoveColumn)) {
1999 22
            return false;
2000
        }
2001
2002 192
        $eventArgs = new SchemaAlterTableRemoveColumnEventArgs($column, $diff, $this);
2003 192
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTableRemoveColumn, $eventArgs);
2004
2005 192
        $columnSql = array_merge($columnSql, $eventArgs->getSql());
2006
2007 192
        return $eventArgs->isDefaultPrevented();
2008
    }
2009
2010
    /**
2011
     * @param \Doctrine\DBAL\Schema\ColumnDiff $columnDiff
2012
     * @param \Doctrine\DBAL\Schema\TableDiff  $diff
2013
     * @param array                            $columnSql
2014
     *
2015
     * @return bool
2016
     */
2017 2158
    protected function onSchemaAlterTableChangeColumn(ColumnDiff $columnDiff, TableDiff $diff, &$columnSql)
2018
    {
2019 2158
        if (null === $this->_eventManager) {
2020 1744
            return false;
2021
        }
2022
2023 414
        if ( ! $this->_eventManager->hasListeners(Events::onSchemaAlterTableChangeColumn)) {
2024 222
            return false;
2025
        }
2026
2027 192
        $eventArgs = new SchemaAlterTableChangeColumnEventArgs($columnDiff, $diff, $this);
2028 192
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTableChangeColumn, $eventArgs);
2029
2030 192
        $columnSql = array_merge($columnSql, $eventArgs->getSql());
2031
2032 192
        return $eventArgs->isDefaultPrevented();
2033
    }
2034
2035
    /**
2036
     * @param string                          $oldColumnName
2037
     * @param \Doctrine\DBAL\Schema\Column    $column
2038
     * @param \Doctrine\DBAL\Schema\TableDiff $diff
2039
     * @param array                           $columnSql
2040
     *
2041
     * @return bool
2042
     */
2043 835
    protected function onSchemaAlterTableRenameColumn($oldColumnName, Column $column, TableDiff $diff, &$columnSql)
2044
    {
2045 835
        if (null === $this->_eventManager) {
2046 624
            return false;
2047
        }
2048
2049 211
        if ( ! $this->_eventManager->hasListeners(Events::onSchemaAlterTableRenameColumn)) {
2050 19
            return false;
2051
        }
2052
2053 192
        $eventArgs = new SchemaAlterTableRenameColumnEventArgs($oldColumnName, $column, $diff, $this);
2054 192
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTableRenameColumn, $eventArgs);
2055
2056 192
        $columnSql = array_merge($columnSql, $eventArgs->getSql());
2057
2058 192
        return $eventArgs->isDefaultPrevented();
2059
    }
2060
2061
    /**
2062
     * @param \Doctrine\DBAL\Schema\TableDiff $diff
2063
     * @param array                           $sql
2064
     *
2065
     * @return bool
2066
     */
2067 4220
    protected function onSchemaAlterTable(TableDiff $diff, &$sql)
2068
    {
2069 4220
        if (null === $this->_eventManager) {
2070 3728
            return false;
2071
        }
2072
2073 492
        if ( ! $this->_eventManager->hasListeners(Events::onSchemaAlterTable)) {
2074 300
            return false;
2075
        }
2076
2077 192
        $eventArgs = new SchemaAlterTableEventArgs($diff, $this);
2078 192
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTable, $eventArgs);
2079
2080 192
        $sql = array_merge($sql, $eventArgs->getSql());
2081
2082 192
        return $eventArgs->isDefaultPrevented();
2083
    }
2084
2085
    /**
2086
     * @param \Doctrine\DBAL\Schema\TableDiff $diff
2087
     *
2088
     * @return array
2089
     */
2090 3993
    protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
2091
    {
2092 3993
        $tableName = $diff->getName($this)->getQuotedName($this);
2093
2094 3993
        $sql = [];
2095 3993
        if ($this->supportsForeignKeyConstraints()) {
2096 3993
            foreach ($diff->removedForeignKeys as $foreignKey) {
2097 287
                $sql[] = $this->getDropForeignKeySQL($foreignKey, $tableName);
2098
            }
2099 3993
            foreach ($diff->changedForeignKeys as $foreignKey) {
2100 224
                $sql[] = $this->getDropForeignKeySQL($foreignKey, $tableName);
2101
            }
2102
        }
2103
2104 3993
        foreach ($diff->removedIndexes as $index) {
2105 175
            $sql[] = $this->getDropIndexSQL($index, $tableName);
2106
        }
2107 3993
        foreach ($diff->changedIndexes as $index) {
2108 286
            $sql[] = $this->getDropIndexSQL($index, $tableName);
2109
        }
2110
2111 3993
        return $sql;
2112
    }
2113
2114
    /**
2115
     * @param \Doctrine\DBAL\Schema\TableDiff $diff
2116
     *
2117
     * @return array
2118
     */
2119 3993
    protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff)
2120
    {
2121 3993
        $tableName = (false !== $diff->newName)
2122 354
            ? $diff->getNewName()->getQuotedName($this)
2123 3993
            : $diff->getName($this)->getQuotedName($this);
2124
2125 3993
        $sql = [];
2126
2127 3993
        if ($this->supportsForeignKeyConstraints()) {
2128 3993
            foreach ($diff->addedForeignKeys as $foreignKey) {
2129 254
                $sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName);
2130
            }
2131
2132 3993
            foreach ($diff->changedForeignKeys as $foreignKey) {
2133 224
                $sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName);
2134
            }
2135
        }
2136
2137 3993
        foreach ($diff->addedIndexes as $index) {
2138 63
            $sql[] = $this->getCreateIndexSQL($index, $tableName);
2139
        }
2140
2141 3993
        foreach ($diff->changedIndexes as $index) {
2142 286
            $sql[] = $this->getCreateIndexSQL($index, $tableName);
2143
        }
2144
2145 3993
        foreach ($diff->renamedIndexes as $oldIndexName => $index) {
2146 910
            $oldIndexName = new Identifier($oldIndexName);
2147 910
            $sql          = array_merge(
2148 910
                $sql,
2149 910
                $this->getRenameIndexSQL($oldIndexName->getQuotedName($this), $index, $tableName)
2150
            );
2151
        }
2152
2153 3993
        return $sql;
2154
    }
2155
2156
    /**
2157
     * Returns the SQL for renaming an index on a table.
2158
     *
2159
     * @param string                      $oldIndexName The name of the index to rename from.
2160
     * @param \Doctrine\DBAL\Schema\Index $index        The definition of the index to rename to.
2161
     * @param string                      $tableName    The table to rename the given index on.
2162
     *
2163
     * @return array The sequence of SQL statements for renaming the given index.
2164
     */
2165 172
    protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
2166
    {
2167
        return [
2168 172
            $this->getDropIndexSQL($oldIndexName, $tableName),
2169 172
            $this->getCreateIndexSQL($index, $tableName)
2170
        ];
2171
    }
2172
2173
    /**
2174
     * Common code for alter table statement generation that updates the changed Index and Foreign Key definitions.
2175
     *
2176
     * @param \Doctrine\DBAL\Schema\TableDiff $diff
2177
     *
2178
     * @return array
2179
     */
2180
    protected function _getAlterTableIndexForeignKeySQL(TableDiff $diff)
2181
    {
2182
        return array_merge($this->getPreAlterTableIndexForeignKeySQL($diff), $this->getPostAlterTableIndexForeignKeySQL($diff));
2183
    }
2184
2185
    /**
2186
     * Gets declaration of a number of fields in bulk.
2187
     *
2188
     * @param array $fields A multidimensional associative array.
2189
     *                      The first dimension determines the field name, while the second
2190
     *                      dimension is keyed with the name of the properties
2191
     *                      of the field being declared as array indexes. Currently, the types
2192
     *                      of supported field properties are as follows:
2193
     *
2194
     *      length
2195
     *          Integer value that determines the maximum length of the text
2196
     *          field. If this argument is missing the field should be
2197
     *          declared to have the longest length allowed by the DBMS.
2198
     *
2199
     *      default
2200
     *          Text value to be used as default for this field.
2201
     *
2202
     *      notnull
2203
     *          Boolean flag that indicates whether this field is constrained
2204
     *          to not be set to null.
2205
     *      charset
2206
     *          Text value with the default CHARACTER SET for this field.
2207
     *      collation
2208
     *          Text value with the default COLLATION for this field.
2209
     *      unique
2210
     *          unique constraint
2211
     *
2212
     * @return string
2213
     */
2214 5354
    public function getColumnDeclarationListSQL(array $fields)
2215
    {
2216 5354
        $queryFields = [];
2217
2218 5354
        foreach ($fields as $fieldName => $field) {
2219 5354
            $queryFields[] = $this->getColumnDeclarationSQL($fieldName, $field);
2220
        }
2221
2222 5354
        return implode(', ', $queryFields);
2223
    }
2224
2225
    /**
2226
     * Obtains DBMS specific SQL code portion needed to declare a generic type
2227
     * field to be used in statements like CREATE TABLE.
2228
     *
2229
     * @param string $name  The name the field to be declared.
2230
     * @param array  $field An associative array with the name of the properties
2231
     *                      of the field being declared as array indexes. Currently, the types
2232
     *                      of supported field properties are as follows:
2233
     *
2234
     *      length
2235
     *          Integer value that determines the maximum length of the text
2236
     *          field. If this argument is missing the field should be
2237
     *          declared to have the longest length allowed by the DBMS.
2238
     *
2239
     *      default
2240
     *          Text value to be used as default for this field.
2241
     *
2242
     *      notnull
2243
     *          Boolean flag that indicates whether this field is constrained
2244
     *          to not be set to null.
2245
     *      charset
2246
     *          Text value with the default CHARACTER SET for this field.
2247
     *      collation
2248
     *          Text value with the default COLLATION for this field.
2249
     *      unique
2250
     *          unique constraint
2251
     *      check
2252
     *          column check constraint
2253
     *      columnDefinition
2254
     *          a string that defines the complete column
2255
     *
2256
     * @return string DBMS specific SQL code portion that should be used to declare the column.
2257
     */
2258 5596
    public function getColumnDeclarationSQL($name, array $field)
2259
    {
2260 5596
        if (isset($field['columnDefinition'])) {
2261 153
            $columnDef = $this->getCustomTypeDeclarationSQL($field);
2262
        } else {
2263 5452
            $default = $this->getDefaultValueDeclarationSQL($field);
2264
2265 5452
            $charset = (isset($field['charset']) && $field['charset']) ?
2266 5452
                ' ' . $this->getColumnCharsetDeclarationSQL($field['charset']) : '';
2267
2268 5452
            $collation = (isset($field['collation']) && $field['collation']) ?
2269 5452
                ' ' . $this->getColumnCollationDeclarationSQL($field['collation']) : '';
2270
2271 5452
            $notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
2272
2273 5452
            $unique = (isset($field['unique']) && $field['unique']) ?
2274 5452
                ' ' . $this->getUniqueFieldDeclarationSQL() : '';
2275
2276 5452
            $check = (isset($field['check']) && $field['check']) ?
2277 5452
                ' ' . $field['check'] : '';
2278
2279 5452
            $typeDecl = $field['type']->getSQLDeclaration($field, $this);
2280 5452
            $columnDef = $typeDecl . $charset . $default . $notnull . $unique . $check . $collation;
2281
2282 5452
            if ($this->supportsInlineColumnComments() && isset($field['comment']) && $field['comment'] !== '') {
2283 636
                $columnDef .= ' ' . $this->getInlineColumnCommentSQL($field['comment']);
2284
            }
2285
        }
2286
2287 5596
        return $name . ' ' . $columnDef;
2288
    }
2289
2290
    /**
2291
     * Returns the SQL snippet that declares a floating point column of arbitrary precision.
2292
     *
2293
     * @param array $columnDef
2294
     *
2295
     * @return string
2296
     */
2297 1543
    public function getDecimalTypeDeclarationSQL(array $columnDef)
2298
    {
2299 1543
        $columnDef['precision'] = ( ! isset($columnDef['precision']) || empty($columnDef['precision']))
2300 1543
            ? 10 : $columnDef['precision'];
2301 1543
        $columnDef['scale'] = ( ! isset($columnDef['scale']) || empty($columnDef['scale']))
2302 1543
            ? 0 : $columnDef['scale'];
2303
2304 1543
        return 'NUMERIC(' . $columnDef['precision'] . ', ' . $columnDef['scale'] . ')';
2305
    }
2306
2307
    /**
2308
     * Obtains DBMS specific SQL code portion needed to set a default value
2309
     * declaration to be used in statements like CREATE TABLE.
2310
     *
2311
     * @param array $field The field definition array.
2312
     *
2313
     * @return string DBMS specific SQL code portion needed to set a default value.
2314
     */
2315 6106
    public function getDefaultValueDeclarationSQL($field)
2316
    {
2317 6106
        if ( ! isset($field['default'])) {
2318 5348
            return empty($field['notnull']) ? ' DEFAULT NULL' : '';
2319
        }
2320
2321 1050
        $default = $field['default'];
2322
2323 1050
        if ( ! isset($field['type'])) {
2324
            return " DEFAULT '" . $default . "'";
2325
        }
2326
2327 1050
        $type = $field['type'];
2328
2329 1050
        if ($type instanceof Types\PhpIntegerMappingType) {
2330 316
            return ' DEFAULT ' . $default;
2331
        }
2332
2333 775
        if ($type instanceof Types\PhpDateTimeMappingType && $default === $this->getCurrentTimestampSQL()) {
2334 178
            return ' DEFAULT ' . $this->getCurrentTimestampSQL();
2335
        }
2336
2337 607
        if ($type instanceof Types\TimeType && $default === $this->getCurrentTimeSQL()) {
2338 2
            return ' DEFAULT ' . $this->getCurrentTimeSQL();
2339
        }
2340
2341 607
        if ($type instanceof Types\DateType && $default === $this->getCurrentDateSQL()) {
2342 162
            return ' DEFAULT ' . $this->getCurrentDateSQL();
2343
        }
2344
2345 445
        if ($type instanceof Types\BooleanType) {
2346 165
            return " DEFAULT '" . $this->convertBooleans($default) . "'";
2347
        }
2348
2349 440
        return " DEFAULT '" . $default . "'";
2350
    }
2351
2352
    /**
2353
     * Obtains DBMS specific SQL code portion needed to set a CHECK constraint
2354
     * declaration to be used in statements like CREATE TABLE.
2355
     *
2356
     * @param array $definition The check definition.
2357
     *
2358
     * @return string DBMS specific SQL code portion needed to set a CHECK constraint.
2359
     */
2360 1374
    public function getCheckDeclarationSQL(array $definition)
2361
    {
2362 1374
        $constraints = [];
2363 1374
        foreach ($definition as $field => $def) {
2364 1374
            if (is_string($def)) {
2365
                $constraints[] = 'CHECK (' . $def . ')';
2366
            } else {
2367 1374
                if (isset($def['min'])) {
2368 32
                    $constraints[] = 'CHECK (' . $field . ' >= ' . $def['min'] . ')';
2369
                }
2370
2371 1374
                if (isset($def['max'])) {
2372 1374
                    $constraints[] = 'CHECK (' . $field . ' <= ' . $def['max'] . ')';
2373
                }
2374
            }
2375
        }
2376
2377 1374
        return implode(', ', $constraints);
2378
    }
2379
2380
    /**
2381
     * Obtains DBMS specific SQL code portion needed to set a unique
2382
     * constraint declaration to be used in statements like CREATE TABLE.
2383
     *
2384
     * @param string                                 $name  The name of the unique constraint.
2385
     * @param \Doctrine\DBAL\Schema\UniqueConstraint $constraint The unique constraint definition.
2386
     *
2387
     * @return string DBMS specific SQL code portion needed to set a constraint.
2388
     *
2389
     * @throws \InvalidArgumentException
2390
     */
2391 416
    public function getUniqueConstraintDeclarationSQL($name, UniqueConstraint $constraint)
2392
    {
2393 416
        $columns = $constraint->getQuotedColumns($this);
2394 416
        $name    = new Identifier($name);
2395
2396 416
        if (count($columns) === 0) {
2397 16
            throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
2398
        }
2399
2400 400
        $flags = ['UNIQUE'];
2401
2402 400
        if ($constraint->hasFlag('clustered')) {
2403 16
            $flags[] = 'CLUSTERED';
2404
        }
2405
2406 400
        $constraintName  = $name->getQuotedName($this);
2407 400
        $constraintName  = ! empty($constraintName) ? $constraintName . ' ' : '';
2408 400
        $columnListNames = $this->getIndexFieldDeclarationListSQL($columns);
2409
2410 400
        return sprintf('CONSTRAINT %s%s (%s)', $constraintName, implode(' ', $flags), $columnListNames);
2411
    }
2412
2413
    /**
2414
     * Obtains DBMS specific SQL code portion needed to set an index
2415
     * declaration to be used in statements like CREATE TABLE.
2416
     *
2417
     * @param string                      $name  The name of the index.
2418
     * @param \Doctrine\DBAL\Schema\Index $index The index definition.
2419
     *
2420
     * @return string DBMS specific SQL code portion needed to set an index.
2421
     *
2422
     * @throws \InvalidArgumentException
2423
     */
2424 768
    public function getIndexDeclarationSQL($name, Index $index)
2425
    {
2426 768
        $columns = $index->getQuotedColumns($this);
2427 768
        $name = new Identifier($name);
2428
2429 768
        if (count($columns) === 0) {
2430
            throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
2431
        }
2432
2433 768
        return $this->getCreateIndexSQLFlags($index) . 'INDEX ' . $name->getQuotedName($this) . ' ('
2434 768
            . $this->getIndexFieldDeclarationListSQL($columns)
2435 768
            . ')' . $this->getPartialIndexSQL($index);
2436
    }
2437
2438
    /**
2439
     * Obtains SQL code portion needed to create a custom column,
2440
     * e.g. when a field has the "columnDefinition" keyword.
2441
     * Only "AUTOINCREMENT" and "PRIMARY KEY" are added if appropriate.
2442
     *
2443
     * @param array $columnDef
2444
     *
2445
     * @return string
2446
     */
2447 201
    public function getCustomTypeDeclarationSQL(array $columnDef)
2448
    {
2449 201
        return $columnDef['columnDefinition'];
2450
    }
2451
2452
    /**
2453
     * Obtains DBMS specific SQL code portion needed to set an index
2454
     * declaration to be used in statements like CREATE TABLE.
2455
     *
2456
     * @param array $fields
2457
     *
2458
     * @return string
2459
     */
2460 2873
    public function getIndexFieldDeclarationListSQL(array $fields)
2461
    {
2462 2873
        $ret = [];
2463
2464 2873
        foreach ($fields as $field => $definition) {
2465 2873
            if (is_array($definition)) {
2466
                $ret[] = $field;
2467
            } else {
2468 2873
                $ret[] = $definition;
2469
            }
2470
        }
2471
2472 2873
        return implode(', ', $ret);
2473
    }
2474
2475
    /**
2476
     * Returns the required SQL string that fits between CREATE ... TABLE
2477
     * to create the table as a temporary table.
2478
     *
2479
     * Should be overridden in driver classes to return the correct string for the
2480
     * specific database type.
2481
     *
2482
     * The default is to return the string "TEMPORARY" - this will result in a
2483
     * SQL error for any database that does not support temporary tables, or that
2484
     * requires a different SQL command from "CREATE TEMPORARY TABLE".
2485
     *
2486
     * @return string The string required to be placed between "CREATE" and "TABLE"
2487
     *                to generate a temporary table, if possible.
2488
     */
2489
    public function getTemporaryTableSQL()
2490
    {
2491
        return 'TEMPORARY';
2492
    }
2493
2494
    /**
2495
     * Some vendors require temporary table names to be qualified specially.
2496
     *
2497
     * @param string $tableName
2498
     *
2499
     * @return string
2500
     */
2501 26
    public function getTemporaryTableName($tableName)
2502
    {
2503 26
        return $tableName;
2504
    }
2505
2506
    /**
2507
     * Obtain DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
2508
     * of a field declaration to be used in statements like CREATE TABLE.
2509
     *
2510
     * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey
2511
     *
2512
     * @return string DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
2513
     *                of a field declaration.
2514
     */
2515 1268
    public function getForeignKeyDeclarationSQL(ForeignKeyConstraint $foreignKey)
2516
    {
2517 1268
        $sql  = $this->getForeignKeyBaseDeclarationSQL($foreignKey);
2518 1220
        $sql .= $this->getAdvancedForeignKeyOptionsSQL($foreignKey);
2519
2520 1220
        return $sql;
2521
    }
2522
2523
    /**
2524
     * Returns the FOREIGN KEY query section dealing with non-standard options
2525
     * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
2526
     *
2527
     * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey The foreign key definition.
2528
     *
2529
     * @return string
2530
     */
2531 1156
    public function getAdvancedForeignKeyOptionsSQL(ForeignKeyConstraint $foreignKey)
2532
    {
2533 1156
        $query = '';
2534 1156
        if ($this->supportsForeignKeyOnUpdate() && $foreignKey->hasOption('onUpdate')) {
2535 16
            $query .= ' ON UPDATE ' . $this->getForeignKeyReferentialActionSQL($foreignKey->getOption('onUpdate'));
2536
        }
2537 1156
        if ($foreignKey->hasOption('onDelete')) {
2538 79
            $query .= ' ON DELETE ' . $this->getForeignKeyReferentialActionSQL($foreignKey->getOption('onDelete'));
2539
        }
2540
2541 1156
        return $query;
2542
    }
2543
2544
    /**
2545
     * Returns the given referential action in uppercase if valid, otherwise throws an exception.
2546
     *
2547
     * @param string $action The foreign key referential action.
2548
     *
2549
     * @return string
2550
     *
2551
     * @throws \InvalidArgumentException if unknown referential action given
2552
     */
2553 1263
    public function getForeignKeyReferentialActionSQL($action)
2554
    {
2555 1263
        $upper = strtoupper($action);
2556 1263
        switch ($upper) {
2557 1263
            case 'CASCADE':
2558 853
            case 'SET NULL':
2559 661
            case 'NO ACTION':
2560 501
            case 'RESTRICT':
2561 357
            case 'SET DEFAULT':
2562 1087
                return $upper;
2563
            default:
2564 176
                throw new \InvalidArgumentException('Invalid foreign key action: ' . $upper);
2565
        }
2566
    }
2567
2568
    /**
2569
     * Obtains DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
2570
     * of a field declaration to be used in statements like CREATE TABLE.
2571
     *
2572
     * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey
2573
     *
2574
     * @return string
2575
     *
2576
     * @throws \InvalidArgumentException
2577
     */
2578 1108
    public function getForeignKeyBaseDeclarationSQL(ForeignKeyConstraint $foreignKey)
2579
    {
2580 1108
        $sql = '';
2581 1108
        if (strlen($foreignKey->getName())) {
2582 932
            $sql .= 'CONSTRAINT ' . $foreignKey->getQuotedName($this) . ' ';
2583
        }
2584 1108
        $sql .= 'FOREIGN KEY (';
2585
2586 1108
        if (count($foreignKey->getLocalColumns()) === 0) {
2587
            throw new \InvalidArgumentException("Incomplete definition. 'local' required.");
2588
        }
2589 1108
        if (count($foreignKey->getForeignColumns()) === 0) {
2590
            throw new \InvalidArgumentException("Incomplete definition. 'foreign' required.");
2591
        }
2592 1108
        if (strlen($foreignKey->getForeignTableName()) === 0) {
2593
            throw new \InvalidArgumentException("Incomplete definition. 'foreignTable' required.");
2594
        }
2595
2596 1108
        $sql .= implode(', ', $foreignKey->getQuotedLocalColumns($this))
2597 1108
            . ') REFERENCES '
2598 1108
            . $foreignKey->getQuotedForeignTableName($this) . ' ('
2599 1108
            . implode(', ', $foreignKey->getQuotedForeignColumns($this)) . ')';
2600
2601 1108
        return $sql;
2602
    }
2603
2604
    /**
2605
     * Obtains DBMS specific SQL code portion needed to set the UNIQUE constraint
2606
     * of a field declaration to be used in statements like CREATE TABLE.
2607
     *
2608
     * @return string DBMS specific SQL code portion needed to set the UNIQUE constraint
2609
     *                of a field declaration.
2610
     */
2611
    public function getUniqueFieldDeclarationSQL()
2612
    {
2613
        return 'UNIQUE';
2614
    }
2615
2616
    /**
2617
     * Obtains DBMS specific SQL code portion needed to set the CHARACTER SET
2618
     * of a field declaration to be used in statements like CREATE TABLE.
2619
     *
2620
     * @param string $charset The name of the charset.
2621
     *
2622
     * @return string DBMS specific SQL code portion needed to set the CHARACTER SET
2623
     *                of a field declaration.
2624
     */
2625
    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

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

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

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

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

Loading history...
2890
    {
2891
        throw NotSupported::new(__METHOD__);
2892
    }
2893
2894
    /**
2895
     * @param string $table
2896
     *
2897
     * @return string
2898
     *
2899
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
2900
     */
2901
    public function getListTableForeignKeysSQL($table)
2902
    {
2903
        throw NotSupported::new(__METHOD__);
2904
    }
2905
2906
    /**
2907
     * @param string $name
2908
     * @param string $sql
2909
     *
2910
     * @return string
2911
     *
2912
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
2913
     */
2914
    public function getCreateViewSQL($name, $sql)
2915
    {
2916
        throw NotSupported::new(__METHOD__);
2917
    }
2918
2919
    /**
2920
     * @param string $name
2921
     *
2922
     * @return string
2923
     *
2924
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
2925
     */
2926
    public function getDropViewSQL($name)
2927
    {
2928
        throw NotSupported::new(__METHOD__);
2929
    }
2930
2931
    /**
2932
     * Returns the SQL snippet to drop an existing sequence.
2933
     *
2934
     * @param Sequence|string $sequence
2935
     *
2936
     * @return string
2937
     *
2938
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
2939
     */
2940
    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

2940
    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...
2941
    {
2942
        throw NotSupported::new(__METHOD__);
2943
    }
2944
2945
    /**
2946
     * @param string $sequenceName
2947
     *
2948
     * @return string
2949
     *
2950
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
2951
     */
2952
    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

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

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