-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
188 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Mssql\Column; | ||
|
||
use Yiisoft\Db\Schema\Column\AbstractColumnFactory; | ||
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; | ||
use Yiisoft\Db\Schema\SchemaInterface; | ||
|
||
final class ColumnFactory extends AbstractColumnFactory | ||
{ | ||
/** | ||
* Mapping from physical column types (keys) to abstract column types (values). | ||
* | ||
* @var string[] | ||
*/ | ||
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, | ||
|
||
/** Approximate numbers */ | ||
'float' => SchemaInterface::TYPE_FLOAT, | ||
'real' => SchemaInterface::TYPE_FLOAT, | ||
'double' => SchemaInterface::TYPE_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, | ||
|
||
/** Character strings */ | ||
'char' => SchemaInterface::TYPE_CHAR, | ||
'varchar' => SchemaInterface::TYPE_STRING, | ||
'text' => SchemaInterface::TYPE_TEXT, | ||
|
||
/** Unicode character strings */ | ||
'nchar' => SchemaInterface::TYPE_CHAR, | ||
'nvarchar' => SchemaInterface::TYPE_STRING, | ||
'ntext' => SchemaInterface::TYPE_TEXT, | ||
|
||
/** Binary strings */ | ||
'binary' => SchemaInterface::TYPE_BINARY, | ||
'varbinary' => SchemaInterface::TYPE_BINARY, | ||
'image' => SchemaInterface::TYPE_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, | ||
]; | ||
|
||
protected function getType(string $dbType, array $info = []): string | ||
{ | ||
return self::TYPE_MAP[$dbType] ?? SchemaInterface::TYPE_STRING; | ||
} | ||
|
||
public function fromType(string $type, array $info = []): ColumnSchemaInterface | ||
{ | ||
if ($type === SchemaInterface::TYPE_BINARY) { | ||
return (new BinaryColumnSchema($type))->load($info); | ||
} | ||
|
||
return parent::fromType($type, $info); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Mssql\Tests; | ||
|
||
use Yiisoft\Db\Mssql\Tests\Support\TestTrait; | ||
use Yiisoft\Db\Tests\AbstractColumnFactoryTest; | ||
|
||
/** | ||
* @group mssql | ||
*/ | ||
final class ColumnFactoryTest extends AbstractColumnFactoryTest | ||
{ | ||
use TestTrait; | ||
|
||
/** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ColumnFactoryProvider::dbTypes */ | ||
public function testFromDbType(string $dbType, string $expectedType, string $expectedInstanceOf): void | ||
{ | ||
parent::testFromDbType($dbType, $expectedType, $expectedInstanceOf); | ||
} | ||
|
||
/** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ColumnFactoryProvider::definitions */ | ||
public function testFromDefinition(string $definition, string $expectedType, string $expectedInstanceOf, array $expectedInfo = []): void | ||
{ | ||
parent::testFromDefinition($definition, $expectedType, $expectedInstanceOf, $expectedInfo); | ||
} | ||
|
||
/** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ColumnFactoryProvider::types */ | ||
public function testFromType(string $type, string $expectedType, string $expectedInstanceOf): void | ||
{ | ||
parent::testFromType($type, $expectedType, $expectedInstanceOf); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Mssql\Tests\Provider; | ||
|
||
use Yiisoft\Db\Mssql\Column\BinaryColumnSchema; | ||
use Yiisoft\Db\Schema\Column\BooleanColumnSchema; | ||
use Yiisoft\Db\Schema\Column\DoubleColumnSchema; | ||
use Yiisoft\Db\Schema\Column\IntegerColumnSchema; | ||
use Yiisoft\Db\Schema\Column\StringColumnSchema; | ||
|
||
final class ColumnFactoryProvider extends \Yiisoft\Db\Tests\Provider\ColumnFactoryProvider | ||
{ | ||
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], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters