1
1
<?php
2
2
3
3
declare (strict_types=1 );
4
- // lib/php/db.php 20150225 - 20180512
5
- // Copyright (C) 2015-2018 Mark Constable <markc@renta.net> (AGPL-3.0)
4
+ // lib/php/db.php 20150225 - 20250128
5
+ // Copyright (C) 2015-2025 Mark Constable <markc@renta.net> (AGPL-3.0)
6
6
7
7
class Db extends \PDO
8
8
{
@@ -13,11 +13,12 @@ public function __construct(array $dbcfg)
13
13
{
14
14
elog (__METHOD__ );
15
15
16
+ dbg ($ dbcfg );
16
17
if (is_null (self ::$ dbh )) {
17
18
extract ($ dbcfg );
18
19
$ dsn = 'mysql ' === $ type
19
- ? 'mysql: ' . ($ sock ? 'unix_socket= ' . $ sock : 'host= ' . $ host. ';port= ' . $ port ). ';dbname= ' . $ name
20
- : 'sqlite: ' . $ path ;
20
+ ? 'mysql: ' . ($ sock ? 'unix_socket= ' . $ sock : 'host= ' . $ host . ';port= ' . $ port ) . ';dbname= ' . $ name
21
+ : 'sqlite: ' . $ path ;
21
22
$ pass = file_exists ($ pass ) ? trim (file_get_contents ($ pass )) : $ pass ;
22
23
23
24
try {
@@ -27,7 +28,7 @@ public function __construct(array $dbcfg)
27
28
\PDO ::ATTR_DEFAULT_FETCH_MODE => \PDO ::FETCH_ASSOC ,
28
29
]);
29
30
} catch (\PDOException $ e ) {
30
- exit (__FILE__ . ' ' . __LINE__ . "<br> \n" . $ e ->getMessage ());
31
+ exit (__FILE__ . ' ' . __LINE__ . "<br> \n" . $ e ->getMessage ());
31
32
}
32
33
}
33
34
}
@@ -47,7 +48,7 @@ public static function create(array $ary)
47
48
$ values = rtrim ($ values , ', ' );
48
49
49
50
$ sql = '
50
- INSERT INTO ` '. self ::$ tbl. "` ( {$ fields })
51
+ INSERT INTO ` ' . self ::$ tbl . "` ( {$ fields })
51
52
VALUES ( {$ values }) " ;
52
53
53
54
elog ("sql= {$ sql }" );
@@ -59,7 +60,7 @@ public static function create(array $ary)
59
60
60
61
return self ::$ dbh ->lastInsertId ();
61
62
} catch (\PDOException $ e ) {
62
- exit (__FILE__ . ' ' . __LINE__ . "<br> \n" . $ e ->getMessage ());
63
+ exit (__FILE__ . ' ' . __LINE__ . "<br> \n" . $ e ->getMessage ());
63
64
}
64
65
}
65
66
@@ -69,8 +70,7 @@ public static function read(
69
70
string $ wval = '' ,
70
71
string $ extra = '' ,
71
72
string $ type = 'all '
72
- )
73
- {
73
+ ) {
74
74
elog (__METHOD__ );
75
75
76
76
$ w = $ where ? "
@@ -80,7 +80,7 @@ public static function read(
80
80
81
81
$ sql = "
82
82
SELECT {$ field }
83
- FROM ` ". self ::$ tbl. "` {$ w } {$ extra }" ;
83
+ FROM ` " . self ::$ tbl . "` {$ w } {$ extra }" ;
84
84
85
85
elog ("sql= {$ sql }" );
86
86
@@ -101,13 +101,13 @@ public static function update(array $set, array $where)
101
101
$ where_str = '' ;
102
102
$ where_ary = [];
103
103
foreach ($ where as $ k => $ v ) {
104
- $ where_str .= ' ' . $ v [0 ]. ' ' . $ v [1 ]. ' : ' . $ v [0 ];
104
+ $ where_str .= ' ' . $ v [0 ] . ' ' . $ v [1 ] . ' : ' . $ v [0 ];
105
105
$ where_ary [$ v [0 ]] = $ v [2 ];
106
106
}
107
107
$ ary = array_merge ($ set , $ where_ary );
108
108
109
109
$ sql = '
110
- UPDATE ` '. self ::$ tbl. "` SET {$ set_str }
110
+ UPDATE ` ' . self ::$ tbl . "` SET {$ set_str }
111
111
WHERE {$ where_str }" ;
112
112
113
113
elog ("sql= {$ sql }" );
@@ -118,7 +118,7 @@ public static function update(array $set, array $where)
118
118
119
119
return $ stm ->execute ();
120
120
} catch (\PDOException $ e ) {
121
- exit (__FILE__ . ' ' . __LINE__ . "<br> \n" . $ e ->getMessage ());
121
+ exit (__FILE__ . ' ' . __LINE__ . "<br> \n" . $ e ->getMessage ());
122
122
}
123
123
}
124
124
@@ -129,12 +129,12 @@ public static function delete(array $where)
129
129
$ where_str = '' ;
130
130
$ where_ary = [];
131
131
foreach ($ where as $ k => $ v ) {
132
- $ where_str .= ' ' . $ v [0 ]. ' ' . $ v [1 ]. ' : ' . $ v [0 ];
132
+ $ where_str .= ' ' . $ v [0 ] . ' ' . $ v [1 ] . ' : ' . $ v [0 ];
133
133
$ where_ary [$ v [0 ]] = $ v [2 ];
134
134
}
135
135
136
136
$ sql = '
137
- DELETE FROM ` '. self ::$ tbl. "`
137
+ DELETE FROM ` ' . self ::$ tbl . "`
138
138
WHERE {$ where_str }" ;
139
139
140
140
elog ("sql= {$ sql }" );
@@ -145,7 +145,7 @@ public static function delete(array $where)
145
145
146
146
return $ stm ->execute ();
147
147
} catch (\PDOException $ e ) {
148
- exit (__FILE__ . ' ' . __LINE__ . "<br> \n" . $ e ->getMessage ());
148
+ exit (__FILE__ . ' ' . __LINE__ . "<br> \n" . $ e ->getMessage ());
149
149
}
150
150
}
151
151
@@ -177,7 +177,7 @@ public static function qry(string $sql, array $ary = [], string $type = 'all')
177
177
178
178
return false ;
179
179
} catch (\PDOException $ e ) {
180
- exit (__FILE__ . ' ' . __LINE__ . "<br> \n" . $ e ->getMessage ());
180
+ exit (__FILE__ . ' ' . __LINE__ . "<br> \n" . $ e ->getMessage ());
181
181
}
182
182
}
183
183
@@ -213,7 +213,7 @@ public static function simple($request, $table, $primaryKey, $columns, $extra =
213
213
elog (__METHOD__ );
214
214
215
215
$ db = self ::$ dbh ;
216
- $ cols = '` ' . implode ('`, ` ' , self ::pluck ($ columns , 'db ' )). '` ' ;
216
+ $ cols = '` ' . implode ('`, ` ' , self ::pluck ($ columns , 'db ' )) . '` ' ;
217
217
$ bind = [];
218
218
219
219
$ limit = self ::limit ($ request , $ columns );
@@ -283,7 +283,7 @@ public static function limit($request, $columns)
283
283
$ limit = '' ;
284
284
285
285
if (isset ($ request ['start ' ]) && -1 != $ request ['length ' ]) {
286
- $ limit = 'LIMIT ' . intval ($ request ['start ' ]). ', ' . intval ($ request ['length ' ]);
286
+ $ limit = 'LIMIT ' . intval ($ request ['start ' ]) . ', ' . intval ($ request ['length ' ]);
287
287
}
288
288
289
289
return $ limit ;
@@ -297,23 +297,23 @@ public static function order($request, $columns)
297
297
298
298
if (isset ($ request ['order ' ]) && count ($ request ['order ' ])) {
299
299
$ orderBy = [];
300
- // $dtColumns = self::pluck($columns, 'dt');
300
+ // $dtColumns = self::pluck($columns, 'dt');
301
301
302
302
for ($ i = 0 , $ ien = count ($ request ['order ' ]); $ i < $ ien ; ++$ i ) {
303
303
$ columnIdx = intval ($ request ['order ' ][$ i ]['column ' ]);
304
304
$ requestColumn = $ request ['columns ' ][$ columnIdx ];
305
- // $columnIdx = array_search($requestColumn['data'], $dtColumns); // don't use $dtColumns
305
+ // $columnIdx = array_search($requestColumn['data'], $dtColumns); // don't use $dtColumns
306
306
$ columnIdx = array_search ($ requestColumn ['data ' ], array_column ($ columns , 'dt ' ));
307
307
$ column = $ columns [$ columnIdx ];
308
308
309
309
if ('true ' == $ requestColumn ['orderable ' ]) {
310
310
$ dir = 'asc ' === $ request ['order ' ][$ i ]['dir ' ] ? 'ASC ' : 'DESC ' ;
311
- $ orderBy [] = '` ' . $ column ['db ' ]. '` ' . $ dir ;
311
+ $ orderBy [] = '` ' . $ column ['db ' ] . '` ' . $ dir ;
312
312
}
313
313
}
314
314
315
315
if (count ($ orderBy )) {
316
- $ order = 'ORDER BY ' . implode (', ' , $ orderBy );
316
+ $ order = 'ORDER BY ' . implode (', ' , $ orderBy );
317
317
}
318
318
}
319
319
@@ -336,8 +336,8 @@ public static function filter($request, $columns, &$bindings)
336
336
$ column = $ columns [$ columnIdx ];
337
337
338
338
if ('true ' == $ requestColumn ['searchable ' ] && $ column ['db ' ]) {
339
- $ binding = self ::bind ($ bindings , '% ' . $ str. '% ' , PDO ::PARAM_STR );
340
- $ globalSearch [] = '` ' . $ column ['db ' ]. '` LIKE ' . $ binding ;
339
+ $ binding = self ::bind ($ bindings , '% ' . $ str . '% ' , PDO ::PARAM_STR );
340
+ $ globalSearch [] = '` ' . $ column ['db ' ] . '` LIKE ' . $ binding ;
341
341
}
342
342
}
343
343
}
@@ -352,9 +352,9 @@ public static function filter($request, $columns, &$bindings)
352
352
$ str = $ requestColumn ['search ' ]['value ' ];
353
353
354
354
if ('true ' == $ requestColumn ['searchable ' ] && '' != $ str && null !== $ column ['db ' ]) {
355
- $ binding = self ::bind ($ bindings , '% ' . $ str. '% ' , PDO ::PARAM_STR );
355
+ $ binding = self ::bind ($ bindings , '% ' . $ str . '% ' , PDO ::PARAM_STR );
356
356
if ($ column ['db ' ]) {
357
- $ columnSearch [] = '` ' . $ column ['db ' ]. '` LIKE ' . $ binding ;
357
+ $ columnSearch [] = '` ' . $ column ['db ' ] . '` LIKE ' . $ binding ;
358
358
}
359
359
}
360
360
}
@@ -364,17 +364,17 @@ public static function filter($request, $columns, &$bindings)
364
364
$ where = '' ;
365
365
366
366
if (count ($ globalSearch )) {
367
- $ where = '( ' . implode (' OR ' , $ globalSearch ). ') ' ;
367
+ $ where = '( ' . implode (' OR ' , $ globalSearch ) . ') ' ;
368
368
}
369
369
370
370
if (count ($ columnSearch )) {
371
371
$ where = '' === $ where ?
372
372
implode (' AND ' , $ columnSearch ) :
373
- $ where. ' AND ' . implode (' AND ' , $ columnSearch );
373
+ $ where . ' AND ' . implode (' AND ' , $ columnSearch );
374
374
}
375
375
376
376
if ('' !== $ where ) {
377
- $ where = 'WHERE ' . $ where ;
377
+ $ where = 'WHERE ' . $ where ;
378
378
}
379
379
380
380
return $ where ;
@@ -405,7 +405,7 @@ public static function sql_exec($db, $bindings, $sql = null, string $type = 'all
405
405
try {
406
406
$ stmt ->execute ();
407
407
} catch (PDOException $ e ) {
408
- self ::fatal ('An SQL error occurred: ' . $ e ->getMessage ());
408
+ self ::fatal ('An SQL error occurred: ' . $ e ->getMessage ());
409
409
}
410
410
411
411
if ('all ' === $ type ) {
@@ -435,7 +435,7 @@ private static function bind(&$a, $val, $type)
435
435
{
436
436
elog (__METHOD__ );
437
437
438
- $ key = ':binding_ ' . count ($ a );
438
+ $ key = ':binding_ ' . count ($ a );
439
439
$ a [] = ['key ' => $ key , 'val ' => $ val , 'type ' => $ type ];
440
440
441
441
return $ key ;
0 commit comments