diff --git a/Database.php b/Database.php index 717f593..6cff276 100644 --- a/Database.php +++ b/Database.php @@ -1,38 +1,40 @@ table = $table; } - public function data(string $col, mixed $val): InsertionBuilder { + public function data(string $col, mixed $val): Insertion { if (!isset($this->table->columns[$col])) throw new InvalidArgumentException("Column '$col' does not exist."); @@ -30,7 +29,7 @@ class Insertion implements InsertionBuilder return $this; } - public function done(): InsertionBuilder { + public function done(): Insertion { if (!empty($this->currentRow)) { $this->batchRows[] = $this->currentRow; $this->currentRow = []; diff --git a/Query.php b/Query.php index ed6883a..0a3cd8a 100644 --- a/Query.php +++ b/Query.php @@ -1,14 +1,12 @@ columnTypes = array_map(fn($col) => $col['colType'], $table->columns); $this->table = $table; } - public function select(string ...$cols): QueryBuilder { + public function select(string ...$cols): Query { if (!empty($cols)) $this->columns = $cols; return $this; } - public function where(string $col, string $operator, mixed $val): QueryBuilder { + public function where(string $col, string $operator, mixed $val): Query { return $this->addCondition($col, $operator, $val, "AND"); } - public function andWhere(string $col, string $operator, mixed $val): QueryBuilder { + public function andWhere(string $col, string $operator, mixed $val): Query { return $this->addCondition($col, $operator, $val, "AND"); } - public function orWhere(string $col, string $operator, mixed $val): QueryBuilder { + public function orWhere(string $col, string $operator, mixed $val): Query { return $this->addCondition($col, $operator, $val, "OR"); } - private function addCondition(string $colName, string $operator, mixed $val, string $prefix): QueryBuilder { + private function addCondition(string $colName, string $operator, mixed $val, string $prefix): Query { if (!isset($this->columnTypes[$colName])) throw new InvalidArgumentException("Unknown column: $colName"); diff --git a/Table.php b/Table.php index f4a8cce..7455760 100644 --- a/Table.php +++ b/Table.php @@ -6,10 +6,9 @@ use DatabaseHelper\enums\CharsetTypes; use DatabaseHelper\enums\CollationTypes; use DatabaseHelper\enums\ColumnTypes; use DatabaseHelper\enums\EngineTypes; -use DatabaseHelper\interfaces\TableBlueprint; use InvalidArgumentException; -class Table implements TableBlueprint +class Table { public string $tableName; @@ -29,7 +28,7 @@ class Table implements TableBlueprint $this->tableName = $tableName; } - public function column(string $colName, ColumnTypes $colType, mixed $default = null, bool $nullable = false, bool $unique = false): TableBlueprint { + public function column(string $colName, ColumnTypes $colType, mixed $default = null, bool $nullable = false, bool $unique = false): Table { if(isset($this->columns[$colName])) throw new InvalidArgumentException('Column name already exists.'); @@ -44,7 +43,7 @@ class Table implements TableBlueprint return $this; } - public function primary(string $colName, ColumnTypes $colType, bool $autoInc = false): TableBlueprint { + public function primary(string $colName, ColumnTypes $colType, bool $autoInc = false): Table { if(isset($this->primaryKey)) throw new InvalidArgumentException('Primary column already exists.'); @@ -57,7 +56,7 @@ class Table implements TableBlueprint return $this->column($colName, $colType); } - public function referenceColumn(TableBlueprint $foreignTable, CascadeTypes $onDelete = CascadeTypes::CASCADE, CascadeTypes $onUpdate = CascadeTypes::CASCADE): TableBlueprint { + public function referenceColumn(Table $foreignTable, CascadeTypes $onDelete = CascadeTypes::CASCADE, CascadeTypes $onUpdate = CascadeTypes::CASCADE): Table { $colName = $foreignTable->primaryKey['colName']; if(isset($this->columns[$colName])) throw new InvalidArgumentException('Column name already exists.'); @@ -73,17 +72,17 @@ class Table implements TableBlueprint return $this; } - public function engine(EngineTypes $engine): TableBlueprint { + public function engine(EngineTypes $engine): Table { $this->engine = $engine; return $this; } - public function charset(CharsetTypes $charset): TableBlueprint { + public function charset(CharsetTypes $charset): Table { $this->charset = $charset; return $this; } - public function collation(CollationTypes $collation): TableBlueprint { + public function collation(CollationTypes $collation): Table { $this->collation = $collation; return $this; } @@ -126,7 +125,7 @@ class Table implements TableBlueprint return esc_sql($sql); } - public function create(): TableBlueprint { + public function create(): Table { global $wpdb; if (empty($this->primaryKey)) diff --git a/index.php b/index.php index 1f16e18..1e31f13 100644 --- a/index.php +++ b/index.php @@ -19,10 +19,24 @@ try { ->column('col-two', ColumnTypes::STRING) ->create(); - Database::makeInsertion($table) - ->data('col-one', true) - ->data('col-two', 'A bomb is exploding here!') - ->insert(); + $batches = [ + [ + ['col-one', true], + ['col-two', 'EXPLODING!!!'] + ], + [ + ['col-one', false], + ['col-two', 'EXPLODING!!!'] + ], + ]; + + $batchInsert = Database::makeInsertion($table); + foreach($batches as $batch) { + foreach($batch as $col => $value) + $batchInsert->data($col, $value); + $batchInsert->done(); + } + $batchInsert->insert(); $results = Database::makeQuery($table) ->select('col-primary', 'col-one') diff --git a/interfaces/DatabaseHelper.php b/interfaces/DatabaseHelper.php deleted file mode 100644 index d8be49e..0000000 --- a/interfaces/DatabaseHelper.php +++ /dev/null @@ -1,17 +0,0 @@ -', '<'). - * @param mixed $val Value to compare against. - * @return QueryBuilder Current instance for method chaining. - */ - public function where(string $col, string $operator, mixed $val): QueryBuilder; - - /** - * Adds an AND WHERE condition to the query. - * @param string $col Column name to apply the condition to. - * @param string $operator Operator to use in the condition (e.g., '=', '>', '<'). - * @param mixed $val Value to compare against. - * @return QueryBuilder Current instance for method chaining. - */ - public function andWhere(string $col, string $operator, mixed $val): QueryBuilder; - - /** - * Adds an OR WHERE condition to the query. - * @param string $col Column name to apply the condition to. - * @param string $operator Operator to use in the condition (e.g., '=', '>', '<'). - * @param mixed $val Value to compare against. - * @return QueryBuilder Current instance for method chaining. - */ - public function orWhere(string $col, string $operator, mixed $val): QueryBuilder; - - /** - * Executes the query and returns the result as an associative array. - * @return array Result of the query execution. - */ - public function query(): array; -} - diff --git a/interfaces/TableBlueprint.php b/interfaces/TableBlueprint.php deleted file mode 100644 index 797cfdd..0000000 --- a/interfaces/TableBlueprint.php +++ /dev/null @@ -1,59 +0,0 @@ -