-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbAccess.php
455 lines (410 loc) · 9.91 KB
/
dbAccess.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
449
450
451
452
453
454
455
<?php
/**
* @name dbAccess
* @package IntelliRadio.dbAccess
* @authors Ramindu Deshapriya
*
* @desc Class to handle database connections
*/
class dbAccess {
protected static $dParams = array(
'host' => 'localhost',
'user' => 'snifbiz1_admin',
'password' => 'AlphA1694',
'database' => 'snifbiz1_snifferbot'
);
protected $_connection = null;
/**
* The database driver name
*
* @var string
*/
protected $_sql = null;
/**
* The null/zero date string
*
* @var string
*/
protected $_nullDate = '0000-00-00 00:00:00';
/**
* Quote for named objects
*
* @var string
*/
protected $_nameQuote = '`';
protected static $dbInstance;
final private function __construct($host,$user,$password,$db) {
if ( !($this->_connection = @mysql_connect($host,$user,$password,true)) ) {
die('Unable to connect to database, recheck your DB server');
}
mysql_select_db($db, $this->_connection);
return $this;
}
/**
* Function to return instance of dbAccess class
* @return dbAccess singleton object
*/
final public static function getInstance() {
if ( is_null(dbAccess::$dbInstance) ) {
dbAccess::$dbInstance = new dbAccess( dbAccess::$dParams['host'],
dbAccess::$dParams['user'],
dbAccess::$dParams['password'],
dbAccess::$dParams['database']
);
}
return dbAccess::$dbInstance;
}
public function setQuery($query, $offset = 0, $limit = 0)
{
$this->_sql = $query;
$this->_limit = (int) $limit;
$this->_offset = (int) $offset;
return $this;
}
public function query()
{
if (!is_resource($this->_connection)) {
echo 'DB Connection not available';
return false;
}
// Take a local copy so that we don't modify the original query and cause issues later
$sql = (string) $this->_sql;
if ($this->_limit > 0 || $this->_offset > 0) {
$sql .= ' LIMIT '.$this->_offset.', '.$this->_limit;
}
$this->_errorNum = 0;
$this->_errorMsg = '';
$this->_cursor = mysql_query($sql, $this->_connection);
if (!$this->_cursor) {
$this->_errorNum = mysql_errno($this->_connection);
$this->_errorMsg = mysql_error($this->_connection)." SQL=$sql";
return false;
}
return $this->_cursor;
}
/**
* Description
*
* @access public
*/
function insertid()
{
return mysql_insert_id( $this->_connection);
}
/**
* Get a database escaped string
*
* @param string The string to be escaped
* @param boolean Optional parameter to provide extra escaping
* @return string
* @access public
* @abstract
*/
function getEscaped( $text, $extra = false )
{
$result = mysql_real_escape_string( $text, $this->_connection );
if ($extra) {
$result = addcslashes( $result, '%_' );
}
return $result;
}
/**
* Get a quoted database escaped string
*
* @param string A string
* @param boolean Default true to escape string, false to leave the string unchanged
* @return string
* @access public
*/
function Quote( $text, $escaped = true )
{
return '\''.($escaped ? $this->getEscaped( $text ) : $text).'\'';
}
public function nameQuote($s)
{
$q = $this->_nameQuote;
if (strlen($q) == 1) {
return $q.$s.$q;
} else {
return $q{0}.$s.$q{1};
}
}
public function isQuoted($fieldName)
{
if ($this->_hasQuoted) {
return in_array($fieldName, $this->_quoted);
} else {
return true;
}
}
/**
* This method loads the first field of the first row returned by the query.
*
* @return mixed The value returned in the query or null if the query failed.
*/
public function loadResult()
{
if (!($cur = $this->query())) {
return null;
}
$ret = null;
if ($row = mysql_fetch_row($cur)) {
$ret = $row[0];
}
mysql_free_result($cur);
return $ret;
}
/**
* Load an array of single field results into an array
*/
public function loadResultArray($numinarray = 0)
{
if (!($cur = $this->query())) {
return null;
}
$array = array();
while ($row = mysql_fetch_row($cur)) {
$array[] = $row[$numinarray];
}
mysql_free_result($cur);
return $array;
}
/**
* Fetch a result row as an associative array
*
* @return array
*/
public function loadAssoc()
{
if (!($cur = $this->query())) {
return null;
}
$ret = null;
if ($array = mysql_fetch_assoc($cur)) {
$ret = $array;
}
mysql_free_result($cur);
return $ret;
}
/**
* Load a assoc list of database rows.
*
* @param string The field name of a primary key.
* @param string An optional column name. Instead of the whole row, only this column value will be in the return array.
* @return array If <var>key</var> is empty as sequential list of returned records.
*/
public function loadAssocList($key = null, $column = null)
{
if (!($cur = $this->query())) {
print $this->query();
return null;
}
$array = array();
while ($row = mysql_fetch_assoc($cur)) {
$value = ($column) ? (isset($row[$column]) ? $row[$column] : $row) : $row;
if ($key) {
$array[$row[$key]] = $value;
} else {
$array[] = $value;
}
}
mysql_free_result($cur);
return $array;
}
/**
* This global function loads the first row of a query into an object.
*
* @param string The name of the class to return (stdClass by default).
*
* @return object
*/
public function loadObject($className = 'stdClass')
{
if (!($cur = $this->query())) {
return null;
}
$ret = null;
if ($object = mysql_fetch_object($cur, $className)) {
$ret = $object;
}
mysql_free_result($cur);
return $ret;
}
/**
* Load a list of database objects
*
* If <var>key</var> is not empty then the returned array is indexed by the value
* the database key. Returns <var>null</var> if the query fails.
*
* @param string The field name of a primary key
* @param string The name of the class to return (stdClass by default).
*
* @return array If <var>key</var> is empty as sequential list of returned records.
*/
public function loadObjectList($key='', $className = 'stdClass')
{
if (!($cur = $this->query())) {
return null;
}
$array = array();
while ($row = mysql_fetch_object($cur, $className)) {
if ($key) {
$array[$row->$key] = $row;
} else {
$array[] = $row;
}
}
mysql_free_result($cur);
return $array;
}
/**
* Description
*
* @return The first row of the query.
*/
public function loadRow()
{
if (!($cur = $this->query())) {
return null;
}
$ret = null;
if ($row = mysql_fetch_row($cur)) {
$ret = $row;
}
mysql_free_result($cur);
return $ret;
}
/**
* Load a list of database rows (numeric column indexing)
*
* @param string The field name of a primary key
* @return array If <var>key</var> is empty as sequential list of returned records.
* If <var>key</var> is not empty then the returned array is indexed by the value
* the database key. Returns <var>null</var> if the query fails.
*/
public function loadRowList($key=null)
{
if (!($cur = $this->query())) {
return null;
}
$array = array();
while ($row = mysql_fetch_row($cur)) {
if ($key !== null) {
$array[$row[$key]] = $row;
} else {
$array[] = $row;
}
}
mysql_free_result($cur);
return $array;
}
/**
* Load the next row returned by the query.
*
* @return mixed The result of the query as an array, false if there are no more rows, or null on an error.
*
* @since 1.6.0
*/
public function loadNextRow()
{
static $cur;
if (!($cur = $this->query())) {
return $this->_errorNum ? null : false;
}
if ($row = mysql_fetch_row($cur)) {
return $row;
}
mysql_free_result($cur);
$cur = null;
return false;
}
/**
* Load the next row returned by the query.
*
* @param string The name of the class to return (stdClass by default).
*
* @return mixed The result of the query as an object, false if there are no more rows, or null on an error.
*
* @since 1.6.0
*/
public function loadNextObject($className = 'stdClass')
{
static $cur;
if (!($cur = $this->query())) {
return $this->_errorNum ? null : false;
}
if ($row = mysql_fetch_object($cur, $className)) {
return $row;
}
mysql_free_result($cur);
$cur = null;
return false;
}
/**
* Inserts a row into a table based on an objects properties
*
* @param string The name of the table
* @param object An object whose properties match table fields
* @param string The name of the primary key. If provided the object property is updated.
*/
public function insertObject($table, &$object, $keyName = NULL)
{
$fmtsql = 'INSERT INTO '.$this->nameQuote($table).' (%s) VALUES (%s) ';
$fields = array();
foreach (get_object_vars($object) as $k => $v) {
if (is_array($v) or is_object($v) or $v === NULL) {
continue;
}
if ($k[0] == '_') { // internal field
continue;
}
$fields[] = $this->nameQuote($k);
$values[] = $this->isQuoted($k) ? $this->Quote($v) : (int) $v;
}
$this->setQuery(sprintf($fmtsql, implode(",", $fields) , implode(",", $values)));
if (!$this->query()) {
return false;
}
$id = $this->insertid();
if ($keyName && $id) {
$object->$keyName = $id;
}
return true;
}
/**
* Description
*
* @param [type] $updateNulls
*/
public function updateObject($table, &$object, $keyName, $updateNulls=false)
{
$fmtsql = 'UPDATE '.$this->nameQuote($table).' SET %s WHERE %s';
$tmp = array();
foreach (get_object_vars($object) as $k => $v) {
if (is_array($v) or is_object($v) or $k[0] == '_') { // internal or NA field
continue;
}
if ($k == $keyName) {
// PK not to be updated
$where = $keyName . '=' . $this->Quote($v);
continue;
}
if ($v === null) {
if ($updateNulls) {
$val = 'NULL';
} else {
continue;
}
} else {
$val = $this->isQuoted($k) ? $this->Quote($v) : (int) $v;
}
$tmp[] = $this->nameQuote($k) . '=' . $val;
}
// Nothing to update.
if (empty($tmp)) {
return true;
}
$this->setQuery(sprintf($fmtsql, implode(",", $tmp) , $where));
return $this->query();
}
}