16
16
use Doctrine \DBAL \LockMode ;
17
17
use Doctrine \DBAL \Platforms \Exception \NoColumnsSpecifiedForTable ;
18
18
use Doctrine \DBAL \Platforms \Exception \NotSupported ;
19
+ use Doctrine \DBAL \Platforms \Exception \UnsupportedPrimaryKeyConstraintDefinition ;
19
20
use Doctrine \DBAL \Schema \AbstractSchemaManager ;
20
21
use Doctrine \DBAL \Schema \Column ;
21
22
use Doctrine \DBAL \Schema \Exception \UnspecifiedConstraintName ;
26
27
use Doctrine \DBAL \Schema \Name \OptionallyQualifiedName ;
27
28
use Doctrine \DBAL \Schema \Name \UnqualifiedName ;
28
29
use Doctrine \DBAL \Schema \Name \UnquotedIdentifierFolding ;
30
+ use Doctrine \DBAL \Schema \PrimaryKeyConstraint ;
29
31
use Doctrine \DBAL \Schema \SchemaDiff ;
30
32
use Doctrine \DBAL \Schema \Sequence ;
31
33
use Doctrine \DBAL \Schema \Table ;
42
44
use Doctrine \DBAL \Types \Exception \TypeNotFound ;
43
45
use Doctrine \DBAL \Types \Exception \TypesException ;
44
46
use Doctrine \DBAL \Types \Type ;
45
- use Doctrine \Deprecations \Deprecation ;
46
47
47
48
use function addcslashes ;
48
49
use function array_map ;
71
72
*
72
73
* @phpstan-import-type ColumnProperties from Column
73
74
* @phpstan-type CreateTableParameters = array{
74
- * primary_index?: Index,
75
75
* indexes: list<Index>,
76
+ * primaryKey: ?PrimaryKeyConstraint,
76
77
* uniqueConstraints: list<UniqueConstraint>,
77
78
* foreignKeys: list<ForeignKeyConstraint>,
78
79
* comment?: string,
@@ -834,18 +835,13 @@ private function buildCreateTableSQL(Table $table, bool $createForeignKeys): arr
834
835
835
836
$ tableName = $ table ->getObjectName ();
836
837
$ parameters = $ table ->getOptions ();
838
+ $ parameters ['primaryKey ' ] = $ table ->getPrimaryKeyConstraint ();
837
839
$ parameters ['indexes ' ] = [];
838
840
$ parameters ['uniqueConstraints ' ] = [];
839
841
$ parameters ['foreignKeys ' ] = [];
840
842
841
843
foreach ($ table ->getIndexes () as $ index ) {
842
- if (! $ index ->isPrimary ()) {
843
- $ parameters ['indexes ' ][] = $ index ;
844
-
845
- continue ;
846
- }
847
-
848
- $ parameters ['primary_index ' ] = $ index ;
844
+ $ parameters ['indexes ' ][] = $ index ;
849
845
}
850
846
851
847
foreach ($ table ->getUniqueConstraints () as $ uniqueConstraint ) {
@@ -995,11 +991,8 @@ protected function _getCreateTableSQL(OptionallyQualifiedName $tableName, array
995
991
$ elements [] = $ this ->getUniqueConstraintDeclarationSQL ($ definition );
996
992
}
997
993
998
- if (isset ($ parameters ['primary_index ' ])) {
999
- $ elements [] = sprintf (
1000
- 'PRIMARY KEY (%s) ' ,
1001
- implode (', ' , $ parameters ['primary_index ' ]->getQuotedColumns ($ this )),
1002
- );
994
+ if (isset ($ parameters ['primaryKey ' ])) {
995
+ $ elements [] = $ this ->getPrimaryKeyConstraintDeclarationSQL ($ parameters ['primaryKey ' ]);
1003
996
}
1004
997
1005
998
foreach ($ parameters ['indexes ' ] as $ definition ) {
@@ -1114,10 +1107,6 @@ public function getCreateIndexSQL(Index $index, string $table): string
1114
1107
));
1115
1108
}
1116
1109
1117
- if ($ index ->isPrimary ()) {
1118
- return $ this ->getCreatePrimaryKeySQL ($ index , $ table );
1119
- }
1120
-
1121
1110
$ query = 'CREATE ' . $ this ->getCreateIndexSQLFlags ($ index ) . 'INDEX ' . $ name . ' ON ' . $ table ;
1122
1111
$ query .= ' ( ' . implode (', ' , $ index ->getQuotedColumns ($ this )) . ') ' . $ this ->getPartialIndexSQL ($ index );
1123
1112
@@ -1144,23 +1133,6 @@ protected function getCreateIndexSQLFlags(Index $index): string
1144
1133
return $ index ->isUnique () ? 'UNIQUE ' : '' ;
1145
1134
}
1146
1135
1147
- /**
1148
- * Returns the SQL to create an unnamed primary key constraint.
1149
- *
1150
- * @deprecated
1151
- */
1152
- public function getCreatePrimaryKeySQL (Index $ index , string $ table ): string
1153
- {
1154
- Deprecation::triggerIfCalledFromOutside (
1155
- 'doctrine/dbal ' ,
1156
- 'https://github.com/doctrine/dbal/pull/6867 ' ,
1157
- '%s() is deprecated. ' ,
1158
- __METHOD__ ,
1159
- );
1160
-
1161
- return 'ALTER TABLE ' . $ table . ' ADD PRIMARY KEY ( ' . implode (', ' , $ index ->getQuotedColumns ($ this )) . ') ' ;
1162
- }
1163
-
1164
1136
/**
1165
1137
* Returns the SQL to create a named schema.
1166
1138
*/
@@ -1470,10 +1442,7 @@ protected function getUniqueConstraintDeclarationSQL(UniqueConstraint $constrain
1470
1442
$ chunks [] = 'CLUSTERED ' ;
1471
1443
}
1472
1444
1473
- $ chunks [] = sprintf ('(%s) ' , implode (', ' , array_map (
1474
- fn (UnqualifiedName $ columnName ) => $ columnName ->toSQL ($ this ),
1475
- $ constraint ->getColumnNames (),
1476
- )));
1445
+ $ chunks [] = $ this ->buildUnqualifiedNameListSQL ($ constraint ->getColumnNames ());
1477
1446
1478
1447
return implode (' ' , $ chunks );
1479
1448
}
@@ -1506,6 +1475,48 @@ public function getTemporaryTableName(string $tableName): string
1506
1475
return $ tableName ;
1507
1476
}
1508
1477
1478
+ /**
1479
+ * Returns declaration of a primary key constraint.
1480
+ */
1481
+ protected function getPrimaryKeyConstraintDeclarationSQL (PrimaryKeyConstraint $ constraint ): string
1482
+ {
1483
+ $ chunks = [];
1484
+
1485
+ $ name = $ constraint ->getObjectName ();
1486
+ if ($ name !== null ) {
1487
+ $ chunks [] = 'CONSTRAINT ' ;
1488
+ $ chunks [] = $ name ->toSQL ($ this );
1489
+ }
1490
+
1491
+ $ chunks [] = 'PRIMARY KEY ' ;
1492
+
1493
+ if (! $ constraint ->isClustered ()) {
1494
+ $ chunks [] = 'NONCLUSTERED ' ;
1495
+ }
1496
+
1497
+ $ chunks [] = $ this ->buildUnqualifiedNameListSQL ($ constraint ->getColumnNames ());
1498
+
1499
+ return implode (' ' , $ chunks );
1500
+ }
1501
+
1502
+ final protected function ensurePrimaryKeyConstraintIsNotNamed (PrimaryKeyConstraint $ constraint ): void
1503
+ {
1504
+ if ($ constraint ->getObjectName () === null ) {
1505
+ return ;
1506
+ }
1507
+
1508
+ throw UnsupportedPrimaryKeyConstraintDefinition::fromNamedConstraint (static ::class);
1509
+ }
1510
+
1511
+ final protected function ensurePrimaryKeyConstraintIsClustered (PrimaryKeyConstraint $ constraint ): void
1512
+ {
1513
+ if ($ constraint ->isClustered ()) {
1514
+ return ;
1515
+ }
1516
+
1517
+ throw UnsupportedPrimaryKeyConstraintDefinition::fromNonClusteredConstraint (static ::class);
1518
+ }
1519
+
1509
1520
/**
1510
1521
* Obtain DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
1511
1522
* of a column declaration to be used in statements like CREATE TABLE.
@@ -1560,25 +1571,21 @@ protected function getForeignKeyReferentialActionSQL(ReferentialAction $action):
1560
1571
*/
1561
1572
protected function getForeignKeyBaseDeclarationSQL (ForeignKeyConstraint $ foreignKey ): string
1562
1573
{
1563
- $ name = $ foreignKey -> getObjectName () ;
1574
+ $ chunks = [] ;
1564
1575
1565
- $ sql = '' ;
1576
+ $ name = $ foreignKey -> getObjectName () ;
1566
1577
if ($ name !== null ) {
1567
- $ sql .= 'CONSTRAINT ' . $ name ->toSQL ($ this ) . ' ' ;
1568
- }
1569
-
1570
- return $ sql . sprintf (
1571
- 'FOREIGN KEY (%s) REFERENCES %s (%s) ' ,
1572
- implode (', ' , array_map (
1573
- fn (UnqualifiedName $ columnName ) => $ columnName ->toSQL ($ this ),
1574
- $ foreignKey ->getReferencingColumnNames (),
1575
- )),
1576
- $ foreignKey ->getReferencedTableName ()->toSQL ($ this ),
1577
- implode (', ' , array_map (
1578
- fn (UnqualifiedName $ columnName ) => $ columnName ->toSQL ($ this ),
1579
- $ foreignKey ->getReferencedColumnNames (),
1580
- )),
1581
- );
1578
+ $ chunks [] = 'CONSTRAINT ' ;
1579
+ $ chunks [] = $ name ->toSQL ($ this );
1580
+ }
1581
+
1582
+ $ chunks [] = 'FOREIGN KEY ' ;
1583
+ $ chunks [] = $ this ->buildUnqualifiedNameListSQL ($ foreignKey ->getReferencingColumnNames ());
1584
+ $ chunks [] = 'REFERENCES ' ;
1585
+ $ chunks [] = $ foreignKey ->getReferencedTableName ()->toSQL ($ this );
1586
+ $ chunks [] = $ this ->buildUnqualifiedNameListSQL ($ foreignKey ->getReferencedColumnNames ());
1587
+
1588
+ return implode (' ' , $ chunks );
1582
1589
}
1583
1590
1584
1591
/**
@@ -1609,6 +1616,15 @@ protected function getColumnCollationDeclarationSQL(string $collation): string
1609
1616
return $ this ->supportsColumnCollation () ? 'COLLATE ' . $ this ->quoteSingleIdentifier ($ collation ) : '' ;
1610
1617
}
1611
1618
1619
+ /** @param non-empty-list<UnqualifiedName> $names */
1620
+ private function buildUnqualifiedNameListSQL (array $ names ): string
1621
+ {
1622
+ return sprintf ('(%s) ' , implode (', ' , array_map (
1623
+ fn (UnqualifiedName $ columnName ) => $ columnName ->toSQL ($ this ),
1624
+ $ names ,
1625
+ )));
1626
+ }
1627
+
1612
1628
/**
1613
1629
* Some platforms need the boolean values to be converted.
1614
1630
*
0 commit comments