/
class-DB.php
607 lines (516 loc) · 14.9 KB
/
class-DB.php
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
<?php
# Copyright (C) 2015, 2018, 2019, 2020, 2021 Valerio Bozzolan
#
# This program 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 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
/*
* Use: esc_html(), DEBUG, force_array()
*/
/**
* A simple but effective database class!
*
* This class was forked from my defunct project called Open Student.
*/
class DB {
/**
* MySQL connection link
*/
private $mysqli;
/**
* Table prefix
*
* @var string
*/
private $prefix;
/**
* Number of executed queries
*
* Read by get_num_queries()
*
* @var int
*/
public $queries = 0;
/**
* Last query result
*
* @var query-result
*/
private $last = false;
/**
* Transaction nest level
*
* @var int
*/
private $transactions = 0;
/**
* Check if a rollback was fired
*
* @var bool
*/
private $rollback = false;
/**
* Singleton instance
*
* @var self
*/
private static $_instance;
/**
* Get the singleton instance of this class
*
* @return self
*/
public static function instance() {
if( ! self::$_instance ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Check if ::instance() was called
*
* @return boolean
*/
public static function instanced() {
return isset( self::$_instance );
}
/**
* Constructor
*
* @param string $username Database username
* @param string $password Database password
* @param string $location Database location
* @param string $database Database name
* @param string $prefix Table Prefix
* @param string $charset Connection charset
*/
function __construct( $username = null, $password = null, $location = null, $database = null, $prefix = '', $charset = null ) {
// default credentials usually defined from your load.php
if( ! func_num_args() ) {
$username = $GLOBALS['username'];
$password = $GLOBALS['password'];
$location = $GLOBALS['location'];
$database = $GLOBALS['database'];
$prefix = $GLOBALS['prefix'];
}
$this->prefix = $prefix;
// create database connection
@$this->mysqli = new mysqli( $location, $username, $password, $database );
if( $this->mysqli->connect_errno ) {
$length = strlen( $password );
throw new SucklessException( "unable to connect to the database '$database' using user '$username' and password ($length characters) on MySQL/MariaDB host '$location'" );
}
// eventually inherit default charset
if( !$charset && isset( $GLOBALS['charset'] ) ) {
$charset = $GLOBALS['charset'];
}
// eventually set connection charset
if( $charset ) {
$this->mysqli->set_charset( $charset );
}
}
/**
* Execute an SQL query
*
* @param string $query The SQL query to execute
*/
public function query( $query ) {
$this->queries++;
if( $this->last !== false && $this->last !== true ) {
$this->last->free();
}
$this->last = $this->mysqli->query( $query );
if( DEBUG_QUERIES ) {
error( "query n. {$this->queries}: $query" );
}
if( !$this->last ) {
throw new SucklessException( $this->getQueryErrorMessage( $query ) );
}
return $this->last;
}
/**
* Select only a row from the database
*/
public function getRow( $query, $class_name = null, $params = [] ) {
$results = $this->getResults( $query, $class_name, $params );
return isset( $results[ 0 ] ) ? $results[ 0 ] : null;
}
/**
* Select only a column from a single row
*/
public function getValue( $query, $column_name, $class_name = null, $params = [] ) {
$row = $this->getRow( $query, $class_name, $params );
return isset( $row->{ $column_name } ) ? $row->{ $column_name } : null;
}
/**
* Execute a query and return an array of $class_name objects.
*
* @param string $query Database SQL query.
* @param string $class_name The name of the class to instantiate.
* @param array $params Optional data for the $class_name constructor.
* @see http://php.net/manual/en/mysqli-result.fetch-object.php
*/
public function getResults( $query, $class_name = null, $params = [] ) {
if( !$class_name ) {
$class_name = 'Queried';
}
$this->query( $query );
$res = [];
while( $row = $this->last->fetch_object( $class_name, $params ) ) {
$res[] = $row;
}
return $res;
}
/**
* Execute a query and return a generator of $class_name objects.
*
* @param string $query Database SQL query.
* @param string $class_name The name of the class to instantiate.
* @param array $params Optional data for the $class_name constructor.
* @See http://php.net/manual/en/mysqli-result.fetch-object.php
*/
public function getGenerator( $query, $class_name = null, $params = [] ) {
if( !$class_name ) {
$class_name = 'Queried';
}
$result = $this->query( $query );
$this->last = true; // to don't be killed from another query() call
while( $row = $result->fetch_object( $class_name, $params ) ) {
yield $row;
}
$result->free();
}
/**
* Run an INSERT query for a single row
*
* @param string $table
* @param DBCol[] $cols
* @param array $args arguments like 'replace-into
*/
public function insertRow( $table, $cols, $args = [] ) {
$cols = DBCol::normalizeArray( $cols );
// build column names
$columns = [];
foreach( $cols as $col ) {
$columns[] = "`{$col->column}`";
}
// sanitize values
$values = [];
foreach( $cols as $col ) {
$values[] = $this->forceType( $col->value, $col->forceType );
}
// default arguments
$args = array_replace( [
'replace-into' => false,
], $args );
$what = $args[ 'replace-into' ] ? 'REPLACE' : 'INSERT';
$table = $this->getTable( $table, false );
$columns_comma = implode( ', ', $columns );
$values_comma = implode( ', ', $values );
return $this->query("$what INTO $table ($columns_comma) VALUES ($values_comma)");
}
/**
* Executes one or multiple queries which are concatenated by a semicolon
*
* @param $queries string
*/
public function multiQuery( $queries ) {
$i = 1;
if( !$this->mysqli->multi_query( $queries ) ) {
throw new SucklessException( "error in MySQLi#multi_query() with statement n. $i (starting from 1): {$this->mysqli->error}" );
}
while( $this->mysqli->more_results() ) {
$i++;
if( !$this->mysqli->next_result() ) {
throw new SucklessException( "error in MySQLi#multi_query() with statement n. $i (starting from 1): {$this->mysqli->error}" );
}
}
}
/**
* Run an INSERT query for multiple rows
*
* @param string $table Table Name without prefix
* @param array $columns Assoc array of types ('ID' => 'null', 'name' => 's', ..)
* @param array $rows Array of rows (or just a row)
* @param array $args Extra arguments
*/
public function insert( $table, $columns, $rows, $args = [] ) {
// default arguments
$args = array_replace( [
'replace-into' => false,
], $args );
// allow columns to be specified as an associative array
$columns = DBCol::normalizeArray( $columns );
// backticked column names
$column_names = [];
foreach( $columns as $column => $type ) {
$column_names[] = "`$column`";
}
$columns_comma = implode( ', ', $column_names );
// backward compatibility
force_array( $rows );
if( ! @is_array( $rows[0] ) ) {
$rows = [$rows];
}
// just the types (in order to be indexed numerically)
$types = array_values( $columns );
$n_columns = count( $types );
$value_groups = [];
foreach( $rows as $i => $row ) {
$query_values = [];
if( $n_columns !== count( $row ) ) {
throw new SucklessException( sprintf(
"error using insert() in table %s: %d columns but %d values in row %d",
$table,
$n_columns,
count( $row ),
$i
) );
}
$values_escaped = [];
foreach( $types as $j => $type ) {
$values_escaped[] = $this->forceType( $row[ $j ], $type );
}
$values_grouped = implode( ', ', $values_escaped );
$value_groups[] = "($values_grouped)";
}
$value_groups_comma = implode( ', ', $value_groups );
$action = $args['replace-into'] ? 'REPLACE' : 'INSERT';
$table = $this->getTable( $table, false );
return $this->query( "$action INTO $table ($columns_comma) VALUES $value_groups_comma" );
}
/**
* Run an UPDATE query
*
* @param string $table Table name without prefix and backticks
* @param array $columns Array of DBCol(s)
* @param string $conditions Part after WHERE
* @param string $after
*/
public function update( $table, $columns, $conditions, $after = '' ) {
$table = $this->getTable( $table, true );
return $this->query( $this->buildUpdateQuery( $table, $columns, $conditions, $after ) );
}
/**
* Eventually run a START TRANSACTION
*
* It's safe to be used in nested transactions.
*
* Remember to call self#commit() or self#rollback() at the end.
*
* @return self
*/
public function startTransaction() {
// do nothing in any nested transaction
if( $this->transactions++ === 0 ) {
$this->query( 'START TRANSACTION' );
}
return $this;
}
/**
* Eventually run a COMMIT
*
* It's safe to be used in nested transactions.
*
* @return self
*/
public function commit() {
// do nothing in any nested transaction
if( $this->transactions-- === 1 ) {
// check if the commit was aborted
if( $this->rollback ) {
$this->query( 'ROLLBACK' );
$this->rollback = false;
} else {
$this->query( 'COMMIT' );
}
}
return $this;
}
/**
* Eventually run a ROLLBACK
*
* It's safe to be used in nested transactions.
*
* @return self
*/
public function rollback() {
$this->rollback = true;
return $this->commit();
}
/**
* Build an UPDATE query
*
* @param string $table_raw Table name with prefix and backticks
* @param array $columns Array of DBCol[], or an associative array of column and its value
* @param string $conditions part after WHERE
* @param string $after
* @return string
*/
public function buildUpdateQuery( $table_raw, $columns, $conditions, $after = '' ) {
// for backward compatibility allow a single column
force_array( $columns );
// allow columns to be specified as an associative array
$columns = DBCol::normalizeArray( $columns );
// build the value assignation for each column
$sets = [];
foreach( $columns as $column ) {
$name = $column->column;
$value = $this->forceType( $column->value, $column->forceType );
$sets[] = "`$name` = $value";
}
$sets_comma = implode( ', ', $sets );
return "UPDATE $table_raw SET $sets_comma WHERE $conditions $after";
}
/**
* Get the last inserted ID
*
* @return string
*/
public function getLastInsertedID() {
return $this->mysqli->insert_id;
}
/**
* Get the last number of rows retrieved
*
* @return int
*/
public function getLastNumRows() {
return $this->last->num_rows;
}
/**
* Escape a string
*
* @param string $s String to be escaped
* @return string String escaped.
*/
public function escapeString( $s ) {
return $this->mysqli->real_escape_string( $s );
}
/**
* Get the table prefix
*
* @return string
*/
public function getPrefix() {
return $this->prefix;
}
/**
* Get table name with it's prefix (if any)
*
* @param string $name Table name
* @param mixed $as True to create an alias without the table prefix, or specify your alias
* @return string Table eventually with the alias
*/
public function getTable( $name, $as = true ) {
$table = "`{$this->prefix}$name`";
if( $as ) {
if( $as === true ) {
$as = $name;
}
$table .= " AS `$as`";
}
return $table;
}
/**
* Get the list of every table name inserted as arguments or as an array
*
* @return string
*/
public function getTables( $args = [] ) {
$tables = [];
if( ! is_array( $args ) ) {
$args = func_get_args();
}
foreach( $args as $arg ) {
$tables[] = $this->getTable( $arg, true );
}
return implode( ' JOIN ', $tables );
}
/**
* Force a string to a defined type
*
* @param string $s String to be forced.
* @param string $type Type ('d' for integer, 's' for string, 'f' for float, 'null' for autoincrement values or for "don't care" values).
* @see http://news.php.net/php.bugs/195815
* @return string Forced string
*/
public function forceType( $s, $type ) {
if( $type === 'd' )
return (int) $s; // Integer
if( $type === 'f' )
return (float) $s; // Float
if( $type === 'dnull' )
return ($s === null) ? 'NULL' : (int) $s; // Integer - or NULL
if( $type === 'fnull' )
return ($s === null) ? 'NULL' : (float) $s; // Float - or NULL
if( $type === 's' )
return "'{$this->escapeString($s)}'"; // String escaped
if( $type === 'snull' )
return ($s === null) ? 'NULL' : "'{$this->escapeString($s)}'"; // String escaped - or NULL
if( $type === 'f' )
return (float) $s; // Float value
if( $type === '-' )
return $s; // Float value
if( $type === null || $type === 'null' )
return 'NULL'; // 'NULL' literally for indexes
error( "type $type unexpected in DB::forceType() and so it will be seen as 's'" );
return $this->forceType( $s, 's' );
}
/**
* USE another database
*
* @param string $database
*/
public function selectDB( $database ) {
$ok = $this->mysqli->select_db( $database );
if( !$ok ) {
throw new SucklessException( $this->getQueryErrorMessage( "USE $database" ) );
}
}
/**
* Get the number of affected rows
*
* @return int
*/
public function affectedRows() {
return $this->mysqli->affected_rows;
}
/**
* Please close the door when you leave a room
*/
public function closeConnection() {
if( $this->mysqli ) {
@$this->mysqli->close();
}
}
/**
* Automatically close the door when you leave the room
*/
public function __destruct() {
$this->closeConnection();
}
/**
* Show a friendly error about last MySQL query
*
* @param string $query SQL query executed during the error
* @return string
*/
private function getQueryErrorMessage( $query ) {
return sprintf(
"error executing the query n. %d |%s| error: %s",
$this->queries,
$query,
$this->mysqli->error
);
}
}