Skip to content

Commit

Permalink
Refactor ColumnDefinitionBuilder (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Nov 17, 2024
1 parent 989e887 commit e3fad7b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- Enh #354: Separate column type constants (@Tigrov)
- New #355: Realize `ColumnBuilder` class (@Tigrov)
- Enh #357: Update according changes in `ColumnSchemaInterface` (@Tigrov)
- New #358: Add `ColumnDefinitionBuilder` class (@Tigrov)
- New #358, #365: Add `ColumnDefinitionBuilder` class (@Tigrov)
- Enh #359: Refactor `Dsn` class (@Tigrov)
- Enh #361, #362: Refactor `Schema::findColumns()` method (@Tigrov)
- Enh #363: Refactor `Schema::normalizeDefaultValue()` method and move it to `ColumnFactory` class (@Tigrov)
Expand Down
12 changes: 9 additions & 3 deletions src/Column/ColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder
{
protected const AUTO_INCREMENT_KEYWORD = 'AUTO_INCREMENT';

protected const GENERATE_UUID_EXPRESSION = 'uuid_to_bin(uuid())';
protected const GENERATE_UUID_EXPRESSION = "unhex(replace(uuid(),'-',''))";

protected const TYPES_WITH_SIZE = [
'bit',
Expand Down Expand Up @@ -70,7 +70,7 @@ protected function buildComment(ColumnSchemaInterface $column): string
protected function getDbType(ColumnSchemaInterface $column): string
{
/** @psalm-suppress DocblockTypeContradiction */
return match ($column->getType()) {
$dbType = $column->getDbType() ?? match ($column->getType()) {
ColumnType::BOOLEAN => 'bit(1)',
ColumnType::BIT => 'bit',
ColumnType::TINYINT => 'tinyint',
Expand All @@ -82,7 +82,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 => 'binary(16)',
Expand All @@ -95,5 +95,11 @@ protected function getDbType(ColumnSchemaInterface $column): string
ColumnType::JSON => 'json',
default => 'varchar',
};

if ($dbType === 'double' && $column->getSize() !== null) {
return 'double(' . $column->getSize() . ',' . ($column->getScale() ?? 0) . ')';
}

return $dbType;
}
}
14 changes: 11 additions & 3 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Expression\JsonExpression;
use Yiisoft\Db\Mysql\Column\ColumnBuilder;
use Yiisoft\Db\Mysql\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;

Expand Down Expand Up @@ -184,25 +185,30 @@ public static function buildColumnDefinition(): array
$values[PseudoType::UPK][0] = 'int UNSIGNED PRIMARY KEY AUTO_INCREMENT';
$values[PseudoType::BIGPK][0] = 'bigint PRIMARY KEY AUTO_INCREMENT';
$values[PseudoType::UBIGPK][0] = 'bigint UNSIGNED PRIMARY KEY AUTO_INCREMENT';
$values[PseudoType::UUID_PK][0] = 'binary(16) PRIMARY KEY DEFAULT uuid_to_bin(uuid())';
$values[PseudoType::UUID_PK_SEQ][0] = 'binary(16) PRIMARY KEY DEFAULT uuid_to_bin(uuid())';
$values[PseudoType::UUID_PK][0] = "binary(16) PRIMARY KEY DEFAULT unhex(replace(uuid(),'-',''))";
$values[PseudoType::UUID_PK_SEQ][0] = "binary(16) PRIMARY KEY DEFAULT unhex(replace(uuid(),'-',''))";
$values['primaryKey()'][0] = 'int PRIMARY KEY AUTO_INCREMENT';
$values['primaryKey(false)'][0] = 'int PRIMARY KEY';
$values['smallPrimaryKey()'][0] = 'smallint PRIMARY KEY AUTO_INCREMENT';
$values['bigPrimaryKey()'][0] = 'bigint PRIMARY KEY AUTO_INCREMENT';
$values['uuidPrimaryKey()'][0] = 'binary(16) PRIMARY KEY DEFAULT uuid_to_bin(uuid())';
$values['uuidPrimaryKey()'][0] = "binary(16) PRIMARY KEY DEFAULT unhex(replace(uuid(),'-',''))";
$values['uuidPrimaryKey(false)'][0] = 'binary(16) PRIMARY KEY';
$values['boolean()'][0] = 'bit(1)';
$values['boolean(100)'][0] = 'bit(1)';
$values['integer()'][0] = 'int';
$values['integer(8)'][0] = 'int(8)';
$values['double(10)'][0] = 'double(10,0)';
$values['money()'][0] = 'decimal(19,4)';
$values['money(10)'][0] = 'decimal(10,4)';
$values['money(10,2)'][0] = 'decimal(10,2)';
$values['money(null)'][0] = 'decimal';
$values['binary()'][0] = 'blob';
$values['binary(1000)'][0] = 'blob(1000)';
$values['uuid()'][0] = 'binary(16)';
$values["check('value > 5')"][0] = 'int CHECK (`col_59` > 5)';
$values["check('')"][0] = 'int';
$values['check(null)'][0] = 'int';
$values['defaultValue($expression)'][0] = 'int DEFAULT (1 + 2)';
$values["comment('comment')"][0] = "varchar(255) COMMENT 'comment'";
$values["comment('')"][0] = "varchar(255) COMMENT ''";
$values['integer()->primaryKey()'][0] = 'int PRIMARY KEY';
Expand All @@ -212,6 +218,8 @@ public static function buildColumnDefinition(): array
$values['reference($reference)'][0] = 'int REFERENCES `ref_table` (`id`) ON DELETE CASCADE ON UPDATE CASCADE';
$values['reference($referenceWithSchema)'][0] = 'int REFERENCES `ref_schema`.`ref_table` (`id`) ON DELETE CASCADE ON UPDATE CASCADE';

$values[] = ["enum('a','b','c')", ColumnBuilder::string()->dbType("enum('a','b','c')")];

return $values;
}
}

0 comments on commit e3fad7b

Please sign in to comment.