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

Separate column type constants #354

Merged
merged 2 commits into from
Sep 13, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 33 additions & 33 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand All @@ -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;
Expand Down
50 changes: 26 additions & 24 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand Down
9 changes: 5 additions & 4 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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] . ')' : ''));
Expand Down
7 changes: 4 additions & 3 deletions tests/JsonCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
61 changes: 31 additions & 30 deletions tests/Provider/ColumnFactoryProvider.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\ColumnType;
use Yiisoft\Db\Schema\Column\BinaryColumnSchema;
use Yiisoft\Db\Schema\Column\BitColumnSchema;
use Yiisoft\Db\Schema\Column\BooleanColumnSchema;
Expand All @@ -18,43 +19,43 @@ 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],
];
}

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;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Provider/ColumnSchemaBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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'],
Expand Down
8 changes: 4 additions & 4 deletions tests/QueryBuilderJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down
8 changes: 5 additions & 3 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down