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 #922

Merged
merged 5 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
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
- 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, #914: Realize `ColumnDefinitionParser` class (@Tigrov)
- New #878, #900, #914, #922: Realize `ColumnDefinitionParser` class (@Tigrov)
samdark marked this conversation as resolved.
Show resolved Hide resolved
- 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)
- New #883, #901, #922: Add `ColumnDefinitionBuilder` class and `QueryBuilderInterface::buildColumnDefinition()` method (@Tigrov)
- Enh #885: Refactor `AbstractDsn` class (@Tigrov)
- Chg #889: Update `AbstractDMLQueryBuilder::insertBatch()` method (@Tigrov)
- Enh #890: Add properties of `AbstractColumnSchema` class to constructor (@Tigrov)
Expand Down
23 changes: 18 additions & 5 deletions src/QueryBuilder/AbstractColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,7 @@ protected function buildType(ColumnInterface $column): string
{
$dbType = $this->getDbType($column);

if (empty($dbType)
|| $dbType[-1] === ')'
|| !in_array(strtolower($dbType), static::TYPES_WITH_SIZE, true)
) {
if (empty($dbType) || $dbType[-1] === ')' || !$this->isAllowSize($dbType)) {
return $dbType;
}

Expand All @@ -269,7 +266,7 @@ protected function buildType(ColumnInterface $column): string

$scale = $column->getScale();

if ($scale === null || !in_array(strtolower($dbType), static::TYPES_WITH_SCALE, true)) {
if ($scale === null || !$this->isAllowScale($dbType)) {
return "$dbType($size)";
}

Expand Down Expand Up @@ -308,4 +305,20 @@ protected function getDefaultUuidExpression(): string
{
return '';
}

/**
* Check if the database column type allow scale specification.
Tigrov marked this conversation as resolved.
Show resolved Hide resolved
*/
protected function isAllowScale(string $dbType): bool
Tigrov marked this conversation as resolved.
Show resolved Hide resolved
{
return in_array(strtolower($dbType), static::TYPES_WITH_SCALE, true);
}

/**
* Check if the database column type allow size specification.
Tigrov marked this conversation as resolved.
Show resolved Hide resolved
*/
protected function isAllowSize(string $dbType): bool
Tigrov marked this conversation as resolved.
Show resolved Hide resolved
{
return in_array(strtolower($dbType), static::TYPES_WITH_SIZE, true);
}
}
8 changes: 4 additions & 4 deletions src/Syntax/ColumnDefinitionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* Parses column definition string. For example, `string(255)` or `int unsigned`.
*/
final class ColumnDefinitionParser
class ColumnDefinitionParser
{
/**
* Parses column definition string.
Expand Down Expand Up @@ -62,7 +62,7 @@ public function parse(string $definition): array
/**
* @psalm-return array{enumValues: list<string>}
*/
private function enumInfo(string $values): array
protected function enumInfo(string $values): array
{
preg_match_all("/'([^']*)'/", $values, $matches);

Expand All @@ -80,7 +80,7 @@ private function enumInfo(string $values): array
* unsigned?: bool
* }
*/
private function extraInfo(string $extra): array
protected function extraInfo(string $extra): array
{
if (empty($extra)) {
return [];
Expand Down Expand Up @@ -137,7 +137,7 @@ private function extraInfo(string $extra): array
/**
* @psalm-return array{size: int, scale?: int}
*/
private function sizeInfo(string $size): array
protected function sizeInfo(string $size): array
{
$values = explode(',', $size);

Expand Down
32 changes: 32 additions & 0 deletions tests/AbstractColumnDefinitionParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Tests;

use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Syntax\ColumnDefinitionParser;
use Yiisoft\Db\Tests\Support\TestTrait;

/**
* @group db
*/
abstract class AbstractColumnDefinitionParserTest extends TestCase
{
use TestTrait;

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

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\ColumnDefinitionParserProvider::parse
*/
public function testParse(string $definition, array $expected): void
{
$parser = $this->createColumnDefinitionParser();

$this->assertSame($expected, $parser->parse($definition));
}
}
17 changes: 2 additions & 15 deletions tests/Db/Syntax/ColumnDefinitionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,11 @@

namespace Yiisoft\Db\Tests\Db\Syntax;

use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Syntax\ColumnDefinitionParser;
use Yiisoft\Db\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\AbstractColumnDefinitionParserTest;

/**
* @group db
*/
final class ColumnDefinitionParserTest extends TestCase
final class ColumnDefinitionParserTest extends AbstractColumnDefinitionParserTest
samdark marked this conversation as resolved.
Show resolved Hide resolved
{
use TestTrait;

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\ColumnDefinitionParserProvider::parse
*/
public function testParse(string $definition, array $expected): void
{
$parser = new ColumnDefinitionParser();

$this->assertSame($expected, $parser->parse($definition));
}
}
Loading