Skip to content

Commit

Permalink
Realize ColumnBuilder (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Sep 16, 2024
1 parent d1b78b4 commit 30513b4
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Enh #347, #353: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov)
- Bug #349, #352: Restore connection if closed by connection timeout (@Tigrov)
- Enh #354: Separate column type constants (@Tigrov)
- Enh #355: Realize `ColumnBuilder` class (@Tigrov)

## 1.2.0 March 21, 2024

Expand Down
9 changes: 9 additions & 0 deletions src/Column/ColumnBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mysql\Column;

final class ColumnBuilder extends \Yiisoft\Db\Schema\Column\ColumnBuilder
{
}
20 changes: 5 additions & 15 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Schema\Column\AbstractColumnFactory;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;

final class ColumnFactory extends AbstractColumnFactory
{
Expand Down Expand Up @@ -49,20 +48,6 @@ final class ColumnFactory extends AbstractColumnFactory
'json' => ColumnType::JSON,
];

public function fromDefinition(string $definition, array $info = []): ColumnSchemaInterface
{
if (str_starts_with($definition, 'enum(')) {
preg_match('/^enum\(([^)]+)\)\s*/', $definition, $matches);
preg_match_all("/'([^']*)'/", $matches[1], $values);

$info['enum_values'] = $values[1];

return $this->fromDbType('enum', $info);
}

return parent::fromDefinition($definition, $info);
}

protected function getType(string $dbType, array $info = []): string
{
$type = self::TYPE_MAP[$dbType] ?? ColumnType::STRING;
Expand All @@ -73,4 +58,9 @@ protected function getType(string $dbType, array $info = []): string

return $type;
}

protected function isDbType(string $dbType): bool
{
return isset(self::TYPE_MAP[$dbType]);
}
}
7 changes: 7 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use Throwable;
use Yiisoft\Db\Driver\Pdo\AbstractPdoConnection;
use Yiisoft\Db\Driver\Pdo\PdoCommandInterface;
use Yiisoft\Db\Mysql\Column\ColumnFactory;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
use Yiisoft\Db\Schema\Column\ColumnFactoryInterface;
use Yiisoft\Db\Schema\QuoterInterface;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Transaction\TransactionInterface;
Expand Down Expand Up @@ -63,6 +65,11 @@ public function createTransaction(): TransactionInterface
return new Transaction($this);
}

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

public function getQueryBuilder(): QueryBuilderInterface
{
if ($this->queryBuilder === null) {
Expand Down
11 changes: 3 additions & 8 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Helper\DbArrayHelper;
use Yiisoft\Db\Mysql\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 @@ -83,11 +81,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 unique indexes for the given table.
*
Expand Down Expand Up @@ -418,9 +411,11 @@ protected function getCreateTableSql(TableSchemaInterface $table): string
*/
private function loadColumnSchema(array $info): ColumnSchemaInterface
{
$columnFactory = $this->db->getColumnFactory();

$dbType = $info['type'];
/** @psalm-var ColumnInfoArray $info */
$column = $this->getColumnFactory()->fromDefinition($dbType);
$column = $columnFactory->fromDefinition($dbType);
$column->name($info['field']);
$column->allowNull($info['null'] === 'YES');
$column->primaryKey(str_contains($info['key'], 'PRI'));
Expand Down
22 changes: 22 additions & 0 deletions tests/ColumnBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mysql\Tests;

use Yiisoft\Db\Mysql\Column\ColumnBuilder;
use Yiisoft\Db\Mysql\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\AbstractColumnBuilderTest;

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

public function getColumnBuilderClass(): string
{
return ColumnBuilder::class;
}
}
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\Mysql\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\Mysql\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\Mysql\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\IntegrityException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Mysql\Column\ColumnFactory;
use Yiisoft\Db\Mysql\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Common\CommonConnectionTest;
use Yiisoft\Db\Transaction\TransactionInterface;
Expand Down Expand Up @@ -156,4 +157,11 @@ public function testNotRestartConnectionOnTimeoutInTransaction(): void

$db->createCommand('SELECT 1')->queryScalar();
}

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

$this->assertInstanceOf(ColumnFactory::class, $db->getColumnFactory());
}
}
2 changes: 1 addition & 1 deletion tests/Provider/ColumnFactoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static function definitions(): array
{
$definitions = parent::definitions();

$definitions[] = ['bit(1)', ColumnType::BOOLEAN, BooleanColumnSchema::class, ['getSize' => 1]];
$definitions[] = ['bit(1)', ColumnType::BOOLEAN, BooleanColumnSchema::class, ['getDbType' => 'bit', 'getSize' => 1]];

return $definitions;
}
Expand Down
9 changes: 0 additions & 9 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Mysql\Column;
use Yiisoft\Db\Mysql\Column\ColumnFactory;
use Yiisoft\Db\Mysql\Schema;
use Yiisoft\Db\Mysql\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
Expand Down Expand Up @@ -566,12 +565,4 @@ public function testInsertDefaultValues()
'numeric_col' => '-33.22',
], $row);
}

public function testGetColumnFactory(): void
{
$db = $this->getConnection();
$factory = $db->getSchema()->getColumnFactory();

$this->assertInstanceOf(ColumnFactory::class, $factory);
}
}

0 comments on commit 30513b4

Please sign in to comment.