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 index 5a984093..945c43f9 100644 --- a/src/Column/ColumnBuilder.php +++ b/src/Column/ColumnBuilder.php @@ -4,12 +4,14 @@ namespace Yiisoft\Db\Mssql\Column; -use Yiisoft\Db\Schema\Column\ColumnFactoryInterface; +use Yiisoft\Db\Constant\ColumnType; +use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; final class ColumnBuilder extends \Yiisoft\Db\Schema\Column\ColumnBuilder { - public static function columnFactory(): ColumnFactoryInterface + public static function binary(int|null $size = null): ColumnSchemaInterface { - return new ColumnFactory(); + return (new BinaryColumnSchema(ColumnType::BINARY)) + ->size($size); } } diff --git a/src/Connection.php b/src/Connection.php index 899621da..6320efbb 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -6,10 +6,11 @@ use Yiisoft\Db\Driver\Pdo\AbstractPdoConnection; use Yiisoft\Db\Driver\Pdo\PdoCommandInterface; -use Yiisoft\Db\Mssql\Column\ColumnBuilder; +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; @@ -50,9 +51,9 @@ public function createTransaction(): TransactionInterface return new Transaction($this); } - public function getColumnBuilderClass(): string + public function getColumnFactory(): ColumnFactoryInterface { - return ColumnBuilder::class; + return new ColumnFactory(); } public function getQueryBuilder(): QueryBuilderInterface diff --git a/src/Schema.php b/src/Schema.php index 8e390f89..fadea8bc 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -359,7 +359,7 @@ protected function loadTableDefaultValues(string $tableName): array */ private function loadColumnSchema(array $info): ColumnSchemaInterface { - $columnFactory = $this->db->getColumnBuilderClass()::columnFactory(); + $columnFactory = $this->db->getColumnFactory(); $dbType = $info['data_type']; /** @psalm-var ColumnArray $info */ diff --git a/tests/ColumnBuilderTest.php b/tests/ColumnBuilderTest.php index 0a30cd32..c95cf159 100644 --- a/tests/ColumnBuilderTest.php +++ b/tests/ColumnBuilderTest.php @@ -2,7 +2,9 @@ declare(strict_types=1); -use Yiisoft\Db\Mssql\Column\ColumnFactory; +namespace Yiisoft\Db\Mssql\Tests; + +use Yiisoft\Db\Mssql\Column\ColumnBuilder; use Yiisoft\Db\Mssql\Tests\Support\TestTrait; use Yiisoft\Db\Tests\AbstractColumnBuilderTest; @@ -13,11 +15,21 @@ class ColumnBuilderTest extends AbstractColumnBuilderTest { use TestTrait; - public function testColumnFactory(): void + public function getColumnBuilderClass(): string { - $db = $this->getConnection(); - $columnBuilderClass = $db->getColumnBuilderClass(); + return ColumnBuilder::class; + } - $this->assertInstanceOf(ColumnFactory::class, $columnBuilderClass::columnFactory()); + /** + * @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ColumnBuilderProvider::buildingMethods + */ + public function testBuildingMethods( + string $buildingMethod, + array $args, + string $expectedInstanceOf, + string $expectedType, + array $expectedMethodResults = [], + ): void { + parent::testBuildingMethods($buildingMethod, $args, $expectedInstanceOf, $expectedType, $expectedMethodResults); } } diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 9eec959b..f92ffbef 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -10,7 +10,7 @@ use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; -use Yiisoft\Db\Mssql\Column\ColumnBuilder; +use Yiisoft\Db\Mssql\Column\ColumnFactory; use Yiisoft\Db\Mssql\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonConnectionTest; use Yiisoft\Db\Transaction\TransactionInterface; @@ -89,10 +89,10 @@ public function testSettingDefaultAttributes(): void $this->assertSame(PDO::ERRMODE_EXCEPTION, $db->getActivePDO()?->getAttribute(PDO::ATTR_ERRMODE)); } - public function testGetColumnBuilderClass(): void + public function testGetColumnFactory(): void { $db = $this->getConnection(); - $this->assertSame(ColumnBuilder::class, $db->getColumnBuilderClass()); + $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]], + ]; + } +}