proceed further with beans
This commit is contained in:
		@@ -22,18 +22,19 @@ trait Conditionable
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected function addCondition(string $col, string|Operator $operator, mixed $val, string $prefix): self {
 | 
					    protected function addCondition(string $col, string|Operator $operator, mixed $val, string $prefix): self {
 | 
				
			||||||
        $this->schema->requireColumn($col);
 | 
					        $this->schema->reqCol($col);
 | 
				
			||||||
 | 
					        $info = $this->schema->getCol($col);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Convert the operator string to a ConditionOperator enum if needed.
 | 
					        // Convert the operator string to a ConditionOperator enum if needed.
 | 
				
			||||||
        if (is_string($operator))
 | 
					        if (is_string($operator))
 | 
				
			||||||
            $operator = Operator::fromString($operator);
 | 
					            $operator = Operator::fromString($operator);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $columnType = $this->schema->columnType($col);
 | 
					        $type = $info->type;
 | 
				
			||||||
        $castedValue = $columnType->dbCast($val);
 | 
					        $castedVal = $type->dbCast($val);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (!empty($this->conditions))
 | 
					        if (!empty($this->conditions))
 | 
				
			||||||
            $this->conditions[] = $prefix;
 | 
					            $this->conditions[] = $prefix;
 | 
				
			||||||
        $this->conditions[] = "$col " . $operator->toString() . " $castedValue";
 | 
					        $this->conditions[] = "$col " . $operator->toString() . " $castedVal";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										21
									
								
								Database.php
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								Database.php
									
									
									
									
									
								
							@@ -5,31 +5,19 @@ use DatabaseHelper\beans\Schema;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class Database
 | 
					class Database
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Creates a new TableBlueprint instance for the specified table name.
 | 
					 | 
				
			||||||
     * @param string $tableName The name of the table to create.
 | 
					 | 
				
			||||||
     * @return Table instance supporting method chaining
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public static function makeTable(string $tableName): Table {
 | 
					    public static function makeTable(string $tableName): Table {
 | 
				
			||||||
        return new Table($tableName);
 | 
					        return new Table($tableName);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static function makeMigration(Schema $table): Migration {
 | 
					    public static function makeMigration(Schema $table): Migration {
 | 
				
			||||||
        // TODO: Implement makeMigration() method.
 | 
					        return new Migration($table);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Creates a new QueryBuilder instance for the specified table.
 | 
					 | 
				
			||||||
     * @param Schema $table The table to query on.
 | 
					 | 
				
			||||||
     * @return Query instance supporting method chaining.
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public static function makeQuery(Schema $table): Query {
 | 
					    public static function makeQuery(Schema $table): Query {
 | 
				
			||||||
        return new Query($table);
 | 
					        return new Query($table);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
    public static function makeDeletion(Schema $table): Deletion {
 | 
					    public static function makeDeletion(Schema $table): Deletion {
 | 
				
			||||||
        // TODO: Implement makeDeletion() method.
 | 
					        return new Deletion($table);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static function makeInsertion(Schema $table): Insertion {
 | 
					    public static function makeInsertion(Schema $table): Insertion {
 | 
				
			||||||
@@ -40,11 +28,6 @@ class Database
 | 
				
			|||||||
        return new Update($table);
 | 
					        return new Update($table);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Adds the WordPress database prefix to table names if not already present.
 | 
					 | 
				
			||||||
     * @param string ...$tableName Array of table names to process
 | 
					 | 
				
			||||||
     * @return void
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public static function standardizeTableNames(string &...$tableName): void {
 | 
					    public static function standardizeTableNames(string &...$tableName): void {
 | 
				
			||||||
        global $wpdb;
 | 
					        global $wpdb;
 | 
				
			||||||
        $dbPrefix = $wpdb->prefix;
 | 
					        $dbPrefix = $wpdb->prefix;
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										23
									
								
								Deletion.php
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								Deletion.php
									
									
									
									
									
								
							@@ -9,19 +9,14 @@ class Deletion
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    use Conditionable;
 | 
					    use Conditionable;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected Schema $table;
 | 
					    protected Schema $schema;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function __construct(Schema $table) {
 | 
					    public function __construct(Schema $table) {
 | 
				
			||||||
        $this->table = $table;
 | 
					        $this->schema = $table;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Generates the SQL statement.
 | 
					 | 
				
			||||||
     * @return string SQL query.
 | 
					 | 
				
			||||||
     * @throws InvalidArgumentException
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public function toSql(): string {
 | 
					    public function toSql(): string {
 | 
				
			||||||
        $table = $this->table->name;
 | 
					        $table = $this->schema->name;
 | 
				
			||||||
        $whereClause = $this->combineConditions();
 | 
					        $whereClause = $this->combineConditions();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (!$this->isConditioned())
 | 
					        if (!$this->isConditioned())
 | 
				
			||||||
@@ -30,11 +25,6 @@ class Deletion
 | 
				
			|||||||
        return "DELETE FROM $table WHERE $whereClause";
 | 
					        return "DELETE FROM $table WHERE $whereClause";
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Executes the DELETE query.
 | 
					 | 
				
			||||||
     * @return int Number of affected rows.
 | 
					 | 
				
			||||||
     * @throws Exception
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public function delete(): int {
 | 
					    public function delete(): int {
 | 
				
			||||||
        global $wpdb;
 | 
					        global $wpdb;
 | 
				
			||||||
        $query = $this->toSql();
 | 
					        $query = $this->toSql();
 | 
				
			||||||
@@ -45,14 +35,9 @@ class Deletion
 | 
				
			|||||||
        return intval($result);
 | 
					        return intval($result);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Clears all entries from the table.
 | 
					 | 
				
			||||||
     * @return int Number of affected rows.
 | 
					 | 
				
			||||||
     * @throws Exception
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public function truncate(): int {
 | 
					    public function truncate(): int {
 | 
				
			||||||
        global $wpdb;
 | 
					        global $wpdb;
 | 
				
			||||||
        $table = $this->table->name;
 | 
					        $table = $this->schema->name;
 | 
				
			||||||
        $query = esc_sql("TRUNCATE TABLE $table");
 | 
					        $query = esc_sql("TRUNCATE TABLE $table");
 | 
				
			||||||
        $result = $wpdb->query($query);
 | 
					        $result = $wpdb->query($query);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,75 +6,52 @@ use InvalidArgumentException;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class Insertion
 | 
					class Insertion
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    private Schema $table;
 | 
					    private Schema $schema;
 | 
				
			||||||
    private array $currentRow = [];
 | 
					    private array $currInsert = [];
 | 
				
			||||||
    private array $batchRows = [];
 | 
					    private array $batchInserts = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function __construct(Schema $table) {
 | 
					    public function __construct(Schema $table) {
 | 
				
			||||||
        $this->table = $table;
 | 
					        $this->schema = $table;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Adds a single column-value pair to the row.
 | 
					 | 
				
			||||||
     * @param string $col Column name.
 | 
					 | 
				
			||||||
     * @param mixed $val Value to insert.
 | 
					 | 
				
			||||||
     * @return Insertion Current instance for method chaining.
 | 
					 | 
				
			||||||
     * @throws InvalidArgumentException
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public function data(string $col, mixed $val): Insertion {
 | 
					    public function data(string $col, mixed $val): Insertion {
 | 
				
			||||||
        if (!isset($this->table->columns[$col]))
 | 
					        $this->schema->reqCol($col);
 | 
				
			||||||
            throw new InvalidArgumentException("Column '$col' does not exist.");
 | 
					        $info = $this->schema->getCol($col);
 | 
				
			||||||
 | 
					        $type = $info->type;
 | 
				
			||||||
        $columnType = $this->table->columns[$col]['colType'];
 | 
					        $this->currInsert[$col] = $type->dbCast($val);
 | 
				
			||||||
        $this->currentRow[$col] = $columnType->dbCast($val);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Adds multiple column-value pairs to the row.
 | 
					 | 
				
			||||||
     * @param array $data Associative array of column-value pairs.
 | 
					 | 
				
			||||||
     * @return Insertion Current instance for method chaining.
 | 
					 | 
				
			||||||
     * @throws InvalidArgumentException
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public function batchData(array $data): Insertion {
 | 
					    public function batchData(array $data): Insertion {
 | 
				
			||||||
        foreach ($data as $key => $value)
 | 
					        foreach ($data as $key => $value)
 | 
				
			||||||
            $this->data($key, $value);
 | 
					            $this->data($key, $value);
 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Finalizes the current row and stacks it for insertion.
 | 
					 | 
				
			||||||
     * @return Insertion Current instance for method chaining.
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public function stack(): Insertion {
 | 
					    public function stack(): Insertion {
 | 
				
			||||||
        if (!empty($this->currentRow))
 | 
					        if (!empty($this->currInsert))
 | 
				
			||||||
            $this->stackForInsertion();
 | 
					            $this->stackForInsertion();
 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private function stackForInsertion(): void {
 | 
					    private function stackForInsertion(): void {
 | 
				
			||||||
        $this->batchRows[] = $this->currentRow;
 | 
					        $this->batchInserts[] = $this->currInsert;
 | 
				
			||||||
        $this->currentRow = [];
 | 
					        $this->currInsert = [];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Executes the insertion of all batched rows into the database.
 | 
					 | 
				
			||||||
     * @throws InvalidArgumentException
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public function insert(): bool {
 | 
					    public function insert(): bool {
 | 
				
			||||||
        global $wpdb;
 | 
					        global $wpdb;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Convert single to batch queries.
 | 
					        // Convert single to batch queries.
 | 
				
			||||||
        if (!empty($this->currentRow))
 | 
					        if (!empty($this->currInsert))
 | 
				
			||||||
            $this->stackForInsertion();
 | 
					            $this->stackForInsertion();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (empty($this->batchRows))
 | 
					        if (empty($this->batchInserts))
 | 
				
			||||||
            throw new InvalidArgumentException("No data set for insertion.");
 | 
					            throw new InvalidArgumentException("No data set for insertion.");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        foreach ($this->batchRows as $row) {
 | 
					        foreach ($this->batchInserts as $row) {
 | 
				
			||||||
            if (!empty($row)) {
 | 
					            if (!empty($row)) {
 | 
				
			||||||
                $result = $wpdb->insert($this->table->name, $row);
 | 
					                $result = $wpdb->insert($this->schema->name, $row);
 | 
				
			||||||
                if ($result === false)
 | 
					                if ($result === false)
 | 
				
			||||||
                    return false;
 | 
					                    return false;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										105
									
								
								Migration.php
									
									
									
									
									
								
							
							
						
						
									
										105
									
								
								Migration.php
									
									
									
									
									
								
							@@ -1,6 +1,8 @@
 | 
				
			|||||||
<?php
 | 
					<?php
 | 
				
			||||||
namespace DatabaseHelper;
 | 
					namespace DatabaseHelper;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use DatabaseHelper\beans\Column;
 | 
				
			||||||
 | 
					use DatabaseHelper\beans\Reference;
 | 
				
			||||||
use DatabaseHelper\beans\Schema;
 | 
					use DatabaseHelper\beans\Schema;
 | 
				
			||||||
use DatabaseHelper\enums\Propagation;
 | 
					use DatabaseHelper\enums\Propagation;
 | 
				
			||||||
use DatabaseHelper\enums\Type;
 | 
					use DatabaseHelper\enums\Type;
 | 
				
			||||||
@@ -9,81 +11,76 @@ use InvalidArgumentException;
 | 
				
			|||||||
class Migration
 | 
					class Migration
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    protected Schema $schema;
 | 
					    protected Schema $schema;
 | 
				
			||||||
    protected array $columnsToAdd = [];
 | 
					    protected array $newCols = [];
 | 
				
			||||||
    protected array $columnsToModify = [];
 | 
					    protected array $editCols = [];
 | 
				
			||||||
    protected array $columnsToDrop = [];
 | 
					    protected array $dropCols = [];
 | 
				
			||||||
    protected array $foreignKeysToAdd = [];
 | 
					    protected array $addRefs = [];
 | 
				
			||||||
    protected array $foreignKeysToDrop = [];
 | 
					    protected array $dropRefs = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function __construct(Schema $table) {
 | 
					    public function __construct(Schema $table) {
 | 
				
			||||||
        $this->schema = $table->copy();
 | 
					        $this->schema = $table;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function column(string $name, Type $type, mixed $default = null, bool $isNullable = false, bool $isUnique = false): Migration {
 | 
					    public function column(string $name, Type $type, mixed $default = null, bool $isNullable = false, bool $isUnique = false): Migration {
 | 
				
			||||||
        if ($this->schema->existsColumn($name))
 | 
					        $table = $this->schema->name;
 | 
				
			||||||
            throw new InvalidArgumentException("Column '$name' already exists.");
 | 
					        if ($this->schema->existsCol($name))
 | 
				
			||||||
 | 
					            throw new InvalidArgumentException("Column '$name' already exists in $table.");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->schema->columns[$name] = [
 | 
					        $col = new Column;
 | 
				
			||||||
            'name'       => $name,
 | 
					        $col->name = $name;
 | 
				
			||||||
            'type'       => $type,
 | 
					        $col->type = $type;
 | 
				
			||||||
            'default'    => $default,
 | 
					        $col->default = $default;
 | 
				
			||||||
            'isNullable' => $isNullable,
 | 
					        $col->isUnique = $isUnique;
 | 
				
			||||||
            'isUnique'   => $isUnique
 | 
					        $col->isNullable = $isNullable;
 | 
				
			||||||
        ];
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->columnsToAdd[] = $name;
 | 
					        $this->schema->cols[$name] = $col;
 | 
				
			||||||
 | 
					        $this->newCols[] = $name;
 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    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->reqCol($name);
 | 
				
			||||||
        if (isset($this->schema->references[$name]))
 | 
					        $col = $this->schema->getCol($name);
 | 
				
			||||||
            throw new InvalidArgumentException('Referencing columns cannot be modified.');
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->schema->columns[$name] = [
 | 
					        $col->name = $name;
 | 
				
			||||||
            'name'       => $name,
 | 
					        $col->type = $type;
 | 
				
			||||||
            'type'       => $type,
 | 
					        $col->default = $default;
 | 
				
			||||||
            'default'    => $default,
 | 
					        $col->isNullable = $isNullable;
 | 
				
			||||||
            'isNullable' => $isNullable,
 | 
					        $col->isUnique = $isUnique;#
 | 
				
			||||||
            'isUnique'   => $isUnique
 | 
					 | 
				
			||||||
        ];
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->columnsToModify[] = $name;
 | 
					        $this->editCols[$name] = $col;
 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function drop(string $name): Migration {
 | 
					    public function drop(string $name): Migration {
 | 
				
			||||||
        $this->schema->requireColumn($name);
 | 
					        unset($this->schema->cols[$name]);
 | 
				
			||||||
        unset($this->schema->columns[$name]);
 | 
					        $this->dropCols[] = $name;
 | 
				
			||||||
        $this->columnsToDrop[] = $name;
 | 
					 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function reference(Schema $foreignTable, Propagation $onDelete = Propagation::CASCADE, Propagation $onUpdate = Propagation::CASCADE): Migration {
 | 
					    public function reference(Schema $foreignTable, Propagation $onDelete = Propagation::CASCADE, Propagation $onUpdate = Propagation::CASCADE): Migration {
 | 
				
			||||||
        $name = $foreignTable->primaryKey();
 | 
					        if($this->schema->existsRef($foreignTable))
 | 
				
			||||||
        $this->schema->requireColumn($name);
 | 
					            throw new InvalidArgumentException("Foreign table '$foreignTable->name' is already referenced in '{$this->schema->name}'.");
 | 
				
			||||||
 | 
					        $name = $foreignTable->id->name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->schema->references[$name] = [
 | 
					        $ref = new Reference;
 | 
				
			||||||
            'name'     => $name,
 | 
					        $ref->name = $name;
 | 
				
			||||||
            'table'    => $foreignTable->name,
 | 
					        $ref->onUpdate = $onUpdate;
 | 
				
			||||||
            'onDelete' => $onDelete,
 | 
					        $ref->onDelete = $onDelete;
 | 
				
			||||||
            'onUpdate' => $onUpdate
 | 
					
 | 
				
			||||||
        ];
 | 
					        $this->schema->refs[$name] = $ref;
 | 
				
			||||||
        $this->schema->columns[$name] = $foreignTable->columns[$name];
 | 
					        $this->addRefs[] = $name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->foreignKeysToAdd[] = $name;
 | 
					 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function dereference(Schema $foreignTable): Migration {
 | 
					    public function dereference(Schema $foreignTable): Migration {
 | 
				
			||||||
        $name = $foreignTable->primaryKey();
 | 
					        $this->schema->reqRef($foreignTable);
 | 
				
			||||||
        if ($this->schema->existsReference($foreignTable))
 | 
					        $name = $foreignTable->id->name;
 | 
				
			||||||
            throw new InvalidArgumentException('Foreign table is not referenced.');
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        unset($this->schema->references[$name]);
 | 
					        unset($this->schema->refs[$name]);
 | 
				
			||||||
        $this->drop($name);
 | 
					        $this->dropRefs[] = $name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->foreignKeysToDrop[] = $name;
 | 
					 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -94,8 +91,8 @@ class Migration
 | 
				
			|||||||
        $clauses = [];
 | 
					        $clauses = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Process new columns.
 | 
					        // Process new columns.
 | 
				
			||||||
        foreach ($this->columnsToAdd as $columnName) {
 | 
					        foreach ($this->newCols as $columnName) {
 | 
				
			||||||
            $col = $this->schema->columns[$columnName];
 | 
					            $col = $this->schema->cols[$columnName];
 | 
				
			||||||
            $clause = "ADD COLUMN `{$col['name']}` {$col['type']->toString()}";
 | 
					            $clause = "ADD COLUMN `{$col['name']}` {$col['type']->toString()}";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $clause .= !$col['isNullable'] ? " NOT NULL" : "";
 | 
					            $clause .= !$col['isNullable'] ? " NOT NULL" : "";
 | 
				
			||||||
@@ -110,8 +107,8 @@ class Migration
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Process modified columns.
 | 
					        // Process modified columns.
 | 
				
			||||||
        foreach ($this->columnsToModify as $columnName) {
 | 
					        foreach ($this->editCols as $columnName) {
 | 
				
			||||||
            $col = $this->schema->columns[$columnName];
 | 
					            $col = $this->schema->cols[$columnName];
 | 
				
			||||||
            $clause = "MODIFY COLUMN `{$col['name']}` {$col['type']->toString()}";
 | 
					            $clause = "MODIFY COLUMN `{$col['name']}` {$col['type']->toString()}";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $clause .= !$col['isNullable'] ? " NOT NULL" : "";
 | 
					            $clause .= !$col['isNullable'] ? " NOT NULL" : "";
 | 
				
			||||||
@@ -127,12 +124,12 @@ class Migration
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Process dropped columns.
 | 
					        // Process dropped columns.
 | 
				
			||||||
        foreach ($this->columnsToDrop as $columnName)
 | 
					        foreach ($this->dropCols as $columnName)
 | 
				
			||||||
            $clauses[] = "DROP COLUMN `$columnName`";
 | 
					            $clauses[] = "DROP COLUMN `$columnName`";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Process foreign keys to add.
 | 
					        // Process foreign keys to add.
 | 
				
			||||||
        foreach ($this->foreignKeysToAdd as $key) {
 | 
					        foreach ($this->addRefs as $key) {
 | 
				
			||||||
            $foreignKey = $this->schema->references[$key];
 | 
					            $foreignKey = $this->schema->refs[$key];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $clause = "  ADD CONSTRAINT `fk_{$foreignKey['name']}`";
 | 
					            $clause = "  ADD CONSTRAINT `fk_{$foreignKey['name']}`";
 | 
				
			||||||
            $clause .= " FOREIGN KEY (`{$foreignKey['name']}`)";
 | 
					            $clause .= " FOREIGN KEY (`{$foreignKey['name']}`)";
 | 
				
			||||||
@@ -144,7 +141,7 @@ class Migration
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Process foreign keys to drop.
 | 
					        // Process foreign keys to drop.
 | 
				
			||||||
        foreach ($this->foreignKeysToDrop as $fkName)
 | 
					        foreach ($this->dropRefs as $fkName)
 | 
				
			||||||
            $clauses[] = "DROP FOREIGN KEY `fk_{$fkName}`";
 | 
					            $clauses[] = "DROP FOREIGN KEY `fk_{$fkName}`";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (empty($clauses))
 | 
					        if (empty($clauses))
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										33
									
								
								Query.php
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								Query.php
									
									
									
									
									
								
							@@ -1,6 +1,7 @@
 | 
				
			|||||||
<?php
 | 
					<?php
 | 
				
			||||||
namespace DatabaseHelper;
 | 
					namespace DatabaseHelper;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use DatabaseHelper\beans\OrderBy;
 | 
				
			||||||
use DatabaseHelper\beans\Schema;
 | 
					use DatabaseHelper\beans\Schema;
 | 
				
			||||||
use DatabaseHelper\enums\Aggregation;
 | 
					use DatabaseHelper\enums\Aggregation;
 | 
				
			||||||
use DatabaseHelper\enums\Join;
 | 
					use DatabaseHelper\enums\Join;
 | 
				
			||||||
@@ -12,10 +13,10 @@ class Query
 | 
				
			|||||||
    use Conditionable;
 | 
					    use Conditionable;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected Schema $schema;
 | 
					    protected Schema $schema;
 | 
				
			||||||
    protected array $columns = ['*'];
 | 
					    protected array $cols = ['*'];
 | 
				
			||||||
    protected array $aggregations = [];
 | 
					    protected array $aggregates = [];
 | 
				
			||||||
    protected array $joins = [];
 | 
					    protected array $joins = [];
 | 
				
			||||||
    public array $orderBy;
 | 
					    public OrderBy $orderBy;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function __construct(Schema $table) {
 | 
					    public function __construct(Schema $table) {
 | 
				
			||||||
        $this->schema = $table;
 | 
					        $this->schema = $table;
 | 
				
			||||||
@@ -23,27 +24,27 @@ class Query
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    public function select(string ...$cols): Query {
 | 
					    public function select(string ...$cols): Query {
 | 
				
			||||||
        if (!empty($cols))
 | 
					        if (!empty($cols))
 | 
				
			||||||
            $this->columns = $cols;
 | 
					            $this->cols = $cols;
 | 
				
			||||||
        foreach ($cols as $col)
 | 
					        foreach ($cols as $col)
 | 
				
			||||||
            $this->schema->requireColumn($col);
 | 
					            if (!isset($this->schema->cols[$col]))
 | 
				
			||||||
 | 
					                throw new InvalidArgumentException("Column '$col' does not exist");
 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function orderBy(string $col, Order $order): Query {
 | 
					    public function orderBy(string $col, Order $order): Query {
 | 
				
			||||||
        $this->schema->requireColumn($col);
 | 
					        $this->schema->reqCol($col);
 | 
				
			||||||
        $this->orderBy = [
 | 
					        $this->orderBy = new OrderBy;
 | 
				
			||||||
            'name'  => $col,
 | 
					        $this->orderBy->col = $col;
 | 
				
			||||||
            'order' => $order
 | 
					        $this->orderBy->order = $order;
 | 
				
			||||||
        ];
 | 
					 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    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->references[$other->name];
 | 
					            $foreignKey = $this->schema->refs[$other->name];
 | 
				
			||||||
        if ($other->existsReference($this->schema))
 | 
					        if ($other->existsReference($this->schema))
 | 
				
			||||||
            $foreignKey = $other->references[$this->schema->name];
 | 
					            $foreignKey = $other->refs[$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.');
 | 
				
			||||||
@@ -68,9 +69,9 @@ class Query
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    public function toSql(): string {
 | 
					    public function toSql(): string {
 | 
				
			||||||
        // Merge any aggregations with the standard columns.
 | 
					        // Merge any aggregations with the standard columns.
 | 
				
			||||||
        $selectColumns = $this->columns;
 | 
					        $selectColumns = $this->cols;
 | 
				
			||||||
        if ($this->hasAggregations())
 | 
					        if ($this->hasAggregations())
 | 
				
			||||||
            $selectColumns = array_merge($selectColumns, $this->aggregations);
 | 
					            $selectColumns = array_merge($selectColumns, $this->aggregates);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Build the SELECT clause.
 | 
					        // Build the SELECT clause.
 | 
				
			||||||
        $columns = implode(", ", $selectColumns);
 | 
					        $columns = implode(", ", $selectColumns);
 | 
				
			||||||
@@ -100,12 +101,12 @@ class Query
 | 
				
			|||||||
    public function aggregate(string $col, string $alias, Aggregation $func): Query {
 | 
					    public function aggregate(string $col, string $alias, Aggregation $func): Query {
 | 
				
			||||||
        if ($col != '*')
 | 
					        if ($col != '*')
 | 
				
			||||||
            $this->schema->requireColumn($col);
 | 
					            $this->schema->requireColumn($col);
 | 
				
			||||||
        $this->aggregations[] = strtoupper($func->toString()) . "($col) AS $alias";
 | 
					        $this->aggregates[] = strtoupper($func->toString()) . "($col) AS $alias";
 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function hasAggregations(): bool {
 | 
					    public function hasAggregations(): bool {
 | 
				
			||||||
        return !empty($this->aggregations);
 | 
					        return !empty($this->aggregates);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function query(): mixed {
 | 
					    public function query(): mixed {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										41
									
								
								Table.php
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								Table.php
									
									
									
									
									
								
							@@ -10,15 +10,14 @@ use DatabaseHelper\enums\Charset;
 | 
				
			|||||||
use DatabaseHelper\enums\Collation;
 | 
					use DatabaseHelper\enums\Collation;
 | 
				
			||||||
use DatabaseHelper\enums\Type;
 | 
					use DatabaseHelper\enums\Type;
 | 
				
			||||||
use DatabaseHelper\enums\Engine;
 | 
					use DatabaseHelper\enums\Engine;
 | 
				
			||||||
use InvalidArgumentException;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Table
 | 
					class Table
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    protected Schema $table;
 | 
					    protected Schema $schema;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function __construct(string $tableName) {
 | 
					    public function __construct(string $tableName) {
 | 
				
			||||||
        Database::standardizeTableNames($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 {
 | 
					    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->isUnique = $isUnique;
 | 
				
			||||||
        $col->isNullable = $isNullable;
 | 
					        $col->isNullable = $isNullable;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->table->columns[$name] = $col;
 | 
					        $this->schema->cols[$name] = $col;
 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected function id(): Table {
 | 
					    protected function id(): Table {
 | 
				
			||||||
        $id = new Primary;
 | 
					        $id = new Primary;
 | 
				
			||||||
        $id->name = $this->table->name . '_id';
 | 
					        $id->name = $this->schema->name . '_id';
 | 
				
			||||||
        $this->table->primary = $id;
 | 
					        $this->schema->id = $id;
 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    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->primary->name;
 | 
					        $name = $foreignTable->id->name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $ref = new Reference;
 | 
					        $ref = new Reference;
 | 
				
			||||||
        $ref->name = $name;
 | 
					        $ref->name = $name;
 | 
				
			||||||
@@ -49,31 +48,31 @@ class Table
 | 
				
			|||||||
        $ref->onDelete = $onDelete;
 | 
					        $ref->onDelete = $onDelete;
 | 
				
			||||||
        $ref->onUpdate = $onUpdate;
 | 
					        $ref->onUpdate = $onUpdate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->table->references[$name] = $ref;
 | 
					        $this->schema->refs[$name] = $ref;
 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function engine(Engine $engine): Table {
 | 
					    public function engine(Engine $engine): Table {
 | 
				
			||||||
        $this->table->engine = $engine;
 | 
					        $this->schema->engine = $engine;
 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function charset(Charset $charset): Table {
 | 
					    public function charset(Charset $charset): Table {
 | 
				
			||||||
        $this->table->charset = $charset;
 | 
					        $this->schema->charset = $charset;
 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function collation(Collation $collation): Table {
 | 
					    public function collation(Collation $collation): Table {
 | 
				
			||||||
        $this->table->collation = $collation;
 | 
					        $this->schema->collation = $collation;
 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function toSql(): string {
 | 
					    public function toSql(): string {
 | 
				
			||||||
        $primaryKey = $this->table->primary->name;
 | 
					        $primaryKey = $this->schema->id->name;
 | 
				
			||||||
        $clause = "CREATE TABLE `{$this->table->name}` (\n";
 | 
					        $clause = "CREATE TABLE `{$this->schema->name}` (\n";
 | 
				
			||||||
        $clause .= "  PRIMARY KEY (`$primaryKey`),\n";
 | 
					        $clause .= "  PRIMARY KEY (`$primaryKey`),\n";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        foreach ($this->table->columns as $col) {
 | 
					        foreach ($this->schema->cols 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->isNullable ? ' NOT NULL' : '';
 | 
				
			||||||
@@ -89,7 +88,7 @@ class Table
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Add foreign keys
 | 
					        // Add foreign keys
 | 
				
			||||||
        foreach ($this->table->references as $ref) {
 | 
					        foreach ($this->schema->refs as $ref) {
 | 
				
			||||||
            $clause .= "  FOREIGN KEY (`{$ref->name}`)";
 | 
					            $clause .= "  FOREIGN KEY (`{$ref->name}`)";
 | 
				
			||||||
            $clause .= " REFERENCES `{$ref->otherTable->name}` (`{$ref->name}`)";
 | 
					            $clause .= " REFERENCES `{$ref->otherTable->name}` (`{$ref->name}`)";
 | 
				
			||||||
            $clause .= " ON DELETE {$ref->onDelete->toString()}";
 | 
					            $clause .= " ON DELETE {$ref->onDelete->toString()}";
 | 
				
			||||||
@@ -98,9 +97,9 @@ class Table
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        // 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->schema->engine->toString()} ";
 | 
				
			||||||
        $clause .= "CHARSET={$this->table->charset->toString()} ";
 | 
					        $clause .= "CHARSET={$this->schema->charset->toString()} ";
 | 
				
			||||||
        $clause .= "COLLATE={$this->table->collation->toString()};";
 | 
					        $clause .= "COLLATE={$this->schema->collation->toString()};";
 | 
				
			||||||
        return $clause;
 | 
					        return $clause;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -108,8 +107,8 @@ class Table
 | 
				
			|||||||
        global $wpdb;
 | 
					        global $wpdb;
 | 
				
			||||||
        $this->id();
 | 
					        $this->id();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Database::standardizeTableNames($this->table->name);
 | 
					        Database::standardizeTableNames($this->schema->name);
 | 
				
			||||||
        $tableName = $this->table->name;
 | 
					        $tableName = $this->schema->name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $foundTables = $wpdb->get_var("SHOW TABLES LIKE '$tableName'");
 | 
					        $foundTables = $wpdb->get_var("SHOW TABLES LIKE '$tableName'");
 | 
				
			||||||
        if (is_null($foundTables)) {
 | 
					        if (is_null($foundTables)) {
 | 
				
			||||||
@@ -117,6 +116,6 @@ class Table
 | 
				
			|||||||
            require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
 | 
					            require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
 | 
				
			||||||
            dbDelta($sql);
 | 
					            dbDelta($sql);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return $this->table;
 | 
					        return $this->schema;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										29
									
								
								Update.php
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								Update.php
									
									
									
									
									
								
							@@ -10,39 +10,26 @@ class Update
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    use Conditionable;
 | 
					    use Conditionable;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected Schema $table;
 | 
					    protected Schema $schema;
 | 
				
			||||||
    protected array $values = [];
 | 
					    protected array $values = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function __construct(Schema $table) {
 | 
					    public function __construct(Schema $table) {
 | 
				
			||||||
        $this->table = $table;
 | 
					        $this->schema = $table;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Sets a new value for a column.
 | 
					 | 
				
			||||||
     * @param string $col Column name.
 | 
					 | 
				
			||||||
     * @param mixed $val Value to set.
 | 
					 | 
				
			||||||
     * @return self Current instance for method chaining.
 | 
					 | 
				
			||||||
     * @throws InvalidArgumentException
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public function set(string $col, mixed $val): self {
 | 
					    public function set(string $col, mixed $val): self {
 | 
				
			||||||
        $this->table->requireColumn($col);
 | 
					        $info = $this->schema->cols[$col];
 | 
				
			||||||
 | 
					        $type = $info->type;
 | 
				
			||||||
        $columnType = $this->table->columnType($col);
 | 
					        $castedValue = $type->dbCast($val);
 | 
				
			||||||
        $castedValue = $columnType->dbCast($val);
 | 
					 | 
				
			||||||
        $this->values[$col] = $castedValue;
 | 
					        $this->values[$col] = $castedValue;
 | 
				
			||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Generates the SQL statement.
 | 
					 | 
				
			||||||
     * @return string SQL query.
 | 
					 | 
				
			||||||
     * @throws InvalidArgumentException
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public function toSql(): string {
 | 
					    public function toSql(): string {
 | 
				
			||||||
        if (empty($this->values))
 | 
					        if (empty($this->values))
 | 
				
			||||||
            throw new InvalidArgumentException("No values have been set for the Update.");
 | 
					            throw new InvalidArgumentException("No values have been set for the Update.");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $table = $this->table->name;
 | 
					        $table = $this->schema->name;
 | 
				
			||||||
        $setParts = [];
 | 
					        $setParts = [];
 | 
				
			||||||
        foreach ($this->values as $col => $value)
 | 
					        foreach ($this->values as $col => $value)
 | 
				
			||||||
            $setParts[] = "$col = $value";
 | 
					            $setParts[] = "$col = $value";
 | 
				
			||||||
@@ -57,10 +44,6 @@ class Update
 | 
				
			|||||||
        return $update;
 | 
					        return $update;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Executes the SQL UPDATE query.
 | 
					 | 
				
			||||||
     * @return bool Returns true on success.
 | 
					 | 
				
			||||||
    */
 | 
					 | 
				
			||||||
    public function update(): bool {
 | 
					    public function update(): bool {
 | 
				
			||||||
        global $wpdb;
 | 
					        global $wpdb;
 | 
				
			||||||
        $query = $this->toSql();
 | 
					        $query = $this->toSql();
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										9
									
								
								beans/OrderBy.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								beans/OrderBy.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					namespace DatabaseHelper\beans;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use DatabaseHelper\enums\Order;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class OrderBy {
 | 
				
			||||||
 | 
					    public string $col;
 | 
				
			||||||
 | 
					    public Order $order;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -10,19 +10,43 @@ use http\Exception\InvalidArgumentException;
 | 
				
			|||||||
class Schema
 | 
					class Schema
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    public string $name = '';
 | 
					    public string $name = '';
 | 
				
			||||||
    public Primary $primary;
 | 
					    public Primary $id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * @var array<string, Column>
 | 
					     * @var array<string, Column>
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public array $columns = [];
 | 
					    public array $cols = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * @var array<string, Reference>
 | 
					     * @var array<string, Reference>
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public array $references = [];
 | 
					    public array $refs = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public Engine $engine = Engine::INNODB;
 | 
					    public Engine $engine = Engine::INNODB;
 | 
				
			||||||
    public Charset $charset = Charset::UTF8;
 | 
					    public Charset $charset = Charset::UTF8;
 | 
				
			||||||
    public Collation $collation = Collation::UTF8_GENERAL_CI;
 | 
					    public Collation $collation = Collation::UTF8_GENERAL_CI;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function existsCol(string $name): bool {
 | 
				
			||||||
 | 
					        return isset($this->cols[$name]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function reqCol(string $name): void {
 | 
				
			||||||
 | 
					        if (!$this->existsCol($name))
 | 
				
			||||||
 | 
					            throw new InvalidArgumentException("Column $name does not exist in schema $this->name.");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function getCol(string $name): Column {
 | 
				
			||||||
 | 
					        return $this->cols[$name];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function existsRef(Schema $other): bool {
 | 
				
			||||||
 | 
					        $otherId = $other->id;
 | 
				
			||||||
 | 
					        // TODO
 | 
				
			||||||
 | 
					        return true;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function reqRef(Schema $other): void {
 | 
				
			||||||
 | 
					        if (!$this->existsRef($other))
 | 
				
			||||||
 | 
					            throw new InvalidArgumentException("A reference to $other->name is not defined in  $this->name.");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user