From 87f8e1a2ee8350d7bf8528726733966e0d82e89f Mon Sep 17 00:00:00 2001 From: Tigrov Date: Fri, 8 Nov 2024 08:24:05 +0700 Subject: [PATCH 1/3] Use new column definition builder --- src/Column.php | 4 +++ src/DDLQueryBuilder.php | 6 ++-- src/Schema.php | 1 + tests/ColumnSchemaBuilderTest.php | 8 ----- .../Provider/ColumnSchemaBuilderProvider.php | 19 ---------- tests/Provider/QueryBuilderProvider.php | 8 +++++ tests/QueryBuilderTest.php | 36 +++++++------------ 7 files changed, 29 insertions(+), 53 deletions(-) diff --git a/src/Column.php b/src/Column.php index 51efdc13..be6dc324 100644 --- a/src/Column.php +++ b/src/Column.php @@ -20,6 +20,10 @@ * * Provides a fluent interface, which means that the methods can be chained together to create a column schema with * many properties in a single line of code. + * + * @psalm-suppress DeprecatedClass + * + * @deprecated Use {@see StringColumnSchema} or other column classes instead. Will be removed in 2.0.0. */ final class Column extends AbstractColumn { diff --git a/src/DDLQueryBuilder.php b/src/DDLQueryBuilder.php index 972dcd3c..0b3d7048 100644 --- a/src/DDLQueryBuilder.php +++ b/src/DDLQueryBuilder.php @@ -8,6 +8,7 @@ use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder; use Yiisoft\Db\Schema\Builder\ColumnInterface; +use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; /** * Implements a (Data Definition Language) SQL statements for Oracle Server. @@ -45,14 +46,13 @@ public function addForeignKey( return $sql; } - public function alterColumn(string $table, string $column, ColumnInterface|string $type): string + public function alterColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): string { - /** @psalm-suppress DeprecatedMethod */ return 'ALTER TABLE ' . $this->quoter->quoteTableName($table) . ' MODIFY ' . $this->quoter->quoteColumnName($column) - . ' ' . $this->queryBuilder->getColumnType($type); + . ' ' . $this->queryBuilder->buildColumnDefinition($type); } public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string diff --git a/src/Schema.php b/src/Schema.php index b2364ea7..736968f2 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -73,6 +73,7 @@ public function __construct(protected ConnectionInterface $db, SchemaCache $sche /** @deprecated Use {@see ColumnBuilder} instead. Will be removed in 2.0. */ public function createColumn(string $type, array|int|string $length = null): ColumnInterface { + /** @psalm-suppress DeprecatedClass */ return new Column($type, $length); } diff --git a/tests/ColumnSchemaBuilderTest.php b/tests/ColumnSchemaBuilderTest.php index c6c46c34..41150090 100644 --- a/tests/ColumnSchemaBuilderTest.php +++ b/tests/ColumnSchemaBuilderTest.php @@ -23,12 +23,4 @@ public function testCustomTypes(string $expected, string $type, int|null $length { $this->checkBuildString($expected, $type, $length, $calls); } - - /** - * @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\ColumnSchemaBuilderProvider::createColumnTypes - */ - public function testCreateColumnTypes(string $expected, string $type, ?int $length, array $calls): void - { - parent::testCreateColumnTypes($expected, $type, $length, $calls); - } } diff --git a/tests/Provider/ColumnSchemaBuilderProvider.php b/tests/Provider/ColumnSchemaBuilderProvider.php index 9c065cf4..b157b445 100644 --- a/tests/Provider/ColumnSchemaBuilderProvider.php +++ b/tests/Provider/ColumnSchemaBuilderProvider.php @@ -22,23 +22,4 @@ public static function types(): array ['integer UNSIGNED', ColumnType::INTEGER, null, [['unsigned']]], ]; } - - public static function createColumnTypes(): array - { - $types = parent::createColumnTypes(); - $types['integer'][0] = '"column" NUMBER(10)'; - - $types['uuid'][0] = '"column" RAW(16)'; - $types['uuid not null'][0] = '"column" RAW(16) NOT NULL'; - - $types['uuid with default'][0] = '"column" RAW(16) DEFAULT HEXTORAW(REGEXP_REPLACE(\'875343b3-6bd0-4bec-81bb-aa68bb52d945\', \'-\', \'\'))'; - $types['uuid with default'][3] = [['defaultExpression', 'HEXTORAW(REGEXP_REPLACE(\'875343b3-6bd0-4bec-81bb-aa68bb52d945\', \'-\', \'\'))']]; - - $types['uuid pk'][0] = '"column" RAW(16) DEFAULT SYS_GUID() PRIMARY KEY'; - $types['uuid pk not null'][0] = '"column" RAW(16) DEFAULT SYS_GUID() PRIMARY KEY NOT NULL'; - $types['uuid pk not null with default'][0] = '"column" RAW(16) DEFAULT SYS_GUID() PRIMARY KEY NOT NULL'; - $types['uuid pk not null with default'][3] = [['notNull']]; - - return $types; - } } diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 731a7997..4655bb23 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Oracle\Tests\Provider; use Exception; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Constant\PseudoType; use Yiisoft\Db\Constraint\ForeignKeyConstraint; use Yiisoft\Db\Expression\Expression; @@ -44,6 +45,13 @@ public static function addForeignKey(): array return $addForeingKey; } + public static function alterColumn(): array + { + return [ + [ColumnType::STRING, 'ALTER TABLE "foo1" MODIFY "bar" varchar2(255)'], + ]; + } + public static function batchInsert(): array { $batchInsert = parent::batchInsert(); diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 75ce0b61..10859887 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -6,7 +6,6 @@ use PHPUnit\Framework\Attributes\DataProviderExternal; use Throwable; -use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidConfigException; @@ -28,6 +27,11 @@ final class QueryBuilderTest extends CommonQueryBuilderTest { use TestTrait; + public function getBuildColumnDefinitionProvider(): array + { + return QueryBuilderProvider::buildColumnDefinition(); + } + /** * @throws Exception * @throws InvalidConfigException @@ -99,24 +103,10 @@ public function testAddUnique(string $name, string $table, array|string $columns parent::testAddUnique($name, $table, $columns, $expected); } - /** - * @throws Exception - * @throws InvalidConfigException - */ - public function testAlterColumn(): void + #[DataProviderExternal(QueryBuilderProvider::class, 'alterColumn')] + public function testAlterColumn(string|ColumnSchemaInterface $type, string $expected): void { - $db = $this->getConnection(); - - $qb = $db->getQueryBuilder(); - - $this->assertSame( - <<alterColumn('customer', 'email', ColumnType::STRING), - ); - - $db->close(); + parent::testAlterColumn($type, $expected); } /** @@ -312,11 +302,11 @@ public function testCreateTable(): void $this->assertSame( <<createTable( From bc7ee430089da62b05d260504b5fb5d4d53a9ecf Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 11 Dec 2024 17:32:02 +0700 Subject: [PATCH 2/3] Add `ColumnDefinitionBuilder::getDefaultUuidExpression()` method --- src/Column/ColumnDefinitionBuilder.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Column/ColumnDefinitionBuilder.php b/src/Column/ColumnDefinitionBuilder.php index 10c5eb16..82ba9885 100644 --- a/src/Column/ColumnDefinitionBuilder.php +++ b/src/Column/ColumnDefinitionBuilder.php @@ -16,8 +16,6 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder { protected const AUTO_INCREMENT_KEYWORD = 'GENERATED BY DEFAULT AS IDENTITY'; - protected const GENERATE_UUID_EXPRESSION = 'sys_guid()'; - protected const TYPES_WITH_SIZE = [ 'char', 'nchar', @@ -99,4 +97,9 @@ protected function getDbType(ColumnSchemaInterface $column): string default => 'varchar2', }; } + + protected function getDefaultUuidExpression(): string + { + return 'sys_guid()'; + } } From e4b83ac61649d8a1c97750fa6067138ff19d40cc Mon Sep 17 00:00:00 2001 From: Tigrov Date: Fri, 13 Dec 2024 10:26:40 +0700 Subject: [PATCH 3/3] Add line to CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b40e537d..47b3c6b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ - Enh #289: Refactor `Schema::normalizeDefaultValue()` method and move it to `ColumnFactory` class (@Tigrov) - New #292: Override `QueryBuilder::prepareBinary()` method (@Tigrov) - Chg #294: Update `QueryBuilder` constructor (@Tigrov) +- Enh #293: Use `ColumnDefinitionBuilder` to generate table column SQL representation (@Tigrov) ## 1.3.0 March 21, 2024