about summary refs log tree commit diff stats
path: root/classes
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2024-02-18 12:34:17 +0300
committerAndrew Dolgov <fox@fakecake.org>2024-02-18 12:34:56 +0300
commiteb52a7da8e2cafa1892d3f8810a69435b7a9b854 (patch)
tree286a54c4ea0d153fb31ce69edb0b8233e8046934 /classes
parent19b3e7fff80d430a9f287138f9cf84a1faa681fd (diff)
downloadthe-epube-eb52a7da8e2cafa1892d3f8810a69435b7a9b854.tar.gz
the-epube-eb52a7da8e2cafa1892d3f8810a69435b7a9b854.tar.xz
add pgsql support
Diffstat (limited to 'classes')
-rw-r--r--classes/Config.php11
-rw-r--r--classes/Db.php38
2 files changed, 43 insertions, 6 deletions
diff --git a/classes/Config.php b/classes/Config.php
index 3fc8358..5f7b710 100644
--- a/classes/Config.php
+++ b/classes/Config.php
@@ -9,6 +9,12 @@ class Config {
 	// override defaults, defined below in _DEFAULTS[], via environment: DB_TYPE becomes EPUBE_DB_TYPE, etc
 
 	const DB_TYPE = "DB_TYPE";
+	const DB_HOST = "DB_HOST";
+	const DB_USER = "DB_USER";
+	const DB_NAME = "DB_NAME";
+	const DB_PASS = "DB_PASS";
+	const DB_PORT = "DB_PORT";
+
 	const SCRATCH_DB = "SCRATCH_DB";
 	const CALIBRE_DB = "CALIBRE_DB";
 	const BOOKS_DIR = "BOOKS_DIR";
@@ -22,6 +28,11 @@ class Config {
 
 	private const _DEFAULTS = [
 		Config::DB_TYPE => [ "sqlite", Config::T_STRING ],
+		Config::DB_HOST => [ "db", Config::T_STRING ],
+		Config::DB_USER => [ "", Config::T_STRING ],
+		Config::DB_NAME => [ "", Config::T_STRING ],
+		Config::DB_PASS => [ "", Config::T_STRING ],
+		Config::DB_PORT => [ "5432", Config::T_STRING ],
 		Config::SCRATCH_DB => [ "db/scratch.db", Config::T_STRING ],
 		Config::CALIBRE_DB => [ "", Config::T_STRING ],
 		Config::BOOKS_DIR => [ "", Config::T_STRING ],
diff --git a/classes/Db.php b/classes/Db.php
index bf14468..dcaa482 100644
--- a/classes/Db.php
+++ b/classes/Db.php
@@ -1,24 +1,40 @@
 <?php
+
 class Db {
 	private static ?Db $instance = null;
 
 	private PDO $pdo;
 
 	private function __construct() {
+		$db_type = Config::get(Config::DB_TYPE);
+
 		try {
-			$this->pdo = new PDO(self::get_dsn());
+			$this->pdo = new PDO(self::get_dsn(),
+				Config::get(Config::DB_USER),
+				Config::get(Config::DB_PASS));
+
+			if ($db_type == 'sqlite') {
+				$this->pdo->query('PRAGMA journal_mode = wal');
+			}
 		} catch (Exception $e) {
 			user_error($e, E_USER_WARNING);
-			die("Unable to initialize database driver (SQLite).");
+			die("Unable to initialize database driver: " . Config::get(Config::DB_TYPE));
 		}
-		//$this->dbh->busyTimeout(30*1000);
+
 		$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
-		$this->pdo->query('PRAGMA journal_mode = wal');
 
 		try {
 			ORM::configure(self::get_dsn());
 			ORM::configure('return_result_sets', true);
-			ORM::raw_execute('PRAGMA journal_mode = wal');
+
+			if ($db_type == 'pgsql') {
+				ORM::configure('username', Config::get(Config::DB_USER));
+				ORM::configure('password', Config::get(Config::DB_PASS));
+				ORM::configure('return_result_sets', true);
+			} else {
+				ORM::raw_execute('PRAGMA journal_mode = wal');
+			}
+
 		} catch (Exception $e) {
 			user_error($e, E_USER_WARNING);
 			die("Unable to initialize ORM layer.");
@@ -26,7 +42,17 @@ class Db {
 	}
 
 	public static function get_dsn() : string {
-		return Config::get(Config::DB_TYPE) . ':' . Config::get(Config::SCRATCH_DB);
+		$db_type = Config::get(Config::DB_TYPE);
+
+		if ($db_type == 'pgsql') {
+			$db_port = Config::get(Config::DB_PORT) ? ';port=' . Config::get(Config::DB_PORT) : '';
+			$db_host = Config::get(Config::DB_HOST) ? ';host=' . Config::get(Config::DB_HOST) : '';
+
+			return $db_type . ':dbname=' . Config::get(Config::DB_NAME) . $db_host . $db_port;
+
+		} else {
+			return $db_type . ':' . Config::get(Config::SCRATCH_DB);
+		}
 	}
 
 	public static function pdo() : PDO {