2020-04-02 00:02:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Driver\SQL;
|
|
|
|
|
2020-04-03 17:39:58 +02:00
|
|
|
use Driver\SQL\Column\Column;
|
|
|
|
use Driver\SQL\Condition\Compare;
|
|
|
|
use Driver\SQL\Condition\CondBool;
|
2020-06-23 22:24:56 +02:00
|
|
|
use Driver\SQL\Condition\CondIn;
|
2020-06-26 14:58:17 +02:00
|
|
|
use Driver\SQL\Condition\Condition;
|
2020-06-24 16:09:04 +02:00
|
|
|
use Driver\SQL\Condition\CondKeyword;
|
2020-06-26 14:58:17 +02:00
|
|
|
use Driver\SQL\Condition\CondNot;
|
2021-01-13 01:36:04 +01:00
|
|
|
use Driver\Sql\Condition\CondNull;
|
2020-04-03 17:39:58 +02:00
|
|
|
use Driver\SQL\Condition\CondOr;
|
|
|
|
use Driver\SQL\Constraint\Constraint;
|
2020-04-03 14:46:29 +02:00
|
|
|
use \Driver\SQL\Constraint\Unique;
|
|
|
|
use \Driver\SQL\Constraint\PrimaryKey;
|
|
|
|
use \Driver\SQL\Constraint\ForeignKey;
|
2020-04-03 17:39:58 +02:00
|
|
|
use Driver\SQL\Query\CreateTable;
|
|
|
|
use Driver\SQL\Query\Delete;
|
2021-01-07 14:59:36 +01:00
|
|
|
use Driver\SQL\Query\Drop;
|
2020-04-03 17:39:58 +02:00
|
|
|
use Driver\SQL\Query\Insert;
|
2020-06-26 23:32:45 +02:00
|
|
|
use Driver\SQL\Query\Query;
|
2020-04-03 17:39:58 +02:00
|
|
|
use Driver\SQL\Query\Select;
|
|
|
|
use Driver\SQL\Query\Truncate;
|
|
|
|
use Driver\SQL\Query\Update;
|
|
|
|
use Driver\SQL\Strategy\CascadeStrategy;
|
|
|
|
use Driver\SQL\Strategy\SetDefaultStrategy;
|
|
|
|
use Driver\SQL\Strategy\SetNullStrategy;
|
|
|
|
use Driver\SQL\Strategy\Strategy;
|
|
|
|
use Objects\ConnectionData;
|
2020-04-03 14:46:29 +02:00
|
|
|
|
2020-04-02 00:02:51 +02:00
|
|
|
abstract class SQL {
|
|
|
|
|
2020-04-03 17:39:58 +02:00
|
|
|
protected string $lastError;
|
2020-04-02 00:02:51 +02:00
|
|
|
protected $connection;
|
2020-04-03 17:39:58 +02:00
|
|
|
protected ConnectionData $connectionData;
|
|
|
|
protected int $lastInsertId;
|
2020-04-02 00:02:51 +02:00
|
|
|
|
2020-04-02 01:48:46 +02:00
|
|
|
public function __construct($connectionData) {
|
2020-04-02 00:02:51 +02:00
|
|
|
$this->connection = NULL;
|
2020-04-03 14:46:29 +02:00
|
|
|
$this->lastError = 'Unknown Error';
|
2020-04-02 00:02:51 +02:00
|
|
|
$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) {
|
2020-06-26 23:32:45 +02:00
|
|
|
return new CreateTable($this, $tableName);
|
2020-04-02 00:02:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function insert($tableName, $columns=array()) {
|
2020-06-26 23:32:45 +02:00
|
|
|
return new Insert($this, $tableName, $columns);
|
2020-04-02 00:02:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function select(...$columNames) {
|
2020-06-26 23:32:45 +02:00
|
|
|
return new Select($this, $columNames);
|
2020-04-02 00:02:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function truncate($table) {
|
2020-06-26 23:32:45 +02:00
|
|
|
return new Truncate($this, $table);
|
2020-04-02 00:02:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($table) {
|
2020-06-26 23:32:45 +02:00
|
|
|
return new Delete($this, $table);
|
2020-04-02 00:02:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function update($table) {
|
2020-06-26 23:32:45 +02:00
|
|
|
return new Update($this, $table);
|
2020-04-02 00:02:51 +02:00
|
|
|
}
|
|
|
|
|
2021-01-07 14:59:36 +01:00
|
|
|
public function drop(string $table) {
|
|
|
|
return new Drop($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 00:02:51 +02:00
|
|
|
// Querybuilder
|
2020-06-26 23:32:45 +02:00
|
|
|
protected function buildQuery(Query $query, array &$params) {
|
|
|
|
if ($query instanceof Select) {
|
|
|
|
$select = $query;
|
|
|
|
$columns = $this->columnName($select->getColumns());
|
|
|
|
$tables = $select->getTables();
|
|
|
|
|
|
|
|
if (!$tables) {
|
|
|
|
return $this->execute("SELECT $columns", $params, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$tables = $this->tableName($tables);
|
|
|
|
$where = $this->getWhereClause($select->getConditions(), $params);
|
|
|
|
|
|
|
|
$joinStr = "";
|
|
|
|
$joins = $select->getJoins();
|
|
|
|
if (!empty($joins)) {
|
|
|
|
foreach($joins as $join) {
|
|
|
|
$type = $join->getType();
|
|
|
|
$joinTable = $this->tableName($join->getTable());
|
|
|
|
$columnA = $this->columnName($join->getColumnA());
|
|
|
|
$columnB = $this->columnName($join->getColumnB());
|
2021-01-07 14:59:36 +01:00
|
|
|
$tableAlias = ($join->getTableAlias() ? " " . $join->getTableAlias() : "");
|
|
|
|
|
|
|
|
$joinStr .= " $type JOIN $joinTable$tableAlias ON $columnA=$columnB";
|
2020-06-26 23:32:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$groupBy = "";
|
|
|
|
$groupColumns = $select->getGroupBy();
|
|
|
|
if (!empty($groupColumns)) {
|
|
|
|
$groupBy = " GROUP BY " . $this->columnName($groupColumns);
|
|
|
|
}
|
|
|
|
|
|
|
|
$orderBy = "";
|
|
|
|
$orderColumns = $select->getOrderBy();
|
|
|
|
if (!empty($orderColumns)) {
|
|
|
|
$orderBy = " ORDER BY " . $this->columnName($orderColumns);
|
|
|
|
$orderBy .= ($select->isOrderedAscending() ? " ASC" : " DESC");
|
|
|
|
}
|
|
|
|
|
|
|
|
$limit = ($select->getLimit() > 0 ? (" LIMIT " . $select->getLimit()) : "");
|
|
|
|
$offset = ($select->getOffset() > 0 ? (" OFFSET " . $select->getOffset()) : "");
|
|
|
|
return "SELECT $columns FROM $tables$joinStr$where$groupBy$orderBy$limit$offset";
|
|
|
|
} else {
|
|
|
|
$this->lastError = "buildQuery() not implemented for type: " . get_class($query);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 17:39:58 +02:00
|
|
|
public function executeCreateTable(CreateTable $createTable) {
|
2020-04-02 21:19:06 +02:00
|
|
|
$tableName = $this->tableName($createTable->getTableName());
|
|
|
|
$ifNotExists = $createTable->ifNotExists() ? " IF NOT EXISTS": "";
|
|
|
|
|
|
|
|
$entries = array();
|
|
|
|
foreach($createTable->getColumns() as $column) {
|
|
|
|
$entries[] = ($tmp = $this->getColumnDefinition($column));
|
|
|
|
if (is_null($tmp)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($createTable->getConstraints() as $constraint) {
|
|
|
|
$entries[] = ($tmp = $this->getConstraintDefinition($constraint));
|
|
|
|
if (is_null($tmp)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$entries = implode(",", $entries);
|
|
|
|
$query = "CREATE TABLE$ifNotExists $tableName ($entries)";
|
|
|
|
return $this->execute($query);
|
|
|
|
}
|
|
|
|
|
2020-04-03 17:39:58 +02:00
|
|
|
public function executeInsert(Insert $insert) {
|
2020-04-03 14:46:29 +02:00
|
|
|
|
2020-04-03 17:39:58 +02:00
|
|
|
$tableName = $this->tableName($insert->getTableName());
|
|
|
|
$columns = $insert->getColumns();
|
|
|
|
$rows = $insert->getRows();
|
|
|
|
|
|
|
|
if (empty($rows)) {
|
|
|
|
$this->lastError = "No rows to insert given.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($columns) || empty($columns)) {
|
|
|
|
$columnStr = "";
|
|
|
|
} else {
|
|
|
|
$columnStr = " (" . $this->columnName($columns) . ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
$parameters = array();
|
|
|
|
$values = array();
|
|
|
|
foreach($rows as $row) {
|
|
|
|
$rowPlaceHolder = array();
|
|
|
|
foreach($row as $val) {
|
|
|
|
$rowPlaceHolder[] = $this->addValue($val, $parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
$values[] = "(" . implode(",", $rowPlaceHolder) . ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
$values = implode(",", $values);
|
|
|
|
|
|
|
|
$onDuplicateKey = $this->getOnDuplicateStrategy($insert->onDuplicateKey(), $parameters);
|
|
|
|
if ($onDuplicateKey === FALSE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$returningCol = $insert->getReturning();
|
|
|
|
$returning = $this->getReturning($returningCol);
|
|
|
|
|
2020-06-18 15:08:09 +02:00
|
|
|
$query = "INSERT INTO $tableName$columnStr VALUES $values$onDuplicateKey$returning";
|
2020-04-04 01:15:59 +02:00
|
|
|
if($insert->dump) { var_dump($query); var_dump($parameters); }
|
2020-04-03 17:39:58 +02:00
|
|
|
$res = $this->execute($query, $parameters, !empty($returning));
|
|
|
|
$success = ($res !== FALSE);
|
|
|
|
|
|
|
|
if($success && $returningCol) {
|
|
|
|
$this->fetchReturning($res, $returningCol);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function executeSelect(Select $select) {
|
2020-04-03 14:46:29 +02:00
|
|
|
$params = array();
|
2020-06-26 23:32:45 +02:00
|
|
|
$query = $this->buildQuery($select, $params);
|
2020-04-04 01:15:59 +02:00
|
|
|
if($select->dump) { var_dump($query); var_dump($params); }
|
2020-04-03 14:46:29 +02:00
|
|
|
return $this->execute($query, $params, true);
|
|
|
|
}
|
|
|
|
|
2020-04-03 17:39:58 +02:00
|
|
|
public function executeDelete(Delete $delete) {
|
2020-04-03 14:46:29 +02:00
|
|
|
|
2020-06-23 20:57:54 +02:00
|
|
|
$params = array();
|
2020-04-03 14:46:29 +02:00
|
|
|
$table = $this->tableName($delete->getTable());
|
|
|
|
$where = $this->getWhereClause($delete->getConditions(), $params);
|
|
|
|
|
|
|
|
$query = "DELETE FROM $table$where";
|
2020-04-04 01:15:59 +02:00
|
|
|
if($delete->dump) { var_dump($query); }
|
2020-06-23 20:57:54 +02:00
|
|
|
return $this->execute($query, $params);
|
2020-04-03 14:46:29 +02:00
|
|
|
}
|
|
|
|
|
2020-04-03 17:39:58 +02:00
|
|
|
public function executeTruncate(Truncate $truncate) {
|
2020-06-25 16:54:58 +02:00
|
|
|
$query = "TRUNCATE " . $this->tableName($truncate->getTable());
|
|
|
|
if ($truncate->dump) { var_dump($query); }
|
|
|
|
return $this->execute($query);
|
2020-04-03 14:46:29 +02:00
|
|
|
}
|
|
|
|
|
2020-04-03 17:39:58 +02:00
|
|
|
public function executeUpdate(Update $update) {
|
2020-04-03 14:46:29 +02:00
|
|
|
|
|
|
|
$params = array();
|
|
|
|
$table = $this->tableName($update->getTable());
|
|
|
|
|
|
|
|
$valueStr = array();
|
|
|
|
foreach($update->getValues() as $key => $val) {
|
2020-06-23 20:57:54 +02:00
|
|
|
$valueStr[] = $this->columnName($key) . "=" . $this->addValue($val, $params);
|
2020-04-03 14:46:29 +02:00
|
|
|
}
|
|
|
|
$valueStr = implode(",", $valueStr);
|
|
|
|
|
|
|
|
$where = $this->getWhereClause($update->getConditions(), $params);
|
|
|
|
$query = "UPDATE $table SET $valueStr$where";
|
2020-04-04 01:15:59 +02:00
|
|
|
if($update->dump) { var_dump($query); var_dump($params); }
|
2020-04-03 14:46:29 +02:00
|
|
|
return $this->execute($query, $params);
|
|
|
|
}
|
|
|
|
|
2021-01-07 14:59:36 +01:00
|
|
|
public function executeDrop(Drop $drop) {
|
|
|
|
$query = "DROP TABLE " . $this->tableName($drop->getTable());
|
|
|
|
if ($drop->dump) { var_dump($query); }
|
|
|
|
return $this->execute($query);
|
|
|
|
}
|
|
|
|
|
2020-04-03 14:46:29 +02:00
|
|
|
protected function getWhereClause($conditions, &$params) {
|
|
|
|
if (!$conditions) {
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
return " WHERE " . $this->buildCondition($conditions, $params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 17:39:58 +02:00
|
|
|
public function getConstraintDefinition(Constraint $constraint) {
|
|
|
|
$columnName = $this->columnName($constraint->getColumnNames());
|
2020-04-03 14:46:29 +02:00
|
|
|
if ($constraint instanceof PrimaryKey) {
|
|
|
|
return "PRIMARY KEY ($columnName)";
|
|
|
|
} else if ($constraint instanceof Unique) {
|
|
|
|
return "UNIQUE ($columnName)";
|
|
|
|
} else if ($constraint instanceof ForeignKey) {
|
|
|
|
$refTable = $this->tableName($constraint->getReferencedTable());
|
|
|
|
$refColumn = $this->columnName($constraint->getReferencedColumn());
|
|
|
|
$strategy = $constraint->onDelete();
|
|
|
|
$code = "FOREIGN KEY ($columnName) REFERENCES $refTable ($refColumn)";
|
|
|
|
if ($strategy instanceof SetDefaultStrategy) {
|
|
|
|
$code .= " ON DELETE SET DEFAULT";
|
|
|
|
} else if($strategy instanceof SetNullStrategy) {
|
|
|
|
$code .= " ON DELETE SET NULL";
|
|
|
|
} else if($strategy instanceof CascadeStrategy) {
|
|
|
|
$code .= " ON DELETE CASCADE";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $code;
|
|
|
|
} else {
|
2020-04-03 17:39:58 +02:00
|
|
|
$this->lastError = "Unsupported constraint type: " . get_class($constraint);
|
|
|
|
return false;
|
2020-04-03 14:46:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 17:39:58 +02:00
|
|
|
protected function getReturning(?string $columns) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract function getColumnDefinition(Column $column);
|
|
|
|
protected abstract function fetchReturning($res, string $returningCol);
|
|
|
|
protected abstract function getOnDuplicateStrategy(?Strategy $strategy, &$params);
|
|
|
|
|
2020-04-02 00:02:51 +02:00
|
|
|
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
|
2020-06-22 21:50:58 +02:00
|
|
|
public function now() { return $this->currentTimestamp(); }
|
2020-04-02 01:48:46 +02:00
|
|
|
public abstract function currentTimestamp();
|
2020-04-02 21:19:06 +02:00
|
|
|
|
|
|
|
public function count($col = NULL) {
|
|
|
|
if (is_null($col)) {
|
|
|
|
return new Keyword("COUNT(*) AS count");
|
2020-07-18 12:51:36 +02:00
|
|
|
} else if($col instanceof Keyword) {
|
|
|
|
return new Keyword("COUNT(" . $col->getValue() . ") AS count");
|
2020-04-02 21:19:06 +02:00
|
|
|
} else {
|
2020-06-15 21:14:59 +02:00
|
|
|
$countCol = strtolower(str_replace(".","_", $col)) . "_count";
|
2020-04-02 21:19:06 +02:00
|
|
|
$col = $this->columnName($col);
|
2020-06-15 21:14:59 +02:00
|
|
|
return new Keyword("COUNT($col) AS $countCol");
|
2020-04-02 21:19:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 23:50:08 +02:00
|
|
|
public function sum($col) {
|
|
|
|
$sumCol = strtolower(str_replace(".","_", $col)) . "_sum";
|
|
|
|
$col = $this->columnName($col);
|
|
|
|
return new Keyword("SUM($col) AS $sumCol");
|
|
|
|
}
|
|
|
|
|
2020-04-02 21:19:06 +02:00
|
|
|
public function distinct($col) {
|
|
|
|
$col = $this->columnName($col);
|
|
|
|
return new Keyword("DISTINCT($col)");
|
|
|
|
}
|
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) {
|
2020-04-04 01:15:59 +02:00
|
|
|
|
2020-04-03 17:39:58 +02:00
|
|
|
if ($condition instanceof CondOr) {
|
2020-04-02 01:48:46 +02:00
|
|
|
$conditions = array();
|
|
|
|
foreach($condition->getConditions() as $cond) {
|
|
|
|
$conditions[] = $this->buildCondition($cond, $params);
|
|
|
|
}
|
|
|
|
return "(" . implode(" OR ", $conditions) . ")";
|
2020-04-03 17:39:58 +02:00
|
|
|
} else if ($condition instanceof 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);
|
2020-04-03 17:39:58 +02:00
|
|
|
} else if ($condition instanceof 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)) {
|
2020-04-04 01:15:59 +02:00
|
|
|
if (count($condition) === 1) {
|
2020-04-02 01:48:46 +02:00
|
|
|
return $this->buildCondition($condition[0], $params);
|
|
|
|
} else {
|
|
|
|
$conditions = array();
|
2020-06-19 14:12:07 +02:00
|
|
|
foreach ($condition as $cond) {
|
2020-04-02 01:48:46 +02:00
|
|
|
$conditions[] = $this->buildCondition($cond, $params);
|
|
|
|
}
|
|
|
|
return implode(" AND ", $conditions);
|
|
|
|
}
|
2020-06-23 22:24:56 +02:00
|
|
|
} else if($condition instanceof CondIn) {
|
2020-06-26 14:58:17 +02:00
|
|
|
|
2020-06-26 23:32:45 +02:00
|
|
|
$expression = $condition->getExpression();
|
|
|
|
if (is_array($expression)) {
|
|
|
|
$values = array();
|
|
|
|
foreach ($expression as $value) {
|
|
|
|
$values[] = $this->addValue($value, $params);
|
|
|
|
}
|
2020-06-26 14:58:17 +02:00
|
|
|
|
2020-06-26 23:32:45 +02:00
|
|
|
$values = implode(",", $values);
|
|
|
|
} else if($expression instanceof Select) {
|
|
|
|
$values = $this->buildQuery($expression, $params);
|
|
|
|
} else {
|
|
|
|
$this->lastError = "Unsupported in-expression value: " . get_class($condition);
|
|
|
|
return false;
|
2020-06-23 22:24:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->columnName($condition->getColumn()) . " IN ($values)";
|
2020-06-24 16:09:04 +02:00
|
|
|
} else if($condition instanceof CondKeyword) {
|
|
|
|
$left = $condition->getLeftExp();
|
|
|
|
$right = $condition->getRightExp();
|
|
|
|
$keyword = $condition->getKeyword();
|
|
|
|
$left = ($left instanceof Column) ? $this->columnName($left->getName()) : $this->addValue($left, $params);
|
|
|
|
$right = ($right instanceof Column) ? $this->columnName($right->getName()) : $this->addValue($right, $params);
|
|
|
|
return "$left $keyword $right ";
|
2020-06-26 14:58:17 +02:00
|
|
|
} else if($condition instanceof CondNot) {
|
|
|
|
$expression = $condition->getExpression();
|
|
|
|
if ($expression instanceof Condition) {
|
|
|
|
$expression = $this->buildCondition($expression, $params);
|
|
|
|
} else {
|
|
|
|
$expression = $this->columnName($expression);
|
|
|
|
}
|
|
|
|
|
|
|
|
return "NOT $expression";
|
2021-01-13 01:36:04 +01:00
|
|
|
} else if($condition instanceof CondNull) {
|
|
|
|
return $this->columnName($condition->getColumn()) . " IS NULL";
|
2020-04-03 17:39:58 +02:00
|
|
|
} else {
|
|
|
|
$this->lastError = "Unsupported condition type: " . get_class($condition);
|
|
|
|
return false;
|
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
|
|
|
}
|
|
|
|
|
2020-04-03 17:39:58 +02:00
|
|
|
public static function createConnection(ConnectionData $connectionData) {
|
2020-04-02 00:02:51 +02:00
|
|
|
$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;
|
|
|
|
}
|
2020-06-19 13:13:13 +02:00
|
|
|
|
|
|
|
public abstract function getStatus();
|
2020-06-25 16:54:58 +02:00
|
|
|
|
|
|
|
public function parseBool($val) : bool {
|
2020-06-25 21:53:33 +02:00
|
|
|
return in_array($val, array(true, 1, '1', 't', 'true', 'TRUE'), true);
|
2020-06-25 16:54:58 +02:00
|
|
|
}
|
2020-04-03 17:39:58 +02:00
|
|
|
}
|