1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MDB2 Wrapper Made To Handle Like Our Other Classes Related Functionality |
4
|
|
|
* @author Joe Huss <[email protected]> |
5
|
|
|
* @copyright 2018 |
6
|
|
|
* @package MyAdmin |
7
|
|
|
* @category SQL |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace MyDb\Mdb2; |
11
|
|
|
|
12
|
|
|
use \MyDb\Generic; |
13
|
|
|
use \MyDb\Db_Interface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Db |
17
|
|
|
* |
18
|
|
|
* @access public |
19
|
|
|
*/ |
20
|
|
|
class Db extends Generic implements Db_Interface { |
21
|
|
|
public $host = 'localhost'; |
22
|
|
|
public $user = 'pdns'; |
23
|
|
|
public $password = ''; |
24
|
|
|
public $database = 'pdns'; |
25
|
|
|
public $type = 'mdb2'; |
26
|
|
|
public $error = false; |
27
|
|
|
public $message = ''; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Db::quote() |
31
|
|
|
* @param string $text |
32
|
|
|
* @param string $type |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public function quote($text = '', $type = 'text') { |
36
|
|
|
switch ($type) { |
37
|
|
|
case 'text': |
38
|
|
|
return "'".mysqli_real_escape_string($this->linkId, $text)."'"; |
|
|
|
|
39
|
|
|
break; |
|
|
|
|
40
|
|
|
case 'integer': |
41
|
|
|
return (int) $text; |
42
|
|
|
break; |
43
|
|
|
default: |
44
|
|
|
return $text; |
45
|
|
|
break; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Db::queryOne() |
51
|
|
|
* |
52
|
|
|
* @param mixed $query |
53
|
|
|
* @param string $line |
54
|
|
|
* @param string $file |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function queryOne($query, $line = '', $file = '') { |
58
|
|
|
$this->query($query, $line, $file); |
59
|
|
|
if ($this->num_rows() > 0) { |
60
|
|
|
$this->next_record(); |
61
|
|
|
return $this->f(0); |
62
|
|
|
} else |
63
|
|
|
return 0; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Db::queryRow() |
68
|
|
|
* |
69
|
|
|
* @param mixed $query |
70
|
|
|
* @param string $line |
71
|
|
|
* @param string $file |
72
|
|
|
* @return array|bool |
73
|
|
|
*/ |
74
|
|
|
public function queryRow($query, $line = '', $file = '') { |
75
|
|
|
$this->query($query, $line, $file); |
76
|
|
|
if ($this->num_rows() > 0) { |
77
|
|
|
$this->next_record(); |
78
|
|
|
return $this->Record; |
79
|
|
|
} else |
80
|
|
|
return 0; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Db::lastInsertId() |
85
|
|
|
* @param mixed $table |
86
|
|
|
* @param mixed $field |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
|
public function lastInsertId($table, $field) { |
90
|
|
|
return $this->getLastInsertId($table, $field); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* alias function of select_db, changes the database we are working with. |
95
|
|
|
* |
96
|
|
|
* @param string $database the name of the database to use |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
public function useDb($database) { |
100
|
|
|
$this->selectDb($database); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* changes the database we are working with. |
105
|
|
|
* |
106
|
|
|
* @param string $database the name of the database to use |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
public function selectDb($database) { |
110
|
|
|
$this->connect(); |
111
|
|
|
mysqli_select_db($this->linkId, $database); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/* public: connection management */ |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Db::connect() |
118
|
|
|
* @param string $database |
119
|
|
|
* @param string $host |
120
|
|
|
* @param string $user |
121
|
|
|
* @param string $password |
122
|
|
|
* @return int|\mysqli |
123
|
|
|
*/ |
124
|
|
|
public function connect($database = '', $host = '', $user = '', $password = '') { |
125
|
|
|
/* Handle defaults */ |
126
|
|
|
if ('' == $database) |
127
|
|
|
$database = $this->database; |
128
|
|
|
if ('' == $host) |
129
|
|
|
$host = $this->host; |
130
|
|
|
if ('' == $user) |
131
|
|
|
$user = $this->user; |
132
|
|
|
if ('' == $password) |
133
|
|
|
$password = $this->password; |
134
|
|
|
/* establish connection, select database */ |
135
|
|
|
if (!is_object($this->linkId)) { |
|
|
|
|
136
|
|
|
$this->connectionAttempt++; |
137
|
|
|
if ($this->connectionAttempt > 1) |
138
|
|
|
error_log("MySQLi Connection Attempt #{$this->connectionAttempt}/{$this->maxConnectErrors}"); |
139
|
|
|
if ($this->connectionAttempt >= $this->maxConnectErrors) { |
140
|
|
|
$this->halt("connect($host, $user, \$password) failed. ".$mysqli->connect_error); |
|
|
|
|
141
|
|
|
return 0; |
142
|
|
|
} |
143
|
|
|
$this->linkId = mysqli_init(); |
144
|
|
|
$this->linkId->options(MYSQLI_INIT_COMMAND, "SET NAMES {$this->characterSet} COLLATE {$this->collation}, COLLATION_CONNECTION = {$this->collation}, COLLATION_DATABASE = {$this->collation}"); |
145
|
|
|
$this->linkId->real_connect($host, $user, $password, $database); |
146
|
|
|
$this->linkId->set_charset($this->characterSet); |
147
|
|
|
if ($this->linkId->connect_errno) { |
148
|
|
|
$this->halt("connect($host, $user, \$password) failed. ".$mysqli->connect_error); |
149
|
|
|
return 0; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
return $this->linkId; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/* This only affects systems not using persistent connections */ |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Db::disconnect() |
159
|
|
|
* @return int |
160
|
|
|
*/ |
161
|
|
|
public function disconnect() { |
162
|
|
|
if (is_object($this->linkId)) |
|
|
|
|
163
|
|
|
return $this->linkId->close(); |
164
|
|
|
else |
165
|
|
|
return 0; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param $string |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function real_escape($string) { |
173
|
|
|
if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect()) |
|
|
|
|
174
|
|
|
return mysqli_escape_string($link, $string); |
|
|
|
|
175
|
|
|
return mysqli_real_escape_string($this->linkId, $string); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param $string |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function escape($string) { |
183
|
|
|
return mysqli_real_escape_string($this->linkId, $string); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Db::dbAddslashes() |
188
|
|
|
* @param mixed $str |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
|
|
public function dbAddslashes($str) { |
192
|
|
|
if (!isset($str) || $str == '') |
193
|
|
|
return ''; |
194
|
|
|
|
195
|
|
|
return addslashes($str); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* discard the query result |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
|
|
public function free() { |
203
|
|
|
if (is_resource($this->queryId)) |
|
|
|
|
204
|
|
|
@mysqli_free_result($this->queryId); |
|
|
|
|
205
|
|
|
$this->queryId = 0; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Db::queryReturn() |
210
|
|
|
* |
211
|
|
|
* Sends an SQL query to the server like the normal query() command but iterates through |
212
|
|
|
* any rows and returns the row or rows immediately or FALSE on error |
213
|
|
|
* |
214
|
|
|
* @param mixed $query SQL Query to be used |
215
|
|
|
* @param string $line optionally pass __LINE__ calling the query for logging |
216
|
|
|
* @param string $file optionally pass __FILE__ calling the query for logging |
217
|
|
|
* @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
218
|
|
|
*/ |
219
|
|
|
public function queryReturn($query, $line = '', $file = '') { |
220
|
|
|
$this->query($query, $line, $file); |
221
|
|
|
if ($this->num_rows() == 0) { |
222
|
|
|
return false; |
223
|
|
|
} elseif ($this->num_rows() == 1) { |
224
|
|
|
$this->next_record(MYSQL_ASSOC); |
|
|
|
|
225
|
|
|
return $this->Record; |
226
|
|
|
} else { |
227
|
|
|
$out = []; |
228
|
|
|
while ($this->next_record(MYSQL_ASSOC)) |
|
|
|
|
229
|
|
|
$out[] = $this->Record; |
230
|
|
|
return $out; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* db:qr() |
236
|
|
|
* |
237
|
|
|
* alias of queryReturn() |
238
|
|
|
* |
239
|
|
|
* @param mixed $query SQL Query to be used |
240
|
|
|
* @param string $line optionally pass __LINE__ calling the query for logging |
241
|
|
|
* @param string $file optionally pass __FILE__ calling the query for logging |
242
|
|
|
* @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
243
|
|
|
*/ |
244
|
|
|
public function qr($query, $line = '', $file = '') { |
245
|
|
|
return $this->queryReturn($query, $line, $file); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Db::query() |
250
|
|
|
* |
251
|
|
|
* Sends an SQL query to the database |
252
|
|
|
* |
253
|
|
|
* @param mixed $queryString |
254
|
|
|
* @param string $line |
255
|
|
|
* @param string $file |
256
|
|
|
* @return mixed 0 if no query or query id handler, safe to ignore this return |
257
|
|
|
*/ |
258
|
|
|
public function query($queryString, $line = '', $file = '') { |
259
|
|
|
/* No empty queries, please, since PHP4 chokes on them. */ |
260
|
|
|
/* The empty query string is passed on from the constructor, |
261
|
|
|
* when calling the class without a query, e.g. in situations |
262
|
|
|
* like these: '$db = new db_Subclass;' |
263
|
|
|
*/ |
264
|
|
|
if ($queryString == '') |
265
|
|
|
return 0; |
266
|
|
|
if (!$this->connect()) { |
267
|
|
|
return 0; |
268
|
|
|
/* we already complained in connect() about that. */ |
269
|
|
|
} |
270
|
|
|
$haltPrev = $this->haltOnError; |
271
|
|
|
$this->haltOnError = 'no'; |
272
|
|
|
// New query, discard previous result. |
273
|
|
|
if (is_resource($this->queryId)) |
|
|
|
|
274
|
|
|
$this->free(); |
275
|
|
|
if ($this->Debug) |
276
|
|
|
printf("Debug: query = %s<br>\n", $queryString); |
277
|
|
|
if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) |
278
|
|
|
$this->log($queryString, $line, $file); |
279
|
|
|
$tries = 3; |
280
|
|
|
$try = 1; |
281
|
|
|
$this->queryId = @mysqli_query($this->linkId, $queryString, MYSQLI_STORE_RESULT); |
|
|
|
|
282
|
|
|
$this->Row = 0; |
283
|
|
|
$this->Errno = @mysqli_errno($this->linkId); |
|
|
|
|
284
|
|
|
$this->Error = @mysqli_error($this->linkId); |
|
|
|
|
285
|
|
|
while ($this->queryId === false && $try <= $tries) { |
286
|
|
|
$this->message = 'MySQL error '.@mysqli_errno($this->linkId).': '.@mysqli_error($this->linkId).' Query: '.$this->query; |
|
|
|
|
287
|
|
|
$this->error = true; |
288
|
|
|
@mysqli_close($this->linkId); |
|
|
|
|
289
|
|
|
$this->connect(); |
290
|
|
|
$try++; |
291
|
|
|
} |
292
|
|
|
$this->haltOnError = $haltPrev; |
293
|
|
|
if ($this->queryId === false) { |
294
|
|
|
$this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
// Will return nada if it fails. That's fine. |
298
|
|
|
return $this->queryId; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/* public: walk result set */ |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Db::next_record() |
305
|
|
|
* |
306
|
|
|
* @param mixed $resultType |
307
|
|
|
* @return bool |
308
|
|
|
*/ |
309
|
|
|
public function next_record($resultType = MYSQLI_BOTH) { |
310
|
|
|
if ($this->queryId === false) { |
|
|
|
|
311
|
|
|
$this->halt('next_record called with no query pending.'); |
312
|
|
|
return 0; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
$this->Record = @mysqli_fetch_array($this->queryId, $resultType); |
|
|
|
|
316
|
|
|
++$this->Row; |
317
|
|
|
$this->Errno = mysqli_errno($this->linkId); |
|
|
|
|
318
|
|
|
$this->Error = mysqli_error($this->linkId); |
|
|
|
|
319
|
|
|
|
320
|
|
|
$stat = is_array($this->Record); |
321
|
|
|
if (!$stat && $this->autoFree && is_resource($this->queryId)) |
|
|
|
|
322
|
|
|
$this->free(); |
323
|
|
|
return $stat; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/* public: position in result set */ |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Db::seek() |
330
|
|
|
* @param integer $pos |
331
|
|
|
* @return int |
332
|
|
|
*/ |
333
|
|
|
public function seek($pos = 0) { |
334
|
|
|
$status = @mysqli_data_seek($this->queryId, $pos); |
|
|
|
|
335
|
|
|
if ($status) { |
336
|
|
|
$this->Row = $pos; |
337
|
|
|
} else { |
338
|
|
|
$this->halt("seek($pos) failed: result has ".$this->num_rows().' rows'); |
339
|
|
|
/* half assed attempt to save the day, |
340
|
|
|
* but do not consider this documented or even |
341
|
|
|
* desirable behaviour. |
342
|
|
|
*/ |
343
|
|
|
@mysqli_data_seek($this->queryId, $this->num_rows()); |
|
|
|
|
344
|
|
|
$this->Row = $this->num_rows; |
|
|
|
|
345
|
|
|
return 0; |
346
|
|
|
} |
347
|
|
|
return 1; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Db::transactionBegin() |
352
|
|
|
* @return bool |
353
|
|
|
*/ |
354
|
|
|
public function transactionBegin() { |
355
|
|
|
return true; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Db::transactionCommit() |
360
|
|
|
* @return bool |
361
|
|
|
*/ |
362
|
|
|
public function transactionCommit() { |
363
|
|
|
return true; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Db::transactionAbort() |
368
|
|
|
* @return bool |
369
|
|
|
*/ |
370
|
|
|
public function transactionAbort() { |
371
|
|
|
return true; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Db::getLastInsertId() |
376
|
|
|
* @param mixed $table |
377
|
|
|
* @param mixed $field |
378
|
|
|
* @return int|string |
379
|
|
|
*/ |
380
|
|
|
public function getLastInsertId($table, $field) { |
381
|
|
|
/* This will get the last insert ID created on the current connection. Should only be called |
382
|
|
|
* after an insert query is run on a table that has an auto incrementing field. $table and |
383
|
|
|
* $field are required, but unused here since it's unnecessary for mysql. For compatibility |
384
|
|
|
* with pgsql, the params must be supplied. |
385
|
|
|
*/ |
386
|
|
|
|
387
|
|
|
if (!isset($table) || $table == '' || !isset($field) || $field == '') |
388
|
|
|
return -1; |
389
|
|
|
|
390
|
|
|
return @mysqli_insert_id($this->linkId); |
|
|
|
|
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/* public: table locking */ |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Db::lock() |
397
|
|
|
* @param mixed $table |
398
|
|
|
* @param string $mode |
399
|
|
|
* @return bool|int|\mysqli_result |
400
|
|
|
*/ |
401
|
|
|
public function lock($table, $mode = 'write') { |
402
|
|
|
$this->connect(); |
403
|
|
|
|
404
|
|
|
$query = 'lock tables '; |
405
|
|
|
if (is_array($table)) { |
406
|
|
|
while (list($key, $value) = each($table)) { |
|
|
|
|
407
|
|
|
if ($key == 'read' && $key != 0) { |
408
|
|
|
$query .= "$value read, "; |
409
|
|
|
} else { |
410
|
|
|
$query .= "$value $mode, "; |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
$query = mb_substr($query, 0, -2); |
414
|
|
|
} else { |
415
|
|
|
$query .= "$table $mode"; |
416
|
|
|
} |
417
|
|
|
$res = @mysqli_query($this->linkId, $query); |
|
|
|
|
418
|
|
|
if (!$res) { |
419
|
|
|
$this->halt("lock($table, $mode) failed."); |
420
|
|
|
return 0; |
421
|
|
|
} |
422
|
|
|
return $res; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Db::unlock() |
427
|
|
|
* @return bool|int|\mysqli_result |
428
|
|
|
*/ |
429
|
|
|
public function unlock() { |
430
|
|
|
$this->connect(); |
431
|
|
|
|
432
|
|
|
$res = @mysqli_query($this->linkId, 'unlock tables'); |
|
|
|
|
433
|
|
|
if (!$res) { |
434
|
|
|
$this->halt('unlock() failed.'); |
435
|
|
|
return 0; |
436
|
|
|
} |
437
|
|
|
return $res; |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/* public: evaluate the result (size, width) */ |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Db::affectedRows() |
444
|
|
|
* @return int |
445
|
|
|
*/ |
446
|
|
|
public function affectedRows() { |
447
|
|
|
return @mysqli_affected_rows($this->linkId); |
|
|
|
|
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* Db::num_rows() |
452
|
|
|
* @return int |
453
|
|
|
*/ |
454
|
|
|
public function num_rows() { |
455
|
|
|
return @mysqli_num_rows($this->queryId); |
|
|
|
|
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Db::num_fields() |
460
|
|
|
* @return int |
461
|
|
|
*/ |
462
|
|
|
public function num_fields() { |
463
|
|
|
return @mysqli_num_fields($this->queryId); |
|
|
|
|
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Db::tableNames() |
468
|
|
|
* |
469
|
|
|
* @return array |
470
|
|
|
*/ |
471
|
|
|
public function tableNames() { |
472
|
|
|
$return = []; |
473
|
|
|
$this->query('SHOW TABLES'); |
474
|
|
|
$i = 0; |
475
|
|
|
while ($info = $this->queryId->fetch_row()) { |
|
|
|
|
476
|
|
|
$return[$i]['table_name'] = $info[0]; |
477
|
|
|
$return[$i]['tablespace_name'] = $this->database; |
478
|
|
|
$return[$i]['database'] = $this->database; |
479
|
|
|
++$i; |
480
|
|
|
} |
481
|
|
|
return $return; |
482
|
|
|
} |
483
|
|
|
} |
484
|
|
|
|