Skip to content

Commit

Permalink
Realize ColumnBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Sep 8, 2024
1 parent 9fee262 commit 58f47da
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 20 deletions.
15 changes: 15 additions & 0 deletions src/Column/ColumnBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

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();
}
}
5 changes: 5 additions & 0 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ protected function getType(string $dbType, array $info = []): string

return $type;
}

protected function isDbType(string $dbType): bool
{
return isset(self::TYPE_MAP[$dbType]);
}
}
6 changes: 6 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Yiisoft\Db\Schema\Quoter;
use Yiisoft\Db\Schema\QuoterInterface;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Sqlite\Column\ColumnBuilder;
use Yiisoft\Db\Transaction\TransactionInterface;

use function str_starts_with;
Expand Down Expand Up @@ -70,6 +71,11 @@ public function getQueryBuilder(): QueryBuilderInterface
return $this->queryBuilder;
}

public function getColumnBuilderClass(): string
{
return ColumnBuilder::class;
}

public function getQuoter(): QuoterInterface
{
if ($this->quoter === null) {
Expand Down
12 changes: 4 additions & 8 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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']);
Expand Down
23 changes: 23 additions & 0 deletions tests/ColumnBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Yiisoft\Db\Sqlite\Column\ColumnFactory;
use Yiisoft\Db\Sqlite\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\AbstractColumnBuilderTest;

/**
* @group sqlite
*/
class ColumnBuilderTest extends AbstractColumnBuilderTest
{
use TestTrait;

public function testColumnFactory(): void
{
$db = $this->getConnection();
$columnBuilderClass = $db->getColumnBuilderClass();

$this->assertInstanceOf(ColumnFactory::class, $columnBuilderClass::columnFactory());
}
}
20 changes: 17 additions & 3 deletions tests/ColumnFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
8 changes: 8 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 0 additions & 9 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 58f47da

Please sign in to comment.