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, 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 // Extract any aliases that might exist. We use this information
271 // in the _protect_identifiers to know whether to add a table prefix
272 $this->_track_aliases($val);
274 $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
276 if ($this->ar_caching === TRUE)
278 $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
279 $this->ar_cache_exists[] = 'from';
286 // --------------------------------------------------------------------
291 * Generates the JOIN portion of the query
295 * @param string the join condition
296 * @param string the type of join
299 function join($table, $cond, $type = '')
303 $type = strtoupper(trim($type));
305 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
315 // Extract any aliases that might exist. We use this information
316 // in the _protect_identifiers to know whether to add a table prefix
317 $this->_track_aliases($table);
319 // Strip apart the condition and protect the identifiers
320 if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
322 $match[1] = $this->_protect_identifiers($match[1]);
323 $match[3] = $this->_protect_identifiers($match[3]);
325 $cond = $match[1].$match[2].$match[3];
328 // Assemble the JOIN statement
329 $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
331 $this->ar_join[] = $join;
332 if ($this->ar_caching === TRUE)
334 $this->ar_cache_join[] = $join;
335 $this->ar_cache_exists[] = 'join';
341 // --------------------------------------------------------------------
346 * Generates the WHERE portion of the query. Separates
347 * multiple calls with AND
354 function where($key, $value = NULL, $escape = TRUE)
356 return $this->_where($key, $value, 'AND ', $escape);
359 // --------------------------------------------------------------------
364 * Generates the WHERE portion of the query. Separates
365 * multiple calls with OR
372 function or_where($key, $value = NULL, $escape = TRUE)
374 return $this->_where($key, $value, 'OR ', $escape);
377 // --------------------------------------------------------------------
380 * orwhere() is an alias of or_where()
381 * this function is here for backwards compatibility, as
382 * orwhere() has been deprecated
384 function orwhere($key, $value = NULL, $escape = TRUE)
386 return $this->or_where($key, $value, $escape);
389 // --------------------------------------------------------------------
394 * Called by where() or orwhere()
402 function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
404 if ( ! is_array($key))
406 $key = array($key => $value);
409 // If the escape value was not set will will base it on the global setting
410 if ( ! is_bool($escape))
412 $escape = $this->_protect_identifiers;
415 foreach ($key as $k => $v)
417 $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type;
419 if (is_null($v) && ! $this->_has_operator($k))
421 // value appears not to have been set, assign the test to IS NULL
427 if ($escape === TRUE)
429 $k = $this->_protect_identifiers($k, FALSE, $escape);
431 $v = ' '.$this->escape($v);
434 if ( ! $this->_has_operator($k))
441 $k = $this->_protect_identifiers($k, FALSE, $escape);
444 $this->ar_where[] = $prefix.$k.$v;
446 if ($this->ar_caching === TRUE)
448 $this->ar_cache_where[] = $prefix.$k.$v;
449 $this->ar_cache_exists[] = 'where';
457 // --------------------------------------------------------------------
462 * Generates a WHERE field IN ('item', 'item') SQL query joined with
466 * @param string The field to search
467 * @param array The values searched on
470 function where_in($key = NULL, $values = NULL)
472 return $this->_where_in($key, $values);
475 // --------------------------------------------------------------------
480 * Generates a WHERE field IN ('item', 'item') SQL query joined with
484 * @param string The field to search
485 * @param array The values searched on
488 function or_where_in($key = NULL, $values = NULL)
490 return $this->_where_in($key, $values, FALSE, 'OR ');
493 // --------------------------------------------------------------------
498 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
499 * with AND if appropriate
502 * @param string The field to search
503 * @param array The values searched on
506 function where_not_in($key = NULL, $values = NULL)
508 return $this->_where_in($key, $values, TRUE);
511 // --------------------------------------------------------------------
516 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
517 * with OR if appropriate
520 * @param string The field to search
521 * @param array The values searched on
524 function or_where_not_in($key = NULL, $values = NULL)
526 return $this->_where_in($key, $values, TRUE, 'OR ');
529 // --------------------------------------------------------------------
534 * Called by where_in, where_in_or, where_not_in, where_not_in_or
537 * @param string The field to search
538 * @param array The values searched on
539 * @param boolean If the statement would be IN or NOT IN
543 function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
545 if ($key === NULL OR $values === NULL)
550 if ( ! is_array($values))
552 $values = array($values);
555 $not = ($not) ? ' NOT' : '';
557 foreach ($values as $value)
559 $this->ar_wherein[] = $this->escape($value);
562 $prefix = (count($this->ar_where) == 0) ? '' : $type;
564 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
566 $this->ar_where[] = $where_in;
567 if ($this->ar_caching === TRUE)
569 $this->ar_cache_where[] = $where_in;
570 $this->ar_cache_exists[] = 'where';
573 // reset the array for multiple calls
574 $this->ar_wherein = array();
578 // --------------------------------------------------------------------
583 * Generates a %LIKE% portion of the query. Separates
584 * multiple calls with AND
591 function like($field, $match = '', $side = 'both')
593 return $this->_like($field, $match, 'AND ', $side);
596 // --------------------------------------------------------------------
601 * Generates a NOT LIKE portion of the query. Separates
602 * multiple calls with AND
609 function not_like($field, $match = '', $side = 'both')
611 return $this->_like($field, $match, 'AND ', $side, 'NOT');
614 // --------------------------------------------------------------------
619 * Generates a %LIKE% portion of the query. Separates
620 * multiple calls with OR
627 function or_like($field, $match = '', $side = 'both')
629 return $this->_like($field, $match, 'OR ', $side);
632 // --------------------------------------------------------------------
637 * Generates a NOT LIKE portion of the query. Separates
638 * multiple calls with OR
645 function or_not_like($field, $match = '', $side = 'both')
647 return $this->_like($field, $match, 'OR ', $side, 'NOT');
650 // --------------------------------------------------------------------
653 * orlike() is an alias of or_like()
654 * this function is here for backwards compatibility, as
655 * orlike() has been deprecated
657 function orlike($field, $match = '', $side = 'both')
659 return $this->or_like($field, $match, $side);
662 // --------------------------------------------------------------------
667 * Called by like() or orlike()
675 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
677 if ( ! is_array($field))
679 $field = array($field => $match);
682 foreach ($field as $k => $v)
684 $k = $this->_protect_identifiers($k);
686 $prefix = (count($this->ar_like) == 0) ? '' : $type;
688 $v = $this->escape_str($v);
690 if ($side == 'before')
692 $like_statement = $prefix." $k $not LIKE '%{$v}'";
694 elseif ($side == 'after')
696 $like_statement = $prefix." $k $not LIKE '{$v}%'";
700 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
703 $this->ar_like[] = $like_statement;
704 if ($this->ar_caching === TRUE)
706 $this->ar_cache_like[] = $like_statement;
707 $this->ar_cache_exists[] = 'like';
714 // --------------------------------------------------------------------
723 function group_by($by)
727 $by = explode(',', $by);
730 foreach ($by as $val)
736 $this->ar_groupby[] = $this->_protect_identifiers($val);
738 if ($this->ar_caching === TRUE)
740 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
741 $this->ar_cache_exists[] = 'groupby';
748 // --------------------------------------------------------------------
751 * groupby() is an alias of group_by()
752 * this function is here for backwards compatibility, as
753 * groupby() has been deprecated
755 function groupby($by)
757 return $this->group_by($by);
760 // --------------------------------------------------------------------
763 * Sets the HAVING value
765 * Separates multiple calls with AND
772 function having($key, $value = '', $escape = TRUE)
774 return $this->_having($key, $value, 'AND ', $escape);
777 // --------------------------------------------------------------------
780 * orhaving() is an alias of or_having()
781 * this function is here for backwards compatibility, as
782 * orhaving() has been deprecated
785 function orhaving($key, $value = '', $escape = TRUE)
787 return $this->or_having($key, $value, $escape);
789 // --------------------------------------------------------------------
792 * Sets the OR HAVING value
794 * Separates multiple calls with OR
801 function or_having($key, $value = '', $escape = TRUE)
803 return $this->_having($key, $value, 'OR ', $escape);
806 // --------------------------------------------------------------------
809 * Sets the HAVING values
811 * Called by having() or or_having()
818 function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
820 if ( ! is_array($key))
822 $key = array($key => $value);
825 foreach ($key as $k => $v)
827 $prefix = (count($this->ar_having) == 0) ? '' : $type;
829 if ($escape === TRUE)
831 $k = $this->_protect_identifiers($k);
834 if ( ! $this->_has_operator($k))
841 $v = ' '.$this->escape_str($v);
844 $this->ar_having[] = $prefix.$k.$v;
845 if ($this->ar_caching === TRUE)
847 $this->ar_cache_having[] = $prefix.$k.$v;
848 $this->ar_cache_exists[] = 'having';
855 // --------------------------------------------------------------------
858 * Sets the ORDER BY value
862 * @param string direction: asc or desc
865 function order_by($orderby, $direction = '')
867 if (strtolower($direction) == 'random')
869 $orderby = ''; // Random results want or don't need a field name
870 $direction = $this->_random_keyword;
872 elseif (trim($direction) != '')
874 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
877 $orderby_statement = $this->_protect_identifiers($orderby).$direction;
879 $this->ar_orderby[] = $orderby_statement;
880 if ($this->ar_caching === TRUE)
882 $this->ar_cache_orderby[] = $orderby_statement;
883 $this->ar_cache_exists[] = 'orderby';
889 // --------------------------------------------------------------------
892 * orderby() is an alias of order_by()
893 * this function is here for backwards compatibility, as
894 * orderby() has been deprecated
896 function orderby($orderby, $direction = '')
898 return $this->order_by($orderby, $direction);
901 // --------------------------------------------------------------------
904 * Sets the LIMIT value
907 * @param integer the limit value
908 * @param integer the offset value
911 function limit($value, $offset = '')
913 $this->ar_limit = $value;
917 $this->ar_offset = $offset;
923 // --------------------------------------------------------------------
926 * Sets the OFFSET value
929 * @param integer the offset value
932 function offset($offset)
934 $this->ar_offset = $offset;
938 // --------------------------------------------------------------------
941 * The "set" function. Allows key/value pairs to be set for inserting or updating
949 function set($key, $value = '', $escape = TRUE)
951 $key = $this->_object_to_array($key);
953 if ( ! is_array($key))
955 $key = array($key => $value);
958 foreach ($key as $k => $v)
960 if ($escape === FALSE)
962 $this->ar_set[$this->_protect_identifiers($k)] = $v;
966 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
973 // --------------------------------------------------------------------
978 * Compiles the select statement based on the other functions called
982 * @param string the table
983 * @param string the limit clause
984 * @param string the offset clause
987 function get($table = '', $limit = null, $offset = null)
991 $this->_track_aliases($table);
995 if ( ! is_null($limit))
997 $this->limit($limit, $offset);
1000 $sql = $this->_compile_select();
1002 $result = $this->query($sql);
1003 $this->_reset_select();
1008 * "Count All Results" query
1010 * Generates a platform-specific query string that counts all records
1011 * returned by an Active Record query.
1017 function count_all_results($table = '')
1021 $this->_track_aliases($table);
1022 $this->from($table);
1025 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
1027 $query = $this->query($sql);
1028 $this->_reset_select();
1030 if ($query->num_rows() == 0)
1035 $row = $query->row();
1036 return $row->numrows;
1039 // --------------------------------------------------------------------
1044 * Allows the where clause, limit and offset to be added directly
1047 * @param string the where clause
1048 * @param string the limit clause
1049 * @param string the offset clause
1052 function get_where($table = '', $where = null, $limit = null, $offset = null)
1056 $this->from($table);
1059 if ( ! is_null($where))
1061 $this->where($where);
1064 if ( ! is_null($limit))
1066 $this->limit($limit, $offset);
1069 $sql = $this->_compile_select();
1071 $result = $this->query($sql);
1072 $this->_reset_select();
1076 // --------------------------------------------------------------------
1079 * getwhere() is an alias of get_where()
1080 * this function is here for backwards compatibility, as
1081 * getwhere() has been deprecated
1083 function getwhere($table = '', $where = null, $limit = null, $offset = null)
1085 return $this->get_where($table, $where, $limit, $offset);
1088 // --------------------------------------------------------------------
1093 * Compiles an insert string and runs the query
1096 * @param string the table to retrieve the results from
1097 * @param array an associative array of insert values
1100 function insert($table = '', $set = NULL)
1102 if ( ! is_null($set))
1107 if (count($this->ar_set) == 0)
1109 if ($this->db_debug)
1111 return $this->display_error('db_must_use_set');
1118 if ( ! isset($this->ar_from[0]))
1120 if ($this->db_debug)
1122 return $this->display_error('db_must_set_table');
1127 $table = $this->ar_from[0];
1130 $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
1132 $this->_reset_write();
1133 return $this->query($sql);
1136 // --------------------------------------------------------------------
1141 * Compiles an update string and runs the query
1144 * @param string the table to retrieve the results from
1145 * @param array an associative array of update values
1146 * @param mixed the where clause
1149 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
1151 // Combine any cached components with the current statements
1152 $this->_merge_cache();
1154 if ( ! is_null($set))
1159 if (count($this->ar_set) == 0)
1161 if ($this->db_debug)
1163 return $this->display_error('db_must_use_set');
1170 if ( ! isset($this->ar_from[0]))
1172 if ($this->db_debug)
1174 return $this->display_error('db_must_set_table');
1179 $table = $this->ar_from[0];
1184 $this->where($where);
1189 $this->limit($limit);
1192 $sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
1194 $this->_reset_write();
1195 return $this->query($sql);
1198 // --------------------------------------------------------------------
1203 * Compiles a delete string and runs "DELETE FROM table"
1206 * @param string the table to empty
1209 function empty_table($table = '')
1213 if ( ! isset($this->ar_from[0]))
1215 if ($this->db_debug)
1217 return $this->display_error('db_must_set_table');
1222 $table = $this->ar_from[0];
1226 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1229 $sql = $this->_delete($table);
1231 $this->_reset_write();
1233 return $this->query($sql);
1236 // --------------------------------------------------------------------
1241 * Compiles a truncate string and runs the query
1242 * If the database does not support the truncate() command
1243 * This function maps to "DELETE FROM table"
1246 * @param string the table to truncate
1249 function truncate($table = '')
1253 if ( ! isset($this->ar_from[0]))
1255 if ($this->db_debug)
1257 return $this->display_error('db_must_set_table');
1262 $table = $this->ar_from[0];
1266 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1269 $sql = $this->_truncate($table);
1271 $this->_reset_write();
1273 return $this->query($sql);
1276 // --------------------------------------------------------------------
1281 * Compiles a delete string and runs the query
1284 * @param mixed the table(s) to delete from. String or array
1285 * @param mixed the where clause
1286 * @param mixed the limit clause
1290 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
1292 // Combine any cached components with the current statements
1293 $this->_merge_cache();
1297 if ( ! isset($this->ar_from[0]))
1299 if ($this->db_debug)
1301 return $this->display_error('db_must_set_table');
1306 $table = $this->ar_from[0];
1308 elseif (is_array($table))
1310 foreach($table as $single_table)
1312 $this->delete($single_table, $where, $limit, FALSE);
1315 $this->_reset_write();
1320 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1325 $this->where($where);
1330 $this->limit($limit);
1333 if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
1335 if ($this->db_debug)
1337 return $this->display_error('db_del_must_use_where');
1343 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
1347 $this->_reset_write();
1350 return $this->query($sql);
1353 // --------------------------------------------------------------------
1358 * Prepends a database prefix if one exists in configuration
1361 * @param string the table
1364 function dbprefix($table = '')
1368 $this->display_error('db_table_name_required');
1371 return $this->dbprefix.$table;
1374 // --------------------------------------------------------------------
1379 * Used to track SQL statements written with aliased tables.
1382 * @param string The table to inspect
1385 function _track_aliases($table)
1387 if (is_array($table))
1389 foreach ($table as $t)
1391 $this->_track_aliases($t);
1396 // Does the string contain a comma? If so, we need to separate
1397 // the string into discreet statements
1398 if (strpos($table, ',') !== FALSE)
1400 return $this->_track_aliases(explode(',', $table));
1403 // if a table alias is used we can recognize it by a space
1404 if (strpos($table, " ") !== FALSE)
1406 // if the alias is written with the AS keyword, remove it
1407 $table = preg_replace('/ AS /i', ' ', $table);
1410 $table = trim(strrchr($table, " "));
1412 // Store the alias, if it doesn't already exist
1413 if ( ! in_array($table, $this->ar_aliased_tables))
1415 $this->ar_aliased_tables[] = $table;
1420 // --------------------------------------------------------------------
1423 * Compile the SELECT statement
1425 * Generates a query string based on which functions were used.
1426 * Should not be called directly. The get() function calls it.
1431 function _compile_select($select_override = FALSE)
1433 // Combine any cached components with the current statements
1434 $this->_merge_cache();
1436 // ----------------------------------------------------------------
1438 // Write the "select" portion of the query
1440 if ($select_override !== FALSE)
1442 $sql = $select_override;
1446 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
1448 if (count($this->ar_select) == 0)
1454 // Cycle through the "select" portion of the query and prep each column name.
1455 // The reason we protect identifiers here rather then in the select() function
1456 // is because until the user calls the from() function we don't know if there are aliases
1457 foreach ($this->ar_select as $key => $val)
1459 $this->ar_select[$key] = $this->_protect_identifiers($val);
1462 $sql .= implode(', ', $this->ar_select);
1466 // ----------------------------------------------------------------
1468 // Write the "FROM" portion of the query
1470 if (count($this->ar_from) > 0)
1474 $sql .= $this->_from_tables($this->ar_from);
1477 // ----------------------------------------------------------------
1479 // Write the "JOIN" portion of the query
1481 if (count($this->ar_join) > 0)
1485 $sql .= implode("\n", $this->ar_join);
1488 // ----------------------------------------------------------------
1490 // Write the "WHERE" portion of the query
1492 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1499 $sql .= implode("\n", $this->ar_where);
1501 // ----------------------------------------------------------------
1503 // Write the "LIKE" portion of the query
1505 if (count($this->ar_like) > 0)
1507 if (count($this->ar_where) > 0)
1512 $sql .= implode("\n", $this->ar_like);
1515 // ----------------------------------------------------------------
1517 // Write the "GROUP BY" portion of the query
1519 if (count($this->ar_groupby) > 0)
1521 $sql .= "\nGROUP BY ";
1523 $sql .= implode(', ', $this->ar_groupby);
1526 // ----------------------------------------------------------------
1528 // Write the "HAVING" portion of the query
1530 if (count($this->ar_having) > 0)
1532 $sql .= "\nHAVING ";
1533 $sql .= implode("\n", $this->ar_having);
1536 // ----------------------------------------------------------------
1538 // Write the "ORDER BY" portion of the query
1540 if (count($this->ar_orderby) > 0)
1542 $sql .= "\nORDER BY ";
1543 $sql .= implode(', ', $this->ar_orderby);
1545 if ($this->ar_order !== FALSE)
1547 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1551 // ----------------------------------------------------------------
1553 // Write the "LIMIT" portion of the query
1555 if (is_numeric($this->ar_limit))
1558 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1564 // --------------------------------------------------------------------
1569 * Takes an object as input and converts the class variables to array key/vals
1575 function _object_to_array($object)
1577 if ( ! is_object($object))
1583 foreach (get_object_vars($object) as $key => $val)
1585 // There are some built in keys we need to ignore for this conversion
1586 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
1588 $array[$key] = $val;
1595 // --------------------------------------------------------------------
1605 function start_cache()
1607 $this->ar_caching = TRUE;
1610 // --------------------------------------------------------------------
1620 function stop_cache()
1622 $this->ar_caching = FALSE;
1625 // --------------------------------------------------------------------
1630 * Empties the AR cache
1635 function flush_cache()
1639 'ar_cache_select' => array(),
1640 'ar_cache_from' => array(),
1641 'ar_cache_join' => array(),
1642 'ar_cache_where' => array(),
1643 'ar_cache_like' => array(),
1644 'ar_cache_groupby' => array(),
1645 'ar_cache_having' => array(),
1646 'ar_cache_orderby' => array(),
1647 'ar_cache_set' => array(),
1648 'ar_cache_exists' => array()
1653 // --------------------------------------------------------------------
1658 * When called, this function merges any cached AR arrays with
1659 * locally called ones.
1664 function _merge_cache()
1666 if (count($this->ar_cache_exists) == 0)
1671 foreach ($this->ar_cache_exists as $val)
1673 $ar_variable = 'ar_'.$val;
1674 $ar_cache_var = 'ar_cache_'.$val;
1676 if (count($this->$ar_cache_var) == 0)
1681 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
1684 // If we are "protecting identifiers" we need to examine the "from"
1685 // portion of the query to determine if there are any aliases
1686 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
1688 $this->_track_aliases($this->ar_from);
1692 // --------------------------------------------------------------------
1695 * Resets the active record values. Called by the get() function
1698 * @param array An array of fields to reset
1701 function _reset_run($ar_reset_items)
1703 foreach ($ar_reset_items as $item => $default_value)
1705 if ( ! in_array($item, $this->ar_store_array))
1707 $this->$item = $default_value;
1712 // --------------------------------------------------------------------
1715 * Resets the active record values. Called by the get() function
1720 function _reset_select()
1722 $ar_reset_items = array(
1723 'ar_select' => array(),
1724 'ar_from' => array(),
1725 'ar_join' => array(),
1726 'ar_where' => array(),
1727 'ar_like' => array(),
1728 'ar_groupby' => array(),
1729 'ar_having' => array(),
1730 'ar_orderby' => array(),
1731 'ar_wherein' => array(),
1732 'ar_aliased_tables' => array(),
1733 'ar_distinct' => FALSE,
1734 'ar_limit' => FALSE,
1735 'ar_offset' => FALSE,
1736 'ar_order' => FALSE,
1739 $this->_reset_run($ar_reset_items);
1742 // --------------------------------------------------------------------
1745 * Resets the active record "write" values.
1747 * Called by the insert() update() and delete() functions
1752 function _reset_write()
1754 $ar_reset_items = array(
1755 'ar_set' => array(),
1756 'ar_from' => array(),
1757 'ar_where' => array(),
1758 'ar_like' => array(),
1759 'ar_orderby' => array(),
1760 'ar_limit' => FALSE,
1764 $this->_reset_run($ar_reset_items);
1769 /* End of file DB_active_rec.php */
1770 /* Location: ./system/database/DB_active_rec.php */