Skip to content

Commit

Permalink
Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed May 5, 2024
1 parent 21f67d5 commit 9341cb8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
62 changes: 0 additions & 62 deletions src/ColumnSchema.php

This file was deleted.

15 changes: 8 additions & 7 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions tests/ColumnSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand All @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion tests/Provider/ColumnSchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 9341cb8

Please sign in to comment.