blob: fccf1b0a26939def25917b10ebbfc92a49899cc2 [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 {
443 $k = preg_replace("/([A-Za-z_0-9]+)/", $this->_protect_identifiers('$1'), $k);
444 }
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
Derek Allard09de1852007-02-14 01:35:56 +0000704 $v = $this->escape_str($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 *
824 * Called by having() or orhaving()
825 *
826 * @access private
827 * @param string
Derek Allard9b3e7b52008-02-04 23:20:34 +0000828
Derek Allard09de1852007-02-14 01:35:56 +0000829 * @param string
830 * @return object
831 */
Derek Allarde808aac2008-04-06 18:22:00 +0000832 function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000833 {
Derek Jones0b59f272008-05-13 04:22:33 +0000834 if ( ! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000835 {
836 $key = array($key => $value);
837 }
838
839 foreach ($key as $k => $v)
840 {
841 $prefix = (count($this->ar_having) == 0) ? '' : $type;
Derek Allarde808aac2008-04-06 18:22:00 +0000842
843 if ($escape === TRUE)
844 {
845 $k = $this->_protect_identifiers($k);
846 }
847
Derek Allarda459b462008-05-22 13:01:39 +0000848 if ( ! $this->_has_operator($k))
849 {
850 $k .= ' = ';
851 }
852
Derek Allard09de1852007-02-14 01:35:56 +0000853 if ($v != '')
854 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000855 $v = ' '.$this->escape_str($v);
Derek Allard09de1852007-02-14 01:35:56 +0000856 }
857
858 $this->ar_having[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000859 if ($this->ar_caching === TRUE)
860 {
861 $this->ar_cache_having[] = $prefix.$k.$v;
862 }
Derek Allard09de1852007-02-14 01:35:56 +0000863 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000864
Derek Allard09de1852007-02-14 01:35:56 +0000865 return $this;
866 }
867
868 // --------------------------------------------------------------------
869
870 /**
871 * Sets the ORDER BY value
872 *
873 * @access public
874 * @param string
875 * @param string direction: asc or desc
876 * @return object
877 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000878 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000879 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000880 if (strtolower($direction) == 'random')
881 {
882 $orderby = ''; // Random results want or don't need a field name
883 $direction = $this->_random_keyword;
884 }
885 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000886 {
Derek Allard92782492007-08-10 11:26:01 +0000887 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000888 }
889
Derek Allard9b3e7b52008-02-04 23:20:34 +0000890 $orderby_statement = $this->_protect_identifiers($orderby, TRUE).$direction;
891
892 $this->ar_orderby[] = $orderby_statement;
893 if ($this->ar_caching === TRUE)
894 {
895 $this->ar_cache_orderby[] = $orderby_statement;
896 }
897
Derek Allard09de1852007-02-14 01:35:56 +0000898 return $this;
899 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000900
Derek Allard218e2bc2007-12-17 21:18:14 +0000901 // --------------------------------------------------------------------
902
903 /**
904 * orderby() is an alias of order_by()
905 * this function is here for backwards compatibility, as
906 * orderby() has been deprecated
907 */
908 function orderby($orderby, $direction = '')
909 {
910 return $this->order_by($orderby, $direction);
911 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000912
Derek Allard09de1852007-02-14 01:35:56 +0000913 // --------------------------------------------------------------------
914
915 /**
916 * Sets the LIMIT value
917 *
918 * @access public
919 * @param integer the limit value
920 * @param integer the offset value
921 * @return object
922 */
923 function limit($value, $offset = '')
924 {
925 $this->ar_limit = $value;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000926 if ($this->ar_caching === TRUE)
927 {
928 $this->ar_cache_limit[] = $value;
929 }
930
Derek Allard09de1852007-02-14 01:35:56 +0000931 if ($offset != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000932 {
Derek Allard09de1852007-02-14 01:35:56 +0000933 $this->ar_offset = $offset;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000934 if ($this->ar_caching === TRUE)
935 {
936 $this->ar_cache_offset[] = $offset;
937 }
938 }
Derek Allard09de1852007-02-14 01:35:56 +0000939
940 return $this;
941 }
942
943 // --------------------------------------------------------------------
944
945 /**
946 * Sets the OFFSET value
947 *
948 * @access public
949 * @param integer the offset value
950 * @return object
951 */
Derek Allard9b3e7b52008-02-04 23:20:34 +0000952 function offset($offset)
Derek Allard09de1852007-02-14 01:35:56 +0000953 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000954 $this->ar_offset = $offset;
955 if ($this->ar_caching === TRUE)
956 {
957 $this->ar_cache_offset[] = $offset;
958 }
959
Derek Allard09de1852007-02-14 01:35:56 +0000960 return $this;
961 }
962
963 // --------------------------------------------------------------------
964
965 /**
966 * The "set" function. Allows key/value pairs to be set for inserting or updating
967 *
968 * @access public
969 * @param mixed
970 * @param string
Derek Allard39b622d2008-01-16 21:10:09 +0000971 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +0000972 * @return object
973 */
Derek Allard39b622d2008-01-16 21:10:09 +0000974 function set($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000975 {
976 $key = $this->_object_to_array($key);
977
Derek Jones0b59f272008-05-13 04:22:33 +0000978 if ( ! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000979 {
980 $key = array($key => $value);
981 }
982
983 foreach ($key as $k => $v)
984 {
Derek Allard39b622d2008-01-16 21:10:09 +0000985 if ($escape === FALSE)
986 {
987 $this->ar_set[$this->_protect_identifiers($k)] = $v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000988 if ($this->ar_caching === TRUE)
989 {
990 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $v;
991 }
Derek Allard39b622d2008-01-16 21:10:09 +0000992 }
993 else
994 {
995 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000996 if ($this->ar_caching === TRUE)
997 {
998 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $this->escape($v);
999 }
Derek Allard39b622d2008-01-16 21:10:09 +00001000 }
Derek Allard09de1852007-02-14 01:35:56 +00001001 }
1002
1003 return $this;
1004 }
1005
1006 // --------------------------------------------------------------------
1007
1008 /**
1009 * Get
1010 *
1011 * Compiles the select statement based on the other functions called
1012 * and runs the query
1013 *
1014 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +00001015 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +00001016 * @param string the limit clause
1017 * @param string the offset clause
1018 * @return object
1019 */
1020 function get($table = '', $limit = null, $offset = null)
1021 {
1022 if ($table != '')
1023 {
Derek Allard5e128942007-12-28 21:33:03 +00001024 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001025 $this->from($table);
1026 }
1027
Derek Jones0b59f272008-05-13 04:22:33 +00001028 if ( ! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001029 {
1030 $this->limit($limit, $offset);
1031 }
1032
1033 $sql = $this->_compile_select();
1034
1035 $result = $this->query($sql);
1036 $this->_reset_select();
1037 return $result;
1038 }
1039
Derek Allard09de1852007-02-14 01:35:56 +00001040 /**
Derek Allard694b5b82007-12-18 15:58:03 +00001041 * "Count All Results" query
1042 *
1043 * Generates a platform-specific query string that counts all records
1044 * returned by an Active Record query.
1045 *
1046 * @access public
1047 * @param string
1048 * @return string
1049 */
1050 function count_all_results($table = '')
1051 {
1052 if ($table != '')
1053 {
Derek Allard5e128942007-12-28 21:33:03 +00001054 $this->_track_aliases($table);
Derek Allard694b5b82007-12-18 15:58:03 +00001055 $this->from($table);
1056 }
1057
Derek Allard39b622d2008-01-16 21:10:09 +00001058 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
Derek Allard694b5b82007-12-18 15:58:03 +00001059
1060 $query = $this->query($sql);
1061 $this->_reset_select();
1062
1063 if ($query->num_rows() == 0)
1064 {
1065 return '0';
1066 }
1067
1068 $row = $query->row();
1069 return $row->numrows;
1070 }
1071
1072 // --------------------------------------------------------------------
1073
1074 /**
Derek Allard218e2bc2007-12-17 21:18:14 +00001075 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +00001076 *
1077 * Allows the where clause, limit and offset to be added directly
1078 *
1079 * @access public
1080 * @param string the where clause
1081 * @param string the limit clause
1082 * @param string the offset clause
1083 * @return object
1084 */
Derek Allard218e2bc2007-12-17 21:18:14 +00001085 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +00001086 {
1087 if ($table != '')
1088 {
Derek Allard5e128942007-12-28 21:33:03 +00001089 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001090 $this->from($table);
1091 }
1092
Derek Jones0b59f272008-05-13 04:22:33 +00001093 if ( ! is_null($where))
Derek Allard09de1852007-02-14 01:35:56 +00001094 {
1095 $this->where($where);
1096 }
1097
Derek Jones0b59f272008-05-13 04:22:33 +00001098 if ( ! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001099 {
1100 $this->limit($limit, $offset);
1101 }
1102
1103 $sql = $this->_compile_select();
1104
1105 $result = $this->query($sql);
1106 $this->_reset_select();
1107 return $result;
1108 }
Derek Allard218e2bc2007-12-17 21:18:14 +00001109
1110 // --------------------------------------------------------------------
1111
1112 /**
1113 * getwhere() is an alias of get_where()
1114 * this function is here for backwards compatibility, as
1115 * getwhere() has been deprecated
1116 */
1117 function getwhere($table = '', $where = null, $limit = null, $offset = null)
1118 {
1119 return $this->get_where($table, $where, $limit, $offset);
1120 }
Derek Allard09de1852007-02-14 01:35:56 +00001121
1122 // --------------------------------------------------------------------
1123
1124 /**
1125 * Insert
1126 *
1127 * Compiles an insert string and runs the query
1128 *
1129 * @access public
1130 * @param string the table to retrieve the results from
1131 * @param array an associative array of insert values
1132 * @return object
1133 */
1134 function insert($table = '', $set = NULL)
1135 {
Derek Jones0b59f272008-05-13 04:22:33 +00001136 if ( ! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001137 {
1138 $this->set($set);
1139 }
1140
1141 if (count($this->ar_set) == 0)
1142 {
1143 if ($this->db_debug)
1144 {
1145 return $this->display_error('db_must_use_set');
1146 }
1147 return FALSE;
1148 }
1149
1150 if ($table == '')
1151 {
Derek Jones0b59f272008-05-13 04:22:33 +00001152 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001153 {
1154 if ($this->db_debug)
1155 {
1156 return $this->display_error('db_must_set_table');
1157 }
1158 return FALSE;
1159 }
1160
1161 $table = $this->ar_from[0];
1162 }
Derek Allard39b622d2008-01-16 21:10:09 +00001163
1164 $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 +00001165
1166 $this->_reset_write();
1167 return $this->query($sql);
1168 }
1169
1170 // --------------------------------------------------------------------
1171
1172 /**
1173 * Update
1174 *
1175 * Compiles an update string and runs the query
1176 *
1177 * @access public
1178 * @param string the table to retrieve the results from
1179 * @param array an associative array of update values
1180 * @param mixed the where clause
1181 * @return object
1182 */
Derek Allard5e128942007-12-28 21:33:03 +00001183 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001184 {
Derek Jones0b59f272008-05-13 04:22:33 +00001185 if ( ! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001186 {
1187 $this->set($set);
1188 }
1189
1190 if (count($this->ar_set) == 0)
1191 {
1192 if ($this->db_debug)
1193 {
1194 return $this->display_error('db_must_use_set');
1195 }
1196 return FALSE;
1197 }
1198
1199 if ($table == '')
1200 {
Derek Jones0b59f272008-05-13 04:22:33 +00001201 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001202 {
1203 if ($this->db_debug)
1204 {
1205 return $this->display_error('db_must_set_table');
1206 }
1207 return FALSE;
1208 }
1209
1210 $table = $this->ar_from[0];
1211 }
1212
Derek Allarde77d77c2007-12-19 15:01:55 +00001213 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001214 {
1215 $this->where($where);
1216 }
Derek Allardda6d2402007-12-19 14:49:29 +00001217
Derek Allarde77d77c2007-12-19 15:01:55 +00001218 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +00001219 {
1220 $this->limit($limit);
1221 }
Derek Allard09de1852007-02-14 01:35:56 +00001222
Derek Allard39b622d2008-01-16 21:10:09 +00001223 $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 +00001224
1225 $this->_reset_write();
1226 return $this->query($sql);
1227 }
Derek Allard39b622d2008-01-16 21:10:09 +00001228
1229 // --------------------------------------------------------------------
1230
1231 /**
1232 * Empty Table
1233 *
1234 * Compiles a delete string and runs "DELETE FROM table"
1235 *
1236 * @access public
1237 * @param string the table to empty
1238 * @return object
1239 */
1240 function empty_table($table = '')
1241 {
1242 if ($table == '')
1243 {
Derek Jones0b59f272008-05-13 04:22:33 +00001244 if ( ! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001245 {
1246 if ($this->db_debug)
1247 {
1248 return $this->display_error('db_must_set_table');
1249 }
1250 return FALSE;
1251 }
1252
1253 $table = $this->ar_from[0];
1254 }
1255 else
1256 {
1257 $table = $this->_protect_identifiers($this->dbprefix.$table);
1258 }
1259
1260
1261 $sql = $this->_delete($table);
1262
1263 $this->_reset_write();
1264
1265 return $this->query($sql);
1266 }
1267
1268 // --------------------------------------------------------------------
1269
1270 /**
1271 * Truncate
1272 *
1273 * Compiles a truncate string and runs the query
1274 * If the database does not support the truncate() command
1275 * This function maps to "DELETE FROM table"
1276 *
1277 * @access public
1278 * @param string the table to truncate
1279 * @return object
1280 */
1281 function truncate($table = '')
1282 {
1283 if ($table == '')
1284 {
Derek Jones0b59f272008-05-13 04:22:33 +00001285 if ( ! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001286 {
1287 if ($this->db_debug)
1288 {
1289 return $this->display_error('db_must_set_table');
1290 }
1291 return FALSE;
1292 }
1293
1294 $table = $this->ar_from[0];
1295 }
1296 else
1297 {
1298 $table = $this->_protect_identifiers($this->dbprefix.$table);
1299 }
1300
1301
1302 $sql = $this->_truncate($table);
1303
1304 $this->_reset_write();
1305
1306 return $this->query($sql);
1307 }
Derek Allard09de1852007-02-14 01:35:56 +00001308
1309 // --------------------------------------------------------------------
1310
1311 /**
1312 * Delete
1313 *
1314 * Compiles a delete string and runs the query
1315 *
1316 * @access public
Derek Allard41f60d42007-12-20 20:09:22 +00001317 * @param mixed the table(s) to delete from. String or array
Derek Allard09de1852007-02-14 01:35:56 +00001318 * @param mixed the where clause
Derek Allard41f60d42007-12-20 20:09:22 +00001319 * @param mixed the limit clause
1320 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +00001321 * @return object
1322 */
Derek Allard41f60d42007-12-20 20:09:22 +00001323 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +00001324 {
1325 if ($table == '')
1326 {
Derek Jones0b59f272008-05-13 04:22:33 +00001327 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001328 {
1329 if ($this->db_debug)
1330 {
1331 return $this->display_error('db_must_set_table');
1332 }
1333 return FALSE;
1334 }
Derek Allard39b622d2008-01-16 21:10:09 +00001335
Derek Allard09de1852007-02-14 01:35:56 +00001336 $table = $this->ar_from[0];
1337 }
Derek Allard39b622d2008-01-16 21:10:09 +00001338 elseif (is_array($table))
Derek Allard41f60d42007-12-20 20:09:22 +00001339 {
1340 foreach($table as $single_table)
1341 {
Derek Allard39b622d2008-01-16 21:10:09 +00001342 $this->delete($single_table, $where, $limit, FALSE);
Derek Allard41f60d42007-12-20 20:09:22 +00001343 }
Derek Allard39b622d2008-01-16 21:10:09 +00001344
Derek Allard41f60d42007-12-20 20:09:22 +00001345 $this->_reset_write();
1346 return;
1347 }
Derek Allard39b622d2008-01-16 21:10:09 +00001348 else
1349 {
1350 $table = $this->_protect_identifiers($this->dbprefix.$table);
1351 }
Derek Allard41f60d42007-12-20 20:09:22 +00001352
Derek Allard09de1852007-02-14 01:35:56 +00001353 if ($where != '')
1354 {
1355 $this->where($where);
1356 }
1357
Derek Allarde77d77c2007-12-19 15:01:55 +00001358 if ($limit != NULL)
1359 {
1360 $this->limit($limit);
1361 }
1362
Derek Allard39b622d2008-01-16 21:10:09 +00001363 if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
Derek Allard09de1852007-02-14 01:35:56 +00001364 {
1365 if ($this->db_debug)
1366 {
1367 return $this->display_error('db_del_must_use_where');
1368 }
Derek Allard39b622d2008-01-16 21:10:09 +00001369
Derek Allard09de1852007-02-14 01:35:56 +00001370 return FALSE;
1371 }
Derek Allard41f60d42007-12-20 20:09:22 +00001372
Derek Allard39b622d2008-01-16 21:10:09 +00001373 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001374
Derek Allard41f60d42007-12-20 20:09:22 +00001375 if ($reset_data)
1376 {
1377 $this->_reset_write();
1378 }
Derek Allard39b622d2008-01-16 21:10:09 +00001379
Derek Allard09de1852007-02-14 01:35:56 +00001380 return $this->query($sql);
1381 }
Derek Allard15ddc9d2007-12-20 13:54:39 +00001382
Derek Allard09de1852007-02-14 01:35:56 +00001383 // --------------------------------------------------------------------
1384
1385 /**
1386 * Use Table - DEPRECATED
1387 *
1388 * @deprecated use $this->db->from instead
1389 */
1390 function use_table($table)
1391 {
1392 return $this->from($table);
Derek Allard09de1852007-02-14 01:35:56 +00001393 }
Derek Allard09de1852007-02-14 01:35:56 +00001394
Derek Allard09de1852007-02-14 01:35:56 +00001395 // --------------------------------------------------------------------
1396
1397 /**
Derek Allard5e128942007-12-28 21:33:03 +00001398 * Track Aliases
1399 *
1400 * Used to track SQL statements written with aliased tables.
1401 *
1402 * @access private
1403 * @param string The table to inspect
1404 * @return string
1405 */
1406 function _track_aliases($table)
1407 {
1408 // if a table alias is used we can recognize it by a space
1409 if (strpos($table, " ") !== FALSE)
1410 {
1411 // if the alias is written with the AS keyowrd, get it out
Derek Allard39b622d2008-01-16 21:10:09 +00001412 $table = preg_replace('/ AS /i', ' ', $table);
Derek Allard5e128942007-12-28 21:33:03 +00001413
Derek Allard39b622d2008-01-16 21:10:09 +00001414 $this->ar_aliased_tables[] = trim(strrchr($table, " "));
Derek Allard5e128942007-12-28 21:33:03 +00001415 }
1416
1417 return $this->dbprefix.$table;
1418 }
1419
1420 // --------------------------------------------------------------------
1421
1422 /**
1423 * Filter Table Aliases
1424 *
1425 * Intelligently removes database prefixes from aliased tables
1426 *
1427 * @access private
1428 * @param array An array of compiled SQL
1429 * @return array Cleaned up statement with aliases accounted for
1430 */
1431 function _filter_table_aliases($statements)
1432 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001433
Derek Allard39b622d2008-01-16 21:10:09 +00001434 foreach ($statements as $k => $v)
Derek Allard5e128942007-12-28 21:33:03 +00001435 {
Derek Allard39b622d2008-01-16 21:10:09 +00001436 foreach ($this->ar_aliased_tables as $table)
Derek Allard5e128942007-12-28 21:33:03 +00001437 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001438 $statements[$k] = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $statements[$k]); // makes `table.field`
1439 $statements[$k] = str_replace($this->dbprefix.$table.'.', $table.'.', $statements[$k]);
Derek Allard5e128942007-12-28 21:33:03 +00001440 }
Derek Allard5e128942007-12-28 21:33:03 +00001441 }
Derek Allard39b622d2008-01-16 21:10:09 +00001442 return $statements;
Derek Allard5e128942007-12-28 21:33:03 +00001443 }
1444
1445 // --------------------------------------------------------------------
1446
1447 /**
Derek Allard09de1852007-02-14 01:35:56 +00001448 * Compile the SELECT statement
1449 *
1450 * Generates a query string based on which functions were used.
1451 * Should not be called directly. The get() function calls it.
1452 *
1453 * @access private
1454 * @return string
1455 */
Derek Allard694b5b82007-12-18 15:58:03 +00001456 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001457 {
Derek Allard78255262008-02-06 13:54:23 +00001458 $this->_merge_cache();
Derek Allard9b3e7b52008-02-04 23:20:34 +00001459
Derek Jones0b59f272008-05-13 04:22:33 +00001460 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Derek Allard09de1852007-02-14 01:35:56 +00001461
Derek Allard5162fc72008-04-15 20:05:37 +00001462 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->_filter_table_aliases($this->ar_select));
Derek Allard09de1852007-02-14 01:35:56 +00001463
Derek Allard694b5b82007-12-18 15:58:03 +00001464 if ($select_override !== FALSE)
1465 {
1466 $sql = $select_override;
1467 }
1468
Derek Allard09de1852007-02-14 01:35:56 +00001469 if (count($this->ar_from) > 0)
1470 {
1471 $sql .= "\nFROM ";
Derek Jonesc6ad0232008-01-29 18:44:54 +00001472 $sql .= $this->_from_tables($this->ar_from);
Derek Allard09de1852007-02-14 01:35:56 +00001473 }
1474
1475 if (count($this->ar_join) > 0)
Derek Allard5e128942007-12-28 21:33:03 +00001476 {
Derek Allard09de1852007-02-14 01:35:56 +00001477 $sql .= "\n";
Derek Allard5e128942007-12-28 21:33:03 +00001478
1479 // special consideration for table aliases
1480 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1481 {
1482 $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
1483 }
1484 else
1485 {
1486 $sql .= implode("\n", $this->ar_join);
1487 }
1488
Derek Allard09de1852007-02-14 01:35:56 +00001489 }
1490
1491 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1492 {
1493 $sql .= "\nWHERE ";
1494 }
1495
1496 $sql .= implode("\n", $this->ar_where);
1497
1498 if (count($this->ar_like) > 0)
1499 {
1500 if (count($this->ar_where) > 0)
1501 {
1502 $sql .= " AND ";
1503 }
1504
1505 $sql .= implode("\n", $this->ar_like);
1506 }
1507
1508 if (count($this->ar_groupby) > 0)
1509 {
Derek Allard5e128942007-12-28 21:33:03 +00001510
Derek Allard09de1852007-02-14 01:35:56 +00001511 $sql .= "\nGROUP BY ";
Derek Allard5e128942007-12-28 21:33:03 +00001512
1513 // special consideration for table aliases
1514 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1515 {
1516 $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
1517 }
1518 else
1519 {
1520 $sql .= implode(', ', $this->ar_groupby);
1521 }
Derek Allard09de1852007-02-14 01:35:56 +00001522 }
1523
1524 if (count($this->ar_having) > 0)
1525 {
1526 $sql .= "\nHAVING ";
1527 $sql .= implode("\n", $this->ar_having);
1528 }
1529
1530 if (count($this->ar_orderby) > 0)
1531 {
1532 $sql .= "\nORDER BY ";
1533 $sql .= implode(', ', $this->ar_orderby);
1534
1535 if ($this->ar_order !== FALSE)
1536 {
1537 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1538 }
1539 }
1540
1541 if (is_numeric($this->ar_limit))
1542 {
1543 $sql .= "\n";
1544 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1545 }
1546
1547 return $sql;
1548 }
1549
1550 // --------------------------------------------------------------------
1551
1552 /**
1553 * Object to Array
1554 *
1555 * Takes an object as input and converts the class variables to array key/vals
1556 *
1557 * @access public
1558 * @param object
1559 * @return array
1560 */
1561 function _object_to_array($object)
1562 {
Derek Jones0b59f272008-05-13 04:22:33 +00001563 if ( ! is_object($object))
Derek Allard09de1852007-02-14 01:35:56 +00001564 {
1565 return $object;
1566 }
1567
1568 $array = array();
1569 foreach (get_object_vars($object) as $key => $val)
1570 {
Derek Allard848b7762007-12-31 16:43:05 +00001571 // There are some built in keys we need to ignore for this conversion
Derek Jones0b59f272008-05-13 04:22:33 +00001572 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
Derek Allard848b7762007-12-31 16:43:05 +00001573
Derek Allard09de1852007-02-14 01:35:56 +00001574 {
1575 $array[$key] = $val;
1576 }
1577 }
1578
1579 return $array;
1580 }
1581
1582 // --------------------------------------------------------------------
1583
1584 /**
Derek Allard9b3e7b52008-02-04 23:20:34 +00001585 * Start Cache
1586 *
1587 * Starts AR caching
1588 *
1589 * @access public
1590 * @return void
1591 */
1592 function start_cache()
1593 {
1594 $this->ar_caching = TRUE;
1595 }
1596
1597 // --------------------------------------------------------------------
1598
1599 /**
1600 * Stop Cache
1601 *
1602 * Stops AR caching
1603 *
1604 * @access public
1605 * @return void
1606 */
1607 function stop_cache()
1608 {
1609 $this->ar_caching = FALSE;
1610 }
1611
1612
1613 // --------------------------------------------------------------------
1614
1615 /**
1616 * Flush Cache
1617 *
1618 * Empties the AR cache
1619 *
1620 * @access public
1621 * @return void
1622 */
1623 function flush_cache()
1624 {
1625 $ar_reset_items = array(
1626 'ar_cache_select' => array(),
1627 'ar_cache_from' => array(),
1628 'ar_cache_join' => array(),
1629 'ar_cache_where' => array(),
1630 'ar_cache_like' => array(),
1631 'ar_cache_groupby' => array(),
1632 'ar_cache_having' =>array(),
1633 'ar_cache_orderby' => array(),
1634 'ar_cache_set' => array()
1635 );
1636
1637 $this->_reset_run($ar_reset_items);
1638 }
1639
1640 // --------------------------------------------------------------------
1641
1642 /**
1643 * Merge Cache
1644 *
1645 * When called, this function merges any cached AR arrays with
1646 * locally called ones.
1647 *
1648 * @access private
1649 * @return void
1650 */
1651 function _merge_cache()
1652 {
1653 $ar_items = array('select', 'from', 'join', 'where', 'like', 'groupby', 'having', 'orderby', 'set');
1654
1655 foreach ($ar_items as $ar_item)
1656 {
1657 $ar_cache_item = 'ar_cache_'.$ar_item;
1658 $ar_item = 'ar_'.$ar_item;
1659 $this->$ar_item = array_unique(array_merge($this->$ar_item, $this->$ar_cache_item));
1660 }
1661 }
1662
1663 // --------------------------------------------------------------------
1664
1665 /**
1666 * Resets the active record values. Called by the get() function
1667 *
1668 * @access private
1669 * @param array An array of fields to reset
1670 * @return void
1671 */
1672 function _reset_run($ar_reset_items)
1673 {
1674 foreach ($ar_reset_items as $item => $default_value)
1675 {
Derek Jones0b59f272008-05-13 04:22:33 +00001676 if ( ! in_array($item, $this->ar_store_array))
Derek Allard9b3e7b52008-02-04 23:20:34 +00001677 {
1678 $this->$item = $default_value;
1679 }
1680 }
1681 }
1682
1683 // --------------------------------------------------------------------
1684
1685 /**
Derek Allard09de1852007-02-14 01:35:56 +00001686 * Resets the active record values. Called by the get() function
1687 *
1688 * @access private
1689 * @return void
1690 */
1691 function _reset_select()
1692 {
Derek Allard9b3e7b52008-02-04 23:20:34 +00001693 $ar_reset_items = array(
1694 'ar_select' => array(),
1695 'ar_from' => array(),
1696 'ar_join' => array(),
1697 'ar_where' => array(),
1698 'ar_like' => array(),
1699 'ar_groupby' => array(),
1700 'ar_having' => array(),
1701 'ar_orderby' => array(),
1702 'ar_wherein' => array(),
1703 'ar_aliased_tables' => array(),
1704 'ar_distinct' => FALSE,
1705 'ar_limit' => FALSE,
1706 'ar_offset' => FALSE,
1707 'ar_order' => FALSE,
1708 );
1709
1710 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001711 }
1712
1713 // --------------------------------------------------------------------
1714
1715 /**
1716 * Resets the active record "write" values.
1717 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001718 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001719 *
1720 * @access private
1721 * @return void
1722 */
1723 function _reset_write()
Derek Allard9b3e7b52008-02-04 23:20:34 +00001724 {
1725 $ar_reset_items = array(
1726 'ar_set' => array(),
1727 'ar_from' => array(),
1728 'ar_where' => array(),
1729 'ar_like' => array(),
1730 'ar_orderby' => array(),
1731 'ar_limit' => FALSE,
1732 'ar_order' => FALSE
1733 );
1734
1735 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001736 }
1737
1738}
Derek Allarda6325892008-05-12 17:51:47 +00001739
1740/* End of file DB_active_rec.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00001741/* Location: ./system/database/DB_active_rec.php */