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 ColumnDefinitionBuilder #358

Merged
merged 6 commits into from
Oct 15, 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
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
- Bug #320: Change visibility of `DDLQueryBuilder::getColumnDefinition()` method to `private` (@Tigrov)
- Enh #321: Implement `SqlParser` and `ExpressionBuilder` driver classes (@Tigrov)
- Chg #339: Replace call of `SchemaInterface::getRawTableName()` to `QuoterInterface::getRawTableName()` (@Tigrov)
- Enh #342: Add JSON overlaps condition builder (@Tigrov)
- New #342: Add JSON overlaps condition builder (@Tigrov)
- Enh #344: Update `bit` type according to main PR yiisoft/db#860 (@Tigrov)
- Enh #346: Implement `ColumnFactory` class (@Tigrov)
- New #346: Implement `ColumnFactory` class (@Tigrov)
- Enh #347, #353: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov)
- Bug #349, #352: Restore connection if closed by connection timeout (@Tigrov)
- Enh #354: Separate column type constants (@Tigrov)
- Enh #355: Realize `ColumnBuilder` class (@Tigrov)
- New #355: Realize `ColumnBuilder` class (@Tigrov)
- Enh #357: Update according changes in `ColumnSchemaInterface` (@Tigrov)
- New #358: Add `ColumnDefinitionBuilder` class (@Tigrov)

## 1.2.0 March 21, 2024

Expand Down
99 changes: 99 additions & 0 deletions src/Column/ColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mysql\Column;

use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\QueryBuilder\AbstractColumnDefinitionBuilder;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;

final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder
{
protected const AUTO_INCREMENT_KEYWORD = 'AUTO_INCREMENT';

protected const GENERATE_UUID_EXPRESSION = 'uuid_to_bin(uuid())';

protected const TYPES_WITH_SIZE = [
'bit',
'tinyint',
'smallint',
'mediumint',
'int',
'integer',
'bigint',
'float',
'real',
'double',
'double precision',
'decimal',
'numeric',
'dec',
'fixed',
'char',
'character',
'national char',
'nchar',
'varchar',
'character varying',
'national varchar',
'nvarchar',
'text',
'binary',
'char byte',
'varbinary',
'blob',
'year',
'time',
'datetime',
'timestamp',
];

protected const TYPES_WITH_SCALE = [
'float',
'real',
'double',
'double precision',
'decimal',
'numeric',
'dec',
'fixed',
];

protected function buildComment(ColumnSchemaInterface $column): string
{
$comment = $column->getComment();

return $comment === null ? '' : ' COMMENT ' . (string) $this->queryBuilder->quoter()->quoteValue($comment);
}

protected function getDbType(ColumnSchemaInterface $column): string
{
/** @psalm-suppress DocblockTypeContradiction */
return match ($column->getType()) {
ColumnType::BOOLEAN => 'bit(1)',
ColumnType::BIT => 'bit',
ColumnType::TINYINT => 'tinyint',
ColumnType::SMALLINT => 'smallint',
ColumnType::INTEGER => 'int',
ColumnType::BIGINT => 'bigint',
ColumnType::FLOAT => 'float',
ColumnType::DOUBLE => 'double',
ColumnType::DECIMAL => 'decimal',
ColumnType::MONEY => 'decimal',
ColumnType::CHAR => 'char',
ColumnType::STRING => 'varchar',
ColumnType::TEXT => 'text',
ColumnType::BINARY => 'blob',
ColumnType::UUID => 'binary(16)',
ColumnType::DATETIME => 'datetime',
ColumnType::TIMESTAMP => 'timestamp',
ColumnType::DATE => 'date',
ColumnType::TIME => 'time',
ColumnType::ARRAY => 'json',
ColumnType::STRUCTURED => 'json',
ColumnType::JSON => 'json',
default => 'varchar',
};
}
}
2 changes: 1 addition & 1 deletion src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ final class ColumnFactory extends AbstractColumnFactory
'numeric' => ColumnType::DECIMAL,
'char' => ColumnType::CHAR,
'varchar' => ColumnType::STRING,
'string' => ColumnType::STRING,
'enum' => ColumnType::STRING,
'tinytext' => ColumnType::TEXT,
'mediumtext' => ColumnType::TEXT,
'longtext' => ColumnType::TEXT,
'text' => ColumnType::TEXT,
'binary' => ColumnType::BINARY,
'varbinary' => ColumnType::BINARY,
'blob' => ColumnType::BINARY,
'longblob' => ColumnType::BINARY,
Expand Down
7 changes: 0 additions & 7 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
use Throwable;
use Yiisoft\Db\Driver\Pdo\AbstractPdoConnection;
use Yiisoft\Db\Driver\Pdo\PdoCommandInterface;
use Yiisoft\Db\Mysql\Column\ColumnFactory;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
use Yiisoft\Db\Schema\Column\ColumnFactoryInterface;
use Yiisoft\Db\Schema\QuoterInterface;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Transaction\TransactionInterface;
Expand Down Expand Up @@ -65,11 +63,6 @@ public function createTransaction(): TransactionInterface
return new Transaction($this);
}

public function getColumnFactory(): ColumnFactoryInterface
{
return new ColumnFactory();
}

public function getQueryBuilder(): QueryBuilderInterface
{
if ($this->queryBuilder === null) {
Expand Down
5 changes: 4 additions & 1 deletion src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\Mysql\Column\ColumnDefinitionBuilder;
use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder;
use Yiisoft\Db\Schema\QuoterInterface;
use Yiisoft\Db\Schema\SchemaInterface;
Expand Down Expand Up @@ -52,6 +53,8 @@ public function __construct(
$ddlBuilder = new DDLQueryBuilder($this, $quoter, $schema);
$dmlBuilder = new DMLQueryBuilder($this, $quoter, $schema);
$dqlBuilder = new DQLQueryBuilder($this, $quoter);
parent::__construct($quoter, $schema, $ddlBuilder, $dmlBuilder, $dqlBuilder);
$columnDefinitionBuilder = new ColumnDefinitionBuilder($this);

parent::__construct($quoter, $schema, $ddlBuilder, $dmlBuilder, $dqlBuilder, $columnDefinitionBuilder);
}
}
11 changes: 10 additions & 1 deletion src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Helper\DbArrayHelper;
use Yiisoft\Db\Mysql\Column\ColumnBuilder;
use Yiisoft\Db\Mysql\Column\ColumnFactory;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\Column\ColumnFactoryInterface;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
use Yiisoft\Db\Schema\TableSchemaInterface;

Expand Down Expand Up @@ -75,11 +78,17 @@
*/
final class Schema extends AbstractPdoSchema
{
/** @deprecated Use {@see ColumnBuilder} instead. Will be removed in 2.0. */
public function createColumn(string $type, array|int|string $length = null): ColumnInterface
{
return new Column($type, $length);
}

public function getColumnFactory(): ColumnFactoryInterface
{
return new ColumnFactory();
}

/**
* Returns all unique indexes for the given table.
*
Expand Down Expand Up @@ -410,7 +419,7 @@ protected function getCreateTableSql(TableSchemaInterface $table): string
*/
private function loadColumnSchema(array $info): ColumnSchemaInterface
{
$columnFactory = $this->db->getColumnFactory();
$columnFactory = $this->getColumnFactory();

$dbType = $info['type'];
/** @psalm-var ColumnInfoArray $info */
Expand Down
8 changes: 0 additions & 8 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Yiisoft\Db\Exception\IntegrityException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Mysql\Column\ColumnFactory;
use Yiisoft\Db\Mysql\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Common\CommonConnectionTest;
use Yiisoft\Db\Transaction\TransactionInterface;
Expand Down Expand Up @@ -157,11 +156,4 @@ public function testNotRestartConnectionOnTimeoutInTransaction(): void

$db->createCommand('SELECT 1')->queryScalar();
}

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

$this->assertInstanceOf(ColumnFactory::class, $db->getColumnFactory());
}
}
40 changes: 40 additions & 0 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\Mysql\Tests\Provider;

use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Expression\JsonExpression;
use Yiisoft\Db\Mysql\Tests\Support\TestTrait;
Expand Down Expand Up @@ -174,4 +175,43 @@ public static function upsert(): array

return $upsert;
}

public static function buildColumnDefinition(): array
{
$values = parent::buildColumnDefinition();

$values[PseudoType::PK][0] = 'int PRIMARY KEY AUTO_INCREMENT';
$values[PseudoType::UPK][0] = 'int UNSIGNED PRIMARY KEY AUTO_INCREMENT';
$values[PseudoType::BIGPK][0] = 'bigint PRIMARY KEY AUTO_INCREMENT';
$values[PseudoType::UBIGPK][0] = 'bigint UNSIGNED PRIMARY KEY AUTO_INCREMENT';
$values[PseudoType::UUID_PK][0] = 'binary(16) PRIMARY KEY DEFAULT uuid_to_bin(uuid())';
$values[PseudoType::UUID_PK_SEQ][0] = 'binary(16) PRIMARY KEY DEFAULT uuid_to_bin(uuid())';
$values['primaryKey()'][0] = 'int PRIMARY KEY AUTO_INCREMENT';
$values['primaryKey(false)'][0] = 'int PRIMARY KEY';
$values['smallPrimaryKey()'][0] = 'smallint PRIMARY KEY AUTO_INCREMENT';
$values['bigPrimaryKey()'][0] = 'bigint PRIMARY KEY AUTO_INCREMENT';
$values['uuidPrimaryKey()'][0] = 'binary(16) PRIMARY KEY DEFAULT uuid_to_bin(uuid())';
$values['uuidPrimaryKey(false)'][0] = 'binary(16) PRIMARY KEY';
$values['boolean()'][0] = 'bit(1)';
$values['boolean(100)'][0] = 'bit(1)';
$values['integer()'][0] = 'int';
$values['integer(8)'][0] = 'int(8)';
$values['money()'][0] = 'decimal(19,4)';
$values['money(10)'][0] = 'decimal(10,4)';
$values['money(10,2)'][0] = 'decimal(10,2)';
$values['money(null)'][0] = 'decimal';
$values['binary()'][0] = 'blob';
$values['binary(1000)'][0] = 'blob(1000)';
$values['uuid()'][0] = 'binary(16)';
$values["comment('comment')"][0] = "varchar(255) COMMENT 'comment'";
$values["comment('')"][0] = "varchar(255) COMMENT ''";
$values['integer()->primaryKey()'][0] = 'int PRIMARY KEY';
$values["integer()->defaultValue('')"][0] = 'int';
$values['unsigned()'][0] = 'int UNSIGNED';
$values['integer(8)->scale(2)'][0] = 'int(8)';
$values['reference($reference)'][0] = 'int REFERENCES `ref_table` (`id`) ON DELETE CASCADE ON UPDATE CASCADE';
$values['reference($referenceWithSchema)'][0] = 'int REFERENCES `ref_schema`.`ref_table` (`id`) ON DELETE CASCADE ON UPDATE CASCADE';

return $values;
}
}
7 changes: 7 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\QueryBuilder\Condition\JsonOverlapsCondition;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
use Yiisoft\Db\Tests\Common\CommonQueryBuilderTest;

use function str_contains;
Expand Down Expand Up @@ -731,4 +732,10 @@ public function testJsonOverlapsConditionOperator(iterable|ExpressionInterface $

$db->close();
}

/** @dataProvider \Yiisoft\Db\Mysql\Tests\Provider\QueryBuilderProvider::buildColumnDefinition() */
public function testBuildColumnDefinition(string $expected, ColumnSchemaInterface|string $column): void
{
parent::testBuildColumnDefinition($expected, $column);
}
}
8 changes: 8 additions & 0 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Mysql\Column;
use Yiisoft\Db\Mysql\Column\ColumnFactory;
use Yiisoft\Db\Mysql\Schema;
use Yiisoft\Db\Mysql\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
Expand Down Expand Up @@ -573,4 +574,11 @@ public function testInsertDefaultValues()
'numeric_col' => '-33.22',
], $row);
}

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

$this->assertInstanceOf(ColumnFactory::class, $db->getSchema()->getColumnFactory());
}
}