blob: 4eff400c1d256d09ad2496ecffed71f5e0e56f86 [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
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, 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
439 if ($escape === TRUE)
440 {
441 // exception for "field<=" keys
442 if ($this->_has_operator($k))
443 {
444 $k = preg_replace("/([A-Za-z_0-9]+)/", $this->_protect_identifiers('$1'), $k);
445 }
446 else
447 {
448 $k = $this->_protect_identifiers($k);
449 }
Derek Allardd8364c42008-06-04 17:01:00 +0000450
451 $v = ' '.$this->escape($v);
452
Derek Allard39b622d2008-01-16 21:10:09 +0000453 }
454
Derek Jones0b59f272008-05-13 04:22:33 +0000455 if ( ! $this->_has_operator($k))
Derek Allard09de1852007-02-14 01:35:56 +0000456 {
457 $k .= ' =';
458 }
Derek Allard5fe155e2008-05-12 19:14:57 +0000459
Derek Allard09de1852007-02-14 01:35:56 +0000460 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000461 else
462 {
Derek Allard5fe155e2008-05-12 19:14:57 +0000463
Derek Allard16629b12008-04-06 19:58:20 +0000464 if ($escape === TRUE)
465 {
466 $k = $this->_protect_identifiers($k, TRUE);
467 }
468
Derek Allard9b3e7b52008-02-04 23:20:34 +0000469 }
470
Derek Allard09de1852007-02-14 01:35:56 +0000471 $this->ar_where[] = $prefix.$k.$v;
Derek Allardd8364c42008-06-04 17:01:00 +0000472
Derek Allard9b3e7b52008-02-04 23:20:34 +0000473 if ($this->ar_caching === TRUE)
474 {
475 $this->ar_cache_where[] = $prefix.$k.$v;
476 }
477
Derek Allard09de1852007-02-14 01:35:56 +0000478 }
479 return $this;
480 }
Derek Allard80dd7022007-12-18 23:55:06 +0000481
482 // --------------------------------------------------------------------
483
484 /**
485 * Where_in
486 *
Derek Allardc6935512007-12-19 14:23:19 +0000487 * Generates a WHERE field IN ('item', 'item') SQL query joined with
488 * AND if appropriate
Derek Allard80dd7022007-12-18 23:55:06 +0000489 *
490 * @access public
491 * @param string The field to search
492 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000493 * @return object
494 */
495 function where_in($key = NULL, $values = NULL)
496 {
497 return $this->_where_in($key, $values);
498 }
499
500 // --------------------------------------------------------------------
501
502 /**
503 * Where_in_or
504 *
505 * Generates a WHERE field IN ('item', 'item') SQL query joined with
506 * OR if appropriate
507 *
508 * @access public
509 * @param string The field to search
510 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000511 * @return object
512 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000513 function or_where_in($key = NULL, $values = NULL)
Derek Allard5fe155e2008-05-12 19:14:57 +0000514 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000515 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000516 }
517
518 // --------------------------------------------------------------------
519
520 /**
521 * Where_not_in
522 *
523 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
524 * with AND if appropriate
525 *
526 * @access public
527 * @param string The field to search
528 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000529 * @return object
530 */
531 function where_not_in($key = NULL, $values = NULL)
Derek Allard5fe155e2008-05-12 19:14:57 +0000532 {
Derek Allardc6935512007-12-19 14:23:19 +0000533 return $this->_where_in($key, $values, TRUE);
534 }
535
536 // --------------------------------------------------------------------
537
538 /**
539 * Where_not_in_or
540 *
541 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
542 * with OR if appropriate
543 *
544 * @access public
545 * @param string The field to search
546 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000547 * @return object
548 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000549 function or_where_not_in($key = NULL, $values = NULL)
Derek Allard5fe155e2008-05-12 19:14:57 +0000550 {
Derek Jonesd0072432008-05-07 22:06:51 +0000551 return $this->_where_in($key, $values, TRUE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000552 }
553
554 // --------------------------------------------------------------------
555
556 /**
557 * Where_in
558 *
559 * Called by where_in, where_in_or, where_not_in, where_not_in_or
560 *
561 * @access public
562 * @param string The field to search
563 * @param array The values searched on
564 * @param boolean If the statement whould be IN or NOT IN
Derek Allard80dd7022007-12-18 23:55:06 +0000565 * @param string
566 * @return object
567 */
Derek Allard15ddc9d2007-12-20 13:54:39 +0000568 function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard5fe155e2008-05-12 19:14:57 +0000569 {
Derek Jones0b59f272008-05-13 04:22:33 +0000570 if ($key === NULL OR ! is_array($values))
Derek Allard80dd7022007-12-18 23:55:06 +0000571 {
572 return;
573 }
574
Derek Allardc6935512007-12-19 14:23:19 +0000575 $not = ($not) ? ' NOT ' : '';
Derek Allard80dd7022007-12-18 23:55:06 +0000576
577 foreach ($values as $value)
578 {
579 $this->ar_wherein[] = $this->escape($value);
580 }
581
582 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Derek Allard5fe155e2008-05-12 19:14:57 +0000583
Derek Allard9b3e7b52008-02-04 23:20:34 +0000584 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
585
586 $this->ar_where[] = $where_in;
587 if ($this->ar_caching === TRUE)
588 {
589 $this->ar_cache_where[] = $where_in;
590 }
Derek Allard80dd7022007-12-18 23:55:06 +0000591
Derek Allard8f000212008-01-18 14:45:59 +0000592 // reset the array for multiple calls
593 $this->ar_wherein = array();
Derek Allard80dd7022007-12-18 23:55:06 +0000594 return $this;
595 }
596
Derek Allard09de1852007-02-14 01:35:56 +0000597 // --------------------------------------------------------------------
598
599 /**
600 * Like
601 *
602 * Generates a %LIKE% portion of the query. Separates
603 * multiple calls with AND
604 *
605 * @access public
606 * @param mixed
607 * @param mixed
608 * @return object
609 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000610 function like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000611 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000612 return $this->_like($field, $match, 'AND ', $side);
Derek Allard09de1852007-02-14 01:35:56 +0000613 }
Derek Allarde54e3d22007-12-19 15:53:44 +0000614
615 // --------------------------------------------------------------------
616
617 /**
618 * Not Like
619 *
620 * Generates a NOT LIKE portion of the query. Separates
621 * multiple calls with AND
622 *
623 * @access public
624 * @param mixed
625 * @param mixed
626 * @return object
627 */
628 function not_like($field, $match = '', $side = 'both')
629 {
630 return $this->_like($field, $match, 'AND ', $side, ' NOT');
631 }
632
Derek Allard09de1852007-02-14 01:35:56 +0000633 // --------------------------------------------------------------------
634
635 /**
636 * OR Like
637 *
638 * Generates a %LIKE% portion of the query. Separates
639 * multiple calls with OR
640 *
641 * @access public
642 * @param mixed
643 * @param mixed
644 * @return object
645 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000646 function or_like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000647 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000648 return $this->_like($field, $match, 'OR ', $side);
649 }
650
651 // --------------------------------------------------------------------
652
653 /**
Derek Allarde54e3d22007-12-19 15:53:44 +0000654 * OR Not Like
655 *
656 * Generates a NOT LIKE portion of the query. Separates
657 * multiple calls with OR
658 *
659 * @access public
660 * @param mixed
661 * @param mixed
662 * @return object
663 */
664 function or_not_like($field, $match = '', $side = 'both')
665 {
666 return $this->_like($field, $match, 'OR ', $side, 'NOT ');
667 }
668
669 // --------------------------------------------------------------------
670
671 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000672 * orlike() is an alias of or_like()
673 * this function is here for backwards compatibility, as
674 * orlike() has been deprecated
675 */
676 function orlike($field, $match = '', $side = 'both')
677 {
Derek Allard4a310b72008-01-30 21:32:47 +0000678 return $this->or_like($field, $match, $side);
Derek Allard09de1852007-02-14 01:35:56 +0000679 }
680
681 // --------------------------------------------------------------------
682
683 /**
684 * Like
685 *
686 * Called by like() or orlike()
687 *
688 * @access private
689 * @param mixed
690 * @param mixed
691 * @param string
692 * @return object
693 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000694 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard09de1852007-02-14 01:35:56 +0000695 {
Derek Jones0b59f272008-05-13 04:22:33 +0000696 if ( ! is_array($field))
Derek Allard09de1852007-02-14 01:35:56 +0000697 {
698 $field = array($field => $match);
699 }
700
701 foreach ($field as $k => $v)
Derek Allarde54e3d22007-12-19 15:53:44 +0000702 {
703
Derek Allard39b622d2008-01-16 21:10:09 +0000704 $k = $this->_protect_identifiers($k);
705
Derek Allard09de1852007-02-14 01:35:56 +0000706 $prefix = (count($this->ar_like) == 0) ? '' : $type;
Derek Allarde54e3d22007-12-19 15:53:44 +0000707
Derek Allard09de1852007-02-14 01:35:56 +0000708 $v = $this->escape_str($v);
Derek Allarde54e3d22007-12-19 15:53:44 +0000709
Derek Allard218e2bc2007-12-17 21:18:14 +0000710 if ($side == 'before')
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 elseif ($side == 'after')
715 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000716 $like_statement = $prefix." $k $not LIKE '{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000717 }
718 else
719 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000720 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000721 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000722
723 $this->ar_like[] = $like_statement;
724 if ($this->ar_caching === TRUE)
725 {
726 $this->ar_cache_like[] = $like_statement;
727 }
728
Derek Allard09de1852007-02-14 01:35:56 +0000729 }
730 return $this;
731 }
732
733 // --------------------------------------------------------------------
734
735 /**
736 * GROUP BY
737 *
738 * @access public
739 * @param string
740 * @return object
741 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000742 function group_by($by)
Derek Allard09de1852007-02-14 01:35:56 +0000743 {
744 if (is_string($by))
745 {
746 $by = explode(',', $by);
747 }
748
749 foreach ($by as $val)
750 {
751 $val = trim($val);
752
753 if ($val != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000754 {
Derek Allard39b622d2008-01-16 21:10:09 +0000755 $this->ar_groupby[] = $this->_protect_identifiers($val);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000756 if ($this->ar_caching === TRUE)
757 {
758 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
759 }
760 }
Derek Allard09de1852007-02-14 01:35:56 +0000761 }
762 return $this;
763 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000764
765 // --------------------------------------------------------------------
766
767 /**
768 * groupby() is an alias of group_by()
769 * this function is here for backwards compatibility, as
770 * groupby() has been deprecated
771 */
772 function groupby($by)
773 {
774 return $this->group_by($by);
775 }
776
Derek Allard09de1852007-02-14 01:35:56 +0000777 // --------------------------------------------------------------------
778
779 /**
780 * Sets the HAVING value
781 *
782 * Separates multiple calls with AND
783 *
784 * @access public
785 * @param string
786 * @param string
787 * @return object
788 */
Derek Allarde808aac2008-04-06 18:22:00 +0000789 function having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000790 {
Derek Allarde808aac2008-04-06 18:22:00 +0000791 return $this->_having($key, $value, 'AND ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000792 }
Derek Allard43e8cbf2008-03-01 23:14:43 +0000793
794 // --------------------------------------------------------------------
795
796 /**
797 * orhaving() is an alias of or_having()
798 * this function is here for backwards compatibility, as
799 * orhaving() has been deprecated
800 */
801
Derek Allarde808aac2008-04-06 18:22:00 +0000802 function orhaving($key, $value = '', $escape = TRUE)
Derek Allard43e8cbf2008-03-01 23:14:43 +0000803 {
Derek Allarda459b462008-05-22 13:01:39 +0000804 return $this->or_having($key, $value, $escape);
Derek Allard43e8cbf2008-03-01 23:14:43 +0000805 }
Derek Allard09de1852007-02-14 01:35:56 +0000806 // --------------------------------------------------------------------
807
808 /**
809 * Sets the OR HAVING value
810 *
811 * Separates multiple calls with OR
812 *
813 * @access public
814 * @param string
815 * @param string
816 * @return object
817 */
Derek Allarde808aac2008-04-06 18:22:00 +0000818 function or_having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000819 {
Derek Allarde808aac2008-04-06 18:22:00 +0000820 return $this->_having($key, $value, 'OR ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000821 }
822
823 // --------------------------------------------------------------------
824
825 /**
826 * Sets the HAVING values
827 *
828 * Called by having() or orhaving()
829 *
830 * @access private
831 * @param string
Derek Allard9b3e7b52008-02-04 23:20:34 +0000832
Derek Allard09de1852007-02-14 01:35:56 +0000833 * @param string
834 * @return object
835 */
Derek Allarde808aac2008-04-06 18:22:00 +0000836 function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000837 {
Derek Jones0b59f272008-05-13 04:22:33 +0000838 if ( ! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000839 {
840 $key = array($key => $value);
841 }
842
843 foreach ($key as $k => $v)
844 {
845 $prefix = (count($this->ar_having) == 0) ? '' : $type;
Derek Allarde808aac2008-04-06 18:22:00 +0000846
847 if ($escape === TRUE)
848 {
849 $k = $this->_protect_identifiers($k);
850 }
851
Derek Allarda459b462008-05-22 13:01:39 +0000852 if ( ! $this->_has_operator($k))
853 {
854 $k .= ' = ';
855 }
856
Derek Allard09de1852007-02-14 01:35:56 +0000857 if ($v != '')
858 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000859 $v = ' '.$this->escape_str($v);
Derek Allard09de1852007-02-14 01:35:56 +0000860 }
861
862 $this->ar_having[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000863 if ($this->ar_caching === TRUE)
864 {
865 $this->ar_cache_having[] = $prefix.$k.$v;
866 }
Derek Allard09de1852007-02-14 01:35:56 +0000867 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000868
Derek Allard09de1852007-02-14 01:35:56 +0000869 return $this;
870 }
871
872 // --------------------------------------------------------------------
873
874 /**
875 * Sets the ORDER BY value
876 *
877 * @access public
878 * @param string
879 * @param string direction: asc or desc
880 * @return object
881 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000882 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000883 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000884 if (strtolower($direction) == 'random')
885 {
886 $orderby = ''; // Random results want or don't need a field name
887 $direction = $this->_random_keyword;
888 }
889 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000890 {
Derek Allard92782492007-08-10 11:26:01 +0000891 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000892 }
893
Derek Allard9b3e7b52008-02-04 23:20:34 +0000894 $orderby_statement = $this->_protect_identifiers($orderby, TRUE).$direction;
895
896 $this->ar_orderby[] = $orderby_statement;
897 if ($this->ar_caching === TRUE)
898 {
899 $this->ar_cache_orderby[] = $orderby_statement;
900 }
901
Derek Allard09de1852007-02-14 01:35:56 +0000902 return $this;
903 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000904
Derek Allard218e2bc2007-12-17 21:18:14 +0000905 // --------------------------------------------------------------------
906
907 /**
908 * orderby() is an alias of order_by()
909 * this function is here for backwards compatibility, as
910 * orderby() has been deprecated
911 */
912 function orderby($orderby, $direction = '')
913 {
914 return $this->order_by($orderby, $direction);
915 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000916
Derek Allard09de1852007-02-14 01:35:56 +0000917 // --------------------------------------------------------------------
918
919 /**
920 * Sets the LIMIT value
921 *
922 * @access public
923 * @param integer the limit value
924 * @param integer the offset value
925 * @return object
926 */
927 function limit($value, $offset = '')
928 {
929 $this->ar_limit = $value;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000930 if ($this->ar_caching === TRUE)
931 {
932 $this->ar_cache_limit[] = $value;
933 }
934
Derek Allard09de1852007-02-14 01:35:56 +0000935 if ($offset != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000936 {
Derek Allard09de1852007-02-14 01:35:56 +0000937 $this->ar_offset = $offset;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000938 if ($this->ar_caching === TRUE)
939 {
940 $this->ar_cache_offset[] = $offset;
941 }
942 }
Derek Allard09de1852007-02-14 01:35:56 +0000943
944 return $this;
945 }
946
947 // --------------------------------------------------------------------
948
949 /**
950 * Sets the OFFSET value
951 *
952 * @access public
953 * @param integer the offset value
954 * @return object
955 */
Derek Allard9b3e7b52008-02-04 23:20:34 +0000956 function offset($offset)
Derek Allard09de1852007-02-14 01:35:56 +0000957 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000958 $this->ar_offset = $offset;
959 if ($this->ar_caching === TRUE)
960 {
961 $this->ar_cache_offset[] = $offset;
962 }
963
Derek Allard09de1852007-02-14 01:35:56 +0000964 return $this;
965 }
966
967 // --------------------------------------------------------------------
968
969 /**
970 * The "set" function. Allows key/value pairs to be set for inserting or updating
971 *
972 * @access public
973 * @param mixed
974 * @param string
Derek Allard39b622d2008-01-16 21:10:09 +0000975 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +0000976 * @return object
977 */
Derek Allard39b622d2008-01-16 21:10:09 +0000978 function set($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000979 {
980 $key = $this->_object_to_array($key);
981
Derek Jones0b59f272008-05-13 04:22:33 +0000982 if ( ! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000983 {
984 $key = array($key => $value);
985 }
986
987 foreach ($key as $k => $v)
988 {
Derek Allard39b622d2008-01-16 21:10:09 +0000989 if ($escape === FALSE)
990 {
991 $this->ar_set[$this->_protect_identifiers($k)] = $v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000992 if ($this->ar_caching === TRUE)
993 {
994 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $v;
995 }
Derek Allard39b622d2008-01-16 21:10:09 +0000996 }
997 else
998 {
999 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
Derek Allard9b3e7b52008-02-04 23:20:34 +00001000 if ($this->ar_caching === TRUE)
1001 {
1002 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $this->escape($v);
1003 }
Derek Allard39b622d2008-01-16 21:10:09 +00001004 }
Derek Allard09de1852007-02-14 01:35:56 +00001005 }
1006
1007 return $this;
1008 }
1009
1010 // --------------------------------------------------------------------
1011
1012 /**
1013 * Get
1014 *
1015 * Compiles the select statement based on the other functions called
1016 * and runs the query
1017 *
1018 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +00001019 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +00001020 * @param string the limit clause
1021 * @param string the offset clause
1022 * @return object
1023 */
1024 function get($table = '', $limit = null, $offset = null)
1025 {
1026 if ($table != '')
1027 {
Derek Allard5e128942007-12-28 21:33:03 +00001028 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001029 $this->from($table);
1030 }
1031
Derek Jones0b59f272008-05-13 04:22:33 +00001032 if ( ! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001033 {
1034 $this->limit($limit, $offset);
1035 }
1036
1037 $sql = $this->_compile_select();
1038
1039 $result = $this->query($sql);
1040 $this->_reset_select();
1041 return $result;
1042 }
1043
Derek Allard09de1852007-02-14 01:35:56 +00001044 /**
Derek Allard694b5b82007-12-18 15:58:03 +00001045 * "Count All Results" query
1046 *
1047 * Generates a platform-specific query string that counts all records
1048 * returned by an Active Record query.
1049 *
1050 * @access public
1051 * @param string
1052 * @return string
1053 */
1054 function count_all_results($table = '')
1055 {
1056 if ($table != '')
1057 {
Derek Allard5e128942007-12-28 21:33:03 +00001058 $this->_track_aliases($table);
Derek Allard694b5b82007-12-18 15:58:03 +00001059 $this->from($table);
1060 }
1061
Derek Allard39b622d2008-01-16 21:10:09 +00001062 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
Derek Allard694b5b82007-12-18 15:58:03 +00001063
1064 $query = $this->query($sql);
1065 $this->_reset_select();
1066
1067 if ($query->num_rows() == 0)
1068 {
1069 return '0';
1070 }
1071
1072 $row = $query->row();
1073 return $row->numrows;
1074 }
1075
1076 // --------------------------------------------------------------------
1077
1078 /**
Derek Allard218e2bc2007-12-17 21:18:14 +00001079 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +00001080 *
1081 * Allows the where clause, limit and offset to be added directly
1082 *
1083 * @access public
1084 * @param string the where clause
1085 * @param string the limit clause
1086 * @param string the offset clause
1087 * @return object
1088 */
Derek Allard218e2bc2007-12-17 21:18:14 +00001089 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +00001090 {
1091 if ($table != '')
1092 {
Derek Allard5e128942007-12-28 21:33:03 +00001093 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001094 $this->from($table);
1095 }
1096
Derek Jones0b59f272008-05-13 04:22:33 +00001097 if ( ! is_null($where))
Derek Allard09de1852007-02-14 01:35:56 +00001098 {
1099 $this->where($where);
1100 }
1101
Derek Jones0b59f272008-05-13 04:22:33 +00001102 if ( ! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001103 {
1104 $this->limit($limit, $offset);
1105 }
1106
1107 $sql = $this->_compile_select();
1108
1109 $result = $this->query($sql);
1110 $this->_reset_select();
1111 return $result;
1112 }
Derek Allard218e2bc2007-12-17 21:18:14 +00001113
1114 // --------------------------------------------------------------------
1115
1116 /**
1117 * getwhere() is an alias of get_where()
1118 * this function is here for backwards compatibility, as
1119 * getwhere() has been deprecated
1120 */
1121 function getwhere($table = '', $where = null, $limit = null, $offset = null)
1122 {
1123 return $this->get_where($table, $where, $limit, $offset);
1124 }
Derek Allard09de1852007-02-14 01:35:56 +00001125
1126 // --------------------------------------------------------------------
1127
1128 /**
1129 * Insert
1130 *
1131 * Compiles an insert string and runs the query
1132 *
1133 * @access public
1134 * @param string the table to retrieve the results from
1135 * @param array an associative array of insert values
1136 * @return object
1137 */
1138 function insert($table = '', $set = NULL)
1139 {
Derek Jones0b59f272008-05-13 04:22:33 +00001140 if ( ! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001141 {
1142 $this->set($set);
1143 }
1144
1145 if (count($this->ar_set) == 0)
1146 {
1147 if ($this->db_debug)
1148 {
1149 return $this->display_error('db_must_use_set');
1150 }
1151 return FALSE;
1152 }
1153
1154 if ($table == '')
1155 {
Derek Jones0b59f272008-05-13 04:22:33 +00001156 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001157 {
1158 if ($this->db_debug)
1159 {
1160 return $this->display_error('db_must_set_table');
1161 }
1162 return FALSE;
1163 }
1164
1165 $table = $this->ar_from[0];
1166 }
Derek Allard39b622d2008-01-16 21:10:09 +00001167
1168 $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 +00001169
1170 $this->_reset_write();
1171 return $this->query($sql);
1172 }
1173
1174 // --------------------------------------------------------------------
1175
1176 /**
1177 * Update
1178 *
1179 * Compiles an update string and runs the query
1180 *
1181 * @access public
1182 * @param string the table to retrieve the results from
1183 * @param array an associative array of update values
1184 * @param mixed the where clause
1185 * @return object
1186 */
Derek Allard5e128942007-12-28 21:33:03 +00001187 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001188 {
Derek Jones0b59f272008-05-13 04:22:33 +00001189 if ( ! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001190 {
1191 $this->set($set);
1192 }
1193
1194 if (count($this->ar_set) == 0)
1195 {
1196 if ($this->db_debug)
1197 {
1198 return $this->display_error('db_must_use_set');
1199 }
1200 return FALSE;
1201 }
1202
1203 if ($table == '')
1204 {
Derek Jones0b59f272008-05-13 04:22:33 +00001205 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001206 {
1207 if ($this->db_debug)
1208 {
1209 return $this->display_error('db_must_set_table');
1210 }
1211 return FALSE;
1212 }
1213
1214 $table = $this->ar_from[0];
1215 }
1216
Derek Allarde77d77c2007-12-19 15:01:55 +00001217 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001218 {
1219 $this->where($where);
1220 }
Derek Allardda6d2402007-12-19 14:49:29 +00001221
Derek Allarde77d77c2007-12-19 15:01:55 +00001222 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +00001223 {
1224 $this->limit($limit);
1225 }
Derek Allard09de1852007-02-14 01:35:56 +00001226
Derek Allard39b622d2008-01-16 21:10:09 +00001227 $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 +00001228
1229 $this->_reset_write();
1230 return $this->query($sql);
1231 }
Derek Allard39b622d2008-01-16 21:10:09 +00001232
1233 // --------------------------------------------------------------------
1234
1235 /**
1236 * Empty Table
1237 *
1238 * Compiles a delete string and runs "DELETE FROM table"
1239 *
1240 * @access public
1241 * @param string the table to empty
1242 * @return object
1243 */
1244 function empty_table($table = '')
1245 {
1246 if ($table == '')
1247 {
Derek Jones0b59f272008-05-13 04:22:33 +00001248 if ( ! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001249 {
1250 if ($this->db_debug)
1251 {
1252 return $this->display_error('db_must_set_table');
1253 }
1254 return FALSE;
1255 }
1256
1257 $table = $this->ar_from[0];
1258 }
1259 else
1260 {
1261 $table = $this->_protect_identifiers($this->dbprefix.$table);
1262 }
1263
1264
1265 $sql = $this->_delete($table);
1266
1267 $this->_reset_write();
1268
1269 return $this->query($sql);
1270 }
1271
1272 // --------------------------------------------------------------------
1273
1274 /**
1275 * Truncate
1276 *
1277 * Compiles a truncate string and runs the query
1278 * If the database does not support the truncate() command
1279 * This function maps to "DELETE FROM table"
1280 *
1281 * @access public
1282 * @param string the table to truncate
1283 * @return object
1284 */
1285 function truncate($table = '')
1286 {
1287 if ($table == '')
1288 {
Derek Jones0b59f272008-05-13 04:22:33 +00001289 if ( ! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001290 {
1291 if ($this->db_debug)
1292 {
1293 return $this->display_error('db_must_set_table');
1294 }
1295 return FALSE;
1296 }
1297
1298 $table = $this->ar_from[0];
1299 }
1300 else
1301 {
1302 $table = $this->_protect_identifiers($this->dbprefix.$table);
1303 }
1304
1305
1306 $sql = $this->_truncate($table);
1307
1308 $this->_reset_write();
1309
1310 return $this->query($sql);
1311 }
Derek Allard09de1852007-02-14 01:35:56 +00001312
1313 // --------------------------------------------------------------------
1314
1315 /**
1316 * Delete
1317 *
1318 * Compiles a delete string and runs the query
1319 *
1320 * @access public
Derek Allard41f60d42007-12-20 20:09:22 +00001321 * @param mixed the table(s) to delete from. String or array
Derek Allard09de1852007-02-14 01:35:56 +00001322 * @param mixed the where clause
Derek Allard41f60d42007-12-20 20:09:22 +00001323 * @param mixed the limit clause
1324 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +00001325 * @return object
1326 */
Derek Allard41f60d42007-12-20 20:09:22 +00001327 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +00001328 {
1329 if ($table == '')
1330 {
Derek Jones0b59f272008-05-13 04:22:33 +00001331 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001332 {
1333 if ($this->db_debug)
1334 {
1335 return $this->display_error('db_must_set_table');
1336 }
1337 return FALSE;
1338 }
Derek Allard39b622d2008-01-16 21:10:09 +00001339
Derek Allard09de1852007-02-14 01:35:56 +00001340 $table = $this->ar_from[0];
1341 }
Derek Allard39b622d2008-01-16 21:10:09 +00001342 elseif (is_array($table))
Derek Allard41f60d42007-12-20 20:09:22 +00001343 {
1344 foreach($table as $single_table)
1345 {
Derek Allard39b622d2008-01-16 21:10:09 +00001346 $this->delete($single_table, $where, $limit, FALSE);
Derek Allard41f60d42007-12-20 20:09:22 +00001347 }
Derek Allard39b622d2008-01-16 21:10:09 +00001348
Derek Allard41f60d42007-12-20 20:09:22 +00001349 $this->_reset_write();
1350 return;
1351 }
Derek Allard39b622d2008-01-16 21:10:09 +00001352 else
1353 {
1354 $table = $this->_protect_identifiers($this->dbprefix.$table);
1355 }
Derek Allard41f60d42007-12-20 20:09:22 +00001356
Derek Allard09de1852007-02-14 01:35:56 +00001357 if ($where != '')
1358 {
1359 $this->where($where);
1360 }
1361
Derek Allarde77d77c2007-12-19 15:01:55 +00001362 if ($limit != NULL)
1363 {
1364 $this->limit($limit);
1365 }
1366
Derek Allard39b622d2008-01-16 21:10:09 +00001367 if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
Derek Allard09de1852007-02-14 01:35:56 +00001368 {
1369 if ($this->db_debug)
1370 {
1371 return $this->display_error('db_del_must_use_where');
1372 }
Derek Allard39b622d2008-01-16 21:10:09 +00001373
Derek Allard09de1852007-02-14 01:35:56 +00001374 return FALSE;
1375 }
Derek Allard41f60d42007-12-20 20:09:22 +00001376
Derek Allard39b622d2008-01-16 21:10:09 +00001377 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001378
Derek Allard41f60d42007-12-20 20:09:22 +00001379 if ($reset_data)
1380 {
1381 $this->_reset_write();
1382 }
Derek Allard39b622d2008-01-16 21:10:09 +00001383
Derek Allard09de1852007-02-14 01:35:56 +00001384 return $this->query($sql);
1385 }
Derek Allard15ddc9d2007-12-20 13:54:39 +00001386
Derek Allard09de1852007-02-14 01:35:56 +00001387 // --------------------------------------------------------------------
1388
1389 /**
1390 * Use Table - DEPRECATED
1391 *
1392 * @deprecated use $this->db->from instead
1393 */
1394 function use_table($table)
1395 {
1396 return $this->from($table);
Derek Allard09de1852007-02-14 01:35:56 +00001397 }
Derek Allard09de1852007-02-14 01:35:56 +00001398
Derek Allard09de1852007-02-14 01:35:56 +00001399 // --------------------------------------------------------------------
1400
1401 /**
Derek Allard5e128942007-12-28 21:33:03 +00001402 * Track Aliases
1403 *
1404 * Used to track SQL statements written with aliased tables.
1405 *
1406 * @access private
1407 * @param string The table to inspect
1408 * @return string
1409 */
1410 function _track_aliases($table)
1411 {
1412 // if a table alias is used we can recognize it by a space
1413 if (strpos($table, " ") !== FALSE)
1414 {
1415 // if the alias is written with the AS keyowrd, get it out
Derek Allard39b622d2008-01-16 21:10:09 +00001416 $table = preg_replace('/ AS /i', ' ', $table);
Derek Allard5e128942007-12-28 21:33:03 +00001417
Derek Allard39b622d2008-01-16 21:10:09 +00001418 $this->ar_aliased_tables[] = trim(strrchr($table, " "));
Derek Allard5e128942007-12-28 21:33:03 +00001419 }
1420
1421 return $this->dbprefix.$table;
1422 }
1423
1424 // --------------------------------------------------------------------
1425
1426 /**
1427 * Filter Table Aliases
1428 *
1429 * Intelligently removes database prefixes from aliased tables
1430 *
1431 * @access private
1432 * @param array An array of compiled SQL
1433 * @return array Cleaned up statement with aliases accounted for
1434 */
1435 function _filter_table_aliases($statements)
1436 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001437
Derek Allard39b622d2008-01-16 21:10:09 +00001438 foreach ($statements as $k => $v)
Derek Allard5e128942007-12-28 21:33:03 +00001439 {
Derek Allard39b622d2008-01-16 21:10:09 +00001440 foreach ($this->ar_aliased_tables as $table)
Derek Allard5e128942007-12-28 21:33:03 +00001441 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001442 $statements[$k] = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $statements[$k]); // makes `table.field`
1443 $statements[$k] = str_replace($this->dbprefix.$table.'.', $table.'.', $statements[$k]);
Derek Allard5e128942007-12-28 21:33:03 +00001444 }
Derek Allard5e128942007-12-28 21:33:03 +00001445 }
Derek Allard39b622d2008-01-16 21:10:09 +00001446 return $statements;
Derek Allard5e128942007-12-28 21:33:03 +00001447 }
1448
1449 // --------------------------------------------------------------------
1450
1451 /**
Derek Allard09de1852007-02-14 01:35:56 +00001452 * Compile the SELECT statement
1453 *
1454 * Generates a query string based on which functions were used.
1455 * Should not be called directly. The get() function calls it.
1456 *
1457 * @access private
1458 * @return string
1459 */
Derek Allard694b5b82007-12-18 15:58:03 +00001460 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001461 {
Derek Allard78255262008-02-06 13:54:23 +00001462 $this->_merge_cache();
Derek Allard9b3e7b52008-02-04 23:20:34 +00001463
Derek Jones0b59f272008-05-13 04:22:33 +00001464 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Derek Allard09de1852007-02-14 01:35:56 +00001465
Derek Allard5162fc72008-04-15 20:05:37 +00001466 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->_filter_table_aliases($this->ar_select));
Derek Allard09de1852007-02-14 01:35:56 +00001467
Derek Allard694b5b82007-12-18 15:58:03 +00001468 if ($select_override !== FALSE)
1469 {
1470 $sql = $select_override;
1471 }
1472
Derek Allard09de1852007-02-14 01:35:56 +00001473 if (count($this->ar_from) > 0)
1474 {
1475 $sql .= "\nFROM ";
Derek Jonesc6ad0232008-01-29 18:44:54 +00001476 $sql .= $this->_from_tables($this->ar_from);
Derek Allard09de1852007-02-14 01:35:56 +00001477 }
1478
1479 if (count($this->ar_join) > 0)
Derek Allard5e128942007-12-28 21:33:03 +00001480 {
Derek Allard09de1852007-02-14 01:35:56 +00001481 $sql .= "\n";
Derek Allard5e128942007-12-28 21:33:03 +00001482
1483 // special consideration for table aliases
1484 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1485 {
1486 $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
1487 }
1488 else
1489 {
1490 $sql .= implode("\n", $this->ar_join);
1491 }
1492
Derek Allard09de1852007-02-14 01:35:56 +00001493 }
1494
1495 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1496 {
1497 $sql .= "\nWHERE ";
1498 }
1499
1500 $sql .= implode("\n", $this->ar_where);
1501
1502 if (count($this->ar_like) > 0)
1503 {
1504 if (count($this->ar_where) > 0)
1505 {
1506 $sql .= " AND ";
1507 }
1508
1509 $sql .= implode("\n", $this->ar_like);
1510 }
1511
1512 if (count($this->ar_groupby) > 0)
1513 {
Derek Allard5e128942007-12-28 21:33:03 +00001514
Derek Allard09de1852007-02-14 01:35:56 +00001515 $sql .= "\nGROUP BY ";
Derek Allard5e128942007-12-28 21:33:03 +00001516
1517 // special consideration for table aliases
1518 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1519 {
1520 $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
1521 }
1522 else
1523 {
1524 $sql .= implode(', ', $this->ar_groupby);
1525 }
Derek Allard09de1852007-02-14 01:35:56 +00001526 }
1527
1528 if (count($this->ar_having) > 0)
1529 {
1530 $sql .= "\nHAVING ";
1531 $sql .= implode("\n", $this->ar_having);
1532 }
1533
1534 if (count($this->ar_orderby) > 0)
1535 {
1536 $sql .= "\nORDER BY ";
1537 $sql .= implode(', ', $this->ar_orderby);
1538
1539 if ($this->ar_order !== FALSE)
1540 {
1541 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1542 }
1543 }
1544
1545 if (is_numeric($this->ar_limit))
1546 {
1547 $sql .= "\n";
1548 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1549 }
1550
1551 return $sql;
1552 }
1553
1554 // --------------------------------------------------------------------
1555
1556 /**
1557 * Object to Array
1558 *
1559 * Takes an object as input and converts the class variables to array key/vals
1560 *
1561 * @access public
1562 * @param object
1563 * @return array
1564 */
1565 function _object_to_array($object)
1566 {
Derek Jones0b59f272008-05-13 04:22:33 +00001567 if ( ! is_object($object))
Derek Allard09de1852007-02-14 01:35:56 +00001568 {
1569 return $object;
1570 }
1571
1572 $array = array();
1573 foreach (get_object_vars($object) as $key => $val)
1574 {
Derek Allard848b7762007-12-31 16:43:05 +00001575 // There are some built in keys we need to ignore for this conversion
Derek Jones0b59f272008-05-13 04:22:33 +00001576 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
Derek Allard848b7762007-12-31 16:43:05 +00001577
Derek Allard09de1852007-02-14 01:35:56 +00001578 {
1579 $array[$key] = $val;
1580 }
1581 }
1582
1583 return $array;
1584 }
1585
1586 // --------------------------------------------------------------------
1587
1588 /**
Derek Allard9b3e7b52008-02-04 23:20:34 +00001589 * Start Cache
1590 *
1591 * Starts AR caching
1592 *
1593 * @access public
1594 * @return void
1595 */
1596 function start_cache()
1597 {
1598 $this->ar_caching = TRUE;
1599 }
1600
1601 // --------------------------------------------------------------------
1602
1603 /**
1604 * Stop Cache
1605 *
1606 * Stops AR caching
1607 *
1608 * @access public
1609 * @return void
1610 */
1611 function stop_cache()
1612 {
1613 $this->ar_caching = FALSE;
1614 }
1615
1616
1617 // --------------------------------------------------------------------
1618
1619 /**
1620 * Flush Cache
1621 *
1622 * Empties the AR cache
1623 *
1624 * @access public
1625 * @return void
1626 */
1627 function flush_cache()
1628 {
1629 $ar_reset_items = array(
1630 'ar_cache_select' => array(),
1631 'ar_cache_from' => array(),
1632 'ar_cache_join' => array(),
1633 'ar_cache_where' => array(),
1634 'ar_cache_like' => array(),
1635 'ar_cache_groupby' => array(),
1636 'ar_cache_having' =>array(),
1637 'ar_cache_orderby' => array(),
1638 'ar_cache_set' => array()
1639 );
1640
1641 $this->_reset_run($ar_reset_items);
1642 }
1643
1644 // --------------------------------------------------------------------
1645
1646 /**
1647 * Merge Cache
1648 *
1649 * When called, this function merges any cached AR arrays with
1650 * locally called ones.
1651 *
1652 * @access private
1653 * @return void
1654 */
1655 function _merge_cache()
1656 {
1657 $ar_items = array('select', 'from', 'join', 'where', 'like', 'groupby', 'having', 'orderby', 'set');
1658
1659 foreach ($ar_items as $ar_item)
1660 {
1661 $ar_cache_item = 'ar_cache_'.$ar_item;
1662 $ar_item = 'ar_'.$ar_item;
1663 $this->$ar_item = array_unique(array_merge($this->$ar_item, $this->$ar_cache_item));
1664 }
1665 }
1666
1667 // --------------------------------------------------------------------
1668
1669 /**
1670 * Resets the active record values. Called by the get() function
1671 *
1672 * @access private
1673 * @param array An array of fields to reset
1674 * @return void
1675 */
1676 function _reset_run($ar_reset_items)
1677 {
1678 foreach ($ar_reset_items as $item => $default_value)
1679 {
Derek Jones0b59f272008-05-13 04:22:33 +00001680 if ( ! in_array($item, $this->ar_store_array))
Derek Allard9b3e7b52008-02-04 23:20:34 +00001681 {
1682 $this->$item = $default_value;
1683 }
1684 }
1685 }
1686
1687 // --------------------------------------------------------------------
1688
1689 /**
Derek Allard09de1852007-02-14 01:35:56 +00001690 * Resets the active record values. Called by the get() function
1691 *
1692 * @access private
1693 * @return void
1694 */
1695 function _reset_select()
1696 {
Derek Allard9b3e7b52008-02-04 23:20:34 +00001697 $ar_reset_items = array(
1698 'ar_select' => array(),
1699 'ar_from' => array(),
1700 'ar_join' => array(),
1701 'ar_where' => array(),
1702 'ar_like' => array(),
1703 'ar_groupby' => array(),
1704 'ar_having' => array(),
1705 'ar_orderby' => array(),
1706 'ar_wherein' => array(),
1707 'ar_aliased_tables' => array(),
1708 'ar_distinct' => FALSE,
1709 'ar_limit' => FALSE,
1710 'ar_offset' => FALSE,
1711 'ar_order' => FALSE,
1712 );
1713
1714 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001715 }
1716
1717 // --------------------------------------------------------------------
1718
1719 /**
1720 * Resets the active record "write" values.
1721 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001722 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001723 *
1724 * @access private
1725 * @return void
1726 */
1727 function _reset_write()
Derek Allard9b3e7b52008-02-04 23:20:34 +00001728 {
1729 $ar_reset_items = array(
1730 'ar_set' => array(),
1731 'ar_from' => array(),
1732 'ar_where' => array(),
1733 'ar_like' => array(),
1734 'ar_orderby' => array(),
1735 'ar_limit' => FALSE,
1736 'ar_order' => FALSE
1737 );
1738
1739 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001740 }
1741
1742}
Derek Allarda6325892008-05-12 17:51:47 +00001743
1744/* End of file DB_active_rec.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00001745/* Location: ./system/database/DB_active_rec.php */