Skip to content

Commit f926994

Browse files
authored
Use new column definition builder (#329)
1 parent aac0f56 commit f926994

9 files changed

Lines changed: 28 additions & 49 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- Enh #326: Refactor `Schema::normalizeDefaultValue()` method and move it to `ColumnFactory` class (@Tigrov)
2020
- New #328: Override `QueryBuilder::prepareBinary()` method (@Tigrov)
2121
- Chg #330: Update `QueryBuilder` constructor (@Tigrov)
22+
- Enh #329: Use `ColumnDefinitionBuilder` to generate table column SQL representation (@Tigrov)
2223

2324
## 1.2.0 March 21, 2024
2425

src/Column.php

Lines changed: 4 additions & 0 deletions
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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder
1212
{
1313
protected const AUTO_INCREMENT_KEYWORD = 'AUTOINCREMENT';
1414

15-
protected const GENERATE_UUID_EXPRESSION =
16-
"(unhex(format('%016X', random() & 0xFFFFFFFFFFFF4FFF | 0x4000) || format('%016X', random() & 0xBFFFFFFFFFFFFFFF | 0xB000000000000000)))";
17-
1815
protected const TYPES_WITH_SIZE = [
1916
'bit',
2017
'tinyint',
@@ -100,4 +97,9 @@ protected function getDbType(ColumnSchemaInterface $column): string
10097
default => 'varchar',
10198
};
10299
}
100+
101+
protected function getDefaultUuidExpression(): string
102+
{
103+
return '(randomblob(16))';
104+
}
103105
}

src/DDLQueryBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Yiisoft\Db\Exception\NotSupportedException;
88
use Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder;
99
use Yiisoft\Db\Schema\Builder\ColumnInterface;
10+
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
1011

1112
use function count;
1213

@@ -81,7 +82,7 @@ public function addUnique(string $table, string $name, array|string $columns): s
8182
/**
8283
* @throws NotSupportedException SQLite doesn't support this method.
8384
*/
84-
public function alterColumn(string $table, string $column, ColumnInterface|string $type): string
85+
public function alterColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): string
8586
{
8687
throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
8788
}

src/Schema.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ final class Schema extends AbstractPdoSchema
7878
/** @deprecated Use {@see ColumnBuilder} instead. Will be removed in 2.0. */
7979
public function createColumn(string $type, array|int|string $length = null): ColumnInterface
8080
{
81+
/** @psalm-suppress DeprecatedClass */
8182
return new Column($type, $length);
8283
}
8384

tests/ColumnSchemaBuilderTest.php

Lines changed: 0 additions & 8 deletions
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\Sqlite\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

Lines changed: 0 additions & 19 deletions
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-
30-
$types['uuid'][0] = '`column` blob(16)';
31-
$types['uuid not null'][0] = '`column` blob(16) NOT NULL';
32-
33-
$types['uuid with default'][0] = '`column` blob(16) DEFAULT (UNHEX(REPLACE(\'875343b3-6bd0-4bec-81bb-aa68bb52d945\',\'-\',\'\')))';
34-
$types['uuid with default'][3] = [['defaultExpression', '(UNHEX(REPLACE(\'875343b3-6bd0-4bec-81bb-aa68bb52d945\',\'-\',\'\')))']];
35-
36-
$types['uuid pk'][0] = '`column` blob(16) PRIMARY KEY';
37-
$types['uuid pk not null'][0] = '`column` blob(16) PRIMARY KEY NOT NULL';
38-
39-
$types['uuid pk not null with default'][0] = '`column` blob(16) PRIMARY KEY NOT NULL DEFAULT (RANDOMBLOB(16))';
40-
$types['uuid pk not null with default'][3] = [['notNull'],['defaultExpression', '(RANDOMBLOB(16))']];
41-
42-
return $types;
43-
}
4425
}

tests/Provider/QueryBuilderProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,15 @@ public static function buildColumnDefinition(): array
267267
$values[PseudoType::UPK][0] = 'integer PRIMARY KEY AUTOINCREMENT NOT NULL';
268268
$values[PseudoType::BIGPK][0] = 'integer PRIMARY KEY AUTOINCREMENT NOT NULL';
269269
$values[PseudoType::UBIGPK][0] = 'integer PRIMARY KEY AUTOINCREMENT NOT NULL';
270-
$values[PseudoType::UUID_PK][0] = "blob(16) PRIMARY KEY NOT NULL DEFAULT (unhex(format('%016X', random() & 0xFFFFFFFFFFFF4FFF | 0x4000) || format('%016X', random() & 0xBFFFFFFFFFFFFFFF | 0xB000000000000000)))";
271-
$values[PseudoType::UUID_PK_SEQ][0] = "blob(16) PRIMARY KEY NOT NULL DEFAULT (unhex(format('%016X', random() & 0xFFFFFFFFFFFF4FFF | 0x4000) || format('%016X', random() & 0xBFFFFFFFFFFFFFFF | 0xB000000000000000)))";
270+
$values[PseudoType::UUID_PK][0] = 'blob(16) PRIMARY KEY NOT NULL DEFAULT (randomblob(16))';
271+
$values[PseudoType::UUID_PK_SEQ][0] = 'blob(16) PRIMARY KEY NOT NULL DEFAULT (randomblob(16))';
272272
$values['primaryKey()'][0] = 'integer PRIMARY KEY AUTOINCREMENT NOT NULL';
273273
$values['primaryKey(false)'][0] = 'integer PRIMARY KEY NOT NULL';
274274
$values['smallPrimaryKey()'][0] = 'integer PRIMARY KEY AUTOINCREMENT NOT NULL';
275275
$values['smallPrimaryKey(false)'][0] = 'smallint PRIMARY KEY NOT NULL';
276276
$values['bigPrimaryKey()'][0] = 'integer PRIMARY KEY AUTOINCREMENT NOT NULL';
277277
$values['bigPrimaryKey(false)'][0] = 'bigint PRIMARY KEY NOT NULL';
278-
$values['uuidPrimaryKey()'][0] = "blob(16) PRIMARY KEY NOT NULL DEFAULT (unhex(format('%016X', random() & 0xFFFFFFFFFFFF4FFF | 0x4000) || format('%016X', random() & 0xBFFFFFFFFFFFFFFF | 0xB000000000000000)))";
278+
$values['uuidPrimaryKey()'][0] = 'blob(16) PRIMARY KEY NOT NULL DEFAULT (randomblob(16))';
279279
$values['uuidPrimaryKey(false)'][0] = 'blob(16) PRIMARY KEY NOT NULL';
280280
$values['money()'][0] = 'decimal(19,4)';
281281
$values['money(10)'][0] = 'decimal(10,4)';

tests/QueryBuilderTest.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use JsonException;
88
use PHPUnit\Framework\Attributes\DataProviderExternal;
9-
use Yiisoft\Db\Constant\ColumnType;
109
use Yiisoft\Db\Exception\Exception;
1110
use Yiisoft\Db\Exception\InvalidArgumentException;
1211
use Yiisoft\Db\Exception\InvalidConfigException;
@@ -18,7 +17,7 @@
1817
use Yiisoft\Db\Query\QueryInterface;
1918
use Yiisoft\Db\QueryBuilder\Condition\JsonOverlapsCondition;
2019
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
21-
use Yiisoft\Db\Sqlite\Column;
20+
use Yiisoft\Db\Sqlite\Column\ColumnBuilder;
2221
use Yiisoft\Db\Sqlite\Tests\Provider\QueryBuilderProvider;
2322
use Yiisoft\Db\Sqlite\Tests\Support\TestTrait;
2423
use Yiisoft\Db\Tests\Common\CommonQueryBuilderTest;
@@ -32,6 +31,11 @@ final class QueryBuilderTest extends CommonQueryBuilderTest
3231
{
3332
use TestTrait;
3433

34+
public function getBuildColumnDefinitionProvider(): array
35+
{
36+
return QueryBuilderProvider::buildColumnDefinition();
37+
}
38+
3539
/**
3640
* @throws Exception
3741
* @throws InvalidConfigException
@@ -162,20 +166,13 @@ public function testAddUnique(string $name, string $table, array|string $columns
162166
$qb->addUnique($table, $name, $columns);
163167
}
164168

165-
/**
166-
* @throws Exception
167-
* @throws InvalidConfigException
168-
*/
169-
public function testAlterColumn(): void
169+
#[DataProviderExternal(QueryBuilderProvider::class, 'alterColumn')]
170+
public function testAlterColumn(string|ColumnSchemaInterface $type, string $expected): void
170171
{
171-
$db = $this->getConnection();
172-
173-
$qb = $db->getQueryBuilder();
174-
175172
$this->expectException(NotSupportedException::class);
176173
$this->expectExceptionMessage('Yiisoft\Db\Sqlite\DDLQueryBuilder::alterColumn is not supported by SQLite.');
177174

178-
$qb->alterColumn('customer', 'email', ColumnType::STRING);
175+
parent::testAlterColumn($type, $expected);
179176
}
180177

181178
/**
@@ -753,16 +750,16 @@ public function testUpsertExecute(
753750
public function testJsonColumn()
754751
{
755752
$qb = $this->getConnection()->getQueryBuilder();
756-
$columnSchemaBuilder = new Column(ColumnType::JSON);
753+
$column = ColumnBuilder::json();
757754

758755
$this->assertSame(
759756
'ALTER TABLE `json_table` ADD `json_col` json',
760-
$qb->addColumn('json_table', 'json_col', $columnSchemaBuilder->asString()),
757+
$qb->addColumn('json_table', 'json_col', $column),
761758
);
762759

763760
$this->assertSame(
764761
"CREATE TABLE `json_table` (\n\t`json_col` json\n)",
765-
$qb->createTable('json_table', ['json_col' => $columnSchemaBuilder]),
762+
$qb->createTable('json_table', ['json_col' => $column]),
766763
);
767764

768765
$this->assertSame(

0 commit comments

Comments
 (0)