1010use Doctrine \DBAL \Platforms \SQLServer \SQL \Builder \SQLServerSelectSQLBuilder ;
1111use Doctrine \DBAL \Schema \Column ;
1212use Doctrine \DBAL \Schema \ColumnDiff ;
13+ use Doctrine \DBAL \Schema \Exception \InvalidName ;
1314use Doctrine \DBAL \Schema \Exception \UnspecifiedConstraintName ;
1415use Doctrine \DBAL \Schema \ForeignKeyConstraint \ReferentialAction ;
1516use Doctrine \DBAL \Schema \Identifier ;
1617use Doctrine \DBAL \Schema \Index ;
1718use Doctrine \DBAL \Schema \Index \IndexType ;
1819use Doctrine \DBAL \Schema \Name \OptionallyQualifiedName ;
20+ use Doctrine \DBAL \Schema \Name \Parser ;
21+ use Doctrine \DBAL \Schema \Name \Parsers ;
1922use Doctrine \DBAL \Schema \Name \UnqualifiedName ;
2023use Doctrine \DBAL \Schema \Name \UnquotedIdentifierFolding ;
2124use Doctrine \DBAL \Schema \Sequence ;
2831
2932use function array_map ;
3033use function array_merge ;
31- use function explode ;
3234use function implode ;
3335use function is_array ;
3436use function is_bool ;
3537use function is_numeric ;
3638use function preg_match ;
3739use function preg_match_all ;
3840use function sprintf ;
39- use function str_contains ;
40- use function str_ends_with ;
4141use function str_replace ;
42- use function str_starts_with ;
43- use function substr ;
4442use function substr_count ;
4543
4644use const PREG_OFFSET_CAPTURE ;
@@ -203,7 +201,7 @@ protected function _getCreateTableSQL(OptionallyQualifiedName $tableName, array
203201 }
204202
205203 $ commentsSql [] = $ this ->getCreateColumnCommentSQL (
206- $ tableName-> toSQL ( $ this ) ,
204+ $ tableName ,
207205 $ column ['name ' ],
208206 $ column ['comment ' ],
209207 );
@@ -240,13 +238,6 @@ protected function _getCreateTableSQL(OptionallyQualifiedName $tableName, array
240238 return array_merge ($ sql , $ commentsSql , $ defaultConstraintsSql );
241239 }
242240
243- private function unquoteSingleIdentifier (string $ possiblyQuotedName ): string
244- {
245- return str_starts_with ($ possiblyQuotedName , '[ ' ) && str_ends_with ($ possiblyQuotedName , '] ' )
246- ? substr ($ possiblyQuotedName , 1 , -1 )
247- : $ possiblyQuotedName ;
248- }
249-
250241 /**
251242 * Returns the SQL statement for creating a column comment.
252243 *
@@ -260,12 +251,15 @@ private function unquoteSingleIdentifier(string $possiblyQuotedName): string
260251 *
261252 * @link https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-addextendedproperty-transact-sql
262253 *
263- * @param string $tableName The quoted table name to which the column belongs.
264- * @param UnqualifiedName $columnName The column name to create the comment for.
265- * @param string $comment The column's comment.
254+ * @param OptionallyQualifiedName $tableName The name of the table to which the column belongs.
255+ * @param UnqualifiedName $columnName The column name to create the comment for.
256+ * @param string $comment The column's comment.
266257 */
267- private function getCreateColumnCommentSQL (string $ tableName , UnqualifiedName $ columnName , string $ comment ): string
268- {
258+ private function getCreateColumnCommentSQL (
259+ OptionallyQualifiedName $ tableName ,
260+ UnqualifiedName $ columnName ,
261+ string $ comment ,
262+ ): string {
269263 return $ this ->getExecSQL (
270264 'sp_addextendedproperty ' ,
271265 $ this ->quoteNationalStringLiteral ('MS_Description ' ),
@@ -331,7 +325,7 @@ public function getAlterTableSQL(TableDiff $diff): array
331325
332326 $ table = $ diff ->getOldTable ();
333327
334- $ tableName = $ table ->getName ();
328+ $ tableName = $ table ->getObjectName ();
335329
336330 $ droppedPrimaryKeyConstraint = $ diff ->getDroppedPrimaryKeyConstraint ();
337331
@@ -377,19 +371,23 @@ public function getAlterTableSQL(TableDiff $diff): array
377371 $ queryParts [] = 'DROP COLUMN ' . $ column ->getObjectName ()->toSQL ($ this );
378372 }
379373
380- $ tableNameSQL = $ table -> getObjectName () ->toSQL ($ this );
374+ $ tableNameSQL = $ tableName ->toSQL ($ this );
381375
382376 foreach ($ diff ->getChangedColumns () as $ columnDiff ) {
383377 $ newColumn = $ columnDiff ->getNewColumn ();
384378 $ oldColumn = $ columnDiff ->getOldColumn ();
385379 $ nameChanged = $ columnDiff ->hasNameChanged ();
386380
387381 if ($ nameChanged ) {
388- // sp_rename accepts the old name as a qualified name, so it should be quoted .
382+ // sp_rename accepts the old name as a qualified name, so it should be represented as SQL .
389383 $ oldColumnNameSQL = $ oldColumn ->getObjectName ()->toSQL ($ this );
390384
391- // sp_rename accepts the new name as a literal value, so it cannot be quoted.
392- $ newColumnName = $ newColumn ->getName ();
385+ // sp_rename accepts the new name as a literal value.
386+ $ newColumnName = $ newColumn ->getObjectName ()
387+ ->getIdentifier ()
388+ ->toNormalizedValue (
389+ $ this ->getUnquotedIdentifierFolding (),
390+ );
393391
394392 $ sql = array_merge (
395393 $ sql ,
@@ -406,13 +404,13 @@ public function getAlterTableSQL(TableDiff $diff): array
406404 if ($ hasOldComment && $ hasNewComment && $ oldComment !== $ newComment ) {
407405 $ commentsSql [] = $ this ->getAlterColumnCommentSQL (
408406 $ tableName ,
409- $ newColumn ->getObjectName ()-> toSQL ( $ this ) ,
407+ $ newColumn ->getObjectName (),
410408 $ newComment ,
411409 );
412410 } elseif ($ hasOldComment && ! $ hasNewComment ) {
413411 $ commentsSql [] = $ this ->getDropColumnCommentSQL (
414412 $ tableName ,
415- $ newColumn ->getObjectName ()-> toSQL ( $ this ) ,
413+ $ newColumn ->getObjectName (),
416414 );
417415 } elseif (! $ hasOldComment && $ hasNewComment ) {
418416 $ commentsSql [] = $ this ->getCreateColumnCommentSQL (
@@ -449,7 +447,7 @@ public function getAlterTableSQL(TableDiff $diff): array
449447 continue ;
450448 }
451449
452- $ queryParts [] = $ this ->getAlterTableAddDefaultConstraintClause ($ tableName , $ newColumn );
450+ $ queryParts [] = $ this ->getAlterTableAddDefaultConstraintClause ($ newColumn );
453451 }
454452
455453 $ addedPrimaryKeyConstraint = $ diff ->getAddedPrimaryKeyConstraint ();
@@ -478,10 +476,9 @@ public function getRenameTableSQL(string $oldName, string $newName): string
478476 /**
479477 * Returns the SQL clause for adding a default constraint in an ALTER TABLE statement.
480478 *
481- * @param string $tableName The name of the table to generate the clause for.
482- * @param Column $column The column to generate the clause for.
479+ * @param Column $column The column to generate the clause for.
483480 */
484- private function getAlterTableAddDefaultConstraintClause (string $ tableName , Column $ column ): string
481+ private function getAlterTableAddDefaultConstraintClause (Column $ column ): string
485482 {
486483 $ columnDef = $ column ->toArray ();
487484 $ columnDef ['name ' ] = $ column ->getObjectName ();
@@ -498,8 +495,11 @@ private function getAlterTableDropDefaultConstraintClause(Column $column): strin
498495
499496 if ($ constraintName === null ) {
500497 throw new InvalidArgumentException (
501- 'Column ' . $ column ->getName () . ' was not properly introspected as it has a default value '
502- . ' but does not have the default constraint name. ' ,
498+ sprintf (
499+ 'Column %s was not properly introspected as it has a default value '
500+ . ' but does not have the default constraint name. ' ,
501+ $ column ->getObjectName ()->toString (),
502+ ),
503503 );
504504 }
505505
@@ -546,19 +546,26 @@ private function alterColumnRequiresDropDefaultConstraint(ColumnDiff $columnDiff
546546 *
547547 * @link https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-updateextendedproperty-transact-sql
548548 *
549- * @param string $tableName The quoted table name to which the column belongs.
550- * @param string $columnName The quoted column name to alter the comment for.
551- * @param string $comment The column's comment.
549+ * @param OptionallyQualifiedName $tableName The name of the table to which the column belongs.
550+ * @param UnqualifiedName $columnName The name of the column to alter the comment for.
551+ * @param string $comment The column's comment.
552552 */
553- private function getAlterColumnCommentSQL (string $ tableName , string $ columnName , string $ comment ): string
554- {
553+ private function getAlterColumnCommentSQL (
554+ OptionallyQualifiedName $ tableName ,
555+ UnqualifiedName $ columnName ,
556+ string $ comment ,
557+ ): string {
555558 return $ this ->getExecSQL (
556559 'sp_updateextendedproperty ' ,
557560 $ this ->quoteNationalStringLiteral ('MS_Description ' ),
558561 $ this ->quoteNationalStringLiteral ($ comment ),
559562 ...$ this ->getArgumentsForExtendedProperties ([
560563 ...$ this ->getExtendedPropertiesForTable ($ tableName ),
561- 'COLUMN ' => $ this ->quoteStringLiteral ($ this ->unquoteSingleIdentifier ($ columnName )),
564+ 'COLUMN ' => $ this ->quoteStringLiteral (
565+ $ columnName ->getIdentifier ()->toNormalizedValue (
566+ $ this ->getUnquotedIdentifierFolding (),
567+ ),
568+ ),
562569 ]),
563570 );
564571 }
@@ -574,19 +581,23 @@ private function getAlterColumnCommentSQL(string $tableName, string $columnName,
574581 * as column comments are stored in the same property there when
575582 * specifying a column's "Description" attribute.
576583 *
577- * @param string $tableName The quoted table name to which the column belongs.
578- * @param string $columnName The quoted column name to drop the comment for.
584+ * @param OptionallyQualifiedName $tableName The name of the table to which the column belongs.
585+ * @param UnqualifiedName $columnName The name of the column to drop the comment for.
579586 *
580587 * https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-dropextendedproperty-transact-sql
581588 */
582- private function getDropColumnCommentSQL (string $ tableName , string $ columnName ): string
589+ private function getDropColumnCommentSQL (OptionallyQualifiedName $ tableName , UnqualifiedName $ columnName ): string
583590 {
584591 return $ this ->getExecSQL (
585592 'sp_dropextendedproperty ' ,
586593 $ this ->quoteNationalStringLiteral ('MS_Description ' ),
587594 ...$ this ->getArgumentsForExtendedProperties ([
588595 ...$ this ->getExtendedPropertiesForTable ($ tableName ),
589- 'COLUMN ' => $ this ->quoteStringLiteral ($ this ->unquoteSingleIdentifier ($ columnName )),
596+ 'COLUMN ' => $ this ->quoteStringLiteral (
597+ $ columnName ->getIdentifier ()->toNormalizedValue (
598+ $ this ->getUnquotedIdentifierFolding (),
599+ ),
600+ ),
590601 ]),
591602 );
592603 }
@@ -596,7 +607,17 @@ private function getDropColumnCommentSQL(string $tableName, string $columnName):
596607 */
597608 protected function getRenameIndexSQL (string $ oldIndexName , Index $ index , string $ tableName ): array
598609 {
599- return [$ this ->getRenameSQL ($ tableName . '. ' . $ oldIndexName , $ index ->getName (), 'INDEX ' )];
610+ return [
611+ $ this ->getRenameSQL (
612+ $ tableName . '. ' . $ oldIndexName ,
613+ $ index ->getObjectName ()
614+ ->getIdentifier ()
615+ ->toNormalizedValue (
616+ $ this ->getUnquotedIdentifierFolding (),
617+ ),
618+ 'INDEX ' ,
619+ ),
620+ ];
600621 }
601622
602623 /**
@@ -671,17 +692,20 @@ private function getArgumentsForExtendedProperties(array $properties): array
671692 *
672693 * @return array<string,string>
673694 */
674- private function getExtendedPropertiesForTable (string $ tableName ): array
695+ private function getExtendedPropertiesForTable (OptionallyQualifiedName $ tableName ): array
675696 {
676- if (str_contains ($ tableName , '. ' )) {
677- [$ schemaName , $ tableName ] = explode ('. ' , $ tableName );
678- } else {
679- $ schemaName = 'dbo ' ;
680- }
697+ $ folding = $ this ->getUnquotedIdentifierFolding ();
698+
699+ $ unqualifiedName = $ tableName ->getUnqualifiedName ()
700+ ->toNormalizedValue ($ folding );
701+
702+ $ qualifier = $ tableName ->getQualifier ()
703+ ?->toNormalizedValue($ folding )
704+ ?? 'dbo ' ;
681705
682706 return [
683- 'SCHEMA ' => $ this ->quoteStringLiteral ($ this -> unquoteSingleIdentifier ( $ schemaName ) ),
684- 'TABLE ' => $ this ->quoteStringLiteral ($ this -> unquoteSingleIdentifier ( $ tableName ) ),
707+ 'SCHEMA ' => $ this ->quoteStringLiteral ($ qualifier ),
708+ 'TABLE ' => $ this ->quoteStringLiteral ($ unqualifiedName ),
685709 ];
686710 }
687711
@@ -1129,12 +1153,20 @@ protected function getLikeWildcardCharacters(): string
11291153 /** @link https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-addextendedproperty-transact-sql */
11301154 protected function getCommentOnTableSQL (string $ tableName , string $ comment ): string
11311155 {
1156+ $ parser = Parsers::getOptionallyQualifiedNameParser ();
1157+
1158+ try {
1159+ $ parsedName = $ parser ->parse ($ tableName );
1160+ } catch (Parser \Exception $ e ) {
1161+ throw InvalidName::fromParserException ($ tableName , $ e );
1162+ }
1163+
11321164 return $ this ->getExecSQL (
11331165 'sp_addextendedproperty ' ,
11341166 $ this ->quoteNationalStringLiteral ('MS_Description ' ),
11351167 $ this ->quoteNationalStringLiteral ($ comment ),
11361168 ...$ this ->getArgumentsForExtendedProperties (
1137- $ this ->getExtendedPropertiesForTable ($ tableName ),
1169+ $ this ->getExtendedPropertiesForTable ($ parsedName ),
11381170 ),
11391171 );
11401172 }
0 commit comments