Skip to content

Commit

Permalink
Fix ColumnDefinitionParser (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Jan 31, 2025
1 parent 9997cd2 commit 45a4b12
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- Enh #382: Replace `DbArrayHelper::getColumn()` with `array_column()` (@Tigrov)

## 1.3.0 March 21, 2024
Expand Down
10 changes: 8 additions & 2 deletions src/Column/ColumnDefinitionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -40,6 +41,11 @@ public function parse(string $definition): array
}
}

if (isset($matches[5])) {
/** @psalm-var positive-int */
$info['dimension'] = substr_count($matches[5], '[');
}

$extra = substr($definition, strlen($matches[0]));

return $info + $this->extraInfo($extra);
Expand Down
9 changes: 9 additions & 0 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 self::TYPE_MAP[$dbType] ?? ColumnType::STRING;
}

protected function normalizeNotNullDefaultValue(string $defaultValue, ColumnInterface $column): mixed
{
$value = preg_replace("/::[^:']+$/", '$1', $defaultValue);
Expand Down
2 changes: 2 additions & 0 deletions tests/Provider/ColumnDefinitionParserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]],
];
}
}
2 changes: 2 additions & 0 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down

0 comments on commit 45a4b12

Please # to comment.