-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
AbstractPlatform.php
2219 lines (1908 loc) · 70.9 KB
/
AbstractPlatform.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\InvalidArgumentException;
use Doctrine\DBAL\Exception\InvalidColumnDeclaration;
use Doctrine\DBAL\Exception\InvalidColumnType;
use Doctrine\DBAL\Exception\InvalidColumnType\ColumnLengthRequired;
use Doctrine\DBAL\Exception\InvalidColumnType\ColumnPrecisionRequired;
use Doctrine\DBAL\Exception\InvalidColumnType\ColumnScaleRequired;
use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\Platforms\Exception\NoColumnsSpecifiedForTable;
use Doctrine\DBAL\Platforms\Exception\NotSupported;
use Doctrine\DBAL\Platforms\Keywords\KeywordList;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\SchemaDiff;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\UniqueConstraint;
use Doctrine\DBAL\SQL\Builder\DefaultSelectSQLBuilder;
use Doctrine\DBAL\SQL\Builder\SelectSQLBuilder;
use Doctrine\DBAL\SQL\Parser;
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types;
use Doctrine\DBAL\Types\Exception\TypeNotFound;
use Doctrine\DBAL\Types\Type;
use function addcslashes;
use function array_map;
use function array_merge;
use function array_unique;
use function array_values;
use function assert;
use function count;
use function explode;
use function implode;
use function in_array;
use function is_array;
use function is_bool;
use function is_float;
use function is_int;
use function is_string;
use function preg_quote;
use function preg_replace;
use function sprintf;
use function str_contains;
use function str_replace;
use function strlen;
use function strtolower;
use function strtoupper;
/**
* Base class for all DatabasePlatforms. The DatabasePlatforms are the central
* point of abstraction of platform-specific behaviors, features and SQL dialects.
* They are a passive source of information.
*
* @todo Remove any unnecessary methods.
*/
abstract class AbstractPlatform
{
/** @deprecated */
public const CREATE_INDEXES = 1;
/** @deprecated */
public const CREATE_FOREIGNKEYS = 2;
/** @var string[]|null */
protected ?array $doctrineTypeMapping = null;
/**
* Holds the KeywordList instance for the current platform.
*/
protected ?KeywordList $_keywords = null;
/**
* Returns the SQL snippet that declares a boolean column.
*
* @param mixed[] $column
*/
abstract public function getBooleanTypeDeclarationSQL(array $column): string;
/**
* Returns the SQL snippet that declares a 4 byte integer column.
*
* @param mixed[] $column
*/
abstract public function getIntegerTypeDeclarationSQL(array $column): string;
/**
* Returns the SQL snippet that declares an 8 byte integer column.
*
* @param mixed[] $column
*/
abstract public function getBigIntTypeDeclarationSQL(array $column): string;
/**
* Returns the SQL snippet that declares a 2 byte integer column.
*
* @param mixed[] $column
*/
abstract public function getSmallIntTypeDeclarationSQL(array $column): string;
/**
* Returns the SQL snippet that declares common properties of an integer column.
*
* @param mixed[] $column
*/
abstract protected function _getCommonIntegerTypeDeclarationSQL(array $column): string;
/**
* Lazy load Doctrine Type Mappings.
*/
abstract protected function initializeDoctrineTypeMappings(): void;
/**
* Initializes Doctrine Type Mappings with the platform defaults
* and with all additional type mappings.
*/
private function initializeAllDoctrineTypeMappings(): void
{
$this->initializeDoctrineTypeMappings();
foreach (Type::getTypesMap() as $typeName => $className) {
foreach (Type::getType($typeName)->getMappedDatabaseTypes($this) as $dbType) {
$dbType = strtolower($dbType);
$this->doctrineTypeMapping[$dbType] = $typeName;
}
}
}
/**
* Returns the SQL snippet used to declare a column that can
* store characters in the ASCII character set
*
* @param array<string, mixed> $column The column definition.
*/
public function getAsciiStringTypeDeclarationSQL(array $column): string
{
return $this->getStringTypeDeclarationSQL($column);
}
/**
* Returns the SQL snippet used to declare a string column type.
*
* @param array<string, mixed> $column The column definition.
*/
public function getStringTypeDeclarationSQL(array $column): string
{
$length = $column['length'] ?? null;
if (empty($column['fixed'])) {
try {
return $this->getVarcharTypeDeclarationSQLSnippet($length);
} catch (InvalidColumnType $e) {
throw InvalidColumnDeclaration::fromInvalidColumnType($column['name'], $e);
}
}
return $this->getCharTypeDeclarationSQLSnippet($length);
}
/**
* Returns the SQL snippet used to declare a binary string column type.
*
* @param array<string, mixed> $column The column definition.
*/
public function getBinaryTypeDeclarationSQL(array $column): string
{
$length = $column['length'] ?? null;
try {
if (empty($column['fixed'])) {
return $this->getVarbinaryTypeDeclarationSQLSnippet($length);
}
return $this->getBinaryTypeDeclarationSQLSnippet($length);
} catch (InvalidColumnType $e) {
throw InvalidColumnDeclaration::fromInvalidColumnType($column['name'], $e);
}
}
/**
* Returns the SQL snippet to declare a GUID/UUID column.
*
* By default this maps directly to a CHAR(36) and only maps to more
* special datatypes when the underlying databases support this datatype.
*
* @param array<string, mixed> $column The column definition.
*/
public function getGuidTypeDeclarationSQL(array $column): string
{
$column['length'] = 36;
$column['fixed'] = true;
return $this->getStringTypeDeclarationSQL($column);
}
/**
* Returns the SQL snippet to declare a JSON column.
*
* By default this maps directly to a CLOB and only maps to more
* special datatypes when the underlying databases support this datatype.
*
* @param mixed[] $column
*/
public function getJsonTypeDeclarationSQL(array $column): string
{
return $this->getClobTypeDeclarationSQL($column);
}
/**
* @param int|null $length The length of the column in characters
* or NULL if the length should be omitted.
*/
protected function getCharTypeDeclarationSQLSnippet(?int $length): string
{
$sql = 'CHAR';
if ($length !== null) {
$sql .= sprintf('(%d)', $length);
}
return $sql;
}
/**
* @param int|null $length The length of the column in characters
* or NULL if the length should be omitted.
*/
protected function getVarcharTypeDeclarationSQLSnippet(?int $length): string
{
if ($length === null) {
throw ColumnLengthRequired::new($this, 'VARCHAR');
}
return sprintf('VARCHAR(%d)', $length);
}
/**
* Returns the SQL snippet used to declare a fixed length binary column type.
*
* @param int|null $length The length of the column in bytes
* or NULL if the length should be omitted.
*/
protected function getBinaryTypeDeclarationSQLSnippet(?int $length): string
{
$sql = 'BINARY';
if ($length !== null) {
$sql .= sprintf('(%d)', $length);
}
return $sql;
}
/**
* Returns the SQL snippet used to declare a variable length binary column type.
*
* @param int|null $length The length of the column in bytes
* or NULL if the length should be omitted.
*/
protected function getVarbinaryTypeDeclarationSQLSnippet(?int $length): string
{
if ($length === null) {
throw ColumnLengthRequired::new($this, 'VARBINARY');
}
return sprintf('VARBINARY(%d)', $length);
}
/**
* Returns the SQL snippet used to declare a CLOB column type.
*
* @param mixed[] $column
*/
abstract public function getClobTypeDeclarationSQL(array $column): string;
/**
* Returns the SQL Snippet used to declare a BLOB column type.
*
* @param mixed[] $column
*/
abstract public function getBlobTypeDeclarationSQL(array $column): string;
/**
* Registers a doctrine type to be used in conjunction with a column type of this platform.
*
* @throws Exception If the type is not found.
*/
public function registerDoctrineTypeMapping(string $dbType, string $doctrineType): void
{
if ($this->doctrineTypeMapping === null) {
$this->initializeAllDoctrineTypeMappings();
}
if (! Types\Type::hasType($doctrineType)) {
throw TypeNotFound::new($doctrineType);
}
$dbType = strtolower($dbType);
$this->doctrineTypeMapping[$dbType] = $doctrineType;
}
/**
* Gets the Doctrine type that is mapped for the given database column type.
*/
public function getDoctrineTypeMapping(string $dbType): string
{
if ($this->doctrineTypeMapping === null) {
$this->initializeAllDoctrineTypeMappings();
}
$dbType = strtolower($dbType);
if (! isset($this->doctrineTypeMapping[$dbType])) {
throw new InvalidArgumentException(sprintf(
'Unknown database type "%s" requested, %s may not support it.',
$dbType,
static::class,
));
}
return $this->doctrineTypeMapping[$dbType];
}
/**
* Checks if a database type is currently supported by this platform.
*/
public function hasDoctrineTypeMappingFor(string $dbType): bool
{
if ($this->doctrineTypeMapping === null) {
$this->initializeAllDoctrineTypeMappings();
}
$dbType = strtolower($dbType);
return isset($this->doctrineTypeMapping[$dbType]);
}
/**
* Returns the regular expression operator.
*/
public function getRegexpExpression(): string
{
throw NotSupported::new(__METHOD__);
}
/**
* Returns the SQL snippet to get the length of a text column in characters.
*
* @param string $string SQL expression producing the string.
*/
public function getLengthExpression(string $string): string
{
return 'LENGTH(' . $string . ')';
}
/**
* Returns the SQL snippet to get the remainder of the operation of division of dividend by divisor.
*
* @param string $dividend SQL expression producing the dividend.
* @param string $divisor SQL expression producing the divisor.
*/
public function getModExpression(string $dividend, string $divisor): string
{
return 'MOD(' . $dividend . ', ' . $divisor . ')';
}
/**
* Returns the SQL snippet to trim a string.
*
* @param string $str The expression to apply the trim to.
* @param TrimMode $mode The position of the trim.
* @param string|null $char The char to trim, has to be quoted already. Defaults to space.
*/
public function getTrimExpression(
string $str,
TrimMode $mode = TrimMode::UNSPECIFIED,
?string $char = null,
): string {
$tokens = [];
switch ($mode) {
case TrimMode::UNSPECIFIED:
break;
case TrimMode::LEADING:
$tokens[] = 'LEADING';
break;
case TrimMode::TRAILING:
$tokens[] = 'TRAILING';
break;
case TrimMode::BOTH:
$tokens[] = 'BOTH';
break;
}
if ($char !== null) {
$tokens[] = $char;
}
if (count($tokens) > 0) {
$tokens[] = 'FROM';
}
$tokens[] = $str;
return sprintf('TRIM(%s)', implode(' ', $tokens));
}
/**
* Returns the SQL snippet to get the position of the first occurrence of the substring in the string.
*
* @param string $string SQL expression producing the string to locate the substring in.
* @param string $substring SQL expression producing the substring to locate.
* @param string|null $start SQL expression producing the position to start at.
* Defaults to the beginning of the string.
*/
abstract public function getLocateExpression(string $string, string $substring, ?string $start = null): string;
/**
* Returns an SQL snippet to get a substring inside the string.
*
* Note: Not SQL92, but common functionality.
*
* @param string $string SQL expression producing the string from which a substring should be extracted.
* @param string $start SQL expression producing the position to start at,
* @param string|null $length SQL expression producing the length of the substring portion to be returned.
* By default, the entire substring is returned.
*/
public function getSubstringExpression(string $string, string $start, ?string $length = null): string
{
if ($length === null) {
return sprintf('SUBSTRING(%s FROM %s)', $string, $start);
}
return sprintf('SUBSTRING(%s FROM %s FOR %s)', $string, $start, $length);
}
/**
* Returns a SQL snippet to concatenate the given strings.
*/
public function getConcatExpression(string ...$string): string
{
return implode(' || ', $string);
}
/**
* Returns the SQL to calculate the difference in days between the two passed dates.
*
* Computes diff = date1 - date2.
*/
abstract public function getDateDiffExpression(string $date1, string $date2): string;
/**
* Returns the SQL to add the number of given seconds to a date.
*
* @param string $date SQL expression producing the date.
* @param string $seconds SQL expression producing the number of seconds.
*/
public function getDateAddSecondsExpression(string $date, string $seconds): string
{
return $this->getDateArithmeticIntervalExpression($date, '+', $seconds, DateIntervalUnit::SECOND);
}
/**
* Returns the SQL to subtract the number of given seconds from a date.
*
* @param string $date SQL expression producing the date.
* @param string $seconds SQL expression producing the number of seconds.
*/
public function getDateSubSecondsExpression(string $date, string $seconds): string
{
return $this->getDateArithmeticIntervalExpression($date, '-', $seconds, DateIntervalUnit::SECOND);
}
/**
* Returns the SQL to add the number of given minutes to a date.
*
* @param string $date SQL expression producing the date.
* @param string $minutes SQL expression producing the number of minutes.
*/
public function getDateAddMinutesExpression(string $date, string $minutes): string
{
return $this->getDateArithmeticIntervalExpression($date, '+', $minutes, DateIntervalUnit::MINUTE);
}
/**
* Returns the SQL to subtract the number of given minutes from a date.
*
* @param string $date SQL expression producing the date.
* @param string $minutes SQL expression producing the number of minutes.
*/
public function getDateSubMinutesExpression(string $date, string $minutes): string
{
return $this->getDateArithmeticIntervalExpression($date, '-', $minutes, DateIntervalUnit::MINUTE);
}
/**
* Returns the SQL to add the number of given hours to a date.
*
* @param string $date SQL expression producing the date.
* @param string $hours SQL expression producing the number of hours.
*/
public function getDateAddHourExpression(string $date, string $hours): string
{
return $this->getDateArithmeticIntervalExpression($date, '+', $hours, DateIntervalUnit::HOUR);
}
/**
* Returns the SQL to subtract the number of given hours to a date.
*
* @param string $date SQL expression producing the date.
* @param string $hours SQL expression producing the number of hours.
*/
public function getDateSubHourExpression(string $date, string $hours): string
{
return $this->getDateArithmeticIntervalExpression($date, '-', $hours, DateIntervalUnit::HOUR);
}
/**
* Returns the SQL to add the number of given days to a date.
*
* @param string $date SQL expression producing the date.
* @param string $days SQL expression producing the number of days.
*/
public function getDateAddDaysExpression(string $date, string $days): string
{
return $this->getDateArithmeticIntervalExpression($date, '+', $days, DateIntervalUnit::DAY);
}
/**
* Returns the SQL to subtract the number of given days to a date.
*
* @param string $date SQL expression producing the date.
* @param string $days SQL expression producing the number of days.
*/
public function getDateSubDaysExpression(string $date, string $days): string
{
return $this->getDateArithmeticIntervalExpression($date, '-', $days, DateIntervalUnit::DAY);
}
/**
* Returns the SQL to add the number of given weeks to a date.
*
* @param string $date SQL expression producing the date.
* @param string $weeks SQL expression producing the number of weeks.
*/
public function getDateAddWeeksExpression(string $date, string $weeks): string
{
return $this->getDateArithmeticIntervalExpression($date, '+', $weeks, DateIntervalUnit::WEEK);
}
/**
* Returns the SQL to subtract the number of given weeks from a date.
*
* @param string $date SQL expression producing the date.
* @param string $weeks SQL expression producing the number of weeks.
*/
public function getDateSubWeeksExpression(string $date, string $weeks): string
{
return $this->getDateArithmeticIntervalExpression($date, '-', $weeks, DateIntervalUnit::WEEK);
}
/**
* Returns the SQL to add the number of given months to a date.
*
* @param string $date SQL expression producing the date.
* @param string $months SQL expression producing the number of months.
*/
public function getDateAddMonthExpression(string $date, string $months): string
{
return $this->getDateArithmeticIntervalExpression($date, '+', $months, DateIntervalUnit::MONTH);
}
/**
* Returns the SQL to subtract the number of given months to a date.
*
* @param string $date SQL expression producing the date.
* @param string $months SQL expression producing the number of months.
*/
public function getDateSubMonthExpression(string $date, string $months): string
{
return $this->getDateArithmeticIntervalExpression($date, '-', $months, DateIntervalUnit::MONTH);
}
/**
* Returns the SQL to add the number of given quarters to a date.
*
* @param string $date SQL expression producing the date.
* @param string $quarters SQL expression producing the number of quarters.
*/
public function getDateAddQuartersExpression(string $date, string $quarters): string
{
return $this->getDateArithmeticIntervalExpression($date, '+', $quarters, DateIntervalUnit::QUARTER);
}
/**
* Returns the SQL to subtract the number of given quarters from a date.
*
* @param string $date SQL expression producing the date.
* @param string $quarters SQL expression producing the number of quarters.
*/
public function getDateSubQuartersExpression(string $date, string $quarters): string
{
return $this->getDateArithmeticIntervalExpression($date, '-', $quarters, DateIntervalUnit::QUARTER);
}
/**
* Returns the SQL to add the number of given years to a date.
*
* @param string $date SQL expression producing the date.
* @param string $years SQL expression producing the number of years.
*/
public function getDateAddYearsExpression(string $date, string $years): string
{
return $this->getDateArithmeticIntervalExpression($date, '+', $years, DateIntervalUnit::YEAR);
}
/**
* Returns the SQL to subtract the number of given years from a date.
*
* @param string $date SQL expression producing the date.
* @param string $years SQL expression producing the number of years.
*/
public function getDateSubYearsExpression(string $date, string $years): string
{
return $this->getDateArithmeticIntervalExpression($date, '-', $years, DateIntervalUnit::YEAR);
}
/**
* Returns the SQL for a date arithmetic expression.
*
* @param string $date SQL expression representing a date to perform the arithmetic operation on.
* @param string $operator The arithmetic operator (+ or -).
* @param string $interval SQL expression representing the value of the interval that shall be calculated
* into the date.
* @param DateIntervalUnit $unit The unit of the interval that shall be calculated into the date.
*/
abstract protected function getDateArithmeticIntervalExpression(
string $date,
string $operator,
string $interval,
DateIntervalUnit $unit,
): string;
/**
* Generates the SQL expression which represents the given date interval multiplied by a number
*
* @param string $interval SQL expression describing the interval value
* @param int $multiplier Interval multiplier
*/
protected function multiplyInterval(string $interval, int $multiplier): string
{
return sprintf('(%s * %d)', $interval, $multiplier);
}
/**
* Returns the SQL bit AND comparison expression.
*
* @param string $value1 SQL expression producing the first value.
* @param string $value2 SQL expression producing the second value.
*/
public function getBitAndComparisonExpression(string $value1, string $value2): string
{
return '(' . $value1 . ' & ' . $value2 . ')';
}
/**
* Returns the SQL bit OR comparison expression.
*
* @param string $value1 SQL expression producing the first value.
* @param string $value2 SQL expression producing the second value.
*/
public function getBitOrComparisonExpression(string $value1, string $value2): string
{
return '(' . $value1 . ' | ' . $value2 . ')';
}
/**
* Returns the SQL expression which represents the currently selected database.
*/
abstract public function getCurrentDatabaseExpression(): string;
/**
* Honors that some SQL vendors such as MsSql use table hints for locking instead of the
* ANSI SQL FOR UPDATE specification.
*
* @param string $fromClause The FROM clause to append the hint for the given lock mode to
*/
public function appendLockHint(string $fromClause, LockMode $lockMode): string
{
return $fromClause;
}
/**
* Returns the SQL snippet to drop an existing table.
*/
public function getDropTableSQL(string $table): string
{
return 'DROP TABLE ' . $table;
}
/**
* Returns the SQL to safely drop a temporary table WITHOUT implicitly committing an open transaction.
*/
public function getDropTemporaryTableSQL(string $table): string
{
return $this->getDropTableSQL($table);
}
/**
* Returns the SQL to drop an index from a table.
*/
public function getDropIndexSQL(string $name, string $table): string
{
return 'DROP INDEX ' . $name;
}
/**
* Returns the SQL to drop a constraint.
*
* @internal The method should be only used from within the {@see AbstractPlatform} class hierarchy.
*/
protected function getDropConstraintSQL(string $name, string $table): string
{
return 'ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $name;
}
/**
* Returns the SQL to drop a foreign key.
*/
public function getDropForeignKeySQL(string $foreignKey, string $table): string
{
return 'ALTER TABLE ' . $table . ' DROP FOREIGN KEY ' . $foreignKey;
}
/**
* Returns the SQL to drop a unique constraint.
*/
public function getDropUniqueConstraintSQL(string $name, string $tableName): string
{
return $this->getDropConstraintSQL($name, $tableName);
}
/**
* Returns the SQL statement(s) to create a table with the specified name, columns and constraints
* on this platform.
*
* @return list<string> The list of SQL statements.
*/
public function getCreateTableSQL(Table $table): array
{
return $this->buildCreateTableSQL($table, true);
}
public function createSelectSQLBuilder(): SelectSQLBuilder
{
return new DefaultSelectSQLBuilder($this, 'FOR UPDATE', 'SKIP LOCKED');
}
/**
* @internal
*
* @return list<string>
*/
final protected function getCreateTableWithoutForeignKeysSQL(Table $table): array
{
return $this->buildCreateTableSQL($table, false);
}
/** @return list<string> */
private function buildCreateTableSQL(Table $table, bool $createForeignKeys): array
{
if (count($table->getColumns()) === 0) {
throw NoColumnsSpecifiedForTable::new($table->getName());
}
$tableName = $table->getQuotedName($this);
$options = $table->getOptions();
$options['uniqueConstraints'] = [];
$options['indexes'] = [];
$options['primary'] = [];
foreach ($table->getIndexes() as $index) {
if (! $index->isPrimary()) {
$options['indexes'][$index->getQuotedName($this)] = $index;
continue;
}
$options['primary'] = $index->getQuotedColumns($this);
$options['primary_index'] = $index;
}
foreach ($table->getUniqueConstraints() as $uniqueConstraint) {
$options['uniqueConstraints'][$uniqueConstraint->getQuotedName($this)] = $uniqueConstraint;
}
if ($createForeignKeys) {
$options['foreignKeys'] = [];
foreach ($table->getForeignKeys() as $fkConstraint) {
$options['foreignKeys'][] = $fkConstraint;
}
}
$columnSql = [];
$columns = [];
foreach ($table->getColumns() as $column) {
$columnData = $this->columnToArray($column);
if (in_array($column->getName(), $options['primary'], true)) {
$columnData['primary'] = true;
}
$columns[] = $columnData;
}
$sql = $this->_getCreateTableSQL($tableName, $columns, $options);
if ($this->supportsCommentOnStatement()) {
if ($table->hasOption('comment')) {
$sql[] = $this->getCommentOnTableSQL($tableName, $table->getOption('comment'));
}
foreach ($table->getColumns() as $column) {
$comment = $column->getComment();
if ($comment === '') {
continue;
}
$sql[] = $this->getCommentOnColumnSQL($tableName, $column->getQuotedName($this), $comment);
}
}
return array_merge($sql, $columnSql);
}
/**
* @param array<Table> $tables
*
* @return list<string>
*/
public function getCreateTablesSQL(array $tables): array
{
$sql = [];
foreach ($tables as $table) {
$sql = array_merge($sql, $this->getCreateTableWithoutForeignKeysSQL($table));
}
foreach ($tables as $table) {
foreach ($table->getForeignKeys() as $foreignKey) {
$sql[] = $this->getCreateForeignKeySQL(
$foreignKey,
$table->getQuotedName($this),
);
}
}
return $sql;
}
/**
* @param array<Table> $tables
*
* @return list<string>
*/
public function getDropTablesSQL(array $tables): array
{
$sql = [];
foreach ($tables as $table) {
foreach ($table->getForeignKeys() as $foreignKey) {
$sql[] = $this->getDropForeignKeySQL(
$foreignKey->getQuotedName($this),
$table->getQuotedName($this),
);
}
}
foreach ($tables as $table) {
$sql[] = $this->getDropTableSQL($table->getQuotedName($this));
}
return $sql;
}
protected function getCommentOnTableSQL(string $tableName, string $comment): string
{
$tableName = new Identifier($tableName);
return sprintf(
'COMMENT ON TABLE %s IS %s',
$tableName->getQuotedName($this),
$this->quoteStringLiteral($comment),
);
}
/** @internal The method should be only used from within the {@see AbstractPlatform} class hierarchy. */
public function getCommentOnColumnSQL(string $tableName, string $columnName, string $comment): string
{
$tableName = new Identifier($tableName);
$columnName = new Identifier($columnName);
return sprintf(
'COMMENT ON COLUMN %s.%s IS %s',
$tableName->getQuotedName($this),
$columnName->getQuotedName($this),
$this->quoteStringLiteral($comment),
);
}
/**
* Returns the SQL to create inline comment on a column.
*
* @internal The method should be only used from within the {@see AbstractPlatform} class hierarchy.
*/
public function getInlineColumnCommentSQL(string $comment): string
{
if (! $this->supportsInlineColumnComments()) {
throw NotSupported::new(__METHOD__);
}
return 'COMMENT ' . $this->quoteStringLiteral($comment);
}
/**
* Returns the SQL used to create a table.
*
* @param mixed[][] $columns
* @param mixed[] $options
*
* @return array<int, string>
*/
protected function _getCreateTableSQL(string $name, array $columns, array $options = []): array
{
$columnListSql = $this->getColumnDeclarationListSQL($columns);
if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
foreach ($options['uniqueConstraints'] as $definition) {
$columnListSql .= ', ' . $this->getUniqueConstraintDeclarationSQL($definition);
}
}
if (isset($options['primary']) && ! empty($options['primary'])) {
$columnListSql .= ', PRIMARY KEY(' . implode(', ', array_unique(array_values($options['primary']))) . ')';
}
if (isset($options['indexes']) && ! empty($options['indexes'])) {
foreach ($options['indexes'] as $index => $definition) {
$columnListSql .= ', ' . $this->getIndexDeclarationSQL($definition);
}
}
$query = 'CREATE TABLE ' . $name . ' (' . $columnListSql;
$check = $this->getCheckDeclarationSQL($columns);
if (! empty($check)) {
$query .= ', ' . $check;
}
$query .= ')';
$sql = [$query];
if (isset($options['foreignKeys'])) {
foreach ($options['foreignKeys'] as $definition) {
$sql[] = $this->getCreateForeignKeySQL($definition, $name);
}
}
return $sql;
}
public function getCreateTemporaryTableSnippetSQL(): string
{
return 'CREATE TEMPORARY TABLE';
}
/**
* Generates SQL statements that can be used to apply the diff.
*
* @return list<string>
*/
public function getAlterSchemaSQL(SchemaDiff $diff): array