blob: 263172a862e71929d8121ec260b95b8dad18bdc3 [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 {
292 $this->ar_cache_from[] = $this->_protect_identifiers($val);
293 }
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
318 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
319 {
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 {
414 if ( ! is_array($key))
415 {
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 Allard39b622d2008-01-16 21:10:09 +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
429 if ( ! is_null($v))
430 {
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 Allard09de1852007-02-14 01:35:56 +0000445 if ( ! $this->_has_operator($k))
446 {
447 $k .= ' =';
448 }
Derek Allard39b622d2008-01-16 21:10:09 +0000449
Derek Allard09de1852007-02-14 01:35:56 +0000450 $v = ' '.$this->escape($v);
451 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000452 else
453 {
454 $k = $this->_protect_identifiers($k, TRUE);
455 }
456
Derek Allard09de1852007-02-14 01:35:56 +0000457 $this->ar_where[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000458 if ($this->ar_caching === TRUE)
459 {
460 $this->ar_cache_where[] = $prefix.$k.$v;
461 }
462
Derek Allard09de1852007-02-14 01:35:56 +0000463 }
464 return $this;
465 }
Derek Allard80dd7022007-12-18 23:55:06 +0000466
467 // --------------------------------------------------------------------
468
469 /**
470 * Where_in
471 *
Derek Allardc6935512007-12-19 14:23:19 +0000472 * Generates a WHERE field IN ('item', 'item') SQL query joined with
473 * AND if appropriate
Derek Allard80dd7022007-12-18 23:55:06 +0000474 *
475 * @access public
476 * @param string The field to search
477 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000478
479 * @return object
480 */
481 function where_in($key = NULL, $values = NULL)
482 {
483 return $this->_where_in($key, $values);
484 }
485
486 // --------------------------------------------------------------------
487
488 /**
489 * Where_in_or
490 *
491 * Generates a WHERE field IN ('item', 'item') SQL query joined with
492 * OR if appropriate
493 *
494 * @access public
495 * @param string The field to search
496 * @param array The values searched on
497
498 * @return object
499 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000500 function or_where_in($key = NULL, $values = NULL)
Derek Allardc6935512007-12-19 14:23:19 +0000501 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000502 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000503 }
504
505 // --------------------------------------------------------------------
506
507 /**
508 * Where_not_in
509 *
510 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
511 * with AND if appropriate
512 *
513 * @access public
514 * @param string The field to search
515 * @param array The values searched on
516
517 * @return object
518 */
519 function where_not_in($key = NULL, $values = NULL)
520 {
521 return $this->_where_in($key, $values, TRUE);
522 }
523
524 // --------------------------------------------------------------------
525
526 /**
527 * Where_not_in_or
528 *
529 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
530 * with OR if appropriate
531 *
532 * @access public
533 * @param string The field to search
534 * @param array The values searched on
535
536 * @return object
537 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000538 function or_where_not_in($key = NULL, $values = NULL)
Derek Allardc6935512007-12-19 14:23:19 +0000539 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000540 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000541 }
542
543 // --------------------------------------------------------------------
544
545 /**
546 * Where_in
547 *
548 * Called by where_in, where_in_or, where_not_in, where_not_in_or
549 *
550 * @access public
551 * @param string The field to search
552 * @param array The values searched on
553 * @param boolean If the statement whould be IN or NOT IN
Derek Allard80dd7022007-12-18 23:55:06 +0000554 * @param string
555 * @return object
556 */
Derek Allard15ddc9d2007-12-20 13:54:39 +0000557 function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard80dd7022007-12-18 23:55:06 +0000558 {
559 if ($key === NULL || !is_array($values))
560 {
561 return;
562 }
563
Derek Allardc6935512007-12-19 14:23:19 +0000564 $not = ($not) ? ' NOT ' : '';
Derek Allard80dd7022007-12-18 23:55:06 +0000565
566 foreach ($values as $value)
567 {
568 $this->ar_wherein[] = $this->escape($value);
569 }
570
571 $prefix = (count($this->ar_where) == 0) ? '' : $type;
572
Derek Allard9b3e7b52008-02-04 23:20:34 +0000573 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
574
575 $this->ar_where[] = $where_in;
576 if ($this->ar_caching === TRUE)
577 {
578 $this->ar_cache_where[] = $where_in;
579 }
Derek Allard80dd7022007-12-18 23:55:06 +0000580
Derek Allard8f000212008-01-18 14:45:59 +0000581 // reset the array for multiple calls
582 $this->ar_wherein = array();
Derek Allard80dd7022007-12-18 23:55:06 +0000583 return $this;
584 }
585
Derek Allard09de1852007-02-14 01:35:56 +0000586 // --------------------------------------------------------------------
587
588 /**
589 * Like
590 *
591 * Generates a %LIKE% portion of the query. Separates
592 * multiple calls with AND
593 *
594 * @access public
595 * @param mixed
596 * @param mixed
597 * @return object
598 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000599 function like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000600 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000601 return $this->_like($field, $match, 'AND ', $side);
Derek Allard09de1852007-02-14 01:35:56 +0000602 }
Derek Allarde54e3d22007-12-19 15:53:44 +0000603
604 // --------------------------------------------------------------------
605
606 /**
607 * Not Like
608 *
609 * Generates a NOT LIKE portion of the query. Separates
610 * multiple calls with AND
611 *
612 * @access public
613 * @param mixed
614 * @param mixed
615 * @return object
616 */
617 function not_like($field, $match = '', $side = 'both')
618 {
619 return $this->_like($field, $match, 'AND ', $side, ' NOT');
620 }
621
Derek Allard09de1852007-02-14 01:35:56 +0000622 // --------------------------------------------------------------------
623
624 /**
625 * OR Like
626 *
627 * Generates a %LIKE% portion of the query. Separates
628 * multiple calls with OR
629 *
630 * @access public
631 * @param mixed
632 * @param mixed
633 * @return object
634 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000635 function or_like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000636 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000637 return $this->_like($field, $match, 'OR ', $side);
638 }
639
640 // --------------------------------------------------------------------
641
642 /**
Derek Allarde54e3d22007-12-19 15:53:44 +0000643 * OR Not Like
644 *
645 * Generates a NOT LIKE portion of the query. Separates
646 * multiple calls with OR
647 *
648 * @access public
649 * @param mixed
650 * @param mixed
651 * @return object
652 */
653 function or_not_like($field, $match = '', $side = 'both')
654 {
655 return $this->_like($field, $match, 'OR ', $side, 'NOT ');
656 }
657
658 // --------------------------------------------------------------------
659
660 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000661 * orlike() is an alias of or_like()
662 * this function is here for backwards compatibility, as
663 * orlike() has been deprecated
664 */
665 function orlike($field, $match = '', $side = 'both')
666 {
Derek Allard4a310b72008-01-30 21:32:47 +0000667 return $this->or_like($field, $match, $side);
Derek Allard09de1852007-02-14 01:35:56 +0000668 }
669
670 // --------------------------------------------------------------------
671
672 /**
673 * Like
674 *
675 * Called by like() or orlike()
676 *
677 * @access private
678 * @param mixed
679 * @param mixed
680 * @param string
681 * @return object
682 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000683 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard09de1852007-02-14 01:35:56 +0000684 {
685 if ( ! is_array($field))
686 {
687 $field = array($field => $match);
688 }
689
690 foreach ($field as $k => $v)
Derek Allarde54e3d22007-12-19 15:53:44 +0000691 {
692
Derek Allard39b622d2008-01-16 21:10:09 +0000693 $k = $this->_protect_identifiers($k);
694
Derek Allard09de1852007-02-14 01:35:56 +0000695 $prefix = (count($this->ar_like) == 0) ? '' : $type;
Derek Allarde54e3d22007-12-19 15:53:44 +0000696
Derek Allard09de1852007-02-14 01:35:56 +0000697 $v = $this->escape_str($v);
Derek Allarde54e3d22007-12-19 15:53:44 +0000698
Derek Allard218e2bc2007-12-17 21:18:14 +0000699 if ($side == 'before')
700 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000701 $like_statement = $prefix." $k $not LIKE '%{$v}'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000702 }
703 elseif ($side == 'after')
704 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000705 $like_statement = $prefix." $k $not LIKE '{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000706 }
707 else
708 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000709 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000710 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000711
712 $this->ar_like[] = $like_statement;
713 if ($this->ar_caching === TRUE)
714 {
715 $this->ar_cache_like[] = $like_statement;
716 }
717
Derek Allard09de1852007-02-14 01:35:56 +0000718 }
719 return $this;
720 }
721
722 // --------------------------------------------------------------------
723
724 /**
725 * GROUP BY
726 *
727 * @access public
728 * @param string
729 * @return object
730 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000731 function group_by($by)
Derek Allard09de1852007-02-14 01:35:56 +0000732 {
733 if (is_string($by))
734 {
735 $by = explode(',', $by);
736 }
737
738 foreach ($by as $val)
739 {
740 $val = trim($val);
741
742 if ($val != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000743 {
Derek Allard39b622d2008-01-16 21:10:09 +0000744 $this->ar_groupby[] = $this->_protect_identifiers($val);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000745 if ($this->ar_caching === TRUE)
746 {
747 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
748 }
749 }
Derek Allard09de1852007-02-14 01:35:56 +0000750 }
751 return $this;
752 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000753
754 // --------------------------------------------------------------------
755
756 /**
757 * groupby() is an alias of group_by()
758 * this function is here for backwards compatibility, as
759 * groupby() has been deprecated
760 */
761 function groupby($by)
762 {
763 return $this->group_by($by);
764 }
765
Derek Allard09de1852007-02-14 01:35:56 +0000766 // --------------------------------------------------------------------
767
768 /**
769 * Sets the HAVING value
770 *
771 * Separates multiple calls with AND
772 *
773 * @access public
774 * @param string
775 * @param string
776 * @return object
777 */
778 function having($key, $value = '')
779 {
780 return $this->_having($key, $value, 'AND ');
781 }
782
783 // --------------------------------------------------------------------
784
785 /**
786 * Sets the OR HAVING value
787 *
788 * Separates multiple calls with OR
789 *
790 * @access public
791 * @param string
792 * @param string
793 * @return object
794 */
795 function orhaving($key, $value = '')
796 {
797 return $this->_having($key, $value, 'OR ');
798 }
799
800 // --------------------------------------------------------------------
801
802 /**
803 * Sets the HAVING values
804 *
805 * Called by having() or orhaving()
806 *
807 * @access private
808 * @param string
Derek Allard9b3e7b52008-02-04 23:20:34 +0000809
Derek Allard09de1852007-02-14 01:35:56 +0000810 * @param string
811 * @return object
812 */
813 function _having($key, $value = '', $type = 'AND ')
814 {
815 if ( ! is_array($key))
816 {
817 $key = array($key => $value);
818 }
819
820 foreach ($key as $k => $v)
821 {
822 $prefix = (count($this->ar_having) == 0) ? '' : $type;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000823 $k = $this->_protect_identifiers($k);
Derek Allard09de1852007-02-14 01:35:56 +0000824
825 if ($v != '')
826 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000827 $v = ' '.$this->escape_str($v);
Derek Allard09de1852007-02-14 01:35:56 +0000828 }
829
830 $this->ar_having[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000831 if ($this->ar_caching === TRUE)
832 {
833 $this->ar_cache_having[] = $prefix.$k.$v;
834 }
Derek Allard09de1852007-02-14 01:35:56 +0000835 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000836
Derek Allard09de1852007-02-14 01:35:56 +0000837 return $this;
838 }
839
840 // --------------------------------------------------------------------
841
842 /**
843 * Sets the ORDER BY value
844 *
845 * @access public
846 * @param string
847 * @param string direction: asc or desc
848 * @return object
849 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000850 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000851 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000852 if (strtolower($direction) == 'random')
853 {
854 $orderby = ''; // Random results want or don't need a field name
855 $direction = $this->_random_keyword;
856 }
857 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000858 {
Derek Allard92782492007-08-10 11:26:01 +0000859 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000860 }
861
Derek Allard9b3e7b52008-02-04 23:20:34 +0000862 $orderby_statement = $this->_protect_identifiers($orderby, TRUE).$direction;
863
864 $this->ar_orderby[] = $orderby_statement;
865 if ($this->ar_caching === TRUE)
866 {
867 $this->ar_cache_orderby[] = $orderby_statement;
868 }
869
Derek Allard09de1852007-02-14 01:35:56 +0000870 return $this;
871 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000872
Derek Allard218e2bc2007-12-17 21:18:14 +0000873 // --------------------------------------------------------------------
874
875 /**
876 * orderby() is an alias of order_by()
877 * this function is here for backwards compatibility, as
878 * orderby() has been deprecated
879 */
880 function orderby($orderby, $direction = '')
881 {
882 return $this->order_by($orderby, $direction);
883 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000884
Derek Allard09de1852007-02-14 01:35:56 +0000885 // --------------------------------------------------------------------
886
887 /**
888 * Sets the LIMIT value
889 *
890 * @access public
891 * @param integer the limit value
892 * @param integer the offset value
893 * @return object
894 */
895 function limit($value, $offset = '')
896 {
897 $this->ar_limit = $value;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000898 if ($this->ar_caching === TRUE)
899 {
900 $this->ar_cache_limit[] = $value;
901 }
902
Derek Allard09de1852007-02-14 01:35:56 +0000903 if ($offset != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000904 {
Derek Allard09de1852007-02-14 01:35:56 +0000905 $this->ar_offset = $offset;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000906 if ($this->ar_caching === TRUE)
907 {
908 $this->ar_cache_offset[] = $offset;
909 }
910 }
Derek Allard09de1852007-02-14 01:35:56 +0000911
912 return $this;
913 }
914
915 // --------------------------------------------------------------------
916
917 /**
918 * Sets the OFFSET value
919 *
920 * @access public
921 * @param integer the offset value
922 * @return object
923 */
Derek Allard9b3e7b52008-02-04 23:20:34 +0000924 function offset($offset)
Derek Allard09de1852007-02-14 01:35:56 +0000925 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000926 $this->ar_offset = $offset;
927 if ($this->ar_caching === TRUE)
928 {
929 $this->ar_cache_offset[] = $offset;
930 }
931
Derek Allard09de1852007-02-14 01:35:56 +0000932 return $this;
933 }
934
935 // --------------------------------------------------------------------
936
937 /**
938 * The "set" function. Allows key/value pairs to be set for inserting or updating
939 *
940 * @access public
941 * @param mixed
942 * @param string
Derek Allard39b622d2008-01-16 21:10:09 +0000943 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +0000944 * @return object
945 */
Derek Allard39b622d2008-01-16 21:10:09 +0000946 function set($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000947 {
948 $key = $this->_object_to_array($key);
949
950 if ( ! is_array($key))
951 {
952 $key = array($key => $value);
953 }
954
955 foreach ($key as $k => $v)
956 {
Derek Allard39b622d2008-01-16 21:10:09 +0000957 if ($escape === FALSE)
958 {
959 $this->ar_set[$this->_protect_identifiers($k)] = $v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000960 if ($this->ar_caching === TRUE)
961 {
962 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $v;
963 }
Derek Allard39b622d2008-01-16 21:10:09 +0000964 }
965 else
966 {
967 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000968 if ($this->ar_caching === TRUE)
969 {
970 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $this->escape($v);
971 }
Derek Allard39b622d2008-01-16 21:10:09 +0000972 }
Derek Allard09de1852007-02-14 01:35:56 +0000973 }
974
975 return $this;
976 }
977
978 // --------------------------------------------------------------------
979
980 /**
981 * Get
982 *
983 * Compiles the select statement based on the other functions called
984 * and runs the query
985 *
986 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +0000987 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +0000988 * @param string the limit clause
989 * @param string the offset clause
990 * @return object
991 */
992 function get($table = '', $limit = null, $offset = null)
993 {
994 if ($table != '')
995 {
Derek Allard5e128942007-12-28 21:33:03 +0000996 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +0000997 $this->from($table);
998 }
999
1000 if ( ! is_null($limit))
1001 {
1002 $this->limit($limit, $offset);
1003 }
1004
1005 $sql = $this->_compile_select();
1006
1007 $result = $this->query($sql);
1008 $this->_reset_select();
1009 return $result;
1010 }
1011
Derek Allard09de1852007-02-14 01:35:56 +00001012 /**
Derek Allard694b5b82007-12-18 15:58:03 +00001013 * "Count All Results" query
1014 *
1015 * Generates a platform-specific query string that counts all records
1016 * returned by an Active Record query.
1017 *
1018 * @access public
1019 * @param string
1020 * @return string
1021 */
1022 function count_all_results($table = '')
1023 {
1024 if ($table != '')
1025 {
Derek Allard5e128942007-12-28 21:33:03 +00001026 $this->_track_aliases($table);
Derek Allard694b5b82007-12-18 15:58:03 +00001027 $this->from($table);
1028 }
1029
Derek Allard39b622d2008-01-16 21:10:09 +00001030 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
Derek Allard694b5b82007-12-18 15:58:03 +00001031
1032 $query = $this->query($sql);
1033 $this->_reset_select();
1034
1035 if ($query->num_rows() == 0)
1036 {
1037 return '0';
1038 }
1039
1040 $row = $query->row();
1041 return $row->numrows;
1042 }
1043
1044 // --------------------------------------------------------------------
1045
1046 /**
Derek Allard218e2bc2007-12-17 21:18:14 +00001047 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +00001048 *
1049 * Allows the where clause, limit and offset to be added directly
1050 *
1051 * @access public
1052 * @param string the where clause
1053 * @param string the limit clause
1054 * @param string the offset clause
1055 * @return object
1056 */
Derek Allard218e2bc2007-12-17 21:18:14 +00001057 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +00001058 {
1059 if ($table != '')
1060 {
Derek Allard5e128942007-12-28 21:33:03 +00001061 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001062 $this->from($table);
1063 }
1064
1065 if ( ! is_null($where))
1066 {
1067 $this->where($where);
1068 }
1069
1070 if ( ! is_null($limit))
1071 {
1072 $this->limit($limit, $offset);
1073 }
1074
1075 $sql = $this->_compile_select();
1076
1077 $result = $this->query($sql);
1078 $this->_reset_select();
1079 return $result;
1080 }
Derek Allard218e2bc2007-12-17 21:18:14 +00001081
1082 // --------------------------------------------------------------------
1083
1084 /**
1085 * getwhere() is an alias of get_where()
1086 * this function is here for backwards compatibility, as
1087 * getwhere() has been deprecated
1088 */
1089 function getwhere($table = '', $where = null, $limit = null, $offset = null)
1090 {
1091 return $this->get_where($table, $where, $limit, $offset);
1092 }
Derek Allard09de1852007-02-14 01:35:56 +00001093
1094 // --------------------------------------------------------------------
1095
1096 /**
1097 * Insert
1098 *
1099 * Compiles an insert string and runs the query
1100 *
1101 * @access public
1102 * @param string the table to retrieve the results from
1103 * @param array an associative array of insert values
1104 * @return object
1105 */
1106 function insert($table = '', $set = NULL)
1107 {
1108 if ( ! is_null($set))
1109 {
1110 $this->set($set);
1111 }
1112
1113 if (count($this->ar_set) == 0)
1114 {
1115 if ($this->db_debug)
1116 {
1117 return $this->display_error('db_must_use_set');
1118 }
1119 return FALSE;
1120 }
1121
1122 if ($table == '')
1123 {
1124 if ( ! isset($this->ar_from[0]))
1125 {
1126 if ($this->db_debug)
1127 {
1128 return $this->display_error('db_must_set_table');
1129 }
1130 return FALSE;
1131 }
1132
1133 $table = $this->ar_from[0];
1134 }
Derek Allard39b622d2008-01-16 21:10:09 +00001135
1136 $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 +00001137
1138 $this->_reset_write();
1139 return $this->query($sql);
1140 }
1141
1142 // --------------------------------------------------------------------
1143
1144 /**
1145 * Update
1146 *
1147 * Compiles an update string and runs the query
1148 *
1149 * @access public
1150 * @param string the table to retrieve the results from
1151 * @param array an associative array of update values
1152 * @param mixed the where clause
1153 * @return object
1154 */
Derek Allard5e128942007-12-28 21:33:03 +00001155 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001156 {
1157 if ( ! is_null($set))
1158 {
1159 $this->set($set);
1160 }
1161
1162 if (count($this->ar_set) == 0)
1163 {
1164 if ($this->db_debug)
1165 {
1166 return $this->display_error('db_must_use_set');
1167 }
1168 return FALSE;
1169 }
1170
1171 if ($table == '')
1172 {
1173 if ( ! isset($this->ar_from[0]))
1174 {
1175 if ($this->db_debug)
1176 {
1177 return $this->display_error('db_must_set_table');
1178 }
1179 return FALSE;
1180 }
1181
1182 $table = $this->ar_from[0];
1183 }
1184
Derek Allarde77d77c2007-12-19 15:01:55 +00001185 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001186 {
1187 $this->where($where);
1188 }
Derek Allardda6d2402007-12-19 14:49:29 +00001189
Derek Allarde77d77c2007-12-19 15:01:55 +00001190 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +00001191 {
1192 $this->limit($limit);
1193 }
Derek Allard09de1852007-02-14 01:35:56 +00001194
Derek Allard39b622d2008-01-16 21:10:09 +00001195 $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 +00001196
1197 $this->_reset_write();
1198 return $this->query($sql);
1199 }
Derek Allard39b622d2008-01-16 21:10:09 +00001200
1201 // --------------------------------------------------------------------
1202
1203 /**
1204 * Empty Table
1205 *
1206 * Compiles a delete string and runs "DELETE FROM table"
1207 *
1208 * @access public
1209 * @param string the table to empty
1210 * @return object
1211 */
1212 function empty_table($table = '')
1213 {
1214 if ($table == '')
1215 {
1216 if ( ! isset($this->ar_from[0]))
1217 {
1218 if ($this->db_debug)
1219 {
1220 return $this->display_error('db_must_set_table');
1221 }
1222 return FALSE;
1223 }
1224
1225 $table = $this->ar_from[0];
1226 }
1227 else
1228 {
1229 $table = $this->_protect_identifiers($this->dbprefix.$table);
1230 }
1231
1232
1233 $sql = $this->_delete($table);
1234
1235 $this->_reset_write();
1236
1237 return $this->query($sql);
1238 }
1239
1240 // --------------------------------------------------------------------
1241
1242 /**
1243 * Truncate
1244 *
1245 * Compiles a truncate string and runs the query
1246 * If the database does not support the truncate() command
1247 * This function maps to "DELETE FROM table"
1248 *
1249 * @access public
1250 * @param string the table to truncate
1251 * @return object
1252 */
1253 function truncate($table = '')
1254 {
1255 if ($table == '')
1256 {
1257 if ( ! isset($this->ar_from[0]))
1258 {
1259 if ($this->db_debug)
1260 {
1261 return $this->display_error('db_must_set_table');
1262 }
1263 return FALSE;
1264 }
1265
1266 $table = $this->ar_from[0];
1267 }
1268 else
1269 {
1270 $table = $this->_protect_identifiers($this->dbprefix.$table);
1271 }
1272
1273
1274 $sql = $this->_truncate($table);
1275
1276 $this->_reset_write();
1277
1278 return $this->query($sql);
1279 }
Derek Allard09de1852007-02-14 01:35:56 +00001280
1281 // --------------------------------------------------------------------
1282
1283 /**
1284 * Delete
1285 *
1286 * Compiles a delete string and runs the query
1287 *
1288 * @access public
Derek Allard41f60d42007-12-20 20:09:22 +00001289 * @param mixed the table(s) to delete from. String or array
Derek Allard09de1852007-02-14 01:35:56 +00001290 * @param mixed the where clause
Derek Allard41f60d42007-12-20 20:09:22 +00001291 * @param mixed the limit clause
1292 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +00001293 * @return object
1294 */
Derek Allard41f60d42007-12-20 20:09:22 +00001295 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +00001296 {
1297 if ($table == '')
1298 {
1299 if ( ! isset($this->ar_from[0]))
1300 {
1301 if ($this->db_debug)
1302 {
1303 return $this->display_error('db_must_set_table');
1304 }
1305 return FALSE;
1306 }
Derek Allard39b622d2008-01-16 21:10:09 +00001307
Derek Allard09de1852007-02-14 01:35:56 +00001308 $table = $this->ar_from[0];
1309 }
Derek Allard39b622d2008-01-16 21:10:09 +00001310 elseif (is_array($table))
Derek Allard41f60d42007-12-20 20:09:22 +00001311 {
1312 foreach($table as $single_table)
1313 {
Derek Allard39b622d2008-01-16 21:10:09 +00001314 $this->delete($single_table, $where, $limit, FALSE);
Derek Allard41f60d42007-12-20 20:09:22 +00001315 }
Derek Allard39b622d2008-01-16 21:10:09 +00001316
Derek Allard41f60d42007-12-20 20:09:22 +00001317 $this->_reset_write();
1318 return;
1319 }
Derek Allard39b622d2008-01-16 21:10:09 +00001320 else
1321 {
1322 $table = $this->_protect_identifiers($this->dbprefix.$table);
1323 }
Derek Allard41f60d42007-12-20 20:09:22 +00001324
Derek Allard09de1852007-02-14 01:35:56 +00001325 if ($where != '')
1326 {
1327 $this->where($where);
1328 }
1329
Derek Allarde77d77c2007-12-19 15:01:55 +00001330 if ($limit != NULL)
1331 {
1332 $this->limit($limit);
1333 }
1334
Derek Allard39b622d2008-01-16 21:10:09 +00001335 if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
Derek Allard09de1852007-02-14 01:35:56 +00001336 {
1337 if ($this->db_debug)
1338 {
1339 return $this->display_error('db_del_must_use_where');
1340 }
Derek Allard39b622d2008-01-16 21:10:09 +00001341
Derek Allard09de1852007-02-14 01:35:56 +00001342 return FALSE;
1343 }
Derek Allard41f60d42007-12-20 20:09:22 +00001344
Derek Allard39b622d2008-01-16 21:10:09 +00001345 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001346
Derek Allard41f60d42007-12-20 20:09:22 +00001347 if ($reset_data)
1348 {
1349 $this->_reset_write();
1350 }
Derek Allard39b622d2008-01-16 21:10:09 +00001351
Derek Allard09de1852007-02-14 01:35:56 +00001352 return $this->query($sql);
1353 }
Derek Allard15ddc9d2007-12-20 13:54:39 +00001354
Derek Allard09de1852007-02-14 01:35:56 +00001355 // --------------------------------------------------------------------
1356
1357 /**
1358 * Use Table - DEPRECATED
1359 *
1360 * @deprecated use $this->db->from instead
1361 */
1362 function use_table($table)
1363 {
1364 return $this->from($table);
Derek Allard09de1852007-02-14 01:35:56 +00001365 }
1366
1367 // --------------------------------------------------------------------
1368
1369 /**
Derek Allard09de1852007-02-14 01:35:56 +00001370 * Tests whether the string has an SQL operator
1371 *
1372 * @access private
1373 * @param string
1374 * @return bool
1375 */
1376 function _has_operator($str)
1377 {
1378 $str = trim($str);
1379 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
1380 {
1381 return FALSE;
1382 }
1383
1384 return TRUE;
1385 }
1386
1387 // --------------------------------------------------------------------
1388
1389 /**
Derek Allard5e128942007-12-28 21:33:03 +00001390 * Track Aliases
1391 *
1392 * Used to track SQL statements written with aliased tables.
1393 *
1394 * @access private
1395 * @param string The table to inspect
1396 * @return string
1397 */
1398 function _track_aliases($table)
1399 {
1400 // if a table alias is used we can recognize it by a space
1401 if (strpos($table, " ") !== FALSE)
1402 {
1403 // if the alias is written with the AS keyowrd, get it out
Derek Allard39b622d2008-01-16 21:10:09 +00001404 $table = preg_replace('/ AS /i', ' ', $table);
Derek Allard5e128942007-12-28 21:33:03 +00001405
Derek Allard39b622d2008-01-16 21:10:09 +00001406 $this->ar_aliased_tables[] = trim(strrchr($table, " "));
Derek Allard5e128942007-12-28 21:33:03 +00001407 }
1408
1409 return $this->dbprefix.$table;
1410 }
1411
1412 // --------------------------------------------------------------------
1413
1414 /**
1415 * Filter Table Aliases
1416 *
1417 * Intelligently removes database prefixes from aliased tables
1418 *
1419 * @access private
1420 * @param array An array of compiled SQL
1421 * @return array Cleaned up statement with aliases accounted for
1422 */
1423 function _filter_table_aliases($statements)
1424 {
Derek Allard39b622d2008-01-16 21:10:09 +00001425 foreach ($statements as $k => $v)
Derek Allard5e128942007-12-28 21:33:03 +00001426 {
Derek Allard39b622d2008-01-16 21:10:09 +00001427 foreach ($this->ar_aliased_tables as $table)
Derek Allard5e128942007-12-28 21:33:03 +00001428 {
Derek Allard39b622d2008-01-16 21:10:09 +00001429 $statement = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $v); // makes `table.field`
1430 $statement = str_replace(array($this->dbprefix.$table, '.'), array($table, $this->_protect_identifiers('.')), $statement);
Derek Allard5e128942007-12-28 21:33:03 +00001431 }
1432
Derek Allard39b622d2008-01-16 21:10:09 +00001433 $statements[$k] = $statement;
Derek Allard5e128942007-12-28 21:33:03 +00001434 }
1435
Derek Allard39b622d2008-01-16 21:10:09 +00001436 return $statements;
Derek Allard5e128942007-12-28 21:33:03 +00001437 }
1438
1439 // --------------------------------------------------------------------
1440
1441 /**
Derek Allard09de1852007-02-14 01:35:56 +00001442 * Compile the SELECT statement
1443 *
1444 * Generates a query string based on which functions were used.
1445 * Should not be called directly. The get() function calls it.
1446 *
1447 * @access private
1448 * @return string
1449 */
Derek Allard694b5b82007-12-18 15:58:03 +00001450 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001451 {
Derek Allard78255262008-02-06 13:54:23 +00001452 $this->_merge_cache();
Derek Allard9b3e7b52008-02-04 23:20:34 +00001453
Derek Allard09de1852007-02-14 01:35:56 +00001454 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
1455
1456 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->ar_select);
1457
Derek Allard694b5b82007-12-18 15:58:03 +00001458 if ($select_override !== FALSE)
1459 {
1460 $sql = $select_override;
1461 }
1462
Derek Allard09de1852007-02-14 01:35:56 +00001463 if (count($this->ar_from) > 0)
1464 {
1465 $sql .= "\nFROM ";
Derek Jonesc6ad0232008-01-29 18:44:54 +00001466 $sql .= $this->_from_tables($this->ar_from);
Derek Allard09de1852007-02-14 01:35:56 +00001467 }
1468
1469 if (count($this->ar_join) > 0)
Derek Allard5e128942007-12-28 21:33:03 +00001470 {
Derek Allard09de1852007-02-14 01:35:56 +00001471 $sql .= "\n";
Derek Allard5e128942007-12-28 21:33:03 +00001472
1473 // special consideration for table aliases
1474 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1475 {
1476 $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
1477 }
1478 else
1479 {
1480 $sql .= implode("\n", $this->ar_join);
1481 }
1482
Derek Allard09de1852007-02-14 01:35:56 +00001483 }
1484
1485 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1486 {
1487 $sql .= "\nWHERE ";
1488 }
1489
1490 $sql .= implode("\n", $this->ar_where);
1491
1492 if (count($this->ar_like) > 0)
1493 {
1494 if (count($this->ar_where) > 0)
1495 {
1496 $sql .= " AND ";
1497 }
1498
1499 $sql .= implode("\n", $this->ar_like);
1500 }
1501
1502 if (count($this->ar_groupby) > 0)
1503 {
Derek Allard5e128942007-12-28 21:33:03 +00001504
Derek Allard09de1852007-02-14 01:35:56 +00001505 $sql .= "\nGROUP BY ";
Derek Allard5e128942007-12-28 21:33:03 +00001506
1507 // special consideration for table aliases
1508 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1509 {
1510 $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
1511 }
1512 else
1513 {
1514 $sql .= implode(', ', $this->ar_groupby);
1515 }
Derek Allard09de1852007-02-14 01:35:56 +00001516 }
1517
1518 if (count($this->ar_having) > 0)
1519 {
1520 $sql .= "\nHAVING ";
1521 $sql .= implode("\n", $this->ar_having);
1522 }
1523
1524 if (count($this->ar_orderby) > 0)
1525 {
1526 $sql .= "\nORDER BY ";
1527 $sql .= implode(', ', $this->ar_orderby);
1528
1529 if ($this->ar_order !== FALSE)
1530 {
1531 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1532 }
1533 }
1534
1535 if (is_numeric($this->ar_limit))
1536 {
1537 $sql .= "\n";
1538 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1539 }
1540
1541 return $sql;
1542 }
1543
1544 // --------------------------------------------------------------------
1545
1546 /**
1547 * Object to Array
1548 *
1549 * Takes an object as input and converts the class variables to array key/vals
1550 *
1551 * @access public
1552 * @param object
1553 * @return array
1554 */
1555 function _object_to_array($object)
1556 {
1557 if ( ! is_object($object))
1558 {
1559 return $object;
1560 }
1561
1562 $array = array();
1563 foreach (get_object_vars($object) as $key => $val)
1564 {
Derek Allard848b7762007-12-31 16:43:05 +00001565 // There are some built in keys we need to ignore for this conversion
1566 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
1567
Derek Allard09de1852007-02-14 01:35:56 +00001568 {
1569 $array[$key] = $val;
1570 }
1571 }
1572
1573 return $array;
1574 }
1575
1576 // --------------------------------------------------------------------
1577
1578 /**
Derek Allard9b3e7b52008-02-04 23:20:34 +00001579 * Start Cache
1580 *
1581 * Starts AR caching
1582 *
1583 * @access public
1584 * @return void
1585 */
1586 function start_cache()
1587 {
1588 $this->ar_caching = TRUE;
1589 }
1590
1591 // --------------------------------------------------------------------
1592
1593 /**
1594 * Stop Cache
1595 *
1596 * Stops AR caching
1597 *
1598 * @access public
1599 * @return void
1600 */
1601 function stop_cache()
1602 {
1603 $this->ar_caching = FALSE;
1604 }
1605
1606
1607 // --------------------------------------------------------------------
1608
1609 /**
1610 * Flush Cache
1611 *
1612 * Empties the AR cache
1613 *
1614 * @access public
1615 * @return void
1616 */
1617 function flush_cache()
1618 {
1619 $ar_reset_items = array(
1620 'ar_cache_select' => array(),
1621 'ar_cache_from' => array(),
1622 'ar_cache_join' => array(),
1623 'ar_cache_where' => array(),
1624 'ar_cache_like' => array(),
1625 'ar_cache_groupby' => array(),
1626 'ar_cache_having' =>array(),
1627 'ar_cache_orderby' => array(),
1628 'ar_cache_set' => array()
1629 );
1630
1631 $this->_reset_run($ar_reset_items);
1632 }
1633
1634 // --------------------------------------------------------------------
1635
1636 /**
1637 * Merge Cache
1638 *
1639 * When called, this function merges any cached AR arrays with
1640 * locally called ones.
1641 *
1642 * @access private
1643 * @return void
1644 */
1645 function _merge_cache()
1646 {
1647 $ar_items = array('select', 'from', 'join', 'where', 'like', 'groupby', 'having', 'orderby', 'set');
1648
1649 foreach ($ar_items as $ar_item)
1650 {
1651 $ar_cache_item = 'ar_cache_'.$ar_item;
1652 $ar_item = 'ar_'.$ar_item;
1653 $this->$ar_item = array_unique(array_merge($this->$ar_item, $this->$ar_cache_item));
1654 }
1655 }
1656
1657 // --------------------------------------------------------------------
1658
1659 /**
1660 * Resets the active record values. Called by the get() function
1661 *
1662 * @access private
1663 * @param array An array of fields to reset
1664 * @return void
1665 */
1666 function _reset_run($ar_reset_items)
1667 {
1668 foreach ($ar_reset_items as $item => $default_value)
1669 {
1670 if (!in_array($item, $this->ar_store_array))
1671 {
1672 $this->$item = $default_value;
1673 }
1674 }
1675 }
1676
1677 // --------------------------------------------------------------------
1678
1679 /**
Derek Allard09de1852007-02-14 01:35:56 +00001680 * Resets the active record values. Called by the get() function
1681 *
1682 * @access private
1683 * @return void
1684 */
1685 function _reset_select()
1686 {
Derek Allard9b3e7b52008-02-04 23:20:34 +00001687 $ar_reset_items = array(
1688 'ar_select' => array(),
1689 'ar_from' => array(),
1690 'ar_join' => array(),
1691 'ar_where' => array(),
1692 'ar_like' => array(),
1693 'ar_groupby' => array(),
1694 'ar_having' => array(),
1695 'ar_orderby' => array(),
1696 'ar_wherein' => array(),
1697 'ar_aliased_tables' => array(),
1698 'ar_distinct' => FALSE,
1699 'ar_limit' => FALSE,
1700 'ar_offset' => FALSE,
1701 'ar_order' => FALSE,
1702 );
1703
1704 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001705 }
1706
1707 // --------------------------------------------------------------------
1708
1709 /**
1710 * Resets the active record "write" values.
1711 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001712 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001713 *
1714 * @access private
1715 * @return void
1716 */
1717 function _reset_write()
Derek Allard9b3e7b52008-02-04 23:20:34 +00001718 {
1719 $ar_reset_items = array(
1720 'ar_set' => array(),
1721 'ar_from' => array(),
1722 'ar_where' => array(),
1723 'ar_like' => array(),
1724 'ar_orderby' => array(),
1725 'ar_limit' => FALSE,
1726 'ar_order' => FALSE
1727 );
1728
1729 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001730 }
1731
1732}
adminac94f382006-09-24 20:28:12 +00001733?>