Skip to content

Commit e7d2f0b

Browse files
authored
Realize ColumnBuilder (#318)
1 parent 039e09c commit e7d2f0b

9 files changed

Lines changed: 72 additions & 20 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Enh #315: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov)
1212
- Enh #314: Implement `ColumnFactory` class (@Tigrov)
1313
- Enh #317: Separate column type constants (@Tigrov)
14+
- Enh #318: Realize `ColumnBuilder` class (@Tigrov)
1415

1516
## 1.2.0 March 21, 2024
1617

src/Column/ColumnBuilder.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Db\Sqlite\Column;
6+
7+
final class ColumnBuilder extends \Yiisoft\Db\Schema\Column\ColumnBuilder
8+
{
9+
}

src/Column/ColumnFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,9 @@ protected function getType(string $dbType, array $info = []): string
6262

6363
return $type;
6464
}
65+
66+
protected function isDbType(string $dbType): bool
67+
{
68+
return isset(self::TYPE_MAP[$dbType]);
69+
}
6570
}

src/Connection.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
use Yiisoft\Db\Driver\Pdo\AbstractPdoConnection;
88
use Yiisoft\Db\Driver\Pdo\PdoCommandInterface;
99
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
10+
use Yiisoft\Db\Schema\Column\ColumnFactoryInterface;
1011
use Yiisoft\Db\Schema\Quoter;
1112
use Yiisoft\Db\Schema\QuoterInterface;
1213
use Yiisoft\Db\Schema\SchemaInterface;
14+
use Yiisoft\Db\Sqlite\Column\ColumnFactory;
1315
use Yiisoft\Db\Transaction\TransactionInterface;
1416

1517
use function str_starts_with;
@@ -70,6 +72,11 @@ public function getQueryBuilder(): QueryBuilderInterface
7072
return $this->queryBuilder;
7173
}
7274

75+
public function getColumnFactory(): ColumnFactoryInterface
76+
{
77+
return new ColumnFactory();
78+
}
79+
7380
public function getQuoter(): QuoterInterface
7481
{
7582
if ($this->quoter === null) {

src/Schema.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
use Yiisoft\Db\Expression\Expression;
1919
use Yiisoft\Db\Helper\DbArrayHelper;
2020
use Yiisoft\Db\Schema\Builder\ColumnInterface;
21-
use Yiisoft\Db\Schema\Column\ColumnFactoryInterface;
2221
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
2322
use Yiisoft\Db\Schema\TableSchemaInterface;
24-
use Yiisoft\Db\Sqlite\Column\ColumnFactory;
2523

2624
use function array_change_key_case;
2725
use function array_column;
@@ -81,11 +79,6 @@ public function createColumn(string $type, array|int|string $length = null): Col
8179
return new Column($type, $length);
8280
}
8381

84-
public function getColumnFactory(): ColumnFactoryInterface
85-
{
86-
return new ColumnFactory();
87-
}
88-
8982
/**
9083
* Returns all table names in the database.
9184
*
@@ -445,8 +438,10 @@ public function getSchemaDefaultValues(string $schema = '', bool $refresh = fals
445438
*/
446439
private function loadColumnSchema(array $info): ColumnSchemaInterface
447440
{
441+
$columnFactory = $this->db->getColumnFactory();
442+
448443
$dbType = strtolower($info['type']);
449-
$column = $this->getColumnFactory()->fromDefinition($dbType, ['name' => $info['name']]);
444+
$column = $columnFactory->fromDefinition($dbType, ['name' => $info['name']]);
450445
$column->dbType($dbType);
451446
$column->allowNull(!$info['notnull']);
452447
$column->primaryKey((bool) $info['pk']);

tests/ColumnBuilderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Db\Sqlite\Tests;
6+
7+
use Yiisoft\Db\Sqlite\Column\ColumnBuilder;
8+
use Yiisoft\Db\Sqlite\Tests\Support\TestTrait;
9+
use Yiisoft\Db\Tests\AbstractColumnBuilderTest;
10+
11+
/**
12+
* @group sqlite
13+
*/
14+
class ColumnBuilderTest extends AbstractColumnBuilderTest
15+
{
16+
use TestTrait;
17+
18+
public function getColumnBuilderClass(): string
19+
{
20+
return ColumnBuilder::class;
21+
}
22+
}

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\Sqlite\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\Sqlite\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\Sqlite\Tests\Provider\ColumnFactoryProvider::types */

tests/ConnectionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Yiisoft\Db\Exception\InvalidConfigException;
1515
use Yiisoft\Db\Exception\NotSupportedException;
1616
use Yiisoft\Db\Profiler\ProfilerInterface;
17+
use Yiisoft\Db\Sqlite\Column\ColumnFactory;
1718
use Yiisoft\Db\Sqlite\Tests\Support\TestTrait;
1819
use Yiisoft\Db\Tests\Common\CommonConnectionTest;
1920
use Yiisoft\Db\Transaction\TransactionInterface;
@@ -181,6 +182,13 @@ private function runExceptionTest(ConnectionInterface $db): void
181182
$this->assertTrue($thrown, 'An exception should have been thrown by the command.');
182183
}
183184

185+
public function testGetColumnFactory(): void
186+
{
187+
$db = $this->getConnection();
188+
189+
$this->assertInstanceOf(ColumnFactory::class, $db->getColumnFactory());
190+
}
191+
184192
private function createProfiler(): ProfilerInterface
185193
{
186194
return $this->createMock(ProfilerInterface::class);

tests/SchemaTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Yiisoft\Db\Exception\Exception;
1212
use Yiisoft\Db\Exception\InvalidConfigException;
1313
use Yiisoft\Db\Exception\NotSupportedException;
14-
use Yiisoft\Db\Sqlite\Column\ColumnFactory;
1514
use Yiisoft\Db\Sqlite\Schema;
1615
use Yiisoft\Db\Sqlite\Tests\Support\TestTrait;
1716
use Yiisoft\Db\Tests\Common\CommonSchemaTest;
@@ -360,12 +359,4 @@ public function testNotConnectionPDO(): void
360359

361360
$schema->refresh();
362361
}
363-
364-
public function testGetColumnFactory(): void
365-
{
366-
$db = $this->getConnection();
367-
$factory = $db->getSchema()->getColumnFactory();
368-
369-
$this->assertInstanceOf(ColumnFactory::class, $factory);
370-
}
371362
}

0 commit comments

Comments
 (0)