Skip to content

Commit 34ea36f

Browse files
authored
Merge pull request #6600 from morozov/sqlite-quoted-table-comment
Fix parsing quoted table name on SQLite
2 parents 232fd06 + 395269a commit 34ea36f

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

src/Schema/SqliteSchemaManager.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,8 @@ protected function _getPortableTableForeignKeyDefinition($tableForeignKey): Fore
508508

509509
private function parseColumnCollationFromSQL(string $column, string $sql): ?string
510510
{
511-
$pattern = '{(?:\W' . preg_quote($column) . '\W|\W'
512-
. preg_quote($this->_platform->quoteSingleIdentifier($column))
513-
. '\W)[^,(]+(?:\([^()]+\)[^,]*)?(?:(?:DEFAULT|CHECK)\s*(?:\(.*?\))?[^,]*)*COLLATE\s+["\']?([^\s,"\')]+)}is';
511+
$pattern = '{' . $this->buildIdentifierPattern($column)
512+
. '[^,(]+(?:\([^()]+\)[^,]*)?(?:(?:DEFAULT|CHECK)\s*(?:\(.*?\))?[^,]*)*COLLATE\s+["\']?([^\s,"\')]+)}is';
514513

515514
if (preg_match($pattern, $sql, $match) !== 1) {
516515
return null;
@@ -522,9 +521,7 @@ private function parseColumnCollationFromSQL(string $column, string $sql): ?stri
522521
private function parseTableCommentFromSQL(string $table, string $sql): ?string
523522
{
524523
$pattern = '/\s* # Allow whitespace characters at start of line
525-
CREATE\sTABLE # Match "CREATE TABLE"
526-
(?:\W"' . preg_quote($this->_platform->quoteSingleIdentifier($table), '/') . '"\W|\W' . preg_quote($table, '/')
527-
. '\W) # Match table name (quoted and unquoted)
524+
CREATE\sTABLE' . $this->buildIdentifierPattern($table) . '
528525
( # Start capture
529526
(?:\s*--[^\n]*\n?)+ # Capture anything that starts with whitespaces followed by -- until the end of the line(s)
530527
)/ix';
@@ -540,8 +537,8 @@ private function parseTableCommentFromSQL(string $table, string $sql): ?string
540537

541538
private function parseColumnCommentFromSQL(string $column, string $sql): ?string
542539
{
543-
$pattern = '{[\s(,](?:\W' . preg_quote($this->_platform->quoteSingleIdentifier($column))
544-
. '\W|\W' . preg_quote($column) . '\W)(?:\([^)]*?\)|[^,(])*?,?((?:(?!\n))(?:\s*--[^\n]*\n?)+)}i';
540+
$pattern = '{[\s(,]' . $this->buildIdentifierPattern($column)
541+
. '(?:\([^)]*?\)|[^,(])*?,?((?:(?!\n))(?:\s*--[^\n]*\n?)+)}i';
545542

546543
if (preg_match($pattern, $sql, $match) !== 1) {
547544
return null;
@@ -552,6 +549,22 @@ private function parseColumnCommentFromSQL(string $column, string $sql): ?string
552549
return $comment === '' ? null : $comment;
553550
}
554551

552+
/**
553+
* Returns a regular expression pattern that matches the given unquoted or quoted identifier.
554+
*/
555+
private function buildIdentifierPattern(string $identifier): string
556+
{
557+
return '(?:' . implode('|', array_map(
558+
static function (string $sql): string {
559+
return '\W' . preg_quote($sql, '/') . '\W';
560+
},
561+
[
562+
$identifier,
563+
$this->_platform->quoteSingleIdentifier($identifier),
564+
],
565+
)) . ')';
566+
}
567+
555568
/** @throws Exception */
556569
private function getCreateTableSQL(string $table): string
557570
{

tests/Functional/Schema/SqliteSchemaManagerTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,25 @@ public function testShorthandInForeignKeyReferenceWithMultipleColumns(): void
397397
$createTableTrackSql,
398398
);
399399
}
400+
401+
/**
402+
* This test duplicates {@see parent::testCommentInTable()} with the only difference that the name of the table
403+
* being created is quoted. It is only meant to cover the logic of parsing the SQLite CREATE TABLE statement
404+
* when the table name is quoted.
405+
*
406+
* Running the same test for all platforms, on the one hand, won't produce additional coverage, and on the other,
407+
* is not feasible due to the differences in case sensitivity depending on whether the name is quoted.
408+
*
409+
* Once all identifiers are quoted by default, this test can be removed.
410+
*/
411+
public function testCommentInQuotedTable(): void
412+
{
413+
$table = new Table('"table_with_comment"');
414+
$table->addColumn('id', Types::INTEGER);
415+
$table->setComment('This is a comment');
416+
$this->dropAndCreateTable($table);
417+
418+
$table = $this->schemaManager->introspectTable('table_with_comment');
419+
self::assertSame('This is a comment', $table->getComment());
420+
}
400421
}

0 commit comments

Comments
 (0)