13
13
use Doctrine \DBAL \Types \Type ;
14
14
15
15
use function array_change_key_case ;
16
+ use function array_map ;
16
17
use function array_merge ;
17
18
use function assert ;
18
19
use function count ;
@@ -359,9 +360,8 @@ protected function _getPortableTableForeignKeyDefinition(array $tableForeignKey)
359
360
360
361
private function parseColumnCollationFromSQL (string $ column , string $ sql ): ?string
361
362
{
362
- $ pattern = '{(?:\W ' . preg_quote ($ column ) . '\W|\W '
363
- . preg_quote ($ this ->platform ->quoteSingleIdentifier ($ column ))
364
- . '\W)[^,(]+(?:\([^()]+\)[^,]*)?(?:(?:DEFAULT|CHECK)\s*(?:\(.*?\))?[^,]*)*COLLATE\s+[" \']?([^\s," \')]+)}is ' ;
363
+ $ pattern = '{ ' . $ this ->buildIdentifierPattern ($ column )
364
+ . '[^,(]+(?:\([^()]+\)[^,]*)?(?:(?:DEFAULT|CHECK)\s*(?:\(.*?\))?[^,]*)*COLLATE\s+[" \']?([^\s," \')]+)}is ' ;
365
365
366
366
if (preg_match ($ pattern , $ sql , $ match ) !== 1 ) {
367
367
return null ;
@@ -373,9 +373,7 @@ private function parseColumnCollationFromSQL(string $column, string $sql): ?stri
373
373
private function parseTableCommentFromSQL (string $ table , string $ sql ): ?string
374
374
{
375
375
$ pattern = '/\s* # Allow whitespace characters at start of line
376
- CREATE\sTABLE # Match "CREATE TABLE"
377
- (?:\W" ' . preg_quote ($ this ->platform ->quoteSingleIdentifier ($ table ), '/ ' ) . '"\W|\W ' . preg_quote ($ table , '/ ' )
378
- . '\W) # Match table name (quoted and unquoted)
376
+ CREATE\sTABLE ' . $ this ->buildIdentifierPattern ($ table ) . '
379
377
( # Start capture
380
378
(?:\s*--[^\n]*\n?)+ # Capture anything that starts with whitespaces followed by -- until the end of the line(s)
381
379
)/ix ' ;
@@ -391,8 +389,8 @@ private function parseTableCommentFromSQL(string $table, string $sql): ?string
391
389
392
390
private function parseColumnCommentFromSQL (string $ column , string $ sql ): string
393
391
{
394
- $ pattern = '{[\s(,](?:\W ' . preg_quote ( $ this ->platform -> quoteSingleIdentifier ($ column) )
395
- . '\W|\W ' . preg_quote ( $ column ) . ' \W) (?:\([^)]*?\)|[^,(])*?,?((?:(?!\n))(?:\s*--[^\n]*\n?)+)}i ' ;
392
+ $ pattern = '{[\s(,] ' . $ this ->buildIdentifierPattern ($ column )
393
+ . '(?:\([^)]*?\)|[^,(])*?,?((?:(?!\n))(?:\s*--[^\n]*\n?)+)}i ' ;
396
394
397
395
if (preg_match ($ pattern , $ sql , $ match ) !== 1 ) {
398
396
return '' ;
@@ -404,6 +402,22 @@ private function parseColumnCommentFromSQL(string $column, string $sql): string
404
402
return $ comment ;
405
403
}
406
404
405
+ /**
406
+ * Returns a regular expression pattern that matches the given unquoted or quoted identifier.
407
+ */
408
+ private function buildIdentifierPattern (string $ identifier ): string
409
+ {
410
+ return '(?: ' . implode ('| ' , array_map (
411
+ static function (string $ sql ): string {
412
+ return '\W ' . preg_quote ($ sql , '/ ' ) . '\W ' ;
413
+ },
414
+ [
415
+ $ identifier ,
416
+ $ this ->platform ->quoteSingleIdentifier ($ identifier ),
417
+ ],
418
+ )) . ') ' ;
419
+ }
420
+
407
421
/** @throws Exception */
408
422
private function getCreateTableSQL (string $ table ): string
409
423
{
0 commit comments