aboutsummaryrefslogtreecommitdiff
path: root/ratatoeskr/sys/db.php
blob: 92722569ee14e42b0d7098a7e78ec15fe3f200d3 (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
<?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.
 */

use r7r\cms\sys\Database;
use r7r\cms\sys\Env;
use r7r\cms\sys\DbTransaction;

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

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

/**
 * Substitutes "PREFIX_" in the input string with the prefix from the config.
 *
 * @param mixed|string $q
 * @return string
 * @deprecated Use {@see Database::subPrefix()} instead.
 */
function sub_prefix($q): string
{
    return Env::getGlobal()->database()->subPrefix((string)$q);
}

/**
 * Prepares statement (1st argument) like {@see Database::prepStmt()} and executes it with the remaining arguments.
 *
 * @param mixed ...$args
 * @return PDOStatement
 *
 * @deprecated Use {@see Database::query()} instead.
 */
function qdb(...$args): PDOStatement
{
    if (count($args) < 1) {
        throw new InvalidArgumentException("qdb needs at least 1 argument");
    }

    return Env::getGlobal()->database()->query((string)$args[0], ...array_slice($args, 1));
}

/**
 * Makes using transactions easier.
 *
 * @deprecated Use {@see DbTransaction} instead.
 */
class Transaction
{
    /** @var DbTransaction */
    private $tx;

    /**
     * Start a new transaction.
     */
    public function __construct()
    {
        $this->tx = new DbTransaction(Env::getGlobal()->database());
    }

    /**
     * Commit the transaction.
     */
    public function commit(): void
    {
        $this->tx->commit();
    }

    /**
     * Roll the transaction back.
     */
    public function rollback(): void
    {
        $this->tx->rollback();
    }
}