aboutsummaryrefslogtreecommitdiff
path: root/engine/database/SQLiteConnection.php
blob: 6b03c7c25ac323679cc19564e0961e5812565d61 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php

class SQLiteConnection extends CommonDatabase {

    const SCHEMA_VERSION = 0;

    protected SQLite3 $link;

    public function __construct(string $db_path) {
        $will_create = !file_exists($db_path);
        $this->link = new SQLite3($db_path);
        if ($will_create)
            setperm($db_path);
        $this->link->enableExceptions(true);
        $this->upgradeSchema();
    }

    protected function upgradeSchema() {
        $cur = $this->getSchemaVersion();
        if ($cur == self::SCHEMA_VERSION)
            return;

        if ($cur < 1) {
            // TODO
        }

        $this->syncSchemaVersion();
    }

    protected function getSchemaVersion() {
        return $this->link->query("PRAGMA user_version")->fetchArray()[0];
    }

    protected function syncSchemaVersion() {
        $this->link->exec("PRAGMA user_version=".self::SCHEMA_VERSION);
    }

    public function query(string $sql, ...$params): SQLite3Result {
        return $this->link->query($this->prepareQuery($sql, ...$params));
    }

    public function exec(string $sql, ...$params) {
        return $this->link->exec($this->prepareQuery($sql, ...$params));
    }

    public function querySingle(string $sql, ...$params) {
        return $this->link->querySingle($this->prepareQuery($sql, ...$params));
    }

    public function querySingleRow(string $sql, ...$params) {
        return $this->link->querySingle($this->prepareQuery($sql, ...$params), true);
    }

    public function insertId(): int {
        return $this->link->lastInsertRowID();
    }

    public function escape(string $s): string {
        return $this->link->escapeString($s);
    }

    public function fetch($q): ?array {
        // TODO: Implement fetch() method.
    }

    public function fetchAll($q): ?array {
        // TODO: Implement fetchAll() method.
    }

    public function fetchRow($q): ?array {
        // TODO: Implement fetchRow() method.
    }

    public function result($q, int $field = 0) {
        return $q?->fetchArray()[$field];
    }

    public function numRows($q): ?int {
        // TODO: Implement numRows() method.
    }
}