Skip to content

Commit

Permalink
Refactor ColumnDefinitionBuilder (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Nov 17, 2024
1 parent cc707f8 commit 51919cb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
- Enh #317: Separate column type constants (@Tigrov)
- New #318: Realize `ColumnBuilder` class (@Tigrov)
- Enh #320: Update according changes in `ColumnSchemaInterface` (@Tigrov)
- New #322: Add `ColumnDefinitionBuilder` class (@Tigrov)
- New #322, #327: Add `ColumnDefinitionBuilder` class (@Tigrov)
- Enh #323: Refactor `Dsn` class (@Tigrov)
- Enh #324: Set more specific result type in `Connection` methods `createCommand()` and `createTransaction()` (@vjik)
- Enh #326: Refactor `Schema::normalizeDefaultValue()` method and move it to `ColumnFactory` class (@Tigrov)
Expand Down
7 changes: 5 additions & 2 deletions src/Column/ColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder
{
protected const AUTO_INCREMENT_KEYWORD = 'AUTOINCREMENT';

protected const GENERATE_UUID_EXPRESSION =
"(unhex(format('%016X', random() & 0xFFFFFFFFFFFF4FFF | 0x4000) || format('%016X', random() & 0xBFFFFFFFFFFFFFFF | 0xB000000000000000)))";

protected const TYPES_WITH_SIZE = [
'bit',
'tinyint',
Expand Down Expand Up @@ -71,7 +74,7 @@ protected function buildNotNull(ColumnSchemaInterface $column): string
protected function getDbType(ColumnSchemaInterface $column): string
{
/** @psalm-suppress DocblockTypeContradiction */
return match ($column->getType()) {
return $column->getDbType() ?? match ($column->getType()) {
ColumnType::BOOLEAN => 'boolean',
ColumnType::BIT => 'bit',
ColumnType::TINYINT => $column->isAutoIncrement() ? 'integer' : 'tinyint',
Expand All @@ -83,7 +86,7 @@ protected function getDbType(ColumnSchemaInterface $column): string
ColumnType::DECIMAL => 'decimal',
ColumnType::MONEY => 'decimal',
ColumnType::CHAR => 'char',
ColumnType::STRING => 'varchar',
ColumnType::STRING => 'varchar(' . ($column->getSize() ?? 255) . ')',
ColumnType::TEXT => 'text',
ColumnType::BINARY => 'blob',
ColumnType::UUID => 'blob(16)',
Expand Down
7 changes: 4 additions & 3 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,15 @@ public static function buildColumnDefinition(): array
$values[PseudoType::UPK][0] = 'integer PRIMARY KEY AUTOINCREMENT NOT NULL';
$values[PseudoType::BIGPK][0] = 'integer PRIMARY KEY AUTOINCREMENT NOT NULL';
$values[PseudoType::UBIGPK][0] = 'integer PRIMARY KEY AUTOINCREMENT NOT NULL';
$values[PseudoType::UUID_PK][0] = 'blob(16) PRIMARY KEY NOT NULL';
$values[PseudoType::UUID_PK_SEQ][0] = 'blob(16) PRIMARY KEY NOT NULL';
$values[PseudoType::UUID_PK][0] = "blob(16) PRIMARY KEY NOT NULL DEFAULT (unhex(format('%016X', random() & 0xFFFFFFFFFFFF4FFF | 0x4000) || format('%016X', random() & 0xBFFFFFFFFFFFFFFF | 0xB000000000000000)))";
$values[PseudoType::UUID_PK_SEQ][0] = "blob(16) PRIMARY KEY NOT NULL DEFAULT (unhex(format('%016X', random() & 0xFFFFFFFFFFFF4FFF | 0x4000) || format('%016X', random() & 0xBFFFFFFFFFFFFFFF | 0xB000000000000000)))";
$values['primaryKey()'][0] = 'integer PRIMARY KEY AUTOINCREMENT NOT NULL';
$values['primaryKey(false)'][0] = 'integer PRIMARY KEY NOT NULL';
$values['smallPrimaryKey()'][0] = 'integer PRIMARY KEY AUTOINCREMENT NOT NULL';
$values['smallPrimaryKey(false)'][0] = 'smallint PRIMARY KEY NOT NULL';
$values['bigPrimaryKey()'][0] = 'integer PRIMARY KEY AUTOINCREMENT NOT NULL';
$values['bigPrimaryKey(false)'][0] = 'bigint PRIMARY KEY NOT NULL';
$values['uuidPrimaryKey()'][0] = 'blob(16) PRIMARY KEY NOT NULL';
$values['uuidPrimaryKey()'][0] = "blob(16) PRIMARY KEY NOT NULL DEFAULT (unhex(format('%016X', random() & 0xFFFFFFFFFFFF4FFF | 0x4000) || format('%016X', random() & 0xBFFFFFFFFFFFFFFF | 0xB000000000000000)))";
$values['uuidPrimaryKey(false)'][0] = 'blob(16) PRIMARY KEY NOT NULL';
$values['money()'][0] = 'decimal(19,4)';
$values['money(10)'][0] = 'decimal(10,4)';
Expand All @@ -284,6 +284,7 @@ public static function buildColumnDefinition(): array
$values['binary()'][0] = 'blob';
$values['binary(1000)'][0] = 'blob(1000)';
$values['uuid()'][0] = 'blob(16)';
$values["check('value > 5')"][0] = 'integer CHECK (`col_59` > 5)';
$values["comment('comment')"][0] = 'varchar(255) /* comment */';
$values['integer()->primaryKey()'][0] = 'integer PRIMARY KEY NOT NULL';
$values['unsigned()'][0] = 'integer';
Expand Down

0 comments on commit 51919cb

Please sign in to comment.