Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ColumnDefinitionParser #381

Merged
merged 4 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +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)

## 1.3.0 March 21, 2024

Expand Down
46 changes: 46 additions & 0 deletions src/Column/ColumnDefinitionParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Pgsql\Column;

use function preg_match;
use function strlen;
use function strtolower;
use function substr;

/**
* Parses column definition string. For example, `string(255)` or `int unsigned`.
*/
final class ColumnDefinitionParser extends \Yiisoft\Db\Syntax\ColumnDefinitionParser
{
private const TYPE_PATTERN = '/^('
. 'time(?:stamp)?\s*(?:\((\d+)\))? with(?:out)? time zone'
. ')|('
. '(?:character|bit) varying'
. '|double precision'
. '|\w*'
. ')(?:\(([^)]+)\))?\s*/i';

public function parse(string $definition): array
{
preg_match(self::TYPE_PATTERN, $definition, $matches);

$type = strtolower($matches[3] ?? $matches[1]);
$info = ['type' => $type];

$typeDetails = $matches[4] ?? $matches[2] ?? '';

if ($typeDetails !== '') {
if ($type === 'enum') {
$info += $this->enumInfo($typeDetails);
} else {
$info += $this->sizeInfo($typeDetails);
}
}

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

return $info + $this->extraInfo($extra);
}
}
5 changes: 5 additions & 0 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@
return $column;
}

protected function columnDefinitionParser(): ColumnDefinitionParser
{
return new ColumnDefinitionParser();
}

protected function getColumnClass(string $type, array $info = []): string
{
return match ($type) {
Expand All @@ -162,7 +167,7 @@
$value = preg_replace("/::[^:']+$/", '$1', $defaultValue);

if (str_starts_with($value, "B'") && $value[-1] === "'") {
return $column->phpTypecast(substr($value, 2, -1));

Check warning on line 170 in src/Column/ColumnFactory.php

View workflow job for this annotation

GitHub Actions / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ { $value = preg_replace("/::[^:']+\$/", '$1', $defaultValue); if (str_starts_with($value, "B'") && $value[-1] === "'") { - return $column->phpTypecast(substr($value, 2, -1)); + return $column->phpTypecast(substr($value, 1, -1)); } $value = parent::normalizeNotNullDefaultValue($value, $column); if ($value instanceof Expression) {

Check warning on line 170 in src/Column/ColumnFactory.php

View workflow job for this annotation

GitHub Actions / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "UnwrapSubstr": --- Original +++ New @@ @@ { $value = preg_replace("/::[^:']+\$/", '$1', $defaultValue); if (str_starts_with($value, "B'") && $value[-1] === "'") { - return $column->phpTypecast(substr($value, 2, -1)); + return $column->phpTypecast($value); } $value = parent::normalizeNotNullDefaultValue($value, $column); if ($value instanceof Expression) {
}

$value = parent::normalizeNotNullDefaultValue($value, $column);
Expand Down
27 changes: 27 additions & 0 deletions tests/ColumnDefinitionParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Pgsql\Tests;

use Yiisoft\Db\Pgsql\Column\ColumnDefinitionParser;
use Yiisoft\Db\Tests\AbstractColumnDefinitionParserTest;

/**
* @group pgsql
*/
final class ColumnDefinitionParserTest extends AbstractColumnDefinitionParserTest
{
protected function createColumnDefinitionParser(): ColumnDefinitionParser
{
return new ColumnDefinitionParser();
}

/**
* @dataProvider \Yiisoft\Db\Pgsql\Tests\Provider\ColumnDefinitionParserProvider::parse
*/
public function testParse(string $definition, array $expected): void
{
parent::testParse($definition, $expected);
}
}
22 changes: 22 additions & 0 deletions tests/Provider/ColumnDefinitionParserProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Pgsql\Tests\Provider;

class ColumnDefinitionParserProvider extends \Yiisoft\Db\Tests\Provider\ColumnDefinitionParserProvider
{
public static function parse(): array
{
return [
...parent::parse(),
['double precision', ['type' => 'double precision']],
['character varying(126)', ['type' => 'character varying', 'size' => 126]],
['bit varying(8)', ['type' => 'bit varying', 'size' => 8]],
['timestamp without time zone', ['type' => 'timestamp without time zone']],
['timestamp(3) with time zone', ['type' => 'timestamp(3) with time zone', 'size' => 3]],
['time without time zone', ['type' => 'time without time zone']],
['time (3) with time zone', ['type' => 'time (3) with time zone', 'size' => 3]],
];
}
}
Loading