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

Use new column definition builder #905

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;
use function get_resource_type;
Expand Down Expand Up @@ -131,7 +132,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|string $type): static
public function addColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): static
{
$sql = $this->getQueryBuilder()->addColumn($table, $column, $type);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
Expand Down Expand Up @@ -188,7 +189,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|string $type): static
public function alterColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): static
{
$sql = $this->getQueryBuilder()->alterColumn($table, $column, $type);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
Expand Down
21 changes: 11 additions & 10 deletions src/Command/CommandInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\QueryBuilder\DMLQueryBuilderInterface;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;

/**
* This interface represents a database command, such as a `SELECT`, `INSERT`, `UPDATE`, or `DELETE` statement.
Expand Down Expand Up @@ -44,13 +45,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|string $type The column type. {@see QueryBuilder::getColumnType()} will be called
* to convert the given column type to the database one.
* @param ColumnInterface|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|string $type): static;
public function addColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): static;

/**
* Builds an SQL command for adding a comment to a column.
Expand Down Expand Up @@ -144,13 +145,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|string $type The column type. {@see QueryBuilder::getColumnType()} 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`.
* @param ColumnInterface|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|string $type): static;
public function alterColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): static;

/**
* Creates a batch INSERT command.
Expand Down Expand Up @@ -327,8 +328,8 @@ public function createIndex(
* into the generated SQL.
*
* @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} instance.
* @param (ColumnSchemaInterface|string)[] $columns The columns (name => definition) in the new table.
* The definition can be `string` or {@see ColumnSchemaInterface} instance.
* @param string|null $options More SQL fragments to append to the generated SQL.
*
* @throws Exception
Expand All @@ -337,7 +338,7 @@ public function createIndex(
*
* Note: The method will quote the `table` and `columns` parameter before using it in the generated SQL.
*
* @psalm-param array<string, ColumnInterface>|string[] $columns
* @psalm-param array<string, ColumnSchemaInterface>|string[] $columns
*/
public function createTable(string $table, array $columns, string $options = null): static;

Expand Down
5 changes: 3 additions & 2 deletions src/Debug/CommandInterfaceProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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 @@ -30,7 +31,7 @@ public function addCheck(string $table, string $name, string $expression): stati
/**
* @psalm-suppress MixedArgument
*/
public function addColumn(string $table, string $column, ColumnInterface|string $type): static
public function addColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): static
{
return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
}
Expand Down Expand Up @@ -93,7 +94,7 @@ public function addUnique(string $table, string $name, array|string $columns): s
/**
* @psalm-suppress MixedArgument
*/
public function alterColumn(string $table, string $column, ColumnInterface|string $type): static
public function alterColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): static
{
return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
}
Expand Down
5 changes: 5 additions & 0 deletions src/QueryBuilder/AbstractColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
. $this->buildExtra($column);
}

public function buildAlter(ColumnSchemaInterface $column): string

Check warning on line 68 in src/QueryBuilder/AbstractColumnDefinitionBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/QueryBuilder/AbstractColumnDefinitionBuilder.php#L68

Added line #L68 was not covered by tests
{
return $this->build($column);

Check warning on line 70 in src/QueryBuilder/AbstractColumnDefinitionBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/QueryBuilder/AbstractColumnDefinitionBuilder.php#L70

Added line #L70 was not covered by tests
}

/**
* Builds the auto increment clause for the column.
*
Expand Down
14 changes: 6 additions & 8 deletions src/QueryBuilder/AbstractDDLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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 @@ -39,15 +40,14 @@ public function addCheck(string $table, string $name, string $expression): strin
. ' CHECK (' . $this->quoter->quoteSql($expression) . ')';
}

public function addColumn(string $table, string $column, ColumnInterface|string $type): string
public function addColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): string
{
/** @psalm-suppress DeprecatedMethod */
return 'ALTER TABLE '
. $this->quoter->quoteTableName($table)
. ' ADD '
. $this->quoter->quoteColumnName($column)
. ' '
. $this->queryBuilder->getColumnType($type);
. $this->queryBuilder->buildColumnDefinition($type);
}

public function addCommentOnColumn(string $table, string $column, string $comment): string
Expand Down Expand Up @@ -137,16 +137,15 @@ public function addUnique(string $table, string $name, array|string $columns): s
public function alterColumn(
string $table,
string $column,
ColumnInterface|string $type
ColumnInterface|ColumnSchemaInterface|string $type
): string {
/** @psalm-suppress DeprecatedMethod */
return 'ALTER TABLE '
. $this->quoter->quoteTableName($table)
. ' CHANGE '
. $this->quoter->quoteColumnName($column)
. ' '
. $this->quoter->quoteColumnName($column) . ' '
. $this->queryBuilder->getColumnType($type);
. $this->queryBuilder->buildColumnDefinition($type);
}

public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string
Expand All @@ -173,11 +172,10 @@ public function createTable(string $table, array $columns, string $options = nul

foreach ($columns as $name => $type) {
if (is_string($name)) {
/** @psalm-suppress DeprecatedMethod */
$cols[] = "\t"
. $this->quoter->quoteColumnName($name)
. ' '
. $this->queryBuilder->getColumnType($type);
. $this->queryBuilder->buildColumnDefinition($type);
} else {
/** @psalm-var string $type */
$cols[] = "\t" . $type;
Expand Down
15 changes: 12 additions & 3 deletions src/QueryBuilder/AbstractQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,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|string $type): string
public function addColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): string
{
return $this->ddlBuilder->addColumn($table, $column, $type);
}
Expand Down Expand Up @@ -129,7 +129,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|string $type): string
public function alterColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): string
{
return $this->ddlBuilder->alterColumn($table, $column, $type);
}
Expand Down Expand Up @@ -173,8 +173,12 @@ public function build(QueryInterface $query, array $params = []): array
return $this->dqlBuilder->build($query, $params);
}

public function buildColumnDefinition(ColumnSchemaInterface|string $column): string
public function buildColumnDefinition(ColumnInterface|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 @@ -353,6 +357,11 @@ public function dropView(string $viewName): string
return $this->ddlBuilder->dropView($viewName);
}

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
{
Expand Down
5 changes: 5 additions & 0 deletions src/QueryBuilder/ColumnDefinitionBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ interface ColumnDefinitionBuilderInterface
* @return string the column SQL definition.
*/
public function build(ColumnSchemaInterface $column): string;

/**
* Builds column definition for `ALTER` operation based on given column instance.
*/
public function buildAlter(ColumnSchemaInterface $column): string;
}
13 changes: 7 additions & 6 deletions src/QueryBuilder/DDLQueryBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;

/**
* Defines methods for building SQL statements for DDL (data definition language).
Expand All @@ -36,7 +37,7 @@ 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|string $type The column type.
* @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
Expand All @@ -46,7 +47,7 @@ public function addCheck(string $table, string $name, string $expression): strin
*
* 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|string $type): string;
public function addColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): string;

/**
* Builds an SQL command for adding comment to column.
Expand Down Expand Up @@ -159,7 +160,7 @@ 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|string $type The new column type.
* @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
Expand All @@ -169,7 +170,7 @@ public function addUnique(string $table, string $name, array|string $columns): s
*
* 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|string $type): string;
public function alterColumn(string $table, string $column, ColumnInterface|ColumnSchemaInterface|string $type): string;

/**
* Builds an SQL statement for enabling or disabling integrity check.
Expand Down Expand Up @@ -233,14 +234,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} instance.
* The definition can be `string` or {@see ColumnInterface} 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>|string[] $columns
* @psalm-param array<string, ColumnInterface|ColumnSchemaInterface>|string[] $columns
*/
public function createTable(string $table, array $columns, string $options = null): string;

Expand Down
11 changes: 8 additions & 3 deletions src/QueryBuilder/QueryBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public function bindParam(mixed $value, array &$params = []): string;
/**
* Builds column definition based on given column instance.
*
* @param ColumnSchemaInterface|string $column the column instance or string column definition which should be
* converted into a database string representation.
* @param ColumnInterface|ColumnSchemaInterface|string $column the column instance or string column definition which
* should be converted into a database string representation.
*
* @return string the SQL column definition.
*/
public function buildColumnDefinition(ColumnSchemaInterface|string $column): string;
public function buildColumnDefinition(ColumnInterface|ColumnSchemaInterface|string $column): string;

/**
* Converts an abstract column type into a physical column type.
Expand Down Expand Up @@ -98,6 +98,11 @@ public function buildColumnDefinition(ColumnSchemaInterface|string $column): str
*/
public function getColumnType(ColumnInterface|string $type): string;

/**
* Returns the column definition builder for the current DBMS.
*/
public function getColumnDefinitionBuilder(): ColumnDefinitionBuilderInterface;

/**
* Gets an object of {@see ExpressionBuilderInterface} that's suitable for $expression.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Schema/Builder/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Helper\DbStringHelper;
use Yiisoft\Db\Schema\Column\AbstractColumnSchema;

use function gettype;
use function implode;
Expand All @@ -27,6 +28,8 @@
*
* Provides a fluent interface, which means that the methods can be chained together to create a column schema with
* many properties in a single line of code.
*
* @deprecated Use {@see AbstractColumnSchema} instead. Will be removed in 2.0.0.
*/
abstract class AbstractColumn implements ColumnInterface
{
Expand Down
20 changes: 5 additions & 15 deletions tests/AbstractQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function testAddColumn(ColumnInterface|string $type): void
DbHelper::replaceQuotes(
<<<SQL
ALTER TABLE [[table]] ADD [[column]]
SQL . ' ' . $qb->getColumnType($type),
SQL . ' ' . $qb->buildColumnDefinition($type),
$db->getDriverName(),
),
$sql,
Expand Down Expand Up @@ -192,22 +192,12 @@ public function testAddUnique(string $name, string $table, array|string $columns
$this->assertSame($expected, $sql);
}

public function testAlterColumn(): void
#[DataProviderExternal(QueryBuilderProvider::class, 'alterColumn')]
public function testAlterColumn(string|ColumnSchemaInterface $type, string $expected): void
{
$db = $this->getConnection();

$qb = $db->getQueryBuilder();
$sql = $qb->alterColumn('customer', 'email', ColumnType::STRING);
$qb = $this->getConnection()->getQueryBuilder();

$this->assertSame(
DbHelper::replaceQuotes(
<<<SQL
ALTER TABLE [[customer]] CHANGE [[email]] [[email]]
SQL . ' ' . $qb->getColumnType(ColumnType::STRING),
$db->getDriverName(),
),
$sql,
);
$this->assertSame($expected, $qb->alterColumn('foo1', 'bar', $type));
}

/**
Expand Down
Loading
Loading