From ec9f24777d4f271df0d02536c100fb427e00b0c1 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Fri, 6 Sep 2024 13:40:15 +0700 Subject: [PATCH 1/3] Separate column type constants --- src/Column.php | 2 +- src/Column/ColumnFactory.php | 72 ++++++++++++------------ src/QueryBuilder.php | 48 ++++++++-------- tests/Provider/ColumnFactoryProvider.php | 67 +++++++++++----------- tests/QueryBuilderTest.php | 34 +++++------ 5 files changed, 113 insertions(+), 110 deletions(-) diff --git a/src/Column.php b/src/Column.php index efbedea1d..1ce3b73ef 100644 --- a/src/Column.php +++ b/src/Column.php @@ -16,7 +16,7 @@ * For example, the following code creates a column schema for an integer column: * * ```php - * $column = (new Column(SchemaInterface::TYPE_INTEGER))->notNull()->defaultValue(0); + * $column = (new Column(ColumnType::INTEGER))->notNull()->defaultValue(0); * ``` * * Provides a fluent interface, which means that the methods can be chained together to create a column schema with diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index dee9e8a5f..52d22dab0 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -4,9 +4,9 @@ namespace Yiisoft\Db\Mssql\Column; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Schema\Column\AbstractColumnFactory; use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; -use Yiisoft\Db\Schema\SchemaInterface; final class ColumnFactory extends AbstractColumnFactory { @@ -17,63 +17,63 @@ final class ColumnFactory extends AbstractColumnFactory */ private const TYPE_MAP = [ /** Exact numbers */ - 'bit' => SchemaInterface::TYPE_BOOLEAN, - 'tinyint' => SchemaInterface::TYPE_TINYINT, - 'smallint' => SchemaInterface::TYPE_SMALLINT, - 'int' => SchemaInterface::TYPE_INTEGER, - 'bigint' => SchemaInterface::TYPE_BIGINT, - 'numeric' => SchemaInterface::TYPE_DECIMAL, - 'decimal' => SchemaInterface::TYPE_DECIMAL, - 'smallmoney' => SchemaInterface::TYPE_MONEY, - 'money' => SchemaInterface::TYPE_MONEY, + 'bit' => ColumnType::BOOLEAN, + 'tinyint' => ColumnType::TINYINT, + 'smallint' => ColumnType::SMALLINT, + 'int' => ColumnType::INTEGER, + 'bigint' => ColumnType::BIGINT, + 'numeric' => ColumnType::DECIMAL, + 'decimal' => ColumnType::DECIMAL, + 'smallmoney' => ColumnType::MONEY, + 'money' => ColumnType::MONEY, /** Approximate numbers */ - 'float' => SchemaInterface::TYPE_FLOAT, - 'real' => SchemaInterface::TYPE_FLOAT, - 'double' => SchemaInterface::TYPE_DOUBLE, + 'float' => ColumnType::FLOAT, + 'real' => ColumnType::FLOAT, + 'double' => ColumnType::DOUBLE, /** Date and time */ - 'date' => SchemaInterface::TYPE_DATE, - 'time' => SchemaInterface::TYPE_TIME, - 'smalldatetime' => SchemaInterface::TYPE_DATETIME, - 'datetime' => SchemaInterface::TYPE_DATETIME, - 'datetime2' => SchemaInterface::TYPE_DATETIME, - 'datetimeoffset' => SchemaInterface::TYPE_DATETIME, + 'date' => ColumnType::DATE, + 'time' => ColumnType::TIME, + 'smalldatetime' => ColumnType::DATETIME, + 'datetime' => ColumnType::DATETIME, + 'datetime2' => ColumnType::DATETIME, + 'datetimeoffset' => ColumnType::DATETIME, /** Character strings */ - 'char' => SchemaInterface::TYPE_CHAR, - 'varchar' => SchemaInterface::TYPE_STRING, - 'text' => SchemaInterface::TYPE_TEXT, + 'char' => ColumnType::CHAR, + 'varchar' => ColumnType::STRING, + 'text' => ColumnType::TEXT, /** Unicode character strings */ - 'nchar' => SchemaInterface::TYPE_CHAR, - 'nvarchar' => SchemaInterface::TYPE_STRING, - 'ntext' => SchemaInterface::TYPE_TEXT, + 'nchar' => ColumnType::CHAR, + 'nvarchar' => ColumnType::STRING, + 'ntext' => ColumnType::TEXT, /** Binary strings */ - 'binary' => SchemaInterface::TYPE_BINARY, - 'varbinary' => SchemaInterface::TYPE_BINARY, - 'image' => SchemaInterface::TYPE_BINARY, + 'binary' => ColumnType::BINARY, + 'varbinary' => ColumnType::BINARY, + 'image' => ColumnType::BINARY, /** * Other data types 'cursor' type can't be used with tables */ - 'timestamp' => SchemaInterface::TYPE_TIMESTAMP, - 'hierarchyid' => SchemaInterface::TYPE_STRING, - 'uniqueidentifier' => SchemaInterface::TYPE_STRING, - 'sql_variant' => SchemaInterface::TYPE_STRING, - 'xml' => SchemaInterface::TYPE_STRING, - 'table' => SchemaInterface::TYPE_STRING, + 'timestamp' => ColumnType::TIMESTAMP, + 'hierarchyid' => ColumnType::STRING, + 'uniqueidentifier' => ColumnType::STRING, + 'sql_variant' => ColumnType::STRING, + 'xml' => ColumnType::STRING, + 'table' => ColumnType::STRING, ]; protected function getType(string $dbType, array $info = []): string { - return self::TYPE_MAP[$dbType] ?? SchemaInterface::TYPE_STRING; + return self::TYPE_MAP[$dbType] ?? ColumnType::STRING; } public function fromType(string $type, array $info = []): ColumnSchemaInterface { - if ($type === SchemaInterface::TYPE_BINARY) { + if ($type === ColumnType::BINARY) { return (new BinaryColumnSchema($type))->load($info); } diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index bdde10f6e..89bb9cbea 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -4,6 +4,8 @@ namespace Yiisoft\Db\Mssql; +use Yiisoft\Db\Constant\ColumnType; +use Yiisoft\Db\Constant\PseudoType; use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder; use Yiisoft\Db\Schema\Builder\ColumnInterface; use Yiisoft\Db\Schema\QuoterInterface; @@ -20,29 +22,29 @@ final class QueryBuilder extends AbstractQueryBuilder * @psalm-var string[] $typeMap Mapping from abstract column types (keys) to physical column types (values). */ protected array $typeMap = [ - SchemaInterface::TYPE_PK => 'int IDENTITY PRIMARY KEY', - SchemaInterface::TYPE_UPK => 'int IDENTITY PRIMARY KEY', - SchemaInterface::TYPE_BIGPK => 'bigint IDENTITY PRIMARY KEY', - SchemaInterface::TYPE_UBIGPK => 'bigint IDENTITY PRIMARY KEY', - SchemaInterface::TYPE_CHAR => 'nchar(1)', - SchemaInterface::TYPE_STRING => 'nvarchar(255)', - SchemaInterface::TYPE_TEXT => 'nvarchar(max)', - SchemaInterface::TYPE_TINYINT => 'tinyint', - SchemaInterface::TYPE_SMALLINT => 'smallint', - SchemaInterface::TYPE_INTEGER => 'int', - SchemaInterface::TYPE_BIGINT => 'bigint', - SchemaInterface::TYPE_FLOAT => 'float', - SchemaInterface::TYPE_DOUBLE => 'float', - SchemaInterface::TYPE_DECIMAL => 'decimal(18,0)', - SchemaInterface::TYPE_DATETIME => 'datetime', - SchemaInterface::TYPE_TIMESTAMP => 'datetime', - SchemaInterface::TYPE_TIME => 'time', - SchemaInterface::TYPE_DATE => 'date', - SchemaInterface::TYPE_BINARY => 'varbinary(max)', - SchemaInterface::TYPE_BOOLEAN => 'bit', - SchemaInterface::TYPE_MONEY => 'decimal(19,4)', - SchemaInterface::TYPE_UUID => 'UNIQUEIDENTIFIER', - SchemaInterface::TYPE_UUID_PK => 'UNIQUEIDENTIFIER PRIMARY KEY', + PseudoType::PK => 'int IDENTITY PRIMARY KEY', + PseudoType::UPK => 'int IDENTITY PRIMARY KEY', + PseudoType::BIGPK => 'bigint IDENTITY PRIMARY KEY', + PseudoType::UBIGPK => 'bigint IDENTITY PRIMARY KEY', + ColumnType::CHAR => 'nchar(1)', + ColumnType::STRING => 'nvarchar(255)', + ColumnType::TEXT => 'nvarchar(max)', + ColumnType::TINYINT => 'tinyint', + ColumnType::SMALLINT => 'smallint', + ColumnType::INTEGER => 'int', + ColumnType::BIGINT => 'bigint', + ColumnType::FLOAT => 'float', + ColumnType::DOUBLE => 'float', + ColumnType::DECIMAL => 'decimal(18,0)', + ColumnType::DATETIME => 'datetime', + ColumnType::TIMESTAMP => 'datetime', + ColumnType::TIME => 'time', + ColumnType::DATE => 'date', + ColumnType::BINARY => 'varbinary(max)', + ColumnType::BOOLEAN => 'bit', + ColumnType::MONEY => 'decimal(19,4)', + ColumnType::UUID => 'UNIQUEIDENTIFIER', + PseudoType::UUID_PK => 'UNIQUEIDENTIFIER PRIMARY KEY', ]; public function __construct(QuoterInterface $quoter, SchemaInterface $schema) diff --git a/tests/Provider/ColumnFactoryProvider.php b/tests/Provider/ColumnFactoryProvider.php index 1b115a64c..5f0d83507 100644 --- a/tests/Provider/ColumnFactoryProvider.php +++ b/tests/Provider/ColumnFactoryProvider.php @@ -4,6 +4,7 @@ namespace Yiisoft\Db\Mssql\Tests\Provider; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Mssql\Column\BinaryColumnSchema; use Yiisoft\Db\Schema\Column\BooleanColumnSchema; use Yiisoft\Db\Schema\Column\DoubleColumnSchema; @@ -16,39 +17,39 @@ public static function dbTypes(): array { return [ // db type, expected abstract type, expected instance of - ['bit', 'boolean', BooleanColumnSchema::class], - ['tinyint', 'tinyint', IntegerColumnSchema::class], - ['smallint', 'smallint', IntegerColumnSchema::class], - ['int', 'integer', IntegerColumnSchema::class], - ['bigint', 'bigint', IntegerColumnSchema::class], - ['numeric', 'decimal', DoubleColumnSchema::class], - ['decimal', 'decimal', DoubleColumnSchema::class], - ['float', 'float', DoubleColumnSchema::class], - ['real', 'float', DoubleColumnSchema::class], - ['double', 'double', DoubleColumnSchema::class], - ['smallmoney', 'money', StringColumnSchema::class], - ['money', 'money', StringColumnSchema::class], - ['date', 'date', StringColumnSchema::class], - ['time', 'time', StringColumnSchema::class], - ['smalldatetime', 'datetime', StringColumnSchema::class], - ['datetime', 'datetime', StringColumnSchema::class], - ['datetime2', 'datetime', StringColumnSchema::class], - ['datetimeoffset', 'datetime', StringColumnSchema::class], - ['char', 'char', StringColumnSchema::class], - ['varchar', 'string', StringColumnSchema::class], - ['text', 'text', StringColumnSchema::class], - ['nchar', 'char', StringColumnSchema::class], - ['nvarchar', 'string', StringColumnSchema::class], - ['ntext', 'text', StringColumnSchema::class], - ['binary', 'binary', BinaryColumnSchema::class], - ['varbinary', 'binary', BinaryColumnSchema::class], - ['image', 'binary', BinaryColumnSchema::class], - ['timestamp', 'timestamp', StringColumnSchema::class], - ['hierarchyid', 'string', StringColumnSchema::class], - ['uniqueidentifier', 'string', StringColumnSchema::class], - ['sql_variant', 'string', StringColumnSchema::class], - ['xml', 'string', StringColumnSchema::class], - ['table', 'string', StringColumnSchema::class], + ['bit', ColumnType::BOOLEAN, BooleanColumnSchema::class], + ['tinyint', ColumnType::TINYINT, IntegerColumnSchema::class], + ['smallint', ColumnType::SMALLINT, IntegerColumnSchema::class], + ['int', ColumnType::INTEGER, IntegerColumnSchema::class], + ['bigint', ColumnType::BIGINT, IntegerColumnSchema::class], + ['numeric', ColumnType::DECIMAL, DoubleColumnSchema::class], + ['decimal', ColumnType::DECIMAL, DoubleColumnSchema::class], + ['float', ColumnType::FLOAT, DoubleColumnSchema::class], + ['real', ColumnType::FLOAT, DoubleColumnSchema::class], + ['double', ColumnType::DOUBLE, DoubleColumnSchema::class], + ['smallmoney', ColumnType::MONEY, StringColumnSchema::class], + ['money', ColumnType::MONEY, StringColumnSchema::class], + ['date', ColumnType::DATE, StringColumnSchema::class], + ['time', ColumnType::TIME, StringColumnSchema::class], + ['smalldatetime', ColumnType::DATETIME, StringColumnSchema::class], + ['datetime', ColumnType::DATETIME, StringColumnSchema::class], + ['datetime2', ColumnType::DATETIME, StringColumnSchema::class], + ['datetimeoffset', ColumnType::DATETIME, StringColumnSchema::class], + ['char', ColumnType::CHAR, StringColumnSchema::class], + ['varchar', ColumnType::STRING, StringColumnSchema::class], + ['text', ColumnType::TEXT, StringColumnSchema::class], + ['nchar', ColumnType::CHAR, StringColumnSchema::class], + ['nvarchar', ColumnType::STRING, StringColumnSchema::class], + ['ntext', ColumnType::TEXT, StringColumnSchema::class], + ['binary', ColumnType::BINARY, BinaryColumnSchema::class], + ['varbinary', ColumnType::BINARY, BinaryColumnSchema::class], + ['image', ColumnType::BINARY, BinaryColumnSchema::class], + ['timestamp', ColumnType::TIMESTAMP, StringColumnSchema::class], + ['hierarchyid', ColumnType::STRING, StringColumnSchema::class], + ['uniqueidentifier', ColumnType::STRING, StringColumnSchema::class], + ['sql_variant', ColumnType::STRING, StringColumnSchema::class], + ['xml', ColumnType::STRING, StringColumnSchema::class], + ['table', ColumnType::STRING, StringColumnSchema::class], ]; } } diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 626f59b89..ea8e9f979 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -6,6 +6,7 @@ use JsonException; use Throwable; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\IntegrityException; use Yiisoft\Db\Exception\InvalidArgumentException; @@ -17,7 +18,6 @@ use Yiisoft\Db\Mssql\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryInterface; -use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Tests\Common\CommonQueryBuilderTest; use function json_encode; @@ -668,7 +668,7 @@ public function testAlterColumn(): void $sql = $qb->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 255))->notNull() + (new Column(ColumnType::STRING, 255))->notNull() ); $this->assertEquals($expected, $sql); @@ -700,7 +700,7 @@ public function testAlterColumn(): void $sql = $qb->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 255))->check('LEN(bar) > 5') + (new Column(ColumnType::STRING, 255))->check('LEN(bar) > 5') ); $this->assertEquals($expected, $sql); @@ -732,7 +732,7 @@ public function testAlterColumn(): void $sql = $qb->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 255))->defaultValue('') + (new Column(ColumnType::STRING, 255))->defaultValue('') ); $this->assertEquals($expected, $sql); @@ -764,7 +764,7 @@ public function testAlterColumn(): void $sql = $qb->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 255))->defaultValue('AbCdE') + (new Column(ColumnType::STRING, 255))->defaultValue('AbCdE') ); $this->assertEquals($expected, $sql); @@ -796,7 +796,7 @@ public function testAlterColumn(): void $sql = $qb->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_TIMESTAMP))->defaultExpression('CURRENT_TIMESTAMP') + (new Column(ColumnType::TIMESTAMP))->defaultExpression('CURRENT_TIMESTAMP') ); $this->assertEquals($expected, $sql); @@ -828,7 +828,7 @@ public function testAlterColumn(): void $sql = $qb->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 30))->unique() + (new Column(ColumnType::STRING, 30))->unique() ); $this->assertEquals($expected, $sql); } @@ -852,7 +852,7 @@ public function testAlterColumnOnDb(): void $sql = $db->getQueryBuilder()->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 128))->notNull() + (new Column(ColumnType::STRING, 128))->notNull() ); $db->createCommand($sql)->execute(); $schema = $db->getTableSchema('[foo1]', true); @@ -863,11 +863,11 @@ public function testAlterColumnOnDb(): void $sql = $db->getQueryBuilder()->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_TIMESTAMP))->defaultExpression('CURRENT_TIMESTAMP') + (new Column(ColumnType::TIMESTAMP))->defaultExpression('CURRENT_TIMESTAMP') ); $db->createCommand($sql)->execute(); $schema = $db->getTableSchema('[foo1]', true); - $this->assertSame(SchemaInterface::TYPE_DATETIME, $schema?->getColumn('bar')->getDbType()); + $this->assertSame(ColumnType::DATETIME, $schema?->getColumn('bar')->getDbType()); $this->assertSame('getdate()', $schema?->getColumn('bar')->getDefaultValue()); } @@ -883,7 +883,7 @@ public function testAlterColumnWithCheckConstraintOnDb(): void $sql = $db->getQueryBuilder()->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 128))->null()->check('LEN(bar) > 5') + (new Column(ColumnType::STRING, 128))->null()->check('LEN(bar) > 5') ); $db->createCommand($sql)->execute(); $schema = $db->getTableSchema('[foo1]', true); @@ -906,7 +906,7 @@ public function testAlterColumnWithCheckConstraintOnDbWithException(): void $sql = $db->getQueryBuilder()->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 64))->check('LEN(bar) > 5') + (new Column(ColumnType::STRING, 64))->check('LEN(bar) > 5') ); $db->createCommand($sql)->execute(); @@ -927,7 +927,7 @@ public function testAlterColumnWithUniqueConstraintOnDbWithException(): void $sql = $db->getQueryBuilder()->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 64))->unique() + (new Column(ColumnType::STRING, 64))->unique() ); $db->createCommand($sql)->execute(); @@ -1011,7 +1011,7 @@ public function testDropColumnOnDb(): void $sql = $db->getQueryBuilder()->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_STRING, 64)) + (new Column(ColumnType::STRING, 64)) ->defaultValue('') ->check('LEN(bar) < 5') ->unique() @@ -1061,7 +1061,7 @@ public function testAlterColumnWithNull(): void $sql = $qb->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_INTEGER))->null()->defaultValue(null) + (new Column(ColumnType::INTEGER))->null()->defaultValue(null) ); $this->assertEquals($expected, $sql); @@ -1093,7 +1093,7 @@ public function testAlterColumnWithNull(): void $sql = $qb->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_INTEGER))->defaultValue(null) + (new Column(ColumnType::INTEGER))->defaultValue(null) ); $this->assertEquals($expected, $sql); } @@ -1134,7 +1134,7 @@ public function testAlterColumnWithExpression(): void $sql = $qb->alterColumn( 'foo1', 'bar', - (new Column(SchemaInterface::TYPE_INTEGER)) + (new Column(ColumnType::INTEGER)) ->null() ->defaultValue(new Expression('CAST(GETDATE() AS INT)')) ); From f98cb6f8374f2348607991cbb898c81c841529e1 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sun, 8 Sep 2024 14:45:55 +0700 Subject: [PATCH 2/3] Realize `ColumnBuilder` --- src/Column/ColumnBuilder.php | 15 +++++++++++++++ src/Column/ColumnFactory.php | 5 +++++ src/Connection.php | 6 ++++++ src/Schema.php | 11 +++-------- tests/ColumnBuilderTest.php | 23 +++++++++++++++++++++++ tests/ColumnFactoryTest.php | 20 +++++++++++++++++--- tests/ConnectionTest.php | 8 ++++++++ tests/SchemaTest.php | 9 --------- 8 files changed, 77 insertions(+), 20 deletions(-) create mode 100644 src/Column/ColumnBuilder.php create mode 100644 tests/ColumnBuilderTest.php diff --git a/src/Column/ColumnBuilder.php b/src/Column/ColumnBuilder.php new file mode 100644 index 000000000..5a9840931 --- /dev/null +++ b/src/Column/ColumnBuilder.php @@ -0,0 +1,15 @@ +queryBuilder === null) { diff --git a/src/Schema.php b/src/Schema.php index 4bb0598c1..8e390f896 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->getColumnBuilderClass()::columnFactory(); + $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 000000000..0a30cd326 --- /dev/null +++ b/tests/ColumnBuilderTest.php @@ -0,0 +1,23 @@ +getConnection(); + $columnBuilderClass = $db->getColumnBuilderClass(); + + $this->assertInstanceOf(ColumnFactory::class, $columnBuilderClass::columnFactory()); + } +} diff --git a/tests/ColumnFactoryTest.php b/tests/ColumnFactoryTest.php index 21150e9cb..b409eae91 100644 --- a/tests/ColumnFactoryTest.php +++ b/tests/ColumnFactoryTest.php @@ -21,9 +21,23 @@ public function testFromDbType(string $dbType, string $expectedType, string $exp } /** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ColumnFactoryProvider::definitions */ - public function testFromDefinition(string $definition, string $expectedType, string $expectedInstanceOf, array $expectedInfo = []): void - { - parent::testFromDefinition($definition, $expectedType, $expectedInstanceOf, $expectedInfo); + public function testFromDefinition( + string $definition, + string $expectedType, + string $expectedInstanceOf, + array $expectedMethodResults = [] + ): void { + parent::testFromDefinition($definition, $expectedType, $expectedInstanceOf, $expectedMethodResults); + } + + /** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ColumnFactoryProvider::pseudoTypes */ + public function testFromPseudoType( + string $pseudoType, + string $expectedType, + string $expectedInstanceOf, + array $expectedMethodResults = [] + ): void { + parent::testFromPseudoType($pseudoType, $expectedType, $expectedInstanceOf, $expectedMethodResults); } /** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ColumnFactoryProvider::types */ diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index f35853b4c..9eec959ba 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -10,6 +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\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonConnectionTest; use Yiisoft\Db\Transaction\TransactionInterface; @@ -87,4 +88,11 @@ public function testSettingDefaultAttributes(): void $this->assertSame(PDO::ERRMODE_EXCEPTION, $db->getActivePDO()?->getAttribute(PDO::ATTR_ERRMODE)); } + + public function testGetColumnBuilderClass(): void + { + $db = $this->getConnection(); + + $this->assertSame(ColumnBuilder::class, $db->getColumnBuilderClass()); + } } diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 310b52939..3be139c2e 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); - } } From f39ea2655e48413e0d6944697e60043198b0e870 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Mon, 16 Sep 2024 12:25:17 +0700 Subject: [PATCH 3/3] Remove `ColumnFactory` from `ColumnBuilder` --- CHANGELOG.md | 1 + src/Column/ColumnBuilder.php | 8 +++++--- src/Connection.php | 7 ++++--- src/Schema.php | 2 +- tests/ColumnBuilderTest.php | 22 +++++++++++++++++----- tests/ConnectionTest.php | 6 +++--- tests/Provider/ColumnBuilderProvider.php | 21 +++++++++++++++++++++ 7 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 tests/Provider/ColumnBuilderProvider.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 985306c80..49c8f4ab1 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 5a9840931..945c43f97 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 899621da0..6320efbb8 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 8e390f896..fadea8bcf 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 0a30cd326..c95cf1590 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 9eec959ba..f92ffbef1 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 000000000..52e0e36d4 --- /dev/null +++ b/tests/Provider/ColumnBuilderProvider.php @@ -0,0 +1,21 @@ + 8]], + ]; + } +}