From f528ce20dd8a21344910f52778c46b7e548f9325 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 29 Jan 2025 18:41:24 +0700 Subject: [PATCH 1/5] Fix `ColumnDefinitionParser` --- src/Column/ColumnDefinitionParser.php | 9 +++++++-- src/Column/ColumnFactory.php | 9 +++++++++ tests/Provider/ColumnDefinitionParserProvider.php | 2 ++ tests/Provider/QueryBuilderProvider.php | 2 ++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Column/ColumnDefinitionParser.php b/src/Column/ColumnDefinitionParser.php index f55e65d08..96c83583c 100644 --- a/src/Column/ColumnDefinitionParser.php +++ b/src/Column/ColumnDefinitionParser.php @@ -9,19 +9,20 @@ use function strlen; use function strtolower; use function substr; +use function substr_count; /** * Parses column definition string. For example, `string(255)` or `int unsigned`. */ final class ColumnDefinitionParser extends \Yiisoft\Db\Syntax\ColumnDefinitionParser { - private const TYPE_PATTERN = '/^(' + private const TYPE_PATTERN = '/^(?:(' . 'time(?:stamp)?\s*(?:\((\d+)\))? with(?:out)? time zone' . ')|(' . '(?:character|bit) varying' . '|double precision' . '|\w*' - . ')(?:\(([^)]+)\))?\s*/i'; + . ')(?:\(([^)]+)\))?)(\[[\d\[\]]*\])?\s*/i'; public function parse(string $definition): array { @@ -40,6 +41,10 @@ public function parse(string $definition): array } } + if (isset($matches[5])) { + $info['dimension'] = substr_count($matches[5], '['); + } + $extra = substr($definition, strlen($matches[0])); return $info + $this->extraInfo($extra); diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index b2641c60c..dbbd45f65 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -162,6 +162,15 @@ protected function getColumnClass(string $type, array $info = []): string }; } + protected function getType(string $dbType, array $info = []): string + { + if (!empty($info['dimension'])) { + return ColumnType::ARRAY; + } + + return static::TYPE_MAP[$dbType] ?? ColumnType::STRING; + } + protected function normalizeNotNullDefaultValue(string $defaultValue, ColumnInterface $column): mixed { $value = preg_replace("/::[^:']+$/", '$1', $defaultValue); diff --git a/tests/Provider/ColumnDefinitionParserProvider.php b/tests/Provider/ColumnDefinitionParserProvider.php index 327723dbb..fb3df9cf9 100644 --- a/tests/Provider/ColumnDefinitionParserProvider.php +++ b/tests/Provider/ColumnDefinitionParserProvider.php @@ -17,6 +17,8 @@ public static function parse(): array ['timestamp(3) with time zone', ['type' => 'timestamp with time zone', 'size' => 3]], ['time without time zone', ['type' => 'time without time zone']], ['time (3) with time zone', ['type' => 'time with time zone', 'size' => 3]], + ['int[]', ['type' => 'int', 'dimension' => 1]], + ['character varying(126)[][]', ['type' => 'character varying', 'size' => 126, 'dimension' => 2]], ]; } } diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index f8117eb21..ed44567c6 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -638,7 +638,9 @@ public static function buildColumnDefinition(): array return [ ...$values, + ['int[]', 'int[]'], ['character varying(255)', 'character varying(255)'], + ['character varying(255)[][]', 'character varying(255)[][]'], ['timestamp(5)', 'timestamp (5) without time zone'], ['timestamptz', 'timestamp with time zone'], ['time(3)', 'time(3) without time zone'], From 026c4717fb8e6460c787f7b101ca397be5010682 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 29 Jan 2025 11:41:45 +0000 Subject: [PATCH 2/5] Apply fixes from StyleCI --- src/Column/ColumnFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index dbbd45f65..8261f8989 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -168,7 +168,7 @@ protected function getType(string $dbType, array $info = []): string return ColumnType::ARRAY; } - return static::TYPE_MAP[$dbType] ?? ColumnType::STRING; + return self::TYPE_MAP[$dbType] ?? ColumnType::STRING; } protected function normalizeNotNullDefaultValue(string $defaultValue, ColumnInterface $column): mixed From 069fa958e91f4a811b4b9b3b5ffcef999b6cd811 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 29 Jan 2025 18:56:49 +0700 Subject: [PATCH 3/5] Fix psalm --- src/Column/ColumnDefinitionParser.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Column/ColumnDefinitionParser.php b/src/Column/ColumnDefinitionParser.php index 96c83583c..b9dee5986 100644 --- a/src/Column/ColumnDefinitionParser.php +++ b/src/Column/ColumnDefinitionParser.php @@ -42,6 +42,7 @@ public function parse(string $definition): array } if (isset($matches[5])) { + /** @psalm-var positive-int */ $info['dimension'] = substr_count($matches[5], '['); } From c92e1c0e8aedc95adfb23cab4c987c12e5eee0e1 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 30 Jan 2025 10:47:00 +0700 Subject: [PATCH 4/5] Update CHANGELOG.md [skip ci] --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a43be0222..f6e0e81e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,7 +26,7 @@ - Enh #378: Improve loading schemas of views (@Tigrov) - Enh #379: Remove `ColumnInterface` (@Tigrov) - Enh #380: Rename `ColumnSchemaInterface` to `ColumnInterface` (@Tigrov) -- Enh #381: Add `ColumnDefinitionParser` class (@Tigrov) +- Enh #381, #383: Add `ColumnDefinitionParser` class (@Tigrov) ## 1.3.0 March 21, 2024 From ae0788b9ef48b59c285ce356691a74638ec5cc16 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Fri, 31 Jan 2025 12:46:49 +0700 Subject: [PATCH 5/5] Update after merge --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05a64c4ea..9604c6584 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,8 +26,7 @@ - Enh #378: Improve loading schemas of views (@Tigrov) - Enh #379: Remove `ColumnInterface` (@Tigrov) - Enh #380: Rename `ColumnSchemaInterface` to `ColumnInterface` (@Tigrov) -- Enh #381, #383: Add `ColumnDefinitionParser` class (@Tigrov) -- Enh #381: Add `ColumnDefinitionParser` class (@Tigrov) +- Enh #381, #383: Add `ColumnDefinitionParser` class (@Tigrov) - Enh #382: Replace `DbArrayHelper::getColumn()` with `array_column()` (@Tigrov) ## 1.3.0 March 21, 2024