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 d384728 commit 21f5d1e
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 21 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\Oracle\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 @@ -78,4 +78,9 @@ public function fromType(string $type, array $info = []): ColumnSchemaInterface

return parent::fromType($type, $info);
}

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 @@ -11,6 +11,7 @@
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidCallException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Oracle\Column\ColumnBuilder;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
use Yiisoft\Db\Schema\QuoterInterface;
use Yiisoft\Db\Schema\SchemaInterface;
Expand Down Expand Up @@ -47,6 +48,11 @@ public function createTransaction(): TransactionInterface
return new Transaction($this);
}

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

/**
* Override base behaviour to support Oracle sequences.
*
Expand Down
12 changes: 3 additions & 9 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Helper\DbArrayHelper;
use Yiisoft\Db\Oracle\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;

Expand Down Expand Up @@ -79,11 +77,6 @@ public function createColumn(string $type, array|int|string $length = null): Col
return new Column($type, $length);
}

public function getColumnFactory(): ColumnFactoryInterface
{
return new ColumnFactory();
}

protected function resolveTableName(string $name): TableSchemaInterface
{
$resolvedName = new TableSchema();
Expand Down Expand Up @@ -431,8 +424,10 @@ protected function getTableSequenceName(string $tableName): string|null
*/
private function loadColumnSchema(array $info): ColumnSchemaInterface
{
$columnFactory = $this->db->getColumnBuilderClass()::columnFactory();

$dbType = $info['data_type'];
$column = $this->getColumnFactory()->fromDbType($dbType, [
$column = $columnFactory->fromDbType($dbType, [
'scale' => $info['data_scale'],
'precision' => $info['data_precision'],
]);
Expand All @@ -442,7 +437,6 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface
$column->primaryKey((bool) $info['is_pk']);
$column->autoIncrement($info['identity_column'] === 'YES');
$column->size((int) $info['data_length']);
$column->dbType($dbType);
$column->defaultValue($this->normalizeDefaultValue($info['data_default'], $column));

return $column;
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\Oracle\Column\ColumnFactory;
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\AbstractColumnBuilderTest;

/**
* @group oracle
*/
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\Oracle\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\Oracle\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\Oracle\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 @@ -11,6 +11,7 @@
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Oracle\Column\ColumnBuilder;
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Common\CommonConnectionTest;
use Yiisoft\Db\Transaction\TransactionInterface;
Expand Down Expand Up @@ -130,4 +131,11 @@ public function testSerialized(): void
$this->assertEquals(123, $unserialized->createCommand('SELECT 123 FROM DUAL')->queryScalar());
$this->assertNotNull($connection->getPDO());
}

public function testGetColumnBuilderClass(): void
{
$db = $this->getConnection();

$this->assertSame(ColumnBuilder::class, $db->getColumnBuilderClass());
}
}
3 changes: 3 additions & 0 deletions tests/Provider/ColumnFactoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ public static function definitions(): array
$definitions = parent::definitions();

$definitions['text'][0] = 'clob';
$definitions['text'][3]['getDbType'] = 'clob';
$definitions['text NOT NULL'][0] = 'clob NOT NULL';
$definitions['text NOT NULL'][3]['getDbType'] = 'clob';
$definitions['decimal(10,2)'][0] = 'number(10,2)';
$definitions['decimal(10,2)'][3]['getDbType'] = 'number';

unset($definitions['bigint UNSIGNED']);

Expand Down
9 changes: 0 additions & 9 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Oracle\Column\ColumnFactory;
use Yiisoft\Db\Oracle\Schema;
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Common\CommonSchemaTest;
Expand Down Expand Up @@ -296,12 +295,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 21f5d1e

Please sign in to comment.