blob: f4c13cc42bc000e39eba131120d804115f7406b0 [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard09de1852007-02-14 01:35:56 +00002/**
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
Rick Ellis37b3ecf2008-09-12 23:34:18 +00009 * @copyright Copyright (c) 2008, 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
Rick Ellis59523592008-10-17 04:07:40 +000031 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();
44 var $ar_wherein = array();
Derek Allard5e128942007-12-28 21:33:03 +000045 var $ar_aliased_tables = array();
Rick Ellis59523592008-10-17 04:07:40 +000046 var $ar_store_array = array();
47
Derek Allard9b3e7b52008-02-04 23:20:34 +000048 // Active Record Caching variables
Rick Ellis59523592008-10-17 04:07:40 +000049 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();
Derek Allard9b3e7b52008-02-04 23:20:34 +000062
Derek Allard09de1852007-02-14 01:35:56 +000063
Derek Allard3b118682008-01-22 23:44:32 +000064 // --------------------------------------------------------------------
65
66 /**
Derek Allard09de1852007-02-14 01:35:56 +000067 * Select
68 *
69 * Generates the SELECT portion of the query
70 *
71 * @access public
72 * @param string
73 * @return object
74 */
Rick Ellis59523592008-10-17 04:07:40 +000075 function select($select = '*', $escape = NULL)
Derek Allard09de1852007-02-14 01:35:56 +000076 {
Rick Ellis59523592008-10-17 04:07:40 +000077 // Set the global value if this was sepecified
78 if (is_bool($escape))
79 {
80 $this->_protect_identifiers = $escape;
81 }
82
Derek Allard09de1852007-02-14 01:35:56 +000083 if (is_string($select))
84 {
Rick Ellis59523592008-10-17 04:07:40 +000085 $select = explode(',', $select);
Derek Allard09de1852007-02-14 01:35:56 +000086 }
Derek Allard5fe155e2008-05-12 19:14:57 +000087
Derek Allard09de1852007-02-14 01:35:56 +000088 foreach ($select as $val)
89 {
90 $val = trim($val);
Derek Allard39b622d2008-01-16 21:10:09 +000091
Derek Allard09de1852007-02-14 01:35:56 +000092 if ($val != '')
Derek Allard39b622d2008-01-16 21:10:09 +000093 {
Derek Allard09de1852007-02-14 01:35:56 +000094 $this->ar_select[] = $val;
Rick Ellis59523592008-10-17 04:07:40 +000095
Derek Allard9b3e7b52008-02-04 23:20:34 +000096 if ($this->ar_caching === TRUE)
97 {
98 $this->ar_cache_select[] = $val;
99 }
Derek Allard39b622d2008-01-16 21:10:09 +0000100 }
Derek Allard09de1852007-02-14 01:35:56 +0000101 }
102 return $this;
103 }
Derek Allard39b622d2008-01-16 21:10:09 +0000104
105 // --------------------------------------------------------------------
106
107 /**
108 * Select Max
109 *
110 * Generates a SELECT MAX(field) portion of a query
111 *
112 * @access public
113 * @param string the field
114 * @param string an alias
115 * @return object
116 */
Rick Ellis59523592008-10-17 04:07:40 +0000117 function select_max($select = '', $alias = '')
Derek Allard39b622d2008-01-16 21:10:09 +0000118 {
Rick Ellis59523592008-10-17 04:07:40 +0000119 return $this->_max_min_avg_sum($select, $alias, 'MAX');
Derek Allard39b622d2008-01-16 21:10:09 +0000120 }
Rick Ellis59523592008-10-17 04:07:40 +0000121
Derek Allard39b622d2008-01-16 21:10:09 +0000122 // --------------------------------------------------------------------
123
124 /**
125 * Select Min
126 *
127 * Generates a SELECT MIN(field) portion of a query
128 *
129 * @access public
130 * @param string the field
131 * @param string an alias
132 * @return object
133 */
Rick Ellis59523592008-10-17 04:07:40 +0000134 function select_min($select = '', $alias = '')
Derek Allard39b622d2008-01-16 21:10:09 +0000135 {
Rick Ellis59523592008-10-17 04:07:40 +0000136 return $this->_max_min_avg_sum($select, $alias, 'MIN');
Derek Allard39b622d2008-01-16 21:10:09 +0000137 }
138
139 // --------------------------------------------------------------------
140
141 /**
142 * Select Average
143 *
144 * Generates a SELECT AVG(field) portion of a query
145 *
146 * @access public
147 * @param string the field
148 * @param string an alias
149 * @return object
150 */
Rick Ellis59523592008-10-17 04:07:40 +0000151 function select_avg($select = '', $alias = '')
Derek Allard39b622d2008-01-16 21:10:09 +0000152 {
Rick Ellis59523592008-10-17 04:07:40 +0000153 return $this->_max_min_avg_sum($select, $alias, 'AVG');
Derek Allard39b622d2008-01-16 21:10:09 +0000154 }
155
156 // --------------------------------------------------------------------
Derek Allard5fe155e2008-05-12 19:14:57 +0000157
Derek Allard39b622d2008-01-16 21:10:09 +0000158 /**
159 * Select Sum
160 *
161 * Generates a SELECT SUM(field) portion of a query
162 *
163 * @access public
164 * @param string the field
165 * @param string an alias
166 * @return object
167 */
Rick Ellis59523592008-10-17 04:07:40 +0000168 function select_sum($select = '', $alias = '')
169 {
170 return $this->_max_min_avg_sum($select, $alias, 'SUM');
171 }
172
173 // --------------------------------------------------------------------
174
175 /**
176 * Processing Function for the four functions above:
177 *
178 * select_max()
179 * select_min()
180 * select_avg()
181 * select_sum()
182 *
183 * @access public
184 * @param string the field
185 * @param string an alias
186 * @return object
187 */
188 function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard39b622d2008-01-16 21:10:09 +0000189 {
Derek Jones0b59f272008-05-13 04:22:33 +0000190 if ( ! is_string($select) OR $select == '')
Derek Allard39b622d2008-01-16 21:10:09 +0000191 {
192 $this->display_error('db_invalid_query');
193 }
Derek Allard39b622d2008-01-16 21:10:09 +0000194
Rick Ellis59523592008-10-17 04:07:40 +0000195 $type = strtoupper($type);
196
197 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
198 {
199 show_error('Invalid function type: '.$type);
200 }
201
202 if ($alias == '')
203 {
204 $alias = $this->_create_alias_from_table(trim($select));
205 }
206
207 $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$alias;
Derek Allard39b622d2008-01-16 21:10:09 +0000208
209 $this->ar_select[] = $sql;
Rick Ellis59523592008-10-17 04:07:40 +0000210
Derek Allard9b3e7b52008-02-04 23:20:34 +0000211 if ($this->ar_caching === TRUE)
212 {
213 $this->ar_cache_select[] = $sql;
214 }
Rick Ellis59523592008-10-17 04:07:40 +0000215
Derek Allard39b622d2008-01-16 21:10:09 +0000216 return $this;
217 }
218
Derek Allard09de1852007-02-14 01:35:56 +0000219 // --------------------------------------------------------------------
220
221 /**
Rick Ellis59523592008-10-17 04:07:40 +0000222 * Determines the alias name based on the table
223 *
224 * @access private
225 * @param string
226 * @return string
227 */
228 function _create_alias_from_table($item)
229 {
230 if (strpos($item, '.') !== FALSE)
231 {
232 return end(explode('.', $item));
233 }
234
235 return $item;
236 }
237
238 // --------------------------------------------------------------------
239
240 /**
Derek Allard09de1852007-02-14 01:35:56 +0000241 * DISTINCT
242 *
243 * Sets a flag which tells the query string compiler to add DISTINCT
244 *
245 * @access public
246 * @param bool
247 * @return object
248 */
249 function distinct($val = TRUE)
250 {
251 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
252 return $this;
253 }
254
255 // --------------------------------------------------------------------
256
257 /**
258 * From
259 *
260 * Generates the FROM portion of the query
261 *
262 * @access public
263 * @param mixed can be a string or array
264 * @return object
265 */
266 function from($from)
267 {
268 foreach ((array)$from as $val)
269 {
Rick Ellis59523592008-10-17 04:07:40 +0000270 // Extract any aliases that might exist. We use this information
271 // in the _protect_identifiers to know whether to add a table prefix
272 $this->_track_aliases($val);
273
274 $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
275
Derek Allard9b3e7b52008-02-04 23:20:34 +0000276 if ($this->ar_caching === TRUE)
277 {
Rick Ellis59523592008-10-17 04:07:40 +0000278 $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000279 }
Derek Allard09de1852007-02-14 01:35:56 +0000280 }
Derek Allard5e128942007-12-28 21:33:03 +0000281
Derek Allard09de1852007-02-14 01:35:56 +0000282 return $this;
283 }
Derek Allard5fe155e2008-05-12 19:14:57 +0000284
Derek Allard09de1852007-02-14 01:35:56 +0000285 // --------------------------------------------------------------------
286
287 /**
288 * Join
289 *
290 * Generates the JOIN portion of the query
291 *
292 * @access public
293 * @param string
294 * @param string the join condition
295 * @param string the type of join
296 * @return object
297 */
298 function join($table, $cond, $type = '')
299 {
300 if ($type != '')
301 {
302 $type = strtoupper(trim($type));
303
Rick Ellis59523592008-10-17 04:07:40 +0000304 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
Derek Allard09de1852007-02-14 01:35:56 +0000305 {
306 $type = '';
307 }
308 else
309 {
310 $type .= ' ';
311 }
312 }
313
Rick Ellis59523592008-10-17 04:07:40 +0000314 // Extract any aliases that might exist. We use this information
315 // in the _protect_identifiers to know whether to add a table prefix
316 $this->_track_aliases($table);
317
318 // Strip apart the condition and protect the identifiers
319 if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
Derek Allard09de1852007-02-14 01:35:56 +0000320 {
Rick Ellis59523592008-10-17 04:07:40 +0000321 $match[1] = $this->_protect_identifiers($match[1]);
322 $match[3] = $this->_protect_identifiers($match[3]);
323
324 $cond = $match[1].$match[2].$match[3];
Derek Allard5fe155e2008-05-12 19:14:57 +0000325 }
Rick Ellis59523592008-10-17 04:07:40 +0000326
327 // Assemble the JOIN statement
328 $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000329
330 $this->ar_join[] = $join;
331 if ($this->ar_caching === TRUE)
332 {
333 $this->ar_cache_join[] = $join;
334 }
335
Derek Allard09de1852007-02-14 01:35:56 +0000336 return $this;
337 }
Derek Allard5fe155e2008-05-12 19:14:57 +0000338
Derek Allard09de1852007-02-14 01:35:56 +0000339 // --------------------------------------------------------------------
340
341 /**
342 * Where
343 *
344 * Generates the WHERE portion of the query. Separates
345 * multiple calls with AND
346 *
347 * @access public
348 * @param mixed
349 * @param mixed
350 * @return object
351 */
Derek Allard39b622d2008-01-16 21:10:09 +0000352 function where($key, $value = NULL, $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000353 {
Derek Allard39b622d2008-01-16 21:10:09 +0000354 return $this->_where($key, $value, 'AND ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000355 }
356
357 // --------------------------------------------------------------------
358
359 /**
360 * OR Where
361 *
362 * Generates the WHERE portion of the query. Separates
363 * multiple calls with OR
364 *
365 * @access public
366 * @param mixed
367 * @param mixed
368 * @return object
369 */
Derek Allard39b622d2008-01-16 21:10:09 +0000370 function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000371 {
Derek Allard39b622d2008-01-16 21:10:09 +0000372 return $this->_where($key, $value, 'OR ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000373 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000374
375 // --------------------------------------------------------------------
376
377 /**
378 * orwhere() is an alias of or_where()
379 * this function is here for backwards compatibility, as
380 * orwhere() has been deprecated
381 */
Derek Allard39b622d2008-01-16 21:10:09 +0000382 function orwhere($key, $value = NULL, $escape = TRUE)
Derek Allard218e2bc2007-12-17 21:18:14 +0000383 {
Derek Allard39b622d2008-01-16 21:10:09 +0000384 return $this->or_where($key, $value, $escape);
Derek Allard218e2bc2007-12-17 21:18:14 +0000385 }
Derek Allard67b44ed2008-01-12 16:18:02 +0000386
387 // --------------------------------------------------------------------
388
389 /**
Derek Allard09de1852007-02-14 01:35:56 +0000390 * Where
391 *
392 * Called by where() or orwhere()
393 *
394 * @access private
395 * @param mixed
396 * @param mixed
397 * @param string
398 * @return object
399 */
Rick Ellis59523592008-10-17 04:07:40 +0000400 function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard09de1852007-02-14 01:35:56 +0000401 {
Derek Jones0b59f272008-05-13 04:22:33 +0000402 if ( ! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000403 {
404 $key = array($key => $value);
405 }
Rick Ellis59523592008-10-17 04:07:40 +0000406
407 // If the escape value was not set will will base it on the global setting
408 if ( ! is_bool($escape))
409 {
410 $escape = $this->_protect_identifiers;
411 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000412
Derek Allard09de1852007-02-14 01:35:56 +0000413 foreach ($key as $k => $v)
414 {
415 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Derek Allard15ddc9d2007-12-20 13:54:39 +0000416
Derek Allardd8364c42008-06-04 17:01:00 +0000417 if (is_null($v) && ! $this->_has_operator($k))
Derek Allard15ddc9d2007-12-20 13:54:39 +0000418 {
419 // value appears not to have been set, assign the test to IS NULL
420 $k .= ' IS NULL';
421 }
Derek Allard09de1852007-02-14 01:35:56 +0000422
Derek Jones0b59f272008-05-13 04:22:33 +0000423 if ( ! is_null($v))
Derek Allard09de1852007-02-14 01:35:56 +0000424 {
Derek Allard39b622d2008-01-16 21:10:09 +0000425 if ($escape === TRUE)
426 {
Rick Ellis59523592008-10-17 04:07:40 +0000427 $k = $this->_protect_identifiers($k, FALSE, $escape);
Derek Allardd8364c42008-06-04 17:01:00 +0000428
429 $v = ' '.$this->escape($v);
Derek Allard39b622d2008-01-16 21:10:09 +0000430 }
431
Derek Jones0b59f272008-05-13 04:22:33 +0000432 if ( ! $this->_has_operator($k))
Derek Allard09de1852007-02-14 01:35:56 +0000433 {
434 $k .= ' =';
435 }
Derek Allard09de1852007-02-14 01:35:56 +0000436 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000437 else
438 {
Rick Ellis59523592008-10-17 04:07:40 +0000439 $k = $this->_protect_identifiers($k, FALSE, $escape);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000440 }
441
Derek Allard09de1852007-02-14 01:35:56 +0000442 $this->ar_where[] = $prefix.$k.$v;
Derek Allardd8364c42008-06-04 17:01:00 +0000443
Derek Allard9b3e7b52008-02-04 23:20:34 +0000444 if ($this->ar_caching === TRUE)
445 {
446 $this->ar_cache_where[] = $prefix.$k.$v;
447 }
448
Derek Allard09de1852007-02-14 01:35:56 +0000449 }
Derek Jones7e98a272008-06-04 17:05:44 +0000450
Derek Allard09de1852007-02-14 01:35:56 +0000451 return $this;
452 }
Derek Allard80dd7022007-12-18 23:55:06 +0000453
454 // --------------------------------------------------------------------
455
456 /**
457 * Where_in
458 *
Derek Allardc6935512007-12-19 14:23:19 +0000459 * Generates a WHERE field IN ('item', 'item') SQL query joined with
460 * AND if appropriate
Derek Allard80dd7022007-12-18 23:55:06 +0000461 *
462 * @access public
463 * @param string The field to search
464 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000465 * @return object
466 */
467 function where_in($key = NULL, $values = NULL)
Rick Ellis59523592008-10-17 04:07:40 +0000468 {
Derek Allardc6935512007-12-19 14:23:19 +0000469 return $this->_where_in($key, $values);
470 }
471
472 // --------------------------------------------------------------------
473
474 /**
475 * Where_in_or
476 *
477 * Generates a WHERE field IN ('item', 'item') SQL query joined with
478 * OR if appropriate
479 *
480 * @access public
481 * @param string The field to search
482 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000483 * @return object
484 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000485 function or_where_in($key = NULL, $values = NULL)
Derek Allard5fe155e2008-05-12 19:14:57 +0000486 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000487 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000488 }
489
490 // --------------------------------------------------------------------
491
492 /**
493 * Where_not_in
494 *
495 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
496 * with AND if appropriate
497 *
498 * @access public
499 * @param string The field to search
500 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000501 * @return object
502 */
503 function where_not_in($key = NULL, $values = NULL)
Derek Allard5fe155e2008-05-12 19:14:57 +0000504 {
Derek Allardc6935512007-12-19 14:23:19 +0000505 return $this->_where_in($key, $values, TRUE);
506 }
507
508 // --------------------------------------------------------------------
509
510 /**
511 * Where_not_in_or
512 *
513 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
514 * with OR if appropriate
515 *
516 * @access public
517 * @param string The field to search
518 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000519 * @return object
520 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000521 function or_where_not_in($key = NULL, $values = NULL)
Derek Allard5fe155e2008-05-12 19:14:57 +0000522 {
Derek Jonesd0072432008-05-07 22:06:51 +0000523 return $this->_where_in($key, $values, TRUE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000524 }
525
526 // --------------------------------------------------------------------
527
528 /**
529 * Where_in
530 *
531 * Called by where_in, where_in_or, where_not_in, where_not_in_or
532 *
533 * @access public
534 * @param string The field to search
535 * @param array The values searched on
Rick Ellis59523592008-10-17 04:07:40 +0000536 * @param boolean If the statement would be IN or NOT IN
Derek Allard80dd7022007-12-18 23:55:06 +0000537 * @param string
538 * @return object
539 */
Derek Allard15ddc9d2007-12-20 13:54:39 +0000540 function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard5fe155e2008-05-12 19:14:57 +0000541 {
Rick Ellis59523592008-10-17 04:07:40 +0000542 if ($key === NULL OR $values === NULL)
Derek Allard80dd7022007-12-18 23:55:06 +0000543 {
544 return;
545 }
Rick Ellis59523592008-10-17 04:07:40 +0000546
547 if ( ! is_array($values))
548 {
549 $values = array($values);
550 }
551
552 $not = ($not) ? ' NOT' : '';
Derek Allard80dd7022007-12-18 23:55:06 +0000553
554 foreach ($values as $value)
555 {
556 $this->ar_wherein[] = $this->escape($value);
557 }
558
559 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Derek Allard5fe155e2008-05-12 19:14:57 +0000560
Derek Allard9b3e7b52008-02-04 23:20:34 +0000561 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
562
563 $this->ar_where[] = $where_in;
564 if ($this->ar_caching === TRUE)
565 {
566 $this->ar_cache_where[] = $where_in;
567 }
Derek Allard80dd7022007-12-18 23:55:06 +0000568
Derek Allard8f000212008-01-18 14:45:59 +0000569 // reset the array for multiple calls
570 $this->ar_wherein = array();
Derek Allard80dd7022007-12-18 23:55:06 +0000571 return $this;
572 }
573
Derek Allard09de1852007-02-14 01:35:56 +0000574 // --------------------------------------------------------------------
575
576 /**
577 * Like
578 *
579 * Generates a %LIKE% portion of the query. Separates
580 * multiple calls with AND
581 *
582 * @access public
583 * @param mixed
584 * @param mixed
585 * @return object
586 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000587 function like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000588 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000589 return $this->_like($field, $match, 'AND ', $side);
Derek Allard09de1852007-02-14 01:35:56 +0000590 }
Derek Allarde54e3d22007-12-19 15:53:44 +0000591
592 // --------------------------------------------------------------------
593
594 /**
595 * Not Like
596 *
597 * Generates a NOT LIKE portion of the query. Separates
598 * multiple calls with AND
599 *
600 * @access public
601 * @param mixed
602 * @param mixed
603 * @return object
604 */
605 function not_like($field, $match = '', $side = 'both')
606 {
Rick Ellis59523592008-10-17 04:07:40 +0000607 return $this->_like($field, $match, 'AND ', $side, 'NOT');
Derek Allarde54e3d22007-12-19 15:53:44 +0000608 }
609
Derek Allard09de1852007-02-14 01:35:56 +0000610 // --------------------------------------------------------------------
611
612 /**
613 * OR Like
614 *
615 * Generates a %LIKE% portion of the query. Separates
616 * multiple calls with OR
617 *
618 * @access public
619 * @param mixed
620 * @param mixed
621 * @return object
622 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000623 function or_like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000624 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000625 return $this->_like($field, $match, 'OR ', $side);
626 }
627
628 // --------------------------------------------------------------------
629
630 /**
Derek Allarde54e3d22007-12-19 15:53:44 +0000631 * OR Not Like
632 *
633 * Generates a NOT LIKE portion of the query. Separates
634 * multiple calls with OR
635 *
636 * @access public
637 * @param mixed
638 * @param mixed
639 * @return object
640 */
641 function or_not_like($field, $match = '', $side = 'both')
642 {
Rick Ellis59523592008-10-17 04:07:40 +0000643 return $this->_like($field, $match, 'OR ', $side, 'NOT');
Derek Allarde54e3d22007-12-19 15:53:44 +0000644 }
645
646 // --------------------------------------------------------------------
647
648 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000649 * orlike() is an alias of or_like()
650 * this function is here for backwards compatibility, as
651 * orlike() has been deprecated
652 */
653 function orlike($field, $match = '', $side = 'both')
654 {
Derek Allard4a310b72008-01-30 21:32:47 +0000655 return $this->or_like($field, $match, $side);
Derek Allard09de1852007-02-14 01:35:56 +0000656 }
657
658 // --------------------------------------------------------------------
659
660 /**
661 * Like
662 *
663 * Called by like() or orlike()
664 *
665 * @access private
666 * @param mixed
667 * @param mixed
668 * @param string
669 * @return object
670 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000671 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard09de1852007-02-14 01:35:56 +0000672 {
Derek Jones0b59f272008-05-13 04:22:33 +0000673 if ( ! is_array($field))
Derek Allard09de1852007-02-14 01:35:56 +0000674 {
675 $field = array($field => $match);
676 }
677
678 foreach ($field as $k => $v)
Rick Ellis59523592008-10-17 04:07:40 +0000679 {
Derek Allard39b622d2008-01-16 21:10:09 +0000680 $k = $this->_protect_identifiers($k);
681
Derek Allard09de1852007-02-14 01:35:56 +0000682 $prefix = (count($this->ar_like) == 0) ? '' : $type;
Derek Allarde54e3d22007-12-19 15:53:44 +0000683
Rick Ellis59523592008-10-17 04:07:40 +0000684 $v = $this->escape_str($v);
Derek Allarde54e3d22007-12-19 15:53:44 +0000685
Derek Allard218e2bc2007-12-17 21:18:14 +0000686 if ($side == 'before')
687 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000688 $like_statement = $prefix." $k $not LIKE '%{$v}'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000689 }
690 elseif ($side == 'after')
691 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000692 $like_statement = $prefix." $k $not LIKE '{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000693 }
694 else
695 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000696 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000697 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000698
699 $this->ar_like[] = $like_statement;
700 if ($this->ar_caching === TRUE)
701 {
702 $this->ar_cache_like[] = $like_statement;
703 }
704
Derek Allard09de1852007-02-14 01:35:56 +0000705 }
706 return $this;
707 }
708
709 // --------------------------------------------------------------------
710
711 /**
712 * GROUP BY
713 *
714 * @access public
715 * @param string
716 * @return object
717 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000718 function group_by($by)
Derek Allard09de1852007-02-14 01:35:56 +0000719 {
720 if (is_string($by))
721 {
722 $by = explode(',', $by);
723 }
724
725 foreach ($by as $val)
726 {
727 $val = trim($val);
728
729 if ($val != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000730 {
Derek Allard39b622d2008-01-16 21:10:09 +0000731 $this->ar_groupby[] = $this->_protect_identifiers($val);
Rick Ellis59523592008-10-17 04:07:40 +0000732
Derek Allard9b3e7b52008-02-04 23:20:34 +0000733 if ($this->ar_caching === TRUE)
734 {
735 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
736 }
737 }
Derek Allard09de1852007-02-14 01:35:56 +0000738 }
739 return $this;
740 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000741
742 // --------------------------------------------------------------------
743
744 /**
745 * groupby() is an alias of group_by()
746 * this function is here for backwards compatibility, as
747 * groupby() has been deprecated
748 */
749 function groupby($by)
750 {
751 return $this->group_by($by);
752 }
753
Derek Allard09de1852007-02-14 01:35:56 +0000754 // --------------------------------------------------------------------
755
756 /**
757 * Sets the HAVING value
758 *
759 * Separates multiple calls with AND
760 *
761 * @access public
762 * @param string
763 * @param string
764 * @return object
765 */
Derek Allarde808aac2008-04-06 18:22:00 +0000766 function having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000767 {
Derek Allarde808aac2008-04-06 18:22:00 +0000768 return $this->_having($key, $value, 'AND ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000769 }
Derek Allard43e8cbf2008-03-01 23:14:43 +0000770
771 // --------------------------------------------------------------------
772
773 /**
774 * orhaving() is an alias of or_having()
775 * this function is here for backwards compatibility, as
776 * orhaving() has been deprecated
777 */
778
Derek Allarde808aac2008-04-06 18:22:00 +0000779 function orhaving($key, $value = '', $escape = TRUE)
Derek Allard43e8cbf2008-03-01 23:14:43 +0000780 {
Derek Allarda459b462008-05-22 13:01:39 +0000781 return $this->or_having($key, $value, $escape);
Derek Allard43e8cbf2008-03-01 23:14:43 +0000782 }
Derek Allard09de1852007-02-14 01:35:56 +0000783 // --------------------------------------------------------------------
784
785 /**
786 * Sets the OR HAVING value
787 *
788 * Separates multiple calls with OR
789 *
790 * @access public
791 * @param string
792 * @param string
793 * @return object
794 */
Derek Allarde808aac2008-04-06 18:22:00 +0000795 function or_having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000796 {
Derek Allarde808aac2008-04-06 18:22:00 +0000797 return $this->_having($key, $value, 'OR ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000798 }
799
800 // --------------------------------------------------------------------
801
802 /**
803 * Sets the HAVING values
804 *
Rick Ellisff734012008-09-30 20:38:12 +0000805 * Called by having() or or_having()
Derek Allard09de1852007-02-14 01:35:56 +0000806 *
807 * @access private
808 * @param string
809 * @param string
810 * @return object
811 */
Derek Allarde808aac2008-04-06 18:22:00 +0000812 function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000813 {
Derek Jones0b59f272008-05-13 04:22:33 +0000814 if ( ! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000815 {
816 $key = array($key => $value);
817 }
818
819 foreach ($key as $k => $v)
820 {
821 $prefix = (count($this->ar_having) == 0) ? '' : $type;
Derek Allarde808aac2008-04-06 18:22:00 +0000822
823 if ($escape === TRUE)
824 {
825 $k = $this->_protect_identifiers($k);
826 }
827
Derek Allarda459b462008-05-22 13:01:39 +0000828 if ( ! $this->_has_operator($k))
829 {
830 $k .= ' = ';
831 }
832
Derek Allard09de1852007-02-14 01:35:56 +0000833 if ($v != '')
834 {
Rick Ellis59523592008-10-17 04:07:40 +0000835 $v = ' '.$this->escape_str($v);
Derek Allard09de1852007-02-14 01:35:56 +0000836 }
837
838 $this->ar_having[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000839 if ($this->ar_caching === TRUE)
840 {
841 $this->ar_cache_having[] = $prefix.$k.$v;
842 }
Derek Allard09de1852007-02-14 01:35:56 +0000843 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000844
Derek Allard09de1852007-02-14 01:35:56 +0000845 return $this;
846 }
847
848 // --------------------------------------------------------------------
849
850 /**
851 * Sets the ORDER BY value
852 *
853 * @access public
854 * @param string
855 * @param string direction: asc or desc
856 * @return object
857 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000858 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000859 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000860 if (strtolower($direction) == 'random')
861 {
862 $orderby = ''; // Random results want or don't need a field name
863 $direction = $this->_random_keyword;
864 }
865 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000866 {
Derek Allard92782492007-08-10 11:26:01 +0000867 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000868 }
869
Rick Ellis59523592008-10-17 04:07:40 +0000870 $orderby_statement = $this->_protect_identifiers($orderby).$direction;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000871
872 $this->ar_orderby[] = $orderby_statement;
873 if ($this->ar_caching === TRUE)
874 {
875 $this->ar_cache_orderby[] = $orderby_statement;
876 }
877
Derek Allard09de1852007-02-14 01:35:56 +0000878 return $this;
879 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000880
Derek Allard218e2bc2007-12-17 21:18:14 +0000881 // --------------------------------------------------------------------
882
883 /**
884 * orderby() is an alias of order_by()
885 * this function is here for backwards compatibility, as
886 * orderby() has been deprecated
887 */
888 function orderby($orderby, $direction = '')
889 {
890 return $this->order_by($orderby, $direction);
891 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000892
Derek Allard09de1852007-02-14 01:35:56 +0000893 // --------------------------------------------------------------------
894
895 /**
896 * Sets the LIMIT value
897 *
898 * @access public
899 * @param integer the limit value
900 * @param integer the offset value
901 * @return object
902 */
903 function limit($value, $offset = '')
904 {
905 $this->ar_limit = $value;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000906 if ($this->ar_caching === TRUE)
907 {
908 $this->ar_cache_limit[] = $value;
909 }
910
Derek Allard09de1852007-02-14 01:35:56 +0000911 if ($offset != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000912 {
Derek Allard09de1852007-02-14 01:35:56 +0000913 $this->ar_offset = $offset;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000914 if ($this->ar_caching === TRUE)
915 {
916 $this->ar_cache_offset[] = $offset;
917 }
918 }
Derek Allard09de1852007-02-14 01:35:56 +0000919
920 return $this;
921 }
922
923 // --------------------------------------------------------------------
924
925 /**
926 * Sets the OFFSET value
927 *
928 * @access public
929 * @param integer the offset value
930 * @return object
931 */
Derek Allard9b3e7b52008-02-04 23:20:34 +0000932 function offset($offset)
Derek Allard09de1852007-02-14 01:35:56 +0000933 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000934 $this->ar_offset = $offset;
935 if ($this->ar_caching === TRUE)
936 {
937 $this->ar_cache_offset[] = $offset;
938 }
939
Derek Allard09de1852007-02-14 01:35:56 +0000940 return $this;
941 }
942
943 // --------------------------------------------------------------------
944
945 /**
946 * The "set" function. Allows key/value pairs to be set for inserting or updating
947 *
948 * @access public
949 * @param mixed
950 * @param string
Derek Allard39b622d2008-01-16 21:10:09 +0000951 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +0000952 * @return object
953 */
Derek Allard39b622d2008-01-16 21:10:09 +0000954 function set($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000955 {
956 $key = $this->_object_to_array($key);
957
Derek Jones0b59f272008-05-13 04:22:33 +0000958 if ( ! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000959 {
960 $key = array($key => $value);
961 }
962
963 foreach ($key as $k => $v)
964 {
Derek Allard39b622d2008-01-16 21:10:09 +0000965 if ($escape === FALSE)
966 {
967 $this->ar_set[$this->_protect_identifiers($k)] = $v;
Rick Ellis59523592008-10-17 04:07:40 +0000968
Derek Allard9b3e7b52008-02-04 23:20:34 +0000969 if ($this->ar_caching === TRUE)
970 {
971 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $v;
972 }
Derek Allard39b622d2008-01-16 21:10:09 +0000973 }
974 else
975 {
976 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
Rick Ellis59523592008-10-17 04:07:40 +0000977
Derek Allard9b3e7b52008-02-04 23:20:34 +0000978 if ($this->ar_caching === TRUE)
979 {
980 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $this->escape($v);
981 }
Derek Allard39b622d2008-01-16 21:10:09 +0000982 }
Derek Allard09de1852007-02-14 01:35:56 +0000983 }
984
985 return $this;
986 }
987
988 // --------------------------------------------------------------------
989
990 /**
991 * Get
992 *
993 * Compiles the select statement based on the other functions called
994 * and runs the query
995 *
996 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +0000997 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +0000998 * @param string the limit clause
999 * @param string the offset clause
1000 * @return object
1001 */
1002 function get($table = '', $limit = null, $offset = null)
1003 {
1004 if ($table != '')
1005 {
Derek Allard5e128942007-12-28 21:33:03 +00001006 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001007 $this->from($table);
1008 }
1009
Derek Jones0b59f272008-05-13 04:22:33 +00001010 if ( ! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001011 {
1012 $this->limit($limit, $offset);
1013 }
1014
1015 $sql = $this->_compile_select();
1016
1017 $result = $this->query($sql);
1018 $this->_reset_select();
1019 return $result;
1020 }
1021
Derek Allard09de1852007-02-14 01:35:56 +00001022 /**
Derek Allard694b5b82007-12-18 15:58:03 +00001023 * "Count All Results" query
1024 *
1025 * Generates a platform-specific query string that counts all records
1026 * returned by an Active Record query.
1027 *
1028 * @access public
1029 * @param string
1030 * @return string
1031 */
1032 function count_all_results($table = '')
1033 {
1034 if ($table != '')
1035 {
Derek Allard5e128942007-12-28 21:33:03 +00001036 $this->_track_aliases($table);
Derek Allard694b5b82007-12-18 15:58:03 +00001037 $this->from($table);
1038 }
1039
Derek Allard39b622d2008-01-16 21:10:09 +00001040 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
Derek Allard694b5b82007-12-18 15:58:03 +00001041
1042 $query = $this->query($sql);
1043 $this->_reset_select();
1044
1045 if ($query->num_rows() == 0)
1046 {
1047 return '0';
1048 }
1049
1050 $row = $query->row();
1051 return $row->numrows;
1052 }
1053
1054 // --------------------------------------------------------------------
1055
1056 /**
Derek Allard218e2bc2007-12-17 21:18:14 +00001057 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +00001058 *
1059 * Allows the where clause, limit and offset to be added directly
1060 *
1061 * @access public
1062 * @param string the where clause
1063 * @param string the limit clause
1064 * @param string the offset clause
1065 * @return object
1066 */
Derek Allard218e2bc2007-12-17 21:18:14 +00001067 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +00001068 {
1069 if ($table != '')
1070 {
1071 $this->from($table);
1072 }
1073
Derek Jones0b59f272008-05-13 04:22:33 +00001074 if ( ! is_null($where))
Derek Allard09de1852007-02-14 01:35:56 +00001075 {
1076 $this->where($where);
1077 }
1078
Derek Jones0b59f272008-05-13 04:22:33 +00001079 if ( ! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001080 {
1081 $this->limit($limit, $offset);
1082 }
1083
1084 $sql = $this->_compile_select();
1085
1086 $result = $this->query($sql);
1087 $this->_reset_select();
1088 return $result;
1089 }
Derek Allard218e2bc2007-12-17 21:18:14 +00001090
1091 // --------------------------------------------------------------------
1092
1093 /**
1094 * getwhere() is an alias of get_where()
1095 * this function is here for backwards compatibility, as
1096 * getwhere() has been deprecated
1097 */
1098 function getwhere($table = '', $where = null, $limit = null, $offset = null)
1099 {
1100 return $this->get_where($table, $where, $limit, $offset);
1101 }
Derek Allard09de1852007-02-14 01:35:56 +00001102
1103 // --------------------------------------------------------------------
1104
1105 /**
1106 * Insert
1107 *
1108 * Compiles an insert string and runs the query
1109 *
1110 * @access public
1111 * @param string the table to retrieve the results from
1112 * @param array an associative array of insert values
1113 * @return object
1114 */
1115 function insert($table = '', $set = NULL)
1116 {
Derek Jones0b59f272008-05-13 04:22:33 +00001117 if ( ! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001118 {
1119 $this->set($set);
1120 }
1121
1122 if (count($this->ar_set) == 0)
1123 {
1124 if ($this->db_debug)
1125 {
1126 return $this->display_error('db_must_use_set');
1127 }
1128 return FALSE;
1129 }
1130
1131 if ($table == '')
1132 {
Derek Jones0b59f272008-05-13 04:22:33 +00001133 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001134 {
1135 if ($this->db_debug)
1136 {
1137 return $this->display_error('db_must_set_table');
1138 }
1139 return FALSE;
1140 }
1141
1142 $table = $this->ar_from[0];
1143 }
Derek Allard39b622d2008-01-16 21:10:09 +00001144
Rick Ellis59523592008-10-17 04:07:40 +00001145 $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
Derek Allard09de1852007-02-14 01:35:56 +00001146
1147 $this->_reset_write();
1148 return $this->query($sql);
1149 }
1150
1151 // --------------------------------------------------------------------
1152
1153 /**
1154 * Update
1155 *
1156 * Compiles an update string and runs the query
1157 *
1158 * @access public
1159 * @param string the table to retrieve the results from
1160 * @param array an associative array of update values
1161 * @param mixed the where clause
1162 * @return object
1163 */
Derek Allard5e128942007-12-28 21:33:03 +00001164 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001165 {
Derek Jones0b59f272008-05-13 04:22:33 +00001166 if ( ! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001167 {
1168 $this->set($set);
1169 }
1170
1171 if (count($this->ar_set) == 0)
1172 {
1173 if ($this->db_debug)
1174 {
1175 return $this->display_error('db_must_use_set');
1176 }
1177 return FALSE;
1178 }
1179
1180 if ($table == '')
1181 {
Derek Jones0b59f272008-05-13 04:22:33 +00001182 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001183 {
1184 if ($this->db_debug)
1185 {
1186 return $this->display_error('db_must_set_table');
1187 }
1188 return FALSE;
1189 }
1190
1191 $table = $this->ar_from[0];
1192 }
1193
Derek Allarde77d77c2007-12-19 15:01:55 +00001194 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001195 {
1196 $this->where($where);
1197 }
Derek Allardda6d2402007-12-19 14:49:29 +00001198
Derek Allarde77d77c2007-12-19 15:01:55 +00001199 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +00001200 {
1201 $this->limit($limit);
1202 }
Derek Allard09de1852007-02-14 01:35:56 +00001203
Rick Ellis59523592008-10-17 04:07:40 +00001204 $sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001205
1206 $this->_reset_write();
1207 return $this->query($sql);
1208 }
Derek Allard39b622d2008-01-16 21:10:09 +00001209
1210 // --------------------------------------------------------------------
1211
1212 /**
1213 * Empty Table
1214 *
1215 * Compiles a delete string and runs "DELETE FROM table"
1216 *
1217 * @access public
1218 * @param string the table to empty
1219 * @return object
1220 */
1221 function empty_table($table = '')
1222 {
1223 if ($table == '')
1224 {
Derek Jones0b59f272008-05-13 04:22:33 +00001225 if ( ! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001226 {
1227 if ($this->db_debug)
1228 {
1229 return $this->display_error('db_must_set_table');
1230 }
1231 return FALSE;
1232 }
1233
1234 $table = $this->ar_from[0];
1235 }
1236 else
1237 {
Rick Ellis59523592008-10-17 04:07:40 +00001238 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard39b622d2008-01-16 21:10:09 +00001239 }
1240
Derek Allard39b622d2008-01-16 21:10:09 +00001241 $sql = $this->_delete($table);
1242
1243 $this->_reset_write();
1244
1245 return $this->query($sql);
1246 }
1247
1248 // --------------------------------------------------------------------
1249
1250 /**
1251 * Truncate
1252 *
1253 * Compiles a truncate string and runs the query
1254 * If the database does not support the truncate() command
1255 * This function maps to "DELETE FROM table"
1256 *
1257 * @access public
1258 * @param string the table to truncate
1259 * @return object
1260 */
1261 function truncate($table = '')
1262 {
1263 if ($table == '')
1264 {
Derek Jones0b59f272008-05-13 04:22:33 +00001265 if ( ! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001266 {
1267 if ($this->db_debug)
1268 {
1269 return $this->display_error('db_must_set_table');
1270 }
1271 return FALSE;
1272 }
1273
1274 $table = $this->ar_from[0];
1275 }
1276 else
1277 {
Rick Ellis59523592008-10-17 04:07:40 +00001278 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard39b622d2008-01-16 21:10:09 +00001279 }
1280
Derek Allard39b622d2008-01-16 21:10:09 +00001281 $sql = $this->_truncate($table);
1282
1283 $this->_reset_write();
1284
1285 return $this->query($sql);
1286 }
Derek Allard09de1852007-02-14 01:35:56 +00001287
1288 // --------------------------------------------------------------------
1289
1290 /**
1291 * Delete
1292 *
1293 * Compiles a delete string and runs the query
1294 *
1295 * @access public
Derek Allard41f60d42007-12-20 20:09:22 +00001296 * @param mixed the table(s) to delete from. String or array
Derek Allard09de1852007-02-14 01:35:56 +00001297 * @param mixed the where clause
Derek Allard41f60d42007-12-20 20:09:22 +00001298 * @param mixed the limit clause
1299 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +00001300 * @return object
1301 */
Derek Allard41f60d42007-12-20 20:09:22 +00001302 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +00001303 {
1304 if ($table == '')
1305 {
Derek Jones0b59f272008-05-13 04:22:33 +00001306 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001307 {
1308 if ($this->db_debug)
1309 {
1310 return $this->display_error('db_must_set_table');
1311 }
1312 return FALSE;
1313 }
Derek Allard39b622d2008-01-16 21:10:09 +00001314
Derek Allard09de1852007-02-14 01:35:56 +00001315 $table = $this->ar_from[0];
1316 }
Derek Allard39b622d2008-01-16 21:10:09 +00001317 elseif (is_array($table))
Derek Allard41f60d42007-12-20 20:09:22 +00001318 {
1319 foreach($table as $single_table)
1320 {
Derek Allard39b622d2008-01-16 21:10:09 +00001321 $this->delete($single_table, $where, $limit, FALSE);
Derek Allard41f60d42007-12-20 20:09:22 +00001322 }
Derek Allard39b622d2008-01-16 21:10:09 +00001323
Derek Allard41f60d42007-12-20 20:09:22 +00001324 $this->_reset_write();
1325 return;
1326 }
Derek Allard39b622d2008-01-16 21:10:09 +00001327 else
1328 {
Rick Ellis59523592008-10-17 04:07:40 +00001329 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard39b622d2008-01-16 21:10:09 +00001330 }
Derek Allard41f60d42007-12-20 20:09:22 +00001331
Derek Allard09de1852007-02-14 01:35:56 +00001332 if ($where != '')
1333 {
1334 $this->where($where);
1335 }
1336
Derek Allarde77d77c2007-12-19 15:01:55 +00001337 if ($limit != NULL)
1338 {
1339 $this->limit($limit);
1340 }
1341
Derek Allard39b622d2008-01-16 21:10:09 +00001342 if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
Derek Allard09de1852007-02-14 01:35:56 +00001343 {
1344 if ($this->db_debug)
1345 {
1346 return $this->display_error('db_del_must_use_where');
1347 }
Derek Allard39b622d2008-01-16 21:10:09 +00001348
Derek Allard09de1852007-02-14 01:35:56 +00001349 return FALSE;
1350 }
Derek Allard41f60d42007-12-20 20:09:22 +00001351
Derek Allard39b622d2008-01-16 21:10:09 +00001352 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001353
Derek Allard41f60d42007-12-20 20:09:22 +00001354 if ($reset_data)
1355 {
1356 $this->_reset_write();
1357 }
Derek Allard39b622d2008-01-16 21:10:09 +00001358
Derek Allard09de1852007-02-14 01:35:56 +00001359 return $this->query($sql);
1360 }
Derek Allard15ddc9d2007-12-20 13:54:39 +00001361
Derek Allard09de1852007-02-14 01:35:56 +00001362 // --------------------------------------------------------------------
1363
1364 /**
Rick Ellis59523592008-10-17 04:07:40 +00001365 * DB Prefix
Derek Allard09de1852007-02-14 01:35:56 +00001366 *
Rick Ellis59523592008-10-17 04:07:40 +00001367 * Prepends a database prefix if one exists in configuration
1368 *
1369 * @access public
1370 * @param string the table
1371 * @return string
Derek Allard09de1852007-02-14 01:35:56 +00001372 */
Rick Ellis59523592008-10-17 04:07:40 +00001373 function dbprefix($table = '')
Derek Allard09de1852007-02-14 01:35:56 +00001374 {
Rick Ellis59523592008-10-17 04:07:40 +00001375 if ($table == '')
1376 {
1377 $this->display_error('db_table_name_required');
1378 }
1379
1380 return $this->dbprefix.$table;
Derek Allard09de1852007-02-14 01:35:56 +00001381 }
Derek Allard09de1852007-02-14 01:35:56 +00001382
Derek Allard09de1852007-02-14 01:35:56 +00001383 // --------------------------------------------------------------------
1384
1385 /**
Derek Allard5e128942007-12-28 21:33:03 +00001386 * Track Aliases
1387 *
1388 * Used to track SQL statements written with aliased tables.
1389 *
1390 * @access private
1391 * @param string The table to inspect
1392 * @return string
1393 */
1394 function _track_aliases($table)
1395 {
Rick Ellis59523592008-10-17 04:07:40 +00001396 if (is_array($table))
1397 {
1398 foreach ($table as $t)
1399 {
1400 $this->_track_aliases($t);
1401 }
1402 return;
1403 }
1404
1405 // Does the string contain a comma? If so, we need to separate
1406 // the string into discreet statements
1407 if (strpos($table, ',') !== FALSE)
1408 {
1409 return $this->_track_aliases(explode(',', $table));
1410 }
1411
Derek Allard5e128942007-12-28 21:33:03 +00001412 // if a table alias is used we can recognize it by a space
1413 if (strpos($table, " ") !== FALSE)
1414 {
Rick Ellis59523592008-10-17 04:07:40 +00001415 // if the alias is written with the AS keyword, remove it
1416 $table = preg_replace('/ AS /i', ' ', $table);
1417
1418 // Grab the alias
1419 $table = trim(strrchr($table, " "));
1420
1421 // Store the alias, if it doesn't already exist
1422 if ( ! in_array($table, $this->ar_aliased_tables))
Derek Allard5e128942007-12-28 21:33:03 +00001423 {
Rick Ellis59523592008-10-17 04:07:40 +00001424 $this->ar_aliased_tables[] = $table;
Derek Allard5e128942007-12-28 21:33:03 +00001425 }
Derek Allard5e128942007-12-28 21:33:03 +00001426 }
Derek Allard5e128942007-12-28 21:33:03 +00001427 }
1428
1429 // --------------------------------------------------------------------
1430
1431 /**
Derek Allard09de1852007-02-14 01:35:56 +00001432 * Compile the SELECT statement
1433 *
1434 * Generates a query string based on which functions were used.
1435 * Should not be called directly. The get() function calls it.
1436 *
1437 * @access private
1438 * @return string
1439 */
Derek Allard694b5b82007-12-18 15:58:03 +00001440 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001441 {
Derek Allard78255262008-02-06 13:54:23 +00001442 $this->_merge_cache();
Derek Allard9b3e7b52008-02-04 23:20:34 +00001443
Rick Ellis59523592008-10-17 04:07:40 +00001444 // ----------------------------------------------------------------
1445
1446 // Write the "select" portion of the query
Derek Allard09de1852007-02-14 01:35:56 +00001447
Derek Allard694b5b82007-12-18 15:58:03 +00001448 if ($select_override !== FALSE)
1449 {
1450 $sql = $select_override;
1451 }
Rick Ellis59523592008-10-17 04:07:40 +00001452 else
1453 {
1454 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
1455
1456 if (count($this->ar_select) == 0)
1457 {
1458 $sql .= '*';
1459 }
1460 else
1461 {
1462 // Cycle through the "select" portion of the query and prep each column name.
1463 // The reason we protect identifiers here rather then in the select() function
1464 // is because until the user calls the from() function we don't know if there are aliases
1465 foreach ($this->ar_select as $key => $val)
1466 {
1467 $this->ar_select[$key] = $this->_protect_identifiers($val);
1468 }
1469
1470 $sql .= implode(', ', $this->ar_select);
1471 }
1472 }
1473
1474 // ----------------------------------------------------------------
1475
1476 // Write the "FROM" portion of the query
Derek Allard694b5b82007-12-18 15:58:03 +00001477
Derek Allard09de1852007-02-14 01:35:56 +00001478 if (count($this->ar_from) > 0)
1479 {
1480 $sql .= "\nFROM ";
Rick Ellis59523592008-10-17 04:07:40 +00001481
Derek Jonesc6ad0232008-01-29 18:44:54 +00001482 $sql .= $this->_from_tables($this->ar_from);
Derek Allard09de1852007-02-14 01:35:56 +00001483 }
1484
Rick Ellis59523592008-10-17 04:07:40 +00001485 // ----------------------------------------------------------------
1486
1487 // Write the "JOIN" portion of the query
1488
Derek Allard09de1852007-02-14 01:35:56 +00001489 if (count($this->ar_join) > 0)
Derek Allard5e128942007-12-28 21:33:03 +00001490 {
Derek Allard09de1852007-02-14 01:35:56 +00001491 $sql .= "\n";
Derek Allard5e128942007-12-28 21:33:03 +00001492
Rick Ellis59523592008-10-17 04:07:40 +00001493 $sql .= implode("\n", $this->ar_join);
Derek Allard09de1852007-02-14 01:35:56 +00001494 }
1495
Rick Ellis59523592008-10-17 04:07:40 +00001496 // ----------------------------------------------------------------
1497
1498 // Write the "WHERE" portion of the query
1499
Derek Allard09de1852007-02-14 01:35:56 +00001500 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1501 {
Rick Ellis59523592008-10-17 04:07:40 +00001502 $sql .= "\n";
1503
1504 $sql .= "WHERE ";
Derek Allard09de1852007-02-14 01:35:56 +00001505 }
1506
1507 $sql .= implode("\n", $this->ar_where);
Rick Ellis59523592008-10-17 04:07:40 +00001508
1509 // ----------------------------------------------------------------
Derek Allard09de1852007-02-14 01:35:56 +00001510
Rick Ellis59523592008-10-17 04:07:40 +00001511 // Write the "LIKE" portion of the query
1512
Derek Allard09de1852007-02-14 01:35:56 +00001513 if (count($this->ar_like) > 0)
1514 {
1515 if (count($this->ar_where) > 0)
1516 {
Rick Ellis59523592008-10-17 04:07:40 +00001517 $sql .= "\nAND ";
Derek Allard09de1852007-02-14 01:35:56 +00001518 }
1519
1520 $sql .= implode("\n", $this->ar_like);
1521 }
Rick Ellis59523592008-10-17 04:07:40 +00001522
1523 // ----------------------------------------------------------------
Derek Allard09de1852007-02-14 01:35:56 +00001524
Rick Ellis59523592008-10-17 04:07:40 +00001525 // Write the "GROUP BY" portion of the query
1526
Derek Allard09de1852007-02-14 01:35:56 +00001527 if (count($this->ar_groupby) > 0)
1528 {
1529 $sql .= "\nGROUP BY ";
Derek Allard5e128942007-12-28 21:33:03 +00001530
Rick Ellis59523592008-10-17 04:07:40 +00001531 $sql .= implode(', ', $this->ar_groupby);
Derek Allard09de1852007-02-14 01:35:56 +00001532 }
Rick Ellis59523592008-10-17 04:07:40 +00001533
1534 // ----------------------------------------------------------------
1535
1536 // Write the "HAVING" portion of the query
Derek Allard09de1852007-02-14 01:35:56 +00001537
1538 if (count($this->ar_having) > 0)
1539 {
1540 $sql .= "\nHAVING ";
1541 $sql .= implode("\n", $this->ar_having);
1542 }
1543
Rick Ellis59523592008-10-17 04:07:40 +00001544 // ----------------------------------------------------------------
1545
1546 // Write the "ORDER BY" portion of the query
1547
Derek Allard09de1852007-02-14 01:35:56 +00001548 if (count($this->ar_orderby) > 0)
1549 {
1550 $sql .= "\nORDER BY ";
1551 $sql .= implode(', ', $this->ar_orderby);
1552
1553 if ($this->ar_order !== FALSE)
1554 {
1555 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1556 }
1557 }
Rick Ellis59523592008-10-17 04:07:40 +00001558
1559 // ----------------------------------------------------------------
1560
1561 // Write the "LIMIT" portion of the query
Derek Allard09de1852007-02-14 01:35:56 +00001562
1563 if (is_numeric($this->ar_limit))
1564 {
1565 $sql .= "\n";
1566 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1567 }
1568
1569 return $sql;
1570 }
1571
1572 // --------------------------------------------------------------------
1573
1574 /**
1575 * Object to Array
1576 *
1577 * Takes an object as input and converts the class variables to array key/vals
1578 *
1579 * @access public
1580 * @param object
1581 * @return array
1582 */
1583 function _object_to_array($object)
1584 {
Derek Jones0b59f272008-05-13 04:22:33 +00001585 if ( ! is_object($object))
Derek Allard09de1852007-02-14 01:35:56 +00001586 {
1587 return $object;
1588 }
1589
1590 $array = array();
1591 foreach (get_object_vars($object) as $key => $val)
1592 {
Derek Allard848b7762007-12-31 16:43:05 +00001593 // There are some built in keys we need to ignore for this conversion
Derek Jones0b59f272008-05-13 04:22:33 +00001594 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
Derek Allard09de1852007-02-14 01:35:56 +00001595 {
1596 $array[$key] = $val;
1597 }
1598 }
1599
1600 return $array;
1601 }
1602
1603 // --------------------------------------------------------------------
1604
1605 /**
Derek Allard9b3e7b52008-02-04 23:20:34 +00001606 * Start Cache
1607 *
1608 * Starts AR caching
1609 *
1610 * @access public
1611 * @return void
1612 */
1613 function start_cache()
1614 {
1615 $this->ar_caching = TRUE;
1616 }
1617
1618 // --------------------------------------------------------------------
1619
1620 /**
1621 * Stop Cache
1622 *
1623 * Stops AR caching
1624 *
1625 * @access public
1626 * @return void
1627 */
1628 function stop_cache()
1629 {
1630 $this->ar_caching = FALSE;
1631 }
1632
Derek Allard9b3e7b52008-02-04 23:20:34 +00001633 // --------------------------------------------------------------------
1634
1635 /**
1636 * Flush Cache
1637 *
1638 * Empties the AR cache
1639 *
1640 * @access public
1641 * @return void
1642 */
1643 function flush_cache()
1644 {
1645 $ar_reset_items = array(
Rick Ellis59523592008-10-17 04:07:40 +00001646 'ar_cache_select' => array(),
1647 'ar_cache_from' => array(),
1648 'ar_cache_join' => array(),
1649 'ar_cache_where' => array(),
1650 'ar_cache_like' => array(),
1651 'ar_cache_groupby' => array(),
1652 'ar_cache_having' =>array(),
1653 'ar_cache_orderby' => array(),
1654 'ar_cache_set' => array()
1655 );
Derek Allard9b3e7b52008-02-04 23:20:34 +00001656
1657 $this->_reset_run($ar_reset_items);
1658 }
1659
1660 // --------------------------------------------------------------------
1661
1662 /**
1663 * Merge Cache
1664 *
1665 * When called, this function merges any cached AR arrays with
1666 * locally called ones.
1667 *
1668 * @access private
1669 * @return void
1670 */
1671 function _merge_cache()
1672 {
Rick Ellis59523592008-10-17 04:07:40 +00001673 if ($this->ar_caching == FALSE)
1674 {
1675 return;
1676 }
1677
Derek Allard9b3e7b52008-02-04 23:20:34 +00001678 $ar_items = array('select', 'from', 'join', 'where', 'like', 'groupby', 'having', 'orderby', 'set');
1679
1680 foreach ($ar_items as $ar_item)
1681 {
1682 $ar_cache_item = 'ar_cache_'.$ar_item;
1683 $ar_item = 'ar_'.$ar_item;
1684 $this->$ar_item = array_unique(array_merge($this->$ar_item, $this->$ar_cache_item));
1685 }
Rick Ellis59523592008-10-17 04:07:40 +00001686
1687 // If we are "protecting identifiers" we need to examine the "from"
1688 // portion of the query to determine if there are any aliases
1689 if ($this->_protect_identifiers === TRUE)
1690 {
1691 $this->_track_aliases($this->ar_from);
1692 }
Derek Allard9b3e7b52008-02-04 23:20:34 +00001693 }
1694
1695 // --------------------------------------------------------------------
1696
1697 /**
1698 * Resets the active record values. Called by the get() function
1699 *
1700 * @access private
1701 * @param array An array of fields to reset
1702 * @return void
1703 */
1704 function _reset_run($ar_reset_items)
1705 {
1706 foreach ($ar_reset_items as $item => $default_value)
1707 {
Derek Jones0b59f272008-05-13 04:22:33 +00001708 if ( ! in_array($item, $this->ar_store_array))
Derek Allard9b3e7b52008-02-04 23:20:34 +00001709 {
1710 $this->$item = $default_value;
1711 }
1712 }
1713 }
Rick Ellis59523592008-10-17 04:07:40 +00001714
Derek Allard9b3e7b52008-02-04 23:20:34 +00001715 // --------------------------------------------------------------------
1716
1717 /**
Derek Allard09de1852007-02-14 01:35:56 +00001718 * Resets the active record values. Called by the get() function
1719 *
1720 * @access private
1721 * @return void
1722 */
1723 function _reset_select()
1724 {
Derek Allard9b3e7b52008-02-04 23:20:34 +00001725 $ar_reset_items = array(
Rick Ellis59523592008-10-17 04:07:40 +00001726 'ar_select' => array(),
1727 'ar_from' => array(),
1728 'ar_join' => array(),
1729 'ar_where' => array(),
1730 'ar_like' => array(),
1731 'ar_groupby' => array(),
1732 'ar_having' => array(),
1733 'ar_orderby' => array(),
1734 'ar_wherein' => array(),
1735 'ar_aliased_tables' => array(),
1736 'ar_distinct' => FALSE,
1737 'ar_limit' => FALSE,
1738 'ar_offset' => FALSE,
1739 'ar_order' => FALSE,
1740 );
Derek Allard9b3e7b52008-02-04 23:20:34 +00001741
1742 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001743 }
1744
1745 // --------------------------------------------------------------------
1746
1747 /**
1748 * Resets the active record "write" values.
1749 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001750 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001751 *
1752 * @access private
1753 * @return void
1754 */
1755 function _reset_write()
Derek Allard9b3e7b52008-02-04 23:20:34 +00001756 {
1757 $ar_reset_items = array(
Rick Ellis59523592008-10-17 04:07:40 +00001758 'ar_set' => array(),
1759 'ar_from' => array(),
1760 'ar_where' => array(),
1761 'ar_like' => array(),
1762 'ar_orderby' => array(),
1763 'ar_limit' => FALSE,
1764 'ar_order' => FALSE
1765 );
Derek Allard9b3e7b52008-02-04 23:20:34 +00001766
1767 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001768 }
1769
1770}
Derek Allarda6325892008-05-12 17:51:47 +00001771
1772/* End of file DB_active_rec.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00001773/* Location: ./system/database/DB_active_rec.php */