From 51919cbba6a735967b41b7266c8575b8b7ac96cd Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Sun, 17 Nov 2024 20:16:53 +0700 Subject: [PATCH] Refactor `ColumnDefinitionBuilder` (#327) --- CHANGELOG.md | 2 +- src/Column/ColumnDefinitionBuilder.php | 7 +++++-- tests/Provider/QueryBuilderProvider.php | 7 ++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a43ba890..bde18788 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/Column/ColumnDefinitionBuilder.php b/src/Column/ColumnDefinitionBuilder.php index b0534999..0f2cb892 100644 --- a/src/Column/ColumnDefinitionBuilder.php +++ b/src/Column/ColumnDefinitionBuilder.php @@ -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', @@ -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', @@ -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)', diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 2e050c11..c3f89526 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -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)'; @@ -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';