aboutsummaryrefslogtreecommitdiff
path: root/ratatoeskr/sys/db.php
blob: 17a8b8a770ee91d29731da937863e058b7a8e950 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/*
 * File: ratatoeskr/sys/db.php
 *
 * Helper functions for dealing with MySQL.
 *
 * License:
 * This file is part of Ratatöskr.
 * Ratatöskr is licensed unter the MIT / X11 License.
 * See "ratatoeskr/licenses/ratatoeskr" for more information.
 */

if (!defined("SETUP")) {
    require_once(dirname(__FILE__) . "/../config.php");
}

require_once(dirname(__FILE__) . "/utils.php");

$db_con = null;

/*
 * Function: db_connect
 *
 * Establish a connection to the MySQL database.
 */
function db_connect()
{
    global $config;
    global $db_con;

    $db_con = new PDO(
        "mysql:host=" . $config["mysql"]["server"] . ";dbname=" . $config["mysql"]["db"] . ";charset=utf8",
        $config["mysql"]["user"],
        $config["mysql"]["passwd"],
        [
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
        ]
    );
    $db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

/*
 * Function: sub_prefix
 *
 * Substitutes "PREFIX_" in the input string with the prefix from the config.
 */
function sub_prefix($q)
{
    global $config;
    return str_replace("PREFIX_", $config["mysql"]["prefix"], $q);
}

/*
 * Function: prep_stmt
 *
 * Prepares a SQL statement using the global DB connection.
 * This will also replace "PREFIX_" with the prefix defined in 'config.php'.
 *
 * Parameters:
 *  $q - The query / statement to prepare.
 *
 * Returns:
 *  A PDOStatement object.
 */
function prep_stmt($q)
{
    global $db_con;

    return $db_con->prepare(sub_prefix($q));
}

/*
 * Function: qdb
 *
 * Prepares statement (1st argument) with <prep_stmt> and executes it with the remaining arguments.
 *
 * Returns:
 *  A PDOStatement object.
 */
function qdb()
{
    $args = func_get_args();
    if (count($args) < 1) {
        throw new InvalidArgumentException("qdb needs at least 1 argument");
    }

    $stmt = prep_stmt($args[0]);
    $stmt->execute(array_slice($args, 1));
    return $stmt;
}

/*
 * Class: Transaction
 *
 * Makes using transactions easier.
 */
class Transaction
{
    public $startedhere;

    /*
     * Constructor: __construct
     *
     * Start a new transaction.
     */
    public function __construct()
    {
        global $db_con;
        $this->startedhere = !($db_con->inTransaction());
        if ($this->startedhere) {
            $db_con->beginTransaction();
        }
    }

    /*
     * Function: commit
     *
     * Commit the transaction.
     */
    public function commit()
    {
        global $db_con;

        if ($this->startedhere) {
            $db_con->commit();
        }
    }

    /*
     * Function: rollback
     *
     * Toll the transaction back.
     */
    public function rollback()
    {
        global $db_con;

        if ($this->startedhere) {
            $db_con->rollBack();
        }
    }
}