blob: bde43abf02dd6e7a323e238b858eaae896bf4078 [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 */
778 function having($key, $value = '')
779 {
780 return $this->_having($key, $value, 'AND ');
781 }
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
791 function orhaving($key, $value = '')
792 {
Derek Allard49ef0422008-03-04 21:31:16 +0000793 return $this->or_having($key, $value = '');
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 Allard43e8cbf2008-03-01 23:14:43 +0000807 function or_having($key, $value = '')
Derek Allard09de1852007-02-14 01:35:56 +0000808 {
809 return $this->_having($key, $value, 'OR ');
810 }
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 */
825 function _having($key, $value = '', $type = 'AND ')
826 {
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 Allard9b3e7b52008-02-04 23:20:34 +0000835 $k = $this->_protect_identifiers($k);
Derek Allard09de1852007-02-14 01:35:56 +0000836
837 if ($v != '')
838 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000839 $v = ' '.$this->escape_str($v);
Derek Allard09de1852007-02-14 01:35:56 +0000840 }
841
842 $this->ar_having[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000843 if ($this->ar_caching === TRUE)
844 {
845 $this->ar_cache_having[] = $prefix.$k.$v;
846 }
Derek Allard09de1852007-02-14 01:35:56 +0000847 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000848
Derek Allard09de1852007-02-14 01:35:56 +0000849 return $this;
850 }
851
852 // --------------------------------------------------------------------
853
854 /**
855 * Sets the ORDER BY value
856 *
857 * @access public
858 * @param string
859 * @param string direction: asc or desc
860 * @return object
861 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000862 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000863 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000864 if (strtolower($direction) == 'random')
865 {
866 $orderby = ''; // Random results want or don't need a field name
867 $direction = $this->_random_keyword;
868 }
869 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000870 {
Derek Allard92782492007-08-10 11:26:01 +0000871 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000872 }
873
Derek Allard9b3e7b52008-02-04 23:20:34 +0000874 $orderby_statement = $this->_protect_identifiers($orderby, TRUE).$direction;
875
876 $this->ar_orderby[] = $orderby_statement;
877 if ($this->ar_caching === TRUE)
878 {
879 $this->ar_cache_orderby[] = $orderby_statement;
880 }
881
Derek Allard09de1852007-02-14 01:35:56 +0000882 return $this;
883 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000884
Derek Allard218e2bc2007-12-17 21:18:14 +0000885 // --------------------------------------------------------------------
886
887 /**
888 * orderby() is an alias of order_by()
889 * this function is here for backwards compatibility, as
890 * orderby() has been deprecated
891 */
892 function orderby($orderby, $direction = '')
893 {
894 return $this->order_by($orderby, $direction);
895 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000896
Derek Allard09de1852007-02-14 01:35:56 +0000897 // --------------------------------------------------------------------
898
899 /**
900 * Sets the LIMIT value
901 *
902 * @access public
903 * @param integer the limit value
904 * @param integer the offset value
905 * @return object
906 */
907 function limit($value, $offset = '')
908 {
909 $this->ar_limit = $value;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000910 if ($this->ar_caching === TRUE)
911 {
912 $this->ar_cache_limit[] = $value;
913 }
914
Derek Allard09de1852007-02-14 01:35:56 +0000915 if ($offset != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000916 {
Derek Allard09de1852007-02-14 01:35:56 +0000917 $this->ar_offset = $offset;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000918 if ($this->ar_caching === TRUE)
919 {
920 $this->ar_cache_offset[] = $offset;
921 }
922 }
Derek Allard09de1852007-02-14 01:35:56 +0000923
924 return $this;
925 }
926
927 // --------------------------------------------------------------------
928
929 /**
930 * Sets the OFFSET value
931 *
932 * @access public
933 * @param integer the offset value
934 * @return object
935 */
Derek Allard9b3e7b52008-02-04 23:20:34 +0000936 function offset($offset)
Derek Allard09de1852007-02-14 01:35:56 +0000937 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000938 $this->ar_offset = $offset;
939 if ($this->ar_caching === TRUE)
940 {
941 $this->ar_cache_offset[] = $offset;
942 }
943
Derek Allard09de1852007-02-14 01:35:56 +0000944 return $this;
945 }
946
947 // --------------------------------------------------------------------
948
949 /**
950 * The "set" function. Allows key/value pairs to be set for inserting or updating
951 *
952 * @access public
953 * @param mixed
954 * @param string
Derek Allard39b622d2008-01-16 21:10:09 +0000955 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +0000956 * @return object
957 */
Derek Allard39b622d2008-01-16 21:10:09 +0000958 function set($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000959 {
960 $key = $this->_object_to_array($key);
961
962 if ( ! is_array($key))
963 {
964 $key = array($key => $value);
965 }
966
967 foreach ($key as $k => $v)
968 {
Derek Allard39b622d2008-01-16 21:10:09 +0000969 if ($escape === FALSE)
970 {
971 $this->ar_set[$this->_protect_identifiers($k)] = $v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000972 if ($this->ar_caching === TRUE)
973 {
974 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $v;
975 }
Derek Allard39b622d2008-01-16 21:10:09 +0000976 }
977 else
978 {
979 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000980 if ($this->ar_caching === TRUE)
981 {
982 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $this->escape($v);
983 }
Derek Allard39b622d2008-01-16 21:10:09 +0000984 }
Derek Allard09de1852007-02-14 01:35:56 +0000985 }
986
987 return $this;
988 }
989
990 // --------------------------------------------------------------------
991
992 /**
993 * Get
994 *
995 * Compiles the select statement based on the other functions called
996 * and runs the query
997 *
998 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +0000999 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +00001000 * @param string the limit clause
1001 * @param string the offset clause
1002 * @return object
1003 */
1004 function get($table = '', $limit = null, $offset = null)
1005 {
1006 if ($table != '')
1007 {
Derek Allard5e128942007-12-28 21:33:03 +00001008 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001009 $this->from($table);
1010 }
1011
1012 if ( ! is_null($limit))
1013 {
1014 $this->limit($limit, $offset);
1015 }
1016
1017 $sql = $this->_compile_select();
1018
1019 $result = $this->query($sql);
1020 $this->_reset_select();
1021 return $result;
1022 }
1023
Derek Allard09de1852007-02-14 01:35:56 +00001024 /**
Derek Allard694b5b82007-12-18 15:58:03 +00001025 * "Count All Results" query
1026 *
1027 * Generates a platform-specific query string that counts all records
1028 * returned by an Active Record query.
1029 *
1030 * @access public
1031 * @param string
1032 * @return string
1033 */
1034 function count_all_results($table = '')
1035 {
1036 if ($table != '')
1037 {
Derek Allard5e128942007-12-28 21:33:03 +00001038 $this->_track_aliases($table);
Derek Allard694b5b82007-12-18 15:58:03 +00001039 $this->from($table);
1040 }
1041
Derek Allard39b622d2008-01-16 21:10:09 +00001042 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
Derek Allard694b5b82007-12-18 15:58:03 +00001043
1044 $query = $this->query($sql);
1045 $this->_reset_select();
1046
1047 if ($query->num_rows() == 0)
1048 {
1049 return '0';
1050 }
1051
1052 $row = $query->row();
1053 return $row->numrows;
1054 }
1055
1056 // --------------------------------------------------------------------
1057
1058 /**
Derek Allard218e2bc2007-12-17 21:18:14 +00001059 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +00001060 *
1061 * Allows the where clause, limit and offset to be added directly
1062 *
1063 * @access public
1064 * @param string the where clause
1065 * @param string the limit clause
1066 * @param string the offset clause
1067 * @return object
1068 */
Derek Allard218e2bc2007-12-17 21:18:14 +00001069 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +00001070 {
1071 if ($table != '')
1072 {
Derek Allard5e128942007-12-28 21:33:03 +00001073 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001074 $this->from($table);
1075 }
1076
1077 if ( ! is_null($where))
1078 {
1079 $this->where($where);
1080 }
1081
1082 if ( ! is_null($limit))
1083 {
1084 $this->limit($limit, $offset);
1085 }
1086
1087 $sql = $this->_compile_select();
1088
1089 $result = $this->query($sql);
1090 $this->_reset_select();
1091 return $result;
1092 }
Derek Allard218e2bc2007-12-17 21:18:14 +00001093
1094 // --------------------------------------------------------------------
1095
1096 /**
1097 * getwhere() is an alias of get_where()
1098 * this function is here for backwards compatibility, as
1099 * getwhere() has been deprecated
1100 */
1101 function getwhere($table = '', $where = null, $limit = null, $offset = null)
1102 {
1103 return $this->get_where($table, $where, $limit, $offset);
1104 }
Derek Allard09de1852007-02-14 01:35:56 +00001105
1106 // --------------------------------------------------------------------
1107
1108 /**
1109 * Insert
1110 *
1111 * Compiles an insert string and runs the query
1112 *
1113 * @access public
1114 * @param string the table to retrieve the results from
1115 * @param array an associative array of insert values
1116 * @return object
1117 */
1118 function insert($table = '', $set = NULL)
1119 {
1120 if ( ! is_null($set))
1121 {
1122 $this->set($set);
1123 }
1124
1125 if (count($this->ar_set) == 0)
1126 {
1127 if ($this->db_debug)
1128 {
1129 return $this->display_error('db_must_use_set');
1130 }
1131 return FALSE;
1132 }
1133
1134 if ($table == '')
1135 {
1136 if ( ! isset($this->ar_from[0]))
1137 {
1138 if ($this->db_debug)
1139 {
1140 return $this->display_error('db_must_set_table');
1141 }
1142 return FALSE;
1143 }
1144
1145 $table = $this->ar_from[0];
1146 }
Derek Allard39b622d2008-01-16 21:10:09 +00001147
1148 $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 +00001149
1150 $this->_reset_write();
1151 return $this->query($sql);
1152 }
1153
1154 // --------------------------------------------------------------------
1155
1156 /**
1157 * Update
1158 *
1159 * Compiles an update string and runs the query
1160 *
1161 * @access public
1162 * @param string the table to retrieve the results from
1163 * @param array an associative array of update values
1164 * @param mixed the where clause
1165 * @return object
1166 */
Derek Allard5e128942007-12-28 21:33:03 +00001167 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001168 {
1169 if ( ! is_null($set))
1170 {
1171 $this->set($set);
1172 }
1173
1174 if (count($this->ar_set) == 0)
1175 {
1176 if ($this->db_debug)
1177 {
1178 return $this->display_error('db_must_use_set');
1179 }
1180 return FALSE;
1181 }
1182
1183 if ($table == '')
1184 {
1185 if ( ! isset($this->ar_from[0]))
1186 {
1187 if ($this->db_debug)
1188 {
1189 return $this->display_error('db_must_set_table');
1190 }
1191 return FALSE;
1192 }
1193
1194 $table = $this->ar_from[0];
1195 }
1196
Derek Allarde77d77c2007-12-19 15:01:55 +00001197 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001198 {
1199 $this->where($where);
1200 }
Derek Allardda6d2402007-12-19 14:49:29 +00001201
Derek Allarde77d77c2007-12-19 15:01:55 +00001202 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +00001203 {
1204 $this->limit($limit);
1205 }
Derek Allard09de1852007-02-14 01:35:56 +00001206
Derek Allard39b622d2008-01-16 21:10:09 +00001207 $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 +00001208
1209 $this->_reset_write();
1210 return $this->query($sql);
1211 }
Derek Allard39b622d2008-01-16 21:10:09 +00001212
1213 // --------------------------------------------------------------------
1214
1215 /**
1216 * Empty Table
1217 *
1218 * Compiles a delete string and runs "DELETE FROM table"
1219 *
1220 * @access public
1221 * @param string the table to empty
1222 * @return object
1223 */
1224 function empty_table($table = '')
1225 {
1226 if ($table == '')
1227 {
1228 if ( ! isset($this->ar_from[0]))
1229 {
1230 if ($this->db_debug)
1231 {
1232 return $this->display_error('db_must_set_table');
1233 }
1234 return FALSE;
1235 }
1236
1237 $table = $this->ar_from[0];
1238 }
1239 else
1240 {
1241 $table = $this->_protect_identifiers($this->dbprefix.$table);
1242 }
1243
1244
1245 $sql = $this->_delete($table);
1246
1247 $this->_reset_write();
1248
1249 return $this->query($sql);
1250 }
1251
1252 // --------------------------------------------------------------------
1253
1254 /**
1255 * Truncate
1256 *
1257 * Compiles a truncate string and runs the query
1258 * If the database does not support the truncate() command
1259 * This function maps to "DELETE FROM table"
1260 *
1261 * @access public
1262 * @param string the table to truncate
1263 * @return object
1264 */
1265 function truncate($table = '')
1266 {
1267 if ($table == '')
1268 {
1269 if ( ! isset($this->ar_from[0]))
1270 {
1271 if ($this->db_debug)
1272 {
1273 return $this->display_error('db_must_set_table');
1274 }
1275 return FALSE;
1276 }
1277
1278 $table = $this->ar_from[0];
1279 }
1280 else
1281 {
1282 $table = $this->_protect_identifiers($this->dbprefix.$table);
1283 }
1284
1285
1286 $sql = $this->_truncate($table);
1287
1288 $this->_reset_write();
1289
1290 return $this->query($sql);
1291 }
Derek Allard09de1852007-02-14 01:35:56 +00001292
1293 // --------------------------------------------------------------------
1294
1295 /**
1296 * Delete
1297 *
1298 * Compiles a delete string and runs the query
1299 *
1300 * @access public
Derek Allard41f60d42007-12-20 20:09:22 +00001301 * @param mixed the table(s) to delete from. String or array
Derek Allard09de1852007-02-14 01:35:56 +00001302 * @param mixed the where clause
Derek Allard41f60d42007-12-20 20:09:22 +00001303 * @param mixed the limit clause
1304 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +00001305 * @return object
1306 */
Derek Allard41f60d42007-12-20 20:09:22 +00001307 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +00001308 {
1309 if ($table == '')
1310 {
1311 if ( ! isset($this->ar_from[0]))
1312 {
1313 if ($this->db_debug)
1314 {
1315 return $this->display_error('db_must_set_table');
1316 }
1317 return FALSE;
1318 }
Derek Allard39b622d2008-01-16 21:10:09 +00001319
Derek Allard09de1852007-02-14 01:35:56 +00001320 $table = $this->ar_from[0];
1321 }
Derek Allard39b622d2008-01-16 21:10:09 +00001322 elseif (is_array($table))
Derek Allard41f60d42007-12-20 20:09:22 +00001323 {
1324 foreach($table as $single_table)
1325 {
Derek Allard39b622d2008-01-16 21:10:09 +00001326 $this->delete($single_table, $where, $limit, FALSE);
Derek Allard41f60d42007-12-20 20:09:22 +00001327 }
Derek Allard39b622d2008-01-16 21:10:09 +00001328
Derek Allard41f60d42007-12-20 20:09:22 +00001329 $this->_reset_write();
1330 return;
1331 }
Derek Allard39b622d2008-01-16 21:10:09 +00001332 else
1333 {
1334 $table = $this->_protect_identifiers($this->dbprefix.$table);
1335 }
Derek Allard41f60d42007-12-20 20:09:22 +00001336
Derek Allard09de1852007-02-14 01:35:56 +00001337 if ($where != '')
1338 {
1339 $this->where($where);
1340 }
1341
Derek Allarde77d77c2007-12-19 15:01:55 +00001342 if ($limit != NULL)
1343 {
1344 $this->limit($limit);
1345 }
1346
Derek Allard39b622d2008-01-16 21:10:09 +00001347 if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
Derek Allard09de1852007-02-14 01:35:56 +00001348 {
1349 if ($this->db_debug)
1350 {
1351 return $this->display_error('db_del_must_use_where');
1352 }
Derek Allard39b622d2008-01-16 21:10:09 +00001353
Derek Allard09de1852007-02-14 01:35:56 +00001354 return FALSE;
1355 }
Derek Allard41f60d42007-12-20 20:09:22 +00001356
Derek Allard39b622d2008-01-16 21:10:09 +00001357 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001358
Derek Allard41f60d42007-12-20 20:09:22 +00001359 if ($reset_data)
1360 {
1361 $this->_reset_write();
1362 }
Derek Allard39b622d2008-01-16 21:10:09 +00001363
Derek Allard09de1852007-02-14 01:35:56 +00001364 return $this->query($sql);
1365 }
Derek Allard15ddc9d2007-12-20 13:54:39 +00001366
Derek Allard09de1852007-02-14 01:35:56 +00001367 // --------------------------------------------------------------------
1368
1369 /**
1370 * Use Table - DEPRECATED
1371 *
1372 * @deprecated use $this->db->from instead
1373 */
1374 function use_table($table)
1375 {
1376 return $this->from($table);
Derek Allard09de1852007-02-14 01:35:56 +00001377 }
1378
1379 // --------------------------------------------------------------------
1380
1381 /**
Derek Allard09de1852007-02-14 01:35:56 +00001382 * Tests whether the string has an SQL operator
1383 *
1384 * @access private
1385 * @param string
1386 * @return bool
1387 */
1388 function _has_operator($str)
1389 {
1390 $str = trim($str);
1391 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
1392 {
1393 return FALSE;
1394 }
1395
1396 return TRUE;
1397 }
1398
1399 // --------------------------------------------------------------------
1400
1401 /**
Derek Allard5e128942007-12-28 21:33:03 +00001402 * Track Aliases
1403 *
1404 * Used to track SQL statements written with aliased tables.
1405 *
1406 * @access private
1407 * @param string The table to inspect
1408 * @return string
1409 */
1410 function _track_aliases($table)
1411 {
1412 // if a table alias is used we can recognize it by a space
1413 if (strpos($table, " ") !== FALSE)
1414 {
1415 // if the alias is written with the AS keyowrd, get it out
Derek Allard39b622d2008-01-16 21:10:09 +00001416 $table = preg_replace('/ AS /i', ' ', $table);
Derek Allard5e128942007-12-28 21:33:03 +00001417
Derek Allard39b622d2008-01-16 21:10:09 +00001418 $this->ar_aliased_tables[] = trim(strrchr($table, " "));
Derek Allard5e128942007-12-28 21:33:03 +00001419 }
1420
1421 return $this->dbprefix.$table;
1422 }
1423
1424 // --------------------------------------------------------------------
1425
1426 /**
1427 * Filter Table Aliases
1428 *
1429 * Intelligently removes database prefixes from aliased tables
1430 *
1431 * @access private
1432 * @param array An array of compiled SQL
1433 * @return array Cleaned up statement with aliases accounted for
1434 */
1435 function _filter_table_aliases($statements)
1436 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001437
Derek Allard39b622d2008-01-16 21:10:09 +00001438 foreach ($statements as $k => $v)
Derek Allard5e128942007-12-28 21:33:03 +00001439 {
Derek Allard39b622d2008-01-16 21:10:09 +00001440 foreach ($this->ar_aliased_tables as $table)
Derek Allard5e128942007-12-28 21:33:03 +00001441 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001442 $statements[$k] = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $statements[$k]); // makes `table.field`
1443 $statements[$k] = str_replace($this->dbprefix.$table.'.', $table.'.', $statements[$k]);
Derek Allard5e128942007-12-28 21:33:03 +00001444 }
Derek Allard5e128942007-12-28 21:33:03 +00001445 }
Derek Allard39b622d2008-01-16 21:10:09 +00001446 return $statements;
Derek Allard5e128942007-12-28 21:33:03 +00001447 }
1448
1449 // --------------------------------------------------------------------
1450
1451 /**
Derek Allard09de1852007-02-14 01:35:56 +00001452 * Compile the SELECT statement
1453 *
1454 * Generates a query string based on which functions were used.
1455 * Should not be called directly. The get() function calls it.
1456 *
1457 * @access private
1458 * @return string
1459 */
Derek Allard694b5b82007-12-18 15:58:03 +00001460 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001461 {
Derek Allard78255262008-02-06 13:54:23 +00001462 $this->_merge_cache();
Derek Allard9b3e7b52008-02-04 23:20:34 +00001463
Derek Allard09de1852007-02-14 01:35:56 +00001464 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
1465
1466 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->ar_select);
1467
Derek Allard694b5b82007-12-18 15:58:03 +00001468 if ($select_override !== FALSE)
1469 {
1470 $sql = $select_override;
1471 }
1472
Derek Allard09de1852007-02-14 01:35:56 +00001473 if (count($this->ar_from) > 0)
1474 {
1475 $sql .= "\nFROM ";
Derek Jonesc6ad0232008-01-29 18:44:54 +00001476 $sql .= $this->_from_tables($this->ar_from);
Derek Allard09de1852007-02-14 01:35:56 +00001477 }
1478
1479 if (count($this->ar_join) > 0)
Derek Allard5e128942007-12-28 21:33:03 +00001480 {
Derek Allard09de1852007-02-14 01:35:56 +00001481 $sql .= "\n";
Derek Allard5e128942007-12-28 21:33:03 +00001482
1483 // special consideration for table aliases
1484 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1485 {
1486 $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
1487 }
1488 else
1489 {
1490 $sql .= implode("\n", $this->ar_join);
1491 }
1492
Derek Allard09de1852007-02-14 01:35:56 +00001493 }
1494
1495 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1496 {
1497 $sql .= "\nWHERE ";
1498 }
1499
1500 $sql .= implode("\n", $this->ar_where);
1501
1502 if (count($this->ar_like) > 0)
1503 {
1504 if (count($this->ar_where) > 0)
1505 {
1506 $sql .= " AND ";
1507 }
1508
1509 $sql .= implode("\n", $this->ar_like);
1510 }
1511
1512 if (count($this->ar_groupby) > 0)
1513 {
Derek Allard5e128942007-12-28 21:33:03 +00001514
Derek Allard09de1852007-02-14 01:35:56 +00001515 $sql .= "\nGROUP BY ";
Derek Allard5e128942007-12-28 21:33:03 +00001516
1517 // special consideration for table aliases
1518 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1519 {
1520 $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
1521 }
1522 else
1523 {
1524 $sql .= implode(', ', $this->ar_groupby);
1525 }
Derek Allard09de1852007-02-14 01:35:56 +00001526 }
1527
1528 if (count($this->ar_having) > 0)
1529 {
1530 $sql .= "\nHAVING ";
1531 $sql .= implode("\n", $this->ar_having);
1532 }
1533
1534 if (count($this->ar_orderby) > 0)
1535 {
1536 $sql .= "\nORDER BY ";
1537 $sql .= implode(', ', $this->ar_orderby);
1538
1539 if ($this->ar_order !== FALSE)
1540 {
1541 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1542 }
1543 }
1544
1545 if (is_numeric($this->ar_limit))
1546 {
1547 $sql .= "\n";
1548 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1549 }
1550
1551 return $sql;
1552 }
1553
1554 // --------------------------------------------------------------------
1555
1556 /**
1557 * Object to Array
1558 *
1559 * Takes an object as input and converts the class variables to array key/vals
1560 *
1561 * @access public
1562 * @param object
1563 * @return array
1564 */
1565 function _object_to_array($object)
1566 {
1567 if ( ! is_object($object))
1568 {
1569 return $object;
1570 }
1571
1572 $array = array();
1573 foreach (get_object_vars($object) as $key => $val)
1574 {
Derek Allard848b7762007-12-31 16:43:05 +00001575 // There are some built in keys we need to ignore for this conversion
1576 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
1577
Derek Allard09de1852007-02-14 01:35:56 +00001578 {
1579 $array[$key] = $val;
1580 }
1581 }
1582
1583 return $array;
1584 }
1585
1586 // --------------------------------------------------------------------
1587
1588 /**
Derek Allard9b3e7b52008-02-04 23:20:34 +00001589 * Start Cache
1590 *
1591 * Starts AR caching
1592 *
1593 * @access public
1594 * @return void
1595 */
1596 function start_cache()
1597 {
1598 $this->ar_caching = TRUE;
1599 }
1600
1601 // --------------------------------------------------------------------
1602
1603 /**
1604 * Stop Cache
1605 *
1606 * Stops AR caching
1607 *
1608 * @access public
1609 * @return void
1610 */
1611 function stop_cache()
1612 {
1613 $this->ar_caching = FALSE;
1614 }
1615
1616
1617 // --------------------------------------------------------------------
1618
1619 /**
1620 * Flush Cache
1621 *
1622 * Empties the AR cache
1623 *
1624 * @access public
1625 * @return void
1626 */
1627 function flush_cache()
1628 {
1629 $ar_reset_items = array(
1630 'ar_cache_select' => array(),
1631 'ar_cache_from' => array(),
1632 'ar_cache_join' => array(),
1633 'ar_cache_where' => array(),
1634 'ar_cache_like' => array(),
1635 'ar_cache_groupby' => array(),
1636 'ar_cache_having' =>array(),
1637 'ar_cache_orderby' => array(),
1638 'ar_cache_set' => array()
1639 );
1640
1641 $this->_reset_run($ar_reset_items);
1642 }
1643
1644 // --------------------------------------------------------------------
1645
1646 /**
1647 * Merge Cache
1648 *
1649 * When called, this function merges any cached AR arrays with
1650 * locally called ones.
1651 *
1652 * @access private
1653 * @return void
1654 */
1655 function _merge_cache()
1656 {
1657 $ar_items = array('select', 'from', 'join', 'where', 'like', 'groupby', 'having', 'orderby', 'set');
1658
1659 foreach ($ar_items as $ar_item)
1660 {
1661 $ar_cache_item = 'ar_cache_'.$ar_item;
1662 $ar_item = 'ar_'.$ar_item;
1663 $this->$ar_item = array_unique(array_merge($this->$ar_item, $this->$ar_cache_item));
1664 }
1665 }
1666
1667 // --------------------------------------------------------------------
1668
1669 /**
1670 * Resets the active record values. Called by the get() function
1671 *
1672 * @access private
1673 * @param array An array of fields to reset
1674 * @return void
1675 */
1676 function _reset_run($ar_reset_items)
1677 {
1678 foreach ($ar_reset_items as $item => $default_value)
1679 {
1680 if (!in_array($item, $this->ar_store_array))
1681 {
1682 $this->$item = $default_value;
1683 }
1684 }
1685 }
1686
1687 // --------------------------------------------------------------------
1688
1689 /**
Derek Allard09de1852007-02-14 01:35:56 +00001690 * Resets the active record values. Called by the get() function
1691 *
1692 * @access private
1693 * @return void
1694 */
1695 function _reset_select()
1696 {
Derek Allard9b3e7b52008-02-04 23:20:34 +00001697 $ar_reset_items = array(
1698 'ar_select' => array(),
1699 'ar_from' => array(),
1700 'ar_join' => array(),
1701 'ar_where' => array(),
1702 'ar_like' => array(),
1703 'ar_groupby' => array(),
1704 'ar_having' => array(),
1705 'ar_orderby' => array(),
1706 'ar_wherein' => array(),
1707 'ar_aliased_tables' => array(),
1708 'ar_distinct' => FALSE,
1709 'ar_limit' => FALSE,
1710 'ar_offset' => FALSE,
1711 'ar_order' => FALSE,
1712 );
1713
1714 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001715 }
1716
1717 // --------------------------------------------------------------------
1718
1719 /**
1720 * Resets the active record "write" values.
1721 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001722 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001723 *
1724 * @access private
1725 * @return void
1726 */
1727 function _reset_write()
Derek Allard9b3e7b52008-02-04 23:20:34 +00001728 {
1729 $ar_reset_items = array(
1730 'ar_set' => array(),
1731 'ar_from' => array(),
1732 'ar_where' => array(),
1733 'ar_like' => array(),
1734 'ar_orderby' => array(),
1735 'ar_limit' => FALSE,
1736 'ar_order' => FALSE
1737 );
1738
1739 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001740 }
1741
1742}
adminac94f382006-09-24 20:28:12 +00001743?>