Skip to content

Commit

Permalink
Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Nov 15, 2024
1 parent 11f2bb1 commit f55b3b1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
13 changes: 8 additions & 5 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->getDefaultValue() === null
) {
if ($column->isAutoIncrement() && $column->getType() !== ColumnType::UUID) {
return '';
}

Expand All @@ -149,7 +147,7 @@ protected function buildDefaultValue(ColumnSchemaInterface $column): string|null
{
$value = $column->dbTypecast($column->getDefaultValue());

if ($value === null) {
if ($value === null && (!$column->hasDefaultValue() || $column->isNotNull())) {
return null;
}

Expand All @@ -162,6 +160,7 @@ protected function buildDefaultValue(ColumnSchemaInterface $column): string|null
GettypeResult::INTEGER => (string) $value,
GettypeResult::DOUBLE => (string) $value,
GettypeResult::BOOLEAN => $value ? 'TRUE' : 'FALSE',
GettypeResult::NULL => 'NULL',
default => $this->queryBuilder->quoter()->quoteValue((string) $value),
};
}
Expand All @@ -186,7 +185,11 @@ protected function buildExtra(ColumnSchemaInterface $column): string
*/
protected function buildNotNull(ColumnSchemaInterface $column): string
{
return $column->isNotNull() ? ' NOT NULL' : '';
return match ($column->isNotNull()) {
true => ' NOT NULL',
false => ' NULL',
default => '',
};
}

/**
Expand Down
6 changes: 4 additions & 2 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -1663,10 +1663,12 @@ public static function buildColumnDefinition(): array
'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)', ColumnBuilder::string()->defaultValue(null)],
'defaultValue(null)' => ['varchar(255) DEFAULT NULL', ColumnBuilder::string()->defaultValue(null)],
'defaultValue($expression)' => ['varchar(255) DEFAULT expression', ColumnBuilder::string()->defaultValue(new Expression('expression'))],
"integer()->defaultValue('')" => ['integer', ColumnBuilder::integer()->defaultValue('')],
'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()],
'null()' => ['varchar(255) NULL', ColumnBuilder::string()->null()],
'integer()->primaryKey()' => ['integer PRIMARY KEY', ColumnBuilder::integer()->primaryKey()],
'size(10)' => ['varchar(10)', ColumnBuilder::string()->size(10)],
'unique()' => ['varchar(255) UNIQUE', ColumnBuilder::string()->unique()],
Expand Down

0 comments on commit f55b3b1

Please sign in to comment.