122 lines
3.8 KiB
PHP
122 lines
3.8 KiB
PHP
<?php
|
|
namespace DatabaseHelper;
|
|
|
|
use DatabaseHelper\beans\Column;
|
|
use DatabaseHelper\beans\Primary;
|
|
use DatabaseHelper\beans\Reference;
|
|
use DatabaseHelper\beans\Schema;
|
|
use DatabaseHelper\enums\Propagation;
|
|
use DatabaseHelper\enums\Charset;
|
|
use DatabaseHelper\enums\Collation;
|
|
use DatabaseHelper\enums\Type;
|
|
use DatabaseHelper\enums\Engine;
|
|
|
|
class Table
|
|
{
|
|
protected Schema $schema;
|
|
|
|
public function __construct(string $tableName) {
|
|
Database::standardizeTableNames($tableName);
|
|
$this->schema = new Schema($tableName);
|
|
}
|
|
|
|
public function column(string $name, Type $type, mixed $default = null, bool $isNullable = false, bool $isUnique = false): Table {
|
|
$col = new Column;
|
|
$col->name = $name;
|
|
$col->default = $default;
|
|
$col->type = $type;
|
|
$col->isUnique = $isUnique;
|
|
$col->isNullable = $isNullable;
|
|
|
|
$this->schema->cols[$name] = $col;
|
|
return $this;
|
|
}
|
|
|
|
protected function id(): Table {
|
|
$id = new Primary;
|
|
$id->name = $this->schema->name . '_id';
|
|
$this->schema->id = $id;
|
|
return $this;
|
|
}
|
|
|
|
public function reference(Schema $foreignTable, Propagation $onDelete = Propagation::CASCADE, Propagation $onUpdate = Propagation::CASCADE): Table {
|
|
$name = $foreignTable->id->name;
|
|
|
|
$ref = new Reference;
|
|
$ref->name = $name;
|
|
$ref->otherTable = $foreignTable;
|
|
$ref->onDelete = $onDelete;
|
|
$ref->onUpdate = $onUpdate;
|
|
|
|
$this->schema->refs[$name] = $ref;
|
|
return $this;
|
|
}
|
|
|
|
public function engine(Engine $engine): Table {
|
|
$this->schema->engine = $engine;
|
|
return $this;
|
|
}
|
|
|
|
public function charset(Charset $charset): Table {
|
|
$this->schema->charset = $charset;
|
|
return $this;
|
|
}
|
|
|
|
public function collation(Collation $collation): Table {
|
|
$this->schema->collation = $collation;
|
|
return $this;
|
|
}
|
|
|
|
public function toSql(): string {
|
|
$primaryKey = $this->schema->id->name;
|
|
$clause = "CREATE TABLE `{$this->schema->name}` (\n";
|
|
$clause .= " PRIMARY KEY (`$primaryKey`),\n";
|
|
|
|
foreach ($this->schema->cols as $col) {
|
|
if ($col->name !== $primaryKey) {
|
|
$clause .= " `$col->name` {$col->type->toString()}";
|
|
$clause .= !$col->isNullable ? ' NOT NULL' : '';
|
|
$clause .= $col->isUnique ? ' UNIQUE' : '';
|
|
|
|
if (!is_null($col->default)) {
|
|
$default = is_string($col->default) ? "'{$col->default}'" : $col->default;
|
|
$clause .= " DEFAULT $default";
|
|
}
|
|
|
|
$clause .= ",\n";
|
|
}
|
|
}
|
|
|
|
// Add foreign keys
|
|
foreach ($this->schema->refs as $ref) {
|
|
$clause .= " FOREIGN KEY (`{$ref->name}`)";
|
|
$clause .= " REFERENCES `{$ref->otherTable->name}` (`{$ref->name}`)";
|
|
$clause .= " ON DELETE {$ref->onDelete->toString()}";
|
|
$clause .= " ON UPDATE {$ref->onUpdate->toString()},\n";
|
|
}
|
|
|
|
// Close the SQL string and add constraints
|
|
$clause = rtrim($clause, ",\n") . "\n) ";
|
|
$clause .= "ENGINE={$this->schema->engine->toString()} ";
|
|
$clause .= "CHARSET={$this->schema->charset->toString()} ";
|
|
$clause .= "COLLATE={$this->schema->collation->toString()};";
|
|
return $clause;
|
|
}
|
|
|
|
public function create(): Schema {
|
|
global $wpdb;
|
|
$this->id();
|
|
|
|
Database::standardizeTableNames($this->schema->name);
|
|
$tableName = $this->schema->name;
|
|
|
|
$foundTables = $wpdb->get_var("SHOW TABLES LIKE '$tableName'");
|
|
if (is_null($foundTables)) {
|
|
$sql = $this->toSql();
|
|
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
|
|
dbDelta($sql);
|
|
}
|
|
return $this->schema;
|
|
}
|
|
}
|