blob: e93b99e3df2b1cfc01aacfc7eae98ec200b7a342 [file] [log] [blame]
Derek Allard09de1852007-02-14 01:35:56 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
Derek Allardd2df9bc2007-04-15 17:41:17 +00003 * CodeIgniter
Derek Allard09de1852007-02-14 01:35:56 +00004 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Allardcdd2ab22008-01-23 00:05:38 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allard09de1852007-02-14 01:35:56 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Active Record Class
20 *
21 * This is the platform-independent base Active Record implementation class.
22 *
23 * @package CodeIgniter
24 * @subpackage Drivers
25 * @category Database
Derek Allard3d879d52008-01-18 19:41:32 +000026 * @author ExpressionEngine Dev Team
Derek Allardcdd2ab22008-01-23 00:05:38 +000027 * @link http://codeigniter.com/user_guide/database/
Derek Allard09de1852007-02-14 01:35:56 +000028 */
29class CI_DB_active_record extends CI_DB_driver {
30
31 var $ar_select = array();
32 var $ar_distinct = FALSE;
33 var $ar_from = array();
34 var $ar_join = array();
35 var $ar_where = array();
36 var $ar_like = array();
37 var $ar_groupby = array();
38 var $ar_having = array();
39 var $ar_limit = FALSE;
40 var $ar_offset = FALSE;
41 var $ar_order = FALSE;
42 var $ar_orderby = array();
43 var $ar_set = array();
Derek Allard80dd7022007-12-18 23:55:06 +000044 var $ar_wherein = array();
Derek Allard5e128942007-12-28 21:33:03 +000045 var $ar_aliased_tables = array();
Derek Allard09de1852007-02-14 01:35:56 +000046
Derek Allard09de1852007-02-14 01:35:56 +000047 /**
Derek Allard3b118682008-01-22 23:44:32 +000048 * DB Prefix
49 *
50 * Prepends a database prefix if one exists in configuration
51 *
52 * @access public
53 * @param string the table
54 * @return string
55 */
56 function dbprefix($table = '')
57 {
58 if ($table == '')
59 {
60 $this->display_error('db_table_name_required');
61 }
62
63 return $this->dbprefix.$table;
64 }
65
66 // --------------------------------------------------------------------
67
68 /**
Derek Allard09de1852007-02-14 01:35:56 +000069 * Select
70 *
71 * Generates the SELECT portion of the query
72 *
73 * @access public
74 * @param string
75 * @return object
76 */
Derek Allard39b622d2008-01-16 21:10:09 +000077 function select($select = '*', $protect_identifiers = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +000078 {
79 if (is_string($select))
80 {
81 $select = explode(',', $select);
82 }
83
84 foreach ($select as $val)
85 {
86 $val = trim($val);
Derek Allard39b622d2008-01-16 21:10:09 +000087
88 if ($val != '*' && $protect_identifiers !== FALSE)
89 {
90 $val = $this->_protect_identifiers($val);
91 }
Derek Allard09de1852007-02-14 01:35:56 +000092
93 if ($val != '')
Derek Allard39b622d2008-01-16 21:10:09 +000094 {
Derek Allard09de1852007-02-14 01:35:56 +000095 $this->ar_select[] = $val;
Derek Allard39b622d2008-01-16 21:10:09 +000096 }
Derek Allard09de1852007-02-14 01:35:56 +000097 }
98 return $this;
99 }
Derek Allard39b622d2008-01-16 21:10:09 +0000100
101 // --------------------------------------------------------------------
102
103 /**
104 * Select Max
105 *
106 * Generates a SELECT MAX(field) portion of a query
107 *
108 * @access public
109 * @param string the field
110 * @param string an alias
111 * @return object
112 */
113 function select_max($select = '', $alias='')
114 {
115 if (!is_string($select) || $select == '')
116 {
117 $this->display_error('db_invalid_query');
118 }
Derek Allard09de1852007-02-14 01:35:56 +0000119
Derek Allard39b622d2008-01-16 21:10:09 +0000120 $alias = ($alias != '') ? $alias : $select;
121
122 $sql = 'MAX('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
123
124 $this->ar_select[] = $sql;
125
126 return $this;
Derek Allard39b622d2008-01-16 21:10:09 +0000127 }
128
129 // --------------------------------------------------------------------
130
131 /**
132 * Select Min
133 *
134 * Generates a SELECT MIN(field) portion of a query
135 *
136 * @access public
137 * @param string the field
138 * @param string an alias
139 * @return object
140 */
141 function select_min($select = '', $alias='')
142 {
143 if (!is_string($select) || $select == '')
144 {
145 $this->display_error('db_invalid_query');
146 }
147
148 $alias = ($alias != '') ? $alias : $select;
149
150 $sql = 'MIN('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
151
152 $this->ar_select[] = $sql;
153
154 return $this;
155 }
156
157 // --------------------------------------------------------------------
158
159 /**
160 * Select Average
161 *
162 * Generates a SELECT AVG(field) portion of a query
163 *
164 * @access public
165 * @param string the field
166 * @param string an alias
167 * @return object
168 */
169 function select_avg($select = '', $alias='')
170 {
171 if (!is_string($select) || $select == '')
172 {
173 $this->display_error('db_invalid_query');
174 }
175
176 $alias = ($alias != '') ? $alias : $select;
177
178 $sql = 'AVG('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
179
180 $this->ar_select[] = $sql;
181
182 return $this;
183 }
184
185 // --------------------------------------------------------------------
186
187 /**
188 * Select Sum
189 *
190 * Generates a SELECT SUM(field) portion of a query
191 *
192 * @access public
193 * @param string the field
194 * @param string an alias
195 * @return object
196 */
197 function select_sum($select = '', $alias='')
198 {
199 if (!is_string($select) || $select == '')
200 {
201 $this->display_error('db_invalid_query');
202 }
203
204 $alias = ($alias != '') ? $alias : $select;
205
206 $sql = 'SUM('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
207
208 $this->ar_select[] = $sql;
209
210 return $this;
211 }
212
Derek Allard09de1852007-02-14 01:35:56 +0000213 // --------------------------------------------------------------------
214
215 /**
216 * DISTINCT
217 *
218 * Sets a flag which tells the query string compiler to add DISTINCT
219 *
220 * @access public
221 * @param bool
222 * @return object
223 */
224 function distinct($val = TRUE)
225 {
226 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
227 return $this;
228 }
229
230 // --------------------------------------------------------------------
231
232 /**
233 * From
234 *
235 * Generates the FROM portion of the query
236 *
237 * @access public
238 * @param mixed can be a string or array
239 * @return object
240 */
241 function from($from)
242 {
243 foreach ((array)$from as $val)
244 {
Derek Allard39b622d2008-01-16 21:10:09 +0000245 $this->ar_from[] = $this->_protect_identifiers($this->_track_aliases($val));
Derek Allard09de1852007-02-14 01:35:56 +0000246 }
Derek Allard5e128942007-12-28 21:33:03 +0000247
Derek Allard09de1852007-02-14 01:35:56 +0000248 return $this;
249 }
250
251 // --------------------------------------------------------------------
252
253 /**
254 * Join
255 *
256 * Generates the JOIN portion of the query
257 *
258 * @access public
259 * @param string
260 * @param string the join condition
261 * @param string the type of join
262 * @return object
263 */
264 function join($table, $cond, $type = '')
265 {
266 if ($type != '')
267 {
268 $type = strtoupper(trim($type));
269
270 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
271 {
272 $type = '';
273 }
274 else
275 {
276 $type .= ' ';
277 }
278 }
279
Derek Allard09de1852007-02-14 01:35:56 +0000280 // If a DB prefix is used we might need to add it to the column names
281 if ($this->dbprefix)
282 {
Derek Allard39b622d2008-01-16 21:10:09 +0000283 $this->_track_aliases($table);
284
Derek Allard09de1852007-02-14 01:35:56 +0000285 // First we remove any existing prefixes in the condition to avoid duplicates
286 $cond = preg_replace('|('.$this->dbprefix.')([\w\.]+)([\W\s]+)|', "$2$3", $cond);
287
288 // Next we add the prefixes to the condition
289 $cond = preg_replace('|([\w\.]+)([\W\s]+)(.+)|', $this->dbprefix . "$1$2" . $this->dbprefix . "$3", $cond);
Derek Allard5e128942007-12-28 21:33:03 +0000290 }
291
Derek Allard39b622d2008-01-16 21:10:09 +0000292 $this->ar_join[] = $type.'JOIN '.$this->_protect_identifiers($this->dbprefix.$table, TRUE).' ON '.$cond;
Derek Allard09de1852007-02-14 01:35:56 +0000293 return $this;
294 }
295
296 // --------------------------------------------------------------------
297
298 /**
299 * Where
300 *
301 * Generates the WHERE portion of the query. Separates
302 * multiple calls with AND
303 *
304 * @access public
305 * @param mixed
306 * @param mixed
307 * @return object
308 */
Derek Allard39b622d2008-01-16 21:10:09 +0000309 function where($key, $value = NULL, $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000310 {
Derek Allard39b622d2008-01-16 21:10:09 +0000311 return $this->_where($key, $value, 'AND ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000312 }
313
314 // --------------------------------------------------------------------
315
316 /**
317 * OR Where
318 *
319 * Generates the WHERE portion of the query. Separates
320 * multiple calls with OR
321 *
322 * @access public
323 * @param mixed
324 * @param mixed
325 * @return object
326 */
Derek Allard39b622d2008-01-16 21:10:09 +0000327 function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000328 {
Derek Allard39b622d2008-01-16 21:10:09 +0000329 return $this->_where($key, $value, 'OR ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000330 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000331
332 // --------------------------------------------------------------------
333
334 /**
335 * orwhere() is an alias of or_where()
336 * this function is here for backwards compatibility, as
337 * orwhere() has been deprecated
338 */
Derek Allard39b622d2008-01-16 21:10:09 +0000339 function orwhere($key, $value = NULL, $escape = TRUE)
Derek Allard218e2bc2007-12-17 21:18:14 +0000340 {
Derek Allard39b622d2008-01-16 21:10:09 +0000341 return $this->or_where($key, $value, $escape);
Derek Allard218e2bc2007-12-17 21:18:14 +0000342 }
Derek Allard67b44ed2008-01-12 16:18:02 +0000343
344 // --------------------------------------------------------------------
345
346 /**
Derek Allard09de1852007-02-14 01:35:56 +0000347 * Where
348 *
349 * Called by where() or orwhere()
350 *
351 * @access private
352 * @param mixed
353 * @param mixed
354 * @param string
355 * @return object
356 */
Derek Allard39b622d2008-01-16 21:10:09 +0000357 function _where($key, $value = NULL, $type = 'AND ', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000358 {
359 if ( ! is_array($key))
360 {
361 $key = array($key => $value);
362 }
363
364 foreach ($key as $k => $v)
365 {
366 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Derek Allard15ddc9d2007-12-20 13:54:39 +0000367
Derek Allard39b622d2008-01-16 21:10:09 +0000368 if ( ! $this->_has_operator($k) && is_null($key[$k]))
Derek Allard15ddc9d2007-12-20 13:54:39 +0000369 {
370 // value appears not to have been set, assign the test to IS NULL
371 $k .= ' IS NULL';
372 }
Derek Allard09de1852007-02-14 01:35:56 +0000373
374 if ( ! is_null($v))
375 {
Derek Allard39b622d2008-01-16 21:10:09 +0000376
377 if ($escape === TRUE)
378 {
379 // exception for "field<=" keys
380 if ($this->_has_operator($k))
381 {
382 $k = preg_replace("/([A-Za-z_0-9]+)/", $this->_protect_identifiers('$1'), $k);
383 }
384 else
385 {
386 $k = $this->_protect_identifiers($k);
387 }
388 }
389
Derek Allard09de1852007-02-14 01:35:56 +0000390 if ( ! $this->_has_operator($k))
391 {
392 $k .= ' =';
393 }
Derek Allard39b622d2008-01-16 21:10:09 +0000394
Derek Allard09de1852007-02-14 01:35:56 +0000395 $v = ' '.$this->escape($v);
Derek Allard15ddc9d2007-12-20 13:54:39 +0000396
Derek Allard09de1852007-02-14 01:35:56 +0000397 }
398
399 $this->ar_where[] = $prefix.$k.$v;
400 }
401 return $this;
402 }
Derek Allard80dd7022007-12-18 23:55:06 +0000403
404 // --------------------------------------------------------------------
405
406 /**
407 * Where_in
408 *
Derek Allardc6935512007-12-19 14:23:19 +0000409 * Generates a WHERE field IN ('item', 'item') SQL query joined with
410 * AND if appropriate
Derek Allard80dd7022007-12-18 23:55:06 +0000411 *
412 * @access public
413 * @param string The field to search
414 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000415
416 * @return object
417 */
418 function where_in($key = NULL, $values = NULL)
419 {
420 return $this->_where_in($key, $values);
421 }
422
423 // --------------------------------------------------------------------
424
425 /**
426 * Where_in_or
427 *
428 * Generates a WHERE field IN ('item', 'item') SQL query joined with
429 * OR if appropriate
430 *
431 * @access public
432 * @param string The field to search
433 * @param array The values searched on
434
435 * @return object
436 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000437 function or_where_in($key = NULL, $values = NULL)
Derek Allardc6935512007-12-19 14:23:19 +0000438 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000439 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000440 }
441
442 // --------------------------------------------------------------------
443
444 /**
445 * Where_not_in
446 *
447 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
448 * with AND if appropriate
449 *
450 * @access public
451 * @param string The field to search
452 * @param array The values searched on
453
454 * @return object
455 */
456 function where_not_in($key = NULL, $values = NULL)
457 {
458 return $this->_where_in($key, $values, TRUE);
459 }
460
461 // --------------------------------------------------------------------
462
463 /**
464 * Where_not_in_or
465 *
466 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
467 * with OR if appropriate
468 *
469 * @access public
470 * @param string The field to search
471 * @param array The values searched on
472
473 * @return object
474 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000475 function or_where_not_in($key = NULL, $values = NULL)
Derek Allardc6935512007-12-19 14:23:19 +0000476 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000477 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000478 }
479
480 // --------------------------------------------------------------------
481
482 /**
483 * Where_in
484 *
485 * Called by where_in, where_in_or, where_not_in, where_not_in_or
486 *
487 * @access public
488 * @param string The field to search
489 * @param array The values searched on
490 * @param boolean If the statement whould be IN or NOT IN
Derek Allard80dd7022007-12-18 23:55:06 +0000491 * @param string
492 * @return object
493 */
Derek Allard15ddc9d2007-12-20 13:54:39 +0000494 function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard80dd7022007-12-18 23:55:06 +0000495 {
496 if ($key === NULL || !is_array($values))
497 {
498 return;
499 }
500
Derek Allardc6935512007-12-19 14:23:19 +0000501 $not = ($not) ? ' NOT ' : '';
Derek Allard80dd7022007-12-18 23:55:06 +0000502
503 foreach ($values as $value)
504 {
505 $this->ar_wherein[] = $this->escape($value);
506 }
507
508 $prefix = (count($this->ar_where) == 0) ? '' : $type;
509
Derek Allard39b622d2008-01-16 21:10:09 +0000510 $this->ar_where[] = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
Derek Allard80dd7022007-12-18 23:55:06 +0000511
Derek Allard8f000212008-01-18 14:45:59 +0000512 // reset the array for multiple calls
513 $this->ar_wherein = array();
Derek Allard80dd7022007-12-18 23:55:06 +0000514 return $this;
515 }
516
Derek Allard09de1852007-02-14 01:35:56 +0000517 // --------------------------------------------------------------------
518
519 /**
520 * Like
521 *
522 * Generates a %LIKE% portion of the query. Separates
523 * multiple calls with AND
524 *
525 * @access public
526 * @param mixed
527 * @param mixed
528 * @return object
529 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000530 function like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000531 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000532 return $this->_like($field, $match, 'AND ', $side);
Derek Allard09de1852007-02-14 01:35:56 +0000533 }
Derek Allarde54e3d22007-12-19 15:53:44 +0000534
535 // --------------------------------------------------------------------
536
537 /**
538 * Not Like
539 *
540 * Generates a NOT LIKE portion of the query. Separates
541 * multiple calls with AND
542 *
543 * @access public
544 * @param mixed
545 * @param mixed
546 * @return object
547 */
548 function not_like($field, $match = '', $side = 'both')
549 {
550 return $this->_like($field, $match, 'AND ', $side, ' NOT');
551 }
552
Derek Allard09de1852007-02-14 01:35:56 +0000553 // --------------------------------------------------------------------
554
555 /**
556 * OR Like
557 *
558 * Generates a %LIKE% portion of the query. Separates
559 * multiple calls with OR
560 *
561 * @access public
562 * @param mixed
563 * @param mixed
564 * @return object
565 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000566 function or_like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000567 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000568 return $this->_like($field, $match, 'OR ', $side);
569 }
570
571 // --------------------------------------------------------------------
572
573 /**
Derek Allarde54e3d22007-12-19 15:53:44 +0000574 * OR Not Like
575 *
576 * Generates a NOT LIKE portion of the query. Separates
577 * multiple calls with OR
578 *
579 * @access public
580 * @param mixed
581 * @param mixed
582 * @return object
583 */
584 function or_not_like($field, $match = '', $side = 'both')
585 {
586 return $this->_like($field, $match, 'OR ', $side, 'NOT ');
587 }
588
589 // --------------------------------------------------------------------
590
591 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000592 * orlike() is an alias of or_like()
593 * this function is here for backwards compatibility, as
594 * orlike() has been deprecated
595 */
596 function orlike($field, $match = '', $side = 'both')
597 {
Derek Allard4a310b72008-01-30 21:32:47 +0000598 return $this->or_like($field, $match, $side);
Derek Allard09de1852007-02-14 01:35:56 +0000599 }
600
601 // --------------------------------------------------------------------
602
603 /**
604 * Like
605 *
606 * Called by like() or orlike()
607 *
608 * @access private
609 * @param mixed
610 * @param mixed
611 * @param string
612 * @return object
613 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000614 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard09de1852007-02-14 01:35:56 +0000615 {
616 if ( ! is_array($field))
617 {
618 $field = array($field => $match);
619 }
620
621 foreach ($field as $k => $v)
Derek Allarde54e3d22007-12-19 15:53:44 +0000622 {
623
Derek Allard39b622d2008-01-16 21:10:09 +0000624 $k = $this->_protect_identifiers($k);
625
Derek Allard09de1852007-02-14 01:35:56 +0000626 $prefix = (count($this->ar_like) == 0) ? '' : $type;
Derek Allarde54e3d22007-12-19 15:53:44 +0000627
Derek Allard09de1852007-02-14 01:35:56 +0000628 $v = $this->escape_str($v);
Derek Allarde54e3d22007-12-19 15:53:44 +0000629
Derek Allard218e2bc2007-12-17 21:18:14 +0000630 if ($side == 'before')
631 {
Derek Allarde54e3d22007-12-19 15:53:44 +0000632 $this->ar_like[] = $prefix." $k $not LIKE '%{$v}'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000633 }
634 elseif ($side == 'after')
635 {
Derek Allarde54e3d22007-12-19 15:53:44 +0000636 $this->ar_like[] = $prefix." $k $not LIKE '{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000637 }
638 else
639 {
Derek Allarde54e3d22007-12-19 15:53:44 +0000640 $this->ar_like[] = $prefix." $k $not LIKE '%{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000641 }
Derek Allard09de1852007-02-14 01:35:56 +0000642 }
643 return $this;
644 }
645
646 // --------------------------------------------------------------------
647
648 /**
649 * GROUP BY
650 *
651 * @access public
652 * @param string
653 * @return object
654 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000655 function group_by($by)
Derek Allard09de1852007-02-14 01:35:56 +0000656 {
657 if (is_string($by))
658 {
659 $by = explode(',', $by);
660 }
661
662 foreach ($by as $val)
663 {
664 $val = trim($val);
665
666 if ($val != '')
Derek Allard39b622d2008-01-16 21:10:09 +0000667 $this->ar_groupby[] = $this->_protect_identifiers($val);
Derek Allard09de1852007-02-14 01:35:56 +0000668 }
669 return $this;
670 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000671
672 // --------------------------------------------------------------------
673
674 /**
675 * groupby() is an alias of group_by()
676 * this function is here for backwards compatibility, as
677 * groupby() has been deprecated
678 */
679 function groupby($by)
680 {
681 return $this->group_by($by);
682 }
683
Derek Allard09de1852007-02-14 01:35:56 +0000684 // --------------------------------------------------------------------
685
686 /**
687 * Sets the HAVING value
688 *
689 * Separates multiple calls with AND
690 *
691 * @access public
692 * @param string
693 * @param string
694 * @return object
695 */
696 function having($key, $value = '')
697 {
698 return $this->_having($key, $value, 'AND ');
699 }
700
701 // --------------------------------------------------------------------
702
703 /**
704 * Sets the OR HAVING value
705 *
706 * Separates multiple calls with OR
707 *
708 * @access public
709 * @param string
710 * @param string
711 * @return object
712 */
713 function orhaving($key, $value = '')
714 {
715 return $this->_having($key, $value, 'OR ');
716 }
717
718 // --------------------------------------------------------------------
719
720 /**
721 * Sets the HAVING values
722 *
723 * Called by having() or orhaving()
724 *
725 * @access private
726 * @param string
727 * @param string
728 * @return object
729 */
730 function _having($key, $value = '', $type = 'AND ')
731 {
732 if ( ! is_array($key))
733 {
734 $key = array($key => $value);
735 }
736
737 foreach ($key as $k => $v)
738 {
739 $prefix = (count($this->ar_having) == 0) ? '' : $type;
740
741 if ($v != '')
742 {
743 $v = ' '.$this->escape($v);
744 }
745
746 $this->ar_having[] = $prefix.$k.$v;
747 }
748 return $this;
749 }
750
751 // --------------------------------------------------------------------
752
753 /**
754 * Sets the ORDER BY value
755 *
756 * @access public
757 * @param string
758 * @param string direction: asc or desc
759 * @return object
760 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000761 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000762 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000763 if (strtolower($direction) == 'random')
764 {
765 $orderby = ''; // Random results want or don't need a field name
766 $direction = $this->_random_keyword;
767 }
768 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000769 {
Derek Allard92782492007-08-10 11:26:01 +0000770 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000771 }
772
Derek Allard05b3a5d2008-01-17 20:25:46 +0000773 $this->ar_orderby[] = $this->_protect_identifiers($orderby, TRUE).$direction;
Derek Allard09de1852007-02-14 01:35:56 +0000774 return $this;
775 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000776
Derek Allard218e2bc2007-12-17 21:18:14 +0000777 // --------------------------------------------------------------------
778
779 /**
780 * orderby() is an alias of order_by()
781 * this function is here for backwards compatibility, as
782 * orderby() has been deprecated
783 */
784 function orderby($orderby, $direction = '')
785 {
786 return $this->order_by($orderby, $direction);
787 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000788
Derek Allard09de1852007-02-14 01:35:56 +0000789 // --------------------------------------------------------------------
790
791 /**
792 * Sets the LIMIT value
793 *
794 * @access public
795 * @param integer the limit value
796 * @param integer the offset value
797 * @return object
798 */
799 function limit($value, $offset = '')
800 {
801 $this->ar_limit = $value;
802
803 if ($offset != '')
804 $this->ar_offset = $offset;
805
806 return $this;
807 }
808
809 // --------------------------------------------------------------------
810
811 /**
812 * Sets the OFFSET value
813 *
814 * @access public
815 * @param integer the offset value
816 * @return object
817 */
818 function offset($value)
819 {
820 $this->ar_offset = $value;
821 return $this;
822 }
823
824 // --------------------------------------------------------------------
825
826 /**
827 * The "set" function. Allows key/value pairs to be set for inserting or updating
828 *
829 * @access public
830 * @param mixed
831 * @param string
Derek Allard39b622d2008-01-16 21:10:09 +0000832 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +0000833 * @return object
834 */
Derek Allard39b622d2008-01-16 21:10:09 +0000835 function set($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000836 {
837 $key = $this->_object_to_array($key);
838
839 if ( ! is_array($key))
840 {
841 $key = array($key => $value);
842 }
843
844 foreach ($key as $k => $v)
845 {
Derek Allard39b622d2008-01-16 21:10:09 +0000846 if ($escape === FALSE)
847 {
848 $this->ar_set[$this->_protect_identifiers($k)] = $v;
849 }
850 else
851 {
852 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
853 }
854
Derek Allard09de1852007-02-14 01:35:56 +0000855 }
856
857 return $this;
858 }
859
860 // --------------------------------------------------------------------
861
862 /**
863 * Get
864 *
865 * Compiles the select statement based on the other functions called
866 * and runs the query
867 *
868 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +0000869 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +0000870 * @param string the limit clause
871 * @param string the offset clause
872 * @return object
873 */
874 function get($table = '', $limit = null, $offset = null)
875 {
876 if ($table != '')
877 {
Derek Allard5e128942007-12-28 21:33:03 +0000878 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +0000879 $this->from($table);
880 }
881
882 if ( ! is_null($limit))
883 {
884 $this->limit($limit, $offset);
885 }
886
887 $sql = $this->_compile_select();
888
889 $result = $this->query($sql);
890 $this->_reset_select();
891 return $result;
892 }
893
Derek Allard09de1852007-02-14 01:35:56 +0000894 /**
Derek Allard694b5b82007-12-18 15:58:03 +0000895 * "Count All Results" query
896 *
897 * Generates a platform-specific query string that counts all records
898 * returned by an Active Record query.
899 *
900 * @access public
901 * @param string
902 * @return string
903 */
904 function count_all_results($table = '')
905 {
906 if ($table != '')
907 {
Derek Allard5e128942007-12-28 21:33:03 +0000908 $this->_track_aliases($table);
Derek Allard694b5b82007-12-18 15:58:03 +0000909 $this->from($table);
910 }
911
Derek Allard39b622d2008-01-16 21:10:09 +0000912 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
Derek Allard694b5b82007-12-18 15:58:03 +0000913
914 $query = $this->query($sql);
915 $this->_reset_select();
916
917 if ($query->num_rows() == 0)
918 {
919 return '0';
920 }
921
922 $row = $query->row();
923 return $row->numrows;
924 }
925
926 // --------------------------------------------------------------------
927
928 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000929 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +0000930 *
931 * Allows the where clause, limit and offset to be added directly
932 *
933 * @access public
934 * @param string the where clause
935 * @param string the limit clause
936 * @param string the offset clause
937 * @return object
938 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000939 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +0000940 {
941 if ($table != '')
942 {
Derek Allard5e128942007-12-28 21:33:03 +0000943 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +0000944 $this->from($table);
945 }
946
947 if ( ! is_null($where))
948 {
949 $this->where($where);
950 }
951
952 if ( ! is_null($limit))
953 {
954 $this->limit($limit, $offset);
955 }
956
957 $sql = $this->_compile_select();
958
959 $result = $this->query($sql);
960 $this->_reset_select();
961 return $result;
962 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000963
964 // --------------------------------------------------------------------
965
966 /**
967 * getwhere() is an alias of get_where()
968 * this function is here for backwards compatibility, as
969 * getwhere() has been deprecated
970 */
971 function getwhere($table = '', $where = null, $limit = null, $offset = null)
972 {
973 return $this->get_where($table, $where, $limit, $offset);
974 }
Derek Allard09de1852007-02-14 01:35:56 +0000975
976 // --------------------------------------------------------------------
977
978 /**
979 * Insert
980 *
981 * Compiles an insert string and runs the query
982 *
983 * @access public
984 * @param string the table to retrieve the results from
985 * @param array an associative array of insert values
986 * @return object
987 */
988 function insert($table = '', $set = NULL)
989 {
990 if ( ! is_null($set))
991 {
992 $this->set($set);
993 }
994
995 if (count($this->ar_set) == 0)
996 {
997 if ($this->db_debug)
998 {
999 return $this->display_error('db_must_use_set');
1000 }
1001 return FALSE;
1002 }
1003
1004 if ($table == '')
1005 {
1006 if ( ! isset($this->ar_from[0]))
1007 {
1008 if ($this->db_debug)
1009 {
1010 return $this->display_error('db_must_set_table');
1011 }
1012 return FALSE;
1013 }
1014
1015 $table = $this->ar_from[0];
1016 }
Derek Allard39b622d2008-01-16 21:10:09 +00001017
1018 $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 +00001019
1020 $this->_reset_write();
1021 return $this->query($sql);
1022 }
1023
1024 // --------------------------------------------------------------------
1025
1026 /**
1027 * Update
1028 *
1029 * Compiles an update string and runs the query
1030 *
1031 * @access public
1032 * @param string the table to retrieve the results from
1033 * @param array an associative array of update values
1034 * @param mixed the where clause
1035 * @return object
1036 */
Derek Allard5e128942007-12-28 21:33:03 +00001037 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001038 {
1039 if ( ! is_null($set))
1040 {
1041 $this->set($set);
1042 }
1043
1044 if (count($this->ar_set) == 0)
1045 {
1046 if ($this->db_debug)
1047 {
1048 return $this->display_error('db_must_use_set');
1049 }
1050 return FALSE;
1051 }
1052
1053 if ($table == '')
1054 {
1055 if ( ! isset($this->ar_from[0]))
1056 {
1057 if ($this->db_debug)
1058 {
1059 return $this->display_error('db_must_set_table');
1060 }
1061 return FALSE;
1062 }
1063
1064 $table = $this->ar_from[0];
1065 }
1066
Derek Allarde77d77c2007-12-19 15:01:55 +00001067 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001068 {
1069 $this->where($where);
1070 }
Derek Allardda6d2402007-12-19 14:49:29 +00001071
Derek Allarde77d77c2007-12-19 15:01:55 +00001072 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +00001073 {
1074 $this->limit($limit);
1075 }
Derek Allard09de1852007-02-14 01:35:56 +00001076
Derek Allard39b622d2008-01-16 21:10:09 +00001077 $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 +00001078
1079 $this->_reset_write();
1080 return $this->query($sql);
1081 }
Derek Allard39b622d2008-01-16 21:10:09 +00001082
1083 // --------------------------------------------------------------------
1084
1085 /**
1086 * Empty Table
1087 *
1088 * Compiles a delete string and runs "DELETE FROM table"
1089 *
1090 * @access public
1091 * @param string the table to empty
1092 * @return object
1093 */
1094 function empty_table($table = '')
1095 {
1096 if ($table == '')
1097 {
1098 if ( ! isset($this->ar_from[0]))
1099 {
1100 if ($this->db_debug)
1101 {
1102 return $this->display_error('db_must_set_table');
1103 }
1104 return FALSE;
1105 }
1106
1107 $table = $this->ar_from[0];
1108 }
1109 else
1110 {
1111 $table = $this->_protect_identifiers($this->dbprefix.$table);
1112 }
1113
1114
1115 $sql = $this->_delete($table);
1116
1117 $this->_reset_write();
1118
1119 return $this->query($sql);
1120 }
1121
1122 // --------------------------------------------------------------------
1123
1124 /**
1125 * Truncate
1126 *
1127 * Compiles a truncate string and runs the query
1128 * If the database does not support the truncate() command
1129 * This function maps to "DELETE FROM table"
1130 *
1131 * @access public
1132 * @param string the table to truncate
1133 * @return object
1134 */
1135 function truncate($table = '')
1136 {
1137 if ($table == '')
1138 {
1139 if ( ! isset($this->ar_from[0]))
1140 {
1141 if ($this->db_debug)
1142 {
1143 return $this->display_error('db_must_set_table');
1144 }
1145 return FALSE;
1146 }
1147
1148 $table = $this->ar_from[0];
1149 }
1150 else
1151 {
1152 $table = $this->_protect_identifiers($this->dbprefix.$table);
1153 }
1154
1155
1156 $sql = $this->_truncate($table);
1157
1158 $this->_reset_write();
1159
1160 return $this->query($sql);
1161 }
Derek Allard09de1852007-02-14 01:35:56 +00001162
1163 // --------------------------------------------------------------------
1164
1165 /**
1166 * Delete
1167 *
1168 * Compiles a delete string and runs the query
1169 *
1170 * @access public
Derek Allard41f60d42007-12-20 20:09:22 +00001171 * @param mixed the table(s) to delete from. String or array
Derek Allard09de1852007-02-14 01:35:56 +00001172 * @param mixed the where clause
Derek Allard41f60d42007-12-20 20:09:22 +00001173 * @param mixed the limit clause
1174 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +00001175 * @return object
1176 */
Derek Allard41f60d42007-12-20 20:09:22 +00001177 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +00001178 {
1179 if ($table == '')
1180 {
1181 if ( ! isset($this->ar_from[0]))
1182 {
1183 if ($this->db_debug)
1184 {
1185 return $this->display_error('db_must_set_table');
1186 }
1187 return FALSE;
1188 }
Derek Allard39b622d2008-01-16 21:10:09 +00001189
Derek Allard09de1852007-02-14 01:35:56 +00001190 $table = $this->ar_from[0];
1191 }
Derek Allard39b622d2008-01-16 21:10:09 +00001192 elseif (is_array($table))
Derek Allard41f60d42007-12-20 20:09:22 +00001193 {
1194 foreach($table as $single_table)
1195 {
Derek Allard39b622d2008-01-16 21:10:09 +00001196 $this->delete($single_table, $where, $limit, FALSE);
Derek Allard41f60d42007-12-20 20:09:22 +00001197 }
Derek Allard39b622d2008-01-16 21:10:09 +00001198
Derek Allard41f60d42007-12-20 20:09:22 +00001199 $this->_reset_write();
1200 return;
1201 }
Derek Allard39b622d2008-01-16 21:10:09 +00001202 else
1203 {
1204 $table = $this->_protect_identifiers($this->dbprefix.$table);
1205 }
Derek Allard41f60d42007-12-20 20:09:22 +00001206
Derek Allard09de1852007-02-14 01:35:56 +00001207 if ($where != '')
1208 {
1209 $this->where($where);
1210 }
1211
Derek Allarde77d77c2007-12-19 15:01:55 +00001212 if ($limit != NULL)
1213 {
1214 $this->limit($limit);
1215 }
1216
Derek Allard39b622d2008-01-16 21:10:09 +00001217 if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
Derek Allard09de1852007-02-14 01:35:56 +00001218 {
1219 if ($this->db_debug)
1220 {
1221 return $this->display_error('db_del_must_use_where');
1222 }
Derek Allard39b622d2008-01-16 21:10:09 +00001223
Derek Allard09de1852007-02-14 01:35:56 +00001224 return FALSE;
1225 }
Derek Allard41f60d42007-12-20 20:09:22 +00001226
Derek Allard39b622d2008-01-16 21:10:09 +00001227 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001228
Derek Allard41f60d42007-12-20 20:09:22 +00001229 if ($reset_data)
1230 {
1231 $this->_reset_write();
1232 }
Derek Allard39b622d2008-01-16 21:10:09 +00001233
Derek Allard09de1852007-02-14 01:35:56 +00001234 return $this->query($sql);
1235 }
Derek Allard15ddc9d2007-12-20 13:54:39 +00001236
Derek Allard09de1852007-02-14 01:35:56 +00001237 // --------------------------------------------------------------------
1238
1239 /**
1240 * Use Table - DEPRECATED
1241 *
1242 * @deprecated use $this->db->from instead
1243 */
1244 function use_table($table)
1245 {
1246 return $this->from($table);
Derek Allard09de1852007-02-14 01:35:56 +00001247 }
1248
1249 // --------------------------------------------------------------------
1250
1251 /**
Derek Allard09de1852007-02-14 01:35:56 +00001252 * Tests whether the string has an SQL operator
1253 *
1254 * @access private
1255 * @param string
1256 * @return bool
1257 */
1258 function _has_operator($str)
1259 {
1260 $str = trim($str);
1261 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
1262 {
1263 return FALSE;
1264 }
1265
1266 return TRUE;
1267 }
1268
1269 // --------------------------------------------------------------------
1270
1271 /**
Derek Allard5e128942007-12-28 21:33:03 +00001272 * Track Aliases
1273 *
1274 * Used to track SQL statements written with aliased tables.
1275 *
1276 * @access private
1277 * @param string The table to inspect
1278 * @return string
1279 */
1280 function _track_aliases($table)
1281 {
1282 // if a table alias is used we can recognize it by a space
1283 if (strpos($table, " ") !== FALSE)
1284 {
1285 // if the alias is written with the AS keyowrd, get it out
Derek Allard39b622d2008-01-16 21:10:09 +00001286 $table = preg_replace('/ AS /i', ' ', $table);
Derek Allard5e128942007-12-28 21:33:03 +00001287
Derek Allard39b622d2008-01-16 21:10:09 +00001288 $this->ar_aliased_tables[] = trim(strrchr($table, " "));
Derek Allard5e128942007-12-28 21:33:03 +00001289 }
1290
1291 return $this->dbprefix.$table;
1292 }
1293
1294 // --------------------------------------------------------------------
1295
1296 /**
1297 * Filter Table Aliases
1298 *
1299 * Intelligently removes database prefixes from aliased tables
1300 *
1301 * @access private
1302 * @param array An array of compiled SQL
1303 * @return array Cleaned up statement with aliases accounted for
1304 */
1305 function _filter_table_aliases($statements)
1306 {
Derek Allard39b622d2008-01-16 21:10:09 +00001307 foreach ($statements as $k => $v)
Derek Allard5e128942007-12-28 21:33:03 +00001308 {
Derek Allard39b622d2008-01-16 21:10:09 +00001309 foreach ($this->ar_aliased_tables as $table)
Derek Allard5e128942007-12-28 21:33:03 +00001310 {
Derek Allard39b622d2008-01-16 21:10:09 +00001311 $statement = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $v); // makes `table.field`
1312 $statement = str_replace(array($this->dbprefix.$table, '.'), array($table, $this->_protect_identifiers('.')), $statement);
Derek Allard5e128942007-12-28 21:33:03 +00001313 }
1314
Derek Allard39b622d2008-01-16 21:10:09 +00001315 $statements[$k] = $statement;
Derek Allard5e128942007-12-28 21:33:03 +00001316 }
1317
Derek Allard39b622d2008-01-16 21:10:09 +00001318 return $statements;
Derek Allard5e128942007-12-28 21:33:03 +00001319 }
1320
1321 // --------------------------------------------------------------------
1322
1323 /**
Derek Allard09de1852007-02-14 01:35:56 +00001324 * Compile the SELECT statement
1325 *
1326 * Generates a query string based on which functions were used.
1327 * Should not be called directly. The get() function calls it.
1328 *
1329 * @access private
1330 * @return string
1331 */
Derek Allard694b5b82007-12-18 15:58:03 +00001332 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001333 {
1334 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
1335
1336 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->ar_select);
1337
Derek Allard694b5b82007-12-18 15:58:03 +00001338 if ($select_override !== FALSE)
1339 {
1340 $sql = $select_override;
1341 }
1342
Derek Allard09de1852007-02-14 01:35:56 +00001343 if (count($this->ar_from) > 0)
1344 {
1345 $sql .= "\nFROM ";
Derek Jonesc6ad0232008-01-29 18:44:54 +00001346 $sql .= $this->_from_tables($this->ar_from);
Derek Allard09de1852007-02-14 01:35:56 +00001347 }
1348
1349 if (count($this->ar_join) > 0)
Derek Allard5e128942007-12-28 21:33:03 +00001350 {
Derek Allard09de1852007-02-14 01:35:56 +00001351 $sql .= "\n";
Derek Allard5e128942007-12-28 21:33:03 +00001352
1353 // special consideration for table aliases
1354 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1355 {
1356 $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
1357 }
1358 else
1359 {
1360 $sql .= implode("\n", $this->ar_join);
1361 }
1362
Derek Allard09de1852007-02-14 01:35:56 +00001363 }
1364
1365 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1366 {
1367 $sql .= "\nWHERE ";
1368 }
1369
1370 $sql .= implode("\n", $this->ar_where);
1371
1372 if (count($this->ar_like) > 0)
1373 {
1374 if (count($this->ar_where) > 0)
1375 {
1376 $sql .= " AND ";
1377 }
1378
1379 $sql .= implode("\n", $this->ar_like);
1380 }
1381
1382 if (count($this->ar_groupby) > 0)
1383 {
Derek Allard5e128942007-12-28 21:33:03 +00001384
Derek Allard09de1852007-02-14 01:35:56 +00001385 $sql .= "\nGROUP BY ";
Derek Allard5e128942007-12-28 21:33:03 +00001386
1387 // special consideration for table aliases
1388 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1389 {
1390 $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
1391 }
1392 else
1393 {
1394 $sql .= implode(', ', $this->ar_groupby);
1395 }
Derek Allard09de1852007-02-14 01:35:56 +00001396 }
1397
1398 if (count($this->ar_having) > 0)
1399 {
1400 $sql .= "\nHAVING ";
1401 $sql .= implode("\n", $this->ar_having);
1402 }
1403
1404 if (count($this->ar_orderby) > 0)
1405 {
1406 $sql .= "\nORDER BY ";
1407 $sql .= implode(', ', $this->ar_orderby);
1408
1409 if ($this->ar_order !== FALSE)
1410 {
1411 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1412 }
1413 }
1414
1415 if (is_numeric($this->ar_limit))
1416 {
1417 $sql .= "\n";
1418 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1419 }
1420
1421 return $sql;
1422 }
1423
1424 // --------------------------------------------------------------------
1425
1426 /**
1427 * Object to Array
1428 *
1429 * Takes an object as input and converts the class variables to array key/vals
1430 *
1431 * @access public
1432 * @param object
1433 * @return array
1434 */
1435 function _object_to_array($object)
1436 {
1437 if ( ! is_object($object))
1438 {
1439 return $object;
1440 }
1441
1442 $array = array();
1443 foreach (get_object_vars($object) as $key => $val)
1444 {
Derek Allard848b7762007-12-31 16:43:05 +00001445 // There are some built in keys we need to ignore for this conversion
1446 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
1447
Derek Allard09de1852007-02-14 01:35:56 +00001448 {
1449 $array[$key] = $val;
1450 }
1451 }
1452
1453 return $array;
1454 }
1455
1456 // --------------------------------------------------------------------
1457
1458 /**
1459 * Resets the active record values. Called by the get() function
1460 *
1461 * @access private
1462 * @return void
1463 */
1464 function _reset_select()
1465 {
1466 $this->ar_select = array();
1467 $this->ar_distinct = FALSE;
1468 $this->ar_from = array();
1469 $this->ar_join = array();
1470 $this->ar_where = array();
1471 $this->ar_like = array();
1472 $this->ar_groupby = array();
1473 $this->ar_having = array();
1474 $this->ar_limit = FALSE;
1475 $this->ar_offset = FALSE;
1476 $this->ar_order = FALSE;
1477 $this->ar_orderby = array();
Derek Allardc6935512007-12-19 14:23:19 +00001478 $this->ar_wherein = array();
Derek Allard5e128942007-12-28 21:33:03 +00001479 $this->ar_aliased_tables = array();
Derek Allard09de1852007-02-14 01:35:56 +00001480 }
1481
1482 // --------------------------------------------------------------------
1483
1484 /**
1485 * Resets the active record "write" values.
1486 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001487 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001488 *
1489 * @access private
1490 * @return void
1491 */
1492 function _reset_write()
1493 {
1494 $this->ar_set = array();
1495 $this->ar_from = array();
1496 $this->ar_where = array();
Derek Allard39b622d2008-01-16 21:10:09 +00001497 $this->ar_like = array();
Derek Allardda6d2402007-12-19 14:49:29 +00001498 $this->ar_limit = FALSE;
Derek Allard39b622d2008-01-16 21:10:09 +00001499 $this->ar_order = FALSE;
1500 $this->ar_orderby = array();
Derek Allard09de1852007-02-14 01:35:56 +00001501 }
1502
1503}
1504
adminac94f382006-09-24 20:28:12 +00001505?>