Skip to content

Commit 45a4b12

Browse files
authored
Fix ColumnDefinitionParser (#383)
1 parent 9997cd2 commit 45a4b12

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
- Enh #378: Improve loading schemas of views (@Tigrov)
2727
- Enh #379: Remove `ColumnInterface` (@Tigrov)
2828
- Enh #380: Rename `ColumnSchemaInterface` to `ColumnInterface` (@Tigrov)
29-
- Enh #381: Add `ColumnDefinitionParser` class (@Tigrov)
29+
- Enh #381, #383: Add `ColumnDefinitionParser` class (@Tigrov)
3030
- Enh #382: Replace `DbArrayHelper::getColumn()` with `array_column()` (@Tigrov)
3131

3232
## 1.3.0 March 21, 2024

src/Column/ColumnDefinitionParser.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@
99
use function strlen;
1010
use function strtolower;
1111
use function substr;
12+
use function substr_count;
1213

1314
/**
1415
* Parses column definition string. For example, `string(255)` or `int unsigned`.
1516
*/
1617
final class ColumnDefinitionParser extends \Yiisoft\Db\Syntax\ColumnDefinitionParser
1718
{
18-
private const TYPE_PATTERN = '/^('
19+
private const TYPE_PATTERN = '/^(?:('
1920
. 'time(?:stamp)?\s*(?:\((\d+)\))? with(?:out)? time zone'
2021
. ')|('
2122
. '(?:character|bit) varying'
2223
. '|double precision'
2324
. '|\w*'
24-
. ')(?:\(([^)]+)\))?\s*/i';
25+
. ')(?:\(([^)]+)\))?)(\[[\d\[\]]*\])?\s*/i';
2526

2627
public function parse(string $definition): array
2728
{
@@ -40,6 +41,11 @@ public function parse(string $definition): array
4041
}
4142
}
4243

44+
if (isset($matches[5])) {
45+
/** @psalm-var positive-int */
46+
$info['dimension'] = substr_count($matches[5], '[');
47+
}
48+
4349
$extra = substr($definition, strlen($matches[0]));
4450

4551
return $info + $this->extraInfo($extra);

src/Column/ColumnFactory.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ protected function getColumnClass(string $type, array $info = []): string
162162
};
163163
}
164164

165+
protected function getType(string $dbType, array $info = []): string
166+
{
167+
if (!empty($info['dimension'])) {
168+
return ColumnType::ARRAY;
169+
}
170+
171+
return self::TYPE_MAP[$dbType] ?? ColumnType::STRING;
172+
}
173+
165174
protected function normalizeNotNullDefaultValue(string $defaultValue, ColumnInterface $column): mixed
166175
{
167176
$value = preg_replace("/::[^:']+$/", '$1', $defaultValue);

tests/Provider/ColumnDefinitionParserProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public static function parse(): array
1717
['timestamp(3) with time zone', ['type' => 'timestamp with time zone', 'size' => 3]],
1818
['time without time zone', ['type' => 'time without time zone']],
1919
['time (3) with time zone', ['type' => 'time with time zone', 'size' => 3]],
20+
['int[]', ['type' => 'int', 'dimension' => 1]],
21+
['character varying(126)[][]', ['type' => 'character varying', 'size' => 126, 'dimension' => 2]],
2022
];
2123
}
2224
}

tests/Provider/QueryBuilderProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,9 @@ public static function buildColumnDefinition(): array
638638

639639
return [
640640
...$values,
641+
['int[]', 'int[]'],
641642
['character varying(255)', 'character varying(255)'],
643+
['character varying(255)[][]', 'character varying(255)[][]'],
642644
['timestamp(5)', 'timestamp (5) without time zone'],
643645
['timestamptz', 'timestamp with time zone'],
644646
['time(3)', 'time(3) without time zone'],

0 commit comments

Comments
 (0)