blob: e3798254cfe01671f331cecc3b39ac2f63c0f4d7 [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard09de1852007-02-14 01:35:56 +00002/**
Derek Allardd2df9bc2007-04-15 17:41:17 +00003 * CodeIgniter
Derek Allard09de1852007-02-14 01:35:56 +00004 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Rick Ellis37b3ecf2008-09-12 23:34:18 +00009 * @copyright Copyright (c) 2008, EllisLab, Inc.
Derek Allardcdd2ab22008-01-23 00:05:38 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allard09de1852007-02-14 01:35:56 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Active Record Class
20 *
21 * This is the platform-independent base Active Record implementation class.
22 *
23 * @package CodeIgniter
24 * @subpackage Drivers
25 * @category Database
Derek Allard3d879d52008-01-18 19:41:32 +000026 * @author ExpressionEngine Dev Team
Derek Allardcdd2ab22008-01-23 00:05:38 +000027 * @link http://codeigniter.com/user_guide/database/
Derek Allard09de1852007-02-14 01:35:56 +000028 */
29class CI_DB_active_record extends CI_DB_driver {
30
Rick Ellis59523592008-10-17 04:07:40 +000031 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();
Derek Allard5e128942007-12-28 21:33:03 +000045 var $ar_aliased_tables = array();
Rick Ellis59523592008-10-17 04:07:40 +000046 var $ar_store_array = array();
47
Derek Allard9b3e7b52008-02-04 23:20:34 +000048 // Active Record Caching variables
Rick Ellis59523592008-10-17 04:07:40 +000049 var $ar_caching = FALSE;
Rick Ellis392f09d2008-10-18 02:03:14 +000050 var $ar_cache_exists = array();
Rick Ellis59523592008-10-17 04:07:40 +000051 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();
Rick Ellis59523592008-10-17 04:07:40 +000058 var $ar_cache_orderby = array();
59 var $ar_cache_set = array();
Derek Allard9b3e7b52008-02-04 23:20:34 +000060
Derek Allard09de1852007-02-14 01:35:56 +000061
Derek Allard3b118682008-01-22 23:44:32 +000062 // --------------------------------------------------------------------
63
64 /**
Derek Allard09de1852007-02-14 01:35:56 +000065 * Select
66 *
67 * Generates the SELECT portion of the query
68 *
69 * @access public
70 * @param string
71 * @return object
72 */
Rick Ellis59523592008-10-17 04:07:40 +000073 function select($select = '*', $escape = NULL)
Derek Allard09de1852007-02-14 01:35:56 +000074 {
Rick Ellis59523592008-10-17 04:07:40 +000075 // Set the global value if this was sepecified
76 if (is_bool($escape))
77 {
78 $this->_protect_identifiers = $escape;
79 }
80
Derek Allard09de1852007-02-14 01:35:56 +000081 if (is_string($select))
82 {
Rick Ellis59523592008-10-17 04:07:40 +000083 $select = explode(',', $select);
Derek Allard09de1852007-02-14 01:35:56 +000084 }
Derek Allard5fe155e2008-05-12 19:14:57 +000085
Derek Allard09de1852007-02-14 01:35:56 +000086 foreach ($select as $val)
87 {
88 $val = trim($val);
Derek Allard39b622d2008-01-16 21:10:09 +000089
Derek Allard09de1852007-02-14 01:35:56 +000090 if ($val != '')
Derek Allard39b622d2008-01-16 21:10:09 +000091 {
Derek Allard09de1852007-02-14 01:35:56 +000092 $this->ar_select[] = $val;
Rick Ellis59523592008-10-17 04:07:40 +000093
Derek Allard9b3e7b52008-02-04 23:20:34 +000094 if ($this->ar_caching === TRUE)
95 {
96 $this->ar_cache_select[] = $val;
Rick Ellis392f09d2008-10-18 02:03:14 +000097 $this->ar_cache_exists[] = 'select';
Derek Allard9b3e7b52008-02-04 23:20:34 +000098 }
Derek Allard39b622d2008-01-16 21:10:09 +000099 }
Derek Allard09de1852007-02-14 01:35:56 +0000100 }
101 return $this;
102 }
Derek Allard39b622d2008-01-16 21:10:09 +0000103
104 // --------------------------------------------------------------------
105
106 /**
107 * Select Max
108 *
109 * Generates a SELECT MAX(field) portion of a query
110 *
111 * @access public
112 * @param string the field
113 * @param string an alias
114 * @return object
115 */
Rick Ellis59523592008-10-17 04:07:40 +0000116 function select_max($select = '', $alias = '')
Derek Allard39b622d2008-01-16 21:10:09 +0000117 {
Rick Ellis59523592008-10-17 04:07:40 +0000118 return $this->_max_min_avg_sum($select, $alias, 'MAX');
Derek Allard39b622d2008-01-16 21:10:09 +0000119 }
Rick Ellis59523592008-10-17 04:07:40 +0000120
Derek Allard39b622d2008-01-16 21:10:09 +0000121 // --------------------------------------------------------------------
122
123 /**
124 * Select Min
125 *
126 * Generates a SELECT MIN(field) portion of a query
127 *
128 * @access public
129 * @param string the field
130 * @param string an alias
131 * @return object
132 */
Rick Ellis59523592008-10-17 04:07:40 +0000133 function select_min($select = '', $alias = '')
Derek Allard39b622d2008-01-16 21:10:09 +0000134 {
Rick Ellis59523592008-10-17 04:07:40 +0000135 return $this->_max_min_avg_sum($select, $alias, 'MIN');
Derek Allard39b622d2008-01-16 21:10:09 +0000136 }
137
138 // --------------------------------------------------------------------
139
140 /**
141 * Select Average
142 *
143 * Generates a SELECT AVG(field) portion of a query
144 *
145 * @access public
146 * @param string the field
147 * @param string an alias
148 * @return object
149 */
Rick Ellis59523592008-10-17 04:07:40 +0000150 function select_avg($select = '', $alias = '')
Derek Allard39b622d2008-01-16 21:10:09 +0000151 {
Rick Ellis59523592008-10-17 04:07:40 +0000152 return $this->_max_min_avg_sum($select, $alias, 'AVG');
Derek Allard39b622d2008-01-16 21:10:09 +0000153 }
154
155 // --------------------------------------------------------------------
Derek Allard5fe155e2008-05-12 19:14:57 +0000156
Derek Allard39b622d2008-01-16 21:10:09 +0000157 /**
158 * Select Sum
159 *
160 * Generates a SELECT SUM(field) portion of a query
161 *
162 * @access public
163 * @param string the field
164 * @param string an alias
165 * @return object
166 */
Rick Ellis59523592008-10-17 04:07:40 +0000167 function select_sum($select = '', $alias = '')
168 {
169 return $this->_max_min_avg_sum($select, $alias, 'SUM');
170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Processing Function for the four functions above:
176 *
177 * select_max()
178 * select_min()
179 * select_avg()
180 * select_sum()
181 *
182 * @access public
183 * @param string the field
184 * @param string an alias
185 * @return object
186 */
187 function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard39b622d2008-01-16 21:10:09 +0000188 {
Derek Jones0b59f272008-05-13 04:22:33 +0000189 if ( ! is_string($select) OR $select == '')
Derek Allard39b622d2008-01-16 21:10:09 +0000190 {
191 $this->display_error('db_invalid_query');
192 }
Derek Allard39b622d2008-01-16 21:10:09 +0000193
Rick Ellis59523592008-10-17 04:07:40 +0000194 $type = strtoupper($type);
195
196 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
197 {
198 show_error('Invalid function type: '.$type);
199 }
200
201 if ($alias == '')
202 {
203 $alias = $this->_create_alias_from_table(trim($select));
204 }
205
206 $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$alias;
Derek Allard39b622d2008-01-16 21:10:09 +0000207
208 $this->ar_select[] = $sql;
Rick Ellis59523592008-10-17 04:07:40 +0000209
Derek Allard9b3e7b52008-02-04 23:20:34 +0000210 if ($this->ar_caching === TRUE)
211 {
212 $this->ar_cache_select[] = $sql;
Rick Ellis392f09d2008-10-18 02:03:14 +0000213 $this->ar_cache_exists[] = 'select';
Derek Allard9b3e7b52008-02-04 23:20:34 +0000214 }
Rick Ellis59523592008-10-17 04:07:40 +0000215
Derek Allard39b622d2008-01-16 21:10:09 +0000216 return $this;
217 }
218
Derek Allard09de1852007-02-14 01:35:56 +0000219 // --------------------------------------------------------------------
220
221 /**
Rick Ellis59523592008-10-17 04:07:40 +0000222 * Determines the alias name based on the table
223 *
224 * @access private
225 * @param string
226 * @return string
227 */
228 function _create_alias_from_table($item)
229 {
230 if (strpos($item, '.') !== FALSE)
231 {
232 return end(explode('.', $item));
233 }
234
235 return $item;
236 }
237
238 // --------------------------------------------------------------------
239
240 /**
Derek Allard09de1852007-02-14 01:35:56 +0000241 * DISTINCT
242 *
243 * Sets a flag which tells the query string compiler to add DISTINCT
244 *
245 * @access public
246 * @param bool
247 * @return object
248 */
249 function distinct($val = TRUE)
250 {
251 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
252 return $this;
253 }
254
255 // --------------------------------------------------------------------
256
257 /**
258 * From
259 *
260 * Generates the FROM portion of the query
261 *
262 * @access public
263 * @param mixed can be a string or array
264 * @return object
265 */
266 function from($from)
267 {
268 foreach ((array)$from as $val)
269 {
Rick Ellis59523592008-10-17 04:07:40 +0000270 // 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);
273
274 $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
275
Derek Allard9b3e7b52008-02-04 23:20:34 +0000276 if ($this->ar_caching === TRUE)
277 {
Rick Ellis59523592008-10-17 04:07:40 +0000278 $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
Rick Ellis392f09d2008-10-18 02:03:14 +0000279 $this->ar_cache_exists[] = 'from';
Derek Allard9b3e7b52008-02-04 23:20:34 +0000280 }
Derek Allard09de1852007-02-14 01:35:56 +0000281 }
Derek Allard5e128942007-12-28 21:33:03 +0000282
Derek Allard09de1852007-02-14 01:35:56 +0000283 return $this;
284 }
Derek Allard5fe155e2008-05-12 19:14:57 +0000285
Derek Allard09de1852007-02-14 01:35:56 +0000286 // --------------------------------------------------------------------
287
288 /**
289 * Join
290 *
291 * Generates the JOIN portion of the query
292 *
293 * @access public
294 * @param string
295 * @param string the join condition
296 * @param string the type of join
297 * @return object
298 */
299 function join($table, $cond, $type = '')
300 {
301 if ($type != '')
302 {
303 $type = strtoupper(trim($type));
304
Rick Ellis59523592008-10-17 04:07:40 +0000305 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
Derek Allard09de1852007-02-14 01:35:56 +0000306 {
307 $type = '';
308 }
309 else
310 {
311 $type .= ' ';
312 }
313 }
314
Rick Ellis59523592008-10-17 04:07:40 +0000315 // 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);
318
319 // Strip apart the condition and protect the identifiers
320 if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
Derek Allard09de1852007-02-14 01:35:56 +0000321 {
Rick Ellis59523592008-10-17 04:07:40 +0000322 $match[1] = $this->_protect_identifiers($match[1]);
323 $match[3] = $this->_protect_identifiers($match[3]);
324
325 $cond = $match[1].$match[2].$match[3];
Derek Allard5fe155e2008-05-12 19:14:57 +0000326 }
Rick Ellis59523592008-10-17 04:07:40 +0000327
328 // Assemble the JOIN statement
329 $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000330
331 $this->ar_join[] = $join;
332 if ($this->ar_caching === TRUE)
333 {
334 $this->ar_cache_join[] = $join;
Rick Ellis392f09d2008-10-18 02:03:14 +0000335 $this->ar_cache_exists[] = 'join';
Derek Allard9b3e7b52008-02-04 23:20:34 +0000336 }
337
Derek Allard09de1852007-02-14 01:35:56 +0000338 return $this;
339 }
Derek Allard5fe155e2008-05-12 19:14:57 +0000340
Derek Allard09de1852007-02-14 01:35:56 +0000341 // --------------------------------------------------------------------
342
343 /**
344 * Where
345 *
346 * Generates the WHERE portion of the query. Separates
347 * multiple calls with AND
348 *
349 * @access public
350 * @param mixed
351 * @param mixed
352 * @return object
353 */
Derek Allard39b622d2008-01-16 21:10:09 +0000354 function where($key, $value = NULL, $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000355 {
Derek Allard39b622d2008-01-16 21:10:09 +0000356 return $this->_where($key, $value, 'AND ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000357 }
358
359 // --------------------------------------------------------------------
360
361 /**
362 * OR Where
363 *
364 * Generates the WHERE portion of the query. Separates
365 * multiple calls with OR
366 *
367 * @access public
368 * @param mixed
369 * @param mixed
370 * @return object
371 */
Derek Allard39b622d2008-01-16 21:10:09 +0000372 function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000373 {
Derek Allard39b622d2008-01-16 21:10:09 +0000374 return $this->_where($key, $value, 'OR ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000375 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000376
377 // --------------------------------------------------------------------
378
379 /**
380 * orwhere() is an alias of or_where()
381 * this function is here for backwards compatibility, as
382 * orwhere() has been deprecated
383 */
Derek Allard39b622d2008-01-16 21:10:09 +0000384 function orwhere($key, $value = NULL, $escape = TRUE)
Derek Allard218e2bc2007-12-17 21:18:14 +0000385 {
Derek Allard39b622d2008-01-16 21:10:09 +0000386 return $this->or_where($key, $value, $escape);
Derek Allard218e2bc2007-12-17 21:18:14 +0000387 }
Derek Allard67b44ed2008-01-12 16:18:02 +0000388
389 // --------------------------------------------------------------------
390
391 /**
Derek Allard09de1852007-02-14 01:35:56 +0000392 * Where
393 *
394 * Called by where() or orwhere()
395 *
396 * @access private
397 * @param mixed
398 * @param mixed
399 * @param string
400 * @return object
401 */
Rick Ellis59523592008-10-17 04:07:40 +0000402 function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard09de1852007-02-14 01:35:56 +0000403 {
Derek Jones0b59f272008-05-13 04:22:33 +0000404 if ( ! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000405 {
406 $key = array($key => $value);
407 }
Rick Ellis59523592008-10-17 04:07:40 +0000408
409 // If the escape value was not set will will base it on the global setting
410 if ( ! is_bool($escape))
411 {
412 $escape = $this->_protect_identifiers;
413 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000414
Derek Allard09de1852007-02-14 01:35:56 +0000415 foreach ($key as $k => $v)
416 {
Rick Ellis000f89d2008-10-17 23:26:15 +0000417 $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type;
Derek Allard15ddc9d2007-12-20 13:54:39 +0000418
Derek Allardd8364c42008-06-04 17:01:00 +0000419 if (is_null($v) && ! $this->_has_operator($k))
Derek Allard15ddc9d2007-12-20 13:54:39 +0000420 {
421 // value appears not to have been set, assign the test to IS NULL
422 $k .= ' IS NULL';
423 }
Derek Allard09de1852007-02-14 01:35:56 +0000424
Derek Jones0b59f272008-05-13 04:22:33 +0000425 if ( ! is_null($v))
Derek Allard09de1852007-02-14 01:35:56 +0000426 {
Derek Allard39b622d2008-01-16 21:10:09 +0000427 if ($escape === TRUE)
428 {
Rick Ellis59523592008-10-17 04:07:40 +0000429 $k = $this->_protect_identifiers($k, FALSE, $escape);
Derek Allardd8364c42008-06-04 17:01:00 +0000430
431 $v = ' '.$this->escape($v);
Derek Allard39b622d2008-01-16 21:10:09 +0000432 }
433
Derek Jones0b59f272008-05-13 04:22:33 +0000434 if ( ! $this->_has_operator($k))
Derek Allard09de1852007-02-14 01:35:56 +0000435 {
436 $k .= ' =';
437 }
Derek Allard09de1852007-02-14 01:35:56 +0000438 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000439 else
440 {
Rick Ellis59523592008-10-17 04:07:40 +0000441 $k = $this->_protect_identifiers($k, FALSE, $escape);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000442 }
443
Derek Allard09de1852007-02-14 01:35:56 +0000444 $this->ar_where[] = $prefix.$k.$v;
Derek Allardd8364c42008-06-04 17:01:00 +0000445
Derek Allard9b3e7b52008-02-04 23:20:34 +0000446 if ($this->ar_caching === TRUE)
447 {
448 $this->ar_cache_where[] = $prefix.$k.$v;
Rick Ellis392f09d2008-10-18 02:03:14 +0000449 $this->ar_cache_exists[] = 'where';
Derek Allard9b3e7b52008-02-04 23:20:34 +0000450 }
451
Derek Allard09de1852007-02-14 01:35:56 +0000452 }
Derek Jones7e98a272008-06-04 17:05:44 +0000453
Derek Allard09de1852007-02-14 01:35:56 +0000454 return $this;
455 }
Derek Allard80dd7022007-12-18 23:55:06 +0000456
457 // --------------------------------------------------------------------
458
459 /**
460 * Where_in
461 *
Derek Allardc6935512007-12-19 14:23:19 +0000462 * Generates a WHERE field IN ('item', 'item') SQL query joined with
463 * AND if appropriate
Derek Allard80dd7022007-12-18 23:55:06 +0000464 *
465 * @access public
466 * @param string The field to search
467 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000468 * @return object
469 */
470 function where_in($key = NULL, $values = NULL)
Rick Ellis59523592008-10-17 04:07:40 +0000471 {
Derek Allardc6935512007-12-19 14:23:19 +0000472 return $this->_where_in($key, $values);
473 }
474
475 // --------------------------------------------------------------------
476
477 /**
478 * Where_in_or
479 *
480 * Generates a WHERE field IN ('item', 'item') SQL query joined with
481 * OR if appropriate
482 *
483 * @access public
484 * @param string The field to search
485 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000486 * @return object
487 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000488 function or_where_in($key = NULL, $values = NULL)
Derek Allard5fe155e2008-05-12 19:14:57 +0000489 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000490 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000491 }
492
493 // --------------------------------------------------------------------
494
495 /**
496 * Where_not_in
497 *
498 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
499 * with AND if appropriate
500 *
501 * @access public
502 * @param string The field to search
503 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000504 * @return object
505 */
506 function where_not_in($key = NULL, $values = NULL)
Derek Allard5fe155e2008-05-12 19:14:57 +0000507 {
Derek Allardc6935512007-12-19 14:23:19 +0000508 return $this->_where_in($key, $values, TRUE);
509 }
510
511 // --------------------------------------------------------------------
512
513 /**
514 * Where_not_in_or
515 *
516 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
517 * with OR if appropriate
518 *
519 * @access public
520 * @param string The field to search
521 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000522 * @return object
523 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000524 function or_where_not_in($key = NULL, $values = NULL)
Derek Allard5fe155e2008-05-12 19:14:57 +0000525 {
Derek Jonesd0072432008-05-07 22:06:51 +0000526 return $this->_where_in($key, $values, TRUE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000527 }
528
529 // --------------------------------------------------------------------
530
531 /**
532 * Where_in
533 *
534 * Called by where_in, where_in_or, where_not_in, where_not_in_or
535 *
536 * @access public
537 * @param string The field to search
538 * @param array The values searched on
Rick Ellis59523592008-10-17 04:07:40 +0000539 * @param boolean If the statement would be IN or NOT IN
Derek Allard80dd7022007-12-18 23:55:06 +0000540 * @param string
541 * @return object
542 */
Derek Allard15ddc9d2007-12-20 13:54:39 +0000543 function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard5fe155e2008-05-12 19:14:57 +0000544 {
Rick Ellis59523592008-10-17 04:07:40 +0000545 if ($key === NULL OR $values === NULL)
Derek Allard80dd7022007-12-18 23:55:06 +0000546 {
547 return;
548 }
Rick Ellis59523592008-10-17 04:07:40 +0000549
550 if ( ! is_array($values))
551 {
552 $values = array($values);
553 }
554
555 $not = ($not) ? ' NOT' : '';
Derek Allard80dd7022007-12-18 23:55:06 +0000556
557 foreach ($values as $value)
558 {
559 $this->ar_wherein[] = $this->escape($value);
560 }
561
562 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Derek Allard5fe155e2008-05-12 19:14:57 +0000563
Derek Allard9b3e7b52008-02-04 23:20:34 +0000564 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
565
566 $this->ar_where[] = $where_in;
567 if ($this->ar_caching === TRUE)
568 {
569 $this->ar_cache_where[] = $where_in;
Rick Ellis392f09d2008-10-18 02:03:14 +0000570 $this->ar_cache_exists[] = 'where';
Derek Allard9b3e7b52008-02-04 23:20:34 +0000571 }
Derek Allard80dd7022007-12-18 23:55:06 +0000572
Derek Allard8f000212008-01-18 14:45:59 +0000573 // reset the array for multiple calls
574 $this->ar_wherein = array();
Derek Allard80dd7022007-12-18 23:55:06 +0000575 return $this;
576 }
577
Derek Allard09de1852007-02-14 01:35:56 +0000578 // --------------------------------------------------------------------
579
580 /**
581 * Like
582 *
583 * Generates a %LIKE% portion of the query. Separates
584 * multiple calls with AND
585 *
586 * @access public
587 * @param mixed
588 * @param mixed
589 * @return object
590 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000591 function like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000592 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000593 return $this->_like($field, $match, 'AND ', $side);
Derek Allard09de1852007-02-14 01:35:56 +0000594 }
Derek Allarde54e3d22007-12-19 15:53:44 +0000595
596 // --------------------------------------------------------------------
597
598 /**
599 * Not Like
600 *
601 * Generates a NOT LIKE portion of the query. Separates
602 * multiple calls with AND
603 *
604 * @access public
605 * @param mixed
606 * @param mixed
607 * @return object
608 */
609 function not_like($field, $match = '', $side = 'both')
610 {
Rick Ellis59523592008-10-17 04:07:40 +0000611 return $this->_like($field, $match, 'AND ', $side, 'NOT');
Derek Allarde54e3d22007-12-19 15:53:44 +0000612 }
613
Derek Allard09de1852007-02-14 01:35:56 +0000614 // --------------------------------------------------------------------
615
616 /**
617 * OR Like
618 *
619 * Generates a %LIKE% portion of the query. Separates
620 * multiple calls with OR
621 *
622 * @access public
623 * @param mixed
624 * @param mixed
625 * @return object
626 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000627 function or_like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000628 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000629 return $this->_like($field, $match, 'OR ', $side);
630 }
631
632 // --------------------------------------------------------------------
633
634 /**
Derek Allarde54e3d22007-12-19 15:53:44 +0000635 * OR Not Like
636 *
637 * Generates a NOT LIKE portion of the query. Separates
638 * multiple calls with OR
639 *
640 * @access public
641 * @param mixed
642 * @param mixed
643 * @return object
644 */
645 function or_not_like($field, $match = '', $side = 'both')
646 {
Rick Ellis59523592008-10-17 04:07:40 +0000647 return $this->_like($field, $match, 'OR ', $side, 'NOT');
Derek Allarde54e3d22007-12-19 15:53:44 +0000648 }
649
650 // --------------------------------------------------------------------
651
652 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000653 * orlike() is an alias of or_like()
654 * this function is here for backwards compatibility, as
655 * orlike() has been deprecated
656 */
657 function orlike($field, $match = '', $side = 'both')
658 {
Derek Allard4a310b72008-01-30 21:32:47 +0000659 return $this->or_like($field, $match, $side);
Derek Allard09de1852007-02-14 01:35:56 +0000660 }
661
662 // --------------------------------------------------------------------
663
664 /**
665 * Like
666 *
667 * Called by like() or orlike()
668 *
669 * @access private
670 * @param mixed
671 * @param mixed
672 * @param string
673 * @return object
674 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000675 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard09de1852007-02-14 01:35:56 +0000676 {
Derek Jones0b59f272008-05-13 04:22:33 +0000677 if ( ! is_array($field))
Derek Allard09de1852007-02-14 01:35:56 +0000678 {
679 $field = array($field => $match);
680 }
681
682 foreach ($field as $k => $v)
Rick Ellis59523592008-10-17 04:07:40 +0000683 {
Derek Allard39b622d2008-01-16 21:10:09 +0000684 $k = $this->_protect_identifiers($k);
685
Derek Allard09de1852007-02-14 01:35:56 +0000686 $prefix = (count($this->ar_like) == 0) ? '' : $type;
Derek Allarde54e3d22007-12-19 15:53:44 +0000687
Rick Ellis59523592008-10-17 04:07:40 +0000688 $v = $this->escape_str($v);
Derek Allarde54e3d22007-12-19 15:53:44 +0000689
Derek Allard218e2bc2007-12-17 21:18:14 +0000690 if ($side == 'before')
691 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000692 $like_statement = $prefix." $k $not LIKE '%{$v}'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000693 }
694 elseif ($side == 'after')
695 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000696 $like_statement = $prefix." $k $not LIKE '{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000697 }
698 else
699 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000700 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000701 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000702
703 $this->ar_like[] = $like_statement;
704 if ($this->ar_caching === TRUE)
705 {
706 $this->ar_cache_like[] = $like_statement;
Rick Ellis392f09d2008-10-18 02:03:14 +0000707 $this->ar_cache_exists[] = 'like';
Derek Allard9b3e7b52008-02-04 23:20:34 +0000708 }
709
Derek Allard09de1852007-02-14 01:35:56 +0000710 }
711 return $this;
712 }
713
714 // --------------------------------------------------------------------
715
716 /**
717 * GROUP BY
718 *
719 * @access public
720 * @param string
721 * @return object
722 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000723 function group_by($by)
Derek Allard09de1852007-02-14 01:35:56 +0000724 {
725 if (is_string($by))
726 {
727 $by = explode(',', $by);
728 }
729
730 foreach ($by as $val)
731 {
732 $val = trim($val);
733
734 if ($val != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000735 {
Derek Allard39b622d2008-01-16 21:10:09 +0000736 $this->ar_groupby[] = $this->_protect_identifiers($val);
Rick Ellis59523592008-10-17 04:07:40 +0000737
Derek Allard9b3e7b52008-02-04 23:20:34 +0000738 if ($this->ar_caching === TRUE)
739 {
740 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
Rick Ellis392f09d2008-10-18 02:03:14 +0000741 $this->ar_cache_exists[] = 'groupby';
Derek Allard9b3e7b52008-02-04 23:20:34 +0000742 }
743 }
Derek Allard09de1852007-02-14 01:35:56 +0000744 }
745 return $this;
746 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000747
748 // --------------------------------------------------------------------
749
750 /**
751 * groupby() is an alias of group_by()
752 * this function is here for backwards compatibility, as
753 * groupby() has been deprecated
754 */
755 function groupby($by)
756 {
757 return $this->group_by($by);
758 }
759
Derek Allard09de1852007-02-14 01:35:56 +0000760 // --------------------------------------------------------------------
761
762 /**
763 * Sets the HAVING value
764 *
765 * Separates multiple calls with AND
766 *
767 * @access public
768 * @param string
769 * @param string
770 * @return object
771 */
Derek Allarde808aac2008-04-06 18:22:00 +0000772 function having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000773 {
Derek Allarde808aac2008-04-06 18:22:00 +0000774 return $this->_having($key, $value, 'AND ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000775 }
Derek Allard43e8cbf2008-03-01 23:14:43 +0000776
777 // --------------------------------------------------------------------
778
779 /**
780 * orhaving() is an alias of or_having()
781 * this function is here for backwards compatibility, as
782 * orhaving() has been deprecated
783 */
784
Derek Allarde808aac2008-04-06 18:22:00 +0000785 function orhaving($key, $value = '', $escape = TRUE)
Derek Allard43e8cbf2008-03-01 23:14:43 +0000786 {
Derek Allarda459b462008-05-22 13:01:39 +0000787 return $this->or_having($key, $value, $escape);
Derek Allard43e8cbf2008-03-01 23:14:43 +0000788 }
Derek Allard09de1852007-02-14 01:35:56 +0000789 // --------------------------------------------------------------------
790
791 /**
792 * Sets the OR HAVING value
793 *
794 * Separates multiple calls with OR
795 *
796 * @access public
797 * @param string
798 * @param string
799 * @return object
800 */
Derek Allarde808aac2008-04-06 18:22:00 +0000801 function or_having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000802 {
Derek Allarde808aac2008-04-06 18:22:00 +0000803 return $this->_having($key, $value, 'OR ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000804 }
805
806 // --------------------------------------------------------------------
807
808 /**
809 * Sets the HAVING values
810 *
Rick Ellisff734012008-09-30 20:38:12 +0000811 * Called by having() or or_having()
Derek Allard09de1852007-02-14 01:35:56 +0000812 *
813 * @access private
814 * @param string
815 * @param string
816 * @return object
817 */
Derek Allarde808aac2008-04-06 18:22:00 +0000818 function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000819 {
Derek Jones0b59f272008-05-13 04:22:33 +0000820 if ( ! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000821 {
822 $key = array($key => $value);
823 }
824
825 foreach ($key as $k => $v)
826 {
827 $prefix = (count($this->ar_having) == 0) ? '' : $type;
Derek Allarde808aac2008-04-06 18:22:00 +0000828
829 if ($escape === TRUE)
830 {
831 $k = $this->_protect_identifiers($k);
832 }
833
Derek Allarda459b462008-05-22 13:01:39 +0000834 if ( ! $this->_has_operator($k))
835 {
836 $k .= ' = ';
837 }
838
Derek Allard09de1852007-02-14 01:35:56 +0000839 if ($v != '')
840 {
Rick Ellis59523592008-10-17 04:07:40 +0000841 $v = ' '.$this->escape_str($v);
Derek Allard09de1852007-02-14 01:35:56 +0000842 }
843
844 $this->ar_having[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000845 if ($this->ar_caching === TRUE)
846 {
847 $this->ar_cache_having[] = $prefix.$k.$v;
Rick Ellis392f09d2008-10-18 02:03:14 +0000848 $this->ar_cache_exists[] = 'having';
Derek Allard9b3e7b52008-02-04 23:20:34 +0000849 }
Derek Allard09de1852007-02-14 01:35:56 +0000850 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000851
Derek Allard09de1852007-02-14 01:35:56 +0000852 return $this;
853 }
854
855 // --------------------------------------------------------------------
856
857 /**
858 * Sets the ORDER BY value
859 *
860 * @access public
861 * @param string
862 * @param string direction: asc or desc
863 * @return object
864 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000865 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000866 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000867 if (strtolower($direction) == 'random')
868 {
869 $orderby = ''; // Random results want or don't need a field name
870 $direction = $this->_random_keyword;
871 }
872 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000873 {
Derek Allard92782492007-08-10 11:26:01 +0000874 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000875 }
876
Rick Ellis59523592008-10-17 04:07:40 +0000877 $orderby_statement = $this->_protect_identifiers($orderby).$direction;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000878
879 $this->ar_orderby[] = $orderby_statement;
880 if ($this->ar_caching === TRUE)
881 {
882 $this->ar_cache_orderby[] = $orderby_statement;
Rick Ellis392f09d2008-10-18 02:03:14 +0000883 $this->ar_cache_exists[] = 'orderby';
Derek Allard9b3e7b52008-02-04 23:20:34 +0000884 }
885
Derek Allard09de1852007-02-14 01:35:56 +0000886 return $this;
887 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000888
Derek Allard218e2bc2007-12-17 21:18:14 +0000889 // --------------------------------------------------------------------
890
891 /**
892 * orderby() is an alias of order_by()
893 * this function is here for backwards compatibility, as
894 * orderby() has been deprecated
895 */
896 function orderby($orderby, $direction = '')
897 {
898 return $this->order_by($orderby, $direction);
899 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000900
Derek Allard09de1852007-02-14 01:35:56 +0000901 // --------------------------------------------------------------------
902
903 /**
904 * Sets the LIMIT value
905 *
906 * @access public
907 * @param integer the limit value
908 * @param integer the offset value
909 * @return object
910 */
911 function limit($value, $offset = '')
912 {
913 $this->ar_limit = $value;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000914
Derek Allard09de1852007-02-14 01:35:56 +0000915 if ($offset != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000916 {
Derek Allard09de1852007-02-14 01:35:56 +0000917 $this->ar_offset = $offset;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000918 }
Derek Allard09de1852007-02-14 01:35:56 +0000919
920 return $this;
921 }
922
923 // --------------------------------------------------------------------
924
925 /**
926 * Sets the OFFSET value
927 *
928 * @access public
929 * @param integer the offset value
930 * @return object
931 */
Derek Allard9b3e7b52008-02-04 23:20:34 +0000932 function offset($offset)
Derek Allard09de1852007-02-14 01:35:56 +0000933 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000934 $this->ar_offset = $offset;
Derek Allard09de1852007-02-14 01:35:56 +0000935 return $this;
936 }
937
938 // --------------------------------------------------------------------
939
940 /**
941 * The "set" function. Allows key/value pairs to be set for inserting or updating
942 *
943 * @access public
944 * @param mixed
945 * @param string
Derek Allard39b622d2008-01-16 21:10:09 +0000946 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +0000947 * @return object
948 */
Derek Allard39b622d2008-01-16 21:10:09 +0000949 function set($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000950 {
951 $key = $this->_object_to_array($key);
952
Derek Jones0b59f272008-05-13 04:22:33 +0000953 if ( ! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000954 {
955 $key = array($key => $value);
956 }
957
958 foreach ($key as $k => $v)
959 {
Derek Allard39b622d2008-01-16 21:10:09 +0000960 if ($escape === FALSE)
961 {
962 $this->ar_set[$this->_protect_identifiers($k)] = $v;
963 }
964 else
965 {
966 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
967 }
Derek Allard09de1852007-02-14 01:35:56 +0000968 }
969
970 return $this;
971 }
972
973 // --------------------------------------------------------------------
974
975 /**
976 * Get
977 *
978 * Compiles the select statement based on the other functions called
979 * and runs the query
980 *
981 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +0000982 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +0000983 * @param string the limit clause
984 * @param string the offset clause
985 * @return object
986 */
987 function get($table = '', $limit = null, $offset = null)
988 {
989 if ($table != '')
990 {
Derek Allard5e128942007-12-28 21:33:03 +0000991 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +0000992 $this->from($table);
993 }
994
Derek Jones0b59f272008-05-13 04:22:33 +0000995 if ( ! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +0000996 {
997 $this->limit($limit, $offset);
998 }
999
1000 $sql = $this->_compile_select();
1001
1002 $result = $this->query($sql);
1003 $this->_reset_select();
1004 return $result;
1005 }
1006
Derek Allard09de1852007-02-14 01:35:56 +00001007 /**
Derek Allard694b5b82007-12-18 15:58:03 +00001008 * "Count All Results" query
1009 *
1010 * Generates a platform-specific query string that counts all records
1011 * returned by an Active Record query.
1012 *
1013 * @access public
1014 * @param string
1015 * @return string
1016 */
1017 function count_all_results($table = '')
1018 {
1019 if ($table != '')
1020 {
Derek Allard5e128942007-12-28 21:33:03 +00001021 $this->_track_aliases($table);
Derek Allard694b5b82007-12-18 15:58:03 +00001022 $this->from($table);
1023 }
1024
Derek Allard39b622d2008-01-16 21:10:09 +00001025 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
Derek Allard694b5b82007-12-18 15:58:03 +00001026
1027 $query = $this->query($sql);
1028 $this->_reset_select();
1029
1030 if ($query->num_rows() == 0)
1031 {
1032 return '0';
1033 }
1034
1035 $row = $query->row();
1036 return $row->numrows;
1037 }
1038
1039 // --------------------------------------------------------------------
1040
1041 /**
Derek Allard218e2bc2007-12-17 21:18:14 +00001042 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +00001043 *
1044 * Allows the where clause, limit and offset to be added directly
1045 *
1046 * @access public
1047 * @param string the where clause
1048 * @param string the limit clause
1049 * @param string the offset clause
1050 * @return object
1051 */
Derek Allard218e2bc2007-12-17 21:18:14 +00001052 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +00001053 {
1054 if ($table != '')
1055 {
1056 $this->from($table);
1057 }
1058
Derek Jones0b59f272008-05-13 04:22:33 +00001059 if ( ! is_null($where))
Derek Allard09de1852007-02-14 01:35:56 +00001060 {
1061 $this->where($where);
1062 }
1063
Derek Jones0b59f272008-05-13 04:22:33 +00001064 if ( ! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001065 {
1066 $this->limit($limit, $offset);
1067 }
1068
1069 $sql = $this->_compile_select();
1070
1071 $result = $this->query($sql);
1072 $this->_reset_select();
1073 return $result;
1074 }
Derek Allard218e2bc2007-12-17 21:18:14 +00001075
1076 // --------------------------------------------------------------------
1077
1078 /**
1079 * getwhere() is an alias of get_where()
1080 * this function is here for backwards compatibility, as
1081 * getwhere() has been deprecated
1082 */
1083 function getwhere($table = '', $where = null, $limit = null, $offset = null)
1084 {
1085 return $this->get_where($table, $where, $limit, $offset);
1086 }
Derek Allard09de1852007-02-14 01:35:56 +00001087
1088 // --------------------------------------------------------------------
1089
1090 /**
1091 * Insert
1092 *
1093 * Compiles an insert string and runs the query
1094 *
1095 * @access public
1096 * @param string the table to retrieve the results from
1097 * @param array an associative array of insert values
1098 * @return object
1099 */
1100 function insert($table = '', $set = NULL)
Rick Ellis849ecde2008-10-18 06:47:53 +00001101 {
Derek Jones0b59f272008-05-13 04:22:33 +00001102 if ( ! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001103 {
1104 $this->set($set);
1105 }
1106
1107 if (count($this->ar_set) == 0)
1108 {
1109 if ($this->db_debug)
1110 {
1111 return $this->display_error('db_must_use_set');
1112 }
1113 return FALSE;
1114 }
1115
1116 if ($table == '')
1117 {
Derek Jones0b59f272008-05-13 04:22:33 +00001118 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001119 {
1120 if ($this->db_debug)
1121 {
1122 return $this->display_error('db_must_set_table');
1123 }
1124 return FALSE;
1125 }
1126
1127 $table = $this->ar_from[0];
1128 }
Derek Allard39b622d2008-01-16 21:10:09 +00001129
Rick Ellis59523592008-10-17 04:07:40 +00001130 $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
Derek Allard09de1852007-02-14 01:35:56 +00001131
1132 $this->_reset_write();
1133 return $this->query($sql);
1134 }
1135
1136 // --------------------------------------------------------------------
1137
1138 /**
1139 * Update
1140 *
1141 * Compiles an update string and runs the query
1142 *
1143 * @access public
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
1147 * @return object
1148 */
Derek Allard5e128942007-12-28 21:33:03 +00001149 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001150 {
Rick Ellis849ecde2008-10-18 06:47:53 +00001151 // Combine any cached components with the current statements
1152 $this->_merge_cache();
1153
Derek Jones0b59f272008-05-13 04:22:33 +00001154 if ( ! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001155 {
1156 $this->set($set);
1157 }
1158
1159 if (count($this->ar_set) == 0)
1160 {
1161 if ($this->db_debug)
1162 {
1163 return $this->display_error('db_must_use_set');
1164 }
1165 return FALSE;
1166 }
1167
1168 if ($table == '')
1169 {
Derek Jones0b59f272008-05-13 04:22:33 +00001170 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001171 {
1172 if ($this->db_debug)
1173 {
1174 return $this->display_error('db_must_set_table');
1175 }
1176 return FALSE;
1177 }
1178
1179 $table = $this->ar_from[0];
1180 }
1181
Derek Allarde77d77c2007-12-19 15:01:55 +00001182 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001183 {
1184 $this->where($where);
1185 }
Derek Allardda6d2402007-12-19 14:49:29 +00001186
Derek Allarde77d77c2007-12-19 15:01:55 +00001187 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +00001188 {
1189 $this->limit($limit);
1190 }
Derek Allard09de1852007-02-14 01:35:56 +00001191
Rick Ellis59523592008-10-17 04:07:40 +00001192 $sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001193
1194 $this->_reset_write();
1195 return $this->query($sql);
1196 }
Derek Allard39b622d2008-01-16 21:10:09 +00001197
1198 // --------------------------------------------------------------------
1199
1200 /**
1201 * Empty Table
1202 *
1203 * Compiles a delete string and runs "DELETE FROM table"
1204 *
1205 * @access public
1206 * @param string the table to empty
1207 * @return object
1208 */
1209 function empty_table($table = '')
1210 {
1211 if ($table == '')
1212 {
Derek Jones0b59f272008-05-13 04:22:33 +00001213 if ( ! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001214 {
1215 if ($this->db_debug)
1216 {
1217 return $this->display_error('db_must_set_table');
1218 }
1219 return FALSE;
1220 }
1221
1222 $table = $this->ar_from[0];
1223 }
1224 else
1225 {
Rick Ellis59523592008-10-17 04:07:40 +00001226 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard39b622d2008-01-16 21:10:09 +00001227 }
1228
Derek Allard39b622d2008-01-16 21:10:09 +00001229 $sql = $this->_delete($table);
1230
1231 $this->_reset_write();
1232
1233 return $this->query($sql);
1234 }
1235
1236 // --------------------------------------------------------------------
1237
1238 /**
1239 * Truncate
1240 *
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"
1244 *
1245 * @access public
1246 * @param string the table to truncate
1247 * @return object
1248 */
1249 function truncate($table = '')
1250 {
1251 if ($table == '')
1252 {
Derek Jones0b59f272008-05-13 04:22:33 +00001253 if ( ! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001254 {
1255 if ($this->db_debug)
1256 {
1257 return $this->display_error('db_must_set_table');
1258 }
1259 return FALSE;
1260 }
1261
1262 $table = $this->ar_from[0];
1263 }
1264 else
1265 {
Rick Ellis59523592008-10-17 04:07:40 +00001266 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard39b622d2008-01-16 21:10:09 +00001267 }
1268
Derek Allard39b622d2008-01-16 21:10:09 +00001269 $sql = $this->_truncate($table);
1270
1271 $this->_reset_write();
1272
1273 return $this->query($sql);
1274 }
Derek Allard09de1852007-02-14 01:35:56 +00001275
1276 // --------------------------------------------------------------------
1277
1278 /**
1279 * Delete
1280 *
1281 * Compiles a delete string and runs the query
1282 *
1283 * @access public
Derek Allard41f60d42007-12-20 20:09:22 +00001284 * @param mixed the table(s) to delete from. String or array
Derek Allard09de1852007-02-14 01:35:56 +00001285 * @param mixed the where clause
Derek Allard41f60d42007-12-20 20:09:22 +00001286 * @param mixed the limit clause
1287 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +00001288 * @return object
1289 */
Derek Allard41f60d42007-12-20 20:09:22 +00001290 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +00001291 {
Rick Ellis849ecde2008-10-18 06:47:53 +00001292 // Combine any cached components with the current statements
1293 $this->_merge_cache();
1294
Derek Allard09de1852007-02-14 01:35:56 +00001295 if ($table == '')
1296 {
Derek Jones0b59f272008-05-13 04:22:33 +00001297 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001298 {
1299 if ($this->db_debug)
1300 {
1301 return $this->display_error('db_must_set_table');
1302 }
1303 return FALSE;
1304 }
Derek Allard39b622d2008-01-16 21:10:09 +00001305
Derek Allard09de1852007-02-14 01:35:56 +00001306 $table = $this->ar_from[0];
1307 }
Derek Allard39b622d2008-01-16 21:10:09 +00001308 elseif (is_array($table))
Derek Allard41f60d42007-12-20 20:09:22 +00001309 {
1310 foreach($table as $single_table)
1311 {
Derek Allard39b622d2008-01-16 21:10:09 +00001312 $this->delete($single_table, $where, $limit, FALSE);
Derek Allard41f60d42007-12-20 20:09:22 +00001313 }
Derek Allard39b622d2008-01-16 21:10:09 +00001314
Derek Allard41f60d42007-12-20 20:09:22 +00001315 $this->_reset_write();
1316 return;
1317 }
Derek Allard39b622d2008-01-16 21:10:09 +00001318 else
1319 {
Rick Ellis59523592008-10-17 04:07:40 +00001320 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard39b622d2008-01-16 21:10:09 +00001321 }
Derek Allard41f60d42007-12-20 20:09:22 +00001322
Derek Allard09de1852007-02-14 01:35:56 +00001323 if ($where != '')
1324 {
1325 $this->where($where);
1326 }
1327
Derek Allarde77d77c2007-12-19 15:01:55 +00001328 if ($limit != NULL)
1329 {
1330 $this->limit($limit);
1331 }
1332
Derek Allard39b622d2008-01-16 21:10:09 +00001333 if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
Derek Allard09de1852007-02-14 01:35:56 +00001334 {
1335 if ($this->db_debug)
1336 {
1337 return $this->display_error('db_del_must_use_where');
1338 }
Derek Allard39b622d2008-01-16 21:10:09 +00001339
Derek Allard09de1852007-02-14 01:35:56 +00001340 return FALSE;
1341 }
Derek Allard41f60d42007-12-20 20:09:22 +00001342
Derek Allard39b622d2008-01-16 21:10:09 +00001343 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001344
Derek Allard41f60d42007-12-20 20:09:22 +00001345 if ($reset_data)
1346 {
1347 $this->_reset_write();
1348 }
Derek Allard39b622d2008-01-16 21:10:09 +00001349
Derek Allard09de1852007-02-14 01:35:56 +00001350 return $this->query($sql);
1351 }
Derek Allard15ddc9d2007-12-20 13:54:39 +00001352
Derek Allard09de1852007-02-14 01:35:56 +00001353 // --------------------------------------------------------------------
1354
1355 /**
Rick Ellis59523592008-10-17 04:07:40 +00001356 * DB Prefix
Derek Allard09de1852007-02-14 01:35:56 +00001357 *
Rick Ellis59523592008-10-17 04:07:40 +00001358 * Prepends a database prefix if one exists in configuration
1359 *
1360 * @access public
1361 * @param string the table
1362 * @return string
Derek Allard09de1852007-02-14 01:35:56 +00001363 */
Rick Ellis59523592008-10-17 04:07:40 +00001364 function dbprefix($table = '')
Derek Allard09de1852007-02-14 01:35:56 +00001365 {
Rick Ellis59523592008-10-17 04:07:40 +00001366 if ($table == '')
1367 {
1368 $this->display_error('db_table_name_required');
1369 }
1370
1371 return $this->dbprefix.$table;
Derek Allard09de1852007-02-14 01:35:56 +00001372 }
Derek Allard09de1852007-02-14 01:35:56 +00001373
Derek Allard09de1852007-02-14 01:35:56 +00001374 // --------------------------------------------------------------------
1375
1376 /**
Derek Allard5e128942007-12-28 21:33:03 +00001377 * Track Aliases
1378 *
1379 * Used to track SQL statements written with aliased tables.
1380 *
1381 * @access private
1382 * @param string The table to inspect
1383 * @return string
1384 */
1385 function _track_aliases($table)
1386 {
Rick Ellis59523592008-10-17 04:07:40 +00001387 if (is_array($table))
1388 {
1389 foreach ($table as $t)
1390 {
1391 $this->_track_aliases($t);
1392 }
1393 return;
1394 }
1395
1396 // Does the string contain a comma? If so, we need to separate
1397 // the string into discreet statements
1398 if (strpos($table, ',') !== FALSE)
1399 {
1400 return $this->_track_aliases(explode(',', $table));
1401 }
1402
Derek Allard5e128942007-12-28 21:33:03 +00001403 // if a table alias is used we can recognize it by a space
1404 if (strpos($table, " ") !== FALSE)
1405 {
Rick Ellis59523592008-10-17 04:07:40 +00001406 // if the alias is written with the AS keyword, remove it
1407 $table = preg_replace('/ AS /i', ' ', $table);
1408
1409 // Grab the alias
1410 $table = trim(strrchr($table, " "));
1411
1412 // Store the alias, if it doesn't already exist
1413 if ( ! in_array($table, $this->ar_aliased_tables))
Derek Allard5e128942007-12-28 21:33:03 +00001414 {
Rick Ellis59523592008-10-17 04:07:40 +00001415 $this->ar_aliased_tables[] = $table;
Derek Allard5e128942007-12-28 21:33:03 +00001416 }
Derek Allard5e128942007-12-28 21:33:03 +00001417 }
Derek Allard5e128942007-12-28 21:33:03 +00001418 }
1419
1420 // --------------------------------------------------------------------
1421
1422 /**
Derek Allard09de1852007-02-14 01:35:56 +00001423 * Compile the SELECT statement
1424 *
1425 * Generates a query string based on which functions were used.
1426 * Should not be called directly. The get() function calls it.
1427 *
1428 * @access private
1429 * @return string
1430 */
Derek Allard694b5b82007-12-18 15:58:03 +00001431 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001432 {
Rick Ellis65837c72008-10-18 06:39:49 +00001433 // Combine any cached components with the current statements
Derek Allard78255262008-02-06 13:54:23 +00001434 $this->_merge_cache();
Derek Allard9b3e7b52008-02-04 23:20:34 +00001435
Rick Ellis59523592008-10-17 04:07:40 +00001436 // ----------------------------------------------------------------
1437
1438 // Write the "select" portion of the query
Derek Allard09de1852007-02-14 01:35:56 +00001439
Derek Allard694b5b82007-12-18 15:58:03 +00001440 if ($select_override !== FALSE)
1441 {
1442 $sql = $select_override;
1443 }
Rick Ellis59523592008-10-17 04:07:40 +00001444 else
1445 {
1446 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
1447
1448 if (count($this->ar_select) == 0)
1449 {
1450 $sql .= '*';
1451 }
1452 else
1453 {
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)
1458 {
1459 $this->ar_select[$key] = $this->_protect_identifiers($val);
1460 }
1461
1462 $sql .= implode(', ', $this->ar_select);
1463 }
1464 }
1465
1466 // ----------------------------------------------------------------
1467
1468 // Write the "FROM" portion of the query
Derek Allard694b5b82007-12-18 15:58:03 +00001469
Derek Allard09de1852007-02-14 01:35:56 +00001470 if (count($this->ar_from) > 0)
1471 {
1472 $sql .= "\nFROM ";
Rick Ellis59523592008-10-17 04:07:40 +00001473
Derek Jonesc6ad0232008-01-29 18:44:54 +00001474 $sql .= $this->_from_tables($this->ar_from);
Derek Allard09de1852007-02-14 01:35:56 +00001475 }
1476
Rick Ellis59523592008-10-17 04:07:40 +00001477 // ----------------------------------------------------------------
1478
1479 // Write the "JOIN" portion of the query
1480
Derek Allard09de1852007-02-14 01:35:56 +00001481 if (count($this->ar_join) > 0)
Derek Allard5e128942007-12-28 21:33:03 +00001482 {
Derek Allard09de1852007-02-14 01:35:56 +00001483 $sql .= "\n";
Derek Allard5e128942007-12-28 21:33:03 +00001484
Rick Ellis59523592008-10-17 04:07:40 +00001485 $sql .= implode("\n", $this->ar_join);
Derek Allard09de1852007-02-14 01:35:56 +00001486 }
1487
Rick Ellis59523592008-10-17 04:07:40 +00001488 // ----------------------------------------------------------------
1489
1490 // Write the "WHERE" portion of the query
1491
Derek Allard09de1852007-02-14 01:35:56 +00001492 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1493 {
Rick Ellis59523592008-10-17 04:07:40 +00001494 $sql .= "\n";
1495
1496 $sql .= "WHERE ";
Derek Allard09de1852007-02-14 01:35:56 +00001497 }
1498
1499 $sql .= implode("\n", $this->ar_where);
Rick Ellis59523592008-10-17 04:07:40 +00001500
1501 // ----------------------------------------------------------------
Derek Allard09de1852007-02-14 01:35:56 +00001502
Rick Ellis59523592008-10-17 04:07:40 +00001503 // Write the "LIKE" portion of the query
1504
Derek Allard09de1852007-02-14 01:35:56 +00001505 if (count($this->ar_like) > 0)
1506 {
1507 if (count($this->ar_where) > 0)
1508 {
Rick Ellis59523592008-10-17 04:07:40 +00001509 $sql .= "\nAND ";
Derek Allard09de1852007-02-14 01:35:56 +00001510 }
1511
1512 $sql .= implode("\n", $this->ar_like);
1513 }
Rick Ellis59523592008-10-17 04:07:40 +00001514
1515 // ----------------------------------------------------------------
Derek Allard09de1852007-02-14 01:35:56 +00001516
Rick Ellis59523592008-10-17 04:07:40 +00001517 // Write the "GROUP BY" portion of the query
1518
Derek Allard09de1852007-02-14 01:35:56 +00001519 if (count($this->ar_groupby) > 0)
1520 {
1521 $sql .= "\nGROUP BY ";
Derek Allard5e128942007-12-28 21:33:03 +00001522
Rick Ellis59523592008-10-17 04:07:40 +00001523 $sql .= implode(', ', $this->ar_groupby);
Derek Allard09de1852007-02-14 01:35:56 +00001524 }
Rick Ellis59523592008-10-17 04:07:40 +00001525
1526 // ----------------------------------------------------------------
1527
1528 // Write the "HAVING" portion of the query
Derek Allard09de1852007-02-14 01:35:56 +00001529
1530 if (count($this->ar_having) > 0)
1531 {
1532 $sql .= "\nHAVING ";
1533 $sql .= implode("\n", $this->ar_having);
1534 }
1535
Rick Ellis59523592008-10-17 04:07:40 +00001536 // ----------------------------------------------------------------
1537
1538 // Write the "ORDER BY" portion of the query
1539
Derek Allard09de1852007-02-14 01:35:56 +00001540 if (count($this->ar_orderby) > 0)
1541 {
1542 $sql .= "\nORDER BY ";
1543 $sql .= implode(', ', $this->ar_orderby);
1544
1545 if ($this->ar_order !== FALSE)
1546 {
1547 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1548 }
1549 }
Rick Ellis59523592008-10-17 04:07:40 +00001550
1551 // ----------------------------------------------------------------
1552
1553 // Write the "LIMIT" portion of the query
Derek Allard09de1852007-02-14 01:35:56 +00001554
1555 if (is_numeric($this->ar_limit))
1556 {
1557 $sql .= "\n";
1558 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1559 }
1560
1561 return $sql;
1562 }
1563
1564 // --------------------------------------------------------------------
1565
1566 /**
1567 * Object to Array
1568 *
1569 * Takes an object as input and converts the class variables to array key/vals
1570 *
1571 * @access public
1572 * @param object
1573 * @return array
1574 */
1575 function _object_to_array($object)
1576 {
Derek Jones0b59f272008-05-13 04:22:33 +00001577 if ( ! is_object($object))
Derek Allard09de1852007-02-14 01:35:56 +00001578 {
1579 return $object;
1580 }
1581
1582 $array = array();
1583 foreach (get_object_vars($object) as $key => $val)
1584 {
Derek Allard848b7762007-12-31 16:43:05 +00001585 // There are some built in keys we need to ignore for this conversion
Derek Jones0b59f272008-05-13 04:22:33 +00001586 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
Derek Allard09de1852007-02-14 01:35:56 +00001587 {
1588 $array[$key] = $val;
1589 }
1590 }
1591
1592 return $array;
1593 }
1594
1595 // --------------------------------------------------------------------
1596
1597 /**
Derek Allard9b3e7b52008-02-04 23:20:34 +00001598 * Start Cache
1599 *
1600 * Starts AR caching
1601 *
1602 * @access public
1603 * @return void
1604 */
1605 function start_cache()
1606 {
1607 $this->ar_caching = TRUE;
1608 }
1609
1610 // --------------------------------------------------------------------
1611
1612 /**
1613 * Stop Cache
1614 *
1615 * Stops AR caching
1616 *
1617 * @access public
1618 * @return void
1619 */
1620 function stop_cache()
1621 {
1622 $this->ar_caching = FALSE;
1623 }
1624
Derek Allard9b3e7b52008-02-04 23:20:34 +00001625 // --------------------------------------------------------------------
1626
1627 /**
1628 * Flush Cache
1629 *
1630 * Empties the AR cache
1631 *
1632 * @access public
1633 * @return void
1634 */
1635 function flush_cache()
1636 {
Rick Ellis65837c72008-10-18 06:39:49 +00001637 $this->_reset_run(
1638 array(
Rick Ellis59523592008-10-17 04:07:40 +00001639 '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(),
Rick Ellis23012592008-10-18 02:11:06 +00001645 'ar_cache_having' => array(),
Rick Ellis59523592008-10-17 04:07:40 +00001646 'ar_cache_orderby' => array(),
Rick Ellis23012592008-10-18 02:11:06 +00001647 'ar_cache_set' => array(),
1648 'ar_cache_exists' => array()
Rick Ellis65837c72008-10-18 06:39:49 +00001649 )
1650 );
Derek Allard9b3e7b52008-02-04 23:20:34 +00001651 }
1652
1653 // --------------------------------------------------------------------
1654
1655 /**
1656 * Merge Cache
1657 *
1658 * When called, this function merges any cached AR arrays with
1659 * locally called ones.
1660 *
1661 * @access private
1662 * @return void
1663 */
1664 function _merge_cache()
1665 {
Rick Ellis392f09d2008-10-18 02:03:14 +00001666 if (count($this->ar_cache_exists) == 0)
Rick Ellis59523592008-10-17 04:07:40 +00001667 {
1668 return;
1669 }
Rick Ellis392f09d2008-10-18 02:03:14 +00001670
1671 foreach ($this->ar_cache_exists as $val)
Derek Allard9b3e7b52008-02-04 23:20:34 +00001672 {
Rick Ellis000f89d2008-10-17 23:26:15 +00001673 $ar_variable = 'ar_'.$val;
1674 $ar_cache_var = 'ar_cache_'.$val;
1675
1676 if (count($this->$ar_cache_var) == 0)
1677 {
1678 continue;
1679 }
Rick Ellis392f09d2008-10-18 02:03:14 +00001680
Rick Ellis000f89d2008-10-17 23:26:15 +00001681 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
Derek Allard9b3e7b52008-02-04 23:20:34 +00001682 }
Rick Ellis392f09d2008-10-18 02:03:14 +00001683
Rick Ellis59523592008-10-17 04:07:40 +00001684 // If we are "protecting identifiers" we need to examine the "from"
1685 // portion of the query to determine if there are any aliases
Rick Ellis392f09d2008-10-18 02:03:14 +00001686 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
Rick Ellis59523592008-10-17 04:07:40 +00001687 {
1688 $this->_track_aliases($this->ar_from);
1689 }
Derek Allard9b3e7b52008-02-04 23:20:34 +00001690 }
1691
1692 // --------------------------------------------------------------------
1693
1694 /**
1695 * Resets the active record values. Called by the get() function
1696 *
1697 * @access private
1698 * @param array An array of fields to reset
1699 * @return void
1700 */
1701 function _reset_run($ar_reset_items)
1702 {
1703 foreach ($ar_reset_items as $item => $default_value)
1704 {
Derek Jones0b59f272008-05-13 04:22:33 +00001705 if ( ! in_array($item, $this->ar_store_array))
Derek Allard9b3e7b52008-02-04 23:20:34 +00001706 {
1707 $this->$item = $default_value;
1708 }
1709 }
1710 }
Rick Ellis59523592008-10-17 04:07:40 +00001711
Derek Allard9b3e7b52008-02-04 23:20:34 +00001712 // --------------------------------------------------------------------
1713
1714 /**
Derek Allard09de1852007-02-14 01:35:56 +00001715 * Resets the active record values. Called by the get() function
1716 *
1717 * @access private
1718 * @return void
1719 */
1720 function _reset_select()
1721 {
Derek Allard9b3e7b52008-02-04 23:20:34 +00001722 $ar_reset_items = array(
Rick Ellis59523592008-10-17 04:07:40 +00001723 '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,
1737 );
Derek Allard9b3e7b52008-02-04 23:20:34 +00001738
1739 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001740 }
1741
1742 // --------------------------------------------------------------------
1743
1744 /**
1745 * Resets the active record "write" values.
1746 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001747 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001748 *
1749 * @access private
1750 * @return void
1751 */
1752 function _reset_write()
Derek Allard9b3e7b52008-02-04 23:20:34 +00001753 {
1754 $ar_reset_items = array(
Rick Ellis59523592008-10-17 04:07:40 +00001755 'ar_set' => array(),
1756 'ar_from' => array(),
1757 'ar_where' => array(),
1758 'ar_like' => array(),
1759 'ar_orderby' => array(),
1760 'ar_limit' => FALSE,
1761 'ar_order' => FALSE
1762 );
Derek Allard9b3e7b52008-02-04 23:20:34 +00001763
1764 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001765 }
1766
1767}
Derek Allarda6325892008-05-12 17:51:47 +00001768
1769/* End of file DB_active_rec.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00001770/* Location: ./system/database/DB_active_rec.php */