From 7df600ce762cedc447772468bd20390d00de509d Mon Sep 17 00:00:00 2001 From: Tigrov Date: Fri, 8 Nov 2024 20:44:07 +0700 Subject: [PATCH] Refactor `normalizeDefaultValue()` --- src/Column/ColumnFactory.php | 7 +++++ src/Schema.php | 36 ++---------------------- tests/ColumnFactoryTest.php | 16 ++++++++--- tests/Provider/ColumnFactoryProvider.php | 13 +++++++++ tests/Provider/SchemaProvider.php | 6 ++-- 5 files changed, 37 insertions(+), 41 deletions(-) diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index 96f77bb..c3d9c2b 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -6,8 +6,10 @@ use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Schema\Column\AbstractColumnFactory; +use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; use function preg_replace; +use function rtrim; use function strtolower; final class ColumnFactory extends AbstractColumnFactory @@ -74,4 +76,9 @@ protected function getColumnClass(string $type, array $info = []): string return parent::getColumnClass($type, $info); } + + protected function normalizeNotNullDefaultValue(string $defaultValue, ColumnSchemaInterface $column): mixed + { + return parent::normalizeNotNullDefaultValue(rtrim($defaultValue), $column); + } } diff --git a/src/Schema.php b/src/Schema.php index 1dd61e3..4102f3f 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -449,9 +449,10 @@ protected function getTableSequenceName(string $tableName): string|null */ private function loadColumnSchema(array $info): ColumnSchemaInterface { - $column = $this->getColumnFactory()->fromDbType($info['data_type'], [ + return $this->getColumnFactory()->fromDbType($info['data_type'], [ 'autoIncrement' => $info['identity_column'] === 'YES', 'comment' => $info['column_comment'], + 'defaultValueRaw' => $info['data_default'], 'name' => $info['column_name'], 'notNull' => $info['nullable'] !== 'Y', 'primaryKey' => $info['constraint_type'] === 'P', @@ -461,39 +462,6 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface 'table' => $info['table'], 'unique' => $info['constraint_type'] === 'U', ]); - - return $column->defaultValue($this->normalizeDefaultValue($info['data_default'], $column)); - } - - /** - * Converts column's default value according to {@see ColumnSchema::phpType} after retrieval from the database. - * - * @param string|null $defaultValue The default value retrieved from the database. - * @param ColumnSchemaInterface $column The column schema object. - * - * @return mixed The normalized default value. - */ - private function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaInterface $column): mixed - { - if ($defaultValue === null || $column->isPrimaryKey()) { - return null; - } - - $defaultValue = trim($defaultValue); - - if ($defaultValue === 'NULL') { - return null; - } - - if ($column->getType() === ColumnType::TIMESTAMP && $defaultValue === 'CURRENT_TIMESTAMP') { - return new Expression($defaultValue); - } - - if (preg_match("/^'(.*)'$/s", $defaultValue, $matches) === 1) { - $defaultValue = str_replace("''", "'", $matches[1]); - } - - return $column->phpTypecast($defaultValue); } /** diff --git a/tests/ColumnFactoryTest.php b/tests/ColumnFactoryTest.php index 3cbaaf3..78d8e6b 100644 --- a/tests/ColumnFactoryTest.php +++ b/tests/ColumnFactoryTest.php @@ -4,6 +4,8 @@ namespace Yiisoft\Db\Oracle\Tests; +use PHPUnit\Framework\Attributes\DataProviderExternal; +use Yiisoft\Db\Oracle\Tests\Provider\ColumnFactoryProvider; use Yiisoft\Db\Oracle\Tests\Support\TestTrait; use Yiisoft\Db\Tests\AbstractColumnFactoryTest; @@ -14,13 +16,13 @@ final class ColumnFactoryTest extends AbstractColumnFactoryTest { use TestTrait; - /** @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\ColumnFactoryProvider::dbTypes */ + #[DataProviderExternal(ColumnFactoryProvider::class, 'dbTypes')] public function testFromDbType(string $dbType, string $expectedType, string $expectedInstanceOf): void { parent::testFromDbType($dbType, $expectedType, $expectedInstanceOf); } - /** @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\ColumnFactoryProvider::definitions */ + #[DataProviderExternal(ColumnFactoryProvider::class, 'definitions')] public function testFromDefinition( string $definition, string $expectedType, @@ -30,7 +32,7 @@ public function testFromDefinition( parent::testFromDefinition($definition, $expectedType, $expectedInstanceOf, $expectedMethodResults); } - /** @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\ColumnFactoryProvider::pseudoTypes */ + #[DataProviderExternal(ColumnFactoryProvider::class, 'pseudoTypes')] public function testFromPseudoType( string $pseudoType, string $expectedType, @@ -40,9 +42,15 @@ public function testFromPseudoType( parent::testFromPseudoType($pseudoType, $expectedType, $expectedInstanceOf, $expectedMethodResults); } - /** @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\ColumnFactoryProvider::types */ + #[DataProviderExternal(ColumnFactoryProvider::class, 'types')] public function testFromType(string $type, string $expectedType, string $expectedInstanceOf): void { parent::testFromType($type, $expectedType, $expectedInstanceOf); } + + #[DataProviderExternal(ColumnFactoryProvider::class, 'defaultValueRaw')] + public function testFromTypeDefaultValueRaw(string $type, string|null $defaultValueRaw, mixed $expected): void + { + parent::testFromTypeDefaultValueRaw($type, $defaultValueRaw, $expected); + } } diff --git a/tests/Provider/ColumnFactoryProvider.php b/tests/Provider/ColumnFactoryProvider.php index 88731d4..f31b092 100644 --- a/tests/Provider/ColumnFactoryProvider.php +++ b/tests/Provider/ColumnFactoryProvider.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Oracle\Tests\Provider; use Yiisoft\Db\Constant\ColumnType; +use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Oracle\Column\BinaryColumnSchema; use Yiisoft\Db\Schema\Column\DoubleColumnSchema; use Yiisoft\Db\Schema\Column\StringColumnSchema; @@ -53,4 +54,16 @@ public static function definitions(): array return $definitions; } + + public static function defaultValueRaw(): array + { + $defaultValueRaw = parent::defaultValueRaw(); + + $defaultValueRaw[] = [ColumnType::STRING, 'NULL ', null]; + $defaultValueRaw[] = [ColumnType::STRING, "'str'ing' ", "str'ing"]; + $defaultValueRaw[] = [ColumnType::INTEGER, '-1 ', -1]; + $defaultValueRaw[] = [ColumnType::TIMESTAMP, 'now() ', new Expression('now()')]; + + return $defaultValueRaw; + } } diff --git a/tests/Provider/SchemaProvider.php b/tests/Provider/SchemaProvider.php index 4d5460a..8a406d8 100644 --- a/tests/Provider/SchemaProvider.php +++ b/tests/Provider/SchemaProvider.php @@ -169,7 +169,7 @@ public static function columns(): array 'enumValues' => null, 'size' => null, 'scale' => 6, - 'defaultValue' => "to_timestamp('2002-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')", + 'defaultValue' => new Expression("to_timestamp('2002-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')"), ], 'time_col' => [ 'type' => 'time', @@ -181,7 +181,7 @@ public static function columns(): array 'enumValues' => null, 'size' => 0, 'scale' => 0, - 'defaultValue' => "INTERVAL '0 10:33:21' DAY(0) TO SECOND(0)", + 'defaultValue' => new Expression("INTERVAL '0 10:33:21' DAY(0) TO SECOND(0)"), ], 'interval_day_col' => [ 'type' => 'string', @@ -193,7 +193,7 @@ public static function columns(): array 'enumValues' => null, 'size' => 1, 'scale' => 0, - 'defaultValue' => "INTERVAL '2 04:56:12' DAY(1) TO SECOND(0)", + 'defaultValue' => new Expression("INTERVAL '2 04:56:12' DAY(1) TO SECOND(0)"), ], 'bool_col' => [ 'type' => 'char',