Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor AbstractColumnDefinitionBuilder #901

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 6 additions & 8 deletions src/QueryBuilder/AbstractColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}

Expand All @@ -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 */
Expand Down Expand Up @@ -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] === ')'
Expand Down
33 changes: 16 additions & 17 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
Expand All @@ -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()],
Expand Down Expand Up @@ -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()],
Expand All @@ -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()],
Expand Down
6 changes: 3 additions & 3 deletions tests/Support/Stub/ColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()';

Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down