Skip to content

Commit a43382d

Browse files
authored
Use new column definition builder (#293)
1 parent 5546189 commit a43382d

9 files changed

+35
-55
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- Enh #289: Refactor `Schema::normalizeDefaultValue()` method and move it to `ColumnFactory` class (@Tigrov)
2424
- New #292: Override `QueryBuilder::prepareBinary()` method (@Tigrov)
2525
- Chg #294: Update `QueryBuilder` constructor (@Tigrov)
26+
- Enh #293: Use `ColumnDefinitionBuilder` to generate table column SQL representation (@Tigrov)
2627

2728
## 1.3.0 March 21, 2024
2829

src/Column.php

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
*
2121
* Provides a fluent interface, which means that the methods can be chained together to create a column schema with
2222
* many properties in a single line of code.
23+
*
24+
* @psalm-suppress DeprecatedClass
25+
*
26+
* @deprecated Use {@see StringColumnSchema} or other column classes instead. Will be removed in 2.0.0.
2327
*/
2428
final class Column extends AbstractColumn
2529
{

src/Column/ColumnDefinitionBuilder.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder
1616
{
1717
protected const AUTO_INCREMENT_KEYWORD = 'GENERATED BY DEFAULT AS IDENTITY';
1818

19-
protected const GENERATE_UUID_EXPRESSION = 'sys_guid()';
20-
2119
protected const TYPES_WITH_SIZE = [
2220
'char',
2321
'nchar',
@@ -99,4 +97,9 @@ protected function getDbType(ColumnSchemaInterface $column): string
9997
default => 'varchar2',
10098
};
10199
}
100+
101+
protected function getDefaultUuidExpression(): string
102+
{
103+
return 'sys_guid()';
104+
}
102105
}

src/DDLQueryBuilder.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Yiisoft\Db\Exception\NotSupportedException;
99
use Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder;
1010
use Yiisoft\Db\Schema\Builder\ColumnInterface;
11+
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
1112

1213
/**
1314
* Implements a (Data Definition Language) SQL statements for Oracle Server.
@@ -45,14 +46,13 @@ public function addForeignKey(
4546
return $sql;
4647
}
4748

48-
public function alterColumn(string $table, string $column, ColumnInterface|string $type): string
49+
public function alterColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): string
4950
{
50-
/** @psalm-suppress DeprecatedMethod */
5151
return 'ALTER TABLE '
5252
. $this->quoter->quoteTableName($table)
5353
. ' MODIFY '
5454
. $this->quoter->quoteColumnName($column)
55-
. ' ' . $this->queryBuilder->getColumnType($type);
55+
. ' ' . $this->queryBuilder->buildColumnDefinition($type);
5656
}
5757

5858
public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string

src/Schema.php

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public function __construct(protected ConnectionInterface $db, SchemaCache $sche
7373
/** @deprecated Use {@see ColumnBuilder} instead. Will be removed in 2.0. */
7474
public function createColumn(string $type, array|int|string $length = null): ColumnInterface
7575
{
76+
/** @psalm-suppress DeprecatedClass */
7677
return new Column($type, $length);
7778
}
7879

tests/ColumnSchemaBuilderTest.php

-8
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,4 @@ public function testCustomTypes(string $expected, string $type, int|null $length
2323
{
2424
$this->checkBuildString($expected, $type, $length, $calls);
2525
}
26-
27-
/**
28-
* @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\ColumnSchemaBuilderProvider::createColumnTypes
29-
*/
30-
public function testCreateColumnTypes(string $expected, string $type, ?int $length, array $calls): void
31-
{
32-
parent::testCreateColumnTypes($expected, $type, $length, $calls);
33-
}
3426
}

tests/Provider/ColumnSchemaBuilderProvider.php

-19
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,4 @@ public static function types(): array
2222
['integer UNSIGNED', ColumnType::INTEGER, null, [['unsigned']]],
2323
];
2424
}
25-
26-
public static function createColumnTypes(): array
27-
{
28-
$types = parent::createColumnTypes();
29-
$types['integer'][0] = '"column" NUMBER(10)';
30-
31-
$types['uuid'][0] = '"column" RAW(16)';
32-
$types['uuid not null'][0] = '"column" RAW(16) NOT NULL';
33-
34-
$types['uuid with default'][0] = '"column" RAW(16) DEFAULT HEXTORAW(REGEXP_REPLACE(\'875343b3-6bd0-4bec-81bb-aa68bb52d945\', \'-\', \'\'))';
35-
$types['uuid with default'][3] = [['defaultExpression', 'HEXTORAW(REGEXP_REPLACE(\'875343b3-6bd0-4bec-81bb-aa68bb52d945\', \'-\', \'\'))']];
36-
37-
$types['uuid pk'][0] = '"column" RAW(16) DEFAULT SYS_GUID() PRIMARY KEY';
38-
$types['uuid pk not null'][0] = '"column" RAW(16) DEFAULT SYS_GUID() PRIMARY KEY NOT NULL';
39-
$types['uuid pk not null with default'][0] = '"column" RAW(16) DEFAULT SYS_GUID() PRIMARY KEY NOT NULL';
40-
$types['uuid pk not null with default'][3] = [['notNull']];
41-
42-
return $types;
43-
}
4425
}

tests/Provider/QueryBuilderProvider.php

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Yiisoft\Db\Oracle\Tests\Provider;
66

77
use Exception;
8+
use Yiisoft\Db\Constant\ColumnType;
89
use Yiisoft\Db\Constant\PseudoType;
910
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
1011
use Yiisoft\Db\Expression\Expression;
@@ -44,6 +45,13 @@ public static function addForeignKey(): array
4445
return $addForeingKey;
4546
}
4647

48+
public static function alterColumn(): array
49+
{
50+
return [
51+
[ColumnType::STRING, 'ALTER TABLE "foo1" MODIFY "bar" varchar2(255)'],
52+
];
53+
}
54+
4755
public static function batchInsert(): array
4856
{
4957
$batchInsert = parent::batchInsert();

tests/QueryBuilderTest.php

+13-23
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PHPUnit\Framework\Attributes\DataProviderExternal;
88
use Throwable;
9-
use Yiisoft\Db\Constant\ColumnType;
109
use Yiisoft\Db\Exception\Exception;
1110
use Yiisoft\Db\Exception\InvalidArgumentException;
1211
use Yiisoft\Db\Exception\InvalidConfigException;
@@ -28,6 +27,11 @@ final class QueryBuilderTest extends CommonQueryBuilderTest
2827
{
2928
use TestTrait;
3029

30+
public function getBuildColumnDefinitionProvider(): array
31+
{
32+
return QueryBuilderProvider::buildColumnDefinition();
33+
}
34+
3135
/**
3236
* @throws Exception
3337
* @throws InvalidConfigException
@@ -99,24 +103,10 @@ public function testAddUnique(string $name, string $table, array|string $columns
99103
parent::testAddUnique($name, $table, $columns, $expected);
100104
}
101105

102-
/**
103-
* @throws Exception
104-
* @throws InvalidConfigException
105-
*/
106-
public function testAlterColumn(): void
106+
#[DataProviderExternal(QueryBuilderProvider::class, 'alterColumn')]
107+
public function testAlterColumn(string|ColumnSchemaInterface $type, string $expected): void
107108
{
108-
$db = $this->getConnection();
109-
110-
$qb = $db->getQueryBuilder();
111-
112-
$this->assertSame(
113-
<<<SQL
114-
ALTER TABLE "customer" MODIFY "email" VARCHAR2(255)
115-
SQL,
116-
$qb->alterColumn('customer', 'email', ColumnType::STRING),
117-
);
118-
119-
$db->close();
109+
parent::testAlterColumn($type, $expected);
120110
}
121111

122112
/**
@@ -312,11 +302,11 @@ public function testCreateTable(): void
312302
$this->assertSame(
313303
<<<SQL
314304
CREATE TABLE "test" (
315-
\t"id" NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,
316-
\t"name" VARCHAR2(255) NOT NULL,
317-
\t"email" VARCHAR2(255) NOT NULL,
318-
\t"status" NUMBER(10) NOT NULL,
319-
\t"created_at" TIMESTAMP(0) NOT NULL
305+
\t"id" number(10) GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
306+
\t"name" varchar2(255) NOT NULL,
307+
\t"email" varchar2(255) NOT NULL,
308+
\t"status" number(10) NOT NULL,
309+
\t"created_at" timestamp NOT NULL
320310
)
321311
SQL,
322312
$qb->createTable(

0 commit comments

Comments
 (0)