Passed
Push — 1.0.0-dev ( 7e13dc...c7a39c )
by nguereza
06:11
created
core/classes/Database.php 1 patch
Indentation   +1236 added lines, -1236 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-    defined('ROOT_PATH') || exit('Access denied');
2
+	defined('ROOT_PATH') || exit('Access denied');
3 3
   /**
4 4
    * TNH Framework
5 5
    *
@@ -22,188 +22,188 @@  discard block
 block discarded – undo
22 22
    * You should have received a copy of the GNU General Public License
23 23
    * along with this program; if not, write to the Free Software
24 24
    * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
-  */
25
+   */
26 26
   class Database{
27 27
 	
28 28
 	/**
29 29
 	 * The PDO instance
30 30
 	 * @var object
31
-	*/
32
-    private $pdo                 = null;
31
+	 */
32
+	private $pdo                 = null;
33 33
     
34 34
 	/**
35 35
 	 * The database name used for the application
36 36
 	 * @var string
37
-	*/
37
+	 */
38 38
 	private $databaseName        = null;
39 39
     
40 40
 	/**
41 41
 	 * The SQL SELECT statment
42 42
 	 * @var string
43
-	*/
43
+	 */
44 44
 	private $select              = '*';
45 45
 	
46 46
 	/**
47 47
 	 * The SQL FROM statment
48 48
 	 * @var string
49
-	*/
50
-    private $from                = null;
49
+	 */
50
+	private $from                = null;
51 51
 	
52 52
 	/**
53 53
 	 * The SQL WHERE statment
54 54
 	 * @var string
55
-	*/
56
-    private $where               = null;
55
+	 */
56
+	private $where               = null;
57 57
 	
58 58
 	/**
59 59
 	 * The SQL LIMIT statment
60 60
 	 * @var string
61
-	*/
62
-    private $limit               = null;
61
+	 */
62
+	private $limit               = null;
63 63
 	
64 64
 	/**
65 65
 	 * The SQL JOIN statment
66 66
 	 * @var string
67
-	*/
68
-    private $join                = null;
67
+	 */
68
+	private $join                = null;
69 69
 	
70 70
 	/**
71 71
 	 * The SQL ORDER BY statment
72 72
 	 * @var string
73
-	*/
74
-    private $orderBy             = null;
73
+	 */
74
+	private $orderBy             = null;
75 75
 	
76 76
 	/**
77 77
 	 * The SQL GROUP BY statment
78 78
 	 * @var string
79
-	*/
80
-    private $groupBy             = null;
79
+	 */
80
+	private $groupBy             = null;
81 81
 	
82 82
 	/**
83 83
 	 * The SQL HAVING statment
84 84
 	 * @var string
85
-	*/
86
-    private $having              = null;
85
+	 */
86
+	private $having              = null;
87 87
 	
88 88
 	/**
89 89
 	 * The number of rows returned by the last query
90 90
 	 * @var int
91
-	*/
92
-    private $numRows             = 0;
91
+	 */
92
+	private $numRows             = 0;
93 93
 	
94 94
 	/**
95 95
 	 * The last insert id for the primary key column that have auto increment or sequence
96 96
 	 * @var mixed
97
-	*/
98
-    private $insertId            = null;
97
+	 */
98
+	private $insertId            = null;
99 99
 	
100 100
 	/**
101 101
 	 * The full SQL query statment after build for each command
102 102
 	 * @var string
103
-	*/
104
-    private $query               = null;
103
+	 */
104
+	private $query               = null;
105 105
 	
106 106
 	/**
107 107
 	 * The error returned for the last query
108 108
 	 * @var string
109
-	*/
110
-    private $error               = null;
109
+	 */
110
+	private $error               = null;
111 111
 	
112 112
 	/**
113 113
 	 * The result returned for the last query
114 114
 	 * @var mixed
115
-	*/
116
-    private $result              = array();
115
+	 */
116
+	private $result              = array();
117 117
 	
118 118
 	/**
119 119
 	 * The prefix used in each database table
120 120
 	 * @var string
121
-	*/
122
-    private $prefix              = null;
121
+	 */
122
+	private $prefix              = null;
123 123
 	
124 124
 	/**
125 125
 	 * The list of SQL valid operators
126 126
 	 * @var array
127
-	*/
128
-    private $operatorList        = array('=','!=','<','>','<=','>=','<>');
127
+	 */
128
+	private $operatorList        = array('=','!=','<','>','<=','>=','<>');
129 129
     
130 130
 	/**
131 131
 	 * The cache default time to live in second. 0 means no need to use the cache feature
132 132
 	 * @var int
133
-	*/
133
+	 */
134 134
 	private $cacheTtl              = 0;
135 135
 	
136 136
 	/**
137 137
 	 * The cache current time to live. 0 means no need to use the cache feature
138 138
 	 * @var int
139
-	*/
140
-    private $temporaryCacheTtl   = 0;
139
+	 */
140
+	private $temporaryCacheTtl   = 0;
141 141
 	
142 142
 	/**
143 143
 	 * The number of executed query for the current request
144 144
 	 * @var int
145
-	*/
146
-    private $queryCount          = 0;
145
+	 */
146
+	private $queryCount          = 0;
147 147
 	
148 148
 	/**
149 149
 	 * The default data to be used in the statments query INSERT, UPDATE
150 150
 	 * @var array
151
-	*/
152
-    private $data                = array();
151
+	 */
152
+	private $data                = array();
153 153
 	
154 154
 	/**
155 155
 	 * The database configuration
156 156
 	 * @var array
157
-	*/
158
-    private $config              = array();
157
+	 */
158
+	private $config              = array();
159 159
 	
160 160
 	/**
161 161
 	 * The logger instance
162 162
 	 * @var Log
163 163
 	 */
164
-    private $logger              = null;
165
-
166
-
167
-    /**
168
-    * The cache instance
169
-    * @var CacheInterface
170
-    */
171
-    private $cacheInstance       = null;
172
-
173
-     /**
174
-    * The benchmark instance
175
-    * @var Benchmark
176
-    */
177
-    private $benchmarkInstance   = null;
178
-
179
-
180
-    /**
181
-     * Construct new database
182
-     * @param array $overwriteConfig the config to overwrite with the config set in database.php
183
-     * @param object $logger the log instance
184
-     */
185
-    public function __construct($overwriteConfig = array(), Log $logger = null){
186
-        /**
187
-         * instance of the Log class
188
-         */
189
-        if(is_object($logger)){
190
-          $this->logger = $logger;
191
-        }
192
-        else{
193
-            $this->logger =& class_loader('Log', 'classes');
194
-            $this->logger->setLogger('Library::Database');
195
-        }
196
-
197
-        $db = array();
198
-      	if(file_exists(CONFIG_PATH . 'database.php')){
199
-          //here don't use require_once because somewhere user can create database instance directly
200
-      	  require CONFIG_PATH . 'database.php';
201
-        }
164
+	private $logger              = null;
165
+
166
+
167
+	/**
168
+	 * The cache instance
169
+	 * @var CacheInterface
170
+	 */
171
+	private $cacheInstance       = null;
172
+
173
+	 /**
174
+	  * The benchmark instance
175
+	  * @var Benchmark
176
+	  */
177
+	private $benchmarkInstance   = null;
178
+
179
+
180
+	/**
181
+	 * Construct new database
182
+	 * @param array $overwriteConfig the config to overwrite with the config set in database.php
183
+	 * @param object $logger the log instance
184
+	 */
185
+	public function __construct($overwriteConfig = array(), Log $logger = null){
186
+		/**
187
+		 * instance of the Log class
188
+		 */
189
+		if(is_object($logger)){
190
+		  $this->logger = $logger;
191
+		}
192
+		else{
193
+			$this->logger =& class_loader('Log', 'classes');
194
+			$this->logger->setLogger('Library::Database');
195
+		}
196
+
197
+		$db = array();
198
+	  	if(file_exists(CONFIG_PATH . 'database.php')){
199
+		  //here don't use require_once because somewhere user can create database instance directly
200
+	  	  require CONFIG_PATH . 'database.php';
201
+		}
202 202
           
203 203
 				if(! empty($overwriteConfig)){
204 204
 				  $db = array_merge($db, $overwriteConfig);
205 205
 				}
206
-        $config = array();
206
+		$config = array();
207 207
 				$config['driver']    = isset($db['driver']) ? $db['driver'] : 'mysql';
208 208
 				$config['username']  = isset($db['username']) ? $db['username'] : 'root';
209 209
 				$config['password']  = isset($db['password']) ? $db['password'] : '';
@@ -212,1029 +212,1029 @@  discard block
 block discarded – undo
212 212
 				$config['charset']   = isset($db['charset']) ? $db['charset'] : 'utf8';
213 213
 				$config['collation'] = isset($db['collation']) ? $db['collation'] : 'utf8_general_ci';
214 214
 				$config['prefix']    = isset($db['prefix']) ? $db['prefix'] : '';
215
-        $port = '';
216
-        if(strstr($config['hostname'], ':')){
217
-          $p = explode(':', $config['hostname']);
218
-          $port = isset($p[1]) ? $p[1] : '';
219
-          $config['hostname'] = isset($p[0]) ? $p[0] : '';
220
-        }
215
+		$port = '';
216
+		if(strstr($config['hostname'], ':')){
217
+		  $p = explode(':', $config['hostname']);
218
+		  $port = isset($p[1]) ? $p[1] : '';
219
+		  $config['hostname'] = isset($p[0]) ? $p[0] : '';
220
+		}
221 221
 				$config['port']      = $port;
222 222
 				
223 223
 		  	$this->setDatabaseConfiguration($config);
224
-    		$this->temporaryCacheTtl = $this->cacheTtl;
225
-    }
226
-
227
-    /**
228
-     * This is used to connect to database
229
-     * @return bool 
230
-     */
231
-    public function connect(){
232
-      $config = $this->getDatabaseConfiguration();
233
-      if(! empty($config)){
234
-        try{
235
-            $driverDsnMap = array(
236
-              'mysql' => 'mysql:host=' . $config['hostname'] . ';' 
237
-                          . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') 
238
-                          . 'dbname=' . $config['database'],
239
-              'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' 
240
-                          . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '')
241
-                          . 'dbname=' . $config['database'],
242
-              'sqlite' => 'sqlite:' . $config['database'],
243
-              'oracle' => 'oci:dbname=' . $config['hostname'] 
244
-                          . (($config['port']) != '' ? ':' . $config['port'] : '')
245
-                          . '/' . $config['database']
246
-            );
247
-            $dsn = isset($driverDsnMap[$config['driver']]) ? $driverDsnMap[$config['driver']] : '';
224
+			$this->temporaryCacheTtl = $this->cacheTtl;
225
+	}
226
+
227
+	/**
228
+	 * This is used to connect to database
229
+	 * @return bool 
230
+	 */
231
+	public function connect(){
232
+	  $config = $this->getDatabaseConfiguration();
233
+	  if(! empty($config)){
234
+		try{
235
+			$driverDsnMap = array(
236
+			  'mysql' => 'mysql:host=' . $config['hostname'] . ';' 
237
+						  . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '') 
238
+						  . 'dbname=' . $config['database'],
239
+			  'pgsql' => 'pgsql:host=' . $config['hostname'] . ';' 
240
+						  . (($config['port']) != '' ? 'port=' . $config['port'] . ';' : '')
241
+						  . 'dbname=' . $config['database'],
242
+			  'sqlite' => 'sqlite:' . $config['database'],
243
+			  'oracle' => 'oci:dbname=' . $config['hostname'] 
244
+						  . (($config['port']) != '' ? ':' . $config['port'] : '')
245
+						  . '/' . $config['database']
246
+			);
247
+			$dsn = isset($driverDsnMap[$config['driver']]) ? $driverDsnMap[$config['driver']] : '';
248 248
             
249
-            $this->pdo = new PDO($dsn, $config['username'], $config['password']);
250
-            $this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'");
251
-            $this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'");
252
-            $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
253
-            return true;
254
-          }
255
-          catch (PDOException $e){
256
-            $this->logger->fatal($e->getMessage());
257
-            show_error('Cannot connect to Database.');
258
-            return false;
259
-          }
260
-      }
261
-      else{
262
-        show_error('Database configuration is not set.');
263
-        return false;
264
-      }
265
-    }
266
-
267
-    /**
268
-     * Set the SQL FROM statment
269
-     * @param  string|array $table the table name or array of table list
270
-     * @return object        the current Database instance
271
-     */
272
-    public function from($table){
273
-      if(is_array($table)){
274
-        $froms = '';
275
-        foreach($table as $key){
276
-          $froms .= $this->prefix . $key . ', ';
277
-        }
278
-        $this->from = rtrim($froms, ', ');
279
-      }
280
-      else{
281
-        $this->from = $this->prefix . $table;
282
-      }
283
-      return $this;
284
-    }
285
-
286
-    /**
287
-     * Set the SQL SELECT statment
288
-     * @param  string|array $fields the field name or array of field list
289
-     * @return object        the current Database instance
290
-     */
291
-    public function select($fields){
292
-      $select = (is_array($fields) ? implode(', ', $fields) : $fields);
293
-      $this->select = ($this->select == '*' ? $select : $this->select . ', ' . $select);
294
-      return $this;
295
-    }
296
-
297
-    /**
298
-     * Set the SQL SELECT DISTINCT statment
299
-     * @param  string $field the field name to distinct
300
-     * @return object        the current Database instance
301
-     */
302
-    public function distinct($field){
303
-      $distinct = ' DISTINCT ' . $field;
304
-      $this->select = ($this->select == '*' ? $distinct : $this->select . ', ' . $distinct);
305
-
306
-      return $this;
307
-    }
308
-
309
-    /**
310
-     * Set the SQL function MAX in SELECT statment
311
-     * @param  string $field the field name
312
-     * @param  string $name  if is not null represent the alias used for this field in the result
313
-     * @return object        the current Database instance
314
-     */
315
-    public function max($field, $name = null){
316
-      $func = 'MAX(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : '');
317
-      $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func);
318
-      return $this;
319
-    }
320
-
321
-    /**
322
-     * Set the SQL function MIN in SELECT statment
323
-     * @param  string $field the field name
324
-     * @param  string $name  if is not null represent the alias used for this field in the result
325
-     * @return object        the current Database instance
326
-     */
327
-    public function min($field, $name = null){
328
-      $func = 'MIN(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : '');
329
-      $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func);
330
-      return $this;
331
-    }
332
-
333
-    /**
334
-     * Set the SQL function SUM in SELECT statment
335
-     * @param  string $field the field name
336
-     * @param  string $name  if is not null represent the alias used for this field in the result
337
-     * @return object        the current Database instance
338
-     */
339
-    public function sum($field, $name = null){
340
-      $func = 'SUM(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : '');
341
-      $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func);
342
-      return $this;
343
-    }
344
-
345
-    /**
346
-     * Set the SQL function COUNT in SELECT statment
347
-     * @param  string $field the field name
348
-     * @param  string $name  if is not null represent the alias used for this field in the result
349
-     * @return object        the current Database instance
350
-     */
351
-    public function count($field = '*', $name = null){
352
-      $func = 'COUNT(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : '');
353
-      $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func);
354
-      return $this;
355
-    }
356
-
357
-    /**
358
-     * Set the SQL function AVG in SELECT statment
359
-     * @param  string $field the field name
360
-     * @param  string $name  if is not null represent the alias used for this field in the result
361
-     * @return object        the current Database instance
362
-     */
363
-    public function avg($field, $name = null){
364
-      $func = 'AVG(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : '');
365
-      $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func);
366
-      return $this;
367
-    }
368
-
369
-    /**
370
-     * Set the SQL JOIN statment
371
-     * @param  string $table  the join table name
372
-     * @param  string $field1 the first field for join conditions	
373
-     * @param  string $op     the join condition operator. If is null the default will be "="
374
-     * @param  string $field2 the second field for join conditions
375
-     * @param  string $type   the type of join (INNER, LEFT, RIGHT)
376
-     * @return object        the current Database instance
377
-     */
378
-    public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){
379
-      $on = $field1;
380
-      $table = $this->prefix . $table;
381
-      if(! is_null($op)){
382
-        $on = (! in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2);
383
-      }
384
-      if (empty($this->join)){
385
-        $this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on;
386
-      }
387
-      else{
388
-        $this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on;
389
-      }
390
-      return $this;
391
-    }
392
-
393
-    /**
394
-     * Set the SQL INNER JOIN statment
395
-     * @see  Database::join()
396
-     * @return object        the current Database instance
397
-     */
398
-    public function innerJoin($table, $field1, $op = null, $field2 = ''){
399
-      return $this->join($table, $field1, $op, $field2, 'INNER ');
400
-    }
401
-
402
-    /**
403
-     * Set the SQL LEFT JOIN statment
404
-     * @see  Database::join()
405
-     * @return object        the current Database instance
406
-     */
407
-    public function leftJoin($table, $field1, $op = null, $field2 = ''){
408
-      return $this->join($table, $field1, $op, $field2, 'LEFT ');
409
-	}
410
-
411
-	/**
412
-     * Set the SQL RIGHT JOIN statment
413
-     * @see  Database::join()
414
-     * @return object        the current Database instance
415
-     */
416
-    public function rightJoin($table, $field1, $op = null, $field2 = ''){
417
-      return $this->join($table, $field1, $op, $field2, 'RIGHT ');
418
-    }
419
-
420
-    /**
421
-     * Set the SQL FULL OUTER JOIN statment
422
-     * @see  Database::join()
423
-     * @return object        the current Database instance
424
-     */
425
-    public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){
426
-    	return $this->join($table, $field1, $op, $field2, 'FULL OUTER ');
427
-    }
428
-
429
-    /**
430
-     * Set the SQL LEFT OUTER JOIN statment
431
-     * @see  Database::join()
432
-     * @return object        the current Database instance
433
-     */
434
-    public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){
435
-      return $this->join($table, $field1, $op, $field2, 'LEFT OUTER ');
436
-    }
437
-
438
-    /**
439
-     * Set the SQL RIGHT OUTER JOIN statment
440
-     * @see  Database::join()
441
-     * @return object        the current Database instance
442
-     */
443
-    public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){
444
-      return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER ');
445
-    }
446
-
447
-    /**
448
-     * Set the SQL WHERE CLAUSE for IS NULL
449
-     * @param  string|array $field  the field name or array of field list
450
-     * @param  string $andOr the separator type used 'AND', 'OR', etc.
451
-     * @return object        the current Database instance
452
-     */
453
-    public function whereIsNull($field, $andOr = 'AND'){
454
-      if(is_array($field)){
455
-        foreach($field as $f){
456
-        	$this->whereIsNull($f, $andOr);
457
-        }
458
-      }
459
-      else{
460
-        if (! $this->where){
461
-          $this->where = $field.' IS NULL ';
462
-        }
463
-        else{
464
-            $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL ';
465
-          }
466
-      }
467
-      return $this;
468
-    }
469
-
470
-    /**
471
-     * Set the SQL WHERE CLAUSE for IS NOT NULL
472
-     * @param  string|array $field  the field name or array of field list
473
-     * @param  string $andOr the separator type used 'AND', 'OR', etc.
474
-     * @return object        the current Database instance
475
-     */
476
-    public function whereIsNotNull($field, $andOr = 'AND'){
477
-      if(is_array($field)){
478
-        foreach($field as $f){
479
-          $this->whereIsNotNull($f, $andOr);
480
-        }
481
-      }
482
-      else{
483
-        if (! $this->where){
484
-          $this->where = $field.' IS NOT NULL ';
485
-        }
486
-        else{
487
-            $this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL ';
488
-          }
489
-      }
490
-      return $this;
491
-    }
249
+			$this->pdo = new PDO($dsn, $config['username'], $config['password']);
250
+			$this->pdo->exec("SET NAMES '" . $config['charset'] . "' COLLATE '" . $config['collation'] . "'");
251
+			$this->pdo->exec("SET CHARACTER SET '" . $config['charset'] . "'");
252
+			$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
253
+			return true;
254
+		  }
255
+		  catch (PDOException $e){
256
+			$this->logger->fatal($e->getMessage());
257
+			show_error('Cannot connect to Database.');
258
+			return false;
259
+		  }
260
+	  }
261
+	  else{
262
+		show_error('Database configuration is not set.');
263
+		return false;
264
+	  }
265
+	}
266
+
267
+	/**
268
+	 * Set the SQL FROM statment
269
+	 * @param  string|array $table the table name or array of table list
270
+	 * @return object        the current Database instance
271
+	 */
272
+	public function from($table){
273
+	  if(is_array($table)){
274
+		$froms = '';
275
+		foreach($table as $key){
276
+		  $froms .= $this->prefix . $key . ', ';
277
+		}
278
+		$this->from = rtrim($froms, ', ');
279
+	  }
280
+	  else{
281
+		$this->from = $this->prefix . $table;
282
+	  }
283
+	  return $this;
284
+	}
285
+
286
+	/**
287
+	 * Set the SQL SELECT statment
288
+	 * @param  string|array $fields the field name or array of field list
289
+	 * @return object        the current Database instance
290
+	 */
291
+	public function select($fields){
292
+	  $select = (is_array($fields) ? implode(', ', $fields) : $fields);
293
+	  $this->select = ($this->select == '*' ? $select : $this->select . ', ' . $select);
294
+	  return $this;
295
+	}
296
+
297
+	/**
298
+	 * Set the SQL SELECT DISTINCT statment
299
+	 * @param  string $field the field name to distinct
300
+	 * @return object        the current Database instance
301
+	 */
302
+	public function distinct($field){
303
+	  $distinct = ' DISTINCT ' . $field;
304
+	  $this->select = ($this->select == '*' ? $distinct : $this->select . ', ' . $distinct);
305
+
306
+	  return $this;
307
+	}
308
+
309
+	/**
310
+	 * Set the SQL function MAX in SELECT statment
311
+	 * @param  string $field the field name
312
+	 * @param  string $name  if is not null represent the alias used for this field in the result
313
+	 * @return object        the current Database instance
314
+	 */
315
+	public function max($field, $name = null){
316
+	  $func = 'MAX(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : '');
317
+	  $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func);
318
+	  return $this;
319
+	}
320
+
321
+	/**
322
+	 * Set the SQL function MIN in SELECT statment
323
+	 * @param  string $field the field name
324
+	 * @param  string $name  if is not null represent the alias used for this field in the result
325
+	 * @return object        the current Database instance
326
+	 */
327
+	public function min($field, $name = null){
328
+	  $func = 'MIN(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : '');
329
+	  $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func);
330
+	  return $this;
331
+	}
332
+
333
+	/**
334
+	 * Set the SQL function SUM in SELECT statment
335
+	 * @param  string $field the field name
336
+	 * @param  string $name  if is not null represent the alias used for this field in the result
337
+	 * @return object        the current Database instance
338
+	 */
339
+	public function sum($field, $name = null){
340
+	  $func = 'SUM(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : '');
341
+	  $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func);
342
+	  return $this;
343
+	}
344
+
345
+	/**
346
+	 * Set the SQL function COUNT in SELECT statment
347
+	 * @param  string $field the field name
348
+	 * @param  string $name  if is not null represent the alias used for this field in the result
349
+	 * @return object        the current Database instance
350
+	 */
351
+	public function count($field = '*', $name = null){
352
+	  $func = 'COUNT(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : '');
353
+	  $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func);
354
+	  return $this;
355
+	}
356
+
357
+	/**
358
+	 * Set the SQL function AVG in SELECT statment
359
+	 * @param  string $field the field name
360
+	 * @param  string $name  if is not null represent the alias used for this field in the result
361
+	 * @return object        the current Database instance
362
+	 */
363
+	public function avg($field, $name = null){
364
+	  $func = 'AVG(' . $field . ')' . (!is_null($name) ? ' AS ' . $name : '');
365
+	  $this->select = ($this->select == '*' ? $func : $this->select . ', ' . $func);
366
+	  return $this;
367
+	}
368
+
369
+	/**
370
+	 * Set the SQL JOIN statment
371
+	 * @param  string $table  the join table name
372
+	 * @param  string $field1 the first field for join conditions	
373
+	 * @param  string $op     the join condition operator. If is null the default will be "="
374
+	 * @param  string $field2 the second field for join conditions
375
+	 * @param  string $type   the type of join (INNER, LEFT, RIGHT)
376
+	 * @return object        the current Database instance
377
+	 */
378
+	public function join($table, $field1 = null, $op = null, $field2 = null, $type = ''){
379
+	  $on = $field1;
380
+	  $table = $this->prefix . $table;
381
+	  if(! is_null($op)){
382
+		$on = (! in_array($op, $this->operatorList) ? $this->prefix . $field1 . ' = ' . $this->prefix . $op : $this->prefix . $field1 . ' ' . $op . ' ' . $this->prefix . $field2);
383
+	  }
384
+	  if (empty($this->join)){
385
+		$this->join = ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on;
386
+	  }
387
+	  else{
388
+		$this->join = $this->join . ' ' . $type . 'JOIN' . ' ' . $table . ' ON ' . $on;
389
+	  }
390
+	  return $this;
391
+	}
392
+
393
+	/**
394
+	 * Set the SQL INNER JOIN statment
395
+	 * @see  Database::join()
396
+	 * @return object        the current Database instance
397
+	 */
398
+	public function innerJoin($table, $field1, $op = null, $field2 = ''){
399
+	  return $this->join($table, $field1, $op, $field2, 'INNER ');
400
+	}
401
+
402
+	/**
403
+	 * Set the SQL LEFT JOIN statment
404
+	 * @see  Database::join()
405
+	 * @return object        the current Database instance
406
+	 */
407
+	public function leftJoin($table, $field1, $op = null, $field2 = ''){
408
+	  return $this->join($table, $field1, $op, $field2, 'LEFT ');
409
+	}
410
+
411
+	/**
412
+	 * Set the SQL RIGHT JOIN statment
413
+	 * @see  Database::join()
414
+	 * @return object        the current Database instance
415
+	 */
416
+	public function rightJoin($table, $field1, $op = null, $field2 = ''){
417
+	  return $this->join($table, $field1, $op, $field2, 'RIGHT ');
418
+	}
419
+
420
+	/**
421
+	 * Set the SQL FULL OUTER JOIN statment
422
+	 * @see  Database::join()
423
+	 * @return object        the current Database instance
424
+	 */
425
+	public function fullOuterJoin($table, $field1, $op = null, $field2 = ''){
426
+		return $this->join($table, $field1, $op, $field2, 'FULL OUTER ');
427
+	}
428
+
429
+	/**
430
+	 * Set the SQL LEFT OUTER JOIN statment
431
+	 * @see  Database::join()
432
+	 * @return object        the current Database instance
433
+	 */
434
+	public function leftOuterJoin($table, $field1, $op = null, $field2 = ''){
435
+	  return $this->join($table, $field1, $op, $field2, 'LEFT OUTER ');
436
+	}
437
+
438
+	/**
439
+	 * Set the SQL RIGHT OUTER JOIN statment
440
+	 * @see  Database::join()
441
+	 * @return object        the current Database instance
442
+	 */
443
+	public function rightOuterJoin($table, $field1, $op = null, $field2 = ''){
444
+	  return $this->join($table, $field1, $op, $field2, 'RIGHT OUTER ');
445
+	}
446
+
447
+	/**
448
+	 * Set the SQL WHERE CLAUSE for IS NULL
449
+	 * @param  string|array $field  the field name or array of field list
450
+	 * @param  string $andOr the separator type used 'AND', 'OR', etc.
451
+	 * @return object        the current Database instance
452
+	 */
453
+	public function whereIsNull($field, $andOr = 'AND'){
454
+	  if(is_array($field)){
455
+		foreach($field as $f){
456
+			$this->whereIsNull($f, $andOr);
457
+		}
458
+	  }
459
+	  else{
460
+		if (! $this->where){
461
+		  $this->where = $field.' IS NULL ';
462
+		}
463
+		else{
464
+			$this->where = $this->where . ' '.$andOr.' ' . $field.' IS NULL ';
465
+		  }
466
+	  }
467
+	  return $this;
468
+	}
469
+
470
+	/**
471
+	 * Set the SQL WHERE CLAUSE for IS NOT NULL
472
+	 * @param  string|array $field  the field name or array of field list
473
+	 * @param  string $andOr the separator type used 'AND', 'OR', etc.
474
+	 * @return object        the current Database instance
475
+	 */
476
+	public function whereIsNotNull($field, $andOr = 'AND'){
477
+	  if(is_array($field)){
478
+		foreach($field as $f){
479
+		  $this->whereIsNotNull($f, $andOr);
480
+		}
481
+	  }
482
+	  else{
483
+		if (! $this->where){
484
+		  $this->where = $field.' IS NOT NULL ';
485
+		}
486
+		else{
487
+			$this->where = $this->where . ' '.$andOr.' ' . $field.' IS NOT NULL ';
488
+		  }
489
+	  }
490
+	  return $this;
491
+	}
492 492
     
493
-    /**
494
-     * Set the SQL WHERE CLAUSE statment
495
-     * @param  string|array  $where the where field or array of field list
496
-     * @param  array|string  $op     the condition operator. If is null the default will be "="
497
-     * @param  mixed  $val    the where value
498
-     * @param  string  $type   the type used for this where clause (NOT, etc.)
499
-     * @param  string  $andOr the separator type used 'AND', 'OR', etc.
500
-     * @param  boolean $escape whether to escape or not the $val
501
-     * @return object        the current Database instance
502
-     */
503
-    public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){
504
-      if (is_array($where)){
505
-        $_where = array();
506
-        foreach ($where as $column => $data){
507
-          if(is_null($data)){
508
-            $data = '';
509
-          }
510
-          $_where[] = $type . $column . '=' . ($escape ? $this->escape($data) : $data);
511
-        }
512
-        $where = implode(' '.$andOr.' ', $_where);
513
-      }
514
-      else{
515
-        if(is_array($op)){
516
-          $x = explode('?', $where);
517
-          $w = '';
518
-          foreach($x as $k => $v){
519
-            if(! empty($v)){
520
-                if(isset($op[$k]) && is_null($op[$k])){
521
-                  $op[$k] = '';
522
-                }
523
-                $w .= $type . $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : '');
524
-            }
525
-          }
526
-          $where = $w;
527
-        }
528
-        else if (! in_array((string)$op, $this->operatorList)){
529
-          if(is_null($op)){
530
-            $op = '';
531
-          }
532
-        	$where = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op);
533
-        }
534
-        else{
535
-          if(is_null($val)){
536
-            $val = '';
537
-          }
538
-        	$where = $type . $where . $op . ($escape ? $this->escape($val) : $val);
539
-        }
540
-      }
541
-      if (empty($this->where)){
542
-        $this->where = $where;
543
-      }
544
-      else{
545
-        if(substr($this->where, -1) == '('){
546
-          $this->where = $this->where . ' ' . $where;
547
-        }
548
-        else{
549
-          $this->where = $this->where . ' '.$andOr.' ' . $where;
550
-        }
551
-      }
552
-      return $this;
553
-    }
554
-
555
-    /**
556
-     * Set the SQL WHERE CLAUSE statment using OR
557
-     * @see  Database::where()
558
-     * @return object        the current Database instance
559
-     */
560
-    public function orWhere($where, $op = null, $val = null, $escape = true){
561
-      return $this->where($where, $op, $val, '', 'OR', $escape);
562
-    }
563
-
564
-
565
-    /**
566
-     * Set the SQL WHERE CLAUSE statment using AND and NOT
567
-     * @see  Database::where()
568
-     * @return object        the current Database instance
569
-     */
570
-    public function notWhere($where, $op = null, $val = null, $escape = true){
571
-      return $this->where($where, $op, $val, 'NOT ', 'AND', $escape);
572
-    }
573
-
574
-    /**
575
-     * Set the SQL WHERE CLAUSE statment using OR and NOT
576
-     * @see  Database::where()
577
-     * @return object        the current Database instance
578
-     */
579
-    public function orNotWhere($where, $op = null, $val = null, $escape = true){
580
-    	return $this->where($where, $op, $val, 'NOT ', 'OR', $escape);
581
-    }
582
-
583
-    /**
584
-     * Set the opened parenthesis for the complex SQL query
585
-     * @param  string $type   the type of this grouped (NOT, etc.)
586
-     * @param  string $andOr the multiple conditions separator (AND, OR, etc.)
587
-     * @return object        the current Database instance
588
-     */
589
-    public function groupStart($type = '', $andOr = ' AND'){
590
-      if (empty($this->where)){
591
-        $this->where = $type . ' (';
592
-      }
593
-      else{
594
-          if(substr($this->where, -1) == '('){
595
-            $this->where .= $type . ' (';
596
-          }
597
-          else{
598
-          	$this->where .= $andOr . ' ' . $type . ' (';
599
-          }
600
-      }
601
-      return $this;
602
-    }
603
-
604
-    /**
605
-     * Set the opened parenthesis for the complex SQL query using NOT type
606
-     * @see  Database::groupStart()
607
-     * @return object        the current Database instance
608
-     */
609
-    public function notGroupStart(){
610
-      return $this->groupStart('NOT');
611
-    }
612
-
613
-    /**
614
-     * Set the opened parenthesis for the complex SQL query using OR for separator
615
-     * @see  Database::groupStart()
616
-     * @return object        the current Database instance
617
-     */
618
-    public function orGroupStart(){
619
-      return $this->groupStart('', ' OR');
620
-    }
621
-
622
-     /**
623
-     * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type
624
-     * @see  Database::groupStart()
625
-     * @return object        the current Database instance
626
-     */
627
-    public function orNotGroupStart(){
628
-      return $this->groupStart('NOT', ' OR');
629
-    }
630
-
631
-    /**
632
-     * Close the parenthesis for the grouped SQL
633
-     * @return object        the current Database instance
634
-     */
635
-    public function groupEnd(){
636
-      $this->where .= ')';
637
-      return $this;
638
-    }
639
-
640
-    /**
641
-     * Set the SQL WHERE CLAUSE statment for IN
642
-     * @param  string  $field  the field name for IN statment
643
-     * @param  array   $keys   the list of values used
644
-     * @param  string  $type   the condition separator type (NOT)
645
-     * @param  string  $andOr the multiple conditions separator (OR, AND)
646
-     * @param  boolean $escape whether to escape or not the values
647
-     * @return object        the current Database instance
648
-     */
649
-    public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){
650
-      $_keys = array();
651
-      foreach ($keys as $k => $v){
652
-        if(is_null($v)){
653
-          $v = '';
654
-        }
655
-        $_keys[] = (is_numeric($v) ? $v : ($escape ? $this->escape($v) : $v));
656
-      }
657
-      $keys = implode(', ', $_keys);
658
-      if (empty($this->where)){
659
-        $this->where = $field . ' ' . $type . 'IN (' . $keys . ')';
660
-      }
661
-      else{
662
-        if(substr($this->where, -1) == '('){
663
-          $this->where = $this->where . ' ' . $field . ' '.$type.'IN (' . $keys . ')';
664
-        }
665
-        else{
666
-          $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' '.$type.'IN (' . $keys . ')';
667
-        }
668
-      }
669
-      return $this;
670
-    }
671
-
672
-    /**
673
-     * Set the SQL WHERE CLAUSE statment for NOT IN with AND separator
674
-     * @see  Database::in()
675
-     * @return object        the current Database instance
676
-     */
677
-    public function notIn($field, array $keys, $escape = true){
678
-      return $this->in($field, $keys, 'NOT ', 'AND', $escape);
679
-    }
680
-
681
-    /**
682
-     * Set the SQL WHERE CLAUSE statment for IN with OR separator
683
-     * @see  Database::in()
684
-     * @return object        the current Database instance
685
-     */
686
-    public function orIn($field, array $keys, $escape = true){
687
-      return $this->in($field, $keys, '', 'OR', $escape);
688
-    }
689
-
690
-    /**
691
-     * Set the SQL WHERE CLAUSE statment for NOT IN with OR separator
692
-     * @see  Database::in()
693
-     * @return object        the current Database instance
694
-     */
695
-    public function orNotIn($field, array $keys, $escape = true){
696
-      return $this->in($field, $keys, 'NOT ', 'OR', $escape);
697
-    }
698
-
699
-    /**
700
-     * Set the SQL WHERE CLAUSE statment for BETWEEN
701
-     * @param  string  $field  the field used for the BETWEEN statment
702
-     * @param  mixed  $value1 the BETWEEN begin value
703
-     * @param  mixed  $value2 the BETWEEN end value
704
-     * @param  string  $type   the condition separator type (NOT)
705
-     * @param  string  $andOr the multiple conditions separator (OR, AND)
706
-     * @param  boolean $escape whether to escape or not the values
707
-     * @return object        the current Database instance
708
-     */
709
-    public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){
710
-      if(is_null($value1)){
711
-        $value1 = '';
712
-      }
713
-      if(is_null($value2)){
714
-        $value2 = '';
715
-      }
716
-      if (empty($this->where)){
717
-      	$this->where = $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2);
718
-      }
719
-      else{
720
-        if(substr($this->where, -1) == '('){
721
-          $this->where = $this->where . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2);
722
-        }
723
-        else{
724
-          $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2);
725
-        }
726
-      }
727
-      return $this;
728
-    }
729
-
730
-    /**
731
-     * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and AND separator
732
-     * @see  Database::between()
733
-     * @return object        the current Database instance
734
-     */
735
-    public function notBetween($field, $value1, $value2, $escape = true){
736
-      return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape);
737
-    }
738
-
739
-    /**
740
-     * Set the SQL WHERE CLAUSE statment for BETWEEN with OR separator
741
-     * @see  Database::between()
742
-     * @return object        the current Database instance
743
-     */
744
-    public function orBetween($field, $value1, $value2, $escape = true){
745
-      return $this->between($field, $value1, $value2, '', 'OR', $escape);
746
-    }
747
-
748
-    /**
749
-     * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and OR separator
750
-     * @see  Database::between()
751
-     * @return object        the current Database instance
752
-     */
753
-    public function orNotBetween($field, $value1, $value2, $escape = true){
754
-      return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape);
755
-    }
756
-
757
-    /**
758
-     * Set the SQL WHERE CLAUSE statment for LIKE
759
-     * @param  string  $field  the field name used in LIKE statment
760
-     * @param  string  $data   the LIKE value for this field including the '%', and '_' part
761
-     * @param  string  $type   the condition separator type (NOT)
762
-     * @param  string  $andOr the multiple conditions separator (OR, AND)
763
-     * @param  boolean $escape whether to escape or not the values
764
-     * @return object        the current Database instance
765
-     */
766
-    public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){
767
-      if(empty($data)){
768
-        $data = '';
769
-      }
770
-      $like = $escape ? $this->escape($data) : $data;
771
-      if (empty($this->where)){
772
-        $this->where = $field . ' ' . $type . 'LIKE ' . $like;
773
-      }
774
-      else{
775
-        if(substr($this->where, -1) == '('){
776
-          $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like;
777
-        }
778
-        else{
779
-          $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like;
780
-        }
781
-      }
782
-      return $this;
783
-    }
784
-
785
-    /**
786
-     * Set the SQL WHERE CLAUSE statment for LIKE with OR separator
787
-     * @see  Database::like()
788
-     * @return object        the current Database instance
789
-     */
790
-    public function orLike($field, $data, $escape = true){
791
-      return $this->like($field, $data, '', 'OR', $escape);
792
-    }
793
-
794
-    /**
795
-     * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and AND separator
796
-     * @see  Database::like()
797
-     * @return object        the current Database instance
798
-     */
799
-    public function notLike($field, $data, $escape = true){
800
-      return $this->like($field, $data, 'NOT ', 'AND', $escape);
801
-    }
802
-
803
-    /**
804
-     * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and OR separator
805
-     * @see  Database::like()
806
-     * @return object        the current Database instance
807
-     */
808
-    public function orNotLike($field, $data, $escape = true){
809
-      return $this->like($field, $data, 'NOT ', 'OR', $escape);
810
-    }
811
-
812
-    /**
813
-     * Set the SQL LIMIT statment
814
-     * @param  int $limit    the limit offset. If $limitEnd is null this will be the limit count
815
-     * like LIMIT n;
816
-     * @param  int $limitEnd the limit count
817
-     * @return object        the current Database instance
818
-     */
819
-    public function limit($limit, $limitEnd = null){
820
-      if(empty($limit)){
821
-        return;
822
-      }
823
-      if (! is_null($limitEnd)){
824
-        $this->limit = $limit . ', ' . $limitEnd;
825
-      }
826
-      else{
827
-        $this->limit = $limit;
828
-      }
829
-      return $this;
830
-    }
831
-
832
-    /**
833
-     * Set the SQL ORDER BY CLAUSE statment
834
-     * @param  string $orderBy   the field name used for order
835
-     * @param  string $orderDir the order direction (ASC or DESC)
836
-     * @return object        the current Database instance
837
-     */
838
-    public function orderBy($orderBy, $orderDir = ' ASC'){
839
-      if (! empty($orderDir)){
840
-        $this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir);
841
-      }
842
-      else{
843
-        if(stristr($orderBy, ' ') || $orderBy == 'rand()'){
844
-          $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy;
845
-        }
846
-        else{
847
-          $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC');
848
-        }
849
-      }
850
-      return $this;
851
-    }
852
-
853
-    /**
854
-     * Set the SQL GROUP BY CLAUSE statment
855
-     * @param  string|array $field the field name used or array of field list
856
-     * @return object        the current Database instance
857
-     */
858
-    public function groupBy($field){
859
-      if(is_array($field)){
860
-        $this->groupBy = implode(', ', $field);
861
-      }
862
-      else{
863
-        $this->groupBy = $field;
864
-      }
865
-      return $this;
866
-    }
867
-
868
-    /**
869
-     * Set the SQL HAVING CLAUSE statment
870
-     * @param  string  $field  the field name used for HAVING statment
871
-     * @param  string|array  $op     the operator used or array
872
-     * @param  mixed  $val    the value for HAVING comparaison
873
-     * @param  boolean $escape whether to escape or not the values
874
-     * @return object        the current Database instance
875
-     */
876
-    public function having($field, $op = null, $val = null, $escape = true){
877
-      if(is_array($op)){
878
-        $x = explode('?', $field);
879
-        $w = '';
880
-        foreach($x as $k => $v){
881
-  	      if(!empty($v)){
882
-            if(isset($op[$k]) && is_null($op[$k])){
883
-              $op[$k] = '';
884
-            }
885
-  	      	$w .= $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : '');
886
-  	      }
887
-      	}
888
-        $this->having = $w;
889
-      }
890
-      else if (! in_array($op, $this->operatorList)){
891
-        if(is_null($op)){
892
-          $op = '';
893
-        }
894
-        $this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op);
895
-      }
896
-      else{
897
-        if(is_null($val)){
898
-          $val = '';
899
-        }
900
-        $this->having = $field . ' ' . $op . ' ' . ($escape ? $this->escape($val) : $val);
901
-      }
902
-      return $this;
903
-    }
904
-
905
-    /**
906
-     * Return the number of rows returned by the current query
907
-     * @return int
908
-     */
909
-    public function numRows(){
910
-      return $this->numRows;
911
-    }
912
-
913
-    /**
914
-     * Return the last insert id value
915
-     * @return mixed
916
-     */
917
-    public function insertId(){
918
-      return $this->insertId;
919
-    }
920
-
921
-    /**
922
-     * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.)
923
-     */
924
-    public function error(){
493
+	/**
494
+	 * Set the SQL WHERE CLAUSE statment
495
+	 * @param  string|array  $where the where field or array of field list
496
+	 * @param  array|string  $op     the condition operator. If is null the default will be "="
497
+	 * @param  mixed  $val    the where value
498
+	 * @param  string  $type   the type used for this where clause (NOT, etc.)
499
+	 * @param  string  $andOr the separator type used 'AND', 'OR', etc.
500
+	 * @param  boolean $escape whether to escape or not the $val
501
+	 * @return object        the current Database instance
502
+	 */
503
+	public function where($where, $op = null, $val = null, $type = '', $andOr = 'AND', $escape = true){
504
+	  if (is_array($where)){
505
+		$_where = array();
506
+		foreach ($where as $column => $data){
507
+		  if(is_null($data)){
508
+			$data = '';
509
+		  }
510
+		  $_where[] = $type . $column . '=' . ($escape ? $this->escape($data) : $data);
511
+		}
512
+		$where = implode(' '.$andOr.' ', $_where);
513
+	  }
514
+	  else{
515
+		if(is_array($op)){
516
+		  $x = explode('?', $where);
517
+		  $w = '';
518
+		  foreach($x as $k => $v){
519
+			if(! empty($v)){
520
+				if(isset($op[$k]) && is_null($op[$k])){
521
+				  $op[$k] = '';
522
+				}
523
+				$w .= $type . $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : '');
524
+			}
525
+		  }
526
+		  $where = $w;
527
+		}
528
+		else if (! in_array((string)$op, $this->operatorList)){
529
+		  if(is_null($op)){
530
+			$op = '';
531
+		  }
532
+			$where = $type . $where . ' = ' . ($escape ? $this->escape($op) : $op);
533
+		}
534
+		else{
535
+		  if(is_null($val)){
536
+			$val = '';
537
+		  }
538
+			$where = $type . $where . $op . ($escape ? $this->escape($val) : $val);
539
+		}
540
+	  }
541
+	  if (empty($this->where)){
542
+		$this->where = $where;
543
+	  }
544
+	  else{
545
+		if(substr($this->where, -1) == '('){
546
+		  $this->where = $this->where . ' ' . $where;
547
+		}
548
+		else{
549
+		  $this->where = $this->where . ' '.$andOr.' ' . $where;
550
+		}
551
+	  }
552
+	  return $this;
553
+	}
554
+
555
+	/**
556
+	 * Set the SQL WHERE CLAUSE statment using OR
557
+	 * @see  Database::where()
558
+	 * @return object        the current Database instance
559
+	 */
560
+	public function orWhere($where, $op = null, $val = null, $escape = true){
561
+	  return $this->where($where, $op, $val, '', 'OR', $escape);
562
+	}
563
+
564
+
565
+	/**
566
+	 * Set the SQL WHERE CLAUSE statment using AND and NOT
567
+	 * @see  Database::where()
568
+	 * @return object        the current Database instance
569
+	 */
570
+	public function notWhere($where, $op = null, $val = null, $escape = true){
571
+	  return $this->where($where, $op, $val, 'NOT ', 'AND', $escape);
572
+	}
573
+
574
+	/**
575
+	 * Set the SQL WHERE CLAUSE statment using OR and NOT
576
+	 * @see  Database::where()
577
+	 * @return object        the current Database instance
578
+	 */
579
+	public function orNotWhere($where, $op = null, $val = null, $escape = true){
580
+		return $this->where($where, $op, $val, 'NOT ', 'OR', $escape);
581
+	}
582
+
583
+	/**
584
+	 * Set the opened parenthesis for the complex SQL query
585
+	 * @param  string $type   the type of this grouped (NOT, etc.)
586
+	 * @param  string $andOr the multiple conditions separator (AND, OR, etc.)
587
+	 * @return object        the current Database instance
588
+	 */
589
+	public function groupStart($type = '', $andOr = ' AND'){
590
+	  if (empty($this->where)){
591
+		$this->where = $type . ' (';
592
+	  }
593
+	  else{
594
+		  if(substr($this->where, -1) == '('){
595
+			$this->where .= $type . ' (';
596
+		  }
597
+		  else{
598
+		  	$this->where .= $andOr . ' ' . $type . ' (';
599
+		  }
600
+	  }
601
+	  return $this;
602
+	}
603
+
604
+	/**
605
+	 * Set the opened parenthesis for the complex SQL query using NOT type
606
+	 * @see  Database::groupStart()
607
+	 * @return object        the current Database instance
608
+	 */
609
+	public function notGroupStart(){
610
+	  return $this->groupStart('NOT');
611
+	}
612
+
613
+	/**
614
+	 * Set the opened parenthesis for the complex SQL query using OR for separator
615
+	 * @see  Database::groupStart()
616
+	 * @return object        the current Database instance
617
+	 */
618
+	public function orGroupStart(){
619
+	  return $this->groupStart('', ' OR');
620
+	}
621
+
622
+	 /**
623
+	  * Set the opened parenthesis for the complex SQL query using OR for separator and NOT for type
624
+	  * @see  Database::groupStart()
625
+	  * @return object        the current Database instance
626
+	  */
627
+	public function orNotGroupStart(){
628
+	  return $this->groupStart('NOT', ' OR');
629
+	}
630
+
631
+	/**
632
+	 * Close the parenthesis for the grouped SQL
633
+	 * @return object        the current Database instance
634
+	 */
635
+	public function groupEnd(){
636
+	  $this->where .= ')';
637
+	  return $this;
638
+	}
639
+
640
+	/**
641
+	 * Set the SQL WHERE CLAUSE statment for IN
642
+	 * @param  string  $field  the field name for IN statment
643
+	 * @param  array   $keys   the list of values used
644
+	 * @param  string  $type   the condition separator type (NOT)
645
+	 * @param  string  $andOr the multiple conditions separator (OR, AND)
646
+	 * @param  boolean $escape whether to escape or not the values
647
+	 * @return object        the current Database instance
648
+	 */
649
+	public function in($field, array $keys, $type = '', $andOr = 'AND', $escape = true){
650
+	  $_keys = array();
651
+	  foreach ($keys as $k => $v){
652
+		if(is_null($v)){
653
+		  $v = '';
654
+		}
655
+		$_keys[] = (is_numeric($v) ? $v : ($escape ? $this->escape($v) : $v));
656
+	  }
657
+	  $keys = implode(', ', $_keys);
658
+	  if (empty($this->where)){
659
+		$this->where = $field . ' ' . $type . 'IN (' . $keys . ')';
660
+	  }
661
+	  else{
662
+		if(substr($this->where, -1) == '('){
663
+		  $this->where = $this->where . ' ' . $field . ' '.$type.'IN (' . $keys . ')';
664
+		}
665
+		else{
666
+		  $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' '.$type.'IN (' . $keys . ')';
667
+		}
668
+	  }
669
+	  return $this;
670
+	}
671
+
672
+	/**
673
+	 * Set the SQL WHERE CLAUSE statment for NOT IN with AND separator
674
+	 * @see  Database::in()
675
+	 * @return object        the current Database instance
676
+	 */
677
+	public function notIn($field, array $keys, $escape = true){
678
+	  return $this->in($field, $keys, 'NOT ', 'AND', $escape);
679
+	}
680
+
681
+	/**
682
+	 * Set the SQL WHERE CLAUSE statment for IN with OR separator
683
+	 * @see  Database::in()
684
+	 * @return object        the current Database instance
685
+	 */
686
+	public function orIn($field, array $keys, $escape = true){
687
+	  return $this->in($field, $keys, '', 'OR', $escape);
688
+	}
689
+
690
+	/**
691
+	 * Set the SQL WHERE CLAUSE statment for NOT IN with OR separator
692
+	 * @see  Database::in()
693
+	 * @return object        the current Database instance
694
+	 */
695
+	public function orNotIn($field, array $keys, $escape = true){
696
+	  return $this->in($field, $keys, 'NOT ', 'OR', $escape);
697
+	}
698
+
699
+	/**
700
+	 * Set the SQL WHERE CLAUSE statment for BETWEEN
701
+	 * @param  string  $field  the field used for the BETWEEN statment
702
+	 * @param  mixed  $value1 the BETWEEN begin value
703
+	 * @param  mixed  $value2 the BETWEEN end value
704
+	 * @param  string  $type   the condition separator type (NOT)
705
+	 * @param  string  $andOr the multiple conditions separator (OR, AND)
706
+	 * @param  boolean $escape whether to escape or not the values
707
+	 * @return object        the current Database instance
708
+	 */
709
+	public function between($field, $value1, $value2, $type = '', $andOr = 'AND', $escape = true){
710
+	  if(is_null($value1)){
711
+		$value1 = '';
712
+	  }
713
+	  if(is_null($value2)){
714
+		$value2 = '';
715
+	  }
716
+	  if (empty($this->where)){
717
+	  	$this->where = $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2);
718
+	  }
719
+	  else{
720
+		if(substr($this->where, -1) == '('){
721
+		  $this->where = $this->where . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2);
722
+		}
723
+		else{
724
+		  $this->where = $this->where . ' ' . $andOr . ' ' . $field . ' ' . $type . 'BETWEEN ' . ($escape ? $this->escape($value1) : $value1) . ' AND ' . ($escape ? $this->escape($value2) : $value2);
725
+		}
726
+	  }
727
+	  return $this;
728
+	}
729
+
730
+	/**
731
+	 * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and AND separator
732
+	 * @see  Database::between()
733
+	 * @return object        the current Database instance
734
+	 */
735
+	public function notBetween($field, $value1, $value2, $escape = true){
736
+	  return $this->between($field, $value1, $value2, 'NOT ', 'AND', $escape);
737
+	}
738
+
739
+	/**
740
+	 * Set the SQL WHERE CLAUSE statment for BETWEEN with OR separator
741
+	 * @see  Database::between()
742
+	 * @return object        the current Database instance
743
+	 */
744
+	public function orBetween($field, $value1, $value2, $escape = true){
745
+	  return $this->between($field, $value1, $value2, '', 'OR', $escape);
746
+	}
747
+
748
+	/**
749
+	 * Set the SQL WHERE CLAUSE statment for BETWEEN with NOT type and OR separator
750
+	 * @see  Database::between()
751
+	 * @return object        the current Database instance
752
+	 */
753
+	public function orNotBetween($field, $value1, $value2, $escape = true){
754
+	  return $this->between($field, $value1, $value2, 'NOT ', 'OR', $escape);
755
+	}
756
+
757
+	/**
758
+	 * Set the SQL WHERE CLAUSE statment for LIKE
759
+	 * @param  string  $field  the field name used in LIKE statment
760
+	 * @param  string  $data   the LIKE value for this field including the '%', and '_' part
761
+	 * @param  string  $type   the condition separator type (NOT)
762
+	 * @param  string  $andOr the multiple conditions separator (OR, AND)
763
+	 * @param  boolean $escape whether to escape or not the values
764
+	 * @return object        the current Database instance
765
+	 */
766
+	public function like($field, $data, $type = '', $andOr = 'AND', $escape = true){
767
+	  if(empty($data)){
768
+		$data = '';
769
+	  }
770
+	  $like = $escape ? $this->escape($data) : $data;
771
+	  if (empty($this->where)){
772
+		$this->where = $field . ' ' . $type . 'LIKE ' . $like;
773
+	  }
774
+	  else{
775
+		if(substr($this->where, -1) == '('){
776
+		  $this->where = $this->where . ' ' . $field . ' ' . $type . 'LIKE ' . $like;
777
+		}
778
+		else{
779
+		  $this->where = $this->where . ' '.$andOr.' ' . $field . ' ' . $type . 'LIKE ' . $like;
780
+		}
781
+	  }
782
+	  return $this;
783
+	}
784
+
785
+	/**
786
+	 * Set the SQL WHERE CLAUSE statment for LIKE with OR separator
787
+	 * @see  Database::like()
788
+	 * @return object        the current Database instance
789
+	 */
790
+	public function orLike($field, $data, $escape = true){
791
+	  return $this->like($field, $data, '', 'OR', $escape);
792
+	}
793
+
794
+	/**
795
+	 * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and AND separator
796
+	 * @see  Database::like()
797
+	 * @return object        the current Database instance
798
+	 */
799
+	public function notLike($field, $data, $escape = true){
800
+	  return $this->like($field, $data, 'NOT ', 'AND', $escape);
801
+	}
802
+
803
+	/**
804
+	 * Set the SQL WHERE CLAUSE statment for LIKE with NOT type and OR separator
805
+	 * @see  Database::like()
806
+	 * @return object        the current Database instance
807
+	 */
808
+	public function orNotLike($field, $data, $escape = true){
809
+	  return $this->like($field, $data, 'NOT ', 'OR', $escape);
810
+	}
811
+
812
+	/**
813
+	 * Set the SQL LIMIT statment
814
+	 * @param  int $limit    the limit offset. If $limitEnd is null this will be the limit count
815
+	 * like LIMIT n;
816
+	 * @param  int $limitEnd the limit count
817
+	 * @return object        the current Database instance
818
+	 */
819
+	public function limit($limit, $limitEnd = null){
820
+	  if(empty($limit)){
821
+		return;
822
+	  }
823
+	  if (! is_null($limitEnd)){
824
+		$this->limit = $limit . ', ' . $limitEnd;
825
+	  }
826
+	  else{
827
+		$this->limit = $limit;
828
+	  }
829
+	  return $this;
830
+	}
831
+
832
+	/**
833
+	 * Set the SQL ORDER BY CLAUSE statment
834
+	 * @param  string $orderBy   the field name used for order
835
+	 * @param  string $orderDir the order direction (ASC or DESC)
836
+	 * @return object        the current Database instance
837
+	 */
838
+	public function orderBy($orderBy, $orderDir = ' ASC'){
839
+	  if (! empty($orderDir)){
840
+		$this->orderBy = ! $this->orderBy ? ($orderBy . ' ' . strtoupper($orderDir)) : $this->orderBy . ', ' . $orderBy . ' ' . strtoupper($orderDir);
841
+	  }
842
+	  else{
843
+		if(stristr($orderBy, ' ') || $orderBy == 'rand()'){
844
+		  $this->orderBy = ! $this->orderBy ? $orderBy : $this->orderBy . ', ' . $orderBy;
845
+		}
846
+		else{
847
+		  $this->orderBy = ! $this->orderBy ? ($orderBy . ' ASC') : $this->orderBy . ', ' . ($orderBy . ' ASC');
848
+		}
849
+	  }
850
+	  return $this;
851
+	}
852
+
853
+	/**
854
+	 * Set the SQL GROUP BY CLAUSE statment
855
+	 * @param  string|array $field the field name used or array of field list
856
+	 * @return object        the current Database instance
857
+	 */
858
+	public function groupBy($field){
859
+	  if(is_array($field)){
860
+		$this->groupBy = implode(', ', $field);
861
+	  }
862
+	  else{
863
+		$this->groupBy = $field;
864
+	  }
865
+	  return $this;
866
+	}
867
+
868
+	/**
869
+	 * Set the SQL HAVING CLAUSE statment
870
+	 * @param  string  $field  the field name used for HAVING statment
871
+	 * @param  string|array  $op     the operator used or array
872
+	 * @param  mixed  $val    the value for HAVING comparaison
873
+	 * @param  boolean $escape whether to escape or not the values
874
+	 * @return object        the current Database instance
875
+	 */
876
+	public function having($field, $op = null, $val = null, $escape = true){
877
+	  if(is_array($op)){
878
+		$x = explode('?', $field);
879
+		$w = '';
880
+		foreach($x as $k => $v){
881
+  		  if(!empty($v)){
882
+			if(isset($op[$k]) && is_null($op[$k])){
883
+			  $op[$k] = '';
884
+			}
885
+  		  	$w .= $v . (isset($op[$k]) ? ($escape ? $this->escape($op[$k]) : $op[$k]) : '');
886
+  		  }
887
+	  	}
888
+		$this->having = $w;
889
+	  }
890
+	  else if (! in_array($op, $this->operatorList)){
891
+		if(is_null($op)){
892
+		  $op = '';
893
+		}
894
+		$this->having = $field . ' > ' . ($escape ? $this->escape($op) : $op);
895
+	  }
896
+	  else{
897
+		if(is_null($val)){
898
+		  $val = '';
899
+		}
900
+		$this->having = $field . ' ' . $op . ' ' . ($escape ? $this->escape($val) : $val);
901
+	  }
902
+	  return $this;
903
+	}
904
+
905
+	/**
906
+	 * Return the number of rows returned by the current query
907
+	 * @return int
908
+	 */
909
+	public function numRows(){
910
+	  return $this->numRows;
911
+	}
912
+
913
+	/**
914
+	 * Return the last insert id value
915
+	 * @return mixed
916
+	 */
917
+	public function insertId(){
918
+	  return $this->insertId;
919
+	}
920
+
921
+	/**
922
+	 * Show an error got from the current query (SQL command synthax error, database driver returned error, etc.)
923
+	 */
924
+	public function error(){
925 925
   		if($this->error){
926 926
   			show_error('Query: "' . $this->query . '" Error: ' . $this->error, 'Database Error');
927 927
   		}
928
-    }
929
-
930
-    /**
931
-     * Get the result of one record rows returned by the current query
932
-     * @param  boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string.
933
-     * If is string will determine the result type "array" or "object"
934
-     * @return mixed       the query SQL string or the record result
935
-     */
936
-    public function get($returnSQLQueryOrResultType = false){
937
-      $this->limit = 1;
938
-      $query = $this->getAll(true);
939
-      if($returnSQLQueryOrResultType === true){
940
-        return $query;
941
-      }
942
-      else{
943
-        return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) );
944
-      }
945
-    }
946
-
947
-    /**
948
-     * Get the result of record rows list returned by the current query
949
-     * @param  boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string.
950
-     * If is string will determine the result type "array" or "object"
951
-     * @return mixed       the query SQL string or the record result
952
-     */
953
-    public function getAll($returnSQLQueryOrResultType = false){
954
-      $query = 'SELECT ' . $this->select . ' FROM ' . $this->from;
955
-      if (! empty($this->join)){
956
-        $query .= $this->join;
957
-      }
928
+	}
929
+
930
+	/**
931
+	 * Get the result of one record rows returned by the current query
932
+	 * @param  boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string.
933
+	 * If is string will determine the result type "array" or "object"
934
+	 * @return mixed       the query SQL string or the record result
935
+	 */
936
+	public function get($returnSQLQueryOrResultType = false){
937
+	  $this->limit = 1;
938
+	  $query = $this->getAll(true);
939
+	  if($returnSQLQueryOrResultType === true){
940
+		return $query;
941
+	  }
942
+	  else{
943
+		return $this->query( $query, false, (($returnSQLQueryOrResultType == 'array') ? true : false) );
944
+	  }
945
+	}
946
+
947
+	/**
948
+	 * Get the result of record rows list returned by the current query
949
+	 * @param  boolean $returnSQLQueryOrResultType if is boolean and true will return the SQL query string.
950
+	 * If is string will determine the result type "array" or "object"
951
+	 * @return mixed       the query SQL string or the record result
952
+	 */
953
+	public function getAll($returnSQLQueryOrResultType = false){
954
+	  $query = 'SELECT ' . $this->select . ' FROM ' . $this->from;
955
+	  if (! empty($this->join)){
956
+		$query .= $this->join;
957
+	  }
958 958
 	  
959
-      if (! empty($this->where)){
960
-        $query .= ' WHERE ' . $this->where;
961
-      }
959
+	  if (! empty($this->where)){
960
+		$query .= ' WHERE ' . $this->where;
961
+	  }
962 962
 
963
-      if (! empty($this->groupBy)){
964
-        $query .= ' GROUP BY ' . $this->groupBy;
965
-      }
963
+	  if (! empty($this->groupBy)){
964
+		$query .= ' GROUP BY ' . $this->groupBy;
965
+	  }
966 966
 
967
-      if (! empty($this->having)){
968
-        $query .= ' HAVING ' . $this->having;
969
-      }
967
+	  if (! empty($this->having)){
968
+		$query .= ' HAVING ' . $this->having;
969
+	  }
970 970
 
971
-      if (! empty($this->orderBy)){
972
-          $query .= ' ORDER BY ' . $this->orderBy;
973
-      }
971
+	  if (! empty($this->orderBy)){
972
+		  $query .= ' ORDER BY ' . $this->orderBy;
973
+	  }
974 974
 
975
-      if(! empty($this->limit)){
976
-      	$query .= ' LIMIT ' . $this->limit;
977
-      }
975
+	  if(! empty($this->limit)){
976
+	  	$query .= ' LIMIT ' . $this->limit;
977
+	  }
978 978
 	  
979 979
 	   if($returnSQLQueryOrResultType === true){
980
-      	return $query;
981
-      }
982
-      else{
983
-    	   return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) );
984
-      }
985
-    }
986
-
987
-    /**
988
-     * Insert new record in the database
989
-     * @param  array   $data   the record data if is empty will use the $this->data array.
990
-     * @param  boolean $escape  whether to escape or not the values
991
-     * @return mixed          the insert id of the new record or null
992
-     */
993
-    public function insert($data = array(), $escape = true){
994
-      $column = array();
995
-      $val = array();
996
-      if(empty($data) && $this->getData()){
997
-        $columns = array_keys($this->getData());
998
-        $column = implode(',', $columns);
999
-        $val = implode(', ', $this->getData());
1000
-      }
1001
-      else{
1002
-        $columns = array_keys($data);
1003
-        $column = implode(',', $columns);
1004
-        $val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data));
1005
-      }
1006
-
1007
-      $query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')';
1008
-      $query = $this->query($query);
1009
-
1010
-      if ($query){
1011
-        if(! $this->pdo){
1012
-          $this->connect();
1013
-        }
1014
-        $this->insertId = $this->pdo->lastInsertId();
1015
-        return $this->insertId();
1016
-      }
1017
-      else{
980
+	  	return $query;
981
+	  }
982
+	  else{
983
+		   return $this->query($query, true, (($returnSQLQueryOrResultType == 'array') ? true : false) );
984
+	  }
985
+	}
986
+
987
+	/**
988
+	 * Insert new record in the database
989
+	 * @param  array   $data   the record data if is empty will use the $this->data array.
990
+	 * @param  boolean $escape  whether to escape or not the values
991
+	 * @return mixed          the insert id of the new record or null
992
+	 */
993
+	public function insert($data = array(), $escape = true){
994
+	  $column = array();
995
+	  $val = array();
996
+	  if(empty($data) && $this->getData()){
997
+		$columns = array_keys($this->getData());
998
+		$column = implode(',', $columns);
999
+		$val = implode(', ', $this->getData());
1000
+	  }
1001
+	  else{
1002
+		$columns = array_keys($data);
1003
+		$column = implode(',', $columns);
1004
+		$val = implode(', ', ($escape ? array_map(array($this, 'escape'), $data) : $data));
1005
+	  }
1006
+
1007
+	  $query = 'INSERT INTO ' . $this->from . ' (' . $column . ') VALUES (' . $val . ')';
1008
+	  $query = $this->query($query);
1009
+
1010
+	  if ($query){
1011
+		if(! $this->pdo){
1012
+		  $this->connect();
1013
+		}
1014
+		$this->insertId = $this->pdo->lastInsertId();
1015
+		return $this->insertId();
1016
+	  }
1017
+	  else{
1018 1018
 		  return false;
1019
-      }
1020
-    }
1021
-
1022
-    /**
1023
-     * Update record in the database
1024
-     * @param  array   $data   the record data if is empty will use the $this->data array.
1025
-     * @param  boolean $escape  whether to escape or not the values
1026
-     * @return mixed          the update status
1027
-     */
1028
-    public function update($data = array(), $escape = true){
1029
-      $query = 'UPDATE ' . $this->from . ' SET ';
1030
-      $values = array();
1031
-      if(empty($data) && $this->getData()){
1032
-        foreach ($this->getData() as $column => $val){
1033
-          $values[] = $column . ' = ' . $val;
1034
-        }
1035
-      }
1036
-      else{
1037
-        foreach ($data as $column => $val){
1038
-          $values[] = $column . '=' . ($escape ? $this->escape($val) : $val);
1039
-        }
1040
-      }
1041
-      $query .= implode(', ', $values);
1042
-      if (! empty($this->where)){
1043
-        $query .= ' WHERE ' . $this->where;
1044
-      }
1045
-
1046
-      if (! empty($this->orderBy)){
1047
-        $query .= ' ORDER BY ' . $this->orderBy;
1048
-      }
1049
-
1050
-      if (! empty($this->limit)){
1051
-        $query .= ' LIMIT ' . $this->limit;
1052
-      }
1053
-      return $this->query($query);
1054
-    }
1055
-
1056
-    /**
1057
-     * Delete the record in database
1058
-     * @return mixed the delete status
1059
-     */
1060
-    public function delete(){
1061
-    	$query = 'DELETE FROM ' . $this->from;
1062
-
1063
-    	if (! empty($this->where)){
1064
-    		$query .= ' WHERE ' . $this->where;
1065
-      	}
1066
-
1067
-    	if (! empty($this->orderBy)){
1068
-    	  $query .= ' ORDER BY ' . $this->orderBy;
1069
-      	}
1070
-
1071
-    	if (! empty($this->limit)){
1072
-    		$query .= ' LIMIT ' . $this->limit;
1073
-      	}
1074
-
1075
-    	if($query == 'DELETE FROM ' . $this->from && $this->config['driver'] != 'sqlite'){  
1076
-    		$query = 'TRUNCATE TABLE ' . $this->from;
1077
-      }
1078
-    	return $this->query($query);
1079
-    }
1080
-
1081
-    /**
1082
-     * Execute an SQL query
1083
-     * @param  string  $query the query SQL string
1084
-     * @param  boolean $all   whether to return all record or not
1085
-     * @param  boolean $array return the result as array
1086
-     * @return mixed         the query result
1087
-     */
1088
-    public function query($query, $all = true, $array = false){
1089
-      $this->reset();
1090
-      if(is_array($all)){
1091
-        $x = explode('?', $query);
1092
-        $q = '';
1093
-        foreach($x as $k => $v){
1094
-          if(! empty($v)){
1095
-            $q .= $v . (isset($all[$k]) ? $this->escape($all[$k]) : '');
1096
-          }
1097
-        }
1098
-        $query = $q;
1099
-      }
1100
-
1101
-      $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query));
1102
-      $sqlSELECTQuery = stristr($this->query, 'SELECT');
1103
-      $this->logger->info('Execute SQL query ['.$this->query.'], return type: ' . ($array?'ARRAY':'OBJECT') .', return as list: ' . ($all ? 'YES':'NO'));
1104
-      //cache expire time
1019
+	  }
1020
+	}
1021
+
1022
+	/**
1023
+	 * Update record in the database
1024
+	 * @param  array   $data   the record data if is empty will use the $this->data array.
1025
+	 * @param  boolean $escape  whether to escape or not the values
1026
+	 * @return mixed          the update status
1027
+	 */
1028
+	public function update($data = array(), $escape = true){
1029
+	  $query = 'UPDATE ' . $this->from . ' SET ';
1030
+	  $values = array();
1031
+	  if(empty($data) && $this->getData()){
1032
+		foreach ($this->getData() as $column => $val){
1033
+		  $values[] = $column . ' = ' . $val;
1034
+		}
1035
+	  }
1036
+	  else{
1037
+		foreach ($data as $column => $val){
1038
+		  $values[] = $column . '=' . ($escape ? $this->escape($val) : $val);
1039
+		}
1040
+	  }
1041
+	  $query .= implode(', ', $values);
1042
+	  if (! empty($this->where)){
1043
+		$query .= ' WHERE ' . $this->where;
1044
+	  }
1045
+
1046
+	  if (! empty($this->orderBy)){
1047
+		$query .= ' ORDER BY ' . $this->orderBy;
1048
+	  }
1049
+
1050
+	  if (! empty($this->limit)){
1051
+		$query .= ' LIMIT ' . $this->limit;
1052
+	  }
1053
+	  return $this->query($query);
1054
+	}
1055
+
1056
+	/**
1057
+	 * Delete the record in database
1058
+	 * @return mixed the delete status
1059
+	 */
1060
+	public function delete(){
1061
+		$query = 'DELETE FROM ' . $this->from;
1062
+
1063
+		if (! empty($this->where)){
1064
+			$query .= ' WHERE ' . $this->where;
1065
+	  	}
1066
+
1067
+		if (! empty($this->orderBy)){
1068
+		  $query .= ' ORDER BY ' . $this->orderBy;
1069
+	  	}
1070
+
1071
+		if (! empty($this->limit)){
1072
+			$query .= ' LIMIT ' . $this->limit;
1073
+	  	}
1074
+
1075
+		if($query == 'DELETE FROM ' . $this->from && $this->config['driver'] != 'sqlite'){  
1076
+			$query = 'TRUNCATE TABLE ' . $this->from;
1077
+	  }
1078
+		return $this->query($query);
1079
+	}
1080
+
1081
+	/**
1082
+	 * Execute an SQL query
1083
+	 * @param  string  $query the query SQL string
1084
+	 * @param  boolean $all   whether to return all record or not
1085
+	 * @param  boolean $array return the result as array
1086
+	 * @return mixed         the query result
1087
+	 */
1088
+	public function query($query, $all = true, $array = false){
1089
+	  $this->reset();
1090
+	  if(is_array($all)){
1091
+		$x = explode('?', $query);
1092
+		$q = '';
1093
+		foreach($x as $k => $v){
1094
+		  if(! empty($v)){
1095
+			$q .= $v . (isset($all[$k]) ? $this->escape($all[$k]) : '');
1096
+		  }
1097
+		}
1098
+		$query = $q;
1099
+	  }
1100
+
1101
+	  $this->query = preg_replace('/\s\s+|\t\t+/', ' ', trim($query));
1102
+	  $sqlSELECTQuery = stristr($this->query, 'SELECT');
1103
+	  $this->logger->info('Execute SQL query ['.$this->query.'], return type: ' . ($array?'ARRAY':'OBJECT') .', return as list: ' . ($all ? 'YES':'NO'));
1104
+	  //cache expire time
1105 1105
   	  $cacheExpire = $this->temporaryCacheTtl;
1106 1106
   	  
1107 1107
   	  //return to the initial cache time
1108 1108
   	  $this->temporaryCacheTtl = $this->cacheTtl;
1109 1109
   	  
1110 1110
   	  //config for cache
1111
-        $cacheEnable = get_config('cache_enable');
1111
+		$cacheEnable = get_config('cache_enable');
1112 1112
   	  
1113 1113
   	  //the database cache content
1114
-        $cacheContent = null;
1114
+		$cacheContent = null;
1115 1115
   	  
1116 1116
   	  //this database query cache key
1117
-        $cacheKey = null;
1117
+		$cacheKey = null;
1118 1118
   	  
1119 1119
   	  //the cache manager instance
1120
-      $cacheInstance = null;
1120
+	  $cacheInstance = null;
1121 1121
   	  
1122 1122
   	  //if can use cache feature for this query
1123 1123
   	  $dbCacheStatus = $cacheEnable && $cacheExpire > 0;
1124 1124
 	  
1125
-      if ($dbCacheStatus && $sqlSELECTQuery){
1126
-        $this->logger->info('The cache is enabled for this query, try to get result from cache'); 
1127
-        $cacheKey = md5($query . $all . $array);
1128
-        if(is_object($this->cacheInstance)){
1129
-          $cacheInstance = $this->cacheInstance;
1130
-        }
1131
-        else{
1132
-          $obj = & get_instance();
1133
-          $cacheInstance = $obj->cache;  
1134
-        }
1135
-        $cacheContent = $cacheInstance->get($cacheKey);        
1136
-      }
1137
-      else{
1125
+	  if ($dbCacheStatus && $sqlSELECTQuery){
1126
+		$this->logger->info('The cache is enabled for this query, try to get result from cache'); 
1127
+		$cacheKey = md5($query . $all . $array);
1128
+		if(is_object($this->cacheInstance)){
1129
+		  $cacheInstance = $this->cacheInstance;
1130
+		}
1131
+		else{
1132
+		  $obj = & get_instance();
1133
+		  $cacheInstance = $obj->cache;  
1134
+		}
1135
+		$cacheContent = $cacheInstance->get($cacheKey);        
1136
+	  }
1137
+	  else{
1138 1138
 		  $this->logger->info('The cache is not enabled for this query or is not the SELECT query, get the result directly from real database');
1139
-      }
1139
+	  }
1140 1140
 
1141
-      if(! $this->pdo){
1142
-        $this->connect();
1143
-      }
1141
+	  if(! $this->pdo){
1142
+		$this->connect();
1143
+	  }
1144 1144
       
1145
-      if (! $cacheContent && $sqlSELECTQuery){
1146
-		    //for database query execution time
1147
-        $benchmarkMarkerKey = md5($query . $all . $array);
1148
-        $bench = null;
1149
-        if(is_object($this->benchmarkInstance)){
1150
-          $bench = $this->benchmarkInstance;
1151
-        }
1152
-        else{
1153
-          $obj = & get_instance();
1154
-          $bench = $obj->benchmark;  
1155
-        }
1156
-        $bench->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')');
1157
-        //Now execute the query
1158
-		    $sqlQuery = $this->pdo->query($this->query);
1145
+	  if (! $cacheContent && $sqlSELECTQuery){
1146
+			//for database query execution time
1147
+		$benchmarkMarkerKey = md5($query . $all . $array);
1148
+		$bench = null;
1149
+		if(is_object($this->benchmarkInstance)){
1150
+		  $bench = $this->benchmarkInstance;
1151
+		}
1152
+		else{
1153
+		  $obj = & get_instance();
1154
+		  $bench = $obj->benchmark;  
1155
+		}
1156
+		$bench->mark('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')');
1157
+		//Now execute the query
1158
+			$sqlQuery = $this->pdo->query($this->query);
1159 1159
         
1160
-    		//get response time for this query
1161
-        $responseTime = $bench->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')');
1162
-	     	//TODO use the configuration value for the high response time currently is 1 second
1163
-        if($responseTime >= 1 ){
1164
-            $this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.');
1165
-        }
1166
-        if ($sqlQuery){
1167
-            //if need return all result like list of record
1168
-            if ($all){
1169
-    				    $this->result = ($array === false) ? $sqlQuery->fetchAll(PDO::FETCH_OBJ) : $sqlQuery->fetchAll(PDO::FETCH_ASSOC);
1170
-    		    }
1171
-            else{
1172
-				        $this->result = ($array === false) ? $sqlQuery->fetch(PDO::FETCH_OBJ) : $sqlQuery->fetch(PDO::FETCH_ASSOC);
1173
-            }
1174
-            //Sqlite and pgsql always return 0 when using rowCount()
1175
-            if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){
1176
-              $this->numRows = count($this->result);  
1177
-            }
1178
-            else{
1179
-              $this->numRows = $sqlQuery->rowCount(); 
1180
-            }
1181
-
1182
-          if ($dbCacheStatus && $sqlSELECTQuery){
1183
-              $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use');
1184
-              $cacheInstance->set($cacheKey, $this->result, $cacheExpire);
1185
-          }
1186
-        }
1187
-        else{
1188
-          $error = $this->pdo->errorInfo();
1189
-          $this->error = isset($error[2]) ? $error[2] : '';
1190
-          $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error));
1191
-          $this->error();
1192
-        }
1193
-      }
1194
-      else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){
1195
-    		$queryStr = $this->pdo->query($this->query);
1196
-    		if($queryStr){
1197
-          //Sqlite and pgsql always return 0 when using rowCount()
1198
-          if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){
1199
-            $this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE
1200
-            $this->numRows = 1;  
1201
-          }
1202
-          else{
1203
-              $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE
1204
-              $this->numRows = $queryStr->rowCount(); 
1205
-          }
1206
-    		}
1207
-        if (! $this->result){
1208
-          $error = $this->pdo->errorInfo();
1209
-          $this->error = isset($error[2]) ? $error[2] : '';
1210
-          $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error));
1211
-          $this->error();
1212
-        }
1213
-      }
1214
-      else{
1215
-        $this->logger->info('The result for query [' .$this->query. '] already cached use it');
1216
-        $this->result = $cacheContent;
1217
-	     	$this->numRows = count($this->result);
1218
-      }
1219
-      $this->queryCount++;
1220
-      if(! $this->result){
1221
-        $this->logger->info('No result where found for the query [' . $query . ']');
1222
-      }
1223
-      return $this->result;
1224
-    }
1225
-
1226
-    /**
1227
-     * Set database cache time to live
1228
-     * @param integer $ttl the cache time to live in second
1229
-     * @return object        the current Database instance
1230
-     */
1231
-    public function setCache($ttl = 0){
1232
-      if($ttl > 0){
1233
-        $this->cacheTtl = $ttl;
1234
-		    $this->temporaryCacheTtl = $ttl;
1235
-      }
1236
-      return $this;
1237
-    }
1160
+			//get response time for this query
1161
+		$responseTime = $bench->elapsedTime('DATABASE_QUERY_START(' . $benchmarkMarkerKey . ')', 'DATABASE_QUERY_END(' . $benchmarkMarkerKey . ')');
1162
+		 	//TODO use the configuration value for the high response time currently is 1 second
1163
+		if($responseTime >= 1 ){
1164
+			$this->logger->warning('High response time while processing database query [' .$query. ']. The response time is [' .$responseTime. '] sec.');
1165
+		}
1166
+		if ($sqlQuery){
1167
+			//if need return all result like list of record
1168
+			if ($all){
1169
+						$this->result = ($array === false) ? $sqlQuery->fetchAll(PDO::FETCH_OBJ) : $sqlQuery->fetchAll(PDO::FETCH_ASSOC);
1170
+				}
1171
+			else{
1172
+						$this->result = ($array === false) ? $sqlQuery->fetch(PDO::FETCH_OBJ) : $sqlQuery->fetch(PDO::FETCH_ASSOC);
1173
+			}
1174
+			//Sqlite and pgsql always return 0 when using rowCount()
1175
+			if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){
1176
+			  $this->numRows = count($this->result);  
1177
+			}
1178
+			else{
1179
+			  $this->numRows = $sqlQuery->rowCount(); 
1180
+			}
1181
+
1182
+		  if ($dbCacheStatus && $sqlSELECTQuery){
1183
+			  $this->logger->info('Save the result for query [' .$this->query. '] into cache for future use');
1184
+			  $cacheInstance->set($cacheKey, $this->result, $cacheExpire);
1185
+		  }
1186
+		}
1187
+		else{
1188
+		  $error = $this->pdo->errorInfo();
1189
+		  $this->error = isset($error[2]) ? $error[2] : '';
1190
+		  $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error));
1191
+		  $this->error();
1192
+		}
1193
+	  }
1194
+	  else if ((! $cacheContent && !$sqlSELECTQuery) || ($cacheContent && !$sqlSELECTQuery)){
1195
+			$queryStr = $this->pdo->query($this->query);
1196
+			if($queryStr){
1197
+		  //Sqlite and pgsql always return 0 when using rowCount()
1198
+		  if(in_array($this->config['driver'], array('sqlite', 'pgsql'))){
1199
+			$this->result = 1; //to test the result for the query like UPDATE, INSERT, DELETE
1200
+			$this->numRows = 1;  
1201
+		  }
1202
+		  else{
1203
+			  $this->result = $queryStr->rowCount() >= 0; //to test the result for the query like UPDATE, INSERT, DELETE
1204
+			  $this->numRows = $queryStr->rowCount(); 
1205
+		  }
1206
+			}
1207
+		if (! $this->result){
1208
+		  $error = $this->pdo->errorInfo();
1209
+		  $this->error = isset($error[2]) ? $error[2] : '';
1210
+		  $this->logger->fatal('The database query execution got error: ' . stringfy_vars($error));
1211
+		  $this->error();
1212
+		}
1213
+	  }
1214
+	  else{
1215
+		$this->logger->info('The result for query [' .$this->query. '] already cached use it');
1216
+		$this->result = $cacheContent;
1217
+		 	$this->numRows = count($this->result);
1218
+	  }
1219
+	  $this->queryCount++;
1220
+	  if(! $this->result){
1221
+		$this->logger->info('No result where found for the query [' . $query . ']');
1222
+	  }
1223
+	  return $this->result;
1224
+	}
1225
+
1226
+	/**
1227
+	 * Set database cache time to live
1228
+	 * @param integer $ttl the cache time to live in second
1229
+	 * @return object        the current Database instance
1230
+	 */
1231
+	public function setCache($ttl = 0){
1232
+	  if($ttl > 0){
1233
+		$this->cacheTtl = $ttl;
1234
+			$this->temporaryCacheTtl = $ttl;
1235
+	  }
1236
+	  return $this;
1237
+	}
1238 1238
 	
1239 1239
 	/**
1240 1240
 	 * Enabled cache temporary for the current query not globally	
@@ -1242,186 +1242,186 @@  discard block
 block discarded – undo
1242 1242
 	 * @return object        the current Database instance
1243 1243
 	 */
1244 1244
 	public function cached($ttl = 0){
1245
-      if($ttl > 0){
1246
-        $this->temporaryCacheTtl = $ttl;
1247
-      }
1245
+	  if($ttl > 0){
1246
+		$this->temporaryCacheTtl = $ttl;
1247
+	  }
1248 1248
 	  return $this;
1249
-    }
1250
-
1251
-    /**
1252
-     * Escape the data before execute query useful for security.
1253
-     * @param  mixed $data the data to be escaped
1254
-     * @return mixed       the data after escaped
1255
-     */
1256
-    public function escape($data){
1257
-      if(is_null($data)){
1258
-        return null;
1259
-      }
1260
-      if(! $this->pdo){
1261
-        $this->connect();
1262
-      }
1263
-      return $this->pdo->quote(trim($data));
1264
-    }
1265
-
1266
-    /**
1267
-     * Return the number query executed count for the current request
1268
-     * @return int
1269
-     */
1270
-    public function queryCount(){
1271
-      return $this->queryCount;
1272
-    }
1273
-
1274
-    /**
1275
-     * Return the current query SQL string
1276
-     * @return string
1277
-     */
1278
-    public function getQuery(){
1279
-      return $this->query;
1280
-    }
1281
-
1282
-    /**
1283
-     * Return the application database name
1284
-     * @return string
1285
-     */
1286
-    public function getDatabaseName(){
1287
-      return $this->databaseName;
1288
-    }
1289
-
1290
-     /**
1291
-     * Return the database configuration
1292
-     * @return array
1293
-     */
1294
-    public  function getDatabaseConfiguration(){
1295
-      return $this->config;
1296
-    }
1297
-
1298
-    /**
1299
-     * set the database configuration
1300
-     * @param array $config the configuration
1301
-     */
1302
-    public function setDatabaseConfiguration(array $config){
1303
-      $this->config = array_merge($this->config, $config);
1304
-      $this->prefix = $this->config['prefix'];
1305
-      $this->databaseName = $this->config['database'];
1306
-      $this->logger->info('The database configuration are listed below: ' . stringfy_vars(array_merge($this->config, array('password' => string_hidden($this->config['password'])))));
1307
-      return $this;
1308
-    }
1309
-
1310
-    /**
1311
-     * Return the PDO instance
1312
-     * @return PDO
1313
-     */
1314
-    public function getPdo(){
1315
-      return $this->pdo;
1316
-    }
1317
-
1318
-    /**
1319
-     * Set the PDO instance
1320
-     * @param PDO $pdo the pdo object
1321
-     */
1322
-    public function setPdo(PDO $pdo){
1323
-      $this->pdo = $pdo;
1324
-      return $this;
1325
-    }
1326
-
1327
-
1328
-    /**
1329
-     * Return the Log instance
1330
-     * @return Log
1331
-     */
1332
-    public function getLogger(){
1333
-      return $this->logger;
1334
-    }
1335
-
1336
-    /**
1337
-     * Set the log instance
1338
-     * @param Log $logger the log object
1339
-     */
1340
-    public function setLogger($logger){
1341
-      $this->logger = $logger;
1342
-      return $this;
1343
-    }
1344
-
1345
-     /**
1346
-     * Return the cache instance
1347
-     * @return CacheInterface
1348
-     */
1349
-    public function getCacheInstance(){
1350
-      return $this->cacheInstance;
1351
-    }
1352
-
1353
-    /**
1354
-     * Set the cache instance
1355
-     * @param CacheInterface $cache the cache object
1356
-     */
1357
-    public function setCacheInstance($cache){
1358
-      $this->cacheInstance = $cache;
1359
-      return $this;
1360
-    }
1361
-
1362
-    /**
1363
-     * Return the benchmark instance
1364
-     * @return Benchmark
1365
-     */
1366
-    public function getBenchmark(){
1367
-      return $this->benchmarkInstance;
1368
-    }
1369
-
1370
-    /**
1371
-     * Set the benchmark instance
1372
-     * @param Benchmark $cache the cache object
1373
-     */
1374
-    public function setBenchmark($benchmark){
1375
-      $this->benchmarkInstance = $benchmark;
1376
-      return $this;
1377
-    }
1378
-
1379
-    /**
1380
-     * Return the data to be used for insert, update, etc.
1381
-     * @return array
1382
-     */
1383
-    public function getData(){
1384
-      return $this->data;
1385
-    }
1386
-
1387
-    /**
1388
-     * Set the data to be used for insert, update, etc.
1389
-     * @param string $key the data key identified
1390
-     * @param mixed $value the data value
1391
-     * @param boolean $escape whether to escape or not the $value
1392
-     * @return object        the current Database instance
1393
-     */
1394
-    public function setData($key, $value, $escape = true){
1395
-      $this->data[$key] = $escape ? $this->escape($value) : $value;
1396
-      return $this;
1397
-    }
1249
+	}
1250
+
1251
+	/**
1252
+	 * Escape the data before execute query useful for security.
1253
+	 * @param  mixed $data the data to be escaped
1254
+	 * @return mixed       the data after escaped
1255
+	 */
1256
+	public function escape($data){
1257
+	  if(is_null($data)){
1258
+		return null;
1259
+	  }
1260
+	  if(! $this->pdo){
1261
+		$this->connect();
1262
+	  }
1263
+	  return $this->pdo->quote(trim($data));
1264
+	}
1265
+
1266
+	/**
1267
+	 * Return the number query executed count for the current request
1268
+	 * @return int
1269
+	 */
1270
+	public function queryCount(){
1271
+	  return $this->queryCount;
1272
+	}
1273
+
1274
+	/**
1275
+	 * Return the current query SQL string
1276
+	 * @return string
1277
+	 */
1278
+	public function getQuery(){
1279
+	  return $this->query;
1280
+	}
1281
+
1282
+	/**
1283
+	 * Return the application database name
1284
+	 * @return string
1285
+	 */
1286
+	public function getDatabaseName(){
1287
+	  return $this->databaseName;
1288
+	}
1289
+
1290
+	 /**
1291
+	  * Return the database configuration
1292
+	  * @return array
1293
+	  */
1294
+	public  function getDatabaseConfiguration(){
1295
+	  return $this->config;
1296
+	}
1297
+
1298
+	/**
1299
+	 * set the database configuration
1300
+	 * @param array $config the configuration
1301
+	 */
1302
+	public function setDatabaseConfiguration(array $config){
1303
+	  $this->config = array_merge($this->config, $config);
1304
+	  $this->prefix = $this->config['prefix'];
1305
+	  $this->databaseName = $this->config['database'];
1306
+	  $this->logger->info('The database configuration are listed below: ' . stringfy_vars(array_merge($this->config, array('password' => string_hidden($this->config['password'])))));
1307
+	  return $this;
1308
+	}
1309
+
1310
+	/**
1311
+	 * Return the PDO instance
1312
+	 * @return PDO
1313
+	 */
1314
+	public function getPdo(){
1315
+	  return $this->pdo;
1316
+	}
1317
+
1318
+	/**
1319
+	 * Set the PDO instance
1320
+	 * @param PDO $pdo the pdo object
1321
+	 */
1322
+	public function setPdo(PDO $pdo){
1323
+	  $this->pdo = $pdo;
1324
+	  return $this;
1325
+	}
1326
+
1327
+
1328
+	/**
1329
+	 * Return the Log instance
1330
+	 * @return Log
1331
+	 */
1332
+	public function getLogger(){
1333
+	  return $this->logger;
1334
+	}
1335
+
1336
+	/**
1337
+	 * Set the log instance
1338
+	 * @param Log $logger the log object
1339
+	 */
1340
+	public function setLogger($logger){
1341
+	  $this->logger = $logger;
1342
+	  return $this;
1343
+	}
1344
+
1345
+	 /**
1346
+	  * Return the cache instance
1347
+	  * @return CacheInterface
1348
+	  */
1349
+	public function getCacheInstance(){
1350
+	  return $this->cacheInstance;
1351
+	}
1352
+
1353
+	/**
1354
+	 * Set the cache instance
1355
+	 * @param CacheInterface $cache the cache object
1356
+	 */
1357
+	public function setCacheInstance($cache){
1358
+	  $this->cacheInstance = $cache;
1359
+	  return $this;
1360
+	}
1361
+
1362
+	/**
1363
+	 * Return the benchmark instance
1364
+	 * @return Benchmark
1365
+	 */
1366
+	public function getBenchmark(){
1367
+	  return $this->benchmarkInstance;
1368
+	}
1369
+
1370
+	/**
1371
+	 * Set the benchmark instance
1372
+	 * @param Benchmark $cache the cache object
1373
+	 */
1374
+	public function setBenchmark($benchmark){
1375
+	  $this->benchmarkInstance = $benchmark;
1376
+	  return $this;
1377
+	}
1378
+
1379
+	/**
1380
+	 * Return the data to be used for insert, update, etc.
1381
+	 * @return array
1382
+	 */
1383
+	public function getData(){
1384
+	  return $this->data;
1385
+	}
1386
+
1387
+	/**
1388
+	 * Set the data to be used for insert, update, etc.
1389
+	 * @param string $key the data key identified
1390
+	 * @param mixed $value the data value
1391
+	 * @param boolean $escape whether to escape or not the $value
1392
+	 * @return object        the current Database instance
1393
+	 */
1394
+	public function setData($key, $value, $escape = true){
1395
+	  $this->data[$key] = $escape ? $this->escape($value) : $value;
1396
+	  return $this;
1397
+	}
1398 1398
 
1399 1399
 
1400 1400
   /**
1401 1401
    * Reset the database class attributs to the initail values before each query.
1402 1402
    */
1403 1403
   private function reset(){
1404
-    $this->select   = '*';
1405
-    $this->from     = null;
1406
-    $this->where    = null;
1407
-    $this->limit    = null;
1408
-    $this->orderBy  = null;
1409
-    $this->groupBy  = null;
1410
-    $this->having   = null;
1411
-    $this->join     = null;
1412
-    $this->numRows  = 0;
1413
-    $this->insertId = null;
1414
-    $this->query    = null;
1415
-    $this->error    = null;
1416
-    $this->result   = array();
1417
-    $this->data     = array();
1404
+	$this->select   = '*';
1405
+	$this->from     = null;
1406
+	$this->where    = null;
1407
+	$this->limit    = null;
1408
+	$this->orderBy  = null;
1409
+	$this->groupBy  = null;
1410
+	$this->having   = null;
1411
+	$this->join     = null;
1412
+	$this->numRows  = 0;
1413
+	$this->insertId = null;
1414
+	$this->query    = null;
1415
+	$this->error    = null;
1416
+	$this->result   = array();
1417
+	$this->data     = array();
1418 1418
   }
1419 1419
 
1420 1420
   /**
1421 1421
    * The class destructor
1422 1422
    */
1423 1423
   public function __destruct(){
1424
-    $this->pdo = null;
1424
+	$this->pdo = null;
1425 1425
   }
1426 1426
 
1427 1427
 }
Please login to merge, or discard this patch.