From 209c594816b3e9b373862291adca73f5e8902548 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Tue, 7 Jan 2025 08:49:47 +0700 Subject: [PATCH] Parse comment from column definition (#914) --- CHANGELOG.md | 2 +- src/Syntax/ColumnDefinitionParser.php | 7 +++++++ tests/Provider/ColumnDefinitionParserProvider.php | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f488b79a..f47ef94d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,7 +39,7 @@ - Enh #875: Ignore "Packets out of order..." warnings in `AbstractPdoCommand::internalExecute()` method (@Tigrov) - Enh #877: Separate column type constants (@Tigrov) - New #878: Realize `ColumnBuilder` class (@Tigrov) -- New #878, #900: Realize `ColumnDefinitionParser` class (@Tigrov) +- New #878, #900, #914: Realize `ColumnDefinitionParser` class (@Tigrov) - Enh #881: Refactor `ColumnSchemaInterface` and `AbstractColumnSchema` (@Tigrov) - New #882: Move `ArrayColumnSchema` and `StructuredColumnSchema` classes from `db-pgsql` package (@Tigrov) - New #883, #901: Add `ColumnDefinitionBuilder` class and `QueryBuilderInterface::buildColumnDefinition()` method (@Tigrov) diff --git a/src/Syntax/ColumnDefinitionParser.php b/src/Syntax/ColumnDefinitionParser.php index ebc0b40f4..c6280fa04 100644 --- a/src/Syntax/ColumnDefinitionParser.php +++ b/src/Syntax/ColumnDefinitionParser.php @@ -27,6 +27,7 @@ final class ColumnDefinitionParser * * @psalm-return array{ * check?: string, + * comment?: string, * defaultValueRaw?: string, * enumValues?: list, * extra?: string, @@ -71,6 +72,7 @@ private function enumInfo(string $values): array /** * @psalm-return array{ * check?: string, + * comment?: string, * defaultValueRaw?: string, * extra?: string, * notNull?: bool, @@ -93,6 +95,11 @@ private function extraInfo(string $extra): array $extra = str_replace($matches[0], '', $extra); } + if (preg_match("/\\s*\\bCOMMENT\\s+'((?:[^']|'')*)'/i", $extra, $matches) === 1) { + $info['comment'] = str_replace("''", "'", $matches[1]); + $extra = str_replace($matches[0], '', $extra); + } + if (preg_match("/\\s*\\bCHECK\\s+$bracketsPattern/i", $extra, $matches) === 1) { $info['check'] = substr($matches[1], 1, -1); $extra = str_replace($matches[0], '', $extra); diff --git a/tests/Provider/ColumnDefinitionParserProvider.php b/tests/Provider/ColumnDefinitionParserProvider.php index 91816f253..79e8bdc38 100644 --- a/tests/Provider/ColumnDefinitionParserProvider.php +++ b/tests/Provider/ColumnDefinitionParserProvider.php @@ -25,6 +25,7 @@ public static function parse(): array ['varchar(36) DEFAULT uuid()', ['type' => 'varchar', 'size' => 36, 'defaultValueRaw' => 'uuid()']], ['varchar(36) DEFAULT uuid()::varchar(36)', ['type' => 'varchar', 'size' => 36, 'defaultValueRaw' => 'uuid()::varchar(36)']], ['int DEFAULT (1 + 2)', ['type' => 'int', 'defaultValueRaw' => '(1 + 2)']], + ["int COMMENT '''Quoted'' comment'", ['type' => 'int', 'comment' => "'Quoted' comment"]], ['int CHECK (value > (1 + 5))', ['type' => 'int', 'check' => 'value > (1 + 5)']], ["enum('a','b','c')", ['type' => 'enum', 'enumValues' => ['a', 'b', 'c']]], ["enum('a','b','c') NOT NULL", ['type' => 'enum', 'enumValues' => ['a', 'b', 'c'], 'notNull' => true]],