Skip to content

Commit

Permalink
Remove ColumnInterface (#915)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Jan 8, 2025
1 parent 209c594 commit 8de1111
Show file tree
Hide file tree
Showing 23 changed files with 108 additions and 1,918 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
- Enh #902: Refactor `Quoter::quoteValue()` method (@Tigrov)
- New #906: Add `ServerInfoInterface` and its implementation (@Tigrov)
- Enh #905: Use `AbstractColumnDefinitionBuilder` to generate table column SQL representation (@Tigrov)
- Enh #915: Remove `ColumnInterface` (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
20 changes: 14 additions & 6 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@ application when you upgrade the package from one version to another.
## Upgrade from 1.x to 2.x

### `ColumnInterface` as column type
### Remove `ColumnInterface`

Add `ColumnInterface` support and change type of parameter `$type` from `string` to `ColumnInterface|string`
in `addColumn()` method of your classes that implement the following interfaces:
Remove `ColumnInterface` and use `ColumnSchemaInterface` instead.

### `ColumnSchemaInterface` as column type

Add `ColumnSchemaInterface` support and change type of parameter `$type` to `ColumnSchemaInterface|string`
in the following methods:
- `addColumn()`
- `alterColumn()`

in classes that implement the following interfaces:
- `Yiisoft\Db\Command\CommandInterface`;
- `Yiisoft\Db\QueryBuilder\DDLQueryBuilderInterface`;

… or inherit from the following classes:

or inherit from the following classes:
- `Yiisoft\Db\Command\AbstractCommand`;
- `Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder`;
- `Yiisoft\Db\QueryBuilder\AbstractQueryBuilder`.
Expand Down Expand Up @@ -115,11 +121,13 @@ Each table column has its own class in the `Yiisoft\Db\Schema\Column` namespace

### Remove methods

- `AbstractQueryBuilder::getColumnType()`
- `AbstractDMLQueryBuilder::getTypecastValue()`
- `TableSchemaInterface::compositeForeignKey()`
- `SchemaInterface::createColumn()`
- `SchemaInterface::isReadQuery()`
- `AbstractSchema::isReadQuery()`
- `SchemaInterface::getRawTableName()`
- `AbstractSchema::isReadQuery()`
- `AbstractSchema::getRawTableName()`
- `AbstractSchema::normalizeRowKeyCase()`
- `Quoter::unquoteParts()`
Expand Down
5 changes: 2 additions & 3 deletions src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\QueryBuilder\DMLQueryBuilderInterface;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;

use function explode;
Expand Down Expand Up @@ -132,7 +131,7 @@ public function addCheck(string $table, string $name, string $expression): stati
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}

public function addColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): static
public function addColumn(string $table, string $column, ColumnSchemaInterface|string $type): static
{
$sql = $this->getQueryBuilder()->addColumn($table, $column, $type);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
Expand Down Expand Up @@ -189,7 +188,7 @@ public function addUnique(string $table, string $name, array|string $columns): s
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}

public function alterColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): static
public function alterColumn(string $table, string $column, ColumnSchemaInterface|string $type): static
{
$sql = $this->getQueryBuilder()->alterColumn($table, $column, $type);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
Expand Down
11 changes: 5 additions & 6 deletions src/Command/CommandInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Yiisoft\Db\Query\Data\DataReaderInterface;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\QueryBuilder\DMLQueryBuilderInterface;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;

/**
Expand Down Expand Up @@ -47,13 +46,13 @@ public function addCheck(string $table, string $name, string $expression): stati
*
* @param string $table The name of the table to add new column to.
* @param string $column The name of the new column.
* @param ColumnInterface|ColumnSchemaInterface|string $type The column type.
* @param ColumnSchemaInterface|string $type The column type.
* {@see QueryBuilder::buildColumnDefinition()} will be called to convert the given column type to the database one.
* For example, `string` will be converted to `varchar(255)`, and `string not null` becomes `varchar(255) not null`.
*
* Note: The method will quote the `table` and `column` parameters before using them in the generated SQL.
*/
public function addColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): static;
public function addColumn(string $table, string $column, ColumnSchemaInterface|string $type): static;

/**
* Builds an SQL command for adding a comment to a column.
Expand Down Expand Up @@ -147,13 +146,13 @@ public function addPrimaryKey(string $table, string $name, array|string $columns
*
* @param string $table The table whose column is to change.
* @param string $column The name of the column to change.
* @param ColumnInterface|ColumnSchemaInterface|string $type The column type.
* @param ColumnSchemaInterface|string $type The column type.
* {@see QueryBuilder::buildColumnDefinition()} will be called to convert the give column type to the physical one.
* For example, `string` will be converted as `varchar(255)`, and `string not null` becomes `varchar(255) not null`.
*
* Note: The method will quote the `table` and `column` parameters before using them in the generated SQL.
*/
public function alterColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): static;
public function alterColumn(string $table, string $column, ColumnSchemaInterface|string $type): static;

/**
* Creates a batch INSERT command.
Expand Down Expand Up @@ -326,7 +325,7 @@ public function createIndex(
* into SQL representation. For example, it will convert `string not null` to `varchar(255) not null`
* and `pk` to `int PRIMARY KEY AUTO_INCREMENT` (for MySQL).
*
* The preferred method is to use {@see ColumnBuilder} to generate column definitions as instances of
* The preferred way is to use {@see ColumnBuilder} to generate column definitions as instances of
* {@see ColumnSchemaInterface}.
*
* ```php
Expand Down
5 changes: 2 additions & 3 deletions src/Debug/CommandInterfaceProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Yiisoft\Db\Command\CommandInterface;
use Yiisoft\Db\Query\Data\DataReaderInterface;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;

final class CommandInterfaceProxy implements CommandInterface
Expand All @@ -31,7 +30,7 @@ public function addCheck(string $table, string $name, string $expression): stati
/**
* @psalm-suppress MixedArgument
*/
public function addColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): static
public function addColumn(string $table, string $column, ColumnSchemaInterface|string $type): static
{
return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
}
Expand Down Expand Up @@ -94,7 +93,7 @@ public function addUnique(string $table, string $name, array|string $columns): s
/**
* @psalm-suppress MixedArgument
*/
public function alterColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): static
public function alterColumn(string $table, string $column, ColumnSchemaInterface|string $type): static
{
return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
}
Expand Down
10 changes: 3 additions & 7 deletions src/QueryBuilder/AbstractDDLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
use Yiisoft\Db\Schema\QuoterInterface;
use Yiisoft\Db\Schema\SchemaInterface;
Expand Down Expand Up @@ -40,7 +39,7 @@ public function addCheck(string $table, string $name, string $expression): strin
. ' CHECK (' . $this->quoter->quoteSql($expression) . ')';
}

public function addColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): string
public function addColumn(string $table, string $column, ColumnSchemaInterface|string $type): string
{
return 'ALTER TABLE '
. $this->quoter->quoteTableName($table)
Expand Down Expand Up @@ -134,11 +133,8 @@ public function addUnique(string $table, string $name, array|string $columns): s
. ' UNIQUE (' . implode(', ', $columns) . ')';
}

public function alterColumn(
string $table,
string $column,
ColumnInterface|ColumnSchemaInterface|string $type
): string {
public function alterColumn(string $table, string $column, ColumnSchemaInterface|string $type): string
{
return 'ALTER TABLE '
. $this->quoter->quoteTableName($table)
. ' CHANGE '
Expand Down
41 changes: 3 additions & 38 deletions src/QueryBuilder/AbstractQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\QueryBuilder\Condition\Interface\ConditionInterface;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
use Yiisoft\Db\Schema\QuoterInterface;
use Yiisoft\Db\Schema\SchemaInterface;
Expand All @@ -25,8 +24,6 @@
use function get_resource_type;
use function gettype;
use function is_string;
use function preg_match;
use function preg_replace;
use function stream_get_contents;

/**
Expand Down Expand Up @@ -81,7 +78,7 @@ public function addCheck(string $table, string $name, string $expression): strin
return $this->ddlBuilder->addCheck($table, $name, $expression);
}

public function addColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): string
public function addColumn(string $table, string $column, ColumnSchemaInterface|string $type): string
{
return $this->ddlBuilder->addColumn($table, $column, $type);
}
Expand Down Expand Up @@ -131,7 +128,7 @@ public function addUnique(string $table, string $name, array|string $columns): s
return $this->ddlBuilder->addUnique($table, $name, $columns);
}

public function alterColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): string
public function alterColumn(string $table, string $column, ColumnSchemaInterface|string $type): string
{
return $this->ddlBuilder->alterColumn($table, $column, $type);
}
Expand Down Expand Up @@ -175,12 +172,8 @@ public function build(QueryInterface $query, array $params = []): array
return $this->dqlBuilder->build($query, $params);
}

public function buildColumnDefinition(ColumnInterface|ColumnSchemaInterface|string $column): string
public function buildColumnDefinition(ColumnSchemaInterface|string $column): string
{
if ($column instanceof ColumnInterface) {
$column = $column->asString();
}

if (is_string($column)) {
$column = $this->schema->getColumnFactory()->fromDefinition($column);
}
Expand Down Expand Up @@ -364,34 +357,6 @@ public function getColumnDefinitionBuilder(): ColumnDefinitionBuilderInterface
return $this->columnDefinitionBuilder;
}

/** @deprecated Use {@see buildColumnDefinition()}. Will be removed in version 2.0. */
public function getColumnType(ColumnInterface|string $type): string
{
if ($type instanceof ColumnInterface) {
$type = $type->asString();
}

if (isset($this->typeMap[$type])) {
return $this->typeMap[$type];
}

if (preg_match('/^(\w+)\((.+?)\)(.*)$/', $type, $matches)) {
if (isset($this->typeMap[$matches[1]])) {
return preg_replace(
'/\(.+\)/',
'(' . $matches[2] . ')',
$this->typeMap[$matches[1]]
) . $matches[3];
}
} elseif (preg_match('/^(\w+)\s+/', $type, $matches)) {
if (isset($this->typeMap[$matches[1]])) {
return preg_replace('/^\w+/', $this->typeMap[$matches[1]], $type);
}
}

return $type;
}

public function getExpressionBuilder(ExpressionInterface $expression): object
{
return $this->dqlBuilder->getExpressionBuilder($expression);
Expand Down
41 changes: 25 additions & 16 deletions src/QueryBuilder/DDLQueryBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;

/**
Expand Down Expand Up @@ -37,17 +36,22 @@ public function addCheck(string $table, string $name, string $expression): strin
*
* @param string $table The table to add the new column will to.
* @param string $column The name of the new column.
* @param ColumnInterface|ColumnSchemaInterface|string $type The column type.
* {@see getColumnType()} Method will be invoked to convert an abstract column type (if any) into the physical one.
* Anything that isn't recognized as an abstract type will be kept in the generated SQL.
* For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become
* 'varchar(255) not null'.
* @param ColumnSchemaInterface|string $type The column type which can contain a native database column type,
* {@see ColumnType abstract} or {@see PseudoType pseudo} type, or can be represented as instance of
* {@see ColumnSchemaInterface}.
*
* The {@see QueryBuilderInterface::buildColumnDefinition()} method will be invoked to convert column definitions
* into SQL representation. For example, it will convert `string not null` to `varchar(255) not null`
* and `pk` to `int PRIMARY KEY AUTO_INCREMENT` (for MySQL).
*
* The preferred way is to use {@see ColumnBuilder} to generate column definitions as instances of
* {@see ColumnSchemaInterface}.
*
* @return string The SQL statement for adding a new column.
*
* Note: The method will quote the `table` and `column` parameters before using them in the generated SQL.
*/
public function addColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): string;
public function addColumn(string $table, string $column, ColumnSchemaInterface|string $type): string;

/**
* Builds an SQL command for adding comment to column.
Expand Down Expand Up @@ -160,17 +164,22 @@ public function addUnique(string $table, string $name, array|string $columns): s
*
* @param string $table The table whose column is to change.
* @param string $column The name of the column to change.
* @param ColumnInterface|ColumnSchemaInterface|string $type The new column type.
* {@see getColumnType()} Method will be invoked to convert an abstract column type (if any) into the physical one.
* Anything that isn't recognized as an abstract type will be kept in the generated SQL.
* For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become
* 'varchar(255) not null'.
* @param ColumnSchemaInterface|string $type The column type which can contain a native database column type,
* {@see ColumnType abstract} or {@see PseudoType pseudo} type, or can be represented as instance of
* {@see ColumnSchemaInterface}.
*
* The {@see QueryBuilderInterface::buildColumnDefinition()} method will be invoked to convert column definitions
* into SQL representation. For example, it will convert `string not null` to `varchar(255) not null`
* and `pk` to `int PRIMARY KEY AUTO_INCREMENT` (for MySQL).
*
* The preferred way is to use {@see ColumnBuilder} to generate column definitions as instances of
* {@see ColumnSchemaInterface}.
*
* @return string The SQL statement for changing the definition of a column.
*
* Note: The method will quote the `table` and `column` parameters before using them in the generated SQL.
*/
public function alterColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): string;
public function alterColumn(string $table, string $column, ColumnSchemaInterface|string $type): string;

/**
* Builds an SQL statement for enabling or disabling integrity check.
Expand Down Expand Up @@ -226,7 +235,7 @@ public function createIndex(
* into SQL representation. For example, it will convert `string not null` to `varchar(255) not null`
* and `pk` to `int PRIMARY KEY AUTO_INCREMENT` (for MySQL).
*
* The preferred method is to use {@see ColumnBuilder} to generate column definitions as instances of
* The preferred way is to use {@see ColumnBuilder} to generate column definitions as instances of
* {@see ColumnSchemaInterface}.
*
* ```php
Expand All @@ -250,14 +259,14 @@ public function createIndex(
*
* @param string $table The name of the table to create.
* @param array $columns The columns (name => definition) in the new table.
* The definition can be `string` or {@see ColumnInterface} or {@see ColumnSchemaInterface} instance.
* The definition can be `string` or {@see ColumnSchemaInterface} instance.
* @param string|null $options More SQL fragments to append to the generated SQL.
*
* @return string The SQL statement for creating a new DB table.
*
* Note: The method will quote the `table` and `columns` parameter before using it in the generated SQL.
*
* @psalm-param array<string, ColumnInterface|ColumnSchemaInterface>|string[] $columns
* @psalm-param array<string, ColumnSchemaInterface>|string[] $columns
*/
public function createTable(string $table, array $columns, string $options = null): string;

Expand Down
Loading

0 comments on commit 8de1111

Please sign in to comment.