blob: 23a5a7adffd0bbb014c39cc79b0c90844367f28a [file] [log] [blame]
Derek Allard09de1852007-02-14 01:35:56 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
Derek Allardd2df9bc2007-04-15 17:41:17 +00003 * CodeIgniter
Derek Allard09de1852007-02-14 01:35:56 +00004 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Allardcdd2ab22008-01-23 00:05:38 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allard09de1852007-02-14 01:35:56 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Active Record Class
20 *
21 * This is the platform-independent base Active Record implementation class.
22 *
23 * @package CodeIgniter
24 * @subpackage Drivers
25 * @category Database
Derek Allard3d879d52008-01-18 19:41:32 +000026 * @author ExpressionEngine Dev Team
Derek Allardcdd2ab22008-01-23 00:05:38 +000027 * @link http://codeigniter.com/user_guide/database/
Derek Allard09de1852007-02-14 01:35:56 +000028 */
29class CI_DB_active_record extends CI_DB_driver {
30
31 var $ar_select = array();
32 var $ar_distinct = FALSE;
33 var $ar_from = array();
34 var $ar_join = array();
35 var $ar_where = array();
36 var $ar_like = array();
37 var $ar_groupby = array();
38 var $ar_having = array();
39 var $ar_limit = FALSE;
40 var $ar_offset = FALSE;
41 var $ar_order = FALSE;
42 var $ar_orderby = array();
43 var $ar_set = array();
Derek Allard80dd7022007-12-18 23:55:06 +000044 var $ar_wherein = array();
Derek Allard5e128942007-12-28 21:33:03 +000045 var $ar_aliased_tables = array();
Derek Allard9b3e7b52008-02-04 23:20:34 +000046 var $ar_store_array = array();
47
48 // Active Record Caching variables
49 var $ar_caching = FALSE;
50 var $ar_cache_select = array();
51 var $ar_cache_from = array();
52 var $ar_cache_join = array();
53 var $ar_cache_where = array();
54 var $ar_cache_like = array();
55 var $ar_cache_groupby = array();
56 var $ar_cache_having = array();
57 var $ar_cache_limit = FALSE;
58 var $ar_cache_offset = FALSE;
59 var $ar_cache_order = FALSE;
60 var $ar_cache_orderby = array();
61 var $ar_cache_set = array();
62
Derek Allard09de1852007-02-14 01:35:56 +000063
Derek Allard09de1852007-02-14 01:35:56 +000064 /**
Derek Allard3b118682008-01-22 23:44:32 +000065 * DB Prefix
66 *
67 * Prepends a database prefix if one exists in configuration
68 *
69 * @access public
70 * @param string the table
71 * @return string
72 */
73 function dbprefix($table = '')
74 {
75 if ($table == '')
76 {
77 $this->display_error('db_table_name_required');
78 }
79
80 return $this->dbprefix.$table;
81 }
82
83 // --------------------------------------------------------------------
84
85 /**
Derek Allard09de1852007-02-14 01:35:56 +000086 * Select
87 *
88 * Generates the SELECT portion of the query
89 *
90 * @access public
91 * @param string
92 * @return object
93 */
Derek Allard39b622d2008-01-16 21:10:09 +000094 function select($select = '*', $protect_identifiers = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +000095 {
96 if (is_string($select))
97 {
98 $select = explode(',', $select);
99 }
100
101 foreach ($select as $val)
102 {
103 $val = trim($val);
Derek Allard39b622d2008-01-16 21:10:09 +0000104
105 if ($val != '*' && $protect_identifiers !== FALSE)
106 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000107 if (strpos($val, '.') !== FALSE)
108 {
109 $val = $this->dbprefix.$val;
110 }
111 else
112 {
113 $val = $this->_protect_identifiers($val);
114 }
Derek Allard39b622d2008-01-16 21:10:09 +0000115 }
Derek Allard09de1852007-02-14 01:35:56 +0000116
117 if ($val != '')
Derek Allard39b622d2008-01-16 21:10:09 +0000118 {
Derek Allard09de1852007-02-14 01:35:56 +0000119 $this->ar_select[] = $val;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000120 if ($this->ar_caching === TRUE)
121 {
122 $this->ar_cache_select[] = $val;
123 }
Derek Allard39b622d2008-01-16 21:10:09 +0000124 }
Derek Allard09de1852007-02-14 01:35:56 +0000125 }
126 return $this;
127 }
Derek Allard39b622d2008-01-16 21:10:09 +0000128
129 // --------------------------------------------------------------------
130
131 /**
132 * Select Max
133 *
134 * Generates a SELECT MAX(field) portion of a query
135 *
136 * @access public
137 * @param string the field
138 * @param string an alias
139 * @return object
140 */
141 function select_max($select = '', $alias='')
142 {
143 if (!is_string($select) || $select == '')
144 {
145 $this->display_error('db_invalid_query');
146 }
Derek Allard09de1852007-02-14 01:35:56 +0000147
Derek Allard39b622d2008-01-16 21:10:09 +0000148 $alias = ($alias != '') ? $alias : $select;
149
150 $sql = 'MAX('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
151
152 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000153 if ($this->ar_caching === TRUE)
154 {
155 $this->ar_cache_select[] = $sql;
156 }
Derek Allard39b622d2008-01-16 21:10:09 +0000157
158 return $this;
Derek Allard39b622d2008-01-16 21:10:09 +0000159 }
160
161 // --------------------------------------------------------------------
162
163 /**
164 * Select Min
165 *
166 * Generates a SELECT MIN(field) portion of a query
167 *
168 * @access public
169 * @param string the field
170 * @param string an alias
171 * @return object
172 */
173 function select_min($select = '', $alias='')
174 {
175 if (!is_string($select) || $select == '')
176 {
177 $this->display_error('db_invalid_query');
178 }
179
180 $alias = ($alias != '') ? $alias : $select;
181
182 $sql = 'MIN('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
183
184 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000185 if ($this->ar_caching === TRUE)
186 {
187 $this->ar_cache_select[] = $sql;
188 }
189
Derek Allard39b622d2008-01-16 21:10:09 +0000190 return $this;
191 }
192
193 // --------------------------------------------------------------------
194
195 /**
196 * Select Average
197 *
198 * Generates a SELECT AVG(field) portion of a query
199 *
200 * @access public
201 * @param string the field
202 * @param string an alias
203 * @return object
204 */
205 function select_avg($select = '', $alias='')
206 {
207 if (!is_string($select) || $select == '')
208 {
209 $this->display_error('db_invalid_query');
210 }
211
212 $alias = ($alias != '') ? $alias : $select;
213
214 $sql = 'AVG('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
215
216 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000217 if ($this->ar_caching === TRUE)
218 {
219 $this->ar_cache_select[] = $sql;
220 }
221
Derek Allard39b622d2008-01-16 21:10:09 +0000222 return $this;
223 }
224
225 // --------------------------------------------------------------------
226
227 /**
228 * Select Sum
229 *
230 * Generates a SELECT SUM(field) portion of a query
231 *
232 * @access public
233 * @param string the field
234 * @param string an alias
235 * @return object
236 */
237 function select_sum($select = '', $alias='')
238 {
239 if (!is_string($select) || $select == '')
240 {
241 $this->display_error('db_invalid_query');
242 }
243
244 $alias = ($alias != '') ? $alias : $select;
245
246 $sql = 'SUM('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
247
248 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000249 if ($this->ar_caching === TRUE)
250 {
251 $this->ar_cache_select[] = $sql;
252 }
253
Derek Allard39b622d2008-01-16 21:10:09 +0000254 return $this;
255 }
256
Derek Allard09de1852007-02-14 01:35:56 +0000257 // --------------------------------------------------------------------
258
259 /**
260 * DISTINCT
261 *
262 * Sets a flag which tells the query string compiler to add DISTINCT
263 *
264 * @access public
265 * @param bool
266 * @return object
267 */
268 function distinct($val = TRUE)
269 {
270 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
271 return $this;
272 }
273
274 // --------------------------------------------------------------------
275
276 /**
277 * From
278 *
279 * Generates the FROM portion of the query
280 *
281 * @access public
282 * @param mixed can be a string or array
283 * @return object
284 */
285 function from($from)
286 {
287 foreach ((array)$from as $val)
288 {
Derek Allard39b622d2008-01-16 21:10:09 +0000289 $this->ar_from[] = $this->_protect_identifiers($this->_track_aliases($val));
Derek Allard9b3e7b52008-02-04 23:20:34 +0000290 if ($this->ar_caching === TRUE)
291 {
Derek Allard9a4d1da2008-02-25 14:18:38 +0000292 $this->ar_cache_from[] = $this->_protect_identifiers($this->_track_aliases($val));
Derek Allard9b3e7b52008-02-04 23:20:34 +0000293 }
Derek Allard09de1852007-02-14 01:35:56 +0000294 }
Derek Allard5e128942007-12-28 21:33:03 +0000295
Derek Allard09de1852007-02-14 01:35:56 +0000296 return $this;
297 }
298
299 // --------------------------------------------------------------------
300
301 /**
302 * Join
303 *
304 * Generates the JOIN portion of the query
305 *
306 * @access public
307 * @param string
308 * @param string the join condition
309 * @param string the type of join
310 * @return object
311 */
312 function join($table, $cond, $type = '')
313 {
314 if ($type != '')
315 {
316 $type = strtoupper(trim($type));
317
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 */
Derek Allarde808aac2008-04-06 18:22:00 +0000778 function having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000779 {
Derek Allarde808aac2008-04-06 18:22:00 +0000780 return $this->_having($key, $value, 'AND ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000781 }
Derek Allard43e8cbf2008-03-01 23:14:43 +0000782
783 // --------------------------------------------------------------------
784
785 /**
786 * orhaving() is an alias of or_having()
787 * this function is here for backwards compatibility, as
788 * orhaving() has been deprecated
789 */
790
Derek Allarde808aac2008-04-06 18:22:00 +0000791 function orhaving($key, $value = '', $escape = TRUE)
Derek Allard43e8cbf2008-03-01 23:14:43 +0000792 {
Derek Allarde808aac2008-04-06 18:22:00 +0000793 return $this->or_having($key, $value = '', $escape);
Derek Allard43e8cbf2008-03-01 23:14:43 +0000794 }
Derek Allard09de1852007-02-14 01:35:56 +0000795 // --------------------------------------------------------------------
796
797 /**
798 * Sets the OR HAVING value
799 *
800 * Separates multiple calls with OR
801 *
802 * @access public
803 * @param string
804 * @param string
805 * @return object
806 */
Derek Allarde808aac2008-04-06 18:22:00 +0000807 function or_having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000808 {
Derek Allarde808aac2008-04-06 18:22:00 +0000809 return $this->_having($key, $value, 'OR ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000810 }
811
812 // --------------------------------------------------------------------
813
814 /**
815 * Sets the HAVING values
816 *
817 * Called by having() or orhaving()
818 *
819 * @access private
820 * @param string
Derek Allard9b3e7b52008-02-04 23:20:34 +0000821
Derek Allard09de1852007-02-14 01:35:56 +0000822 * @param string
823 * @return object
824 */
Derek Allarde808aac2008-04-06 18:22:00 +0000825 function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000826 {
827 if ( ! is_array($key))
828 {
829 $key = array($key => $value);
830 }
831
832 foreach ($key as $k => $v)
833 {
834 $prefix = (count($this->ar_having) == 0) ? '' : $type;
Derek Allarde808aac2008-04-06 18:22:00 +0000835
836 if ($escape === TRUE)
837 {
838 $k = $this->_protect_identifiers($k);
839 }
840
Derek Allard09de1852007-02-14 01:35:56 +0000841
842 if ($v != '')
843 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000844 $v = ' '.$this->escape_str($v);
Derek Allard09de1852007-02-14 01:35:56 +0000845 }
846
847 $this->ar_having[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000848 if ($this->ar_caching === TRUE)
849 {
850 $this->ar_cache_having[] = $prefix.$k.$v;
851 }
Derek Allard09de1852007-02-14 01:35:56 +0000852 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000853
Derek Allard09de1852007-02-14 01:35:56 +0000854 return $this;
855 }
856
857 // --------------------------------------------------------------------
858
859 /**
860 * Sets the ORDER BY value
861 *
862 * @access public
863 * @param string
864 * @param string direction: asc or desc
865 * @return object
866 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000867 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000868 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000869 if (strtolower($direction) == 'random')
870 {
871 $orderby = ''; // Random results want or don't need a field name
872 $direction = $this->_random_keyword;
873 }
874 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000875 {
Derek Allard92782492007-08-10 11:26:01 +0000876 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000877 }
878
Derek Allard9b3e7b52008-02-04 23:20:34 +0000879 $orderby_statement = $this->_protect_identifiers($orderby, TRUE).$direction;
880
881 $this->ar_orderby[] = $orderby_statement;
882 if ($this->ar_caching === TRUE)
883 {
884 $this->ar_cache_orderby[] = $orderby_statement;
885 }
886
Derek Allard09de1852007-02-14 01:35:56 +0000887 return $this;
888 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000889
Derek Allard218e2bc2007-12-17 21:18:14 +0000890 // --------------------------------------------------------------------
891
892 /**
893 * orderby() is an alias of order_by()
894 * this function is here for backwards compatibility, as
895 * orderby() has been deprecated
896 */
897 function orderby($orderby, $direction = '')
898 {
899 return $this->order_by($orderby, $direction);
900 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000901
Derek Allard09de1852007-02-14 01:35:56 +0000902 // --------------------------------------------------------------------
903
904 /**
905 * Sets the LIMIT value
906 *
907 * @access public
908 * @param integer the limit value
909 * @param integer the offset value
910 * @return object
911 */
912 function limit($value, $offset = '')
913 {
914 $this->ar_limit = $value;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000915 if ($this->ar_caching === TRUE)
916 {
917 $this->ar_cache_limit[] = $value;
918 }
919
Derek Allard09de1852007-02-14 01:35:56 +0000920 if ($offset != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000921 {
Derek Allard09de1852007-02-14 01:35:56 +0000922 $this->ar_offset = $offset;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000923 if ($this->ar_caching === TRUE)
924 {
925 $this->ar_cache_offset[] = $offset;
926 }
927 }
Derek Allard09de1852007-02-14 01:35:56 +0000928
929 return $this;
930 }
931
932 // --------------------------------------------------------------------
933
934 /**
935 * Sets the OFFSET value
936 *
937 * @access public
938 * @param integer the offset value
939 * @return object
940 */
Derek Allard9b3e7b52008-02-04 23:20:34 +0000941 function offset($offset)
Derek Allard09de1852007-02-14 01:35:56 +0000942 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000943 $this->ar_offset = $offset;
944 if ($this->ar_caching === TRUE)
945 {
946 $this->ar_cache_offset[] = $offset;
947 }
948
Derek Allard09de1852007-02-14 01:35:56 +0000949 return $this;
950 }
951
952 // --------------------------------------------------------------------
953
954 /**
955 * The "set" function. Allows key/value pairs to be set for inserting or updating
956 *
957 * @access public
958 * @param mixed
959 * @param string
Derek Allard39b622d2008-01-16 21:10:09 +0000960 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +0000961 * @return object
962 */
Derek Allard39b622d2008-01-16 21:10:09 +0000963 function set($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000964 {
965 $key = $this->_object_to_array($key);
966
967 if ( ! is_array($key))
968 {
969 $key = array($key => $value);
970 }
971
972 foreach ($key as $k => $v)
973 {
Derek Allard39b622d2008-01-16 21:10:09 +0000974 if ($escape === FALSE)
975 {
976 $this->ar_set[$this->_protect_identifiers($k)] = $v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000977 if ($this->ar_caching === TRUE)
978 {
979 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $v;
980 }
Derek Allard39b622d2008-01-16 21:10:09 +0000981 }
982 else
983 {
984 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000985 if ($this->ar_caching === TRUE)
986 {
987 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $this->escape($v);
988 }
Derek Allard39b622d2008-01-16 21:10:09 +0000989 }
Derek Allard09de1852007-02-14 01:35:56 +0000990 }
991
992 return $this;
993 }
994
995 // --------------------------------------------------------------------
996
997 /**
998 * Get
999 *
1000 * Compiles the select statement based on the other functions called
1001 * and runs the query
1002 *
1003 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +00001004 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +00001005 * @param string the limit clause
1006 * @param string the offset clause
1007 * @return object
1008 */
1009 function get($table = '', $limit = null, $offset = null)
1010 {
1011 if ($table != '')
1012 {
Derek Allard5e128942007-12-28 21:33:03 +00001013 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001014 $this->from($table);
1015 }
1016
1017 if ( ! is_null($limit))
1018 {
1019 $this->limit($limit, $offset);
1020 }
1021
1022 $sql = $this->_compile_select();
1023
1024 $result = $this->query($sql);
1025 $this->_reset_select();
1026 return $result;
1027 }
1028
Derek Allard09de1852007-02-14 01:35:56 +00001029 /**
Derek Allard694b5b82007-12-18 15:58:03 +00001030 * "Count All Results" query
1031 *
1032 * Generates a platform-specific query string that counts all records
1033 * returned by an Active Record query.
1034 *
1035 * @access public
1036 * @param string
1037 * @return string
1038 */
1039 function count_all_results($table = '')
1040 {
1041 if ($table != '')
1042 {
Derek Allard5e128942007-12-28 21:33:03 +00001043 $this->_track_aliases($table);
Derek Allard694b5b82007-12-18 15:58:03 +00001044 $this->from($table);
1045 }
1046
Derek Allard39b622d2008-01-16 21:10:09 +00001047 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
Derek Allard694b5b82007-12-18 15:58:03 +00001048
1049 $query = $this->query($sql);
1050 $this->_reset_select();
1051
1052 if ($query->num_rows() == 0)
1053 {
1054 return '0';
1055 }
1056
1057 $row = $query->row();
1058 return $row->numrows;
1059 }
1060
1061 // --------------------------------------------------------------------
1062
1063 /**
Derek Allard218e2bc2007-12-17 21:18:14 +00001064 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +00001065 *
1066 * Allows the where clause, limit and offset to be added directly
1067 *
1068 * @access public
1069 * @param string the where clause
1070 * @param string the limit clause
1071 * @param string the offset clause
1072 * @return object
1073 */
Derek Allard218e2bc2007-12-17 21:18:14 +00001074 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +00001075 {
1076 if ($table != '')
1077 {
Derek Allard5e128942007-12-28 21:33:03 +00001078 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001079 $this->from($table);
1080 }
1081
1082 if ( ! is_null($where))
1083 {
1084 $this->where($where);
1085 }
1086
1087 if ( ! is_null($limit))
1088 {
1089 $this->limit($limit, $offset);
1090 }
1091
1092 $sql = $this->_compile_select();
1093
1094 $result = $this->query($sql);
1095 $this->_reset_select();
1096 return $result;
1097 }
Derek Allard218e2bc2007-12-17 21:18:14 +00001098
1099 // --------------------------------------------------------------------
1100
1101 /**
1102 * getwhere() is an alias of get_where()
1103 * this function is here for backwards compatibility, as
1104 * getwhere() has been deprecated
1105 */
1106 function getwhere($table = '', $where = null, $limit = null, $offset = null)
1107 {
1108 return $this->get_where($table, $where, $limit, $offset);
1109 }
Derek Allard09de1852007-02-14 01:35:56 +00001110
1111 // --------------------------------------------------------------------
1112
1113 /**
1114 * Insert
1115 *
1116 * Compiles an insert string and runs the query
1117 *
1118 * @access public
1119 * @param string the table to retrieve the results from
1120 * @param array an associative array of insert values
1121 * @return object
1122 */
1123 function insert($table = '', $set = NULL)
1124 {
1125 if ( ! is_null($set))
1126 {
1127 $this->set($set);
1128 }
1129
1130 if (count($this->ar_set) == 0)
1131 {
1132 if ($this->db_debug)
1133 {
1134 return $this->display_error('db_must_use_set');
1135 }
1136 return FALSE;
1137 }
1138
1139 if ($table == '')
1140 {
1141 if ( ! isset($this->ar_from[0]))
1142 {
1143 if ($this->db_debug)
1144 {
1145 return $this->display_error('db_must_set_table');
1146 }
1147 return FALSE;
1148 }
1149
1150 $table = $this->ar_from[0];
1151 }
Derek Allard39b622d2008-01-16 21:10:09 +00001152
1153 $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 +00001154
1155 $this->_reset_write();
1156 return $this->query($sql);
1157 }
1158
1159 // --------------------------------------------------------------------
1160
1161 /**
1162 * Update
1163 *
1164 * Compiles an update string and runs the query
1165 *
1166 * @access public
1167 * @param string the table to retrieve the results from
1168 * @param array an associative array of update values
1169 * @param mixed the where clause
1170 * @return object
1171 */
Derek Allard5e128942007-12-28 21:33:03 +00001172 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001173 {
1174 if ( ! is_null($set))
1175 {
1176 $this->set($set);
1177 }
1178
1179 if (count($this->ar_set) == 0)
1180 {
1181 if ($this->db_debug)
1182 {
1183 return $this->display_error('db_must_use_set');
1184 }
1185 return FALSE;
1186 }
1187
1188 if ($table == '')
1189 {
1190 if ( ! isset($this->ar_from[0]))
1191 {
1192 if ($this->db_debug)
1193 {
1194 return $this->display_error('db_must_set_table');
1195 }
1196 return FALSE;
1197 }
1198
1199 $table = $this->ar_from[0];
1200 }
1201
Derek Allarde77d77c2007-12-19 15:01:55 +00001202 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001203 {
1204 $this->where($where);
1205 }
Derek Allardda6d2402007-12-19 14:49:29 +00001206
Derek Allarde77d77c2007-12-19 15:01:55 +00001207 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +00001208 {
1209 $this->limit($limit);
1210 }
Derek Allard09de1852007-02-14 01:35:56 +00001211
Derek Allard39b622d2008-01-16 21:10:09 +00001212 $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 +00001213
1214 $this->_reset_write();
1215 return $this->query($sql);
1216 }
Derek Allard39b622d2008-01-16 21:10:09 +00001217
1218 // --------------------------------------------------------------------
1219
1220 /**
1221 * Empty Table
1222 *
1223 * Compiles a delete string and runs "DELETE FROM table"
1224 *
1225 * @access public
1226 * @param string the table to empty
1227 * @return object
1228 */
1229 function empty_table($table = '')
1230 {
1231 if ($table == '')
1232 {
1233 if ( ! isset($this->ar_from[0]))
1234 {
1235 if ($this->db_debug)
1236 {
1237 return $this->display_error('db_must_set_table');
1238 }
1239 return FALSE;
1240 }
1241
1242 $table = $this->ar_from[0];
1243 }
1244 else
1245 {
1246 $table = $this->_protect_identifiers($this->dbprefix.$table);
1247 }
1248
1249
1250 $sql = $this->_delete($table);
1251
1252 $this->_reset_write();
1253
1254 return $this->query($sql);
1255 }
1256
1257 // --------------------------------------------------------------------
1258
1259 /**
1260 * Truncate
1261 *
1262 * Compiles a truncate string and runs the query
1263 * If the database does not support the truncate() command
1264 * This function maps to "DELETE FROM table"
1265 *
1266 * @access public
1267 * @param string the table to truncate
1268 * @return object
1269 */
1270 function truncate($table = '')
1271 {
1272 if ($table == '')
1273 {
1274 if ( ! isset($this->ar_from[0]))
1275 {
1276 if ($this->db_debug)
1277 {
1278 return $this->display_error('db_must_set_table');
1279 }
1280 return FALSE;
1281 }
1282
1283 $table = $this->ar_from[0];
1284 }
1285 else
1286 {
1287 $table = $this->_protect_identifiers($this->dbprefix.$table);
1288 }
1289
1290
1291 $sql = $this->_truncate($table);
1292
1293 $this->_reset_write();
1294
1295 return $this->query($sql);
1296 }
Derek Allard09de1852007-02-14 01:35:56 +00001297
1298 // --------------------------------------------------------------------
1299
1300 /**
1301 * Delete
1302 *
1303 * Compiles a delete string and runs the query
1304 *
1305 * @access public
Derek Allard41f60d42007-12-20 20:09:22 +00001306 * @param mixed the table(s) to delete from. String or array
Derek Allard09de1852007-02-14 01:35:56 +00001307 * @param mixed the where clause
Derek Allard41f60d42007-12-20 20:09:22 +00001308 * @param mixed the limit clause
1309 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +00001310 * @return object
1311 */
Derek Allard41f60d42007-12-20 20:09:22 +00001312 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +00001313 {
1314 if ($table == '')
1315 {
1316 if ( ! isset($this->ar_from[0]))
1317 {
1318 if ($this->db_debug)
1319 {
1320 return $this->display_error('db_must_set_table');
1321 }
1322 return FALSE;
1323 }
Derek Allard39b622d2008-01-16 21:10:09 +00001324
Derek Allard09de1852007-02-14 01:35:56 +00001325 $table = $this->ar_from[0];
1326 }
Derek Allard39b622d2008-01-16 21:10:09 +00001327 elseif (is_array($table))
Derek Allard41f60d42007-12-20 20:09:22 +00001328 {
1329 foreach($table as $single_table)
1330 {
Derek Allard39b622d2008-01-16 21:10:09 +00001331 $this->delete($single_table, $where, $limit, FALSE);
Derek Allard41f60d42007-12-20 20:09:22 +00001332 }
Derek Allard39b622d2008-01-16 21:10:09 +00001333
Derek Allard41f60d42007-12-20 20:09:22 +00001334 $this->_reset_write();
1335 return;
1336 }
Derek Allard39b622d2008-01-16 21:10:09 +00001337 else
1338 {
1339 $table = $this->_protect_identifiers($this->dbprefix.$table);
1340 }
Derek Allard41f60d42007-12-20 20:09:22 +00001341
Derek Allard09de1852007-02-14 01:35:56 +00001342 if ($where != '')
1343 {
1344 $this->where($where);
1345 }
1346
Derek Allarde77d77c2007-12-19 15:01:55 +00001347 if ($limit != NULL)
1348 {
1349 $this->limit($limit);
1350 }
1351
Derek Allard39b622d2008-01-16 21:10:09 +00001352 if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
Derek Allard09de1852007-02-14 01:35:56 +00001353 {
1354 if ($this->db_debug)
1355 {
1356 return $this->display_error('db_del_must_use_where');
1357 }
Derek Allard39b622d2008-01-16 21:10:09 +00001358
Derek Allard09de1852007-02-14 01:35:56 +00001359 return FALSE;
1360 }
Derek Allard41f60d42007-12-20 20:09:22 +00001361
Derek Allard39b622d2008-01-16 21:10:09 +00001362 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001363
Derek Allard41f60d42007-12-20 20:09:22 +00001364 if ($reset_data)
1365 {
1366 $this->_reset_write();
1367 }
Derek Allard39b622d2008-01-16 21:10:09 +00001368
Derek Allard09de1852007-02-14 01:35:56 +00001369 return $this->query($sql);
1370 }
Derek Allard15ddc9d2007-12-20 13:54:39 +00001371
Derek Allard09de1852007-02-14 01:35:56 +00001372 // --------------------------------------------------------------------
1373
1374 /**
1375 * Use Table - DEPRECATED
1376 *
1377 * @deprecated use $this->db->from instead
1378 */
1379 function use_table($table)
1380 {
1381 return $this->from($table);
Derek Allard09de1852007-02-14 01:35:56 +00001382 }
1383
1384 // --------------------------------------------------------------------
1385
1386 /**
Derek Allard09de1852007-02-14 01:35:56 +00001387 * Tests whether the string has an SQL operator
1388 *
1389 * @access private
1390 * @param string
1391 * @return bool
1392 */
1393 function _has_operator($str)
1394 {
1395 $str = trim($str);
1396 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
1397 {
1398 return FALSE;
1399 }
1400
1401 return TRUE;
1402 }
1403
1404 // --------------------------------------------------------------------
1405
1406 /**
Derek Allard5e128942007-12-28 21:33:03 +00001407 * Track Aliases
1408 *
1409 * Used to track SQL statements written with aliased tables.
1410 *
1411 * @access private
1412 * @param string The table to inspect
1413 * @return string
1414 */
1415 function _track_aliases($table)
1416 {
1417 // if a table alias is used we can recognize it by a space
1418 if (strpos($table, " ") !== FALSE)
1419 {
1420 // if the alias is written with the AS keyowrd, get it out
Derek Allard39b622d2008-01-16 21:10:09 +00001421 $table = preg_replace('/ AS /i', ' ', $table);
Derek Allard5e128942007-12-28 21:33:03 +00001422
Derek Allard39b622d2008-01-16 21:10:09 +00001423 $this->ar_aliased_tables[] = trim(strrchr($table, " "));
Derek Allard5e128942007-12-28 21:33:03 +00001424 }
1425
1426 return $this->dbprefix.$table;
1427 }
1428
1429 // --------------------------------------------------------------------
1430
1431 /**
1432 * Filter Table Aliases
1433 *
1434 * Intelligently removes database prefixes from aliased tables
1435 *
1436 * @access private
1437 * @param array An array of compiled SQL
1438 * @return array Cleaned up statement with aliases accounted for
1439 */
1440 function _filter_table_aliases($statements)
1441 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001442
Derek Allard39b622d2008-01-16 21:10:09 +00001443 foreach ($statements as $k => $v)
Derek Allard5e128942007-12-28 21:33:03 +00001444 {
Derek Allard39b622d2008-01-16 21:10:09 +00001445 foreach ($this->ar_aliased_tables as $table)
Derek Allard5e128942007-12-28 21:33:03 +00001446 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001447 $statements[$k] = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $statements[$k]); // makes `table.field`
1448 $statements[$k] = str_replace($this->dbprefix.$table.'.', $table.'.', $statements[$k]);
Derek Allard5e128942007-12-28 21:33:03 +00001449 }
Derek Allard5e128942007-12-28 21:33:03 +00001450 }
Derek Allard39b622d2008-01-16 21:10:09 +00001451 return $statements;
Derek Allard5e128942007-12-28 21:33:03 +00001452 }
1453
1454 // --------------------------------------------------------------------
1455
1456 /**
Derek Allard09de1852007-02-14 01:35:56 +00001457 * Compile the SELECT statement
1458 *
1459 * Generates a query string based on which functions were used.
1460 * Should not be called directly. The get() function calls it.
1461 *
1462 * @access private
1463 * @return string
1464 */
Derek Allard694b5b82007-12-18 15:58:03 +00001465 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001466 {
Derek Allard78255262008-02-06 13:54:23 +00001467 $this->_merge_cache();
Derek Allard9b3e7b52008-02-04 23:20:34 +00001468
Derek Allard09de1852007-02-14 01:35:56 +00001469 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
1470
1471 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->ar_select);
1472
Derek Allard694b5b82007-12-18 15:58:03 +00001473 if ($select_override !== FALSE)
1474 {
1475 $sql = $select_override;
1476 }
1477
Derek Allard09de1852007-02-14 01:35:56 +00001478 if (count($this->ar_from) > 0)
1479 {
1480 $sql .= "\nFROM ";
Derek Jonesc6ad0232008-01-29 18:44:54 +00001481 $sql .= $this->_from_tables($this->ar_from);
Derek Allard09de1852007-02-14 01:35:56 +00001482 }
1483
1484 if (count($this->ar_join) > 0)
Derek Allard5e128942007-12-28 21:33:03 +00001485 {
Derek Allard09de1852007-02-14 01:35:56 +00001486 $sql .= "\n";
Derek Allard5e128942007-12-28 21:33:03 +00001487
1488 // special consideration for table aliases
1489 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1490 {
1491 $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
1492 }
1493 else
1494 {
1495 $sql .= implode("\n", $this->ar_join);
1496 }
1497
Derek Allard09de1852007-02-14 01:35:56 +00001498 }
1499
1500 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1501 {
1502 $sql .= "\nWHERE ";
1503 }
1504
1505 $sql .= implode("\n", $this->ar_where);
1506
1507 if (count($this->ar_like) > 0)
1508 {
1509 if (count($this->ar_where) > 0)
1510 {
1511 $sql .= " AND ";
1512 }
1513
1514 $sql .= implode("\n", $this->ar_like);
1515 }
1516
1517 if (count($this->ar_groupby) > 0)
1518 {
Derek Allard5e128942007-12-28 21:33:03 +00001519
Derek Allard09de1852007-02-14 01:35:56 +00001520 $sql .= "\nGROUP BY ";
Derek Allard5e128942007-12-28 21:33:03 +00001521
1522 // special consideration for table aliases
1523 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1524 {
1525 $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
1526 }
1527 else
1528 {
1529 $sql .= implode(', ', $this->ar_groupby);
1530 }
Derek Allard09de1852007-02-14 01:35:56 +00001531 }
1532
1533 if (count($this->ar_having) > 0)
1534 {
1535 $sql .= "\nHAVING ";
1536 $sql .= implode("\n", $this->ar_having);
1537 }
1538
1539 if (count($this->ar_orderby) > 0)
1540 {
1541 $sql .= "\nORDER BY ";
1542 $sql .= implode(', ', $this->ar_orderby);
1543
1544 if ($this->ar_order !== FALSE)
1545 {
1546 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1547 }
1548 }
1549
1550 if (is_numeric($this->ar_limit))
1551 {
1552 $sql .= "\n";
1553 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1554 }
1555
1556 return $sql;
1557 }
1558
1559 // --------------------------------------------------------------------
1560
1561 /**
1562 * Object to Array
1563 *
1564 * Takes an object as input and converts the class variables to array key/vals
1565 *
1566 * @access public
1567 * @param object
1568 * @return array
1569 */
1570 function _object_to_array($object)
1571 {
1572 if ( ! is_object($object))
1573 {
1574 return $object;
1575 }
1576
1577 $array = array();
1578 foreach (get_object_vars($object) as $key => $val)
1579 {
Derek Allard848b7762007-12-31 16:43:05 +00001580 // There are some built in keys we need to ignore for this conversion
1581 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
1582
Derek Allard09de1852007-02-14 01:35:56 +00001583 {
1584 $array[$key] = $val;
1585 }
1586 }
1587
1588 return $array;
1589 }
1590
1591 // --------------------------------------------------------------------
1592
1593 /**
Derek Allard9b3e7b52008-02-04 23:20:34 +00001594 * Start Cache
1595 *
1596 * Starts AR caching
1597 *
1598 * @access public
1599 * @return void
1600 */
1601 function start_cache()
1602 {
1603 $this->ar_caching = TRUE;
1604 }
1605
1606 // --------------------------------------------------------------------
1607
1608 /**
1609 * Stop Cache
1610 *
1611 * Stops AR caching
1612 *
1613 * @access public
1614 * @return void
1615 */
1616 function stop_cache()
1617 {
1618 $this->ar_caching = FALSE;
1619 }
1620
1621
1622 // --------------------------------------------------------------------
1623
1624 /**
1625 * Flush Cache
1626 *
1627 * Empties the AR cache
1628 *
1629 * @access public
1630 * @return void
1631 */
1632 function flush_cache()
1633 {
1634 $ar_reset_items = array(
1635 'ar_cache_select' => array(),
1636 'ar_cache_from' => array(),
1637 'ar_cache_join' => array(),
1638 'ar_cache_where' => array(),
1639 'ar_cache_like' => array(),
1640 'ar_cache_groupby' => array(),
1641 'ar_cache_having' =>array(),
1642 'ar_cache_orderby' => array(),
1643 'ar_cache_set' => array()
1644 );
1645
1646 $this->_reset_run($ar_reset_items);
1647 }
1648
1649 // --------------------------------------------------------------------
1650
1651 /**
1652 * Merge Cache
1653 *
1654 * When called, this function merges any cached AR arrays with
1655 * locally called ones.
1656 *
1657 * @access private
1658 * @return void
1659 */
1660 function _merge_cache()
1661 {
1662 $ar_items = array('select', 'from', 'join', 'where', 'like', 'groupby', 'having', 'orderby', 'set');
1663
1664 foreach ($ar_items as $ar_item)
1665 {
1666 $ar_cache_item = 'ar_cache_'.$ar_item;
1667 $ar_item = 'ar_'.$ar_item;
1668 $this->$ar_item = array_unique(array_merge($this->$ar_item, $this->$ar_cache_item));
1669 }
1670 }
1671
1672 // --------------------------------------------------------------------
1673
1674 /**
1675 * Resets the active record values. Called by the get() function
1676 *
1677 * @access private
1678 * @param array An array of fields to reset
1679 * @return void
1680 */
1681 function _reset_run($ar_reset_items)
1682 {
1683 foreach ($ar_reset_items as $item => $default_value)
1684 {
1685 if (!in_array($item, $this->ar_store_array))
1686 {
1687 $this->$item = $default_value;
1688 }
1689 }
1690 }
1691
1692 // --------------------------------------------------------------------
1693
1694 /**
Derek Allard09de1852007-02-14 01:35:56 +00001695 * Resets the active record values. Called by the get() function
1696 *
1697 * @access private
1698 * @return void
1699 */
1700 function _reset_select()
1701 {
Derek Allard9b3e7b52008-02-04 23:20:34 +00001702 $ar_reset_items = array(
1703 'ar_select' => array(),
1704 'ar_from' => array(),
1705 'ar_join' => array(),
1706 'ar_where' => array(),
1707 'ar_like' => array(),
1708 'ar_groupby' => array(),
1709 'ar_having' => array(),
1710 'ar_orderby' => array(),
1711 'ar_wherein' => array(),
1712 'ar_aliased_tables' => array(),
1713 'ar_distinct' => FALSE,
1714 'ar_limit' => FALSE,
1715 'ar_offset' => FALSE,
1716 'ar_order' => FALSE,
1717 );
1718
1719 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001720 }
1721
1722 // --------------------------------------------------------------------
1723
1724 /**
1725 * Resets the active record "write" values.
1726 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001727 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001728 *
1729 * @access private
1730 * @return void
1731 */
1732 function _reset_write()
Derek Allard9b3e7b52008-02-04 23:20:34 +00001733 {
1734 $ar_reset_items = array(
1735 'ar_set' => array(),
1736 'ar_from' => array(),
1737 'ar_where' => array(),
1738 'ar_like' => array(),
1739 'ar_orderby' => array(),
1740 'ar_limit' => FALSE,
1741 'ar_order' => FALSE
1742 );
1743
1744 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001745 }
1746
1747}
adminac94f382006-09-24 20:28:12 +00001748?>