blob: 9a7ef5def419b62d446bd1b8e9f22a7a426f2962 [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 {
Derek Allarda6325892008-05-12 17:51:47 +000098 if ($protect_identifiers !== FALSE)
99 {
100 $select = explode(',', $select);
101 }
102 else
103 {
104 $select = array($select);
105 }
Derek Allard09de1852007-02-14 01:35:56 +0000106 }
107
108 foreach ($select as $val)
109 {
110 $val = trim($val);
Derek Allard39b622d2008-01-16 21:10:09 +0000111
112 if ($val != '*' && $protect_identifiers !== FALSE)
113 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000114 if (strpos($val, '.') !== FALSE)
115 {
116 $val = $this->dbprefix.$val;
117 }
118 else
119 {
120 $val = $this->_protect_identifiers($val);
121 }
Derek Allard39b622d2008-01-16 21:10:09 +0000122 }
Derek Allard09de1852007-02-14 01:35:56 +0000123
124 if ($val != '')
Derek Allard39b622d2008-01-16 21:10:09 +0000125 {
Derek Allard09de1852007-02-14 01:35:56 +0000126 $this->ar_select[] = $val;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000127 if ($this->ar_caching === TRUE)
128 {
129 $this->ar_cache_select[] = $val;
130 }
Derek Allard39b622d2008-01-16 21:10:09 +0000131 }
Derek Allard09de1852007-02-14 01:35:56 +0000132 }
133 return $this;
134 }
Derek Allard39b622d2008-01-16 21:10:09 +0000135
136 // --------------------------------------------------------------------
137
138 /**
139 * Select Max
140 *
141 * Generates a SELECT MAX(field) portion of a query
142 *
143 * @access public
144 * @param string the field
145 * @param string an alias
146 * @return object
147 */
148 function select_max($select = '', $alias='')
149 {
150 if (!is_string($select) || $select == '')
151 {
152 $this->display_error('db_invalid_query');
153 }
Derek Allard09de1852007-02-14 01:35:56 +0000154
Derek Allard39b622d2008-01-16 21:10:09 +0000155 $alias = ($alias != '') ? $alias : $select;
156
157 $sql = 'MAX('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
158
159 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000160 if ($this->ar_caching === TRUE)
161 {
162 $this->ar_cache_select[] = $sql;
163 }
Derek Allard39b622d2008-01-16 21:10:09 +0000164
165 return $this;
Derek Allard39b622d2008-01-16 21:10:09 +0000166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Select Min
172 *
173 * Generates a SELECT MIN(field) portion of a query
174 *
175 * @access public
176 * @param string the field
177 * @param string an alias
178 * @return object
179 */
180 function select_min($select = '', $alias='')
181 {
182 if (!is_string($select) || $select == '')
183 {
184 $this->display_error('db_invalid_query');
185 }
186
187 $alias = ($alias != '') ? $alias : $select;
188
189 $sql = 'MIN('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
190
191 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000192 if ($this->ar_caching === TRUE)
193 {
194 $this->ar_cache_select[] = $sql;
195 }
196
Derek Allard39b622d2008-01-16 21:10:09 +0000197 return $this;
198 }
199
200 // --------------------------------------------------------------------
201
202 /**
203 * Select Average
204 *
205 * Generates a SELECT AVG(field) portion of a query
206 *
207 * @access public
208 * @param string the field
209 * @param string an alias
210 * @return object
211 */
212 function select_avg($select = '', $alias='')
213 {
214 if (!is_string($select) || $select == '')
215 {
216 $this->display_error('db_invalid_query');
217 }
218
219 $alias = ($alias != '') ? $alias : $select;
220
221 $sql = 'AVG('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
222
223 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000224 if ($this->ar_caching === TRUE)
225 {
226 $this->ar_cache_select[] = $sql;
227 }
228
Derek Allard39b622d2008-01-16 21:10:09 +0000229 return $this;
230 }
231
232 // --------------------------------------------------------------------
233
234 /**
235 * Select Sum
236 *
237 * Generates a SELECT SUM(field) portion of a query
238 *
239 * @access public
240 * @param string the field
241 * @param string an alias
242 * @return object
243 */
244 function select_sum($select = '', $alias='')
245 {
246 if (!is_string($select) || $select == '')
247 {
248 $this->display_error('db_invalid_query');
249 }
250
251 $alias = ($alias != '') ? $alias : $select;
252
253 $sql = 'SUM('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
254
255 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000256 if ($this->ar_caching === TRUE)
257 {
258 $this->ar_cache_select[] = $sql;
259 }
260
Derek Allard39b622d2008-01-16 21:10:09 +0000261 return $this;
262 }
263
Derek Allard09de1852007-02-14 01:35:56 +0000264 // --------------------------------------------------------------------
265
266 /**
267 * DISTINCT
268 *
269 * Sets a flag which tells the query string compiler to add DISTINCT
270 *
271 * @access public
272 * @param bool
273 * @return object
274 */
275 function distinct($val = TRUE)
276 {
277 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
278 return $this;
279 }
280
281 // --------------------------------------------------------------------
282
283 /**
284 * From
285 *
286 * Generates the FROM portion of the query
287 *
288 * @access public
289 * @param mixed can be a string or array
290 * @return object
291 */
292 function from($from)
293 {
294 foreach ((array)$from as $val)
295 {
Derek Allard39b622d2008-01-16 21:10:09 +0000296 $this->ar_from[] = $this->_protect_identifiers($this->_track_aliases($val));
Derek Allard9b3e7b52008-02-04 23:20:34 +0000297 if ($this->ar_caching === TRUE)
298 {
Derek Allard9a4d1da2008-02-25 14:18:38 +0000299 $this->ar_cache_from[] = $this->_protect_identifiers($this->_track_aliases($val));
Derek Allard9b3e7b52008-02-04 23:20:34 +0000300 }
Derek Allard09de1852007-02-14 01:35:56 +0000301 }
Derek Allard5e128942007-12-28 21:33:03 +0000302
Derek Allard09de1852007-02-14 01:35:56 +0000303 return $this;
304 }
305
306 // --------------------------------------------------------------------
307
308 /**
309 * Join
310 *
311 * Generates the JOIN portion of the query
312 *
313 * @access public
314 * @param string
315 * @param string the join condition
316 * @param string the type of join
317 * @return object
318 */
319 function join($table, $cond, $type = '')
320 {
321 if ($type != '')
322 {
323 $type = strtoupper(trim($type));
324
Derek Allard73274992008-05-05 16:39:18 +0000325 if (! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
Derek Allard09de1852007-02-14 01:35:56 +0000326 {
327 $type = '';
328 }
329 else
330 {
331 $type .= ' ';
332 }
333 }
334
Derek Allard09de1852007-02-14 01:35:56 +0000335 // If a DB prefix is used we might need to add it to the column names
336 if ($this->dbprefix)
337 {
Derek Allard39b622d2008-01-16 21:10:09 +0000338 $this->_track_aliases($table);
339
Derek Allard09de1852007-02-14 01:35:56 +0000340 // First we remove any existing prefixes in the condition to avoid duplicates
341 $cond = preg_replace('|('.$this->dbprefix.')([\w\.]+)([\W\s]+)|', "$2$3", $cond);
342
343 // Next we add the prefixes to the condition
344 $cond = preg_replace('|([\w\.]+)([\W\s]+)(.+)|', $this->dbprefix . "$1$2" . $this->dbprefix . "$3", $cond);
Derek Allard5e128942007-12-28 21:33:03 +0000345 }
346
Derek Allard9b3e7b52008-02-04 23:20:34 +0000347 $join = $type.'JOIN '.$this->_protect_identifiers($this->dbprefix.$table, TRUE).' ON '.$cond;
348
349 $this->ar_join[] = $join;
350 if ($this->ar_caching === TRUE)
351 {
352 $this->ar_cache_join[] = $join;
353 }
354
Derek Allard09de1852007-02-14 01:35:56 +0000355 return $this;
356 }
357
358 // --------------------------------------------------------------------
359
360 /**
361 * Where
362 *
363 * Generates the WHERE portion of the query. Separates
364 * multiple calls with AND
365 *
366 * @access public
367 * @param mixed
368 * @param mixed
369 * @return object
370 */
Derek Allard39b622d2008-01-16 21:10:09 +0000371 function where($key, $value = NULL, $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000372 {
Derek Allard39b622d2008-01-16 21:10:09 +0000373 return $this->_where($key, $value, 'AND ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000374 }
375
376 // --------------------------------------------------------------------
377
378 /**
379 * OR Where
380 *
381 * Generates the WHERE portion of the query. Separates
382 * multiple calls with OR
383 *
384 * @access public
385 * @param mixed
386 * @param mixed
387 * @return object
388 */
Derek Allard39b622d2008-01-16 21:10:09 +0000389 function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000390 {
Derek Allard39b622d2008-01-16 21:10:09 +0000391 return $this->_where($key, $value, 'OR ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000392 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000393
394 // --------------------------------------------------------------------
395
396 /**
397 * orwhere() is an alias of or_where()
398 * this function is here for backwards compatibility, as
399 * orwhere() has been deprecated
400 */
Derek Allard39b622d2008-01-16 21:10:09 +0000401 function orwhere($key, $value = NULL, $escape = TRUE)
Derek Allard218e2bc2007-12-17 21:18:14 +0000402 {
Derek Allard39b622d2008-01-16 21:10:09 +0000403 return $this->or_where($key, $value, $escape);
Derek Allard218e2bc2007-12-17 21:18:14 +0000404 }
Derek Allard67b44ed2008-01-12 16:18:02 +0000405
406 // --------------------------------------------------------------------
407
408 /**
Derek Allard09de1852007-02-14 01:35:56 +0000409 * Where
410 *
411 * Called by where() or orwhere()
412 *
413 * @access private
414 * @param mixed
415 * @param mixed
416 * @param string
417 * @return object
418 */
Derek Allard39b622d2008-01-16 21:10:09 +0000419 function _where($key, $value = NULL, $type = 'AND ', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000420 {
Derek Allard73274992008-05-05 16:39:18 +0000421 if (! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000422 {
423 $key = array($key => $value);
424 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000425
Derek Allard09de1852007-02-14 01:35:56 +0000426 foreach ($key as $k => $v)
427 {
428 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Derek Allard15ddc9d2007-12-20 13:54:39 +0000429
Derek Allard73274992008-05-05 16:39:18 +0000430 if (! $this->_has_operator($k) && is_null($key[$k]))
Derek Allard15ddc9d2007-12-20 13:54:39 +0000431 {
432 // value appears not to have been set, assign the test to IS NULL
433 $k .= ' IS NULL';
434 }
Derek Allard09de1852007-02-14 01:35:56 +0000435
Derek Allard73274992008-05-05 16:39:18 +0000436 if (! is_null($v))
Derek Allard09de1852007-02-14 01:35:56 +0000437 {
Derek Allard39b622d2008-01-16 21:10:09 +0000438
439 if ($escape === TRUE)
440 {
441 // exception for "field<=" keys
442 if ($this->_has_operator($k))
443 {
444 $k = preg_replace("/([A-Za-z_0-9]+)/", $this->_protect_identifiers('$1'), $k);
445 }
446 else
447 {
448 $k = $this->_protect_identifiers($k);
449 }
450 }
451
Derek Allard73274992008-05-05 16:39:18 +0000452 if (! $this->_has_operator($k))
Derek Allard09de1852007-02-14 01:35:56 +0000453 {
454 $k .= ' =';
455 }
Derek Allard39b622d2008-01-16 21:10:09 +0000456
Derek Allard16629b12008-04-06 19:58:20 +0000457 if ($v !== '' AND $v !== NULL)
Derek Allard96863ce2008-04-06 19:20:17 +0000458 {
459 $v = ' '.$this->escape($v);
460 }
Derek Allard09de1852007-02-14 01:35:56 +0000461 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000462 else
463 {
Derek Allard16629b12008-04-06 19:58:20 +0000464
465 if ($escape === TRUE)
466 {
467 $k = $this->_protect_identifiers($k, TRUE);
468 }
469
Derek Allard9b3e7b52008-02-04 23:20:34 +0000470 }
471
Derek Allard09de1852007-02-14 01:35:56 +0000472 $this->ar_where[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000473 if ($this->ar_caching === TRUE)
474 {
475 $this->ar_cache_where[] = $prefix.$k.$v;
476 }
477
Derek Allard09de1852007-02-14 01:35:56 +0000478 }
479 return $this;
480 }
Derek Allard80dd7022007-12-18 23:55:06 +0000481
482 // --------------------------------------------------------------------
483
484 /**
485 * Where_in
486 *
Derek Allardc6935512007-12-19 14:23:19 +0000487 * Generates a WHERE field IN ('item', 'item') SQL query joined with
488 * AND if appropriate
Derek Allard80dd7022007-12-18 23:55:06 +0000489 *
490 * @access public
491 * @param string The field to search
492 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000493 * @return object
494 */
495 function where_in($key = NULL, $values = NULL)
496 {
497 return $this->_where_in($key, $values);
498 }
499
500 // --------------------------------------------------------------------
501
502 /**
503 * Where_in_or
504 *
505 * Generates a WHERE field IN ('item', 'item') SQL query joined with
506 * OR if appropriate
507 *
508 * @access public
509 * @param string The field to search
510 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000511 * @return object
512 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000513 function or_where_in($key = NULL, $values = NULL)
Derek Allardc6935512007-12-19 14:23:19 +0000514 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000515 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000516 }
517
518 // --------------------------------------------------------------------
519
520 /**
521 * Where_not_in
522 *
523 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
524 * with AND if appropriate
525 *
526 * @access public
527 * @param string The field to search
528 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000529 * @return object
530 */
531 function where_not_in($key = NULL, $values = NULL)
532 {
533 return $this->_where_in($key, $values, TRUE);
534 }
535
536 // --------------------------------------------------------------------
537
538 /**
539 * Where_not_in_or
540 *
541 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
542 * with OR if appropriate
543 *
544 * @access public
545 * @param string The field to search
546 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000547 * @return object
548 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000549 function or_where_not_in($key = NULL, $values = NULL)
Derek Allardc6935512007-12-19 14:23:19 +0000550 {
Derek Jonesd0072432008-05-07 22:06:51 +0000551 return $this->_where_in($key, $values, TRUE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000552 }
553
554 // --------------------------------------------------------------------
555
556 /**
557 * Where_in
558 *
559 * Called by where_in, where_in_or, where_not_in, where_not_in_or
560 *
561 * @access public
562 * @param string The field to search
563 * @param array The values searched on
564 * @param boolean If the statement whould be IN or NOT IN
Derek Allard80dd7022007-12-18 23:55:06 +0000565 * @param string
566 * @return object
567 */
Derek Allard15ddc9d2007-12-20 13:54:39 +0000568 function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard80dd7022007-12-18 23:55:06 +0000569 {
570 if ($key === NULL || !is_array($values))
571 {
572 return;
573 }
574
Derek Allardc6935512007-12-19 14:23:19 +0000575 $not = ($not) ? ' NOT ' : '';
Derek Allard80dd7022007-12-18 23:55:06 +0000576
577 foreach ($values as $value)
578 {
579 $this->ar_wherein[] = $this->escape($value);
580 }
581
582 $prefix = (count($this->ar_where) == 0) ? '' : $type;
583
Derek Allard9b3e7b52008-02-04 23:20:34 +0000584 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
585
586 $this->ar_where[] = $where_in;
587 if ($this->ar_caching === TRUE)
588 {
589 $this->ar_cache_where[] = $where_in;
590 }
Derek Allard80dd7022007-12-18 23:55:06 +0000591
Derek Allard8f000212008-01-18 14:45:59 +0000592 // reset the array for multiple calls
593 $this->ar_wherein = array();
Derek Allard80dd7022007-12-18 23:55:06 +0000594 return $this;
595 }
596
Derek Allard09de1852007-02-14 01:35:56 +0000597 // --------------------------------------------------------------------
598
599 /**
600 * Like
601 *
602 * Generates a %LIKE% portion of the query. Separates
603 * multiple calls with AND
604 *
605 * @access public
606 * @param mixed
607 * @param mixed
608 * @return object
609 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000610 function like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000611 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000612 return $this->_like($field, $match, 'AND ', $side);
Derek Allard09de1852007-02-14 01:35:56 +0000613 }
Derek Allarde54e3d22007-12-19 15:53:44 +0000614
615 // --------------------------------------------------------------------
616
617 /**
618 * Not Like
619 *
620 * Generates a NOT LIKE portion of the query. Separates
621 * multiple calls with AND
622 *
623 * @access public
624 * @param mixed
625 * @param mixed
626 * @return object
627 */
628 function not_like($field, $match = '', $side = 'both')
629 {
630 return $this->_like($field, $match, 'AND ', $side, ' NOT');
631 }
632
Derek Allard09de1852007-02-14 01:35:56 +0000633 // --------------------------------------------------------------------
634
635 /**
636 * OR Like
637 *
638 * Generates a %LIKE% portion of the query. Separates
639 * multiple calls with OR
640 *
641 * @access public
642 * @param mixed
643 * @param mixed
644 * @return object
645 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000646 function or_like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000647 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000648 return $this->_like($field, $match, 'OR ', $side);
649 }
650
651 // --------------------------------------------------------------------
652
653 /**
Derek Allarde54e3d22007-12-19 15:53:44 +0000654 * OR Not Like
655 *
656 * Generates a NOT LIKE portion of the query. Separates
657 * multiple calls with OR
658 *
659 * @access public
660 * @param mixed
661 * @param mixed
662 * @return object
663 */
664 function or_not_like($field, $match = '', $side = 'both')
665 {
666 return $this->_like($field, $match, 'OR ', $side, 'NOT ');
667 }
668
669 // --------------------------------------------------------------------
670
671 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000672 * orlike() is an alias of or_like()
673 * this function is here for backwards compatibility, as
674 * orlike() has been deprecated
675 */
676 function orlike($field, $match = '', $side = 'both')
677 {
Derek Allard4a310b72008-01-30 21:32:47 +0000678 return $this->or_like($field, $match, $side);
Derek Allard09de1852007-02-14 01:35:56 +0000679 }
680
681 // --------------------------------------------------------------------
682
683 /**
684 * Like
685 *
686 * Called by like() or orlike()
687 *
688 * @access private
689 * @param mixed
690 * @param mixed
691 * @param string
692 * @return object
693 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000694 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard09de1852007-02-14 01:35:56 +0000695 {
Derek Allard73274992008-05-05 16:39:18 +0000696 if (! is_array($field))
Derek Allard09de1852007-02-14 01:35:56 +0000697 {
698 $field = array($field => $match);
699 }
700
701 foreach ($field as $k => $v)
Derek Allarde54e3d22007-12-19 15:53:44 +0000702 {
703
Derek Allard39b622d2008-01-16 21:10:09 +0000704 $k = $this->_protect_identifiers($k);
705
Derek Allard09de1852007-02-14 01:35:56 +0000706 $prefix = (count($this->ar_like) == 0) ? '' : $type;
Derek Allarde54e3d22007-12-19 15:53:44 +0000707
Derek Allard09de1852007-02-14 01:35:56 +0000708 $v = $this->escape_str($v);
Derek Allarde54e3d22007-12-19 15:53:44 +0000709
Derek Allard218e2bc2007-12-17 21:18:14 +0000710 if ($side == 'before')
711 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000712 $like_statement = $prefix." $k $not LIKE '%{$v}'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000713 }
714 elseif ($side == 'after')
715 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000716 $like_statement = $prefix." $k $not LIKE '{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000717 }
718 else
719 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000720 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000721 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000722
723 $this->ar_like[] = $like_statement;
724 if ($this->ar_caching === TRUE)
725 {
726 $this->ar_cache_like[] = $like_statement;
727 }
728
Derek Allard09de1852007-02-14 01:35:56 +0000729 }
730 return $this;
731 }
732
733 // --------------------------------------------------------------------
734
735 /**
736 * GROUP BY
737 *
738 * @access public
739 * @param string
740 * @return object
741 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000742 function group_by($by)
Derek Allard09de1852007-02-14 01:35:56 +0000743 {
744 if (is_string($by))
745 {
746 $by = explode(',', $by);
747 }
748
749 foreach ($by as $val)
750 {
751 $val = trim($val);
752
753 if ($val != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000754 {
Derek Allard39b622d2008-01-16 21:10:09 +0000755 $this->ar_groupby[] = $this->_protect_identifiers($val);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000756 if ($this->ar_caching === TRUE)
757 {
758 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
759 }
760 }
Derek Allard09de1852007-02-14 01:35:56 +0000761 }
762 return $this;
763 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000764
765 // --------------------------------------------------------------------
766
767 /**
768 * groupby() is an alias of group_by()
769 * this function is here for backwards compatibility, as
770 * groupby() has been deprecated
771 */
772 function groupby($by)
773 {
774 return $this->group_by($by);
775 }
776
Derek Allard09de1852007-02-14 01:35:56 +0000777 // --------------------------------------------------------------------
778
779 /**
780 * Sets the HAVING value
781 *
782 * Separates multiple calls with AND
783 *
784 * @access public
785 * @param string
786 * @param string
787 * @return object
788 */
Derek Allarde808aac2008-04-06 18:22:00 +0000789 function having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000790 {
Derek Allarde808aac2008-04-06 18:22:00 +0000791 return $this->_having($key, $value, 'AND ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000792 }
Derek Allard43e8cbf2008-03-01 23:14:43 +0000793
794 // --------------------------------------------------------------------
795
796 /**
797 * orhaving() is an alias of or_having()
798 * this function is here for backwards compatibility, as
799 * orhaving() has been deprecated
800 */
801
Derek Allarde808aac2008-04-06 18:22:00 +0000802 function orhaving($key, $value = '', $escape = TRUE)
Derek Allard43e8cbf2008-03-01 23:14:43 +0000803 {
Derek Allarde808aac2008-04-06 18:22:00 +0000804 return $this->or_having($key, $value = '', $escape);
Derek Allard43e8cbf2008-03-01 23:14:43 +0000805 }
Derek Allard09de1852007-02-14 01:35:56 +0000806 // --------------------------------------------------------------------
807
808 /**
809 * Sets the OR HAVING value
810 *
811 * Separates multiple calls with OR
812 *
813 * @access public
814 * @param string
815 * @param string
816 * @return object
817 */
Derek Allarde808aac2008-04-06 18:22:00 +0000818 function or_having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000819 {
Derek Allarde808aac2008-04-06 18:22:00 +0000820 return $this->_having($key, $value, 'OR ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000821 }
822
823 // --------------------------------------------------------------------
824
825 /**
826 * Sets the HAVING values
827 *
828 * Called by having() or orhaving()
829 *
830 * @access private
831 * @param string
Derek Allard9b3e7b52008-02-04 23:20:34 +0000832
Derek Allard09de1852007-02-14 01:35:56 +0000833 * @param string
834 * @return object
835 */
Derek Allarde808aac2008-04-06 18:22:00 +0000836 function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000837 {
Derek Allard73274992008-05-05 16:39:18 +0000838 if (! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000839 {
840 $key = array($key => $value);
841 }
842
843 foreach ($key as $k => $v)
844 {
845 $prefix = (count($this->ar_having) == 0) ? '' : $type;
Derek Allarde808aac2008-04-06 18:22:00 +0000846
847 if ($escape === TRUE)
848 {
849 $k = $this->_protect_identifiers($k);
850 }
851
Derek Allard09de1852007-02-14 01:35:56 +0000852
853 if ($v != '')
854 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000855 $v = ' '.$this->escape_str($v);
Derek Allard09de1852007-02-14 01:35:56 +0000856 }
857
858 $this->ar_having[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000859 if ($this->ar_caching === TRUE)
860 {
861 $this->ar_cache_having[] = $prefix.$k.$v;
862 }
Derek Allard09de1852007-02-14 01:35:56 +0000863 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000864
Derek Allard09de1852007-02-14 01:35:56 +0000865 return $this;
866 }
867
868 // --------------------------------------------------------------------
869
870 /**
871 * Sets the ORDER BY value
872 *
873 * @access public
874 * @param string
875 * @param string direction: asc or desc
876 * @return object
877 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000878 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000879 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000880 if (strtolower($direction) == 'random')
881 {
882 $orderby = ''; // Random results want or don't need a field name
883 $direction = $this->_random_keyword;
884 }
885 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000886 {
Derek Allard92782492007-08-10 11:26:01 +0000887 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000888 }
889
Derek Allard9b3e7b52008-02-04 23:20:34 +0000890 $orderby_statement = $this->_protect_identifiers($orderby, TRUE).$direction;
891
892 $this->ar_orderby[] = $orderby_statement;
893 if ($this->ar_caching === TRUE)
894 {
895 $this->ar_cache_orderby[] = $orderby_statement;
896 }
897
Derek Allard09de1852007-02-14 01:35:56 +0000898 return $this;
899 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000900
Derek Allard218e2bc2007-12-17 21:18:14 +0000901 // --------------------------------------------------------------------
902
903 /**
904 * orderby() is an alias of order_by()
905 * this function is here for backwards compatibility, as
906 * orderby() has been deprecated
907 */
908 function orderby($orderby, $direction = '')
909 {
910 return $this->order_by($orderby, $direction);
911 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000912
Derek Allard09de1852007-02-14 01:35:56 +0000913 // --------------------------------------------------------------------
914
915 /**
916 * Sets the LIMIT value
917 *
918 * @access public
919 * @param integer the limit value
920 * @param integer the offset value
921 * @return object
922 */
923 function limit($value, $offset = '')
924 {
925 $this->ar_limit = $value;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000926 if ($this->ar_caching === TRUE)
927 {
928 $this->ar_cache_limit[] = $value;
929 }
930
Derek Allard09de1852007-02-14 01:35:56 +0000931 if ($offset != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000932 {
Derek Allard09de1852007-02-14 01:35:56 +0000933 $this->ar_offset = $offset;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000934 if ($this->ar_caching === TRUE)
935 {
936 $this->ar_cache_offset[] = $offset;
937 }
938 }
Derek Allard09de1852007-02-14 01:35:56 +0000939
940 return $this;
941 }
942
943 // --------------------------------------------------------------------
944
945 /**
946 * Sets the OFFSET value
947 *
948 * @access public
949 * @param integer the offset value
950 * @return object
951 */
Derek Allard9b3e7b52008-02-04 23:20:34 +0000952 function offset($offset)
Derek Allard09de1852007-02-14 01:35:56 +0000953 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000954 $this->ar_offset = $offset;
955 if ($this->ar_caching === TRUE)
956 {
957 $this->ar_cache_offset[] = $offset;
958 }
959
Derek Allard09de1852007-02-14 01:35:56 +0000960 return $this;
961 }
962
963 // --------------------------------------------------------------------
964
965 /**
966 * The "set" function. Allows key/value pairs to be set for inserting or updating
967 *
968 * @access public
969 * @param mixed
970 * @param string
Derek Allard39b622d2008-01-16 21:10:09 +0000971 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +0000972 * @return object
973 */
Derek Allard39b622d2008-01-16 21:10:09 +0000974 function set($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000975 {
976 $key = $this->_object_to_array($key);
977
Derek Allard73274992008-05-05 16:39:18 +0000978 if (! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000979 {
980 $key = array($key => $value);
981 }
982
983 foreach ($key as $k => $v)
984 {
Derek Allard39b622d2008-01-16 21:10:09 +0000985 if ($escape === FALSE)
986 {
987 $this->ar_set[$this->_protect_identifiers($k)] = $v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000988 if ($this->ar_caching === TRUE)
989 {
990 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $v;
991 }
Derek Allard39b622d2008-01-16 21:10:09 +0000992 }
993 else
994 {
995 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000996 if ($this->ar_caching === TRUE)
997 {
998 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $this->escape($v);
999 }
Derek Allard39b622d2008-01-16 21:10:09 +00001000 }
Derek Allard09de1852007-02-14 01:35:56 +00001001 }
1002
1003 return $this;
1004 }
1005
1006 // --------------------------------------------------------------------
1007
1008 /**
1009 * Get
1010 *
1011 * Compiles the select statement based on the other functions called
1012 * and runs the query
1013 *
1014 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +00001015 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +00001016 * @param string the limit clause
1017 * @param string the offset clause
1018 * @return object
1019 */
1020 function get($table = '', $limit = null, $offset = null)
1021 {
1022 if ($table != '')
1023 {
Derek Allard5e128942007-12-28 21:33:03 +00001024 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001025 $this->from($table);
1026 }
1027
Derek Allard73274992008-05-05 16:39:18 +00001028 if (! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001029 {
1030 $this->limit($limit, $offset);
1031 }
1032
1033 $sql = $this->_compile_select();
1034
1035 $result = $this->query($sql);
1036 $this->_reset_select();
1037 return $result;
1038 }
1039
Derek Allard09de1852007-02-14 01:35:56 +00001040 /**
Derek Allard694b5b82007-12-18 15:58:03 +00001041 * "Count All Results" query
1042 *
1043 * Generates a platform-specific query string that counts all records
1044 * returned by an Active Record query.
1045 *
1046 * @access public
1047 * @param string
1048 * @return string
1049 */
1050 function count_all_results($table = '')
1051 {
1052 if ($table != '')
1053 {
Derek Allard5e128942007-12-28 21:33:03 +00001054 $this->_track_aliases($table);
Derek Allard694b5b82007-12-18 15:58:03 +00001055 $this->from($table);
1056 }
1057
Derek Allard39b622d2008-01-16 21:10:09 +00001058 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
Derek Allard694b5b82007-12-18 15:58:03 +00001059
1060 $query = $this->query($sql);
1061 $this->_reset_select();
1062
1063 if ($query->num_rows() == 0)
1064 {
1065 return '0';
1066 }
1067
1068 $row = $query->row();
1069 return $row->numrows;
1070 }
1071
1072 // --------------------------------------------------------------------
1073
1074 /**
Derek Allard218e2bc2007-12-17 21:18:14 +00001075 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +00001076 *
1077 * Allows the where clause, limit and offset to be added directly
1078 *
1079 * @access public
1080 * @param string the where clause
1081 * @param string the limit clause
1082 * @param string the offset clause
1083 * @return object
1084 */
Derek Allard218e2bc2007-12-17 21:18:14 +00001085 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +00001086 {
1087 if ($table != '')
1088 {
Derek Allard5e128942007-12-28 21:33:03 +00001089 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001090 $this->from($table);
1091 }
1092
Derek Allard73274992008-05-05 16:39:18 +00001093 if (! is_null($where))
Derek Allard09de1852007-02-14 01:35:56 +00001094 {
1095 $this->where($where);
1096 }
1097
Derek Allard73274992008-05-05 16:39:18 +00001098 if (! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001099 {
1100 $this->limit($limit, $offset);
1101 }
1102
1103 $sql = $this->_compile_select();
1104
1105 $result = $this->query($sql);
1106 $this->_reset_select();
1107 return $result;
1108 }
Derek Allard218e2bc2007-12-17 21:18:14 +00001109
1110 // --------------------------------------------------------------------
1111
1112 /**
1113 * getwhere() is an alias of get_where()
1114 * this function is here for backwards compatibility, as
1115 * getwhere() has been deprecated
1116 */
1117 function getwhere($table = '', $where = null, $limit = null, $offset = null)
1118 {
1119 return $this->get_where($table, $where, $limit, $offset);
1120 }
Derek Allard09de1852007-02-14 01:35:56 +00001121
1122 // --------------------------------------------------------------------
1123
1124 /**
1125 * Insert
1126 *
1127 * Compiles an insert string and runs the query
1128 *
1129 * @access public
1130 * @param string the table to retrieve the results from
1131 * @param array an associative array of insert values
1132 * @return object
1133 */
1134 function insert($table = '', $set = NULL)
1135 {
Derek Allard73274992008-05-05 16:39:18 +00001136 if (! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001137 {
1138 $this->set($set);
1139 }
1140
1141 if (count($this->ar_set) == 0)
1142 {
1143 if ($this->db_debug)
1144 {
1145 return $this->display_error('db_must_use_set');
1146 }
1147 return FALSE;
1148 }
1149
1150 if ($table == '')
1151 {
Derek Allard73274992008-05-05 16:39:18 +00001152 if (! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001153 {
1154 if ($this->db_debug)
1155 {
1156 return $this->display_error('db_must_set_table');
1157 }
1158 return FALSE;
1159 }
1160
1161 $table = $this->ar_from[0];
1162 }
Derek Allard39b622d2008-01-16 21:10:09 +00001163
1164 $sql = $this->_insert($this->_protect_identifiers($this->dbprefix.$table), array_keys($this->ar_set), array_values($this->ar_set));
Derek Allard09de1852007-02-14 01:35:56 +00001165
1166 $this->_reset_write();
1167 return $this->query($sql);
1168 }
1169
1170 // --------------------------------------------------------------------
1171
1172 /**
1173 * Update
1174 *
1175 * Compiles an update string and runs the query
1176 *
1177 * @access public
1178 * @param string the table to retrieve the results from
1179 * @param array an associative array of update values
1180 * @param mixed the where clause
1181 * @return object
1182 */
Derek Allard5e128942007-12-28 21:33:03 +00001183 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001184 {
Derek Allard73274992008-05-05 16:39:18 +00001185 if (! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001186 {
1187 $this->set($set);
1188 }
1189
1190 if (count($this->ar_set) == 0)
1191 {
1192 if ($this->db_debug)
1193 {
1194 return $this->display_error('db_must_use_set');
1195 }
1196 return FALSE;
1197 }
1198
1199 if ($table == '')
1200 {
Derek Allard73274992008-05-05 16:39:18 +00001201 if (! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001202 {
1203 if ($this->db_debug)
1204 {
1205 return $this->display_error('db_must_set_table');
1206 }
1207 return FALSE;
1208 }
1209
1210 $table = $this->ar_from[0];
1211 }
1212
Derek Allarde77d77c2007-12-19 15:01:55 +00001213 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001214 {
1215 $this->where($where);
1216 }
Derek Allardda6d2402007-12-19 14:49:29 +00001217
Derek Allarde77d77c2007-12-19 15:01:55 +00001218 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +00001219 {
1220 $this->limit($limit);
1221 }
Derek Allard09de1852007-02-14 01:35:56 +00001222
Derek Allard39b622d2008-01-16 21:10:09 +00001223 $sql = $this->_update($this->_protect_identifiers($this->dbprefix.$table), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001224
1225 $this->_reset_write();
1226 return $this->query($sql);
1227 }
Derek Allard39b622d2008-01-16 21:10:09 +00001228
1229 // --------------------------------------------------------------------
1230
1231 /**
1232 * Empty Table
1233 *
1234 * Compiles a delete string and runs "DELETE FROM table"
1235 *
1236 * @access public
1237 * @param string the table to empty
1238 * @return object
1239 */
1240 function empty_table($table = '')
1241 {
1242 if ($table == '')
1243 {
Derek Allard73274992008-05-05 16:39:18 +00001244 if (! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001245 {
1246 if ($this->db_debug)
1247 {
1248 return $this->display_error('db_must_set_table');
1249 }
1250 return FALSE;
1251 }
1252
1253 $table = $this->ar_from[0];
1254 }
1255 else
1256 {
1257 $table = $this->_protect_identifiers($this->dbprefix.$table);
1258 }
1259
1260
1261 $sql = $this->_delete($table);
1262
1263 $this->_reset_write();
1264
1265 return $this->query($sql);
1266 }
1267
1268 // --------------------------------------------------------------------
1269
1270 /**
1271 * Truncate
1272 *
1273 * Compiles a truncate string and runs the query
1274 * If the database does not support the truncate() command
1275 * This function maps to "DELETE FROM table"
1276 *
1277 * @access public
1278 * @param string the table to truncate
1279 * @return object
1280 */
1281 function truncate($table = '')
1282 {
1283 if ($table == '')
1284 {
Derek Allard73274992008-05-05 16:39:18 +00001285 if (! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001286 {
1287 if ($this->db_debug)
1288 {
1289 return $this->display_error('db_must_set_table');
1290 }
1291 return FALSE;
1292 }
1293
1294 $table = $this->ar_from[0];
1295 }
1296 else
1297 {
1298 $table = $this->_protect_identifiers($this->dbprefix.$table);
1299 }
1300
1301
1302 $sql = $this->_truncate($table);
1303
1304 $this->_reset_write();
1305
1306 return $this->query($sql);
1307 }
Derek Allard09de1852007-02-14 01:35:56 +00001308
1309 // --------------------------------------------------------------------
1310
1311 /**
1312 * Delete
1313 *
1314 * Compiles a delete string and runs the query
1315 *
1316 * @access public
Derek Allard41f60d42007-12-20 20:09:22 +00001317 * @param mixed the table(s) to delete from. String or array
Derek Allard09de1852007-02-14 01:35:56 +00001318 * @param mixed the where clause
Derek Allard41f60d42007-12-20 20:09:22 +00001319 * @param mixed the limit clause
1320 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +00001321 * @return object
1322 */
Derek Allard41f60d42007-12-20 20:09:22 +00001323 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +00001324 {
1325 if ($table == '')
1326 {
Derek Allard73274992008-05-05 16:39:18 +00001327 if (! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001328 {
1329 if ($this->db_debug)
1330 {
1331 return $this->display_error('db_must_set_table');
1332 }
1333 return FALSE;
1334 }
Derek Allard39b622d2008-01-16 21:10:09 +00001335
Derek Allard09de1852007-02-14 01:35:56 +00001336 $table = $this->ar_from[0];
1337 }
Derek Allard39b622d2008-01-16 21:10:09 +00001338 elseif (is_array($table))
Derek Allard41f60d42007-12-20 20:09:22 +00001339 {
1340 foreach($table as $single_table)
1341 {
Derek Allard39b622d2008-01-16 21:10:09 +00001342 $this->delete($single_table, $where, $limit, FALSE);
Derek Allard41f60d42007-12-20 20:09:22 +00001343 }
Derek Allard39b622d2008-01-16 21:10:09 +00001344
Derek Allard41f60d42007-12-20 20:09:22 +00001345 $this->_reset_write();
1346 return;
1347 }
Derek Allard39b622d2008-01-16 21:10:09 +00001348 else
1349 {
1350 $table = $this->_protect_identifiers($this->dbprefix.$table);
1351 }
Derek Allard41f60d42007-12-20 20:09:22 +00001352
Derek Allard09de1852007-02-14 01:35:56 +00001353 if ($where != '')
1354 {
1355 $this->where($where);
1356 }
1357
Derek Allarde77d77c2007-12-19 15:01:55 +00001358 if ($limit != NULL)
1359 {
1360 $this->limit($limit);
1361 }
1362
Derek Allard39b622d2008-01-16 21:10:09 +00001363 if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
Derek Allard09de1852007-02-14 01:35:56 +00001364 {
1365 if ($this->db_debug)
1366 {
1367 return $this->display_error('db_del_must_use_where');
1368 }
Derek Allard39b622d2008-01-16 21:10:09 +00001369
Derek Allard09de1852007-02-14 01:35:56 +00001370 return FALSE;
1371 }
Derek Allard41f60d42007-12-20 20:09:22 +00001372
Derek Allard39b622d2008-01-16 21:10:09 +00001373 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001374
Derek Allard41f60d42007-12-20 20:09:22 +00001375 if ($reset_data)
1376 {
1377 $this->_reset_write();
1378 }
Derek Allard39b622d2008-01-16 21:10:09 +00001379
Derek Allard09de1852007-02-14 01:35:56 +00001380 return $this->query($sql);
1381 }
Derek Allard15ddc9d2007-12-20 13:54:39 +00001382
Derek Allard09de1852007-02-14 01:35:56 +00001383 // --------------------------------------------------------------------
1384
1385 /**
1386 * Use Table - DEPRECATED
1387 *
1388 * @deprecated use $this->db->from instead
1389 */
1390 function use_table($table)
1391 {
1392 return $this->from($table);
Derek Allard09de1852007-02-14 01:35:56 +00001393 }
1394
1395 // --------------------------------------------------------------------
1396
1397 /**
Derek Allard09de1852007-02-14 01:35:56 +00001398 * Tests whether the string has an SQL operator
1399 *
1400 * @access private
1401 * @param string
1402 * @return bool
1403 */
1404 function _has_operator($str)
1405 {
1406 $str = trim($str);
Derek Allard73274992008-05-05 16:39:18 +00001407 if (! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
Derek Allard09de1852007-02-14 01:35:56 +00001408 {
1409 return FALSE;
1410 }
1411
1412 return TRUE;
1413 }
1414
1415 // --------------------------------------------------------------------
1416
1417 /**
Derek Allard5e128942007-12-28 21:33:03 +00001418 * Track Aliases
1419 *
1420 * Used to track SQL statements written with aliased tables.
1421 *
1422 * @access private
1423 * @param string The table to inspect
1424 * @return string
1425 */
1426 function _track_aliases($table)
1427 {
1428 // if a table alias is used we can recognize it by a space
1429 if (strpos($table, " ") !== FALSE)
1430 {
1431 // if the alias is written with the AS keyowrd, get it out
Derek Allard39b622d2008-01-16 21:10:09 +00001432 $table = preg_replace('/ AS /i', ' ', $table);
Derek Allard5e128942007-12-28 21:33:03 +00001433
Derek Allard39b622d2008-01-16 21:10:09 +00001434 $this->ar_aliased_tables[] = trim(strrchr($table, " "));
Derek Allard5e128942007-12-28 21:33:03 +00001435 }
1436
1437 return $this->dbprefix.$table;
1438 }
1439
1440 // --------------------------------------------------------------------
1441
1442 /**
1443 * Filter Table Aliases
1444 *
1445 * Intelligently removes database prefixes from aliased tables
1446 *
1447 * @access private
1448 * @param array An array of compiled SQL
1449 * @return array Cleaned up statement with aliases accounted for
1450 */
1451 function _filter_table_aliases($statements)
1452 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001453
Derek Allard39b622d2008-01-16 21:10:09 +00001454 foreach ($statements as $k => $v)
Derek Allard5e128942007-12-28 21:33:03 +00001455 {
Derek Allard39b622d2008-01-16 21:10:09 +00001456 foreach ($this->ar_aliased_tables as $table)
Derek Allard5e128942007-12-28 21:33:03 +00001457 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001458 $statements[$k] = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $statements[$k]); // makes `table.field`
1459 $statements[$k] = str_replace($this->dbprefix.$table.'.', $table.'.', $statements[$k]);
Derek Allard5e128942007-12-28 21:33:03 +00001460 }
Derek Allard5e128942007-12-28 21:33:03 +00001461 }
Derek Allard39b622d2008-01-16 21:10:09 +00001462 return $statements;
Derek Allard5e128942007-12-28 21:33:03 +00001463 }
1464
1465 // --------------------------------------------------------------------
1466
1467 /**
Derek Allard09de1852007-02-14 01:35:56 +00001468 * Compile the SELECT statement
1469 *
1470 * Generates a query string based on which functions were used.
1471 * Should not be called directly. The get() function calls it.
1472 *
1473 * @access private
1474 * @return string
1475 */
Derek Allard694b5b82007-12-18 15:58:03 +00001476 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001477 {
Derek Allard78255262008-02-06 13:54:23 +00001478 $this->_merge_cache();
Derek Allard9b3e7b52008-02-04 23:20:34 +00001479
Derek Allard73274992008-05-05 16:39:18 +00001480 $sql = (! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Derek Allard09de1852007-02-14 01:35:56 +00001481
Derek Allard5162fc72008-04-15 20:05:37 +00001482 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->_filter_table_aliases($this->ar_select));
Derek Allard09de1852007-02-14 01:35:56 +00001483
Derek Allard694b5b82007-12-18 15:58:03 +00001484 if ($select_override !== FALSE)
1485 {
1486 $sql = $select_override;
1487 }
1488
Derek Allard09de1852007-02-14 01:35:56 +00001489 if (count($this->ar_from) > 0)
1490 {
1491 $sql .= "\nFROM ";
Derek Jonesc6ad0232008-01-29 18:44:54 +00001492 $sql .= $this->_from_tables($this->ar_from);
Derek Allard09de1852007-02-14 01:35:56 +00001493 }
1494
1495 if (count($this->ar_join) > 0)
Derek Allard5e128942007-12-28 21:33:03 +00001496 {
Derek Allard09de1852007-02-14 01:35:56 +00001497 $sql .= "\n";
Derek Allard5e128942007-12-28 21:33:03 +00001498
1499 // special consideration for table aliases
1500 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1501 {
1502 $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
1503 }
1504 else
1505 {
1506 $sql .= implode("\n", $this->ar_join);
1507 }
1508
Derek Allard09de1852007-02-14 01:35:56 +00001509 }
1510
1511 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1512 {
1513 $sql .= "\nWHERE ";
1514 }
1515
1516 $sql .= implode("\n", $this->ar_where);
1517
1518 if (count($this->ar_like) > 0)
1519 {
1520 if (count($this->ar_where) > 0)
1521 {
1522 $sql .= " AND ";
1523 }
1524
1525 $sql .= implode("\n", $this->ar_like);
1526 }
1527
1528 if (count($this->ar_groupby) > 0)
1529 {
Derek Allard5e128942007-12-28 21:33:03 +00001530
Derek Allard09de1852007-02-14 01:35:56 +00001531 $sql .= "\nGROUP BY ";
Derek Allard5e128942007-12-28 21:33:03 +00001532
1533 // special consideration for table aliases
1534 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1535 {
1536 $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
1537 }
1538 else
1539 {
1540 $sql .= implode(', ', $this->ar_groupby);
1541 }
Derek Allard09de1852007-02-14 01:35:56 +00001542 }
1543
1544 if (count($this->ar_having) > 0)
1545 {
1546 $sql .= "\nHAVING ";
1547 $sql .= implode("\n", $this->ar_having);
1548 }
1549
1550 if (count($this->ar_orderby) > 0)
1551 {
1552 $sql .= "\nORDER BY ";
1553 $sql .= implode(', ', $this->ar_orderby);
1554
1555 if ($this->ar_order !== FALSE)
1556 {
1557 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1558 }
1559 }
1560
1561 if (is_numeric($this->ar_limit))
1562 {
1563 $sql .= "\n";
1564 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1565 }
1566
1567 return $sql;
1568 }
1569
1570 // --------------------------------------------------------------------
1571
1572 /**
1573 * Object to Array
1574 *
1575 * Takes an object as input and converts the class variables to array key/vals
1576 *
1577 * @access public
1578 * @param object
1579 * @return array
1580 */
1581 function _object_to_array($object)
1582 {
Derek Allard73274992008-05-05 16:39:18 +00001583 if (! is_object($object))
Derek Allard09de1852007-02-14 01:35:56 +00001584 {
1585 return $object;
1586 }
1587
1588 $array = array();
1589 foreach (get_object_vars($object) as $key => $val)
1590 {
Derek Allard848b7762007-12-31 16:43:05 +00001591 // There are some built in keys we need to ignore for this conversion
Derek Allard73274992008-05-05 16:39:18 +00001592 if (! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
Derek Allard848b7762007-12-31 16:43:05 +00001593
Derek Allard09de1852007-02-14 01:35:56 +00001594 {
1595 $array[$key] = $val;
1596 }
1597 }
1598
1599 return $array;
1600 }
1601
1602 // --------------------------------------------------------------------
1603
1604 /**
Derek Allard9b3e7b52008-02-04 23:20:34 +00001605 * Start Cache
1606 *
1607 * Starts AR caching
1608 *
1609 * @access public
1610 * @return void
1611 */
1612 function start_cache()
1613 {
1614 $this->ar_caching = TRUE;
1615 }
1616
1617 // --------------------------------------------------------------------
1618
1619 /**
1620 * Stop Cache
1621 *
1622 * Stops AR caching
1623 *
1624 * @access public
1625 * @return void
1626 */
1627 function stop_cache()
1628 {
1629 $this->ar_caching = FALSE;
1630 }
1631
1632
1633 // --------------------------------------------------------------------
1634
1635 /**
1636 * Flush Cache
1637 *
1638 * Empties the AR cache
1639 *
1640 * @access public
1641 * @return void
1642 */
1643 function flush_cache()
1644 {
1645 $ar_reset_items = array(
1646 'ar_cache_select' => array(),
1647 'ar_cache_from' => array(),
1648 'ar_cache_join' => array(),
1649 'ar_cache_where' => array(),
1650 'ar_cache_like' => array(),
1651 'ar_cache_groupby' => array(),
1652 'ar_cache_having' =>array(),
1653 'ar_cache_orderby' => array(),
1654 'ar_cache_set' => array()
1655 );
1656
1657 $this->_reset_run($ar_reset_items);
1658 }
1659
1660 // --------------------------------------------------------------------
1661
1662 /**
1663 * Merge Cache
1664 *
1665 * When called, this function merges any cached AR arrays with
1666 * locally called ones.
1667 *
1668 * @access private
1669 * @return void
1670 */
1671 function _merge_cache()
1672 {
1673 $ar_items = array('select', 'from', 'join', 'where', 'like', 'groupby', 'having', 'orderby', 'set');
1674
1675 foreach ($ar_items as $ar_item)
1676 {
1677 $ar_cache_item = 'ar_cache_'.$ar_item;
1678 $ar_item = 'ar_'.$ar_item;
1679 $this->$ar_item = array_unique(array_merge($this->$ar_item, $this->$ar_cache_item));
1680 }
1681 }
1682
1683 // --------------------------------------------------------------------
1684
1685 /**
1686 * Resets the active record values. Called by the get() function
1687 *
1688 * @access private
1689 * @param array An array of fields to reset
1690 * @return void
1691 */
1692 function _reset_run($ar_reset_items)
1693 {
1694 foreach ($ar_reset_items as $item => $default_value)
1695 {
1696 if (!in_array($item, $this->ar_store_array))
1697 {
1698 $this->$item = $default_value;
1699 }
1700 }
1701 }
1702
1703 // --------------------------------------------------------------------
1704
1705 /**
Derek Allard09de1852007-02-14 01:35:56 +00001706 * Resets the active record values. Called by the get() function
1707 *
1708 * @access private
1709 * @return void
1710 */
1711 function _reset_select()
1712 {
Derek Allard9b3e7b52008-02-04 23:20:34 +00001713 $ar_reset_items = array(
1714 'ar_select' => array(),
1715 'ar_from' => array(),
1716 'ar_join' => array(),
1717 'ar_where' => array(),
1718 'ar_like' => array(),
1719 'ar_groupby' => array(),
1720 'ar_having' => array(),
1721 'ar_orderby' => array(),
1722 'ar_wherein' => array(),
1723 'ar_aliased_tables' => array(),
1724 'ar_distinct' => FALSE,
1725 'ar_limit' => FALSE,
1726 'ar_offset' => FALSE,
1727 'ar_order' => FALSE,
1728 );
1729
1730 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001731 }
1732
1733 // --------------------------------------------------------------------
1734
1735 /**
1736 * Resets the active record "write" values.
1737 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001738 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001739 *
1740 * @access private
1741 * @return void
1742 */
1743 function _reset_write()
Derek Allard9b3e7b52008-02-04 23:20:34 +00001744 {
1745 $ar_reset_items = array(
1746 'ar_set' => array(),
1747 'ar_from' => array(),
1748 'ar_where' => array(),
1749 'ar_like' => array(),
1750 'ar_orderby' => array(),
1751 'ar_limit' => FALSE,
1752 'ar_order' => FALSE
1753 );
1754
1755 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001756 }
1757
1758}
Derek Allarda6325892008-05-12 17:51:47 +00001759
1760/* End of file DB_active_rec.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00001761/* Location: ./system/database/DB_active_rec.php */