Skip to content

Commit

Permalink
Improve regex, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Jan 18, 2024
1 parent ce91d21 commit 18146e2
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -771,8 +771,8 @@ private function findComments(TableSchemaInterface $tableSchema): void

$columnsDefinition = $matches[1];

$identifierPattern = '(?:([`"])([^`"]+)\1|\[([^\]]+)\]|([A-Za-z_]\w*))';
$notCommaPattern = '(?:[^,]|\([^()]+\))*?';
$identifierPattern = '(?:([`"])([^`"]+)\1|\[([^\]]+)\]|([a-zA-Z_]\w*))';
$notCommaPattern = "(?:[^,]|\([^()]+\)|'[^']+')*?";
$commentPattern = '(?:\s*--[^\n]*|\s*/\*.*?\*/)';

$pattern = "#$identifierPattern\s*$notCommaPattern,?($commentPattern+)#";
Expand Down
1 change: 1 addition & 0 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ public function testColumnComments(): void
$table = $schema->getTableSchema('comment');

$this->assertSame('primary key', $table->getColumn('id')->getComment());
$this->assertSame('USD', $table->getColumn('price')->getComment());
$this->assertSame("Column comment\nsecond line\nthird line", $table->getColumn('name')->getComment());
}
}
3 changes: 2 additions & 1 deletion tests/Support/Fixture/sqlite.sql
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ CREATE TABLE `comment` -- Table comment
/* third line */
(
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL, -- primary key
`name` varchar(100) -- Column comment
price decimal(10,2), -- USD
`name` varchar(100) DEFAULT 'Pan, Peter' -- Column comment
-- second line
/* third line */
);
Expand Down

0 comments on commit 18146e2

Please sign in to comment.