Skip to content

Commit

Permalink
Refactor ColumnSchemaInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Sep 20, 2024
1 parent c765ca4 commit 0a01e1c
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 55 deletions.
95 changes: 82 additions & 13 deletions src/Schema/Column/AbstractColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\PhpType;

use Yiisoft\Db\Constraint\ForeignKeyConstraint;

use function is_array;

/**
* Represents the metadata of a column in a database table.
*
* It provides information about the column's name, type, size, precision, and other details.
* It provides information about the column's type, size, scale, and other details.
*
* The `ColumnSchema` class is used to store and retrieve metadata about a column in a database table.
*
Expand All @@ -25,19 +27,18 @@
* use Yiisoft\Db\Schema\ColumnSchema;
*
* $column = (new IntegerColumnSchema())
* ->name('id')
* ->allowNull(false)
* ->dbType('int(11)')
* ->type('integer')
* ->notNull()
* ->dbType('int')
* ->size(11)
* ->defaultValue(0)
* ->autoIncrement()
* ->primaryKey();
* ```
*/
abstract class AbstractColumnSchema implements ColumnSchemaInterface
{
private bool $allowNull = false;
private bool $autoIncrement = false;
private string|null $check = null;
private string|null $comment = null;
private bool $computed = false;
private string|null $dbType = null;
Expand All @@ -46,9 +47,11 @@ abstract class AbstractColumnSchema implements ColumnSchemaInterface
private string|null $extra = null;
private bool $isPrimaryKey = false;
private string|null $name = null;
private int|null $precision = null;
private bool $notNull = false;
private ForeignKeyConstraint|null $reference = null;
private int|null $scale = null;
private int|null $size = null;
private bool $unique = false;
private bool $unsigned = false;

/**
Expand All @@ -59,9 +62,12 @@ public function __construct(
) {
}

/**
* @deprecated Use {@see notNull()} instead. Will be removed in version 2.0.
*/
public function allowNull(bool $allowNull = true): static
{
$this->allowNull = $allowNull;
$this->notNull(!$allowNull);
return $this;
}

Expand All @@ -71,6 +77,12 @@ public function autoIncrement(bool $autoIncrement = true): static
return $this;
}

public function check(string|null $check): static
{
$this->check = $check;
return $this;
}

public function comment(string|null $comment): static
{
$this->comment = $comment;
Expand Down Expand Up @@ -107,6 +119,11 @@ public function extra(string|null $extra): static
return $this;
}

public function getCheck(): string|null
{
return $this->check;
}

public function getComment(): string|null
{
return $this->comment;
Expand All @@ -132,21 +149,32 @@ public function getExtra(): string|null
return $this->extra;
}

/**
* @deprecated Will be removed in version 2.0.
*/
public function getName(): string|null
{
return $this->name;
}

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

public function getPhpType(): string
{
return PhpType::MIXED;
}

public function getReference(): ForeignKeyConstraint|null
{
return $this->reference;
}

public function getScale(): int|null
{
return $this->scale;
Expand All @@ -162,9 +190,12 @@ public function getType(): string
return $this->type;
}

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

public function isAutoIncrement(): bool
Expand All @@ -177,11 +208,21 @@ public function isComputed(): bool
return $this->computed;
}

public function isNotNull(): bool
{
return $this->notNull;
}

public function isPrimaryKey(): bool
{
return $this->isPrimaryKey;
}

public function isUnique(): bool
{
return $this->unique;
}

public function isUnsigned(): bool
{
return $this->unsigned;
Expand All @@ -192,22 +233,27 @@ public function load(array $info): static
foreach ($info as $key => $value) {
/**
* @psalm-suppress PossiblyInvalidCast
* @psalm-suppress RiskyCast
* @psalm-suppress InvalidCast
* @psalm-suppress DeprecatedMethod
*/
match ($key) {
'allow_null' => $this->allowNull((bool) $value),
'auto_increment' => $this->autoIncrement((bool) $value),
'check' => $this->check($value !== null ? (string) $value : null),
'comment' => $this->comment($value !== null ? (string) $value : null),
'computed' => $this->computed((bool) $value),
'db_type' => $this->dbType($value !== null ? (string) $value : null),
'default_value' => $this->defaultValue($value),
'enum_values' => $this->enumValues(is_array($value) ? $value : null),
'extra' => $this->extra($value !== null ? (string) $value : null),
'name' => $this->name($value !== null ? (string) $value : null),
'not_null' => $this->notNull((bool) $value),
'primary_key' => $this->primaryKey((bool) $value),
'precision' => $this->precision($value !== null ? (int) $value : null),
'reference' => $this->reference($value instanceof ForeignKeyConstraint ? $value : null),
'scale' => $this->scale($value !== null ? (int) $value : null),
'size' => $this->size($value !== null ? (int) $value : null),
'unique' => $this->unique((bool) $value),
'unsigned' => $this->unsigned((bool) $value),
default => null,
};
Expand All @@ -216,24 +262,41 @@ public function load(array $info): static
return $this;
}

/**
* @deprecated Will be removed in version 2.0.
*/
public function name(string|null $name): static
{
$this->name = $name;
return $this;
}

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

/**
* @deprecated Use {@see size()} instead. Will be removed in version 2.0.
*/
public function precision(int|null $precision): static
{
return $this->size($precision);
}

public function primaryKey(bool $isPrimaryKey = true): static
{
$this->isPrimaryKey = $isPrimaryKey;
return $this;
}

public function reference(ForeignKeyConstraint|null $reference): static
{
$this->reference = $reference;
return $this;
}

public function scale(int|null $scale): static
{
$this->scale = $scale;
Expand All @@ -252,6 +315,12 @@ public function type(string $type): static
return $this;
}

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

public function unsigned(bool $unsigned = true): static
{
$this->unsigned = $unsigned;
Expand Down
12 changes: 4 additions & 8 deletions src/Schema/Column/ColumnBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public static function primaryKey(bool $autoIncrement = true): ColumnSchemaInter
{
return static::integer()
->primaryKey()
->autoIncrement($autoIncrement)
->allowNull(false);
->autoIncrement($autoIncrement);
}

/**
Expand All @@ -32,8 +31,7 @@ public static function smallPrimaryKey(bool $autoIncrement = true): ColumnSchema
{
return static::smallint()
->primaryKey()
->autoIncrement($autoIncrement)
->allowNull(false);
->autoIncrement($autoIncrement);
}

/**
Expand All @@ -43,8 +41,7 @@ public static function bigPrimaryKey(bool $autoIncrement = true): ColumnSchemaIn
{
return static::bigint()
->primaryKey()
->autoIncrement($autoIncrement)
->allowNull(false);
->autoIncrement($autoIncrement);
}

/**
Expand All @@ -54,8 +51,7 @@ public static function uuidPrimaryKey(bool $autoIncrement = false): ColumnSchema
{
return static::uuid()
->primaryKey()
->autoIncrement($autoIncrement)
->allowNull(false);
->autoIncrement($autoIncrement);
}

// Abstract type column builders
Expand Down
Loading

0 comments on commit 0a01e1c

Please sign in to comment.