blob: b7f43945c04bb48e707587831beacd4f4aefe81d [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
WanWizard7219c072011-12-28 14:09:05 +01008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
WanWizard7219c072011-12-28 14:09:05 +010010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Active Record Class
32 *
33 * This is the platform-independent base Active Record implementation class.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_active_record extends CI_DB_driver {
42
WanWizard7219c072011-12-28 14:09:05 +010043 protected $return_delete_sql = FALSE;
44 protected $reset_delete_data = FALSE;
45
46 protected $ar_select = array();
47 protected $ar_distinct = FALSE;
48 protected $ar_from = array();
49 protected $ar_join = array();
50 protected $ar_where = array();
51 protected $ar_like = array();
52 protected $ar_groupby = array();
53 protected $ar_having = array();
54 protected $ar_keys = array();
55 protected $ar_limit = FALSE;
56 protected $ar_offset = FALSE;
57 protected $ar_order = FALSE;
58 protected $ar_orderby = array();
59 protected $ar_set = array();
60 protected $ar_wherein = array();
61 protected $ar_aliased_tables = array();
62 protected $ar_store_array = array();
63 protected $ar_where_group_started = FALSE;
64 protected $ar_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +020065
Derek Allard2067d1a2008-11-13 22:59:24 +000066 // Active Record Caching variables
WanWizard7219c072011-12-28 14:09:05 +010067 protected $ar_caching = FALSE;
68 protected $ar_cache_exists = array();
69 protected $ar_cache_select = array();
70 protected $ar_cache_from = array();
71 protected $ar_cache_join = array();
72 protected $ar_cache_where = array();
73 protected $ar_cache_like = array();
74 protected $ar_cache_groupby = array();
75 protected $ar_cache_having = array();
76 protected $ar_cache_orderby = array();
77 protected $ar_cache_set = array();
78
79 protected $ar_no_escape = array();
80 protected $ar_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000081
82 // --------------------------------------------------------------------
83
84 /**
85 * Select
86 *
87 * Generates the SELECT portion of the query
88 *
Derek Allard2067d1a2008-11-13 22:59:24 +000089 * @param string
90 * @return object
91 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060092 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000093 {
Derek Allard2067d1a2008-11-13 22:59:24 +000094 if (is_string($select))
95 {
96 $select = explode(',', $select);
97 }
98
99 foreach ($select as $val)
100 {
101 $val = trim($val);
102
103 if ($val != '')
104 {
105 $this->ar_select[] = $val;
Greg Akere156c6e2011-04-20 16:03:04 -0500106 $this->ar_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000107
108 if ($this->ar_caching === TRUE)
109 {
110 $this->ar_cache_select[] = $val;
111 $this->ar_cache_exists[] = 'select';
Greg Aker2e1837a2011-05-06 12:17:04 -0500112 $this->ar_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
114 }
115 }
116 return $this;
117 }
118
119 // --------------------------------------------------------------------
120
121 /**
122 * Select Max
123 *
124 * Generates a SELECT MAX(field) portion of a query
125 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 * @param string the field
127 * @param string an alias
128 * @return object
129 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600130 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
132 return $this->_max_min_avg_sum($select, $alias, 'MAX');
133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 // --------------------------------------------------------------------
136
137 /**
138 * Select Min
139 *
140 * Generates a SELECT MIN(field) portion of a query
141 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 * @param string the field
143 * @param string an alias
144 * @return object
145 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600146 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
148 return $this->_max_min_avg_sum($select, $alias, 'MIN');
149 }
150
151 // --------------------------------------------------------------------
152
153 /**
154 * Select Average
155 *
156 * Generates a SELECT AVG(field) portion of a query
157 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 * @param string the field
159 * @param string an alias
160 * @return object
161 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600162 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
164 return $this->_max_min_avg_sum($select, $alias, 'AVG');
165 }
166
167 // --------------------------------------------------------------------
168
169 /**
170 * Select Sum
171 *
172 * Generates a SELECT SUM(field) portion of a query
173 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 * @param string the field
175 * @param string an alias
176 * @return object
177 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600178 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
180 return $this->_max_min_avg_sum($select, $alias, 'SUM');
181 }
182
183 // --------------------------------------------------------------------
184
185 /**
186 * Processing Function for the four functions above:
187 *
188 * select_max()
189 * select_min()
190 * select_avg()
Derek Jones37f4b9c2011-07-01 17:56:50 -0500191 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200192 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 * @param string the field
194 * @param string an alias
195 * @return object
196 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600197 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
199 if ( ! is_string($select) OR $select == '')
200 {
201 $this->display_error('db_invalid_query');
202 }
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
207 {
208 show_error('Invalid function type: '.$type);
209 }
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 if ($alias == '')
212 {
213 $alias = $this->_create_alias_from_table(trim($select));
214 }
Barry Mienydd671972010-10-04 16:33:58 +0200215
Juan José González8b4d83b2011-09-27 17:21:14 -0500216 $sql = $this->_protect_identifiers($type.'('.trim($select).')').' AS '.$this->_protect_identifiers(trim($alias));
Derek Allard2067d1a2008-11-13 22:59:24 +0000217
218 $this->ar_select[] = $sql;
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 if ($this->ar_caching === TRUE)
221 {
222 $this->ar_cache_select[] = $sql;
223 $this->ar_cache_exists[] = 'select';
224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 return $this;
227 }
228
229 // --------------------------------------------------------------------
230
231 /**
232 * Determines the alias name based on the table
233 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 * @param string
235 * @return string
236 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600237 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
239 if (strpos($item, '.') !== FALSE)
240 {
241 return end(explode('.', $item));
242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 return $item;
245 }
246
247 // --------------------------------------------------------------------
248
249 /**
250 * DISTINCT
251 *
252 * Sets a flag which tells the query string compiler to add DISTINCT
253 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 * @param bool
255 * @return object
256 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600257 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
259 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
260 return $this;
261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 // --------------------------------------------------------------------
264
265 /**
266 * From
267 *
268 * Generates the FROM portion of the query
269 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 * @param mixed can be a string or array
271 * @return object
272 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600273 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 {
275 foreach ((array)$from as $val)
276 {
277 if (strpos($val, ',') !== FALSE)
278 {
279 foreach (explode(',', $val) as $v)
280 {
281 $v = trim($v);
282 $this->_track_aliases($v);
283
284 $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 if ($this->ar_caching === TRUE)
287 {
288 $this->ar_cache_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
289 $this->ar_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200290 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
292
293 }
294 else
295 {
296 $val = trim($val);
297
Derek Jones37f4b9c2011-07-01 17:56:50 -0500298 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200299 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 if ($this->ar_caching === TRUE)
305 {
306 $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
307 $this->ar_cache_exists[] = 'from';
308 }
309 }
310 }
311
312 return $this;
313 }
314
315 // --------------------------------------------------------------------
316
317 /**
318 * Join
319 *
320 * Generates the JOIN portion of the query
321 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 * @param string
323 * @param string the join condition
324 * @param string the type of join
325 * @return object
326 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600327 public function join($table, $cond, $type = '')
Barry Mienydd671972010-10-04 16:33:58 +0200328 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 if ($type != '')
330 {
331 $type = strtoupper(trim($type));
332
333 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
334 {
335 $type = '';
336 }
337 else
338 {
339 $type .= ' ';
340 }
341 }
342
Derek Jones37f4b9c2011-07-01 17:56:50 -0500343 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200344 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 $this->_track_aliases($table);
346
347 // Strip apart the condition and protect the identifiers
348 if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
349 {
350 $match[1] = $this->_protect_identifiers($match[1]);
351 $match[3] = $this->_protect_identifiers($match[3]);
Barry Mienydd671972010-10-04 16:33:58 +0200352
353 $cond = $match[1].$match[2].$match[3];
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 // Assemble the JOIN statement
357 $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
358
359 $this->ar_join[] = $join;
360 if ($this->ar_caching === TRUE)
361 {
362 $this->ar_cache_join[] = $join;
363 $this->ar_cache_exists[] = 'join';
364 }
365
366 return $this;
367 }
368
369 // --------------------------------------------------------------------
370
371 /**
372 * Where
373 *
374 * Generates the WHERE portion of the query. Separates
375 * multiple calls with AND
376 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 * @param mixed
378 * @param mixed
379 * @return object
380 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600381 public function where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
383 return $this->_where($key, $value, 'AND ', $escape);
384 }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 // --------------------------------------------------------------------
387
388 /**
389 * OR Where
390 *
391 * Generates the WHERE portion of the query. Separates
392 * multiple calls with OR
393 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 * @param mixed
395 * @param mixed
396 * @return object
397 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600398 public function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
400 return $this->_where($key, $value, 'OR ', $escape);
401 }
402
403 // --------------------------------------------------------------------
404
405 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 * Where
407 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600408 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 * @param mixed
411 * @param mixed
412 * @param string
413 * @return object
414 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600415 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
WanWizard7219c072011-12-28 14:09:05 +0100417 $type = $this->_group_get_type($type);
418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 if ( ! is_array($key))
420 {
421 $key = array($key => $value);
422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 // If the escape value was not set will will base it on the global setting
425 if ( ! is_bool($escape))
426 {
427 $escape = $this->_protect_identifiers;
428 }
429
430 foreach ($key as $k => $v)
431 {
432 $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type;
433
434 if (is_null($v) && ! $this->_has_operator($k))
435 {
436 // value appears not to have been set, assign the test to IS NULL
437 $k .= ' IS NULL';
438 }
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 if ( ! is_null($v))
441 {
442 if ($escape === TRUE)
443 {
444 $k = $this->_protect_identifiers($k, FALSE, $escape);
Barry Mienydd671972010-10-04 16:33:58 +0200445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 $v = ' '.$this->escape($v);
447 }
WanWizard7219c072011-12-28 14:09:05 +0100448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 if ( ! $this->_has_operator($k))
450 {
Greg Akere156c6e2011-04-20 16:03:04 -0500451 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 }
453 }
454 else
455 {
Barry Mienydd671972010-10-04 16:33:58 +0200456 $k = $this->_protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
458
459 $this->ar_where[] = $prefix.$k.$v;
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 if ($this->ar_caching === TRUE)
462 {
463 $this->ar_cache_where[] = $prefix.$k.$v;
464 $this->ar_cache_exists[] = 'where';
465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 return $this;
470 }
471
472 // --------------------------------------------------------------------
473
474 /**
475 * Where_in
476 *
477 * Generates a WHERE field IN ('item', 'item') SQL query joined with
478 * AND if appropriate
479 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 * @param string The field to search
481 * @param array The values searched on
482 * @return object
483 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600484 public function where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
486 return $this->_where_in($key, $values);
487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 // --------------------------------------------------------------------
490
491 /**
492 * Where_in_or
493 *
494 * Generates a WHERE field IN ('item', 'item') SQL query joined with
495 * OR if appropriate
496 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 * @param string The field to search
498 * @param array The values searched on
499 * @return object
500 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600501 public function or_where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 {
503 return $this->_where_in($key, $values, FALSE, 'OR ');
504 }
505
506 // --------------------------------------------------------------------
507
508 /**
509 * Where_not_in
510 *
511 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
512 * with AND if appropriate
513 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 * @param string The field to search
515 * @param array The values searched on
516 * @return object
517 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600518 public function where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 {
520 return $this->_where_in($key, $values, TRUE);
521 }
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 // --------------------------------------------------------------------
524
525 /**
526 * Where_not_in_or
527 *
528 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
529 * with OR if appropriate
530 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 * @param string The field to search
532 * @param array The values searched on
533 * @return object
534 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600535 public function or_where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 {
537 return $this->_where_in($key, $values, TRUE, 'OR ');
538 }
539
540 // --------------------------------------------------------------------
541
542 /**
543 * Where_in
544 *
545 * Called by where_in, where_in_or, where_not_in, where_not_in_or
546 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 * @param string The field to search
548 * @param array The values searched on
549 * @param boolean If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200550 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 * @return object
552 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600553 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 {
555 if ($key === NULL OR $values === NULL)
556 {
557 return;
558 }
Barry Mienydd671972010-10-04 16:33:58 +0200559
WanWizard7219c072011-12-28 14:09:05 +0100560 $type = $this->_group_get_type($type);
561
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 if ( ! is_array($values))
563 {
564 $values = array($values);
565 }
Barry Mienydd671972010-10-04 16:33:58 +0200566
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 $not = ($not) ? ' NOT' : '';
568
569 foreach ($values as $value)
570 {
571 $this->ar_wherein[] = $this->escape($value);
572 }
573
574 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Barry Mienydd671972010-10-04 16:33:58 +0200575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
577
578 $this->ar_where[] = $where_in;
579 if ($this->ar_caching === TRUE)
580 {
581 $this->ar_cache_where[] = $where_in;
582 $this->ar_cache_exists[] = 'where';
583 }
584
585 // reset the array for multiple calls
586 $this->ar_wherein = array();
587 return $this;
588 }
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 // --------------------------------------------------------------------
591
592 /**
593 * Like
594 *
595 * Generates a %LIKE% portion of the query. Separates
596 * multiple calls with AND
597 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 * @param mixed
599 * @param mixed
600 * @return object
601 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600602 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 {
604 return $this->_like($field, $match, 'AND ', $side);
605 }
606
607 // --------------------------------------------------------------------
608
609 /**
610 * Not Like
611 *
612 * Generates a NOT LIKE portion of the query. Separates
613 * multiple calls with AND
614 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 * @param mixed
616 * @param mixed
617 * @return object
618 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600619 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 {
621 return $this->_like($field, $match, 'AND ', $side, 'NOT');
622 }
Barry Mienydd671972010-10-04 16:33:58 +0200623
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 // --------------------------------------------------------------------
625
626 /**
627 * OR Like
628 *
629 * Generates a %LIKE% portion of the query. Separates
630 * multiple calls with OR
631 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 * @param mixed
633 * @param mixed
634 * @return object
635 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600636 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 {
638 return $this->_like($field, $match, 'OR ', $side);
639 }
640
641 // --------------------------------------------------------------------
642
643 /**
644 * OR Not Like
645 *
646 * Generates a NOT LIKE portion of the query. Separates
647 * multiple calls with OR
648 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 * @param mixed
650 * @param mixed
651 * @return object
652 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600653 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 {
655 return $this->_like($field, $match, 'OR ', $side, 'NOT');
656 }
Barry Mienydd671972010-10-04 16:33:58 +0200657
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 // --------------------------------------------------------------------
659
660 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 * Like
662 *
663 * Called by like() or orlike()
664 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 * @param mixed
666 * @param mixed
667 * @param string
668 * @return object
669 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600670 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 {
WanWizard7219c072011-12-28 14:09:05 +0100672 $type = $this->_group_get_type($type);
673
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 if ( ! is_array($field))
675 {
676 $field = array($field => $match);
677 }
Barry Mienydd671972010-10-04 16:33:58 +0200678
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 foreach ($field as $k => $v)
680 {
681 $k = $this->_protect_identifiers($k);
682
683 $prefix = (count($this->ar_like) == 0) ? '' : $type;
684
Derek Jonese4ed5832009-02-20 21:44:59 +0000685 $v = $this->escape_like_str($v);
Kyle Farris0c147b32011-08-26 02:29:31 -0400686
Kyle Farris81ef70f2011-08-31 11:59:12 -0400687 if ($side == 'none')
688 {
689 $like_statement = $prefix." $k $not LIKE '{$v}'";
690 }
691 elseif ($side == 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
693 $like_statement = $prefix." $k $not LIKE '%{$v}'";
694 }
695 elseif ($side == 'after')
696 {
697 $like_statement = $prefix." $k $not LIKE '{$v}%'";
698 }
699 else
700 {
701 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
702 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600703
Derek Jonese4ed5832009-02-20 21:44:59 +0000704 // some platforms require an escape sequence definition for LIKE wildcards
705 if ($this->_like_escape_str != '')
706 {
Greg Aker0d424892010-01-26 02:14:44 +0000707 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000708 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600709
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 $this->ar_like[] = $like_statement;
711 if ($this->ar_caching === TRUE)
712 {
713 $this->ar_cache_like[] = $like_statement;
714 $this->ar_cache_exists[] = 'like';
715 }
Barry Mienydd671972010-10-04 16:33:58 +0200716
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 }
718 return $this;
719 }
Barry Mienydd671972010-10-04 16:33:58 +0200720
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 // --------------------------------------------------------------------
722
723 /**
WanWizard7219c072011-12-28 14:09:05 +0100724 * Starts a query group.
725 *
726 * @param string (Internal use only)
727 * @param string (Internal use only)
728 * @return object
729 */
730 public function group_start($not = '', $type = 'AND ')
731 {
732 $type = $this->_group_get_type($type);
733
734 $this->ar_where_group_started = TRUE;
735
736 $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type;
737 $value = $prefix . $not . str_repeat(' ', ++$this->ar_where_group_count) . ' (';
738
739 $this->ar_where[] = $value;
740 if ($this->ar_caching)
741 {
742 $this->ar_cache_where[] = $value;
743 }
744
745 return $this;
746 }
747
748 // --------------------------------------------------------------------
749
750 /**
751 * Starts a query group, but ORs the group
752 *
753 * @return object
754 */
755 public function or_group_start()
756 {
757 return $this->group_start('', 'OR ');
758 }
759
760 // --------------------------------------------------------------------
761
762 /**
763 * Starts a query group, but NOTs the group
764 *
765 * @return object
766 */
767 public function not_group_start()
768 {
769 return $this->group_start('NOT ', 'AND ');
770 }
771
772 // --------------------------------------------------------------------
773
774 /**
775 * Starts a query group, but OR NOTs the group
776 *
777 * @return object
778 */
779 public function or_not_group_start()
780 {
781 return $this->group_start('NOT ', 'OR ');
782 }
783
784 // --------------------------------------------------------------------
785
786 /**
787 * Ends a query group
788 *
789 * @return object
790 */
791 public function group_end()
792 {
793 $value = str_repeat(' ', $this->ar_where_group_count--) . ')';
794
795 $this->ar_where[] = $value;
796 if ($this->ar_caching)
797 {
798 $this->ar_cache_where[] = $value;
799 }
800
801 $this->ar_where_group_started = FALSE;
802
803 return $this;
804 }
805
806 // --------------------------------------------------------------------
807
808 /**
809 * Group_get_type
810 *
811 * Called by group_start(), _like(), _where() and _where_in()
812 *
813 * @param string
814 * @return string
815 */
816 protected function _group_get_type($type)
817 {
818 if ($this->ar_where_group_started)
819 {
820 $type = '';
821 $this->ar_where_group_started = FALSE;
822 }
823
824 return $type;
825 }
826
827 // --------------------------------------------------------------------
828
829 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 * GROUP BY
831 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 * @param string
833 * @return object
834 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600835 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 {
837 if (is_string($by))
838 {
839 $by = explode(',', $by);
840 }
Barry Mienydd671972010-10-04 16:33:58 +0200841
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 foreach ($by as $val)
843 {
844 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200845
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 if ($val != '')
847 {
848 $this->ar_groupby[] = $this->_protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200849
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 if ($this->ar_caching === TRUE)
851 {
852 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
853 $this->ar_cache_exists[] = 'groupby';
854 }
855 }
856 }
857 return $this;
858 }
859
860 // --------------------------------------------------------------------
861
862 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 * Sets the HAVING value
864 *
865 * Separates multiple calls with AND
866 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 * @param string
868 * @param string
869 * @return object
870 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600871 public function having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 {
873 return $this->_having($key, $value, 'AND ', $escape);
874 }
Barry Mienydd671972010-10-04 16:33:58 +0200875
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 // --------------------------------------------------------------------
877
878 /**
879 * Sets the OR HAVING value
880 *
881 * Separates multiple calls with OR
882 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 * @param string
884 * @param string
885 * @return object
886 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600887 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 {
889 return $this->_having($key, $value, 'OR ', $escape);
890 }
Barry Mienydd671972010-10-04 16:33:58 +0200891
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 // --------------------------------------------------------------------
893
894 /**
895 * Sets the HAVING values
896 *
897 * Called by having() or or_having()
898 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 * @param string
900 * @param string
901 * @return object
902 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600903 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 {
905 if ( ! is_array($key))
906 {
907 $key = array($key => $value);
908 }
Barry Mienydd671972010-10-04 16:33:58 +0200909
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 foreach ($key as $k => $v)
911 {
912 $prefix = (count($this->ar_having) == 0) ? '' : $type;
913
914 if ($escape === TRUE)
915 {
916 $k = $this->_protect_identifiers($k);
917 }
918
919 if ( ! $this->_has_operator($k))
920 {
921 $k .= ' = ';
922 }
923
924 if ($v != '')
925 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400926 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 }
Barry Mienydd671972010-10-04 16:33:58 +0200928
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 $this->ar_having[] = $prefix.$k.$v;
930 if ($this->ar_caching === TRUE)
931 {
932 $this->ar_cache_having[] = $prefix.$k.$v;
933 $this->ar_cache_exists[] = 'having';
934 }
935 }
Barry Mienydd671972010-10-04 16:33:58 +0200936
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 return $this;
938 }
Barry Mienydd671972010-10-04 16:33:58 +0200939
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 // --------------------------------------------------------------------
941
942 /**
943 * Sets the ORDER BY value
944 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 * @param string
946 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100947 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 * @return object
949 */
pporlan2c685fb2011-12-22 12:15:25 +0100950 public function order_by($orderby, $direction = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 {
952 if (strtolower($direction) == 'random')
953 {
954 $orderby = ''; // Random results want or don't need a field name
955 $direction = $this->_random_keyword;
956 }
957 elseif (trim($direction) != '')
958 {
959 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
960 }
Barry Mienydd671972010-10-04 16:33:58 +0200961
962
pporlan2c685fb2011-12-22 12:15:25 +0100963 if ((strpos($orderby, ',') !== FALSE) && ($escape === TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 {
965 $temp = array();
966 foreach (explode(',', $orderby) as $part)
967 {
968 $part = trim($part);
969 if ( ! in_array($part, $this->ar_aliased_tables))
970 {
971 $part = $this->_protect_identifiers(trim($part));
972 }
Barry Mienydd671972010-10-04 16:33:58 +0200973
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 $temp[] = $part;
975 }
Barry Mienydd671972010-10-04 16:33:58 +0200976
977 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 }
Derek Allarde37ab382009-02-03 16:13:57 +0000979 else if ($direction != $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 {
pporlan2c685fb2011-12-22 12:15:25 +0100981 if ($escape === TRUE)
982 {
983 $orderby = $this->_protect_identifiers($orderby);
984 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 }
Barry Mienydd671972010-10-04 16:33:58 +0200986
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200988
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 $this->ar_orderby[] = $orderby_statement;
990 if ($this->ar_caching === TRUE)
991 {
992 $this->ar_cache_orderby[] = $orderby_statement;
993 $this->ar_cache_exists[] = 'orderby';
994 }
995
996 return $this;
997 }
Barry Mienydd671972010-10-04 16:33:58 +0200998
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 // --------------------------------------------------------------------
1000
1001 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 * Sets the LIMIT value
1003 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 * @param integer the limit value
1005 * @param integer the offset value
1006 * @return object
1007 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +02001008 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 {
Kyle Farris76116012011-08-31 11:17:48 -04001010 $this->ar_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +00001011
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +02001012 if ( ! is_null($offset))
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 {
Kyle Farris76116012011-08-31 11:17:48 -04001014 $this->ar_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 }
Barry Mienydd671972010-10-04 16:33:58 +02001016
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 return $this;
1018 }
Barry Mienydd671972010-10-04 16:33:58 +02001019
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 // --------------------------------------------------------------------
1021
1022 /**
1023 * Sets the OFFSET value
1024 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 * @param integer the offset value
1026 * @return object
1027 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001028 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 {
kenjis0e857632011-09-02 08:41:17 +09001030 $this->ar_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 return $this;
1032 }
Barry Mienydd671972010-10-04 16:33:58 +02001033
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 // --------------------------------------------------------------------
1035
1036 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001037 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 * @param mixed
1040 * @param string
1041 * @param boolean
1042 * @return object
1043 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001044 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 {
1046 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001047
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 if ( ! is_array($key))
1049 {
1050 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001051 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001052
1053 foreach ($key as $k => $v)
1054 {
1055 if ($escape === FALSE)
1056 {
1057 $this->ar_set[$this->_protect_identifiers($k)] = $v;
1058 }
1059 else
1060 {
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001061 $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 }
1063 }
Barry Mienydd671972010-10-04 16:33:58 +02001064
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 return $this;
1066 }
WanWizard7219c072011-12-28 14:09:05 +01001067
Kyle Farris0c147b32011-08-26 02:29:31 -04001068 // --------------------------------------------------------------------
1069
1070 /**
1071 * Get SELECT query string
1072 *
1073 * Compiles a SELECT query string and returns the sql.
1074 *
1075 * @access public
1076 * @param string the table name to select from (optional)
1077 * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone
1078 * @return string
1079 */
WanWizard7219c072011-12-28 14:09:05 +01001080 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001081 {
1082 if ($table != '')
kylefarris0a3176b2011-08-26 02:37:52 -04001083 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001084 $this->_track_aliases($table);
1085 $this->from($table);
1086 }
WanWizard7219c072011-12-28 14:09:05 +01001087
Kyle Farris0c147b32011-08-26 02:29:31 -04001088 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001089
Kyle Farris0c147b32011-08-26 02:29:31 -04001090 if ($reset === TRUE)
1091 {
1092 $this->_reset_select();
1093 }
WanWizard7219c072011-12-28 14:09:05 +01001094
Kyle Farris0c147b32011-08-26 02:29:31 -04001095 return $select;
1096 }
WanWizard7219c072011-12-28 14:09:05 +01001097
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 // --------------------------------------------------------------------
1099
1100 /**
1101 * Get
1102 *
1103 * Compiles the select statement based on the other functions called
1104 * and runs the query
1105 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 * @param string the table
1107 * @param string the limit clause
1108 * @param string the offset clause
1109 * @return object
1110 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001111 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 {
1113 if ($table != '')
1114 {
1115 $this->_track_aliases($table);
1116 $this->from($table);
1117 }
Barry Mienydd671972010-10-04 16:33:58 +02001118
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 if ( ! is_null($limit))
1120 {
1121 $this->limit($limit, $offset);
1122 }
Barry Mienydd671972010-10-04 16:33:58 +02001123
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 $sql = $this->_compile_select();
1125
1126 $result = $this->query($sql);
1127 $this->_reset_select();
1128 return $result;
1129 }
1130
1131 /**
1132 * "Count All Results" query
1133 *
Barry Mienydd671972010-10-04 16:33:58 +02001134 * Generates a platform-specific query string that counts all records
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 * returned by an Active Record query.
1136 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 * @param string
1138 * @return string
1139 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001140 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 {
1142 if ($table != '')
1143 {
1144 $this->_track_aliases($table);
1145 $this->from($table);
1146 }
Barry Mienydd671972010-10-04 16:33:58 +02001147
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
1149
1150 $query = $this->query($sql);
1151 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001152
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 if ($query->num_rows() == 0)
1154 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001155 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 }
1157
1158 $row = $query->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001159 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 }
1161
1162 // --------------------------------------------------------------------
1163
1164 /**
1165 * Get_Where
1166 *
1167 * Allows the where clause, limit and offset to be added directly
1168 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 * @param string the where clause
1170 * @param string the limit clause
1171 * @param string the offset clause
1172 * @return object
1173 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001174 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 {
1176 if ($table != '')
1177 {
1178 $this->from($table);
1179 }
1180
1181 if ( ! is_null($where))
1182 {
1183 $this->where($where);
1184 }
Barry Mienydd671972010-10-04 16:33:58 +02001185
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 if ( ! is_null($limit))
1187 {
1188 $this->limit($limit, $offset);
1189 }
Barry Mienydd671972010-10-04 16:33:58 +02001190
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 $sql = $this->_compile_select();
1192
1193 $result = $this->query($sql);
1194 $this->_reset_select();
1195 return $result;
1196 }
1197
1198 // --------------------------------------------------------------------
1199
1200 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001201 * Insert_Batch
1202 *
1203 * Compiles batch insert strings and runs the queries
1204 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001205 * @param string the table to retrieve the results from
1206 * @param array an associative array of insert values
1207 * @return object
1208 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001209 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001210 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001211 if ( ! is_null($set))
1212 {
1213 $this->set_insert_batch($set);
1214 }
Barry Mienydd671972010-10-04 16:33:58 +02001215
Derek Jonesd10e8962010-03-02 17:10:36 -06001216 if (count($this->ar_set) == 0)
1217 {
1218 if ($this->db_debug)
1219 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001220 //No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001221 return $this->display_error('db_must_use_set');
1222 }
1223 return FALSE;
1224 }
1225
1226 if ($table == '')
1227 {
1228 if ( ! isset($this->ar_from[0]))
1229 {
1230 if ($this->db_debug)
1231 {
1232 return $this->display_error('db_must_set_table');
1233 }
1234 return FALSE;
1235 }
Barry Mienydd671972010-10-04 16:33:58 +02001236
Derek Jonesd10e8962010-03-02 17:10:36 -06001237 $table = $this->ar_from[0];
1238 }
1239
1240 // Batch this baby
1241 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1242 {
Barry Mienydd671972010-10-04 16:33:58 +02001243
Derek Jonesd10e8962010-03-02 17:10:36 -06001244 $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
1245
1246 //echo $sql;
1247
1248 $this->query($sql);
1249 }
Barry Mienydd671972010-10-04 16:33:58 +02001250
Derek Jonesd10e8962010-03-02 17:10:36 -06001251 $this->_reset_write();
1252
1253
Barry Mienydd671972010-10-04 16:33:58 +02001254 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001255 }
1256
1257 // --------------------------------------------------------------------
1258
1259 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001260 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001261 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001262 * @param mixed
1263 * @param string
1264 * @param boolean
1265 * @return object
1266 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001267 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001268 {
1269 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001270
Derek Jonesd10e8962010-03-02 17:10:36 -06001271 if ( ! is_array($key))
1272 {
1273 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001274 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001275
1276 $keys = array_keys(current($key));
1277 sort($keys);
1278
1279 foreach ($key as $row)
1280 {
Barry Mienydd671972010-10-04 16:33:58 +02001281 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1282 {
1283 // batch function above returns an error on an empty array
1284 $this->ar_set[] = array();
1285 return;
1286 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001287
Barry Mienydd671972010-10-04 16:33:58 +02001288 ksort($row); // puts $row in the same order as our keys
1289
Derek Jonesd10e8962010-03-02 17:10:36 -06001290 if ($escape === FALSE)
1291 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001292 $this->ar_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001293 }
1294 else
1295 {
1296 $clean = array();
1297
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001298 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001299 {
Barry Mienydd671972010-10-04 16:33:58 +02001300 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001301 }
1302
Derek Jones37f4b9c2011-07-01 17:56:50 -05001303 $this->ar_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001304 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001305 }
1306
1307 foreach ($keys as $k)
1308 {
1309 $this->ar_keys[] = $this->_protect_identifiers($k);
1310 }
Barry Mienydd671972010-10-04 16:33:58 +02001311
Derek Jonesd10e8962010-03-02 17:10:36 -06001312 return $this;
1313 }
WanWizard7219c072011-12-28 14:09:05 +01001314
Kyle Farris0c147b32011-08-26 02:29:31 -04001315 // --------------------------------------------------------------------
1316
1317 /**
1318 * Get INSERT query string
1319 *
1320 * Compiles an insert query and returns the sql
1321 *
1322 * @access public
1323 * @param string the table to insert into
1324 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1325 * @return string
1326 */
1327 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001328 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001329 if ($this->_validate_insert($table) === FALSE)
1330 {
1331 return FALSE;
1332 }
WanWizard7219c072011-12-28 14:09:05 +01001333
Kyle Farris76116012011-08-31 11:17:48 -04001334 $sql = $this->_insert(
1335 $this->_protect_identifiers(
1336 $this->ar_from[0], TRUE, NULL, FALSE
1337 ),
WanWizard7219c072011-12-28 14:09:05 +01001338 array_keys($this->ar_set),
Kyle Farris76116012011-08-31 11:17:48 -04001339 array_values($this->ar_set)
1340 );
WanWizard7219c072011-12-28 14:09:05 +01001341
Kyle Farris0c147b32011-08-26 02:29:31 -04001342 if ($reset === TRUE)
1343 {
1344 $this->_reset_write();
1345 }
WanWizard7219c072011-12-28 14:09:05 +01001346
Kyle Farris0c147b32011-08-26 02:29:31 -04001347 return $sql;
1348 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001349
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 // --------------------------------------------------------------------
1351
1352 /**
1353 * Insert
1354 *
1355 * Compiles an insert string and runs the query
1356 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001357 * @access public
Phil Sturgeon9789f322011-07-15 15:14:05 -06001358 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 * @param array an associative array of insert values
1360 * @return object
1361 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001362 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001363 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001364 if ( ! is_null($set))
1365 {
1366 $this->set($set);
1367 }
WanWizard7219c072011-12-28 14:09:05 +01001368
Kyle Farris0c147b32011-08-26 02:29:31 -04001369 if ($this->_validate_insert($table) === FALSE)
1370 {
1371 return FALSE;
1372 }
WanWizard7219c072011-12-28 14:09:05 +01001373
Kyle Farris76116012011-08-31 11:17:48 -04001374 $sql = $this->_insert(
1375 $this->_protect_identifiers(
1376 $this->ar_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001377 ),
1378 array_keys($this->ar_set),
Kyle Farris76116012011-08-31 11:17:48 -04001379 array_values($this->ar_set)
1380 );
Barry Mienydd671972010-10-04 16:33:58 +02001381
Kyle Farris0c147b32011-08-26 02:29:31 -04001382 $this->_reset_write();
1383 return $this->query($sql);
1384 }
WanWizard7219c072011-12-28 14:09:05 +01001385
Kyle Farris0c147b32011-08-26 02:29:31 -04001386 // --------------------------------------------------------------------
1387
1388 /**
1389 * Validate Insert
1390 *
1391 * This method is used by both insert() and get_compiled_insert() to
1392 * validate that the there data is actually being set and that table
1393 * has been chosen to be inserted into.
1394 *
1395 * @access public
1396 * @param string the table to insert data into
1397 * @return string
1398 */
WanWizard7219c072011-12-28 14:09:05 +01001399 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001400 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 if (count($this->ar_set) == 0)
1402 {
1403 if ($this->db_debug)
1404 {
1405 return $this->display_error('db_must_use_set');
1406 }
1407 return FALSE;
1408 }
1409
1410 if ($table == '')
1411 {
1412 if ( ! isset($this->ar_from[0]))
1413 {
1414 if ($this->db_debug)
1415 {
1416 return $this->display_error('db_must_set_table');
1417 }
1418 return FALSE;
1419 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001420 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001421 else
1422 {
1423 $this->ar_from[0] = $table;
1424 }
WanWizard7219c072011-12-28 14:09:05 +01001425
Kyle Farris0c147b32011-08-26 02:29:31 -04001426 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 }
Barry Mienydd671972010-10-04 16:33:58 +02001428
Phil Sturgeon9789f322011-07-15 15:14:05 -06001429 // --------------------------------------------------------------------
1430
1431 /**
1432 * Replace
1433 *
1434 * Compiles an replace into string and runs the query
1435 *
1436 * @param string the table to replace data into
1437 * @param array an associative array of insert values
1438 * @return object
1439 */
1440 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001441 {
1442 if ( ! is_null($set))
1443 {
1444 $this->set($set);
1445 }
Barry Mienydd671972010-10-04 16:33:58 +02001446
Derek Jonesd10e8962010-03-02 17:10:36 -06001447 if (count($this->ar_set) == 0)
1448 {
1449 if ($this->db_debug)
1450 {
1451 return $this->display_error('db_must_use_set');
1452 }
1453 return FALSE;
1454 }
1455
1456 if ($table == '')
1457 {
1458 if ( ! isset($this->ar_from[0]))
1459 {
1460 if ($this->db_debug)
1461 {
1462 return $this->display_error('db_must_set_table');
1463 }
1464 return FALSE;
1465 }
Barry Mienydd671972010-10-04 16:33:58 +02001466
Derek Jonesd10e8962010-03-02 17:10:36 -06001467 $table = $this->ar_from[0];
1468 }
1469
1470 $sql = $this->_replace($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
Barry Mienydd671972010-10-04 16:33:58 +02001471
Derek Jonesd10e8962010-03-02 17:10:36 -06001472 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001473 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001474 }
WanWizard7219c072011-12-28 14:09:05 +01001475
Kyle Farris0c147b32011-08-26 02:29:31 -04001476 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001477
Kyle Farris0c147b32011-08-26 02:29:31 -04001478 /**
1479 * Get UPDATE query string
1480 *
1481 * Compiles an update query and returns the sql
1482 *
1483 * @access public
1484 * @param string the table to update
1485 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1486 * @return string
1487 */
1488 public function get_compiled_update($table = '', $reset = TRUE)
1489 {
1490 // Combine any cached components with the current statements
1491 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001492
Kyle Farris0c147b32011-08-26 02:29:31 -04001493 if ($this->_validate_update($table) === FALSE)
1494 {
1495 return FALSE;
1496 }
WanWizard7219c072011-12-28 14:09:05 +01001497
Kyle Farris0c147b32011-08-26 02:29:31 -04001498 $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
WanWizard7219c072011-12-28 14:09:05 +01001499
Kyle Farris0c147b32011-08-26 02:29:31 -04001500 if ($reset === TRUE)
1501 {
1502 $this->_reset_write();
1503 }
WanWizard7219c072011-12-28 14:09:05 +01001504
Kyle Farris0c147b32011-08-26 02:29:31 -04001505 return $sql;
1506 }
WanWizard7219c072011-12-28 14:09:05 +01001507
Derek Allard2067d1a2008-11-13 22:59:24 +00001508 // --------------------------------------------------------------------
1509
1510 /**
1511 * Update
1512 *
1513 * Compiles an update string and runs the query
1514 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001515 * @param string the table to retrieve the results from
1516 * @param array an associative array of update values
1517 * @param mixed the where clause
1518 * @return object
1519 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001520 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001521 {
1522 // Combine any cached components with the current statements
1523 $this->_merge_cache();
1524
1525 if ( ! is_null($set))
1526 {
1527 $this->set($set);
1528 }
Barry Mienydd671972010-10-04 16:33:58 +02001529
Kyle Farris0c147b32011-08-26 02:29:31 -04001530 if ($this->_validate_update($table) === FALSE)
1531 {
1532 return FALSE;
1533 }
1534
1535 if ($where != NULL)
1536 {
1537 $this->where($where);
1538 }
1539
1540 if ($limit != NULL)
1541 {
1542 $this->limit($limit);
1543 }
1544
Mancy0d91fd22011-12-20 13:13:14 +03001545 $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit, $this->ar_like);
Kyle Farris0c147b32011-08-26 02:29:31 -04001546
1547 $this->_reset_write();
1548 return $this->query($sql);
1549 }
WanWizard7219c072011-12-28 14:09:05 +01001550
Kyle Farris0c147b32011-08-26 02:29:31 -04001551 // --------------------------------------------------------------------
1552
1553 /**
1554 * Validate Update
1555 *
1556 * This method is used by both update() and get_compiled_update() to
1557 * validate that data is actually being set and that a table has been
1558 * chosen to be update.
1559 *
1560 * @access public
1561 * @param string the table to update data on
1562 * @return string
1563 */
1564 protected function _validate_update($table = '')
1565 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001566 if (count($this->ar_set) == 0)
1567 {
1568 if ($this->db_debug)
1569 {
1570 return $this->display_error('db_must_use_set');
1571 }
1572 return FALSE;
1573 }
1574
1575 if ($table == '')
1576 {
1577 if ( ! isset($this->ar_from[0]))
1578 {
1579 if ($this->db_debug)
1580 {
1581 return $this->display_error('db_must_set_table');
1582 }
1583 return FALSE;
1584 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001585 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001586 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001587 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001588 $this->ar_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001589 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001590 }
WanWizard7219c072011-12-28 14:09:05 +01001591
Derek Jonesd10e8962010-03-02 17:10:36 -06001592 // --------------------------------------------------------------------
1593
1594 /**
1595 * Update_Batch
1596 *
1597 * Compiles an update string and runs the query
1598 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001599 * @param string the table to retrieve the results from
1600 * @param array an associative array of update values
1601 * @param string the where key
1602 * @return object
1603 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001604 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001605 {
1606 // Combine any cached components with the current statements
1607 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001608
Derek Jonesd10e8962010-03-02 17:10:36 -06001609 if (is_null($index))
1610 {
1611 if ($this->db_debug)
1612 {
Kyle Farris2de2fa02011-08-31 11:52:20 -04001613 return $this->display_error('db_must_use_index');
Derek Jonesd10e8962010-03-02 17:10:36 -06001614 }
1615
1616 return FALSE;
1617 }
1618
1619 if ( ! is_null($set))
1620 {
1621 $this->set_update_batch($set, $index);
1622 }
1623
1624 if (count($this->ar_set) == 0)
1625 {
1626 if ($this->db_debug)
1627 {
1628 return $this->display_error('db_must_use_set');
1629 }
1630
1631 return FALSE;
1632 }
1633
1634 if ($table == '')
1635 {
1636 if ( ! isset($this->ar_from[0]))
1637 {
1638 if ($this->db_debug)
1639 {
1640 return $this->display_error('db_must_set_table');
1641 }
1642 return FALSE;
1643 }
Barry Mienydd671972010-10-04 16:33:58 +02001644
Derek Jonesd10e8962010-03-02 17:10:36 -06001645 $table = $this->ar_from[0];
1646 }
Barry Mienydd671972010-10-04 16:33:58 +02001647
Derek Jonesd10e8962010-03-02 17:10:36 -06001648 // Batch this baby
1649 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1650 {
1651 $sql = $this->_update_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->ar_set, $i, 100), $this->_protect_identifiers($index), $this->ar_where);
1652
1653 $this->query($sql);
1654 }
Barry Mienydd671972010-10-04 16:33:58 +02001655
Derek Jonesd10e8962010-03-02 17:10:36 -06001656 $this->_reset_write();
1657 }
1658
1659 // --------------------------------------------------------------------
1660
1661 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001662 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001663 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001664 * @param array
1665 * @param string
1666 * @param boolean
1667 * @return object
1668 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001669 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001670 {
1671 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001672
Derek Jonesd10e8962010-03-02 17:10:36 -06001673 if ( ! is_array($key))
1674 {
1675 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001676 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001677
1678 foreach ($key as $k => $v)
1679 {
1680 $index_set = FALSE;
1681 $clean = array();
1682
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001683 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001684 {
1685 if ($k2 == $index)
1686 {
1687 $index_set = TRUE;
1688 }
1689 else
1690 {
1691 $not[] = $k.'-'.$v;
1692 }
1693
1694 if ($escape === FALSE)
1695 {
1696 $clean[$this->_protect_identifiers($k2)] = $v2;
1697 }
1698 else
1699 {
Barry Mienydd671972010-10-04 16:33:58 +02001700 $clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001701 }
1702 }
1703
1704 if ($index_set == FALSE)
1705 {
1706 return $this->display_error('db_batch_missing_index');
1707 }
1708
1709 $this->ar_set[] = $clean;
1710 }
Barry Mienydd671972010-10-04 16:33:58 +02001711
Derek Jonesd10e8962010-03-02 17:10:36 -06001712 return $this;
1713 }
1714
Derek Allard2067d1a2008-11-13 22:59:24 +00001715 // --------------------------------------------------------------------
1716
1717 /**
1718 * Empty Table
1719 *
1720 * Compiles a delete string and runs "DELETE FROM table"
1721 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001722 * @param string the table to empty
1723 * @return object
1724 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001725 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001726 {
1727 if ($table == '')
1728 {
1729 if ( ! isset($this->ar_from[0]))
1730 {
1731 if ($this->db_debug)
1732 {
1733 return $this->display_error('db_must_set_table');
1734 }
1735 return FALSE;
1736 }
1737
1738 $table = $this->ar_from[0];
1739 }
1740 else
1741 {
1742 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1743 }
1744
1745 $sql = $this->_delete($table);
1746
1747 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001748
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 return $this->query($sql);
1750 }
1751
1752 // --------------------------------------------------------------------
1753
1754 /**
1755 * Truncate
1756 *
1757 * Compiles a truncate string and runs the query
1758 * If the database does not support the truncate() command
1759 * This function maps to "DELETE FROM table"
1760 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001761 * @param string the table to truncate
1762 * @return object
1763 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001764 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001765 {
1766 if ($table == '')
1767 {
1768 if ( ! isset($this->ar_from[0]))
1769 {
1770 if ($this->db_debug)
1771 {
1772 return $this->display_error('db_must_set_table');
1773 }
1774 return FALSE;
1775 }
1776
1777 $table = $this->ar_from[0];
1778 }
1779 else
1780 {
1781 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1782 }
1783
1784 $sql = $this->_truncate($table);
1785
1786 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001787
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 return $this->query($sql);
1789 }
WanWizard7219c072011-12-28 14:09:05 +01001790
Kyle Farris0c147b32011-08-26 02:29:31 -04001791 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001792
Kyle Farris0c147b32011-08-26 02:29:31 -04001793 /**
1794 * Get DELETE query string
1795 *
1796 * Compiles a delete query string and returns the sql
1797 *
1798 * @access public
1799 * @param string the table to delete from
1800 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1801 * @return string
1802 */
1803 public function get_compiled_delete($table = '', $reset = TRUE)
1804 {
1805 $this->return_delete_sql = TRUE;
1806 $sql = $this->delete($table, '', NULL, $reset);
1807 $this->return_delete_sql = FALSE;
1808 return $sql;
1809 }
WanWizard7219c072011-12-28 14:09:05 +01001810
Derek Allard2067d1a2008-11-13 22:59:24 +00001811 // --------------------------------------------------------------------
1812
1813 /**
1814 * Delete
1815 *
1816 * Compiles a delete string and runs the query
1817 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001818 * @param mixed the table(s) to delete from. String or array
1819 * @param mixed the where clause
1820 * @param mixed the limit clause
1821 * @param boolean
1822 * @return object
1823 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001824 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001825 {
1826 // Combine any cached components with the current statements
1827 $this->_merge_cache();
1828
1829 if ($table == '')
1830 {
1831 if ( ! isset($this->ar_from[0]))
1832 {
1833 if ($this->db_debug)
1834 {
1835 return $this->display_error('db_must_set_table');
1836 }
1837 return FALSE;
1838 }
1839
1840 $table = $this->ar_from[0];
1841 }
1842 elseif (is_array($table))
1843 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001844 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001845 {
1846 $this->delete($single_table, $where, $limit, FALSE);
1847 }
1848
1849 $this->_reset_write();
1850 return;
1851 }
1852 else
1853 {
1854 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1855 }
1856
1857 if ($where != '')
1858 {
1859 $this->where($where);
1860 }
1861
1862 if ($limit != NULL)
1863 {
1864 $this->limit($limit);
1865 }
1866
Derek Allard03d783b2009-05-03 19:45:45 +00001867 if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001868 {
1869 if ($this->db_debug)
1870 {
1871 return $this->display_error('db_del_must_use_where');
1872 }
1873
1874 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001875 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001876
1877 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
1878
1879 if ($reset_data)
1880 {
1881 $this->_reset_write();
1882 }
WanWizard7219c072011-12-28 14:09:05 +01001883
Kyle Farris0c147b32011-08-26 02:29:31 -04001884 if ($this->return_delete_sql === true)
1885 {
1886 return $sql;
1887 }
Barry Mienydd671972010-10-04 16:33:58 +02001888
Derek Allard2067d1a2008-11-13 22:59:24 +00001889 return $this->query($sql);
1890 }
WanWizard7219c072011-12-28 14:09:05 +01001891
Derek Allard2067d1a2008-11-13 22:59:24 +00001892 // --------------------------------------------------------------------
1893
1894 /**
1895 * DB Prefix
1896 *
1897 * Prepends a database prefix if one exists in configuration
1898 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001899 * @param string the table
1900 * @return string
1901 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001902 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001903 {
1904 if ($table == '')
1905 {
1906 $this->display_error('db_table_name_required');
1907 }
1908
1909 return $this->dbprefix.$table;
1910 }
1911
1912 // --------------------------------------------------------------------
1913
1914 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001915 * Set DB Prefix
1916 *
1917 * Set's the DB Prefix to something new without needing to reconnect
1918 *
1919 * @param string the prefix
1920 * @return string
1921 */
1922 public function set_dbprefix($prefix = '')
1923 {
1924 return $this->dbprefix = $prefix;
1925 }
1926
1927 // --------------------------------------------------------------------
1928
1929 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001930 * Track Aliases
1931 *
1932 * Used to track SQL statements written with aliased tables.
1933 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001934 * @param string The table to inspect
1935 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001936 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001937 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001938 {
1939 if (is_array($table))
1940 {
1941 foreach ($table as $t)
1942 {
1943 $this->_track_aliases($t);
1944 }
1945 return;
1946 }
Barry Mienydd671972010-10-04 16:33:58 +02001947
Derek Jones37f4b9c2011-07-01 17:56:50 -05001948 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001949 // the string into discreet statements
1950 if (strpos($table, ',') !== FALSE)
1951 {
1952 return $this->_track_aliases(explode(',', $table));
1953 }
Barry Mienydd671972010-10-04 16:33:58 +02001954
Derek Allard2067d1a2008-11-13 22:59:24 +00001955 // if a table alias is used we can recognize it by a space
1956 if (strpos($table, " ") !== FALSE)
1957 {
1958 // if the alias is written with the AS keyword, remove it
1959 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001960
Derek Allard2067d1a2008-11-13 22:59:24 +00001961 // Grab the alias
1962 $table = trim(strrchr($table, " "));
Barry Mienydd671972010-10-04 16:33:58 +02001963
Derek Allard2067d1a2008-11-13 22:59:24 +00001964 // Store the alias, if it doesn't already exist
1965 if ( ! in_array($table, $this->ar_aliased_tables))
1966 {
1967 $this->ar_aliased_tables[] = $table;
1968 }
1969 }
1970 }
WanWizard7219c072011-12-28 14:09:05 +01001971
Derek Allard2067d1a2008-11-13 22:59:24 +00001972 // --------------------------------------------------------------------
1973
1974 /**
1975 * Compile the SELECT statement
1976 *
1977 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001978 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001979 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001980 * @return string
1981 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001982 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001983 {
1984 // Combine any cached components with the current statements
1985 $this->_merge_cache();
1986
1987 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001988
Derek Allard2067d1a2008-11-13 22:59:24 +00001989 // Write the "select" portion of the query
1990
1991 if ($select_override !== FALSE)
1992 {
1993 $sql = $select_override;
1994 }
1995 else
1996 {
1997 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001998
Derek Allard2067d1a2008-11-13 22:59:24 +00001999 if (count($this->ar_select) == 0)
2000 {
Barry Mienydd671972010-10-04 16:33:58 +02002001 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002002 }
2003 else
Barry Mienydd671972010-10-04 16:33:58 +02002004 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002005 // Cycle through the "select" portion of the query and prep each column name.
2006 // The reason we protect identifiers here rather then in the select() function
2007 // is because until the user calls the from() function we don't know if there are aliases
2008 foreach ($this->ar_select as $key => $val)
2009 {
Phil Sturgeon77cc0282011-08-09 16:03:49 -06002010 $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL;
2011 $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002012 }
Barry Mienydd671972010-10-04 16:33:58 +02002013
Derek Allard2067d1a2008-11-13 22:59:24 +00002014 $sql .= implode(', ', $this->ar_select);
2015 }
2016 }
2017
2018 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02002019
Derek Allard2067d1a2008-11-13 22:59:24 +00002020 // Write the "FROM" portion of the query
2021
2022 if (count($this->ar_from) > 0)
2023 {
2024 $sql .= "\nFROM ";
2025
2026 $sql .= $this->_from_tables($this->ar_from);
2027 }
2028
2029 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02002030
Derek Allard2067d1a2008-11-13 22:59:24 +00002031 // Write the "JOIN" portion of the query
2032
2033 if (count($this->ar_join) > 0)
2034 {
2035 $sql .= "\n";
2036
2037 $sql .= implode("\n", $this->ar_join);
2038 }
2039
2040 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02002041
Derek Allard2067d1a2008-11-13 22:59:24 +00002042 // Write the "WHERE" portion of the query
2043
2044 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
2045 {
Greg Akere156c6e2011-04-20 16:03:04 -05002046 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00002047 }
2048
2049 $sql .= implode("\n", $this->ar_where);
2050
2051 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02002052
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 // Write the "LIKE" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02002054
Derek Allard2067d1a2008-11-13 22:59:24 +00002055 if (count($this->ar_like) > 0)
2056 {
2057 if (count($this->ar_where) > 0)
2058 {
2059 $sql .= "\nAND ";
2060 }
2061
2062 $sql .= implode("\n", $this->ar_like);
2063 }
2064
2065 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02002066
Derek Allard2067d1a2008-11-13 22:59:24 +00002067 // Write the "GROUP BY" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02002068
Derek Allard2067d1a2008-11-13 22:59:24 +00002069 if (count($this->ar_groupby) > 0)
2070 {
2071 $sql .= "\nGROUP BY ";
Barry Mienydd671972010-10-04 16:33:58 +02002072
Derek Allard2067d1a2008-11-13 22:59:24 +00002073 $sql .= implode(', ', $this->ar_groupby);
2074 }
2075
2076 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02002077
Derek Allard2067d1a2008-11-13 22:59:24 +00002078 // Write the "HAVING" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02002079
Derek Allard2067d1a2008-11-13 22:59:24 +00002080 if (count($this->ar_having) > 0)
2081 {
2082 $sql .= "\nHAVING ";
2083 $sql .= implode("\n", $this->ar_having);
2084 }
2085
2086 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02002087
Derek Allard2067d1a2008-11-13 22:59:24 +00002088 // Write the "ORDER BY" portion of the query
2089
2090 if (count($this->ar_orderby) > 0)
2091 {
2092 $sql .= "\nORDER BY ";
2093 $sql .= implode(', ', $this->ar_orderby);
Barry Mienydd671972010-10-04 16:33:58 +02002094
Derek Allard2067d1a2008-11-13 22:59:24 +00002095 if ($this->ar_order !== FALSE)
2096 {
2097 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02002098 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002099 }
2100
2101 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02002102
Derek Allard2067d1a2008-11-13 22:59:24 +00002103 // Write the "LIMIT" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02002104
Derek Allard2067d1a2008-11-13 22:59:24 +00002105 if (is_numeric($this->ar_limit))
2106 {
2107 $sql .= "\n";
2108 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
2109 }
2110
2111 return $sql;
2112 }
2113
2114 // --------------------------------------------------------------------
2115
2116 /**
2117 * Object to Array
2118 *
2119 * Takes an object as input and converts the class variables to array key/vals
2120 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002121 * @param object
2122 * @return array
2123 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002124 public function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002125 {
2126 if ( ! is_object($object))
2127 {
2128 return $object;
2129 }
Barry Mienydd671972010-10-04 16:33:58 +02002130
Derek Allard2067d1a2008-11-13 22:59:24 +00002131 $array = array();
2132 foreach (get_object_vars($object) as $key => $val)
2133 {
2134 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06002135 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002136 {
2137 $array[$key] = $val;
2138 }
2139 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002140
2141 return $array;
2142 }
Barry Mienydd671972010-10-04 16:33:58 +02002143
Derek Jonesd10e8962010-03-02 17:10:36 -06002144 // --------------------------------------------------------------------
2145
2146 /**
2147 * Object to Array
2148 *
2149 * Takes an object as input and converts the class variables to array key/vals
2150 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002151 * @param object
2152 * @return array
2153 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002154 public function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002155 {
2156 if ( ! is_object($object))
2157 {
2158 return $object;
2159 }
Barry Mienydd671972010-10-04 16:33:58 +02002160
Derek Jonesd10e8962010-03-02 17:10:36 -06002161 $array = array();
2162 $out = get_object_vars($object);
2163 $fields = array_keys($out);
2164
2165 foreach ($fields as $val)
2166 {
2167 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06002168 if ($val != '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002169 {
2170
2171 $i = 0;
2172 foreach ($out[$val] as $data)
2173 {
2174 $array[$i][$val] = $data;
2175 $i++;
2176 }
2177 }
2178 }
2179
Derek Allard2067d1a2008-11-13 22:59:24 +00002180 return $array;
2181 }
Barry Mienydd671972010-10-04 16:33:58 +02002182
Derek Allard2067d1a2008-11-13 22:59:24 +00002183 // --------------------------------------------------------------------
2184
2185 /**
2186 * Start Cache
2187 *
2188 * Starts AR caching
2189 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002190 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002191 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002192 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002193 {
2194 $this->ar_caching = TRUE;
2195 }
2196
2197 // --------------------------------------------------------------------
2198
2199 /**
2200 * Stop Cache
2201 *
2202 * Stops AR caching
2203 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002204 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002205 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002206 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002207 {
2208 $this->ar_caching = FALSE;
2209 }
2210
2211 // --------------------------------------------------------------------
2212
2213 /**
2214 * Flush Cache
2215 *
2216 * Empties the AR cache
2217 *
2218 * @access public
2219 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002220 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002221 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002222 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002223 $this->_reset_run(array(
2224 'ar_cache_select' => array(),
2225 'ar_cache_from' => array(),
2226 'ar_cache_join' => array(),
2227 'ar_cache_where' => array(),
2228 'ar_cache_like' => array(),
2229 'ar_cache_groupby' => array(),
2230 'ar_cache_having' => array(),
2231 'ar_cache_orderby' => array(),
2232 'ar_cache_set' => array(),
2233 'ar_cache_exists' => array(),
2234 'ar_cache_no_escape' => array()
2235 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002236 }
2237
2238 // --------------------------------------------------------------------
2239
2240 /**
2241 * Merge Cache
2242 *
Barry Mienydd671972010-10-04 16:33:58 +02002243 * When called, this function merges any cached AR arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002244 * locally called ones.
2245 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002246 * @return void
2247 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002248 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002249 {
2250 if (count($this->ar_cache_exists) == 0)
2251 {
2252 return;
2253 }
2254
2255 foreach ($this->ar_cache_exists as $val)
2256 {
2257 $ar_variable = 'ar_'.$val;
2258 $ar_cache_var = 'ar_cache_'.$val;
2259
2260 if (count($this->$ar_cache_var) == 0)
2261 {
2262 continue;
2263 }
2264
2265 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
2266 }
2267
2268 // If we are "protecting identifiers" we need to examine the "from"
2269 // portion of the query to determine if there are any aliases
2270 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
2271 {
2272 $this->_track_aliases($this->ar_from);
2273 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002274
2275 $this->ar_no_escape = $this->ar_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002276 }
WanWizard7219c072011-12-28 14:09:05 +01002277
Kyle Farris0c147b32011-08-26 02:29:31 -04002278 // --------------------------------------------------------------------
2279
2280 /**
2281 * Reset Active Record values.
WanWizard7219c072011-12-28 14:09:05 +01002282 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002283 * Publicly-visible method to reset the AR values.
2284 *
2285 * @access public
2286 * @return void
2287 */
2288 public function reset_query()
2289 {
2290 $this->_reset_select();
2291 $this->_reset_write();
2292 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002293
2294 // --------------------------------------------------------------------
2295
2296 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002297 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002298 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002299 * @param array An array of fields to reset
2300 * @return void
2301 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002302 protected function _reset_run($ar_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002303 {
2304 foreach ($ar_reset_items as $item => $default_value)
2305 {
2306 if ( ! in_array($item, $this->ar_store_array))
2307 {
2308 $this->$item = $default_value;
2309 }
2310 }
2311 }
2312
2313 // --------------------------------------------------------------------
2314
2315 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002316 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002317 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002318 * @return void
2319 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002320 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002321 {
2322 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06002323 'ar_select' => array(),
2324 'ar_from' => array(),
2325 'ar_join' => array(),
2326 'ar_where' => array(),
2327 'ar_like' => array(),
2328 'ar_groupby' => array(),
2329 'ar_having' => array(),
2330 'ar_orderby' => array(),
2331 'ar_wherein' => array(),
2332 'ar_aliased_tables' => array(),
2333 'ar_no_escape' => array(),
2334 'ar_distinct' => FALSE,
2335 'ar_limit' => FALSE,
2336 'ar_offset' => FALSE,
2337 'ar_order' => FALSE,
2338 );
Barry Mienydd671972010-10-04 16:33:58 +02002339
Derek Allard2067d1a2008-11-13 22:59:24 +00002340 $this->_reset_run($ar_reset_items);
2341 }
Barry Mienydd671972010-10-04 16:33:58 +02002342
Derek Allard2067d1a2008-11-13 22:59:24 +00002343 // --------------------------------------------------------------------
2344
2345 /**
2346 * Resets the active record "write" values.
2347 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002348 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002349 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002350 * @return void
2351 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002352 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002353 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002354 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06002355 'ar_set' => array(),
2356 'ar_from' => array(),
2357 'ar_where' => array(),
2358 'ar_like' => array(),
2359 'ar_orderby' => array(),
2360 'ar_keys' => array(),
2361 'ar_limit' => FALSE,
2362 'ar_order' => FALSE
2363 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002364
2365 $this->_reset_run($ar_reset_items);
2366 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002367}
2368
2369/* End of file DB_active_rec.php */
WanWizard7219c072011-12-28 14:09:05 +01002370/* Location: ./system/database/DB_active_rec.php */