Database abstraction
This commit is contained in:
97
core/Driver/SQL/Query/CreateTable.class.php
Normal file
97
core/Driver/SQL/Query/CreateTable.class.php
Normal 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; }
|
||||
};
|
||||
|
||||
?>
|
||||
29
core/Driver/SQL/Query/Delete.class.php
Normal file
29
core/Driver/SQL/Query/Delete.class.php
Normal 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; }
|
||||
};
|
||||
|
||||
?>
|
||||
40
core/Driver/SQL/Query/Insert.class.php
Normal file
40
core/Driver/SQL/Query/Insert.class.php
Normal 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; }
|
||||
};
|
||||
|
||||
?>
|
||||
17
core/Driver/SQL/Query/Query.class.php
Normal file
17
core/Driver/SQL/Query/Query.class.php
Normal 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();
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
50
core/Driver/SQL/Query/Select.class.php
Normal file
50
core/Driver/SQL/Query/Select.class.php
Normal 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; }
|
||||
};
|
||||
|
||||
?>
|
||||
21
core/Driver/SQL/Query/Truncate.class.php
Normal file
21
core/Driver/SQL/Query/Truncate.class.php
Normal 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; }
|
||||
};
|
||||
|
||||
?>
|
||||
37
core/Driver/SQL/Query/Update.class.php
Normal file
37
core/Driver/SQL/Query/Update.class.php
Normal 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; }
|
||||
};
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user