delete interfaces
This commit is contained in:
parent
e6d0374797
commit
ed099a8bcb
32
Database.php
32
Database.php
@ -1,38 +1,40 @@
|
||||
<?php
|
||||
namespace DatabaseHelper;
|
||||
|
||||
use DatabaseHelper\classes\Query;
|
||||
use DatabaseHelper\interfaces\MigrationBlueprint;
|
||||
use DatabaseHelper\interfaces\TableBlueprint;
|
||||
use DatabaseHelper\interfaces\DeletionBuilder;
|
||||
use DatabaseHelper\interfaces\UpdateBuilder;
|
||||
use DatabaseHelper\interfaces\InsertionBuilder;
|
||||
use DatabaseHelper\interfaces\QueryBuilder;
|
||||
|
||||
class Database implements interfaces\DatabaseHelper
|
||||
class Database
|
||||
{
|
||||
public static function makeTable(string $tableName): TableBlueprint {
|
||||
/**
|
||||
* Creates a new TableBlueprint instance for the specified table name.
|
||||
* @param string $tableName The name of the table to create.
|
||||
* @return Table A blueprint instance representing the table.
|
||||
*/
|
||||
public static function makeTable(string $tableName): Table {
|
||||
return new Table($tableName);
|
||||
}
|
||||
|
||||
public static function makeMigration(TableBlueprint $table): MigrationBlueprint {
|
||||
public static function makeMigration(Table $table): Migration {
|
||||
// TODO: Implement makeMigration() method.
|
||||
}
|
||||
|
||||
public static function makeQuery(TableBlueprint $table): QueryBuilder {
|
||||
/**
|
||||
* Creates a new QueryBuilder instance for the specified table.
|
||||
* @param Table $table The table to query on.
|
||||
* @return Query instance supporting method chaining.
|
||||
*/
|
||||
public static function makeQuery(Table $table): Query {
|
||||
return new Query($table);
|
||||
}
|
||||
|
||||
|
||||
public static function makeDeletion(TableBlueprint $table): DeletionBuilder {
|
||||
public static function makeDeletion(Table $table): Deletion {
|
||||
// TODO: Implement makeDeletion() method.
|
||||
}
|
||||
|
||||
public static function makeInsertion(TableBlueprint $table): InsertionBuilder {
|
||||
public static function makeInsertion(Table $table): Insertion {
|
||||
// TODO: Implement makeInsertion() method.
|
||||
}
|
||||
|
||||
public static function makeUpdate(TableBlueprint $table): UpdateBuilder {
|
||||
public static function makeUpdate(Table $table): Update {
|
||||
// TODO: Implement makeUpdate() method.
|
||||
}
|
||||
|
||||
|
@ -1,26 +1,25 @@
|
||||
<?php
|
||||
namespace DatabaseHelper;
|
||||
|
||||
use DatabaseHelper\interfaces\InsertionBuilder;
|
||||
use DatabaseHelper\interfaces\TableBlueprint;
|
||||
namespace DatabaseHelper;
|
||||
use DatabaseHelper\enums\ColumnTypes;
|
||||
use InvalidArgumentException;
|
||||
use wpdb;
|
||||
|
||||
global $wpdb;
|
||||
|
||||
class Insertion implements InsertionBuilder
|
||||
class Insertion
|
||||
{
|
||||
private TableBlueprint $table;
|
||||
private Table $table;
|
||||
private array $currentRow = [];
|
||||
private array $batchRows = [];
|
||||
private bool $isReady = false;
|
||||
|
||||
public function __construct(TableBlueprint $table) {
|
||||
public function __construct(Table $table) {
|
||||
$this->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 = [];
|
||||
|
20
Query.php
20
Query.php
@ -1,14 +1,12 @@
|
||||
<?php
|
||||
namespace DatabaseHelper\classes;
|
||||
namespace DatabaseHelper;
|
||||
|
||||
use DatabaseHelper\enums\ColumnTypes;
|
||||
use DatabaseHelper\interfaces\QueryBuilder;
|
||||
use DatabaseHelper\interfaces\TableBlueprint;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class Query implements QueryBuilder
|
||||
class Query
|
||||
{
|
||||
private TableBlueprint $table;
|
||||
private Table $table;
|
||||
private array $columns = ['*'];
|
||||
private array $conditions = [];
|
||||
|
||||
@ -17,30 +15,30 @@ class Query implements QueryBuilder
|
||||
*/
|
||||
private array $columnTypes = [];
|
||||
|
||||
public function __construct(TableBlueprint $table) {
|
||||
public function __construct(Table $table) {
|
||||
$this->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");
|
||||
|
||||
|
17
Table.php
17
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))
|
||||
|
22
index.php
22
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')
|
||||
|
@ -1,17 +0,0 @@
|
||||
<?php
|
||||
namespace DatabaseHelper\interfaces;
|
||||
|
||||
interface DatabaseHelper
|
||||
{
|
||||
public static function makeTable(string $tableName): TableBlueprint;
|
||||
|
||||
public static function makeMigration(TableBlueprint $table): MigrationBlueprint;
|
||||
|
||||
public static function makeQuery(TableBlueprint $table): QueryBuilder;
|
||||
|
||||
public static function makeInsertion(TableBlueprint $table): InsertionBuilder;
|
||||
|
||||
public static function makeDeletion(TableBlueprint $table): DeletionBuilder;
|
||||
|
||||
public static function makeUpdate(TableBlueprint $table): UpdateBuilder;
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DatabaseHelper\interfaces;
|
||||
|
||||
interface DeletionBuilder
|
||||
{
|
||||
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
<?php
|
||||
namespace DatabaseHelper\interfaces;
|
||||
|
||||
interface InsertionBuilder
|
||||
{
|
||||
/**
|
||||
* Initializes an InsertionBuilder with a table blueprint.
|
||||
* @param TableBlueprint $table Table to insert data into.
|
||||
*/
|
||||
public function __construct(TableBlueprint $table);
|
||||
|
||||
/**
|
||||
* Specifies a single column-value pair for insertion.
|
||||
* @param string $col Column name to insert into.
|
||||
* @param mixed $val Value to insert.
|
||||
* @return InsertionBuilder Current instance for method chaining.
|
||||
*/
|
||||
public function data(string $col, mixed $val): InsertionBuilder;
|
||||
|
||||
/**
|
||||
* Marks the current row as complete and finalizes it for insertion.
|
||||
* After calling done(), only finalized rows will be inserted.
|
||||
* @return InsertionBuilder Current instance for method chaining.
|
||||
*/
|
||||
public function done(): InsertionBuilder;
|
||||
|
||||
/**
|
||||
* Executes the insert query.
|
||||
* Automatically determines whether to insert a single row or batch insert.
|
||||
* @return void
|
||||
*/
|
||||
public function insert(): void;
|
||||
}
|
||||
|
@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DatabaseHelper\interfaces;
|
||||
|
||||
interface MigrationBlueprint
|
||||
{
|
||||
public function exec() : array;
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
<?php
|
||||
namespace DatabaseHelper\interfaces;
|
||||
|
||||
interface QueryBuilder
|
||||
{
|
||||
/**
|
||||
* Initializes a query builder with a table blueprint.
|
||||
* @param TableBlueprint $table Table to build queries for.
|
||||
*/
|
||||
public function __construct(TableBlueprint $table);
|
||||
|
||||
/**
|
||||
* Specifies the columns to select in the query.
|
||||
* @param string ...$cols The columns to select.
|
||||
* @return QueryBuilder Current instance for method chaining.
|
||||
*/
|
||||
public function select(string ...$cols): QueryBuilder;
|
||||
|
||||
/**
|
||||
* Adds a 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 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;
|
||||
}
|
||||
|
@ -1,59 +0,0 @@
|
||||
<?php
|
||||
namespace DatabaseHelper\interfaces;
|
||||
|
||||
use DatabaseHelper\enums\ColumnTypes;
|
||||
use DatabaseHelper\enums\CascadeTypes;
|
||||
use DatabaseHelper\enums\EngineTypes;
|
||||
use DatabaseHelper\enums\CharsetTypes;
|
||||
use DatabaseHelper\enums\CollationTypes;
|
||||
|
||||
/**
|
||||
* Defines a blueprint for building database tables with a fluent interface, which supports method chaining.
|
||||
*/
|
||||
interface TableBlueprint
|
||||
{
|
||||
public function __construct(string $tableName);
|
||||
|
||||
/**
|
||||
* Adds a column.
|
||||
* @param string $colName Column name.
|
||||
* @param ColumnTypes $colType Column type.
|
||||
* @param mixed|null $default Default value (optional).
|
||||
* @param bool $nullable Nullable column (default: false).
|
||||
* @param bool $unique Unique constraint (default: false).
|
||||
* @return TableBlueprint Current instance for method chaining.
|
||||
*/
|
||||
public function column(string $colName, ColumnTypes $colType, mixed $default = null, bool $nullable = false, bool $unique = false): TableBlueprint;
|
||||
|
||||
public function primary(string $colName, ColumnTypes $colType, bool $autoInc = false): TableBlueprint;
|
||||
|
||||
public function referenceColumn(TableBlueprint $foreignTable, CascadeTypes $onDelete, CascadeTypes $onUpdate): TableBlueprint;
|
||||
|
||||
/**
|
||||
* Sets the table engine.
|
||||
* @param EngineTypes $engine Storage engine.
|
||||
* @return TableBlueprint Current instance for method chaining.
|
||||
*/
|
||||
public function engine(EngineTypes $engine): TableBlueprint;
|
||||
|
||||
/**
|
||||
* Sets the character set.
|
||||
* @param CharsetTypes $charset Character set.
|
||||
* @return TableBlueprint Current instance for method chaining.
|
||||
*/
|
||||
public function charset(CharsetTypes $charset): TableBlueprint;
|
||||
|
||||
/**
|
||||
* Sets the collation.
|
||||
* @param CollationTypes $collation Collation to use.
|
||||
* @return TableBlueprint Current instance for method chaining.
|
||||
*/
|
||||
public function collation(CollationTypes $collation): TableBlueprint;
|
||||
|
||||
/**
|
||||
* Creates the blueprinted table.
|
||||
* @return TableBlueprint
|
||||
*/
|
||||
public function create(): TableBlueprint;
|
||||
}
|
||||
|
@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DatabaseHelper\interfaces;
|
||||
|
||||
interface UpdateBuilder
|
||||
{
|
||||
public function exec() : array;
|
||||
}
|
Loading…
Reference in New Issue
Block a user