Skip to content

Commit

Permalink
Load column properties via constructor (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Oct 18, 2024
1 parent 82efe1a commit 4f85a22
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 52 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 @@
- Enh #362: Update according changes in `ColumnSchemaInterface` (@Tigrov)
- New #364: Add `ColumnDefinitionBuilder` class (@Tigrov)
- Enh #365: Refactor `Dsn` class (@Tigrov)
- Enh #366: Use constructor to create columns and initialize properties (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
25 changes: 8 additions & 17 deletions src/Column/ColumnBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,41 @@ public static function boolean(): ColumnSchemaInterface

public static function bit(int|null $size = null): ColumnSchemaInterface
{
return (new BitColumnSchema(ColumnType::BIT))
->size($size);
return new BitColumnSchema(ColumnType::BIT, size: $size);
}

public static function tinyint(int|null $size = null): ColumnSchemaInterface
{
return (new IntegerColumnSchema(ColumnType::TINYINT))
->size($size);
return new IntegerColumnSchema(ColumnType::TINYINT, size: $size);
}

public static function smallint(int|null $size = null): ColumnSchemaInterface
{
return (new IntegerColumnSchema(ColumnType::SMALLINT))
->size($size);
return new IntegerColumnSchema(ColumnType::SMALLINT, size: $size);
}

public static function integer(int|null $size = null): ColumnSchemaInterface
{
return (new IntegerColumnSchema(ColumnType::INTEGER))
->size($size);
return new IntegerColumnSchema(ColumnType::INTEGER, size: $size);
}

public static function bigint(int|null $size = null): ColumnSchemaInterface
{
return (new IntegerColumnSchema(ColumnType::BIGINT))
->size($size);
return new IntegerColumnSchema(ColumnType::BIGINT, size: $size);
}

public static function binary(int|null $size = null): ColumnSchemaInterface
{
return (new BinaryColumnSchema(ColumnType::BINARY))
->size($size);
return new BinaryColumnSchema(ColumnType::BINARY, size: $size);
}

public static function array(ColumnSchemaInterface|null $column = null): ColumnSchemaInterface
{
return (new ArrayColumnSchema(ColumnType::ARRAY))
->column($column);
return new ArrayColumnSchema(ColumnType::ARRAY, column: $column);
}

public static function structured(string|null $dbType = null, array $columns = []): ColumnSchemaInterface
{
return (new StructuredColumnSchema(ColumnType::STRUCTURED))
->dbType($dbType)
->columns($columns);
return new StructuredColumnSchema(ColumnType::STRUCTURED, dbType: $dbType, columns: $columns);
}
}
39 changes: 19 additions & 20 deletions src/Column/ColumnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,33 +120,32 @@ final class ColumnFactory extends AbstractColumnFactory
* @psalm-param ColumnInfo $info
* @psalm-suppress MoreSpecificImplementedParamType
* @psalm-suppress ArgumentTypeCoercion
* @psalm-suppress InvalidNamedArgument
* @psalm-suppress PossiblyInvalidArgument
*/
public function fromType(string $type, array $info = []): ColumnSchemaInterface
{
$dimension = (int)($info['dimension'] ?? 0);
$dimension = $info['dimension'] ?? 0;
unset($info['dimension']);

if ($dimension > 0) {
unset($info['dimension']);
$column = (new ArrayColumnSchema())
->dimension($dimension)
->column($info['column'] ?? $this->fromType($type, $info));
} else {
$column = match ($type) {
ColumnType::BOOLEAN => new BooleanColumnSchema($type),
ColumnType::BIT => new BitColumnSchema($type),
ColumnType::TINYINT => new IntegerColumnSchema($type),
ColumnType::SMALLINT => new IntegerColumnSchema($type),
ColumnType::INTEGER => new IntegerColumnSchema($type),
ColumnType::BIGINT => PHP_INT_SIZE !== 8
? new BigIntColumnSchema($type)
: new IntegerColumnSchema($type),
ColumnType::BINARY => new BinaryColumnSchema($type),
ColumnType::STRUCTURED => (new StructuredColumnSchema($type))->columns($info['columns'] ?? []),
default => parent::fromType($type, $info),
};
$info['column'] ??= $this->fromType($type, $info);
return new ArrayColumnSchema(...$info, dimension: $dimension);
}

return $column->load($info);
return match ($type) {
ColumnType::BOOLEAN => new BooleanColumnSchema($type, ...$info),
ColumnType::BIT => new BitColumnSchema($type, ...$info),
ColumnType::TINYINT => new IntegerColumnSchema($type, ...$info),
ColumnType::SMALLINT => new IntegerColumnSchema($type, ...$info),
ColumnType::INTEGER => new IntegerColumnSchema($type, ...$info),
ColumnType::BIGINT => PHP_INT_SIZE !== 8
? new BigIntColumnSchema($type, ...$info)
: new IntegerColumnSchema($type, ...$info),
ColumnType::BINARY => new BinaryColumnSchema($type, ...$info),
ColumnType::STRUCTURED => new StructuredColumnSchema($type, ...$info),
default => parent::fromType($type, $info),
};
}

protected function getType(string $dbType, array $info = []): string
Expand Down
34 changes: 19 additions & 15 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@
* data_type: string,
* type_type: string|null,
* type_scheme: string|null,
* character_maximum_length: int,
* character_maximum_length: int|string,
* column_comment: string|null,
* is_nullable: bool,
* is_nullable: bool|string,
* column_default: string|null,
* is_autoinc: bool,
* is_autoinc: bool|string,
* sequence_name: string|null,
* enum_values: string|null,
* size: int|null,
* scale: int|null,
* size: int|string|null,
* scale: int|string|null,
* contype: string|null,
* dimension: int
* dimension: int|string
* }
* @psalm-type ConstraintArray = array<
* array-key,
Expand Down Expand Up @@ -742,26 +742,31 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface
$columns = $structured->getColumns();
}

/** @psalm-suppress ArgumentTypeCoercion */
$column = $columnFactory
->fromType(ColumnType::STRUCTURED, ['dimension' => $info['dimension'], 'columns' => $columns]);
->fromType(ColumnType::STRUCTURED, [
'columns' => $columns,
'dbType' => $dbType,
'dimension' => (int) $info['dimension'],
]);
} else {
/** @psalm-suppress ArgumentTypeCoercion */
$column = $columnFactory
->fromDbType($dbType, ['dimension' => $info['dimension']]);
->fromDbType($dbType, ['dimension' => (int) $info['dimension']]);
}

/** @psalm-suppress DeprecatedMethod */
$column->name($info['column_name']);
$column->dbType($dbType);
$column->notNull(!$info['is_nullable']);
$column->autoIncrement($info['is_autoinc']);
$column->autoIncrement((bool) $info['is_autoinc']);
$column->comment($info['column_comment']);
$column->enumValues($info['enum_values'] !== null
? explode(',', str_replace(["''"], ["'"], $info['enum_values']))
: null);
$column->primaryKey($info['contype'] === 'p');
$column->unique($info['contype'] === 'u');
$column->scale($info['scale']);
$column->size($info['size']);
$column->scale($info['scale'] !== null ? (int) $info['scale'] : null);
$column->size($info['size'] !== null ? (int) $info['size'] : null);

/**
* pg_get_serial_sequence() doesn't track DEFAULT value change.
Expand All @@ -781,10 +786,9 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface
} elseif ($column instanceof ArrayColumnSchema) {
/** @var ColumnSchemaInterface $arrayColumn */
$arrayColumn = $column->getColumn();
$arrayColumn->dbType($dbType);
$arrayColumn->enumValues($column->getEnumValues());
$arrayColumn->scale($info['scale']);
$arrayColumn->size($info['size']);
$arrayColumn->scale($column->getScale());
$arrayColumn->size($column->getSize());
}

$column->defaultValue($this->normalizeDefaultValue($defaultValue, $column));
Expand Down

0 comments on commit 4f85a22

Please sign in to comment.