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

Add method chaining for columns #852

Merged
merged 7 commits into from
Jun 23, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- Chg #845: Remove `AbstractSchema::normalizeRowKeyCase()` method (@Tigrov)
- Chg #846: Remove `SchemaInterface::isReadQuery()` and `AbstractSchema::isReadQuery()` methods (@Tigrov)
- Chg #847: Remove `SchemaInterface::getRawTableName()` and `AbstractSchema::getRawTableName()` methods (@Tigrov)
- Enh #852: Add method chaining for column classes (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
3 changes: 2 additions & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ and the following changes were made:

- `getName()` method can return `string` or `null`;
- `name(string|null $name)` method is added;
- constructor of `AbstractColumnSchema` class is changed to `__construct(string $type, string|null $phpType = null)`.
- constructor of `AbstractColumnSchema` class is changed to `__construct(string $type, string|null $phpType = null)`;
- added method chaining.

### New classes for table columns

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"require-dev": {
"maglnet/composer-require-checker": "^4.2",
"phpunit/phpunit": "^9.6|^10.0",
"rector/rector": "^1.0",
"rector/rector": "^1.1.1",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.30|^5.24",
Expand Down
98 changes: 57 additions & 41 deletions src/Schema/Column/AbstractColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
* ```php
* use Yiisoft\Db\Schema\ColumnSchema;
*
* $column = new ColumnSchema();
* $column->name('id');
* $column->allowNull(false);
* $column->dbType('int(11)');
* $column->phpType('integer');
* $column->type('integer');
* $column->defaultValue(0);
* $column->autoIncrement(true);
* $column->primaryKey(true);
* ``
* $column = (new ColumnSchema())
* ->name('id')
* ->allowNull(false)
* ->dbType('int(11)')
* ->phpType('integer')
* ->type('integer')
* ->defaultValue(0)
* ->autoIncrement()
* ->primaryKey();
* ```
*/
abstract class AbstractColumnSchema implements ColumnSchemaInterface
{
Expand All @@ -53,44 +53,52 @@ public function __construct(
) {
}

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

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

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

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

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

public function defaultValue(mixed $value): void
public function defaultValue(mixed $defaultValue): static
{
$this->defaultValue = $value;
$this->defaultValue = $defaultValue;
return $this;
}

public function enumValues(array|null $value): void
public function enumValues(array|null $enumValues): static
{
$this->enumValues = $value;
$this->enumValues = $enumValues;
return $this;
}

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

public function getComment(): string|null
Expand Down Expand Up @@ -173,43 +181,51 @@ public function isUnsigned(): bool
return $this->unsigned;
}

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

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

public function precision(int|null $value): void
public function precision(int|null $precision): static
{
$this->precision = $value;
$this->precision = $precision;
return $this;
}

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

public function scale(int|null $value): void
public function scale(int|null $scale): static
{
$this->scale = $value;
$this->scale = $scale;
return $this;
}

public function size(int|null $value): void
public function size(int|null $size): static
{
$this->size = $value;
$this->size = $size;
return $this;
}

public function type(string $value): void
public function type(string $type): static
{
$this->type = $value;
$this->type = $type;
return $this;
}

public function unsigned(bool $value): void
public function unsigned(bool $unsigned = true): static
{
$this->unsigned = $value;
$this->unsigned = $unsigned;
return $this;
}
}
52 changes: 26 additions & 26 deletions src/Schema/Column/ColumnSchemaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ interface ColumnSchemaInterface
*
* ```php
* $columns = [
* 'description' => $this->text()->allowNull(true),
* 'description' => $this->text()->allowNull(),
* ];
* ```
*/
public function allowNull(bool $value): void;
public function allowNull(bool $allowNull = true): static;

/**
* The database assigns auto incremented column a unique value automatically whenever you insert a new row into
Expand All @@ -32,11 +32,11 @@ public function allowNull(bool $value): void;
*
* ```php
* $columns = [
* 'id' => $this->primaryKey()->autoIncrement(true),
* 'id' => $this->primaryKey()->autoIncrement(),
* ];
* ```
*/
public function autoIncrement(bool $value): void;
public function autoIncrement(bool $autoIncrement = true): static;

/**
* The comment for a column in a database table.
Expand All @@ -49,7 +49,7 @@ public function autoIncrement(bool $value): void;
* ];
* ```
*/
public function comment(string|null $value): void;
public function comment(string|null $comment): static;

/**
* A computed column is a virtual column that computes its values from an expression.
Expand All @@ -62,13 +62,13 @@ public function comment(string|null $value): void;
* ];
* ```
*/
public function computed(bool $value): void;
public function computed(bool $computed = true): static;

/**
* The database data-type of column.
* Sets a database data type for the column.
*
* The data type can be one of the built-in data types supported by the database server (such as `INTEGER`, `VARCHAR`,
* `DATETIME`, etc.), a custom data type defined by the database server, or `null` if the database
* The data type can be one of the built-in data types supported by the database server (such as `INTEGER`,
* `VARCHAR`, `DATETIME`, etc.), a custom data type defined by the database server, or `null` if the database
* allows untyped columns.
*
* ```php
Expand All @@ -77,7 +77,7 @@ public function computed(bool $value): void;
* ];
* ```
*/
public function dbType(string|null $value): void;
public function dbType(string|null $dbType): static;

/**
* Convert a value from its PHP representation to a database-specific representation.
Expand All @@ -100,7 +100,7 @@ public function dbTypecast(mixed $value): mixed;
* ];
* ```
*/
public function defaultValue(mixed $value): void;
public function defaultValue(mixed $defaultValue): static;

/**
* The list of possible values for the `ENUM` column.
Expand All @@ -111,7 +111,7 @@ public function defaultValue(mixed $value): void;
* ];
* ```
*/
public function enumValues(array|null $value): void;
public function enumValues(array|null $enumValues): static;

/**
* Extra SQL to append to the generated SQL for a column.
Expand All @@ -125,7 +125,7 @@ public function enumValues(array|null $value): void;
* ];
* ```
*/
public function extra(string|null $value): void;
public function extra(string|null $extra): static;

/**
* @return string|null The comment of the column.
Expand All @@ -135,7 +135,7 @@ public function extra(string|null $value): void;
public function getComment(): string|null;

/**
* @return string|null The database type of the column.
* @return string|null The database data type of the column.
* Null means the column has no type in the database.
*
* Note that the type includes size for columns supporting it, e.g. `varchar(128)`. The size can be obtained
Expand Down Expand Up @@ -230,14 +230,14 @@ public function isAutoIncrement(): bool;
public function isComputed(): bool;

/**
* Whether this column is a primary key.
* Whether this column is a part of primary key.
*
* @see primaryKey()
*/
public function isPrimaryKey(): bool;

/**
* Whether this column is unsigned. This is only meaningful when {@see type} is `smallint`, `integer`
* Whether this column is unsigned. This is only meaningful when {@see type} is `tinyint`, `smallint`, `integer`
* or `bigint`.
*
* @see unsigned()
Expand All @@ -253,7 +253,7 @@ public function isUnsigned(): bool;
* ];
* ```
*/
public function name(string|null $name): void;
public function name(string|null $name): static;

/**
* The PHP data type for representing the data stored in the column.
Expand All @@ -270,7 +270,7 @@ public function name(string|null $name): void;
* ];
* ```
*/
public function phpType(string|null $value): void;
public function phpType(string|null $phpType): static;

/**
* Converts the input value according to {@see phpType} after retrieval from the database.
Expand All @@ -288,7 +288,7 @@ public function phpTypecast(mixed $value): mixed;
* 'price' => $this->decimal(10, 2)->precision(10),
* ];
*/
public function precision(int|null $value): void;
public function precision(int|null $precision): static;

/**
* The primary key is a column or set of columns that uniquely identifies each row in a table.
Expand All @@ -299,7 +299,7 @@ public function precision(int|null $value): void;
* ];
* ```
*/
public function primaryKey(bool $value): void;
public function primaryKey(bool $isPrimaryKey = true): static;

/**
* The scale is the number of digits to the right of the decimal point and is only meaningful when {@see type} is
Expand All @@ -311,20 +311,20 @@ public function primaryKey(bool $value): void;
* ];
* ```
*/
public function scale(int|null $value): void;
public function scale(int|null $scale): static;

/**
* The size refers to the number of characters or digits allowed in a column of a database table. The size is
* typically used for character or numeric data types, such as `VARCHAR` or `INT`, to specify the maximum length or
* precision of the data in the column.
* typically used for character or numeric data types, such as `VARCHAR`, `INT` or DECIMAL, to specify the maximum
* length or precision of the data in the column.
*
* ```php
* $columns = [
* 'name' => $this->string()->size(255),
* ];
* ```
*/
public function size(int|null $value): void;
public function size(int|null $size): static;

/**
* The database type of the column.
Expand All @@ -334,7 +334,7 @@ public function size(int|null $value): void;
* 'description' => $this->text()->type('text'),
* ];
*/
public function type(string $value): void;
public function type(string $type): static;

/**
* Whether the column type is an unsigned integer.
Expand All @@ -346,5 +346,5 @@ public function type(string $value): void;
* ];
* ```
*/
public function unsigned(bool $value): void;
public function unsigned(bool $unsigned = true): static;
}
Loading