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