1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Mysql Database class |
5
|
|
|
* |
6
|
|
|
* François - aka fser - Serman |
7
|
|
|
* |
8
|
|
|
* 2014/06/24 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
class DB_Sql { |
12
|
|
|
|
13
|
|
|
/* public: connection parameters */ |
14
|
|
|
private $Host; |
15
|
|
|
private $Database; |
16
|
|
|
private $User; |
17
|
|
|
private $Password; |
18
|
|
|
|
19
|
|
|
/* public: configuration parameters */ |
20
|
|
|
private $Auto_Free = False; // Set to True for automatic mysql_free_result() |
21
|
|
|
private $Debug = False; // Set to 1 for debugging messages. |
22
|
|
|
private $Halt_On_Error = "no"; // "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning) |
23
|
|
|
private $Seq_Table = "db_sequence"; |
24
|
|
|
|
25
|
|
|
/* public: result array and current row number */ |
26
|
|
|
public /* FIXME */ $Record = array(); |
27
|
|
|
private $Row = 0; |
28
|
|
|
private $num_rows; |
29
|
|
|
|
30
|
|
|
/* public: current error number and error text */ |
31
|
|
|
private $Errno; |
32
|
|
|
private $Error; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/* private: link and query handles */ |
36
|
|
|
private $Query_String; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/* PDO related variables */ |
40
|
|
|
private $pdo_instance = NULL; |
41
|
|
|
private $pdo_query = NULL; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor |
45
|
|
|
*/ |
46
|
|
|
function __construct($db, $host, $user, $passwd) { |
|
|
|
|
47
|
|
|
|
48
|
|
|
$dsn = sprintf('mysql:dbname=%s;host=%s', $db, $host); |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
$this->pdo_instance = new PDO($dsn, $user, $passwd); |
52
|
|
|
} catch (PDOException $e) { |
53
|
|
|
echo "Mysql", "PDO instance", $e->getMessage(); |
54
|
|
|
return FALSE; |
|
|
|
|
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* function for MySQL database connection management |
60
|
|
|
* |
61
|
|
|
* This function manages the connection to the MySQL database. |
62
|
|
|
* |
63
|
|
|
* @param $Database name of the database |
64
|
|
|
* @param $Host DNS of the MySQL hosting server |
65
|
|
|
* @param $User the user's name |
66
|
|
|
* @param $Password the user's password |
67
|
|
|
* |
68
|
|
|
* @return the class variable $Link_ID |
69
|
|
|
*/ |
70
|
|
|
function connect($Database = "", $Host = "", $User = "", $Password = "") { |
71
|
|
|
global $err; |
72
|
|
|
$this->halt('Mysql::connect() : This function should no longer be used'); |
73
|
|
|
/* Handle defaults */ |
74
|
|
|
if ("" == $Database) |
75
|
|
|
$Database = $this->Database; |
76
|
|
|
if ("" == $Host) |
77
|
|
|
$Host = $this->Host; |
78
|
|
|
if ("" == $User) |
79
|
|
|
$User = $this->User; |
80
|
|
|
if ("" == $Password) |
81
|
|
|
$Password = $this->Password; |
82
|
|
|
|
83
|
|
|
if (!$this->pdo_instance) { |
84
|
|
|
$dsn = sprintf('mysql:dbname=%s;host=%s', $Database, $Host); |
85
|
|
|
|
86
|
|
|
try { |
87
|
|
|
$this->pdo_instance = new PDO($dsn, $User, $Password); |
88
|
|
|
} catch (PDOException $e) { |
89
|
|
|
$this->halt("Mysql::PDO_instance" . $e->getMessage()); |
90
|
|
|
return FALSE; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return True; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Discard the query result |
99
|
|
|
* |
100
|
|
|
* This function discards the last query result. |
101
|
|
|
*/ |
102
|
|
|
function free() { |
103
|
|
|
$this->pdo_query->closeCursor(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
function is_connected() { |
|
|
|
|
107
|
|
|
return $this->pdo_instance != FALSE; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
function last_error() { |
|
|
|
|
111
|
|
|
return $this->Error; |
112
|
|
|
} |
113
|
|
|
/** |
114
|
|
|
* Perform a query |
115
|
|
|
* |
116
|
|
|
* This function performs the MySQL query described in the string parameter |
117
|
|
|
* |
118
|
|
|
* @param a string describing the MySQL query |
119
|
|
|
* @param arguments is an optionnal array for future use with PDO parametrized requests |
120
|
|
|
* @return the $Query_ID class variable (null if fails) |
121
|
|
|
*/ |
122
|
|
|
function query($Query_String, $arguments = false) { |
|
|
|
|
123
|
|
|
global $debug_alternc; |
124
|
|
|
|
125
|
|
|
if (empty($Query_String) || !$this->is_connected()) |
126
|
|
|
return FALSE; |
127
|
|
|
|
128
|
|
|
$this->Query_String = $Query_String; |
129
|
|
|
if ($this->Debug) |
130
|
|
|
printf("Debug: query = %s<br />\n", $Query_String); |
131
|
|
|
|
132
|
|
|
$debug_chrono_start = microtime(true); |
133
|
|
|
|
134
|
|
|
if ($arguments===false) { |
135
|
|
|
$this->pdo_query = $this->pdo_instance->query($Query_String); |
136
|
|
|
$exec_state = is_object($this->pdo_query); |
137
|
|
|
|
138
|
|
|
} else { |
139
|
|
|
|
140
|
|
|
$this->pdo_query = $this->pdo_instance->prepare($this->Query_String); |
141
|
|
|
$exec_state = ($arguments) ? $this->pdo_query->execute($arguments) |
142
|
|
|
: $this->pdo_query->execute(); |
143
|
|
|
// WARNING: this ternary is when we pass array() as $arguments |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$debug_chrono_start = (microtime(true) - $debug_chrono_start)*1000; |
147
|
|
|
$this->Row = 0; |
148
|
|
|
|
149
|
|
|
if ($exec_state == FALSE) { |
|
|
|
|
150
|
|
|
if (is_object($this->pdo_query)) { |
151
|
|
|
$this->Errno = $this->pdo_query->errorCode(); |
152
|
|
|
$this->Error = $this->pdo_query->errorInfo(); |
153
|
|
|
} else { |
154
|
|
|
$this->Errno = $this->pdo_instance->errorCode(); |
155
|
|
|
$this->Error = $this->pdo_instance->errorInfo(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if( defined("THROW_EXCEPTIONS") && THROW_EXCEPTIONS ){ |
159
|
|
|
throw new \Exception("Mysql query failed : $this->Error"); |
160
|
|
|
} |
161
|
|
|
$this->halt("SQL Error: ".$Query_String); |
162
|
|
|
return FALSE; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (isset($debug_alternc)) { |
166
|
|
|
$debug_alternc->add("SQL Query : (".substr($debug_chrono_start,0,5)." ms)\t $Query_String"); |
167
|
|
|
$debug_alternc->nb_sql_query++; |
168
|
|
|
$debug_alternc->tps_sql_query += $debug_chrono_start; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return TRUE; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* walk result set |
176
|
|
|
* |
177
|
|
|
* This function tests if a new record is available in the current |
178
|
|
|
* query result. |
179
|
|
|
* |
180
|
|
|
* @return TRUE if a new record is available |
181
|
|
|
*/ |
182
|
|
|
function next_record() { |
183
|
|
|
if (!$this->pdo_query) { |
184
|
|
|
$this->halt("next_record called with no query pending."); |
185
|
|
|
return FALSE; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$this->Record = $this->pdo_query->fetch(PDO::FETCH_BOTH); |
|
|
|
|
189
|
|
|
$this->Row++; |
190
|
|
|
$this->Errno = $this->pdo_query->errorCode(); |
191
|
|
|
$this->Error = $this->pdo_query->errorInfo(); |
192
|
|
|
|
193
|
|
|
if ($this->Record == FALSE) { |
194
|
|
|
if ($this->Auto_Free) |
195
|
|
|
$this->free(); |
196
|
|
|
return FALSE; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return TRUE; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/* public: table locking */ |
203
|
|
|
function lock($table, $mode="write") { |
204
|
|
|
if (!$this->is_connected()) |
205
|
|
|
return FALSE; |
206
|
|
|
|
207
|
|
|
$query="lock tables "; |
208
|
|
|
if (is_array($table)) { |
209
|
|
|
while (list($key,$value)=each($table)) { |
210
|
|
|
if ($key=="read" && $key!=0) { |
211
|
|
|
$query.="$value read, "; |
212
|
|
|
} else { |
213
|
|
|
$query.="$value $mode, "; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
$query=substr($query,0,-2); |
217
|
|
|
} else { |
218
|
|
|
$query.="$table $mode"; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
if (!$this->query($query)) { |
223
|
|
|
$this->halt("lock($table, $mode) failed."); |
224
|
|
|
return FALSE; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return TRUE; |
228
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
function unlock() { |
232
|
|
|
if (!$this->is_connected()) |
233
|
|
|
return FALSE; |
234
|
|
|
|
235
|
|
|
if (!$this->query('unlock tables')) { |
236
|
|
|
$this->halt("unlock() failed."); |
237
|
|
|
return FALSE; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
|
242
|
|
|
/* public: evaluate the result (size, width) */ |
243
|
|
|
function affected_rows() { |
244
|
|
|
return $this->pdo_query->rowCount(); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
function num_rows() { |
248
|
|
|
return $this->pdo_query->rowCount(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
function num_fields() { |
252
|
|
|
return $this->pdo_query->columnCount(); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/* public: shorthand notation */ |
256
|
|
|
function nf() { |
257
|
|
|
return $this->num_rows(); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
function np() { |
261
|
|
|
print $this->num_rows(); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param string $Name |
266
|
|
|
* @return integer |
267
|
|
|
*/ |
268
|
|
|
function f($Name) { |
269
|
|
|
if (isset($this->Record[$Name])) |
270
|
|
|
return $this->Record[$Name]; |
271
|
|
|
else |
272
|
|
|
return false; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
function current_record() { |
|
|
|
|
276
|
|
|
return $this->Record; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
function p($Name) { |
280
|
|
|
print $this->Record[$Name]; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
function lastid() { |
284
|
|
|
return $this->pdo_instance->lastInsertId(); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Escape a string to use it into a SQL PDO query |
289
|
|
|
* @param string string to escape |
290
|
|
|
* @return string escaped string |
291
|
|
|
*/ |
292
|
|
|
function quote($string) { |
|
|
|
|
293
|
|
|
return $this->pdo_instance->quote($string); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Execute a direct query, not getting any result back |
299
|
|
|
* @param query string query to execute |
300
|
|
|
* @return integer the number of affected rows |
301
|
|
|
*/ |
302
|
|
|
function exec($query) { |
|
|
|
|
303
|
|
|
return $this->pdo_instance->exec($query); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
|
307
|
|
|
/* public: sequence numbers */ |
308
|
|
|
function nextid($seq_name) { |
309
|
|
|
if (!$this->is_connected()) |
310
|
|
|
return FALSE; |
311
|
|
|
|
312
|
|
|
if ($this->lock($this->Seq_Table)) { |
313
|
|
|
/* get sequence number (locked) and increment */ |
314
|
|
|
$q = sprintf("select nextid from %s where seq_name = '%s'", |
315
|
|
|
$this->Seq_Table, |
316
|
|
|
$seq_name); |
317
|
|
|
$this->query($q); |
318
|
|
|
$this->next_record(); |
319
|
|
|
|
320
|
|
|
$id = $this->f('nextid'); |
321
|
|
|
|
322
|
|
|
/* No current value, make one */ |
323
|
|
|
if (!$id) { |
324
|
|
|
$currentid = 0; |
325
|
|
|
$q = sprintf("insert into %s values('%s', %s)", |
326
|
|
|
$this->Seq_Table, |
327
|
|
|
$seq_name, |
328
|
|
|
$currentid); |
329
|
|
|
$this->query($q); |
330
|
|
|
} else { |
331
|
|
|
$currentid = $id; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
$nextid = $currentid + 1; |
335
|
|
|
$q = sprintf("update %s set nextid = '%s' where seq_name = '%s'", |
336
|
|
|
$this->Seq_Table, |
337
|
|
|
$nextid, |
338
|
|
|
$seq_name); |
339
|
|
|
$this->query($q); |
340
|
|
|
$this->unlock(); |
341
|
|
|
} else { |
342
|
|
|
$this->halt("cannot lock ".$this->Seq_Table." - has it been created?"); |
343
|
|
|
return FALSE; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
return $nextid; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/* public: return table metadata */ |
350
|
|
|
function metadata($table='',$full=false) { |
|
|
|
|
351
|
|
|
global $err; |
352
|
|
|
$err->raise('Mysql', 'function is no longer implemented (metadata())'); |
353
|
|
|
return FALSE; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/* private: error handling */ |
357
|
|
|
function halt($msg) { |
358
|
|
|
if ($this->Halt_On_Error == "no") |
359
|
|
|
return; |
360
|
|
|
|
361
|
|
|
$this->haltmsg($msg); |
362
|
|
|
|
363
|
|
|
if ($this->Halt_On_Error != "report") |
364
|
|
|
die("Session halted."); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
function haltmsg($msg) { |
368
|
|
|
printf("</td></tr></table><b>Database error:</b> %s<br />\n", $msg); |
369
|
|
|
printf("<b>MySQL Error</b>: %s (%s)<br />\n", |
370
|
|
|
$this->Errno, |
371
|
|
|
implode("\n", $this->Error)); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
function table_names() { |
375
|
|
|
$this->query("SHOW TABLES"); |
376
|
|
|
$return = array(); |
377
|
|
|
while ($this->next_record()) |
378
|
|
|
$return[] = array('table_name' => $this->p(0), 'tablespace_name' => $this->Database, 'database' => $this->Database); |
379
|
|
|
|
380
|
|
|
return $return; |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
?> |
|
|
|
|
384
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.