Database abstraction

This commit is contained in:
2020-04-02 00:02:51 +02:00
parent 26d28377be
commit 81995b06b8
51 changed files with 1660 additions and 641 deletions

View File

@@ -0,0 +1,97 @@
<?php
namespace Driver\SQL\Query;
use Driver\SQL\Column\SerialColumn;
use Driver\SQL\Column\StringColumn;
use Driver\SQL\Column\IntColumn;
use Driver\SQL\Column\DateTimeColumn;
use Driver\SQL\Column\EnumColumn;
use Driver\SQL\Column\BoolColumn;
use Driver\SQL\Column\JsonColumn;
use Driver\SQL\Constraint\PrimaryKey;
use Driver\SQL\Constraint\Unique;
use Driver\SQL\Constraint\ForeignKey;
class CreateTable extends Query {
private $tableName;
private $columns;
private $constraints;
private $ifNotExists;
public function __construct($sql, $name) {
parent::__construct($sql);
$this->tableName = $name;
$this->columns = array();
$this->constraints = array();
$this->ifNotExists = false;
}
public function addSerial($name) {
$this->columns[$name] = new SerialColumn($name);
return $this;
}
public function addString($name, $maxSize=NULL, $nullable=false, $defaultValue=NULL) {
$this->columns[$name] = new StringColumn($name, $maxSize, $nullable, $defaultValue);
return $this;
}
public function addDateTime($name, $nullable=false, $defaultNow=false) {
$this->columns[$name] = new DateTimeColumn($name, $nullable, $defaultNow);
return $this;
}
public function addInt($name, $nullable=false, $defaultValue=NULL) {
$this->columns[$name] = new IntColumn($name, $nullable, $defaultValue);
return $this;
}
public function addBool($name, $defaultValue=false) {
$this->columns[$name] = new BoolColumn($name, $defaultValue);
return $this;
}
public function addJson($name, $nullable=false, $defaultValue=NULL) {
$this->columns[$name] = new JsonColumn($name, $nullable, $defaultValue);
return $this;
}
public function addEnum($name, $values, $nullable=false, $defaultValue=NULL) {
$this->columns[$name] = new EnumColumn($name, $values, $nullable, $defaultValue);
return $this;
}
public function primaryKey(...$names) {
$this->constraints[] = new PrimaryKey($names);
return $this;
}
public function unique(...$names) {
$this->constraints[] = new Unique($names);
return $this;
}
public function foreignKey($name, $refTable, $refColumn, $strategy = NULL) {
$this->constraints[] = new ForeignKey($name, $refTable, $refColumn, $strategy);
return $this;
}
public function onlyIfNotExists() {
$this->ifNotExists = true;
return $this;
}
public function execute() {
return $this->sql->executeCreateTable($this);
}
public function ifNotExists() { return $this->ifNotExists; }
public function getTableName() { return $this->tableName; }
public function getColumns() { return $this->columns; }
public function getConstraints() { return $this->constraints; }
};
?>

View File

@@ -0,0 +1,29 @@
<?php
namespace Driver\SQL\Query;
class Delete extends Query {
private $table;
private $conditions;
public function __construct($sql, $table) {
parent::__construct($sql);
$this->table = $table;
$this->conditions = array();
}
public function where(...$conditions) {
$this->conditions = array_merge($this->conditions, $conditions);
return $this;
}
public function execute() {
return $this->sql->executeDelete($this);
}
public function getTable() { return $this->table; }
public function getConditions() { return $this->conditions; }
};
?>

View File

@@ -0,0 +1,40 @@
<?php
namespace Driver\SQL\Query;
class Insert extends Query {
private $tableName;
private $columns;
private $rows;
private $onDuplicateKey;
public function __construct($sql, $name, $columns=array()) {
parent::__construct($sql);
$this->tableName = $name;
$this->columns = $columns;
$this->rows = array();
$this->onDuplicateKey = NULL;
}
public function addRow(...$values) {
$this->rows[] = $values;
return $this;
}
public function onDuplicateKeyStrategy($strategy) {
$this->onDuplicateKey = $strategy;
return $this;
}
public function execute() {
return $this->sql->executeInsert($this);
}
public function getTableName() { return $this->tableName; }
public function getColumns() { return $this->columns; }
public function getRows() { return $this->rows; }
public function onDuplicateKey() { return $this->onDuplicateKey; }
};
?>

View File

@@ -0,0 +1,17 @@
<?php
namespace Driver\SQL\Query;
abstract class Query {
protected $sql;
public function __construct($sql) {
$this->sql = $sql;
}
public abstract function execute();
};
?>

View File

@@ -0,0 +1,50 @@
<?php
namespace Driver\SQL\Query;
class Select extends Query {
private $columns;
private $tables;
private $conditions;
private $joins;
public function __construct($sql, ...$columns) {
parent::__construct($sql);
$this->columns = (!empty($columns) && is_array($columns[0])) ? $columns[0] : $columns;
$this->tables = array();
$this->conditions = array();
$this->joins = array();
}
public function from(...$tables) {
$this->tables = array_merge($this->tables, $tables);
return $this;
}
public function where(...$conditions) {
$this->conditions = array_merge($this->conditions, $conditions);
return $this;
}
public function innerJoin($table, $columnA, $columnB) {
$this->joins[] = new \Driver\SQL\Join("INNER", $table, $columnA, $columnB);
return $this;
}
public function leftJoin($table, $columnA, $columnB) {
$this->joins[] = new \Driver\SQL\Join("LEFT", $table, $columnA, $columnB);
return $this;
}
public function execute() {
return $this->sql->executeSelect($this);
}
public function getColumns() { return $this->columns; }
public function getTables() { return $this->tables; }
public function getConditions() { return $this->conditions; }
public function getJoins() { return $this->joins; }
};
?>

View File

@@ -0,0 +1,21 @@
<?php
namespace Driver\SQL\Query;
class Truncate extends Query {
private $tableName;
public function __construct($sql, $name) {
parent::__construct($sql);
$this->tableName = $name;
}
public function execute() {
return $this->sql->executeTruncate($this);
}
public function getTableName() { return $this->tableName; }
};
?>

View File

@@ -0,0 +1,37 @@
<?php
namespace Driver\SQL\Query;
class Update extends Query {
private $values;
private $table;
private $conditions;
public function __construct($sql, $table) {
parent::__construct($sql);
$this->values = array();
$this->table = $table;
$this->conditions = array();
}
public function where(...$conditions) {
$this->conditions = array_merge($this->conditions, $conditions);
return $this;
}
public function set($key, $val) {
$this->values[$key] = $val;
return $this;
}
public function execute() {
return $this->sql->executeUpdate($this);
}
public function getTable() { return $this->table; }
public function getConditions() { return $this->conditions; }
public function getValues() { return $this->values; }
};
?>