blob: 1820bd2029982414fc2fab761838f9c089748bff [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
487 * @return object
488 */
489 function where_in($key = NULL, $values = NULL)
490 {
491 return $this->_where_in($key, $values);
492 }
493
494 // --------------------------------------------------------------------
495
496 /**
497 * Where_in_or
498 *
499 * Generates a WHERE field IN ('item', 'item') SQL query joined with
500 * OR if appropriate
501 *
502 * @access public
503 * @param string The field to search
504 * @param array The values searched on
505
506 * @return object
507 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000508 function or_where_in($key = NULL, $values = NULL)
Derek Allardc6935512007-12-19 14:23:19 +0000509 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000510 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000511 }
512
513 // --------------------------------------------------------------------
514
515 /**
516 * Where_not_in
517 *
518 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
519 * with AND if appropriate
520 *
521 * @access public
522 * @param string The field to search
523 * @param array The values searched on
524
525 * @return object
526 */
527 function where_not_in($key = NULL, $values = NULL)
528 {
529 return $this->_where_in($key, $values, TRUE);
530 }
531
532 // --------------------------------------------------------------------
533
534 /**
535 * Where_not_in_or
536 *
537 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
538 * with OR if appropriate
539 *
540 * @access public
541 * @param string The field to search
542 * @param array The values searched on
543
544 * @return object
545 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000546 function or_where_not_in($key = NULL, $values = NULL)
Derek Allardc6935512007-12-19 14:23:19 +0000547 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000548 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000549 }
550
551 // --------------------------------------------------------------------
552
553 /**
554 * Where_in
555 *
556 * Called by where_in, where_in_or, where_not_in, where_not_in_or
557 *
558 * @access public
559 * @param string The field to search
560 * @param array The values searched on
561 * @param boolean If the statement whould be IN or NOT IN
Derek Allard80dd7022007-12-18 23:55:06 +0000562 * @param string
563 * @return object
564 */
Derek Allard15ddc9d2007-12-20 13:54:39 +0000565 function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard80dd7022007-12-18 23:55:06 +0000566 {
567 if ($key === NULL || !is_array($values))
568 {
569 return;
570 }
571
Derek Allardc6935512007-12-19 14:23:19 +0000572 $not = ($not) ? ' NOT ' : '';
Derek Allard80dd7022007-12-18 23:55:06 +0000573
574 foreach ($values as $value)
575 {
576 $this->ar_wherein[] = $this->escape($value);
577 }
578
579 $prefix = (count($this->ar_where) == 0) ? '' : $type;
580
Derek Allard9b3e7b52008-02-04 23:20:34 +0000581 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
582
583 $this->ar_where[] = $where_in;
584 if ($this->ar_caching === TRUE)
585 {
586 $this->ar_cache_where[] = $where_in;
587 }
Derek Allard80dd7022007-12-18 23:55:06 +0000588
Derek Allard8f000212008-01-18 14:45:59 +0000589 // reset the array for multiple calls
590 $this->ar_wherein = array();
Derek Allard80dd7022007-12-18 23:55:06 +0000591 return $this;
592 }
593
Derek Allard09de1852007-02-14 01:35:56 +0000594 // --------------------------------------------------------------------
595
596 /**
597 * Like
598 *
599 * Generates a %LIKE% portion of the query. Separates
600 * multiple calls with AND
601 *
602 * @access public
603 * @param mixed
604 * @param mixed
605 * @return object
606 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000607 function like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000608 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000609 return $this->_like($field, $match, 'AND ', $side);
Derek Allard09de1852007-02-14 01:35:56 +0000610 }
Derek Allarde54e3d22007-12-19 15:53:44 +0000611
612 // --------------------------------------------------------------------
613
614 /**
615 * Not Like
616 *
617 * Generates a NOT LIKE portion of the query. Separates
618 * multiple calls with AND
619 *
620 * @access public
621 * @param mixed
622 * @param mixed
623 * @return object
624 */
625 function not_like($field, $match = '', $side = 'both')
626 {
627 return $this->_like($field, $match, 'AND ', $side, ' NOT');
628 }
629
Derek Allard09de1852007-02-14 01:35:56 +0000630 // --------------------------------------------------------------------
631
632 /**
633 * OR Like
634 *
635 * Generates a %LIKE% portion of the query. Separates
636 * multiple calls with OR
637 *
638 * @access public
639 * @param mixed
640 * @param mixed
641 * @return object
642 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000643 function or_like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000644 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000645 return $this->_like($field, $match, 'OR ', $side);
646 }
647
648 // --------------------------------------------------------------------
649
650 /**
Derek Allarde54e3d22007-12-19 15:53:44 +0000651 * OR Not Like
652 *
653 * Generates a NOT LIKE portion of the query. Separates
654 * multiple calls with OR
655 *
656 * @access public
657 * @param mixed
658 * @param mixed
659 * @return object
660 */
661 function or_not_like($field, $match = '', $side = 'both')
662 {
663 return $this->_like($field, $match, 'OR ', $side, 'NOT ');
664 }
665
666 // --------------------------------------------------------------------
667
668 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000669 * orlike() is an alias of or_like()
670 * this function is here for backwards compatibility, as
671 * orlike() has been deprecated
672 */
673 function orlike($field, $match = '', $side = 'both')
674 {
Derek Allard4a310b72008-01-30 21:32:47 +0000675 return $this->or_like($field, $match, $side);
Derek Allard09de1852007-02-14 01:35:56 +0000676 }
677
678 // --------------------------------------------------------------------
679
680 /**
681 * Like
682 *
683 * Called by like() or orlike()
684 *
685 * @access private
686 * @param mixed
687 * @param mixed
688 * @param string
689 * @return object
690 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000691 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard09de1852007-02-14 01:35:56 +0000692 {
Derek Allard73274992008-05-05 16:39:18 +0000693 if (! is_array($field))
Derek Allard09de1852007-02-14 01:35:56 +0000694 {
695 $field = array($field => $match);
696 }
697
698 foreach ($field as $k => $v)
Derek Allarde54e3d22007-12-19 15:53:44 +0000699 {
700
Derek Allard39b622d2008-01-16 21:10:09 +0000701 $k = $this->_protect_identifiers($k);
702
Derek Allard09de1852007-02-14 01:35:56 +0000703 $prefix = (count($this->ar_like) == 0) ? '' : $type;
Derek Allarde54e3d22007-12-19 15:53:44 +0000704
Derek Allard09de1852007-02-14 01:35:56 +0000705 $v = $this->escape_str($v);
Derek Allarde54e3d22007-12-19 15:53:44 +0000706
Derek Allard218e2bc2007-12-17 21:18:14 +0000707 if ($side == 'before')
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 elseif ($side == 'after')
712 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000713 $like_statement = $prefix." $k $not LIKE '{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000714 }
715 else
716 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000717 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000718 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000719
720 $this->ar_like[] = $like_statement;
721 if ($this->ar_caching === TRUE)
722 {
723 $this->ar_cache_like[] = $like_statement;
724 }
725
Derek Allard09de1852007-02-14 01:35:56 +0000726 }
727 return $this;
728 }
729
730 // --------------------------------------------------------------------
731
732 /**
733 * GROUP BY
734 *
735 * @access public
736 * @param string
737 * @return object
738 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000739 function group_by($by)
Derek Allard09de1852007-02-14 01:35:56 +0000740 {
741 if (is_string($by))
742 {
743 $by = explode(',', $by);
744 }
745
746 foreach ($by as $val)
747 {
748 $val = trim($val);
749
750 if ($val != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000751 {
Derek Allard39b622d2008-01-16 21:10:09 +0000752 $this->ar_groupby[] = $this->_protect_identifiers($val);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000753 if ($this->ar_caching === TRUE)
754 {
755 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
756 }
757 }
Derek Allard09de1852007-02-14 01:35:56 +0000758 }
759 return $this;
760 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000761
762 // --------------------------------------------------------------------
763
764 /**
765 * groupby() is an alias of group_by()
766 * this function is here for backwards compatibility, as
767 * groupby() has been deprecated
768 */
769 function groupby($by)
770 {
771 return $this->group_by($by);
772 }
773
Derek Allard09de1852007-02-14 01:35:56 +0000774 // --------------------------------------------------------------------
775
776 /**
777 * Sets the HAVING value
778 *
779 * Separates multiple calls with AND
780 *
781 * @access public
782 * @param string
783 * @param string
784 * @return object
785 */
Derek Allarde808aac2008-04-06 18:22:00 +0000786 function having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000787 {
Derek Allarde808aac2008-04-06 18:22:00 +0000788 return $this->_having($key, $value, 'AND ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000789 }
Derek Allard43e8cbf2008-03-01 23:14:43 +0000790
791 // --------------------------------------------------------------------
792
793 /**
794 * orhaving() is an alias of or_having()
795 * this function is here for backwards compatibility, as
796 * orhaving() has been deprecated
797 */
798
Derek Allarde808aac2008-04-06 18:22:00 +0000799 function orhaving($key, $value = '', $escape = TRUE)
Derek Allard43e8cbf2008-03-01 23:14:43 +0000800 {
Derek Allarde808aac2008-04-06 18:22:00 +0000801 return $this->or_having($key, $value = '', $escape);
Derek Allard43e8cbf2008-03-01 23:14:43 +0000802 }
Derek Allard09de1852007-02-14 01:35:56 +0000803 // --------------------------------------------------------------------
804
805 /**
806 * Sets the OR HAVING value
807 *
808 * Separates multiple calls with OR
809 *
810 * @access public
811 * @param string
812 * @param string
813 * @return object
814 */
Derek Allarde808aac2008-04-06 18:22:00 +0000815 function or_having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000816 {
Derek Allarde808aac2008-04-06 18:22:00 +0000817 return $this->_having($key, $value, 'OR ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000818 }
819
820 // --------------------------------------------------------------------
821
822 /**
823 * Sets the HAVING values
824 *
825 * Called by having() or orhaving()
826 *
827 * @access private
828 * @param string
Derek Allard9b3e7b52008-02-04 23:20:34 +0000829
Derek Allard09de1852007-02-14 01:35:56 +0000830 * @param string
831 * @return object
832 */
Derek Allarde808aac2008-04-06 18:22:00 +0000833 function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000834 {
Derek Allard73274992008-05-05 16:39:18 +0000835 if (! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000836 {
837 $key = array($key => $value);
838 }
839
840 foreach ($key as $k => $v)
841 {
842 $prefix = (count($this->ar_having) == 0) ? '' : $type;
Derek Allarde808aac2008-04-06 18:22:00 +0000843
844 if ($escape === TRUE)
845 {
846 $k = $this->_protect_identifiers($k);
847 }
848
Derek Allard09de1852007-02-14 01:35:56 +0000849
850 if ($v != '')
851 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000852 $v = ' '.$this->escape_str($v);
Derek Allard09de1852007-02-14 01:35:56 +0000853 }
854
855 $this->ar_having[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000856 if ($this->ar_caching === TRUE)
857 {
858 $this->ar_cache_having[] = $prefix.$k.$v;
859 }
Derek Allard09de1852007-02-14 01:35:56 +0000860 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000861
Derek Allard09de1852007-02-14 01:35:56 +0000862 return $this;
863 }
864
865 // --------------------------------------------------------------------
866
867 /**
868 * Sets the ORDER BY value
869 *
870 * @access public
871 * @param string
872 * @param string direction: asc or desc
873 * @return object
874 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000875 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000876 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000877 if (strtolower($direction) == 'random')
878 {
879 $orderby = ''; // Random results want or don't need a field name
880 $direction = $this->_random_keyword;
881 }
882 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000883 {
Derek Allard92782492007-08-10 11:26:01 +0000884 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000885 }
886
Derek Allard9b3e7b52008-02-04 23:20:34 +0000887 $orderby_statement = $this->_protect_identifiers($orderby, TRUE).$direction;
888
889 $this->ar_orderby[] = $orderby_statement;
890 if ($this->ar_caching === TRUE)
891 {
892 $this->ar_cache_orderby[] = $orderby_statement;
893 }
894
Derek Allard09de1852007-02-14 01:35:56 +0000895 return $this;
896 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000897
Derek Allard218e2bc2007-12-17 21:18:14 +0000898 // --------------------------------------------------------------------
899
900 /**
901 * orderby() is an alias of order_by()
902 * this function is here for backwards compatibility, as
903 * orderby() has been deprecated
904 */
905 function orderby($orderby, $direction = '')
906 {
907 return $this->order_by($orderby, $direction);
908 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000909
Derek Allard09de1852007-02-14 01:35:56 +0000910 // --------------------------------------------------------------------
911
912 /**
913 * Sets the LIMIT value
914 *
915 * @access public
916 * @param integer the limit value
917 * @param integer the offset value
918 * @return object
919 */
920 function limit($value, $offset = '')
921 {
922 $this->ar_limit = $value;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000923 if ($this->ar_caching === TRUE)
924 {
925 $this->ar_cache_limit[] = $value;
926 }
927
Derek Allard09de1852007-02-14 01:35:56 +0000928 if ($offset != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000929 {
Derek Allard09de1852007-02-14 01:35:56 +0000930 $this->ar_offset = $offset;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000931 if ($this->ar_caching === TRUE)
932 {
933 $this->ar_cache_offset[] = $offset;
934 }
935 }
Derek Allard09de1852007-02-14 01:35:56 +0000936
937 return $this;
938 }
939
940 // --------------------------------------------------------------------
941
942 /**
943 * Sets the OFFSET value
944 *
945 * @access public
946 * @param integer the offset value
947 * @return object
948 */
Derek Allard9b3e7b52008-02-04 23:20:34 +0000949 function offset($offset)
Derek Allard09de1852007-02-14 01:35:56 +0000950 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000951 $this->ar_offset = $offset;
952 if ($this->ar_caching === TRUE)
953 {
954 $this->ar_cache_offset[] = $offset;
955 }
956
Derek Allard09de1852007-02-14 01:35:56 +0000957 return $this;
958 }
959
960 // --------------------------------------------------------------------
961
962 /**
963 * The "set" function. Allows key/value pairs to be set for inserting or updating
964 *
965 * @access public
966 * @param mixed
967 * @param string
Derek Allard39b622d2008-01-16 21:10:09 +0000968 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +0000969 * @return object
970 */
Derek Allard39b622d2008-01-16 21:10:09 +0000971 function set($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000972 {
973 $key = $this->_object_to_array($key);
974
Derek Allard73274992008-05-05 16:39:18 +0000975 if (! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000976 {
977 $key = array($key => $value);
978 }
979
980 foreach ($key as $k => $v)
981 {
Derek Allard39b622d2008-01-16 21:10:09 +0000982 if ($escape === FALSE)
983 {
984 $this->ar_set[$this->_protect_identifiers($k)] = $v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000985 if ($this->ar_caching === TRUE)
986 {
987 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $v;
988 }
Derek Allard39b622d2008-01-16 21:10:09 +0000989 }
990 else
991 {
992 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000993 if ($this->ar_caching === TRUE)
994 {
995 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $this->escape($v);
996 }
Derek Allard39b622d2008-01-16 21:10:09 +0000997 }
Derek Allard09de1852007-02-14 01:35:56 +0000998 }
999
1000 return $this;
1001 }
1002
1003 // --------------------------------------------------------------------
1004
1005 /**
1006 * Get
1007 *
1008 * Compiles the select statement based on the other functions called
1009 * and runs the query
1010 *
1011 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +00001012 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +00001013 * @param string the limit clause
1014 * @param string the offset clause
1015 * @return object
1016 */
1017 function get($table = '', $limit = null, $offset = null)
1018 {
1019 if ($table != '')
1020 {
Derek Allard5e128942007-12-28 21:33:03 +00001021 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001022 $this->from($table);
1023 }
1024
Derek Allard73274992008-05-05 16:39:18 +00001025 if (! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001026 {
1027 $this->limit($limit, $offset);
1028 }
1029
1030 $sql = $this->_compile_select();
1031
1032 $result = $this->query($sql);
1033 $this->_reset_select();
1034 return $result;
1035 }
1036
Derek Allard09de1852007-02-14 01:35:56 +00001037 /**
Derek Allard694b5b82007-12-18 15:58:03 +00001038 * "Count All Results" query
1039 *
1040 * Generates a platform-specific query string that counts all records
1041 * returned by an Active Record query.
1042 *
1043 * @access public
1044 * @param string
1045 * @return string
1046 */
1047 function count_all_results($table = '')
1048 {
1049 if ($table != '')
1050 {
Derek Allard5e128942007-12-28 21:33:03 +00001051 $this->_track_aliases($table);
Derek Allard694b5b82007-12-18 15:58:03 +00001052 $this->from($table);
1053 }
1054
Derek Allard39b622d2008-01-16 21:10:09 +00001055 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
Derek Allard694b5b82007-12-18 15:58:03 +00001056
1057 $query = $this->query($sql);
1058 $this->_reset_select();
1059
1060 if ($query->num_rows() == 0)
1061 {
1062 return '0';
1063 }
1064
1065 $row = $query->row();
1066 return $row->numrows;
1067 }
1068
1069 // --------------------------------------------------------------------
1070
1071 /**
Derek Allard218e2bc2007-12-17 21:18:14 +00001072 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +00001073 *
1074 * Allows the where clause, limit and offset to be added directly
1075 *
1076 * @access public
1077 * @param string the where clause
1078 * @param string the limit clause
1079 * @param string the offset clause
1080 * @return object
1081 */
Derek Allard218e2bc2007-12-17 21:18:14 +00001082 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +00001083 {
1084 if ($table != '')
1085 {
Derek Allard5e128942007-12-28 21:33:03 +00001086 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001087 $this->from($table);
1088 }
1089
Derek Allard73274992008-05-05 16:39:18 +00001090 if (! is_null($where))
Derek Allard09de1852007-02-14 01:35:56 +00001091 {
1092 $this->where($where);
1093 }
1094
Derek Allard73274992008-05-05 16:39:18 +00001095 if (! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001096 {
1097 $this->limit($limit, $offset);
1098 }
1099
1100 $sql = $this->_compile_select();
1101
1102 $result = $this->query($sql);
1103 $this->_reset_select();
1104 return $result;
1105 }
Derek Allard218e2bc2007-12-17 21:18:14 +00001106
1107 // --------------------------------------------------------------------
1108
1109 /**
1110 * getwhere() is an alias of get_where()
1111 * this function is here for backwards compatibility, as
1112 * getwhere() has been deprecated
1113 */
1114 function getwhere($table = '', $where = null, $limit = null, $offset = null)
1115 {
1116 return $this->get_where($table, $where, $limit, $offset);
1117 }
Derek Allard09de1852007-02-14 01:35:56 +00001118
1119 // --------------------------------------------------------------------
1120
1121 /**
1122 * Insert
1123 *
1124 * Compiles an insert string and runs the query
1125 *
1126 * @access public
1127 * @param string the table to retrieve the results from
1128 * @param array an associative array of insert values
1129 * @return object
1130 */
1131 function insert($table = '', $set = NULL)
1132 {
Derek Allard73274992008-05-05 16:39:18 +00001133 if (! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001134 {
1135 $this->set($set);
1136 }
1137
1138 if (count($this->ar_set) == 0)
1139 {
1140 if ($this->db_debug)
1141 {
1142 return $this->display_error('db_must_use_set');
1143 }
1144 return FALSE;
1145 }
1146
1147 if ($table == '')
1148 {
Derek Allard73274992008-05-05 16:39:18 +00001149 if (! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001150 {
1151 if ($this->db_debug)
1152 {
1153 return $this->display_error('db_must_set_table');
1154 }
1155 return FALSE;
1156 }
1157
1158 $table = $this->ar_from[0];
1159 }
Derek Allard39b622d2008-01-16 21:10:09 +00001160
1161 $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 +00001162
1163 $this->_reset_write();
1164 return $this->query($sql);
1165 }
1166
1167 // --------------------------------------------------------------------
1168
1169 /**
1170 * Update
1171 *
1172 * Compiles an update string and runs the query
1173 *
1174 * @access public
1175 * @param string the table to retrieve the results from
1176 * @param array an associative array of update values
1177 * @param mixed the where clause
1178 * @return object
1179 */
Derek Allard5e128942007-12-28 21:33:03 +00001180 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001181 {
Derek Allard73274992008-05-05 16:39:18 +00001182 if (! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001183 {
1184 $this->set($set);
1185 }
1186
1187 if (count($this->ar_set) == 0)
1188 {
1189 if ($this->db_debug)
1190 {
1191 return $this->display_error('db_must_use_set');
1192 }
1193 return FALSE;
1194 }
1195
1196 if ($table == '')
1197 {
Derek Allard73274992008-05-05 16:39:18 +00001198 if (! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001199 {
1200 if ($this->db_debug)
1201 {
1202 return $this->display_error('db_must_set_table');
1203 }
1204 return FALSE;
1205 }
1206
1207 $table = $this->ar_from[0];
1208 }
1209
Derek Allarde77d77c2007-12-19 15:01:55 +00001210 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001211 {
1212 $this->where($where);
1213 }
Derek Allardda6d2402007-12-19 14:49:29 +00001214
Derek Allarde77d77c2007-12-19 15:01:55 +00001215 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +00001216 {
1217 $this->limit($limit);
1218 }
Derek Allard09de1852007-02-14 01:35:56 +00001219
Derek Allard39b622d2008-01-16 21:10:09 +00001220 $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 +00001221
1222 $this->_reset_write();
1223 return $this->query($sql);
1224 }
Derek Allard39b622d2008-01-16 21:10:09 +00001225
1226 // --------------------------------------------------------------------
1227
1228 /**
1229 * Empty Table
1230 *
1231 * Compiles a delete string and runs "DELETE FROM table"
1232 *
1233 * @access public
1234 * @param string the table to empty
1235 * @return object
1236 */
1237 function empty_table($table = '')
1238 {
1239 if ($table == '')
1240 {
Derek Allard73274992008-05-05 16:39:18 +00001241 if (! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001242 {
1243 if ($this->db_debug)
1244 {
1245 return $this->display_error('db_must_set_table');
1246 }
1247 return FALSE;
1248 }
1249
1250 $table = $this->ar_from[0];
1251 }
1252 else
1253 {
1254 $table = $this->_protect_identifiers($this->dbprefix.$table);
1255 }
1256
1257
1258 $sql = $this->_delete($table);
1259
1260 $this->_reset_write();
1261
1262 return $this->query($sql);
1263 }
1264
1265 // --------------------------------------------------------------------
1266
1267 /**
1268 * Truncate
1269 *
1270 * Compiles a truncate string and runs the query
1271 * If the database does not support the truncate() command
1272 * This function maps to "DELETE FROM table"
1273 *
1274 * @access public
1275 * @param string the table to truncate
1276 * @return object
1277 */
1278 function truncate($table = '')
1279 {
1280 if ($table == '')
1281 {
Derek Allard73274992008-05-05 16:39:18 +00001282 if (! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001283 {
1284 if ($this->db_debug)
1285 {
1286 return $this->display_error('db_must_set_table');
1287 }
1288 return FALSE;
1289 }
1290
1291 $table = $this->ar_from[0];
1292 }
1293 else
1294 {
1295 $table = $this->_protect_identifiers($this->dbprefix.$table);
1296 }
1297
1298
1299 $sql = $this->_truncate($table);
1300
1301 $this->_reset_write();
1302
1303 return $this->query($sql);
1304 }
Derek Allard09de1852007-02-14 01:35:56 +00001305
1306 // --------------------------------------------------------------------
1307
1308 /**
1309 * Delete
1310 *
1311 * Compiles a delete string and runs the query
1312 *
1313 * @access public
Derek Allard41f60d42007-12-20 20:09:22 +00001314 * @param mixed the table(s) to delete from. String or array
Derek Allard09de1852007-02-14 01:35:56 +00001315 * @param mixed the where clause
Derek Allard41f60d42007-12-20 20:09:22 +00001316 * @param mixed the limit clause
1317 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +00001318 * @return object
1319 */
Derek Allard41f60d42007-12-20 20:09:22 +00001320 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +00001321 {
1322 if ($table == '')
1323 {
Derek Allard73274992008-05-05 16:39:18 +00001324 if (! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001325 {
1326 if ($this->db_debug)
1327 {
1328 return $this->display_error('db_must_set_table');
1329 }
1330 return FALSE;
1331 }
Derek Allard39b622d2008-01-16 21:10:09 +00001332
Derek Allard09de1852007-02-14 01:35:56 +00001333 $table = $this->ar_from[0];
1334 }
Derek Allard39b622d2008-01-16 21:10:09 +00001335 elseif (is_array($table))
Derek Allard41f60d42007-12-20 20:09:22 +00001336 {
1337 foreach($table as $single_table)
1338 {
Derek Allard39b622d2008-01-16 21:10:09 +00001339 $this->delete($single_table, $where, $limit, FALSE);
Derek Allard41f60d42007-12-20 20:09:22 +00001340 }
Derek Allard39b622d2008-01-16 21:10:09 +00001341
Derek Allard41f60d42007-12-20 20:09:22 +00001342 $this->_reset_write();
1343 return;
1344 }
Derek Allard39b622d2008-01-16 21:10:09 +00001345 else
1346 {
1347 $table = $this->_protect_identifiers($this->dbprefix.$table);
1348 }
Derek Allard41f60d42007-12-20 20:09:22 +00001349
Derek Allard09de1852007-02-14 01:35:56 +00001350 if ($where != '')
1351 {
1352 $this->where($where);
1353 }
1354
Derek Allarde77d77c2007-12-19 15:01:55 +00001355 if ($limit != NULL)
1356 {
1357 $this->limit($limit);
1358 }
1359
Derek Allard39b622d2008-01-16 21:10:09 +00001360 if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
Derek Allard09de1852007-02-14 01:35:56 +00001361 {
1362 if ($this->db_debug)
1363 {
1364 return $this->display_error('db_del_must_use_where');
1365 }
Derek Allard39b622d2008-01-16 21:10:09 +00001366
Derek Allard09de1852007-02-14 01:35:56 +00001367 return FALSE;
1368 }
Derek Allard41f60d42007-12-20 20:09:22 +00001369
Derek Allard39b622d2008-01-16 21:10:09 +00001370 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001371
Derek Allard41f60d42007-12-20 20:09:22 +00001372 if ($reset_data)
1373 {
1374 $this->_reset_write();
1375 }
Derek Allard39b622d2008-01-16 21:10:09 +00001376
Derek Allard09de1852007-02-14 01:35:56 +00001377 return $this->query($sql);
1378 }
Derek Allard15ddc9d2007-12-20 13:54:39 +00001379
Derek Allard09de1852007-02-14 01:35:56 +00001380 // --------------------------------------------------------------------
1381
1382 /**
1383 * Use Table - DEPRECATED
1384 *
1385 * @deprecated use $this->db->from instead
1386 */
1387 function use_table($table)
1388 {
1389 return $this->from($table);
Derek Allard09de1852007-02-14 01:35:56 +00001390 }
1391
1392 // --------------------------------------------------------------------
1393
1394 /**
Derek Allard09de1852007-02-14 01:35:56 +00001395 * Tests whether the string has an SQL operator
1396 *
1397 * @access private
1398 * @param string
1399 * @return bool
1400 */
1401 function _has_operator($str)
1402 {
1403 $str = trim($str);
Derek Allard73274992008-05-05 16:39:18 +00001404 if (! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
Derek Allard09de1852007-02-14 01:35:56 +00001405 {
1406 return FALSE;
1407 }
1408
1409 return TRUE;
1410 }
1411
1412 // --------------------------------------------------------------------
1413
1414 /**
Derek Allard5e128942007-12-28 21:33:03 +00001415 * Track Aliases
1416 *
1417 * Used to track SQL statements written with aliased tables.
1418 *
1419 * @access private
1420 * @param string The table to inspect
1421 * @return string
1422 */
1423 function _track_aliases($table)
1424 {
1425 // if a table alias is used we can recognize it by a space
1426 if (strpos($table, " ") !== FALSE)
1427 {
1428 // if the alias is written with the AS keyowrd, get it out
Derek Allard39b622d2008-01-16 21:10:09 +00001429 $table = preg_replace('/ AS /i', ' ', $table);
Derek Allard5e128942007-12-28 21:33:03 +00001430
Derek Allard39b622d2008-01-16 21:10:09 +00001431 $this->ar_aliased_tables[] = trim(strrchr($table, " "));
Derek Allard5e128942007-12-28 21:33:03 +00001432 }
1433
1434 return $this->dbprefix.$table;
1435 }
1436
1437 // --------------------------------------------------------------------
1438
1439 /**
1440 * Filter Table Aliases
1441 *
1442 * Intelligently removes database prefixes from aliased tables
1443 *
1444 * @access private
1445 * @param array An array of compiled SQL
1446 * @return array Cleaned up statement with aliases accounted for
1447 */
1448 function _filter_table_aliases($statements)
1449 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001450
Derek Allard39b622d2008-01-16 21:10:09 +00001451 foreach ($statements as $k => $v)
Derek Allard5e128942007-12-28 21:33:03 +00001452 {
Derek Allard39b622d2008-01-16 21:10:09 +00001453 foreach ($this->ar_aliased_tables as $table)
Derek Allard5e128942007-12-28 21:33:03 +00001454 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001455 $statements[$k] = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $statements[$k]); // makes `table.field`
1456 $statements[$k] = str_replace($this->dbprefix.$table.'.', $table.'.', $statements[$k]);
Derek Allard5e128942007-12-28 21:33:03 +00001457 }
Derek Allard5e128942007-12-28 21:33:03 +00001458 }
Derek Allard39b622d2008-01-16 21:10:09 +00001459 return $statements;
Derek Allard5e128942007-12-28 21:33:03 +00001460 }
1461
1462 // --------------------------------------------------------------------
1463
1464 /**
Derek Allard09de1852007-02-14 01:35:56 +00001465 * Compile the SELECT statement
1466 *
1467 * Generates a query string based on which functions were used.
1468 * Should not be called directly. The get() function calls it.
1469 *
1470 * @access private
1471 * @return string
1472 */
Derek Allard694b5b82007-12-18 15:58:03 +00001473 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001474 {
Derek Allard78255262008-02-06 13:54:23 +00001475 $this->_merge_cache();
Derek Allard9b3e7b52008-02-04 23:20:34 +00001476
Derek Allard73274992008-05-05 16:39:18 +00001477 $sql = (! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Derek Allard09de1852007-02-14 01:35:56 +00001478
Derek Allard5162fc72008-04-15 20:05:37 +00001479 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->_filter_table_aliases($this->ar_select));
Derek Allard09de1852007-02-14 01:35:56 +00001480
Derek Allard694b5b82007-12-18 15:58:03 +00001481 if ($select_override !== FALSE)
1482 {
1483 $sql = $select_override;
1484 }
1485
Derek Allard09de1852007-02-14 01:35:56 +00001486 if (count($this->ar_from) > 0)
1487 {
1488 $sql .= "\nFROM ";
Derek Jonesc6ad0232008-01-29 18:44:54 +00001489 $sql .= $this->_from_tables($this->ar_from);
Derek Allard09de1852007-02-14 01:35:56 +00001490 }
1491
1492 if (count($this->ar_join) > 0)
Derek Allard5e128942007-12-28 21:33:03 +00001493 {
Derek Allard09de1852007-02-14 01:35:56 +00001494 $sql .= "\n";
Derek Allard5e128942007-12-28 21:33:03 +00001495
1496 // special consideration for table aliases
1497 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1498 {
1499 $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
1500 }
1501 else
1502 {
1503 $sql .= implode("\n", $this->ar_join);
1504 }
1505
Derek Allard09de1852007-02-14 01:35:56 +00001506 }
1507
1508 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1509 {
1510 $sql .= "\nWHERE ";
1511 }
1512
1513 $sql .= implode("\n", $this->ar_where);
1514
1515 if (count($this->ar_like) > 0)
1516 {
1517 if (count($this->ar_where) > 0)
1518 {
1519 $sql .= " AND ";
1520 }
1521
1522 $sql .= implode("\n", $this->ar_like);
1523 }
1524
1525 if (count($this->ar_groupby) > 0)
1526 {
Derek Allard5e128942007-12-28 21:33:03 +00001527
Derek Allard09de1852007-02-14 01:35:56 +00001528 $sql .= "\nGROUP BY ";
Derek Allard5e128942007-12-28 21:33:03 +00001529
1530 // special consideration for table aliases
1531 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1532 {
1533 $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
1534 }
1535 else
1536 {
1537 $sql .= implode(', ', $this->ar_groupby);
1538 }
Derek Allard09de1852007-02-14 01:35:56 +00001539 }
1540
1541 if (count($this->ar_having) > 0)
1542 {
1543 $sql .= "\nHAVING ";
1544 $sql .= implode("\n", $this->ar_having);
1545 }
1546
1547 if (count($this->ar_orderby) > 0)
1548 {
1549 $sql .= "\nORDER BY ";
1550 $sql .= implode(', ', $this->ar_orderby);
1551
1552 if ($this->ar_order !== FALSE)
1553 {
1554 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1555 }
1556 }
1557
1558 if (is_numeric($this->ar_limit))
1559 {
1560 $sql .= "\n";
1561 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1562 }
1563
1564 return $sql;
1565 }
1566
1567 // --------------------------------------------------------------------
1568
1569 /**
1570 * Object to Array
1571 *
1572 * Takes an object as input and converts the class variables to array key/vals
1573 *
1574 * @access public
1575 * @param object
1576 * @return array
1577 */
1578 function _object_to_array($object)
1579 {
Derek Allard73274992008-05-05 16:39:18 +00001580 if (! is_object($object))
Derek Allard09de1852007-02-14 01:35:56 +00001581 {
1582 return $object;
1583 }
1584
1585 $array = array();
1586 foreach (get_object_vars($object) as $key => $val)
1587 {
Derek Allard848b7762007-12-31 16:43:05 +00001588 // There are some built in keys we need to ignore for this conversion
Derek Allard73274992008-05-05 16:39:18 +00001589 if (! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
Derek Allard848b7762007-12-31 16:43:05 +00001590
Derek Allard09de1852007-02-14 01:35:56 +00001591 {
1592 $array[$key] = $val;
1593 }
1594 }
1595
1596 return $array;
1597 }
1598
1599 // --------------------------------------------------------------------
1600
1601 /**
Derek Allard9b3e7b52008-02-04 23:20:34 +00001602 * Start Cache
1603 *
1604 * Starts AR caching
1605 *
1606 * @access public
1607 * @return void
1608 */
1609 function start_cache()
1610 {
1611 $this->ar_caching = TRUE;
1612 }
1613
1614 // --------------------------------------------------------------------
1615
1616 /**
1617 * Stop Cache
1618 *
1619 * Stops AR caching
1620 *
1621 * @access public
1622 * @return void
1623 */
1624 function stop_cache()
1625 {
1626 $this->ar_caching = FALSE;
1627 }
1628
1629
1630 // --------------------------------------------------------------------
1631
1632 /**
1633 * Flush Cache
1634 *
1635 * Empties the AR cache
1636 *
1637 * @access public
1638 * @return void
1639 */
1640 function flush_cache()
1641 {
1642 $ar_reset_items = array(
1643 'ar_cache_select' => array(),
1644 'ar_cache_from' => array(),
1645 'ar_cache_join' => array(),
1646 'ar_cache_where' => array(),
1647 'ar_cache_like' => array(),
1648 'ar_cache_groupby' => array(),
1649 'ar_cache_having' =>array(),
1650 'ar_cache_orderby' => array(),
1651 'ar_cache_set' => array()
1652 );
1653
1654 $this->_reset_run($ar_reset_items);
1655 }
1656
1657 // --------------------------------------------------------------------
1658
1659 /**
1660 * Merge Cache
1661 *
1662 * When called, this function merges any cached AR arrays with
1663 * locally called ones.
1664 *
1665 * @access private
1666 * @return void
1667 */
1668 function _merge_cache()
1669 {
1670 $ar_items = array('select', 'from', 'join', 'where', 'like', 'groupby', 'having', 'orderby', 'set');
1671
1672 foreach ($ar_items as $ar_item)
1673 {
1674 $ar_cache_item = 'ar_cache_'.$ar_item;
1675 $ar_item = 'ar_'.$ar_item;
1676 $this->$ar_item = array_unique(array_merge($this->$ar_item, $this->$ar_cache_item));
1677 }
1678 }
1679
1680 // --------------------------------------------------------------------
1681
1682 /**
1683 * Resets the active record values. Called by the get() function
1684 *
1685 * @access private
1686 * @param array An array of fields to reset
1687 * @return void
1688 */
1689 function _reset_run($ar_reset_items)
1690 {
1691 foreach ($ar_reset_items as $item => $default_value)
1692 {
1693 if (!in_array($item, $this->ar_store_array))
1694 {
1695 $this->$item = $default_value;
1696 }
1697 }
1698 }
1699
1700 // --------------------------------------------------------------------
1701
1702 /**
Derek Allard09de1852007-02-14 01:35:56 +00001703 * Resets the active record values. Called by the get() function
1704 *
1705 * @access private
1706 * @return void
1707 */
1708 function _reset_select()
1709 {
Derek Allard9b3e7b52008-02-04 23:20:34 +00001710 $ar_reset_items = array(
1711 'ar_select' => array(),
1712 'ar_from' => array(),
1713 'ar_join' => array(),
1714 'ar_where' => array(),
1715 'ar_like' => array(),
1716 'ar_groupby' => array(),
1717 'ar_having' => array(),
1718 'ar_orderby' => array(),
1719 'ar_wherein' => array(),
1720 'ar_aliased_tables' => array(),
1721 'ar_distinct' => FALSE,
1722 'ar_limit' => FALSE,
1723 'ar_offset' => FALSE,
1724 'ar_order' => FALSE,
1725 );
1726
1727 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001728 }
1729
1730 // --------------------------------------------------------------------
1731
1732 /**
1733 * Resets the active record "write" values.
1734 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001735 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001736 *
1737 * @access private
1738 * @return void
1739 */
1740 function _reset_write()
Derek Allard9b3e7b52008-02-04 23:20:34 +00001741 {
1742 $ar_reset_items = array(
1743 'ar_set' => array(),
1744 'ar_from' => array(),
1745 'ar_where' => array(),
1746 'ar_like' => array(),
1747 'ar_orderby' => array(),
1748 'ar_limit' => FALSE,
1749 'ar_order' => FALSE
1750 );
1751
1752 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001753 }
1754
1755}
adminac94f382006-09-24 20:28:12 +00001756?>