Postgres Support
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Api;
|
||||
|
||||
use \Driver\SQL\Keyword;
|
||||
use \Driver\SQL\Condition\Compare;
|
||||
|
||||
class GetApiKeys extends Request {
|
||||
@@ -21,7 +20,7 @@ class GetApiKeys extends Request {
|
||||
$res = $sql->select("uid", "api_key", "valid_until")
|
||||
->from("ApiKey")
|
||||
->where(new Compare("user_id", $this->user->getId()))
|
||||
->where(new Compare("valid_until", new Keyword($sql->currentTimestamp()), ">"))
|
||||
->where(new Compare("valid_until", $sql->currentTimestamp(), ">"))
|
||||
->where(new Compare("active", true))
|
||||
->execute();
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace Api;
|
||||
|
||||
use \Api\Parameter\Parameter;
|
||||
use \Driver\SQL\Keyword;
|
||||
use \Driver\SQL\Condition\Compare;
|
||||
|
||||
class RefreshApiKey extends Request {
|
||||
@@ -23,7 +22,7 @@ class RefreshApiKey extends Request {
|
||||
->from("ApiKey")
|
||||
->where(new Compare("uid", $id))
|
||||
->where(new Compare("user_id", $this->user->getId()))
|
||||
->where(new Compare("valid_until", new Keyword($sql->currentTimestamp()), ">"))
|
||||
->where(new Compare("valid_until", $sql->currentTimestamp(), ">"))
|
||||
->where(new Compare("active", 1))
|
||||
->execute();
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace Api;
|
||||
|
||||
use \Api\Parameter\Parameter;
|
||||
use \Driver\SQL\Keyword;
|
||||
use \Driver\SQL\Condition\Compare;
|
||||
|
||||
class RevokeApiKey extends Request {
|
||||
@@ -23,7 +22,7 @@ class RevokeApiKey extends Request {
|
||||
->from("ApiKey")
|
||||
->where(new Compare("uid", $id))
|
||||
->where(new Compare("user_id", $this->user->getId()))
|
||||
->where(new Compare("valid_until", new Keyword($sql->currentTimestamp()), ">"))
|
||||
->where(new Compare("valid_until", $sql->currentTimestamp(), ">"))
|
||||
->where(new Compare("active", 1))
|
||||
->execute();
|
||||
|
||||
|
||||
@@ -97,7 +97,9 @@ namespace Documents\Install {
|
||||
return self::DATABASE_CONFIGURATION;
|
||||
}
|
||||
|
||||
$res = $user->getSQL()->select("COUNT(*) as count")->from("User")->execute();
|
||||
$sql = $user->getSQL();
|
||||
$countKeyword = $sql->count();
|
||||
$res = $sql->select($countKeyword)->from("User")->execute();
|
||||
if ($res === FALSE) {
|
||||
return self::DATABASE_CONFIGURATION;
|
||||
} else {
|
||||
|
||||
@@ -26,14 +26,13 @@ class MySQL extends SQL {
|
||||
|
||||
public function __construct($connectionData) {
|
||||
parent::__construct($connectionData);
|
||||
$this->installLink = ;
|
||||
}
|
||||
|
||||
public function checkRequirements() {
|
||||
return function_exists('mysqli_connect');
|
||||
}
|
||||
|
||||
public abstract function getDriverName() {
|
||||
public function getDriverName() {
|
||||
return 'mysqli';
|
||||
}
|
||||
|
||||
@@ -248,7 +247,16 @@ class MySQL extends SQL {
|
||||
|
||||
public function executeSelect($select) {
|
||||
|
||||
$columns = implode(",", $select->getColumns());
|
||||
$columns = array();
|
||||
foreach($select->getColumns() as $col) {
|
||||
if ($col instanceof Keyword) {
|
||||
$columns[] = $col->getValue();
|
||||
} else {
|
||||
$columns[] = "`$col`";
|
||||
}
|
||||
}
|
||||
|
||||
$columns = implode(",", $columns);
|
||||
$tables = $select->getTables();
|
||||
$params = array();
|
||||
|
||||
@@ -364,7 +372,7 @@ class MySQL extends SQL {
|
||||
if (!is_null($column->getDefaultValue()) || !$column->notNull()) {
|
||||
$defaultValue = " DEFAULT " . $this->getValueDefinition($column->getDefaultValue());
|
||||
}
|
||||
|
||||
|
||||
return "`$columnName` $type$notNull$defaultValue";
|
||||
}
|
||||
|
||||
@@ -416,7 +424,28 @@ class MySQL extends SQL {
|
||||
}
|
||||
}
|
||||
|
||||
public function currentTimestamp() {
|
||||
return "NOW()";
|
||||
protected function tableName($table) {
|
||||
return "`$table`";
|
||||
}
|
||||
|
||||
protected function columnName($col) {
|
||||
if ($col instanceof KeyWord) {
|
||||
return $col->getValue();
|
||||
} else {
|
||||
return "`$col`";
|
||||
}
|
||||
}
|
||||
|
||||
public function currentTimestamp() {
|
||||
return new KeyWord("NOW()");
|
||||
}
|
||||
|
||||
public function count($col = NULL) {
|
||||
if (is_null($col)) {
|
||||
return new Keyword("COUNT(*)");
|
||||
} else {
|
||||
return new Keyword("COUNT($col)");
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -137,7 +137,7 @@ class PostgreSQL extends SQL {
|
||||
|
||||
// Querybuilder
|
||||
public function executeCreateTable($createTable) {
|
||||
$tableName = $createTable->getTableName();
|
||||
$tableName = $this->tableName($createTable->getTableName());
|
||||
$ifNotExists = $createTable->ifNotExists() ? " IF NOT EXISTS": "";
|
||||
|
||||
$entries = array();
|
||||
@@ -156,13 +156,13 @@ class PostgreSQL extends SQL {
|
||||
}
|
||||
|
||||
$entries = implode(",", $entries);
|
||||
$query = "CREATE TABLE$ifNotExists \"$tableName\" ($entries)";
|
||||
$query = "CREATE TABLE$ifNotExists $tableName ($entries)";
|
||||
return $this->execute($query);
|
||||
}
|
||||
|
||||
public function executeInsert($insert) {
|
||||
|
||||
$tableName = $insert->getTableName();
|
||||
$tableName = $this->tableName($insert->getTableName());
|
||||
$columns = $insert->getColumns();
|
||||
$rows = $insert->getRows();
|
||||
$onDuplicateKey = $insert->onDuplicateKey() ?? "";
|
||||
@@ -173,11 +173,15 @@ class PostgreSQL extends SQL {
|
||||
}
|
||||
|
||||
if (is_null($columns) || empty($columns)) {
|
||||
$columns = "";
|
||||
$columnStr = "";
|
||||
$numColumns = count($rows[0]);
|
||||
} else {
|
||||
$numColumns = count($columns);
|
||||
$columns = " (\"" . implode("\", \"", $columns) . "\")";
|
||||
$columnStr = array();
|
||||
foreach($columns as $col) {
|
||||
$columnStr[] = $this->columnName($col);
|
||||
}
|
||||
$columnStr = " (" . implode(",", $columnStr) . ")";
|
||||
}
|
||||
|
||||
$numRows = count($rows);
|
||||
@@ -196,7 +200,7 @@ class PostgreSQL extends SQL {
|
||||
$values = implode(",", $values);
|
||||
|
||||
if ($onDuplicateKey) {
|
||||
if ($onDuplicateKey instanceof UpdateStrategy) {
|
||||
/*if ($onDuplicateKey instanceof UpdateStrategy) {
|
||||
$updateValues = array();
|
||||
foreach($onDuplicateKey->getValues() as $key => $value) {
|
||||
if ($value instanceof Column) {
|
||||
@@ -208,7 +212,7 @@ class PostgreSQL extends SQL {
|
||||
}
|
||||
|
||||
$onDuplicateKey = " ON CONFLICT DO UPDATE SET " . implode(",", $updateValues);
|
||||
} else {
|
||||
} else*/ {
|
||||
$strategy = get_class($onDuplicateKey);
|
||||
$this->lastError = "ON DUPLICATE Strategy $strategy is not supported yet.";
|
||||
return false;
|
||||
@@ -216,9 +220,9 @@ class PostgreSQL extends SQL {
|
||||
}
|
||||
|
||||
$returningCol = $insert->getReturning();
|
||||
$returning = $returningCol ? " RETURNING \"$returningCol\"" : "";
|
||||
$returning = $returningCol ? (" RETURNING " . $this->columnName($returningCol)) : "";
|
||||
|
||||
$query = "INSERT INTO \"$tableName\"$columns VALUES$values$onDuplicateKey$returning";
|
||||
$query = "INSERT INTO $tableName$columnStr VALUES$values$onDuplicateKey$returning";
|
||||
$res = $this->execute($query, $parameters, !empty($returning));
|
||||
$success = ($res !== FALSE);
|
||||
|
||||
@@ -229,11 +233,93 @@ class PostgreSQL extends SQL {
|
||||
return $success;
|
||||
}
|
||||
|
||||
// TODO:
|
||||
public function executeSelect($query) { }
|
||||
public function executeDelete($query) { }
|
||||
public function executeTruncate($query) { }
|
||||
public function executeUpdate($query) { }
|
||||
public function executeSelect($select) {
|
||||
|
||||
$columns = array();
|
||||
foreach($select->getColumns() as $col) {
|
||||
$columns[] = $this->columnName($col);
|
||||
}
|
||||
|
||||
$columns = implode(",", $columns);
|
||||
$tables = $select->getTables();
|
||||
$params = array();
|
||||
|
||||
if (is_null($tables) || empty($tables)) {
|
||||
return "SELECT $columns";
|
||||
} else {
|
||||
$tableStr = array();
|
||||
foreach($tables as $table) {
|
||||
$tableStr[] = $this->tableName($table);
|
||||
}
|
||||
$tableStr = implode(",", $tableStr);
|
||||
}
|
||||
|
||||
$conditions = $select->getConditions();
|
||||
if (!empty($conditions)) {
|
||||
$condition = " WHERE " . $this->buildCondition($conditions, $params);
|
||||
} else {
|
||||
$condition = "";
|
||||
}
|
||||
|
||||
$joinStr = "";
|
||||
$joins = $select->getJoins();
|
||||
if (!empty($joins)) {
|
||||
$joinStr = "";
|
||||
foreach($joins as $join) {
|
||||
$type = $join->getType();
|
||||
$joinTable = $this->tableName($join->getTable());
|
||||
$columnA = $this->columnName($join->getColumnA());
|
||||
$columnB = $this->columnName($join->getColumnB());
|
||||
$joinStr .= " $type JOIN $joinTable ON $columnA=$columnB";
|
||||
}
|
||||
}
|
||||
|
||||
$orderBy = "";
|
||||
$limit = "";
|
||||
$offset = "";
|
||||
|
||||
$query = "SELECT $columns FROM $tableStr$joinStr$condition$orderBy$limit$offset";
|
||||
return $this->execute($query, $params, true);
|
||||
}
|
||||
|
||||
public function executeDelete($delete) {
|
||||
$table = $delete->getTable();
|
||||
$conditions = $delete->getConditions();
|
||||
if (!empty($conditions)) {
|
||||
$condition = " WHERE " . $this->buildCondition($conditions, $params);
|
||||
} else {
|
||||
$condition = "";
|
||||
}
|
||||
|
||||
$query = "DELETE FROM \"$table\"$condition";
|
||||
return $this->execute($query);
|
||||
}
|
||||
|
||||
public function executeTruncate($truncate) {
|
||||
$table = $truncate->getTable();
|
||||
return $this->execute("TRUNCATE \"$table\"");
|
||||
}
|
||||
|
||||
public function executeUpdate($update) {
|
||||
$params = array();
|
||||
$table = $update->getTable();
|
||||
|
||||
$valueStr = array();
|
||||
foreach($update->getValues() as $key => $val) {
|
||||
$valueStr[] = "$key=" . $this->addValue($val, $params);
|
||||
}
|
||||
$valueStr = implode(",", $valueStr);
|
||||
|
||||
$conditions = $update->getConditions();
|
||||
if (!empty($conditions)) {
|
||||
$condition = " WHERE " . $this->buildCondition($conditions, $params);
|
||||
} else {
|
||||
$condition = "";
|
||||
}
|
||||
|
||||
$query = "UPDATE \"$table\" SET $valueStr$condition";
|
||||
return $this->execute($query, $params);
|
||||
}
|
||||
|
||||
// UGLY but.. what should i do?
|
||||
private function createEnum($enumColumn) {
|
||||
@@ -344,9 +430,36 @@ class PostgreSQL extends SQL {
|
||||
}
|
||||
}
|
||||
|
||||
protected function tableName($table) {
|
||||
return "\"$table\"";
|
||||
}
|
||||
|
||||
protected function columnName($col) {
|
||||
if ($col instanceof KeyWord) {
|
||||
return $col->getValue();
|
||||
} else {
|
||||
$index = strrpos($col, ".");
|
||||
if ($index === FALSE) {
|
||||
return "\"$col\"";
|
||||
} else {
|
||||
$tableName = $this->tableName(substr($col, 0, $index));
|
||||
$columnName = $this->columnName(substr($col, $index + 1));
|
||||
return "$tableName.$columnName";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Special Keywords and functions
|
||||
public function currentTimestamp() {
|
||||
return "CURRENT_TIMESTAMP";
|
||||
return new Keyword("CURRENT_TIMESTAMP");
|
||||
}
|
||||
|
||||
public function count($col = NULL) {
|
||||
if (is_null($col)) {
|
||||
return new Keyword("COUNT(*)");
|
||||
} else {
|
||||
return new Keyword("COUNT(\"$col\")");
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
@@ -72,8 +72,12 @@ abstract class SQL {
|
||||
protected abstract function getValueDefinition($val);
|
||||
protected abstract function addValue($val, &$params);
|
||||
|
||||
protected abstract function tableName($table);
|
||||
protected abstract function columnName($col);
|
||||
|
||||
// Special Keywords and functions
|
||||
public abstract function currentTimestamp();
|
||||
public abstract function count($col = NULL);
|
||||
|
||||
// Statements
|
||||
protected abstract function execute($query, $values=NULL, $returnValues=false);
|
||||
@@ -86,12 +90,12 @@ abstract class SQL {
|
||||
}
|
||||
return "(" . implode(" OR ", $conditions) . ")";
|
||||
} else if ($condition instanceof \Driver\SQL\Condition\Compare) {
|
||||
$column = $condition->getColumn();
|
||||
$column = $this->columnName($condition->getColumn());
|
||||
$value = $condition->getValue();
|
||||
$operator = $condition->getOperator();
|
||||
return $column . $operator . $this->addValue($value, $params);
|
||||
} else if ($condition instanceof \Driver\SQL\Condition\CondBool) {
|
||||
return $condition->getValue();
|
||||
return $this->columnName($condition->getValue());
|
||||
} else if (is_array($condition)) {
|
||||
if (count($condition) == 1) {
|
||||
return $this->buildCondition($condition[0], $params);
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Objects;
|
||||
|
||||
use Driver\SQL\Keyword;
|
||||
use Driver\SQL\Column\Column;
|
||||
use Driver\SQL\Condition\Compare;
|
||||
use Driver\SQL\Condition\CondBool;
|
||||
@@ -113,7 +112,7 @@ class User extends ApiObject {
|
||||
->where(new Compare("User.uid", $userId))
|
||||
->where(new Compare("Session.uid", $sessionId))
|
||||
->where(new Compare("Session.active", true))
|
||||
->where(new CondBool("Session.stay_logged_in"), new Compare("Session.expires", new Keyword($this->sql->currentTimestamp()), '>'))
|
||||
->where(new CondBool("Session.stay_logged_in"), new Compare("Session.expires", $this->sql->currentTimestamp(), '>'))
|
||||
->execute();
|
||||
|
||||
$success = ($res !== FALSE);
|
||||
@@ -189,7 +188,7 @@ class User extends ApiObject {
|
||||
->innerJoin("User", "ApiKey.user_id", "User.uid")
|
||||
->leftJoin("Language", "User.language_id", "Language.uid")
|
||||
->where(new Compare("ApiKey.api_key", $apiKey))
|
||||
->where(new Compare("valid_until", new Keyword($this->sql->currentTimestamp()), ">"))
|
||||
->where(new Compare("valid_until", $this->sql->currentTimestamp(), ">"))
|
||||
->where(new COmpare("ApiKey.active", 1))
|
||||
->execute();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user