34
34
use function preg_match_all ;
35
35
use function sprintf ;
36
36
use function str_contains ;
37
+ use function str_ends_with ;
37
38
use function str_replace ;
39
+ use function str_starts_with ;
38
40
use function strtoupper ;
41
+ use function substr ;
39
42
use function substr_count ;
40
43
41
44
use const PREG_OFFSET_CAPTURE ;
@@ -256,6 +259,13 @@ public function getCreatePrimaryKeySQL(Index $index, string $table): string
256
259
return $ sql . ' ( ' . implode (', ' , $ index ->getQuotedColumns ($ this )) . ') ' ;
257
260
}
258
261
262
+ private function unquoteSingleIdentifier (string $ possiblyQuotedName ): string
263
+ {
264
+ return str_starts_with ($ possiblyQuotedName , '[ ' ) && str_ends_with ($ possiblyQuotedName , '] ' )
265
+ ? substr ($ possiblyQuotedName , 1 , -1 )
266
+ : $ possiblyQuotedName ;
267
+ }
268
+
259
269
/**
260
270
* Returns the SQL statement for creating a column comment.
261
271
*
@@ -274,23 +284,20 @@ public function getCreatePrimaryKeySQL(Index $index, string $table): string
274
284
protected function getCreateColumnCommentSQL (string $ tableName , string $ columnName , string $ comment ): string
275
285
{
276
286
if (str_contains ($ tableName , '. ' )) {
277
- [$ schemaSQL , $ tableSQL ] = explode ('. ' , $ tableName );
278
- $ schemaSQL = $ this ->quoteStringLiteral ($ schemaSQL );
279
- $ tableSQL = $ this ->quoteStringLiteral ($ tableSQL );
287
+ [$ schemaName , $ tableName ] = explode ('. ' , $ tableName );
280
288
} else {
281
- $ schemaSQL = "'dbo' " ;
282
- $ tableSQL = $ this ->quoteStringLiteral ($ tableName );
289
+ $ schemaName = 'dbo ' ;
283
290
}
284
291
285
292
return $ this ->getAddExtendedPropertySQL (
286
293
'MS_Description ' ,
287
294
$ comment ,
288
295
'SCHEMA ' ,
289
- $ schemaSQL ,
296
+ $ this -> quoteStringLiteral ( $ this -> unquoteSingleIdentifier ( $ schemaName )) ,
290
297
'TABLE ' ,
291
- $ tableSQL ,
298
+ $ this -> quoteStringLiteral ( $ this -> unquoteSingleIdentifier ( $ tableName )) ,
292
299
'COLUMN ' ,
293
- $ columnName ,
300
+ $ this -> quoteStringLiteral ( $ this -> unquoteSingleIdentifier ( $ columnName)) ,
294
301
);
295
302
}
296
303
@@ -568,23 +575,20 @@ private function alterColumnRequiresDropDefaultConstraint(ColumnDiff $columnDiff
568
575
protected function getAlterColumnCommentSQL (string $ tableName , string $ columnName , string $ comment ): string
569
576
{
570
577
if (str_contains ($ tableName , '. ' )) {
571
- [$ schemaSQL , $ tableSQL ] = explode ('. ' , $ tableName );
572
- $ schemaSQL = $ this ->quoteStringLiteral ($ schemaSQL );
573
- $ tableSQL = $ this ->quoteStringLiteral ($ tableSQL );
578
+ [$ schemaName , $ tableName ] = explode ('. ' , $ tableName );
574
579
} else {
575
- $ schemaSQL = "'dbo' " ;
576
- $ tableSQL = $ this ->quoteStringLiteral ($ tableName );
580
+ $ schemaName = 'dbo ' ;
577
581
}
578
582
579
583
return $ this ->getUpdateExtendedPropertySQL (
580
584
'MS_Description ' ,
581
585
$ comment ,
582
586
'SCHEMA ' ,
583
- $ schemaSQL ,
587
+ $ this -> quoteStringLiteral ( $ this -> unquoteSingleIdentifier ( $ schemaName )) ,
584
588
'TABLE ' ,
585
- $ tableSQL ,
589
+ $ this -> quoteStringLiteral ( $ this -> unquoteSingleIdentifier ( $ tableName )) ,
586
590
'COLUMN ' ,
587
- $ columnName ,
591
+ $ this -> quoteStringLiteral ( $ this -> unquoteSingleIdentifier ( $ columnName)) ,
588
592
);
589
593
}
590
594
@@ -605,22 +609,19 @@ protected function getAlterColumnCommentSQL(string $tableName, string $columnNam
605
609
protected function getDropColumnCommentSQL (string $ tableName , string $ columnName ): string
606
610
{
607
611
if (str_contains ($ tableName , '. ' )) {
608
- [$ schemaSQL , $ tableSQL ] = explode ('. ' , $ tableName );
609
- $ schemaSQL = $ this ->quoteStringLiteral ($ schemaSQL );
610
- $ tableSQL = $ this ->quoteStringLiteral ($ tableSQL );
612
+ [$ schemaName , $ tableName ] = explode ('. ' , $ tableName );
611
613
} else {
612
- $ schemaSQL = "'dbo' " ;
613
- $ tableSQL = $ this ->quoteStringLiteral ($ tableName );
614
+ $ schemaName = 'dbo ' ;
614
615
}
615
616
616
617
return $ this ->getDropExtendedPropertySQL (
617
618
'MS_Description ' ,
618
619
'SCHEMA ' ,
619
- $ schemaSQL ,
620
+ $ this -> quoteStringLiteral ( $ this -> unquoteSingleIdentifier ( $ schemaName )) ,
620
621
'TABLE ' ,
621
- $ tableSQL ,
622
+ $ this -> quoteStringLiteral ( $ this -> unquoteSingleIdentifier ( $ tableName )) ,
622
623
'COLUMN ' ,
623
- $ columnName ,
624
+ $ this -> quoteStringLiteral ( $ this -> unquoteSingleIdentifier ( $ columnName)) ,
624
625
);
625
626
}
626
627
@@ -663,10 +664,13 @@ protected function getAddExtendedPropertySQL(
663
664
?string $ level2Name = null ,
664
665
): string {
665
666
return 'EXEC sp_addextendedproperty ' .
666
- 'N ' . $ this ->quoteStringLiteral ($ name ) . ', N ' . $ this ->quoteStringLiteral ((string ) $ value ) . ', ' .
667
- 'N ' . $ this ->quoteStringLiteral ((string ) $ level0Type ) . ', ' . $ level0Name . ', ' .
668
- 'N ' . $ this ->quoteStringLiteral ((string ) $ level1Type ) . ', ' . $ level1Name . ', ' .
669
- 'N ' . $ this ->quoteStringLiteral ((string ) $ level2Type ) . ', ' . $ level2Name ;
667
+ 'N ' . $ this ->quoteStringLiteral ($ name ) . ', N ' . $ this ->quoteStringLiteral ($ value ?? '' ) . ', ' .
668
+ 'N ' . $ this ->quoteStringLiteral ($ level0Type ?? '' ) . ', ' . $ level0Name . ', ' .
669
+ 'N ' . $ this ->quoteStringLiteral ($ level1Type ?? '' ) . ', ' . $ level1Name .
670
+ ($ level2Type !== null || $ level2Name !== null
671
+ ? ', N ' . $ this ->quoteStringLiteral ($ level2Type ?? '' ) . ', ' . $ level2Name
672
+ : ''
673
+ );
670
674
}
671
675
672
676
/**
@@ -693,9 +697,12 @@ protected function getDropExtendedPropertySQL(
693
697
): string {
694
698
return 'EXEC sp_dropextendedproperty ' .
695
699
'N ' . $ this ->quoteStringLiteral ($ name ) . ', ' .
696
- 'N ' . $ this ->quoteStringLiteral ((string ) $ level0Type ) . ', ' . $ level0Name . ', ' .
697
- 'N ' . $ this ->quoteStringLiteral ((string ) $ level1Type ) . ', ' . $ level1Name . ', ' .
698
- 'N ' . $ this ->quoteStringLiteral ((string ) $ level2Type ) . ', ' . $ level2Name ;
700
+ 'N ' . $ this ->quoteStringLiteral ($ level0Type ?? '' ) . ', ' . $ level0Name . ', ' .
701
+ 'N ' . $ this ->quoteStringLiteral ($ level1Type ?? '' ) . ', ' . $ level1Name .
702
+ ($ level2Type !== null || $ level2Name !== null
703
+ ? ', N ' . $ this ->quoteStringLiteral ($ level2Type ?? '' ) . ', ' . $ level2Name
704
+ : ''
705
+ );
699
706
}
700
707
701
708
/**
@@ -723,10 +730,13 @@ protected function getUpdateExtendedPropertySQL(
723
730
?string $ level2Name = null ,
724
731
): string {
725
732
return 'EXEC sp_updateextendedproperty ' .
726
- 'N ' . $ this ->quoteStringLiteral ($ name ) . ', N ' . $ this ->quoteStringLiteral ((string ) $ value ) . ', ' .
727
- 'N ' . $ this ->quoteStringLiteral ((string ) $ level0Type ) . ', ' . $ level0Name . ', ' .
728
- 'N ' . $ this ->quoteStringLiteral ((string ) $ level1Type ) . ', ' . $ level1Name . ', ' .
729
- 'N ' . $ this ->quoteStringLiteral ((string ) $ level2Type ) . ', ' . $ level2Name ;
733
+ 'N ' . $ this ->quoteStringLiteral ($ name ) . ', N ' . $ this ->quoteStringLiteral ($ value ?? '' ) . ', ' .
734
+ 'N ' . $ this ->quoteStringLiteral ($ level0Type ?? '' ) . ', ' . $ level0Name . ', ' .
735
+ 'N ' . $ this ->quoteStringLiteral ($ level1Type ?? '' ) . ', ' . $ level1Name .
736
+ ($ level2Type !== null || $ level2Name !== null
737
+ ? ', N ' . $ this ->quoteStringLiteral ($ level2Type ?? '' ) . ', ' . $ level2Name
738
+ : ''
739
+ );
730
740
}
731
741
732
742
public function getEmptyIdentityInsertSQL (string $ quotedTableName , string $ quotedIdentifierColumnName ): string
@@ -1168,15 +1178,13 @@ protected function getLikeWildcardCharacters(): string
1168
1178
1169
1179
protected function getCommentOnTableSQL (string $ tableName , string $ comment ): string
1170
1180
{
1171
- return sprintf (
1172
- <<<'SQL'
1173
- EXEC sys.sp_addextendedproperty @name=N'MS_Description',
1174
- @value=N%s, @level0type=N'SCHEMA', @level0name=N'dbo',
1175
- @level1type=N'TABLE', @level1name=N%s
1176
- SQL
1177
- ,
1178
- $ this ->quoteStringLiteral ($ comment ),
1179
- $ this ->quoteStringLiteral ($ tableName ),
1181
+ return $ this ->getAddExtendedPropertySQL (
1182
+ 'MS_Description ' ,
1183
+ $ comment ,
1184
+ 'SCHEMA ' ,
1185
+ $ this ->quoteStringLiteral ('dbo ' ),
1186
+ 'TABLE ' ,
1187
+ $ this ->quoteStringLiteral ($ this ->unquoteSingleIdentifier ($ tableName )),
1180
1188
);
1181
1189
}
1182
1190
0 commit comments