-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.php
448 lines (415 loc) · 13.3 KB
/
Database.php
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
<?php
/**
* Database class
*
* A simple database wrapper, inspired by ADOdb.
*/
class Database {
/**
* Database connection credentials.
*/
private $host;
private $username;
private $password;
private $database;
private $port;
/**
* MySQL link identifier of open database connection.
* @var Resource.
*/
private $db = NULL;
/**
* Log of all executed queries.
* @var array
*/
public $queries = array();
/**
* Tracks nested transaction handling.
*/
private $transaction = FALSE;
/**
* Class constructor, which stores connection credentials.
*
* @param string $host
* @param string $username
* @param string $password
* @param string $database
* @param int $port
**/
public function __construct($host, $username, $password, $database, $port = NULL) {
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->database = $database;
$this->port = $port;
}
/**
* Connects to database.
*/
public function connect() {
$host = $this->port === NULL ? $this->host : "{$this->host}:{$this->port}";
$this->db = mysqli_connect($host, $this->username, $this->password, $this->database) or trigger_error("Could not connect to '{$host}'.", E_USER_ERROR);
}
/**
* Executes query.
*
* @param string $query The query string.
* @param mixed $vars Values to inject into $query.
*
* @return mixed Result set for some queries (SELECT, etc.), TRUE/FALSE for
* others (INSERT, UPDATE, etc.)
**/
public function &query($query, $vars = array()) {
// Connect if not already connected.
if ($this->db === NULL) {
$this->connect();
}
// Inject query vars.
$query = $this->replace($query, $vars);
// Execute query.
$results = mysqli_query($this->db, $query) or trigger_error('Query failed: ' . mysqli_error($this->db) . " (Query: $query)", E_USER_WARNING);
// Log the query.
$this->queries[] = $query;
return $results;
}
/**
* Safely injects query replacements.
*
* Query replacements can be named:
*
* <code>
* $query1 = $db->replace("SELECT * FROM users WHERE id = :id", array(':id' => 13));
* $query2 = $db->replace("SELECT * FROM users WHERE name = :name AND type IN :type", array(
* ':name' => 'John',
* ':type' => array('active', 'blocked'),
* ));
* </code>
*
* Or, they can be sequential question marks:
*
* <code>
* $query1 = $db->replace("SELECT * FROM table WHERE id = ?", 13);
* $query2 = $db->replace("SELECT * FROM table WHERE name = ? AND type IN ?", array(
* 'John',
* array('active', 'blocked'),
* ));
* </code>
*
* @param string $query The query string.
* @param mixed $vars Values to escape and inject into $query.
*
* @return string Query with injected replacements.
**/
public function replace($query, $vars = array()) {
// Skip if no vars were sent.
if (empty($vars)) {
return $query;
}
// Named substitutions.
if (strpos($query, ':')) {
$query = strtr($query, array_map(array($this, 'escape'), $vars));
}
// Sequential question-mark substitutions.
else if (strpos($query, '?')) {
$queryParts = explode('?', $query);
settype($vars, 'array');
if (count($queryParts) != count($vars) + 1) {
$array = !empty($vars) ? 'Input $vars array of ' . print_r($vars, 1) : 'Empty $vars array';
trigger_error("{$array} does not match number of ?'s in query '{$query}'", E_USER_ERROR);
}
$query = '';
for ($i = 0; $i < count($vars); $i++) {
$query .= $queryParts[$i] . $this->escape($vars[$i]);
}
$query .= $queryParts[$i];
}
return $query;
}
/**
* Saves $record to a $table record, either inserting or updating.
*
* This function helps avoid writing long queries for a common task - saving
* a record to the database. It follows several conventions:
*
* - If each $primaryKeys exists and is not empty in $record, and exists in
* $table, then it updates the record. Otherwise, it inserts the record.
* - Only those $record keys which correspond to actual column names in
* $table will be saved.
* - Any of $table's columns which are not in $record will remain untouched
* in the database record.
*
* As a convenience, when a record is inserted, each of the table's primary
* keys will be populated into the corresponding $record->$primaryKey.
*
* @param $table
* A string containing the table in which to save the record.
* @param &$record
* An object or array representing the record to write, passed in by
* reference, in which the keys correspond to the $table's column names.
* @param $where
* A string containing conditions used when updating; defaults to
* "id = $data[$primaryKey]" (for each primary key).
* @param $primaryKeys
* An array containing the primary keys. Defaults to primary keys in table.
*
* @return boolean TRUE if save was successful, FALSE otherwise.
**/
public function save($table, &$record, $where = NULL, $primaryKeys = array()) {
static $columns = array();
// Convert to object.
$object = (object) $record;
// Get column info about table.
if (!isset($columns[$table])) {
$columns[$table] = $this->getAll("SHOW COLUMNS FROM `{$table}`");
}
// Auto-determine primary keys (if not provided).
if (empty($primaryKeys)) {
foreach ($columns[$table] as $column) {
if ($column['Key'] == 'PRI') {
$primaryKeys[] = $column['Field'];
}
}
}
// Determine if inserting or updating.
$updating = TRUE;
$conditions = array();
// If any primary key is not set or is empty, then we're inserting.
foreach ($primaryKeys as $primaryKey) {
if (!isset($object->$primaryKey) || empty($object->$primaryKey)) {
$updating = FALSE;
break;
}
}
// If all primary keys have values, check if row exists.
if ($updating) {
foreach ($primaryKeys as $primaryKey) {
$conditions[] = "`{$primaryKey}` = " . $this->escape($object->$primaryKey);
}
if (!$this->getOne("SELECT COUNT(*) FROM `{$table}` WHERE " . implode(' AND ', $conditions))) {
$updating = FALSE;
}
}
// Prepare object based on table's columns.
$queryData = $queryFields = array();
foreach ($columns[$table] as $column) {
$field = $column['Field'];
// If column exists in object.
if (property_exists($object, $field)) {
// Updating.
if ($updating) {
if (!in_array($field, $primaryKeys)) {
$queryData[] = "`{$field}` = " . $this->escape($object->$field);
}
}
// Inserting.
else {
$queryFields[] = "`{$field}`";
$queryData[] = $this->escape($object->$field);
}
}
}
// Build query.
if ($updating) {
if ($where === NULL) {
foreach ($primaryKeys as $primaryKey) {
$conditions[] = "`{$primaryKey}` = " . $this->escape($object->$primaryKey);
}
$where = implode(' AND ', $conditions);
}
$query = sprintf("UPDATE `%s` SET %s WHERE %s LIMIT 1", $table, implode(', ', $queryData), $where);
}
else {
$query = sprintf("INSERT INTO `%s` (%s) VALUES (%s)", $table, implode(', ', $queryFields), implode(', ', $queryData));
}
// Execute query.
$success = $this->query($query);
// If successful, inserting a new record, there is only one primary key,
// and $object's $primaryKey is not set, then set $object's $primaryKey.
if ($success && !$updating && count($primaryKeys) == 1 && empty($object->$primaryKey)) {
$object->$primaryKey = mysqli_insert_id($this->db);
}
// If we began with an array, convert back.
if (is_array($record)) {
$record = (array) $object;
}
return $success;
}
/**
* Escapes a value to be injected into a query.
*
* @param mixed $value Value to escape.
* @param boolean $quote Unless FALSE, if $value is a string, the escaped
* value will be encapsulated in this value, or single quotes if TRUE.
*
* @return string The escaped value.
**/
public function escape($value, $quote = TRUE) {
// Connect if not already connected.
if ($this->db === NULL) {
$this->connect();
}
// For arrays, recursively escape and join in comma-separated list.
if (is_array($value)) {
return '(' . join(', ', array_map(array($this, 'escape'), $value, array(TRUE))) . ')';
}
// Booleans get converted to 1 or 0.
if (is_bool($value)) {
return $value ? 1 : 0;
}
// Numeric values remain as-is.
if (is_numeric($value)) {
return $value;
}
// Null values are converted to NULL string.
if (is_null($value)) {
return 'NULL';
}
// String values are escaped, and optionally wrapped in quotes.
$value = mysqli_real_escape_string($this->db, $value);
if ($quote !== FALSE) {
$separator = TRUE ? "'" : $quote;
$value = $separator . $value . $separator;
}
return $value;
}
/**
* Gets $column's enum options.
*
* @param string $table The table.
* @param string $column The table's column.
*
* @return array
**/
public function enum($table, $column) {
$info = $this->getRow("DESCRIBE `{$table}` `{$column}`");
return explode(',', preg_replace('/enum\(([^\)]+)\)/', '$1', str_replace("'", '', $info['Type'])));
}
/**
* Gets all query results.
*
* @param string $query The query string.
* @param mixed $vars Values inject into $query.
*
* @return array Associative array of results.
*/
public function getAll($query, $vars = array()) {
$result =& $this->query($query, $vars);
$rows = array();
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$rows[] = $row;
}
return $rows;
}
/**
* Gets first row of query results.
*
* "LIMIT 1" will be auto-appended to $query, if not already included, and if
* not a DESCRIBE query.
*
* @param string $query The query string.
* @param mixed $vars Values to inject into $query.
*
* @return array Associative array of first row of results.
*/
public function getRow($query, $vars = array()) {
if (!strpos($query, 'LIMIT 1') && substr($query, 0, 8) != 'DESCRIBE') {
$query .= ' LIMIT 1';
}
$result =& $this->query($query, $vars);
return mysqli_fetch_array($result, MYSQL_ASSOC);
}
/**
* Gets first column of first row of query results.
*
* "LIMIT 1" will be auto-appended to $query, if not already included.
*
* @param string $query The query string.
* @param mixed $vars Values to inject into $query.
*
* @return mixed The first column of the first row.
*/
public function getOne($query, $vars = FALSE) {
if (!strpos($query, 'LIMIT 1')) {
$query .= ' LIMIT 1';
}
$result =& $this->query($query, $vars);
$row = mysqli_fetch_array($result, MYSQL_NUM);
return $row[0];
}
/**
* Gets first column of all query results.
*
* @param string $query The query string.
* @param mixed $vars Values to inject into $query.
*
* @return array Array of first column of all rows.
*/
public function getCol($query, $vars = FALSE) {
$result =& $this->query($query, $vars);
$rows = array();
while ($row = mysqli_fetch_array($result, MYSQL_NUM)) {
$rows[] = $row[0];
}
return $rows;
}
/**
* Generates a key => value list from query results.
*
* This function will create an associative array where the first column is
* the key, and the second column is the value.
*
* <code>
* $list1 = $db->getList("SELECT id, name FROM users WHERE id < ?", 10);
* $list2 = $db->getList("SELECT id, first_name, last_name FROM users", FALSE, ' ');
* $list3 = $db->getList("SELECT id, first_name, last_name, email FROM users", FALSE, TRUE);
* </code>
*
* @param string $query The query string.
* @param mixed $vars Values to inject into $query.
* @param mixed $join If TRUE, remaining columns will be an associative
* array. If a string, that string will be used to join all remaining
* columns.
*
* @return array Array of key => values.
*/
public function getList($query, $vars = array(), $join = FALSE) {
$results =& $this->query($query, $vars);
$list = array();
if ($join === TRUE) {
while ($row = mysqli_fetch_array($results, MYSQL_ASSOC)) {
$list[reset($row)] = $row;
}
}
else {
while ($row = mysqli_fetch_array($results, MYSQL_NUM)) {
$value = is_string($join) ? implode($join, array_slice($row, 1)) : $row[1];
$list[$row[0]] = $value;
}
}
return $list;
}
/**
* Tracks database transactions, gracefuly handling nested transactions.
*
* @param string $action The action to take, either "start", "rollback", or
* "commit".
*/
public function transaction($action = 'start') {
switch ($action) {
case 'start':
if (!$this->transaction) {
$this->query('START TRANSACTION');
$this->transaction = TRUE;
}
return;
case 'rollback':
$this->query("ROLLBACK");
$this->transaction = FALSE;
return;
case 'commit':
$this->query("COMMIT");
$this->transaction = FALSE;
return;
}
}
}