aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Db.php
blob: c9f9df99e265ce62af5bb41fce66bb1d5a880a6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
namespace Digitigrade;

use Digitigrade\Model\Actor;

class Db extends Singleton {
    private const INIT_SCHEMA_PATH = __DIR__ . '/../schema.sql';

    private \PDO $conn;

    protected function __construct() {
        $conf = GlobalConfig::getInstance();
        $this->conn = new \PDO($conf->getDbConnection(), $conf->getDbUser(), $conf->getDbPassword(), [
            \PDO::ATTR_PERSISTENT => true,
        ]);
    }

    public function runInitialSchema() {
        $this->conn->exec(file_get_contents(self::INIT_SCHEMA_PATH));
    }

    public function getDbVersion(): int {
        $result = $this->conn->query('SELECT db_version FROM db_version');
        return $result->fetch()['db_version'];
    }

    /**
     * Changes the database schema version
     * 
     * CAREFUL! this shouldn't be used unless you absolutely know why
     * @param int $newVersion the new version number
     * @return void
     */
    public function setDbVersion(int $newVersion) {
        $stmt = $this->conn->prepare('UPDATE db_version SET db_version = ?');
        $stmt->execute([$newVersion]);
    }

    public function getPdo(): \PDO {
        return $this->conn;
    }
}