File: db.inc.php

package info (click to toggle)
zoph 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,632 kB
  • sloc: php: 28,044; javascript: 10,435; sql: 527; sh: 153; makefile: 4
file content (246 lines) | stat: -rw-r--r-- 7,215 bytes parent folder | download
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/**
 * Database connection class
 *
 * This file is part of Zoph.
 *
 * Zoph is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Zoph is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Zoph; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @package Zoph
 * @author Jeroen Roos
 */

namespace db;

use \PDO;
use \log;
use db\exception\connectionException as dbConnectionException;

/**
 * The db object is used to connect to the database
 * Example code:
 * $qry=new select("photos");
 * var_dump(db::query($qry));

 *
 * @package Zoph
 * @author Jeroen Roos
 */
class db {

    public const MYSQL = 100;
    public const MARIADB = 101;

    /** @var PDO holds connection */
    private static $connection=false;

    /** @var string database host */
    private static $dbhost;
    /** @var string database name */
    private static $dbname;
    /** @var string database user */
    private static $dbuser;
    /** @var string database password */
    private static $dbpass;
    /** @var string table prefix */
    private static $dbprefix;

    private function __construct() {
        // No intstantiating of this class, all methods are static
    }

    /**
     * Get handle to database
     * Make connection first, if it has not been made
     */
    public static function getHandle() {
        if (!static::$connection) {
            static::connect();
        }
        return static::$connection;
    }

    /**
     * Set login details
     * @param string database hostname
     * @param string database name
     * @param string database user
     * @param string database password
     * @param string database table prefix
     * @codeCoverageIgnore - runs in test setup
     */
    public static function setLoginDetails($dbhost, $dbname, $dbuser, $dbpass, $dbprefix) {
        static::$dbhost=$dbhost;
        static::$dbname=$dbname;
        static::$dbuser=$dbuser;
        static::$dbpass=$dbpass;
        static::$dbprefix=$dbprefix;
    }

    /**
     * Get database login details
     * @return array login details
     */
    public static function getLoginDetails() {
        return array(
            "host"      => static::$dbhost,
            "dbname"    => static::$dbname,
            "user"      => static::$dbuser,
            "pass"      => static::$dbpass,
            "prefix"    => static::$dbprefix
        );
    }

    public static function disconnect() {
        static::$connection=null;
    }

    public static function testConnection() {
        try {
            static::SQL("SELECT \"connect\"");
        } catch (exception $e) {
            log::msg($e->getMessage(), log::DEBUG, log::DB);
            throw new dbConnectionException("Could not connect to database");
        }
    }
    /**
     * Get table prefix
     */
    public static function getPrefix() {
        return static::$dbprefix;
    }

    /**
     * Connect to database
     * @param string PDO DSN
     * @codeCoverageIgnore - runs in test setup
     */
    private static function connect($dsn=null) {
        if (!$dsn) {
            $dsn=static::getDSN();
        }
        static::$connection=new PDO($dsn, static::$dbuser, static::$dbpass, array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
        ));
        static::$connection->setAttribute(
            PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        static::$connection->setAttribute(
            PDO::ATTR_EMULATE_PREPARES, false);
    }

    /**
     * Get the Data Source Name for the database connection
     * Currently hardcoded to MySQL, in the future this might change
     */
    private static function getDSN() {
        $db="mysql";

        return sprintf("%s:host=%s;dbname=%s", $db, static::$dbhost, static::$dbname);
    }

    /**
     * BEGIN a transaction
     */
    public static function beginTransaction() {
        $db=static::getHandle();
        $db->beginTransaction();
    }

    /**
     * COMMIT a transaction
     */
    public static function commit() {
        $db=static::getHandle();
        $db->commit();
    }

    /**
     * ROLLBACK transaction
     */
    public static function rollback() {
        $db=static::getHandle();
        $db->rollback();
    }

    /**
     * Run a query
     * @param query Query to run
     */
    public static function query(query $query) {
        $db=static::getHandle();
        try {
            log::msg("SQL Query: " . (string) $query, log::DEBUG, log::SQL);
            $stmt=$db->prepare($query);
            foreach ($query->getParams() as $param) {
                if ($param instanceof param) {
                    log::msg("Param: <b>" . $param->getName() . "</b>: " . $param->getValue(), log::DEBUG, log::SQL);
                    $stmt->bindValue($param->getName(), $param->getValue(), $param->getType());
                }
            }

            /**
             * Set LOG_SEVERITY to log::MOREDEBUG and LOG_SUBJECT to log::SQL in config.inc.php
             * to create a log of all SQL queries + execution times in /tmp
             */
            if ((LOG_SEVERITY == log::TOFILE) && (LOG_SUBJECT & log::SQL)) {
                $start=$query->logToFile("");
            }
            $stmt->execute();
            if (isset($start)) {
                $time=microtime(true)-$start;
                file_put_contents("/tmp/zophdebug", ": " . $time . "\n", FILE_APPEND);
            }

        } catch (\PDOException $e) {
            $trace  = $e->getTraceAsString();
            $trace .= "\n" . $query->prettyPrint() . "\n";
            $trace .= $e->getMessage() . "\n";
            throw new exception("SQL failed\n" . $trace);
        }

        return $stmt;
    }

    /**
     * Execute an SQL query
     * This is meant to execute queries that cannot be handled via the query builder
     * it should not be used for SELECT, UPDATE, DELETE or INSERT queries,
     * these can be handled via their respective objects
     * @param string SQL
     */
    public static function SQL($sql) {
        try {
            $db=static::getHandle();
            $stmt=$db->prepare($sql);
            $stmt->execute();
            return $stmt;
        } catch (\PDOException $e) {
            $trace  = $e->getTraceAsString();
            $trace .= "\n" . $sql . "\n";
            $trace .= $e->getMessage() . "\n";
            throw new exception("SQL failed\n" . $trace);
        }
    }

    public static function getServerType() {
        $db=static::getHandle();
        $version = ($db->getAttribute(PDO::ATTR_SERVER_VERSION));
        if (strpos($version, "MariaDB") === false) {
            return self::MYSQL;
        } else {
            return self::MARIADB;
        }
    }
}