Skip to content

Commit

Permalink
Rename class ColumnSchemaBuilder (#594)
Browse files Browse the repository at this point in the history
* Rename class ColumnSchemaBuilder

* Merge branch 'master' of github.com:darkdef/db-mysql into columnbuilder

# Conflicts:
#	src/QueryBuilder.php
#	src/Schema.php

Merge branch 'master' of github.com:darkdef/db-mssql into columnbuilder

# Conflicts:
#	src/DDLQueryBuilder.php

* merges and bugfixes

* fix after merge
  • Loading branch information
darkdef committed Mar 11, 2023
1 parent 04ab257 commit 40092e7
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 93 deletions.
6 changes: 4 additions & 2 deletions docs/en/command/ddl.md
Expand Up @@ -34,12 +34,13 @@ The following example shows how to add a new column to an existing table.
declare(strict_types=1);

use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Mysql\Column;

/** @var ConnectionInterface $db */
$db->createCommand()->addColumn(
'{{%customer}}',
'profile_id',
$db->getSchema()->createColumnSchemaBuilder('integer')
new Column('integer')
)->execute();
```

Expand Down Expand Up @@ -168,12 +169,13 @@ The following example shows how to change an existing column.
declare(strict_types=1);

use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Mysql\Column;

/** @var ConnectionInterface $db */
$db->createCommand()->alterColumn(
'{{%customer}}',
'profile_id',
$db->getSchema()->createColumnSchemaBuilder('integer')->notNull()
new Column('integer')->notNull()
)->execute();
```

Expand Down
4 changes: 2 additions & 2 deletions src/QueryBuilder/AbstractDDLQueryBuilder.php
Expand Up @@ -6,7 +6,7 @@

use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\Schema\ColumnSchemaBuilderInterface;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\QuoterInterface;
use Yiisoft\Db\Schema\SchemaInterface;

Expand Down Expand Up @@ -136,7 +136,7 @@ public function addUnique(string $name, string $table, array|string $columns): s
public function alterColumn(
string $table,
string $column,
ColumnSchemaBuilderInterface|string $type
ColumnInterface|string $type
): string {
return 'ALTER TABLE '
. $this->quoter->quoteTableName($table)
Expand Down
8 changes: 4 additions & 4 deletions src/QueryBuilder/AbstractQueryBuilder.php
Expand Up @@ -9,7 +9,7 @@
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\QueryBuilder\Condition\Interface\ConditionInterface;
use Yiisoft\Db\Schema\ColumnSchemaBuilderInterface;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\QuoterInterface;
use Yiisoft\Db\Schema\SchemaInterface;

Expand Down Expand Up @@ -96,7 +96,7 @@ public function addUnique(string $name, string $table, array|string $columns): s
return $this->ddlBuilder->addUnique($name, $table, $columns);
}

public function alterColumn(string $table, string $column, ColumnSchemaBuilderInterface|string $type): string
public function alterColumn(string $table, string $column, ColumnInterface|string $type): string
{
return $this->ddlBuilder->alterColumn($table, $column, $type);
}
Expand Down Expand Up @@ -298,9 +298,9 @@ public function dropView(string $viewName): string
return $this->ddlBuilder->dropView($viewName);
}

public function getColumnType(ColumnSchemaBuilderInterface|string $type): string
public function getColumnType(ColumnInterface|string $type): string
{
if ($type instanceof ColumnSchemaBuilderInterface) {
if ($type instanceof ColumnInterface) {
$type = $type->asString();
}

Expand Down
6 changes: 3 additions & 3 deletions src/QueryBuilder/DDLQueryBuilderInterface.php
Expand Up @@ -9,7 +9,7 @@
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\Schema\ColumnSchemaBuilderInterface;
use Yiisoft\Db\Schema\Builder\ColumnInterface;

/**
* Defines methods for building SQL statements for DDL (data definition language).
Expand Down Expand Up @@ -159,7 +159,7 @@ public function addUnique(string $name, string $table, array|string $columns): s
*
* @param string $table The table whose column is to be changed.
* @param string $column The name of the column to be changed.
* @param ColumnSchemaBuilderInterface|string $type The new column type.
* @param ColumnInterface|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 +169,7 @@ public function addUnique(string $name, string $table, 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, ColumnSchemaBuilderInterface|string $type): string;
public function alterColumn(string $table, string $column, ColumnInterface|string $type): string;

/**
* Builds an SQL statement for enabling or disabling integrity check.
Expand Down
6 changes: 3 additions & 3 deletions src/QueryBuilder/QueryBuilderInterface.php
Expand Up @@ -7,7 +7,7 @@
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Schema\ColumnSchemaBuilderInterface;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\QuoterInterface;

/**
Expand Down Expand Up @@ -73,11 +73,11 @@ public function bindParam(mixed $value, array &$params = []): string;
*
* If a type can't be found in {@see typeMap}, it will be returned without any change.
*
* @param ColumnSchemaBuilderInterface|string $type Abstract column type.
* @param ColumnInterface|string $type Abstract column type.
*
* @return string Physical column type.
*/
public function getColumnType(ColumnSchemaBuilderInterface|string $type): string;
public function getColumnType(ColumnInterface|string $type): string;

/**
* Gets an object of {@see ExpressionBuilderInterface} that's suitable for $expression.
Expand Down
Expand Up @@ -2,10 +2,11 @@

declare(strict_types=1);

namespace Yiisoft\Db\Schema;
namespace Yiisoft\Db\Schema\Builder;

use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Helper\StringHelper;
use Yiisoft\Db\Schema\SchemaInterface;

use function gettype;
use function implode;
Expand All @@ -20,13 +21,13 @@
* For example, the following code creates a column schema for an integer column:
*
* ```php
* $column = (new ColumnSchemaBuilder(SchemaInterface::TYPE_INTEGER))->notNull()->defaultValue(0);
* $column = (new Column(SchemaInterface::TYPE_INTEGER))->notNull()->defaultValue(0);
* ```
*
* 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.
*/
abstract class AbstractColumnSchemaBuilder implements ColumnSchemaBuilderInterface
abstract class AbstractColumn implements ColumnInterface
{
/**
* Internally used constants representing categories that abstract column types fall under.
Expand Down
Expand Up @@ -2,15 +2,15 @@

declare(strict_types=1);

namespace Yiisoft\Db\Schema;
namespace Yiisoft\Db\Schema\Builder;

/**
* This interface defines the methods that must be implemented by classes that build the schema of a database column.
*
* It provides methods for setting the column name, type, length, precision, scale, default value, and other properties
* of the column, as well as methods for adding constraints, such as a primary key, unique, and not null.
*/
interface ColumnSchemaBuilderInterface
interface ColumnInterface
{
/**
* Specify more SQL to be appended to column definition.
Expand Down
26 changes: 6 additions & 20 deletions src/Schema/SchemaInterface.php
Expand Up @@ -9,6 +9,7 @@
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Schema\Builder\ColumnInterface;

/**
* Represents the schema for a database table.
Expand Down Expand Up @@ -77,6 +78,11 @@ interface SchemaInterface extends ConstraintSchemaInterface
public const PHP_TYPE_ARRAY = 'array';
public const PHP_TYPE_NULL = 'NULL';

/**
* @psalm-param string[]|int[]|int|string|null $length
*/
public function createColumn(string $type, array|int|string $length = null): ColumnInterface;

/**
* @return string|null The default schema name.
*/
Expand Down Expand Up @@ -132,26 +138,6 @@ public function getSchemaNames(bool $refresh = false): array;
*/
public function getTableNames(string $schema = '', bool $refresh = false): array;

/**
* Create a column schema builder instance giving the type and value precision.
*
* This method may be overridden by child classes to create a DBMS-specific column schema builder.
*
* @param string $type the type of the column.
* {@see AbstractColumnSchemaBuilder::$type} for supported types.
* @param array|int|string|null $length The length or precision of the column.
*
* {@see ColumnSchemaBuilderInterface::$length}.
*
* @return ColumnSchemaBuilderInterface column schema builder instance
*
* @psalm-param string[]|int[]|int|string|null $length
*/
public function createColumnSchemaBuilder(
string $type,
array|int|string $length = null
): ColumnSchemaBuilderInterface;

/**
* Returns all unique indexes for the given table.
*
Expand Down
9 changes: 3 additions & 6 deletions tests/AbstractSchemaTest.php
Expand Up @@ -6,7 +6,7 @@

use PDO;
use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Schema\ColumnSchemaBuilderInterface;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Tests\Support\Assert;
use Yiisoft\Db\Tests\Support\Stub\ColumnSchema;
Expand All @@ -22,12 +22,9 @@ abstract class AbstractSchemaTest extends TestCase

public function testCreateColumnSchemaBuilder(): void
{
$db = $this->getConnection();

$schema = $db->getSchema();
$columnSchemaBuilder = $schema->createColumnSchemaBuilder('string');
$columnSchemaBuilder = $this->getConnection()->getSchema()->createColumn('string');

$this->assertInstanceOf(ColumnSchemaBuilderInterface::class, $columnSchemaBuilder);
$this->assertInstanceOf(ColumnInterface::class, $columnSchemaBuilder);
$this->assertSame('string', $columnSchemaBuilder->getType());
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Common/CommonColumnSchemaBuilderTest.php
Expand Up @@ -27,7 +27,7 @@ protected function checkBuildString(string $expected, string $type, int|null $le
$db = $this->getConnection();

$schema = $db->getSchema();
$builder = $schema->createColumnSchemaBuilder($type, $length);
$builder = $schema->createColumn($type, $length);

foreach ($calls as $call) {
$method = array_shift($call);
Expand Down

0 comments on commit 40092e7

Please sign in to comment.