-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDb.class.php
More file actions
666 lines (538 loc) · 15.4 KB
/
Db.class.php
File metadata and controls
666 lines (538 loc) · 15.4 KB
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
<?PHP
/*
#LICENSE BEGIN
**********************************************************************
* OgerArch - Archaeological Database is released under the GNU General Public License (GPL) <http://www.gnu.org/licenses>
* Copyright (C) Gerhard Öttl <gerhard.oettl@ogersoft.at>
**********************************************************************
#LICENSE END
*/
/**
* Handle db related things
*/
class Db {
private static $conn;
private static $dbName;
private static $dbUser;
private static $dbPass;
private static $dbAttrib;
const ACTION_INSERT = 'INSERT';
const ACTION_UPDATE = 'UPDATE';
/**
* Init db class.
*/
public static function init($dbName, $dbUser, $dbPass, $dbAttrib) {
self::$dbName = $dbName;
self::$dbUser = $dbUser;
self::$dbPass = $dbPass;
self::$dbAttrib = $dbAttrib;
self::$conn = null;
} // eo init
/**
* Get connection
*/
public static function getConn() {
if (self::$conn === null) {
self::$conn = new PDO(self::$dbName, self::$dbUser, self::$dbPass);
if (self::$dbAttrib['errorMode']) {
self::$conn->setAttribute(PDO::ATTR_ERRMODE, self::$dbAttrib['errorMode']);
}
// TODO maybe the following settings are mysql specific?
if (self::$dbAttrib['connectionCharset']) {
self::$conn->exec('SET CHARACTER SET ' . self::$dbAttrib['connectionCharset']);
}
if (self::$dbAttrib['connectionTimeZone']) {
self::$conn->exec('SET time_zone = ' . self::$dbAttrib['connectionTimeZone']);
}
}
return self::$conn;
} // eo get connection
/**
* Prepare statement with optional where part and more
*/
public static function prepare($stmt, $where = null, $moreStmt = null) {
if (is_string($where)) {
$stmt .= " WHERE $where";
}
elseif (is_array($where)) {
$stmt .= self::createWhereStmt($where, null, true);
}
if ($moreStmt) {
$stmt .= " $moreStmt";
}
self::getConn();
return self::$conn->prepare($stmt);
} // eo prepare
/**
* Check parameters for statement.
* Only used for debugging, because the error messages of the pdo-drivers are very sparingly.
* @adjust: If params should be adjusted. Defaults to false.
* - true: params are adjusted silently and no error message is thrown
* - false: An error message is thrown if the params does not fit.
*/
public static function checkStmtParams($stmt, &$params = array(), $returnMsg = false, $adjust = false) {
// check for required params
$requiredParams = self::getStmtPlaceHolder($stmt);
foreach ($requiredParams as $key) {
// remove leading ':'
$key = substr($key, 1);
// check with and without ':'
if (!array_key_exists($key, $params) && !array_key_exists(':' . $key, $params)) {
if ($adjust) {
$params[$key] = ''; // fill with empty string
}
else {
$msg .= "Required key $key not found in params.\n";
}
}
} // eo foreach required param
// check for too much params (work on copy)
$tmp = $params;
foreach ($tmp as $key => $value) {
// param must have ':' prefix for this check to match placeholder in statement
if (substr($key, 0, 1) != ':') {
$key = ':' . $key;
}
if (!array_key_exists($key, $requiredParams)) {
if ($adjust) {
unset($params[$key]); // remove array entry
}
else {
$msg .= "No statement placeholder found for param key $key.\n";
}
}
} // eo foreach given param
// check for duplicates (key exists with and without ':')
// I hope php drops one of this silently! If not we can add this later.
// if errormessage than return or throw exception
if ($msg) {
$msg = "Sql prepare statement check failure: $msg in $stmt with given parameters: " . str_replace("\n", '' ,var_export($params, true));
if ($returnMsg) {
return $msg;
}
else {
throw new Exception($msg);
}
}
} // eo check statement params
/**
* get parameters of sql statement
* currently named parameters only
*/
// TODO handle positional parameters too ???
private static function getStmtPlaceholder($stmt) {
preg_match_all('/(:\w+)/', $stmt, $matches);
return $matches[1];
}
/**
* process query
**/
public static function query($stmt) {
self::getConn();
return self::$conn->query($stmt);
}
/**
* execute statement (non-query)
**/
public static function exec($stmt) {
self::getConn();
return self::$conn->exec($stmt);
}
/**
* get named sql parameters (by default from post data)
*/
public static function fillStmtParms($stmt, $values = null) {
// fill values array from post variables by default
if ($values === null) {
$values = $_POST;
}
// if values are from an object then convert to array
if (is_object($values)) {
$values = get_object_vars($values);
}
$names = static::getStmtPlaceholder($stmt);
$result = array();
// fill parameters where corresponding key exists in values array
foreach($values as $key => $value) {
$key = ':' . $key;
if (array_search($key, $names) !== false) {
$result[$key] = $value;
}
}
// fill missing parameters with empty strings
foreach($names as $key) {
if (!array_key_exists($key, $result)) {
$result[$key] = '';
}
}
return $result;
} // end of fill stmt parameters
/**
* remove entries from search values when no corresponding sql parameters exists
*/
public static function cleanStmtParms($stmt, $values = null) {
// if values are from an object then convert to array
if (is_object($values)) {
$values = get_object_vars($values);
}
$names = static::getStmtPlaceholder($stmt);
// check keys
foreach($values as $key => $value) {
$key = ':' . $key;
if (array_search($key, $names) === false) {
unset($values[$key]);
}
}
return $values;
} // end of cleaning statement parameters
/**
* create where clause for prepared statement
* ---
* Memo on prepared statements (php 5.3):
* Looks like repared statements are always filled in as strings, even
* if they are forced to numbers (for example multiplied with 1).
* So whe have to explicitly cast them in the were statement if
* we need numbers.
*/
public static function createWhereStmt($fields, $andOr = 'AND', $withWhere = false) {
$stmt = '';
// allow "parameter skiping"
if (!$andOr) {
$andOr = 'AND';
}
// try to detect associative arrays and use array_keys instead
if (OgerFunc::isAssoc($fields)) {
$fields = array_keys($fields);
}
// create where clause
// fieldname can contain cast-operator, comparation-operator and andOr operator
foreach ($fields as $fieldName) {
list($fieldName, $compOp, $valueName, $andOrOp) = explode(",", $fieldName);
list($fieldName, $cast) = explode("#", $fieldName);
$fieldName = trim($fieldName);
if (!$fieldName) {
continue;
}
if (!static::checkFieldName($fieldName, false)) {
continue;
}
$valueName = trim($valueName);
if (!$valueName) {
$valueName = $fieldName;
}
if (substr($valueName, 0, 1) != ":") {
$valueName = ":" . $valueName;
}
$cast = trim($cast);
if ($cast) {
$valueName = "CAST($valueName AS $cast)";
}
$compOp = trim($compOp);
switch ($compOp) {
case "=":
case "!=":
case "<":
case "<=":
case ">":
case ">=":
break;
default:
$compOp = "=";
}
$andOrOp = trim($andOrOp);
if ($andOrOp) {
$andOr = $andOrOp; // fieldspecific values overwrite general settings
}
$stmt .= ($stmt ? " $andOr " : '') . "`$fieldName` $compOp $valueName";
}
// return nothing if no statement created
if (!$stmt) {
return '';
}
return ($withWhere ? ' WHERE ' : '') . $stmt;
} // end of create where for prepared statement
/**
* Cleanup where vals from additional parameters like comparison operation etc
* Fieldnames are NOT checked. Should be save here.
*/
public static function getCleanWhereVals($values) {
if ($values === null) {
$values = array();
}
$whereVals = array();
// fieldname can contain cast-operator, comparation-operator and andOr operator
foreach ($values as $key => $value) {
list($fieldName, $compOp, $valueName, $$andOrOp) = explode(",", $key);
list($fieldName, $cast) = explode("#", $fieldName);
$fieldName = trim($fieldName);
if (!$fieldName) {
continue;
}
$valueName = trim($valueName);
if (!$valueName) {
$valueName = $fieldName;
}
$whereVals[$valueName] = $value;
}
return $whereVals;
} // end of clean where vals
/**
* create order by statement
*/
public static function createOrderByStmt($fields, $withOrderBy = false) {
$stmt = '';
foreach ($fields as $key => $direction) {
$direction = ($direction ? strtoupper($direction) : 'ASC');
$stmt .= ($stmt ? ',' : '') . "$key $direction";
}
return ($withOrderBy ? ' ORDER BY ' : '') . $stmt;
} // eo create order by
/**
* Reverse order by statement
* orderBy: array with key=fieldname, value=direction pairs
* or string with order by statement
*/
public static function reverseOrderByStmt($orderBy) {
if (is_string($orderBy)) {
$orderStmt = trim($orderBy);
if (strtoupper(substr($orderStmt, 0, 8)) == "ORDER BY") {
$orderStmt = trim(substr($orderStmt, 8));
$withOrderBy = true;
}
$orderStmt = explode(',', $orderStmt);
$orderBy = array();
foreach ($orderStmt as $fieldStmt) {
$fieldStmt = trim($fieldStmt);
list($field, $direction) = preg_split('/\s+/', $fieldStmt, 2);
$orderBy[$field] = $direction;
}
}
foreach ($orderBy as $field => $direction) {
$direction = ($direction ? strtoupper($direction) : 'ASC');
// reverse
$direction = ($direction == 'ASC' ? 'DESC' : 'ASC');
$stmt .= ($stmt ? ',' : '') . "$field $direction";
}
return ($withOrderBy ? ' ORDER BY ' : '') . $stmt;
} // eo reverse order
/**
* create statement string for insert or update
*/
public static function createStmt($action, $table, $fields, $where = null) {
$stmt = '';
if (is_array($where)) {
$where = self::createWhereStmt($where);
}
switch ($action) {
case self::ACTION_INSERT:
foreach ($fields as $field) {
if (!$field) {
continue;
}
$stmtField .= ($stmtField ? "," : '') . "`$field`";
$stmtValue .= ($stmtValue ? "," : '') . ":$field";
}
$stmt .= "INSERT INTO `$table` ($stmtField) VALUES ($stmtValue)";
break;
case self::ACTION_UPDATE:
foreach ($fields as $field) {
if (!$field) {
continue;
}
$stmtSet .= ($stmtSet ? "," : '') . "`$field`=:$field";
}
$stmt .= "UPDATE `$table` SET $stmtSet";
break;
default:
throw new Exception('Unknown Db::action: ' . $action . '.');
}
if ($where) $stmt .= ' WHERE ' . $where;
return $stmt;
} // end of create statement
/**
* Create statement for select
* Opts parameter consists of keywords array pairs where the array contains the data for the given keyword.
* If a string is given istead of an array than this string is used directly.
*/
/* from mysql 5 docu
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[INTO OUTFILE 'file_name' export_options
| INTO DUMPFILE 'file_name'
| INTO var_name [, var_name]]
[FOR UPDATE | LOCK IN SHARE MODE]]
*/
public static function createSelectStmt($opts) {
$stmt = '';
if (!array_key_exists('checkFieldNames', $opts)) {
$opts['checkFieldNames'] = true;
}
// SELECT statment
if ($opts['select'] === false) {
// suppress 'SELECT' statement
}
else {
$stmt .= ($opts['select'] ?: 'SELECT');
}
// ALL, DISTINCT, PRIORITY, etc
if ($opts['extra']) {
if (is_string($opts['extra'])) {
$stmt .= ' ' . $opts['extra'];
}
else {
$stmt .= ' ' . implode(' ', $opts['extra']);
}
}
// result fields (if associative arrays use keys insted of values)
if ($opts['fields']) {
$fieldNames = $opts['fields'];
if (is_string($fieldNames)) {
$fieldNames = explode(',' , $opts['fields']);
}
if (OgerFunc::isAssoc($fieldNames)) {
$fieldNames = array_keys($fieldNames);
}
if ($opts['checkFieldNames']) {
foreach($fieldNames as $fieldName) {
static::checkFieldName($fieldName, false);
}
}
$stmt .= ' ' . implode(' ', $fieldNames);
}
// FROM table reference(s)
if ($opts['table']) {
$stmt .= ' FROM ' . $opts['table'];
}
// WHERE
if ($opts['where']) {
if (is_string($opts['where'])) {
$str = trim($opts['where']);
if (strtoupper(substr($str, 0, 5)) != "WHERE") {
$str = " WHERE $str";
}
$stmt .= $str;
}
else {
$stmt .= self::createWhereStmt($opts['where'], null, true);
}
}
// GROUP BY
if ($opts['groupBy']) {
if (is_string($opts['groupBy'])) {
$str = trim($opts['groupBy']);
if (strtoupper(substr($str, 0, 8)) != "GROUP BY") {
$str = " GROUP BY $str";
}
$stmt .= $str;
}
else {
if (OgerFunc::isAssoc($opts['groupBy'])) {
$opts['groupBy'] = array_keys($opts['groupBy']);
}
$stmt .= ' ' . implode(' ', $opts['groupBy']);
}
}
// HAVING
if ($opts['having']) {
if (is_string($opts['having'])) {
$str = trim($opts['having']);
if (strtoupper(substr($str, 0, 6)) != "HAVING") {
$str = " HAVING $str";
}
$stmt .= $str;
}
else {
$stmt .= ' HAVING ' . self::createWhereStmt($opts['having'], null, false);
}
}
// ORDER BY
if ($opts['orderBy']) {
if (is_string($opts['orderBy'])) {
$str = trim($opts['orderBy']);
if (strtoupper(substr($str, 0, 8)) != "ORDER BY") {
$str = " ORDER BY $str";
}
$stmt .= $str;
}
else {
$stmt .= self::createOrderByStmt($opts['orderBy'], true);
}
}
// LIMIT
if ($opts['limit']) {
if (is_string($opts['limit'])) {
$str = trim($opts['limit']);
if (strtoupper(substr($str, 0, 5)) != "LIMIT") {
$str = " LIMIT $str";
}
$stmt .= $str;
}
else {
if (count($opts['limit']) == 2) {
if (OgerFunc::isAssoc($opts['limit'])) {
$limit = $opts['limit']['start'] . ',' . $opts['limit']['limit'];
}
else {
$limit = implode(',', $opts['limit']);
}
}
else {
if (is_array($opts['limit'])) {
$limit = '' . reset($opts['limit']);
}
else {
$limit = '' . $opts['limit'];
}
}
$stmt .= " LIMIT $limit";
}
}
// PROCEDURE
// lock options
//if ($opts['lock']) {
// return full statement
return $stmt;
} // eo create select statement
/**
* Check one fieldname. Throw excaption if fieldname invalid.
*/
public static function checkFieldName($fieldName, $silent) {
if (!preg_match('/\w+|\*/', trim($fieldName))) {
if ($silent) {
return false;
}
else {
$ex = new Exception("Db::checkFieldName: Invalid fieldname $fieldName.");
$ex->invalidFieldName = $fieldName;
throw $ex;
}
}
return true;
} // eo check fieldname with exception
/**
* Check one or more fieldnames. Throw excaption if fieldname invalid.
*/
public static function checkFieldNamesEx($fieldNames) {
if (is_string($fieldNames)) {
$fieldNames = array($fieldNames);
}
foreach ($fieldNames as $fieldName) {
static::checkFieldName($fieldName, false);
}
} // eo check fieldname with exception
} // end of class
?>