From 58f47dae5cae751484d9f3ee9443a80c52d1c2e0 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sun, 8 Sep 2024 14:59:39 +0700 Subject: [PATCH] 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); - } }