blob: af042dcd2b3a32662d6448572246a4b23027abb0 [file] [log] [blame]
Derek Allard09de1852007-02-14 01:35:56 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
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
72 */
73 function dbprefix($table = '')
74 {
75 if ($table == '')
76 {
77 $this->display_error('db_table_name_required');
78 }
79
80 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 {
98 $select = explode(',', $select);
99 }
100
101 foreach ($select as $val)
102 {
103 $val = trim($val);
Derek Allard39b622d2008-01-16 21:10:09 +0000104
105 if ($val != '*' && $protect_identifiers !== FALSE)
106 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000107 if (strpos($val, '.') !== FALSE)
108 {
109 $val = $this->dbprefix.$val;
110 }
111 else
112 {
113 $val = $this->_protect_identifiers($val);
114 }
Derek Allard39b622d2008-01-16 21:10:09 +0000115 }
Derek Allard09de1852007-02-14 01:35:56 +0000116
117 if ($val != '')
Derek Allard39b622d2008-01-16 21:10:09 +0000118 {
Derek Allard09de1852007-02-14 01:35:56 +0000119 $this->ar_select[] = $val;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000120 if ($this->ar_caching === TRUE)
121 {
122 $this->ar_cache_select[] = $val;
123 }
Derek Allard39b622d2008-01-16 21:10:09 +0000124 }
Derek Allard09de1852007-02-14 01:35:56 +0000125 }
126 return $this;
127 }
Derek Allard39b622d2008-01-16 21:10:09 +0000128
129 // --------------------------------------------------------------------
130
131 /**
132 * Select Max
133 *
134 * Generates a SELECT MAX(field) portion of a query
135 *
136 * @access public
137 * @param string the field
138 * @param string an alias
139 * @return object
140 */
141 function select_max($select = '', $alias='')
142 {
143 if (!is_string($select) || $select == '')
144 {
145 $this->display_error('db_invalid_query');
146 }
Derek Allard09de1852007-02-14 01:35:56 +0000147
Derek Allard39b622d2008-01-16 21:10:09 +0000148 $alias = ($alias != '') ? $alias : $select;
149
150 $sql = 'MAX('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
151
152 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000153 if ($this->ar_caching === TRUE)
154 {
155 $this->ar_cache_select[] = $sql;
156 }
Derek Allard39b622d2008-01-16 21:10:09 +0000157
158 return $this;
Derek Allard39b622d2008-01-16 21:10:09 +0000159 }
160
161 // --------------------------------------------------------------------
162
163 /**
164 * Select Min
165 *
166 * Generates a SELECT MIN(field) portion of a query
167 *
168 * @access public
169 * @param string the field
170 * @param string an alias
171 * @return object
172 */
173 function select_min($select = '', $alias='')
174 {
175 if (!is_string($select) || $select == '')
176 {
177 $this->display_error('db_invalid_query');
178 }
179
180 $alias = ($alias != '') ? $alias : $select;
181
182 $sql = 'MIN('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
183
184 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000185 if ($this->ar_caching === TRUE)
186 {
187 $this->ar_cache_select[] = $sql;
188 }
189
Derek Allard39b622d2008-01-16 21:10:09 +0000190 return $this;
191 }
192
193 // --------------------------------------------------------------------
194
195 /**
196 * Select Average
197 *
198 * Generates a SELECT AVG(field) portion of a query
199 *
200 * @access public
201 * @param string the field
202 * @param string an alias
203 * @return object
204 */
205 function select_avg($select = '', $alias='')
206 {
207 if (!is_string($select) || $select == '')
208 {
209 $this->display_error('db_invalid_query');
210 }
211
212 $alias = ($alias != '') ? $alias : $select;
213
214 $sql = 'AVG('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
215
216 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000217 if ($this->ar_caching === TRUE)
218 {
219 $this->ar_cache_select[] = $sql;
220 }
221
Derek Allard39b622d2008-01-16 21:10:09 +0000222 return $this;
223 }
224
225 // --------------------------------------------------------------------
226
227 /**
228 * Select Sum
229 *
230 * Generates a SELECT SUM(field) portion of a query
231 *
232 * @access public
233 * @param string the field
234 * @param string an alias
235 * @return object
236 */
237 function select_sum($select = '', $alias='')
238 {
239 if (!is_string($select) || $select == '')
240 {
241 $this->display_error('db_invalid_query');
242 }
243
244 $alias = ($alias != '') ? $alias : $select;
245
246 $sql = 'SUM('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
247
248 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000249 if ($this->ar_caching === TRUE)
250 {
251 $this->ar_cache_select[] = $sql;
252 }
253
Derek Allard39b622d2008-01-16 21:10:09 +0000254 return $this;
255 }
256
Derek Allard09de1852007-02-14 01:35:56 +0000257 // --------------------------------------------------------------------
258
259 /**
260 * DISTINCT
261 *
262 * Sets a flag which tells the query string compiler to add DISTINCT
263 *
264 * @access public
265 * @param bool
266 * @return object
267 */
268 function distinct($val = TRUE)
269 {
270 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
271 return $this;
272 }
273
274 // --------------------------------------------------------------------
275
276 /**
277 * From
278 *
279 * Generates the FROM portion of the query
280 *
281 * @access public
282 * @param mixed can be a string or array
283 * @return object
284 */
285 function from($from)
286 {
287 foreach ((array)$from as $val)
288 {
Derek Allard39b622d2008-01-16 21:10:09 +0000289 $this->ar_from[] = $this->_protect_identifiers($this->_track_aliases($val));
Derek Allard9b3e7b52008-02-04 23:20:34 +0000290 if ($this->ar_caching === TRUE)
291 {
Derek Allard9a4d1da2008-02-25 14:18:38 +0000292 $this->ar_cache_from[] = $this->_protect_identifiers($this->_track_aliases($val));
Derek Allard9b3e7b52008-02-04 23:20:34 +0000293 }
Derek Allard09de1852007-02-14 01:35:56 +0000294 }
Derek Allard5e128942007-12-28 21:33:03 +0000295
Derek Allard09de1852007-02-14 01:35:56 +0000296 return $this;
297 }
298
299 // --------------------------------------------------------------------
300
301 /**
302 * Join
303 *
304 * Generates the JOIN portion of the query
305 *
306 * @access public
307 * @param string
308 * @param string the join condition
309 * @param string the type of join
310 * @return object
311 */
312 function join($table, $cond, $type = '')
313 {
314 if ($type != '')
315 {
316 $type = strtoupper(trim($type));
317
Derek Allard73274992008-05-05 16:39:18 +0000318 if (! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
Derek Allard09de1852007-02-14 01:35:56 +0000319 {
320 $type = '';
321 }
322 else
323 {
324 $type .= ' ';
325 }
326 }
327
Derek Allard09de1852007-02-14 01:35:56 +0000328 // If a DB prefix is used we might need to add it to the column names
329 if ($this->dbprefix)
330 {
Derek Allard39b622d2008-01-16 21:10:09 +0000331 $this->_track_aliases($table);
332
Derek Allard09de1852007-02-14 01:35:56 +0000333 // First we remove any existing prefixes in the condition to avoid duplicates
334 $cond = preg_replace('|('.$this->dbprefix.')([\w\.]+)([\W\s]+)|', "$2$3", $cond);
335
336 // Next we add the prefixes to the condition
337 $cond = preg_replace('|([\w\.]+)([\W\s]+)(.+)|', $this->dbprefix . "$1$2" . $this->dbprefix . "$3", $cond);
Derek Allard5e128942007-12-28 21:33:03 +0000338 }
339
Derek Allard9b3e7b52008-02-04 23:20:34 +0000340 $join = $type.'JOIN '.$this->_protect_identifiers($this->dbprefix.$table, TRUE).' ON '.$cond;
341
342 $this->ar_join[] = $join;
343 if ($this->ar_caching === TRUE)
344 {
345 $this->ar_cache_join[] = $join;
346 }
347
Derek Allard09de1852007-02-14 01:35:56 +0000348 return $this;
349 }
350
351 // --------------------------------------------------------------------
352
353 /**
354 * Where
355 *
356 * Generates the WHERE portion of the query. Separates
357 * multiple calls with AND
358 *
359 * @access public
360 * @param mixed
361 * @param mixed
362 * @return object
363 */
Derek Allard39b622d2008-01-16 21:10:09 +0000364 function where($key, $value = NULL, $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000365 {
Derek Allard39b622d2008-01-16 21:10:09 +0000366 return $this->_where($key, $value, 'AND ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000367 }
368
369 // --------------------------------------------------------------------
370
371 /**
372 * OR Where
373 *
374 * Generates the WHERE portion of the query. Separates
375 * multiple calls with OR
376 *
377 * @access public
378 * @param mixed
379 * @param mixed
380 * @return object
381 */
Derek Allard39b622d2008-01-16 21:10:09 +0000382 function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000383 {
Derek Allard39b622d2008-01-16 21:10:09 +0000384 return $this->_where($key, $value, 'OR ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000385 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000386
387 // --------------------------------------------------------------------
388
389 /**
390 * orwhere() is an alias of or_where()
391 * this function is here for backwards compatibility, as
392 * orwhere() has been deprecated
393 */
Derek Allard39b622d2008-01-16 21:10:09 +0000394 function orwhere($key, $value = NULL, $escape = TRUE)
Derek Allard218e2bc2007-12-17 21:18:14 +0000395 {
Derek Allard39b622d2008-01-16 21:10:09 +0000396 return $this->or_where($key, $value, $escape);
Derek Allard218e2bc2007-12-17 21:18:14 +0000397 }
Derek Allard67b44ed2008-01-12 16:18:02 +0000398
399 // --------------------------------------------------------------------
400
401 /**
Derek Allard09de1852007-02-14 01:35:56 +0000402 * Where
403 *
404 * Called by where() or orwhere()
405 *
406 * @access private
407 * @param mixed
408 * @param mixed
409 * @param string
410 * @return object
411 */
Derek Allard39b622d2008-01-16 21:10:09 +0000412 function _where($key, $value = NULL, $type = 'AND ', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000413 {
Derek Allard73274992008-05-05 16:39:18 +0000414 if (! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000415 {
416 $key = array($key => $value);
417 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000418
Derek Allard09de1852007-02-14 01:35:56 +0000419 foreach ($key as $k => $v)
420 {
421 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Derek Allard15ddc9d2007-12-20 13:54:39 +0000422
Derek Allard73274992008-05-05 16:39:18 +0000423 if (! $this->_has_operator($k) && is_null($key[$k]))
Derek Allard15ddc9d2007-12-20 13:54:39 +0000424 {
425 // value appears not to have been set, assign the test to IS NULL
426 $k .= ' IS NULL';
427 }
Derek Allard09de1852007-02-14 01:35:56 +0000428
Derek Allard73274992008-05-05 16:39:18 +0000429 if (! is_null($v))
Derek Allard09de1852007-02-14 01:35:56 +0000430 {
Derek Allard39b622d2008-01-16 21:10:09 +0000431
432 if ($escape === TRUE)
433 {
434 // exception for "field<=" keys
435 if ($this->_has_operator($k))
436 {
437 $k = preg_replace("/([A-Za-z_0-9]+)/", $this->_protect_identifiers('$1'), $k);
438 }
439 else
440 {
441 $k = $this->_protect_identifiers($k);
442 }
443 }
444
Derek Allard73274992008-05-05 16:39:18 +0000445 if (! $this->_has_operator($k))
Derek Allard09de1852007-02-14 01:35:56 +0000446 {
447 $k .= ' =';
448 }
Derek Allard39b622d2008-01-16 21:10:09 +0000449
Derek Allard16629b12008-04-06 19:58:20 +0000450 if ($v !== '' AND $v !== NULL)
Derek Allard96863ce2008-04-06 19:20:17 +0000451 {
452 $v = ' '.$this->escape($v);
453 }
Derek Allard09de1852007-02-14 01:35:56 +0000454 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000455 else
456 {
Derek Allard16629b12008-04-06 19:58:20 +0000457
458 if ($escape === TRUE)
459 {
460 $k = $this->_protect_identifiers($k, TRUE);
461 }
462
Derek Allard9b3e7b52008-02-04 23:20:34 +0000463 }
464
Derek Allard09de1852007-02-14 01:35:56 +0000465 $this->ar_where[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000466 if ($this->ar_caching === TRUE)
467 {
468 $this->ar_cache_where[] = $prefix.$k.$v;
469 }
470
Derek Allard09de1852007-02-14 01:35:56 +0000471 }
472 return $this;
473 }
Derek Allard80dd7022007-12-18 23:55:06 +0000474
475 // --------------------------------------------------------------------
476
477 /**
478 * Where_in
479 *
Derek Allardc6935512007-12-19 14:23:19 +0000480 * Generates a WHERE field IN ('item', 'item') SQL query joined with
481 * AND if appropriate
Derek Allard80dd7022007-12-18 23:55:06 +0000482 *
483 * @access public
484 * @param string The field to search
485 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000486 * @return object
487 */
488 function where_in($key = NULL, $values = NULL)
489 {
490 return $this->_where_in($key, $values);
491 }
492
493 // --------------------------------------------------------------------
494
495 /**
496 * Where_in_or
497 *
498 * Generates a WHERE field IN ('item', 'item') SQL query joined with
499 * OR if appropriate
500 *
501 * @access public
502 * @param string The field to search
503 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000504 * @return object
505 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000506 function or_where_in($key = NULL, $values = NULL)
Derek Allardc6935512007-12-19 14:23:19 +0000507 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000508 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000509 }
510
511 // --------------------------------------------------------------------
512
513 /**
514 * Where_not_in
515 *
516 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
517 * with AND if appropriate
518 *
519 * @access public
520 * @param string The field to search
521 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000522 * @return object
523 */
524 function where_not_in($key = NULL, $values = NULL)
525 {
526 return $this->_where_in($key, $values, TRUE);
527 }
528
529 // --------------------------------------------------------------------
530
531 /**
532 * Where_not_in_or
533 *
534 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
535 * with OR if appropriate
536 *
537 * @access public
538 * @param string The field to search
539 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000540 * @return object
541 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000542 function or_where_not_in($key = NULL, $values = NULL)
Derek Allardc6935512007-12-19 14:23:19 +0000543 {
Derek Jonesd0072432008-05-07 22:06:51 +0000544 return $this->_where_in($key, $values, TRUE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000545 }
546
547 // --------------------------------------------------------------------
548
549 /**
550 * Where_in
551 *
552 * Called by where_in, where_in_or, where_not_in, where_not_in_or
553 *
554 * @access public
555 * @param string The field to search
556 * @param array The values searched on
557 * @param boolean If the statement whould be IN or NOT IN
Derek Allard80dd7022007-12-18 23:55:06 +0000558 * @param string
559 * @return object
560 */
Derek Allard15ddc9d2007-12-20 13:54:39 +0000561 function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard80dd7022007-12-18 23:55:06 +0000562 {
563 if ($key === NULL || !is_array($values))
564 {
565 return;
566 }
567
Derek Allardc6935512007-12-19 14:23:19 +0000568 $not = ($not) ? ' NOT ' : '';
Derek Allard80dd7022007-12-18 23:55:06 +0000569
570 foreach ($values as $value)
571 {
572 $this->ar_wherein[] = $this->escape($value);
573 }
574
575 $prefix = (count($this->ar_where) == 0) ? '' : $type;
576
Derek Allard9b3e7b52008-02-04 23:20:34 +0000577 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
578
579 $this->ar_where[] = $where_in;
580 if ($this->ar_caching === TRUE)
581 {
582 $this->ar_cache_where[] = $where_in;
583 }
Derek Allard80dd7022007-12-18 23:55:06 +0000584
Derek Allard8f000212008-01-18 14:45:59 +0000585 // reset the array for multiple calls
586 $this->ar_wherein = array();
Derek Allard80dd7022007-12-18 23:55:06 +0000587 return $this;
588 }
589
Derek Allard09de1852007-02-14 01:35:56 +0000590 // --------------------------------------------------------------------
591
592 /**
593 * Like
594 *
595 * Generates a %LIKE% portion of the query. Separates
596 * multiple calls with AND
597 *
598 * @access public
599 * @param mixed
600 * @param mixed
601 * @return object
602 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000603 function like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000604 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000605 return $this->_like($field, $match, 'AND ', $side);
Derek Allard09de1852007-02-14 01:35:56 +0000606 }
Derek Allarde54e3d22007-12-19 15:53:44 +0000607
608 // --------------------------------------------------------------------
609
610 /**
611 * Not Like
612 *
613 * Generates a NOT LIKE portion of the query. Separates
614 * multiple calls with AND
615 *
616 * @access public
617 * @param mixed
618 * @param mixed
619 * @return object
620 */
621 function not_like($field, $match = '', $side = 'both')
622 {
623 return $this->_like($field, $match, 'AND ', $side, ' NOT');
624 }
625
Derek Allard09de1852007-02-14 01:35:56 +0000626 // --------------------------------------------------------------------
627
628 /**
629 * OR Like
630 *
631 * Generates a %LIKE% portion of the query. Separates
632 * multiple calls with OR
633 *
634 * @access public
635 * @param mixed
636 * @param mixed
637 * @return object
638 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000639 function or_like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000640 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000641 return $this->_like($field, $match, 'OR ', $side);
642 }
643
644 // --------------------------------------------------------------------
645
646 /**
Derek Allarde54e3d22007-12-19 15:53:44 +0000647 * OR Not Like
648 *
649 * Generates a NOT LIKE portion of the query. Separates
650 * multiple calls with OR
651 *
652 * @access public
653 * @param mixed
654 * @param mixed
655 * @return object
656 */
657 function or_not_like($field, $match = '', $side = 'both')
658 {
659 return $this->_like($field, $match, 'OR ', $side, 'NOT ');
660 }
661
662 // --------------------------------------------------------------------
663
664 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000665 * orlike() is an alias of or_like()
666 * this function is here for backwards compatibility, as
667 * orlike() has been deprecated
668 */
669 function orlike($field, $match = '', $side = 'both')
670 {
Derek Allard4a310b72008-01-30 21:32:47 +0000671 return $this->or_like($field, $match, $side);
Derek Allard09de1852007-02-14 01:35:56 +0000672 }
673
674 // --------------------------------------------------------------------
675
676 /**
677 * Like
678 *
679 * Called by like() or orlike()
680 *
681 * @access private
682 * @param mixed
683 * @param mixed
684 * @param string
685 * @return object
686 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000687 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard09de1852007-02-14 01:35:56 +0000688 {
Derek Allard73274992008-05-05 16:39:18 +0000689 if (! is_array($field))
Derek Allard09de1852007-02-14 01:35:56 +0000690 {
691 $field = array($field => $match);
692 }
693
694 foreach ($field as $k => $v)
Derek Allarde54e3d22007-12-19 15:53:44 +0000695 {
696
Derek Allard39b622d2008-01-16 21:10:09 +0000697 $k = $this->_protect_identifiers($k);
698
Derek Allard09de1852007-02-14 01:35:56 +0000699 $prefix = (count($this->ar_like) == 0) ? '' : $type;
Derek Allarde54e3d22007-12-19 15:53:44 +0000700
Derek Allard09de1852007-02-14 01:35:56 +0000701 $v = $this->escape_str($v);
Derek Allarde54e3d22007-12-19 15:53:44 +0000702
Derek Allard218e2bc2007-12-17 21:18:14 +0000703 if ($side == 'before')
704 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000705 $like_statement = $prefix." $k $not LIKE '%{$v}'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000706 }
707 elseif ($side == 'after')
708 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000709 $like_statement = $prefix." $k $not LIKE '{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000710 }
711 else
712 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000713 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000714 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000715
716 $this->ar_like[] = $like_statement;
717 if ($this->ar_caching === TRUE)
718 {
719 $this->ar_cache_like[] = $like_statement;
720 }
721
Derek Allard09de1852007-02-14 01:35:56 +0000722 }
723 return $this;
724 }
725
726 // --------------------------------------------------------------------
727
728 /**
729 * GROUP BY
730 *
731 * @access public
732 * @param string
733 * @return object
734 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000735 function group_by($by)
Derek Allard09de1852007-02-14 01:35:56 +0000736 {
737 if (is_string($by))
738 {
739 $by = explode(',', $by);
740 }
741
742 foreach ($by as $val)
743 {
744 $val = trim($val);
745
746 if ($val != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000747 {
Derek Allard39b622d2008-01-16 21:10:09 +0000748 $this->ar_groupby[] = $this->_protect_identifiers($val);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000749 if ($this->ar_caching === TRUE)
750 {
751 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
752 }
753 }
Derek Allard09de1852007-02-14 01:35:56 +0000754 }
755 return $this;
756 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000757
758 // --------------------------------------------------------------------
759
760 /**
761 * groupby() is an alias of group_by()
762 * this function is here for backwards compatibility, as
763 * groupby() has been deprecated
764 */
765 function groupby($by)
766 {
767 return $this->group_by($by);
768 }
769
Derek Allard09de1852007-02-14 01:35:56 +0000770 // --------------------------------------------------------------------
771
772 /**
773 * Sets the HAVING value
774 *
775 * Separates multiple calls with AND
776 *
777 * @access public
778 * @param string
779 * @param string
780 * @return object
781 */
Derek Allarde808aac2008-04-06 18:22:00 +0000782 function having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000783 {
Derek Allarde808aac2008-04-06 18:22:00 +0000784 return $this->_having($key, $value, 'AND ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000785 }
Derek Allard43e8cbf2008-03-01 23:14:43 +0000786
787 // --------------------------------------------------------------------
788
789 /**
790 * orhaving() is an alias of or_having()
791 * this function is here for backwards compatibility, as
792 * orhaving() has been deprecated
793 */
794
Derek Allarde808aac2008-04-06 18:22:00 +0000795 function orhaving($key, $value = '', $escape = TRUE)
Derek Allard43e8cbf2008-03-01 23:14:43 +0000796 {
Derek Allarde808aac2008-04-06 18:22:00 +0000797 return $this->or_having($key, $value = '', $escape);
Derek Allard43e8cbf2008-03-01 23:14:43 +0000798 }
Derek Allard09de1852007-02-14 01:35:56 +0000799 // --------------------------------------------------------------------
800
801 /**
802 * Sets the OR HAVING value
803 *
804 * Separates multiple calls with OR
805 *
806 * @access public
807 * @param string
808 * @param string
809 * @return object
810 */
Derek Allarde808aac2008-04-06 18:22:00 +0000811 function or_having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000812 {
Derek Allarde808aac2008-04-06 18:22:00 +0000813 return $this->_having($key, $value, 'OR ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000814 }
815
816 // --------------------------------------------------------------------
817
818 /**
819 * Sets the HAVING values
820 *
821 * Called by having() or orhaving()
822 *
823 * @access private
824 * @param string
Derek Allard9b3e7b52008-02-04 23:20:34 +0000825
Derek Allard09de1852007-02-14 01:35:56 +0000826 * @param string
827 * @return object
828 */
Derek Allarde808aac2008-04-06 18:22:00 +0000829 function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000830 {
Derek Allard73274992008-05-05 16:39:18 +0000831 if (! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000832 {
833 $key = array($key => $value);
834 }
835
836 foreach ($key as $k => $v)
837 {
838 $prefix = (count($this->ar_having) == 0) ? '' : $type;
Derek Allarde808aac2008-04-06 18:22:00 +0000839
840 if ($escape === TRUE)
841 {
842 $k = $this->_protect_identifiers($k);
843 }
844
Derek Allard09de1852007-02-14 01:35:56 +0000845
846 if ($v != '')
847 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000848 $v = ' '.$this->escape_str($v);
Derek Allard09de1852007-02-14 01:35:56 +0000849 }
850
851 $this->ar_having[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000852 if ($this->ar_caching === TRUE)
853 {
854 $this->ar_cache_having[] = $prefix.$k.$v;
855 }
Derek Allard09de1852007-02-14 01:35:56 +0000856 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000857
Derek Allard09de1852007-02-14 01:35:56 +0000858 return $this;
859 }
860
861 // --------------------------------------------------------------------
862
863 /**
864 * Sets the ORDER BY value
865 *
866 * @access public
867 * @param string
868 * @param string direction: asc or desc
869 * @return object
870 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000871 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000872 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000873 if (strtolower($direction) == 'random')
874 {
875 $orderby = ''; // Random results want or don't need a field name
876 $direction = $this->_random_keyword;
877 }
878 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000879 {
Derek Allard92782492007-08-10 11:26:01 +0000880 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000881 }
882
Derek Allard9b3e7b52008-02-04 23:20:34 +0000883 $orderby_statement = $this->_protect_identifiers($orderby, TRUE).$direction;
884
885 $this->ar_orderby[] = $orderby_statement;
886 if ($this->ar_caching === TRUE)
887 {
888 $this->ar_cache_orderby[] = $orderby_statement;
889 }
890
Derek Allard09de1852007-02-14 01:35:56 +0000891 return $this;
892 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000893
Derek Allard218e2bc2007-12-17 21:18:14 +0000894 // --------------------------------------------------------------------
895
896 /**
897 * orderby() is an alias of order_by()
898 * this function is here for backwards compatibility, as
899 * orderby() has been deprecated
900 */
901 function orderby($orderby, $direction = '')
902 {
903 return $this->order_by($orderby, $direction);
904 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000905
Derek Allard09de1852007-02-14 01:35:56 +0000906 // --------------------------------------------------------------------
907
908 /**
909 * Sets the LIMIT value
910 *
911 * @access public
912 * @param integer the limit value
913 * @param integer the offset value
914 * @return object
915 */
916 function limit($value, $offset = '')
917 {
918 $this->ar_limit = $value;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000919 if ($this->ar_caching === TRUE)
920 {
921 $this->ar_cache_limit[] = $value;
922 }
923
Derek Allard09de1852007-02-14 01:35:56 +0000924 if ($offset != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000925 {
Derek Allard09de1852007-02-14 01:35:56 +0000926 $this->ar_offset = $offset;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000927 if ($this->ar_caching === TRUE)
928 {
929 $this->ar_cache_offset[] = $offset;
930 }
931 }
Derek Allard09de1852007-02-14 01:35:56 +0000932
933 return $this;
934 }
935
936 // --------------------------------------------------------------------
937
938 /**
939 * Sets the OFFSET value
940 *
941 * @access public
942 * @param integer the offset value
943 * @return object
944 */
Derek Allard9b3e7b52008-02-04 23:20:34 +0000945 function offset($offset)
Derek Allard09de1852007-02-14 01:35:56 +0000946 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000947 $this->ar_offset = $offset;
948 if ($this->ar_caching === TRUE)
949 {
950 $this->ar_cache_offset[] = $offset;
951 }
952
Derek Allard09de1852007-02-14 01:35:56 +0000953 return $this;
954 }
955
956 // --------------------------------------------------------------------
957
958 /**
959 * The "set" function. Allows key/value pairs to be set for inserting or updating
960 *
961 * @access public
962 * @param mixed
963 * @param string
Derek Allard39b622d2008-01-16 21:10:09 +0000964 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +0000965 * @return object
966 */
Derek Allard39b622d2008-01-16 21:10:09 +0000967 function set($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000968 {
969 $key = $this->_object_to_array($key);
970
Derek Allard73274992008-05-05 16:39:18 +0000971 if (! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000972 {
973 $key = array($key => $value);
974 }
975
976 foreach ($key as $k => $v)
977 {
Derek Allard39b622d2008-01-16 21:10:09 +0000978 if ($escape === FALSE)
979 {
980 $this->ar_set[$this->_protect_identifiers($k)] = $v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000981 if ($this->ar_caching === TRUE)
982 {
983 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $v;
984 }
Derek Allard39b622d2008-01-16 21:10:09 +0000985 }
986 else
987 {
988 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000989 if ($this->ar_caching === TRUE)
990 {
991 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $this->escape($v);
992 }
Derek Allard39b622d2008-01-16 21:10:09 +0000993 }
Derek Allard09de1852007-02-14 01:35:56 +0000994 }
995
996 return $this;
997 }
998
999 // --------------------------------------------------------------------
1000
1001 /**
1002 * Get
1003 *
1004 * Compiles the select statement based on the other functions called
1005 * and runs the query
1006 *
1007 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +00001008 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +00001009 * @param string the limit clause
1010 * @param string the offset clause
1011 * @return object
1012 */
1013 function get($table = '', $limit = null, $offset = null)
1014 {
1015 if ($table != '')
1016 {
Derek Allard5e128942007-12-28 21:33:03 +00001017 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001018 $this->from($table);
1019 }
1020
Derek Allard73274992008-05-05 16:39:18 +00001021 if (! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001022 {
1023 $this->limit($limit, $offset);
1024 }
1025
1026 $sql = $this->_compile_select();
1027
1028 $result = $this->query($sql);
1029 $this->_reset_select();
1030 return $result;
1031 }
1032
Derek Allard09de1852007-02-14 01:35:56 +00001033 /**
Derek Allard694b5b82007-12-18 15:58:03 +00001034 * "Count All Results" query
1035 *
1036 * Generates a platform-specific query string that counts all records
1037 * returned by an Active Record query.
1038 *
1039 * @access public
1040 * @param string
1041 * @return string
1042 */
1043 function count_all_results($table = '')
1044 {
1045 if ($table != '')
1046 {
Derek Allard5e128942007-12-28 21:33:03 +00001047 $this->_track_aliases($table);
Derek Allard694b5b82007-12-18 15:58:03 +00001048 $this->from($table);
1049 }
1050
Derek Allard39b622d2008-01-16 21:10:09 +00001051 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
Derek Allard694b5b82007-12-18 15:58:03 +00001052
1053 $query = $this->query($sql);
1054 $this->_reset_select();
1055
1056 if ($query->num_rows() == 0)
1057 {
1058 return '0';
1059 }
1060
1061 $row = $query->row();
1062 return $row->numrows;
1063 }
1064
1065 // --------------------------------------------------------------------
1066
1067 /**
Derek Allard218e2bc2007-12-17 21:18:14 +00001068 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +00001069 *
1070 * Allows the where clause, limit and offset to be added directly
1071 *
1072 * @access public
1073 * @param string the where clause
1074 * @param string the limit clause
1075 * @param string the offset clause
1076 * @return object
1077 */
Derek Allard218e2bc2007-12-17 21:18:14 +00001078 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +00001079 {
1080 if ($table != '')
1081 {
Derek Allard5e128942007-12-28 21:33:03 +00001082 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001083 $this->from($table);
1084 }
1085
Derek Allard73274992008-05-05 16:39:18 +00001086 if (! is_null($where))
Derek Allard09de1852007-02-14 01:35:56 +00001087 {
1088 $this->where($where);
1089 }
1090
Derek Allard73274992008-05-05 16:39:18 +00001091 if (! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001092 {
1093 $this->limit($limit, $offset);
1094 }
1095
1096 $sql = $this->_compile_select();
1097
1098 $result = $this->query($sql);
1099 $this->_reset_select();
1100 return $result;
1101 }
Derek Allard218e2bc2007-12-17 21:18:14 +00001102
1103 // --------------------------------------------------------------------
1104
1105 /**
1106 * getwhere() is an alias of get_where()
1107 * this function is here for backwards compatibility, as
1108 * getwhere() has been deprecated
1109 */
1110 function getwhere($table = '', $where = null, $limit = null, $offset = null)
1111 {
1112 return $this->get_where($table, $where, $limit, $offset);
1113 }
Derek Allard09de1852007-02-14 01:35:56 +00001114
1115 // --------------------------------------------------------------------
1116
1117 /**
1118 * Insert
1119 *
1120 * Compiles an insert string and runs the query
1121 *
1122 * @access public
1123 * @param string the table to retrieve the results from
1124 * @param array an associative array of insert values
1125 * @return object
1126 */
1127 function insert($table = '', $set = NULL)
1128 {
Derek Allard73274992008-05-05 16:39:18 +00001129 if (! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001130 {
1131 $this->set($set);
1132 }
1133
1134 if (count($this->ar_set) == 0)
1135 {
1136 if ($this->db_debug)
1137 {
1138 return $this->display_error('db_must_use_set');
1139 }
1140 return FALSE;
1141 }
1142
1143 if ($table == '')
1144 {
Derek Allard73274992008-05-05 16:39:18 +00001145 if (! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001146 {
1147 if ($this->db_debug)
1148 {
1149 return $this->display_error('db_must_set_table');
1150 }
1151 return FALSE;
1152 }
1153
1154 $table = $this->ar_from[0];
1155 }
Derek Allard39b622d2008-01-16 21:10:09 +00001156
1157 $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 +00001158
1159 $this->_reset_write();
1160 return $this->query($sql);
1161 }
1162
1163 // --------------------------------------------------------------------
1164
1165 /**
1166 * Update
1167 *
1168 * Compiles an update string and runs the query
1169 *
1170 * @access public
1171 * @param string the table to retrieve the results from
1172 * @param array an associative array of update values
1173 * @param mixed the where clause
1174 * @return object
1175 */
Derek Allard5e128942007-12-28 21:33:03 +00001176 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001177 {
Derek Allard73274992008-05-05 16:39:18 +00001178 if (! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001179 {
1180 $this->set($set);
1181 }
1182
1183 if (count($this->ar_set) == 0)
1184 {
1185 if ($this->db_debug)
1186 {
1187 return $this->display_error('db_must_use_set');
1188 }
1189 return FALSE;
1190 }
1191
1192 if ($table == '')
1193 {
Derek Allard73274992008-05-05 16:39:18 +00001194 if (! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001195 {
1196 if ($this->db_debug)
1197 {
1198 return $this->display_error('db_must_set_table');
1199 }
1200 return FALSE;
1201 }
1202
1203 $table = $this->ar_from[0];
1204 }
1205
Derek Allarde77d77c2007-12-19 15:01:55 +00001206 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001207 {
1208 $this->where($where);
1209 }
Derek Allardda6d2402007-12-19 14:49:29 +00001210
Derek Allarde77d77c2007-12-19 15:01:55 +00001211 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +00001212 {
1213 $this->limit($limit);
1214 }
Derek Allard09de1852007-02-14 01:35:56 +00001215
Derek Allard39b622d2008-01-16 21:10:09 +00001216 $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 +00001217
1218 $this->_reset_write();
1219 return $this->query($sql);
1220 }
Derek Allard39b622d2008-01-16 21:10:09 +00001221
1222 // --------------------------------------------------------------------
1223
1224 /**
1225 * Empty Table
1226 *
1227 * Compiles a delete string and runs "DELETE FROM table"
1228 *
1229 * @access public
1230 * @param string the table to empty
1231 * @return object
1232 */
1233 function empty_table($table = '')
1234 {
1235 if ($table == '')
1236 {
Derek Allard73274992008-05-05 16:39:18 +00001237 if (! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001238 {
1239 if ($this->db_debug)
1240 {
1241 return $this->display_error('db_must_set_table');
1242 }
1243 return FALSE;
1244 }
1245
1246 $table = $this->ar_from[0];
1247 }
1248 else
1249 {
1250 $table = $this->_protect_identifiers($this->dbprefix.$table);
1251 }
1252
1253
1254 $sql = $this->_delete($table);
1255
1256 $this->_reset_write();
1257
1258 return $this->query($sql);
1259 }
1260
1261 // --------------------------------------------------------------------
1262
1263 /**
1264 * Truncate
1265 *
1266 * Compiles a truncate string and runs the query
1267 * If the database does not support the truncate() command
1268 * This function maps to "DELETE FROM table"
1269 *
1270 * @access public
1271 * @param string the table to truncate
1272 * @return object
1273 */
1274 function truncate($table = '')
1275 {
1276 if ($table == '')
1277 {
Derek Allard73274992008-05-05 16:39:18 +00001278 if (! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001279 {
1280 if ($this->db_debug)
1281 {
1282 return $this->display_error('db_must_set_table');
1283 }
1284 return FALSE;
1285 }
1286
1287 $table = $this->ar_from[0];
1288 }
1289 else
1290 {
1291 $table = $this->_protect_identifiers($this->dbprefix.$table);
1292 }
1293
1294
1295 $sql = $this->_truncate($table);
1296
1297 $this->_reset_write();
1298
1299 return $this->query($sql);
1300 }
Derek Allard09de1852007-02-14 01:35:56 +00001301
1302 // --------------------------------------------------------------------
1303
1304 /**
1305 * Delete
1306 *
1307 * Compiles a delete string and runs the query
1308 *
1309 * @access public
Derek Allard41f60d42007-12-20 20:09:22 +00001310 * @param mixed the table(s) to delete from. String or array
Derek Allard09de1852007-02-14 01:35:56 +00001311 * @param mixed the where clause
Derek Allard41f60d42007-12-20 20:09:22 +00001312 * @param mixed the limit clause
1313 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +00001314 * @return object
1315 */
Derek Allard41f60d42007-12-20 20:09:22 +00001316 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +00001317 {
1318 if ($table == '')
1319 {
Derek Allard73274992008-05-05 16:39:18 +00001320 if (! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001321 {
1322 if ($this->db_debug)
1323 {
1324 return $this->display_error('db_must_set_table');
1325 }
1326 return FALSE;
1327 }
Derek Allard39b622d2008-01-16 21:10:09 +00001328
Derek Allard09de1852007-02-14 01:35:56 +00001329 $table = $this->ar_from[0];
1330 }
Derek Allard39b622d2008-01-16 21:10:09 +00001331 elseif (is_array($table))
Derek Allard41f60d42007-12-20 20:09:22 +00001332 {
1333 foreach($table as $single_table)
1334 {
Derek Allard39b622d2008-01-16 21:10:09 +00001335 $this->delete($single_table, $where, $limit, FALSE);
Derek Allard41f60d42007-12-20 20:09:22 +00001336 }
Derek Allard39b622d2008-01-16 21:10:09 +00001337
Derek Allard41f60d42007-12-20 20:09:22 +00001338 $this->_reset_write();
1339 return;
1340 }
Derek Allard39b622d2008-01-16 21:10:09 +00001341 else
1342 {
1343 $table = $this->_protect_identifiers($this->dbprefix.$table);
1344 }
Derek Allard41f60d42007-12-20 20:09:22 +00001345
Derek Allard09de1852007-02-14 01:35:56 +00001346 if ($where != '')
1347 {
1348 $this->where($where);
1349 }
1350
Derek Allarde77d77c2007-12-19 15:01:55 +00001351 if ($limit != NULL)
1352 {
1353 $this->limit($limit);
1354 }
1355
Derek Allard39b622d2008-01-16 21:10:09 +00001356 if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
Derek Allard09de1852007-02-14 01:35:56 +00001357 {
1358 if ($this->db_debug)
1359 {
1360 return $this->display_error('db_del_must_use_where');
1361 }
Derek Allard39b622d2008-01-16 21:10:09 +00001362
Derek Allard09de1852007-02-14 01:35:56 +00001363 return FALSE;
1364 }
Derek Allard41f60d42007-12-20 20:09:22 +00001365
Derek Allard39b622d2008-01-16 21:10:09 +00001366 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001367
Derek Allard41f60d42007-12-20 20:09:22 +00001368 if ($reset_data)
1369 {
1370 $this->_reset_write();
1371 }
Derek Allard39b622d2008-01-16 21:10:09 +00001372
Derek Allard09de1852007-02-14 01:35:56 +00001373 return $this->query($sql);
1374 }
Derek Allard15ddc9d2007-12-20 13:54:39 +00001375
Derek Allard09de1852007-02-14 01:35:56 +00001376 // --------------------------------------------------------------------
1377
1378 /**
1379 * Use Table - DEPRECATED
1380 *
1381 * @deprecated use $this->db->from instead
1382 */
1383 function use_table($table)
1384 {
1385 return $this->from($table);
Derek Allard09de1852007-02-14 01:35:56 +00001386 }
1387
1388 // --------------------------------------------------------------------
1389
1390 /**
Derek Allard09de1852007-02-14 01:35:56 +00001391 * Tests whether the string has an SQL operator
1392 *
1393 * @access private
1394 * @param string
1395 * @return bool
1396 */
1397 function _has_operator($str)
1398 {
1399 $str = trim($str);
Derek Allard73274992008-05-05 16:39:18 +00001400 if (! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
Derek Allard09de1852007-02-14 01:35:56 +00001401 {
1402 return FALSE;
1403 }
1404
1405 return TRUE;
1406 }
1407
1408 // --------------------------------------------------------------------
1409
1410 /**
Derek Allard5e128942007-12-28 21:33:03 +00001411 * Track Aliases
1412 *
1413 * Used to track SQL statements written with aliased tables.
1414 *
1415 * @access private
1416 * @param string The table to inspect
1417 * @return string
1418 */
1419 function _track_aliases($table)
1420 {
1421 // if a table alias is used we can recognize it by a space
1422 if (strpos($table, " ") !== FALSE)
1423 {
1424 // if the alias is written with the AS keyowrd, get it out
Derek Allard39b622d2008-01-16 21:10:09 +00001425 $table = preg_replace('/ AS /i', ' ', $table);
Derek Allard5e128942007-12-28 21:33:03 +00001426
Derek Allard39b622d2008-01-16 21:10:09 +00001427 $this->ar_aliased_tables[] = trim(strrchr($table, " "));
Derek Allard5e128942007-12-28 21:33:03 +00001428 }
1429
1430 return $this->dbprefix.$table;
1431 }
1432
1433 // --------------------------------------------------------------------
1434
1435 /**
1436 * Filter Table Aliases
1437 *
1438 * Intelligently removes database prefixes from aliased tables
1439 *
1440 * @access private
1441 * @param array An array of compiled SQL
1442 * @return array Cleaned up statement with aliases accounted for
1443 */
1444 function _filter_table_aliases($statements)
1445 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001446
Derek Allard39b622d2008-01-16 21:10:09 +00001447 foreach ($statements as $k => $v)
Derek Allard5e128942007-12-28 21:33:03 +00001448 {
Derek Allard39b622d2008-01-16 21:10:09 +00001449 foreach ($this->ar_aliased_tables as $table)
Derek Allard5e128942007-12-28 21:33:03 +00001450 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001451 $statements[$k] = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $statements[$k]); // makes `table.field`
1452 $statements[$k] = str_replace($this->dbprefix.$table.'.', $table.'.', $statements[$k]);
Derek Allard5e128942007-12-28 21:33:03 +00001453 }
Derek Allard5e128942007-12-28 21:33:03 +00001454 }
Derek Allard39b622d2008-01-16 21:10:09 +00001455 return $statements;
Derek Allard5e128942007-12-28 21:33:03 +00001456 }
1457
1458 // --------------------------------------------------------------------
1459
1460 /**
Derek Allard09de1852007-02-14 01:35:56 +00001461 * Compile the SELECT statement
1462 *
1463 * Generates a query string based on which functions were used.
1464 * Should not be called directly. The get() function calls it.
1465 *
1466 * @access private
1467 * @return string
1468 */
Derek Allard694b5b82007-12-18 15:58:03 +00001469 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001470 {
Derek Allard78255262008-02-06 13:54:23 +00001471 $this->_merge_cache();
Derek Allard9b3e7b52008-02-04 23:20:34 +00001472
Derek Allard73274992008-05-05 16:39:18 +00001473 $sql = (! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Derek Allard09de1852007-02-14 01:35:56 +00001474
Derek Allard5162fc72008-04-15 20:05:37 +00001475 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->_filter_table_aliases($this->ar_select));
Derek Allard09de1852007-02-14 01:35:56 +00001476
Derek Allard694b5b82007-12-18 15:58:03 +00001477 if ($select_override !== FALSE)
1478 {
1479 $sql = $select_override;
1480 }
1481
Derek Allard09de1852007-02-14 01:35:56 +00001482 if (count($this->ar_from) > 0)
1483 {
1484 $sql .= "\nFROM ";
Derek Jonesc6ad0232008-01-29 18:44:54 +00001485 $sql .= $this->_from_tables($this->ar_from);
Derek Allard09de1852007-02-14 01:35:56 +00001486 }
1487
1488 if (count($this->ar_join) > 0)
Derek Allard5e128942007-12-28 21:33:03 +00001489 {
Derek Allard09de1852007-02-14 01:35:56 +00001490 $sql .= "\n";
Derek Allard5e128942007-12-28 21:33:03 +00001491
1492 // special consideration for table aliases
1493 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1494 {
1495 $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
1496 }
1497 else
1498 {
1499 $sql .= implode("\n", $this->ar_join);
1500 }
1501
Derek Allard09de1852007-02-14 01:35:56 +00001502 }
1503
1504 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1505 {
1506 $sql .= "\nWHERE ";
1507 }
1508
1509 $sql .= implode("\n", $this->ar_where);
1510
1511 if (count($this->ar_like) > 0)
1512 {
1513 if (count($this->ar_where) > 0)
1514 {
1515 $sql .= " AND ";
1516 }
1517
1518 $sql .= implode("\n", $this->ar_like);
1519 }
1520
1521 if (count($this->ar_groupby) > 0)
1522 {
Derek Allard5e128942007-12-28 21:33:03 +00001523
Derek Allard09de1852007-02-14 01:35:56 +00001524 $sql .= "\nGROUP BY ";
Derek Allard5e128942007-12-28 21:33:03 +00001525
1526 // special consideration for table aliases
1527 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1528 {
1529 $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
1530 }
1531 else
1532 {
1533 $sql .= implode(', ', $this->ar_groupby);
1534 }
Derek Allard09de1852007-02-14 01:35:56 +00001535 }
1536
1537 if (count($this->ar_having) > 0)
1538 {
1539 $sql .= "\nHAVING ";
1540 $sql .= implode("\n", $this->ar_having);
1541 }
1542
1543 if (count($this->ar_orderby) > 0)
1544 {
1545 $sql .= "\nORDER BY ";
1546 $sql .= implode(', ', $this->ar_orderby);
1547
1548 if ($this->ar_order !== FALSE)
1549 {
1550 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1551 }
1552 }
1553
1554 if (is_numeric($this->ar_limit))
1555 {
1556 $sql .= "\n";
1557 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1558 }
1559
1560 return $sql;
1561 }
1562
1563 // --------------------------------------------------------------------
1564
1565 /**
1566 * Object to Array
1567 *
1568 * Takes an object as input and converts the class variables to array key/vals
1569 *
1570 * @access public
1571 * @param object
1572 * @return array
1573 */
1574 function _object_to_array($object)
1575 {
Derek Allard73274992008-05-05 16:39:18 +00001576 if (! is_object($object))
Derek Allard09de1852007-02-14 01:35:56 +00001577 {
1578 return $object;
1579 }
1580
1581 $array = array();
1582 foreach (get_object_vars($object) as $key => $val)
1583 {
Derek Allard848b7762007-12-31 16:43:05 +00001584 // There are some built in keys we need to ignore for this conversion
Derek Allard73274992008-05-05 16:39:18 +00001585 if (! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
Derek Allard848b7762007-12-31 16:43:05 +00001586
Derek Allard09de1852007-02-14 01:35:56 +00001587 {
1588 $array[$key] = $val;
1589 }
1590 }
1591
1592 return $array;
1593 }
1594
1595 // --------------------------------------------------------------------
1596
1597 /**
Derek Allard9b3e7b52008-02-04 23:20:34 +00001598 * Start Cache
1599 *
1600 * Starts AR caching
1601 *
1602 * @access public
1603 * @return void
1604 */
1605 function start_cache()
1606 {
1607 $this->ar_caching = TRUE;
1608 }
1609
1610 // --------------------------------------------------------------------
1611
1612 /**
1613 * Stop Cache
1614 *
1615 * Stops AR caching
1616 *
1617 * @access public
1618 * @return void
1619 */
1620 function stop_cache()
1621 {
1622 $this->ar_caching = FALSE;
1623 }
1624
1625
1626 // --------------------------------------------------------------------
1627
1628 /**
1629 * Flush Cache
1630 *
1631 * Empties the AR cache
1632 *
1633 * @access public
1634 * @return void
1635 */
1636 function flush_cache()
1637 {
1638 $ar_reset_items = array(
1639 'ar_cache_select' => array(),
1640 'ar_cache_from' => array(),
1641 'ar_cache_join' => array(),
1642 'ar_cache_where' => array(),
1643 'ar_cache_like' => array(),
1644 'ar_cache_groupby' => array(),
1645 'ar_cache_having' =>array(),
1646 'ar_cache_orderby' => array(),
1647 'ar_cache_set' => array()
1648 );
1649
1650 $this->_reset_run($ar_reset_items);
1651 }
1652
1653 // --------------------------------------------------------------------
1654
1655 /**
1656 * Merge Cache
1657 *
1658 * When called, this function merges any cached AR arrays with
1659 * locally called ones.
1660 *
1661 * @access private
1662 * @return void
1663 */
1664 function _merge_cache()
1665 {
1666 $ar_items = array('select', 'from', 'join', 'where', 'like', 'groupby', 'having', 'orderby', 'set');
1667
1668 foreach ($ar_items as $ar_item)
1669 {
1670 $ar_cache_item = 'ar_cache_'.$ar_item;
1671 $ar_item = 'ar_'.$ar_item;
1672 $this->$ar_item = array_unique(array_merge($this->$ar_item, $this->$ar_cache_item));
1673 }
1674 }
1675
1676 // --------------------------------------------------------------------
1677
1678 /**
1679 * Resets the active record values. Called by the get() function
1680 *
1681 * @access private
1682 * @param array An array of fields to reset
1683 * @return void
1684 */
1685 function _reset_run($ar_reset_items)
1686 {
1687 foreach ($ar_reset_items as $item => $default_value)
1688 {
1689 if (!in_array($item, $this->ar_store_array))
1690 {
1691 $this->$item = $default_value;
1692 }
1693 }
1694 }
1695
1696 // --------------------------------------------------------------------
1697
1698 /**
Derek Allard09de1852007-02-14 01:35:56 +00001699 * Resets the active record values. Called by the get() function
1700 *
1701 * @access private
1702 * @return void
1703 */
1704 function _reset_select()
1705 {
Derek Allard9b3e7b52008-02-04 23:20:34 +00001706 $ar_reset_items = array(
1707 'ar_select' => array(),
1708 'ar_from' => array(),
1709 'ar_join' => array(),
1710 'ar_where' => array(),
1711 'ar_like' => array(),
1712 'ar_groupby' => array(),
1713 'ar_having' => array(),
1714 'ar_orderby' => array(),
1715 'ar_wherein' => array(),
1716 'ar_aliased_tables' => array(),
1717 'ar_distinct' => FALSE,
1718 'ar_limit' => FALSE,
1719 'ar_offset' => FALSE,
1720 'ar_order' => FALSE,
1721 );
1722
1723 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001724 }
1725
1726 // --------------------------------------------------------------------
1727
1728 /**
1729 * Resets the active record "write" values.
1730 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001731 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001732 *
1733 * @access private
1734 * @return void
1735 */
1736 function _reset_write()
Derek Allard9b3e7b52008-02-04 23:20:34 +00001737 {
1738 $ar_reset_items = array(
1739 'ar_set' => array(),
1740 'ar_from' => array(),
1741 'ar_where' => array(),
1742 'ar_like' => array(),
1743 'ar_orderby' => array(),
1744 'ar_limit' => FALSE,
1745 'ar_order' => FALSE
1746 );
1747
1748 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001749 }
1750
1751}