Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -177,6 +177,7 @@
- Bug #1103: Fix view names' cache refreshing after "drop table" command execution (@vjik)
- Chg #1103: Remove `AbstractCommand::refreshTableSchema()` method (@vjik)
- Chg #1106: Remove parameters from `PdoConnectionInterface::getActivePdo()` method (@vjik)
- Bug #1109: Fix column definition parsing in cases with brackets and escaped quotes (@vjik)

## 1.3.0 March 21, 2024

Expand Down
11 changes: 8 additions & 3 deletions src/Syntax/ColumnDefinitionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ColumnDefinitionParser
*/
public function parse(string $definition): array
{
preg_match('/^(\w*)(?:\(([^)]+)\))?(\[[\d\[\]]*\])?\s*/', $definition, $matches);
preg_match("/^(\w*)(?:\(((?:'[^']*'|[^)])+)\))?(\[[\d\[\]]*\])?\s*/", $definition, $matches);

$type = strtolower($matches[1]);
$info = ['type' => $type];
Expand Down Expand Up @@ -84,9 +84,14 @@ public function parse(string $definition): array
*/
protected function enumInfo(string $values): array
{
preg_match_all("/'([^']*)'/", $values, $matches);
preg_match_all("/'((?:''|[^'])*)'/", $values, $matches);

return ['enumValues' => $matches[1]];
$values = array_map(
static fn(string $value): string => str_replace("''", "'", $value),
$matches[1],
);

return ['enumValues' => $values];
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return ['enumValues' => $values];
return ['values' => $values];

Copy link
Member Author

@vjik vjik Nov 27, 2025

Choose a reason for hiding this comment

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

I'll change it in a separate PR

}

/**
Expand Down
8 changes: 1 addition & 7 deletions tests/Common/CommonColumnDefinitionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
use Yiisoft\Db\Syntax\ColumnDefinitionParser;
use Yiisoft\Db\Tests\Provider\ColumnDefinitionParserProvider;

/**
* @group db
*/
abstract class CommonColumnDefinitionParserTest extends TestCase
{
#[DataProviderExternal(ColumnDefinitionParserProvider::class, 'parse')]
Expand All @@ -22,8 +19,5 @@ public function testParse(string $definition, array $expected): void
$this->assertSame($expected, $parser->parse($definition));
}

protected function createColumnDefinitionParser(): ColumnDefinitionParser
{
return new ColumnDefinitionParser();
}
abstract protected function createColumnDefinitionParser(): ColumnDefinitionParser;
}
19 changes: 19 additions & 0 deletions tests/Db/Syntax/ColumnDefinitionParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Tests\Db\Syntax;

use Yiisoft\Db\Syntax\ColumnDefinitionParser;
use Yiisoft\Db\Tests\Common\CommonColumnDefinitionParserTest;

/**
* @group db
*/
final class ColumnDefinitionParserTest extends CommonColumnDefinitionParserTest
{
protected function createColumnDefinitionParser(): ColumnDefinitionParser
{
return new ColumnDefinitionParser();
}
}
26 changes: 24 additions & 2 deletions tests/Provider/ColumnDefinitionParserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,30 @@ public static function parse(): array
['int DEFAULT (1 + 2)', ['type' => 'int', 'defaultValueRaw' => '(1 + 2)']],
["int COMMENT '''Quoted'' comment'", ['type' => 'int', 'comment' => "'Quoted' comment"]],
['int CHECK (value > (1 + 5))', ['type' => 'int', 'check' => 'value > (1 + 5)']],
["enum('a','b','c')", ['type' => 'enum', 'enumValues' => ['a', 'b', 'c']]],
["enum('a','b','c') NOT NULL", ['type' => 'enum', 'enumValues' => ['a', 'b', 'c'], 'notNull' => true]],
[
"enum('a','b','c')",
['type' => 'enum', 'enumValues' => ['a', 'b', 'c']],
],
[
"enum('a','b','c') NOT NULL",
['type' => 'enum', 'enumValues' => ['a', 'b', 'c'], 'notNull' => true],
],
'enum-with-square-brackets' => [
"enum('[one]', 'the [two]', 'the [three] to') NOT NULL",
['type' => 'enum', 'enumValues' => ['[one]', 'the [two]', 'the [three] to'], 'notNull' => true],
],
'enum-with-parentheses' => [
"enum('(one)', 'the (two)', 'the (three) to') NOT NULL",
['type' => 'enum', 'enumValues' => ['(one)', 'the (two)', 'the (three) to'], 'notNull' => true],
],
'enum-with-escaped-quotes' => [
"enum('hello''world''', 'the ''[feature]''') NOT NULL",
['type' => 'enum', 'enumValues' => ["hello'world'", "the '[feature]'"], 'notNull' => true],
],
'enum-with-parentheses-and-escaped-quotes' => [
"enum('''hey (one)', 'the (t''wo)', 'the (three) ''to') NOT NULL",
['type' => 'enum', 'enumValues' => ['\'hey (one)', 'the (t\'wo)', 'the (three) \'to'], 'notNull' => true],
],
['int[]', ['type' => 'int', 'dimension' => 1]],
['string(126)[][]', ['type' => 'string', 'size' => 126, 'dimension' => 2]],
];
Expand Down