Skip to content

Commit

Permalink
schema: rewrite to readonly classes
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Feb 12, 2023
1 parent 0c62a78 commit 36626a8
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 94 deletions.
30 changes: 17 additions & 13 deletions src/Platforms/Data/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ class Column
use StrictObjectTrait;


public string $name;
public string $type;
public ?int $size;
public ?string $default;
public bool $isPrimary;
public bool $isAutoincrement;
public bool $isUnsigned;
public bool $isNullable;
/**
* @var mixed[]
* @phpstan-var array<string, mixed>
*/
public array $meta = [];
public function __construct(
public readonly string $name,
public readonly string $type,
public readonly ?int $size,
public readonly ?string $default,
public readonly bool $isPrimary,
public readonly bool $isAutoincrement,
public readonly bool $isUnsigned,
public readonly bool $isNullable,
/**
* @var mixed[]
* @phpstan-var array<string, mixed>
*/
public array $meta = [],
)
{
}
}
16 changes: 10 additions & 6 deletions src/Platforms/Data/ForeignKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ class ForeignKey
use StrictObjectTrait;


public string $name;
public string $schema;
public string $column;
public string $refTable;
public string $refTableSchema;
public string $refColumn;
public function __construct(
public readonly string $name,
public readonly string $schema,
public readonly string $column,
public readonly string $refTable,
public readonly string $refTableSchema,
public readonly string $refColumn,
)
{
}


public function getNameFqn(): string
Expand Down
10 changes: 7 additions & 3 deletions src/Platforms/Data/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ class Table
use StrictObjectTrait;


public string $name;
public string $schema;
public bool $isView;
public function __construct(
public readonly string $name,
public readonly string $schema,
public readonly bool $isView = false,
)
{
}


/**
Expand Down
48 changes: 24 additions & 24 deletions src/Platforms/MySqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public function getTables(?string $schema = null): array

$tables = [];
foreach ($result as $row) {
$table = new Table();
$table->name = (string) $row->TABLE_NAME;
$table->schema = (string) $row->TABLE_SCHEMA;
$table->isView = $row->TABLE_TYPE === 'VIEW';

$table = new Table(
name: (string) $row->TABLE_NAME,
schema: (string) $row->TABLE_SCHEMA,
isView: $row->TABLE_TYPE === 'VIEW',
);
$tables[$table->getUnescapedFqn()] = $table;
}
return $tables;
Expand All @@ -81,17 +81,17 @@ public function getColumns(string $table, ?string $schema = null): array
foreach ($query as $row) {
$type = explode('(', (string) $row->Type);

$column = new Column();
$column->name = (string) $row->Field;
$column->type = strtoupper($type[0]);
$column->size = isset($type[1]) ? (int) $type[1] : null;
$column->default = $row->Default !== null ? (string) $row->Default : null;
$column->isPrimary = $row->Key === 'PRI';
$column->isAutoincrement = $row->Extra === 'auto_increment';
$column->isUnsigned = (bool) strstr((string) $row->Type, 'unsigned');
$column->isNullable = $row->Null === 'YES';
$column->meta = [];

$column = new Column(
name: (string) $row->Field,
type: strtoupper($type[0]),
size: isset($type[1]) ? (int) $type[1] : null,
default: $row->Default !== null ? (string) $row->Default : null,
isPrimary: $row->Key === 'PRI',
isAutoincrement: $row->Extra === 'auto_increment',
isUnsigned: (bool) strstr((string) $row->Type, 'unsigned'),
isNullable: $row->Null === 'YES',
meta: [],
);
$columns[$column->name] = $column;
}
return $columns;
Expand Down Expand Up @@ -122,14 +122,14 @@ public function getForeignKeys(string $table, ?string $schema = null): array
/** @var array<string, ForeignKey> $keys */
$keys = [];
foreach ($result as $row) {
$foreignKey = new ForeignKey();
$foreignKey->name = (string) $row->CONSTRAINT_NAME;
$foreignKey->schema = (string) $row->CONSTRAINT_SCHEMA;
$foreignKey->column = (string) $row->COLUMN_NAME;
$foreignKey->refTable = (string) $row->REFERENCED_TABLE_NAME;
$foreignKey->refTableSchema = (string) $row->REFERENCED_TABLE_SCHEMA;
$foreignKey->refColumn = (string) $row->REFERENCED_COLUMN_NAME;

$foreignKey = new ForeignKey(
name: (string) $row->CONSTRAINT_NAME,
schema: (string) $row->CONSTRAINT_SCHEMA,
column: (string) $row->COLUMN_NAME,
refTable: (string) $row->REFERENCED_TABLE_NAME,
refTableSchema: (string) $row->REFERENCED_TABLE_SCHEMA,
refColumn: (string) $row->REFERENCED_COLUMN_NAME,
);
$keys[$foreignKey->column] = $foreignKey;
}
return $keys;
Expand Down
48 changes: 24 additions & 24 deletions src/Platforms/PostgreSqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ public function getTables(?string $schema = null): array

$tables = [];
foreach ($result as $row) {
$table = new Table();
$table->name = (string) $row->name;
$table->schema = (string) $row->schema;
$table->isView = (bool) $row->is_view;

$table = new Table(
name: (string) $row->name,
schema: (string) $row->schema,
isView: (bool) $row->is_view,
);
$tables[$table->getUnescapedFqn()] = $table;
}
return $tables;
Expand Down Expand Up @@ -110,17 +110,17 @@ public function getColumns(string $table, ?string $schema = null): array

$columns = [];
foreach ($result as $row) {
$column = new Column();
$column->name = (string) $row->name;
$column->type = (string) $row->type;
$column->size = $row->size !== null ? (int) $row->size : null;
$column->default = $row->default !== null ? (string) $row->default : null;
$column->isPrimary = (bool) $row->is_primary;
$column->isAutoincrement = (bool) $row->is_autoincrement;
$column->isUnsigned = false;
$column->isNullable = (bool) $row->is_nullable;
$column->meta = isset($row->sequence) ? ['sequence' => $row->sequence] : [];

$column = new Column(
name: (string) $row->name,
type: (string) $row->type,
size: $row->size !== null ? (int) $row->size : null,
default: $row->default !== null ? (string) $row->default : null,
isPrimary: (bool) $row->is_primary,
isAutoincrement: (bool) $row->is_autoincrement,
isUnsigned: false,
isNullable: (bool) $row->is_nullable,
meta: isset($row->sequence) ? ['sequence' => $row->sequence] : [],
);
$columns[$column->name] = $column;
}
return $columns;
Expand Down Expand Up @@ -157,14 +157,14 @@ public function getForeignKeys(string $table, ?string $schema = null): array

$keys = [];
foreach ($result as $row) {
$foreignKey = new ForeignKey();
$foreignKey->name = (string) $row->name;
$foreignKey->schema = (string) $row->schema;
$foreignKey->column = (string) $row->column;
$foreignKey->refTable = (string) $row->ref_table;
$foreignKey->refTableSchema = (string) $row->ref_table_schema;
$foreignKey->refColumn = (string) $row->ref_column;

$foreignKey = new ForeignKey(
name: (string) $row->name,
schema: (string) $row->schema,
column: (string) $row->column,
refTable: (string) $row->ref_table,
refTableSchema: (string) $row->ref_table_schema,
refColumn: (string) $row->ref_column,
);
$keys[$foreignKey->column] = $foreignKey;
}
return $keys;
Expand Down
48 changes: 24 additions & 24 deletions src/Platforms/SqlServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public function getTables(?string $schema = null): array

$tables = [];
foreach ($result as $row) {
$table = new Table();
$table->name = (string) $row->TABLE_NAME;
$table->schema = (string) $row->TABLE_SCHEMA;
$table->isView = $row->TABLE_TYPE === 'VIEW';

$table = new Table(
name: (string) $row->TABLE_NAME,
schema: (string) $row->TABLE_SCHEMA,
isView: $row->TABLE_TYPE === 'VIEW',
);
$tables[$table->getUnescapedFqn()] = $table;
}
return $tables;
Expand Down Expand Up @@ -105,17 +105,17 @@ public function getColumns(string $table, ?string $schema = null): array

$columns = [];
foreach ($result as $row) {
$column = new Column();
$column->name = (string) $row->name;
$column->type = (string) $row->type;
$column->size = $row->size !== null ? (int) $row->size : null;
$column->default = $row->default !== null ? (string) $row->default : null;
$column->isPrimary = (bool) $row->is_primary;
$column->isAutoincrement = (bool) $row->is_autoincrement;
$column->isUnsigned = false; // not available in SqlServer
$column->isNullable = (bool) $row->is_nullable;
$column->meta = [];

$column = new Column(
name: (string) $row->name,
type: (string) $row->type,
size: $row->size !== null ? (int) $row->size : null,
default: $row->default !== null ? (string) $row->default : null,
isPrimary: (bool) $row->is_primary,
isAutoincrement: (bool) $row->is_autoincrement,
isUnsigned: false, // not available in SqlServer
isNullable: (bool) $row->is_nullable,
meta: [],
);
$columns[$column->name] = $column;
}

Expand Down Expand Up @@ -154,14 +154,14 @@ public function getForeignKeys(string $table, ?string $schema = null): array

$keys = [];
foreach ($result as $row) {
$foreignKey = new ForeignKey();
$foreignKey->name = (string) $row->name;
$foreignKey->schema = (string) $row->schema;
$foreignKey->column = (string) $row->column;
$foreignKey->refTable = (string) $row->ref_table;
$foreignKey->refTableSchema = (string) $row->ref_table_schema;
$foreignKey->refColumn = (string) $row->ref_column;

$foreignKey = new ForeignKey(
name: (string) $row->name,
schema: (string) $row->schema,
column: (string) $row->column,
refTable: (string) $row->ref_table,
refTableSchema: (string) $row->ref_table_schema,
refColumn: (string) $row->ref_column,
);
$keys[$foreignKey->column] = $foreignKey;
}
return $keys;
Expand Down

0 comments on commit 36626a8

Please # to comment.