summaryrefslogtreecommitdiff
path: root/engine/database/MySQLConnection.php
blob: 9b473cbdd95409a38bda3bed1decfd3fa6dc0950 (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
<?php

class MySQLConnection extends CommonDatabase {

    protected ?mysqli $link = null;

    public function __construct(
        protected string $host,
        protected string $user,
        protected string $password,
        protected string $database) {}

    public function __destruct() {
        if ($this->link)
            $this->link->close();
    }

    public function connect(): bool {
        $this->link = new mysqli();
        return !!$this->link->real_connect($this->host, $this->user, $this->password, $this->database);
    }

    public function query(string $sql, ...$args): mysqli_result|bool {
        $sql = $this->prepareQuery($sql, ...$args);
        $q = $this->link->query($sql);
        if (!$q)
            logError(__METHOD__.': '.$this->link->error."\n$sql\n".backtrace(1));
        return $q;
    }

    public function fetch($q): ?array {
        $row = $q->fetch_assoc();
        if (!$row) {
            $q->free();
            return null;
        }
        return $row;
    }

    public function fetchAll($q): ?array {
        if (!$q)
            return null;
        $list = [];
        while ($f = $q->fetch_assoc()) {
            $list[] = $f;
        }
        $q->free();
        return $list;
    }

    public function fetchRow($q): ?array {
        return $q?->fetch_row();
    }

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

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

    public function numRows($q): ?int {
        return $q?->num_rows;
    }

    // public function affectedRows() {
    //     return $this->link->affected_rows;
    // }
    //
    // public function foundRows() {
    //     return $this->fetch($this->query("SELECT FOUND_ROWS() AS `count`"))['count'];
    // }

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

}