1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
21 * This is the platform-independent base Active Record implementation class.
23 * @package CodeIgniter
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/database/
29 class CI_DB_active_record extends CI_DB_driver {
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_limit = FALSE;
40 var $ar_offset = FALSE;
41 var $ar_order = FALSE;
42 var $ar_orderby = array();
43 var $ar_set = array();
44 var $ar_wherein = array();
45 var $ar_aliased_tables = array();
46 var $ar_store_array = array();
48 // Active Record Caching variables
49 var $ar_caching = FALSE;
50 var $ar_cache_exists = array();
51 var $ar_cache_select = array();
52 var $ar_cache_from = array();
53 var $ar_cache_join = array();
54 var $ar_cache_where = array();
55 var $ar_cache_like = array();
56 var $ar_cache_groupby = array();
57 var $ar_cache_having = array();
58 var $ar_cache_orderby = array();
59 var $ar_cache_set = array();
62 // --------------------------------------------------------------------
67 * Generates the SELECT portion of the query
73 function select($select = '*', $escape = NULL)
75 // Set the global value if this was sepecified
78 $this->_protect_identifiers = $escape;
81 if (is_string($select))
83 $select = explode(',', $select);
86 foreach ($select as $val)
92 $this->ar_select[] = $val;
94 if ($this->ar_caching === TRUE)
96 $this->ar_cache_select[] = $val;
97 $this->ar_cache_exists[] = 'select';
104 // --------------------------------------------------------------------
109 * Generates a SELECT MAX(field) portion of a query
112 * @param string the field
113 * @param string an alias
116 function select_max($select = '', $alias = '')
118 return $this->_max_min_avg_sum($select, $alias, 'MAX');
121 // --------------------------------------------------------------------
126 * Generates a SELECT MIN(field) portion of a query
129 * @param string the field
130 * @param string an alias
133 function select_min($select = '', $alias = '')
135 return $this->_max_min_avg_sum($select, $alias, 'MIN');
138 // --------------------------------------------------------------------
143 * Generates a SELECT AVG(field) portion of a query
146 * @param string the field
147 * @param string an alias
150 function select_avg($select = '', $alias = '')
152 return $this->_max_min_avg_sum($select, $alias, 'AVG');
155 // --------------------------------------------------------------------
160 * Generates a SELECT SUM(field) portion of a query
163 * @param string the field
164 * @param string an alias
167 function select_sum($select = '', $alias = '')
169 return $this->_max_min_avg_sum($select, $alias, 'SUM');
172 // --------------------------------------------------------------------
175 * Processing Function for the four functions above:
183 * @param string the field
184 * @param string an alias
187 function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
189 if ( ! is_string($select) OR $select == '')
191 $this->display_error('db_invalid_query');
194 $type = strtoupper($type);
196 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
198 show_error('Invalid function type: '.$type);
203 $alias = $this->_create_alias_from_table(trim($select));
206 $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$alias;
208 $this->ar_select[] = $sql;
210 if ($this->ar_caching === TRUE)
212 $this->ar_cache_select[] = $sql;
213 $this->ar_cache_exists[] = 'select';
219 // --------------------------------------------------------------------
222 * Determines the alias name based on the table
228 function _create_alias_from_table($item)
230 if (strpos($item, '.') !== FALSE)
232 return end(explode('.', $item));
238 // --------------------------------------------------------------------
243 * Sets a flag which tells the query string compiler to add DISTINCT
249 function distinct($val = TRUE)
251 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
255 // --------------------------------------------------------------------
260 * Generates the FROM portion of the query
263 * @param mixed can be a string or array
268 foreach ((array)$from as $val)
270 if (strpos($val, ',') !== FALSE)
272 foreach (explode(',', $val) as $v)
275 $this->_track_aliases($v);
277 $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
279 if ($this->ar_caching === TRUE)
281 $this->ar_cache_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
282 $this->ar_cache_exists[] = 'from';
291 // Extract any aliases that might exist. We use this information
292 // in the _protect_identifiers to know whether to add a table prefix
293 $this->_track_aliases($val);
295 $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
297 if ($this->ar_caching === TRUE)
299 $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
300 $this->ar_cache_exists[] = 'from';
308 // --------------------------------------------------------------------
313 * Generates the JOIN portion of the query
317 * @param string the join condition
318 * @param string the type of join
321 function join($table, $cond, $type = '')
325 $type = strtoupper(trim($type));
327 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
337 // Extract any aliases that might exist. We use this information
338 // in the _protect_identifiers to know whether to add a table prefix
339 $this->_track_aliases($table);
341 // Strip apart the condition and protect the identifiers
342 if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
344 $match[1] = $this->_protect_identifiers($match[1]);
345 $match[3] = $this->_protect_identifiers($match[3]);
347 $cond = $match[1].$match[2].$match[3];
350 // Assemble the JOIN statement
351 $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
353 $this->ar_join[] = $join;
354 if ($this->ar_caching === TRUE)
356 $this->ar_cache_join[] = $join;
357 $this->ar_cache_exists[] = 'join';
363 // --------------------------------------------------------------------
368 * Generates the WHERE portion of the query. Separates
369 * multiple calls with AND
376 function where($key, $value = NULL, $escape = TRUE)
378 return $this->_where($key, $value, 'AND ', $escape);
381 // --------------------------------------------------------------------
386 * Generates the WHERE portion of the query. Separates
387 * multiple calls with OR
394 function or_where($key, $value = NULL, $escape = TRUE)
396 return $this->_where($key, $value, 'OR ', $escape);
399 // --------------------------------------------------------------------
402 * orwhere() is an alias of or_where()
403 * this function is here for backwards compatibility, as
404 * orwhere() has been deprecated
406 function orwhere($key, $value = NULL, $escape = TRUE)
408 return $this->or_where($key, $value, $escape);
411 // --------------------------------------------------------------------
416 * Called by where() or orwhere()
424 function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
426 if ( ! is_array($key))
428 $key = array($key => $value);
431 // If the escape value was not set will will base it on the global setting
432 if ( ! is_bool($escape))
434 $escape = $this->_protect_identifiers;
437 foreach ($key as $k => $v)
439 $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type;
441 if (is_null($v) && ! $this->_has_operator($k))
443 // value appears not to have been set, assign the test to IS NULL
449 if ($escape === TRUE)
451 $k = $this->_protect_identifiers($k, FALSE, $escape);
453 $v = ' '.$this->escape($v);
456 if ( ! $this->_has_operator($k))
463 $k = $this->_protect_identifiers($k, FALSE, $escape);
466 $this->ar_where[] = $prefix.$k.$v;
468 if ($this->ar_caching === TRUE)
470 $this->ar_cache_where[] = $prefix.$k.$v;
471 $this->ar_cache_exists[] = 'where';
479 // --------------------------------------------------------------------
484 * Generates a WHERE field IN ('item', 'item') SQL query joined with
488 * @param string The field to search
489 * @param array The values searched on
492 function where_in($key = NULL, $values = NULL)
494 return $this->_where_in($key, $values);
497 // --------------------------------------------------------------------
502 * Generates a WHERE field IN ('item', 'item') SQL query joined with
506 * @param string The field to search
507 * @param array The values searched on
510 function or_where_in($key = NULL, $values = NULL)
512 return $this->_where_in($key, $values, FALSE, 'OR ');
515 // --------------------------------------------------------------------
520 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
521 * with AND if appropriate
524 * @param string The field to search
525 * @param array The values searched on
528 function where_not_in($key = NULL, $values = NULL)
530 return $this->_where_in($key, $values, TRUE);
533 // --------------------------------------------------------------------
538 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
539 * with OR if appropriate
542 * @param string The field to search
543 * @param array The values searched on
546 function or_where_not_in($key = NULL, $values = NULL)
548 return $this->_where_in($key, $values, TRUE, 'OR ');
551 // --------------------------------------------------------------------
556 * Called by where_in, where_in_or, where_not_in, where_not_in_or
559 * @param string The field to search
560 * @param array The values searched on
561 * @param boolean If the statement would be IN or NOT IN
565 function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
567 if ($key === NULL OR $values === NULL)
572 if ( ! is_array($values))
574 $values = array($values);
577 $not = ($not) ? ' NOT' : '';
579 foreach ($values as $value)
581 $this->ar_wherein[] = $this->escape($value);
584 $prefix = (count($this->ar_where) == 0) ? '' : $type;
586 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
588 $this->ar_where[] = $where_in;
589 if ($this->ar_caching === TRUE)
591 $this->ar_cache_where[] = $where_in;
592 $this->ar_cache_exists[] = 'where';
595 // reset the array for multiple calls
596 $this->ar_wherein = array();
600 // --------------------------------------------------------------------
605 * Generates a %LIKE% portion of the query. Separates
606 * multiple calls with AND
613 function like($field, $match = '', $side = 'both')
615 return $this->_like($field, $match, 'AND ', $side);
618 // --------------------------------------------------------------------
623 * Generates a NOT LIKE portion of the query. Separates
624 * multiple calls with AND
631 function not_like($field, $match = '', $side = 'both')
633 return $this->_like($field, $match, 'AND ', $side, 'NOT');
636 // --------------------------------------------------------------------
641 * Generates a %LIKE% portion of the query. Separates
642 * multiple calls with OR
649 function or_like($field, $match = '', $side = 'both')
651 return $this->_like($field, $match, 'OR ', $side);
654 // --------------------------------------------------------------------
659 * Generates a NOT LIKE portion of the query. Separates
660 * multiple calls with OR
667 function or_not_like($field, $match = '', $side = 'both')
669 return $this->_like($field, $match, 'OR ', $side, 'NOT');
672 // --------------------------------------------------------------------
675 * orlike() is an alias of or_like()
676 * this function is here for backwards compatibility, as
677 * orlike() has been deprecated
679 function orlike($field, $match = '', $side = 'both')
681 return $this->or_like($field, $match, $side);
684 // --------------------------------------------------------------------
689 * Called by like() or orlike()
697 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
699 if ( ! is_array($field))
701 $field = array($field => $match);
704 foreach ($field as $k => $v)
706 $k = $this->_protect_identifiers($k);
708 $prefix = (count($this->ar_like) == 0) ? '' : $type;
710 $v = $this->escape_like_str($v);
712 if ($side == 'before')
714 $like_statement = $prefix." $k $not LIKE '%{$v}'";
716 elseif ($side == 'after')
718 $like_statement = $prefix." $k $not LIKE '{$v}%'";
722 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
725 // some platforms require an escape sequence definition for LIKE wildcards
726 if ($this->_like_escape_str != '')
728 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_char);
731 $this->ar_like[] = $like_statement;
732 if ($this->ar_caching === TRUE)
734 $this->ar_cache_like[] = $like_statement;
735 $this->ar_cache_exists[] = 'like';
742 // --------------------------------------------------------------------
751 function group_by($by)
755 $by = explode(',', $by);
758 foreach ($by as $val)
764 $this->ar_groupby[] = $this->_protect_identifiers($val);
766 if ($this->ar_caching === TRUE)
768 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
769 $this->ar_cache_exists[] = 'groupby';
776 // --------------------------------------------------------------------
779 * groupby() is an alias of group_by()
780 * this function is here for backwards compatibility, as
781 * groupby() has been deprecated
783 function groupby($by)
785 return $this->group_by($by);
788 // --------------------------------------------------------------------
791 * Sets the HAVING value
793 * Separates multiple calls with AND
800 function having($key, $value = '', $escape = TRUE)
802 return $this->_having($key, $value, 'AND ', $escape);
805 // --------------------------------------------------------------------
808 * orhaving() is an alias of or_having()
809 * this function is here for backwards compatibility, as
810 * orhaving() has been deprecated
813 function orhaving($key, $value = '', $escape = TRUE)
815 return $this->or_having($key, $value, $escape);
817 // --------------------------------------------------------------------
820 * Sets the OR HAVING value
822 * Separates multiple calls with OR
829 function or_having($key, $value = '', $escape = TRUE)
831 return $this->_having($key, $value, 'OR ', $escape);
834 // --------------------------------------------------------------------
837 * Sets the HAVING values
839 * Called by having() or or_having()
846 function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
848 if ( ! is_array($key))
850 $key = array($key => $value);
853 foreach ($key as $k => $v)
855 $prefix = (count($this->ar_having) == 0) ? '' : $type;
857 if ($escape === TRUE)
859 $k = $this->_protect_identifiers($k);
862 if ( ! $this->_has_operator($k))
869 $v = ' '.$this->escape_str($v);
872 $this->ar_having[] = $prefix.$k.$v;
873 if ($this->ar_caching === TRUE)
875 $this->ar_cache_having[] = $prefix.$k.$v;
876 $this->ar_cache_exists[] = 'having';
883 // --------------------------------------------------------------------
886 * Sets the ORDER BY value
890 * @param string direction: asc or desc
893 function order_by($orderby, $direction = '')
895 if (strtolower($direction) == 'random')
897 $orderby = ''; // Random results want or don't need a field name
898 $direction = $this->_random_keyword;
900 elseif (trim($direction) != '')
902 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
906 if (strpos($orderby, ',') !== FALSE)
909 foreach (explode(',', $orderby) as $part)
912 if ( ! in_array($part, $this->ar_aliased_tables))
914 $part = $this->_protect_identifiers(trim($part));
920 $orderby = implode(', ', $temp);
922 else if ($direction != $this->_random_keyword)
924 $orderby = $this->_protect_identifiers($orderby);
927 $orderby_statement = $orderby.$direction;
929 $this->ar_orderby[] = $orderby_statement;
930 if ($this->ar_caching === TRUE)
932 $this->ar_cache_orderby[] = $orderby_statement;
933 $this->ar_cache_exists[] = 'orderby';
939 // --------------------------------------------------------------------
942 * orderby() is an alias of order_by()
943 * this function is here for backwards compatibility, as
944 * orderby() has been deprecated
946 function orderby($orderby, $direction = '')
948 return $this->order_by($orderby, $direction);
951 // --------------------------------------------------------------------
954 * Sets the LIMIT value
957 * @param integer the limit value
958 * @param integer the offset value
961 function limit($value, $offset = '')
963 $this->ar_limit = $value;
967 $this->ar_offset = $offset;
973 // --------------------------------------------------------------------
976 * Sets the OFFSET value
979 * @param integer the offset value
982 function offset($offset)
984 $this->ar_offset = $offset;
988 // --------------------------------------------------------------------
991 * The "set" function. Allows key/value pairs to be set for inserting or updating
999 function set($key, $value = '', $escape = TRUE)
1001 $key = $this->_object_to_array($key);
1003 if ( ! is_array($key))
1005 $key = array($key => $value);
1008 foreach ($key as $k => $v)
1010 if ($escape === FALSE)
1012 $this->ar_set[$this->_protect_identifiers($k)] = $v;
1016 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
1023 // --------------------------------------------------------------------
1028 * Compiles the select statement based on the other functions called
1029 * and runs the query
1032 * @param string the table
1033 * @param string the limit clause
1034 * @param string the offset clause
1037 function get($table = '', $limit = null, $offset = null)
1041 $this->_track_aliases($table);
1042 $this->from($table);
1045 if ( ! is_null($limit))
1047 $this->limit($limit, $offset);
1050 $sql = $this->_compile_select();
1052 $result = $this->query($sql);
1053 $this->_reset_select();
1058 * "Count All Results" query
1060 * Generates a platform-specific query string that counts all records
1061 * returned by an Active Record query.
1067 function count_all_results($table = '')
1071 $this->_track_aliases($table);
1072 $this->from($table);
1075 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
1077 $query = $this->query($sql);
1078 $this->_reset_select();
1080 if ($query->num_rows() == 0)
1085 $row = $query->row();
1086 return $row->numrows;
1089 // --------------------------------------------------------------------
1094 * Allows the where clause, limit and offset to be added directly
1097 * @param string the where clause
1098 * @param string the limit clause
1099 * @param string the offset clause
1102 function get_where($table = '', $where = null, $limit = null, $offset = null)
1106 $this->from($table);
1109 if ( ! is_null($where))
1111 $this->where($where);
1114 if ( ! is_null($limit))
1116 $this->limit($limit, $offset);
1119 $sql = $this->_compile_select();
1121 $result = $this->query($sql);
1122 $this->_reset_select();
1126 // --------------------------------------------------------------------
1129 * getwhere() is an alias of get_where()
1130 * this function is here for backwards compatibility, as
1131 * getwhere() has been deprecated
1133 function getwhere($table = '', $where = null, $limit = null, $offset = null)
1135 return $this->get_where($table, $where, $limit, $offset);
1138 // --------------------------------------------------------------------
1143 * Compiles an insert string and runs the query
1146 * @param string the table to retrieve the results from
1147 * @param array an associative array of insert values
1150 function insert($table = '', $set = NULL)
1152 if ( ! is_null($set))
1157 if (count($this->ar_set) == 0)
1159 if ($this->db_debug)
1161 return $this->display_error('db_must_use_set');
1168 if ( ! isset($this->ar_from[0]))
1170 if ($this->db_debug)
1172 return $this->display_error('db_must_set_table');
1177 $table = $this->ar_from[0];
1180 $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
1182 $this->_reset_write();
1183 return $this->query($sql);
1186 // --------------------------------------------------------------------
1191 * Compiles an update string and runs the query
1194 * @param string the table to retrieve the results from
1195 * @param array an associative array of update values
1196 * @param mixed the where clause
1199 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
1201 // Combine any cached components with the current statements
1202 $this->_merge_cache();
1204 if ( ! is_null($set))
1209 if (count($this->ar_set) == 0)
1211 if ($this->db_debug)
1213 return $this->display_error('db_must_use_set');
1220 if ( ! isset($this->ar_from[0]))
1222 if ($this->db_debug)
1224 return $this->display_error('db_must_set_table');
1229 $table = $this->ar_from[0];
1234 $this->where($where);
1239 $this->limit($limit);
1242 $sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
1244 $this->_reset_write();
1245 return $this->query($sql);
1248 // --------------------------------------------------------------------
1253 * Compiles a delete string and runs "DELETE FROM table"
1256 * @param string the table to empty
1259 function empty_table($table = '')
1263 if ( ! isset($this->ar_from[0]))
1265 if ($this->db_debug)
1267 return $this->display_error('db_must_set_table');
1272 $table = $this->ar_from[0];
1276 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1279 $sql = $this->_delete($table);
1281 $this->_reset_write();
1283 return $this->query($sql);
1286 // --------------------------------------------------------------------
1291 * Compiles a truncate string and runs the query
1292 * If the database does not support the truncate() command
1293 * This function maps to "DELETE FROM table"
1296 * @param string the table to truncate
1299 function truncate($table = '')
1303 if ( ! isset($this->ar_from[0]))
1305 if ($this->db_debug)
1307 return $this->display_error('db_must_set_table');
1312 $table = $this->ar_from[0];
1316 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1319 $sql = $this->_truncate($table);
1321 $this->_reset_write();
1323 return $this->query($sql);
1326 // --------------------------------------------------------------------
1331 * Compiles a delete string and runs the query
1334 * @param mixed the table(s) to delete from. String or array
1335 * @param mixed the where clause
1336 * @param mixed the limit clause
1340 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
1342 // Combine any cached components with the current statements
1343 $this->_merge_cache();
1347 if ( ! isset($this->ar_from[0]))
1349 if ($this->db_debug)
1351 return $this->display_error('db_must_set_table');
1356 $table = $this->ar_from[0];
1358 elseif (is_array($table))
1360 foreach($table as $single_table)
1362 $this->delete($single_table, $where, $limit, FALSE);
1365 $this->_reset_write();
1370 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1375 $this->where($where);
1380 $this->limit($limit);
1383 if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0)
1385 if ($this->db_debug)
1387 return $this->display_error('db_del_must_use_where');
1393 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
1397 $this->_reset_write();
1400 return $this->query($sql);
1403 // --------------------------------------------------------------------
1408 * Prepends a database prefix if one exists in configuration
1411 * @param string the table
1414 function dbprefix($table = '')
1418 $this->display_error('db_table_name_required');
1421 return $this->dbprefix.$table;
1424 // --------------------------------------------------------------------
1429 * Used to track SQL statements written with aliased tables.
1432 * @param string The table to inspect
1435 function _track_aliases($table)
1437 if (is_array($table))
1439 foreach ($table as $t)
1441 $this->_track_aliases($t);
1446 // Does the string contain a comma? If so, we need to separate
1447 // the string into discreet statements
1448 if (strpos($table, ',') !== FALSE)
1450 return $this->_track_aliases(explode(',', $table));
1453 // if a table alias is used we can recognize it by a space
1454 if (strpos($table, " ") !== FALSE)
1456 // if the alias is written with the AS keyword, remove it
1457 $table = preg_replace('/ AS /i', ' ', $table);
1460 $table = trim(strrchr($table, " "));
1462 // Store the alias, if it doesn't already exist
1463 if ( ! in_array($table, $this->ar_aliased_tables))
1465 $this->ar_aliased_tables[] = $table;
1470 // --------------------------------------------------------------------
1473 * Compile the SELECT statement
1475 * Generates a query string based on which functions were used.
1476 * Should not be called directly. The get() function calls it.
1481 function _compile_select($select_override = FALSE)
1483 // Combine any cached components with the current statements
1484 $this->_merge_cache();
1486 // ----------------------------------------------------------------
1488 // Write the "select" portion of the query
1490 if ($select_override !== FALSE)
1492 $sql = $select_override;
1496 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
1498 if (count($this->ar_select) == 0)
1504 // Cycle through the "select" portion of the query and prep each column name.
1505 // The reason we protect identifiers here rather then in the select() function
1506 // is because until the user calls the from() function we don't know if there are aliases
1507 foreach ($this->ar_select as $key => $val)
1509 $this->ar_select[$key] = $this->_protect_identifiers($val);
1512 $sql .= implode(', ', $this->ar_select);
1516 // ----------------------------------------------------------------
1518 // Write the "FROM" portion of the query
1520 if (count($this->ar_from) > 0)
1524 $sql .= $this->_from_tables($this->ar_from);
1527 // ----------------------------------------------------------------
1529 // Write the "JOIN" portion of the query
1531 if (count($this->ar_join) > 0)
1535 $sql .= implode("\n", $this->ar_join);
1538 // ----------------------------------------------------------------
1540 // Write the "WHERE" portion of the query
1542 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1549 $sql .= implode("\n", $this->ar_where);
1551 // ----------------------------------------------------------------
1553 // Write the "LIKE" portion of the query
1555 if (count($this->ar_like) > 0)
1557 if (count($this->ar_where) > 0)
1562 $sql .= implode("\n", $this->ar_like);
1565 // ----------------------------------------------------------------
1567 // Write the "GROUP BY" portion of the query
1569 if (count($this->ar_groupby) > 0)
1571 $sql .= "\nGROUP BY ";
1573 $sql .= implode(', ', $this->ar_groupby);
1576 // ----------------------------------------------------------------
1578 // Write the "HAVING" portion of the query
1580 if (count($this->ar_having) > 0)
1582 $sql .= "\nHAVING ";
1583 $sql .= implode("\n", $this->ar_having);
1586 // ----------------------------------------------------------------
1588 // Write the "ORDER BY" portion of the query
1590 if (count($this->ar_orderby) > 0)
1592 $sql .= "\nORDER BY ";
1593 $sql .= implode(', ', $this->ar_orderby);
1595 if ($this->ar_order !== FALSE)
1597 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1601 // ----------------------------------------------------------------
1603 // Write the "LIMIT" portion of the query
1605 if (is_numeric($this->ar_limit))
1608 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1614 // --------------------------------------------------------------------
1619 * Takes an object as input and converts the class variables to array key/vals
1625 function _object_to_array($object)
1627 if ( ! is_object($object))
1633 foreach (get_object_vars($object) as $key => $val)
1635 // There are some built in keys we need to ignore for this conversion
1636 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
1638 $array[$key] = $val;
1645 // --------------------------------------------------------------------
1655 function start_cache()
1657 $this->ar_caching = TRUE;
1660 // --------------------------------------------------------------------
1670 function stop_cache()
1672 $this->ar_caching = FALSE;
1675 // --------------------------------------------------------------------
1680 * Empties the AR cache
1685 function flush_cache()
1689 'ar_cache_select' => array(),
1690 'ar_cache_from' => array(),
1691 'ar_cache_join' => array(),
1692 'ar_cache_where' => array(),
1693 'ar_cache_like' => array(),
1694 'ar_cache_groupby' => array(),
1695 'ar_cache_having' => array(),
1696 'ar_cache_orderby' => array(),
1697 'ar_cache_set' => array(),
1698 'ar_cache_exists' => array()
1703 // --------------------------------------------------------------------
1708 * When called, this function merges any cached AR arrays with
1709 * locally called ones.
1714 function _merge_cache()
1716 if (count($this->ar_cache_exists) == 0)
1721 foreach ($this->ar_cache_exists as $val)
1723 $ar_variable = 'ar_'.$val;
1724 $ar_cache_var = 'ar_cache_'.$val;
1726 if (count($this->$ar_cache_var) == 0)
1731 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
1734 // If we are "protecting identifiers" we need to examine the "from"
1735 // portion of the query to determine if there are any aliases
1736 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
1738 $this->_track_aliases($this->ar_from);
1742 // --------------------------------------------------------------------
1745 * Resets the active record values. Called by the get() function
1748 * @param array An array of fields to reset
1751 function _reset_run($ar_reset_items)
1753 foreach ($ar_reset_items as $item => $default_value)
1755 if ( ! in_array($item, $this->ar_store_array))
1757 $this->$item = $default_value;
1762 // --------------------------------------------------------------------
1765 * Resets the active record values. Called by the get() function
1770 function _reset_select()
1772 $ar_reset_items = array(
1773 'ar_select' => array(),
1774 'ar_from' => array(),
1775 'ar_join' => array(),
1776 'ar_where' => array(),
1777 'ar_like' => array(),
1778 'ar_groupby' => array(),
1779 'ar_having' => array(),
1780 'ar_orderby' => array(),
1781 'ar_wherein' => array(),
1782 'ar_aliased_tables' => array(),
1783 'ar_distinct' => FALSE,
1784 'ar_limit' => FALSE,
1785 'ar_offset' => FALSE,
1786 'ar_order' => FALSE,
1789 $this->_reset_run($ar_reset_items);
1792 // --------------------------------------------------------------------
1795 * Resets the active record "write" values.
1797 * Called by the insert() update() and delete() functions
1802 function _reset_write()
1804 $ar_reset_items = array(
1805 'ar_set' => array(),
1806 'ar_from' => array(),
1807 'ar_where' => array(),
1808 'ar_like' => array(),
1809 'ar_orderby' => array(),
1810 'ar_limit' => FALSE,
1814 $this->_reset_run($ar_reset_items);
1819 /* End of file DB_active_rec.php */
1820 /* Location: ./system/database/DB_active_rec.php */