From 796315c00c249422d427f0267bd514e03be1f840 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sat, 10 Nov 2018 00:02:38 -0500 Subject: [PATCH] Basic stub of PDO-base PostgreSQL driver --- lib/Conf.php | 12 ++++ lib/Db/PostgreSQL/Driver.php | 124 ++++++++++++++++++++++++++++++++ lib/Db/PostgreSQL/PDODriver.php | 47 ++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 lib/Db/PostgreSQL/Driver.php create mode 100644 lib/Db/PostgreSQL/PDODriver.php diff --git a/lib/Conf.php b/lib/Conf.php index 3d4eeb23..9173d3cb 100644 --- a/lib/Conf.php +++ b/lib/Conf.php @@ -25,6 +25,18 @@ class Conf { public $dbSQLite3Key = ""; /** @var integer Number of seconds for SQLite to wait before returning a timeout error when writing to the database */ public $dbSQLite3Timeout = 60; + /** @var string Host name, address, or socket path of PostgreSQL database server (if using PostgreSQL) */ + public $dbPostgreSQLHost = ""; + /** @var string Log-in user name for PostgreSQL database server (if using PostgreSQL) */ + public $dbPostgreSQLUser = "arsse"; + /** @var string Log-in password for PostgreSQL database server (if using PostgreSQL) */ + public $dbPostgreSQLPass = ""; + /** @var integer Listening port for PostgreSQL database server (if using PostgreSQL over TCP) */ + public $dbPostgreSQLPort = 5432; + /** @var string Database name on PostgreSQL database server (if using PostgreSQL) */ + public $dbPostgreSQLDb = "arsse"; + /** @var string Schema name on PostgreSQL database server (if using PostgreSQL) */ + public $dbPostgreSQLSchema = ""; /** @var string Class of the user management driver in use (Internal by default) */ public $userDriver = User\Internal\Driver::class; diff --git a/lib/Db/PostgreSQL/Driver.php b/lib/Db/PostgreSQL/Driver.php new file mode 100644 index 00000000..2ba5baa0 --- /dev/null +++ b/lib/Db/PostgreSQL/Driver.php @@ -0,0 +1,124 @@ +dbPostgreSQLUser; + $pass = $pass ?? Arsse::$conf->dbPostgreSQLPass; + $db = $db ?? Arsse::$conf->dbPostgreSQLDb; + $host = $host ?? Arsse::$conf->dbPostgreSQLHost; + $port = $port ?? Arsse::$conf->dbPostgreSQLPort; + $schema = $schema ?? Arsse::$conf->dbPostgreSQLSchema; + $this->makeConnection($user, $pass, $db, $host, $port); + } + + public static function requirementsMet(): bool { + // stub: native interface is not yet supported + return false; + } + + protected function makeConnection(string $user, string $pass, string $db, string $host, int $port) { + // stub: native interface is not yet supported + throw new \Exception; + } + + protected function makeConnectionString(bool $pdo, string $user, string $pass, string $db, string $host, int $port): string { + $out = ['dbname' => $db]; + if ($host != "") { + $out['host'] = $host; + $out['port'] = (string) $port; + } + if (!$pdo) { + $out['user'] = $user; + $out['password'] = $pass; + } + $out = array_map(function($v, $k) { + return "$k='".str_replace("'", "\\'", str_replace("\\", "\\\\", $v))."'"; + }, $out, array_keys($out)); + return implode(($pdo ? ";" : " "), $out); + } + + public function __destruct() { + } + + /** @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 driverName(): string { + return Arsse::$lang->msg("Driver.Db.PostgreSQL.Name"); + } + + public static function schemaID(): string { + return "PostgreSQL"; + } + + public function schemaVersion(): int { + // stub + return 0; + } + + public function schemaUpdate(int $to, string $basePath = null): bool { + // stub + return false; + } + + public function charsetAcceptable(): bool { + // stub + return true; + } + + protected function getError(): string { + // stub + return ""; + } + + public function exec(string $query): bool { + // stub + return true; + } + + public function query(string $query): \JKingWeb\Arsse\Db\Result { + // stub + return new ResultEmpty; + } + + public function prepareArray(string $query, array $paramTypes): \JKingWeb\Arsse\Db\Statement { + // stub + return new Statement($this->db, $s, $paramTypes); + } + + protected function lock(): bool { + // stub + return true; + } + + protected function unlock(bool $rollback = false): bool { + // stub + return true; + } +} diff --git a/lib/Db/PostgreSQL/PDODriver.php b/lib/Db/PostgreSQL/PDODriver.php new file mode 100644 index 00000000..fd43780e --- /dev/null +++ b/lib/Db/PostgreSQL/PDODriver.php @@ -0,0 +1,47 @@ +makeconnectionString(true, $user, $pass, $db, $host, $port); + $this->db = new \PDO("pgsql:$dsn", $user, $pass); + } + + 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"); + } +}