From 316762545dcef6cece276e527ca1102155582513 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Sun, 17 Nov 2024 20:16:44 +0700 Subject: [PATCH] Refactor `AbstractColumnDefinitionBuilder` (#901) --- CHANGELOG.md | 2 +- .../AbstractColumnDefinitionBuilder.php | 14 ++++---- tests/Provider/QueryBuilderProvider.php | 33 +++++++++---------- .../Support/Stub/ColumnDefinitionBuilder.php | 6 ++-- 4 files changed, 26 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bdae0719c..5b4b4e502 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,7 +41,7 @@ - Enh #878: Realize `ColumnBuilder` class (@Tigrov) - Enh #881: Refactor `ColumnSchemaInterface` and `AbstractColumnSchema` (@Tigrov) - New #882: Move `ArrayColumnSchema` and `StructuredColumnSchema` classes from `db-pgsql` package (@Tigrov) -- New #883: Add `ColumnDefinitionBuilder` class and `QueryBuilderInterface::buildColumnDefinition()` method (@Tigrov) +- New #883, #901: Add `ColumnDefinitionBuilder` class and `QueryBuilderInterface::buildColumnDefinition()` method (@Tigrov) - Enh #885: Refactor `AbstractDsn` class (@Tigrov) - Chg #889: Update `AbstractDMLQueryBuilder::insertBatch()` method (@Tigrov) - Enh #890: Add properties of `AbstractColumnSchema` class to constructor (@Tigrov) diff --git a/src/QueryBuilder/AbstractColumnDefinitionBuilder.php b/src/QueryBuilder/AbstractColumnDefinitionBuilder.php index ac08b2032..dee3a1424 100644 --- a/src/QueryBuilder/AbstractColumnDefinitionBuilder.php +++ b/src/QueryBuilder/AbstractColumnDefinitionBuilder.php @@ -125,9 +125,7 @@ protected function buildDefault(ColumnSchemaInterface $column): string return ' DEFAULT ' . static::GENERATE_UUID_EXPRESSION; } - if ($column->isAutoIncrement() && $column->getType() !== ColumnType::UUID - || !$column->hasDefaultValue() - ) { + if ($column->isAutoIncrement() && $column->getType() !== ColumnType::UUID) { return ''; } @@ -147,6 +145,10 @@ protected function buildDefault(ColumnSchemaInterface $column): string */ protected function buildDefaultValue(ColumnSchemaInterface $column): string|null { + if (!$column->hasDefaultValue()) { + return null; + } + $value = $column->dbTypecast($column->getDefaultValue()); /** @var string */ @@ -274,11 +276,7 @@ protected function buildOnUpdate(string $onUpdate): string */ protected function buildType(ColumnSchemaInterface $column): string { - $dbType = $column->getDbType(); - - if ($dbType === null) { - $dbType = $this->getDbType($column); - } + $dbType = $this->getDbType($column); if (empty($dbType) || $dbType[-1] === ')' diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index c6161d200..4bb133c9a 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -1578,20 +1578,20 @@ public static function buildColumnDefinition(): array $referenceWithSchema->foreignSchemaName('ref_schema'); return [ - PseudoType::PK => ['integer PRIMARY KEY AUTO_INCREMENT', PseudoType::PK], - PseudoType::UPK => ['integer UNSIGNED PRIMARY KEY AUTO_INCREMENT', PseudoType::UPK], - PseudoType::BIGPK => ['bigint PRIMARY KEY AUTO_INCREMENT', PseudoType::BIGPK], - PseudoType::UBIGPK => ['bigint UNSIGNED PRIMARY KEY AUTO_INCREMENT', PseudoType::UBIGPK], + PseudoType::PK => ['integer PRIMARY KEY AUTOINCREMENT', PseudoType::PK], + PseudoType::UPK => ['integer UNSIGNED PRIMARY KEY AUTOINCREMENT', PseudoType::UPK], + PseudoType::BIGPK => ['bigint PRIMARY KEY AUTOINCREMENT', PseudoType::BIGPK], + PseudoType::UBIGPK => ['bigint UNSIGNED PRIMARY KEY AUTOINCREMENT', PseudoType::UBIGPK], PseudoType::UUID_PK => ['uuid PRIMARY KEY DEFAULT uuid()', PseudoType::UUID_PK], PseudoType::UUID_PK_SEQ => ['uuid PRIMARY KEY DEFAULT uuid()', PseudoType::UUID_PK_SEQ], - 'STRING' => ['varchar', ColumnType::STRING], + 'STRING' => ['varchar(255)', ColumnType::STRING], 'STRING(100)' => ['varchar(100)', ColumnType::STRING . '(100)'], - 'primaryKey()' => ['integer PRIMARY KEY AUTO_INCREMENT', ColumnBuilder::primaryKey()], + 'primaryKey()' => ['integer PRIMARY KEY AUTOINCREMENT', ColumnBuilder::primaryKey()], 'primaryKey(false)' => ['integer PRIMARY KEY', ColumnBuilder::primaryKey(false)], - 'smallPrimaryKey()' => ['smallint PRIMARY KEY AUTO_INCREMENT', ColumnBuilder::smallPrimaryKey()], + 'smallPrimaryKey()' => ['smallint PRIMARY KEY AUTOINCREMENT', ColumnBuilder::smallPrimaryKey()], 'smallPrimaryKey(false)' => ['smallint PRIMARY KEY', ColumnBuilder::smallPrimaryKey(false)], - 'bigPrimaryKey()' => ['bigint PRIMARY KEY AUTO_INCREMENT', ColumnBuilder::bigPrimaryKey()], + 'bigPrimaryKey()' => ['bigint PRIMARY KEY AUTOINCREMENT', ColumnBuilder::bigPrimaryKey()], 'bigPrimaryKey(false)' => ['bigint PRIMARY KEY', ColumnBuilder::bigPrimaryKey(false)], 'uuidPrimaryKey()' => ['uuid PRIMARY KEY DEFAULT uuid()', ColumnBuilder::uuidPrimaryKey()], 'uuidPrimaryKey(false)' => ['uuid PRIMARY KEY', ColumnBuilder::uuidPrimaryKey(false)], @@ -1601,7 +1601,7 @@ public static function buildColumnDefinition(): array 'bit()' => ['bit', ColumnBuilder::bit()], 'bit(1)' => ['bit(1)', ColumnBuilder::bit(1)], 'bit(8)' => ['bit(8)', ColumnBuilder::bit(8)], - 'bit(1000)' => ['bit(1000)', ColumnBuilder::bit(1000)], + 'bit(64)' => ['bit(64)', ColumnBuilder::bit(64)], 'tinyint()' => ['tinyint', ColumnBuilder::tinyint()], 'tinyint(2)' => ['tinyint(2)', ColumnBuilder::tinyint(2)], 'smallint()' => ['smallint', ColumnBuilder::smallint()], @@ -1629,7 +1629,7 @@ public static function buildColumnDefinition(): array 'char(null)' => ['char', ColumnBuilder::char(null)], 'string()' => ['varchar(255)', ColumnBuilder::string()], 'string(100)' => ['varchar(100)', ColumnBuilder::string(100)], - 'string(null)' => ['varchar', ColumnBuilder::string(null)], + 'string(null)' => ['varchar(255)', ColumnBuilder::string(null)], 'text()' => ['text', ColumnBuilder::text()], 'text(1000)' => ['text(1000)', ColumnBuilder::text(1000)], 'binary()' => ['binary', ColumnBuilder::binary()], @@ -1648,23 +1648,22 @@ public static function buildColumnDefinition(): array 'time(null)' => ['time', ColumnBuilder::time(null)], 'array()' => ['json', ColumnBuilder::array()], 'structured()' => ['json', ColumnBuilder::structured()], - "structured('structured_type')" => ['structured_type', ColumnBuilder::structured('structured_type')], + "structured('json')" => ['json', ColumnBuilder::structured('json')], 'json()' => ['json', ColumnBuilder::json()], 'json(100)' => ['json', ColumnBuilder::json()->size(100)], - "enum('a','b','c')" => ["enum('a','b','c')", ColumnBuilder::string()->dbType("enum('a','b','c')")], - "extra('bar')" => ['varchar(255) bar', ColumnBuilder::string()->extra('bar')], + "extra('NOT NULL')" => ['varchar(255) NOT NULL', ColumnBuilder::string()->extra('NOT NULL')], "extra('')" => ['varchar(255)', ColumnBuilder::string()->extra('')], - "check('value > 5')" => ['varchar(255) CHECK (value > 5)', ColumnBuilder::string()->check('value > 5')], - "check('')" => ['varchar(255)', ColumnBuilder::string()->check('')], - 'check(null)' => ['varchar(255)', ColumnBuilder::string()->check(null)], + "check('value > 5')" => ['integer CHECK ([col_59] > 5)', ColumnBuilder::integer()->check(DbHelper::replaceQuotes('[[col_59]] > 5', static::$driverName))], + "check('')" => ['integer', ColumnBuilder::integer()->check('')], + 'check(null)' => ['integer', ColumnBuilder::integer()->check(null)], "comment('comment')" => ['varchar(255)', ColumnBuilder::string()->comment('comment')], "comment('')" => ['varchar(255)', ColumnBuilder::string()->comment('')], 'comment(null)' => ['varchar(255)', ColumnBuilder::string()->comment(null)], "defaultValue('value')" => ["varchar(255) DEFAULT 'value'", ColumnBuilder::string()->defaultValue('value')], "defaultValue('')" => ["varchar(255) DEFAULT ''", ColumnBuilder::string()->defaultValue('')], 'defaultValue(null)' => ['varchar(255) DEFAULT NULL', ColumnBuilder::string()->defaultValue(null)], - 'defaultValue($expression)' => ['varchar(255) DEFAULT expression', ColumnBuilder::string()->defaultValue(new Expression('expression'))], + 'defaultValue($expression)' => ['integer DEFAULT (1 + 2)', ColumnBuilder::integer()->defaultValue(new Expression('(1 + 2)'))], 'notNull()->defaultValue(null)' => ['varchar(255) NOT NULL', ColumnBuilder::string()->notNull()->defaultValue(null)], "integer()->defaultValue('')" => ['integer DEFAULT NULL', ColumnBuilder::integer()->defaultValue('')], 'notNull()' => ['varchar(255) NOT NULL', ColumnBuilder::string()->notNull()], diff --git a/tests/Support/Stub/ColumnDefinitionBuilder.php b/tests/Support/Stub/ColumnDefinitionBuilder.php index 199943ef6..07d137477 100644 --- a/tests/Support/Stub/ColumnDefinitionBuilder.php +++ b/tests/Support/Stub/ColumnDefinitionBuilder.php @@ -10,7 +10,7 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder { - protected const AUTO_INCREMENT_KEYWORD = 'AUTO_INCREMENT'; + protected const AUTO_INCREMENT_KEYWORD = 'AUTOINCREMENT'; protected const GENERATE_UUID_EXPRESSION = 'uuid()'; @@ -40,7 +40,7 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder protected function getDbType(ColumnSchemaInterface $column): string { - return match ($column->getType()) { + return $column->getDbType() ?? match ($column->getType()) { ColumnType::BOOLEAN => 'boolean', ColumnType::BIT => 'bit', ColumnType::TINYINT => 'tinyint', @@ -52,7 +52,7 @@ protected function getDbType(ColumnSchemaInterface $column): string ColumnType::DECIMAL => 'decimal', ColumnType::MONEY => 'money', ColumnType::CHAR => 'char', - ColumnType::STRING => 'varchar', + ColumnType::STRING => 'varchar(' . ($column->getSize() ?? 255) . ')', ColumnType::TEXT => 'text', ColumnType::BINARY => 'binary', ColumnType::UUID => 'uuid',