2018-11-10 05:02:38 +00:00
|
|
|
<?php
|
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2021-04-14 15:17:01 +00:00
|
|
|
|
2018-11-10 05:02:38 +00:00
|
|
|
namespace JKingWeb\Arsse\Db\PostgreSQL;
|
|
|
|
|
|
|
|
use JKingWeb\Arsse\Arsse;
|
|
|
|
use JKingWeb\Arsse\Db\Exception;
|
|
|
|
|
|
|
|
class PDODriver extends Driver {
|
|
|
|
use \JKingWeb\Arsse\Db\PDODriver;
|
|
|
|
|
|
|
|
protected $db;
|
|
|
|
|
|
|
|
public static function requirementsMet(): bool {
|
|
|
|
return class_exists("PDO") && in_array("pgsql", \PDO::getAvailableDrivers());
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:34:03 +00:00
|
|
|
protected function makeConnection(string $user, string $pass, string $db, string $host, int $port, string $service): void {
|
2018-11-17 02:20:54 +00:00
|
|
|
$dsn = $this->makeconnectionString(true, $user, $pass, $db, $host, $port, $service);
|
2018-12-12 16:15:07 +00:00
|
|
|
try {
|
|
|
|
$this->db = new \PDO("pgsql:$dsn", $user, $pass, [
|
2020-03-01 20:16:50 +00:00
|
|
|
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
|
2018-12-12 16:15:07 +00:00
|
|
|
\PDO::ATTR_PERSISTENT => true,
|
|
|
|
]);
|
|
|
|
} catch (\PDOException $e) {
|
|
|
|
if ($e->getCode() == 7) {
|
|
|
|
switch (substr($e->getMessage(), 9, 5)) {
|
|
|
|
case "08006":
|
2019-01-15 15:51:55 +00:00
|
|
|
throw new Exception("connectionFailure", ['engine' => "PostgreSQL", 'message' => substr($e->getMessage(), 28)]);
|
2018-12-12 16:15:07 +00:00
|
|
|
default:
|
|
|
|
throw $e; // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw $e; // @codeCoverageIgnore
|
|
|
|
}
|
2018-11-10 05:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct() {
|
|
|
|
unset($this->db);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @codeCoverageIgnore */
|
|
|
|
public static function create(): \JKingWeb\Arsse\Db\Driver {
|
|
|
|
if (self::requirementsMet()) {
|
|
|
|
return new self;
|
|
|
|
} elseif (Driver::requirementsMet()) {
|
|
|
|
return new Driver;
|
|
|
|
} else {
|
|
|
|
throw new Exception("extMissing", self::driverName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function driverName(): string {
|
|
|
|
return Arsse::$lang->msg("Driver.Db.PostgreSQLPDO.Name");
|
|
|
|
}
|
2018-11-29 18:45:37 +00:00
|
|
|
|
|
|
|
public function prepareArray(string $query, array $paramTypes): \JKingWeb\Arsse\Db\Statement {
|
|
|
|
return new PDOStatement($this->db, $query, $paramTypes);
|
|
|
|
}
|
2018-11-10 05:02:38 +00:00
|
|
|
}
|