Skip to content

Commit

Permalink
Separate column type constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Sep 6, 2024
1 parent d19eeeb commit 9fee262
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 99 deletions.
2 changes: 1 addition & 1 deletion src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,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,8 +4,8 @@

namespace Yiisoft\Db\Sqlite\Column;

use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Schema\Column\AbstractColumnFactory;
use Yiisoft\Db\Schema\SchemaInterface;

final class ColumnFactory extends AbstractColumnFactory
{
Expand All @@ -17,47 +17,47 @@ final class ColumnFactory extends AbstractColumnFactory
* @psalm-suppress MissingClassConstType
*/
private const TYPE_MAP = [
'bool' => SchemaInterface::TYPE_BOOLEAN,
'boolean' => SchemaInterface::TYPE_BOOLEAN,
'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,
'blob' => 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,
'bool' => ColumnType::BOOLEAN,
'boolean' => ColumnType::BOOLEAN,
'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,
'blob' => ColumnType::BINARY,
'year' => ColumnType::DATE,
'date' => ColumnType::DATE,
'time' => ColumnType::TIME,
'datetime' => ColumnType::DATETIME,
'timestamp' => ColumnType::TIMESTAMP,
'json' => ColumnType::JSON,
];

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 || $type === SchemaInterface::TYPE_TINYINT)
($type === ColumnType::BIT || $type === ColumnType::TINYINT)
&& isset($info['size'])
&& $info['size'] === 1
) {
return SchemaInterface::TYPE_BOOLEAN;
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\Sqlite;

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
* @var string[] Mapping from abstract column types (keys) to physical column types (values).
*/
protected array $typeMap = [
SchemaInterface::TYPE_PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL',
SchemaInterface::TYPE_UPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL',
SchemaInterface::TYPE_BIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL',
SchemaInterface::TYPE_UBIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL',
SchemaInterface::TYPE_CHAR => 'char(1)',
SchemaInterface::TYPE_STRING => 'varchar(255)',
SchemaInterface::TYPE_TEXT => 'text',
SchemaInterface::TYPE_TINYINT => 'tinyint',
SchemaInterface::TYPE_SMALLINT => 'smallint',
SchemaInterface::TYPE_INTEGER => 'integer',
SchemaInterface::TYPE_BIGINT => 'bigint',
SchemaInterface::TYPE_FLOAT => 'float',
SchemaInterface::TYPE_DOUBLE => 'double',
SchemaInterface::TYPE_DECIMAL => 'decimal(10,0)',
SchemaInterface::TYPE_DATETIME => 'datetime',
SchemaInterface::TYPE_TIMESTAMP => 'timestamp',
SchemaInterface::TYPE_TIME => 'time',
SchemaInterface::TYPE_DATE => 'date',
SchemaInterface::TYPE_BINARY => 'blob',
SchemaInterface::TYPE_BOOLEAN => 'boolean',
SchemaInterface::TYPE_MONEY => 'decimal(19,4)',
SchemaInterface::TYPE_UUID => 'blob(16)',
SchemaInterface::TYPE_UUID_PK => 'blob(16) PRIMARY KEY',
SchemaInterface::TYPE_JSON => 'json',
PseudoType::PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL',
PseudoType::UPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL',
PseudoType::BIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL',
PseudoType::UBIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL',
ColumnType::CHAR => 'char(1)',
ColumnType::STRING => 'varchar(255)',
ColumnType::TEXT => 'text',
ColumnType::TINYINT => 'tinyint',
ColumnType::SMALLINT => 'smallint',
ColumnType::INTEGER => 'integer',
ColumnType::BIGINT => 'bigint',
ColumnType::FLOAT => 'float',
ColumnType::DOUBLE => 'double',
ColumnType::DECIMAL => 'decimal(10,0)',
ColumnType::DATETIME => 'datetime',
ColumnType::TIMESTAMP => 'timestamp',
ColumnType::TIME => 'time',
ColumnType::DATE => 'date',
ColumnType::BINARY => 'blob',
ColumnType::BOOLEAN => 'boolean',
ColumnType::MONEY => 'decimal(19,4)',
ColumnType::UUID => 'blob(16)',
PseudoType::UUID_PK => 'blob(16) PRIMARY KEY',
ColumnType::JSON => 'json',
];

public function __construct(QuoterInterface $quoter, SchemaInterface $schema)
Expand Down
2 changes: 1 addition & 1 deletion src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ protected function findColumns(TableSchemaInterface $table): bool

foreach ($columns as $info) {
if (in_array($info['name'], $jsonColumns, true)) {
$info['type'] = self::TYPE_JSON;
$info['type'] = ColumnType::JSON;

Check failure on line 340 in src/Schema.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

UndefinedClass

src/Schema.php:340:33: UndefinedClass: Class, interface or enum named Yiisoft\Db\Sqlite\ColumnType does not exist (see https://psalm.dev/019)

Check failure on line 340 in src/Schema.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

UndefinedClass

src/Schema.php:340:33: UndefinedClass: Class, interface or enum named Yiisoft\Db\Sqlite\ColumnType does not exist (see https://psalm.dev/019)
}

$column = $this->loadColumnSchema($info);

Check failure on line 343 in src/Schema.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

MixedArgumentTypeCoercion

src/Schema.php:343:47: MixedArgumentTypeCoercion: Argument 1 of Yiisoft\Db\Sqlite\Schema::loadColumnSchema expects array{cid: string, dflt_value: null|string, name: string, notnull: string, pk: string, precision?: int, scale?: int, size?: int, type: string}, but parent type array{cid: string, dflt_value: null|string, name: string, notnull: string, pk: string, precision?: int, scale?: int, size?: int, type: mixed|string} provided (see https://psalm.dev/194)

Check failure on line 343 in src/Schema.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

MixedArgumentTypeCoercion

src/Schema.php:343:47: MixedArgumentTypeCoercion: Argument 1 of Yiisoft\Db\Sqlite\Schema::loadColumnSchema expects array{cid: string, dflt_value: null|string, name: string, notnull: string, pk: string, precision?: int, scale?: int, size?: int, type: string}, but parent type array{cid: string, dflt_value: null|string, name: string, notnull: string, pk: string, precision?: int, scale?: int, size?: int, type: mixed|string} provided (see https://psalm.dev/194)
Expand Down
9 changes: 5 additions & 4 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
namespace Yiisoft\Db\Sqlite\Tests;

use Throwable;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\JsonExpression;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Sqlite\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Common\CommonCommandTest;

Expand Down Expand Up @@ -391,7 +392,7 @@ public function testResetSequence(): void

$command->createTable(
'{{testCreateTable}}',
['id' => SchemaInterface::TYPE_PK, 'bar' => SchemaInterface::TYPE_INTEGER],
['id' => PseudoType::PK, 'bar' => ColumnType::INTEGER],
)->execute();

$command->insert('{{testCreateTable}}', ['bar' => 1])->execute();
Expand Down Expand Up @@ -508,8 +509,8 @@ public function testJsonTable(): void
}

$command->createTable('json_table', [
'id' => SchemaInterface::TYPE_PK,
'json_col' => SchemaInterface::TYPE_JSON,
'id' => PseudoType::PK,
'json_col' => ColumnType::JSON,
])->execute();

$command->insert('json_table', ['id' => 1, 'json_col' => ['a' => 1, 'b' => 2]])->execute();
Expand Down
63 changes: 32 additions & 31 deletions tests/Provider/ColumnFactoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\Sqlite\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,44 +19,44 @@ public static function dbTypes(): array
{
return [
// db type, expected abstract type, expected instance of
['bool', 'boolean', BooleanColumnSchema::class],
['boolean', 'boolean', BooleanColumnSchema::class],
['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],
['blob', '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],
['bool', ColumnType::BOOLEAN, BooleanColumnSchema::class],
['boolean', ColumnType::BOOLEAN, BooleanColumnSchema::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],
['blob', 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[] = ['tinyint(1)', 'boolean', BooleanColumnSchema::class, ['getSize' => 1]];
$definitions[] = ['bit(1)', ColumnType::BOOLEAN, BooleanColumnSchema::class, ['getDbType' => 'bit', 'getSize' => 1]];
$definitions[] = ['tinyint(1)', ColumnType::BOOLEAN, BooleanColumnSchema::class, ['getDbType' => 'tinyint', 'getSize' => 1]];

return $definitions;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Provider/ColumnSchemaBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Db\Sqlite\Tests\Provider;

use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Constant\ColumnType;

final class ColumnSchemaBuilderProvider extends \Yiisoft\Db\Tests\Provider\ColumnSchemaBuilderProvider
{
Expand All @@ -19,7 +19,7 @@ public static function types(): array

return [
...$types,
['integer UNSIGNED', SchemaInterface::TYPE_INTEGER, null, [['unsigned']]],
['integer UNSIGNED', ColumnType::INTEGER, null, [['unsigned']]],
];
}

Expand Down
6 changes: 3 additions & 3 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Db\Sqlite\Tests;

use JsonException;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
Expand All @@ -15,7 +16,6 @@
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\QueryBuilder\Condition\JsonOverlapsCondition;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Sqlite\Column;
use Yiisoft\Db\Sqlite\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Common\CommonQueryBuilderTest;
Expand Down Expand Up @@ -172,7 +172,7 @@ public function testAlterColumn(): void
$this->expectException(NotSupportedException::class);
$this->expectExceptionMessage('Yiisoft\Db\Sqlite\DDLQueryBuilder::alterColumn is not supported by SQLite.');

$qb->alterColumn('customer', 'email', SchemaInterface::TYPE_STRING);
$qb->alterColumn('customer', 'email', ColumnType::STRING);
}

/**
Expand Down Expand Up @@ -750,7 +750,7 @@ public function testUpsertExecute(
public function testJsonColumn()
{
$qb = $this->getConnection()->getQueryBuilder();
$columnSchemaBuilder = new Column(SchemaInterface::TYPE_JSON);
$columnSchemaBuilder = new Column(ColumnType::JSON);

$this->assertSame(
'ALTER TABLE `json_table` ADD `json_col` json',
Expand Down

0 comments on commit 9fee262

Please sign in to comment.