diff --git a/CHANGELOG.md b/CHANGELOG.md index 985306c8..49c8f4ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - Enh #317: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov) - Enh #316: Implement `ColumnFactory` class (@Tigrov) - Enh #319: Separate column type constants (@Tigrov) +- Enh #320: Realize `ColumnBuilder` class (@Tigrov) ## 1.2.0 March 21, 2024 diff --git a/src/Column/ColumnBuilder.php b/src/Column/ColumnBuilder.php new file mode 100644 index 00000000..945c43f9 --- /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 52d22dab..e95fd8c5 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -79,4 +79,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 79cca8b6..6320efbb 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -6,9 +6,11 @@ use Yiisoft\Db\Driver\Pdo\AbstractPdoConnection; use Yiisoft\Db\Driver\Pdo\PdoCommandInterface; +use Yiisoft\Db\Mssql\Column\ColumnFactory; use Yiisoft\Db\Query\BatchQueryResultInterface; use Yiisoft\Db\Query\QueryInterface; 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; @@ -49,6 +51,11 @@ public function createTransaction(): TransactionInterface return new Transaction($this); } + public function getColumnFactory(): ColumnFactoryInterface + { + return new ColumnFactory(); + } + public function getQueryBuilder(): QueryBuilderInterface { if ($this->queryBuilder === null) { diff --git a/src/Schema.php b/src/Schema.php index 4bb0598c..fadea8bc 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -14,9 +14,7 @@ use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Helper\DbArrayHelper; -use Yiisoft\Db\Mssql\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; @@ -72,11 +70,6 @@ public function createColumn(string $type, array|int|string|null $length = null) return new Column($type, $length); } - public function getColumnFactory(): ColumnFactoryInterface - { - return new ColumnFactory(); - } - /** * Resolves the table name and schema name (if any). * @@ -366,9 +359,11 @@ protected function loadTableDefaultValues(string $tableName): array */ private function loadColumnSchema(array $info): ColumnSchemaInterface { + $columnFactory = $this->db->getColumnFactory(); + $dbType = $info['data_type']; /** @psalm-var ColumnArray $info */ - $column = $this->getColumnFactory()->fromDefinition($dbType); + $column = $columnFactory->fromDefinition($dbType); $column->name($info['column_name']); $column->allowNull($info['is_nullable'] === 'YES'); $column->dbType($dbType); diff --git a/tests/ColumnBuilderTest.php b/tests/ColumnBuilderTest.php new file mode 100644 index 00000000..c95cf159 --- /dev/null +++ b/tests/ColumnBuilderTest.php @@ -0,0 +1,35 @@ +assertSame(PDO::ERRMODE_EXCEPTION, $db->getActivePDO()?->getAttribute(PDO::ATTR_ERRMODE)); } + + 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 00000000..52e0e36d --- /dev/null +++ b/tests/Provider/ColumnBuilderProvider.php @@ -0,0 +1,21 @@ + 8]], + ]; + } +} diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 310b5293..3be139c2 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -11,7 +11,6 @@ use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; -use Yiisoft\Db\Mssql\Column\ColumnFactory; use Yiisoft\Db\Mssql\Schema; use Yiisoft\Db\Mssql\Tests\Support\TestTrait; use Yiisoft\Db\Schema\SchemaInterface; @@ -198,12 +197,4 @@ public function testNegativeDefaultValues(): void $db->close(); } - - public function testGetColumnFactory(): void - { - $db = $this->getConnection(); - $factory = $db->getSchema()->getColumnFactory(); - - $this->assertInstanceOf(ColumnFactory::class, $factory); - } }