WP-Database-Helper/Table.php
2025-02-21 19:06:16 +01:00

123 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;
use InvalidArgumentException;
class Table
{
protected Schema $table;
public function __construct(string $tableName) {
Database::standardizeTableNames($tableName);
$this->table = 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->table->columns[$name] = $col;
return $this;
}
protected function id(): Table {
$id = new Primary;
$id->name = $this->table->name . '_id';
$this->table->primary = $id;
return $this;
}
public function reference(Schema $foreignTable, Propagation $onDelete = Propagation::CASCADE, Propagation $onUpdate = Propagation::CASCADE): Table {
$name = $foreignTable->primary->name;
$ref = new Reference;
$ref->name = $name;
$ref->otherTable = $foreignTable;
$ref->onDelete = $onDelete;
$ref->onUpdate = $onUpdate;
$this->table->references[$name] = $ref;
return $this;
}
public function engine(Engine $engine): Table {
$this->table->engine = $engine;
return $this;
}
public function charset(Charset $charset): Table {
$this->table->charset = $charset;
return $this;
}
public function collation(Collation $collation): Table {
$this->table->collation = $collation;
return $this;
}
public function toSql(): string {
$primaryKey = $this->table->primary->name;
$clause = "CREATE TABLE `{$this->table->name}` (\n";
$clause .= " PRIMARY KEY (`$primaryKey`),\n";
foreach ($this->table->columns 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->table->references 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->table->engine->toString()} ";
$clause .= "CHARSET={$this->table->charset->toString()} ";
$clause .= "COLLATE={$this->table->collation->toString()};";
return $clause;
}
public function create(): Schema {
global $wpdb;
$this->id();
Database::standardizeTableNames($this->table->name);
$tableName = $this->table->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->table;
}
}