1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ADOdb SQL Related Functionality |
4
|
|
|
* @author Joe Huss <[email protected]> |
5
|
|
|
* @copyright 2018 |
6
|
|
|
* @package MyAdmin |
7
|
|
|
* @category SQL |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace MyDb\Adodb; |
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 $driver = 'mysql'; |
22
|
|
|
public $Rows = []; |
23
|
|
|
public $type = 'adodb'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Db::connect() |
27
|
|
|
* @param string $database |
28
|
|
|
* @param string $host |
29
|
|
|
* @param string $user |
30
|
|
|
* @param string $password |
31
|
|
|
* @param string $driver |
32
|
|
|
* @return bool|\the |
33
|
|
|
*/ |
34
|
|
|
public function connect($database = '', $host = '', $user = '', $password = '', $driver = 'mysql') { |
35
|
|
|
/* Handle defaults */ |
36
|
|
|
if ('' == $database) |
37
|
|
|
$database = $this->database; |
38
|
|
|
if ('' == $host) |
39
|
|
|
$host = $this->host; |
40
|
|
|
if ('' == $user) |
41
|
|
|
$user = $this->user; |
42
|
|
|
if ('' == $password) |
43
|
|
|
$password = $this->password; |
44
|
|
|
if ('' == $driver) |
45
|
|
|
$driver = $this->driver; |
46
|
|
|
/* establish connection, select database */ |
47
|
|
|
if ($this->linkId === false) { |
|
|
|
|
48
|
|
|
$this->linkId = NewADOConnection($driver); |
49
|
|
|
$this->linkId->Connect($host, $user, $password, $database); |
50
|
|
|
} |
51
|
|
|
return $this->linkId; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/* This only affects systems not using persistent connections */ |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Db::disconnect() |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function disconnect() { |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $string |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function real_escape($string = '') { |
68
|
|
|
return escapeshellarg($string); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* discard the query result |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function free() { |
76
|
|
|
// @mysql_free_result($this->queryId); |
77
|
|
|
// $this->queryId = 0; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Db::queryReturn() |
82
|
|
|
* |
83
|
|
|
* Sends an SQL query to the server like the normal query() command but iterates through |
84
|
|
|
* any rows and returns the row or rows immediately or FALSE on error |
85
|
|
|
* |
86
|
|
|
* @param mixed $query SQL Query to be used |
87
|
|
|
* @param string $line optionally pass __LINE__ calling the query for logging |
88
|
|
|
* @param string $file optionally pass __FILE__ calling the query for logging |
89
|
|
|
* @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
90
|
|
|
*/ |
91
|
|
|
public function queryReturn($query, $line = '', $file = '') { |
92
|
|
|
$this->query($query, $line, $file); |
93
|
|
|
if ($this->num_rows() == 0) { |
94
|
|
|
return false; |
95
|
|
|
} elseif ($this->num_rows() == 1) { |
96
|
|
|
$this->next_record(MYSQL_ASSOC); |
|
|
|
|
97
|
|
|
return $this->Record; |
98
|
|
|
} else { |
99
|
|
|
$out = []; |
100
|
|
|
while ($this->next_record(MYSQL_ASSOC)) |
|
|
|
|
101
|
|
|
$out[] = $this->Record; |
102
|
|
|
return $out; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* db:qr() |
108
|
|
|
* |
109
|
|
|
* alias of queryReturn() |
110
|
|
|
* |
111
|
|
|
* @param mixed $query SQL Query to be used |
112
|
|
|
* @param string $line optionally pass __LINE__ calling the query for logging |
113
|
|
|
* @param string $file optionally pass __FILE__ calling the query for logging |
114
|
|
|
* @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
115
|
|
|
*/ |
116
|
|
|
public function qr($query, $line = '', $file = '') { |
117
|
|
|
return $this->queryReturn($query, $line, $file); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Db::query() |
122
|
|
|
* |
123
|
|
|
* Sends an SQL query to the database |
124
|
|
|
* |
125
|
|
|
* @param mixed $queryString |
126
|
|
|
* @param string $line |
127
|
|
|
* @param string $file |
128
|
|
|
* @return mixed 0 if no query or query id handler, safe to ignore this return |
129
|
|
|
*/ |
130
|
|
|
public function query($queryString, $line = '', $file = '') { |
131
|
|
|
/* No empty queries, please, since PHP4 chokes on them. */ |
132
|
|
|
/* The empty query string is passed on from the constructor, |
133
|
|
|
* when calling the class without a query, e.g. in situations |
134
|
|
|
* like these: '$db = new db_Subclass;' |
135
|
|
|
*/ |
136
|
|
|
if ($queryString == '') |
137
|
|
|
return 0; |
138
|
|
|
if (!$this->connect()) { |
139
|
|
|
return 0; |
140
|
|
|
/* we already complained in connect() about that. */ |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// New query, discard previous result. |
144
|
|
|
if ($this->queryId !== false) |
|
|
|
|
145
|
|
|
$this->free(); |
146
|
|
|
|
147
|
|
|
if ($this->Debug) |
148
|
|
|
printf("Debug: query = %s<br>\n", $queryString); |
149
|
|
|
if ($GLOBALS['log_queries'] !== false) { |
150
|
|
|
$this->log($queryString, $line, $file); |
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
try { |
155
|
|
|
$this->queryId = $this->linkId->Execute($queryString); |
156
|
|
|
} catch (exception $e) { |
|
|
|
|
157
|
|
|
$this->emailError($queryString, $e, $line, $file); |
158
|
|
|
} |
159
|
|
|
$this->log("ADOdb Query $queryString (S:$success) - ".count($this->Rows).' Rows', __LINE__, __FILE__); |
|
|
|
|
160
|
|
|
$this->Row = 0; |
161
|
|
|
|
162
|
|
|
// Will return nada if it fails. That's fine. |
163
|
|
|
return $this->queryId; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Db::next_record() |
168
|
|
|
* @param mixed $resultType |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
|
|
public function next_record($resultType = MYSQL_ASSOC) { |
|
|
|
|
172
|
|
|
if (!$this->queryId) { |
173
|
|
|
$this->halt('next_record called with no query pending.'); |
174
|
|
|
return 0; |
175
|
|
|
} |
176
|
|
|
++$this->Row; |
177
|
|
|
$this->Record = $this->queryId->FetchRow(); |
|
|
|
|
178
|
|
|
$stat = is_array($this->Record); |
179
|
|
|
if (!$stat && $this->autoFree) |
180
|
|
|
$this->free(); |
181
|
|
|
return $stat; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/* public: position in result set */ |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Db::seek() |
188
|
|
|
* @param integer $pos |
189
|
|
|
* @return int |
190
|
|
|
*/ |
191
|
|
|
public function seek($pos = 0) { |
192
|
|
|
if (isset($this->Rows[$pos])) { |
193
|
|
|
$this->Row = $pos; |
194
|
|
|
} else { |
195
|
|
|
$this->halt("seek($pos) failed: result has ".count($this->Rows).' rows'); |
196
|
|
|
/* half assed attempt to save the day, |
197
|
|
|
* but do not consider this documented or even |
198
|
|
|
* desirable behaviour. |
199
|
|
|
*/ |
200
|
|
|
return 0; |
201
|
|
|
} |
202
|
|
|
return 1; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Db::transactionBegin() |
207
|
|
|
* @return bool |
208
|
|
|
*/ |
209
|
|
|
public function transactionBegin() { |
210
|
|
|
return true; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Db::transactionCommit() |
215
|
|
|
* @return bool |
216
|
|
|
*/ |
217
|
|
|
public function transactionCommit() { |
218
|
|
|
return true; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Db::transactionAbort() |
223
|
|
|
* @return bool |
224
|
|
|
*/ |
225
|
|
|
public function transactionAbort() { |
226
|
|
|
return true; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Db::getLastInsertId() |
231
|
|
|
* |
232
|
|
|
* @param mixed $table |
233
|
|
|
* @param mixed $field |
234
|
|
|
* @return mixed |
235
|
|
|
*/ |
236
|
|
|
public function getLastInsertId($table, $field) { |
237
|
|
|
return $this->linkId->Insert_ID($table, $field); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/* public: table locking */ |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Db::lock() |
244
|
|
|
* @param mixed $table |
245
|
|
|
* @param string $mode |
246
|
|
|
* @return void |
247
|
|
|
*/ |
248
|
|
|
public function lock($table, $mode = 'write') { |
|
|
|
|
249
|
|
|
/* $this->connect(); |
250
|
|
|
|
251
|
|
|
* $query = "lock tables "; |
252
|
|
|
* if (is_array($table)) |
253
|
|
|
* { |
254
|
|
|
* foreach ($table as $key => $value) |
255
|
|
|
* { |
256
|
|
|
* if ($key == "read" && $key!=0) |
257
|
|
|
* { |
258
|
|
|
* $query .= "$value read, "; |
259
|
|
|
* } |
260
|
|
|
* else |
261
|
|
|
* { |
262
|
|
|
* $query .= "$value $mode, "; |
263
|
|
|
* } |
264
|
|
|
* } |
265
|
|
|
* $query = mb_substr($query,0,-2); |
266
|
|
|
* } |
267
|
|
|
* else |
268
|
|
|
* { |
269
|
|
|
* $query .= "$table $mode"; |
270
|
|
|
* } |
271
|
|
|
* $res = @mysql_query($query, $this->linkId); |
272
|
|
|
* if (!$res) |
273
|
|
|
* { |
274
|
|
|
* $this->halt("lock($table, $mode) failed."); |
275
|
|
|
* return 0; |
276
|
|
|
* } |
277
|
|
|
* return $res; |
278
|
|
|
*/ |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Db::unlock() |
283
|
|
|
* @return void |
284
|
|
|
*/ |
285
|
|
|
public function unlock() { |
286
|
|
|
/* $this->connect(); |
287
|
|
|
|
288
|
|
|
* $res = @mysql_query("unlock tables"); |
289
|
|
|
* if (!$res) |
290
|
|
|
* { |
291
|
|
|
* $this->halt("unlock() failed."); |
292
|
|
|
* return 0; |
293
|
|
|
* } |
294
|
|
|
* return $res; |
295
|
|
|
*/ |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/* public: evaluate the result (size, width) */ |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Db::affectedRows() |
302
|
|
|
* |
303
|
|
|
* @return mixed |
304
|
|
|
*/ |
305
|
|
|
public function affectedRows() { |
306
|
|
|
return @$this->linkId->Affected_Rows(); |
307
|
|
|
// return @$this->queryId->rowCount(); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Db::num_rows() |
312
|
|
|
* |
313
|
|
|
* @return mixed |
314
|
|
|
*/ |
315
|
|
|
public function num_rows() { |
316
|
|
|
return $this->queryId->NumRows(); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Db::num_fields() |
321
|
|
|
* |
322
|
|
|
* @return mixed |
323
|
|
|
*/ |
324
|
|
|
public function num_fields() { |
325
|
|
|
return $this->queryId->NumCols(); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @param mixed $msg |
330
|
|
|
* @param string $line |
331
|
|
|
* @param string $file |
332
|
|
|
* @return mixed|void |
333
|
|
|
*/ |
334
|
|
|
public function haltmsg($msg, $line = '', $file = '') { |
335
|
|
|
$this->log("Database error: $msg", $line, $file, 'error'); |
336
|
|
|
if ($this->linkId->ErrorNo() != '0' && $this->linkId->ErrorMsg() != '') |
337
|
|
|
$this->log('ADOdb SQL Error: '.$this->linkId->ErrorMsg(), $line, $file, 'error'); |
338
|
|
|
$this->logBackTrace($msg, $line, $file); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Db::tableNames() |
343
|
|
|
* |
344
|
|
|
* @return array |
345
|
|
|
*/ |
346
|
|
|
public function tableNames() { |
347
|
|
|
$return = []; |
348
|
|
|
$this->query('SHOW TABLES'); |
349
|
|
|
$i = 0; |
350
|
|
|
while ($info = $this->queryId->FetchRow()) { |
351
|
|
|
$return[$i]['table_name'] = $info[0]; |
352
|
|
|
$return[$i]['tablespace_name'] = $this->database; |
353
|
|
|
$return[$i]['database'] = $this->database; |
354
|
|
|
++$i; |
355
|
|
|
} |
356
|
|
|
return $return; |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|