From 9341cb8a9e86f0d6b36a991c4207b7f719db70ee Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sun, 5 May 2024 08:34:11 +0700 Subject: [PATCH] Improve --- src/{ => Column}/BinaryColumnSchema.php | 2 +- src/ColumnSchema.php | 62 ------------------------- src/Schema.php | 15 +++--- tests/ColumnSchemaTest.php | 7 +-- tests/Provider/ColumnSchemaProvider.php | 2 +- 5 files changed, 14 insertions(+), 74 deletions(-) rename src/{ => Column}/BinaryColumnSchema.php (95%) delete mode 100644 src/ColumnSchema.php diff --git a/src/BinaryColumnSchema.php b/src/Column/BinaryColumnSchema.php similarity index 95% rename from src/BinaryColumnSchema.php rename to src/Column/BinaryColumnSchema.php index 7f40f2af..f42194a1 100644 --- a/src/BinaryColumnSchema.php +++ b/src/Column/BinaryColumnSchema.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Yiisoft\Db\Mssql; +namespace Yiisoft\Db\Mssql\Column; use Yiisoft\Db\Command\ParamInterface; use Yiisoft\Db\Expression\Expression; diff --git a/src/ColumnSchema.php b/src/ColumnSchema.php deleted file mode 100644 index b2bc3a04..00000000 --- a/src/ColumnSchema.php +++ /dev/null @@ -1,62 +0,0 @@ -name('id'); - * $column->allowNull(false); - * $column->dbType('int'); - * $column->phpType('integer'); - * $column->type('integer'); - * $column->defaultValue(0); - * $column->autoIncrement(true); - * $column->primaryKey(true); - * ``` - * - * @psalm-suppress DeprecatedInterface - * @psalm-suppress DeprecatedClass - */ -final class ColumnSchema extends AbstractColumnSchema -{ - public function dbTypecast(mixed $value): mixed - { - if ($this->getType() === SchemaInterface::TYPE_BINARY && $this->getDbType() === 'varbinary') { - if ($value instanceof ParamInterface && is_string($value->getValue())) { - $value = (string) $value->getValue(); - } - if (is_string($value)) { - return new Expression('CONVERT(VARBINARY(MAX), ' . ('0x' . bin2hex($value)) . ')'); - } - } - - /** @psalm-suppress DeprecatedClass */ - return parent::dbTypecast($value); - } -} diff --git a/src/Schema.php b/src/Schema.php index 1b54028c..18abe6f6 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -14,6 +14,7 @@ use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Helper\DbArrayHelper; +use Yiisoft\Db\Mssql\Column\BinaryColumnSchema; use Yiisoft\Db\Schema\Builder\ColumnInterface; use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; use Yiisoft\Db\Schema\TableSchemaInterface; @@ -467,13 +468,13 @@ private function getColumnType(string $dbType, array &$info): string return self::TYPE_MAP[$dbType] ?? self::TYPE_STRING; } - protected function createPhpTypeColumnSchema(string $phpType, string $name): ColumnSchemaInterface + protected function createColumnSchemaFromPhpType(string $phpType, string $type): ColumnSchemaInterface { if ($phpType === self::PHP_TYPE_RESOURCE) { - return new BinaryColumnSchema($name); + return new BinaryColumnSchema($type, $phpType); } - return parent::createPhpTypeColumnSchema($phpType, $name); + return parent::createColumnSchemaFromPhpType($phpType, $type); } /** @@ -577,10 +578,10 @@ protected function findColumns(TableSchemaInterface $table): bool return false; } - foreach ($columns as $column) { - $column = $this->loadColumnSchema($column); + foreach ($columns as $info) { + $column = $this->loadColumnSchema($info); foreach ($table->getPrimaryKey() as $primaryKey) { - if (strcasecmp($column->getName(), $primaryKey) === 0) { + if (strcasecmp($info['column_name'], $primaryKey) === 0) { $column->primaryKey(true); break; } @@ -590,7 +591,7 @@ protected function findColumns(TableSchemaInterface $table): bool $table->sequenceName(''); } - $table->column($column->getName(), $column); + $table->column($info['column_name'], $column); } return true; diff --git a/tests/ColumnSchemaTest.php b/tests/ColumnSchemaTest.php index c5333f72..e80773b3 100644 --- a/tests/ColumnSchemaTest.php +++ b/tests/ColumnSchemaTest.php @@ -7,9 +7,10 @@ use PDO; use Yiisoft\Db\Command\Param; use Yiisoft\Db\Expression\Expression; -use Yiisoft\Db\Mssql\BinaryColumnSchema; +use Yiisoft\Db\Mssql\Column\BinaryColumnSchema; use Yiisoft\Db\Mssql\Tests\Support\TestTrait; use Yiisoft\Db\Query\Query; +use Yiisoft\Db\Schema\Column\BooleanColumnSchema; use Yiisoft\Db\Schema\Column\DoubleColumnSchema; use Yiisoft\Db\Schema\Column\IntegerColumnSchema; use Yiisoft\Db\Schema\Column\StringColumnSchema; @@ -78,6 +79,7 @@ public function testColumnSchemaInstance() $this->assertInstanceOf(StringColumnSchema::class, $tableSchema->getColumn('char_col')); $this->assertInstanceOf(DoubleColumnSchema::class, $tableSchema->getColumn('float_col')); $this->assertInstanceOf(BinaryColumnSchema::class, $tableSchema->getColumn('blob_col')); + $this->assertInstanceOf(BooleanColumnSchema::class, $tableSchema->getColumn('bool_col')); } /** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ColumnSchemaProvider::predefinedTypes */ @@ -94,10 +96,9 @@ public function testDbTypecastColumns(string $className, array $values) public function testBinaryColumnSchema() { - $binaryCol = new BinaryColumnSchema('binary_col'); + $binaryCol = new BinaryColumnSchema(); $binaryCol->dbType('varbinary'); - $this->assertSame('binary_col', $binaryCol->getName()); $this->assertEquals( new Expression('CONVERT(VARBINARY(MAX), 0x101112)'), $binaryCol->dbTypecast("\x10\x11\x12"), diff --git a/tests/Provider/ColumnSchemaProvider.php b/tests/Provider/ColumnSchemaProvider.php index 77104a9a..b78dc4aa 100644 --- a/tests/Provider/ColumnSchemaProvider.php +++ b/tests/Provider/ColumnSchemaProvider.php @@ -4,7 +4,7 @@ namespace Yiisoft\Db\Mssql\Tests\Provider; -use Yiisoft\Db\Mssql\BinaryColumnSchema; +use Yiisoft\Db\Mssql\Column\BinaryColumnSchema; class ColumnSchemaProvider extends \Yiisoft\Db\Tests\Provider\ColumnSchemaProvider {