Skip to content

Commit

Permalink
Separate column type constants (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Sep 13, 2024
1 parent d19eeeb commit 039e09c
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 99 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Enh #312: Update `bit` type according to main PR yiisoft/db#860 (@Tigrov)
- Enh #315: Raise minimum PHP version to `^8.1` with minor refactoring (@Tigrov)
- Enh #314: Implement `ColumnFactory` class (@Tigrov)
- Enh #317: 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 @@ -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
3 changes: 2 additions & 1 deletion src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Db\Sqlite;

use Throwable;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constraint\CheckConstraint;
use Yiisoft\Db\Constraint\Constraint;
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
Expand Down Expand Up @@ -337,7 +338,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;
}

$column = $this->loadColumnSchema($info);
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 039e09c

Please sign in to comment.