diff --git a/CHANGELOG.md b/CHANGELOG.md index c1281fb..267fc1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - Enh #277: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov) - Enh #276: Implement `ColumnFactory` class (@Tigrov) - Enh #279: Separate column type constants (@Tigrov) +- Enh #280: Realize `ColumnBuilder` class (@Tigrov) ## 1.3.0 March 21, 2024 diff --git a/src/Column/ColumnBuilder.php b/src/Column/ColumnBuilder.php new file mode 100644 index 0000000..a0dcd28 --- /dev/null +++ b/src/Column/ColumnBuilder.php @@ -0,0 +1,17 @@ +size($size); + } +} diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index bfdea88..30a0407 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -78,4 +78,9 @@ public function fromType(string $type, array $info = []): ColumnSchemaInterface return parent::fromType($type, $info); } + + protected function isDbType(string $dbType): bool + { + return isset(self::TYPE_MAP[$dbType]); + } } diff --git a/src/Connection.php b/src/Connection.php index 144dd7d..bed8d20 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -11,7 +11,9 @@ use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidCallException; use Yiisoft\Db\Exception\InvalidConfigException; +use Yiisoft\Db\Oracle\Column\ColumnFactory; use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; +use Yiisoft\Db\Schema\Column\ColumnFactoryInterface; use Yiisoft\Db\Schema\QuoterInterface; use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Transaction\TransactionInterface; @@ -47,6 +49,11 @@ public function createTransaction(): TransactionInterface return new Transaction($this); } + public function getColumnFactory(): ColumnFactoryInterface + { + return new ColumnFactory(); + } + /** * Override base behaviour to support Oracle sequences. * diff --git a/src/Schema.php b/src/Schema.php index d8b712f..fdd0df7 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -18,9 +18,7 @@ use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Helper\DbArrayHelper; -use Yiisoft\Db\Oracle\Column\ColumnFactory; use Yiisoft\Db\Schema\Builder\ColumnInterface; -use Yiisoft\Db\Schema\Column\ColumnFactoryInterface; use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; use Yiisoft\Db\Schema\TableSchemaInterface; @@ -79,11 +77,6 @@ public function createColumn(string $type, array|int|string $length = null): Col return new Column($type, $length); } - public function getColumnFactory(): ColumnFactoryInterface - { - return new ColumnFactory(); - } - protected function resolveTableName(string $name): TableSchemaInterface { $resolvedName = new TableSchema(); @@ -431,8 +424,10 @@ protected function getTableSequenceName(string $tableName): string|null */ private function loadColumnSchema(array $info): ColumnSchemaInterface { + $columnFactory = $this->db->getColumnFactory(); + $dbType = $info['data_type']; - $column = $this->getColumnFactory()->fromDbType($dbType, [ + $column = $columnFactory->fromDbType($dbType, [ 'scale' => $info['data_scale'], 'precision' => $info['data_precision'], ]); @@ -442,7 +437,6 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface $column->primaryKey((bool) $info['is_pk']); $column->autoIncrement($info['identity_column'] === 'YES'); $column->size((int) $info['data_length']); - $column->dbType($dbType); $column->defaultValue($this->normalizeDefaultValue($info['data_default'], $column)); return $column; diff --git a/tests/ColumnBuilderTest.php b/tests/ColumnBuilderTest.php new file mode 100644 index 0000000..d75fffa --- /dev/null +++ b/tests/ColumnBuilderTest.php @@ -0,0 +1,35 @@ +assertEquals(123, $unserialized->createCommand('SELECT 123 FROM DUAL')->queryScalar()); $this->assertNotNull($connection->getPDO()); } + + public function testGetColumnFactory(): void + { + $db = $this->getConnection(); + + $this->assertInstanceOf(ColumnFactory::class, $db->getColumnFactory()); + } } diff --git a/tests/Provider/ColumnBuilderProvider.php b/tests/Provider/ColumnBuilderProvider.php new file mode 100644 index 0000000..bc6bd1f --- /dev/null +++ b/tests/Provider/ColumnBuilderProvider.php @@ -0,0 +1,21 @@ + 8]], + ]; + } +} diff --git a/tests/Provider/ColumnFactoryProvider.php b/tests/Provider/ColumnFactoryProvider.php index f371332..88731d4 100644 --- a/tests/Provider/ColumnFactoryProvider.php +++ b/tests/Provider/ColumnFactoryProvider.php @@ -43,8 +43,11 @@ public static function definitions(): array $definitions = parent::definitions(); $definitions['text'][0] = 'clob'; + $definitions['text'][3]['getDbType'] = 'clob'; $definitions['text NOT NULL'][0] = 'clob NOT NULL'; + $definitions['text NOT NULL'][3]['getDbType'] = 'clob'; $definitions['decimal(10,2)'][0] = 'number(10,2)'; + $definitions['decimal(10,2)'][3]['getDbType'] = 'number'; unset($definitions['bigint UNSIGNED']); diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index b98ae5e..2fb6f0f 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -10,7 +10,6 @@ use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; -use Yiisoft\Db\Oracle\Column\ColumnFactory; use Yiisoft\Db\Oracle\Schema; use Yiisoft\Db\Oracle\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonSchemaTest; @@ -296,12 +295,4 @@ public function testNotConnectionPDO(): void $schema->refresh(); } - - public function testGetColumnFactory(): void - { - $db = $this->getConnection(); - $factory = $db->getSchema()->getColumnFactory(); - - $this->assertInstanceOf(ColumnFactory::class, $factory); - } }