WP-Database-Helper/Table.php

122 lines
3.8 KiB
PHP
Raw Normal View History

2025-02-12 14:03:39 +00:00
<?php
namespace DatabaseHelper;
2025-02-21 18:06:16 +00:00
use DatabaseHelper\beans\Column;
use DatabaseHelper\beans\Primary;
use DatabaseHelper\beans\Reference;
use DatabaseHelper\beans\Schema;
2025-02-12 14:03:39 +00:00
use DatabaseHelper\enums\Propagation;
2025-02-12 15:55:03 +00:00
use DatabaseHelper\enums\Charset;
use DatabaseHelper\enums\Collation;
2025-02-12 14:03:39 +00:00
use DatabaseHelper\enums\Type;
2025-02-12 15:55:03 +00:00
use DatabaseHelper\enums\Engine;
2025-02-12 14:03:39 +00:00
2025-02-12 15:55:03 +00:00
class Table
2025-02-12 14:03:39 +00:00
{
2025-02-24 09:54:51 +00:00
protected Schema $schema;
2025-02-12 15:55:03 +00:00
public function __construct(string $tableName) {
Database::standardizeTableNames($tableName);
2025-02-24 09:54:51 +00:00
$this->schema = new Schema($tableName);
2025-02-12 14:03:39 +00:00
}
2025-02-12 15:55:03 +00:00
public function column(string $name, Type $type, mixed $default = null, bool $isNullable = false, bool $isUnique = false): Table {
2025-02-21 18:06:16 +00:00
$col = new Column;
$col->name = $name;
$col->default = $default;
$col->type = $type;
$col->isUnique = $isUnique;
$col->isNullable = $isNullable;
2025-02-24 09:54:51 +00:00
$this->schema->cols[$name] = $col;
2025-02-12 14:03:39 +00:00
return $this;
}
2025-02-21 18:06:16 +00:00
protected function id(): Table {
$id = new Primary;
2025-02-24 09:54:51 +00:00
$id->name = $this->schema->name . '_id';
$this->schema->id = $id;
2025-02-21 18:06:16 +00:00
return $this;
2025-02-12 14:03:39 +00:00
}
2025-02-12 15:55:03 +00:00
public function reference(Schema $foreignTable, Propagation $onDelete = Propagation::CASCADE, Propagation $onUpdate = Propagation::CASCADE): Table {
2025-02-24 09:54:51 +00:00
$name = $foreignTable->id->name;
2025-02-21 18:06:16 +00:00
$ref = new Reference;
$ref->name = $name;
$ref->otherTable = $foreignTable;
$ref->onDelete = $onDelete;
$ref->onUpdate = $onUpdate;
2025-02-24 09:54:51 +00:00
$this->schema->refs[$name] = $ref;
2025-02-12 14:03:39 +00:00
return $this;
}
2025-02-12 15:55:03 +00:00
public function engine(Engine $engine): Table {
2025-02-24 09:54:51 +00:00
$this->schema->engine = $engine;
2025-02-12 14:03:39 +00:00
return $this;
}
2025-02-12 15:55:03 +00:00
public function charset(Charset $charset): Table {
2025-02-24 09:54:51 +00:00
$this->schema->charset = $charset;
2025-02-12 15:55:03 +00:00
return $this;
}
public function collation(Collation $collation): Table {
2025-02-24 09:54:51 +00:00
$this->schema->collation = $collation;
2025-02-12 15:55:03 +00:00
return $this;
}
2025-02-12 14:03:39 +00:00
public function toSql(): string {
2025-02-24 09:54:51 +00:00
$primaryKey = $this->schema->id->name;
$clause = "CREATE TABLE `{$this->schema->name}` (\n";
2025-02-12 15:55:03 +00:00
$clause .= " PRIMARY KEY (`$primaryKey`),\n";
2025-02-12 14:03:39 +00:00
2025-02-24 09:54:51 +00:00
foreach ($this->schema->cols as $col) {
2025-02-21 18:06:16 +00:00
if ($col->name !== $primaryKey) {
$clause .= " `$col->name` {$col->type->toString()}";
$clause .= !$col->isNullable ? ' NOT NULL' : '';
$clause .= $col->isUnique ? ' UNIQUE' : '';
2025-02-12 15:55:03 +00:00
2025-02-21 18:06:16 +00:00
if (!is_null($col->default)) {
$default = is_string($col->default) ? "'{$col->default}'" : $col->default;
2025-02-12 15:55:03 +00:00
$clause .= " DEFAULT $default";
}
2025-02-12 14:03:39 +00:00
2025-02-12 15:55:03 +00:00
$clause .= ",\n";
}
2025-02-12 14:55:28 +00:00
}
2025-02-12 14:03:39 +00:00
2025-02-12 15:55:03 +00:00
// Add foreign keys
2025-02-24 09:54:51 +00:00
foreach ($this->schema->refs as $ref) {
2025-02-21 18:06:16 +00:00
$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";
2025-02-12 14:03:39 +00:00
}
2025-02-12 14:55:28 +00:00
2025-02-12 15:55:03 +00:00
// Close the SQL string and add constraints
$clause = rtrim($clause, ",\n") . "\n) ";
2025-02-24 09:54:51 +00:00
$clause .= "ENGINE={$this->schema->engine->toString()} ";
$clause .= "CHARSET={$this->schema->charset->toString()} ";
$clause .= "COLLATE={$this->schema->collation->toString()};";
2025-02-12 15:55:03 +00:00
return $clause;
2025-02-12 14:55:28 +00:00
}
2025-02-12 15:55:03 +00:00
public function create(): Schema {
2025-02-12 14:55:28 +00:00
global $wpdb;
2025-02-21 18:06:16 +00:00
$this->id();
2025-02-12 15:55:03 +00:00
2025-02-24 09:54:51 +00:00
Database::standardizeTableNames($this->schema->name);
$tableName = $this->schema->name;
2025-02-12 15:55:03 +00:00
2025-02-21 18:06:16 +00:00
$foundTables = $wpdb->get_var("SHOW TABLES LIKE '$tableName'");
if (is_null($foundTables)) {
2025-02-12 15:55:03 +00:00
$sql = $this->toSql();
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
2025-02-24 09:54:51 +00:00
return $this->schema;
2025-02-12 14:03:39 +00:00
}
2025-02-21 18:06:16 +00:00
}