From b25c397fb8c67bbcbc3f6ee8f3223d52f53273ca Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sun, 8 Sep 2024 12:46:23 +0700 Subject: [PATCH 1/2] Separate column type constants --- src/Column.php | 2 +- src/Column/ColumnFactory.php | 66 +++++++++---------- src/QueryBuilder.php | 50 +++++++------- src/Schema.php | 9 +-- tests/JsonCommandTest.php | 7 +- tests/Provider/ColumnFactoryProvider.php | 61 ++++++++--------- .../Provider/ColumnSchemaBuilderProvider.php | 6 +- tests/QueryBuilderJsonTest.php | 8 +-- tests/SchemaTest.php | 8 ++- 9 files changed, 112 insertions(+), 105 deletions(-) diff --git a/src/Column.php b/src/Column.php index 823acc6f5..65bbb4685 100644 --- a/src/Column.php +++ b/src/Column.php @@ -17,7 +17,7 @@ * For example, the following code creates a column schema for an integer column: * * ```php - * $column = (new Column(SchemaInterface::TYPE_INTEGER))->notNull()->defaultValue(0); + * $column = (new Column(ColumnType::INTEGER))->notNull()->defaultValue(0); * ``` * * Provides a fluent interface, which means that the methods can be chained together to create a column schema with diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index 3b902d5f1..0a2c29d41 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -4,9 +4,9 @@ namespace Yiisoft\Db\Mysql\Column; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Schema\Column\AbstractColumnFactory; use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; -use Yiisoft\Db\Schema\SchemaInterface; final class ColumnFactory extends AbstractColumnFactory { @@ -18,35 +18,35 @@ final class ColumnFactory extends AbstractColumnFactory * @psalm-suppress MissingClassConstType */ private const TYPE_MAP = [ - 'bit' => SchemaInterface::TYPE_BIT, - 'tinyint' => SchemaInterface::TYPE_TINYINT, - 'smallint' => SchemaInterface::TYPE_SMALLINT, - 'mediumint' => SchemaInterface::TYPE_INTEGER, - 'int' => SchemaInterface::TYPE_INTEGER, - 'integer' => SchemaInterface::TYPE_INTEGER, - 'bigint' => SchemaInterface::TYPE_BIGINT, - 'float' => SchemaInterface::TYPE_FLOAT, - 'real' => SchemaInterface::TYPE_FLOAT, - 'double' => SchemaInterface::TYPE_DOUBLE, - 'decimal' => SchemaInterface::TYPE_DECIMAL, - 'numeric' => SchemaInterface::TYPE_DECIMAL, - 'char' => SchemaInterface::TYPE_CHAR, - 'varchar' => SchemaInterface::TYPE_STRING, - 'string' => SchemaInterface::TYPE_STRING, - 'enum' => SchemaInterface::TYPE_STRING, - 'tinytext' => SchemaInterface::TYPE_TEXT, - 'mediumtext' => SchemaInterface::TYPE_TEXT, - 'longtext' => SchemaInterface::TYPE_TEXT, - 'text' => SchemaInterface::TYPE_TEXT, - 'varbinary' => SchemaInterface::TYPE_BINARY, - 'blob' => SchemaInterface::TYPE_BINARY, - 'longblob' => SchemaInterface::TYPE_BINARY, - 'year' => SchemaInterface::TYPE_DATE, - 'date' => SchemaInterface::TYPE_DATE, - 'time' => SchemaInterface::TYPE_TIME, - 'datetime' => SchemaInterface::TYPE_DATETIME, - 'timestamp' => SchemaInterface::TYPE_TIMESTAMP, - 'json' => SchemaInterface::TYPE_JSON, + 'bit' => ColumnType::BIT, + 'tinyint' => ColumnType::TINYINT, + 'smallint' => ColumnType::SMALLINT, + 'mediumint' => ColumnType::INTEGER, + 'int' => ColumnType::INTEGER, + 'integer' => ColumnType::INTEGER, + 'bigint' => ColumnType::BIGINT, + 'float' => ColumnType::FLOAT, + 'real' => ColumnType::FLOAT, + 'double' => ColumnType::DOUBLE, + 'decimal' => ColumnType::DECIMAL, + '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, + 'varbinary' => ColumnType::BINARY, + 'blob' => ColumnType::BINARY, + 'longblob' => ColumnType::BINARY, + 'year' => ColumnType::DATE, + 'date' => ColumnType::DATE, + 'time' => ColumnType::TIME, + 'datetime' => ColumnType::DATETIME, + 'timestamp' => ColumnType::TIMESTAMP, + 'json' => ColumnType::JSON, ]; public function fromDefinition(string $definition, array $info = []): ColumnSchemaInterface @@ -65,10 +65,10 @@ public function fromDefinition(string $definition, array $info = []): ColumnSche protected function getType(string $dbType, array $info = []): string { - $type = self::TYPE_MAP[$dbType] ?? SchemaInterface::TYPE_STRING; + $type = self::TYPE_MAP[$dbType] ?? ColumnType::STRING; - if ($type === SchemaInterface::TYPE_BIT && isset($info['size']) && $info['size'] === 1) { - return SchemaInterface::TYPE_BOOLEAN; + if ($type === ColumnType::BIT && isset($info['size']) && $info['size'] === 1) { + return ColumnType::BOOLEAN; } return $type; diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 7b1204497..0176b9663 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -4,6 +4,8 @@ namespace Yiisoft\Db\Mysql; +use Yiisoft\Db\Constant\ColumnType; +use Yiisoft\Db\Constant\PseudoType; use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder; use Yiisoft\Db\Schema\QuoterInterface; use Yiisoft\Db\Schema\SchemaInterface; @@ -17,30 +19,30 @@ final class QueryBuilder extends AbstractQueryBuilder * @psalm-var string[] $typeMap Mapping from abstract column types (keys) to physical column types (values). */ protected array $typeMap = [ - SchemaInterface::TYPE_PK => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', - SchemaInterface::TYPE_UPK => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - SchemaInterface::TYPE_BIGPK => 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', - SchemaInterface::TYPE_UBIGPK => 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - SchemaInterface::TYPE_CHAR => 'char(1)', - SchemaInterface::TYPE_STRING => 'varchar(255)', - SchemaInterface::TYPE_TEXT => 'text', - SchemaInterface::TYPE_TINYINT => 'tinyint(3)', - SchemaInterface::TYPE_SMALLINT => 'smallint(6)', - SchemaInterface::TYPE_INTEGER => 'int(11)', - SchemaInterface::TYPE_BIGINT => 'bigint(20)', - SchemaInterface::TYPE_FLOAT => 'float', - SchemaInterface::TYPE_DOUBLE => 'double', - SchemaInterface::TYPE_DECIMAL => 'decimal(10,0)', - SchemaInterface::TYPE_DATE => 'date', - SchemaInterface::TYPE_BINARY => 'blob', - SchemaInterface::TYPE_BOOLEAN => 'bit(1)', - SchemaInterface::TYPE_MONEY => 'decimal(19,4)', - SchemaInterface::TYPE_JSON => 'json', - SchemaInterface::TYPE_DATETIME => 'datetime(0)', - SchemaInterface::TYPE_TIMESTAMP => 'timestamp(0)', - SchemaInterface::TYPE_TIME => 'time(0)', - SchemaInterface::TYPE_UUID => 'binary(16)', - SchemaInterface::TYPE_UUID_PK => 'binary(16) PRIMARY KEY', + PseudoType::PK => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', + PseudoType::UPK => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + PseudoType::BIGPK => 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', + PseudoType::UBIGPK => 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + ColumnType::CHAR => 'char(1)', + ColumnType::STRING => 'varchar(255)', + ColumnType::TEXT => 'text', + ColumnType::TINYINT => 'tinyint(3)', + ColumnType::SMALLINT => 'smallint(6)', + ColumnType::INTEGER => 'int(11)', + ColumnType::BIGINT => 'bigint(20)', + ColumnType::FLOAT => 'float', + ColumnType::DOUBLE => 'double', + ColumnType::DECIMAL => 'decimal(10,0)', + ColumnType::DATE => 'date', + ColumnType::BINARY => 'blob', + ColumnType::BOOLEAN => 'bit(1)', + ColumnType::MONEY => 'decimal(19,4)', + ColumnType::JSON => 'json', + ColumnType::DATETIME => 'datetime(0)', + ColumnType::TIMESTAMP => 'timestamp(0)', + ColumnType::TIME => 'time(0)', + ColumnType::UUID => 'binary(16)', + PseudoType::UUID_PK => 'binary(16) PRIMARY KEY', ]; public function __construct( diff --git a/src/Schema.php b/src/Schema.php index d4d461d0c..836f43e63 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Mysql; use Throwable; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Constraint\Constraint; use Yiisoft\Db\Constraint\ForeignKeyConstraint; use Yiisoft\Db\Constraint\IndexConstraint; @@ -187,7 +188,7 @@ protected function findColumns(TableSchemaInterface $table): bool $info['extra_default_value'] = $columnsExtra[$info['field']] ?? ''; if (in_array($info['field'], $jsonColumns, true)) { - $info['type'] = self::TYPE_JSON; + $info['type'] = ColumnType::JSON; } $column = $this->loadColumnSchema($info); @@ -434,8 +435,8 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface && !empty($info['extra_default_value']) && !str_starts_with($info['extra_default_value'], '\'') && in_array($column->getType(), [ - self::TYPE_CHAR, self::TYPE_STRING, self::TYPE_TEXT, - self::TYPE_DATETIME, self::TYPE_TIMESTAMP, self::TYPE_TIME, self::TYPE_DATE, + ColumnType::CHAR, ColumnType::STRING, ColumnType::TEXT, + ColumnType::DATETIME, ColumnType::TIMESTAMP, ColumnType::TIME, ColumnType::DATE, ], true) ) { $extra = 'DEFAULT_GENERATED'; @@ -470,7 +471,7 @@ private function normalizeDefaultValue(?string $defaultValue, ColumnSchemaInterf } if ( - in_array($column->getType(), [self::TYPE_TIMESTAMP, self::TYPE_DATETIME, self::TYPE_DATE, self::TYPE_TIME], true) + in_array($column->getType(), [ColumnType::TIMESTAMP, ColumnType::DATETIME, ColumnType::DATE, ColumnType::TIME], true) && preg_match('/^current_timestamp(?:\((\d*)\))?$/i', $defaultValue, $matches) === 1 ) { return new Expression('CURRENT_TIMESTAMP' . (!empty($matches[1]) ? '(' . $matches[1] . ')' : '')); diff --git a/tests/JsonCommandTest.php b/tests/JsonCommandTest.php index 8d1dcaed9..99fa612af 100644 --- a/tests/JsonCommandTest.php +++ b/tests/JsonCommandTest.php @@ -5,9 +5,10 @@ namespace Yiisoft\Db\Mysql\Tests; use PHPUnit\Framework\TestCase; +use Yiisoft\Db\Constant\ColumnType; +use Yiisoft\Db\Constant\PseudoType; use Yiisoft\Db\Expression\JsonExpression; use Yiisoft\Db\Mysql\Tests\Support\TestTrait; -use Yiisoft\Db\Schema\SchemaInterface; /** * @group mysql @@ -28,8 +29,8 @@ public function testCreateTable(): void $command = $db->createCommand(); $command->createTable('json', [ - 'id' => SchemaInterface::TYPE_PK, - 'data' => SchemaInterface::TYPE_JSON, + 'id' => PseudoType::PK, + 'data' => ColumnType::JSON, ])->execute(); $this->assertTrue($db->getTableSchema('json') !== null); diff --git a/tests/Provider/ColumnFactoryProvider.php b/tests/Provider/ColumnFactoryProvider.php index 61f319f49..e76d21366 100644 --- a/tests/Provider/ColumnFactoryProvider.php +++ b/tests/Provider/ColumnFactoryProvider.php @@ -4,6 +4,7 @@ namespace Yiisoft\Db\Mysql\Tests\Provider; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Schema\Column\BinaryColumnSchema; use Yiisoft\Db\Schema\Column\BitColumnSchema; use Yiisoft\Db\Schema\Column\BooleanColumnSchema; @@ -18,35 +19,35 @@ public static function dbTypes(): array { return [ // db type, expected abstract type, expected instance of - ['bit', 'bit', BitColumnSchema::class], - ['tinyint', 'tinyint', IntegerColumnSchema::class], - ['smallint', 'smallint', IntegerColumnSchema::class], - ['mediumint', 'integer', IntegerColumnSchema::class], - ['int', 'integer', IntegerColumnSchema::class], - ['integer', 'integer', IntegerColumnSchema::class], - ['bigint', 'bigint', IntegerColumnSchema::class], - ['float', 'float', DoubleColumnSchema::class], - ['real', 'float', DoubleColumnSchema::class], - ['double', 'double', DoubleColumnSchema::class], - ['decimal', 'decimal', DoubleColumnSchema::class], - ['numeric', 'decimal', DoubleColumnSchema::class], - ['char', 'char', StringColumnSchema::class], - ['varchar', 'string', StringColumnSchema::class], - ['string', 'string', StringColumnSchema::class], - ['enum', 'string', StringColumnSchema::class], - ['tinytext', 'text', StringColumnSchema::class], - ['mediumtext', 'text', StringColumnSchema::class], - ['longtext', 'text', StringColumnSchema::class], - ['text', 'text', StringColumnSchema::class], - ['varbinary', 'binary', BinaryColumnSchema::class], - ['blob', 'binary', BinaryColumnSchema::class], - ['longblob', 'binary', BinaryColumnSchema::class], - ['year', 'date', StringColumnSchema::class], - ['date', 'date', StringColumnSchema::class], - ['time', 'time', StringColumnSchema::class], - ['datetime', 'datetime', StringColumnSchema::class], - ['timestamp', 'timestamp', StringColumnSchema::class], - ['json', 'json', JsonColumnSchema::class], + ['bit', ColumnType::BIT, BitColumnSchema::class], + ['tinyint', ColumnType::TINYINT, IntegerColumnSchema::class], + ['smallint', ColumnType::SMALLINT, IntegerColumnSchema::class], + ['mediumint', ColumnType::INTEGER, IntegerColumnSchema::class], + ['int', ColumnType::INTEGER, IntegerColumnSchema::class], + ['integer', ColumnType::INTEGER, IntegerColumnSchema::class], + ['bigint', ColumnType::BIGINT, IntegerColumnSchema::class], + ['float', ColumnType::FLOAT, DoubleColumnSchema::class], + ['real', ColumnType::FLOAT, DoubleColumnSchema::class], + ['double', ColumnType::DOUBLE, DoubleColumnSchema::class], + ['decimal', ColumnType::DECIMAL, DoubleColumnSchema::class], + ['numeric', ColumnType::DECIMAL, DoubleColumnSchema::class], + ['char', ColumnType::CHAR, StringColumnSchema::class], + ['varchar', ColumnType::STRING, StringColumnSchema::class], + ['string', ColumnType::STRING, StringColumnSchema::class], + ['enum', ColumnType::STRING, StringColumnSchema::class], + ['tinytext', ColumnType::TEXT, StringColumnSchema::class], + ['mediumtext', ColumnType::TEXT, StringColumnSchema::class], + ['longtext', ColumnType::TEXT, StringColumnSchema::class], + ['text', ColumnType::TEXT, StringColumnSchema::class], + ['varbinary', ColumnType::BINARY, BinaryColumnSchema::class], + ['blob', ColumnType::BINARY, BinaryColumnSchema::class], + ['longblob', ColumnType::BINARY, BinaryColumnSchema::class], + ['year', ColumnType::DATE, StringColumnSchema::class], + ['date', ColumnType::DATE, StringColumnSchema::class], + ['time', ColumnType::TIME, StringColumnSchema::class], + ['datetime', ColumnType::DATETIME, StringColumnSchema::class], + ['timestamp', ColumnType::TIMESTAMP, StringColumnSchema::class], + ['json', ColumnType::JSON, JsonColumnSchema::class], ]; } @@ -54,7 +55,7 @@ public static function definitions(): array { $definitions = parent::definitions(); - $definitions[] = ['bit(1)', 'boolean', BooleanColumnSchema::class, ['getSize' => 1]]; + $definitions[] = ['bit(1)', ColumnType::BOOLEAN, BooleanColumnSchema::class, ['getSize' => 1]]; return $definitions; } diff --git a/tests/Provider/ColumnSchemaBuilderProvider.php b/tests/Provider/ColumnSchemaBuilderProvider.php index 843309434..c85e34697 100644 --- a/tests/Provider/ColumnSchemaBuilderProvider.php +++ b/tests/Provider/ColumnSchemaBuilderProvider.php @@ -4,7 +4,7 @@ namespace Yiisoft\Db\Mysql\Tests\Provider; -use Yiisoft\Db\Schema\SchemaInterface; +use Yiisoft\Db\Constant\ColumnType; final class ColumnSchemaBuilderProvider extends \Yiisoft\Db\Tests\Provider\ColumnSchemaBuilderProvider { @@ -20,14 +20,14 @@ public static function types(): array return [ ...$types, - ['integer UNSIGNED', SchemaInterface::TYPE_INTEGER, null, [['unsigned']]], + ['integer UNSIGNED', ColumnType::INTEGER, null, [['unsigned']]], /** * {@link https://github.com/yiisoft/yii2/issues/11945}, real test against database. */ [ 'string(50) NOT NULL COMMENT \'Property name\' COLLATE ascii_general_ci', - SchemaInterface::TYPE_STRING, 50, + ColumnType::STRING, 50, [ ['comment', 'Property name'], ['append', 'COLLATE ascii_general_ci'], diff --git a/tests/QueryBuilderJsonTest.php b/tests/QueryBuilderJsonTest.php index 26cddefbd..93ac2cd43 100644 --- a/tests/QueryBuilderJsonTest.php +++ b/tests/QueryBuilderJsonTest.php @@ -5,10 +5,10 @@ namespace Yiisoft\Db\Mysql\Tests; use PHPUnit\Framework\TestCase; +use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Expression\JsonExpression; use Yiisoft\Db\Mysql\Column; use Yiisoft\Db\Mysql\Tests\Support\TestTrait; -use Yiisoft\Db\Schema\SchemaInterface; /** * @group mysql @@ -22,7 +22,7 @@ final class QueryBuilderJsonTest extends TestCase public function testAlterColumn() { $qb = $this->getConnection()->getQueryBuilder(); - $columnSchemaBuilder = new Column(SchemaInterface::TYPE_JSON); + $columnSchemaBuilder = new Column(ColumnType::JSON); $sql = $qb->alterColumn('storage', 'id', $columnSchemaBuilder); $this->assertStringEndsWith( @@ -36,7 +36,7 @@ public function testAlterColumn() public function testAddColumn() { $qb = $this->getConnection()->getQueryBuilder(); - $columnSchemaBuilder = new Column(SchemaInterface::TYPE_JSON); + $columnSchemaBuilder = new Column(ColumnType::JSON); $sql = $qb->addColumn('storage', 'abc', $columnSchemaBuilder->asString()); $this->assertSame( @@ -50,7 +50,7 @@ public function testAddColumn() public function testCreateTable() { $qb = $this->getConnection()->getQueryBuilder(); - $columnSchemaBuilder = new Column(SchemaInterface::TYPE_JSON); + $columnSchemaBuilder = new Column(ColumnType::JSON); $sql = $qb->createTable('storage', ['abc' => $columnSchemaBuilder]); $this->assertSame( diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 8485201a1..d459ba465 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -8,6 +8,8 @@ use Throwable; use Yiisoft\Db\Command\CommandInterface; use Yiisoft\Db\Connection\ConnectionInterface; +use Yiisoft\Db\Constant\ColumnType; +use Yiisoft\Db\Constant\PseudoType; use Yiisoft\Db\Constraint\Constraint; use Yiisoft\Db\Driver\Pdo\PdoConnectionInterface; use Yiisoft\Db\Exception\Exception; @@ -521,9 +523,9 @@ public function testTinyInt1() $db->createCommand()->createTable( $tableName, [ - 'id' => new Column(SchemaInterface::TYPE_PK), - 'bool_col' => new Column(SchemaInterface::TYPE_BOOLEAN), - 'status' => new Column(SchemaInterface::TYPE_TINYINT, 1), + 'id' => new Column(PseudoType::PK), + 'bool_col' => new Column(ColumnType::BOOLEAN), + 'status' => new Column(ColumnType::TINYINT, 1), ] )->execute(); From 70b258bed92b9e1967815251cc469a11529daccc Mon Sep 17 00:00:00 2001 From: Tigrov Date: Mon, 9 Sep 2024 09:10:04 +0700 Subject: [PATCH 2/2] Add line to CHANGELOG.md [skip ci] --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cfed07bea..5afb2ca7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Enh #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) ## 1.2.0 March 21, 2024