summaryrefslogtreecommitdiffstats
path: root/lib/class/dba.class.php
blob: 8cbc6f5582ed6ac9ff59b38bb1c4f92d3a860020 (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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<?php
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
/**
 *
 * LICENSE: GNU General Public License, version 2 (GPLv2)
 * Copyright 2001 - 2013 Ampache.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License v2
 * as published by the Free Software Foundation.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

/* Make sure they aren't directly accessing it */
if (!defined('INIT_LOADED') || INIT_LOADED != '1') { exit; }

/**
 * Dba Class
 *
 * This is the database abstraction class
 * It duplicates the functionality of mysql_???
 * with a few exceptions, the row and assoc will always
 * return an array, simplifying checking on the far end
 * it will also auto-connect as needed, and has a default
 * database simplifying queries in most cases.
 *
 */
class Dba {

    public static $stats = array('query'=>0);

    private static $_sql;
    private static $config;

    /**
     * constructor
     * This does nothing with the DBA class
     */
    private function __construct() {

        // Rien a faire

    } // construct

    /**
     * query
     */
    public static function query($sql, $params) {
        // json_encode throws errors about UTF-8 cleanliness, which we don't
        // care about here.
        debug_event('Query', $sql . ' ' . @json_encode($params), 6);

        // Be aggressive, be strong, be dumb
        $tries = 0;
        do {
            $stmt = self::_query($sql, $params);
        } while (!$stmt && $tries++ < 3);

        return $stmt;
    }

    private static function _query($sql, $params) {
        $dbh = self::dbh();
        if (!$dbh) {
            debug_event('Dba', 'Error: failed to get database handle', 1);
            return false;
        }

        // Run the query
        if ($params) {
            $stmt = $dbh->prepare($sql);
            $stmt->execute($params);
        }
        else {
            $stmt = $dbh->query($sql);
        }

        // Save the query, to make debug easier
        self::$_sql = $sql;
        self::$stats['query']++;

        if (!$stmt) {
            debug_event('Dba', 'Error: ' . json_encode($dbh->errorInfo()), 1);
            self::disconnect();
        }
        else if ($stmt->errorCode() && $stmt->errorCode() != '00000') {
            debug_event('Dba', 'Error: ' . json_encode($stmt->errorInfo()), 1);
            self::disconnect();
            return false;
        }

        return $stmt;
    }

    /**
     * read
     */
    public static function read($sql, $params = null) {
        return self::query($sql, $params);
    }

    /**
     * write
     */
    public static function write($sql, $params = null) {
        return self::query($sql, $params);
    }

    /**
     * escape
     *
     * This runs a escape on a variable so that it can be safely inserted
     * into the sql
     */
    public static function escape($var) {
        $var = self::dbh()->quote($var);
        // This is slightly less ugly than it was, but still ugly
        return substr($var, 1, -1);
    }

    /**
     * fetch_assoc
     *
     * This emulates the mysql_fetch_assoc.
     * We force it to always return an array, albeit an empty one
     * The optional finish parameter affects whether we automatically clean
     * up the result set after the last row is read.
     */
    public static function fetch_assoc($resource, $finish = true) {
        if (!$resource) {
            return array();
        }

        $result = $resource->fetch(PDO::FETCH_ASSOC);

        if (!$result) {
            if ($finish) {
                self::finish($resource);
            }
            return array();
        }

        return $result;
    }

    /**
     * fetch_row
     *
     * This emulates the mysql_fetch_row
     * we force it to always return an array, albeit an empty one
     * The optional finish parameter affects whether we automatically clean
     * up the result set after the last row is read.
     */
    public static function fetch_row($resource, $finish = true) {
        if (!$resource) {
            return array();
        }

        $result = $resource->fetch(PDO::FETCH_NUM);;

        if (!$result) {
            if ($finish) {
                self::finish($resource);
            }
            return array();
        }

        return $result;
    }

    /**
     * num_rows
     *
     * This emulates the mysql_num_rows function which is really
     * just a count of rows returned by our select statement, this
     * doesn't work for updates or inserts.
     */
    public static function num_rows($resource) {
        if ($resource) {
            $result = $resource->rowCount();
            if ($result) {
                return $result;
            }
        }

        return 0;
    }

    /**
     * finish
     *
     * This closes a result handle and clears the memory associated with it
     */
    public static function finish($resource) {
        if ($resource) {
            $resource->closeCursor();
        }
    }

    /**
     * affected_rows
     *
     * This emulates the mysql_affected_rows function
     */
    public static function affected_rows($resource) {
        if ($resource) {
            $result = $resource->rowCount();
            if ($result) {
                return $result;
            }
        }

        return 0;
    }

    /**
     * _connect
     *
     * This connects to the database, used by the DBH function
     */
    private static function _connect() {
        $username = Config::get('database_username');
        $hostname = Config::get('database_hostname');
        $password = Config::get('database_password');

        try {
            $dbh = new PDO('mysql:host=' . $hostname, $username, $password);
        }
        catch (PDOException $e) {
            debug_event('Dba', 'Connection failed: ' . $e->getMessage(), 1);
            return null;
        }

        return $dbh;
    }

    private static function _setup_dbh($dbh, $database) {
        if (!$dbh) {
            return false;
        }

        $charset = self::translate_to_mysqlcharset(Config::get('site_charset'));
        $charset = $charset['charset'];
        if ($dbh->exec('SET NAMES ' . $charset) === false) {
            debug_event('Dba', 'Unable to set connection charset to ' . $charset, 1);
        }

        if ($dbh->exec('USE `' . $database . '`') === false) {
            debug_event('Dba', 'Unable to select database ' . $database . ': ' . json_encode($dbh->errorInfo()), 1);
        }

        if (Config::get('sql_profiling')) {
            $dbh->exec('SET profiling=1');
            $dbh->exec('SET profiling_history_size=50');
            $dbh->exec('SET query_cache_type=0');
        }
    }

    /**
     * check_database
     *
     * Make sure that we can connect to the database
     */
    public static function check_database() {
        $dbh = self::_connect();

        if (!$dbh || $dbh->errorCode()) {
            return false;
        }

        return true;
    }

    /**
     * check_database_inserted
     *
     * Checks to make sure that you have inserted the database
     * and that the user you are using has access to it.
     */
    public static function check_database_inserted() {
        $sql = "DESCRIBE session";
        $db_results = Dba::read($sql);

        if (!$db_results) {
            return false;
        }

        // Make sure the whole table is there
        if (Dba::num_rows($db_results) != '7') {
            return false;
        }

        return true;
    }

    /**
     * show_profile
     *
     * This function is used for debug, helps with profiling
     */
    public static function show_profile() {
        if (Config::get('sql_profiling')) {
            print '<br/>Profiling data: <br/>';
            $res = Dba::read('SHOW PROFILES');
            print '<table>';
            while ($r = Dba::fetch_row($res)) {
                print '<tr><td>' . implode('</td><td>', $r) . '</td></tr>';
            }
            print '</table>';
        }
    }

    /**
     * dbh
     *
     * This is called by the class to return the database handle
     * for the specified database, if none is found it connects
     */
    public static function dbh($database='') {
        if (!$database) {
            $database = Config::get('database_name');
        }

        // Assign the Handle name that we are going to store
        $handle = 'dbh_' . $database;

        if (!is_object(Config::get($handle))) {
            $dbh = self::_connect();
            self::_setup_dbh($dbh, $database);
            Config::set($handle, $dbh, true);
            return $dbh;
        }
        else {
            return Config::get($handle);
        }
    }

    /**
     * disconnect
     *
     * This nukes the dbh connection, this isn't used very often...
     */
    public static function disconnect($database = '') {
        if (!$database) {
            $database = Config::get('database_name'); 
        }

        $handle = 'dbh_' . $database;

        // Nuke it
        Config::set($handle, false, true);

        return true;
    }

    /**
     * insert_id
     */
    public static function insert_id() {
        $dbh = self::dbh();
        if ($dbh) {
            return $dbh->lastInsertId();
        }
        return null;
    }

    /**
     * error
     * this returns the error of the db
     */
    public static function error() {
        $dbh = self::dbh();
        if ($dbh) {
            return $dbh->errorCode();
        }
        return false;
    }

    /**
     * translate_to_mysqlcharset
     *
     * This translates the specified charset to a mysql charset.
     */
    public static function translate_to_mysqlcharset($charset) {
        // Translate real charset names into fancy MySQL land names
        switch (strtoupper($charset)) {
            case 'CP1250':
            case 'WINDOWS-1250':
                $target_charset = 'cp1250';
                $target_collation = 'cp1250_general_ci';
                break;
            case 'ISO-8859':
            case 'ISO-8859-2':
                $target_charset = 'latin2';
                $target_collation = 'latin2_general_ci';
                break;
            case 'ISO-8859-1':
            case 'CP1252':
            case 'WINDOWS-1252':
                $target_charset = 'latin1';
                $target_collation = 'latin1_general_ci';
                break;
            case 'EUC-KR':
                $target_charset = 'euckr';
                $target_collation = 'euckr_korean_ci';
                break;
            case 'CP932':
                $target_charset = 'sjis';
                $target_collation = 'sjis_japanese_ci';
                break;
            case 'KOI8-U':
                $target_charset = 'koi8u';
                $target_collation = 'koi8u_general_ci';
                break;
            case 'KOI8-R':
                $target_charset = 'koi8r';
                $target_collation = 'koi8r_general_ci';
                break;
            default;
            case 'UTF-8':
                $target_charset = 'utf8';
                $target_collation = 'utf8_unicode_ci';
                break;
        }

        return array(
            'charset' => $target_charset,
            'collation' => $target_collation
        );
    }

    /**
     * reset_db_charset
     *
     * This cruises through the database and trys to set the charset to the
     * current site charset. This is an admin function that can be run by an
     * administrator only. This can mess up data if you switch between charsets
     * that are not overlapping.
     */
    public static function reset_db_charset() {
        $translated_charset = self::translate_to_mysqlcharset(Config::get('site_charset'));
        $target_charset = $translated_charset['charset'];
        $target_collation = $translated_charset['collation'];

        // Alter the charset for the entire database
        $sql = "ALTER DATABASE `" . Config::get('database_name') . "` DEFAULT CHARACTER SET $target_charset COLLATE $target_collation";
        $db_results = Dba::write($sql);

        $sql = "SHOW TABLES";
        $db_results = Dba::read($sql);

        // Go through the tables!
        while ($row = Dba::fetch_row($db_results)) {
            $sql = "DESCRIBE `" . $row['0'] . "`";
            $describe_results = Dba::read($sql);

            // Change the tables default charset and colliation
            $sql = "ALTER TABLE `" . $row['0'] . "`  DEFAULT CHARACTER SET $target_charset COLLATE $target_collation";
            $alter_table = Dba::write($sql);

            // Iterate through the columns of the table
            while ($table = Dba::fetch_assoc($describe_results)) {
                if (
                (strpos($table['Type'], 'varchar') !== false) ||
                (strpos($table['Type'], 'enum') !== false) ||
                (strpos($table['Table'],'text') !== false)) {
                    $sql = "ALTER TABLE `" . $row['0'] . "` MODIFY `" . $table['Field'] . "` " . $table['Type'] . " CHARACTER SET " . $target_charset;
                    $charset_results = Dba::write($sql);
                    if (!$charset_results) {
                        debug_event('CHARSET','Unable to update the charset of ' . $table['Field'] . '.' . $table['Type'] . ' to ' . $target_charset,'3');
                    } // if it fails
                }
            }
        } 
    }

    /**
     * optimize_tables
     *
     * This runs an optimize on the tables and updates the stats to improve
     * join speed.
     * This can be slow, but is a good idea to do from time to time. We do 
     * it in case the dba isn't doing it... which we're going to assume they
     * aren't.
     */
    public static function optimize_tables() {
        $sql = "SHOW TABLES";
        $db_results = Dba::read($sql);

        while($row = Dba::fetch_row($db_results)) {
            $sql = "OPTIMIZE TABLE `" . $row[0] . "`";
            $db_results_inner = Dba::write($sql);

            $sql = "ANALYZE TABLE `" . $row[0] . "`";
            $db_results_inner = Dba::write($sql);
        }
    }
}
?>