summaryrefslogtreecommitdiff
path: root/classes/Db.php
diff options
context:
space:
mode:
Diffstat (limited to 'classes/Db.php')
-rw-r--r--classes/Db.php38
1 files changed, 32 insertions, 6 deletions
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 {