Skip to content

Commit

Permalink
Remove ColumnFactory from ColumnBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Sep 16, 2024
1 parent 66a3ab1 commit 926c925
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 77 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
- Enh #864: Realize column factory (@Tigrov)
- Enh #875: Ignore "Packets out of order..." warnings in `AbstractPdoCommand::internalExecute()` method (@Tigrov)
- Enh #877: Separate column type constants (@Tigrov)
- Enh #878: Realize `ColumnBuilder` class (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
2 changes: 1 addition & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Each table column has its own class in the `Yiisoft\Db\Schema\Column` namespace
### New methods

- `QuoterInterface::getRawTableName()` - returns the raw table name without quotes;
- `SchemaInterface::getColumnFactory()` - returns the column factory.
- `ConnectionInterface::getColumnFactory()` - returns the column factory object for concrete DBMS.

### Remove methods

Expand Down
8 changes: 3 additions & 5 deletions src/Connection/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Yiisoft\Db\Query\BatchQueryResultInterface;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
use Yiisoft\Db\Schema\Column\ColumnBuilder;
use Yiisoft\Db\Schema\Column\ColumnFactoryInterface;
use Yiisoft\Db\Schema\QuoterInterface;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Schema\TableSchemaInterface;
Expand Down Expand Up @@ -87,11 +87,9 @@ public function createTransaction(): TransactionInterface;
public function close(): void;

/**
* Returns the column builder class name for creating column instances.
*
* @psalm-return class-string<ColumnBuilder>
* Returns the column factory for creating column instances.
*/
public function getColumnBuilderClass(): string;
public function getColumnFactory(): ColumnFactoryInterface;

/**
* Returns the name of the DB driver for the current `dsn`.
Expand Down
5 changes: 3 additions & 2 deletions src/Debug/ConnectionInterfaceProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Yiisoft\Db\Query\BatchQueryResultInterface;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
use Yiisoft\Db\Schema\Column\ColumnFactoryInterface;
use Yiisoft\Db\Schema\QuoterInterface;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Schema\TableSchemaInterface;
Expand Down Expand Up @@ -62,9 +63,9 @@ public function close(): void
$this->connection->close();
}

public function getColumnBuilderClass(): string
public function getColumnFactory(): ColumnFactoryInterface
{
return $this->connection->getColumnBuilderClass();
return $this->connection->getColumnFactory();
}

public function getLastInsertID(string $sequenceName = null): string
Expand Down
78 changes: 31 additions & 47 deletions src/Schema/Column/ColumnBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@
*/
class ColumnBuilder
{
// Pseudo-type column builders
/**
* Creates a new column factory instance to build columns.
* Builds a column as an `integer` primary key.
*/
public static function columnFactory(): ColumnFactoryInterface
{
return new ColumnFactory();
}

// Pseudo-type column builders
public static function primaryKey(bool $autoIncrement = true): ColumnSchemaInterface
{
return static::integer()
Expand All @@ -30,6 +25,9 @@ public static function primaryKey(bool $autoIncrement = true): ColumnSchemaInter
->allowNull(false);
}

/**
* Builds a column as a `smallint` primary key.
*/
public static function smallPrimaryKey(bool $autoIncrement = true): ColumnSchemaInterface
{
return static::smallint()
Expand All @@ -38,6 +36,9 @@ public static function smallPrimaryKey(bool $autoIncrement = true): ColumnSchema
->allowNull(false);
}

/**
* Builds a column as a `bigint` primary key.
*/
public static function bigPrimaryKey(bool $autoIncrement = true): ColumnSchemaInterface
{
return static::bigint()
Expand All @@ -46,6 +47,9 @@ public static function bigPrimaryKey(bool $autoIncrement = true): ColumnSchemaIn
->allowNull(false);
}

/**
* Builds a column as an `uuid` primary key.
*/
public static function uuidPrimaryKey(bool $autoIncrement = false): ColumnSchemaInterface
{
return static::uuid()
Expand All @@ -60,17 +64,15 @@ public static function uuidPrimaryKey(bool $autoIncrement = false): ColumnSchema
*/
public static function boolean(): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::BOOLEAN);
return (new BooleanColumnSchema(ColumnType::BOOLEAN));
}

/**
* Builds a column with the abstract type `bit`.
*/
public static function bit(int|null $size = null): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::BIT)
return (new BitColumnSchema(ColumnType::BIT))
->size($size);
}

Expand All @@ -79,8 +81,7 @@ public static function bit(int|null $size = null): ColumnSchemaInterface
*/
public static function tinyint(int|null $size = null): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::TINYINT)
return (new IntegerColumnSchema(ColumnType::TINYINT))
->size($size);
}

Expand All @@ -89,8 +90,7 @@ public static function tinyint(int|null $size = null): ColumnSchemaInterface
*/
public static function smallint(int|null $size = null): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::SMALLINT)
return (new IntegerColumnSchema(ColumnType::SMALLINT))
->size($size);
}

Expand All @@ -99,8 +99,7 @@ public static function smallint(int|null $size = null): ColumnSchemaInterface
*/
public static function integer(int|null $size = null): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::INTEGER)
return (new IntegerColumnSchema(ColumnType::INTEGER))
->size($size);
}

Expand All @@ -109,8 +108,7 @@ public static function integer(int|null $size = null): ColumnSchemaInterface
*/
public static function bigint(int|null $size = null): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::BIGINT)
return (new IntegerColumnSchema(ColumnType::BIGINT))
->size($size);
}

Expand All @@ -119,8 +117,7 @@ public static function bigint(int|null $size = null): ColumnSchemaInterface
*/
public static function float(int|null $size = null, int|null $scale = null): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::FLOAT)
return (new DoubleColumnSchema(ColumnType::FLOAT))
->size($size)
->scale($scale);
}
Expand All @@ -130,8 +127,7 @@ public static function float(int|null $size = null, int|null $scale = null): Col
*/
public static function double(int|null $size = null, int|null $scale = null): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::DOUBLE)
return (new DoubleColumnSchema(ColumnType::DOUBLE))
->size($size)
->scale($scale);
}
Expand All @@ -141,8 +137,7 @@ public static function double(int|null $size = null, int|null $scale = null): Co
*/
public static function decimal(int|null $size = 10, int|null $scale = 0): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::DECIMAL)
return (new DoubleColumnSchema(ColumnType::DECIMAL))
->size($size)
->scale($scale);
}
Expand All @@ -152,8 +147,7 @@ public static function decimal(int|null $size = 10, int|null $scale = 0): Column
*/
public static function money(int|null $size = 19, int|null $scale = 4): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::MONEY)
return (new DoubleColumnSchema(ColumnType::MONEY))
->size($size)
->scale($scale);
}
Expand All @@ -163,8 +157,7 @@ public static function money(int|null $size = 19, int|null $scale = 4): ColumnSc
*/
public static function char(int|null $size = 1): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::CHAR)
return (new StringColumnSchema(ColumnType::CHAR))
->size($size);
}

Expand All @@ -173,8 +166,7 @@ public static function char(int|null $size = 1): ColumnSchemaInterface
*/
public static function string(int|null $size = 255): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::STRING)
return (new StringColumnSchema(ColumnType::STRING))
->size($size);
}

Expand All @@ -183,8 +175,7 @@ public static function string(int|null $size = 255): ColumnSchemaInterface
*/
public static function text(int|null $size = null): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::TEXT)
return (new StringColumnSchema(ColumnType::TEXT))
->size($size);
}

Expand All @@ -193,8 +184,7 @@ public static function text(int|null $size = null): ColumnSchemaInterface
*/
public static function binary(int|null $size = null): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::BINARY)
return (new BinaryColumnSchema(ColumnType::BINARY))
->size($size);
}

Expand All @@ -203,17 +193,15 @@ public static function binary(int|null $size = null): ColumnSchemaInterface
*/
public static function uuid(): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::UUID);
return (new StringColumnSchema(ColumnType::UUID));
}

/**
* Builds a column with the abstract type `datetime`.
*/
public static function datetime(int|null $size = 0): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::DATETIME)
return (new StringColumnSchema(ColumnType::DATETIME))
->size($size);
}

Expand All @@ -222,8 +210,7 @@ public static function datetime(int|null $size = 0): ColumnSchemaInterface
*/
public static function timestamp(int|null $size = 0): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::TIMESTAMP)
return (new StringColumnSchema(ColumnType::TIMESTAMP))
->size($size);
}

Expand All @@ -232,17 +219,15 @@ public static function timestamp(int|null $size = 0): ColumnSchemaInterface
*/
public static function date(): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::DATE);
return (new StringColumnSchema(ColumnType::DATE));
}

/**
* Builds a column with the abstract type `time`.
*/
public static function time(int|null $size = 0): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::TIME)
return (new StringColumnSchema(ColumnType::TIME))
->size($size);
}

Expand All @@ -251,7 +236,6 @@ public static function time(int|null $size = 0): ColumnSchemaInterface
*/
public static function json(): ColumnSchemaInterface
{
return static::columnFactory()
->fromType(ColumnType::JSON);
return (new JsonColumnSchema(ColumnType::JSON));
}
}
12 changes: 4 additions & 8 deletions tests/AbstractColumnBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Yiisoft\Db\Tests;

use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Schema\Column\ColumnFactory;
use Yiisoft\Db\Schema\Column\ColumnBuilder;
use Yiisoft\Db\Tests\Provider\ColumnBuilderProvider;
use Yiisoft\Db\Tests\Support\TestTrait;

Expand All @@ -15,12 +15,9 @@ abstract class AbstractColumnBuilderTest extends TestCase
{
use TestTrait;

public function testColumnFactory(): void
public function getColumnBuilderClass(): string
{
$db = $this->getConnection();
$columnBuilderClass = $db->getColumnBuilderClass();

$this->assertInstanceOf(ColumnFactory::class, $columnBuilderClass::columnFactory());
return ColumnBuilder::class;
}

/**
Expand All @@ -33,8 +30,7 @@ public function testBuildingMethods(
string $expectedType,
array $expectedMethodResults = [],
): void {
$db = $this->getConnection();
$columnBuilderClass = $db->getColumnBuilderClass();
$columnBuilderClass = $this->getColumnBuilderClass();

$column = $columnBuilderClass::$buildingMethod(...$args);

Expand Down
10 changes: 5 additions & 5 deletions tests/AbstractColumnFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class AbstractColumnFactoryTest extends TestCase
public function testFromDbType(string $dbType, string $expectedType, string $expectedInstanceOf): void
{
$db = $this->getConnection();
$columnFactory = $db->getColumnBuilderClass()::columnFactory();
$columnFactory = $db->getColumnFactory();

$column = $columnFactory->fromDbType($dbType);

Expand All @@ -36,7 +36,7 @@ public function testFromDefinition(
array $expectedMethodResults = []
): void {
$db = $this->getConnection();
$columnFactory = $db->getColumnBuilderClass()::columnFactory();
$columnFactory = $db->getColumnFactory();

$column = $columnFactory->fromDefinition($definition);

Expand All @@ -61,7 +61,7 @@ public function testFromPseudoType(
array $expectedMethodResults = []
): void {
$db = $this->getConnection();
$columnFactory = $db->getColumnBuilderClass()::columnFactory();
$columnFactory = $db->getColumnFactory();

$column = $columnFactory->fromPseudoType($pseudoType);

Expand All @@ -82,7 +82,7 @@ public function testFromPseudoType(
public function testFromType(string $type, string $expectedType, string $expectedInstanceOf): void
{
$db = $this->getConnection();
$columnFactory = $db->getColumnBuilderClass()::columnFactory();
$columnFactory = $db->getColumnFactory();

$column = $columnFactory->fromType($type);

Expand All @@ -93,7 +93,7 @@ public function testFromType(string $type, string $expectedType, string $expecte
public function testFromDefinitionWithExtra(): void
{
$db = $this->getConnection();
$columnFactory = $db->getColumnBuilderClass()::columnFactory();
$columnFactory = $db->getColumnFactory();

$column = $columnFactory->fromDefinition('char(1) NOT NULL', ['extra' => 'UNIQUE']);

Expand Down
Loading

0 comments on commit 926c925

Please sign in to comment.