2018-11-10 00:02:38 -05:00
|
|
|
<?php
|
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace JKingWeb\Arsse\Db\PostgreSQL;
|
|
|
|
|
|
|
|
use JKingWeb\Arsse\Arsse;
|
|
|
|
use JKingWeb\Arsse\Db\Exception;
|
|
|
|
|
|
|
|
class Driver extends \JKingWeb\Arsse\Db\AbstractDriver {
|
2018-12-13 19:47:51 -05:00
|
|
|
use Dispatch;
|
|
|
|
|
2020-03-01 18:32:01 -05:00
|
|
|
protected const TRANSACTIONAL_LOCKS = true;
|
2018-12-20 18:06:28 -05:00
|
|
|
|
2018-12-13 19:47:51 -05:00
|
|
|
protected $db;
|
2018-11-27 17:16:00 -05:00
|
|
|
protected $transStart = 0;
|
|
|
|
|
2019-01-10 19:01:32 -05:00
|
|
|
public function __construct() {
|
2018-11-10 00:02:38 -05:00
|
|
|
// check to make sure required extension is loaded
|
|
|
|
if (!static::requirementsMet()) {
|
2018-11-21 11:06:12 -05:00
|
|
|
throw new Exception("extMissing", static::driverName()); // @codeCoverageIgnore
|
2018-11-10 00:02:38 -05:00
|
|
|
}
|
2019-01-20 22:40:49 -05:00
|
|
|
$user = Arsse::$conf->dbPostgreSQLUser;
|
|
|
|
$pass = Arsse::$conf->dbPostgreSQLPass;
|
|
|
|
$db = Arsse::$conf->dbPostgreSQLDb;
|
|
|
|
$host = Arsse::$conf->dbPostgreSQLHost;
|
|
|
|
$port = Arsse::$conf->dbPostgreSQLPort;
|
|
|
|
$schema = Arsse::$conf->dbPostgreSQLSchema;
|
|
|
|
$service = Arsse::$conf->dbPostgreSQLService;
|
2018-11-16 21:20:54 -05:00
|
|
|
$this->makeConnection($user, $pass, $db, $host, $port, $service);
|
2018-11-21 11:06:12 -05:00
|
|
|
foreach (static::makeSetupQueries($schema) as $q) {
|
|
|
|
$this->exec($q);
|
|
|
|
}
|
2018-11-10 00:02:38 -05:00
|
|
|
}
|
|
|
|
|
2018-11-16 21:20:54 -05:00
|
|
|
public static function makeConnectionString(bool $pdo, string $user, string $pass, string $db, string $host, int $port, string $service): string {
|
|
|
|
$base = [
|
2020-03-01 15:16:50 -05:00
|
|
|
'client_encoding' => "UTF8",
|
2018-11-16 21:20:54 -05:00
|
|
|
'application_name' => "arsse",
|
2020-03-01 15:16:50 -05:00
|
|
|
'connect_timeout' => (string) (int) ceil(Arsse::$conf->dbTimeoutConnect),
|
2018-11-16 21:20:54 -05:00
|
|
|
];
|
|
|
|
$out = [];
|
|
|
|
if ($service != "") {
|
|
|
|
$out['service'] = $service;
|
|
|
|
} else {
|
|
|
|
if ($host != "") {
|
|
|
|
$out['host'] = $host;
|
|
|
|
}
|
|
|
|
if ($port != 5432 && !($host != "" && $host[0] == "/")) {
|
|
|
|
$out['port'] = (string) $port;
|
|
|
|
}
|
|
|
|
if ($db != "") {
|
|
|
|
$out['dbname'] = $db;
|
|
|
|
}
|
|
|
|
if (!$pdo) {
|
|
|
|
$out['user'] = $user;
|
|
|
|
if ($pass != "") {
|
|
|
|
$out['password'] = $pass;
|
|
|
|
}
|
|
|
|
}
|
2018-11-10 00:02:38 -05:00
|
|
|
}
|
2018-11-16 21:20:54 -05:00
|
|
|
ksort($out);
|
|
|
|
ksort($base);
|
|
|
|
$out = array_merge($out, $base);
|
2018-11-10 00:02:38 -05:00
|
|
|
$out = array_map(function($v, $k) {
|
|
|
|
return "$k='".str_replace("'", "\\'", str_replace("\\", "\\\\", $v))."'";
|
|
|
|
}, $out, array_keys($out));
|
2018-11-16 21:20:54 -05:00
|
|
|
return implode(" ", $out);
|
2018-11-10 00:02:38 -05:00
|
|
|
}
|
|
|
|
|
2018-11-21 11:06:12 -05:00
|
|
|
public static function makeSetupQueries(string $schema = ""): array {
|
2019-01-23 16:31:54 -05:00
|
|
|
$timeExec = is_null(Arsse::$conf->dbTimeoutExec) ? 0 : ceil(max(Arsse::$conf->dbTimeoutExec * 1000, 1));
|
|
|
|
$timeLock = is_null(Arsse::$conf->dbTimeoutLock) ? 0 : ceil(max(Arsse::$conf->dbTimeoutLock * 1000, 1));
|
2018-11-21 11:06:12 -05:00
|
|
|
$out = [
|
|
|
|
"SET TIME ZONE UTC",
|
2018-11-22 13:30:13 -05:00
|
|
|
"SET DateStyle = 'ISO, MDY'",
|
2019-01-23 16:31:54 -05:00
|
|
|
"SET statement_timeout = '$timeExec'",
|
|
|
|
"SET lock_timeout = '$timeLock'",
|
2018-11-21 11:06:12 -05:00
|
|
|
];
|
|
|
|
if (strlen($schema) > 0) {
|
2018-11-27 17:39:39 -05:00
|
|
|
$schema = '"'.str_replace('"', '""', $schema).'"';
|
|
|
|
$out[] = "SET search_path = $schema, public";
|
2018-11-21 11:06:12 -05:00
|
|
|
}
|
|
|
|
return $out;
|
2018-11-10 00:02:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @codeCoverageIgnore */
|
|
|
|
public static function create(): \JKingWeb\Arsse\Db\Driver {
|
|
|
|
if (self::requirementsMet()) {
|
|
|
|
return new self;
|
|
|
|
} elseif (PDODriver::requirementsMet()) {
|
|
|
|
return new PDODriver;
|
|
|
|
} else {
|
|
|
|
throw new Exception("extMissing", self::driverName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function schemaID(): string {
|
|
|
|
return "PostgreSQL";
|
|
|
|
}
|
|
|
|
|
2018-11-21 11:06:12 -05:00
|
|
|
public function charsetAcceptable(): bool {
|
|
|
|
return $this->query("SELECT pg_encoding_to_char(encoding) from pg_database where datname = current_database()")->getValue() == "UTF8";
|
2018-11-10 00:02:38 -05:00
|
|
|
}
|
|
|
|
|
2018-11-28 10:46:23 -05:00
|
|
|
public function schemaVersion(): int {
|
|
|
|
if ($this->query("SELECT count(*) from information_schema.tables where table_name = 'arsse_meta' and table_schema = current_schema()")->getValue()) {
|
|
|
|
return (int) $this->query("SELECT value from arsse_meta where key = 'schema_version'")->getValue();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 20:41:21 -05:00
|
|
|
public function sqlToken(string $token): string {
|
2018-12-14 09:18:56 -05:00
|
|
|
switch (strtolower($token)) {
|
|
|
|
case "nocase":
|
|
|
|
return '"und-x-icu"';
|
2019-02-22 11:13:13 -05:00
|
|
|
case "like":
|
|
|
|
return "ilike";
|
2021-02-02 10:00:08 -05:00
|
|
|
case "asc":
|
|
|
|
return "nulls first";
|
|
|
|
case "desc":
|
|
|
|
return "desc nulls last";
|
2018-12-14 09:18:56 -05:00
|
|
|
default:
|
|
|
|
return $token;
|
|
|
|
}
|
2018-12-04 20:41:21 -05:00
|
|
|
}
|
|
|
|
|
2018-11-22 13:30:13 -05:00
|
|
|
public function savepointCreate(bool $lock = false): int {
|
2018-11-27 17:16:00 -05:00
|
|
|
if (!$this->transStart) {
|
2018-11-22 13:30:13 -05:00
|
|
|
$this->exec("BEGIN TRANSACTION");
|
2018-11-27 17:16:00 -05:00
|
|
|
$this->transStart = parent::savepointCreate($lock);
|
|
|
|
return $this->transStart;
|
|
|
|
} else {
|
|
|
|
return parent::savepointCreate($lock);
|
2018-11-22 13:30:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function savepointRelease(int $index = null): bool {
|
2018-11-27 17:16:00 -05:00
|
|
|
$index = $index ?? $this->transDepth;
|
|
|
|
$out = parent::savepointRelease($index);
|
|
|
|
if ($index == $this->transStart) {
|
|
|
|
$this->exec("COMMIT");
|
|
|
|
$this->transStart = 0;
|
2018-11-22 13:30:13 -05:00
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function savepointUndo(int $index = null): bool {
|
2018-11-27 17:16:00 -05:00
|
|
|
$index = $index ?? $this->transDepth;
|
2018-11-22 13:30:13 -05:00
|
|
|
$out = parent::savepointUndo($index);
|
2018-11-27 17:16:00 -05:00
|
|
|
if ($index == $this->transStart) {
|
|
|
|
$this->exec("ROLLBACK");
|
|
|
|
$this->transStart = 0;
|
2018-11-22 13:30:13 -05:00
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2018-11-21 11:06:12 -05:00
|
|
|
protected function lock(): bool {
|
2018-11-27 17:16:00 -05:00
|
|
|
if ($this->query("SELECT count(*) from information_schema.tables where table_schema = current_schema() and table_name = 'arsse_meta'")->getValue()) {
|
2018-11-21 11:06:12 -05:00
|
|
|
$this->exec("LOCK TABLE arsse_meta IN EXCLUSIVE MODE NOWAIT");
|
|
|
|
}
|
|
|
|
return true;
|
2018-11-10 00:02:38 -05:00
|
|
|
}
|
|
|
|
|
2018-11-21 11:06:12 -05:00
|
|
|
protected function unlock(bool $rollback = false): bool {
|
2018-11-27 17:16:00 -05:00
|
|
|
// do nothing; transaction is committed or rolled back later
|
2018-11-10 00:02:38 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-21 11:06:12 -05:00
|
|
|
public function __destruct() {
|
2018-12-13 19:47:51 -05:00
|
|
|
if (isset($this->db)) {
|
|
|
|
pg_close($this->db);
|
|
|
|
unset($this->db);
|
|
|
|
}
|
2018-11-21 11:06:12 -05:00
|
|
|
}
|
|
|
|
|
2018-11-27 17:39:39 -05:00
|
|
|
public static function driverName(): string {
|
|
|
|
return Arsse::$lang->msg("Driver.Db.PostgreSQL.Name");
|
|
|
|
}
|
|
|
|
|
2018-11-21 11:06:12 -05:00
|
|
|
public static function requirementsMet(): bool {
|
2018-12-13 19:47:51 -05:00
|
|
|
return \extension_loaded("pgsql");
|
2018-11-21 11:06:12 -05:00
|
|
|
}
|
|
|
|
|
2020-01-20 13:34:03 -05:00
|
|
|
protected function makeConnection(string $user, string $pass, string $db, string $host, int $port, string $service): void {
|
2018-12-13 19:47:51 -05:00
|
|
|
$dsn = $this->makeconnectionString(false, $user, $pass, $db, $host, $port, $service);
|
|
|
|
set_error_handler(function(int $code, string $msg) {
|
|
|
|
$msg = substr($msg, 62);
|
2019-12-02 17:14:03 -05:00
|
|
|
throw new Exception("connectionFailure", ['engine' => "PostgreSQL", 'message' => $msg]);
|
2018-12-13 19:47:51 -05:00
|
|
|
});
|
|
|
|
try {
|
|
|
|
$this->db = pg_connect($dsn, \PGSQL_CONNECT_FORCE_NEW);
|
|
|
|
} finally {
|
|
|
|
restore_error_handler();
|
|
|
|
}
|
2018-11-21 11:06:12 -05:00
|
|
|
}
|
|
|
|
|
2018-11-10 00:02:38 -05:00
|
|
|
public function exec(string $query): bool {
|
2018-12-13 19:47:51 -05:00
|
|
|
pg_send_query($this->db, $query);
|
|
|
|
while ($result = pg_get_result($this->db)) {
|
|
|
|
if (($code = pg_result_error_field($result, \PGSQL_DIAG_SQLSTATE)) && isset($code) && $code) {
|
2020-03-01 15:16:50 -05:00
|
|
|
[$excClass, $excMsg, $excData] = $this->buildStandardException($code, pg_result_error($result));
|
2018-12-13 19:47:51 -05:00
|
|
|
throw new $excClass($excMsg, $excData);
|
|
|
|
}
|
|
|
|
}
|
2018-11-10 00:02:38 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function query(string $query): \JKingWeb\Arsse\Db\Result {
|
2018-12-13 19:47:51 -05:00
|
|
|
$r = $this->dispatchQuery($query);
|
|
|
|
if (is_resource($r)) {
|
|
|
|
return new Result($this->db, $r);
|
|
|
|
} else {
|
2020-03-01 15:16:50 -05:00
|
|
|
[$excClass, $excMsg, $excData] = $r;
|
2018-12-13 19:47:51 -05:00
|
|
|
throw new $excClass($excMsg, $excData);
|
|
|
|
}
|
2018-11-10 00:02:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function prepareArray(string $query, array $paramTypes): \JKingWeb\Arsse\Db\Statement {
|
2018-12-13 19:47:51 -05:00
|
|
|
return new Statement($this->db, $query, $paramTypes);
|
2018-11-10 00:02:38 -05:00
|
|
|
}
|
2019-03-01 22:36:25 -05:00
|
|
|
|
|
|
|
public function literalString(string $str): string {
|
|
|
|
return pg_escape_literal($this->db, $str);
|
|
|
|
}
|
2019-07-26 09:37:51 -04:00
|
|
|
|
|
|
|
public function maintenance(): bool {
|
|
|
|
// analyze the database
|
|
|
|
$this->exec("ANALYZE");
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-10 00:02:38 -05:00
|
|
|
}
|