web-base/core/Driver/SQL/PostgreSQL.class.php

304 lines
8.7 KiB
PHP
Raw Normal View History

2020-04-02 01:48:46 +02:00
<?php
namespace Driver\SQL;
use \Api\Parameter\Parameter;
2020-06-18 15:08:09 +02:00
use Driver\SQL\Column\Column;
2020-04-02 01:48:46 +02:00
use \Driver\SQL\Column\IntColumn;
use \Driver\SQL\Column\SerialColumn;
use \Driver\SQL\Column\StringColumn;
use \Driver\SQL\Column\EnumColumn;
use \Driver\SQL\Column\DateTimeColumn;
use Driver\SQL\Column\BoolColumn;
use Driver\SQL\Column\JsonColumn;
2020-06-18 15:08:09 +02:00
use Driver\SQL\Expression\Add;
2020-04-03 17:39:58 +02:00
use Driver\SQL\Strategy\Strategy;
2020-06-18 15:08:09 +02:00
use Driver\SQL\Strategy\UpdateStrategy;
2020-04-02 01:48:46 +02:00
class PostgreSQL extends SQL {
public function __construct($connectionData) {
parent::__construct($connectionData);
}
public function checkRequirements() {
return function_exists('pg_connect');
}
public function getDriverName() {
return 'pgsql';
}
2020-04-03 18:09:01 +02:00
// Connection Management
2020-04-02 01:48:46 +02:00
public function connect() {
if(!is_null($this->connection)) {
return true;
}
$config = array(
"host" => $this->connectionData->getHost(),
"port" => $this->connectionData->getPort(),
"dbname" => $this->connectionData->getProperty('database', 'public'),
"user" => $this->connectionData->getLogin(),
"password" => $this->connectionData->getPassword()
);
$connectionString = array();
foreach($config as $key => $val) {
if (!empty($val)) {
$connectionString[] = "$key=$val";
}
}
$this->connection = @pg_connect(implode(" ", $connectionString));
if (!$this->connection) {
$this->lastError = "Failed to connect to Database";
$this->connection = NULL;
return false;
}
pg_set_client_encoding($this->connection, $this->connectionData->getProperty('encoding', 'UTF-8'));
return true;
}
public function disconnect() {
if(is_null($this->connection))
return;
pg_close($this->connection);
}
2020-04-02 21:19:06 +02:00
public function getLastError() {
$lastError = parent::getLastError();
if (empty($lastError)) {
$lastError = pg_last_error($this->connection) . " " . pg_last_error($this->connection);
}
return $lastError;
}
2020-04-02 01:48:46 +02:00
protected function execute($query, $values = NULL, $returnValues = false) {
$this->lastError = "";
$stmt_name = uniqid();
$pgParams = array();
if (!is_null($values)) {
foreach($values as $value) {
$paramType = Parameter::parseType($value);
switch($paramType) {
case Parameter::TYPE_DATE:
$value = $value->format("Y-m-d");
break;
case Parameter::TYPE_TIME:
$value = $value->format("H:i:s");
break;
case Parameter::TYPE_DATE_TIME:
$value = $value->format("Y-m-d H:i:s");
break;
default:
break;
}
$pgParams[] = $value;
}
}
$stmt = @pg_prepare($this->connection, $stmt_name, $query);
if ($stmt === FALSE) {
return false;
}
$result = @pg_execute($this->connection, $stmt_name, $pgParams);
if ($result === FALSE) {
return false;
}
if ($returnValues) {
$rows = pg_fetch_all($result);
if ($rows === FALSE) {
if (empty(trim($this->getLastError()))) {
$rows = array();
}
}
return $rows;
} else {
return true;
}
}
2020-04-03 17:39:58 +02:00
protected function getOnDuplicateStrategy(?Strategy $strategy, &$params) {
if (!is_null($strategy)) {
2020-06-18 15:08:09 +02:00
if ($strategy instanceof UpdateStrategy) {
2020-04-03 17:39:58 +02:00
$updateValues = array();
2020-06-18 15:08:09 +02:00
foreach($strategy->getValues() as $key => $value) {
$leftColumn = $this->columnName($key);
2020-04-03 17:39:58 +02:00
if ($value instanceof Column) {
2020-06-18 15:08:09 +02:00
$columnName = $this->columnName($value->getName());
$updateValues[] = "$leftColumn=$columnName";
} else if ($value instanceof Add) {
$columnName = $this->columnName($value->getColumn());
$operator = $value->getOperator();
$value = $value->getValue();
$updateValues[] = "$leftColumn=$columnName$operator" . $this->addValue($value, $params);
2020-04-03 17:39:58 +02:00
} else {
2020-06-18 15:08:09 +02:00
$updateValues[] = "$leftColumn=" . $this->addValue($value, $parameters);
2020-04-03 17:39:58 +02:00
}
}
2020-06-18 15:08:09 +02:00
$conflictingColumns = $this->columnName($strategy->getConflictingColumns());
$updateValues = implode(",", $updateValues);
return " ON CONFLICT ($conflictingColumns) DO UPDATE SET $updateValues";
} else {
$strategyClass = get_class($strategy);
$this->lastError = "ON DUPLICATE Strategy $strategyClass is not supported yet.";
return false;
}
2020-04-03 17:39:58 +02:00
} else {
return "";
2020-04-02 01:48:46 +02:00
}
2020-04-03 17:39:58 +02:00
}
2020-04-02 01:48:46 +02:00
2020-04-03 17:39:58 +02:00
protected function getReturning(?string $columns) {
return $columns ? (" RETURNING " . $this->columnName($columns)) : "";
}
2020-04-02 01:48:46 +02:00
2020-04-03 17:39:58 +02:00
protected function fetchReturning($res, string $returningCol) {
$this->lastInsertId = $res[0][$returningCol];
2020-04-02 01:48:46 +02:00
}
// UGLY but.. what should i do?
2020-04-03 17:39:58 +02:00
private function createEnum(EnumColumn $enumColumn) {
2020-04-02 01:48:46 +02:00
$typeName = $enumColumn->getName();
if(!endsWith($typeName, "_type")) {
$typeName = "${typeName}_type";
}
$values = array();
foreach($enumColumn->getValues() as $value) {
$values[] = $this->getValueDefinition($value);
}
$values = implode(",", $values);
$query =
"DO $$ BEGIN
CREATE TYPE \"$typeName\" AS ENUM ($values);
EXCEPTION
WHEN duplicate_object THEN null;
END $$;";
$this->execute($query);
return $typeName;
}
protected function getColumnDefinition($column) {
2020-04-02 16:16:58 +02:00
$columnName = $this->columnName($column->getName());
2020-04-02 01:48:46 +02:00
if ($column instanceof StringColumn) {
$maxSize = $column->getMaxSize();
if ($maxSize) {
$type = "VARCHAR($maxSize)";
} else {
$type = "TEXT";
}
} else if($column instanceof SerialColumn) {
$type = "SERIAL";
} else if($column instanceof IntColumn) {
$type = "INTEGER";
} else if($column instanceof DateTimeColumn) {
$type = "TIMESTAMP";
} else if($column instanceof EnumColumn) {
$type = $this->createEnum($column);
} else if($column instanceof BoolColumn) {
$type = "BOOLEAN";
} else if($column instanceof JsonColumn) {
$type = "JSON";
} else {
$this->lastError = "Unsupported Column Type: " . get_class($column);
return NULL;
}
$notNull = $column->notNull() ? " NOT NULL" : "";
$defaultValue = "";
if (!is_null($column->getDefaultValue()) || !$column->notNull()) {
$defaultValue = " DEFAULT " . $this->getValueDefinition($column->getDefaultValue());
}
2020-04-02 16:16:58 +02:00
return "$columnName $type$notNull$defaultValue";
2020-04-02 01:48:46 +02:00
}
protected function getValueDefinition($value) {
if (is_numeric($value)) {
return $value;
} else if(is_bool($value)) {
return $value ? "TRUE" : "FALSE";
} else if(is_null($value)) {
return "NULL";
} else if($value instanceof Keyword) {
return $value->getValue();
} else {
$str = str_replace("'", "''", $value);
return "'$str'";
}
}
protected function addValue($val, &$params) {
if ($val instanceof Keyword) {
return $val->getValue();
} else {
2020-04-02 21:19:06 +02:00
$params[] = is_bool($val) ? ($val ? "TRUE" : "FALSE") : $val;
2020-04-02 01:48:46 +02:00
return '$' . count($params);
}
}
2020-04-02 15:08:14 +02:00
protected function tableName($table) {
2020-04-02 16:16:58 +02:00
if (is_array($table)) {
$tables = array();
foreach($table as $t) $tables[] = $this->tableName($t);
return implode(",", $tables);
} else {
return "\"$table\"";
}
2020-04-02 15:08:14 +02:00
}
protected function columnName($col) {
if ($col instanceof KeyWord) {
return $col->getValue();
2020-04-02 16:16:58 +02:00
} elseif(is_array($col)) {
$columns = array();
foreach($col as $c) $columns[] = $this->columnName($c);
return implode(",", $columns);
2020-04-02 15:08:14 +02:00
} else {
2020-04-02 16:16:58 +02:00
if (($index = strrpos($col, ".")) !== FALSE) {
2020-04-02 15:08:14 +02:00
$tableName = $this->tableName(substr($col, 0, $index));
$columnName = $this->columnName(substr($col, $index + 1));
return "$tableName.$columnName";
2020-04-02 16:16:58 +02:00
} else if(($index = stripos($col, " as ")) !== FALSE) {
$columnName = $this->columnName(trim(substr($col, 0, $index)));
$alias = $this->columnName(trim(substr($col, $index + 4)));
return "$columnName as $alias";
} else {
return "\"$col\"";
2020-04-02 15:08:14 +02:00
}
}
}
2020-04-02 01:48:46 +02:00
// Special Keywords and functions
public function currentTimestamp() {
2020-04-02 15:08:14 +02:00
return new Keyword("CURRENT_TIMESTAMP");
}
2020-06-19 13:13:13 +02:00
public function getStatus() {
$version = pg_version($this->connection)["client"] ?? "??";
$status = pg_connection_status($this->connection);
static $statusTexts = array(
PGSQL_CONNECTION_OK => "PGSQL_CONNECTION_OK",
PGSQL_CONNECTION_BAD => "PGSQL_CONNECTION_BAD",
);
return ($statusTexts[$status] ?? "Unknown") . " (v$version)";
}
2020-04-03 17:39:58 +02:00
}