blob: 83d481699e74baae5066b0994de8b17dceb2ed78 [file] [log] [blame]
Razican114ab092011-04-25 17:26:45 +02001<?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 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Active Record Class
20 *
21 * This is the platform-independent base Active Record implementation class.
22 *
23 * @package CodeIgniter
24 * @subpackage Drivers
25 * @category Database
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/database/
28 */
29class CI_DB_active_record extends CI_DB_driver {
30
31 var $ar_select = array();
32 var $ar_distinct = FALSE;
33 var $ar_from = array();
34 var $ar_join = array();
35 var $ar_where = array();
36 var $ar_like = array();
37 var $ar_groupby = array();
38 var $ar_having = array();
Robin Sowell43753fd2010-09-16 12:52:07 -040039 var $ar_keys = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000040 var $ar_limit = FALSE;
41 var $ar_offset = FALSE;
42 var $ar_order = FALSE;
43 var $ar_orderby = array();
Barry Mienydd671972010-10-04 16:33:58 +020044 var $ar_set = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000045 var $ar_wherein = array();
46 var $ar_aliased_tables = array();
47 var $ar_store_array = array();
Barry Mienydd671972010-10-04 16:33:58 +020048
Derek Allard2067d1a2008-11-13 22:59:24 +000049 // Active Record Caching variables
Barry Mienydd671972010-10-04 16:33:58 +020050 var $ar_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000051 var $ar_cache_exists = array();
52 var $ar_cache_select = array();
53 var $ar_cache_from = array();
54 var $ar_cache_join = array();
55 var $ar_cache_where = array();
56 var $ar_cache_like = array();
57 var $ar_cache_groupby = array();
58 var $ar_cache_having = array();
59 var $ar_cache_orderby = array();
Barry Mienydd671972010-10-04 16:33:58 +020060 var $ar_cache_set = array();
Razican114ab092011-04-25 17:26:45 +020061
Greg Akere156c6e2011-04-20 16:03:04 -050062 var $ar_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000063
64
65 // --------------------------------------------------------------------
66
67 /**
68 * Select
69 *
70 * Generates the SELECT portion of the query
71 *
72 * @access public
73 * @param string
74 * @return object
75 */
76 function select($select = '*', $escape = NULL)
77 {
Derek Allard2067d1a2008-11-13 22:59:24 +000078 if (is_string($select))
79 {
80 $select = explode(',', $select);
81 }
82
83 foreach ($select as $val)
84 {
85 $val = trim($val);
86
87 if ($val != '')
88 {
89 $this->ar_select[] = $val;
Greg Akere156c6e2011-04-20 16:03:04 -050090 $this->ar_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +000091
92 if ($this->ar_caching === TRUE)
93 {
94 $this->ar_cache_select[] = $val;
95 $this->ar_cache_exists[] = 'select';
96 }
97 }
98 }
99 return $this;
100 }
101
102 // --------------------------------------------------------------------
103
104 /**
105 * Select Max
106 *
107 * Generates a SELECT MAX(field) portion of a query
108 *
109 * @access public
110 * @param string the field
111 * @param string an alias
112 * @return object
113 */
114 function select_max($select = '', $alias = '')
115 {
116 return $this->_max_min_avg_sum($select, $alias, 'MAX');
117 }
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 // --------------------------------------------------------------------
120
121 /**
122 * Select Min
123 *
124 * Generates a SELECT MIN(field) portion of a query
125 *
126 * @access public
127 * @param string the field
128 * @param string an alias
129 * @return object
130 */
131 function select_min($select = '', $alias = '')
132 {
133 return $this->_max_min_avg_sum($select, $alias, 'MIN');
134 }
135
136 // --------------------------------------------------------------------
137
138 /**
139 * Select Average
140 *
141 * Generates a SELECT AVG(field) portion of a query
142 *
143 * @access public
144 * @param string the field
145 * @param string an alias
146 * @return object
147 */
148 function select_avg($select = '', $alias = '')
149 {
150 return $this->_max_min_avg_sum($select, $alias, 'AVG');
151 }
152
153 // --------------------------------------------------------------------
154
155 /**
156 * Select Sum
157 *
158 * Generates a SELECT SUM(field) portion of a query
159 *
160 * @access public
161 * @param string the field
162 * @param string an alias
163 * @return object
164 */
165 function select_sum($select = '', $alias = '')
166 {
167 return $this->_max_min_avg_sum($select, $alias, 'SUM');
168 }
169
170 // --------------------------------------------------------------------
171
172 /**
173 * Processing Function for the four functions above:
174 *
175 * select_max()
176 * select_min()
177 * select_avg()
Razican114ab092011-04-25 17:26:45 +0200178 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200179 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 * @access public
181 * @param string the field
182 * @param string an alias
183 * @return object
184 */
185 function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
186 {
187 if ( ! is_string($select) OR $select == '')
188 {
189 $this->display_error('db_invalid_query');
190 }
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
195 {
196 show_error('Invalid function type: '.$type);
197 }
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 if ($alias == '')
200 {
201 $alias = $this->_create_alias_from_table(trim($select));
202 }
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$alias;
205
206 $this->ar_select[] = $sql;
Greg Aker827f3de2011-05-06 11:29:57 -0500207 $this->ar_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 if ($this->ar_caching === TRUE)
210 {
211 $this->ar_cache_select[] = $sql;
212 $this->ar_cache_exists[] = 'select';
213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 return $this;
216 }
217
218 // --------------------------------------------------------------------
219
220 /**
221 * Determines the alias name based on the table
222 *
223 * @access private
224 * @param string
225 * @return string
226 */
227 function _create_alias_from_table($item)
228 {
229 if (strpos($item, '.') !== FALSE)
230 {
231 return end(explode('.', $item));
232 }
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 return $item;
235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * DISTINCT
241 *
242 * Sets a flag which tells the query string compiler to add DISTINCT
243 *
244 * @access public
245 * @param bool
246 * @return object
247 */
248 function distinct($val = TRUE)
249 {
250 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
251 return $this;
252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 // --------------------------------------------------------------------
255
256 /**
257 * From
258 *
259 * Generates the FROM portion of the query
260 *
261 * @access public
262 * @param mixed can be a string or array
263 * @return object
264 */
265 function from($from)
266 {
267 foreach ((array)$from as $val)
268 {
269 if (strpos($val, ',') !== FALSE)
270 {
271 foreach (explode(',', $val) as $v)
272 {
273 $v = trim($v);
274 $this->_track_aliases($v);
275
276 $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200277
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 if ($this->ar_caching === TRUE)
279 {
280 $this->ar_cache_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
281 $this->ar_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200282 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 }
284
285 }
286 else
287 {
288 $val = trim($val);
289
Razican114ab092011-04-25 17:26:45 +0200290 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200291 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200293
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 if ($this->ar_caching === TRUE)
297 {
298 $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
299 $this->ar_cache_exists[] = 'from';
300 }
301 }
302 }
303
304 return $this;
305 }
306
307 // --------------------------------------------------------------------
308
309 /**
310 * Join
311 *
312 * Generates the JOIN portion of the query
313 *
314 * @access public
315 * @param string
316 * @param string the join condition
317 * @param string the type of join
318 * @return object
319 */
320 function join($table, $cond, $type = '')
Barry Mienydd671972010-10-04 16:33:58 +0200321 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 if ($type != '')
323 {
324 $type = strtoupper(trim($type));
325
326 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
327 {
328 $type = '';
329 }
330 else
331 {
332 $type .= ' ';
333 }
334 }
335
Razican114ab092011-04-25 17:26:45 +0200336 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200337 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 $this->_track_aliases($table);
339
340 // Strip apart the condition and protect the identifiers
341 if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
342 {
343 $match[1] = $this->_protect_identifiers($match[1]);
344 $match[3] = $this->_protect_identifiers($match[3]);
Barry Mienydd671972010-10-04 16:33:58 +0200345
346 $cond = $match[1].$match[2].$match[3];
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 // Assemble the JOIN statement
350 $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
351
352 $this->ar_join[] = $join;
353 if ($this->ar_caching === TRUE)
354 {
355 $this->ar_cache_join[] = $join;
356 $this->ar_cache_exists[] = 'join';
357 }
358
359 return $this;
360 }
361
362 // --------------------------------------------------------------------
363
364 /**
365 * Where
366 *
367 * Generates the WHERE portion of the query. Separates
368 * multiple calls with AND
369 *
370 * @access public
371 * @param mixed
372 * @param mixed
373 * @return object
374 */
375 function where($key, $value = NULL, $escape = TRUE)
376 {
377 return $this->_where($key, $value, 'AND ', $escape);
378 }
Barry Mienydd671972010-10-04 16:33:58 +0200379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 // --------------------------------------------------------------------
381
382 /**
383 * OR Where
384 *
385 * Generates the WHERE portion of the query. Separates
386 * multiple calls with OR
387 *
388 * @access public
389 * @param mixed
390 * @param mixed
391 * @return object
392 */
393 function or_where($key, $value = NULL, $escape = TRUE)
394 {
395 return $this->_where($key, $value, 'OR ', $escape);
396 }
397
398 // --------------------------------------------------------------------
399
400 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 * Where
402 *
403 * Called by where() or orwhere()
404 *
405 * @access private
406 * @param mixed
407 * @param mixed
408 * @param string
409 * @return object
410 */
411 function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
412 {
413 if ( ! is_array($key))
414 {
415 $key = array($key => $value);
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // If the escape value was not set will will base it on the global setting
419 if ( ! is_bool($escape))
420 {
421 $escape = $this->_protect_identifiers;
422 }
423
424 foreach ($key as $k => $v)
425 {
426 $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type;
427
428 if (is_null($v) && ! $this->_has_operator($k))
429 {
430 // value appears not to have been set, assign the test to IS NULL
431 $k .= ' IS NULL';
432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 if ( ! is_null($v))
435 {
436 if ($escape === TRUE)
437 {
438 $k = $this->_protect_identifiers($k, FALSE, $escape);
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 $v = ' '.$this->escape($v);
441 }
Razican114ab092011-04-25 17:26:45 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 if ( ! $this->_has_operator($k))
444 {
Greg Akere156c6e2011-04-20 16:03:04 -0500445 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
447 }
448 else
449 {
Barry Mienydd671972010-10-04 16:33:58 +0200450 $k = $this->_protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 }
452
453 $this->ar_where[] = $prefix.$k.$v;
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 if ($this->ar_caching === TRUE)
456 {
457 $this->ar_cache_where[] = $prefix.$k.$v;
458 $this->ar_cache_exists[] = 'where';
459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 return $this;
464 }
465
466 // --------------------------------------------------------------------
467
468 /**
469 * Where_in
470 *
471 * Generates a WHERE field IN ('item', 'item') SQL query joined with
472 * AND if appropriate
473 *
474 * @access public
475 * @param string The field to search
476 * @param array The values searched on
477 * @return object
478 */
479 function where_in($key = NULL, $values = NULL)
480 {
481 return $this->_where_in($key, $values);
482 }
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 // --------------------------------------------------------------------
485
486 /**
487 * Where_in_or
488 *
489 * Generates a WHERE field IN ('item', 'item') SQL query joined with
490 * OR if appropriate
491 *
492 * @access public
493 * @param string The field to search
494 * @param array The values searched on
495 * @return object
496 */
497 function or_where_in($key = NULL, $values = NULL)
498 {
499 return $this->_where_in($key, $values, FALSE, 'OR ');
500 }
501
502 // --------------------------------------------------------------------
503
504 /**
505 * Where_not_in
506 *
507 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
508 * with AND if appropriate
509 *
510 * @access public
511 * @param string The field to search
512 * @param array The values searched on
513 * @return object
514 */
515 function where_not_in($key = NULL, $values = NULL)
516 {
517 return $this->_where_in($key, $values, TRUE);
518 }
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 // --------------------------------------------------------------------
521
522 /**
523 * Where_not_in_or
524 *
525 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
526 * with OR if appropriate
527 *
528 * @access public
529 * @param string The field to search
530 * @param array The values searched on
531 * @return object
532 */
533 function or_where_not_in($key = NULL, $values = NULL)
534 {
535 return $this->_where_in($key, $values, TRUE, 'OR ');
536 }
537
538 // --------------------------------------------------------------------
539
540 /**
541 * Where_in
542 *
543 * Called by where_in, where_in_or, where_not_in, where_not_in_or
544 *
545 * @access public
546 * @param string The field to search
547 * @param array The values searched on
548 * @param boolean If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200549 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 * @return object
551 */
552 function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
553 {
554 if ($key === NULL OR $values === NULL)
555 {
556 return;
557 }
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 if ( ! is_array($values))
560 {
561 $values = array($values);
562 }
Barry Mienydd671972010-10-04 16:33:58 +0200563
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 $not = ($not) ? ' NOT' : '';
565
566 foreach ($values as $value)
567 {
568 $this->ar_wherein[] = $this->escape($value);
569 }
570
571 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Barry Mienydd671972010-10-04 16:33:58 +0200572
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
574
575 $this->ar_where[] = $where_in;
576 if ($this->ar_caching === TRUE)
577 {
578 $this->ar_cache_where[] = $where_in;
579 $this->ar_cache_exists[] = 'where';
580 }
581
582 // reset the array for multiple calls
583 $this->ar_wherein = array();
584 return $this;
585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 // --------------------------------------------------------------------
588
589 /**
590 * Like
591 *
592 * Generates a %LIKE% portion of the query. Separates
593 * multiple calls with AND
594 *
595 * @access public
596 * @param mixed
597 * @param mixed
598 * @return object
599 */
600 function like($field, $match = '', $side = 'both')
601 {
602 return $this->_like($field, $match, 'AND ', $side);
603 }
604
605 // --------------------------------------------------------------------
606
607 /**
608 * Not Like
609 *
610 * Generates a NOT LIKE portion of the query. Separates
611 * multiple calls with AND
612 *
613 * @access public
614 * @param mixed
615 * @param mixed
616 * @return object
617 */
618 function not_like($field, $match = '', $side = 'both')
619 {
620 return $this->_like($field, $match, 'AND ', $side, 'NOT');
621 }
Barry Mienydd671972010-10-04 16:33:58 +0200622
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 // --------------------------------------------------------------------
624
625 /**
626 * OR Like
627 *
628 * Generates a %LIKE% portion of the query. Separates
629 * multiple calls with OR
630 *
631 * @access public
632 * @param mixed
633 * @param mixed
634 * @return object
635 */
636 function or_like($field, $match = '', $side = 'both')
637 {
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 *
649 * @access public
650 * @param mixed
651 * @param mixed
652 * @return object
653 */
654 function or_not_like($field, $match = '', $side = 'both')
655 {
656 return $this->_like($field, $match, 'OR ', $side, 'NOT');
657 }
Barry Mienydd671972010-10-04 16:33:58 +0200658
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 // --------------------------------------------------------------------
660
661 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 * Like
663 *
664 * Called by like() or orlike()
665 *
666 * @access private
667 * @param mixed
668 * @param mixed
669 * @param string
670 * @return object
671 */
672 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
673 {
674 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);
Derek Allard2067d1a2008-11-13 22:59:24 +0000686
687 if ($side == 'before')
688 {
689 $like_statement = $prefix." $k $not LIKE '%{$v}'";
690 }
691 elseif ($side == 'after')
692 {
693 $like_statement = $prefix." $k $not LIKE '{$v}%'";
694 }
695 else
696 {
697 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
698 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600699
Derek Jonese4ed5832009-02-20 21:44:59 +0000700 // some platforms require an escape sequence definition for LIKE wildcards
701 if ($this->_like_escape_str != '')
702 {
Greg Aker0d424892010-01-26 02:14:44 +0000703 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000704 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 $this->ar_like[] = $like_statement;
707 if ($this->ar_caching === TRUE)
708 {
709 $this->ar_cache_like[] = $like_statement;
710 $this->ar_cache_exists[] = 'like';
711 }
Barry Mienydd671972010-10-04 16:33:58 +0200712
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 }
714 return $this;
715 }
Barry Mienydd671972010-10-04 16:33:58 +0200716
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 // --------------------------------------------------------------------
718
719 /**
720 * GROUP BY
721 *
722 * @access public
723 * @param string
724 * @return object
725 */
726 function group_by($by)
727 {
728 if (is_string($by))
729 {
730 $by = explode(',', $by);
731 }
Barry Mienydd671972010-10-04 16:33:58 +0200732
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 foreach ($by as $val)
734 {
735 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200736
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 if ($val != '')
738 {
739 $this->ar_groupby[] = $this->_protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200740
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 if ($this->ar_caching === TRUE)
742 {
743 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
744 $this->ar_cache_exists[] = 'groupby';
745 }
746 }
747 }
748 return $this;
749 }
750
751 // --------------------------------------------------------------------
752
753 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 * Sets the HAVING value
755 *
756 * Separates multiple calls with AND
757 *
758 * @access public
759 * @param string
760 * @param string
761 * @return object
762 */
763 function having($key, $value = '', $escape = TRUE)
764 {
765 return $this->_having($key, $value, 'AND ', $escape);
766 }
Barry Mienydd671972010-10-04 16:33:58 +0200767
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 // --------------------------------------------------------------------
769
770 /**
771 * Sets the OR HAVING value
772 *
773 * Separates multiple calls with OR
774 *
775 * @access public
776 * @param string
777 * @param string
778 * @return object
779 */
780 function or_having($key, $value = '', $escape = TRUE)
781 {
782 return $this->_having($key, $value, 'OR ', $escape);
783 }
Barry Mienydd671972010-10-04 16:33:58 +0200784
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 // --------------------------------------------------------------------
786
787 /**
788 * Sets the HAVING values
789 *
790 * Called by having() or or_having()
791 *
792 * @access private
793 * @param string
794 * @param string
795 * @return object
796 */
797 function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
798 {
799 if ( ! is_array($key))
800 {
801 $key = array($key => $value);
802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 foreach ($key as $k => $v)
805 {
806 $prefix = (count($this->ar_having) == 0) ? '' : $type;
807
808 if ($escape === TRUE)
809 {
810 $k = $this->_protect_identifiers($k);
811 }
812
813 if ( ! $this->_has_operator($k))
814 {
815 $k .= ' = ';
816 }
817
818 if ($v != '')
819 {
820 $v = ' '.$this->escape_str($v);
821 }
Barry Mienydd671972010-10-04 16:33:58 +0200822
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 $this->ar_having[] = $prefix.$k.$v;
824 if ($this->ar_caching === TRUE)
825 {
826 $this->ar_cache_having[] = $prefix.$k.$v;
827 $this->ar_cache_exists[] = 'having';
828 }
829 }
Barry Mienydd671972010-10-04 16:33:58 +0200830
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 return $this;
832 }
Barry Mienydd671972010-10-04 16:33:58 +0200833
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 // --------------------------------------------------------------------
835
836 /**
837 * Sets the ORDER BY value
838 *
839 * @access public
840 * @param string
841 * @param string direction: asc or desc
842 * @return object
843 */
844 function order_by($orderby, $direction = '')
845 {
846 if (strtolower($direction) == 'random')
847 {
848 $orderby = ''; // Random results want or don't need a field name
849 $direction = $this->_random_keyword;
850 }
851 elseif (trim($direction) != '')
852 {
853 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
854 }
Barry Mienydd671972010-10-04 16:33:58 +0200855
856
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 if (strpos($orderby, ',') !== FALSE)
858 {
859 $temp = array();
860 foreach (explode(',', $orderby) as $part)
861 {
862 $part = trim($part);
863 if ( ! in_array($part, $this->ar_aliased_tables))
864 {
865 $part = $this->_protect_identifiers(trim($part));
866 }
Barry Mienydd671972010-10-04 16:33:58 +0200867
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 $temp[] = $part;
869 }
Barry Mienydd671972010-10-04 16:33:58 +0200870
871 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 }
Derek Allarde37ab382009-02-03 16:13:57 +0000873 else if ($direction != $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 {
875 $orderby = $this->_protect_identifiers($orderby);
876 }
Barry Mienydd671972010-10-04 16:33:58 +0200877
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200879
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 $this->ar_orderby[] = $orderby_statement;
881 if ($this->ar_caching === TRUE)
882 {
883 $this->ar_cache_orderby[] = $orderby_statement;
884 $this->ar_cache_exists[] = 'orderby';
885 }
886
887 return $this;
888 }
Barry Mienydd671972010-10-04 16:33:58 +0200889
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 // --------------------------------------------------------------------
891
892 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 * Sets the LIMIT value
894 *
895 * @access public
896 * @param integer the limit value
897 * @param integer the offset value
898 * @return object
899 */
900 function limit($value, $offset = '')
901 {
902 $this->ar_limit = $value;
903
904 if ($offset != '')
905 {
906 $this->ar_offset = $offset;
907 }
Barry Mienydd671972010-10-04 16:33:58 +0200908
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 return $this;
910 }
Barry Mienydd671972010-10-04 16:33:58 +0200911
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 // --------------------------------------------------------------------
913
914 /**
915 * Sets the OFFSET value
916 *
917 * @access public
918 * @param integer the offset value
919 * @return object
920 */
921 function offset($offset)
922 {
923 $this->ar_offset = $offset;
924 return $this;
925 }
Barry Mienydd671972010-10-04 16:33:58 +0200926
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 // --------------------------------------------------------------------
928
929 /**
Razican114ab092011-04-25 17:26:45 +0200930 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 *
932 * @access public
933 * @param mixed
934 * @param string
935 * @param boolean
936 * @return object
937 */
938 function set($key, $value = '', $escape = TRUE)
939 {
940 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +0200941
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 if ( ! is_array($key))
943 {
944 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +0200945 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000946
947 foreach ($key as $k => $v)
948 {
949 if ($escape === FALSE)
950 {
951 $this->ar_set[$this->_protect_identifiers($k)] = $v;
952 }
953 else
954 {
Phil Sturgeond0ac1a22011-02-15 22:54:08 +0000955 $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 }
957 }
Barry Mienydd671972010-10-04 16:33:58 +0200958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 return $this;
960 }
Barry Mienydd671972010-10-04 16:33:58 +0200961
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 // --------------------------------------------------------------------
963
964 /**
965 * Get
966 *
967 * Compiles the select statement based on the other functions called
968 * and runs the query
969 *
970 * @access public
971 * @param string the table
972 * @param string the limit clause
973 * @param string the offset clause
974 * @return object
975 */
976 function get($table = '', $limit = null, $offset = null)
977 {
978 if ($table != '')
979 {
980 $this->_track_aliases($table);
981 $this->from($table);
982 }
Barry Mienydd671972010-10-04 16:33:58 +0200983
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 if ( ! is_null($limit))
985 {
986 $this->limit($limit, $offset);
987 }
Barry Mienydd671972010-10-04 16:33:58 +0200988
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 $sql = $this->_compile_select();
990
991 $result = $this->query($sql);
992 $this->_reset_select();
993 return $result;
994 }
995
996 /**
997 * "Count All Results" query
998 *
Barry Mienydd671972010-10-04 16:33:58 +0200999 * Generates a platform-specific query string that counts all records
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 * returned by an Active Record query.
1001 *
1002 * @access public
1003 * @param string
1004 * @return string
1005 */
1006 function count_all_results($table = '')
1007 {
1008 if ($table != '')
1009 {
1010 $this->_track_aliases($table);
1011 $this->from($table);
1012 }
Barry Mienydd671972010-10-04 16:33:58 +02001013
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
1015
1016 $query = $this->query($sql);
1017 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001018
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 if ($query->num_rows() == 0)
1020 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001021 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 }
1023
1024 $row = $query->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001025 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 }
1027
1028 // --------------------------------------------------------------------
1029
1030 /**
1031 * Get_Where
1032 *
1033 * Allows the where clause, limit and offset to be added directly
1034 *
1035 * @access public
1036 * @param string the where clause
1037 * @param string the limit clause
1038 * @param string the offset clause
1039 * @return object
1040 */
1041 function get_where($table = '', $where = null, $limit = null, $offset = null)
1042 {
1043 if ($table != '')
1044 {
1045 $this->from($table);
1046 }
1047
1048 if ( ! is_null($where))
1049 {
1050 $this->where($where);
1051 }
Barry Mienydd671972010-10-04 16:33:58 +02001052
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 if ( ! is_null($limit))
1054 {
1055 $this->limit($limit, $offset);
1056 }
Barry Mienydd671972010-10-04 16:33:58 +02001057
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 $sql = $this->_compile_select();
1059
1060 $result = $this->query($sql);
1061 $this->_reset_select();
1062 return $result;
1063 }
1064
1065 // --------------------------------------------------------------------
1066
1067 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001068 * Insert_Batch
1069 *
1070 * Compiles batch insert strings and runs the queries
1071 *
1072 * @access public
1073 * @param string the table to retrieve the results from
1074 * @param array an associative array of insert values
1075 * @return object
1076 */
1077 function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001078 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001079 if ( ! is_null($set))
1080 {
1081 $this->set_insert_batch($set);
1082 }
Barry Mienydd671972010-10-04 16:33:58 +02001083
Derek Jonesd10e8962010-03-02 17:10:36 -06001084 if (count($this->ar_set) == 0)
1085 {
1086 if ($this->db_debug)
1087 {
Razican114ab092011-04-25 17:26:45 +02001088 //No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001089 return $this->display_error('db_must_use_set');
1090 }
1091 return FALSE;
1092 }
1093
1094 if ($table == '')
1095 {
1096 if ( ! isset($this->ar_from[0]))
1097 {
1098 if ($this->db_debug)
1099 {
1100 return $this->display_error('db_must_set_table');
1101 }
1102 return FALSE;
1103 }
Barry Mienydd671972010-10-04 16:33:58 +02001104
Derek Jonesd10e8962010-03-02 17:10:36 -06001105 $table = $this->ar_from[0];
1106 }
1107
1108 // Batch this baby
1109 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1110 {
Barry Mienydd671972010-10-04 16:33:58 +02001111
Derek Jonesd10e8962010-03-02 17:10:36 -06001112 $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
1113
1114 //echo $sql;
1115
1116 $this->query($sql);
1117 }
Barry Mienydd671972010-10-04 16:33:58 +02001118
Derek Jonesd10e8962010-03-02 17:10:36 -06001119 $this->_reset_write();
1120
1121
Barry Mienydd671972010-10-04 16:33:58 +02001122 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001123 }
1124
1125 // --------------------------------------------------------------------
1126
1127 /**
Razican114ab092011-04-25 17:26:45 +02001128 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001129 *
1130 * @access public
1131 * @param mixed
1132 * @param string
1133 * @param boolean
1134 * @return object
1135 */
1136
1137 function set_insert_batch($key, $value = '', $escape = TRUE)
1138 {
1139 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001140
Derek Jonesd10e8962010-03-02 17:10:36 -06001141 if ( ! is_array($key))
1142 {
1143 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001144 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001145
1146 $keys = array_keys(current($key));
1147 sort($keys);
1148
1149 foreach ($key as $row)
1150 {
Barry Mienydd671972010-10-04 16:33:58 +02001151 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1152 {
1153 // batch function above returns an error on an empty array
1154 $this->ar_set[] = array();
1155 return;
1156 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001157
Barry Mienydd671972010-10-04 16:33:58 +02001158 ksort($row); // puts $row in the same order as our keys
1159
Derek Jonesd10e8962010-03-02 17:10:36 -06001160 if ($escape === FALSE)
1161 {
Razican114ab092011-04-25 17:26:45 +02001162 $this->ar_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001163 }
1164 else
1165 {
1166 $clean = array();
1167
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001168 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001169 {
Barry Mienydd671972010-10-04 16:33:58 +02001170 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001171 }
1172
Razican114ab092011-04-25 17:26:45 +02001173 $this->ar_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001174 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001175 }
1176
1177 foreach ($keys as $k)
1178 {
1179 $this->ar_keys[] = $this->_protect_identifiers($k);
1180 }
Barry Mienydd671972010-10-04 16:33:58 +02001181
Derek Jonesd10e8962010-03-02 17:10:36 -06001182 return $this;
1183 }
1184
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 // --------------------------------------------------------------------
1186
1187 /**
1188 * Insert
1189 *
1190 * Compiles an insert string and runs the query
1191 *
1192 * @access public
1193 * @param string the table to retrieve the results from
1194 * @param array an associative array of insert values
1195 * @return object
1196 */
1197 function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001198 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 if ( ! is_null($set))
1200 {
1201 $this->set($set);
1202 }
Barry Mienydd671972010-10-04 16:33:58 +02001203
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 if (count($this->ar_set) == 0)
1205 {
1206 if ($this->db_debug)
1207 {
1208 return $this->display_error('db_must_use_set');
1209 }
1210 return FALSE;
1211 }
1212
1213 if ($table == '')
1214 {
1215 if ( ! isset($this->ar_from[0]))
1216 {
1217 if ($this->db_debug)
1218 {
1219 return $this->display_error('db_must_set_table');
1220 }
1221 return FALSE;
1222 }
Barry Mienydd671972010-10-04 16:33:58 +02001223
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 $table = $this->ar_from[0];
1225 }
1226
1227 $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
Barry Mienydd671972010-10-04 16:33:58 +02001228
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001230 return $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 }
Barry Mienydd671972010-10-04 16:33:58 +02001232
Derek Jonesd10e8962010-03-02 17:10:36 -06001233 function replace($table = '', $set = NULL)
1234 {
1235 if ( ! is_null($set))
1236 {
1237 $this->set($set);
1238 }
Barry Mienydd671972010-10-04 16:33:58 +02001239
Derek Jonesd10e8962010-03-02 17:10:36 -06001240 if (count($this->ar_set) == 0)
1241 {
1242 if ($this->db_debug)
1243 {
1244 return $this->display_error('db_must_use_set');
1245 }
1246 return FALSE;
1247 }
1248
1249 if ($table == '')
1250 {
1251 if ( ! isset($this->ar_from[0]))
1252 {
1253 if ($this->db_debug)
1254 {
1255 return $this->display_error('db_must_set_table');
1256 }
1257 return FALSE;
1258 }
Barry Mienydd671972010-10-04 16:33:58 +02001259
Derek Jonesd10e8962010-03-02 17:10:36 -06001260 $table = $this->ar_from[0];
1261 }
1262
1263 $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 +02001264
Derek Jonesd10e8962010-03-02 17:10:36 -06001265 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001266 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001267 }
1268
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 // --------------------------------------------------------------------
1270
1271 /**
1272 * Update
1273 *
1274 * Compiles an update string and runs the query
1275 *
1276 * @access public
1277 * @param string the table to retrieve the results from
1278 * @param array an associative array of update values
1279 * @param mixed the where clause
1280 * @return object
1281 */
1282 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
1283 {
1284 // Combine any cached components with the current statements
1285 $this->_merge_cache();
1286
1287 if ( ! is_null($set))
1288 {
1289 $this->set($set);
1290 }
Barry Mienydd671972010-10-04 16:33:58 +02001291
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 if (count($this->ar_set) == 0)
1293 {
1294 if ($this->db_debug)
1295 {
1296 return $this->display_error('db_must_use_set');
1297 }
1298 return FALSE;
1299 }
1300
1301 if ($table == '')
1302 {
1303 if ( ! isset($this->ar_from[0]))
1304 {
1305 if ($this->db_debug)
1306 {
1307 return $this->display_error('db_must_set_table');
1308 }
1309 return FALSE;
1310 }
Barry Mienydd671972010-10-04 16:33:58 +02001311
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 $table = $this->ar_from[0];
1313 }
Barry Mienydd671972010-10-04 16:33:58 +02001314
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 if ($where != NULL)
1316 {
1317 $this->where($where);
1318 }
1319
1320 if ($limit != NULL)
1321 {
1322 $this->limit($limit);
1323 }
Barry Mienydd671972010-10-04 16:33:58 +02001324
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 $sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
Barry Mienydd671972010-10-04 16:33:58 +02001326
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 $this->_reset_write();
1328 return $this->query($sql);
1329 }
1330
Derek Jonesd10e8962010-03-02 17:10:36 -06001331
1332 // --------------------------------------------------------------------
1333
1334 /**
1335 * Update_Batch
1336 *
1337 * Compiles an update string and runs the query
1338 *
1339 * @access public
1340 * @param string the table to retrieve the results from
1341 * @param array an associative array of update values
1342 * @param string the where key
1343 * @return object
1344 */
1345 function update_batch($table = '', $set = NULL, $index = NULL)
1346 {
1347 // Combine any cached components with the current statements
1348 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001349
Derek Jonesd10e8962010-03-02 17:10:36 -06001350 if (is_null($index))
1351 {
1352 if ($this->db_debug)
1353 {
1354 return $this->display_error('db_myst_use_index');
1355 }
1356
1357 return FALSE;
1358 }
1359
1360 if ( ! is_null($set))
1361 {
1362 $this->set_update_batch($set, $index);
1363 }
1364
1365 if (count($this->ar_set) == 0)
1366 {
1367 if ($this->db_debug)
1368 {
1369 return $this->display_error('db_must_use_set');
1370 }
1371
1372 return FALSE;
1373 }
1374
1375 if ($table == '')
1376 {
1377 if ( ! isset($this->ar_from[0]))
1378 {
1379 if ($this->db_debug)
1380 {
1381 return $this->display_error('db_must_set_table');
1382 }
1383 return FALSE;
1384 }
Barry Mienydd671972010-10-04 16:33:58 +02001385
Derek Jonesd10e8962010-03-02 17:10:36 -06001386 $table = $this->ar_from[0];
1387 }
Barry Mienydd671972010-10-04 16:33:58 +02001388
Derek Jonesd10e8962010-03-02 17:10:36 -06001389 // Batch this baby
1390 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1391 {
1392 $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);
1393
1394 $this->query($sql);
1395 }
Barry Mienydd671972010-10-04 16:33:58 +02001396
Derek Jonesd10e8962010-03-02 17:10:36 -06001397 $this->_reset_write();
1398 }
1399
1400 // --------------------------------------------------------------------
1401
1402 /**
Razican114ab092011-04-25 17:26:45 +02001403 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001404 *
1405 * @access public
1406 * @param array
1407 * @param string
1408 * @param boolean
1409 * @return object
1410 */
1411
1412 function set_update_batch($key, $index = '', $escape = TRUE)
1413 {
1414 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001415
Derek Jonesd10e8962010-03-02 17:10:36 -06001416 if ( ! is_array($key))
1417 {
1418 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001419 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001420
1421 foreach ($key as $k => $v)
1422 {
1423 $index_set = FALSE;
1424 $clean = array();
1425
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001426 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001427 {
1428 if ($k2 == $index)
1429 {
1430 $index_set = TRUE;
1431 }
1432 else
1433 {
1434 $not[] = $k.'-'.$v;
1435 }
1436
1437 if ($escape === FALSE)
1438 {
1439 $clean[$this->_protect_identifiers($k2)] = $v2;
1440 }
1441 else
1442 {
Barry Mienydd671972010-10-04 16:33:58 +02001443 $clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001444 }
1445 }
1446
1447 if ($index_set == FALSE)
1448 {
1449 return $this->display_error('db_batch_missing_index');
1450 }
1451
1452 $this->ar_set[] = $clean;
1453 }
Barry Mienydd671972010-10-04 16:33:58 +02001454
Derek Jonesd10e8962010-03-02 17:10:36 -06001455 return $this;
1456 }
1457
Derek Allard2067d1a2008-11-13 22:59:24 +00001458 // --------------------------------------------------------------------
1459
1460 /**
1461 * Empty Table
1462 *
1463 * Compiles a delete string and runs "DELETE FROM table"
1464 *
1465 * @access public
1466 * @param string the table to empty
1467 * @return object
1468 */
1469 function empty_table($table = '')
1470 {
1471 if ($table == '')
1472 {
1473 if ( ! isset($this->ar_from[0]))
1474 {
1475 if ($this->db_debug)
1476 {
1477 return $this->display_error('db_must_set_table');
1478 }
1479 return FALSE;
1480 }
1481
1482 $table = $this->ar_from[0];
1483 }
1484 else
1485 {
1486 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1487 }
1488
1489 $sql = $this->_delete($table);
1490
1491 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001492
Derek Allard2067d1a2008-11-13 22:59:24 +00001493 return $this->query($sql);
1494 }
1495
1496 // --------------------------------------------------------------------
1497
1498 /**
1499 * Truncate
1500 *
1501 * Compiles a truncate string and runs the query
1502 * If the database does not support the truncate() command
1503 * This function maps to "DELETE FROM table"
1504 *
1505 * @access public
1506 * @param string the table to truncate
1507 * @return object
1508 */
1509 function truncate($table = '')
1510 {
1511 if ($table == '')
1512 {
1513 if ( ! isset($this->ar_from[0]))
1514 {
1515 if ($this->db_debug)
1516 {
1517 return $this->display_error('db_must_set_table');
1518 }
1519 return FALSE;
1520 }
1521
1522 $table = $this->ar_from[0];
1523 }
1524 else
1525 {
1526 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1527 }
1528
1529 $sql = $this->_truncate($table);
1530
1531 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001532
Derek Allard2067d1a2008-11-13 22:59:24 +00001533 return $this->query($sql);
1534 }
Barry Mienydd671972010-10-04 16:33:58 +02001535
Derek Allard2067d1a2008-11-13 22:59:24 +00001536 // --------------------------------------------------------------------
1537
1538 /**
1539 * Delete
1540 *
1541 * Compiles a delete string and runs the query
1542 *
1543 * @access public
1544 * @param mixed the table(s) to delete from. String or array
1545 * @param mixed the where clause
1546 * @param mixed the limit clause
1547 * @param boolean
1548 * @return object
1549 */
1550 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
1551 {
1552 // Combine any cached components with the current statements
1553 $this->_merge_cache();
1554
1555 if ($table == '')
1556 {
1557 if ( ! isset($this->ar_from[0]))
1558 {
1559 if ($this->db_debug)
1560 {
1561 return $this->display_error('db_must_set_table');
1562 }
1563 return FALSE;
1564 }
1565
1566 $table = $this->ar_from[0];
1567 }
1568 elseif (is_array($table))
1569 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001570 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001571 {
1572 $this->delete($single_table, $where, $limit, FALSE);
1573 }
1574
1575 $this->_reset_write();
1576 return;
1577 }
1578 else
1579 {
1580 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1581 }
1582
1583 if ($where != '')
1584 {
1585 $this->where($where);
1586 }
1587
1588 if ($limit != NULL)
1589 {
1590 $this->limit($limit);
1591 }
1592
Derek Allard03d783b2009-05-03 19:45:45 +00001593 if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001594 {
1595 if ($this->db_debug)
1596 {
1597 return $this->display_error('db_del_must_use_where');
1598 }
1599
1600 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001601 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001602
1603 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
1604
1605 if ($reset_data)
1606 {
1607 $this->_reset_write();
1608 }
Barry Mienydd671972010-10-04 16:33:58 +02001609
Derek Allard2067d1a2008-11-13 22:59:24 +00001610 return $this->query($sql);
1611 }
1612
1613 // --------------------------------------------------------------------
1614
1615 /**
1616 * DB Prefix
1617 *
1618 * Prepends a database prefix if one exists in configuration
1619 *
1620 * @access public
1621 * @param string the table
1622 * @return string
1623 */
1624 function dbprefix($table = '')
1625 {
1626 if ($table == '')
1627 {
1628 $this->display_error('db_table_name_required');
1629 }
1630
1631 return $this->dbprefix.$table;
1632 }
1633
1634 // --------------------------------------------------------------------
1635
1636 /**
1637 * Track Aliases
1638 *
1639 * Used to track SQL statements written with aliased tables.
1640 *
1641 * @access private
1642 * @param string The table to inspect
1643 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001644 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001645 function _track_aliases($table)
1646 {
1647 if (is_array($table))
1648 {
1649 foreach ($table as $t)
1650 {
1651 $this->_track_aliases($t);
1652 }
1653 return;
1654 }
Barry Mienydd671972010-10-04 16:33:58 +02001655
Razican114ab092011-04-25 17:26:45 +02001656 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001657 // the string into discreet statements
1658 if (strpos($table, ',') !== FALSE)
1659 {
1660 return $this->_track_aliases(explode(',', $table));
1661 }
Barry Mienydd671972010-10-04 16:33:58 +02001662
Derek Allard2067d1a2008-11-13 22:59:24 +00001663 // if a table alias is used we can recognize it by a space
1664 if (strpos($table, " ") !== FALSE)
1665 {
1666 // if the alias is written with the AS keyword, remove it
1667 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001668
Derek Allard2067d1a2008-11-13 22:59:24 +00001669 // Grab the alias
1670 $table = trim(strrchr($table, " "));
Barry Mienydd671972010-10-04 16:33:58 +02001671
Derek Allard2067d1a2008-11-13 22:59:24 +00001672 // Store the alias, if it doesn't already exist
1673 if ( ! in_array($table, $this->ar_aliased_tables))
1674 {
1675 $this->ar_aliased_tables[] = $table;
1676 }
1677 }
1678 }
1679
1680 // --------------------------------------------------------------------
1681
1682 /**
1683 * Compile the SELECT statement
1684 *
1685 * Generates a query string based on which functions were used.
Razican114ab092011-04-25 17:26:45 +02001686 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001687 *
1688 * @access private
1689 * @return string
1690 */
1691 function _compile_select($select_override = FALSE)
1692 {
1693 // Combine any cached components with the current statements
1694 $this->_merge_cache();
1695
1696 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001697
Derek Allard2067d1a2008-11-13 22:59:24 +00001698 // Write the "select" portion of the query
1699
1700 if ($select_override !== FALSE)
1701 {
1702 $sql = $select_override;
1703 }
1704 else
1705 {
1706 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001707
Derek Allard2067d1a2008-11-13 22:59:24 +00001708 if (count($this->ar_select) == 0)
1709 {
Barry Mienydd671972010-10-04 16:33:58 +02001710 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001711 }
1712 else
Barry Mienydd671972010-10-04 16:33:58 +02001713 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001714 // Cycle through the "select" portion of the query and prep each column name.
1715 // The reason we protect identifiers here rather then in the select() function
1716 // is because until the user calls the from() function we don't know if there are aliases
1717 foreach ($this->ar_select as $key => $val)
1718 {
Greg Akere156c6e2011-04-20 16:03:04 -05001719 $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $this->ar_no_escape[$key]);
Derek Allard2067d1a2008-11-13 22:59:24 +00001720 }
Barry Mienydd671972010-10-04 16:33:58 +02001721
Derek Allard2067d1a2008-11-13 22:59:24 +00001722 $sql .= implode(', ', $this->ar_select);
1723 }
1724 }
1725
1726 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001727
Derek Allard2067d1a2008-11-13 22:59:24 +00001728 // Write the "FROM" portion of the query
1729
1730 if (count($this->ar_from) > 0)
1731 {
1732 $sql .= "\nFROM ";
1733
1734 $sql .= $this->_from_tables($this->ar_from);
1735 }
1736
1737 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001738
Derek Allard2067d1a2008-11-13 22:59:24 +00001739 // Write the "JOIN" portion of the query
1740
1741 if (count($this->ar_join) > 0)
1742 {
1743 $sql .= "\n";
1744
1745 $sql .= implode("\n", $this->ar_join);
1746 }
1747
1748 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001749
Derek Allard2067d1a2008-11-13 22:59:24 +00001750 // Write the "WHERE" portion of the query
1751
1752 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1753 {
Greg Akere156c6e2011-04-20 16:03:04 -05001754 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00001755 }
1756
1757 $sql .= implode("\n", $this->ar_where);
1758
1759 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001760
Derek Allard2067d1a2008-11-13 22:59:24 +00001761 // Write the "LIKE" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001762
Derek Allard2067d1a2008-11-13 22:59:24 +00001763 if (count($this->ar_like) > 0)
1764 {
1765 if (count($this->ar_where) > 0)
1766 {
1767 $sql .= "\nAND ";
1768 }
1769
1770 $sql .= implode("\n", $this->ar_like);
1771 }
1772
1773 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001774
Derek Allard2067d1a2008-11-13 22:59:24 +00001775 // Write the "GROUP BY" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001776
Derek Allard2067d1a2008-11-13 22:59:24 +00001777 if (count($this->ar_groupby) > 0)
1778 {
1779 $sql .= "\nGROUP BY ";
Barry Mienydd671972010-10-04 16:33:58 +02001780
Derek Allard2067d1a2008-11-13 22:59:24 +00001781 $sql .= implode(', ', $this->ar_groupby);
1782 }
1783
1784 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001785
Derek Allard2067d1a2008-11-13 22:59:24 +00001786 // Write the "HAVING" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001787
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 if (count($this->ar_having) > 0)
1789 {
1790 $sql .= "\nHAVING ";
1791 $sql .= implode("\n", $this->ar_having);
1792 }
1793
1794 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001795
Derek Allard2067d1a2008-11-13 22:59:24 +00001796 // Write the "ORDER BY" portion of the query
1797
1798 if (count($this->ar_orderby) > 0)
1799 {
1800 $sql .= "\nORDER BY ";
1801 $sql .= implode(', ', $this->ar_orderby);
Barry Mienydd671972010-10-04 16:33:58 +02001802
Derek Allard2067d1a2008-11-13 22:59:24 +00001803 if ($this->ar_order !== FALSE)
1804 {
1805 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02001806 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001807 }
1808
1809 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001810
Derek Allard2067d1a2008-11-13 22:59:24 +00001811 // Write the "LIMIT" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001812
Derek Allard2067d1a2008-11-13 22:59:24 +00001813 if (is_numeric($this->ar_limit))
1814 {
1815 $sql .= "\n";
1816 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1817 }
1818
1819 return $sql;
1820 }
1821
1822 // --------------------------------------------------------------------
1823
1824 /**
1825 * Object to Array
1826 *
1827 * Takes an object as input and converts the class variables to array key/vals
1828 *
1829 * @access public
1830 * @param object
1831 * @return array
1832 */
1833 function _object_to_array($object)
1834 {
1835 if ( ! is_object($object))
1836 {
1837 return $object;
1838 }
Barry Mienydd671972010-10-04 16:33:58 +02001839
Derek Allard2067d1a2008-11-13 22:59:24 +00001840 $array = array();
1841 foreach (get_object_vars($object) as $key => $val)
1842 {
1843 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06001844 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00001845 {
1846 $array[$key] = $val;
1847 }
1848 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001849
1850 return $array;
1851 }
Barry Mienydd671972010-10-04 16:33:58 +02001852
Derek Jonesd10e8962010-03-02 17:10:36 -06001853 // --------------------------------------------------------------------
1854
1855 /**
1856 * Object to Array
1857 *
1858 * Takes an object as input and converts the class variables to array key/vals
1859 *
1860 * @access public
1861 * @param object
1862 * @return array
1863 */
1864 function _object_to_array_batch($object)
1865 {
1866 if ( ! is_object($object))
1867 {
1868 return $object;
1869 }
Barry Mienydd671972010-10-04 16:33:58 +02001870
Derek Jonesd10e8962010-03-02 17:10:36 -06001871 $array = array();
1872 $out = get_object_vars($object);
1873 $fields = array_keys($out);
1874
1875 foreach ($fields as $val)
1876 {
1877 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06001878 if ($val != '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06001879 {
1880
1881 $i = 0;
1882 foreach ($out[$val] as $data)
1883 {
1884 $array[$i][$val] = $data;
1885 $i++;
1886 }
1887 }
1888 }
1889
Derek Allard2067d1a2008-11-13 22:59:24 +00001890 return $array;
1891 }
Barry Mienydd671972010-10-04 16:33:58 +02001892
Derek Allard2067d1a2008-11-13 22:59:24 +00001893 // --------------------------------------------------------------------
1894
1895 /**
1896 * Start Cache
1897 *
1898 * Starts AR caching
1899 *
1900 * @access public
1901 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001902 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001903 function start_cache()
1904 {
1905 $this->ar_caching = TRUE;
1906 }
1907
1908 // --------------------------------------------------------------------
1909
1910 /**
1911 * Stop Cache
1912 *
1913 * Stops AR caching
1914 *
1915 * @access public
1916 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001917 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001918 function stop_cache()
1919 {
1920 $this->ar_caching = FALSE;
1921 }
1922
1923 // --------------------------------------------------------------------
1924
1925 /**
1926 * Flush Cache
1927 *
1928 * Empties the AR cache
1929 *
1930 * @access public
1931 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001932 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001933 function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02001934 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001935 $this->_reset_run(
1936 array(
Barry Mienydd671972010-10-04 16:33:58 +02001937 'ar_cache_select' => array(),
1938 'ar_cache_from' => array(),
Derek Allard2067d1a2008-11-13 22:59:24 +00001939 'ar_cache_join' => array(),
Barry Mienydd671972010-10-04 16:33:58 +02001940 'ar_cache_where' => array(),
1941 'ar_cache_like' => array(),
1942 'ar_cache_groupby' => array(),
1943 'ar_cache_having' => array(),
1944 'ar_cache_orderby' => array(),
Derek Allard2067d1a2008-11-13 22:59:24 +00001945 'ar_cache_set' => array(),
1946 'ar_cache_exists' => array()
1947 )
Barry Mienydd671972010-10-04 16:33:58 +02001948 );
Derek Allard2067d1a2008-11-13 22:59:24 +00001949 }
1950
1951 // --------------------------------------------------------------------
1952
1953 /**
1954 * Merge Cache
1955 *
Barry Mienydd671972010-10-04 16:33:58 +02001956 * When called, this function merges any cached AR arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00001957 * locally called ones.
1958 *
1959 * @access private
1960 * @return void
1961 */
1962 function _merge_cache()
1963 {
1964 if (count($this->ar_cache_exists) == 0)
1965 {
1966 return;
1967 }
1968
1969 foreach ($this->ar_cache_exists as $val)
1970 {
1971 $ar_variable = 'ar_'.$val;
1972 $ar_cache_var = 'ar_cache_'.$val;
1973
1974 if (count($this->$ar_cache_var) == 0)
1975 {
1976 continue;
1977 }
1978
1979 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
1980 }
1981
1982 // If we are "protecting identifiers" we need to examine the "from"
1983 // portion of the query to determine if there are any aliases
1984 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
1985 {
1986 $this->_track_aliases($this->ar_from);
1987 }
1988 }
1989
1990 // --------------------------------------------------------------------
1991
1992 /**
Razican114ab092011-04-25 17:26:45 +02001993 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00001994 *
1995 * @access private
1996 * @param array An array of fields to reset
1997 * @return void
1998 */
1999 function _reset_run($ar_reset_items)
2000 {
2001 foreach ($ar_reset_items as $item => $default_value)
2002 {
2003 if ( ! in_array($item, $this->ar_store_array))
2004 {
2005 $this->$item = $default_value;
2006 }
2007 }
2008 }
2009
2010 // --------------------------------------------------------------------
2011
2012 /**
Razican114ab092011-04-25 17:26:45 +02002013 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002014 *
2015 * @access private
2016 * @return void
2017 */
2018 function _reset_select()
2019 {
2020 $ar_reset_items = array(
Barry Mienydd671972010-10-04 16:33:58 +02002021 'ar_select' => array(),
2022 'ar_from' => array(),
2023 'ar_join' => array(),
2024 'ar_where' => array(),
2025 'ar_like' => array(),
2026 'ar_groupby' => array(),
2027 'ar_having' => array(),
2028 'ar_orderby' => array(),
2029 'ar_wherein' => array(),
Derek Allard2067d1a2008-11-13 22:59:24 +00002030 'ar_aliased_tables' => array(),
Greg Akere156c6e2011-04-20 16:03:04 -05002031 'ar_no_escape' => array(),
Barry Mienydd671972010-10-04 16:33:58 +02002032 'ar_distinct' => FALSE,
2033 'ar_limit' => FALSE,
2034 'ar_offset' => FALSE,
Derek Allard2067d1a2008-11-13 22:59:24 +00002035 'ar_order' => FALSE,
2036 );
Barry Mienydd671972010-10-04 16:33:58 +02002037
Derek Allard2067d1a2008-11-13 22:59:24 +00002038 $this->_reset_run($ar_reset_items);
2039 }
Barry Mienydd671972010-10-04 16:33:58 +02002040
Derek Allard2067d1a2008-11-13 22:59:24 +00002041 // --------------------------------------------------------------------
2042
2043 /**
2044 * Resets the active record "write" values.
2045 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002046 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002047 *
2048 * @access private
2049 * @return void
2050 */
2051 function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002052 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 $ar_reset_items = array(
Barry Mienydd671972010-10-04 16:33:58 +02002054 'ar_set' => array(),
2055 'ar_from' => array(),
2056 'ar_where' => array(),
Derek Allard2067d1a2008-11-13 22:59:24 +00002057 'ar_like' => array(),
Robin Sowell43753fd2010-09-16 12:52:07 -04002058 'ar_orderby' => array(),
Barry Mienydd671972010-10-04 16:33:58 +02002059 'ar_keys' => array(),
2060 'ar_limit' => FALSE,
Derek Allard2067d1a2008-11-13 22:59:24 +00002061 'ar_order' => FALSE
2062 );
2063
2064 $this->_reset_run($ar_reset_items);
2065 }
Barry Mienydd671972010-10-04 16:33:58 +02002066
Derek Allard2067d1a2008-11-13 22:59:24 +00002067}
2068
2069/* End of file DB_active_rec.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00002070/* Location: ./system/database/DB_active_rec.php */