Skip to content

Commit

Permalink
Parse comment from column definition (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Jan 7, 2025
1 parent 4f9a12a commit 209c594
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/Syntax/ColumnDefinitionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class ColumnDefinitionParser
*
* @psalm-return array{
* check?: string,
* comment?: string,
* defaultValueRaw?: string,
* enumValues?: list<string>,
* extra?: string,
Expand Down Expand Up @@ -71,6 +72,7 @@ private function enumInfo(string $values): array
/**
* @psalm-return array{
* check?: string,
* comment?: string,
* defaultValueRaw?: string,
* extra?: string,
* notNull?: bool,
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions tests/Provider/ColumnDefinitionParserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]],
Expand Down

0 comments on commit 209c594

Please sign in to comment.