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 #319

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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
72 changes: 36 additions & 36 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Yiisoft\Db\Mssql\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 @@ -17,63 +17,63 @@ final class ColumnFactory extends AbstractColumnFactory
*/
private const TYPE_MAP = [
/** Exact numbers */
'bit' => SchemaInterface::TYPE_BOOLEAN,
'tinyint' => SchemaInterface::TYPE_TINYINT,
'smallint' => SchemaInterface::TYPE_SMALLINT,
'int' => SchemaInterface::TYPE_INTEGER,
'bigint' => SchemaInterface::TYPE_BIGINT,
'numeric' => SchemaInterface::TYPE_DECIMAL,
'decimal' => SchemaInterface::TYPE_DECIMAL,
'smallmoney' => SchemaInterface::TYPE_MONEY,
'money' => SchemaInterface::TYPE_MONEY,
'bit' => ColumnType::BOOLEAN,
'tinyint' => ColumnType::TINYINT,
'smallint' => ColumnType::SMALLINT,
'int' => ColumnType::INTEGER,
'bigint' => ColumnType::BIGINT,
'numeric' => ColumnType::DECIMAL,
'decimal' => ColumnType::DECIMAL,
'smallmoney' => ColumnType::MONEY,
'money' => ColumnType::MONEY,

/** Approximate numbers */
'float' => SchemaInterface::TYPE_FLOAT,
'real' => SchemaInterface::TYPE_FLOAT,
'double' => SchemaInterface::TYPE_DOUBLE,
'float' => ColumnType::FLOAT,
'real' => ColumnType::FLOAT,
'double' => ColumnType::DOUBLE,

/** Date and time */
'date' => SchemaInterface::TYPE_DATE,
'time' => SchemaInterface::TYPE_TIME,
'smalldatetime' => SchemaInterface::TYPE_DATETIME,
'datetime' => SchemaInterface::TYPE_DATETIME,
'datetime2' => SchemaInterface::TYPE_DATETIME,
'datetimeoffset' => SchemaInterface::TYPE_DATETIME,
'date' => ColumnType::DATE,
'time' => ColumnType::TIME,
'smalldatetime' => ColumnType::DATETIME,
'datetime' => ColumnType::DATETIME,
'datetime2' => ColumnType::DATETIME,
'datetimeoffset' => ColumnType::DATETIME,

/** Character strings */
'char' => SchemaInterface::TYPE_CHAR,
'varchar' => SchemaInterface::TYPE_STRING,
'text' => SchemaInterface::TYPE_TEXT,
'char' => ColumnType::CHAR,
'varchar' => ColumnType::STRING,
'text' => ColumnType::TEXT,

/** Unicode character strings */
'nchar' => SchemaInterface::TYPE_CHAR,
'nvarchar' => SchemaInterface::TYPE_STRING,
'ntext' => SchemaInterface::TYPE_TEXT,
'nchar' => ColumnType::CHAR,
'nvarchar' => ColumnType::STRING,
'ntext' => ColumnType::TEXT,

/** Binary strings */
'binary' => SchemaInterface::TYPE_BINARY,
'varbinary' => SchemaInterface::TYPE_BINARY,
'image' => SchemaInterface::TYPE_BINARY,
'binary' => ColumnType::BINARY,
'varbinary' => ColumnType::BINARY,
'image' => ColumnType::BINARY,

/**
* Other data types 'cursor' type can't be used with tables
*/
'timestamp' => SchemaInterface::TYPE_TIMESTAMP,
'hierarchyid' => SchemaInterface::TYPE_STRING,
'uniqueidentifier' => SchemaInterface::TYPE_STRING,
'sql_variant' => SchemaInterface::TYPE_STRING,
'xml' => SchemaInterface::TYPE_STRING,
'table' => SchemaInterface::TYPE_STRING,
'timestamp' => ColumnType::TIMESTAMP,
'hierarchyid' => ColumnType::STRING,
'uniqueidentifier' => ColumnType::STRING,
'sql_variant' => ColumnType::STRING,
'xml' => ColumnType::STRING,
'table' => ColumnType::STRING,
];

protected function getType(string $dbType, array $info = []): string
{
return self::TYPE_MAP[$dbType] ?? SchemaInterface::TYPE_STRING;
return self::TYPE_MAP[$dbType] ?? ColumnType::STRING;
}

public function fromType(string $type, array $info = []): ColumnSchemaInterface
{
if ($type === SchemaInterface::TYPE_BINARY) {
if ($type === ColumnType::BINARY) {
return (new BinaryColumnSchema($type))->load($info);
}

Expand Down
48 changes: 25 additions & 23 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Db\Mssql;

use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\QueryBuilder\AbstractQueryBuilder;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\QuoterInterface;
Expand All @@ -20,29 +22,29 @@ 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 IDENTITY PRIMARY KEY',
SchemaInterface::TYPE_UPK => 'int IDENTITY PRIMARY KEY',
SchemaInterface::TYPE_BIGPK => 'bigint IDENTITY PRIMARY KEY',
SchemaInterface::TYPE_UBIGPK => 'bigint IDENTITY PRIMARY KEY',
SchemaInterface::TYPE_CHAR => 'nchar(1)',
SchemaInterface::TYPE_STRING => 'nvarchar(255)',
SchemaInterface::TYPE_TEXT => 'nvarchar(max)',
SchemaInterface::TYPE_TINYINT => 'tinyint',
SchemaInterface::TYPE_SMALLINT => 'smallint',
SchemaInterface::TYPE_INTEGER => 'int',
SchemaInterface::TYPE_BIGINT => 'bigint',
SchemaInterface::TYPE_FLOAT => 'float',
SchemaInterface::TYPE_DOUBLE => 'float',
SchemaInterface::TYPE_DECIMAL => 'decimal(18,0)',
SchemaInterface::TYPE_DATETIME => 'datetime',
SchemaInterface::TYPE_TIMESTAMP => 'datetime',
SchemaInterface::TYPE_TIME => 'time',
SchemaInterface::TYPE_DATE => 'date',
SchemaInterface::TYPE_BINARY => 'varbinary(max)',
SchemaInterface::TYPE_BOOLEAN => 'bit',
SchemaInterface::TYPE_MONEY => 'decimal(19,4)',
SchemaInterface::TYPE_UUID => 'UNIQUEIDENTIFIER',
SchemaInterface::TYPE_UUID_PK => 'UNIQUEIDENTIFIER PRIMARY KEY',
PseudoType::PK => 'int IDENTITY PRIMARY KEY',
PseudoType::UPK => 'int IDENTITY PRIMARY KEY',
PseudoType::BIGPK => 'bigint IDENTITY PRIMARY KEY',
PseudoType::UBIGPK => 'bigint IDENTITY PRIMARY KEY',
ColumnType::CHAR => 'nchar(1)',
ColumnType::STRING => 'nvarchar(255)',
ColumnType::TEXT => 'nvarchar(max)',
ColumnType::TINYINT => 'tinyint',
ColumnType::SMALLINT => 'smallint',
ColumnType::INTEGER => 'int',
ColumnType::BIGINT => 'bigint',
ColumnType::FLOAT => 'float',
ColumnType::DOUBLE => 'float',
ColumnType::DECIMAL => 'decimal(18,0)',
ColumnType::DATETIME => 'datetime',
ColumnType::TIMESTAMP => 'datetime',
ColumnType::TIME => 'time',
ColumnType::DATE => 'date',
ColumnType::BINARY => 'varbinary(max)',
ColumnType::BOOLEAN => 'bit',
ColumnType::MONEY => 'decimal(19,4)',
ColumnType::UUID => 'UNIQUEIDENTIFIER',
PseudoType::UUID_PK => 'UNIQUEIDENTIFIER PRIMARY KEY',
];

public function __construct(QuoterInterface $quoter, SchemaInterface $schema)
Expand Down
67 changes: 34 additions & 33 deletions tests/Provider/ColumnFactoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\Mssql\Tests\Provider;

use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Mssql\Column\BinaryColumnSchema;
use Yiisoft\Db\Schema\Column\BooleanColumnSchema;
use Yiisoft\Db\Schema\Column\DoubleColumnSchema;
Expand All @@ -16,39 +17,39 @@ public static function dbTypes(): array
{
return [
// db type, expected abstract type, expected instance of
['bit', 'boolean', BooleanColumnSchema::class],
['tinyint', 'tinyint', IntegerColumnSchema::class],
['smallint', 'smallint', IntegerColumnSchema::class],
['int', 'integer', IntegerColumnSchema::class],
['bigint', 'bigint', IntegerColumnSchema::class],
['numeric', 'decimal', DoubleColumnSchema::class],
['decimal', 'decimal', DoubleColumnSchema::class],
['float', 'float', DoubleColumnSchema::class],
['real', 'float', DoubleColumnSchema::class],
['double', 'double', DoubleColumnSchema::class],
['smallmoney', 'money', StringColumnSchema::class],
['money', 'money', StringColumnSchema::class],
['date', 'date', StringColumnSchema::class],
['time', 'time', StringColumnSchema::class],
['smalldatetime', 'datetime', StringColumnSchema::class],
['datetime', 'datetime', StringColumnSchema::class],
['datetime2', 'datetime', StringColumnSchema::class],
['datetimeoffset', 'datetime', StringColumnSchema::class],
['char', 'char', StringColumnSchema::class],
['varchar', 'string', StringColumnSchema::class],
['text', 'text', StringColumnSchema::class],
['nchar', 'char', StringColumnSchema::class],
['nvarchar', 'string', StringColumnSchema::class],
['ntext', 'text', StringColumnSchema::class],
['binary', 'binary', BinaryColumnSchema::class],
['varbinary', 'binary', BinaryColumnSchema::class],
['image', 'binary', BinaryColumnSchema::class],
['timestamp', 'timestamp', StringColumnSchema::class],
['hierarchyid', 'string', StringColumnSchema::class],
['uniqueidentifier', 'string', StringColumnSchema::class],
['sql_variant', 'string', StringColumnSchema::class],
['xml', 'string', StringColumnSchema::class],
['table', 'string', StringColumnSchema::class],
['bit', ColumnType::BOOLEAN, BooleanColumnSchema::class],
['tinyint', ColumnType::TINYINT, IntegerColumnSchema::class],
['smallint', ColumnType::SMALLINT, IntegerColumnSchema::class],
['int', ColumnType::INTEGER, IntegerColumnSchema::class],
['bigint', ColumnType::BIGINT, IntegerColumnSchema::class],
['numeric', ColumnType::DECIMAL, DoubleColumnSchema::class],
['decimal', ColumnType::DECIMAL, DoubleColumnSchema::class],
['float', ColumnType::FLOAT, DoubleColumnSchema::class],
['real', ColumnType::FLOAT, DoubleColumnSchema::class],
['double', ColumnType::DOUBLE, DoubleColumnSchema::class],
['smallmoney', ColumnType::MONEY, StringColumnSchema::class],
['money', ColumnType::MONEY, StringColumnSchema::class],
['date', ColumnType::DATE, StringColumnSchema::class],
['time', ColumnType::TIME, StringColumnSchema::class],
['smalldatetime', ColumnType::DATETIME, StringColumnSchema::class],
['datetime', ColumnType::DATETIME, StringColumnSchema::class],
['datetime2', ColumnType::DATETIME, StringColumnSchema::class],
['datetimeoffset', ColumnType::DATETIME, StringColumnSchema::class],
['char', ColumnType::CHAR, StringColumnSchema::class],
['varchar', ColumnType::STRING, StringColumnSchema::class],
['text', ColumnType::TEXT, StringColumnSchema::class],
['nchar', ColumnType::CHAR, StringColumnSchema::class],
['nvarchar', ColumnType::STRING, StringColumnSchema::class],
['ntext', ColumnType::TEXT, StringColumnSchema::class],
['binary', ColumnType::BINARY, BinaryColumnSchema::class],
['varbinary', ColumnType::BINARY, BinaryColumnSchema::class],
['image', ColumnType::BINARY, BinaryColumnSchema::class],
['timestamp', ColumnType::TIMESTAMP, StringColumnSchema::class],
['hierarchyid', ColumnType::STRING, StringColumnSchema::class],
['uniqueidentifier', ColumnType::STRING, StringColumnSchema::class],
['sql_variant', ColumnType::STRING, StringColumnSchema::class],
['xml', ColumnType::STRING, StringColumnSchema::class],
['table', ColumnType::STRING, StringColumnSchema::class],
];
}
}
Loading
Loading