schema with beans instead of arrays
This commit is contained in:
		@@ -1,6 +1,7 @@
 | 
				
			|||||||
<?php
 | 
					<?php
 | 
				
			||||||
namespace DatabaseHelper;
 | 
					namespace DatabaseHelper;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use DatabaseHelper\beans\Schema;
 | 
				
			||||||
use DatabaseHelper\enums\Operator;
 | 
					use DatabaseHelper\enums\Operator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
trait Conditionable
 | 
					trait Conditionable
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,8 @@
 | 
				
			|||||||
<?php
 | 
					<?php
 | 
				
			||||||
namespace DatabaseHelper;
 | 
					namespace DatabaseHelper;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use DatabaseHelper\beans\Schema;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Database
 | 
					class Database
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
<?php
 | 
					<?php
 | 
				
			||||||
namespace DatabaseHelper;
 | 
					namespace DatabaseHelper;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use DatabaseHelper\beans\Schema;
 | 
				
			||||||
use Exception;
 | 
					use Exception;
 | 
				
			||||||
use InvalidArgumentException;
 | 
					use InvalidArgumentException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
<?php
 | 
					<?php
 | 
				
			||||||
namespace DatabaseHelper;
 | 
					namespace DatabaseHelper;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use DatabaseHelper\beans\Schema;
 | 
				
			||||||
use InvalidArgumentException;
 | 
					use InvalidArgumentException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Insertion
 | 
					class Insertion
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
<?php
 | 
					<?php
 | 
				
			||||||
namespace DatabaseHelper;
 | 
					namespace DatabaseHelper;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use DatabaseHelper\beans\Schema;
 | 
				
			||||||
use DatabaseHelper\enums\Propagation;
 | 
					use DatabaseHelper\enums\Propagation;
 | 
				
			||||||
use DatabaseHelper\enums\Type;
 | 
					use DatabaseHelper\enums\Type;
 | 
				
			||||||
use InvalidArgumentException;
 | 
					use InvalidArgumentException;
 | 
				
			||||||
@@ -36,7 +37,7 @@ class Migration
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    public function modify(string $name, Type $type, mixed $default = null, bool $isNullable = false, bool $isUnique = false): Migration {
 | 
					    public function modify(string $name, Type $type, mixed $default = null, bool $isNullable = false, bool $isUnique = false): Migration {
 | 
				
			||||||
        $this->schema->requireColumn($name);
 | 
					        $this->schema->requireColumn($name);
 | 
				
			||||||
        if (isset($this->schema->foreignKeys[$name]))
 | 
					        if (isset($this->schema->references[$name]))
 | 
				
			||||||
            throw new InvalidArgumentException('Referencing columns cannot be modified.');
 | 
					            throw new InvalidArgumentException('Referencing columns cannot be modified.');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->schema->columns[$name] = [
 | 
					        $this->schema->columns[$name] = [
 | 
				
			||||||
@@ -62,7 +63,7 @@ class Migration
 | 
				
			|||||||
        $name = $foreignTable->primaryKey();
 | 
					        $name = $foreignTable->primaryKey();
 | 
				
			||||||
        $this->schema->requireColumn($name);
 | 
					        $this->schema->requireColumn($name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->schema->foreignKeys[$name] = [
 | 
					        $this->schema->references[$name] = [
 | 
				
			||||||
            'name'     => $name,
 | 
					            'name'     => $name,
 | 
				
			||||||
            'table'    => $foreignTable->name,
 | 
					            'table'    => $foreignTable->name,
 | 
				
			||||||
            'onDelete' => $onDelete,
 | 
					            'onDelete' => $onDelete,
 | 
				
			||||||
@@ -79,7 +80,7 @@ class Migration
 | 
				
			|||||||
        if ($this->schema->existsReference($foreignTable))
 | 
					        if ($this->schema->existsReference($foreignTable))
 | 
				
			||||||
            throw new InvalidArgumentException('Foreign table is not referenced.');
 | 
					            throw new InvalidArgumentException('Foreign table is not referenced.');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        unset($this->schema->foreignKeys[$name]);
 | 
					        unset($this->schema->references[$name]);
 | 
				
			||||||
        $this->drop($name);
 | 
					        $this->drop($name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->foreignKeysToDrop[] = $name;
 | 
					        $this->foreignKeysToDrop[] = $name;
 | 
				
			||||||
@@ -131,7 +132,7 @@ class Migration
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        // Process foreign keys to add.
 | 
					        // Process foreign keys to add.
 | 
				
			||||||
        foreach ($this->foreignKeysToAdd as $key) {
 | 
					        foreach ($this->foreignKeysToAdd as $key) {
 | 
				
			||||||
            $foreignKey = $this->schema->foreignKeys[$key];
 | 
					            $foreignKey = $this->schema->references[$key];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $clause = "  ADD CONSTRAINT `fk_{$foreignKey['name']}`";
 | 
					            $clause = "  ADD CONSTRAINT `fk_{$foreignKey['name']}`";
 | 
				
			||||||
            $clause .= " FOREIGN KEY (`{$foreignKey['name']}`)";
 | 
					            $clause .= " FOREIGN KEY (`{$foreignKey['name']}`)";
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,11 +1,11 @@
 | 
				
			|||||||
<?php
 | 
					<?php
 | 
				
			||||||
namespace DatabaseHelper;
 | 
					namespace DatabaseHelper;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use DatabaseHelper\beans\Schema;
 | 
				
			||||||
use DatabaseHelper\enums\Aggregation;
 | 
					use DatabaseHelper\enums\Aggregation;
 | 
				
			||||||
use DatabaseHelper\enums\Join;
 | 
					use DatabaseHelper\enums\Join;
 | 
				
			||||||
use DatabaseHelper\enums\Order;
 | 
					use DatabaseHelper\enums\Order;
 | 
				
			||||||
use http\Exception\InvalidArgumentException;
 | 
					use http\Exception\InvalidArgumentException;
 | 
				
			||||||
use SimplePie\Exception;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Query
 | 
					class Query
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -41,9 +41,9 @@ class Query
 | 
				
			|||||||
    public function join(Join $join, Schema $other): Query {
 | 
					    public function join(Join $join, Schema $other): Query {
 | 
				
			||||||
        $foreignKey = null;
 | 
					        $foreignKey = null;
 | 
				
			||||||
        if($this->schema->existsReference($other))
 | 
					        if($this->schema->existsReference($other))
 | 
				
			||||||
            $foreignKey = $this->schema->foreignKeys[$other->name];
 | 
					            $foreignKey = $this->schema->references[$other->name];
 | 
				
			||||||
        if ($other->existsReference($this->schema))
 | 
					        if ($other->existsReference($this->schema))
 | 
				
			||||||
            $foreignKey = $other->foreignKeys[$this->schema->name];
 | 
					            $foreignKey = $other->references[$this->schema->name];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (is_null($foreignKey))
 | 
					        if (is_null($foreignKey))
 | 
				
			||||||
            throw new InvalidArgumentException('Joins can only applied to referencing columns.');
 | 
					            throw new InvalidArgumentException('Joins can only applied to referencing columns.');
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										81
									
								
								Schema.php
									
									
									
									
									
								
							
							
						
						
									
										81
									
								
								Schema.php
									
									
									
									
									
								
							@@ -1,81 +0,0 @@
 | 
				
			|||||||
<?php
 | 
					 | 
				
			||||||
namespace DatabaseHelper;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
use DatabaseHelper\enums\Aggregation;
 | 
					 | 
				
			||||||
use DatabaseHelper\enums\Charset;
 | 
					 | 
				
			||||||
use DatabaseHelper\enums\Collation;
 | 
					 | 
				
			||||||
use DatabaseHelper\enums\Engine;
 | 
					 | 
				
			||||||
use http\Exception\InvalidArgumentException;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class Schema
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    public string $name = '';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public array $columns = [];
 | 
					 | 
				
			||||||
    public array $primaryKey = [];
 | 
					 | 
				
			||||||
    public array $foreignKeys = [];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public Engine $engine = Engine::INNODB;
 | 
					 | 
				
			||||||
    public Charset $charset = Charset::UTF8;
 | 
					 | 
				
			||||||
    public Collation $collation = Collation::UTF8_GENERAL_CI;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function __construct(string $name) {
 | 
					 | 
				
			||||||
        $this->name = $name;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function requireColumn(string $col): void {
 | 
					 | 
				
			||||||
        if (!$this->existsColumn($col))
 | 
					 | 
				
			||||||
            throw new InvalidArgumentException("Column '$col' is not defined.");
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function existsColumn(string $col): bool {
 | 
					 | 
				
			||||||
        return isset($this->columns[$col]);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function existsReference(Schema $schema): bool {
 | 
					 | 
				
			||||||
        foreach ($this->foreignKeys as $foreignKey)
 | 
					 | 
				
			||||||
            if ($foreignKey['table'] == $schema->name)
 | 
					 | 
				
			||||||
                return true;
 | 
					 | 
				
			||||||
        return false;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function countEntries(): int {
 | 
					 | 
				
			||||||
        return Database::makeQuery($this)
 | 
					 | 
				
			||||||
            ->aggregate('*', 'count', Aggregation::COUNT)
 | 
					 | 
				
			||||||
            ->query();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function columnType(string $col) {
 | 
					 | 
				
			||||||
        return $this->columns[$col]['type'];
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function primaryKey() {
 | 
					 | 
				
			||||||
        return $this->columns['primary']['name'];
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function copy(): Schema {
 | 
					 | 
				
			||||||
        $copy = new Schema($this->name);
 | 
					 | 
				
			||||||
        $copy->columns = genericDeepCopy($this->columns);
 | 
					 | 
				
			||||||
        $copy->primaryKey = genericDeepCopy($this->primaryKey);
 | 
					 | 
				
			||||||
        $copy->foreignKeys = genericDeepCopy($this->foreignKeys);
 | 
					 | 
				
			||||||
        $copy->engine = $this->engine;
 | 
					 | 
				
			||||||
        $copy->charset = $this->charset;
 | 
					 | 
				
			||||||
        $copy->collation = $this->collation;
 | 
					 | 
				
			||||||
        return $copy;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function include(Schema $other): void {
 | 
					 | 
				
			||||||
        // Create a copy of the other schema.
 | 
					 | 
				
			||||||
        $otherCopy = $other->copy();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Add any column that isn't already defined.
 | 
					 | 
				
			||||||
        foreach ($otherCopy->columns as $colName => $colDef)
 | 
					 | 
				
			||||||
            if (!isset($this->columns[$colName]))
 | 
					 | 
				
			||||||
                $this->columns[$colName] = $colDef;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Add any foreign key that doesn't already exist.
 | 
					 | 
				
			||||||
        foreach ($other->foreignKeys as $colName => $foreignKey)
 | 
					 | 
				
			||||||
            if (!isset($this->foreignKeys[$colName]))
 | 
					 | 
				
			||||||
                $this->foreignKeys[$colName] = $foreignKey;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										92
									
								
								Table.php
									
									
									
									
									
								
							
							
						
						
									
										92
									
								
								Table.php
									
									
									
									
									
								
							@@ -1,6 +1,10 @@
 | 
				
			|||||||
<?php
 | 
					<?php
 | 
				
			||||||
namespace DatabaseHelper;
 | 
					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\Propagation;
 | 
				
			||||||
use DatabaseHelper\enums\Charset;
 | 
					use DatabaseHelper\enums\Charset;
 | 
				
			||||||
use DatabaseHelper\enums\Collation;
 | 
					use DatabaseHelper\enums\Collation;
 | 
				
			||||||
@@ -18,42 +22,34 @@ class Table
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function column(string $name, Type $type, mixed $default = null, bool $isNullable = false, bool $isUnique = false): Table {
 | 
					    public function column(string $name, Type $type, mixed $default = null, bool $isNullable = false, bool $isUnique = false): Table {
 | 
				
			||||||
        $this->table->columns[$name] = [
 | 
					        $col = new Column;
 | 
				
			||||||
            'name' => $name,
 | 
					        $col->name = $name;
 | 
				
			||||||
            'default' => $default,
 | 
					        $col->default = $default;
 | 
				
			||||||
            'type' => $type,
 | 
					        $col->type = $type;
 | 
				
			||||||
            'isNullable' => $isNullable,
 | 
					        $col->isUnique = $isUnique;
 | 
				
			||||||
            'isUnique' => $isUnique
 | 
					        $col->isNullable = $isNullable;
 | 
				
			||||||
        ];
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->table->columns[$name] = $col;
 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function primary(string $name, Type $type, bool $autoInc = false): Table {
 | 
					    protected function id(): Table {
 | 
				
			||||||
        if(isset($this->primaryKey))
 | 
					        $id = new Primary;
 | 
				
			||||||
            throw new InvalidArgumentException('Primary column already exists.');
 | 
					        $id->name = $this->table->name . '_id';
 | 
				
			||||||
 | 
					        $this->table->primary = $id;
 | 
				
			||||||
        $this->table->primaryKey = [
 | 
					        return $this;
 | 
				
			||||||
            'name' => $name,
 | 
					 | 
				
			||||||
            'autoInc' => $autoInc
 | 
					 | 
				
			||||||
        ];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return $this->column($name, $type);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function reference(Schema $foreignTable, Propagation $onDelete = Propagation::CASCADE, Propagation $onUpdate = Propagation::CASCADE): Table {
 | 
					    public function reference(Schema $foreignTable, Propagation $onDelete = Propagation::CASCADE, Propagation $onUpdate = Propagation::CASCADE): Table {
 | 
				
			||||||
        $name = $foreignTable->primaryKey();
 | 
					        $name = $foreignTable->primary->name;
 | 
				
			||||||
        if(isset($this->columns[$name]))
 | 
					 | 
				
			||||||
            throw new InvalidArgumentException('Column name already exists.');
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->table->foreignKeys[$name] = [
 | 
					        $ref = new Reference;
 | 
				
			||||||
            'name' => $name,
 | 
					        $ref->name = $name;
 | 
				
			||||||
            'table' => $foreignTable->name,
 | 
					        $ref->otherTable = $foreignTable;
 | 
				
			||||||
            'onDelete' => $onDelete,
 | 
					        $ref->onDelete = $onDelete;
 | 
				
			||||||
            'onUpdate' => $onUpdate
 | 
					        $ref->onUpdate = $onUpdate;
 | 
				
			||||||
        ];
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->table->columns[$name] = $foreignTable->columns[$name];
 | 
					        $this->table->references[$name] = $ref;
 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -72,26 +68,19 @@ class Table
 | 
				
			|||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Generates the SQL statement.
 | 
					 | 
				
			||||||
     * @return string SQL query.
 | 
					 | 
				
			||||||
     * @throws InvalidArgumentException
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public function toSql(): string {
 | 
					    public function toSql(): string {
 | 
				
			||||||
        $primaryKey = $this->table->primaryKey();
 | 
					        $primaryKey = $this->table->primary->name;
 | 
				
			||||||
 | 
					 | 
				
			||||||
        $clause = "CREATE TABLE `{$this->table->name}` (\n";
 | 
					        $clause = "CREATE TABLE `{$this->table->name}` (\n";
 | 
				
			||||||
        $clause .= "  PRIMARY KEY (`$primaryKey`),\n";
 | 
					        $clause .= "  PRIMARY KEY (`$primaryKey`),\n";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        foreach ($this->table->columns as $col) {
 | 
					        foreach ($this->table->columns as $col) {
 | 
				
			||||||
            if ($col['name'] !== $primaryKey) {
 | 
					            if ($col->name !== $primaryKey) {
 | 
				
			||||||
                $clause .= "  `{$col['name']}` {$col['type']->toString()}";
 | 
					                $clause .= "  `$col->name` {$col->type->toString()}";
 | 
				
			||||||
 | 
					                $clause .= !$col->isNullable ? ' NOT NULL' : '';
 | 
				
			||||||
 | 
					                $clause .= $col->isUnique ? ' UNIQUE' : '';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                $clause .= !$col['isNullable'] ? ' NOT NULL' : '';
 | 
					                if (!is_null($col->default)) {
 | 
				
			||||||
                $clause .= $col['isUnique'] ? ' UNIQUE' : '';
 | 
					                    $default = is_string($col->default) ? "'{$col->default}'" : $col->default;
 | 
				
			||||||
 | 
					 | 
				
			||||||
                if (!is_null($col['default'])) {
 | 
					 | 
				
			||||||
                    $default = is_string($col['default']) ? "'{$col['default']}'" : $col['default'];
 | 
					 | 
				
			||||||
                    $clause .= " DEFAULT $default";
 | 
					                    $clause .= " DEFAULT $default";
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -100,29 +89,30 @@ class Table
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Add foreign keys
 | 
					        // Add foreign keys
 | 
				
			||||||
        foreach ($this->table->foreignKeys as $key) {
 | 
					        foreach ($this->table->references as $ref) {
 | 
				
			||||||
            $clause .= "  FOREIGN KEY (`{$key['name']}`)";
 | 
					            $clause .= "  FOREIGN KEY (`{$ref->name}`)";
 | 
				
			||||||
            $clause .= " REFERENCES `{$key['table']}` (`{$key['name']}`)";
 | 
					            $clause .= " REFERENCES `{$ref->otherTable->name}` (`{$ref->name}`)";
 | 
				
			||||||
            $clause .= " ON DELETE {$key['onDelete']->toString()}";
 | 
					            $clause .= " ON DELETE {$ref->onDelete->toString()}";
 | 
				
			||||||
            $clause .= " ON UPDATE {$key['onUpdate']->toString()},\n";
 | 
					            $clause .= " ON UPDATE {$ref->onUpdate->toString()},\n";
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Close the SQL string and add constraints
 | 
					        // Close the SQL string and add constraints
 | 
				
			||||||
        $clause = rtrim($clause, ",\n") . "\n) ";
 | 
					        $clause = rtrim($clause, ",\n") . "\n) ";
 | 
				
			||||||
        $clause .= "ENGINE={$this->table->engine->toString()} ";
 | 
					        $clause .= "ENGINE={$this->table->engine->toString()} ";
 | 
				
			||||||
        $clause .= "CHARSET={$this->table->charset->toString()} ";
 | 
					        $clause .= "CHARSET={$this->table->charset->toString()} ";
 | 
				
			||||||
        $clause -= "COLLATE={$this->table->collation->toString()};";
 | 
					        $clause .= "COLLATE={$this->table->collation->toString()};";
 | 
				
			||||||
        return $clause;
 | 
					        return $clause;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function create(): Schema {
 | 
					    public function create(): Schema {
 | 
				
			||||||
        global $wpdb;
 | 
					        global $wpdb;
 | 
				
			||||||
 | 
					        $this->id();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (empty($this->primaryKey))
 | 
					        Database::standardizeTableNames($this->table->name);
 | 
				
			||||||
            throw new InvalidArgumentException('A primary key must be defined.');
 | 
					        $tableName = $this->table->name;
 | 
				
			||||||
        $table_name = $wpdb->prefix . $this->table->name;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") !== $table_name) {
 | 
					        $foundTables = $wpdb->get_var("SHOW TABLES LIKE '$tableName'");
 | 
				
			||||||
 | 
					        if (is_null($foundTables)) {
 | 
				
			||||||
            $sql = $this->toSql();
 | 
					            $sql = $this->toSql();
 | 
				
			||||||
            require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
 | 
					            require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
 | 
				
			||||||
            dbDelta($sql);
 | 
					            dbDelta($sql);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
<?php
 | 
					<?php
 | 
				
			||||||
namespace DatabaseHelper;
 | 
					namespace DatabaseHelper;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use DatabaseHelper\beans\Schema;
 | 
				
			||||||
use DatabaseHelper\enums\Type;
 | 
					use DatabaseHelper\enums\Type;
 | 
				
			||||||
use DatabaseHelper\enums\Operator;
 | 
					use DatabaseHelper\enums\Operator;
 | 
				
			||||||
use InvalidArgumentException;
 | 
					use InvalidArgumentException;
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										15
									
								
								beans/Column.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								beans/Column.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					namespace DatabaseHelper\beans;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use DatabaseHelper\enums\Type;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Column
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public string $name;
 | 
				
			||||||
 | 
					    public ?string $default = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public Type $type;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public bool $isNullable = false;
 | 
				
			||||||
 | 
					    public bool $isUnique = false;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										10
									
								
								beans/Primary.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								beans/Primary.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					namespace DatabaseHelper\beans;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use DatabaseHelper\enums\Type;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Primary extends Column
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public string $name;
 | 
				
			||||||
 | 
					    public bool $autoInc = true;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										14
									
								
								beans/Reference.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								beans/Reference.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					namespace DatabaseHelper\beans;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use DatabaseHelper\enums\Propagation;
 | 
				
			||||||
 | 
					use DatabaseHelper\enums\Type;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Reference
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public string $name;
 | 
				
			||||||
 | 
					    public ?Schema $otherTable;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public Propagation $onDelete;
 | 
				
			||||||
 | 
					    public Propagation $onUpdate;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										28
									
								
								beans/Schema.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								beans/Schema.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					namespace DatabaseHelper\beans;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use DatabaseHelper\enums\Aggregation;
 | 
				
			||||||
 | 
					use DatabaseHelper\enums\Charset;
 | 
				
			||||||
 | 
					use DatabaseHelper\enums\Collation;
 | 
				
			||||||
 | 
					use DatabaseHelper\enums\Engine;
 | 
				
			||||||
 | 
					use http\Exception\InvalidArgumentException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Schema
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public string $name = '';
 | 
				
			||||||
 | 
					    public Primary $primary;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @var array<string, Column>
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public array $columns = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @var array<string, Reference>
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public array $references = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public Engine $engine = Engine::INNODB;
 | 
				
			||||||
 | 
					    public Charset $charset = Charset::UTF8;
 | 
				
			||||||
 | 
					    public Collation $collation = Collation::UTF8_GENERAL_CI;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user