diff options
Diffstat (limited to 'classes/Db.php')
-rw-r--r-- | classes/Db.php | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/classes/Db.php b/classes/Db.php new file mode 100644 index 0000000..bf14468 --- /dev/null +++ b/classes/Db.php @@ -0,0 +1,39 @@ +<?php +class Db { + private static ?Db $instance = null; + + private PDO $pdo; + + private function __construct() { + try { + $this->pdo = new PDO(self::get_dsn()); + } catch (Exception $e) { + user_error($e, E_USER_WARNING); + die("Unable to initialize database driver (SQLite)."); + } + //$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'); + } catch (Exception $e) { + user_error($e, E_USER_WARNING); + die("Unable to initialize ORM layer."); + } + } + + public static function get_dsn() : string { + return Config::get(Config::DB_TYPE) . ':' . Config::get(Config::SCRATCH_DB); + } + + public static function pdo() : PDO { + if (self::$instance == null) + self::$instance = new self(); + + return self::$instance->pdo; + } + +}; |