blob: b2638f25ca6572ca92094f254d8dc9233fd763d6 [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 Allard96863ce2008-04-06 19:20:17 +0000450 if ($v != '')
451 {
452 $v = ' '.$this->escape($v);
453 }
Derek Allard09de1852007-02-14 01:35:56 +0000454 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000455 else
456 {
457 $k = $this->_protect_identifiers($k, TRUE);
458 }
459
Derek Allard09de1852007-02-14 01:35:56 +0000460 $this->ar_where[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000461 if ($this->ar_caching === TRUE)
462 {
463 $this->ar_cache_where[] = $prefix.$k.$v;
464 }
465
Derek Allard09de1852007-02-14 01:35:56 +0000466 }
467 return $this;
468 }
Derek Allard80dd7022007-12-18 23:55:06 +0000469
470 // --------------------------------------------------------------------
471
472 /**
473 * Where_in
474 *
Derek Allardc6935512007-12-19 14:23:19 +0000475 * Generates a WHERE field IN ('item', 'item') SQL query joined with
476 * AND if appropriate
Derek Allard80dd7022007-12-18 23:55:06 +0000477 *
478 * @access public
479 * @param string The field to search
480 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000481
482 * @return object
483 */
484 function where_in($key = NULL, $values = NULL)
485 {
486 return $this->_where_in($key, $values);
487 }
488
489 // --------------------------------------------------------------------
490
491 /**
492 * Where_in_or
493 *
494 * Generates a WHERE field IN ('item', 'item') SQL query joined with
495 * OR if appropriate
496 *
497 * @access public
498 * @param string The field to search
499 * @param array The values searched on
500
501 * @return object
502 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000503 function or_where_in($key = NULL, $values = NULL)
Derek Allardc6935512007-12-19 14:23:19 +0000504 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000505 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000506 }
507
508 // --------------------------------------------------------------------
509
510 /**
511 * Where_not_in
512 *
513 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
514 * with AND if appropriate
515 *
516 * @access public
517 * @param string The field to search
518 * @param array The values searched on
519
520 * @return object
521 */
522 function where_not_in($key = NULL, $values = NULL)
523 {
524 return $this->_where_in($key, $values, TRUE);
525 }
526
527 // --------------------------------------------------------------------
528
529 /**
530 * Where_not_in_or
531 *
532 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
533 * with OR if appropriate
534 *
535 * @access public
536 * @param string The field to search
537 * @param array The values searched on
538
539 * @return object
540 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000541 function or_where_not_in($key = NULL, $values = NULL)
Derek Allardc6935512007-12-19 14:23:19 +0000542 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000543 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000544 }
545
546 // --------------------------------------------------------------------
547
548 /**
549 * Where_in
550 *
551 * Called by where_in, where_in_or, where_not_in, where_not_in_or
552 *
553 * @access public
554 * @param string The field to search
555 * @param array The values searched on
556 * @param boolean If the statement whould be IN or NOT IN
Derek Allard80dd7022007-12-18 23:55:06 +0000557 * @param string
558 * @return object
559 */
Derek Allard15ddc9d2007-12-20 13:54:39 +0000560 function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard80dd7022007-12-18 23:55:06 +0000561 {
562 if ($key === NULL || !is_array($values))
563 {
564 return;
565 }
566
Derek Allardc6935512007-12-19 14:23:19 +0000567 $not = ($not) ? ' NOT ' : '';
Derek Allard80dd7022007-12-18 23:55:06 +0000568
569 foreach ($values as $value)
570 {
571 $this->ar_wherein[] = $this->escape($value);
572 }
573
574 $prefix = (count($this->ar_where) == 0) ? '' : $type;
575
Derek Allard9b3e7b52008-02-04 23:20:34 +0000576 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
577
578 $this->ar_where[] = $where_in;
579 if ($this->ar_caching === TRUE)
580 {
581 $this->ar_cache_where[] = $where_in;
582 }
Derek Allard80dd7022007-12-18 23:55:06 +0000583
Derek Allard8f000212008-01-18 14:45:59 +0000584 // reset the array for multiple calls
585 $this->ar_wherein = array();
Derek Allard80dd7022007-12-18 23:55:06 +0000586 return $this;
587 }
588
Derek Allard09de1852007-02-14 01:35:56 +0000589 // --------------------------------------------------------------------
590
591 /**
592 * Like
593 *
594 * Generates a %LIKE% portion of the query. Separates
595 * multiple calls with AND
596 *
597 * @access public
598 * @param mixed
599 * @param mixed
600 * @return object
601 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000602 function like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000603 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000604 return $this->_like($field, $match, 'AND ', $side);
Derek Allard09de1852007-02-14 01:35:56 +0000605 }
Derek Allarde54e3d22007-12-19 15:53:44 +0000606
607 // --------------------------------------------------------------------
608
609 /**
610 * Not Like
611 *
612 * Generates a NOT LIKE portion of the query. Separates
613 * multiple calls with AND
614 *
615 * @access public
616 * @param mixed
617 * @param mixed
618 * @return object
619 */
620 function not_like($field, $match = '', $side = 'both')
621 {
622 return $this->_like($field, $match, 'AND ', $side, ' NOT');
623 }
624
Derek Allard09de1852007-02-14 01:35:56 +0000625 // --------------------------------------------------------------------
626
627 /**
628 * OR Like
629 *
630 * Generates a %LIKE% portion of the query. Separates
631 * multiple calls with OR
632 *
633 * @access public
634 * @param mixed
635 * @param mixed
636 * @return object
637 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000638 function or_like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000639 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000640 return $this->_like($field, $match, 'OR ', $side);
641 }
642
643 // --------------------------------------------------------------------
644
645 /**
Derek Allarde54e3d22007-12-19 15:53:44 +0000646 * OR Not Like
647 *
648 * Generates a NOT LIKE portion of the query. Separates
649 * multiple calls with OR
650 *
651 * @access public
652 * @param mixed
653 * @param mixed
654 * @return object
655 */
656 function or_not_like($field, $match = '', $side = 'both')
657 {
658 return $this->_like($field, $match, 'OR ', $side, 'NOT ');
659 }
660
661 // --------------------------------------------------------------------
662
663 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000664 * orlike() is an alias of or_like()
665 * this function is here for backwards compatibility, as
666 * orlike() has been deprecated
667 */
668 function orlike($field, $match = '', $side = 'both')
669 {
Derek Allard4a310b72008-01-30 21:32:47 +0000670 return $this->or_like($field, $match, $side);
Derek Allard09de1852007-02-14 01:35:56 +0000671 }
672
673 // --------------------------------------------------------------------
674
675 /**
676 * Like
677 *
678 * Called by like() or orlike()
679 *
680 * @access private
681 * @param mixed
682 * @param mixed
683 * @param string
684 * @return object
685 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000686 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard09de1852007-02-14 01:35:56 +0000687 {
688 if ( ! is_array($field))
689 {
690 $field = array($field => $match);
691 }
692
693 foreach ($field as $k => $v)
Derek Allarde54e3d22007-12-19 15:53:44 +0000694 {
695
Derek Allard39b622d2008-01-16 21:10:09 +0000696 $k = $this->_protect_identifiers($k);
697
Derek Allard09de1852007-02-14 01:35:56 +0000698 $prefix = (count($this->ar_like) == 0) ? '' : $type;
Derek Allarde54e3d22007-12-19 15:53:44 +0000699
Derek Allard09de1852007-02-14 01:35:56 +0000700 $v = $this->escape_str($v);
Derek Allarde54e3d22007-12-19 15:53:44 +0000701
Derek Allard218e2bc2007-12-17 21:18:14 +0000702 if ($side == 'before')
703 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000704 $like_statement = $prefix." $k $not LIKE '%{$v}'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000705 }
706 elseif ($side == 'after')
707 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000708 $like_statement = $prefix." $k $not LIKE '{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000709 }
710 else
711 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000712 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000713 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000714
715 $this->ar_like[] = $like_statement;
716 if ($this->ar_caching === TRUE)
717 {
718 $this->ar_cache_like[] = $like_statement;
719 }
720
Derek Allard09de1852007-02-14 01:35:56 +0000721 }
722 return $this;
723 }
724
725 // --------------------------------------------------------------------
726
727 /**
728 * GROUP BY
729 *
730 * @access public
731 * @param string
732 * @return object
733 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000734 function group_by($by)
Derek Allard09de1852007-02-14 01:35:56 +0000735 {
736 if (is_string($by))
737 {
738 $by = explode(',', $by);
739 }
740
741 foreach ($by as $val)
742 {
743 $val = trim($val);
744
745 if ($val != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000746 {
Derek Allard39b622d2008-01-16 21:10:09 +0000747 $this->ar_groupby[] = $this->_protect_identifiers($val);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000748 if ($this->ar_caching === TRUE)
749 {
750 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
751 }
752 }
Derek Allard09de1852007-02-14 01:35:56 +0000753 }
754 return $this;
755 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000756
757 // --------------------------------------------------------------------
758
759 /**
760 * groupby() is an alias of group_by()
761 * this function is here for backwards compatibility, as
762 * groupby() has been deprecated
763 */
764 function groupby($by)
765 {
766 return $this->group_by($by);
767 }
768
Derek Allard09de1852007-02-14 01:35:56 +0000769 // --------------------------------------------------------------------
770
771 /**
772 * Sets the HAVING value
773 *
774 * Separates multiple calls with AND
775 *
776 * @access public
777 * @param string
778 * @param string
779 * @return object
780 */
Derek Allarde808aac2008-04-06 18:22:00 +0000781 function having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000782 {
Derek Allarde808aac2008-04-06 18:22:00 +0000783 return $this->_having($key, $value, 'AND ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000784 }
Derek Allard43e8cbf2008-03-01 23:14:43 +0000785
786 // --------------------------------------------------------------------
787
788 /**
789 * orhaving() is an alias of or_having()
790 * this function is here for backwards compatibility, as
791 * orhaving() has been deprecated
792 */
793
Derek Allarde808aac2008-04-06 18:22:00 +0000794 function orhaving($key, $value = '', $escape = TRUE)
Derek Allard43e8cbf2008-03-01 23:14:43 +0000795 {
Derek Allarde808aac2008-04-06 18:22:00 +0000796 return $this->or_having($key, $value = '', $escape);
Derek Allard43e8cbf2008-03-01 23:14:43 +0000797 }
Derek Allard09de1852007-02-14 01:35:56 +0000798 // --------------------------------------------------------------------
799
800 /**
801 * Sets the OR HAVING value
802 *
803 * Separates multiple calls with OR
804 *
805 * @access public
806 * @param string
807 * @param string
808 * @return object
809 */
Derek Allarde808aac2008-04-06 18:22:00 +0000810 function or_having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000811 {
Derek Allarde808aac2008-04-06 18:22:00 +0000812 return $this->_having($key, $value, 'OR ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000813 }
814
815 // --------------------------------------------------------------------
816
817 /**
818 * Sets the HAVING values
819 *
820 * Called by having() or orhaving()
821 *
822 * @access private
823 * @param string
Derek Allard9b3e7b52008-02-04 23:20:34 +0000824
Derek Allard09de1852007-02-14 01:35:56 +0000825 * @param string
826 * @return object
827 */
Derek Allarde808aac2008-04-06 18:22:00 +0000828 function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000829 {
830 if ( ! is_array($key))
831 {
832 $key = array($key => $value);
833 }
834
835 foreach ($key as $k => $v)
836 {
837 $prefix = (count($this->ar_having) == 0) ? '' : $type;
Derek Allarde808aac2008-04-06 18:22:00 +0000838
839 if ($escape === TRUE)
840 {
841 $k = $this->_protect_identifiers($k);
842 }
843
Derek Allard09de1852007-02-14 01:35:56 +0000844
845 if ($v != '')
846 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000847 $v = ' '.$this->escape_str($v);
Derek Allard09de1852007-02-14 01:35:56 +0000848 }
849
850 $this->ar_having[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000851 if ($this->ar_caching === TRUE)
852 {
853 $this->ar_cache_having[] = $prefix.$k.$v;
854 }
Derek Allard09de1852007-02-14 01:35:56 +0000855 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000856
Derek Allard09de1852007-02-14 01:35:56 +0000857 return $this;
858 }
859
860 // --------------------------------------------------------------------
861
862 /**
863 * Sets the ORDER BY value
864 *
865 * @access public
866 * @param string
867 * @param string direction: asc or desc
868 * @return object
869 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000870 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000871 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000872 if (strtolower($direction) == 'random')
873 {
874 $orderby = ''; // Random results want or don't need a field name
875 $direction = $this->_random_keyword;
876 }
877 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000878 {
Derek Allard92782492007-08-10 11:26:01 +0000879 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000880 }
881
Derek Allard9b3e7b52008-02-04 23:20:34 +0000882 $orderby_statement = $this->_protect_identifiers($orderby, TRUE).$direction;
883
884 $this->ar_orderby[] = $orderby_statement;
885 if ($this->ar_caching === TRUE)
886 {
887 $this->ar_cache_orderby[] = $orderby_statement;
888 }
889
Derek Allard09de1852007-02-14 01:35:56 +0000890 return $this;
891 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000892
Derek Allard218e2bc2007-12-17 21:18:14 +0000893 // --------------------------------------------------------------------
894
895 /**
896 * orderby() is an alias of order_by()
897 * this function is here for backwards compatibility, as
898 * orderby() has been deprecated
899 */
900 function orderby($orderby, $direction = '')
901 {
902 return $this->order_by($orderby, $direction);
903 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000904
Derek Allard09de1852007-02-14 01:35:56 +0000905 // --------------------------------------------------------------------
906
907 /**
908 * Sets the LIMIT value
909 *
910 * @access public
911 * @param integer the limit value
912 * @param integer the offset value
913 * @return object
914 */
915 function limit($value, $offset = '')
916 {
917 $this->ar_limit = $value;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000918 if ($this->ar_caching === TRUE)
919 {
920 $this->ar_cache_limit[] = $value;
921 }
922
Derek Allard09de1852007-02-14 01:35:56 +0000923 if ($offset != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000924 {
Derek Allard09de1852007-02-14 01:35:56 +0000925 $this->ar_offset = $offset;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000926 if ($this->ar_caching === TRUE)
927 {
928 $this->ar_cache_offset[] = $offset;
929 }
930 }
Derek Allard09de1852007-02-14 01:35:56 +0000931
932 return $this;
933 }
934
935 // --------------------------------------------------------------------
936
937 /**
938 * Sets the OFFSET value
939 *
940 * @access public
941 * @param integer the offset value
942 * @return object
943 */
Derek Allard9b3e7b52008-02-04 23:20:34 +0000944 function offset($offset)
Derek Allard09de1852007-02-14 01:35:56 +0000945 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000946 $this->ar_offset = $offset;
947 if ($this->ar_caching === TRUE)
948 {
949 $this->ar_cache_offset[] = $offset;
950 }
951
Derek Allard09de1852007-02-14 01:35:56 +0000952 return $this;
953 }
954
955 // --------------------------------------------------------------------
956
957 /**
958 * The "set" function. Allows key/value pairs to be set for inserting or updating
959 *
960 * @access public
961 * @param mixed
962 * @param string
Derek Allard39b622d2008-01-16 21:10:09 +0000963 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +0000964 * @return object
965 */
Derek Allard39b622d2008-01-16 21:10:09 +0000966 function set($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000967 {
968 $key = $this->_object_to_array($key);
969
970 if ( ! is_array($key))
971 {
972 $key = array($key => $value);
973 }
974
975 foreach ($key as $k => $v)
976 {
Derek Allard39b622d2008-01-16 21:10:09 +0000977 if ($escape === FALSE)
978 {
979 $this->ar_set[$this->_protect_identifiers($k)] = $v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000980 if ($this->ar_caching === TRUE)
981 {
982 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $v;
983 }
Derek Allard39b622d2008-01-16 21:10:09 +0000984 }
985 else
986 {
987 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000988 if ($this->ar_caching === TRUE)
989 {
990 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $this->escape($v);
991 }
Derek Allard39b622d2008-01-16 21:10:09 +0000992 }
Derek Allard09de1852007-02-14 01:35:56 +0000993 }
994
995 return $this;
996 }
997
998 // --------------------------------------------------------------------
999
1000 /**
1001 * Get
1002 *
1003 * Compiles the select statement based on the other functions called
1004 * and runs the query
1005 *
1006 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +00001007 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +00001008 * @param string the limit clause
1009 * @param string the offset clause
1010 * @return object
1011 */
1012 function get($table = '', $limit = null, $offset = null)
1013 {
1014 if ($table != '')
1015 {
Derek Allard5e128942007-12-28 21:33:03 +00001016 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001017 $this->from($table);
1018 }
1019
1020 if ( ! is_null($limit))
1021 {
1022 $this->limit($limit, $offset);
1023 }
1024
1025 $sql = $this->_compile_select();
1026
1027 $result = $this->query($sql);
1028 $this->_reset_select();
1029 return $result;
1030 }
1031
Derek Allard09de1852007-02-14 01:35:56 +00001032 /**
Derek Allard694b5b82007-12-18 15:58:03 +00001033 * "Count All Results" query
1034 *
1035 * Generates a platform-specific query string that counts all records
1036 * returned by an Active Record query.
1037 *
1038 * @access public
1039 * @param string
1040 * @return string
1041 */
1042 function count_all_results($table = '')
1043 {
1044 if ($table != '')
1045 {
Derek Allard5e128942007-12-28 21:33:03 +00001046 $this->_track_aliases($table);
Derek Allard694b5b82007-12-18 15:58:03 +00001047 $this->from($table);
1048 }
1049
Derek Allard39b622d2008-01-16 21:10:09 +00001050 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
Derek Allard694b5b82007-12-18 15:58:03 +00001051
1052 $query = $this->query($sql);
1053 $this->_reset_select();
1054
1055 if ($query->num_rows() == 0)
1056 {
1057 return '0';
1058 }
1059
1060 $row = $query->row();
1061 return $row->numrows;
1062 }
1063
1064 // --------------------------------------------------------------------
1065
1066 /**
Derek Allard218e2bc2007-12-17 21:18:14 +00001067 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +00001068 *
1069 * Allows the where clause, limit and offset to be added directly
1070 *
1071 * @access public
1072 * @param string the where clause
1073 * @param string the limit clause
1074 * @param string the offset clause
1075 * @return object
1076 */
Derek Allard218e2bc2007-12-17 21:18:14 +00001077 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +00001078 {
1079 if ($table != '')
1080 {
Derek Allard5e128942007-12-28 21:33:03 +00001081 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001082 $this->from($table);
1083 }
1084
1085 if ( ! is_null($where))
1086 {
1087 $this->where($where);
1088 }
1089
1090 if ( ! is_null($limit))
1091 {
1092 $this->limit($limit, $offset);
1093 }
1094
1095 $sql = $this->_compile_select();
1096
1097 $result = $this->query($sql);
1098 $this->_reset_select();
1099 return $result;
1100 }
Derek Allard218e2bc2007-12-17 21:18:14 +00001101
1102 // --------------------------------------------------------------------
1103
1104 /**
1105 * getwhere() is an alias of get_where()
1106 * this function is here for backwards compatibility, as
1107 * getwhere() has been deprecated
1108 */
1109 function getwhere($table = '', $where = null, $limit = null, $offset = null)
1110 {
1111 return $this->get_where($table, $where, $limit, $offset);
1112 }
Derek Allard09de1852007-02-14 01:35:56 +00001113
1114 // --------------------------------------------------------------------
1115
1116 /**
1117 * Insert
1118 *
1119 * Compiles an insert string and runs the query
1120 *
1121 * @access public
1122 * @param string the table to retrieve the results from
1123 * @param array an associative array of insert values
1124 * @return object
1125 */
1126 function insert($table = '', $set = NULL)
1127 {
1128 if ( ! is_null($set))
1129 {
1130 $this->set($set);
1131 }
1132
1133 if (count($this->ar_set) == 0)
1134 {
1135 if ($this->db_debug)
1136 {
1137 return $this->display_error('db_must_use_set');
1138 }
1139 return FALSE;
1140 }
1141
1142 if ($table == '')
1143 {
1144 if ( ! isset($this->ar_from[0]))
1145 {
1146 if ($this->db_debug)
1147 {
1148 return $this->display_error('db_must_set_table');
1149 }
1150 return FALSE;
1151 }
1152
1153 $table = $this->ar_from[0];
1154 }
Derek Allard39b622d2008-01-16 21:10:09 +00001155
1156 $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 +00001157
1158 $this->_reset_write();
1159 return $this->query($sql);
1160 }
1161
1162 // --------------------------------------------------------------------
1163
1164 /**
1165 * Update
1166 *
1167 * Compiles an update string and runs the query
1168 *
1169 * @access public
1170 * @param string the table to retrieve the results from
1171 * @param array an associative array of update values
1172 * @param mixed the where clause
1173 * @return object
1174 */
Derek Allard5e128942007-12-28 21:33:03 +00001175 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001176 {
1177 if ( ! is_null($set))
1178 {
1179 $this->set($set);
1180 }
1181
1182 if (count($this->ar_set) == 0)
1183 {
1184 if ($this->db_debug)
1185 {
1186 return $this->display_error('db_must_use_set');
1187 }
1188 return FALSE;
1189 }
1190
1191 if ($table == '')
1192 {
1193 if ( ! isset($this->ar_from[0]))
1194 {
1195 if ($this->db_debug)
1196 {
1197 return $this->display_error('db_must_set_table');
1198 }
1199 return FALSE;
1200 }
1201
1202 $table = $this->ar_from[0];
1203 }
1204
Derek Allarde77d77c2007-12-19 15:01:55 +00001205 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001206 {
1207 $this->where($where);
1208 }
Derek Allardda6d2402007-12-19 14:49:29 +00001209
Derek Allarde77d77c2007-12-19 15:01:55 +00001210 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +00001211 {
1212 $this->limit($limit);
1213 }
Derek Allard09de1852007-02-14 01:35:56 +00001214
Derek Allard39b622d2008-01-16 21:10:09 +00001215 $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 +00001216
1217 $this->_reset_write();
1218 return $this->query($sql);
1219 }
Derek Allard39b622d2008-01-16 21:10:09 +00001220
1221 // --------------------------------------------------------------------
1222
1223 /**
1224 * Empty Table
1225 *
1226 * Compiles a delete string and runs "DELETE FROM table"
1227 *
1228 * @access public
1229 * @param string the table to empty
1230 * @return object
1231 */
1232 function empty_table($table = '')
1233 {
1234 if ($table == '')
1235 {
1236 if ( ! isset($this->ar_from[0]))
1237 {
1238 if ($this->db_debug)
1239 {
1240 return $this->display_error('db_must_set_table');
1241 }
1242 return FALSE;
1243 }
1244
1245 $table = $this->ar_from[0];
1246 }
1247 else
1248 {
1249 $table = $this->_protect_identifiers($this->dbprefix.$table);
1250 }
1251
1252
1253 $sql = $this->_delete($table);
1254
1255 $this->_reset_write();
1256
1257 return $this->query($sql);
1258 }
1259
1260 // --------------------------------------------------------------------
1261
1262 /**
1263 * Truncate
1264 *
1265 * Compiles a truncate string and runs the query
1266 * If the database does not support the truncate() command
1267 * This function maps to "DELETE FROM table"
1268 *
1269 * @access public
1270 * @param string the table to truncate
1271 * @return object
1272 */
1273 function truncate($table = '')
1274 {
1275 if ($table == '')
1276 {
1277 if ( ! isset($this->ar_from[0]))
1278 {
1279 if ($this->db_debug)
1280 {
1281 return $this->display_error('db_must_set_table');
1282 }
1283 return FALSE;
1284 }
1285
1286 $table = $this->ar_from[0];
1287 }
1288 else
1289 {
1290 $table = $this->_protect_identifiers($this->dbprefix.$table);
1291 }
1292
1293
1294 $sql = $this->_truncate($table);
1295
1296 $this->_reset_write();
1297
1298 return $this->query($sql);
1299 }
Derek Allard09de1852007-02-14 01:35:56 +00001300
1301 // --------------------------------------------------------------------
1302
1303 /**
1304 * Delete
1305 *
1306 * Compiles a delete string and runs the query
1307 *
1308 * @access public
Derek Allard41f60d42007-12-20 20:09:22 +00001309 * @param mixed the table(s) to delete from. String or array
Derek Allard09de1852007-02-14 01:35:56 +00001310 * @param mixed the where clause
Derek Allard41f60d42007-12-20 20:09:22 +00001311 * @param mixed the limit clause
1312 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +00001313 * @return object
1314 */
Derek Allard41f60d42007-12-20 20:09:22 +00001315 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +00001316 {
1317 if ($table == '')
1318 {
1319 if ( ! isset($this->ar_from[0]))
1320 {
1321 if ($this->db_debug)
1322 {
1323 return $this->display_error('db_must_set_table');
1324 }
1325 return FALSE;
1326 }
Derek Allard39b622d2008-01-16 21:10:09 +00001327
Derek Allard09de1852007-02-14 01:35:56 +00001328 $table = $this->ar_from[0];
1329 }
Derek Allard39b622d2008-01-16 21:10:09 +00001330 elseif (is_array($table))
Derek Allard41f60d42007-12-20 20:09:22 +00001331 {
1332 foreach($table as $single_table)
1333 {
Derek Allard39b622d2008-01-16 21:10:09 +00001334 $this->delete($single_table, $where, $limit, FALSE);
Derek Allard41f60d42007-12-20 20:09:22 +00001335 }
Derek Allard39b622d2008-01-16 21:10:09 +00001336
Derek Allard41f60d42007-12-20 20:09:22 +00001337 $this->_reset_write();
1338 return;
1339 }
Derek Allard39b622d2008-01-16 21:10:09 +00001340 else
1341 {
1342 $table = $this->_protect_identifiers($this->dbprefix.$table);
1343 }
Derek Allard41f60d42007-12-20 20:09:22 +00001344
Derek Allard09de1852007-02-14 01:35:56 +00001345 if ($where != '')
1346 {
1347 $this->where($where);
1348 }
1349
Derek Allarde77d77c2007-12-19 15:01:55 +00001350 if ($limit != NULL)
1351 {
1352 $this->limit($limit);
1353 }
1354
Derek Allard39b622d2008-01-16 21:10:09 +00001355 if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
Derek Allard09de1852007-02-14 01:35:56 +00001356 {
1357 if ($this->db_debug)
1358 {
1359 return $this->display_error('db_del_must_use_where');
1360 }
Derek Allard39b622d2008-01-16 21:10:09 +00001361
Derek Allard09de1852007-02-14 01:35:56 +00001362 return FALSE;
1363 }
Derek Allard41f60d42007-12-20 20:09:22 +00001364
Derek Allard39b622d2008-01-16 21:10:09 +00001365 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001366
Derek Allard41f60d42007-12-20 20:09:22 +00001367 if ($reset_data)
1368 {
1369 $this->_reset_write();
1370 }
Derek Allard39b622d2008-01-16 21:10:09 +00001371
Derek Allard09de1852007-02-14 01:35:56 +00001372 return $this->query($sql);
1373 }
Derek Allard15ddc9d2007-12-20 13:54:39 +00001374
Derek Allard09de1852007-02-14 01:35:56 +00001375 // --------------------------------------------------------------------
1376
1377 /**
1378 * Use Table - DEPRECATED
1379 *
1380 * @deprecated use $this->db->from instead
1381 */
1382 function use_table($table)
1383 {
1384 return $this->from($table);
Derek Allard09de1852007-02-14 01:35:56 +00001385 }
1386
1387 // --------------------------------------------------------------------
1388
1389 /**
Derek Allard09de1852007-02-14 01:35:56 +00001390 * Tests whether the string has an SQL operator
1391 *
1392 * @access private
1393 * @param string
1394 * @return bool
1395 */
1396 function _has_operator($str)
1397 {
1398 $str = trim($str);
1399 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
1400 {
1401 return FALSE;
1402 }
1403
1404 return TRUE;
1405 }
1406
1407 // --------------------------------------------------------------------
1408
1409 /**
Derek Allard5e128942007-12-28 21:33:03 +00001410 * Track Aliases
1411 *
1412 * Used to track SQL statements written with aliased tables.
1413 *
1414 * @access private
1415 * @param string The table to inspect
1416 * @return string
1417 */
1418 function _track_aliases($table)
1419 {
1420 // if a table alias is used we can recognize it by a space
1421 if (strpos($table, " ") !== FALSE)
1422 {
1423 // if the alias is written with the AS keyowrd, get it out
Derek Allard39b622d2008-01-16 21:10:09 +00001424 $table = preg_replace('/ AS /i', ' ', $table);
Derek Allard5e128942007-12-28 21:33:03 +00001425
Derek Allard39b622d2008-01-16 21:10:09 +00001426 $this->ar_aliased_tables[] = trim(strrchr($table, " "));
Derek Allard5e128942007-12-28 21:33:03 +00001427 }
1428
1429 return $this->dbprefix.$table;
1430 }
1431
1432 // --------------------------------------------------------------------
1433
1434 /**
1435 * Filter Table Aliases
1436 *
1437 * Intelligently removes database prefixes from aliased tables
1438 *
1439 * @access private
1440 * @param array An array of compiled SQL
1441 * @return array Cleaned up statement with aliases accounted for
1442 */
1443 function _filter_table_aliases($statements)
1444 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001445
Derek Allard39b622d2008-01-16 21:10:09 +00001446 foreach ($statements as $k => $v)
Derek Allard5e128942007-12-28 21:33:03 +00001447 {
Derek Allard39b622d2008-01-16 21:10:09 +00001448 foreach ($this->ar_aliased_tables as $table)
Derek Allard5e128942007-12-28 21:33:03 +00001449 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001450 $statements[$k] = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $statements[$k]); // makes `table.field`
1451 $statements[$k] = str_replace($this->dbprefix.$table.'.', $table.'.', $statements[$k]);
Derek Allard5e128942007-12-28 21:33:03 +00001452 }
Derek Allard5e128942007-12-28 21:33:03 +00001453 }
Derek Allard39b622d2008-01-16 21:10:09 +00001454 return $statements;
Derek Allard5e128942007-12-28 21:33:03 +00001455 }
1456
1457 // --------------------------------------------------------------------
1458
1459 /**
Derek Allard09de1852007-02-14 01:35:56 +00001460 * Compile the SELECT statement
1461 *
1462 * Generates a query string based on which functions were used.
1463 * Should not be called directly. The get() function calls it.
1464 *
1465 * @access private
1466 * @return string
1467 */
Derek Allard694b5b82007-12-18 15:58:03 +00001468 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001469 {
Derek Allard78255262008-02-06 13:54:23 +00001470 $this->_merge_cache();
Derek Allard9b3e7b52008-02-04 23:20:34 +00001471
Derek Allard09de1852007-02-14 01:35:56 +00001472 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
1473
1474 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->ar_select);
1475
Derek Allard694b5b82007-12-18 15:58:03 +00001476 if ($select_override !== FALSE)
1477 {
1478 $sql = $select_override;
1479 }
1480
Derek Allard09de1852007-02-14 01:35:56 +00001481 if (count($this->ar_from) > 0)
1482 {
1483 $sql .= "\nFROM ";
Derek Jonesc6ad0232008-01-29 18:44:54 +00001484 $sql .= $this->_from_tables($this->ar_from);
Derek Allard09de1852007-02-14 01:35:56 +00001485 }
1486
1487 if (count($this->ar_join) > 0)
Derek Allard5e128942007-12-28 21:33:03 +00001488 {
Derek Allard09de1852007-02-14 01:35:56 +00001489 $sql .= "\n";
Derek Allard5e128942007-12-28 21:33:03 +00001490
1491 // special consideration for table aliases
1492 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1493 {
1494 $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
1495 }
1496 else
1497 {
1498 $sql .= implode("\n", $this->ar_join);
1499 }
1500
Derek Allard09de1852007-02-14 01:35:56 +00001501 }
1502
1503 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1504 {
1505 $sql .= "\nWHERE ";
1506 }
1507
1508 $sql .= implode("\n", $this->ar_where);
1509
1510 if (count($this->ar_like) > 0)
1511 {
1512 if (count($this->ar_where) > 0)
1513 {
1514 $sql .= " AND ";
1515 }
1516
1517 $sql .= implode("\n", $this->ar_like);
1518 }
1519
1520 if (count($this->ar_groupby) > 0)
1521 {
Derek Allard5e128942007-12-28 21:33:03 +00001522
Derek Allard09de1852007-02-14 01:35:56 +00001523 $sql .= "\nGROUP BY ";
Derek Allard5e128942007-12-28 21:33:03 +00001524
1525 // special consideration for table aliases
1526 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1527 {
1528 $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
1529 }
1530 else
1531 {
1532 $sql .= implode(', ', $this->ar_groupby);
1533 }
Derek Allard09de1852007-02-14 01:35:56 +00001534 }
1535
1536 if (count($this->ar_having) > 0)
1537 {
1538 $sql .= "\nHAVING ";
1539 $sql .= implode("\n", $this->ar_having);
1540 }
1541
1542 if (count($this->ar_orderby) > 0)
1543 {
1544 $sql .= "\nORDER BY ";
1545 $sql .= implode(', ', $this->ar_orderby);
1546
1547 if ($this->ar_order !== FALSE)
1548 {
1549 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1550 }
1551 }
1552
1553 if (is_numeric($this->ar_limit))
1554 {
1555 $sql .= "\n";
1556 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1557 }
1558
1559 return $sql;
1560 }
1561
1562 // --------------------------------------------------------------------
1563
1564 /**
1565 * Object to Array
1566 *
1567 * Takes an object as input and converts the class variables to array key/vals
1568 *
1569 * @access public
1570 * @param object
1571 * @return array
1572 */
1573 function _object_to_array($object)
1574 {
1575 if ( ! is_object($object))
1576 {
1577 return $object;
1578 }
1579
1580 $array = array();
1581 foreach (get_object_vars($object) as $key => $val)
1582 {
Derek Allard848b7762007-12-31 16:43:05 +00001583 // There are some built in keys we need to ignore for this conversion
1584 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
1585
Derek Allard09de1852007-02-14 01:35:56 +00001586 {
1587 $array[$key] = $val;
1588 }
1589 }
1590
1591 return $array;
1592 }
1593
1594 // --------------------------------------------------------------------
1595
1596 /**
Derek Allard9b3e7b52008-02-04 23:20:34 +00001597 * Start Cache
1598 *
1599 * Starts AR caching
1600 *
1601 * @access public
1602 * @return void
1603 */
1604 function start_cache()
1605 {
1606 $this->ar_caching = TRUE;
1607 }
1608
1609 // --------------------------------------------------------------------
1610
1611 /**
1612 * Stop Cache
1613 *
1614 * Stops AR caching
1615 *
1616 * @access public
1617 * @return void
1618 */
1619 function stop_cache()
1620 {
1621 $this->ar_caching = FALSE;
1622 }
1623
1624
1625 // --------------------------------------------------------------------
1626
1627 /**
1628 * Flush Cache
1629 *
1630 * Empties the AR cache
1631 *
1632 * @access public
1633 * @return void
1634 */
1635 function flush_cache()
1636 {
1637 $ar_reset_items = array(
1638 'ar_cache_select' => array(),
1639 'ar_cache_from' => array(),
1640 'ar_cache_join' => array(),
1641 'ar_cache_where' => array(),
1642 'ar_cache_like' => array(),
1643 'ar_cache_groupby' => array(),
1644 'ar_cache_having' =>array(),
1645 'ar_cache_orderby' => array(),
1646 'ar_cache_set' => array()
1647 );
1648
1649 $this->_reset_run($ar_reset_items);
1650 }
1651
1652 // --------------------------------------------------------------------
1653
1654 /**
1655 * Merge Cache
1656 *
1657 * When called, this function merges any cached AR arrays with
1658 * locally called ones.
1659 *
1660 * @access private
1661 * @return void
1662 */
1663 function _merge_cache()
1664 {
1665 $ar_items = array('select', 'from', 'join', 'where', 'like', 'groupby', 'having', 'orderby', 'set');
1666
1667 foreach ($ar_items as $ar_item)
1668 {
1669 $ar_cache_item = 'ar_cache_'.$ar_item;
1670 $ar_item = 'ar_'.$ar_item;
1671 $this->$ar_item = array_unique(array_merge($this->$ar_item, $this->$ar_cache_item));
1672 }
1673 }
1674
1675 // --------------------------------------------------------------------
1676
1677 /**
1678 * Resets the active record values. Called by the get() function
1679 *
1680 * @access private
1681 * @param array An array of fields to reset
1682 * @return void
1683 */
1684 function _reset_run($ar_reset_items)
1685 {
1686 foreach ($ar_reset_items as $item => $default_value)
1687 {
1688 if (!in_array($item, $this->ar_store_array))
1689 {
1690 $this->$item = $default_value;
1691 }
1692 }
1693 }
1694
1695 // --------------------------------------------------------------------
1696
1697 /**
Derek Allard09de1852007-02-14 01:35:56 +00001698 * Resets the active record values. Called by the get() function
1699 *
1700 * @access private
1701 * @return void
1702 */
1703 function _reset_select()
1704 {
Derek Allard9b3e7b52008-02-04 23:20:34 +00001705 $ar_reset_items = array(
1706 'ar_select' => array(),
1707 'ar_from' => array(),
1708 'ar_join' => array(),
1709 'ar_where' => array(),
1710 'ar_like' => array(),
1711 'ar_groupby' => array(),
1712 'ar_having' => array(),
1713 'ar_orderby' => array(),
1714 'ar_wherein' => array(),
1715 'ar_aliased_tables' => array(),
1716 'ar_distinct' => FALSE,
1717 'ar_limit' => FALSE,
1718 'ar_offset' => FALSE,
1719 'ar_order' => FALSE,
1720 );
1721
1722 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001723 }
1724
1725 // --------------------------------------------------------------------
1726
1727 /**
1728 * Resets the active record "write" values.
1729 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001730 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001731 *
1732 * @access private
1733 * @return void
1734 */
1735 function _reset_write()
Derek Allard9b3e7b52008-02-04 23:20:34 +00001736 {
1737 $ar_reset_items = array(
1738 'ar_set' => array(),
1739 'ar_from' => array(),
1740 'ar_where' => array(),
1741 'ar_like' => array(),
1742 'ar_orderby' => array(),
1743 'ar_limit' => FALSE,
1744 'ar_order' => FALSE
1745 );
1746
1747 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001748 }
1749
1750}
adminac94f382006-09-24 20:28:12 +00001751?>