summaryrefslogtreecommitdiff
path: root/lib/config.php
blob: bb7e5cae23a0e89cf5fbe4d7a1497f94196bfc47 (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
<?php

class config {

    public static function get(string $key) {
        $db = getDb();
        $q = $db->query("SELECT value FROM config WHERE name=?", $key);
        if (!$db->numRows($q))
            return null;
        return $db->result($q);
    }

    public static function mget($keys) {
        $map = [];
        foreach ($keys as $key) {
            $map[$key] = null;
        }
        
        $db = getDb();
        $keys = array_map(fn($s) => $db->escape($s), $keys);

        $q = $db->query("SELECT * FROM config WHERE name IN('".implode("','", $keys)."')");
        while ($row = $db->fetch($q))
            $map[$row['name']] = $row['value'];

        return $map;
    }

    public static function set($key, $value) {
        $db = getDb();
        return $db->query("REPLACE INTO config (name, value) VALUES (?, ?)", $key, $value);
    }

    public static function mset($map) {
        $rows = [];
        foreach ($map as $name => $value) {
            $rows[] = [
                'name' => $name,
                'value' => $value
            ];
        }
        $db = getDb();
        return $db->multipleReplace('config', $rows);
    }

}