diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ac0433df..44ce8fb13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/UPGRADE.md b/UPGRADE.md index aac6197ae..6fe3a7c57 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -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 diff --git a/src/Connection/ConnectionInterface.php b/src/Connection/ConnectionInterface.php index a91580455..55e24a83c 100644 --- a/src/Connection/ConnectionInterface.php +++ b/src/Connection/ConnectionInterface.php @@ -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; @@ -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 + * 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`. diff --git a/src/Debug/ConnectionInterfaceProxy.php b/src/Debug/ConnectionInterfaceProxy.php index 9bf96b253..802392be8 100644 --- a/src/Debug/ConnectionInterfaceProxy.php +++ b/src/Debug/ConnectionInterfaceProxy.php @@ -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; @@ -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 diff --git a/src/Schema/Column/ColumnBuilder.php b/src/Schema/Column/ColumnBuilder.php index 5f6131869..1d3dd5cbd 100644 --- a/src/Schema/Column/ColumnBuilder.php +++ b/src/Schema/Column/ColumnBuilder.php @@ -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() @@ -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() @@ -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() @@ -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() @@ -60,8 +64,7 @@ public static function uuidPrimaryKey(bool $autoIncrement = false): ColumnSchema */ public static function boolean(): ColumnSchemaInterface { - return static::columnFactory() - ->fromType(ColumnType::BOOLEAN); + return (new BooleanColumnSchema(ColumnType::BOOLEAN)); } /** @@ -69,8 +72,7 @@ public static function boolean(): ColumnSchemaInterface */ public static function bit(int|null $size = null): ColumnSchemaInterface { - return static::columnFactory() - ->fromType(ColumnType::BIT) + return (new BitColumnSchema(ColumnType::BIT)) ->size($size); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -203,8 +193,7 @@ 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)); } /** @@ -212,8 +201,7 @@ public static function uuid(): ColumnSchemaInterface */ public static function datetime(int|null $size = 0): ColumnSchemaInterface { - return static::columnFactory() - ->fromType(ColumnType::DATETIME) + return (new StringColumnSchema(ColumnType::DATETIME)) ->size($size); } @@ -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); } @@ -232,8 +219,7 @@ 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)); } /** @@ -241,8 +227,7 @@ public static function date(): ColumnSchemaInterface */ public static function time(int|null $size = 0): ColumnSchemaInterface { - return static::columnFactory() - ->fromType(ColumnType::TIME) + return (new StringColumnSchema(ColumnType::TIME)) ->size($size); } @@ -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)); } } diff --git a/tests/AbstractColumnBuilderTest.php b/tests/AbstractColumnBuilderTest.php index dccc38997..9707be712 100644 --- a/tests/AbstractColumnBuilderTest.php +++ b/tests/AbstractColumnBuilderTest.php @@ -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; @@ -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; } /** @@ -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); diff --git a/tests/AbstractColumnFactoryTest.php b/tests/AbstractColumnFactoryTest.php index 5e8912d3f..1f457242a 100644 --- a/tests/AbstractColumnFactoryTest.php +++ b/tests/AbstractColumnFactoryTest.php @@ -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); @@ -36,7 +36,7 @@ public function testFromDefinition( array $expectedMethodResults = [] ): void { $db = $this->getConnection(); - $columnFactory = $db->getColumnBuilderClass()::columnFactory(); + $columnFactory = $db->getColumnFactory(); $column = $columnFactory->fromDefinition($definition); @@ -61,7 +61,7 @@ public function testFromPseudoType( array $expectedMethodResults = [] ): void { $db = $this->getConnection(); - $columnFactory = $db->getColumnBuilderClass()::columnFactory(); + $columnFactory = $db->getColumnFactory(); $column = $columnFactory->fromPseudoType($pseudoType); @@ -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); @@ -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']); diff --git a/tests/Common/CommonPdoConnectionTest.php b/tests/Common/CommonPdoConnectionTest.php index 9b194a31b..756616e9c 100644 --- a/tests/Common/CommonPdoConnectionTest.php +++ b/tests/Common/CommonPdoConnectionTest.php @@ -311,7 +311,7 @@ public function testTransactionRollbackTransactionOnLevel(): void 'createTransaction', 'createCommand', 'close', - 'getColumnBuilderClass', + 'getColumnFactory', 'getDriverName', 'getLastInsertID', 'getQueryBuilder', @@ -346,7 +346,7 @@ public function testGetActivePdo(): void $db = $this->getMockBuilder(AbstractPdoConnection::class)->onlyMethods([ 'createCommand', 'createTransaction', - 'getColumnBuilderClass', + 'getColumnFactory', 'getPdo', 'getQueryBuilder', 'getQuoter', diff --git a/tests/Provider/ColumnBuilderProvider.php b/tests/Provider/ColumnBuilderProvider.php index 467f84c9d..7deff7338 100644 --- a/tests/Provider/ColumnBuilderProvider.php +++ b/tests/Provider/ColumnBuilderProvider.php @@ -62,9 +62,9 @@ public static function buildingMethods(): array ['decimal', [], DoubleColumnSchema::class, ColumnType::DECIMAL, ['getSize' => 10, 'getScale' => 0]], ['decimal', [8], DoubleColumnSchema::class, ColumnType::DECIMAL, ['getSize' => 8, 'getScale' => 0]], ['decimal', [8, 2], DoubleColumnSchema::class, ColumnType::DECIMAL, ['getSize' => 8, 'getScale' => 2]], - ['money', [], StringColumnSchema::class, ColumnType::MONEY, ['getSize' => 19, 'getScale' => 4]], - ['money', [8], StringColumnSchema::class, ColumnType::MONEY, ['getSize' => 8, 'getScale' => 4]], - ['money', [8, 2], StringColumnSchema::class, ColumnType::MONEY, ['getSize' => 8, 'getScale' => 2]], + ['money', [], DoubleColumnSchema::class, ColumnType::MONEY, ['getSize' => 19, 'getScale' => 4]], + ['money', [8], DoubleColumnSchema::class, ColumnType::MONEY, ['getSize' => 8, 'getScale' => 4]], + ['money', [8, 2], DoubleColumnSchema::class, ColumnType::MONEY, ['getSize' => 8, 'getScale' => 2]], ['char', [], StringColumnSchema::class, ColumnType::CHAR, ['getSize' => 1]], ['char', [100], StringColumnSchema::class, ColumnType::CHAR, ['getSize' => 100]], ['string', [], StringColumnSchema::class, ColumnType::STRING, ['getSize' => 255]], diff --git a/src/Schema/Column/ColumnFactory.php b/tests/Support/Stub/ColumnFactory.php similarity index 80% rename from src/Schema/Column/ColumnFactory.php rename to tests/Support/Stub/ColumnFactory.php index dade5de53..bd6d4f2bc 100644 --- a/src/Schema/Column/ColumnFactory.php +++ b/tests/Support/Stub/ColumnFactory.php @@ -2,9 +2,10 @@ declare(strict_types=1); -namespace Yiisoft\Db\Schema\Column; +namespace Yiisoft\Db\Tests\Support\Stub; use Yiisoft\Db\Constant\ColumnType; +use Yiisoft\Db\Schema\Column\AbstractColumnFactory; class ColumnFactory extends AbstractColumnFactory { diff --git a/tests/Support/Stub/Connection.php b/tests/Support/Stub/Connection.php index a3fc1fb82..ac616b0af 100644 --- a/tests/Support/Stub/Connection.php +++ b/tests/Support/Stub/Connection.php @@ -8,7 +8,7 @@ use Yiisoft\Db\Command\CommandInterface; use Yiisoft\Db\Driver\Pdo\AbstractPdoConnection; use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; -use Yiisoft\Db\Schema\Column\ColumnBuilder; +use Yiisoft\Db\Schema\Column\ColumnFactoryInterface; use Yiisoft\Db\Schema\Quoter; use Yiisoft\Db\Schema\QuoterInterface; use Yiisoft\Db\Schema\SchemaInterface; @@ -43,9 +43,9 @@ public function createTransaction(): TransactionInterface return new Transaction($this); } - public function getColumnBuilderClass(): string + public function getColumnFactory(): ColumnFactoryInterface { - return ColumnBuilder::class; + return new ColumnFactory(); } public function getQueryBuilder(): QueryBuilderInterface