-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.class.php
More file actions
369 lines (332 loc) · 9.45 KB
/
db.class.php
File metadata and controls
369 lines (332 loc) · 9.45 KB
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
<?php
/**
* @file
* A minimal extension for PHP's PDO class designed to make running SQL
* statements easier.
*
* Project Overview
*
* This project provides a minimal extension for PHP's PDO (PHP Data Objects)
* class designed for ease-of-use and saving development time/effort. This is
* achieved by providing methods - SELECT, INSERT, UPDATE, DELETE - for quickly
* building common SQL statements, handling exceptions when SQL errors are
* produced, and automatically returning results/number of affected rows for
* the appropriate SQL statement types.
*
* System Requirements:
* - PHP 5
* - PDO Extension
* - Appropriate PDO Driver(s) - PDO_SQLITE, PDO_MYSQL, PDO_PGSQL
* - Only MySQL, SQLite, and PostgreSQL database types are currently supported.
*/
/**
* Class db.
*/
class db extends PDO {
private $error;
private $sql;
private $bind;
private $errorCallbackFunction;
private $tablePrefix;
/**
* Class constructor.
*
* @param string $dsn
* More information can be found on how to set the dsn parameter by following
* the links provided below.
*
* - MySQL - http://us3.php.net/manual/en/ref.pdo-mysql.connection.php
* - SQLite - http://us3.php.net/manual/en/ref.pdo-sqlite.connection.php
* - PostreSQL - http://us3.php.net/manual/en/ref.pdo-pgsql.connection.php
*
* @param string $user
* Username for database connection.
*
* @param string $passwd
* Password for database connection.
*
* @param string $prefix
* Prefix for database tables.
*/
public function __construct($dsn = '', $user = '', $passwd = '', $prefix = '') {
$options = array(
PDO::ATTR_PERSISTENT => TRUE,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
try {
parent::__construct($dsn, $user, $passwd, $options);
}
catch (PDOException $e) {
$this->error = $e->getMessage();
}
$this->tablePrefix = $prefix;
}
/**
* SELECT statement.
*
* @param string $table
* Table name.
*
* @param string $where
* WHERE conditions.
*
* @param mixed $bind
* Bind parameters as string or array.
*
* @param string $fields
* Comma separated field names.
*
* @return array|bool|int
* If no SQL errors are produced, this method will return the number of rows
* affected by the SELECT statement.
*/
public function select($table = '', $where = '', $bind = '', $fields = '*') {
$table = $this->tablePrefix . $table;
$sql = "SELECT " . $fields . " FROM `" . $table . "`";
if (!empty($where)) {
$sql .= " WHERE " . $where;
}
$sql .= ";";
return $this->run($sql, $bind);
}
/**
* INSERT statement.
*
* @param string $table
* Table name.
*
* @param array $info
* Associative array with field names and values.
*
* @return array|bool|int
* If no SQL errors are produced, this method will return with the the last
* inserted ID. Otherwise 0.
*/
public function insert($table = '', $info = array()) {
$table = $this->tablePrefix . $table;
$fields = $this->filter($table, $info);
$sql = "INSERT INTO " . $table . " (`" . implode($fields, "`, `") . "`) ";
$sql .= "VALUES (:" . implode($fields, ", :") . ");";
$bind = array();
foreach ($fields as $field) {
$bind[":$field"] = $info[$field];
}
return $this->run($sql, $bind);
}
/**
* UPDATE statement.
*
* @param string $table
* Table name.
*
* @param array $info
* Associated array with fields and their values.
*
* @param string $where
* WHERE conditions.
*
* @param mixed $bind
* Bind parameters as string or array.
*
* @return array|bool|int
* If no SQL errors are produced, this method will return the number of rows
* affected by the UPDATE statement.
*/
public function update($table = '', $info = array(), $where = '', $bind = '') {
$table = $this->tablePrefix . $table;
$fields = $this->filter($table, $info);
$fieldSize = sizeof($fields);
$sql = "UPDATE " . $table . " SET ";
for ($f = 0; $f < $fieldSize; ++$f) {
if ($f > 0) {
$sql .= ", ";
}
$sql .= $fields[$f] . " = :update_" . $fields[$f];
}
$sql .= " WHERE " . $where . ";";
$bind = $this->cleanup($bind);
foreach ($fields as $field) {
$bind[":update_$field"] = $info[$field];
}
return $this->run($sql, $bind);
}
/**
* DELETE statement.
*
* @param string $table
* Table name.
*
* @param string $where
* Where conditions.
*
* @param mixed $bind
* Bind parameters as string or array.
*
* @return array
* If no SQL errors are produced, this method will return the number of rows
* affected by the DELETE statement.
*/
public function delete($table = '', $where = '', $bind = '') {
$table = $this->tablePrefix . $table;
$sql = "DELETE FROM " . $table . " WHERE " . $where . ";";
$this->run($sql, $bind);
}
/**
* This method is used to run free-form SQL statements that can't be handled
* by the included delete, insert, select, or update methods.
*
* @param string $sql
* SQL query.
*
* @param mixed $bind
* Bind parameters as string or array.
*
* @return array|bool|int
* If no SQL errors are produced, this method will return the number of
* affected rows for DELETE and UPDATE statements, the last inserted ID for
* INSERT statement, or an associate array of results for SELECT, DESCRIBE,
* and PRAGMA statements. Otherwise FALSE.
*/
public function run($sql = '', $bind = '') {
$this->sql = trim($sql);
$this->bind = $this->cleanup($bind);
$this->error = '';
try {
$pdostmt = $this->prepare($this->sql);
if ($pdostmt->execute($this->bind) !== FALSE) {
if (preg_match("/^(" . implode("|", array(
"select",
"describe",
"pragma"
)) . ") /i", $this->sql)) {
return $pdostmt->fetchAll(PDO::FETCH_ASSOC);
}
elseif (preg_match("/^(" . implode("|", array(
"delete",
"update"
)) . ") /i", $this->sql)) {
return $pdostmt->rowCount();
}
elseif (preg_match("/^(" . implode("|", array(
"insert",
)) . ") /i", $this->sql)) {
return $this->lastInsertId();
}
}
}
catch (PDOException $e) {
$this->error = $e->getMessage();
$this->debug();
return FALSE;
}
return FALSE;
}
/**
* When a SQL error occurs, this project will send an error message to a
* callback function specified through the setErrorCallbackFunction method.
* The callback function's name should be supplied as a string without
* parenthesis.
*
* If no SQL errors are produced, this method will return an associative
* array of results.
*
* @param $errorCallbackFunction
* Callback function.
*/
public function setErrorCallbackFunction($errorCallbackFunction) {
// Variable functions for won't work with language constructs such as echo
// and print, so these are replaced with print_r.
if (in_array(strtolower($errorCallbackFunction), array("echo", "print"))) {
$errorCallbackFunction = "print_r";
}
if (function_exists($errorCallbackFunction)) {
$this->errorCallbackFunction = $errorCallbackFunction;
}
}
/**
* Debug.
*/
private function debug() {
if (!empty($this->errorCallbackFunction)) {
$error = array(
'Error' => $this->error,
);
if (!empty($this->sql)) {
$error['SQL Statement'] = $this->sql;
}
if (!empty($this->bind)) {
$error['Bind Parameters'] = trim(print_r($this->bind, TRUE));
}
$backtrace = debug_backtrace();
if (!empty($backtrace)) {
foreach ($backtrace as $info) {
if ($info['file'] != __FILE__) {
$error['Backtrace'] = $info['file'] . ' at line ' . $info['line'];
}
}
}
$msg = 'SQL Error' . PHP_EOL . str_repeat('-', 50);
foreach ($error as $key => $val) {
$msg .= PHP_EOL . PHP_EOL . $key . ':' . PHP_EOL . $val;
}
$func = $this->errorCallbackFunction;
$func($msg);
}
}
/**
* Filter.
*
* @param string $table
* Table name.
*
* @param array $info
* Associated array with fields and their values.
*
* @return array
*/
private function filter($table = '', $info = array()) {
$table = $this->tablePrefix . $table;
$driver = $this->getAttribute(PDO::ATTR_DRIVER_NAME);
if ($driver == 'sqlite') {
$sql = "PRAGMA table_info('" . $table . "');";
$key = "name";
}
elseif ($driver == 'mysql') {
$sql = "DESCRIBE `" . $table . "`;";
$key = "Field";
}
else {
$sql = "SELECT column_name FROM information_schema.columns ";
$sql .= "WHERE table_name = '" . $table . "';";
$key = "column_name";
}
if (FALSE !== ($list = $this->run($sql))) {
$fields = array();
foreach ($list as $record) {
$fields[] = $record[$key];
}
return array_values(array_intersect($fields, array_keys($info)));
}
return array();
}
/**
* Cleanup parameters.
*
* @param mixed $bind
* Bind parameters as string/array.
*
* @return array
* Bind parameters as array.
*/
private function cleanup($bind = '') {
if (!is_array($bind)) {
if (!empty($bind)) {
$bind = array($bind);
}
else {
$bind = array();
}
}
return $bind;
}
}