blob: 3cb39822c275a93012b138c4a364cdc188f0aac1 [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 Allarde808aac2008-04-06 18:22:00 +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 Allard09de1852007-02-14 01:35:56 +0000855
856 if ($v != '')
857 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000858 $v = ' '.$this->escape_str($v);
Derek Allard09de1852007-02-14 01:35:56 +0000859 }
860
861 $this->ar_having[] = $prefix.$k.$v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000862 if ($this->ar_caching === TRUE)
863 {
864 $this->ar_cache_having[] = $prefix.$k.$v;
865 }
Derek Allard09de1852007-02-14 01:35:56 +0000866 }
Derek Allard9b3e7b52008-02-04 23:20:34 +0000867
Derek Allard09de1852007-02-14 01:35:56 +0000868 return $this;
869 }
870
871 // --------------------------------------------------------------------
872
873 /**
874 * Sets the ORDER BY value
875 *
876 * @access public
877 * @param string
878 * @param string direction: asc or desc
879 * @return object
880 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000881 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000882 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000883 if (strtolower($direction) == 'random')
884 {
885 $orderby = ''; // Random results want or don't need a field name
886 $direction = $this->_random_keyword;
887 }
888 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000889 {
Derek Allard92782492007-08-10 11:26:01 +0000890 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000891 }
892
Derek Allard9b3e7b52008-02-04 23:20:34 +0000893 $orderby_statement = $this->_protect_identifiers($orderby, TRUE).$direction;
894
895 $this->ar_orderby[] = $orderby_statement;
896 if ($this->ar_caching === TRUE)
897 {
898 $this->ar_cache_orderby[] = $orderby_statement;
899 }
900
Derek Allard09de1852007-02-14 01:35:56 +0000901 return $this;
902 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000903
Derek Allard218e2bc2007-12-17 21:18:14 +0000904 // --------------------------------------------------------------------
905
906 /**
907 * orderby() is an alias of order_by()
908 * this function is here for backwards compatibility, as
909 * orderby() has been deprecated
910 */
911 function orderby($orderby, $direction = '')
912 {
913 return $this->order_by($orderby, $direction);
914 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000915
Derek Allard09de1852007-02-14 01:35:56 +0000916 // --------------------------------------------------------------------
917
918 /**
919 * Sets the LIMIT value
920 *
921 * @access public
922 * @param integer the limit value
923 * @param integer the offset value
924 * @return object
925 */
926 function limit($value, $offset = '')
927 {
928 $this->ar_limit = $value;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000929 if ($this->ar_caching === TRUE)
930 {
931 $this->ar_cache_limit[] = $value;
932 }
933
Derek Allard09de1852007-02-14 01:35:56 +0000934 if ($offset != '')
Derek Allard9b3e7b52008-02-04 23:20:34 +0000935 {
Derek Allard09de1852007-02-14 01:35:56 +0000936 $this->ar_offset = $offset;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000937 if ($this->ar_caching === TRUE)
938 {
939 $this->ar_cache_offset[] = $offset;
940 }
941 }
Derek Allard09de1852007-02-14 01:35:56 +0000942
943 return $this;
944 }
945
946 // --------------------------------------------------------------------
947
948 /**
949 * Sets the OFFSET value
950 *
951 * @access public
952 * @param integer the offset value
953 * @return object
954 */
Derek Allard9b3e7b52008-02-04 23:20:34 +0000955 function offset($offset)
Derek Allard09de1852007-02-14 01:35:56 +0000956 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000957 $this->ar_offset = $offset;
958 if ($this->ar_caching === TRUE)
959 {
960 $this->ar_cache_offset[] = $offset;
961 }
962
Derek Allard09de1852007-02-14 01:35:56 +0000963 return $this;
964 }
965
966 // --------------------------------------------------------------------
967
968 /**
969 * The "set" function. Allows key/value pairs to be set for inserting or updating
970 *
971 * @access public
972 * @param mixed
973 * @param string
Derek Allard39b622d2008-01-16 21:10:09 +0000974 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +0000975 * @return object
976 */
Derek Allard39b622d2008-01-16 21:10:09 +0000977 function set($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000978 {
979 $key = $this->_object_to_array($key);
980
Derek Jones0b59f272008-05-13 04:22:33 +0000981 if ( ! is_array($key))
Derek Allard09de1852007-02-14 01:35:56 +0000982 {
983 $key = array($key => $value);
984 }
985
986 foreach ($key as $k => $v)
987 {
Derek Allard39b622d2008-01-16 21:10:09 +0000988 if ($escape === FALSE)
989 {
990 $this->ar_set[$this->_protect_identifiers($k)] = $v;
Derek Allard9b3e7b52008-02-04 23:20:34 +0000991 if ($this->ar_caching === TRUE)
992 {
993 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $v;
994 }
Derek Allard39b622d2008-01-16 21:10:09 +0000995 }
996 else
997 {
998 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
Derek Allard9b3e7b52008-02-04 23:20:34 +0000999 if ($this->ar_caching === TRUE)
1000 {
1001 $this->ar_cache_offset[$this->_protect_identifiers($k)] = $this->escape($v);
1002 }
Derek Allard39b622d2008-01-16 21:10:09 +00001003 }
Derek Allard09de1852007-02-14 01:35:56 +00001004 }
1005
1006 return $this;
1007 }
1008
1009 // --------------------------------------------------------------------
1010
1011 /**
1012 * Get
1013 *
1014 * Compiles the select statement based on the other functions called
1015 * and runs the query
1016 *
1017 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +00001018 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +00001019 * @param string the limit clause
1020 * @param string the offset clause
1021 * @return object
1022 */
1023 function get($table = '', $limit = null, $offset = null)
1024 {
1025 if ($table != '')
1026 {
Derek Allard5e128942007-12-28 21:33:03 +00001027 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001028 $this->from($table);
1029 }
1030
Derek Jones0b59f272008-05-13 04:22:33 +00001031 if ( ! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001032 {
1033 $this->limit($limit, $offset);
1034 }
1035
1036 $sql = $this->_compile_select();
1037
1038 $result = $this->query($sql);
1039 $this->_reset_select();
1040 return $result;
1041 }
1042
Derek Allard09de1852007-02-14 01:35:56 +00001043 /**
Derek Allard694b5b82007-12-18 15:58:03 +00001044 * "Count All Results" query
1045 *
1046 * Generates a platform-specific query string that counts all records
1047 * returned by an Active Record query.
1048 *
1049 * @access public
1050 * @param string
1051 * @return string
1052 */
1053 function count_all_results($table = '')
1054 {
1055 if ($table != '')
1056 {
Derek Allard5e128942007-12-28 21:33:03 +00001057 $this->_track_aliases($table);
Derek Allard694b5b82007-12-18 15:58:03 +00001058 $this->from($table);
1059 }
1060
Derek Allard39b622d2008-01-16 21:10:09 +00001061 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
Derek Allard694b5b82007-12-18 15:58:03 +00001062
1063 $query = $this->query($sql);
1064 $this->_reset_select();
1065
1066 if ($query->num_rows() == 0)
1067 {
1068 return '0';
1069 }
1070
1071 $row = $query->row();
1072 return $row->numrows;
1073 }
1074
1075 // --------------------------------------------------------------------
1076
1077 /**
Derek Allard218e2bc2007-12-17 21:18:14 +00001078 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +00001079 *
1080 * Allows the where clause, limit and offset to be added directly
1081 *
1082 * @access public
1083 * @param string the where clause
1084 * @param string the limit clause
1085 * @param string the offset clause
1086 * @return object
1087 */
Derek Allard218e2bc2007-12-17 21:18:14 +00001088 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +00001089 {
1090 if ($table != '')
1091 {
Derek Allard5e128942007-12-28 21:33:03 +00001092 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +00001093 $this->from($table);
1094 }
1095
Derek Jones0b59f272008-05-13 04:22:33 +00001096 if ( ! is_null($where))
Derek Allard09de1852007-02-14 01:35:56 +00001097 {
1098 $this->where($where);
1099 }
1100
Derek Jones0b59f272008-05-13 04:22:33 +00001101 if ( ! is_null($limit))
Derek Allard09de1852007-02-14 01:35:56 +00001102 {
1103 $this->limit($limit, $offset);
1104 }
1105
1106 $sql = $this->_compile_select();
1107
1108 $result = $this->query($sql);
1109 $this->_reset_select();
1110 return $result;
1111 }
Derek Allard218e2bc2007-12-17 21:18:14 +00001112
1113 // --------------------------------------------------------------------
1114
1115 /**
1116 * getwhere() is an alias of get_where()
1117 * this function is here for backwards compatibility, as
1118 * getwhere() has been deprecated
1119 */
1120 function getwhere($table = '', $where = null, $limit = null, $offset = null)
1121 {
1122 return $this->get_where($table, $where, $limit, $offset);
1123 }
Derek Allard09de1852007-02-14 01:35:56 +00001124
1125 // --------------------------------------------------------------------
1126
1127 /**
1128 * Insert
1129 *
1130 * Compiles an insert string and runs the query
1131 *
1132 * @access public
1133 * @param string the table to retrieve the results from
1134 * @param array an associative array of insert values
1135 * @return object
1136 */
1137 function insert($table = '', $set = NULL)
1138 {
Derek Jones0b59f272008-05-13 04:22:33 +00001139 if ( ! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001140 {
1141 $this->set($set);
1142 }
1143
1144 if (count($this->ar_set) == 0)
1145 {
1146 if ($this->db_debug)
1147 {
1148 return $this->display_error('db_must_use_set');
1149 }
1150 return FALSE;
1151 }
1152
1153 if ($table == '')
1154 {
Derek Jones0b59f272008-05-13 04:22:33 +00001155 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001156 {
1157 if ($this->db_debug)
1158 {
1159 return $this->display_error('db_must_set_table');
1160 }
1161 return FALSE;
1162 }
1163
1164 $table = $this->ar_from[0];
1165 }
Derek Allard39b622d2008-01-16 21:10:09 +00001166
1167 $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 +00001168
1169 $this->_reset_write();
1170 return $this->query($sql);
1171 }
1172
1173 // --------------------------------------------------------------------
1174
1175 /**
1176 * Update
1177 *
1178 * Compiles an update string and runs the query
1179 *
1180 * @access public
1181 * @param string the table to retrieve the results from
1182 * @param array an associative array of update values
1183 * @param mixed the where clause
1184 * @return object
1185 */
Derek Allard5e128942007-12-28 21:33:03 +00001186 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001187 {
Derek Jones0b59f272008-05-13 04:22:33 +00001188 if ( ! is_null($set))
Derek Allard09de1852007-02-14 01:35:56 +00001189 {
1190 $this->set($set);
1191 }
1192
1193 if (count($this->ar_set) == 0)
1194 {
1195 if ($this->db_debug)
1196 {
1197 return $this->display_error('db_must_use_set');
1198 }
1199 return FALSE;
1200 }
1201
1202 if ($table == '')
1203 {
Derek Jones0b59f272008-05-13 04:22:33 +00001204 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001205 {
1206 if ($this->db_debug)
1207 {
1208 return $this->display_error('db_must_set_table');
1209 }
1210 return FALSE;
1211 }
1212
1213 $table = $this->ar_from[0];
1214 }
1215
Derek Allarde77d77c2007-12-19 15:01:55 +00001216 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001217 {
1218 $this->where($where);
1219 }
Derek Allardda6d2402007-12-19 14:49:29 +00001220
Derek Allarde77d77c2007-12-19 15:01:55 +00001221 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +00001222 {
1223 $this->limit($limit);
1224 }
Derek Allard09de1852007-02-14 01:35:56 +00001225
Derek Allard39b622d2008-01-16 21:10:09 +00001226 $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 +00001227
1228 $this->_reset_write();
1229 return $this->query($sql);
1230 }
Derek Allard39b622d2008-01-16 21:10:09 +00001231
1232 // --------------------------------------------------------------------
1233
1234 /**
1235 * Empty Table
1236 *
1237 * Compiles a delete string and runs "DELETE FROM table"
1238 *
1239 * @access public
1240 * @param string the table to empty
1241 * @return object
1242 */
1243 function empty_table($table = '')
1244 {
1245 if ($table == '')
1246 {
Derek Jones0b59f272008-05-13 04:22:33 +00001247 if ( ! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001248 {
1249 if ($this->db_debug)
1250 {
1251 return $this->display_error('db_must_set_table');
1252 }
1253 return FALSE;
1254 }
1255
1256 $table = $this->ar_from[0];
1257 }
1258 else
1259 {
1260 $table = $this->_protect_identifiers($this->dbprefix.$table);
1261 }
1262
1263
1264 $sql = $this->_delete($table);
1265
1266 $this->_reset_write();
1267
1268 return $this->query($sql);
1269 }
1270
1271 // --------------------------------------------------------------------
1272
1273 /**
1274 * Truncate
1275 *
1276 * Compiles a truncate string and runs the query
1277 * If the database does not support the truncate() command
1278 * This function maps to "DELETE FROM table"
1279 *
1280 * @access public
1281 * @param string the table to truncate
1282 * @return object
1283 */
1284 function truncate($table = '')
1285 {
1286 if ($table == '')
1287 {
Derek Jones0b59f272008-05-13 04:22:33 +00001288 if ( ! isset($this->ar_from[0]))
Derek Allard39b622d2008-01-16 21:10:09 +00001289 {
1290 if ($this->db_debug)
1291 {
1292 return $this->display_error('db_must_set_table');
1293 }
1294 return FALSE;
1295 }
1296
1297 $table = $this->ar_from[0];
1298 }
1299 else
1300 {
1301 $table = $this->_protect_identifiers($this->dbprefix.$table);
1302 }
1303
1304
1305 $sql = $this->_truncate($table);
1306
1307 $this->_reset_write();
1308
1309 return $this->query($sql);
1310 }
Derek Allard09de1852007-02-14 01:35:56 +00001311
1312 // --------------------------------------------------------------------
1313
1314 /**
1315 * Delete
1316 *
1317 * Compiles a delete string and runs the query
1318 *
1319 * @access public
Derek Allard41f60d42007-12-20 20:09:22 +00001320 * @param mixed the table(s) to delete from. String or array
Derek Allard09de1852007-02-14 01:35:56 +00001321 * @param mixed the where clause
Derek Allard41f60d42007-12-20 20:09:22 +00001322 * @param mixed the limit clause
1323 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +00001324 * @return object
1325 */
Derek Allard41f60d42007-12-20 20:09:22 +00001326 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +00001327 {
1328 if ($table == '')
1329 {
Derek Jones0b59f272008-05-13 04:22:33 +00001330 if ( ! isset($this->ar_from[0]))
Derek Allard09de1852007-02-14 01:35:56 +00001331 {
1332 if ($this->db_debug)
1333 {
1334 return $this->display_error('db_must_set_table');
1335 }
1336 return FALSE;
1337 }
Derek Allard39b622d2008-01-16 21:10:09 +00001338
Derek Allard09de1852007-02-14 01:35:56 +00001339 $table = $this->ar_from[0];
1340 }
Derek Allard39b622d2008-01-16 21:10:09 +00001341 elseif (is_array($table))
Derek Allard41f60d42007-12-20 20:09:22 +00001342 {
1343 foreach($table as $single_table)
1344 {
Derek Allard39b622d2008-01-16 21:10:09 +00001345 $this->delete($single_table, $where, $limit, FALSE);
Derek Allard41f60d42007-12-20 20:09:22 +00001346 }
Derek Allard39b622d2008-01-16 21:10:09 +00001347
Derek Allard41f60d42007-12-20 20:09:22 +00001348 $this->_reset_write();
1349 return;
1350 }
Derek Allard39b622d2008-01-16 21:10:09 +00001351 else
1352 {
1353 $table = $this->_protect_identifiers($this->dbprefix.$table);
1354 }
Derek Allard41f60d42007-12-20 20:09:22 +00001355
Derek Allard09de1852007-02-14 01:35:56 +00001356 if ($where != '')
1357 {
1358 $this->where($where);
1359 }
1360
Derek Allarde77d77c2007-12-19 15:01:55 +00001361 if ($limit != NULL)
1362 {
1363 $this->limit($limit);
1364 }
1365
Derek Allard39b622d2008-01-16 21:10:09 +00001366 if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
Derek Allard09de1852007-02-14 01:35:56 +00001367 {
1368 if ($this->db_debug)
1369 {
1370 return $this->display_error('db_del_must_use_where');
1371 }
Derek Allard39b622d2008-01-16 21:10:09 +00001372
Derek Allard09de1852007-02-14 01:35:56 +00001373 return FALSE;
1374 }
Derek Allard41f60d42007-12-20 20:09:22 +00001375
Derek Allard39b622d2008-01-16 21:10:09 +00001376 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001377
Derek Allard41f60d42007-12-20 20:09:22 +00001378 if ($reset_data)
1379 {
1380 $this->_reset_write();
1381 }
Derek Allard39b622d2008-01-16 21:10:09 +00001382
Derek Allard09de1852007-02-14 01:35:56 +00001383 return $this->query($sql);
1384 }
Derek Allard15ddc9d2007-12-20 13:54:39 +00001385
Derek Allard09de1852007-02-14 01:35:56 +00001386 // --------------------------------------------------------------------
1387
1388 /**
1389 * Use Table - DEPRECATED
1390 *
1391 * @deprecated use $this->db->from instead
1392 */
1393 function use_table($table)
1394 {
1395 return $this->from($table);
Derek Allard09de1852007-02-14 01:35:56 +00001396 }
1397
1398 // --------------------------------------------------------------------
1399
1400 /**
Derek Allard09de1852007-02-14 01:35:56 +00001401 * Tests whether the string has an SQL operator
1402 *
1403 * @access private
1404 * @param string
1405 * @return bool
1406 */
1407 function _has_operator($str)
1408 {
1409 $str = trim($str);
Derek Jones0b59f272008-05-13 04:22:33 +00001410 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
Derek Allard09de1852007-02-14 01:35:56 +00001411 {
1412 return FALSE;
1413 }
1414
1415 return TRUE;
1416 }
1417
1418 // --------------------------------------------------------------------
1419
1420 /**
Derek Allard5e128942007-12-28 21:33:03 +00001421 * Track Aliases
1422 *
1423 * Used to track SQL statements written with aliased tables.
1424 *
1425 * @access private
1426 * @param string The table to inspect
1427 * @return string
1428 */
1429 function _track_aliases($table)
1430 {
1431 // if a table alias is used we can recognize it by a space
1432 if (strpos($table, " ") !== FALSE)
1433 {
1434 // if the alias is written with the AS keyowrd, get it out
Derek Allard39b622d2008-01-16 21:10:09 +00001435 $table = preg_replace('/ AS /i', ' ', $table);
Derek Allard5e128942007-12-28 21:33:03 +00001436
Derek Allard39b622d2008-01-16 21:10:09 +00001437 $this->ar_aliased_tables[] = trim(strrchr($table, " "));
Derek Allard5e128942007-12-28 21:33:03 +00001438 }
1439
1440 return $this->dbprefix.$table;
1441 }
1442
1443 // --------------------------------------------------------------------
1444
1445 /**
1446 * Filter Table Aliases
1447 *
1448 * Intelligently removes database prefixes from aliased tables
1449 *
1450 * @access private
1451 * @param array An array of compiled SQL
1452 * @return array Cleaned up statement with aliases accounted for
1453 */
1454 function _filter_table_aliases($statements)
1455 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001456
Derek Allard39b622d2008-01-16 21:10:09 +00001457 foreach ($statements as $k => $v)
Derek Allard5e128942007-12-28 21:33:03 +00001458 {
Derek Allard39b622d2008-01-16 21:10:09 +00001459 foreach ($this->ar_aliased_tables as $table)
Derek Allard5e128942007-12-28 21:33:03 +00001460 {
Derek Allard8b2f19b2008-03-06 15:51:55 +00001461 $statements[$k] = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $statements[$k]); // makes `table.field`
1462 $statements[$k] = str_replace($this->dbprefix.$table.'.', $table.'.', $statements[$k]);
Derek Allard5e128942007-12-28 21:33:03 +00001463 }
Derek Allard5e128942007-12-28 21:33:03 +00001464 }
Derek Allard39b622d2008-01-16 21:10:09 +00001465 return $statements;
Derek Allard5e128942007-12-28 21:33:03 +00001466 }
1467
1468 // --------------------------------------------------------------------
1469
1470 /**
Derek Allard09de1852007-02-14 01:35:56 +00001471 * Compile the SELECT statement
1472 *
1473 * Generates a query string based on which functions were used.
1474 * Should not be called directly. The get() function calls it.
1475 *
1476 * @access private
1477 * @return string
1478 */
Derek Allard694b5b82007-12-18 15:58:03 +00001479 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001480 {
Derek Allard78255262008-02-06 13:54:23 +00001481 $this->_merge_cache();
Derek Allard9b3e7b52008-02-04 23:20:34 +00001482
Derek Jones0b59f272008-05-13 04:22:33 +00001483 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Derek Allard09de1852007-02-14 01:35:56 +00001484
Derek Allard5162fc72008-04-15 20:05:37 +00001485 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->_filter_table_aliases($this->ar_select));
Derek Allard09de1852007-02-14 01:35:56 +00001486
Derek Allard694b5b82007-12-18 15:58:03 +00001487 if ($select_override !== FALSE)
1488 {
1489 $sql = $select_override;
1490 }
1491
Derek Allard09de1852007-02-14 01:35:56 +00001492 if (count($this->ar_from) > 0)
1493 {
1494 $sql .= "\nFROM ";
Derek Jonesc6ad0232008-01-29 18:44:54 +00001495 $sql .= $this->_from_tables($this->ar_from);
Derek Allard09de1852007-02-14 01:35:56 +00001496 }
1497
1498 if (count($this->ar_join) > 0)
Derek Allard5e128942007-12-28 21:33:03 +00001499 {
Derek Allard09de1852007-02-14 01:35:56 +00001500 $sql .= "\n";
Derek Allard5e128942007-12-28 21:33:03 +00001501
1502 // special consideration for table aliases
1503 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1504 {
1505 $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
1506 }
1507 else
1508 {
1509 $sql .= implode("\n", $this->ar_join);
1510 }
1511
Derek Allard09de1852007-02-14 01:35:56 +00001512 }
1513
1514 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1515 {
1516 $sql .= "\nWHERE ";
1517 }
1518
1519 $sql .= implode("\n", $this->ar_where);
1520
1521 if (count($this->ar_like) > 0)
1522 {
1523 if (count($this->ar_where) > 0)
1524 {
1525 $sql .= " AND ";
1526 }
1527
1528 $sql .= implode("\n", $this->ar_like);
1529 }
1530
1531 if (count($this->ar_groupby) > 0)
1532 {
Derek Allard5e128942007-12-28 21:33:03 +00001533
Derek Allard09de1852007-02-14 01:35:56 +00001534 $sql .= "\nGROUP BY ";
Derek Allard5e128942007-12-28 21:33:03 +00001535
1536 // special consideration for table aliases
1537 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1538 {
1539 $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
1540 }
1541 else
1542 {
1543 $sql .= implode(', ', $this->ar_groupby);
1544 }
Derek Allard09de1852007-02-14 01:35:56 +00001545 }
1546
1547 if (count($this->ar_having) > 0)
1548 {
1549 $sql .= "\nHAVING ";
1550 $sql .= implode("\n", $this->ar_having);
1551 }
1552
1553 if (count($this->ar_orderby) > 0)
1554 {
1555 $sql .= "\nORDER BY ";
1556 $sql .= implode(', ', $this->ar_orderby);
1557
1558 if ($this->ar_order !== FALSE)
1559 {
1560 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1561 }
1562 }
1563
1564 if (is_numeric($this->ar_limit))
1565 {
1566 $sql .= "\n";
1567 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1568 }
1569
1570 return $sql;
1571 }
1572
1573 // --------------------------------------------------------------------
1574
1575 /**
1576 * Object to Array
1577 *
1578 * Takes an object as input and converts the class variables to array key/vals
1579 *
1580 * @access public
1581 * @param object
1582 * @return array
1583 */
1584 function _object_to_array($object)
1585 {
Derek Jones0b59f272008-05-13 04:22:33 +00001586 if ( ! is_object($object))
Derek Allard09de1852007-02-14 01:35:56 +00001587 {
1588 return $object;
1589 }
1590
1591 $array = array();
1592 foreach (get_object_vars($object) as $key => $val)
1593 {
Derek Allard848b7762007-12-31 16:43:05 +00001594 // There are some built in keys we need to ignore for this conversion
Derek Jones0b59f272008-05-13 04:22:33 +00001595 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
Derek Allard848b7762007-12-31 16:43:05 +00001596
Derek Allard09de1852007-02-14 01:35:56 +00001597 {
1598 $array[$key] = $val;
1599 }
1600 }
1601
1602 return $array;
1603 }
1604
1605 // --------------------------------------------------------------------
1606
1607 /**
Derek Allard9b3e7b52008-02-04 23:20:34 +00001608 * Start Cache
1609 *
1610 * Starts AR caching
1611 *
1612 * @access public
1613 * @return void
1614 */
1615 function start_cache()
1616 {
1617 $this->ar_caching = TRUE;
1618 }
1619
1620 // --------------------------------------------------------------------
1621
1622 /**
1623 * Stop Cache
1624 *
1625 * Stops AR caching
1626 *
1627 * @access public
1628 * @return void
1629 */
1630 function stop_cache()
1631 {
1632 $this->ar_caching = FALSE;
1633 }
1634
1635
1636 // --------------------------------------------------------------------
1637
1638 /**
1639 * Flush Cache
1640 *
1641 * Empties the AR cache
1642 *
1643 * @access public
1644 * @return void
1645 */
1646 function flush_cache()
1647 {
1648 $ar_reset_items = array(
1649 'ar_cache_select' => array(),
1650 'ar_cache_from' => array(),
1651 'ar_cache_join' => array(),
1652 'ar_cache_where' => array(),
1653 'ar_cache_like' => array(),
1654 'ar_cache_groupby' => array(),
1655 'ar_cache_having' =>array(),
1656 'ar_cache_orderby' => array(),
1657 'ar_cache_set' => array()
1658 );
1659
1660 $this->_reset_run($ar_reset_items);
1661 }
1662
1663 // --------------------------------------------------------------------
1664
1665 /**
1666 * Merge Cache
1667 *
1668 * When called, this function merges any cached AR arrays with
1669 * locally called ones.
1670 *
1671 * @access private
1672 * @return void
1673 */
1674 function _merge_cache()
1675 {
1676 $ar_items = array('select', 'from', 'join', 'where', 'like', 'groupby', 'having', 'orderby', 'set');
1677
1678 foreach ($ar_items as $ar_item)
1679 {
1680 $ar_cache_item = 'ar_cache_'.$ar_item;
1681 $ar_item = 'ar_'.$ar_item;
1682 $this->$ar_item = array_unique(array_merge($this->$ar_item, $this->$ar_cache_item));
1683 }
1684 }
1685
1686 // --------------------------------------------------------------------
1687
1688 /**
1689 * Resets the active record values. Called by the get() function
1690 *
1691 * @access private
1692 * @param array An array of fields to reset
1693 * @return void
1694 */
1695 function _reset_run($ar_reset_items)
1696 {
1697 foreach ($ar_reset_items as $item => $default_value)
1698 {
Derek Jones0b59f272008-05-13 04:22:33 +00001699 if ( ! in_array($item, $this->ar_store_array))
Derek Allard9b3e7b52008-02-04 23:20:34 +00001700 {
1701 $this->$item = $default_value;
1702 }
1703 }
1704 }
1705
1706 // --------------------------------------------------------------------
1707
1708 /**
Derek Allard09de1852007-02-14 01:35:56 +00001709 * Resets the active record values. Called by the get() function
1710 *
1711 * @access private
1712 * @return void
1713 */
1714 function _reset_select()
1715 {
Derek Allard9b3e7b52008-02-04 23:20:34 +00001716 $ar_reset_items = array(
1717 'ar_select' => array(),
1718 'ar_from' => array(),
1719 'ar_join' => array(),
1720 'ar_where' => array(),
1721 'ar_like' => array(),
1722 'ar_groupby' => array(),
1723 'ar_having' => array(),
1724 'ar_orderby' => array(),
1725 'ar_wherein' => array(),
1726 'ar_aliased_tables' => array(),
1727 'ar_distinct' => FALSE,
1728 'ar_limit' => FALSE,
1729 'ar_offset' => FALSE,
1730 'ar_order' => FALSE,
1731 );
1732
1733 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001734 }
1735
1736 // --------------------------------------------------------------------
1737
1738 /**
1739 * Resets the active record "write" values.
1740 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001741 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001742 *
1743 * @access private
1744 * @return void
1745 */
1746 function _reset_write()
Derek Allard9b3e7b52008-02-04 23:20:34 +00001747 {
1748 $ar_reset_items = array(
1749 'ar_set' => array(),
1750 'ar_from' => array(),
1751 'ar_where' => array(),
1752 'ar_like' => array(),
1753 'ar_orderby' => array(),
1754 'ar_limit' => FALSE,
1755 'ar_order' => FALSE
1756 );
1757
1758 $this->_reset_run($ar_reset_items);
Derek Allard09de1852007-02-14 01:35:56 +00001759 }
1760
1761}
Derek Allarda6325892008-05-12 17:51:47 +00001762
1763/* End of file DB_active_rec.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00001764/* Location: ./system/database/DB_active_rec.php */