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

Improve column schema classes #899

Merged
merged 5 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
- Enh #885: Refactor `AbstractDsn` class (@Tigrov)
- Chg #889: Update `AbstractDMLQueryBuilder::insertBatch()` method (@Tigrov)
- Enh #890: Add properties of `AbstractColumnSchema` class to constructor (@Tigrov)
- New #899: Add `ColumnSchemaInterface::hasDefaultValue()` and `ColumnSchemaInterface::null()` methods (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
2 changes: 2 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ and the following changes were made:
- `reference(ForeignKeyConstraint|null $reference)` method is added;
- `getReference()` method is added;
- `notNull(bool $notNull = true)` method is added;
- `null()` method is added;
- `isNotNull()` method is added;
- `unique(bool $unique = true)` method is added;
- `isUnique()` method is added;
- `hasDefaultValue()` method is added;
- all `AbstractColumnSchema` class properties except `$type` moved to constructor;
- added `DEFAULT_TYPE` constant to `AbstractColumnSchema` class;
- added method chaining.
Expand Down
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
56 changes: 48 additions & 8 deletions src/Schema/Column/AbstractColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\PhpType;

use Yiisoft\Db\Constraint\ForeignKeyConstraint;

use function array_key_exists;
use function property_exists;

/**
Expand Down Expand Up @@ -43,6 +43,11 @@ abstract class AbstractColumnSchema implements ColumnSchemaInterface
*/
protected const DEFAULT_TYPE = ColumnType::STRING;

/**
* @var mixed $defaultValue The default value of the column.
*/
private mixed $defaultValue;

/**
* @var string The column abstract type
* @psalm-var ColumnType::*
Expand All @@ -56,12 +61,11 @@ abstract class AbstractColumnSchema implements ColumnSchemaInterface
* @param string|null $comment The column's comment.
* @param bool $computed Whether the column is a computed column.
* @param string|null $dbType The column's database type.
* @param mixed $defaultValue The default value of the column.
* @param array|null $enumValues The list of possible values for an ENUM column.
* @param string|null $extra Any extra information that needs to be appended to the column's definition.
* @param bool $primaryKey Whether the column is a primary key.
* @param string|null $name The column's name.
* @param bool $notNull Whether the column is not nullable.
* @param bool|null $notNull Whether the column is not nullable.
* @param ForeignKeyConstraint|null $reference The foreign key constraint.
* @param int|null $scale The number of digits to the right of the decimal point.
* @param int|null $size The column's size.
Expand All @@ -79,12 +83,11 @@ public function __construct(
private string|null $comment = null,
private bool $computed = false,
private string|null $dbType = null,
private mixed $defaultValue = null,
private array|null $enumValues = null,
private string|null $extra = null,
private bool $primaryKey = false,
private string|null $name = null,
private bool $notNull = false,
private bool|null $notNull = null,
private ForeignKeyConstraint|null $reference = null,
private int|null $scale = null,
private int|null $size = null,
Expand All @@ -94,6 +97,11 @@ public function __construct(
) {
$this->type = $type ?? static::DEFAULT_TYPE;

if (array_key_exists('defaultValue', $args)) {
$this->defaultValue = $args['defaultValue'];
unset($args['defaultValue']);
}

/** @var array<string, mixed> $args */
foreach ($args as $property => $value) {
if (property_exists($this, $property)) {
Expand Down Expand Up @@ -159,38 +167,45 @@ public function extra(string|null $extra): static
return $this;
}

/** @psalm-mutation-free */
public function getCheck(): string|null
{
return $this->check;
}

/** @psalm-mutation-free */
public function getComment(): string|null
{
return $this->comment;
}

/** @psalm-mutation-free */
public function getDbType(): string|null
{
return $this->dbType;
}

/** @psalm-mutation-free */
public function getDefaultValue(): mixed
{
return $this->defaultValue;
return $this->defaultValue ?? null;
}

/** @psalm-mutation-free */
public function getEnumValues(): array|null
{
return $this->enumValues;
}

/** @psalm-mutation-free */
public function getExtra(): string|null
{
return $this->extra;
}

/**
* @deprecated Will be removed in version 2.0.
* @psalm-mutation-free
*/
public function getName(): string|null
{
Expand All @@ -199,70 +214,89 @@ public function getName(): string|null

/**
* @deprecated Use {@see getSize()} instead. Will be removed in version 2.0.
* @psalm-mutation-free
*/
public function getPrecision(): int|null
{
return $this->getSize();
}

/** @psalm-mutation-free */
public function getPhpType(): string
{
return PhpType::MIXED;
}

/** @psalm-mutation-free */
public function getReference(): ForeignKeyConstraint|null
{
return $this->reference;
}

/** @psalm-mutation-free */
public function getScale(): int|null
{
return $this->scale;
}

/** @psalm-mutation-free */
public function getSize(): int|null
{
return $this->size;
}

/** @psalm-mutation-free */
public function getType(): string
{
return $this->type;
}

/** @psalm-mutation-free */
public function hasDefaultValue(): bool
{
return property_exists($this, 'defaultValue');
}

/**
* @deprecated Use {@see isNotNull()} instead. Will be removed in version 2.0.
* @psalm-mutation-free
*/
public function isAllowNull(): bool
{
return !$this->isNotNull();
}

/** @psalm-mutation-free */
public function isAutoIncrement(): bool
{
return $this->autoIncrement;
}

/** @psalm-mutation-free */
public function isComputed(): bool
{
return $this->computed;
}

public function isNotNull(): bool
/** @psalm-mutation-free */
public function isNotNull(): bool|null
{
return $this->notNull;
}

/** @psalm-mutation-free */
public function isPrimaryKey(): bool
{
return $this->primaryKey;
}

/** @psalm-mutation-free */
public function isUnique(): bool
{
return $this->unique;
}

/** @psalm-mutation-free */
public function isUnsigned(): bool
{
return $this->unsigned;
Expand All @@ -277,12 +311,18 @@ public function name(string|null $name): static
return $this;
}

public function notNull(bool $notNull = true): static
public function notNull(bool|null $notNull = true): static
{
$this->notNull = $notNull;
return $this;
}

public function null(): static
{
$this->notNull = false;
return $this;
}

/**
* @deprecated Use {@see size()} instead. Will be removed in version 2.0.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Schema/Column/ArrayColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ public function dimension(int $dimension): static
* @return int the dimension of the array.
*
* @psalm-return positive-int
* @psalm-mutation-free
*/
public function getDimension(): int
{
return $this->dimension;
}

/** @psalm-mutation-free */
public function getPhpType(): string
{
return PhpType::ARRAY;
Expand Down
1 change: 1 addition & 0 deletions src/Schema/Column/BigIntColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function dbTypecast(mixed $value): int|string|ExpressionInterface|null
};
}

/** @psalm-mutation-free */
public function getPhpType(): string
{
return PhpType::STRING;
Expand Down
1 change: 1 addition & 0 deletions src/Schema/Column/BitColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function dbTypecast(mixed $value): int|string|ExpressionInterface|null
};
}

/** @psalm-mutation-free */
public function getPhpType(): string
{
return PhpType::INT;
Expand Down
1 change: 1 addition & 0 deletions src/Schema/Column/BooleanColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function dbTypecast(mixed $value): bool|ExpressionInterface|null
};
}

/** @psalm-mutation-free */
public function getPhpType(): string
{
return PhpType::BOOL;
Expand Down
Loading