proceed further with beans

This commit is contained in:
Jan-Niclas Loosen
2025-02-24 10:54:51 +01:00
parent 024e6e73cb
commit 239b81bf0e
10 changed files with 156 additions and 197 deletions

View File

@@ -10,15 +10,14 @@ use DatabaseHelper\enums\Charset;
use DatabaseHelper\enums\Collation;
use DatabaseHelper\enums\Type;
use DatabaseHelper\enums\Engine;
use InvalidArgumentException;
class Table
{
protected Schema $table;
protected Schema $schema;
public function __construct(string $tableName) {
Database::standardizeTableNames($tableName);
$this->table = new Schema($tableName);
$this->schema = new Schema($tableName);
}
public function column(string $name, Type $type, mixed $default = null, bool $isNullable = false, bool $isUnique = false): Table {
@@ -29,19 +28,19 @@ class Table
$col->isUnique = $isUnique;
$col->isNullable = $isNullable;
$this->table->columns[$name] = $col;
$this->schema->cols[$name] = $col;
return $this;
}
protected function id(): Table {
$id = new Primary;
$id->name = $this->table->name . '_id';
$this->table->primary = $id;
$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->primary->name;
$name = $foreignTable->id->name;
$ref = new Reference;
$ref->name = $name;
@@ -49,31 +48,31 @@ class Table
$ref->onDelete = $onDelete;
$ref->onUpdate = $onUpdate;
$this->table->references[$name] = $ref;
$this->schema->refs[$name] = $ref;
return $this;
}
public function engine(Engine $engine): Table {
$this->table->engine = $engine;
$this->schema->engine = $engine;
return $this;
}
public function charset(Charset $charset): Table {
$this->table->charset = $charset;
$this->schema->charset = $charset;
return $this;
}
public function collation(Collation $collation): Table {
$this->table->collation = $collation;
$this->schema->collation = $collation;
return $this;
}
public function toSql(): string {
$primaryKey = $this->table->primary->name;
$clause = "CREATE TABLE `{$this->table->name}` (\n";
$primaryKey = $this->schema->id->name;
$clause = "CREATE TABLE `{$this->schema->name}` (\n";
$clause .= " PRIMARY KEY (`$primaryKey`),\n";
foreach ($this->table->columns as $col) {
foreach ($this->schema->cols as $col) {
if ($col->name !== $primaryKey) {
$clause .= " `$col->name` {$col->type->toString()}";
$clause .= !$col->isNullable ? ' NOT NULL' : '';
@@ -89,7 +88,7 @@ class Table
}
// Add foreign keys
foreach ($this->table->references as $ref) {
foreach ($this->schema->refs as $ref) {
$clause .= " FOREIGN KEY (`{$ref->name}`)";
$clause .= " REFERENCES `{$ref->otherTable->name}` (`{$ref->name}`)";
$clause .= " ON DELETE {$ref->onDelete->toString()}";
@@ -98,9 +97,9 @@ class Table
// 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()};";
$clause .= "ENGINE={$this->schema->engine->toString()} ";
$clause .= "CHARSET={$this->schema->charset->toString()} ";
$clause .= "COLLATE={$this->schema->collation->toString()};";
return $clause;
}
@@ -108,8 +107,8 @@ class Table
global $wpdb;
$this->id();
Database::standardizeTableNames($this->table->name);
$tableName = $this->table->name;
Database::standardizeTableNames($this->schema->name);
$tableName = $this->schema->name;
$foundTables = $wpdb->get_var("SHOW TABLES LIKE '$tableName'");
if (is_null($foundTables)) {
@@ -117,6 +116,6 @@ class Table
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
return $this->table;
return $this->schema;
}
}