blob: 05a04350de51a73d1878a33c52441c309f202e91 [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
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
Derek Allard5fe155e2008-05-12 19:14:57 +000072 */
Derek Allard3b118682008-01-22 23:44:32 +000073 function dbprefix($table = '')
74 {
75 if ($table == '')
76 {
77 $this->display_error('db_table_name_required');
78 }
Derek Allard5fe155e2008-05-12 19:14:57 +000079
Derek Allard3b118682008-01-22 23:44:32 +000080 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 {
Derek Allarda6325892008-05-12 17:51:47 +000098 if ($protect_identifiers !== FALSE)
99 {
100 $select = explode(',', $select);
101 }
102 else
103 {
104 $select = array($select);
105 }
Derek Allard09de1852007-02-14 01:35:56 +0000106 }
Derek Allard5fe155e2008-05-12 19:14:57 +0000107
Derek Allard09de1852007-02-14 01:35:56 +0000108 foreach ($select as $val)
109 {
110 $val = trim($val);
Derek Allard39b622d2008-01-16 21:10:09 +0000111
112 if ($val != '*' && $protect_identifiers !== FALSE)
113 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000114 if (strpos($val, '.') !== FALSE)
115 {
116 $val = $this->dbprefix.$val;
117 }
118 else
119 {
120 $val = $this->_protect_identifiers($val);
121 }
Derek Allard39b622d2008-01-16 21:10:09 +0000122 }
Derek Allard5fe155e2008-05-12 19:14:57 +0000123
Derek Allard09de1852007-02-14 01:35:56 +0000124 if ($val != '')
Derek Allard39b622d2008-01-16 21:10:09 +0000125 {
Derek Allard09de1852007-02-14 01:35:56 +0000126 $this->ar_select[] = $val;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000127 if ($this->ar_caching === TRUE)
128 {
129 $this->ar_cache_select[] = $val;
130 }
Derek Allard39b622d2008-01-16 21:10:09 +0000131 }
Derek Allard09de1852007-02-14 01:35:56 +0000132 }
133 return $this;
134 }
Derek Allard39b622d2008-01-16 21:10:09 +0000135
136 // --------------------------------------------------------------------
137
138 /**
139 * Select Max
140 *
141 * Generates a SELECT MAX(field) portion of a query
142 *
143 * @access public
144 * @param string the field
145 * @param string an alias
146 * @return object
147 */
148 function select_max($select = '', $alias='')
149 {
Derek Jones0b59f272008-05-13 04:22:33 +0000150 if ( ! is_string($select) OR $select == '')
Derek Allard39b622d2008-01-16 21:10:09 +0000151 {
152 $this->display_error('db_invalid_query');
153 }
Derek Allard09de1852007-02-14 01:35:56 +0000154
Derek Allard39b622d2008-01-16 21:10:09 +0000155 $alias = ($alias != '') ? $alias : $select;
156
157 $sql = 'MAX('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
158
159 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000160 if ($this->ar_caching === TRUE)
161 {
162 $this->ar_cache_select[] = $sql;
163 }
Derek Allard39b622d2008-01-16 21:10:09 +0000164
165 return $this;
Derek Allard39b622d2008-01-16 21:10:09 +0000166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Select Min
172 *
173 * Generates a SELECT MIN(field) portion of a query
174 *
175 * @access public
176 * @param string the field
177 * @param string an alias
178 * @return object
179 */
180 function select_min($select = '', $alias='')
181 {
Derek Jones0b59f272008-05-13 04:22:33 +0000182 if ( ! is_string($select) OR $select == '')
Derek Allard39b622d2008-01-16 21:10:09 +0000183 {
184 $this->display_error('db_invalid_query');
185 }
Derek Allard5fe155e2008-05-12 19:14:57 +0000186
Derek Allard39b622d2008-01-16 21:10:09 +0000187 $alias = ($alias != '') ? $alias : $select;
188
189 $sql = 'MIN('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
190
191 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000192 if ($this->ar_caching === TRUE)
193 {
194 $this->ar_cache_select[] = $sql;
195 }
196
Derek Allard39b622d2008-01-16 21:10:09 +0000197 return $this;
198 }
199
200 // --------------------------------------------------------------------
201
202 /**
203 * Select Average
204 *
205 * Generates a SELECT AVG(field) portion of a query
206 *
207 * @access public
208 * @param string the field
209 * @param string an alias
210 * @return object
211 */
212 function select_avg($select = '', $alias='')
213 {
Derek Jones0b59f272008-05-13 04:22:33 +0000214 if ( ! is_string($select) OR $select == '')
Derek Allard39b622d2008-01-16 21:10:09 +0000215 {
216 $this->display_error('db_invalid_query');
217 }
218
219 $alias = ($alias != '') ? $alias : $select;
Derek Allard5fe155e2008-05-12 19:14:57 +0000220
Derek Allard39b622d2008-01-16 21:10:09 +0000221 $sql = 'AVG('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
222
223 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000224 if ($this->ar_caching === TRUE)
225 {
226 $this->ar_cache_select[] = $sql;
227 }
228
Derek Allard39b622d2008-01-16 21:10:09 +0000229 return $this;
230 }
231
232 // --------------------------------------------------------------------
Derek Allard5fe155e2008-05-12 19:14:57 +0000233
Derek Allard39b622d2008-01-16 21:10:09 +0000234 /**
235 * Select Sum
236 *
237 * Generates a SELECT SUM(field) portion of a query
238 *
239 * @access public
240 * @param string the field
241 * @param string an alias
242 * @return object
243 */
244 function select_sum($select = '', $alias='')
245 {
Derek Jones0b59f272008-05-13 04:22:33 +0000246 if ( ! is_string($select) OR $select == '')
Derek Allard39b622d2008-01-16 21:10:09 +0000247 {
248 $this->display_error('db_invalid_query');
249 }
250
251 $alias = ($alias != '') ? $alias : $select;
252
253 $sql = 'SUM('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
254
255 $this->ar_select[] = $sql;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000256 if ($this->ar_caching === TRUE)
257 {
258 $this->ar_cache_select[] = $sql;
259 }
260
Derek Allard39b622d2008-01-16 21:10:09 +0000261 return $this;
262 }
263
Derek Allard09de1852007-02-14 01:35:56 +0000264 // --------------------------------------------------------------------
265
266 /**
267 * DISTINCT
268 *
269 * Sets a flag which tells the query string compiler to add DISTINCT
270 *
271 * @access public
272 * @param bool
273 * @return object
274 */
275 function distinct($val = TRUE)
276 {
277 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
278 return $this;
279 }
280
281 // --------------------------------------------------------------------
282
283 /**
284 * From
285 *
286 * Generates the FROM portion of the query
287 *
288 * @access public
289 * @param mixed can be a string or array
290 * @return object
291 */
292 function from($from)
293 {
294 foreach ((array)$from as $val)
295 {
Derek Allard39b622d2008-01-16 21:10:09 +0000296 $this->ar_from[] = $this->_protect_identifiers($this->_track_aliases($val));
Derek Allard9b3e7b52008-02-04 23:20:34 +0000297 if ($this->ar_caching === TRUE)
298 {
Derek Allard9a4d1da2008-02-25 14:18:38 +0000299 $this->ar_cache_from[] = $this->_protect_identifiers($this->_track_aliases($val));
Derek Allard9b3e7b52008-02-04 23:20:34 +0000300 }
Derek Allard09de1852007-02-14 01:35:56 +0000301 }
Derek Allard5e128942007-12-28 21:33:03 +0000302
Derek Allard09de1852007-02-14 01:35:56 +0000303 return $this;
304 }
Derek Allard5fe155e2008-05-12 19:14:57 +0000305
Derek Allard09de1852007-02-14 01:35:56 +0000306 // --------------------------------------------------------------------
307
308 /**
309 * Join
310 *
311 * Generates the JOIN portion of the query
312 *
313 * @access public
314 * @param string
315 * @param string the join condition
316 * @param string the type of join
317 * @return object
318 */
319 function join($table, $cond, $type = '')
320 {
321 if ($type != '')
322 {
323 $type = strtoupper(trim($type));
324
Derek Jones0b59f272008-05-13 04:22:33 +0000325 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
Derek Allard09de1852007-02-14 01:35:56 +0000326 {
327 $type = '';
328 }
329 else
330 {
331 $type .= ' ';
332 }
333 }
334
Derek Allard09de1852007-02-14 01:35:56 +0000335 // If a DB prefix is used we might need to add it to the column names
336 if ($this->dbprefix)
337 {
Derek Allard39b622d2008-01-16 21:10:09 +0000338 $this->_track_aliases($table);
339
Derek Allard09de1852007-02-14 01:35:56 +0000340 // First we remove any existing prefixes in the condition to avoid duplicates
341 $cond = preg_replace('|('.$this->dbprefix.')([\w\.]+)([\W\s]+)|', "$2$3", $cond);
Derek Allard5fe155e2008-05-12 19:14:57 +0000342
Derek Allard09de1852007-02-14 01:35:56 +0000343 // Next we add the prefixes to the condition
344 $cond = preg_replace('|([\w\.]+)([\W\s]+)(.+)|', $this->dbprefix . "$1$2" . $this->dbprefix . "$3", $cond);
Derek Allard5fe155e2008-05-12 19:14:57 +0000345 }
Derek Allard5e128942007-12-28 21:33:03 +0000346
Derek Allard9b3e7b52008-02-04 23:20:34 +0000347 $join = $type.'JOIN '.$this->_protect_identifiers($this->dbprefix.$table, TRUE).' ON '.$cond;
348
349 $this->ar_join[] = $join;
350 if ($this->ar_caching === TRUE)
351 {
352 $this->ar_cache_join[] = $join;
353 }
354
Derek Allard09de1852007-02-14 01:35:56 +0000355 return $this;
356 }
Derek Allard5fe155e2008-05-12 19:14:57 +0000357
Derek Allard09de1852007-02-14 01:35:56 +0000358 // --------------------------------------------------------------------
359
360 /**
361 * Where
362 *
363 * Generates the WHERE portion of the query. Separates
364 * multiple calls with AND
365 *
366 * @access public
367 * @param mixed
368 * @param mixed
369 * @return object
370 */
Derek Allard39b622d2008-01-16 21:10:09 +0000371 function where($key, $value = NULL, $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000372 {
Derek Allard39b622d2008-01-16 21:10:09 +0000373 return $this->_where($key, $value, 'AND ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000374 }
375
376 // --------------------------------------------------------------------
377
378 /**
379 * OR Where
380 *
381 * Generates the WHERE portion of the query. Separates
382 * multiple calls with OR
383 *
384 * @access public
385 * @param mixed
386 * @param mixed
387 * @return object
388 */
Derek Allard39b622d2008-01-16 21:10:09 +0000389 function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000390 {
Derek Allard39b622d2008-01-16 21:10:09 +0000391 return $this->_where($key, $value, 'OR ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000392 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000393
394 // --------------------------------------------------------------------
395
396 /**
397 * orwhere() is an alias of or_where()
398 * this function is here for backwards compatibility, as
399 * orwhere() has been deprecated
400 */
Derek Allard39b622d2008-01-16 21:10:09 +0000401 function orwhere($key, $value = NULL, $escape = TRUE)
Derek Allard218e2bc2007-12-17 21:18:14 +0000402 {
Derek Allard39b622d2008-01-16 21:10:09 +0000403 return $this->or_where($key, $value, $escape);
Derek Allard218e2bc2007-12-17 21:18:14 +0000404 }
Derek Allard67b44ed2008-01-12 16:18:02 +0000405
406 // --------------------------------------------------------------------
407
408 /**
Derek Allard09de1852007-02-14 01:35:56 +0000409 * Where
410 *
411 * Called by where() or orwhere()
412 *
413 * @access private
414 * @param mixed
415 * @param mixed
416 * @param string
417 * @return object
418 */
Derek Allard39b622d2008-01-16 21:10:09 +0000419 function _where($key, $value = NULL, $type = 'AND ', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000420 {
Derek Jones0b59f272008-05-13 04:22:33 +0000421 if ( ! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000422 {
423 $key = array($key => $value);
424 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000425
Derek Allard09de1852007-02-14 01:35:56 +0000426 foreach ($key as $k => $v)
427 {
428 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Derek Allard15ddc9d2007-12-20 13:54:39 +0000429
Derek Jones0b59f272008-05-13 04:22:33 +0000430 if ( ! $this->_has_operator($k) && is_null($key[$k]))
Derek Allard15ddc9d2007-12-20 13:54:39 +0000431 {
432 // value appears not to have been set, assign the test to IS NULL
433 $k .= ' IS NULL';
434 }
Derek Allard09de1852007-02-14 01:35:56 +0000435
Derek Jones0b59f272008-05-13 04:22:33 +0000436 if ( ! is_null($v))
Derek Allard09de1852007-02-14 01:35:56 +0000437 {
Derek Allard39b622d2008-01-16 21:10:09 +0000438
439 if ($escape === TRUE)
440 {
441 // exception for "field<=" keys
442 if ($this->_has_operator($k))
443 {
444 $k = preg_replace("/([A-Za-z_0-9]+)/", $this->_protect_identifiers('$1'), $k);
445 }
446 else
447 {
448 $k = $this->_protect_identifiers($k);
449 }
450 }
451
Derek Jones0b59f272008-05-13 04:22:33 +0000452 if ( ! $this->_has_operator($k))
Derek Allard09de1852007-02-14 01:35:56 +0000453 {
454 $k .= ' =';
455 }
Derek Allard5fe155e2008-05-12 19:14:57 +0000456
Derek Allard16629b12008-04-06 19:58:20 +0000457 if ($v !== '' AND $v !== NULL)
Derek Allard96863ce2008-04-06 19:20:17 +0000458 {
Derek Allard5fe155e2008-05-12 19:14:57 +0000459 if ($escape === TRUE)
460 {
461 $v = ' '.$this->escape($v);
462 }
Derek Allard96863ce2008-04-06 19:20:17 +0000463 }
Derek Allard09de1852007-02-14 01:35:56 +0000464 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000465 else
466 {
Derek Allard5fe155e2008-05-12 19:14:57 +0000467
Derek Allard16629b12008-04-06 19:58:20 +0000468 if ($escape === TRUE)
469 {
470 $k = $this->_protect_identifiers($k, TRUE);
471 }
472
Derek Allard9b3e7b52008-02-04 23:20:34 +0000473 }
474
Derek Allard09de1852007-02-14 01:35:56 +0000475 $this->ar_where[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000476 if ($this->ar_caching === TRUE)
477 {
478 $this->ar_cache_where[] = $prefix.$k.$v;
479 }
480
Derek Allard09de1852007-02-14 01:35:56 +0000481 }
482 return $this;
483 }
Derek Allard80dd7022007-12-18 23:55:06 +0000484
485 // --------------------------------------------------------------------
486
487 /**
488 * Where_in
489 *
Derek Allardc6935512007-12-19 14:23:19 +0000490 * Generates a WHERE field IN ('item', 'item') SQL query joined with
491 * AND if appropriate
Derek Allard80dd7022007-12-18 23:55:06 +0000492 *
493 * @access public
494 * @param string The field to search
495 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000496 * @return object
497 */
498 function where_in($key = NULL, $values = NULL)
499 {
500 return $this->_where_in($key, $values);
501 }
502
503 // --------------------------------------------------------------------
504
505 /**
506 * Where_in_or
507 *
508 * Generates a WHERE field IN ('item', 'item') SQL query joined with
509 * OR if appropriate
510 *
511 * @access public
512 * @param string The field to search
513 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000514 * @return object
515 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000516 function or_where_in($key = NULL, $values = NULL)
Derek Allard5fe155e2008-05-12 19:14:57 +0000517 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000518 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000519 }
520
521 // --------------------------------------------------------------------
522
523 /**
524 * Where_not_in
525 *
526 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
527 * with AND if appropriate
528 *
529 * @access public
530 * @param string The field to search
531 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000532 * @return object
533 */
534 function where_not_in($key = NULL, $values = NULL)
Derek Allard5fe155e2008-05-12 19:14:57 +0000535 {
Derek Allardc6935512007-12-19 14:23:19 +0000536 return $this->_where_in($key, $values, TRUE);
537 }
538
539 // --------------------------------------------------------------------
540
541 /**
542 * Where_not_in_or
543 *
544 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
545 * with OR if appropriate
546 *
547 * @access public
548 * @param string The field to search
549 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000550 * @return object
551 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000552 function or_where_not_in($key = NULL, $values = NULL)
Derek Allard5fe155e2008-05-12 19:14:57 +0000553 {
Derek Jonesd0072432008-05-07 22:06:51 +0000554 return $this->_where_in($key, $values, TRUE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000555 }
556
557 // --------------------------------------------------------------------
558
559 /**
560 * Where_in
561 *
562 * Called by where_in, where_in_or, where_not_in, where_not_in_or
563 *
564 * @access public
565 * @param string The field to search
566 * @param array The values searched on
567 * @param boolean If the statement whould be IN or NOT IN
Derek Allard80dd7022007-12-18 23:55:06 +0000568 * @param string
569 * @return object
570 */
Derek Allard15ddc9d2007-12-20 13:54:39 +0000571 function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard5fe155e2008-05-12 19:14:57 +0000572 {
Derek Jones0b59f272008-05-13 04:22:33 +0000573 if ($key === NULL OR ! is_array($values))
Derek Allard80dd7022007-12-18 23:55:06 +0000574 {
575 return;
576 }
577
Derek Allardc6935512007-12-19 14:23:19 +0000578 $not = ($not) ? ' NOT ' : '';
Derek Allard80dd7022007-12-18 23:55:06 +0000579
580 foreach ($values as $value)
581 {
582 $this->ar_wherein[] = $this->escape($value);
583 }
584
585 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Derek Allard5fe155e2008-05-12 19:14:57 +0000586
Derek Allard9b3e7b52008-02-04 23:20:34 +0000587 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
588
589 $this->ar_where[] = $where_in;
590 if ($this->ar_caching === TRUE)
591 {
592 $this->ar_cache_where[] = $where_in;
593 }
Derek Allard80dd7022007-12-18 23:55:06 +0000594
Derek Allard8f000212008-01-18 14:45:59 +0000595 // reset the array for multiple calls
596 $this->ar_wherein = array();
Derek Allard80dd7022007-12-18 23:55:06 +0000597 return $this;
598 }
599
Derek Allard09de1852007-02-14 01:35:56 +0000600 // --------------------------------------------------------------------
601
602 /**
603 * Like
604 *
605 * Generates a %LIKE% portion of the query. Separates
606 * multiple calls with AND
607 *
608 * @access public
609 * @param mixed
610 * @param mixed
611 * @return object
612 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000613 function like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000614 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000615 return $this->_like($field, $match, 'AND ', $side);
Derek Allard09de1852007-02-14 01:35:56 +0000616 }
Derek Allarde54e3d22007-12-19 15:53:44 +0000617
618 // --------------------------------------------------------------------
619
620 /**
621 * Not Like
622 *
623 * Generates a NOT LIKE portion of the query. Separates
624 * multiple calls with AND
625 *
626 * @access public
627 * @param mixed
628 * @param mixed
629 * @return object
630 */
631 function not_like($field, $match = '', $side = 'both')
632 {
633 return $this->_like($field, $match, 'AND ', $side, ' NOT');
634 }
635
Derek Allard09de1852007-02-14 01:35:56 +0000636 // --------------------------------------------------------------------
637
638 /**
639 * OR Like
640 *
641 * Generates a %LIKE% portion of the query. Separates
642 * multiple calls with OR
643 *
644 * @access public
645 * @param mixed
646 * @param mixed
647 * @return object
648 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000649 function or_like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000650 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000651 return $this->_like($field, $match, 'OR ', $side);
652 }
653
654 // --------------------------------------------------------------------
655
656 /**
Derek Allarde54e3d22007-12-19 15:53:44 +0000657 * OR Not Like
658 *
659 * Generates a NOT LIKE portion of the query. Separates
660 * multiple calls with OR
661 *
662 * @access public
663 * @param mixed
664 * @param mixed
665 * @return object
666 */
667 function or_not_like($field, $match = '', $side = 'both')
668 {
669 return $this->_like($field, $match, 'OR ', $side, 'NOT ');
670 }
671
672 // --------------------------------------------------------------------
673
674 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000675 * orlike() is an alias of or_like()
676 * this function is here for backwards compatibility, as
677 * orlike() has been deprecated
678 */
679 function orlike($field, $match = '', $side = 'both')
680 {
Derek Allard4a310b72008-01-30 21:32:47 +0000681 return $this->or_like($field, $match, $side);
Derek Allard09de1852007-02-14 01:35:56 +0000682 }
683
684 // --------------------------------------------------------------------
685
686 /**
687 * Like
688 *
689 * Called by like() or orlike()
690 *
691 * @access private
692 * @param mixed
693 * @param mixed
694 * @param string
695 * @return object
696 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000697 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard09de1852007-02-14 01:35:56 +0000698 {
Derek Jones0b59f272008-05-13 04:22:33 +0000699 if ( ! is_array($field))
Derek Allard09de1852007-02-14 01:35:56 +0000700 {
701 $field = array($field => $match);
702 }
703
704 foreach ($field as $k => $v)
Derek Allarde54e3d22007-12-19 15:53:44 +0000705 {
706
Derek Allard39b622d2008-01-16 21:10:09 +0000707 $k = $this->_protect_identifiers($k);
708
Derek Allard09de1852007-02-14 01:35:56 +0000709 $prefix = (count($this->ar_like) == 0) ? '' : $type;
Derek Allarde54e3d22007-12-19 15:53:44 +0000710
Derek Allard09de1852007-02-14 01:35:56 +0000711 $v = $this->escape_str($v);
Derek Allarde54e3d22007-12-19 15:53:44 +0000712
Derek Allard218e2bc2007-12-17 21:18:14 +0000713 if ($side == 'before')
714 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000715 $like_statement = $prefix." $k $not LIKE '%{$v}'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000716 }
717 elseif ($side == 'after')
718 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000719 $like_statement = $prefix." $k $not LIKE '{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000720 }
721 else
722 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000723 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000724 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000725
726 $this->ar_like[] = $like_statement;
727 if ($this->ar_caching === TRUE)
728 {
729 $this->ar_cache_like[] = $like_statement;
730 }
731
Derek Allard09de1852007-02-14 01:35:56 +0000732 }
733 return $this;
734 }
735
736 // --------------------------------------------------------------------
737
738 /**
739 * GROUP BY
740 *
741 * @access public
742 * @param string
743 * @return object
744 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000745 function group_by($by)
Derek Allard09de1852007-02-14 01:35:56 +0000746 {
747 if (is_string($by))
748 {
749 $by = explode(',', $by);
750 }
751
752 foreach ($by as $val)
753 {
754 $val = trim($val);
755
756 if ($val != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000757 {
Derek Allard39b622d2008-01-16 21:10:09 +0000758 $this->ar_groupby[] = $this->_protect_identifiers($val);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000759 if ($this->ar_caching === TRUE)
760 {
761 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
762 }
763 }
Derek Allard09de1852007-02-14 01:35:56 +0000764 }
765 return $this;
766 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000767
768 // --------------------------------------------------------------------
769
770 /**
771 * groupby() is an alias of group_by()
772 * this function is here for backwards compatibility, as
773 * groupby() has been deprecated
774 */
775 function groupby($by)
776 {
777 return $this->group_by($by);
778 }
779
Derek Allard09de1852007-02-14 01:35:56 +0000780 // --------------------------------------------------------------------
781
782 /**
783 * Sets the HAVING value
784 *
785 * Separates multiple calls with AND
786 *
787 * @access public
788 * @param string
789 * @param string
790 * @return object
791 */
Derek Allarde808aac2008-04-06 18:22:00 +0000792 function having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000793 {
Derek Allarde808aac2008-04-06 18:22:00 +0000794 return $this->_having($key, $value, 'AND ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000795 }
Derek Allard43e8cbf2008-03-01 23:14:43 +0000796
797 // --------------------------------------------------------------------
798
799 /**
800 * orhaving() is an alias of or_having()
801 * this function is here for backwards compatibility, as
802 * orhaving() has been deprecated
803 */
804
Derek Allarde808aac2008-04-06 18:22:00 +0000805 function orhaving($key, $value = '', $escape = TRUE)
Derek Allard43e8cbf2008-03-01 23:14:43 +0000806 {
Derek Allarda459b462008-05-22 13:01:39 +0000807 return $this->or_having($key, $value, $escape);
Derek Allard43e8cbf2008-03-01 23:14:43 +0000808 }
Derek Allard09de1852007-02-14 01:35:56 +0000809 // --------------------------------------------------------------------
810
811 /**
812 * Sets the OR HAVING value
813 *
814 * Separates multiple calls with OR
815 *
816 * @access public
817 * @param string
818 * @param string
819 * @return object
820 */
Derek Allarde808aac2008-04-06 18:22:00 +0000821 function or_having($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000822 {
Derek Allarde808aac2008-04-06 18:22:00 +0000823 return $this->_having($key, $value, 'OR ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000824 }
825
826 // --------------------------------------------------------------------
827
828 /**
829 * Sets the HAVING values
830 *
831 * Called by having() or orhaving()
832 *
833 * @access private
834 * @param string
Derek Allard9b3e7b52008-02-04 23:20:34 +0000835
Derek Allard09de1852007-02-14 01:35:56 +0000836 * @param string
837 * @return object
838 */
Derek Allarde808aac2008-04-06 18:22:00 +0000839 function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000840 {
Derek Jones0b59f272008-05-13 04:22:33 +0000841 if ( ! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000842 {
843 $key = array($key => $value);
844 }
845
846 foreach ($key as $k => $v)
847 {
848 $prefix = (count($this->ar_having) == 0) ? '' : $type;
Derek Allarde808aac2008-04-06 18:22:00 +0000849
850 if ($escape === TRUE)
851 {
852 $k = $this->_protect_identifiers($k);
853 }
854
Derek Allarda459b462008-05-22 13:01:39 +0000855 if ( ! $this->_has_operator($k))
856 {
857 $k .= ' = ';
858 }
859
Derek Allard09de1852007-02-14 01:35:56 +0000860 if ($v != '')
861 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000862 $v = ' '.$this->escape_str($v);
Derek Allard09de1852007-02-14 01:35:56 +0000863 }
864
865 $this->ar_having[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000866 if ($this->ar_caching === TRUE)
867 {
868 $this->ar_cache_having[] = $prefix.$k.$v;
869 }
Derek Allard09de1852007-02-14 01:35:56 +0000870 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000871
Derek Allard09de1852007-02-14 01:35:56 +0000872 return $this;
873 }
874
875 // --------------------------------------------------------------------
876
877 /**
878 * Sets the ORDER BY value
879 *
880 * @access public
881 * @param string
882 * @param string direction: asc or desc
883 * @return object
884 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000885 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000886 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000887 if (strtolower($direction) == 'random')
888 {
889 $orderby = ''; // Random results want or don't need a field name
890 $direction = $this->_random_keyword;
891 }
892 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000893 {
Derek Allard92782492007-08-10 11:26:01 +0000894 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000895 }
896
Derek Allard9b3e7b52008-02-04 23:20:34 +0000897 $orderby_statement = $this->_protect_identifiers($orderby, TRUE).$direction;
898
899 $this->ar_orderby[] = $orderby_statement;
900 if ($this->ar_caching === TRUE)
901 {
902 $this->ar_cache_orderby[] = $orderby_statement;
903 }
904
Derek Allard09de1852007-02-14 01:35:56 +0000905 return $this;
906 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000907
Derek Allard218e2bc2007-12-17 21:18:14 +0000908 // --------------------------------------------------------------------
909
910 /**
911 * orderby() is an alias of order_by()
912 * this function is here for backwards compatibility, as
913 * orderby() has been deprecated
914 */
915 function orderby($orderby, $direction = '')
916 {
917 return $this->order_by($orderby, $direction);
918 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000919
Derek Allard09de1852007-02-14 01:35:56 +0000920 // --------------------------------------------------------------------
921
922 /**
923 * Sets the LIMIT value
924 *
925 * @access public
926 * @param integer the limit value
927 * @param integer the offset value
928 * @return object
929 */
930 function limit($value, $offset = '')
931 {
932 $this->ar_limit = $value;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000933 if ($this->ar_caching === TRUE)
934 {
935 $this->ar_cache_limit[] = $value;
936 }
937
Derek Allard09de1852007-02-14 01:35:56 +0000938 if ($offset != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000939 {
Derek Allard09de1852007-02-14 01:35:56 +0000940 $this->ar_offset = $offset;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000941 if ($this->ar_caching === TRUE)
942 {
943 $this->ar_cache_offset[] = $offset;
944 }
945 }
Derek Allard09de1852007-02-14 01:35:56 +0000946
947 return $this;
948 }
949
950 // --------------------------------------------------------------------
951
952 /**
953 * Sets the OFFSET value
954 *
955 * @access public
956 * @param integer the offset value
957 * @return object
958 */
Derek Allard9b3e7b52008-02-04 23:20:34 +0000959 function offset($offset)
Derek Allard09de1852007-02-14 01:35:56 +0000960 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000961 $this->ar_offset = $offset;
962 if ($this->ar_caching === TRUE)
963 {
964 $this->ar_cache_offset[] = $offset;
965 }
966
Derek Allard09de1852007-02-14 01:35:56 +0000967 return $this;
968 }
969
970 // --------------------------------------------------------------------
971
972 /**
973 * The "set" function. Allows key/value pairs to be set for inserting or updating
974 *
975 * @access public
976 * @param mixed
977 * @param string
Derek Allard39b622d2008-01-16 21:10:09 +0000978 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +0000979 * @return object
980 */
Derek Allard39b622d2008-01-16 21:10:09 +0000981 function set($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000982 {
983 $key = $this->_object_to_array($key);
984
Derek Jones0b59f272008-05-13 04:22:33 +0000985 if ( ! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000986 {
987 $key = array($key => $value);
988 }
989
990 foreach ($key as $k => $v)
991 {
Derek Allard39b622d2008-01-16 21:10:09 +0000992 if ($escape === FALSE)
993 {
994 $this->ar_set[$this->_protect_identifiers($k)] = $v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000995 if ($this->ar_caching === TRUE)
996 {
997 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $v;
998 }
Derek Allard39b622d2008-01-16 21:10:09 +0000999 }
1000 else
1001 {
1002 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
Derek Allard9b3e7b52008-02-04 23:20:34 +00001003 if ($this->ar_caching === TRUE)
1004 {
1005 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $this->escape($v);
1006 }
Derek Allard39b622d2008-01-16 21:10:09 +00001007 }
Derek Allard09de1852007-02-14 01:35:56 +00001008 }
1009
1010 return $this;
1011 }
1012
1013 // --------------------------------------------------------------------
1014
1015 /**
1016 * Get
1017 *
1018 * Compiles the select statement based on the other functions called
1019 * and runs the query
1020 *
1021 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +00001022 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +00001023 * @param string the limit clause
1024 * @param string the offset clause
1025 * @return object
1026 */
1027 function get($table = '', $limit = null, $offset = null)
1028 {
1029 if ($table != '')
1030 {
Derek Allard5e128942007-12-28 21:33:03 +00001031 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001032 $this->from($table);
1033 }
1034
Derek Jones0b59f272008-05-13 04:22:33 +00001035 if ( ! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001036 {
1037 $this->limit($limit, $offset);
1038 }
1039
1040 $sql = $this->_compile_select();
1041
1042 $result = $this->query($sql);
1043 $this->_reset_select();
1044 return $result;
1045 }
1046
Derek Allard09de1852007-02-14 01:35:56 +00001047 /**
Derek Allard694b5b82007-12-18 15:58:03 +00001048 * "Count All Results" query
1049 *
1050 * Generates a platform-specific query string that counts all records
1051 * returned by an Active Record query.
1052 *
1053 * @access public
1054 * @param string
1055 * @return string
1056 */
1057 function count_all_results($table = '')
1058 {
1059 if ($table != '')
1060 {
Derek Allard5e128942007-12-28 21:33:03 +00001061 $this->_track_aliases($table);
Derek Allard694b5b82007-12-18 15:58:03 +00001062 $this->from($table);
1063 }
1064
Derek Allard39b622d2008-01-16 21:10:09 +00001065 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
Derek Allard694b5b82007-12-18 15:58:03 +00001066
1067 $query = $this->query($sql);
1068 $this->_reset_select();
1069
1070 if ($query->num_rows() == 0)
1071 {
1072 return '0';
1073 }
1074
1075 $row = $query->row();
1076 return $row->numrows;
1077 }
1078
1079 // --------------------------------------------------------------------
1080
1081 /**
Derek Allard218e2bc2007-12-17 21:18:14 +00001082 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +00001083 *
1084 * Allows the where clause, limit and offset to be added directly
1085 *
1086 * @access public
1087 * @param string the where clause
1088 * @param string the limit clause
1089 * @param string the offset clause
1090 * @return object
1091 */
Derek Allard218e2bc2007-12-17 21:18:14 +00001092 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +00001093 {
1094 if ($table != '')
1095 {
Derek Allard5e128942007-12-28 21:33:03 +00001096 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001097 $this->from($table);
1098 }
1099
Derek Jones0b59f272008-05-13 04:22:33 +00001100 if ( ! is_null($where))
Derek Allard09de1852007-02-14 01:35:56 +00001101 {
1102 $this->where($where);
1103 }
1104
Derek Jones0b59f272008-05-13 04:22:33 +00001105 if ( ! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001106 {
1107 $this->limit($limit, $offset);
1108 }
1109
1110 $sql = $this->_compile_select();
1111
1112 $result = $this->query($sql);
1113 $this->_reset_select();
1114 return $result;
1115 }
Derek Allard218e2bc2007-12-17 21:18:14 +00001116
1117 // --------------------------------------------------------------------
1118
1119 /**
1120 * getwhere() is an alias of get_where()
1121 * this function is here for backwards compatibility, as
1122 * getwhere() has been deprecated
1123 */
1124 function getwhere($table = '', $where = null, $limit = null, $offset = null)
1125 {
1126 return $this->get_where($table, $where, $limit, $offset);
1127 }
Derek Allard09de1852007-02-14 01:35:56 +00001128
1129 // --------------------------------------------------------------------
1130
1131 /**
1132 * Insert
1133 *
1134 * Compiles an insert string and runs the query
1135 *
1136 * @access public
1137 * @param string the table to retrieve the results from
1138 * @param array an associative array of insert values
1139 * @return object
1140 */
1141 function insert($table = '', $set = NULL)
1142 {
Derek Jones0b59f272008-05-13 04:22:33 +00001143 if ( ! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001144 {
1145 $this->set($set);
1146 }
1147
1148 if (count($this->ar_set) == 0)
1149 {
1150 if ($this->db_debug)
1151 {
1152 return $this->display_error('db_must_use_set');
1153 }
1154 return FALSE;
1155 }
1156
1157 if ($table == '')
1158 {
Derek Jones0b59f272008-05-13 04:22:33 +00001159 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001160 {
1161 if ($this->db_debug)
1162 {
1163 return $this->display_error('db_must_set_table');
1164 }
1165 return FALSE;
1166 }
1167
1168 $table = $this->ar_from[0];
1169 }
Derek Allard39b622d2008-01-16 21:10:09 +00001170
1171 $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 +00001172
1173 $this->_reset_write();
1174 return $this->query($sql);
1175 }
1176
1177 // --------------------------------------------------------------------
1178
1179 /**
1180 * Update
1181 *
1182 * Compiles an update string and runs the query
1183 *
1184 * @access public
1185 * @param string the table to retrieve the results from
1186 * @param array an associative array of update values
1187 * @param mixed the where clause
1188 * @return object
1189 */
Derek Allard5e128942007-12-28 21:33:03 +00001190 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001191 {
Derek Jones0b59f272008-05-13 04:22:33 +00001192 if ( ! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001193 {
1194 $this->set($set);
1195 }
1196
1197 if (count($this->ar_set) == 0)
1198 {
1199 if ($this->db_debug)
1200 {
1201 return $this->display_error('db_must_use_set');
1202 }
1203 return FALSE;
1204 }
1205
1206 if ($table == '')
1207 {
Derek Jones0b59f272008-05-13 04:22:33 +00001208 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001209 {
1210 if ($this->db_debug)
1211 {
1212 return $this->display_error('db_must_set_table');
1213 }
1214 return FALSE;
1215 }
1216
1217 $table = $this->ar_from[0];
1218 }
1219
Derek Allarde77d77c2007-12-19 15:01:55 +00001220 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001221 {
1222 $this->where($where);
1223 }
Derek Allardda6d2402007-12-19 14:49:29 +00001224
Derek Allarde77d77c2007-12-19 15:01:55 +00001225 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +00001226 {
1227 $this->limit($limit);
1228 }
Derek Allard09de1852007-02-14 01:35:56 +00001229
Derek Allard39b622d2008-01-16 21:10:09 +00001230 $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 +00001231
1232 $this->_reset_write();
1233 return $this->query($sql);
1234 }
Derek Allard39b622d2008-01-16 21:10:09 +00001235
1236 // --------------------------------------------------------------------
1237
1238 /**
1239 * Empty Table
1240 *
1241 * Compiles a delete string and runs "DELETE FROM table"
1242 *
1243 * @access public
1244 * @param string the table to empty
1245 * @return object
1246 */
1247 function empty_table($table = '')
1248 {
1249 if ($table == '')
1250 {
Derek Jones0b59f272008-05-13 04:22:33 +00001251 if ( ! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001252 {
1253 if ($this->db_debug)
1254 {
1255 return $this->display_error('db_must_set_table');
1256 }
1257 return FALSE;
1258 }
1259
1260 $table = $this->ar_from[0];
1261 }
1262 else
1263 {
1264 $table = $this->_protect_identifiers($this->dbprefix.$table);
1265 }
1266
1267
1268 $sql = $this->_delete($table);
1269
1270 $this->_reset_write();
1271
1272 return $this->query($sql);
1273 }
1274
1275 // --------------------------------------------------------------------
1276
1277 /**
1278 * Truncate
1279 *
1280 * Compiles a truncate string and runs the query
1281 * If the database does not support the truncate() command
1282 * This function maps to "DELETE FROM table"
1283 *
1284 * @access public
1285 * @param string the table to truncate
1286 * @return object
1287 */
1288 function truncate($table = '')
1289 {
1290 if ($table == '')
1291 {
Derek Jones0b59f272008-05-13 04:22:33 +00001292 if ( ! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001293 {
1294 if ($this->db_debug)
1295 {
1296 return $this->display_error('db_must_set_table');
1297 }
1298 return FALSE;
1299 }
1300
1301 $table = $this->ar_from[0];
1302 }
1303 else
1304 {
1305 $table = $this->_protect_identifiers($this->dbprefix.$table);
1306 }
1307
1308
1309 $sql = $this->_truncate($table);
1310
1311 $this->_reset_write();
1312
1313 return $this->query($sql);
1314 }
Derek Allard09de1852007-02-14 01:35:56 +00001315
1316 // --------------------------------------------------------------------
1317
1318 /**
1319 * Delete
1320 *
1321 * Compiles a delete string and runs the query
1322 *
1323 * @access public
Derek Allard41f60d42007-12-20 20:09:22 +00001324 * @param mixed the table(s) to delete from. String or array
Derek Allard09de1852007-02-14 01:35:56 +00001325 * @param mixed the where clause
Derek Allard41f60d42007-12-20 20:09:22 +00001326 * @param mixed the limit clause
1327 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +00001328 * @return object
1329 */
Derek Allard41f60d42007-12-20 20:09:22 +00001330 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +00001331 {
1332 if ($table == '')
1333 {
Derek Jones0b59f272008-05-13 04:22:33 +00001334 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001335 {
1336 if ($this->db_debug)
1337 {
1338 return $this->display_error('db_must_set_table');
1339 }
1340 return FALSE;
1341 }
Derek Allard39b622d2008-01-16 21:10:09 +00001342
Derek Allard09de1852007-02-14 01:35:56 +00001343 $table = $this->ar_from[0];
1344 }
Derek Allard39b622d2008-01-16 21:10:09 +00001345 elseif (is_array($table))
Derek Allard41f60d42007-12-20 20:09:22 +00001346 {
1347 foreach($table as $single_table)
1348 {
Derek Allard39b622d2008-01-16 21:10:09 +00001349 $this->delete($single_table, $where, $limit, FALSE);
Derek Allard41f60d42007-12-20 20:09:22 +00001350 }
Derek Allard39b622d2008-01-16 21:10:09 +00001351
Derek Allard41f60d42007-12-20 20:09:22 +00001352 $this->_reset_write();
1353 return;
1354 }
Derek Allard39b622d2008-01-16 21:10:09 +00001355 else
1356 {
1357 $table = $this->_protect_identifiers($this->dbprefix.$table);
1358 }
Derek Allard41f60d42007-12-20 20:09:22 +00001359
Derek Allard09de1852007-02-14 01:35:56 +00001360 if ($where != '')
1361 {
1362 $this->where($where);
1363 }
1364
Derek Allarde77d77c2007-12-19 15:01:55 +00001365 if ($limit != NULL)
1366 {
1367 $this->limit($limit);
1368 }
1369
Derek Allard39b622d2008-01-16 21:10:09 +00001370 if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
Derek Allard09de1852007-02-14 01:35:56 +00001371 {
1372 if ($this->db_debug)
1373 {
1374 return $this->display_error('db_del_must_use_where');
1375 }
Derek Allard39b622d2008-01-16 21:10:09 +00001376
Derek Allard09de1852007-02-14 01:35:56 +00001377 return FALSE;
1378 }
Derek Allard41f60d42007-12-20 20:09:22 +00001379
Derek Allard39b622d2008-01-16 21:10:09 +00001380 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001381
Derek Allard41f60d42007-12-20 20:09:22 +00001382 if ($reset_data)
1383 {
1384 $this->_reset_write();
1385 }
Derek Allard39b622d2008-01-16 21:10:09 +00001386
Derek Allard09de1852007-02-14 01:35:56 +00001387 return $this->query($sql);
1388 }
Derek Allard15ddc9d2007-12-20 13:54:39 +00001389
Derek Allard09de1852007-02-14 01:35:56 +00001390 // --------------------------------------------------------------------
1391
1392 /**
1393 * Use Table - DEPRECATED
1394 *
1395 * @deprecated use $this->db->from instead
1396 */
1397 function use_table($table)
1398 {
1399 return $this->from($table);
Derek Allard09de1852007-02-14 01:35:56 +00001400 }
Derek Allard09de1852007-02-14 01:35:56 +00001401
Derek Allard09de1852007-02-14 01:35:56 +00001402 // --------------------------------------------------------------------
1403
1404 /**
Derek Allard5e128942007-12-28 21:33:03 +00001405 * Track Aliases
1406 *
1407 * Used to track SQL statements written with aliased tables.
1408 *
1409 * @access private
1410 * @param string The table to inspect
1411 * @return string
1412 */
1413 function _track_aliases($table)
1414 {
1415 // if a table alias is used we can recognize it by a space
1416 if (strpos($table, " ") !== FALSE)
1417 {
1418 // if the alias is written with the AS keyowrd, get it out
Derek Allard39b622d2008-01-16 21:10:09 +00001419 $table = preg_replace('/ AS /i', ' ', $table);
Derek Allard5e128942007-12-28 21:33:03 +00001420
Derek Allard39b622d2008-01-16 21:10:09 +00001421 $this->ar_aliased_tables[] = trim(strrchr($table, " "));
Derek Allard5e128942007-12-28 21:33:03 +00001422 }
1423
1424 return $this->dbprefix.$table;
1425 }
1426
1427 // --------------------------------------------------------------------
1428
1429 /**
1430 * Filter Table Aliases
1431 *
1432 * Intelligently removes database prefixes from aliased tables
1433 *
1434 * @access private
1435 * @param array An array of compiled SQL
1436 * @return array Cleaned up statement with aliases accounted for
1437 */
1438 function _filter_table_aliases($statements)
1439 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001440
Derek Allard39b622d2008-01-16 21:10:09 +00001441 foreach ($statements as $k => $v)
Derek Allard5e128942007-12-28 21:33:03 +00001442 {
Derek Allard39b622d2008-01-16 21:10:09 +00001443 foreach ($this->ar_aliased_tables as $table)
Derek Allard5e128942007-12-28 21:33:03 +00001444 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001445 $statements[$k] = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $statements[$k]); // makes `table.field`
1446 $statements[$k] = str_replace($this->dbprefix.$table.'.', $table.'.', $statements[$k]);
Derek Allard5e128942007-12-28 21:33:03 +00001447 }
Derek Allard5e128942007-12-28 21:33:03 +00001448 }
Derek Allard39b622d2008-01-16 21:10:09 +00001449 return $statements;
Derek Allard5e128942007-12-28 21:33:03 +00001450 }
1451
1452 // --------------------------------------------------------------------
1453
1454 /**
Derek Allard09de1852007-02-14 01:35:56 +00001455 * Compile the SELECT statement
1456 *
1457 * Generates a query string based on which functions were used.
1458 * Should not be called directly. The get() function calls it.
1459 *
1460 * @access private
1461 * @return string
1462 */
Derek Allard694b5b82007-12-18 15:58:03 +00001463 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001464 {
Derek Allard78255262008-02-06 13:54:23 +00001465 $this->_merge_cache();
Derek Allard9b3e7b52008-02-04 23:20:34 +00001466
Derek Jones0b59f272008-05-13 04:22:33 +00001467 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Derek Allard09de1852007-02-14 01:35:56 +00001468
Derek Allard5162fc72008-04-15 20:05:37 +00001469 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->_filter_table_aliases($this->ar_select));
Derek Allard09de1852007-02-14 01:35:56 +00001470
Derek Allard694b5b82007-12-18 15:58:03 +00001471 if ($select_override !== FALSE)
1472 {
1473 $sql = $select_override;
1474 }
1475
Derek Allard09de1852007-02-14 01:35:56 +00001476 if (count($this->ar_from) > 0)
1477 {
1478 $sql .= "\nFROM ";
Derek Jonesc6ad0232008-01-29 18:44:54 +00001479 $sql .= $this->_from_tables($this->ar_from);
Derek Allard09de1852007-02-14 01:35:56 +00001480 }
1481
1482 if (count($this->ar_join) > 0)
Derek Allard5e128942007-12-28 21:33:03 +00001483 {
Derek Allard09de1852007-02-14 01:35:56 +00001484 $sql .= "\n";
Derek Allard5e128942007-12-28 21:33:03 +00001485
1486 // special consideration for table aliases
1487 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1488 {
1489 $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
1490 }
1491 else
1492 {
1493 $sql .= implode("\n", $this->ar_join);
1494 }
1495
Derek Allard09de1852007-02-14 01:35:56 +00001496 }
1497
1498 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1499 {
1500 $sql .= "\nWHERE ";
1501 }
1502
1503 $sql .= implode("\n", $this->ar_where);
1504
1505 if (count($this->ar_like) > 0)
1506 {
1507 if (count($this->ar_where) > 0)
1508 {
1509 $sql .= " AND ";
1510 }
1511
1512 $sql .= implode("\n", $this->ar_like);
1513 }
1514
1515 if (count($this->ar_groupby) > 0)
1516 {
Derek Allard5e128942007-12-28 21:33:03 +00001517
Derek Allard09de1852007-02-14 01:35:56 +00001518 $sql .= "\nGROUP BY ";
Derek Allard5e128942007-12-28 21:33:03 +00001519
1520 // special consideration for table aliases
1521 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1522 {
1523 $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
1524 }
1525 else
1526 {
1527 $sql .= implode(', ', $this->ar_groupby);
1528 }
Derek Allard09de1852007-02-14 01:35:56 +00001529 }
1530
1531 if (count($this->ar_having) > 0)
1532 {
1533 $sql .= "\nHAVING ";
1534 $sql .= implode("\n", $this->ar_having);
1535 }
1536
1537 if (count($this->ar_orderby) > 0)
1538 {
1539 $sql .= "\nORDER BY ";
1540 $sql .= implode(', ', $this->ar_orderby);
1541
1542 if ($this->ar_order !== FALSE)
1543 {
1544 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1545 }
1546 }
1547
1548 if (is_numeric($this->ar_limit))
1549 {
1550 $sql .= "\n";
1551 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1552 }
1553
1554 return $sql;
1555 }
1556
1557 // --------------------------------------------------------------------
1558
1559 /**
1560 * Object to Array
1561 *
1562 * Takes an object as input and converts the class variables to array key/vals
1563 *
1564 * @access public
1565 * @param object
1566 * @return array
1567 */
1568 function _object_to_array($object)
1569 {
Derek Jones0b59f272008-05-13 04:22:33 +00001570 if ( ! is_object($object))
Derek Allard09de1852007-02-14 01:35:56 +00001571 {
1572 return $object;
1573 }
1574
1575 $array = array();
1576 foreach (get_object_vars($object) as $key => $val)
1577 {
Derek Allard848b7762007-12-31 16:43:05 +00001578 // There are some built in keys we need to ignore for this conversion
Derek Jones0b59f272008-05-13 04:22:33 +00001579 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
Derek Allard848b7762007-12-31 16:43:05 +00001580
Derek Allard09de1852007-02-14 01:35:56 +00001581 {
1582 $array[$key] = $val;
1583 }
1584 }
1585
1586 return $array;
1587 }
1588
1589 // --------------------------------------------------------------------
1590
1591 /**
Derek Allard9b3e7b52008-02-04 23:20:34 +00001592 * Start Cache
1593 *
1594 * Starts AR caching
1595 *
1596 * @access public
1597 * @return void
1598 */
1599 function start_cache()
1600 {
1601 $this->ar_caching = TRUE;
1602 }
1603
1604 // --------------------------------------------------------------------
1605
1606 /**
1607 * Stop Cache
1608 *
1609 * Stops AR caching
1610 *
1611 * @access public
1612 * @return void
1613 */
1614 function stop_cache()
1615 {
1616 $this->ar_caching = FALSE;
1617 }
1618
1619
1620 // --------------------------------------------------------------------
1621
1622 /**
1623 * Flush Cache
1624 *
1625 * Empties the AR cache
1626 *
1627 * @access public
1628 * @return void
1629 */
1630 function flush_cache()
1631 {
1632 $ar_reset_items = array(
1633 'ar_cache_select' => array(),
1634 'ar_cache_from' => array(),
1635 'ar_cache_join' => array(),
1636 'ar_cache_where' => array(),
1637 'ar_cache_like' => array(),
1638 'ar_cache_groupby' => array(),
1639 'ar_cache_having' =>array(),
1640 'ar_cache_orderby' => array(),
1641 'ar_cache_set' => array()
1642 );
1643
1644 $this->_reset_run($ar_reset_items);
1645 }
1646
1647 // --------------------------------------------------------------------
1648
1649 /**
1650 * Merge Cache
1651 *
1652 * When called, this function merges any cached AR arrays with
1653 * locally called ones.
1654 *
1655 * @access private
1656 * @return void
1657 */
1658 function _merge_cache()
1659 {
1660 $ar_items = array('select', 'from', 'join', 'where', 'like', 'groupby', 'having', 'orderby', 'set');
1661
1662 foreach ($ar_items as $ar_item)
1663 {
1664 $ar_cache_item = 'ar_cache_'.$ar_item;
1665 $ar_item = 'ar_'.$ar_item;
1666 $this->$ar_item = array_unique(array_merge($this->$ar_item, $this->$ar_cache_item));
1667 }
1668 }
1669
1670 // --------------------------------------------------------------------
1671
1672 /**
1673 * Resets the active record values. Called by the get() function
1674 *
1675 * @access private
1676 * @param array An array of fields to reset
1677 * @return void
1678 */
1679 function _reset_run($ar_reset_items)
1680 {
1681 foreach ($ar_reset_items as $item => $default_value)
1682 {
Derek Jones0b59f272008-05-13 04:22:33 +00001683 if ( ! in_array($item, $this->ar_store_array))
Derek Allard9b3e7b52008-02-04 23:20:34 +00001684 {
1685 $this->$item = $default_value;
1686 }
1687 }
1688 }
1689
1690 // --------------------------------------------------------------------
1691
1692 /**
Derek Allard09de1852007-02-14 01:35:56 +00001693 * Resets the active record values. Called by the get() function
1694 *
1695 * @access private
1696 * @return void
1697 */
1698 function _reset_select()
1699 {
Derek Allard9b3e7b52008-02-04 23:20:34 +00001700 $ar_reset_items = array(
1701 'ar_select' => array(),
1702 'ar_from' => array(),
1703 'ar_join' => array(),
1704 'ar_where' => array(),
1705 'ar_like' => array(),
1706 'ar_groupby' => array(),
1707 'ar_having' => array(),
1708 'ar_orderby' => array(),
1709 'ar_wherein' => array(),
1710 'ar_aliased_tables' => array(),
1711 'ar_distinct' => FALSE,
1712 'ar_limit' => FALSE,
1713 'ar_offset' => FALSE,
1714 'ar_order' => FALSE,
1715 );
1716
1717 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001718 }
1719
1720 // --------------------------------------------------------------------
1721
1722 /**
1723 * Resets the active record "write" values.
1724 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001725 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001726 *
1727 * @access private
1728 * @return void
1729 */
1730 function _reset_write()
Derek Allard9b3e7b52008-02-04 23:20:34 +00001731 {
1732 $ar_reset_items = array(
1733 'ar_set' => array(),
1734 'ar_from' => array(),
1735 'ar_where' => array(),
1736 'ar_like' => array(),
1737 'ar_orderby' => array(),
1738 'ar_limit' => FALSE,
1739 'ar_order' => FALSE
1740 );
1741
1742 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001743 }
1744
1745}
Derek Allarda6325892008-05-12 17:51:47 +00001746
1747/* End of file DB_active_rec.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00001748/* Location: ./system/database/DB_active_rec.php */