From 9fee2628bb9e5af3ef35ba686c17076e40344320 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Fri, 6 Sep 2024 13:45:28 +0700 Subject: [PATCH 1/3] Separate column type constants --- src/Column.php | 2 +- src/Column/ColumnFactory.php | 66 +++++++++---------- src/QueryBuilder.php | 50 +++++++------- src/Schema.php | 2 +- tests/CommandTest.php | 9 +-- tests/Provider/ColumnFactoryProvider.php | 63 +++++++++--------- .../Provider/ColumnSchemaBuilderProvider.php | 4 +- tests/QueryBuilderTest.php | 6 +- 8 files changed, 103 insertions(+), 99 deletions(-) diff --git a/src/Column.php b/src/Column.php index 7253eebb..d6869619 100644 --- a/src/Column.php +++ b/src/Column.php @@ -15,7 +15,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 7b759d7b..96ab9d18 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -4,8 +4,8 @@ namespace Yiisoft\Db\Sqlite\Column; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Schema\Column\AbstractColumnFactory; -use Yiisoft\Db\Schema\SchemaInterface; final class ColumnFactory extends AbstractColumnFactory { @@ -17,47 +17,47 @@ final class ColumnFactory extends AbstractColumnFactory * @psalm-suppress MissingClassConstType */ private const TYPE_MAP = [ - 'bool' => SchemaInterface::TYPE_BOOLEAN, - 'boolean' => SchemaInterface::TYPE_BOOLEAN, - 'bit' => SchemaInterface::TYPE_BIT, - 'tinyint' => SchemaInterface::TYPE_TINYINT, - 'smallint' => SchemaInterface::TYPE_SMALLINT, - 'mediumint' => SchemaInterface::TYPE_INTEGER, - 'int' => SchemaInterface::TYPE_INTEGER, - 'integer' => SchemaInterface::TYPE_INTEGER, - 'bigint' => SchemaInterface::TYPE_BIGINT, - 'float' => SchemaInterface::TYPE_FLOAT, - 'real' => SchemaInterface::TYPE_FLOAT, - 'double' => SchemaInterface::TYPE_DOUBLE, - 'decimal' => SchemaInterface::TYPE_DECIMAL, - 'numeric' => SchemaInterface::TYPE_DECIMAL, - 'char' => SchemaInterface::TYPE_CHAR, - 'varchar' => SchemaInterface::TYPE_STRING, - 'string' => SchemaInterface::TYPE_STRING, - 'enum' => SchemaInterface::TYPE_STRING, - 'tinytext' => SchemaInterface::TYPE_TEXT, - 'mediumtext' => SchemaInterface::TYPE_TEXT, - 'longtext' => SchemaInterface::TYPE_TEXT, - 'text' => SchemaInterface::TYPE_TEXT, - 'blob' => SchemaInterface::TYPE_BINARY, - 'year' => SchemaInterface::TYPE_DATE, - 'date' => SchemaInterface::TYPE_DATE, - 'time' => SchemaInterface::TYPE_TIME, - 'datetime' => SchemaInterface::TYPE_DATETIME, - 'timestamp' => SchemaInterface::TYPE_TIMESTAMP, - 'json' => SchemaInterface::TYPE_JSON, + 'bool' => ColumnType::BOOLEAN, + 'boolean' => ColumnType::BOOLEAN, + 'bit' => ColumnType::BIT, + 'tinyint' => ColumnType::TINYINT, + 'smallint' => ColumnType::SMALLINT, + 'mediumint' => ColumnType::INTEGER, + 'int' => ColumnType::INTEGER, + 'integer' => ColumnType::INTEGER, + 'bigint' => ColumnType::BIGINT, + 'float' => ColumnType::FLOAT, + 'real' => ColumnType::FLOAT, + 'double' => ColumnType::DOUBLE, + 'decimal' => ColumnType::DECIMAL, + 'numeric' => ColumnType::DECIMAL, + 'char' => ColumnType::CHAR, + 'varchar' => ColumnType::STRING, + 'string' => ColumnType::STRING, + 'enum' => ColumnType::STRING, + 'tinytext' => ColumnType::TEXT, + 'mediumtext' => ColumnType::TEXT, + 'longtext' => ColumnType::TEXT, + 'text' => ColumnType::TEXT, + 'blob' => ColumnType::BINARY, + 'year' => ColumnType::DATE, + 'date' => ColumnType::DATE, + 'time' => ColumnType::TIME, + 'datetime' => ColumnType::DATETIME, + 'timestamp' => ColumnType::TIMESTAMP, + 'json' => ColumnType::JSON, ]; protected function getType(string $dbType, array $info = []): string { - $type = self::TYPE_MAP[$dbType] ?? SchemaInterface::TYPE_STRING; + $type = self::TYPE_MAP[$dbType] ?? ColumnType::STRING; if ( - ($type === SchemaInterface::TYPE_BIT || $type === SchemaInterface::TYPE_TINYINT) + ($type === ColumnType::BIT || $type === ColumnType::TINYINT) && isset($info['size']) && $info['size'] === 1 ) { - return SchemaInterface::TYPE_BOOLEAN; + return ColumnType::BOOLEAN; } return $type; diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 4f867bd2..1b255808 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -4,6 +4,8 @@ namespace Yiisoft\Db\Sqlite; +use Yiisoft\Db\Constant\ColumnType; +use Yiisoft\Db\Constant\PseudoType; use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder; use Yiisoft\Db\Schema\QuoterInterface; use Yiisoft\Db\Schema\SchemaInterface; @@ -17,30 +19,30 @@ final class QueryBuilder extends AbstractQueryBuilder * @var string[] Mapping from abstract column types (keys) to physical column types (values). */ protected array $typeMap = [ - SchemaInterface::TYPE_PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', - SchemaInterface::TYPE_UPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', - SchemaInterface::TYPE_BIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', - SchemaInterface::TYPE_UBIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', - SchemaInterface::TYPE_CHAR => 'char(1)', - SchemaInterface::TYPE_STRING => 'varchar(255)', - SchemaInterface::TYPE_TEXT => 'text', - SchemaInterface::TYPE_TINYINT => 'tinyint', - SchemaInterface::TYPE_SMALLINT => 'smallint', - SchemaInterface::TYPE_INTEGER => 'integer', - SchemaInterface::TYPE_BIGINT => 'bigint', - SchemaInterface::TYPE_FLOAT => 'float', - SchemaInterface::TYPE_DOUBLE => 'double', - SchemaInterface::TYPE_DECIMAL => 'decimal(10,0)', - SchemaInterface::TYPE_DATETIME => 'datetime', - SchemaInterface::TYPE_TIMESTAMP => 'timestamp', - SchemaInterface::TYPE_TIME => 'time', - SchemaInterface::TYPE_DATE => 'date', - SchemaInterface::TYPE_BINARY => 'blob', - SchemaInterface::TYPE_BOOLEAN => 'boolean', - SchemaInterface::TYPE_MONEY => 'decimal(19,4)', - SchemaInterface::TYPE_UUID => 'blob(16)', - SchemaInterface::TYPE_UUID_PK => 'blob(16) PRIMARY KEY', - SchemaInterface::TYPE_JSON => 'json', + PseudoType::PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', + PseudoType::UPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', + PseudoType::BIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', + PseudoType::UBIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', + ColumnType::CHAR => 'char(1)', + ColumnType::STRING => 'varchar(255)', + ColumnType::TEXT => 'text', + ColumnType::TINYINT => 'tinyint', + ColumnType::SMALLINT => 'smallint', + ColumnType::INTEGER => 'integer', + ColumnType::BIGINT => 'bigint', + ColumnType::FLOAT => 'float', + ColumnType::DOUBLE => 'double', + ColumnType::DECIMAL => 'decimal(10,0)', + ColumnType::DATETIME => 'datetime', + ColumnType::TIMESTAMP => 'timestamp', + ColumnType::TIME => 'time', + ColumnType::DATE => 'date', + ColumnType::BINARY => 'blob', + ColumnType::BOOLEAN => 'boolean', + ColumnType::MONEY => 'decimal(19,4)', + ColumnType::UUID => 'blob(16)', + PseudoType::UUID_PK => 'blob(16) PRIMARY KEY', + ColumnType::JSON => 'json', ]; public function __construct(QuoterInterface $quoter, SchemaInterface $schema) diff --git a/src/Schema.php b/src/Schema.php index 0252be1c..aba71a6c 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -337,7 +337,7 @@ protected function findColumns(TableSchemaInterface $table): bool foreach ($columns as $info) { if (in_array($info['name'], $jsonColumns, true)) { - $info['type'] = self::TYPE_JSON; + $info['type'] = ColumnType::JSON; } $column = $this->loadColumnSchema($info); diff --git a/tests/CommandTest.php b/tests/CommandTest.php index aaa7c38a..c68b5ffa 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -5,11 +5,12 @@ namespace Yiisoft\Db\Sqlite\Tests; use Throwable; +use Yiisoft\Db\Constant\ColumnType; +use Yiisoft\Db\Constant\PseudoType; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Expression\JsonExpression; -use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Sqlite\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonCommandTest; @@ -391,7 +392,7 @@ public function testResetSequence(): void $command->createTable( '{{testCreateTable}}', - ['id' => SchemaInterface::TYPE_PK, 'bar' => SchemaInterface::TYPE_INTEGER], + ['id' => PseudoType::PK, 'bar' => ColumnType::INTEGER], )->execute(); $command->insert('{{testCreateTable}}', ['bar' => 1])->execute(); @@ -508,8 +509,8 @@ public function testJsonTable(): void } $command->createTable('json_table', [ - 'id' => SchemaInterface::TYPE_PK, - 'json_col' => SchemaInterface::TYPE_JSON, + 'id' => PseudoType::PK, + 'json_col' => ColumnType::JSON, ])->execute(); $command->insert('json_table', ['id' => 1, 'json_col' => ['a' => 1, 'b' => 2]])->execute(); diff --git a/tests/Provider/ColumnFactoryProvider.php b/tests/Provider/ColumnFactoryProvider.php index c095a7e1..9904e2d1 100644 --- a/tests/Provider/ColumnFactoryProvider.php +++ b/tests/Provider/ColumnFactoryProvider.php @@ -4,6 +4,7 @@ namespace Yiisoft\Db\Sqlite\Tests\Provider; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Schema\Column\BinaryColumnSchema; use Yiisoft\Db\Schema\Column\BitColumnSchema; use Yiisoft\Db\Schema\Column\BooleanColumnSchema; @@ -18,35 +19,35 @@ public static function dbTypes(): array { return [ // db type, expected abstract type, expected instance of - ['bool', 'boolean', BooleanColumnSchema::class], - ['boolean', 'boolean', BooleanColumnSchema::class], - ['bit', 'bit', BitColumnSchema::class], - ['tinyint', 'tinyint', IntegerColumnSchema::class], - ['smallint', 'smallint', IntegerColumnSchema::class], - ['mediumint', 'integer', IntegerColumnSchema::class], - ['int', 'integer', IntegerColumnSchema::class], - ['integer', 'integer', IntegerColumnSchema::class], - ['bigint', 'bigint', IntegerColumnSchema::class], - ['float', 'float', DoubleColumnSchema::class], - ['real', 'float', DoubleColumnSchema::class], - ['double', 'double', DoubleColumnSchema::class], - ['decimal', 'decimal', DoubleColumnSchema::class], - ['numeric', 'decimal', DoubleColumnSchema::class], - ['char', 'char', StringColumnSchema::class], - ['varchar', 'string', StringColumnSchema::class], - ['string', 'string', StringColumnSchema::class], - ['enum', 'string', StringColumnSchema::class], - ['tinytext', 'text', StringColumnSchema::class], - ['mediumtext', 'text', StringColumnSchema::class], - ['longtext', 'text', StringColumnSchema::class], - ['text', 'text', StringColumnSchema::class], - ['blob', 'binary', BinaryColumnSchema::class], - ['year', 'date', StringColumnSchema::class], - ['date', 'date', StringColumnSchema::class], - ['time', 'time', StringColumnSchema::class], - ['datetime', 'datetime', StringColumnSchema::class], - ['timestamp', 'timestamp', StringColumnSchema::class], - ['json', 'json', JsonColumnSchema::class], + ['bool', ColumnType::BOOLEAN, BooleanColumnSchema::class], + ['boolean', ColumnType::BOOLEAN, BooleanColumnSchema::class], + ['bit', ColumnType::BIT, BitColumnSchema::class], + ['tinyint', ColumnType::TINYINT, IntegerColumnSchema::class], + ['smallint', ColumnType::SMALLINT, IntegerColumnSchema::class], + ['mediumint', ColumnType::INTEGER, IntegerColumnSchema::class], + ['int', ColumnType::INTEGER, IntegerColumnSchema::class], + ['integer', ColumnType::INTEGER, IntegerColumnSchema::class], + ['bigint', ColumnType::BIGINT, IntegerColumnSchema::class], + ['float', ColumnType::FLOAT, DoubleColumnSchema::class], + ['real', ColumnType::FLOAT, DoubleColumnSchema::class], + ['double', ColumnType::DOUBLE, DoubleColumnSchema::class], + ['decimal', ColumnType::DECIMAL, DoubleColumnSchema::class], + ['numeric', ColumnType::DECIMAL, DoubleColumnSchema::class], + ['char', ColumnType::CHAR, StringColumnSchema::class], + ['varchar', ColumnType::STRING, StringColumnSchema::class], + ['string', ColumnType::STRING, StringColumnSchema::class], + ['enum', ColumnType::STRING, StringColumnSchema::class], + ['tinytext', ColumnType::TEXT, StringColumnSchema::class], + ['mediumtext', ColumnType::TEXT, StringColumnSchema::class], + ['longtext', ColumnType::TEXT, StringColumnSchema::class], + ['text', ColumnType::TEXT, StringColumnSchema::class], + ['blob', ColumnType::BINARY, BinaryColumnSchema::class], + ['year', ColumnType::DATE, StringColumnSchema::class], + ['date', ColumnType::DATE, StringColumnSchema::class], + ['time', ColumnType::TIME, StringColumnSchema::class], + ['datetime', ColumnType::DATETIME, StringColumnSchema::class], + ['timestamp', ColumnType::TIMESTAMP, StringColumnSchema::class], + ['json', ColumnType::JSON, JsonColumnSchema::class], ]; } @@ -54,8 +55,8 @@ public static function definitions(): array { $definitions = parent::definitions(); - $definitions[] = ['bit(1)', 'boolean', BooleanColumnSchema::class, ['getSize' => 1]]; - $definitions[] = ['tinyint(1)', 'boolean', BooleanColumnSchema::class, ['getSize' => 1]]; + $definitions[] = ['bit(1)', ColumnType::BOOLEAN, BooleanColumnSchema::class, ['getDbType' => 'bit', 'getSize' => 1]]; + $definitions[] = ['tinyint(1)', ColumnType::BOOLEAN, BooleanColumnSchema::class, ['getDbType' => 'tinyint', 'getSize' => 1]]; return $definitions; } diff --git a/tests/Provider/ColumnSchemaBuilderProvider.php b/tests/Provider/ColumnSchemaBuilderProvider.php index cd05c57b..d92bf44c 100644 --- a/tests/Provider/ColumnSchemaBuilderProvider.php +++ b/tests/Provider/ColumnSchemaBuilderProvider.php @@ -4,7 +4,7 @@ namespace Yiisoft\Db\Sqlite\Tests\Provider; -use Yiisoft\Db\Schema\SchemaInterface; +use Yiisoft\Db\Constant\ColumnType; final class ColumnSchemaBuilderProvider extends \Yiisoft\Db\Tests\Provider\ColumnSchemaBuilderProvider { @@ -19,7 +19,7 @@ public static function types(): array return [ ...$types, - ['integer UNSIGNED', SchemaInterface::TYPE_INTEGER, null, [['unsigned']]], + ['integer UNSIGNED', ColumnType::INTEGER, null, [['unsigned']]], ]; } diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 44145585..3f65ed1d 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Sqlite\Tests; use JsonException; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidConfigException; @@ -15,7 +16,6 @@ use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryInterface; use Yiisoft\Db\QueryBuilder\Condition\JsonOverlapsCondition; -use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Sqlite\Column; use Yiisoft\Db\Sqlite\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonQueryBuilderTest; @@ -172,7 +172,7 @@ public function testAlterColumn(): void $this->expectException(NotSupportedException::class); $this->expectExceptionMessage('Yiisoft\Db\Sqlite\DDLQueryBuilder::alterColumn is not supported by SQLite.'); - $qb->alterColumn('customer', 'email', SchemaInterface::TYPE_STRING); + $qb->alterColumn('customer', 'email', ColumnType::STRING); } /** @@ -750,7 +750,7 @@ public function testUpsertExecute( public function testJsonColumn() { $qb = $this->getConnection()->getQueryBuilder(); - $columnSchemaBuilder = new Column(SchemaInterface::TYPE_JSON); + $columnSchemaBuilder = new Column(ColumnType::JSON); $this->assertSame( 'ALTER TABLE `json_table` ADD `json_col` json', From 58f47dae5cae751484d9f3ee9443a80c52d1c2e0 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sun, 8 Sep 2024 14:59:39 +0700 Subject: [PATCH 2/3] Realize `ColumnBuilder` --- src/Column/ColumnBuilder.php | 15 +++++++++++++++ src/Column/ColumnFactory.php | 5 +++++ src/Connection.php | 6 ++++++ src/Schema.php | 12 ++++-------- tests/ColumnBuilderTest.php | 23 +++++++++++++++++++++++ tests/ColumnFactoryTest.php | 20 +++++++++++++++++--- tests/ConnectionTest.php | 8 ++++++++ tests/SchemaTest.php | 9 --------- 8 files changed, 78 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 00000000..11dbfc34 --- /dev/null +++ b/src/Column/ColumnBuilder.php @@ -0,0 +1,15 @@ +queryBuilder; } + public function getColumnBuilderClass(): string + { + return ColumnBuilder::class; + } + public function getQuoter(): QuoterInterface { if ($this->quoter === null) { diff --git a/src/Schema.php b/src/Schema.php index aba71a6c..9766b39f 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Sqlite; use Throwable; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Constraint\CheckConstraint; use Yiisoft\Db\Constraint\Constraint; use Yiisoft\Db\Constraint\ForeignKeyConstraint; @@ -17,10 +18,8 @@ use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Helper\DbArrayHelper; use Yiisoft\Db\Schema\Builder\ColumnInterface; -use Yiisoft\Db\Schema\Column\ColumnFactoryInterface; use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; use Yiisoft\Db\Schema\TableSchemaInterface; -use Yiisoft\Db\Sqlite\Column\ColumnFactory; use function array_change_key_case; use function array_column; @@ -80,11 +79,6 @@ public function createColumn(string $type, array|int|string $length = null): Col return new Column($type, $length); } - public function getColumnFactory(): ColumnFactoryInterface - { - return new ColumnFactory(); - } - /** * Returns all table names in the database. * @@ -444,8 +438,10 @@ public function getSchemaDefaultValues(string $schema = '', bool $refresh = fals */ private function loadColumnSchema(array $info): ColumnSchemaInterface { + $columnFactory = $this->db->getColumnBuilderClass()::columnFactory(); + $dbType = strtolower($info['type']); - $column = $this->getColumnFactory()->fromDefinition($dbType, ['name' => $info['name']]); + $column = $columnFactory->fromDefinition($dbType, ['name' => $info['name']]); $column->dbType($dbType); $column->allowNull(!$info['notnull']); $column->primaryKey((bool) $info['pk']); diff --git a/tests/ColumnBuilderTest.php b/tests/ColumnBuilderTest.php new file mode 100644 index 00000000..6ded372b --- /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 a9b02608..6c6dfd83 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\Sqlite\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\Sqlite\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\Sqlite\Tests\Provider\ColumnFactoryProvider::types */ diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index c6472935..2695f8c3 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -14,6 +14,7 @@ use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Profiler\ProfilerInterface; +use Yiisoft\Db\Sqlite\Column\ColumnBuilder; use Yiisoft\Db\Sqlite\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonConnectionTest; use Yiisoft\Db\Transaction\TransactionInterface; @@ -181,6 +182,13 @@ private function runExceptionTest(ConnectionInterface $db): void $this->assertTrue($thrown, 'An exception should have been thrown by the command.'); } + public function testGetColumnBuilderClass(): void + { + $db = $this->getConnection(); + + $this->assertSame(ColumnBuilder::class, $db->getColumnBuilderClass()); + } + private function createProfiler(): ProfilerInterface { return $this->createMock(ProfilerInterface::class); diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 758383d2..2a171127 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\Sqlite\Column\ColumnFactory; use Yiisoft\Db\Sqlite\Schema; use Yiisoft\Db\Sqlite\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonSchemaTest; @@ -360,12 +359,4 @@ public function testNotConnectionPDO(): void $schema->refresh(); } - - public function testGetColumnFactory(): void - { - $db = $this->getConnection(); - $factory = $db->getSchema()->getColumnFactory(); - - $this->assertInstanceOf(ColumnFactory::class, $factory); - } } From 70fee2345329e32d458833d1beb17e9fee771747 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Mon, 16 Sep 2024 12:25:42 +0700 Subject: [PATCH 3/3] Remove `ColumnFactory` from `ColumnBuilder` --- CHANGELOG.md | 1 + src/Column/ColumnBuilder.php | 6 ------ src/Connection.php | 7 ++++--- src/Schema.php | 2 +- tests/ColumnBuilderTest.php | 11 +++++------ tests/ConnectionTest.php | 6 +++--- 6 files changed, 14 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69152211..cc3a3236 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Enh #315: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov) - Enh #314: Implement `ColumnFactory` class (@Tigrov) - Enh #317: Separate column type constants (@Tigrov) +- Enh #318: Realize `ColumnBuilder` class (@Tigrov) ## 1.2.0 March 21, 2024 diff --git a/src/Column/ColumnBuilder.php b/src/Column/ColumnBuilder.php index 11dbfc34..b0476207 100644 --- a/src/Column/ColumnBuilder.php +++ b/src/Column/ColumnBuilder.php @@ -4,12 +4,6 @@ namespace Yiisoft\Db\Sqlite\Column; -use Yiisoft\Db\Schema\Column\ColumnFactoryInterface; - final class ColumnBuilder extends \Yiisoft\Db\Schema\Column\ColumnBuilder { - public static function columnFactory(): ColumnFactoryInterface - { - return new ColumnFactory(); - } } diff --git a/src/Connection.php b/src/Connection.php index 888fa498..c1308d87 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -7,10 +7,11 @@ use Yiisoft\Db\Driver\Pdo\AbstractPdoConnection; use Yiisoft\Db\Driver\Pdo\PdoCommandInterface; use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; +use Yiisoft\Db\Schema\Column\ColumnFactoryInterface; use Yiisoft\Db\Schema\Quoter; use Yiisoft\Db\Schema\QuoterInterface; use Yiisoft\Db\Schema\SchemaInterface; -use Yiisoft\Db\Sqlite\Column\ColumnBuilder; +use Yiisoft\Db\Sqlite\Column\ColumnFactory; use Yiisoft\Db\Transaction\TransactionInterface; use function str_starts_with; @@ -71,9 +72,9 @@ public function getQueryBuilder(): QueryBuilderInterface return $this->queryBuilder; } - public function getColumnBuilderClass(): string + public function getColumnFactory(): ColumnFactoryInterface { - return ColumnBuilder::class; + return new ColumnFactory(); } public function getQuoter(): QuoterInterface diff --git a/src/Schema.php b/src/Schema.php index 9766b39f..67752a2a 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -438,7 +438,7 @@ public function getSchemaDefaultValues(string $schema = '', bool $refresh = fals */ private function loadColumnSchema(array $info): ColumnSchemaInterface { - $columnFactory = $this->db->getColumnBuilderClass()::columnFactory(); + $columnFactory = $this->db->getColumnFactory(); $dbType = strtolower($info['type']); $column = $columnFactory->fromDefinition($dbType, ['name' => $info['name']]); diff --git a/tests/ColumnBuilderTest.php b/tests/ColumnBuilderTest.php index 6ded372b..61f45283 100644 --- a/tests/ColumnBuilderTest.php +++ b/tests/ColumnBuilderTest.php @@ -2,7 +2,9 @@ declare(strict_types=1); -use Yiisoft\Db\Sqlite\Column\ColumnFactory; +namespace Yiisoft\Db\Sqlite\Tests; + +use Yiisoft\Db\Sqlite\Column\ColumnBuilder; use Yiisoft\Db\Sqlite\Tests\Support\TestTrait; use Yiisoft\Db\Tests\AbstractColumnBuilderTest; @@ -13,11 +15,8 @@ class ColumnBuilderTest extends AbstractColumnBuilderTest { use TestTrait; - public function testColumnFactory(): void + public function getColumnBuilderClass(): string { - $db = $this->getConnection(); - $columnBuilderClass = $db->getColumnBuilderClass(); - - $this->assertInstanceOf(ColumnFactory::class, $columnBuilderClass::columnFactory()); + return ColumnBuilder::class; } } diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 2695f8c3..83371fc2 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -14,7 +14,7 @@ use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Profiler\ProfilerInterface; -use Yiisoft\Db\Sqlite\Column\ColumnBuilder; +use Yiisoft\Db\Sqlite\Column\ColumnFactory; use Yiisoft\Db\Sqlite\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonConnectionTest; use Yiisoft\Db\Transaction\TransactionInterface; @@ -182,11 +182,11 @@ private function runExceptionTest(ConnectionInterface $db): void $this->assertTrue($thrown, 'An exception should have been thrown by the command.'); } - public function testGetColumnBuilderClass(): void + public function testGetColumnFactory(): void { $db = $this->getConnection(); - $this->assertSame(ColumnBuilder::class, $db->getColumnBuilderClass()); + $this->assertInstanceOf(ColumnFactory::class, $db->getColumnFactory()); } private function createProfiler(): ProfilerInterface