blob: e7920d083e9819303298c62eea6d7e71c54feaef [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
31 var $ar_select = array();
32 var $ar_distinct = FALSE;
33 var $ar_from = array();
34 var $ar_join = array();
35 var $ar_where = array();
36 var $ar_like = array();
37 var $ar_groupby = array();
38 var $ar_having = array();
39 var $ar_limit = FALSE;
40 var $ar_offset = FALSE;
41 var $ar_order = FALSE;
42 var $ar_orderby = array();
43 var $ar_set = array();
Derek Allard80dd7022007-12-18 23:55:06 +000044 var $ar_wherein = array();
Derek Allard5e128942007-12-28 21:33:03 +000045 var $ar_aliased_tables = array();
Derek Allard9b3e7b52008-02-04 23:20:34 +000046 var $ar_store_array = array();
47
48 // Active Record Caching variables
49 var $ar_caching = FALSE;
50 var $ar_cache_select = array();
51 var $ar_cache_from = array();
52 var $ar_cache_join = array();
53 var $ar_cache_where = array();
54 var $ar_cache_like = array();
55 var $ar_cache_groupby = array();
56 var $ar_cache_having = array();
57 var $ar_cache_limit = FALSE;
58 var $ar_cache_offset = FALSE;
59 var $ar_cache_order = FALSE;
60 var $ar_cache_orderby = array();
61 var $ar_cache_set = array();
62
Derek Allard09de1852007-02-14 01:35:56 +000063
Derek Allard09de1852007-02-14 01:35:56 +000064 /**
Derek Allard3b118682008-01-22 23:44:32 +000065 * DB Prefix
66 *
67 * Prepends a database prefix if one exists in configuration
68 *
69 * @access public
70 * @param string the table
71 * @return string
Derek Allard5fe155e2008-05-12 19:14:57 +000072 */
Derek Allard3b118682008-01-22 23:44:32 +000073 function dbprefix($table = '')
74 {
75 if ($table == '')
76 {
77 $this->display_error('db_table_name_required');
78 }
Derek Allard5fe155e2008-05-12 19:14:57 +000079
Derek Allard3b118682008-01-22 23:44:32 +000080 return $this->dbprefix.$table;
81 }
82
83 // --------------------------------------------------------------------
84
85 /**
Derek Allard09de1852007-02-14 01:35:56 +000086 * Select
87 *
88 * Generates the SELECT portion of the query
89 *
90 * @access public
91 * @param string
92 * @return object
93 */
Derek Allard39b622d2008-01-16 21:10:09 +000094 function select($select = '*', $protect_identifiers = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +000095 {
96 if (is_string($select))
97 {
Derek Allarda6325892008-05-12 17:51:47 +000098 if ($protect_identifiers !== FALSE)
99 {
100 $select = explode(',', $select);
101 }
102 else
103 {
104 $select = array($select);
105 }
Derek Allard09de1852007-02-14 01:35:56 +0000106 }
Derek Allard5fe155e2008-05-12 19:14:57 +0000107
Derek Allard09de1852007-02-14 01:35:56 +0000108 foreach ($select as $val)
109 {
110 $val = trim($val);
Derek Allard39b622d2008-01-16 21:10:09 +0000111
112 if ($val != '*' && $protect_identifiers !== FALSE)
113 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000114 if (strpos($val, '.') !== FALSE)
115 {
116 $val = $this->dbprefix.$val;
117 }
118 else
119 {
120 $val = $this->_protect_identifiers($val);
121 }
Derek Allard39b622d2008-01-16 21:10:09 +0000122 }
Derek Allard5fe155e2008-05-12 19:14:57 +0000123
Derek Allard09de1852007-02-14 01:35:56 +0000124 if ($val != '')
Derek Allard39b622d2008-01-16 21:10:09 +0000125 {
Derek Allard09de1852007-02-14 01:35:56 +0000126 $this->ar_select[] = $val;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000127 if ($this->ar_caching === TRUE)
128 {
129 $this->ar_cache_select[] = $val;
130 }
Derek Allard39b622d2008-01-16 21:10:09 +0000131 }
Derek Allard09de1852007-02-14 01:35:56 +0000132 }
133 return $this;
134 }
Derek Allard39b622d2008-01-16 21:10:09 +0000135
136 // --------------------------------------------------------------------
137
138 /**
139 * Select Max
140 *
141 * Generates a SELECT MAX(field) portion of a query
142 *
143 * @access public
144 * @param string the field
145 * @param string an alias
146 * @return object
147 */
148 function select_max($select = '', $alias='')
149 {
Derek Jones0b59f272008-05-13 04:22:33 +0000150 if ( ! is_string($select) OR $select == '')
Derek Allard39b622d2008-01-16 21:10:09 +0000151 {
152 $this->display_error('db_invalid_query');
153 }
Derek Allard09de1852007-02-14 01:35:56 +0000154
Derek Allard39b622d2008-01-16 21:10:09 +0000155 $alias = ($alias != '') ? $alias : $select;
156
157 $sql = 'MAX('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
158
159 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000160 if ($this->ar_caching === TRUE)
161 {
162 $this->ar_cache_select[] = $sql;
163 }
Derek Allard39b622d2008-01-16 21:10:09 +0000164
165 return $this;
Derek Allard39b622d2008-01-16 21:10:09 +0000166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Select Min
172 *
173 * Generates a SELECT MIN(field) portion of a query
174 *
175 * @access public
176 * @param string the field
177 * @param string an alias
178 * @return object
179 */
180 function select_min($select = '', $alias='')
181 {
Derek Jones0b59f272008-05-13 04:22:33 +0000182 if ( ! is_string($select) OR $select == '')
Derek Allard39b622d2008-01-16 21:10:09 +0000183 {
184 $this->display_error('db_invalid_query');
185 }
Derek Allard5fe155e2008-05-12 19:14:57 +0000186
Derek Allard39b622d2008-01-16 21:10:09 +0000187 $alias = ($alias != '') ? $alias : $select;
188
189 $sql = 'MIN('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
190
191 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000192 if ($this->ar_caching === TRUE)
193 {
194 $this->ar_cache_select[] = $sql;
195 }
196
Derek Allard39b622d2008-01-16 21:10:09 +0000197 return $this;
198 }
199
200 // --------------------------------------------------------------------
201
202 /**
203 * Select Average
204 *
205 * Generates a SELECT AVG(field) portion of a query
206 *
207 * @access public
208 * @param string the field
209 * @param string an alias
210 * @return object
211 */
212 function select_avg($select = '', $alias='')
213 {
Derek Jones0b59f272008-05-13 04:22:33 +0000214 if ( ! is_string($select) OR $select == '')
Derek Allard39b622d2008-01-16 21:10:09 +0000215 {
216 $this->display_error('db_invalid_query');
217 }
218
219 $alias = ($alias != '') ? $alias : $select;
Derek Allard5fe155e2008-05-12 19:14:57 +0000220
Derek Allard39b622d2008-01-16 21:10:09 +0000221 $sql = 'AVG('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
222
223 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000224 if ($this->ar_caching === TRUE)
225 {
226 $this->ar_cache_select[] = $sql;
227 }
228
Derek Allard39b622d2008-01-16 21:10:09 +0000229 return $this;
230 }
231
232 // --------------------------------------------------------------------
Derek Allard5fe155e2008-05-12 19:14:57 +0000233
Derek Allard39b622d2008-01-16 21:10:09 +0000234 /**
235 * Select Sum
236 *
237 * Generates a SELECT SUM(field) portion of a query
238 *
239 * @access public
240 * @param string the field
241 * @param string an alias
242 * @return object
243 */
244 function select_sum($select = '', $alias='')
245 {
Derek Jones0b59f272008-05-13 04:22:33 +0000246 if ( ! is_string($select) OR $select == '')
Derek Allard39b622d2008-01-16 21:10:09 +0000247 {
248 $this->display_error('db_invalid_query');
249 }
250
251 $alias = ($alias != '') ? $alias : $select;
252
253 $sql = 'SUM('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
254
255 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000256 if ($this->ar_caching === TRUE)
257 {
258 $this->ar_cache_select[] = $sql;
259 }
260
Derek Allard39b622d2008-01-16 21:10:09 +0000261 return $this;
262 }
263
Derek Allard09de1852007-02-14 01:35:56 +0000264 // --------------------------------------------------------------------
265
266 /**
267 * DISTINCT
268 *
269 * Sets a flag which tells the query string compiler to add DISTINCT
270 *
271 * @access public
272 * @param bool
273 * @return object
274 */
275 function distinct($val = TRUE)
276 {
277 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
278 return $this;
279 }
280
281 // --------------------------------------------------------------------
282
283 /**
284 * From
285 *
286 * Generates the FROM portion of the query
287 *
288 * @access public
289 * @param mixed can be a string or array
290 * @return object
291 */
292 function from($from)
293 {
294 foreach ((array)$from as $val)
295 {
Derek Allard39b622d2008-01-16 21:10:09 +0000296 $this->ar_from[] = $this->_protect_identifiers($this->_track_aliases($val));
Derek Allard9b3e7b52008-02-04 23:20:34 +0000297 if ($this->ar_caching === TRUE)
298 {
Derek Allard9a4d1da2008-02-25 14:18:38 +0000299 $this->ar_cache_from[] = $this->_protect_identifiers($this->_track_aliases($val));
Derek Allard9b3e7b52008-02-04 23:20:34 +0000300 }
Derek Allard09de1852007-02-14 01:35:56 +0000301 }
Derek Allard5e128942007-12-28 21:33:03 +0000302
Derek Allard09de1852007-02-14 01:35:56 +0000303 return $this;
304 }
Derek Allard5fe155e2008-05-12 19:14:57 +0000305
Derek Allard09de1852007-02-14 01:35:56 +0000306 // --------------------------------------------------------------------
307
308 /**
309 * Join
310 *
311 * Generates the JOIN portion of the query
312 *
313 * @access public
314 * @param string
315 * @param string the join condition
316 * @param string the type of join
317 * @return object
318 */
319 function join($table, $cond, $type = '')
320 {
321 if ($type != '')
322 {
323 $type = strtoupper(trim($type));
324
Derek Jones0b59f272008-05-13 04:22:33 +0000325 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
Derek Allard09de1852007-02-14 01:35:56 +0000326 {
327 $type = '';
328 }
329 else
330 {
331 $type .= ' ';
332 }
333 }
334
Derek Allard09de1852007-02-14 01:35:56 +0000335 // If a DB prefix is used we might need to add it to the column names
336 if ($this->dbprefix)
337 {
Derek Allard39b622d2008-01-16 21:10:09 +0000338 $this->_track_aliases($table);
339
Derek Allard09de1852007-02-14 01:35:56 +0000340 // First we remove any existing prefixes in the condition to avoid duplicates
341 $cond = preg_replace('|('.$this->dbprefix.')([\w\.]+)([\W\s]+)|', "$2$3", $cond);
Derek Allard5fe155e2008-05-12 19:14:57 +0000342
Derek Allard09de1852007-02-14 01:35:56 +0000343 // Next we add the prefixes to the condition
344 $cond = preg_replace('|([\w\.]+)([\W\s]+)(.+)|', $this->dbprefix . "$1$2" . $this->dbprefix . "$3", $cond);
Derek Allard5fe155e2008-05-12 19:14:57 +0000345 }
Derek Allard5e128942007-12-28 21:33:03 +0000346
Derek Allard9b3e7b52008-02-04 23:20:34 +0000347 $join = $type.'JOIN '.$this->_protect_identifiers($this->dbprefix.$table, TRUE).' ON '.$cond;
348
349 $this->ar_join[] = $join;
350 if ($this->ar_caching === TRUE)
351 {
352 $this->ar_cache_join[] = $join;
353 }
354
Derek Allard09de1852007-02-14 01:35:56 +0000355 return $this;
356 }
Derek Allard5fe155e2008-05-12 19:14:57 +0000357
Derek Allard09de1852007-02-14 01:35:56 +0000358 // --------------------------------------------------------------------
359
360 /**
361 * Where
362 *
363 * Generates the WHERE portion of the query. Separates
364 * multiple calls with AND
365 *
366 * @access public
367 * @param mixed
368 * @param mixed
369 * @return object
370 */
Derek Allard39b622d2008-01-16 21:10:09 +0000371 function where($key, $value = NULL, $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000372 {
Derek Allard39b622d2008-01-16 21:10:09 +0000373 return $this->_where($key, $value, 'AND ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000374 }
375
376 // --------------------------------------------------------------------
377
378 /**
379 * OR Where
380 *
381 * Generates the WHERE portion of the query. Separates
382 * multiple calls with OR
383 *
384 * @access public
385 * @param mixed
386 * @param mixed
387 * @return object
388 */
Derek Allard39b622d2008-01-16 21:10:09 +0000389 function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000390 {
Derek Allard39b622d2008-01-16 21:10:09 +0000391 return $this->_where($key, $value, 'OR ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000392 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000393
394 // --------------------------------------------------------------------
395
396 /**
397 * orwhere() is an alias of or_where()
398 * this function is here for backwards compatibility, as
399 * orwhere() has been deprecated
400 */
Derek Allard39b622d2008-01-16 21:10:09 +0000401 function orwhere($key, $value = NULL, $escape = TRUE)
Derek Allard218e2bc2007-12-17 21:18:14 +0000402 {
Derek Allard39b622d2008-01-16 21:10:09 +0000403 return $this->or_where($key, $value, $escape);
Derek Allard218e2bc2007-12-17 21:18:14 +0000404 }
Derek Allard67b44ed2008-01-12 16:18:02 +0000405
406 // --------------------------------------------------------------------
407
408 /**
Derek Allard09de1852007-02-14 01:35:56 +0000409 * Where
410 *
411 * Called by where() or orwhere()
412 *
413 * @access private
414 * @param mixed
415 * @param mixed
416 * @param string
417 * @return object
418 */
Derek Allard39b622d2008-01-16 21:10:09 +0000419 function _where($key, $value = NULL, $type = 'AND ', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000420 {
Derek Jones0b59f272008-05-13 04:22:33 +0000421 if ( ! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000422 {
423 $key = array($key => $value);
424 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000425
Derek Allard09de1852007-02-14 01:35:56 +0000426 foreach ($key as $k => $v)
427 {
428 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Derek Allard15ddc9d2007-12-20 13:54:39 +0000429
Derek Allardd8364c42008-06-04 17:01:00 +0000430 if (is_null($v) && ! $this->_has_operator($k))
Derek Allard15ddc9d2007-12-20 13:54:39 +0000431 {
432 // value appears not to have been set, assign the test to IS NULL
433 $k .= ' IS NULL';
434 }
Derek Allard09de1852007-02-14 01:35:56 +0000435
Derek Jones0b59f272008-05-13 04:22:33 +0000436 if ( ! is_null($v))
Derek Allard09de1852007-02-14 01:35:56 +0000437 {
Derek Allard39b622d2008-01-16 21:10:09 +0000438 if ($escape === TRUE)
439 {
440 // exception for "field<=" keys
441 if ($this->_has_operator($k))
442 {
Rick Ellis9f02e3c2008-09-30 21:51:01 +0000443 $k = preg_replace("/([A-Za-z_0-9]+)/", $this->_escape_column($this->_protect_identifiers('$1')), $k);
Derek Allard39b622d2008-01-16 21:10:09 +0000444 }
445 else
446 {
447 $k = $this->_protect_identifiers($k);
448 }
Derek Allardd8364c42008-06-04 17:01:00 +0000449
450 $v = ' '.$this->escape($v);
Derek Allard39b622d2008-01-16 21:10:09 +0000451 }
452
Derek Jones0b59f272008-05-13 04:22:33 +0000453 if ( ! $this->_has_operator($k))
Derek Allard09de1852007-02-14 01:35:56 +0000454 {
455 $k .= ' =';
456 }
Derek Allard09de1852007-02-14 01:35:56 +0000457 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000458 else
459 {
Derek Allard16629b12008-04-06 19:58:20 +0000460 if ($escape === TRUE)
461 {
462 $k = $this->_protect_identifiers($k, TRUE);
Derek Jonesc06f58e2008-06-04 17:04:48 +0000463 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000464 }
465
Derek Allard09de1852007-02-14 01:35:56 +0000466 $this->ar_where[] = $prefix.$k.$v;
Derek Allardd8364c42008-06-04 17:01:00 +0000467
Derek Allard9b3e7b52008-02-04 23:20:34 +0000468 if ($this->ar_caching === TRUE)
469 {
470 $this->ar_cache_where[] = $prefix.$k.$v;
471 }
472
Derek Allard09de1852007-02-14 01:35:56 +0000473 }
Derek Jones7e98a272008-06-04 17:05:44 +0000474
Derek Allard09de1852007-02-14 01:35:56 +0000475 return $this;
476 }
Derek Allard80dd7022007-12-18 23:55:06 +0000477
478 // --------------------------------------------------------------------
479
480 /**
481 * Where_in
482 *
Derek Allardc6935512007-12-19 14:23:19 +0000483 * Generates a WHERE field IN ('item', 'item') SQL query joined with
484 * AND if appropriate
Derek Allard80dd7022007-12-18 23:55:06 +0000485 *
486 * @access public
487 * @param string The field to search
488 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000489 * @return object
490 */
491 function where_in($key = NULL, $values = NULL)
492 {
493 return $this->_where_in($key, $values);
494 }
495
496 // --------------------------------------------------------------------
497
498 /**
499 * Where_in_or
500 *
501 * Generates a WHERE field IN ('item', 'item') SQL query joined with
502 * OR if appropriate
503 *
504 * @access public
505 * @param string The field to search
506 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000507 * @return object
508 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000509 function or_where_in($key = NULL, $values = NULL)
Derek Allard5fe155e2008-05-12 19:14:57 +0000510 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000511 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000512 }
513
514 // --------------------------------------------------------------------
515
516 /**
517 * Where_not_in
518 *
519 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
520 * with AND if appropriate
521 *
522 * @access public
523 * @param string The field to search
524 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000525 * @return object
526 */
527 function where_not_in($key = NULL, $values = NULL)
Derek Allard5fe155e2008-05-12 19:14:57 +0000528 {
Derek Allardc6935512007-12-19 14:23:19 +0000529 return $this->_where_in($key, $values, TRUE);
530 }
531
532 // --------------------------------------------------------------------
533
534 /**
535 * Where_not_in_or
536 *
537 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
538 * with OR if appropriate
539 *
540 * @access public
541 * @param string The field to search
542 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000543 * @return object
544 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000545 function or_where_not_in($key = NULL, $values = NULL)
Derek Allard5fe155e2008-05-12 19:14:57 +0000546 {
Derek Jonesd0072432008-05-07 22:06:51 +0000547 return $this->_where_in($key, $values, TRUE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000548 }
549
550 // --------------------------------------------------------------------
551
552 /**
553 * Where_in
554 *
555 * Called by where_in, where_in_or, where_not_in, where_not_in_or
556 *
557 * @access public
558 * @param string The field to search
559 * @param array The values searched on
560 * @param boolean If the statement whould be IN or NOT IN
Derek Allard80dd7022007-12-18 23:55:06 +0000561 * @param string
562 * @return object
563 */
Derek Allard15ddc9d2007-12-20 13:54:39 +0000564 function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard5fe155e2008-05-12 19:14:57 +0000565 {
Derek Jones0b59f272008-05-13 04:22:33 +0000566 if ($key === NULL OR ! is_array($values))
Derek Allard80dd7022007-12-18 23:55:06 +0000567 {
568 return;
569 }
570
Derek Allardc6935512007-12-19 14:23:19 +0000571 $not = ($not) ? ' NOT ' : '';
Derek Allard80dd7022007-12-18 23:55:06 +0000572
573 foreach ($values as $value)
574 {
575 $this->ar_wherein[] = $this->escape($value);
576 }
577
578 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Derek Allard5fe155e2008-05-12 19:14:57 +0000579
Derek Allard9b3e7b52008-02-04 23:20:34 +0000580 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
581
582 $this->ar_where[] = $where_in;
583 if ($this->ar_caching === TRUE)
584 {
585 $this->ar_cache_where[] = $where_in;
586 }
Derek Allard80dd7022007-12-18 23:55:06 +0000587
Derek Allard8f000212008-01-18 14:45:59 +0000588 // reset the array for multiple calls
589 $this->ar_wherein = array();
Derek Allard80dd7022007-12-18 23:55:06 +0000590 return $this;
591 }
592
Derek Allard09de1852007-02-14 01:35:56 +0000593 // --------------------------------------------------------------------
594
595 /**
596 * Like
597 *
598 * Generates a %LIKE% portion of the query. Separates
599 * multiple calls with AND
600 *
601 * @access public
602 * @param mixed
603 * @param mixed
604 * @return object
605 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000606 function like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000607 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000608 return $this->_like($field, $match, 'AND ', $side);
Derek Allard09de1852007-02-14 01:35:56 +0000609 }
Derek Allarde54e3d22007-12-19 15:53:44 +0000610
611 // --------------------------------------------------------------------
612
613 /**
614 * Not Like
615 *
616 * Generates a NOT LIKE portion of the query. Separates
617 * multiple calls with AND
618 *
619 * @access public
620 * @param mixed
621 * @param mixed
622 * @return object
623 */
624 function not_like($field, $match = '', $side = 'both')
625 {
626 return $this->_like($field, $match, 'AND ', $side, ' NOT');
627 }
628
Derek Allard09de1852007-02-14 01:35:56 +0000629 // --------------------------------------------------------------------
630
631 /**
632 * OR Like
633 *
634 * Generates a %LIKE% portion of the query. Separates
635 * multiple calls with OR
636 *
637 * @access public
638 * @param mixed
639 * @param mixed
640 * @return object
641 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000642 function or_like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000643 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000644 return $this->_like($field, $match, 'OR ', $side);
645 }
646
647 // --------------------------------------------------------------------
648
649 /**
Derek Allarde54e3d22007-12-19 15:53:44 +0000650 * OR Not Like
651 *
652 * Generates a NOT LIKE portion of the query. Separates
653 * multiple calls with OR
654 *
655 * @access public
656 * @param mixed
657 * @param mixed
658 * @return object
659 */
660 function or_not_like($field, $match = '', $side = 'both')
661 {
662 return $this->_like($field, $match, 'OR ', $side, 'NOT ');
663 }
664
665 // --------------------------------------------------------------------
666
667 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000668 * orlike() is an alias of or_like()
669 * this function is here for backwards compatibility, as
670 * orlike() has been deprecated
671 */
672 function orlike($field, $match = '', $side = 'both')
673 {
Derek Allard4a310b72008-01-30 21:32:47 +0000674 return $this->or_like($field, $match, $side);
Derek Allard09de1852007-02-14 01:35:56 +0000675 }
676
677 // --------------------------------------------------------------------
678
679 /**
680 * Like
681 *
682 * Called by like() or orlike()
683 *
684 * @access private
685 * @param mixed
686 * @param mixed
687 * @param string
688 * @return object
689 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000690 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard09de1852007-02-14 01:35:56 +0000691 {
Derek Jones0b59f272008-05-13 04:22:33 +0000692 if ( ! is_array($field))
Derek Allard09de1852007-02-14 01:35:56 +0000693 {
694 $field = array($field => $match);
695 }
696
697 foreach ($field as $k => $v)
Derek Allarde54e3d22007-12-19 15:53:44 +0000698 {
699
Derek Allard39b622d2008-01-16 21:10:09 +0000700 $k = $this->_protect_identifiers($k);
701
Derek Allard09de1852007-02-14 01:35:56 +0000702 $prefix = (count($this->ar_like) == 0) ? '' : $type;
Derek Allarde54e3d22007-12-19 15:53:44 +0000703
Rick Ellis852160a2008-09-30 21:55:34 +0000704 $v = $this->escape($v);
Derek Allarde54e3d22007-12-19 15:53:44 +0000705
Derek Allard218e2bc2007-12-17 21:18:14 +0000706 if ($side == 'before')
707 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000708 $like_statement = $prefix." $k $not LIKE '%{$v}'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000709 }
710 elseif ($side == 'after')
711 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000712 $like_statement = $prefix." $k $not LIKE '{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000713 }
714 else
715 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000716 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000717 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000718
719 $this->ar_like[] = $like_statement;
720 if ($this->ar_caching === TRUE)
721 {
722 $this->ar_cache_like[] = $like_statement;
723 }
724
Derek Allard09de1852007-02-14 01:35:56 +0000725 }
726 return $this;
727 }
728
729 // --------------------------------------------------------------------
730
731 /**
732 * GROUP BY
733 *
734 * @access public
735 * @param string
736 * @return object
737 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000738 function group_by($by)
Derek Allard09de1852007-02-14 01:35:56 +0000739 {
740 if (is_string($by))
741 {
742 $by = explode(',', $by);
743 }
744
745 foreach ($by as $val)
746 {
747 $val = trim($val);
748
749 if ($val != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000750 {
Derek Allard39b622d2008-01-16 21:10:09 +0000751 $this->ar_groupby[] = $this->_protect_identifiers($val);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000752 if ($this->ar_caching === TRUE)
753 {
754 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
755 }
756 }
Derek Allard09de1852007-02-14 01:35:56 +0000757 }
758 return $this;
759 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000760
761 // --------------------------------------------------------------------
762
763 /**
764 * groupby() is an alias of group_by()
765 * this function is here for backwards compatibility, as
766 * groupby() has been deprecated
767 */
768 function groupby($by)
769 {
770 return $this->group_by($by);
771 }
772
Derek Allard09de1852007-02-14 01:35:56 +0000773 // --------------------------------------------------------------------
774
775 /**
776 * Sets the HAVING value
777 *
778 * Separates multiple calls with AND
779 *
780 * @access public
781 * @param string
782 * @param string
783 * @return object
784 */
Derek Allarde808aac2008-04-06 18:22:00 +0000785 function having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000786 {
Derek Allarde808aac2008-04-06 18:22:00 +0000787 return $this->_having($key, $value, 'AND ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000788 }
Derek Allard43e8cbf2008-03-01 23:14:43 +0000789
790 // --------------------------------------------------------------------
791
792 /**
793 * orhaving() is an alias of or_having()
794 * this function is here for backwards compatibility, as
795 * orhaving() has been deprecated
796 */
797
Derek Allarde808aac2008-04-06 18:22:00 +0000798 function orhaving($key, $value = '', $escape = TRUE)
Derek Allard43e8cbf2008-03-01 23:14:43 +0000799 {
Derek Allarda459b462008-05-22 13:01:39 +0000800 return $this->or_having($key, $value, $escape);
Derek Allard43e8cbf2008-03-01 23:14:43 +0000801 }
Derek Allard09de1852007-02-14 01:35:56 +0000802 // --------------------------------------------------------------------
803
804 /**
805 * Sets the OR HAVING value
806 *
807 * Separates multiple calls with OR
808 *
809 * @access public
810 * @param string
811 * @param string
812 * @return object
813 */
Derek Allarde808aac2008-04-06 18:22:00 +0000814 function or_having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000815 {
Derek Allarde808aac2008-04-06 18:22:00 +0000816 return $this->_having($key, $value, 'OR ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000817 }
818
819 // --------------------------------------------------------------------
820
821 /**
822 * Sets the HAVING values
823 *
Rick Ellisff734012008-09-30 20:38:12 +0000824 * Called by having() or or_having()
Derek Allard09de1852007-02-14 01:35:56 +0000825 *
826 * @access private
827 * @param string
828 * @param string
829 * @return object
830 */
Derek Allarde808aac2008-04-06 18:22:00 +0000831 function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000832 {
Derek Jones0b59f272008-05-13 04:22:33 +0000833 if ( ! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000834 {
835 $key = array($key => $value);
836 }
837
838 foreach ($key as $k => $v)
839 {
840 $prefix = (count($this->ar_having) == 0) ? '' : $type;
Derek Allarde808aac2008-04-06 18:22:00 +0000841
842 if ($escape === TRUE)
843 {
844 $k = $this->_protect_identifiers($k);
845 }
846
Derek Allarda459b462008-05-22 13:01:39 +0000847 if ( ! $this->_has_operator($k))
848 {
849 $k .= ' = ';
850 }
851
Derek Allard09de1852007-02-14 01:35:56 +0000852 if ($v != '')
853 {
Rick Ellis852160a2008-09-30 21:55:34 +0000854 $v = ' '.$this->escape($v);
Derek Allard09de1852007-02-14 01:35:56 +0000855 }
856
857 $this->ar_having[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000858 if ($this->ar_caching === TRUE)
859 {
860 $this->ar_cache_having[] = $prefix.$k.$v;
861 }
Derek Allard09de1852007-02-14 01:35:56 +0000862 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000863
Derek Allard09de1852007-02-14 01:35:56 +0000864 return $this;
865 }
866
867 // --------------------------------------------------------------------
868
869 /**
870 * Sets the ORDER BY value
871 *
872 * @access public
873 * @param string
874 * @param string direction: asc or desc
875 * @return object
876 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000877 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000878 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000879 if (strtolower($direction) == 'random')
880 {
881 $orderby = ''; // Random results want or don't need a field name
882 $direction = $this->_random_keyword;
883 }
884 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000885 {
Derek Allard92782492007-08-10 11:26:01 +0000886 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000887 }
888
Derek Allard9b3e7b52008-02-04 23:20:34 +0000889 $orderby_statement = $this->_protect_identifiers($orderby, TRUE).$direction;
890
891 $this->ar_orderby[] = $orderby_statement;
892 if ($this->ar_caching === TRUE)
893 {
894 $this->ar_cache_orderby[] = $orderby_statement;
895 }
896
Derek Allard09de1852007-02-14 01:35:56 +0000897 return $this;
898 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000899
Derek Allard218e2bc2007-12-17 21:18:14 +0000900 // --------------------------------------------------------------------
901
902 /**
903 * orderby() is an alias of order_by()
904 * this function is here for backwards compatibility, as
905 * orderby() has been deprecated
906 */
907 function orderby($orderby, $direction = '')
908 {
909 return $this->order_by($orderby, $direction);
910 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000911
Derek Allard09de1852007-02-14 01:35:56 +0000912 // --------------------------------------------------------------------
913
914 /**
915 * Sets the LIMIT value
916 *
917 * @access public
918 * @param integer the limit value
919 * @param integer the offset value
920 * @return object
921 */
922 function limit($value, $offset = '')
923 {
924 $this->ar_limit = $value;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000925 if ($this->ar_caching === TRUE)
926 {
927 $this->ar_cache_limit[] = $value;
928 }
929
Derek Allard09de1852007-02-14 01:35:56 +0000930 if ($offset != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000931 {
Derek Allard09de1852007-02-14 01:35:56 +0000932 $this->ar_offset = $offset;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000933 if ($this->ar_caching === TRUE)
934 {
935 $this->ar_cache_offset[] = $offset;
936 }
937 }
Derek Allard09de1852007-02-14 01:35:56 +0000938
939 return $this;
940 }
941
942 // --------------------------------------------------------------------
943
944 /**
945 * Sets the OFFSET value
946 *
947 * @access public
948 * @param integer the offset value
949 * @return object
950 */
Derek Allard9b3e7b52008-02-04 23:20:34 +0000951 function offset($offset)
Derek Allard09de1852007-02-14 01:35:56 +0000952 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000953 $this->ar_offset = $offset;
954 if ($this->ar_caching === TRUE)
955 {
956 $this->ar_cache_offset[] = $offset;
957 }
958
Derek Allard09de1852007-02-14 01:35:56 +0000959 return $this;
960 }
961
962 // --------------------------------------------------------------------
963
964 /**
965 * The "set" function. Allows key/value pairs to be set for inserting or updating
966 *
967 * @access public
968 * @param mixed
969 * @param string
Derek Allard39b622d2008-01-16 21:10:09 +0000970 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +0000971 * @return object
972 */
Derek Allard39b622d2008-01-16 21:10:09 +0000973 function set($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000974 {
975 $key = $this->_object_to_array($key);
976
Derek Jones0b59f272008-05-13 04:22:33 +0000977 if ( ! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000978 {
979 $key = array($key => $value);
980 }
981
982 foreach ($key as $k => $v)
983 {
Derek Allard39b622d2008-01-16 21:10:09 +0000984 if ($escape === FALSE)
985 {
986 $this->ar_set[$this->_protect_identifiers($k)] = $v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000987 if ($this->ar_caching === TRUE)
988 {
989 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $v;
990 }
Derek Allard39b622d2008-01-16 21:10:09 +0000991 }
992 else
993 {
994 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000995 if ($this->ar_caching === TRUE)
996 {
997 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $this->escape($v);
998 }
Derek Allard39b622d2008-01-16 21:10:09 +0000999 }
Derek Allard09de1852007-02-14 01:35:56 +00001000 }
1001
1002 return $this;
1003 }
1004
1005 // --------------------------------------------------------------------
1006
1007 /**
1008 * Get
1009 *
1010 * Compiles the select statement based on the other functions called
1011 * and runs the query
1012 *
1013 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +00001014 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +00001015 * @param string the limit clause
1016 * @param string the offset clause
1017 * @return object
1018 */
1019 function get($table = '', $limit = null, $offset = null)
1020 {
1021 if ($table != '')
1022 {
Derek Allard5e128942007-12-28 21:33:03 +00001023 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001024 $this->from($table);
1025 }
1026
Derek Jones0b59f272008-05-13 04:22:33 +00001027 if ( ! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001028 {
1029 $this->limit($limit, $offset);
1030 }
1031
1032 $sql = $this->_compile_select();
1033
1034 $result = $this->query($sql);
1035 $this->_reset_select();
1036 return $result;
1037 }
1038
Derek Allard09de1852007-02-14 01:35:56 +00001039 /**
Derek Allard694b5b82007-12-18 15:58:03 +00001040 * "Count All Results" query
1041 *
1042 * Generates a platform-specific query string that counts all records
1043 * returned by an Active Record query.
1044 *
1045 * @access public
1046 * @param string
1047 * @return string
1048 */
1049 function count_all_results($table = '')
1050 {
1051 if ($table != '')
1052 {
Derek Allard5e128942007-12-28 21:33:03 +00001053 $this->_track_aliases($table);
Derek Allard694b5b82007-12-18 15:58:03 +00001054 $this->from($table);
1055 }
1056
Derek Allard39b622d2008-01-16 21:10:09 +00001057 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
Derek Allard694b5b82007-12-18 15:58:03 +00001058
1059 $query = $this->query($sql);
1060 $this->_reset_select();
1061
1062 if ($query->num_rows() == 0)
1063 {
1064 return '0';
1065 }
1066
1067 $row = $query->row();
1068 return $row->numrows;
1069 }
1070
1071 // --------------------------------------------------------------------
1072
1073 /**
Derek Allard218e2bc2007-12-17 21:18:14 +00001074 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +00001075 *
1076 * Allows the where clause, limit and offset to be added directly
1077 *
1078 * @access public
1079 * @param string the where clause
1080 * @param string the limit clause
1081 * @param string the offset clause
1082 * @return object
1083 */
Derek Allard218e2bc2007-12-17 21:18:14 +00001084 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +00001085 {
1086 if ($table != '')
1087 {
Derek Allard5e128942007-12-28 21:33:03 +00001088 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001089 $this->from($table);
1090 }
1091
Derek Jones0b59f272008-05-13 04:22:33 +00001092 if ( ! is_null($where))
Derek Allard09de1852007-02-14 01:35:56 +00001093 {
1094 $this->where($where);
1095 }
1096
Derek Jones0b59f272008-05-13 04:22:33 +00001097 if ( ! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001098 {
1099 $this->limit($limit, $offset);
1100 }
1101
1102 $sql = $this->_compile_select();
1103
1104 $result = $this->query($sql);
1105 $this->_reset_select();
1106 return $result;
1107 }
Derek Allard218e2bc2007-12-17 21:18:14 +00001108
1109 // --------------------------------------------------------------------
1110
1111 /**
1112 * getwhere() is an alias of get_where()
1113 * this function is here for backwards compatibility, as
1114 * getwhere() has been deprecated
1115 */
1116 function getwhere($table = '', $where = null, $limit = null, $offset = null)
1117 {
1118 return $this->get_where($table, $where, $limit, $offset);
1119 }
Derek Allard09de1852007-02-14 01:35:56 +00001120
1121 // --------------------------------------------------------------------
1122
1123 /**
1124 * Insert
1125 *
1126 * Compiles an insert string and runs the query
1127 *
1128 * @access public
1129 * @param string the table to retrieve the results from
1130 * @param array an associative array of insert values
1131 * @return object
1132 */
1133 function insert($table = '', $set = NULL)
1134 {
Derek Jones0b59f272008-05-13 04:22:33 +00001135 if ( ! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001136 {
1137 $this->set($set);
1138 }
1139
1140 if (count($this->ar_set) == 0)
1141 {
1142 if ($this->db_debug)
1143 {
1144 return $this->display_error('db_must_use_set');
1145 }
1146 return FALSE;
1147 }
1148
1149 if ($table == '')
1150 {
Derek Jones0b59f272008-05-13 04:22:33 +00001151 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001152 {
1153 if ($this->db_debug)
1154 {
1155 return $this->display_error('db_must_set_table');
1156 }
1157 return FALSE;
1158 }
1159
1160 $table = $this->ar_from[0];
1161 }
Derek Allard39b622d2008-01-16 21:10:09 +00001162
1163 $sql = $this->_insert($this->_protect_identifiers($this->dbprefix.$table), array_keys($this->ar_set), array_values($this->ar_set));
Derek Allard09de1852007-02-14 01:35:56 +00001164
1165 $this->_reset_write();
1166 return $this->query($sql);
1167 }
1168
1169 // --------------------------------------------------------------------
1170
1171 /**
1172 * Update
1173 *
1174 * Compiles an update string and runs the query
1175 *
1176 * @access public
1177 * @param string the table to retrieve the results from
1178 * @param array an associative array of update values
1179 * @param mixed the where clause
1180 * @return object
1181 */
Derek Allard5e128942007-12-28 21:33:03 +00001182 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001183 {
Derek Jones0b59f272008-05-13 04:22:33 +00001184 if ( ! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001185 {
1186 $this->set($set);
1187 }
1188
1189 if (count($this->ar_set) == 0)
1190 {
1191 if ($this->db_debug)
1192 {
1193 return $this->display_error('db_must_use_set');
1194 }
1195 return FALSE;
1196 }
1197
1198 if ($table == '')
1199 {
Derek Jones0b59f272008-05-13 04:22:33 +00001200 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001201 {
1202 if ($this->db_debug)
1203 {
1204 return $this->display_error('db_must_set_table');
1205 }
1206 return FALSE;
1207 }
1208
1209 $table = $this->ar_from[0];
1210 }
1211
Derek Allarde77d77c2007-12-19 15:01:55 +00001212 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001213 {
1214 $this->where($where);
1215 }
Derek Allardda6d2402007-12-19 14:49:29 +00001216
Derek Allarde77d77c2007-12-19 15:01:55 +00001217 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +00001218 {
1219 $this->limit($limit);
1220 }
Derek Allard09de1852007-02-14 01:35:56 +00001221
Derek Allard39b622d2008-01-16 21:10:09 +00001222 $sql = $this->_update($this->_protect_identifiers($this->dbprefix.$table), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001223
1224 $this->_reset_write();
1225 return $this->query($sql);
1226 }
Derek Allard39b622d2008-01-16 21:10:09 +00001227
1228 // --------------------------------------------------------------------
1229
1230 /**
1231 * Empty Table
1232 *
1233 * Compiles a delete string and runs "DELETE FROM table"
1234 *
1235 * @access public
1236 * @param string the table to empty
1237 * @return object
1238 */
1239 function empty_table($table = '')
1240 {
1241 if ($table == '')
1242 {
Derek Jones0b59f272008-05-13 04:22:33 +00001243 if ( ! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001244 {
1245 if ($this->db_debug)
1246 {
1247 return $this->display_error('db_must_set_table');
1248 }
1249 return FALSE;
1250 }
1251
1252 $table = $this->ar_from[0];
1253 }
1254 else
1255 {
1256 $table = $this->_protect_identifiers($this->dbprefix.$table);
1257 }
1258
1259
1260 $sql = $this->_delete($table);
1261
1262 $this->_reset_write();
1263
1264 return $this->query($sql);
1265 }
1266
1267 // --------------------------------------------------------------------
1268
1269 /**
1270 * Truncate
1271 *
1272 * Compiles a truncate string and runs the query
1273 * If the database does not support the truncate() command
1274 * This function maps to "DELETE FROM table"
1275 *
1276 * @access public
1277 * @param string the table to truncate
1278 * @return object
1279 */
1280 function truncate($table = '')
1281 {
1282 if ($table == '')
1283 {
Derek Jones0b59f272008-05-13 04:22:33 +00001284 if ( ! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001285 {
1286 if ($this->db_debug)
1287 {
1288 return $this->display_error('db_must_set_table');
1289 }
1290 return FALSE;
1291 }
1292
1293 $table = $this->ar_from[0];
1294 }
1295 else
1296 {
1297 $table = $this->_protect_identifiers($this->dbprefix.$table);
1298 }
1299
1300
1301 $sql = $this->_truncate($table);
1302
1303 $this->_reset_write();
1304
1305 return $this->query($sql);
1306 }
Derek Allard09de1852007-02-14 01:35:56 +00001307
1308 // --------------------------------------------------------------------
1309
1310 /**
1311 * Delete
1312 *
1313 * Compiles a delete string and runs the query
1314 *
1315 * @access public
Derek Allard41f60d42007-12-20 20:09:22 +00001316 * @param mixed the table(s) to delete from. String or array
Derek Allard09de1852007-02-14 01:35:56 +00001317 * @param mixed the where clause
Derek Allard41f60d42007-12-20 20:09:22 +00001318 * @param mixed the limit clause
1319 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +00001320 * @return object
1321 */
Derek Allard41f60d42007-12-20 20:09:22 +00001322 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +00001323 {
1324 if ($table == '')
1325 {
Derek Jones0b59f272008-05-13 04:22:33 +00001326 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001327 {
1328 if ($this->db_debug)
1329 {
1330 return $this->display_error('db_must_set_table');
1331 }
1332 return FALSE;
1333 }
Derek Allard39b622d2008-01-16 21:10:09 +00001334
Derek Allard09de1852007-02-14 01:35:56 +00001335 $table = $this->ar_from[0];
1336 }
Derek Allard39b622d2008-01-16 21:10:09 +00001337 elseif (is_array($table))
Derek Allard41f60d42007-12-20 20:09:22 +00001338 {
1339 foreach($table as $single_table)
1340 {
Derek Allard39b622d2008-01-16 21:10:09 +00001341 $this->delete($single_table, $where, $limit, FALSE);
Derek Allard41f60d42007-12-20 20:09:22 +00001342 }
Derek Allard39b622d2008-01-16 21:10:09 +00001343
Derek Allard41f60d42007-12-20 20:09:22 +00001344 $this->_reset_write();
1345 return;
1346 }
Derek Allard39b622d2008-01-16 21:10:09 +00001347 else
1348 {
1349 $table = $this->_protect_identifiers($this->dbprefix.$table);
1350 }
Derek Allard41f60d42007-12-20 20:09:22 +00001351
Derek Allard09de1852007-02-14 01:35:56 +00001352 if ($where != '')
1353 {
1354 $this->where($where);
1355 }
1356
Derek Allarde77d77c2007-12-19 15:01:55 +00001357 if ($limit != NULL)
1358 {
1359 $this->limit($limit);
1360 }
1361
Derek Allard39b622d2008-01-16 21:10:09 +00001362 if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
Derek Allard09de1852007-02-14 01:35:56 +00001363 {
1364 if ($this->db_debug)
1365 {
1366 return $this->display_error('db_del_must_use_where');
1367 }
Derek Allard39b622d2008-01-16 21:10:09 +00001368
Derek Allard09de1852007-02-14 01:35:56 +00001369 return FALSE;
1370 }
Derek Allard41f60d42007-12-20 20:09:22 +00001371
Derek Allard39b622d2008-01-16 21:10:09 +00001372 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001373
Derek Allard41f60d42007-12-20 20:09:22 +00001374 if ($reset_data)
1375 {
1376 $this->_reset_write();
1377 }
Derek Allard39b622d2008-01-16 21:10:09 +00001378
Derek Allard09de1852007-02-14 01:35:56 +00001379 return $this->query($sql);
1380 }
Derek Allard15ddc9d2007-12-20 13:54:39 +00001381
Derek Allard09de1852007-02-14 01:35:56 +00001382 // --------------------------------------------------------------------
1383
1384 /**
1385 * Use Table - DEPRECATED
1386 *
1387 * @deprecated use $this->db->from instead
1388 */
1389 function use_table($table)
1390 {
1391 return $this->from($table);
Derek Allard09de1852007-02-14 01:35:56 +00001392 }
Derek Allard09de1852007-02-14 01:35:56 +00001393
Derek Allard09de1852007-02-14 01:35:56 +00001394 // --------------------------------------------------------------------
1395
1396 /**
Derek Allard5e128942007-12-28 21:33:03 +00001397 * Track Aliases
1398 *
1399 * Used to track SQL statements written with aliased tables.
1400 *
1401 * @access private
1402 * @param string The table to inspect
1403 * @return string
1404 */
1405 function _track_aliases($table)
1406 {
1407 // if a table alias is used we can recognize it by a space
1408 if (strpos($table, " ") !== FALSE)
1409 {
1410 // if the alias is written with the AS keyowrd, get it out
Derek Allard39b622d2008-01-16 21:10:09 +00001411 $table = preg_replace('/ AS /i', ' ', $table);
Derek Allard5e128942007-12-28 21:33:03 +00001412
Derek Allard39b622d2008-01-16 21:10:09 +00001413 $this->ar_aliased_tables[] = trim(strrchr($table, " "));
Derek Allard5e128942007-12-28 21:33:03 +00001414 }
1415
1416 return $this->dbprefix.$table;
1417 }
1418
1419 // --------------------------------------------------------------------
1420
1421 /**
1422 * Filter Table Aliases
1423 *
1424 * Intelligently removes database prefixes from aliased tables
1425 *
1426 * @access private
1427 * @param array An array of compiled SQL
1428 * @return array Cleaned up statement with aliases accounted for
1429 */
1430 function _filter_table_aliases($statements)
1431 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001432
Derek Allard39b622d2008-01-16 21:10:09 +00001433 foreach ($statements as $k => $v)
Derek Allard5e128942007-12-28 21:33:03 +00001434 {
Derek Allard39b622d2008-01-16 21:10:09 +00001435 foreach ($this->ar_aliased_tables as $table)
Derek Allard5e128942007-12-28 21:33:03 +00001436 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001437 $statements[$k] = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $statements[$k]); // makes `table.field`
1438 $statements[$k] = str_replace($this->dbprefix.$table.'.', $table.'.', $statements[$k]);
Derek Allard5e128942007-12-28 21:33:03 +00001439 }
Derek Allard5e128942007-12-28 21:33:03 +00001440 }
Derek Allard39b622d2008-01-16 21:10:09 +00001441 return $statements;
Derek Allard5e128942007-12-28 21:33:03 +00001442 }
1443
1444 // --------------------------------------------------------------------
1445
1446 /**
Derek Allard09de1852007-02-14 01:35:56 +00001447 * Compile the SELECT statement
1448 *
1449 * Generates a query string based on which functions were used.
1450 * Should not be called directly. The get() function calls it.
1451 *
1452 * @access private
1453 * @return string
1454 */
Derek Allard694b5b82007-12-18 15:58:03 +00001455 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001456 {
Derek Allard78255262008-02-06 13:54:23 +00001457 $this->_merge_cache();
Derek Allard9b3e7b52008-02-04 23:20:34 +00001458
Derek Jones0b59f272008-05-13 04:22:33 +00001459 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Derek Allard09de1852007-02-14 01:35:56 +00001460
Derek Allard5162fc72008-04-15 20:05:37 +00001461 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->_filter_table_aliases($this->ar_select));
Derek Allard09de1852007-02-14 01:35:56 +00001462
Derek Allard694b5b82007-12-18 15:58:03 +00001463 if ($select_override !== FALSE)
1464 {
1465 $sql = $select_override;
1466 }
1467
Derek Allard09de1852007-02-14 01:35:56 +00001468 if (count($this->ar_from) > 0)
1469 {
1470 $sql .= "\nFROM ";
Derek Jonesc6ad0232008-01-29 18:44:54 +00001471 $sql .= $this->_from_tables($this->ar_from);
Derek Allard09de1852007-02-14 01:35:56 +00001472 }
1473
1474 if (count($this->ar_join) > 0)
Derek Allard5e128942007-12-28 21:33:03 +00001475 {
Derek Allard09de1852007-02-14 01:35:56 +00001476 $sql .= "\n";
Derek Allard5e128942007-12-28 21:33:03 +00001477
1478 // special consideration for table aliases
1479 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1480 {
1481 $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
1482 }
1483 else
1484 {
1485 $sql .= implode("\n", $this->ar_join);
1486 }
1487
Derek Allard09de1852007-02-14 01:35:56 +00001488 }
1489
1490 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1491 {
1492 $sql .= "\nWHERE ";
1493 }
1494
1495 $sql .= implode("\n", $this->ar_where);
1496
1497 if (count($this->ar_like) > 0)
1498 {
1499 if (count($this->ar_where) > 0)
1500 {
1501 $sql .= " AND ";
1502 }
1503
1504 $sql .= implode("\n", $this->ar_like);
1505 }
1506
1507 if (count($this->ar_groupby) > 0)
1508 {
Derek Allard5e128942007-12-28 21:33:03 +00001509
Derek Allard09de1852007-02-14 01:35:56 +00001510 $sql .= "\nGROUP BY ";
Derek Allard5e128942007-12-28 21:33:03 +00001511
1512 // special consideration for table aliases
1513 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1514 {
1515 $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
1516 }
1517 else
1518 {
1519 $sql .= implode(', ', $this->ar_groupby);
1520 }
Derek Allard09de1852007-02-14 01:35:56 +00001521 }
1522
1523 if (count($this->ar_having) > 0)
1524 {
1525 $sql .= "\nHAVING ";
1526 $sql .= implode("\n", $this->ar_having);
1527 }
1528
1529 if (count($this->ar_orderby) > 0)
1530 {
1531 $sql .= "\nORDER BY ";
1532 $sql .= implode(', ', $this->ar_orderby);
1533
1534 if ($this->ar_order !== FALSE)
1535 {
1536 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1537 }
1538 }
1539
1540 if (is_numeric($this->ar_limit))
1541 {
1542 $sql .= "\n";
1543 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1544 }
1545
1546 return $sql;
1547 }
1548
1549 // --------------------------------------------------------------------
1550
1551 /**
1552 * Object to Array
1553 *
1554 * Takes an object as input and converts the class variables to array key/vals
1555 *
1556 * @access public
1557 * @param object
1558 * @return array
1559 */
1560 function _object_to_array($object)
1561 {
Derek Jones0b59f272008-05-13 04:22:33 +00001562 if ( ! is_object($object))
Derek Allard09de1852007-02-14 01:35:56 +00001563 {
1564 return $object;
1565 }
1566
1567 $array = array();
1568 foreach (get_object_vars($object) as $key => $val)
1569 {
Derek Allard848b7762007-12-31 16:43:05 +00001570 // There are some built in keys we need to ignore for this conversion
Derek Jones0b59f272008-05-13 04:22:33 +00001571 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
Derek Allard848b7762007-12-31 16:43:05 +00001572
Derek Allard09de1852007-02-14 01:35:56 +00001573 {
1574 $array[$key] = $val;
1575 }
1576 }
1577
1578 return $array;
1579 }
1580
1581 // --------------------------------------------------------------------
1582
1583 /**
Derek Allard9b3e7b52008-02-04 23:20:34 +00001584 * Start Cache
1585 *
1586 * Starts AR caching
1587 *
1588 * @access public
1589 * @return void
1590 */
1591 function start_cache()
1592 {
1593 $this->ar_caching = TRUE;
1594 }
1595
1596 // --------------------------------------------------------------------
1597
1598 /**
1599 * Stop Cache
1600 *
1601 * Stops AR caching
1602 *
1603 * @access public
1604 * @return void
1605 */
1606 function stop_cache()
1607 {
1608 $this->ar_caching = FALSE;
1609 }
1610
1611
1612 // --------------------------------------------------------------------
1613
1614 /**
1615 * Flush Cache
1616 *
1617 * Empties the AR cache
1618 *
1619 * @access public
1620 * @return void
1621 */
1622 function flush_cache()
1623 {
1624 $ar_reset_items = array(
1625 'ar_cache_select' => array(),
1626 'ar_cache_from' => array(),
1627 'ar_cache_join' => array(),
1628 'ar_cache_where' => array(),
1629 'ar_cache_like' => array(),
1630 'ar_cache_groupby' => array(),
1631 'ar_cache_having' =>array(),
1632 'ar_cache_orderby' => array(),
1633 'ar_cache_set' => array()
1634 );
1635
1636 $this->_reset_run($ar_reset_items);
1637 }
1638
1639 // --------------------------------------------------------------------
1640
1641 /**
1642 * Merge Cache
1643 *
1644 * When called, this function merges any cached AR arrays with
1645 * locally called ones.
1646 *
1647 * @access private
1648 * @return void
1649 */
1650 function _merge_cache()
1651 {
1652 $ar_items = array('select', 'from', 'join', 'where', 'like', 'groupby', 'having', 'orderby', 'set');
1653
1654 foreach ($ar_items as $ar_item)
1655 {
1656 $ar_cache_item = 'ar_cache_'.$ar_item;
1657 $ar_item = 'ar_'.$ar_item;
1658 $this->$ar_item = array_unique(array_merge($this->$ar_item, $this->$ar_cache_item));
1659 }
1660 }
1661
1662 // --------------------------------------------------------------------
1663
1664 /**
1665 * Resets the active record values. Called by the get() function
1666 *
1667 * @access private
1668 * @param array An array of fields to reset
1669 * @return void
1670 */
1671 function _reset_run($ar_reset_items)
1672 {
1673 foreach ($ar_reset_items as $item => $default_value)
1674 {
Derek Jones0b59f272008-05-13 04:22:33 +00001675 if ( ! in_array($item, $this->ar_store_array))
Derek Allard9b3e7b52008-02-04 23:20:34 +00001676 {
1677 $this->$item = $default_value;
1678 }
1679 }
1680 }
1681
1682 // --------------------------------------------------------------------
1683
1684 /**
Derek Allard09de1852007-02-14 01:35:56 +00001685 * Resets the active record values. Called by the get() function
1686 *
1687 * @access private
1688 * @return void
1689 */
1690 function _reset_select()
1691 {
Derek Allard9b3e7b52008-02-04 23:20:34 +00001692 $ar_reset_items = array(
1693 'ar_select' => array(),
1694 'ar_from' => array(),
1695 'ar_join' => array(),
1696 'ar_where' => array(),
1697 'ar_like' => array(),
1698 'ar_groupby' => array(),
1699 'ar_having' => array(),
1700 'ar_orderby' => array(),
1701 'ar_wherein' => array(),
1702 'ar_aliased_tables' => array(),
1703 'ar_distinct' => FALSE,
1704 'ar_limit' => FALSE,
1705 'ar_offset' => FALSE,
1706 'ar_order' => FALSE,
1707 );
1708
1709 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001710 }
1711
1712 // --------------------------------------------------------------------
1713
1714 /**
1715 * Resets the active record "write" values.
1716 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001717 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001718 *
1719 * @access private
1720 * @return void
1721 */
1722 function _reset_write()
Derek Allard9b3e7b52008-02-04 23:20:34 +00001723 {
1724 $ar_reset_items = array(
1725 'ar_set' => array(),
1726 'ar_from' => array(),
1727 'ar_where' => array(),
1728 'ar_like' => array(),
1729 'ar_orderby' => array(),
1730 'ar_limit' => FALSE,
1731 'ar_order' => FALSE
1732 );
1733
1734 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001735 }
1736
1737}
Derek Allarda6325892008-05-12 17:51:47 +00001738
1739/* End of file DB_active_rec.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00001740/* Location: ./system/database/DB_active_rec.php */