Skip to content

Commit

Permalink
fix: added type_details getter
Browse files Browse the repository at this point in the history
  • Loading branch information
rozsazoltan committed Nov 30, 2024
1 parent 5039d85 commit a555886
Showing 1 changed file with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Schema\ColumnDefinition;
use Illuminate\Support\Collection;
use Schema;
use Str;

class ColumnDefinitionBuilder
{
Expand All @@ -32,7 +33,7 @@ public function get(string $column): ColumnDefinition
throw new ColumnNotFoundException($this->table, $column);
}

$this->definition = $this->applyColumnType($columnData['type_name'], $columnData['type_details']);
$this->definition = $this->applyColumn($columnData);
$this->applyColumnProperties($columnData);

return $this->definition->change();
Expand All @@ -47,18 +48,93 @@ private function getColumnData(): ?array
}

/**
* Apply the column type and its parameters.
* Get column definition
*/
private function applyColumnType(string $type, array $parameters): ColumnDefinition
private function applyColumn(array $column): ColumnDefinition
{
return $this->blueprint->addColumn($this->getLaravelTypeName($type), $this->column, $parameters);
return $this->blueprint->addColumn($this->getLaravelTypeName($column['type_name']), $column['name'], $this->getTypeDetails($column['type']));
}

/**
* Get name of Laravel Type
*/
private function getLaravelTypeName(string $type): string
{
return BlueprintType::fromSqlType($type)->value;
}

/**
* Get details of type
*/
private function getTypeDetails(string $type): array
{
// Parse type details with match
return match (true) {
Str::startsWith($type, 'varchar') ||
Str::startsWith($type, 'char') => [
'length' => (int) Str::of($type)->match('/\((\d+)\)/')->toString() ?: null,
'compressed' => Str::of($type)->lower()->contains('compressed'),
],

Str::startsWith($type, 'binary') => [
'fixed' => true,
],

Str::startsWith($type, 'tinyint') ||
Str::startsWith($type, 'smallint') ||
Str::startsWith($type, 'mediumint') ||
Str::startsWith($type, 'bigint') ||
Str::startsWith($type, 'integer') => [
'zerofill' => Str::of($type)->lower()->contains('zerofill'),
],

Str::startsWith($type, 'float') ||
Str::startsWith($type, 'double') ||
Str::startsWith($type, 'real') => [
'precision' => (int) Str::of($type)->match('/\((\d+)\)/')->toString(),
],

Str::startsWith($type, 'decimal') ||
Str::startsWith($type, 'numeric') => [
'total' => (int) Str::of($type)->match('/\((\d+),\d+\)/')->toString(),
'places' => (int) Str::of($type)->match('/\(\d+,(\d+)\)/')->toString(),
],

Str::startsWith($type, 'datetime') ||
Str::startsWith($type, 'timestamp') => [
'precision' => (int) Str::of($type)->match('/\((\d+)\)/')->toString(),
],

Str::startsWith($type, 'enum') => [
'allowed' => explode(',', Str::of($type)->match('/\((.+)\)/')->replace("'", '')->__toString()),
],

Str::startsWith($type, 'geometry') ||
Str::startsWith($type, 'geography') => [
'subtype' => Str::of($type)->after(':')->before('(')->__toString() ?: null,
'srid' => (int) Str::of($type)->match('/srid=(\d+)/i')->toString(),
],

Str::startsWith($type, 'computed') => [
'expression' => true,
],

Str::startsWith($type, 'vector') => [
'dimensions' => (int) Str::of($type)->match('/\((\d+)\)/')->toString(),
],

Str::startsWith($type, 'bit') => [
'length' => (int) Str::of($type)->match('/\((\d+)\)/')->toString() ?: null,
],

Str::startsWith($type, 'set') => [
'allowed' => explode(',', Str::of($type)->match('/\((.+)\)/')->replace("'", '')->__toString()),
],

default => [],
};
}

/**
* Apply column properties (nullable, default, unsigned, etc.).
*/
Expand Down

0 comments on commit a555886

Please sign in to comment.