From 511e5c1b548040f60b2a02b1a884d60663ade713 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sun, 17 Nov 2024 12:58:50 +0700 Subject: [PATCH] Refactor `ColumnDefinitionBuilder` --- src/Column/ColumnDefinitionBuilder.php | 7 +++++-- tests/Provider/QueryBuilderProvider.php | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) 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';