CodeIgniter installed
[living-lab-site.git] / system / database / DB_active_rec.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2 /**
3  * CodeIgniter
4  *
5  * An open source application development framework for PHP 5.1.6 or newer
6  *
7  * @package             CodeIgniter
8  * @author              ExpressionEngine Dev Team
9  * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
10  * @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  */
29 class 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();
39         var $ar_keys                            = array();
40         var $ar_limit                           = FALSE;
41         var $ar_offset                          = FALSE;
42         var $ar_order                           = FALSE;
43         var $ar_orderby                         = array();
44         var $ar_set                                     = array();
45         var $ar_wherein                         = array();
46         var $ar_aliased_tables          = array();
47         var $ar_store_array                     = array();
48
49         // Active Record Caching variables
50         var $ar_caching                         = FALSE;
51         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();
60         var $ar_cache_set                       = array();
61
62
63         // --------------------------------------------------------------------
64
65         /**
66          * Select
67          *
68          * Generates the SELECT portion of the query
69          *
70          * @access      public
71          * @param       string
72          * @return      object
73          */
74         function select($select = '*', $escape = NULL)
75         {
76                 // Set the global value if this was sepecified
77                 if (is_bool($escape))
78                 {
79                         $this->_protect_identifiers = $escape;
80                 }
81
82                 if (is_string($select))
83                 {
84                         $select = explode(',', $select);
85                 }
86
87                 foreach ($select as $val)
88                 {
89                         $val = trim($val);
90
91                         if ($val != '')
92                         {
93                                 $this->ar_select[] = $val;
94
95                                 if ($this->ar_caching === TRUE)
96                                 {
97                                         $this->ar_cache_select[] = $val;
98                                         $this->ar_cache_exists[] = 'select';
99                                 }
100                         }
101                 }
102                 return $this;
103         }
104
105         // --------------------------------------------------------------------
106
107         /**
108          * Select Max
109          *
110          * Generates a SELECT MAX(field) portion of a query
111          *
112          * @access      public
113          * @param       string  the field
114          * @param       string  an alias
115          * @return      object
116          */
117         function select_max($select = '', $alias = '')
118         {
119                 return $this->_max_min_avg_sum($select, $alias, 'MAX');
120         }
121
122         // --------------------------------------------------------------------
123
124         /**
125          * Select Min
126          *
127          * Generates a SELECT MIN(field) portion of a query
128          *
129          * @access      public
130          * @param       string  the field
131          * @param       string  an alias
132          * @return      object
133          */
134         function select_min($select = '', $alias = '')
135         {
136                 return $this->_max_min_avg_sum($select, $alias, 'MIN');
137         }
138
139         // --------------------------------------------------------------------
140
141         /**
142          * Select Average
143          *
144          * Generates a SELECT AVG(field) portion of a query
145          *
146          * @access      public
147          * @param       string  the field
148          * @param       string  an alias
149          * @return      object
150          */
151         function select_avg($select = '', $alias = '')
152         {
153                 return $this->_max_min_avg_sum($select, $alias, 'AVG');
154         }
155
156         // --------------------------------------------------------------------
157
158         /**
159          * Select Sum
160          *
161          * Generates a SELECT SUM(field) portion of a query
162          *
163          * @access      public
164          * @param       string  the field
165          * @param       string  an alias
166          * @return      object
167          */
168         function select_sum($select = '', $alias = '')
169         {
170                 return $this->_max_min_avg_sum($select, $alias, 'SUM');
171         }
172
173         // --------------------------------------------------------------------
174
175         /**
176          * Processing Function for the four functions above:
177          *
178          *      select_max()
179          *      select_min()
180          *      select_avg()
181          *  select_sum()
182          *
183          * @access      public
184          * @param       string  the field
185          * @param       string  an alias
186          * @return      object
187          */
188         function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
189         {
190                 if ( ! is_string($select) OR $select == '')
191                 {
192                         $this->display_error('db_invalid_query');
193                 }
194
195                 $type = strtoupper($type);
196
197                 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
198                 {
199                         show_error('Invalid function type: '.$type);
200                 }
201
202                 if ($alias == '')
203                 {
204                         $alias = $this->_create_alias_from_table(trim($select));
205                 }
206
207                 $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$alias;
208
209                 $this->ar_select[] = $sql;
210
211                 if ($this->ar_caching === TRUE)
212                 {
213                         $this->ar_cache_select[] = $sql;
214                         $this->ar_cache_exists[] = 'select';
215                 }
216
217                 return $this;
218         }
219
220         // --------------------------------------------------------------------
221
222         /**
223          * Determines the alias name based on the table
224          *
225          * @access      private
226          * @param       string
227          * @return      string
228          */
229         function _create_alias_from_table($item)
230         {
231                 if (strpos($item, '.') !== FALSE)
232                 {
233                         return end(explode('.', $item));
234                 }
235
236                 return $item;
237         }
238
239         // --------------------------------------------------------------------
240
241         /**
242          * DISTINCT
243          *
244          * Sets a flag which tells the query string compiler to add DISTINCT
245          *
246          * @access      public
247          * @param       bool
248          * @return      object
249          */
250         function distinct($val = TRUE)
251         {
252                 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
253                 return $this;
254         }
255
256         // --------------------------------------------------------------------
257
258         /**
259          * From
260          *
261          * Generates the FROM portion of the query
262          *
263          * @access      public
264          * @param       mixed   can be a string or array
265          * @return      object
266          */
267         function from($from)
268         {
269                 foreach ((array)$from as $val)
270                 {
271                         if (strpos($val, ',') !== FALSE)
272                         {
273                                 foreach (explode(',', $val) as $v)
274                                 {
275                                         $v = trim($v);
276                                         $this->_track_aliases($v);
277
278                                         $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
279
280                                         if ($this->ar_caching === TRUE)
281                                         {
282                                                 $this->ar_cache_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
283                                                 $this->ar_cache_exists[] = 'from';
284                                         }
285                                 }
286
287                         }
288                         else
289                         {
290                                 $val = trim($val);
291
292                                 // Extract any aliases that might exist.  We use this information
293                                 // in the _protect_identifiers to know whether to add a table prefix
294                                 $this->_track_aliases($val);
295
296                                 $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
297
298                                 if ($this->ar_caching === TRUE)
299                                 {
300                                         $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
301                                         $this->ar_cache_exists[] = 'from';
302                                 }
303                         }
304                 }
305
306                 return $this;
307         }
308
309         // --------------------------------------------------------------------
310
311         /**
312          * Join
313          *
314          * Generates the JOIN portion of the query
315          *
316          * @access      public
317          * @param       string
318          * @param       string  the join condition
319          * @param       string  the type of join
320          * @return      object
321          */
322         function join($table, $cond, $type = '')
323         {
324                 if ($type != '')
325                 {
326                         $type = strtoupper(trim($type));
327
328                         if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
329                         {
330                                 $type = '';
331                         }
332                         else
333                         {
334                                 $type .= ' ';
335                         }
336                 }
337
338                 // Extract any aliases that might exist.  We use this information
339                 // in the _protect_identifiers to know whether to add a table prefix
340                 $this->_track_aliases($table);
341
342                 // Strip apart the condition and protect the identifiers
343                 if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
344                 {
345                         $match[1] = $this->_protect_identifiers($match[1]);
346                         $match[3] = $this->_protect_identifiers($match[3]);
347
348                         $cond = $match[1].$match[2].$match[3];
349                 }
350
351                 // Assemble the JOIN statement
352                 $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
353
354                 $this->ar_join[] = $join;
355                 if ($this->ar_caching === TRUE)
356                 {
357                         $this->ar_cache_join[] = $join;
358                         $this->ar_cache_exists[] = 'join';
359                 }
360
361                 return $this;
362         }
363
364         // --------------------------------------------------------------------
365
366         /**
367          * Where
368          *
369          * Generates the WHERE portion of the query. Separates
370          * multiple calls with AND
371          *
372          * @access      public
373          * @param       mixed
374          * @param       mixed
375          * @return      object
376          */
377         function where($key, $value = NULL, $escape = TRUE)
378         {
379                 return $this->_where($key, $value, 'AND ', $escape);
380         }
381
382         // --------------------------------------------------------------------
383
384         /**
385          * OR Where
386          *
387          * Generates the WHERE portion of the query. Separates
388          * multiple calls with OR
389          *
390          * @access      public
391          * @param       mixed
392          * @param       mixed
393          * @return      object
394          */
395         function or_where($key, $value = NULL, $escape = TRUE)
396         {
397                 return $this->_where($key, $value, 'OR ', $escape);
398         }
399
400         // --------------------------------------------------------------------
401
402         /**
403          * Where
404          *
405          * Called by where() or orwhere()
406          *
407          * @access      private
408          * @param       mixed
409          * @param       mixed
410          * @param       string
411          * @return      object
412          */
413         function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
414         {
415                 if ( ! is_array($key))
416                 {
417                         $key = array($key => $value);
418                 }
419
420                 // If the escape value was not set will will base it on the global setting
421                 if ( ! is_bool($escape))
422                 {
423                         $escape = $this->_protect_identifiers;
424                 }
425
426                 foreach ($key as $k => $v)
427                 {
428                         $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type;
429
430                         if (is_null($v) && ! $this->_has_operator($k))
431                         {
432                                 // value appears not to have been set, assign the test to IS NULL
433                                 $k .= ' IS NULL';
434                         }
435
436                         if ( ! is_null($v))
437                         {
438                                 if ($escape === TRUE)
439                                 {
440                                         $k = $this->_protect_identifiers($k, FALSE, $escape);
441
442                                         $v = ' '.$this->escape($v);
443                                 }
444
445                                 if ( ! $this->_has_operator($k))
446                                 {
447                                         $k .= ' =';
448                                 }
449                         }
450                         else
451                         {
452                                 $k = $this->_protect_identifiers($k, FALSE, $escape);
453                         }
454
455                         $this->ar_where[] = $prefix.$k.$v;
456
457                         if ($this->ar_caching === TRUE)
458                         {
459                                 $this->ar_cache_where[] = $prefix.$k.$v;
460                                 $this->ar_cache_exists[] = 'where';
461                         }
462
463                 }
464
465                 return $this;
466         }
467
468         // --------------------------------------------------------------------
469
470         /**
471          * Where_in
472          *
473          * Generates a WHERE field IN ('item', 'item') SQL query joined with
474          * AND if appropriate
475          *
476          * @access      public
477          * @param       string  The field to search
478          * @param       array   The values searched on
479          * @return      object
480          */
481         function where_in($key = NULL, $values = NULL)
482         {
483                 return $this->_where_in($key, $values);
484         }
485
486         // --------------------------------------------------------------------
487
488         /**
489          * Where_in_or
490          *
491          * Generates a WHERE field IN ('item', 'item') SQL query joined with
492          * OR if appropriate
493          *
494          * @access      public
495          * @param       string  The field to search
496          * @param       array   The values searched on
497          * @return      object
498          */
499         function or_where_in($key = NULL, $values = NULL)
500         {
501                 return $this->_where_in($key, $values, FALSE, 'OR ');
502         }
503
504         // --------------------------------------------------------------------
505
506         /**
507          * Where_not_in
508          *
509          * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
510          * with AND if appropriate
511          *
512          * @access      public
513          * @param       string  The field to search
514          * @param       array   The values searched on
515          * @return      object
516          */
517         function where_not_in($key = NULL, $values = NULL)
518         {
519                 return $this->_where_in($key, $values, TRUE);
520         }
521
522         // --------------------------------------------------------------------
523
524         /**
525          * Where_not_in_or
526          *
527          * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
528          * with OR if appropriate
529          *
530          * @access      public
531          * @param       string  The field to search
532          * @param       array   The values searched on
533          * @return      object
534          */
535         function or_where_not_in($key = NULL, $values = NULL)
536         {
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          *
547          * @access      public
548          * @param       string  The field to search
549          * @param       array   The values searched on
550          * @param       boolean If the statement would be IN or NOT IN
551          * @param       string
552          * @return      object
553          */
554         function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
555         {
556                 if ($key === NULL OR $values === NULL)
557                 {
558                         return;
559                 }
560
561                 if ( ! is_array($values))
562                 {
563                         $values = array($values);
564                 }
565
566                 $not = ($not) ? ' NOT' : '';
567
568                 foreach ($values as $value)
569                 {
570                         $this->ar_wherein[] = $this->escape($value);
571                 }
572
573                 $prefix = (count($this->ar_where) == 0) ? '' : $type;
574
575                 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
576
577                 $this->ar_where[] = $where_in;
578                 if ($this->ar_caching === TRUE)
579                 {
580                         $this->ar_cache_where[] = $where_in;
581                         $this->ar_cache_exists[] = 'where';
582                 }
583
584                 // reset the array for multiple calls
585                 $this->ar_wherein = array();
586                 return $this;
587         }
588
589         // --------------------------------------------------------------------
590
591         /**
592          * Like
593          *
594          * Generates a %LIKE% portion of the query. Separates
595          * multiple calls with AND
596          *
597          * @access      public
598          * @param       mixed
599          * @param       mixed
600          * @return      object
601          */
602         function like($field, $match = '', $side = 'both')
603         {
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          *
615          * @access      public
616          * @param       mixed
617          * @param       mixed
618          * @return      object
619          */
620         function not_like($field, $match = '', $side = 'both')
621         {
622                 return $this->_like($field, $match, 'AND ', $side, 'NOT');
623         }
624
625         // --------------------------------------------------------------------
626
627         /**
628          * OR Like
629          *
630          * Generates a %LIKE% portion of the query. Separates
631          * multiple calls with OR
632          *
633          * @access      public
634          * @param       mixed
635          * @param       mixed
636          * @return      object
637          */
638         function or_like($field, $match = '', $side = 'both')
639         {
640                 return $this->_like($field, $match, 'OR ', $side);
641         }
642
643         // --------------------------------------------------------------------
644
645         /**
646          * OR Not Like
647          *
648          * Generates a NOT LIKE portion of the query. Separates
649          * multiple calls with OR
650          *
651          * @access      public
652          * @param       mixed
653          * @param       mixed
654          * @return      object
655          */
656         function or_not_like($field, $match = '', $side = 'both')
657         {
658                 return $this->_like($field, $match, 'OR ', $side, 'NOT');
659         }
660
661         // --------------------------------------------------------------------
662
663         /**
664          * Like
665          *
666          * Called by like() or orlike()
667          *
668          * @access      private
669          * @param       mixed
670          * @param       mixed
671          * @param       string
672          * @return      object
673          */
674         function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
675         {
676                 if ( ! is_array($field))
677                 {
678                         $field = array($field => $match);
679                 }
680
681                 foreach ($field as $k => $v)
682                 {
683                         $k = $this->_protect_identifiers($k);
684
685                         $prefix = (count($this->ar_like) == 0) ? '' : $type;
686
687                         $v = $this->escape_like_str($v);
688
689                         if ($side == 'before')
690                         {
691                                 $like_statement = $prefix." $k $not LIKE '%{$v}'";
692                         }
693                         elseif ($side == 'after')
694                         {
695                                 $like_statement = $prefix." $k $not LIKE '{$v}%'";
696                         }
697                         else
698                         {
699                                 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
700                         }
701
702                         // some platforms require an escape sequence definition for LIKE wildcards
703                         if ($this->_like_escape_str != '')
704                         {
705                                 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
706                         }
707
708                         $this->ar_like[] = $like_statement;
709                         if ($this->ar_caching === TRUE)
710                         {
711                                 $this->ar_cache_like[] = $like_statement;
712                                 $this->ar_cache_exists[] = 'like';
713                         }
714
715                 }
716                 return $this;
717         }
718
719         // --------------------------------------------------------------------
720
721         /**
722          * GROUP BY
723          *
724          * @access      public
725          * @param       string
726          * @return      object
727          */
728         function group_by($by)
729         {
730                 if (is_string($by))
731                 {
732                         $by = explode(',', $by);
733                 }
734
735                 foreach ($by as $val)
736                 {
737                         $val = trim($val);
738
739                         if ($val != '')
740                         {
741                                 $this->ar_groupby[] = $this->_protect_identifiers($val);
742
743                                 if ($this->ar_caching === TRUE)
744                                 {
745                                         $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
746                                         $this->ar_cache_exists[] = 'groupby';
747                                 }
748                         }
749                 }
750                 return $this;
751         }
752
753         // --------------------------------------------------------------------
754
755         /**
756          * Sets the HAVING value
757          *
758          * Separates multiple calls with AND
759          *
760          * @access      public
761          * @param       string
762          * @param       string
763          * @return      object
764          */
765         function having($key, $value = '', $escape = TRUE)
766         {
767                 return $this->_having($key, $value, 'AND ', $escape);
768         }
769
770         // --------------------------------------------------------------------
771
772         /**
773          * Sets the OR HAVING value
774          *
775          * Separates multiple calls with OR
776          *
777          * @access      public
778          * @param       string
779          * @param       string
780          * @return      object
781          */
782         function or_having($key, $value = '', $escape = TRUE)
783         {
784                 return $this->_having($key, $value, 'OR ', $escape);
785         }
786
787         // --------------------------------------------------------------------
788
789         /**
790          * Sets the HAVING values
791          *
792          * Called by having() or or_having()
793          *
794          * @access      private
795          * @param       string
796          * @param       string
797          * @return      object
798          */
799         function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
800         {
801                 if ( ! is_array($key))
802                 {
803                         $key = array($key => $value);
804                 }
805
806                 foreach ($key as $k => $v)
807                 {
808                         $prefix = (count($this->ar_having) == 0) ? '' : $type;
809
810                         if ($escape === TRUE)
811                         {
812                                 $k = $this->_protect_identifiers($k);
813                         }
814
815                         if ( ! $this->_has_operator($k))
816                         {
817                                 $k .= ' = ';
818                         }
819
820                         if ($v != '')
821                         {
822                                 $v = ' '.$this->escape_str($v);
823                         }
824
825                         $this->ar_having[] = $prefix.$k.$v;
826                         if ($this->ar_caching === TRUE)
827                         {
828                                 $this->ar_cache_having[] = $prefix.$k.$v;
829                                 $this->ar_cache_exists[] = 'having';
830                         }
831                 }
832
833                 return $this;
834         }
835
836         // --------------------------------------------------------------------
837
838         /**
839          * Sets the ORDER BY value
840          *
841          * @access      public
842          * @param       string
843          * @param       string  direction: asc or desc
844          * @return      object
845          */
846         function order_by($orderby, $direction = '')
847         {
848                 if (strtolower($direction) == 'random')
849                 {
850                         $orderby = ''; // Random results want or don't need a field name
851                         $direction = $this->_random_keyword;
852                 }
853                 elseif (trim($direction) != '')
854                 {
855                         $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
856                 }
857
858
859                 if (strpos($orderby, ',') !== FALSE)
860                 {
861                         $temp = array();
862                         foreach (explode(',', $orderby) as $part)
863                         {
864                                 $part = trim($part);
865                                 if ( ! in_array($part, $this->ar_aliased_tables))
866                                 {
867                                         $part = $this->_protect_identifiers(trim($part));
868                                 }
869
870                                 $temp[] = $part;
871                         }
872
873                         $orderby = implode(', ', $temp);
874                 }
875                 else if ($direction != $this->_random_keyword)
876                 {
877                         $orderby = $this->_protect_identifiers($orderby);
878                 }
879
880                 $orderby_statement = $orderby.$direction;
881
882                 $this->ar_orderby[] = $orderby_statement;
883                 if ($this->ar_caching === TRUE)
884                 {
885                         $this->ar_cache_orderby[] = $orderby_statement;
886                         $this->ar_cache_exists[] = 'orderby';
887                 }
888
889                 return $this;
890         }
891
892         // --------------------------------------------------------------------
893
894         /**
895          * Sets the LIMIT value
896          *
897          * @access      public
898          * @param       integer the limit value
899          * @param       integer the offset value
900          * @return      object
901          */
902         function limit($value, $offset = '')
903         {
904                 $this->ar_limit = $value;
905
906                 if ($offset != '')
907                 {
908                         $this->ar_offset = $offset;
909                 }
910
911                 return $this;
912         }
913
914         // --------------------------------------------------------------------
915
916         /**
917          * Sets the OFFSET value
918          *
919          * @access      public
920          * @param       integer the offset value
921          * @return      object
922          */
923         function offset($offset)
924         {
925                 $this->ar_offset = $offset;
926                 return $this;
927         }
928
929         // --------------------------------------------------------------------
930
931         /**
932          * The "set" function.  Allows key/value pairs to be set for inserting or updating
933          *
934          * @access      public
935          * @param       mixed
936          * @param       string
937          * @param       boolean
938          * @return      object
939          */
940         function set($key, $value = '', $escape = TRUE)
941         {
942                 $key = $this->_object_to_array($key);
943
944                 if ( ! is_array($key))
945                 {
946                         $key = array($key => $value);
947                 }
948
949                 foreach ($key as $k => $v)
950                 {
951                         if ($escape === FALSE)
952                         {
953                                 $this->ar_set[$this->_protect_identifiers($k)] = $v;
954                         }
955                         else
956                         {
957                                 $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
958                         }
959                 }
960
961                 return $this;
962         }
963
964         // --------------------------------------------------------------------
965
966         /**
967          * Get
968          *
969          * Compiles the select statement based on the other functions called
970          * and runs the query
971          *
972          * @access      public
973          * @param       string  the table
974          * @param       string  the limit clause
975          * @param       string  the offset clause
976          * @return      object
977          */
978         function get($table = '', $limit = null, $offset = null)
979         {
980                 if ($table != '')
981                 {
982                         $this->_track_aliases($table);
983                         $this->from($table);
984                 }
985
986                 if ( ! is_null($limit))
987                 {
988                         $this->limit($limit, $offset);
989                 }
990
991                 $sql = $this->_compile_select();
992
993                 $result = $this->query($sql);
994                 $this->_reset_select();
995                 return $result;
996         }
997
998         /**
999          * "Count All Results" query
1000          *
1001          * Generates a platform-specific query string that counts all records
1002          * returned by an Active Record query.
1003          *
1004          * @access      public
1005          * @param       string
1006          * @return      string
1007          */
1008         function count_all_results($table = '')
1009         {
1010                 if ($table != '')
1011                 {
1012                         $this->_track_aliases($table);
1013                         $this->from($table);
1014                 }
1015
1016                 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
1017
1018                 $query = $this->query($sql);
1019                 $this->_reset_select();
1020
1021                 if ($query->num_rows() == 0)
1022                 {
1023                         return 0;
1024                 }
1025
1026                 $row = $query->row();
1027                 return (int) $row->numrows;
1028         }
1029
1030         // --------------------------------------------------------------------
1031
1032         /**
1033          * Get_Where
1034          *
1035          * Allows the where clause, limit and offset to be added directly
1036          *
1037          * @access      public
1038          * @param       string  the where clause
1039          * @param       string  the limit clause
1040          * @param       string  the offset clause
1041          * @return      object
1042          */
1043         function get_where($table = '', $where = null, $limit = null, $offset = null)
1044         {
1045                 if ($table != '')
1046                 {
1047                         $this->from($table);
1048                 }
1049
1050                 if ( ! is_null($where))
1051                 {
1052                         $this->where($where);
1053                 }
1054
1055                 if ( ! is_null($limit))
1056                 {
1057                         $this->limit($limit, $offset);
1058                 }
1059
1060                 $sql = $this->_compile_select();
1061
1062                 $result = $this->query($sql);
1063                 $this->_reset_select();
1064                 return $result;
1065         }
1066
1067         // --------------------------------------------------------------------
1068
1069         /**
1070          * Insert_Batch
1071          *
1072          * Compiles batch insert strings and runs the queries
1073          *
1074          * @access      public
1075          * @param       string  the table to retrieve the results from
1076          * @param       array   an associative array of insert values
1077          * @return      object
1078          */
1079         function insert_batch($table = '', $set = NULL)
1080         {
1081                 if ( ! is_null($set))
1082                 {
1083                         $this->set_insert_batch($set);
1084                 }
1085
1086                 if (count($this->ar_set) == 0)
1087                 {
1088                         if ($this->db_debug)
1089                         {
1090                                 //No valid data array.  Folds in cases where keys and values did not match up
1091                                 return $this->display_error('db_must_use_set');
1092                         }
1093                         return FALSE;
1094                 }
1095
1096                 if ($table == '')
1097                 {
1098                         if ( ! isset($this->ar_from[0]))
1099                         {
1100                                 if ($this->db_debug)
1101                                 {
1102                                         return $this->display_error('db_must_set_table');
1103                                 }
1104                                 return FALSE;
1105                         }
1106
1107                         $table = $this->ar_from[0];
1108                 }
1109
1110                 // Batch this baby
1111                 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1112                 {
1113
1114                         $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
1115
1116                         //echo $sql;
1117
1118                         $this->query($sql);
1119                 }
1120
1121                 $this->_reset_write();
1122
1123
1124                 return TRUE;
1125         }
1126
1127         // --------------------------------------------------------------------
1128
1129         /**
1130          * The "set_insert_batch" function.  Allows key/value pairs to be set for batch inserts
1131          *
1132          * @access      public
1133          * @param       mixed
1134          * @param       string
1135          * @param       boolean
1136          * @return      object
1137          */
1138
1139         function set_insert_batch($key, $value = '', $escape = TRUE)
1140         {
1141                 $key = $this->_object_to_array_batch($key);
1142
1143                 if ( ! is_array($key))
1144                 {
1145                         $key = array($key => $value);
1146                 }
1147
1148                 $keys = array_keys(current($key));
1149                 sort($keys);
1150
1151                 foreach ($key as $row)
1152                 {
1153                         if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1154                         {
1155                                 // batch function above returns an error on an empty array
1156                                 $this->ar_set[] = array();
1157                                 return;
1158                         }
1159
1160                         ksort($row); // puts $row in the same order as our keys
1161
1162                         if ($escape === FALSE)
1163                         {
1164                                 $this->ar_set[] =  '('.implode(',', $row).')';
1165                         }
1166                         else
1167                         {
1168                                 $clean = array();
1169
1170                                 foreach ($row as $value)
1171                                 {
1172                                         $clean[] = $this->escape($value);
1173                                 }
1174
1175                                 $this->ar_set[] =  '('.implode(',', $clean).')';
1176                         }
1177                 }
1178
1179                 foreach ($keys as $k)
1180                 {
1181                         $this->ar_keys[] = $this->_protect_identifiers($k);
1182                 }
1183
1184                 return $this;
1185         }
1186
1187         // --------------------------------------------------------------------
1188
1189         /**
1190          * Insert
1191          *
1192          * Compiles an insert string and runs the query
1193          *
1194          * @access      public
1195          * @param       string  the table to retrieve the results from
1196          * @param       array   an associative array of insert values
1197          * @return      object
1198          */
1199         function insert($table = '', $set = NULL)
1200         {
1201                 if ( ! is_null($set))
1202                 {
1203                         $this->set($set);
1204                 }
1205
1206                 if (count($this->ar_set) == 0)
1207                 {
1208                         if ($this->db_debug)
1209                         {
1210                                 return $this->display_error('db_must_use_set');
1211                         }
1212                         return FALSE;
1213                 }
1214
1215                 if ($table == '')
1216                 {
1217                         if ( ! isset($this->ar_from[0]))
1218                         {
1219                                 if ($this->db_debug)
1220                                 {
1221                                         return $this->display_error('db_must_set_table');
1222                                 }
1223                                 return FALSE;
1224                         }
1225
1226                         $table = $this->ar_from[0];
1227                 }
1228
1229                 $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
1230
1231                 $this->_reset_write();
1232                 return $this->query($sql);
1233         }
1234
1235         function replace($table = '', $set = NULL)
1236         {
1237                 if ( ! is_null($set))
1238                 {
1239                         $this->set($set);
1240                 }
1241
1242                 if (count($this->ar_set) == 0)
1243                 {
1244                         if ($this->db_debug)
1245                         {
1246                                 return $this->display_error('db_must_use_set');
1247                         }
1248                         return FALSE;
1249                 }
1250
1251                 if ($table == '')
1252                 {
1253                         if ( ! isset($this->ar_from[0]))
1254                         {
1255                                 if ($this->db_debug)
1256                                 {
1257                                         return $this->display_error('db_must_set_table');
1258                                 }
1259                                 return FALSE;
1260                         }
1261
1262                         $table = $this->ar_from[0];
1263                 }
1264
1265                 $sql = $this->_replace($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
1266
1267                 $this->_reset_write();
1268                 return $this->query($sql);
1269         }
1270
1271         // --------------------------------------------------------------------
1272
1273         /**
1274          * Update
1275          *
1276          * Compiles an update string and runs the query
1277          *
1278          * @access      public
1279          * @param       string  the table to retrieve the results from
1280          * @param       array   an associative array of update values
1281          * @param       mixed   the where clause
1282          * @return      object
1283          */
1284         function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
1285         {
1286                 // Combine any cached components with the current statements
1287                 $this->_merge_cache();
1288
1289                 if ( ! is_null($set))
1290                 {
1291                         $this->set($set);
1292                 }
1293
1294                 if (count($this->ar_set) == 0)
1295                 {
1296                         if ($this->db_debug)
1297                         {
1298                                 return $this->display_error('db_must_use_set');
1299                         }
1300                         return FALSE;
1301                 }
1302
1303                 if ($table == '')
1304                 {
1305                         if ( ! isset($this->ar_from[0]))
1306                         {
1307                                 if ($this->db_debug)
1308                                 {
1309                                         return $this->display_error('db_must_set_table');
1310                                 }
1311                                 return FALSE;
1312                         }
1313
1314                         $table = $this->ar_from[0];
1315                 }
1316
1317                 if ($where != NULL)
1318                 {
1319                         $this->where($where);
1320                 }
1321
1322                 if ($limit != NULL)
1323                 {
1324                         $this->limit($limit);
1325                 }
1326
1327                 $sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
1328
1329                 $this->_reset_write();
1330                 return $this->query($sql);
1331         }
1332
1333
1334         // --------------------------------------------------------------------
1335
1336         /**
1337          * Update_Batch
1338          *
1339          * Compiles an update string and runs the query
1340          *
1341          * @access      public
1342          * @param       string  the table to retrieve the results from
1343          * @param       array   an associative array of update values
1344          * @param       string  the where key
1345          * @return      object
1346          */
1347         function update_batch($table = '', $set = NULL, $index = NULL)
1348         {
1349                 // Combine any cached components with the current statements
1350                 $this->_merge_cache();
1351
1352                 if (is_null($index))
1353                 {
1354                         if ($this->db_debug)
1355                         {
1356                                 return $this->display_error('db_myst_use_index');
1357                         }
1358
1359                         return FALSE;
1360                 }
1361
1362                 if ( ! is_null($set))
1363                 {
1364                         $this->set_update_batch($set, $index);
1365                 }
1366
1367                 if (count($this->ar_set) == 0)
1368                 {
1369                         if ($this->db_debug)
1370                         {
1371                                 return $this->display_error('db_must_use_set');
1372                         }
1373
1374                         return FALSE;
1375                 }
1376
1377                 if ($table == '')
1378                 {
1379                         if ( ! isset($this->ar_from[0]))
1380                         {
1381                                 if ($this->db_debug)
1382                                 {
1383                                         return $this->display_error('db_must_set_table');
1384                                 }
1385                                 return FALSE;
1386                         }
1387
1388                         $table = $this->ar_from[0];
1389                 }
1390
1391                 // Batch this baby
1392                 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1393                 {
1394                         $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);
1395
1396                         $this->query($sql);
1397                 }
1398
1399                 $this->_reset_write();
1400         }
1401
1402         // --------------------------------------------------------------------
1403
1404         /**
1405          * The "set_update_batch" function.  Allows key/value pairs to be set for batch updating
1406          *
1407          * @access      public
1408          * @param       array
1409          * @param       string
1410          * @param       boolean
1411          * @return      object
1412          */
1413
1414         function set_update_batch($key, $index = '', $escape = TRUE)
1415         {
1416                 $key = $this->_object_to_array_batch($key);
1417
1418                 if ( ! is_array($key))
1419                 {
1420                         // @todo error
1421                 }
1422
1423                 foreach ($key as $k => $v)
1424                 {
1425                         $index_set = FALSE;
1426                         $clean = array();
1427
1428                         foreach ($v as $k2 => $v2)
1429                         {
1430                                 if ($k2 == $index)
1431                                 {
1432                                         $index_set = TRUE;
1433                                 }
1434                                 else
1435                                 {
1436                                         $not[] = $k.'-'.$v;
1437                                 }
1438
1439                                 if ($escape === FALSE)
1440                                 {
1441                                         $clean[$this->_protect_identifiers($k2)] = $v2;
1442                                 }
1443                                 else
1444                                 {
1445                                         $clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
1446                                 }
1447                         }
1448
1449                         if ($index_set == FALSE)
1450                         {
1451                                 return $this->display_error('db_batch_missing_index');
1452                         }
1453
1454                         $this->ar_set[] = $clean;
1455                 }
1456
1457                 return $this;
1458         }
1459
1460         // --------------------------------------------------------------------
1461
1462         /**
1463          * Empty Table
1464          *
1465          * Compiles a delete string and runs "DELETE FROM table"
1466          *
1467          * @access      public
1468          * @param       string  the table to empty
1469          * @return      object
1470          */
1471         function empty_table($table = '')
1472         {
1473                 if ($table == '')
1474                 {
1475                         if ( ! isset($this->ar_from[0]))
1476                         {
1477                                 if ($this->db_debug)
1478                                 {
1479                                         return $this->display_error('db_must_set_table');
1480                                 }
1481                                 return FALSE;
1482                         }
1483
1484                         $table = $this->ar_from[0];
1485                 }
1486                 else
1487                 {
1488                         $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1489                 }
1490
1491                 $sql = $this->_delete($table);
1492
1493                 $this->_reset_write();
1494
1495                 return $this->query($sql);
1496         }
1497
1498         // --------------------------------------------------------------------
1499
1500         /**
1501          * Truncate
1502          *
1503          * Compiles a truncate string and runs the query
1504          * If the database does not support the truncate() command
1505          * This function maps to "DELETE FROM table"
1506          *
1507          * @access      public
1508          * @param       string  the table to truncate
1509          * @return      object
1510          */
1511         function truncate($table = '')
1512         {
1513                 if ($table == '')
1514                 {
1515                         if ( ! isset($this->ar_from[0]))
1516                         {
1517                                 if ($this->db_debug)
1518                                 {
1519                                         return $this->display_error('db_must_set_table');
1520                                 }
1521                                 return FALSE;
1522                         }
1523
1524                         $table = $this->ar_from[0];
1525                 }
1526                 else
1527                 {
1528                         $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1529                 }
1530
1531                 $sql = $this->_truncate($table);
1532
1533                 $this->_reset_write();
1534
1535                 return $this->query($sql);
1536         }
1537
1538         // --------------------------------------------------------------------
1539
1540         /**
1541          * Delete
1542          *
1543          * Compiles a delete string and runs the query
1544          *
1545          * @access      public
1546          * @param       mixed   the table(s) to delete from. String or array
1547          * @param       mixed   the where clause
1548          * @param       mixed   the limit clause
1549          * @param       boolean
1550          * @return      object
1551          */
1552         function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
1553         {
1554                 // Combine any cached components with the current statements
1555                 $this->_merge_cache();
1556
1557                 if ($table == '')
1558                 {
1559                         if ( ! isset($this->ar_from[0]))
1560                         {
1561                                 if ($this->db_debug)
1562                                 {
1563                                         return $this->display_error('db_must_set_table');
1564                                 }
1565                                 return FALSE;
1566                         }
1567
1568                         $table = $this->ar_from[0];
1569                 }
1570                 elseif (is_array($table))
1571                 {
1572                         foreach ($table as $single_table)
1573                         {
1574                                 $this->delete($single_table, $where, $limit, FALSE);
1575                         }
1576
1577                         $this->_reset_write();
1578                         return;
1579                 }
1580                 else
1581                 {
1582                         $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1583                 }
1584
1585                 if ($where != '')
1586                 {
1587                         $this->where($where);
1588                 }
1589
1590                 if ($limit != NULL)
1591                 {
1592                         $this->limit($limit);
1593                 }
1594
1595                 if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0)
1596                 {
1597                         if ($this->db_debug)
1598                         {
1599                                 return $this->display_error('db_del_must_use_where');
1600                         }
1601
1602                         return FALSE;
1603                 }
1604
1605                 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
1606
1607                 if ($reset_data)
1608                 {
1609                         $this->_reset_write();
1610                 }
1611
1612                 return $this->query($sql);
1613         }
1614
1615         // --------------------------------------------------------------------
1616
1617         /**
1618          * DB Prefix
1619          *
1620          * Prepends a database prefix if one exists in configuration
1621          *
1622          * @access      public
1623          * @param       string  the table
1624          * @return      string
1625          */
1626         function dbprefix($table = '')
1627         {
1628                 if ($table == '')
1629                 {
1630                         $this->display_error('db_table_name_required');
1631                 }
1632
1633                 return $this->dbprefix.$table;
1634         }
1635
1636         // --------------------------------------------------------------------
1637
1638         /**
1639          * Track Aliases
1640          *
1641          * Used to track SQL statements written with aliased tables.
1642          *
1643          * @access      private
1644          * @param       string  The table to inspect
1645          * @return      string
1646          */
1647         function _track_aliases($table)
1648         {
1649                 if (is_array($table))
1650                 {
1651                         foreach ($table as $t)
1652                         {
1653                                 $this->_track_aliases($t);
1654                         }
1655                         return;
1656                 }
1657
1658                 // Does the string contain a comma?  If so, we need to separate
1659                 // the string into discreet statements
1660                 if (strpos($table, ',') !== FALSE)
1661                 {
1662                         return $this->_track_aliases(explode(',', $table));
1663                 }
1664
1665                 // if a table alias is used we can recognize it by a space
1666                 if (strpos($table, " ") !== FALSE)
1667                 {
1668                         // if the alias is written with the AS keyword, remove it
1669                         $table = preg_replace('/ AS /i', ' ', $table);
1670
1671                         // Grab the alias
1672                         $table = trim(strrchr($table, " "));
1673
1674                         // Store the alias, if it doesn't already exist
1675                         if ( ! in_array($table, $this->ar_aliased_tables))
1676                         {
1677                                 $this->ar_aliased_tables[] = $table;
1678                         }
1679                 }
1680         }
1681
1682         // --------------------------------------------------------------------
1683
1684         /**
1685          * Compile the SELECT statement
1686          *
1687          * Generates a query string based on which functions were used.
1688          * Should not be called directly.  The get() function calls it.
1689          *
1690          * @access      private
1691          * @return      string
1692          */
1693         function _compile_select($select_override = FALSE)
1694         {
1695                 // Combine any cached components with the current statements
1696                 $this->_merge_cache();
1697
1698                 // ----------------------------------------------------------------
1699
1700                 // Write the "select" portion of the query
1701
1702                 if ($select_override !== FALSE)
1703                 {
1704                         $sql = $select_override;
1705                 }
1706                 else
1707                 {
1708                         $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
1709
1710                         if (count($this->ar_select) == 0)
1711                         {
1712                                 $sql .= '*';
1713                         }
1714                         else
1715                         {
1716                                 // Cycle through the "select" portion of the query and prep each column name.
1717                                 // The reason we protect identifiers here rather then in the select() function
1718                                 // is because until the user calls the from() function we don't know if there are aliases
1719                                 foreach ($this->ar_select as $key => $val)
1720                                 {
1721                                         $this->ar_select[$key] = $this->_protect_identifiers($val);
1722                                 }
1723
1724                                 $sql .= implode(', ', $this->ar_select);
1725                         }
1726                 }
1727
1728                 // ----------------------------------------------------------------
1729
1730                 // Write the "FROM" portion of the query
1731
1732                 if (count($this->ar_from) > 0)
1733                 {
1734                         $sql .= "\nFROM ";
1735
1736                         $sql .= $this->_from_tables($this->ar_from);
1737                 }
1738
1739                 // ----------------------------------------------------------------
1740
1741                 // Write the "JOIN" portion of the query
1742
1743                 if (count($this->ar_join) > 0)
1744                 {
1745                         $sql .= "\n";
1746
1747                         $sql .= implode("\n", $this->ar_join);
1748                 }
1749
1750                 // ----------------------------------------------------------------
1751
1752                 // Write the "WHERE" portion of the query
1753
1754                 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1755                 {
1756                         $sql .= "\n";
1757
1758                         $sql .= "WHERE ";
1759                 }
1760
1761                 $sql .= implode("\n", $this->ar_where);
1762
1763                 // ----------------------------------------------------------------
1764
1765                 // Write the "LIKE" portion of the query
1766
1767                 if (count($this->ar_like) > 0)
1768                 {
1769                         if (count($this->ar_where) > 0)
1770                         {
1771                                 $sql .= "\nAND ";
1772                         }
1773
1774                         $sql .= implode("\n", $this->ar_like);
1775                 }
1776
1777                 // ----------------------------------------------------------------
1778
1779                 // Write the "GROUP BY" portion of the query
1780
1781                 if (count($this->ar_groupby) > 0)
1782                 {
1783                         $sql .= "\nGROUP BY ";
1784
1785                         $sql .= implode(', ', $this->ar_groupby);
1786                 }
1787
1788                 // ----------------------------------------------------------------
1789
1790                 // Write the "HAVING" portion of the query
1791
1792                 if (count($this->ar_having) > 0)
1793                 {
1794                         $sql .= "\nHAVING ";
1795                         $sql .= implode("\n", $this->ar_having);
1796                 }
1797
1798                 // ----------------------------------------------------------------
1799
1800                 // Write the "ORDER BY" portion of the query
1801
1802                 if (count($this->ar_orderby) > 0)
1803                 {
1804                         $sql .= "\nORDER BY ";
1805                         $sql .= implode(', ', $this->ar_orderby);
1806
1807                         if ($this->ar_order !== FALSE)
1808                         {
1809                                 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1810                         }
1811                 }
1812
1813                 // ----------------------------------------------------------------
1814
1815                 // Write the "LIMIT" portion of the query
1816
1817                 if (is_numeric($this->ar_limit))
1818                 {
1819                         $sql .= "\n";
1820                         $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1821                 }
1822
1823                 return $sql;
1824         }
1825
1826         // --------------------------------------------------------------------
1827
1828         /**
1829          * Object to Array
1830          *
1831          * Takes an object as input and converts the class variables to array key/vals
1832          *
1833          * @access      public
1834          * @param       object
1835          * @return      array
1836          */
1837         function _object_to_array($object)
1838         {
1839                 if ( ! is_object($object))
1840                 {
1841                         return $object;
1842                 }
1843
1844                 $array = array();
1845                 foreach (get_object_vars($object) as $key => $val)
1846                 {
1847                         // There are some built in keys we need to ignore for this conversion
1848                         if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
1849                         {
1850                                 $array[$key] = $val;
1851                         }
1852                 }
1853
1854                 return $array;
1855         }
1856
1857         // --------------------------------------------------------------------
1858
1859         /**
1860          * Object to Array
1861          *
1862          * Takes an object as input and converts the class variables to array key/vals
1863          *
1864          * @access      public
1865          * @param       object
1866          * @return      array
1867          */
1868         function _object_to_array_batch($object)
1869         {
1870                 if ( ! is_object($object))
1871                 {
1872                         return $object;
1873                 }
1874
1875                 $array = array();
1876                 $out = get_object_vars($object);
1877                 $fields = array_keys($out);
1878
1879                 foreach ($fields as $val)
1880                 {
1881                         // There are some built in keys we need to ignore for this conversion
1882                         if ($val != '_parent_name')
1883                         {
1884
1885                                 $i = 0;
1886                                 foreach ($out[$val] as $data)
1887                                 {
1888                                         $array[$i][$val] = $data;
1889                                         $i++;
1890                                 }
1891                         }
1892                 }
1893
1894                 return $array;
1895         }
1896
1897         // --------------------------------------------------------------------
1898
1899         /**
1900          * Start Cache
1901          *
1902          * Starts AR caching
1903          *
1904          * @access      public
1905          * @return      void
1906          */
1907         function start_cache()
1908         {
1909                 $this->ar_caching = TRUE;
1910         }
1911
1912         // --------------------------------------------------------------------
1913
1914         /**
1915          * Stop Cache
1916          *
1917          * Stops AR caching
1918          *
1919          * @access      public
1920          * @return      void
1921          */
1922         function stop_cache()
1923         {
1924                 $this->ar_caching = FALSE;
1925         }
1926
1927         // --------------------------------------------------------------------
1928
1929         /**
1930          * Flush Cache
1931          *
1932          * Empties the AR cache
1933          *
1934          * @access      public
1935          * @return      void
1936          */
1937         function flush_cache()
1938         {
1939                 $this->_reset_run(
1940                                                         array(
1941                                                                         'ar_cache_select'       => array(),
1942                                                                         'ar_cache_from'         => array(),
1943                                                                         'ar_cache_join'         => array(),
1944                                                                         'ar_cache_where'        => array(),
1945                                                                         'ar_cache_like'         => array(),
1946                                                                         'ar_cache_groupby'      => array(),
1947                                                                         'ar_cache_having'       => array(),
1948                                                                         'ar_cache_orderby'      => array(),
1949                                                                         'ar_cache_set'          => array(),
1950                                                                         'ar_cache_exists'       => array()
1951                                                                 )
1952                                                         );
1953         }
1954
1955         // --------------------------------------------------------------------
1956
1957         /**
1958          * Merge Cache
1959          *
1960          * When called, this function merges any cached AR arrays with
1961          * locally called ones.
1962          *
1963          * @access      private
1964          * @return      void
1965          */
1966         function _merge_cache()
1967         {
1968                 if (count($this->ar_cache_exists) == 0)
1969                 {
1970                         return;
1971                 }
1972
1973                 foreach ($this->ar_cache_exists as $val)
1974                 {
1975                         $ar_variable    = 'ar_'.$val;
1976                         $ar_cache_var   = 'ar_cache_'.$val;
1977
1978                         if (count($this->$ar_cache_var) == 0)
1979                         {
1980                                 continue;
1981                         }
1982
1983                         $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
1984                 }
1985
1986                 // If we are "protecting identifiers" we need to examine the "from"
1987                 // portion of the query to determine if there are any aliases
1988                 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
1989                 {
1990                         $this->_track_aliases($this->ar_from);
1991                 }
1992         }
1993
1994         // --------------------------------------------------------------------
1995
1996         /**
1997          * Resets the active record values.  Called by the get() function
1998          *
1999          * @access      private
2000          * @param       array   An array of fields to reset
2001          * @return      void
2002          */
2003         function _reset_run($ar_reset_items)
2004         {
2005                 foreach ($ar_reset_items as $item => $default_value)
2006                 {
2007                         if ( ! in_array($item, $this->ar_store_array))
2008                         {
2009                                 $this->$item = $default_value;
2010                         }
2011                 }
2012         }
2013
2014         // --------------------------------------------------------------------
2015
2016         /**
2017          * Resets the active record values.  Called by the get() function
2018          *
2019          * @access      private
2020          * @return      void
2021          */
2022         function _reset_select()
2023         {
2024                 $ar_reset_items = array(
2025                                                                 'ar_select'                     => array(),
2026                                                                 'ar_from'                       => array(),
2027                                                                 'ar_join'                       => array(),
2028                                                                 'ar_where'                      => array(),
2029                                                                 'ar_like'                       => array(),
2030                                                                 'ar_groupby'            => array(),
2031                                                                 'ar_having'                     => array(),
2032                                                                 'ar_orderby'            => array(),
2033                                                                 'ar_wherein'            => array(),
2034                                                                 'ar_aliased_tables'     => array(),
2035                                                                 'ar_distinct'           => FALSE,
2036                                                                 'ar_limit'                      => FALSE,
2037                                                                 'ar_offset'                     => FALSE,
2038                                                                 'ar_order'                      => FALSE,
2039                                                         );
2040
2041                 $this->_reset_run($ar_reset_items);
2042         }
2043
2044         // --------------------------------------------------------------------
2045
2046         /**
2047          * Resets the active record "write" values.
2048          *
2049          * Called by the insert() update() insert_batch() update_batch() and delete() functions
2050          *
2051          * @access      private
2052          * @return      void
2053          */
2054         function _reset_write()
2055         {
2056                 $ar_reset_items = array(
2057                                                                 'ar_set'                => array(),
2058                                                                 'ar_from'               => array(),
2059                                                                 'ar_where'              => array(),
2060                                                                 'ar_like'               => array(),
2061                                                                 'ar_orderby'    => array(),
2062                                                                 'ar_keys'               => array(),
2063                                                                 'ar_limit'              => FALSE,
2064                                                                 'ar_order'              => FALSE
2065                                                                 );
2066
2067                 $this->_reset_run($ar_reset_items);
2068         }
2069
2070 }
2071
2072 /* End of file DB_active_rec.php */
2073 /* Location: ./system/database/DB_active_rec.php */