Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -55,6 +55,7 @@
- Chg #428: Update expression namespaces according to changes in `yiisoft/db` package (@Tigrov)
- Enh #432, #433: Update `DMLQueryBuilder::update()` method to adapt changes in `yiisoft/db` (@rustamwin, @Tigrov)
- Enh #439: Move "Packets out of order" warning suppression from Yii DB (@vjik)
- Chg #447: Refactor `ColumnDefinitionParser` (@vjik)

## 1.2.0 March 21, 2024

Expand Down
40 changes: 39 additions & 1 deletion src/Column/ColumnDefinitionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Db\Mysql\Column;

use Yiisoft\Db\Syntax\AbstractColumnDefinitionParser;

/**
* Parses column definition string. For example, `string(255)` or `int unsigned`.
*
Expand All @@ -19,7 +21,7 @@
* unsigned?: bool
* }
*/
final class ColumnDefinitionParser extends \Yiisoft\Db\Syntax\ColumnDefinitionParser
final class ColumnDefinitionParser extends AbstractColumnDefinitionParser
{
/**
* @psalm-return ExtraInfo
Expand All @@ -27,7 +29,7 @@
*/
protected function extraInfo(string $extra): array
{
$info = parent::extraInfo($extra);

Check failure on line 32 in src/Column/ColumnDefinitionParser.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

ArgumentTypeCoercion

src/Column/ColumnDefinitionParser.php:32:35: ArgumentTypeCoercion: Argument 1 of Yiisoft\Db\Syntax\AbstractColumnDefinitionParser::extraInfo expects non-empty-string, but parent type string provided (see https://psalm.dev/193)

if (empty($info['extra'])) {
return $info;
Expand All @@ -44,4 +46,40 @@

return $info;
}

protected function parseTypeParams(string $type, string $params): array
{
return match ($type) {
'bit',
'bigint',
'binary',
'char',
'decimal',
'double',
'float',
'int',
'integer',
'mediumint',
'numeric',
'real',
'smallint',
'string',
'tinyint',
'varbinary',
'varchar',
'year' => $this->parseSizeInfo($params),
'enum' => $this->parseEnumValues($params),
default => [],
};
Comment on lines +52 to +73
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return match ($type) {
'bit',
'bigint',
'binary',
'char',
'decimal',
'double',
'float',
'int',
'integer',
'mediumint',
'numeric',
'real',
'smallint',
'string',
'tinyint',
'varbinary',
'varchar',
'year' => $this->parseSizeInfo($params),
'enum' => $this->parseEnumValues($params),
default => [],
};
return match ($type) {
'enum' => $this->parseEnumValues($params),
default => $this->parseSizeInfo($params),
};

It seems a mistake to limit the list of types for parsing.
We use column definition parser not only for native db types but for abstract types also.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What case for abstract types?

}

/**
* @psalm-return array{enumValues: list<string>}
*/
protected function parseEnumValues(string $params): array
{
preg_match_all("/'([^']*)'/", $params, $matches);

return ['enumValues' => $matches[1]];
}
}
26 changes: 26 additions & 0 deletions tests/ColumnDefinitionParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

Comment thread
vjik marked this conversation as resolved.
use PHPUnit\Framework\Attributes\DataProviderExternal;
use Yiisoft\Db\Mysql\Column\ColumnDefinitionParser;
use Yiisoft\Db\Mysql\Tests\Provider\ColumnDefinitionParserProvider;
use Yiisoft\Db\Syntax\ColumnDefinitionParserInterface;
use Yiisoft\Db\Tests\Common\CommonColumnDefinitionParserTest;

/**
* @group mysql
*/
final class ColumnDefinitionParserTest extends CommonColumnDefinitionParserTest
{
#[DataProviderExternal(ColumnDefinitionParserProvider::class, 'parse')]
public function testParse(string $definition, array $expected): void
{
parent::testParse($definition, $expected);
}

protected function createColumnDefinitionParser(): ColumnDefinitionParserInterface
{
return new ColumnDefinitionParser();
}
}
17 changes: 17 additions & 0 deletions tests/Provider/ColumnDefinitionParserProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mysql\Tests\Provider;

final class ColumnDefinitionParserProvider extends \Yiisoft\Db\Tests\Provider\ColumnDefinitionParserProvider
{
public static function parse(): array
{
return [
...parent::parse(),
["enum('a','b','c')", ['type' => 'enum', 'enumValues' => ['a', 'b', 'c']]],
["enum('a','b','c') NOT NULL", ['type' => 'enum', 'enumValues' => ['a', 'b', 'c'], 'notNull' => true]],
];
}
}
Loading