Skip to content

Commit

Permalink
Load column properties via constructor (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Oct 18, 2024
1 parent 51becbe commit d992d1a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- New #282: Add `ColumnDefinitionBuilder` class (@Tigrov)
- Bug #285: Fix `DMLQueryBuilder::insertBatch()` method (@Tigrov)
- Enh #283: Refactor `Dsn` class (@Tigrov)
- Enh #286: Use constructor to create columns and initialize properties (@Tigrov)

## 1.3.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 @@ -31,7 +31,6 @@ public static function bigint(int|null $size = 20): ColumnSchemaInterface

public static function binary(int|null $size = null): ColumnSchemaInterface
{
return (new BinaryColumnSchema(ColumnType::BINARY))
->size($size);
return new BinaryColumnSchema(ColumnType::BINARY, size: $size);
}
}
7 changes: 3 additions & 4 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ protected function getType(string $dbType, array $info = []): string
$dbType = strtolower($dbType);

if ($dbType === 'number') {
$scale = isset($info['scale']) ? (int) $info['scale'] : null;

return match ($scale) {
return match ($info['scale'] ?? null) {
null => ColumnType::DOUBLE,
0 => ColumnType::INTEGER,
default => ColumnType::DECIMAL,
Expand All @@ -73,7 +71,8 @@ protected function getType(string $dbType, array $info = []): string
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
6 changes: 3 additions & 3 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* column_name: string,
* data_type: string,
* data_scale: string|null,
* size: string,
* size: string|null,
* nullable: string,
* data_default: string|null,
* is_pk: string|null,
Expand Down Expand Up @@ -430,8 +430,8 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface

$dbType = $info['data_type'];
$column = $columnFactory->fromDbType($dbType, [
'scale' => $info['data_scale'],
'size' => $info['size'],
'scale' => $info['data_scale'] !== null ? (int) $info['data_scale'] : null,
'size' => $info['size'] !== null ? (int) $info['size'] : null,
]);
/** @psalm-suppress DeprecatedMethod */
$column->name($info['column_name']);
Expand Down

0 comments on commit d992d1a

Please sign in to comment.