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

Merged
merged 8 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- Enh #279: Separate column type constants (@Tigrov)
- New #280, #291: Realize `ColumnBuilder` class (@Tigrov)
- Enh #281: Update according changes in `ColumnSchemaInterface` (@Tigrov)
- New #282, #291: Add `ColumnDefinitionBuilder` class (@Tigrov)
- New #282, #291, #299: Add `ColumnDefinitionBuilder` class (@Tigrov)
- Bug #285: Fix `DMLQueryBuilder::insertBatch()` method (@Tigrov)
- Enh #283: Refactor `Dsn` class (@Tigrov)
- Enh #286: Use constructor to create columns and initialize properties (@Tigrov)
Expand All @@ -27,6 +27,7 @@
- Enh #296: Remove `ColumnInterface` (@Tigrov)
- Enh #298: Rename `ColumnSchemaInterface` to `ColumnInterface` (@Tigrov)
- Enh #298: Refactor `DMLQueryBuilder::prepareInsertValues()` method (@Tigrov)
- Enh #299: Add `ColumnDefinitionParser` class (@Tigrov)
samdark marked this conversation as resolved.
Show resolved Hide resolved

## 1.3.0 March 21, 2024

Expand Down
10 changes: 9 additions & 1 deletion src/Column/ColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
use Yiisoft\Db\Schema\Column\ColumnInterface;

use function ceil;
use function in_array;
use function log10;
use function preg_replace;
use function strtolower;
use function strtoupper;

final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder
Expand All @@ -26,7 +29,7 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder
'number',
'float',
'timestamp',
'interval day(0) to second',
'interval day to second',
'raw',
'urowid',
];
Expand Down Expand Up @@ -102,4 +105,9 @@ protected function getDefaultUuidExpression(): string
{
return 'sys_guid()';
}

protected function isAllowSize(string $dbType): bool
{
return in_array(strtolower(preg_replace('/\([^)]+\)/', '', $dbType)), self::TYPES_WITH_SIZE, true);
}
}
47 changes: 47 additions & 0 deletions src/Column/ColumnDefinitionParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Oracle\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 = '/^('
. 'timestamp\s*(?:\((\d+)\))? with(?: local)? time zone'
. '|interval year\s*(?:\(\d+\))? to month'
. ')|('
. 'interval day\s*(?:\(\d+\))? to second'
. '|\w*'
. ')\s*(?:\(([^)]+)\))?\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 @@ -49,6 +49,11 @@ final class ColumnFactory extends AbstractColumnFactory
'long' => ColumnType::TEXT,
];

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

protected function getType(string $dbType, array $info = []): string
{
$dbType = strtolower($dbType);
Expand Down
2 changes: 1 addition & 1 deletion src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Yiisoft\Db\Oracle;

use PDO;
use Yiisoft\Db\Command\DataType;
use Yiisoft\Db\Constant\DataType;
use Yiisoft\Db\Constant\PhpType;
use Yiisoft\Db\Driver\Pdo\AbstractPdoCommand;
use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder;
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\Oracle\Tests;

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

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

/**
* @dataProvider \Yiisoft\Db\Oracle\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\Oracle\Tests\Provider;

class ColumnDefinitionParserProvider extends \Yiisoft\Db\Tests\Provider\ColumnDefinitionParserProvider
{
public static function parse(): array
{
return [
...parent::parse(),
['interval day to second', ['type' => 'interval day to second']],
['interval day to second (2)', ['type' => 'interval day to second', 'size' => 2]],
['interval day(0) to second(2)', ['type' => 'interval day(0) to second', 'size' => 2]],
['timestamp with time zone', ['type' => 'timestamp with time zone']],
['timestamp (3) with time zone', ['type' => 'timestamp (3) with time zone', 'size' => 3]],
['timestamp(3) with local time zone', ['type' => 'timestamp(3) with local time zone', 'size' => 3]],
['interval year to month', ['type' => 'interval year to month']],
];
}
}