Skip to content

Commit 1b2b7ba

Browse files
authored
Realize ColumnBuilder (#280)
1 parent 6ecdb38 commit 1b2b7ba

File tree

11 files changed

+117
-21
lines changed

11 files changed

+117
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- Enh #277: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov)
1414
- Enh #276: Implement `ColumnFactory` class (@Tigrov)
1515
- Enh #279: Separate column type constants (@Tigrov)
16+
- Enh #280: Realize `ColumnBuilder` class (@Tigrov)
1617

1718
## 1.3.0 March 21, 2024
1819

src/Column/ColumnBuilder.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Db\Oracle\Column;
6+
7+
use Yiisoft\Db\Constant\ColumnType;
8+
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
9+
10+
final class ColumnBuilder extends \Yiisoft\Db\Schema\Column\ColumnBuilder
11+
{
12+
public static function binary(int|null $size = null): ColumnSchemaInterface
13+
{
14+
return (new BinaryColumnSchema(ColumnType::BINARY))
15+
->size($size);
16+
}
17+
}

src/Column/ColumnFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,9 @@ public function fromType(string $type, array $info = []): ColumnSchemaInterface
7878

7979
return parent::fromType($type, $info);
8080
}
81+
82+
protected function isDbType(string $dbType): bool
83+
{
84+
return isset(self::TYPE_MAP[$dbType]);
85+
}
8186
}

src/Connection.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use Yiisoft\Db\Exception\InvalidArgumentException;
1212
use Yiisoft\Db\Exception\InvalidCallException;
1313
use Yiisoft\Db\Exception\InvalidConfigException;
14+
use Yiisoft\Db\Oracle\Column\ColumnFactory;
1415
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
16+
use Yiisoft\Db\Schema\Column\ColumnFactoryInterface;
1517
use Yiisoft\Db\Schema\QuoterInterface;
1618
use Yiisoft\Db\Schema\SchemaInterface;
1719
use Yiisoft\Db\Transaction\TransactionInterface;
@@ -47,6 +49,11 @@ public function createTransaction(): TransactionInterface
4749
return new Transaction($this);
4850
}
4951

52+
public function getColumnFactory(): ColumnFactoryInterface
53+
{
54+
return new ColumnFactory();
55+
}
56+
5057
/**
5158
* Override base behaviour to support Oracle sequences.
5259
*

src/Schema.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
use Yiisoft\Db\Exception\NotSupportedException;
1919
use Yiisoft\Db\Expression\Expression;
2020
use Yiisoft\Db\Helper\DbArrayHelper;
21-
use Yiisoft\Db\Oracle\Column\ColumnFactory;
2221
use Yiisoft\Db\Schema\Builder\ColumnInterface;
23-
use Yiisoft\Db\Schema\Column\ColumnFactoryInterface;
2422
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
2523
use Yiisoft\Db\Schema\TableSchemaInterface;
2624

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

82-
public function getColumnFactory(): ColumnFactoryInterface
83-
{
84-
return new ColumnFactory();
85-
}
86-
8780
protected function resolveTableName(string $name): TableSchemaInterface
8881
{
8982
$resolvedName = new TableSchema();
@@ -431,8 +424,10 @@ protected function getTableSequenceName(string $tableName): string|null
431424
*/
432425
private function loadColumnSchema(array $info): ColumnSchemaInterface
433426
{
427+
$columnFactory = $this->db->getColumnFactory();
428+
434429
$dbType = $info['data_type'];
435-
$column = $this->getColumnFactory()->fromDbType($dbType, [
430+
$column = $columnFactory->fromDbType($dbType, [
436431
'scale' => $info['data_scale'],
437432
'precision' => $info['data_precision'],
438433
]);
@@ -442,7 +437,6 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface
442437
$column->primaryKey((bool) $info['is_pk']);
443438
$column->autoIncrement($info['identity_column'] === 'YES');
444439
$column->size((int) $info['data_length']);
445-
$column->dbType($dbType);
446440
$column->defaultValue($this->normalizeDefaultValue($info['data_default'], $column));
447441

448442
return $column;

tests/ColumnBuilderTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Db\Oracle\Tests;
6+
7+
use Yiisoft\Db\Oracle\Column\ColumnBuilder;
8+
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
9+
use Yiisoft\Db\Tests\AbstractColumnBuilderTest;
10+
11+
/**
12+
* @group oracle
13+
*/
14+
class ColumnBuilderTest extends AbstractColumnBuilderTest
15+
{
16+
use TestTrait;
17+
18+
public function getColumnBuilderClass(): string
19+
{
20+
return ColumnBuilder::class;
21+
}
22+
23+
/**
24+
* @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\ColumnBuilderProvider::buildingMethods
25+
*/
26+
public function testBuildingMethods(
27+
string $buildingMethod,
28+
array $args,
29+
string $expectedInstanceOf,
30+
string $expectedType,
31+
array $expectedMethodResults = [],
32+
): void {
33+
parent::testBuildingMethods($buildingMethod, $args, $expectedInstanceOf, $expectedType, $expectedMethodResults);
34+
}
35+
}

tests/ColumnFactoryTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,23 @@ public function testFromDbType(string $dbType, string $expectedType, string $exp
2121
}
2222

2323
/** @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\ColumnFactoryProvider::definitions */
24-
public function testFromDefinition(string $definition, string $expectedType, string $expectedInstanceOf, array $expectedInfo = []): void
25-
{
26-
parent::testFromDefinition($definition, $expectedType, $expectedInstanceOf, $expectedInfo);
24+
public function testFromDefinition(
25+
string $definition,
26+
string $expectedType,
27+
string $expectedInstanceOf,
28+
array $expectedMethodResults = []
29+
): void {
30+
parent::testFromDefinition($definition, $expectedType, $expectedInstanceOf, $expectedMethodResults);
31+
}
32+
33+
/** @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\ColumnFactoryProvider::pseudoTypes */
34+
public function testFromPseudoType(
35+
string $pseudoType,
36+
string $expectedType,
37+
string $expectedInstanceOf,
38+
array $expectedMethodResults = []
39+
): void {
40+
parent::testFromPseudoType($pseudoType, $expectedType, $expectedInstanceOf, $expectedMethodResults);
2741
}
2842

2943
/** @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\ColumnFactoryProvider::types */

tests/ConnectionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Yiisoft\Db\Exception\Exception;
1212
use Yiisoft\Db\Exception\InvalidConfigException;
1313
use Yiisoft\Db\Exception\NotSupportedException;
14+
use Yiisoft\Db\Oracle\Column\ColumnFactory;
1415
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
1516
use Yiisoft\Db\Tests\Common\CommonConnectionTest;
1617
use Yiisoft\Db\Transaction\TransactionInterface;
@@ -130,4 +131,11 @@ public function testSerialized(): void
130131
$this->assertEquals(123, $unserialized->createCommand('SELECT 123 FROM DUAL')->queryScalar());
131132
$this->assertNotNull($connection->getPDO());
132133
}
134+
135+
public function testGetColumnFactory(): void
136+
{
137+
$db = $this->getConnection();
138+
139+
$this->assertInstanceOf(ColumnFactory::class, $db->getColumnFactory());
140+
}
133141
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Db\Oracle\Tests\Provider;
6+
7+
use Yiisoft\Db\Constant\ColumnType;
8+
use Yiisoft\Db\Oracle\Column\BinaryColumnSchema;
9+
10+
class ColumnBuilderProvider extends \Yiisoft\Db\Tests\Provider\ColumnBuilderProvider
11+
{
12+
public static function buildingMethods(): array
13+
{
14+
return [
15+
// building method, args, expected instance of, expected type, expected column method results
16+
...parent::buildingMethods(),
17+
['binary', [], BinaryColumnSchema::class, ColumnType::BINARY],
18+
['binary', [8], BinaryColumnSchema::class, ColumnType::BINARY, ['getSize' => 8]],
19+
];
20+
}
21+
}

tests/Provider/ColumnFactoryProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ public static function definitions(): array
4343
$definitions = parent::definitions();
4444

4545
$definitions['text'][0] = 'clob';
46+
$definitions['text'][3]['getDbType'] = 'clob';
4647
$definitions['text NOT NULL'][0] = 'clob NOT NULL';
48+
$definitions['text NOT NULL'][3]['getDbType'] = 'clob';
4749
$definitions['decimal(10,2)'][0] = 'number(10,2)';
50+
$definitions['decimal(10,2)'][3]['getDbType'] = 'number';
4851

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

0 commit comments

Comments
 (0)