From d384728b478ab4677619212b8375f03b3be831c1 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sat, 7 Sep 2024 21:27:07 +0700 Subject: [PATCH 1/4] Separate column type constants --- src/Column.php | 2 +- src/Column/ColumnFactory.php | 54 +++++++++---------- src/QueryBuilder.php | 48 +++++++++-------- src/Schema.php | 3 +- tests/CommandTest.php | 11 ++-- tests/Provider/ColumnFactoryProvider.php | 41 +++++++------- .../Provider/ColumnSchemaBuilderProvider.php | 4 +- tests/QueryBuilderTest.php | 4 +- 8 files changed, 86 insertions(+), 81 deletions(-) diff --git a/src/Column.php b/src/Column.php index 62ec3491..51efdc13 100644 --- a/src/Column.php +++ b/src/Column.php @@ -15,7 +15,7 @@ * For example, the following code creates a column schema for an integer column: * * ```php - * $column = (new Column(SchemaInterface::TYPE_INTEGER))->notNull()->defaultValue(0); + * $column = (new Column(ColumnType::INTEGER))->notNull()->defaultValue(0); * ``` * * Provides a fluent interface, which means that the methods can be chained together to create a column schema with diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index 3adc1b2c..bfdea887 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -4,9 +4,9 @@ namespace Yiisoft\Db\Oracle\Column; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Schema\Column\AbstractColumnFactory; use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; -use Yiisoft\Db\Schema\SchemaInterface; use function preg_replace; use function strtolower; @@ -23,28 +23,28 @@ final class ColumnFactory extends AbstractColumnFactory * @psalm-suppress MissingClassConstType */ private const TYPE_MAP = [ - 'char' => SchemaInterface::TYPE_CHAR, - 'nchar' => SchemaInterface::TYPE_CHAR, - 'varchar2' => SchemaInterface::TYPE_STRING, - 'nvarchar2' => SchemaInterface::TYPE_STRING, - 'clob' => SchemaInterface::TYPE_TEXT, - 'nclob' => SchemaInterface::TYPE_TEXT, - 'blob' => SchemaInterface::TYPE_BINARY, - 'bfile' => SchemaInterface::TYPE_BINARY, - 'long raw' => SchemaInterface::TYPE_BINARY, - 'raw' => SchemaInterface::TYPE_BINARY, - 'number' => SchemaInterface::TYPE_DECIMAL, - 'binary_float' => SchemaInterface::TYPE_FLOAT, // 32 bit - 'binary_double' => SchemaInterface::TYPE_DOUBLE, // 64 bit - 'float' => SchemaInterface::TYPE_DOUBLE, // 126 bit - 'date' => SchemaInterface::TYPE_DATE, - 'interval day to second' => SchemaInterface::TYPE_TIME, - 'timestamp' => SchemaInterface::TYPE_TIMESTAMP, - 'timestamp with time zone' => SchemaInterface::TYPE_TIMESTAMP, - 'timestamp with local time zone' => SchemaInterface::TYPE_TIMESTAMP, + 'char' => ColumnType::CHAR, + 'nchar' => ColumnType::CHAR, + 'varchar2' => ColumnType::STRING, + 'nvarchar2' => ColumnType::STRING, + 'clob' => ColumnType::TEXT, + 'nclob' => ColumnType::TEXT, + 'blob' => ColumnType::BINARY, + 'bfile' => ColumnType::BINARY, + 'long raw' => ColumnType::BINARY, + 'raw' => ColumnType::BINARY, + 'number' => ColumnType::DECIMAL, + 'binary_float' => ColumnType::FLOAT, // 32 bit + 'binary_double' => ColumnType::DOUBLE, // 64 bit + 'float' => ColumnType::DOUBLE, // 126 bit + 'date' => ColumnType::DATE, + 'interval day to second' => ColumnType::TIME, + 'timestamp' => ColumnType::TIMESTAMP, + 'timestamp with time zone' => ColumnType::TIMESTAMP, + 'timestamp with local time zone' => ColumnType::TIMESTAMP, /** Deprecated */ - 'long' => SchemaInterface::TYPE_TEXT, + 'long' => ColumnType::TEXT, ]; protected function getType(string $dbType, array $info = []): string @@ -55,24 +55,24 @@ protected function getType(string $dbType, array $info = []): string $scale = isset($info['scale']) ? (int) $info['scale'] : null; return match ($scale) { - null => SchemaInterface::TYPE_DOUBLE, - 0 => SchemaInterface::TYPE_INTEGER, - default => SchemaInterface::TYPE_DECIMAL, + null => ColumnType::DOUBLE, + 0 => ColumnType::INTEGER, + default => ColumnType::DECIMAL, }; } $dbType = preg_replace('/\([^)]+\)/', '', $dbType); if ($dbType === 'interval day to second' && isset($info['precision']) && $info['precision'] > 0) { - return SchemaInterface::TYPE_STRING; + return ColumnType::STRING; } - return self::TYPE_MAP[$dbType] ?? SchemaInterface::TYPE_STRING; + return self::TYPE_MAP[$dbType] ?? ColumnType::STRING; } public function fromType(string $type, array $info = []): ColumnSchemaInterface { - if ($type === SchemaInterface::TYPE_BINARY) { + if ($type === ColumnType::BINARY) { return (new BinaryColumnSchema($type))->load($info); } diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index d396a161..f82d8eaa 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -4,6 +4,8 @@ namespace Yiisoft\Db\Oracle; +use Yiisoft\Db\Constant\ColumnType; +use Yiisoft\Db\Constant\PseudoType; use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder; use Yiisoft\Db\Schema\QuoterInterface; use Yiisoft\Db\Schema\SchemaInterface; @@ -17,29 +19,29 @@ final class QueryBuilder extends AbstractQueryBuilder * @psalm-var string[] $typeMap Mapping from abstract column types (keys) to physical column types (values). */ protected array $typeMap = [ - SchemaInterface::TYPE_PK => 'NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY', - SchemaInterface::TYPE_UPK => 'NUMBER(10) GENERATED BY DEFAULT AS IDENTITY UNSIGNED NOT NULL PRIMARY KEY', - SchemaInterface::TYPE_BIGPK => 'NUMBER(20) GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY', - SchemaInterface::TYPE_UBIGPK => 'NUMBER(20) GENERATED BY DEFAULT AS IDENTITY UNSIGNED NOT NULL PRIMARY KEY', - SchemaInterface::TYPE_CHAR => 'CHAR(1)', - SchemaInterface::TYPE_STRING => 'VARCHAR2(255)', - SchemaInterface::TYPE_TEXT => 'CLOB', - SchemaInterface::TYPE_TINYINT => 'NUMBER(3)', - SchemaInterface::TYPE_SMALLINT => 'NUMBER(5)', - SchemaInterface::TYPE_INTEGER => 'NUMBER(10)', - SchemaInterface::TYPE_BIGINT => 'NUMBER(20)', - SchemaInterface::TYPE_FLOAT => 'BINARY_FLOAT', - SchemaInterface::TYPE_DOUBLE => 'BINARY_DOUBLE', - SchemaInterface::TYPE_DECIMAL => 'NUMBER(10,0)', - SchemaInterface::TYPE_DATETIME => 'TIMESTAMP(0)', - SchemaInterface::TYPE_TIMESTAMP => 'TIMESTAMP(0)', - SchemaInterface::TYPE_TIME => 'INTERVAL DAY(0) TO SECOND(0)', - SchemaInterface::TYPE_DATE => 'DATE', - SchemaInterface::TYPE_BINARY => 'BLOB', - SchemaInterface::TYPE_BOOLEAN => 'NUMBER(1)', - SchemaInterface::TYPE_MONEY => 'NUMBER(19,4)', - SchemaInterface::TYPE_UUID => 'RAW(16)', - SchemaInterface::TYPE_UUID_PK => 'RAW(16) DEFAULT SYS_GUID() PRIMARY KEY', + PseudoType::PK => 'NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY', + PseudoType::UPK => 'NUMBER(10) GENERATED BY DEFAULT AS IDENTITY UNSIGNED NOT NULL PRIMARY KEY', + PseudoType::BIGPK => 'NUMBER(20) GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY', + PseudoType::UBIGPK => 'NUMBER(20) GENERATED BY DEFAULT AS IDENTITY UNSIGNED NOT NULL PRIMARY KEY', + ColumnType::CHAR => 'CHAR(1)', + ColumnType::STRING => 'VARCHAR2(255)', + ColumnType::TEXT => 'CLOB', + ColumnType::TINYINT => 'NUMBER(3)', + ColumnType::SMALLINT => 'NUMBER(5)', + ColumnType::INTEGER => 'NUMBER(10)', + ColumnType::BIGINT => 'NUMBER(20)', + ColumnType::FLOAT => 'BINARY_FLOAT', + ColumnType::DOUBLE => 'BINARY_DOUBLE', + ColumnType::DECIMAL => 'NUMBER(10,0)', + ColumnType::DATETIME => 'TIMESTAMP(0)', + ColumnType::TIMESTAMP => 'TIMESTAMP(0)', + ColumnType::TIME => 'INTERVAL DAY(0) TO SECOND(0)', + ColumnType::DATE => 'DATE', + ColumnType::BINARY => 'BLOB', + ColumnType::BOOLEAN => 'NUMBER(1)', + ColumnType::MONEY => 'NUMBER(19,4)', + ColumnType::UUID => 'RAW(16)', + PseudoType::UUID_PK => 'RAW(16) DEFAULT SYS_GUID() PRIMARY KEY', ]; public function __construct(QuoterInterface $quoter, SchemaInterface $schema) diff --git a/src/Schema.php b/src/Schema.php index a5d76d03..d8b712fa 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -7,6 +7,7 @@ use Throwable; use Yiisoft\Db\Cache\SchemaCache; use Yiisoft\Db\Connection\ConnectionInterface; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Constraint\CheckConstraint; use Yiisoft\Db\Constraint\Constraint; use Yiisoft\Db\Constraint\ForeignKeyConstraint; @@ -467,7 +468,7 @@ private function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaIn return null; } - if ($column->getType() === self::TYPE_TIMESTAMP && $defaultValue === 'CURRENT_TIMESTAMP') { + if ($column->getType() === ColumnType::TIMESTAMP && $defaultValue === 'CURRENT_TIMESTAMP') { return new Expression($defaultValue); } diff --git a/tests/CommandTest.php b/tests/CommandTest.php index e48793ab..8bdc7b10 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -6,6 +6,8 @@ use ReflectionException; use Throwable; +use Yiisoft\Db\Constant\ColumnType; +use Yiisoft\Db\Constant\PseudoType; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidCallException; @@ -16,7 +18,6 @@ use Yiisoft\Db\Oracle\Driver; use Yiisoft\Db\Oracle\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; -use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Tests\Common\CommonCommandTest; use Yiisoft\Db\Tests\Support\Assert; use Yiisoft\Db\Tests\Support\DbHelper; @@ -89,7 +90,7 @@ public function testCLOBStringInsertion(): void $command->dropTable('longstring')->execute(); } - $command->createTable('longstring', ['message' => SchemaInterface::TYPE_TEXT])->execute(); + $command->createTable('longstring', ['message' => ColumnType::TEXT])->execute(); $longData = str_pad('-', 4001, '-=', STR_PAD_LEFT); $command->insert('longstring', ['message' => $longData])->execute(); @@ -130,7 +131,7 @@ public function testCreateTable(): void $command->createTable( '{{testCreateTable}}', - ['id' => SchemaInterface::TYPE_PK, 'bar' => SchemaInterface::TYPE_INTEGER] + ['id' => PseudoType::PK, 'bar' => ColumnType::INTEGER] )->execute(); $command->setSql( <<createTable( '{{testCreateViewTable}}', [ - '[[id]]' => SchemaInterface::TYPE_PK, - '[[bar]]' => SchemaInterface::TYPE_INTEGER, + '[[id]]' => PseudoType::PK, + '[[bar]]' => ColumnType::INTEGER, ], )->execute(); $command->setSql( diff --git a/tests/Provider/ColumnFactoryProvider.php b/tests/Provider/ColumnFactoryProvider.php index 8c99a375..f3713321 100644 --- a/tests/Provider/ColumnFactoryProvider.php +++ b/tests/Provider/ColumnFactoryProvider.php @@ -4,6 +4,7 @@ namespace Yiisoft\Db\Oracle\Tests\Provider; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Oracle\Column\BinaryColumnSchema; use Yiisoft\Db\Schema\Column\DoubleColumnSchema; use Yiisoft\Db\Schema\Column\StringColumnSchema; @@ -14,26 +15,26 @@ public static function dbTypes(): array { return [ // db type, expected abstract type, expected instance of - ['char', 'char', StringColumnSchema::class], - ['nchar', 'char', StringColumnSchema::class], - ['varchar2', 'string', StringColumnSchema::class], - ['nvarchar2', 'string', StringColumnSchema::class], - ['clob', 'text', StringColumnSchema::class], - ['nclob', 'text', StringColumnSchema::class], - ['long', 'text', StringColumnSchema::class], - ['blob', 'binary', BinaryColumnSchema::class], - ['bfile', 'binary', BinaryColumnSchema::class], - ['long raw', 'binary', BinaryColumnSchema::class], - ['raw', 'binary', BinaryColumnSchema::class], - ['number', 'double', DoubleColumnSchema::class], - ['binary_float', 'float', DoubleColumnSchema::class], - ['binary_double', 'double', DoubleColumnSchema::class], - ['float', 'double', DoubleColumnSchema::class], - ['date', 'date', StringColumnSchema::class], - ['interval day(0) to second', 'time', StringColumnSchema::class], - ['timestamp', 'timestamp', StringColumnSchema::class], - ['timestamp with time zone', 'timestamp', StringColumnSchema::class], - ['timestamp with local time zone', 'timestamp', StringColumnSchema::class], + ['char', ColumnType::CHAR, StringColumnSchema::class], + ['nchar', ColumnType::CHAR, StringColumnSchema::class], + ['varchar2', ColumnType::STRING, StringColumnSchema::class], + ['nvarchar2', ColumnType::STRING, StringColumnSchema::class], + ['clob', ColumnType::TEXT, StringColumnSchema::class], + ['nclob', ColumnType::TEXT, StringColumnSchema::class], + ['long', ColumnType::TEXT, StringColumnSchema::class], + ['blob', ColumnType::BINARY, BinaryColumnSchema::class], + ['bfile', ColumnType::BINARY, BinaryColumnSchema::class], + ['long raw', ColumnType::BINARY, BinaryColumnSchema::class], + ['raw', ColumnType::BINARY, BinaryColumnSchema::class], + ['number', ColumnType::DOUBLE, DoubleColumnSchema::class], + ['binary_float', ColumnType::FLOAT, DoubleColumnSchema::class], + ['binary_double', ColumnType::DOUBLE, DoubleColumnSchema::class], + ['float', ColumnType::DOUBLE, DoubleColumnSchema::class], + ['date', ColumnType::DATE, StringColumnSchema::class], + ['interval day(0) to second', ColumnType::TIME, StringColumnSchema::class], + ['timestamp', ColumnType::TIMESTAMP, StringColumnSchema::class], + ['timestamp with time zone', ColumnType::TIMESTAMP, StringColumnSchema::class], + ['timestamp with local time zone', ColumnType::TIMESTAMP, StringColumnSchema::class], ]; } diff --git a/tests/Provider/ColumnSchemaBuilderProvider.php b/tests/Provider/ColumnSchemaBuilderProvider.php index 5badb0e7..9c065cf4 100644 --- a/tests/Provider/ColumnSchemaBuilderProvider.php +++ b/tests/Provider/ColumnSchemaBuilderProvider.php @@ -4,7 +4,7 @@ namespace Yiisoft\Db\Oracle\Tests\Provider; -use Yiisoft\Db\Schema\SchemaInterface; +use Yiisoft\Db\Constant\ColumnType; final class ColumnSchemaBuilderProvider extends \Yiisoft\Db\Tests\Provider\ColumnSchemaBuilderProvider { @@ -19,7 +19,7 @@ public static function types(): array return [ ...$types, - ['integer UNSIGNED', SchemaInterface::TYPE_INTEGER, null, [['unsigned']]], + ['integer UNSIGNED', ColumnType::INTEGER, null, [['unsigned']]], ]; } diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index e973c649..bec761e8 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Oracle\Tests; use Throwable; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidConfigException; @@ -13,7 +14,6 @@ use Yiisoft\Db\Oracle\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Query\QueryInterface; -use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Tests\Common\CommonQueryBuilderTest; /** @@ -110,7 +110,7 @@ public function testAlterColumn(): void <<alterColumn('customer', 'email', SchemaInterface::TYPE_STRING), + $qb->alterColumn('customer', 'email', ColumnType::STRING), ); $db->close(); From 21f5d1e6ee771858450774ef3f99f92485fd35c5 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sun, 8 Sep 2024 15:16:57 +0700 Subject: [PATCH 2/4] Realize `ColumnBuilder` --- src/Column/ColumnBuilder.php | 15 +++++++++++++++ src/Column/ColumnFactory.php | 5 +++++ src/Connection.php | 6 ++++++ src/Schema.php | 12 +++--------- tests/ColumnBuilderTest.php | 23 +++++++++++++++++++++++ tests/ColumnFactoryTest.php | 20 +++++++++++++++++--- tests/ConnectionTest.php | 8 ++++++++ tests/Provider/ColumnFactoryProvider.php | 3 +++ tests/SchemaTest.php | 9 --------- 9 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 src/Column/ColumnBuilder.php create mode 100644 tests/ColumnBuilderTest.php diff --git a/src/Column/ColumnBuilder.php b/src/Column/ColumnBuilder.php new file mode 100644 index 00000000..9dcc0714 --- /dev/null +++ b/src/Column/ColumnBuilder.php @@ -0,0 +1,15 @@ +db->getColumnBuilderClass()::columnFactory(); + $dbType = $info['data_type']; - $column = $this->getColumnFactory()->fromDbType($dbType, [ + $column = $columnFactory->fromDbType($dbType, [ 'scale' => $info['data_scale'], 'precision' => $info['data_precision'], ]); @@ -442,7 +437,6 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface $column->primaryKey((bool) $info['is_pk']); $column->autoIncrement($info['identity_column'] === 'YES'); $column->size((int) $info['data_length']); - $column->dbType($dbType); $column->defaultValue($this->normalizeDefaultValue($info['data_default'], $column)); return $column; diff --git a/tests/ColumnBuilderTest.php b/tests/ColumnBuilderTest.php new file mode 100644 index 00000000..0852906f --- /dev/null +++ b/tests/ColumnBuilderTest.php @@ -0,0 +1,23 @@ +getConnection(); + $columnBuilderClass = $db->getColumnBuilderClass(); + + $this->assertInstanceOf(ColumnFactory::class, $columnBuilderClass::columnFactory()); + } +} diff --git a/tests/ColumnFactoryTest.php b/tests/ColumnFactoryTest.php index 390ce1c8..3cbaaf3c 100644 --- a/tests/ColumnFactoryTest.php +++ b/tests/ColumnFactoryTest.php @@ -21,9 +21,23 @@ public function testFromDbType(string $dbType, string $expectedType, string $exp } /** @dataProvider \Yiisoft\Db\Oracle\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\Oracle\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\Oracle\Tests\Provider\ColumnFactoryProvider::types */ diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index fe853afd..7601f04c 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -11,6 +11,7 @@ use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; +use Yiisoft\Db\Oracle\Column\ColumnBuilder; use Yiisoft\Db\Oracle\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonConnectionTest; use Yiisoft\Db\Transaction\TransactionInterface; @@ -130,4 +131,11 @@ public function testSerialized(): void $this->assertEquals(123, $unserialized->createCommand('SELECT 123 FROM DUAL')->queryScalar()); $this->assertNotNull($connection->getPDO()); } + + public function testGetColumnBuilderClass(): void + { + $db = $this->getConnection(); + + $this->assertSame(ColumnBuilder::class, $db->getColumnBuilderClass()); + } } diff --git a/tests/Provider/ColumnFactoryProvider.php b/tests/Provider/ColumnFactoryProvider.php index f3713321..88731d4c 100644 --- a/tests/Provider/ColumnFactoryProvider.php +++ b/tests/Provider/ColumnFactoryProvider.php @@ -43,8 +43,11 @@ public static function definitions(): array $definitions = parent::definitions(); $definitions['text'][0] = 'clob'; + $definitions['text'][3]['getDbType'] = 'clob'; $definitions['text NOT NULL'][0] = 'clob NOT NULL'; + $definitions['text NOT NULL'][3]['getDbType'] = 'clob'; $definitions['decimal(10,2)'][0] = 'number(10,2)'; + $definitions['decimal(10,2)'][3]['getDbType'] = 'number'; unset($definitions['bigint UNSIGNED']); diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index b98ae5e3..2fb6f0fe 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -10,7 +10,6 @@ use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; -use Yiisoft\Db\Oracle\Column\ColumnFactory; use Yiisoft\Db\Oracle\Schema; use Yiisoft\Db\Oracle\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonSchemaTest; @@ -296,12 +295,4 @@ public function testNotConnectionPDO(): void $schema->refresh(); } - - public function testGetColumnFactory(): void - { - $db = $this->getConnection(); - $factory = $db->getSchema()->getColumnFactory(); - - $this->assertInstanceOf(ColumnFactory::class, $factory); - } } From 2b88439becc0f20d0aa894439df5c9bffdb63557 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Mon, 16 Sep 2024 12:25:57 +0700 Subject: [PATCH 3/4] Remove `ColumnFactory` from `ColumnBuilder` --- CHANGELOG.md | 1 + src/Column/ColumnBuilder.php | 8 +++++--- src/Connection.php | 7 ++++--- src/Schema.php | 2 +- tests/ColumnBuilderTest.php | 22 +++++++++++++++++----- tests/ConnectionTest.php | 5 +++-- tests/Provider/ColumnBuilderProvider.php | 21 +++++++++++++++++++++ 7 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 tests/Provider/ColumnBuilderProvider.php diff --git a/CHANGELOG.md b/CHANGELOG.md index c1281fb4..267fc1df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - Enh #277: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov) - Enh #276: Implement `ColumnFactory` class (@Tigrov) - Enh #279: Separate column type constants (@Tigrov) +- Enh #280: Realize `ColumnBuilder` class (@Tigrov) ## 1.3.0 March 21, 2024 diff --git a/src/Column/ColumnBuilder.php b/src/Column/ColumnBuilder.php index 9dcc0714..a0dcd28e 100644 --- a/src/Column/ColumnBuilder.php +++ b/src/Column/ColumnBuilder.php @@ -4,12 +4,14 @@ namespace Yiisoft\Db\Oracle\Column; -use Yiisoft\Db\Schema\Column\ColumnFactoryInterface; +use Yiisoft\Db\Constant\ColumnType; +use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; final class ColumnBuilder extends \Yiisoft\Db\Schema\Column\ColumnBuilder { - public static function columnFactory(): ColumnFactoryInterface + public static function binary(int|null $size = null): ColumnSchemaInterface { - return new ColumnFactory(); + return (new BinaryColumnSchema(ColumnType::BINARY)) + ->size($size); } } diff --git a/src/Connection.php b/src/Connection.php index 4d689ce3..bed8d20c 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -11,8 +11,9 @@ use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidCallException; use Yiisoft\Db\Exception\InvalidConfigException; -use Yiisoft\Db\Oracle\Column\ColumnBuilder; +use Yiisoft\Db\Oracle\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; @@ -48,9 +49,9 @@ public function createTransaction(): TransactionInterface return new Transaction($this); } - public function getColumnBuilderClass(): string + public function getColumnFactory(): ColumnFactoryInterface { - return ColumnBuilder::class; + return new ColumnFactory(); } /** diff --git a/src/Schema.php b/src/Schema.php index f9a3b789..fdd0df75 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -424,7 +424,7 @@ protected function getTableSequenceName(string $tableName): string|null */ private function loadColumnSchema(array $info): ColumnSchemaInterface { - $columnFactory = $this->db->getColumnBuilderClass()::columnFactory(); + $columnFactory = $this->db->getColumnFactory(); $dbType = $info['data_type']; $column = $columnFactory->fromDbType($dbType, [ diff --git a/tests/ColumnBuilderTest.php b/tests/ColumnBuilderTest.php index 0852906f..d75fffaf 100644 --- a/tests/ColumnBuilderTest.php +++ b/tests/ColumnBuilderTest.php @@ -2,7 +2,9 @@ declare(strict_types=1); -use Yiisoft\Db\Oracle\Column\ColumnFactory; +namespace Yiisoft\Db\Oracle\Tests; + +use Yiisoft\Db\Oracle\Column\ColumnBuilder; use Yiisoft\Db\Oracle\Tests\Support\TestTrait; use Yiisoft\Db\Tests\AbstractColumnBuilderTest; @@ -13,11 +15,21 @@ class ColumnBuilderTest extends AbstractColumnBuilderTest { use TestTrait; - public function testColumnFactory(): void + public function getColumnBuilderClass(): string { - $db = $this->getConnection(); - $columnBuilderClass = $db->getColumnBuilderClass(); + return ColumnBuilder::class; + } - $this->assertInstanceOf(ColumnFactory::class, $columnBuilderClass::columnFactory()); + /** + * @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\ColumnBuilderProvider::buildingMethods + */ + public function testBuildingMethods( + string $buildingMethod, + array $args, + string $expectedInstanceOf, + string $expectedType, + array $expectedMethodResults = [], + ): void { + parent::testBuildingMethods($buildingMethod, $args, $expectedInstanceOf, $expectedType, $expectedMethodResults); } } diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 7601f04c..0b0e0da4 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -12,6 +12,7 @@ use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Oracle\Column\ColumnBuilder; +use Yiisoft\Db\Oracle\Column\ColumnFactory; use Yiisoft\Db\Oracle\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonConnectionTest; use Yiisoft\Db\Transaction\TransactionInterface; @@ -132,10 +133,10 @@ public function testSerialized(): void $this->assertNotNull($connection->getPDO()); } - public function testGetColumnBuilderClass(): void + public function testGetColumnFactory(): void { $db = $this->getConnection(); - $this->assertSame(ColumnBuilder::class, $db->getColumnBuilderClass()); + $this->assertInstanceOf(ColumnFactory::class, $db->getColumnFactory()); } } diff --git a/tests/Provider/ColumnBuilderProvider.php b/tests/Provider/ColumnBuilderProvider.php new file mode 100644 index 00000000..bc6bd1f7 --- /dev/null +++ b/tests/Provider/ColumnBuilderProvider.php @@ -0,0 +1,21 @@ + 8]], + ]; + } +} From b706b0a2002dfae3320bf647eb250bc943eb6e3d Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 16 Sep 2024 05:26:25 +0000 Subject: [PATCH 4/4] Apply fixes from StyleCI --- tests/ConnectionTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 0b0e0da4..233f1107 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -11,7 +11,6 @@ use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; -use Yiisoft\Db\Oracle\Column\ColumnBuilder; use Yiisoft\Db\Oracle\Column\ColumnFactory; use Yiisoft\Db\Oracle\Tests\Support\TestTrait; use Yiisoft\Db\Tests\Common\CommonConnectionTest;