Skip to content

Commit

Permalink
Load column properties via constructor (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Oct 18, 2024
1 parent 5171ad7 commit bb9a797
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
- New #320: Realize `ColumnBuilder` class (@Tigrov)
- Enh #321: Update according changes in `ColumnSchemaInterface` (@Tigrov)
- New #322: Add `ColumnDefinitionBuilder` class (@Tigrov)
- Enh #323: Refactor `Dsn` class (@Tigrov)
- Enh #324: Use constructor to create columns and initialize properties (@Tigrov)

## 1.2.0 March 21, 2024

Expand Down
3 changes: 1 addition & 2 deletions src/Column/ColumnBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ final class ColumnBuilder extends \Yiisoft\Db\Schema\Column\ColumnBuilder
{
public static function binary(int|null $size = null): ColumnSchemaInterface
{
return (new BinaryColumnSchema(ColumnType::BINARY))
->size($size);
return new BinaryColumnSchema(ColumnType::BINARY, size: $size);
}
}
13 changes: 9 additions & 4 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Schema\Column\AbstractColumnFactory;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
use Yiisoft\Db\Schema\Column\StringColumnSchema;

final class ColumnFactory extends AbstractColumnFactory
{
Expand Down Expand Up @@ -76,9 +77,12 @@ protected function getType(string $dbType, array $info = []): string
public function fromPseudoType(string $pseudoType, array $info = []): ColumnSchemaInterface
{
if ($pseudoType === PseudoType::UUID_PK_SEQ) {
return ColumnBuilder::uuidPrimaryKey()
->defaultValue(new Expression('newsequentialid()'))
->load($info);
unset($info['type']);
$info['primaryKey'] = true;
$info['autoIncrement'] = true;
$info['defaultValue'] = new Expression('newsequentialid()');

return new StringColumnSchema(ColumnType::UUID, ...$info);
}

return parent::fromPseudoType($pseudoType, $info);
Expand All @@ -87,7 +91,8 @@ public function fromPseudoType(string $pseudoType, array $info = []): ColumnSche
public function fromType(string $type, array $info = []): ColumnSchemaInterface
{
if ($type === ColumnType::BINARY) {
return (new BinaryColumnSchema($type))->load($info);
unset($info['type']);
return new BinaryColumnSchema($type, ...$info);
}

return parent::fromType($type, $info);
Expand Down
2 changes: 1 addition & 1 deletion src/Dsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class Dsn extends AbstractDsn
*/
public function __construct(
string $driver = 'sqlsrv',
string $host = 'localhost',
string $host = '127.0.0.1',
string|null $databaseName = null,
string|null $port = '1433',
array $options = []
Expand Down
2 changes: 1 addition & 1 deletion tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public function testShowDatabases(): void

$command = $db->createCommand();

$this->assertSame('sqlsrv:Server=localhost,1433;Encrypt=no', $db->getDriver()->getDsn());
$this->assertSame('sqlsrv:Server=127.0.0.1,1433;Encrypt=no', $db->getDriver()->getDsn());
$this->assertSame(['yiitest'], $command->showDatabases());
}

Expand Down

0 comments on commit bb9a797

Please sign in to comment.