summaryrefslogtreecommitdiff
path: root/classes/db.php
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2021-03-05 21:14:35 +0300
committerAndrew Dolgov <[email protected]>2021-03-05 21:14:35 +0300
commit2b8b845abe7c13ecbb266613910484310cffe8e1 (patch)
tree90bd2e93737c2aad17cfb09496cc57cf3f9968cd /classes/db.php
parentb2341679d53b227fc90fba34c3a7e6453e3cad6e (diff)
* use ORM for trivial queries
* environment-based configuration * useradm.php -> update.php with new options * support for schema migrations * various fixes
Diffstat (limited to 'classes/db.php')
-rw-r--r--classes/db.php33
1 files changed, 33 insertions, 0 deletions
diff --git a/classes/db.php b/classes/db.php
new file mode 100644
index 0000000..cc835b7
--- /dev/null
+++ b/classes/db.php
@@ -0,0 +1,33 @@
+<?php
+class Db {
+ private static $instance;
+ private $pdo;
+
+ private function __construct() {
+ try {
+ $this->pdo = new PDO(self::get_dsn());
+ } catch (Exception $e) {
+ die("Unable to initialize database driver (SQLite): $e");
+ }
+ //$this->dbh->busyTimeout(30*1000);
+ $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ $this->pdo->query('PRAGMA journal_mode = wal;');
+
+ ORM::configure(self::get_dsn());
+ ORM::configure('return_result_sets', true);
+ }
+
+ public static function get_dsn() {
+ 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;
+ }
+
+};
+
+?>