-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
190 additions
and
81 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,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Mysql\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[] | ||
* | ||
* @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, | ||
]; | ||
|
||
public function fromDefinition(string $definition, array $info = []): ColumnSchemaInterface | ||
{ | ||
if (str_starts_with($definition, 'enum(')) { | ||
preg_match('/^enum\(([^)]+)\)\s*/', $definition, $matches); | ||
preg_match_all("/'([^']*)'/", $matches[1], $values); | ||
|
||
$info['enum_values'] = $values[1]; | ||
|
||
return $this->fromDbType('enum', $info); | ||
} | ||
|
||
return parent::fromDefinition($definition, $info); | ||
} | ||
|
||
protected function getType(string $dbType, array $info = []): string | ||
{ | ||
$type = self::TYPE_MAP[$dbType] ?? SchemaInterface::TYPE_STRING; | ||
|
||
if ($type === SchemaInterface::TYPE_BIT && isset($info['size']) && $info['size'] === 1) { | ||
return SchemaInterface::TYPE_BOOLEAN; | ||
} | ||
|
||
return $type; | ||
} | ||
} |
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\Mysql\Tests; | ||
|
||
use Yiisoft\Db\Mysql\Tests\Support\TestTrait; | ||
use Yiisoft\Db\Tests\AbstractColumnFactoryTest; | ||
|
||
/** | ||
* @group mysql | ||
*/ | ||
final class ColumnFactoryTest extends AbstractColumnFactoryTest | ||
{ | ||
use TestTrait; | ||
|
||
/** @dataProvider \Yiisoft\Db\Mysql\Tests\Provider\ColumnFactoryProvider::dbTypes */ | ||
public function testFromDbType(string $dbType, string $expectedType, string $expectedInstanceOf): void | ||
{ | ||
parent::testFromDbType($dbType, $expectedType, $expectedInstanceOf); | ||
} | ||
|
||
/** @dataProvider \Yiisoft\Db\Mysql\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\Mysql\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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Mysql\Tests\Provider; | ||
|
||
use Yiisoft\Db\Schema\Column\BinaryColumnSchema; | ||
use Yiisoft\Db\Schema\Column\BitColumnSchema; | ||
use Yiisoft\Db\Schema\Column\BooleanColumnSchema; | ||
use Yiisoft\Db\Schema\Column\DoubleColumnSchema; | ||
use Yiisoft\Db\Schema\Column\IntegerColumnSchema; | ||
use Yiisoft\Db\Schema\Column\JsonColumnSchema; | ||
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', '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], | ||
]; | ||
} | ||
|
||
public static function definitions(): array | ||
{ | ||
$definitions = parent::definitions(); | ||
|
||
$definitions[] = ['bit(1)', 'boolean', BooleanColumnSchema::class, ['getSize' => 1]]; | ||
|
||
return $definitions; | ||
} | ||
} |
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