Skip to content

Implement the ability for user-defined type casting #346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 17, 2025
Merged
2 changes: 1 addition & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function createTransaction(): Transaction

public function getColumnFactory(): ColumnFactoryInterface
{
return new ColumnFactory();
return $this->columnFactory ??= new ColumnFactory();
}

public function getQueryBuilder(): QueryBuilderInterface
Expand Down
6 changes: 6 additions & 0 deletions tests/ColumnFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\Attributes\DataProviderExternal;
use Yiisoft\Db\Schema\Column\ColumnInterface;
use Yiisoft\Db\Sqlite\Column\ColumnFactory;
use Yiisoft\Db\Sqlite\Tests\Provider\ColumnFactoryProvider;
use Yiisoft\Db\Sqlite\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\AbstractColumnFactoryTest;
Expand All @@ -17,6 +18,11 @@ final class ColumnFactoryTest extends AbstractColumnFactoryTest
{
use TestTrait;

protected function getColumnFactoryClass(): string
{
return ColumnFactory::class;
}

#[DataProviderExternal(ColumnFactoryProvider::class, 'dbTypes')]
public function testFromDbType(string $dbType, string $expectedType, string $expectedInstanceOf): void
{
Expand Down
15 changes: 15 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Profiler\ProfilerInterface;
use Yiisoft\Db\Sqlite\Column\ColumnFactory;
use Yiisoft\Db\Sqlite\Connection;
use Yiisoft\Db\Sqlite\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Common\CommonConnectionTest;
use Yiisoft\Db\Tests\Support\DbHelper;
use Yiisoft\Db\Transaction\TransactionInterface;

/**
Expand Down Expand Up @@ -192,5 +194,18 @@ public function testGetColumnFactory(): void
$db = $this->getConnection();

$this->assertInstanceOf(ColumnFactory::class, $db->getColumnFactory());

$db->close();
}

public function testUserDefinedColumnFactory(): void
{
$columnFactory = new ColumnFactory();

$db = new Connection($this->getDriver(), DbHelper::getSchemaCache(), $columnFactory);

$this->assertSame($columnFactory, $db->getColumnFactory());

$db->close();
}
}
8 changes: 7 additions & 1 deletion tests/Support/TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\Sqlite\Tests\Support;

use Yiisoft\Db\Driver\Pdo\PdoDriverInterface;
use Yiisoft\Db\Sqlite\Connection;
use Yiisoft\Db\Sqlite\Driver;
use Yiisoft\Db\Sqlite\Dsn;
Expand All @@ -15,7 +16,7 @@ trait TestTrait

protected function getConnection(bool $fixture = false): Connection
{
$db = new Connection(new Driver($this->getDsn()), DbHelper::getSchemaCache());
$db = new Connection($this->getDriver(), DbHelper::getSchemaCache());

if ($fixture) {
DbHelper::loadFixture($db, __DIR__ . '/Fixture/sqlite.sql');
Expand All @@ -33,6 +34,11 @@ protected function getDsn(): string
return $this->dsn;
}

protected function getDriver(): PdoDriverInterface
{
return new Driver($this->getDsn());
}

protected function getDriverName(): string
{
return 'sqlite';
Expand Down
Loading