2020-04-02 00:02:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Driver\SQL;
|
|
|
|
|
|
|
|
abstract class SQL {
|
|
|
|
|
|
|
|
protected $lastError;
|
|
|
|
protected $connection;
|
|
|
|
protected $connectionData;
|
|
|
|
protected $lastInsertId;
|
|
|
|
|
2020-04-02 01:48:46 +02:00
|
|
|
public function __construct($connectionData) {
|
2020-04-02 00:02:51 +02:00
|
|
|
$this->connection = NULL;
|
|
|
|
$this->lastError = 'Not connected';
|
|
|
|
$this->connectionData = $connectionData;
|
|
|
|
$this->lastInsertId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isConnected() {
|
|
|
|
return !is_null($this->connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLastError() {
|
|
|
|
return trim($this->lastError);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createTable($tableName) {
|
|
|
|
return new Query\CreateTable($this, $tableName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function insert($tableName, $columns=array()) {
|
|
|
|
return new Query\Insert($this, $tableName, $columns);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function select(...$columNames) {
|
|
|
|
return new Query\Select($this, $columNames);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function truncate($table) {
|
|
|
|
return new Query\Truncate($this, $table);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($table) {
|
|
|
|
return new Query\Delete($this, $table);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update($table) {
|
|
|
|
return new Query\Update($this, $table);
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:48:46 +02:00
|
|
|
// ####################
|
|
|
|
// ### ABSTRACT METHODS
|
|
|
|
// ####################
|
|
|
|
|
|
|
|
// Misc
|
|
|
|
public abstract function checkRequirements();
|
|
|
|
public abstract function getDriverName();
|
|
|
|
|
|
|
|
// Connection Managment
|
|
|
|
public abstract function connect();
|
|
|
|
public abstract function disconnect();
|
|
|
|
|
2020-04-02 16:31:17 +02:00
|
|
|
// TODO: pull code duplicates up
|
|
|
|
|
2020-04-02 00:02:51 +02:00
|
|
|
// Querybuilder
|
|
|
|
public abstract function executeCreateTable($query);
|
|
|
|
public abstract function executeInsert($query);
|
|
|
|
public abstract function executeSelect($query);
|
|
|
|
public abstract function executeDelete($query);
|
|
|
|
public abstract function executeTruncate($query);
|
|
|
|
public abstract function executeUpdate($query);
|
|
|
|
protected abstract function getColumnDefinition($column);
|
|
|
|
protected abstract function getConstraintDefinition($constraint);
|
|
|
|
protected abstract function getValueDefinition($val);
|
2020-04-02 01:48:46 +02:00
|
|
|
protected abstract function addValue($val, &$params);
|
2020-04-02 00:02:51 +02:00
|
|
|
|
2020-04-02 15:08:14 +02:00
|
|
|
protected abstract function tableName($table);
|
|
|
|
protected abstract function columnName($col);
|
|
|
|
|
2020-04-02 01:48:46 +02:00
|
|
|
// Special Keywords and functions
|
|
|
|
public abstract function currentTimestamp();
|
2020-04-02 15:08:14 +02:00
|
|
|
public abstract function count($col = NULL);
|
2020-04-02 00:02:51 +02:00
|
|
|
|
2020-04-02 01:48:46 +02:00
|
|
|
// Statements
|
|
|
|
protected abstract function execute($query, $values=NULL, $returnValues=false);
|
2020-04-02 00:02:51 +02:00
|
|
|
|
2020-04-02 01:48:46 +02:00
|
|
|
protected function buildCondition($condition, &$params) {
|
|
|
|
if ($condition instanceof \Driver\SQL\Condition\CondOr) {
|
|
|
|
$conditions = array();
|
|
|
|
foreach($condition->getConditions() as $cond) {
|
|
|
|
$conditions[] = $this->buildCondition($cond, $params);
|
|
|
|
}
|
|
|
|
return "(" . implode(" OR ", $conditions) . ")";
|
|
|
|
} else if ($condition instanceof \Driver\SQL\Condition\Compare) {
|
2020-04-02 15:08:14 +02:00
|
|
|
$column = $this->columnName($condition->getColumn());
|
2020-04-02 01:48:46 +02:00
|
|
|
$value = $condition->getValue();
|
|
|
|
$operator = $condition->getOperator();
|
|
|
|
return $column . $operator . $this->addValue($value, $params);
|
|
|
|
} else if ($condition instanceof \Driver\SQL\Condition\CondBool) {
|
2020-04-02 15:08:14 +02:00
|
|
|
return $this->columnName($condition->getValue());
|
2020-04-02 01:48:46 +02:00
|
|
|
} else if (is_array($condition)) {
|
|
|
|
if (count($condition) == 1) {
|
|
|
|
return $this->buildCondition($condition[0], $params);
|
|
|
|
} else {
|
|
|
|
$conditions = array();
|
|
|
|
foreach($condition as $cond) {
|
|
|
|
$conditions[] = $this->buildCondition($cond, $params);
|
|
|
|
}
|
|
|
|
return implode(" AND ", $conditions);
|
|
|
|
}
|
2020-04-02 00:02:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:48:46 +02:00
|
|
|
public function setLastError($str) {
|
|
|
|
$this->lastError = $str;
|
|
|
|
}
|
2020-04-02 00:02:51 +02:00
|
|
|
|
|
|
|
public function getLastInsertId() {
|
|
|
|
return $this->lastInsertId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function close() {
|
2020-04-02 01:48:46 +02:00
|
|
|
$this->disconnect();
|
|
|
|
$this->connection = NULL;
|
2020-04-02 00:02:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function createConnection($connectionData) {
|
|
|
|
$type = $connectionData->getProperty("type");
|
|
|
|
if ($type === "mysql") {
|
|
|
|
$sql = new MySQL($connectionData);
|
2020-04-02 01:48:46 +02:00
|
|
|
} else if ($type === "postgres") {
|
|
|
|
$sql = new PostgreSQL($connectionData);
|
|
|
|
/*} else if ($type === "oracle") {
|
2020-04-02 00:02:51 +02:00
|
|
|
// $sql = new OracleSQL($connectionData);
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
return "Unknown database type";
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:48:46 +02:00
|
|
|
if ($sql->checkRequirements()) {
|
|
|
|
$sql->connect();
|
|
|
|
}
|
|
|
|
|
2020-04-02 00:02:51 +02:00
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|