blob: 0455b981ea352428c47de48606b0c55d3e50af96 [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;
127
128 return $this;
129 }
130
131 // --------------------------------------------------------------------
132
133 /**
134 * Select Min
135 *
136 * Generates a SELECT MIN(field) portion of a query
137 *
138 * @access public
139 * @param string the field
140 * @param string an alias
141 * @return object
142 */
143 function select_min($select = '', $alias='')
144 {
145 if (!is_string($select) || $select == '')
146 {
147 $this->display_error('db_invalid_query');
148 }
149
150 $alias = ($alias != '') ? $alias : $select;
151
152 $sql = 'MIN('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
153
154 $this->ar_select[] = $sql;
155
156 return $this;
157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Select Average
163 *
164 * Generates a SELECT AVG(field) portion of a query
165 *
166 * @access public
167 * @param string the field
168 * @param string an alias
169 * @return object
170 */
171 function select_avg($select = '', $alias='')
172 {
173 if (!is_string($select) || $select == '')
174 {
175 $this->display_error('db_invalid_query');
176 }
177
178 $alias = ($alias != '') ? $alias : $select;
179
180 $sql = 'AVG('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
181
182 $this->ar_select[] = $sql;
183
184 return $this;
185 }
186
187 // --------------------------------------------------------------------
188
189 /**
190 * Select Sum
191 *
192 * Generates a SELECT SUM(field) portion of a query
193 *
194 * @access public
195 * @param string the field
196 * @param string an alias
197 * @return object
198 */
199 function select_sum($select = '', $alias='')
200 {
201 if (!is_string($select) || $select == '')
202 {
203 $this->display_error('db_invalid_query');
204 }
205
206 $alias = ($alias != '') ? $alias : $select;
207
208 $sql = 'SUM('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
209
210 $this->ar_select[] = $sql;
211
212 return $this;
213 }
214
Derek Allard09de1852007-02-14 01:35:56 +0000215 // --------------------------------------------------------------------
216
217 /**
218 * DISTINCT
219 *
220 * Sets a flag which tells the query string compiler to add DISTINCT
221 *
222 * @access public
223 * @param bool
224 * @return object
225 */
226 function distinct($val = TRUE)
227 {
228 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
229 return $this;
230 }
231
232 // --------------------------------------------------------------------
233
234 /**
235 * From
236 *
237 * Generates the FROM portion of the query
238 *
239 * @access public
240 * @param mixed can be a string or array
241 * @return object
242 */
243 function from($from)
244 {
245 foreach ((array)$from as $val)
246 {
Derek Allard39b622d2008-01-16 21:10:09 +0000247 $this->ar_from[] = $this->_protect_identifiers($this->_track_aliases($val));
Derek Allard09de1852007-02-14 01:35:56 +0000248 }
Derek Allard5e128942007-12-28 21:33:03 +0000249
Derek Allard09de1852007-02-14 01:35:56 +0000250 return $this;
251 }
252
253 // --------------------------------------------------------------------
254
255 /**
256 * Join
257 *
258 * Generates the JOIN portion of the query
259 *
260 * @access public
261 * @param string
262 * @param string the join condition
263 * @param string the type of join
264 * @return object
265 */
266 function join($table, $cond, $type = '')
267 {
268 if ($type != '')
269 {
270 $type = strtoupper(trim($type));
271
272 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
273 {
274 $type = '';
275 }
276 else
277 {
278 $type .= ' ';
279 }
280 }
281
Derek Allard09de1852007-02-14 01:35:56 +0000282 // If a DB prefix is used we might need to add it to the column names
283 if ($this->dbprefix)
284 {
Derek Allard39b622d2008-01-16 21:10:09 +0000285 $this->_track_aliases($table);
286
Derek Allard09de1852007-02-14 01:35:56 +0000287 // First we remove any existing prefixes in the condition to avoid duplicates
288 $cond = preg_replace('|('.$this->dbprefix.')([\w\.]+)([\W\s]+)|', "$2$3", $cond);
289
290 // Next we add the prefixes to the condition
291 $cond = preg_replace('|([\w\.]+)([\W\s]+)(.+)|', $this->dbprefix . "$1$2" . $this->dbprefix . "$3", $cond);
Derek Allard5e128942007-12-28 21:33:03 +0000292 }
293
Derek Allard39b622d2008-01-16 21:10:09 +0000294 $this->ar_join[] = $type.'JOIN '.$this->_protect_identifiers($this->dbprefix.$table, TRUE).' ON '.$cond;
Derek Allard09de1852007-02-14 01:35:56 +0000295 return $this;
296 }
297
298 // --------------------------------------------------------------------
299
300 /**
301 * Where
302 *
303 * Generates the WHERE portion of the query. Separates
304 * multiple calls with AND
305 *
306 * @access public
307 * @param mixed
308 * @param mixed
309 * @return object
310 */
Derek Allard39b622d2008-01-16 21:10:09 +0000311 function where($key, $value = NULL, $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000312 {
Derek Allard39b622d2008-01-16 21:10:09 +0000313 return $this->_where($key, $value, 'AND ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000314 }
315
316 // --------------------------------------------------------------------
317
318 /**
319 * OR Where
320 *
321 * Generates the WHERE portion of the query. Separates
322 * multiple calls with OR
323 *
324 * @access public
325 * @param mixed
326 * @param mixed
327 * @return object
328 */
Derek Allard39b622d2008-01-16 21:10:09 +0000329 function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000330 {
Derek Allard39b622d2008-01-16 21:10:09 +0000331 return $this->_where($key, $value, 'OR ', $escape);
Derek Allard09de1852007-02-14 01:35:56 +0000332 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000333
334 // --------------------------------------------------------------------
335
336 /**
337 * orwhere() is an alias of or_where()
338 * this function is here for backwards compatibility, as
339 * orwhere() has been deprecated
340 */
Derek Allard39b622d2008-01-16 21:10:09 +0000341 function orwhere($key, $value = NULL, $escape = TRUE)
Derek Allard218e2bc2007-12-17 21:18:14 +0000342 {
Derek Allard39b622d2008-01-16 21:10:09 +0000343 return $this->or_where($key, $value, $escape);
Derek Allard218e2bc2007-12-17 21:18:14 +0000344 }
Derek Allard67b44ed2008-01-12 16:18:02 +0000345
346 // --------------------------------------------------------------------
347
348 /**
Derek Allard09de1852007-02-14 01:35:56 +0000349 * Where
350 *
351 * Called by where() or orwhere()
352 *
353 * @access private
354 * @param mixed
355 * @param mixed
356 * @param string
357 * @return object
358 */
Derek Allard39b622d2008-01-16 21:10:09 +0000359 function _where($key, $value = NULL, $type = 'AND ', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000360 {
361 if ( ! is_array($key))
362 {
363 $key = array($key => $value);
364 }
365
366 foreach ($key as $k => $v)
367 {
368 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Derek Allard15ddc9d2007-12-20 13:54:39 +0000369
Derek Allard39b622d2008-01-16 21:10:09 +0000370 if ( ! $this->_has_operator($k) && is_null($key[$k]))
Derek Allard15ddc9d2007-12-20 13:54:39 +0000371 {
372 // value appears not to have been set, assign the test to IS NULL
373 $k .= ' IS NULL';
374 }
Derek Allard09de1852007-02-14 01:35:56 +0000375
376 if ( ! is_null($v))
377 {
Derek Allard39b622d2008-01-16 21:10:09 +0000378
379 if ($escape === TRUE)
380 {
381 // exception for "field<=" keys
382 if ($this->_has_operator($k))
383 {
384 $k = preg_replace("/([A-Za-z_0-9]+)/", $this->_protect_identifiers('$1'), $k);
385 }
386 else
387 {
388 $k = $this->_protect_identifiers($k);
389 }
390 }
391
Derek Allard09de1852007-02-14 01:35:56 +0000392 if ( ! $this->_has_operator($k))
393 {
394 $k .= ' =';
395 }
Derek Allard39b622d2008-01-16 21:10:09 +0000396
Derek Allard09de1852007-02-14 01:35:56 +0000397 $v = ' '.$this->escape($v);
Derek Allard15ddc9d2007-12-20 13:54:39 +0000398
Derek Allard09de1852007-02-14 01:35:56 +0000399 }
400
401 $this->ar_where[] = $prefix.$k.$v;
402 }
403 return $this;
404 }
Derek Allard80dd7022007-12-18 23:55:06 +0000405
406 // --------------------------------------------------------------------
407
408 /**
409 * Where_in
410 *
Derek Allardc6935512007-12-19 14:23:19 +0000411 * Generates a WHERE field IN ('item', 'item') SQL query joined with
412 * AND if appropriate
Derek Allard80dd7022007-12-18 23:55:06 +0000413 *
414 * @access public
415 * @param string The field to search
416 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000417
418 * @return object
419 */
420 function where_in($key = NULL, $values = NULL)
421 {
422 return $this->_where_in($key, $values);
423 }
424
425 // --------------------------------------------------------------------
426
427 /**
428 * Where_in_or
429 *
430 * Generates a WHERE field IN ('item', 'item') SQL query joined with
431 * OR if appropriate
432 *
433 * @access public
434 * @param string The field to search
435 * @param array The values searched on
436
437 * @return object
438 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000439 function or_where_in($key = NULL, $values = NULL)
Derek Allardc6935512007-12-19 14:23:19 +0000440 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000441 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000442 }
443
444 // --------------------------------------------------------------------
445
446 /**
447 * Where_not_in
448 *
449 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
450 * with AND if appropriate
451 *
452 * @access public
453 * @param string The field to search
454 * @param array The values searched on
455
456 * @return object
457 */
458 function where_not_in($key = NULL, $values = NULL)
459 {
460 return $this->_where_in($key, $values, TRUE);
461 }
462
463 // --------------------------------------------------------------------
464
465 /**
466 * Where_not_in_or
467 *
468 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
469 * with OR if appropriate
470 *
471 * @access public
472 * @param string The field to search
473 * @param array The values searched on
474
475 * @return object
476 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000477 function or_where_not_in($key = NULL, $values = NULL)
Derek Allardc6935512007-12-19 14:23:19 +0000478 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000479 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000480 }
481
482 // --------------------------------------------------------------------
483
484 /**
485 * Where_in
486 *
487 * Called by where_in, where_in_or, where_not_in, where_not_in_or
488 *
489 * @access public
490 * @param string The field to search
491 * @param array The values searched on
492 * @param boolean If the statement whould be IN or NOT IN
Derek Allard80dd7022007-12-18 23:55:06 +0000493 * @param string
494 * @return object
495 */
Derek Allard15ddc9d2007-12-20 13:54:39 +0000496 function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard80dd7022007-12-18 23:55:06 +0000497 {
498 if ($key === NULL || !is_array($values))
499 {
500 return;
501 }
502
Derek Allardc6935512007-12-19 14:23:19 +0000503 $not = ($not) ? ' NOT ' : '';
Derek Allard80dd7022007-12-18 23:55:06 +0000504
505 foreach ($values as $value)
506 {
507 $this->ar_wherein[] = $this->escape($value);
508 }
509
510 $prefix = (count($this->ar_where) == 0) ? '' : $type;
511
Derek Allard39b622d2008-01-16 21:10:09 +0000512 $this->ar_where[] = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
Derek Allard80dd7022007-12-18 23:55:06 +0000513
Derek Allard8f000212008-01-18 14:45:59 +0000514 // reset the array for multiple calls
515 $this->ar_wherein = array();
Derek Allard80dd7022007-12-18 23:55:06 +0000516 return $this;
517 }
518
Derek Allard09de1852007-02-14 01:35:56 +0000519 // --------------------------------------------------------------------
520
521 /**
522 * Like
523 *
524 * Generates a %LIKE% portion of the query. Separates
525 * multiple calls with AND
526 *
527 * @access public
528 * @param mixed
529 * @param mixed
530 * @return object
531 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000532 function like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000533 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000534 return $this->_like($field, $match, 'AND ', $side);
Derek Allard09de1852007-02-14 01:35:56 +0000535 }
Derek Allarde54e3d22007-12-19 15:53:44 +0000536
537 // --------------------------------------------------------------------
538
539 /**
540 * Not Like
541 *
542 * Generates a NOT LIKE portion of the query. Separates
543 * multiple calls with AND
544 *
545 * @access public
546 * @param mixed
547 * @param mixed
548 * @return object
549 */
550 function not_like($field, $match = '', $side = 'both')
551 {
552 return $this->_like($field, $match, 'AND ', $side, ' NOT');
553 }
554
Derek Allard09de1852007-02-14 01:35:56 +0000555 // --------------------------------------------------------------------
556
557 /**
558 * OR Like
559 *
560 * Generates a %LIKE% portion of the query. Separates
561 * multiple calls with OR
562 *
563 * @access public
564 * @param mixed
565 * @param mixed
566 * @return object
567 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000568 function or_like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000569 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000570 return $this->_like($field, $match, 'OR ', $side);
571 }
572
573 // --------------------------------------------------------------------
574
575 /**
Derek Allarde54e3d22007-12-19 15:53:44 +0000576 * OR Not Like
577 *
578 * Generates a NOT LIKE portion of the query. Separates
579 * multiple calls with OR
580 *
581 * @access public
582 * @param mixed
583 * @param mixed
584 * @return object
585 */
586 function or_not_like($field, $match = '', $side = 'both')
587 {
588 return $this->_like($field, $match, 'OR ', $side, 'NOT ');
589 }
590
591 // --------------------------------------------------------------------
592
593 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000594 * orlike() is an alias of or_like()
595 * this function is here for backwards compatibility, as
596 * orlike() has been deprecated
597 */
598 function orlike($field, $match = '', $side = 'both')
599 {
600 return $this->orlike($field, $match, $side);
Derek Allard09de1852007-02-14 01:35:56 +0000601 }
602
603 // --------------------------------------------------------------------
604
605 /**
606 * Like
607 *
608 * Called by like() or orlike()
609 *
610 * @access private
611 * @param mixed
612 * @param mixed
613 * @param string
614 * @return object
615 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000616 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard09de1852007-02-14 01:35:56 +0000617 {
618 if ( ! is_array($field))
619 {
620 $field = array($field => $match);
621 }
622
623 foreach ($field as $k => $v)
Derek Allarde54e3d22007-12-19 15:53:44 +0000624 {
625
Derek Allard39b622d2008-01-16 21:10:09 +0000626 $k = $this->_protect_identifiers($k);
627
Derek Allard09de1852007-02-14 01:35:56 +0000628 $prefix = (count($this->ar_like) == 0) ? '' : $type;
Derek Allarde54e3d22007-12-19 15:53:44 +0000629
Derek Allard09de1852007-02-14 01:35:56 +0000630 $v = $this->escape_str($v);
Derek Allarde54e3d22007-12-19 15:53:44 +0000631
Derek Allard218e2bc2007-12-17 21:18:14 +0000632 if ($side == 'before')
633 {
Derek Allarde54e3d22007-12-19 15:53:44 +0000634 $this->ar_like[] = $prefix." $k $not LIKE '%{$v}'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000635 }
636 elseif ($side == 'after')
637 {
Derek Allarde54e3d22007-12-19 15:53:44 +0000638 $this->ar_like[] = $prefix." $k $not LIKE '{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000639 }
640 else
641 {
Derek Allarde54e3d22007-12-19 15:53:44 +0000642 $this->ar_like[] = $prefix." $k $not LIKE '%{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000643 }
Derek Allard09de1852007-02-14 01:35:56 +0000644 }
645 return $this;
646 }
647
648 // --------------------------------------------------------------------
649
650 /**
651 * GROUP BY
652 *
653 * @access public
654 * @param string
655 * @return object
656 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000657 function group_by($by)
Derek Allard09de1852007-02-14 01:35:56 +0000658 {
659 if (is_string($by))
660 {
661 $by = explode(',', $by);
662 }
663
664 foreach ($by as $val)
665 {
666 $val = trim($val);
667
668 if ($val != '')
Derek Allard39b622d2008-01-16 21:10:09 +0000669 $this->ar_groupby[] = $this->_protect_identifiers($val);
Derek Allard09de1852007-02-14 01:35:56 +0000670 }
671 return $this;
672 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000673
674 // --------------------------------------------------------------------
675
676 /**
677 * groupby() is an alias of group_by()
678 * this function is here for backwards compatibility, as
679 * groupby() has been deprecated
680 */
681 function groupby($by)
682 {
683 return $this->group_by($by);
684 }
685
Derek Allard09de1852007-02-14 01:35:56 +0000686 // --------------------------------------------------------------------
687
688 /**
689 * Sets the HAVING value
690 *
691 * Separates multiple calls with AND
692 *
693 * @access public
694 * @param string
695 * @param string
696 * @return object
697 */
698 function having($key, $value = '')
699 {
700 return $this->_having($key, $value, 'AND ');
701 }
702
703 // --------------------------------------------------------------------
704
705 /**
706 * Sets the OR HAVING value
707 *
708 * Separates multiple calls with OR
709 *
710 * @access public
711 * @param string
712 * @param string
713 * @return object
714 */
715 function orhaving($key, $value = '')
716 {
717 return $this->_having($key, $value, 'OR ');
718 }
719
720 // --------------------------------------------------------------------
721
722 /**
723 * Sets the HAVING values
724 *
725 * Called by having() or orhaving()
726 *
727 * @access private
728 * @param string
729 * @param string
730 * @return object
731 */
732 function _having($key, $value = '', $type = 'AND ')
733 {
734 if ( ! is_array($key))
735 {
736 $key = array($key => $value);
737 }
738
739 foreach ($key as $k => $v)
740 {
741 $prefix = (count($this->ar_having) == 0) ? '' : $type;
742
743 if ($v != '')
744 {
745 $v = ' '.$this->escape($v);
746 }
747
748 $this->ar_having[] = $prefix.$k.$v;
749 }
750 return $this;
751 }
752
753 // --------------------------------------------------------------------
754
755 /**
756 * Sets the ORDER BY value
757 *
758 * @access public
759 * @param string
760 * @param string direction: asc or desc
761 * @return object
762 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000763 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000764 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000765 if (strtolower($direction) == 'random')
766 {
767 $orderby = ''; // Random results want or don't need a field name
768 $direction = $this->_random_keyword;
769 }
770 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000771 {
Derek Allard92782492007-08-10 11:26:01 +0000772 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000773 }
774
Derek Allard05b3a5d2008-01-17 20:25:46 +0000775 $this->ar_orderby[] = $this->_protect_identifiers($orderby, TRUE).$direction;
Derek Allard09de1852007-02-14 01:35:56 +0000776 return $this;
777 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000778
Derek Allard218e2bc2007-12-17 21:18:14 +0000779 // --------------------------------------------------------------------
780
781 /**
782 * orderby() is an alias of order_by()
783 * this function is here for backwards compatibility, as
784 * orderby() has been deprecated
785 */
786 function orderby($orderby, $direction = '')
787 {
788 return $this->order_by($orderby, $direction);
789 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000790
Derek Allard09de1852007-02-14 01:35:56 +0000791 // --------------------------------------------------------------------
792
793 /**
794 * Sets the LIMIT value
795 *
796 * @access public
797 * @param integer the limit value
798 * @param integer the offset value
799 * @return object
800 */
801 function limit($value, $offset = '')
802 {
803 $this->ar_limit = $value;
804
805 if ($offset != '')
806 $this->ar_offset = $offset;
807
808 return $this;
809 }
810
811 // --------------------------------------------------------------------
812
813 /**
814 * Sets the OFFSET value
815 *
816 * @access public
817 * @param integer the offset value
818 * @return object
819 */
820 function offset($value)
821 {
822 $this->ar_offset = $value;
823 return $this;
824 }
825
826 // --------------------------------------------------------------------
827
828 /**
829 * The "set" function. Allows key/value pairs to be set for inserting or updating
830 *
831 * @access public
832 * @param mixed
833 * @param string
Derek Allard39b622d2008-01-16 21:10:09 +0000834 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +0000835 * @return object
836 */
Derek Allard39b622d2008-01-16 21:10:09 +0000837 function set($key, $value = '', $escape = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000838 {
839 $key = $this->_object_to_array($key);
840
841 if ( ! is_array($key))
842 {
843 $key = array($key => $value);
844 }
845
846 foreach ($key as $k => $v)
847 {
Derek Allard39b622d2008-01-16 21:10:09 +0000848 if ($escape === FALSE)
849 {
850 $this->ar_set[$this->_protect_identifiers($k)] = $v;
851 }
852 else
853 {
854 $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
855 }
856
Derek Allard09de1852007-02-14 01:35:56 +0000857 }
858
859 return $this;
860 }
861
862 // --------------------------------------------------------------------
863
864 /**
865 * Get
866 *
867 * Compiles the select statement based on the other functions called
868 * and runs the query
869 *
870 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +0000871 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +0000872 * @param string the limit clause
873 * @param string the offset clause
874 * @return object
875 */
876 function get($table = '', $limit = null, $offset = null)
877 {
878 if ($table != '')
879 {
Derek Allard5e128942007-12-28 21:33:03 +0000880 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +0000881 $this->from($table);
882 }
883
884 if ( ! is_null($limit))
885 {
886 $this->limit($limit, $offset);
887 }
888
889 $sql = $this->_compile_select();
890
891 $result = $this->query($sql);
892 $this->_reset_select();
893 return $result;
894 }
895
Derek Allard09de1852007-02-14 01:35:56 +0000896 /**
Derek Allard694b5b82007-12-18 15:58:03 +0000897 * "Count All Results" query
898 *
899 * Generates a platform-specific query string that counts all records
900 * returned by an Active Record query.
901 *
902 * @access public
903 * @param string
904 * @return string
905 */
906 function count_all_results($table = '')
907 {
908 if ($table != '')
909 {
Derek Allard5e128942007-12-28 21:33:03 +0000910 $this->_track_aliases($table);
Derek Allard694b5b82007-12-18 15:58:03 +0000911 $this->from($table);
912 }
913
Derek Allard39b622d2008-01-16 21:10:09 +0000914 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
Derek Allard694b5b82007-12-18 15:58:03 +0000915
916 $query = $this->query($sql);
917 $this->_reset_select();
918
919 if ($query->num_rows() == 0)
920 {
921 return '0';
922 }
923
924 $row = $query->row();
925 return $row->numrows;
926 }
927
928 // --------------------------------------------------------------------
929
930 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000931 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +0000932 *
933 * Allows the where clause, limit and offset to be added directly
934 *
935 * @access public
936 * @param string the where clause
937 * @param string the limit clause
938 * @param string the offset clause
939 * @return object
940 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000941 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +0000942 {
943 if ($table != '')
944 {
Derek Allard5e128942007-12-28 21:33:03 +0000945 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +0000946 $this->from($table);
947 }
948
949 if ( ! is_null($where))
950 {
951 $this->where($where);
952 }
953
954 if ( ! is_null($limit))
955 {
956 $this->limit($limit, $offset);
957 }
958
959 $sql = $this->_compile_select();
960
961 $result = $this->query($sql);
962 $this->_reset_select();
963 return $result;
964 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000965
966 // --------------------------------------------------------------------
967
968 /**
969 * getwhere() is an alias of get_where()
970 * this function is here for backwards compatibility, as
971 * getwhere() has been deprecated
972 */
973 function getwhere($table = '', $where = null, $limit = null, $offset = null)
974 {
975 return $this->get_where($table, $where, $limit, $offset);
976 }
Derek Allard09de1852007-02-14 01:35:56 +0000977
978 // --------------------------------------------------------------------
979
980 /**
981 * Insert
982 *
983 * Compiles an insert string and runs the query
984 *
985 * @access public
986 * @param string the table to retrieve the results from
987 * @param array an associative array of insert values
988 * @return object
989 */
990 function insert($table = '', $set = NULL)
991 {
992 if ( ! is_null($set))
993 {
994 $this->set($set);
995 }
996
997 if (count($this->ar_set) == 0)
998 {
999 if ($this->db_debug)
1000 {
1001 return $this->display_error('db_must_use_set');
1002 }
1003 return FALSE;
1004 }
1005
1006 if ($table == '')
1007 {
1008 if ( ! isset($this->ar_from[0]))
1009 {
1010 if ($this->db_debug)
1011 {
1012 return $this->display_error('db_must_set_table');
1013 }
1014 return FALSE;
1015 }
1016
1017 $table = $this->ar_from[0];
1018 }
Derek Allard39b622d2008-01-16 21:10:09 +00001019
1020 $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 +00001021
1022 $this->_reset_write();
1023 return $this->query($sql);
1024 }
1025
1026 // --------------------------------------------------------------------
1027
1028 /**
1029 * Update
1030 *
1031 * Compiles an update string and runs the query
1032 *
1033 * @access public
1034 * @param string the table to retrieve the results from
1035 * @param array an associative array of update values
1036 * @param mixed the where clause
1037 * @return object
1038 */
Derek Allard5e128942007-12-28 21:33:03 +00001039 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001040 {
1041 if ( ! is_null($set))
1042 {
1043 $this->set($set);
1044 }
1045
1046 if (count($this->ar_set) == 0)
1047 {
1048 if ($this->db_debug)
1049 {
1050 return $this->display_error('db_must_use_set');
1051 }
1052 return FALSE;
1053 }
1054
1055 if ($table == '')
1056 {
1057 if ( ! isset($this->ar_from[0]))
1058 {
1059 if ($this->db_debug)
1060 {
1061 return $this->display_error('db_must_set_table');
1062 }
1063 return FALSE;
1064 }
1065
1066 $table = $this->ar_from[0];
1067 }
1068
Derek Allarde77d77c2007-12-19 15:01:55 +00001069 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +00001070 {
1071 $this->where($where);
1072 }
Derek Allardda6d2402007-12-19 14:49:29 +00001073
Derek Allarde77d77c2007-12-19 15:01:55 +00001074 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +00001075 {
1076 $this->limit($limit);
1077 }
Derek Allard09de1852007-02-14 01:35:56 +00001078
Derek Allard39b622d2008-01-16 21:10:09 +00001079 $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 +00001080
1081 $this->_reset_write();
1082 return $this->query($sql);
1083 }
Derek Allard39b622d2008-01-16 21:10:09 +00001084
1085 // --------------------------------------------------------------------
1086
1087 /**
1088 * Empty Table
1089 *
1090 * Compiles a delete string and runs "DELETE FROM table"
1091 *
1092 * @access public
1093 * @param string the table to empty
1094 * @return object
1095 */
1096 function empty_table($table = '')
1097 {
1098 if ($table == '')
1099 {
1100 if ( ! isset($this->ar_from[0]))
1101 {
1102 if ($this->db_debug)
1103 {
1104 return $this->display_error('db_must_set_table');
1105 }
1106 return FALSE;
1107 }
1108
1109 $table = $this->ar_from[0];
1110 }
1111 else
1112 {
1113 $table = $this->_protect_identifiers($this->dbprefix.$table);
1114 }
1115
1116
1117 $sql = $this->_delete($table);
1118
1119 $this->_reset_write();
1120
1121 return $this->query($sql);
1122 }
1123
1124 // --------------------------------------------------------------------
1125
1126 /**
1127 * Truncate
1128 *
1129 * Compiles a truncate string and runs the query
1130 * If the database does not support the truncate() command
1131 * This function maps to "DELETE FROM table"
1132 *
1133 * @access public
1134 * @param string the table to truncate
1135 * @return object
1136 */
1137 function truncate($table = '')
1138 {
1139 if ($table == '')
1140 {
1141 if ( ! isset($this->ar_from[0]))
1142 {
1143 if ($this->db_debug)
1144 {
1145 return $this->display_error('db_must_set_table');
1146 }
1147 return FALSE;
1148 }
1149
1150 $table = $this->ar_from[0];
1151 }
1152 else
1153 {
1154 $table = $this->_protect_identifiers($this->dbprefix.$table);
1155 }
1156
1157
1158 $sql = $this->_truncate($table);
1159
1160 $this->_reset_write();
1161
1162 return $this->query($sql);
1163 }
Derek Allard09de1852007-02-14 01:35:56 +00001164
1165 // --------------------------------------------------------------------
1166
1167 /**
1168 * Delete
1169 *
1170 * Compiles a delete string and runs the query
1171 *
1172 * @access public
Derek Allard41f60d42007-12-20 20:09:22 +00001173 * @param mixed the table(s) to delete from. String or array
Derek Allard09de1852007-02-14 01:35:56 +00001174 * @param mixed the where clause
Derek Allard41f60d42007-12-20 20:09:22 +00001175 * @param mixed the limit clause
1176 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +00001177 * @return object
1178 */
Derek Allard41f60d42007-12-20 20:09:22 +00001179 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +00001180 {
1181 if ($table == '')
1182 {
1183 if ( ! isset($this->ar_from[0]))
1184 {
1185 if ($this->db_debug)
1186 {
1187 return $this->display_error('db_must_set_table');
1188 }
1189 return FALSE;
1190 }
Derek Allard39b622d2008-01-16 21:10:09 +00001191
Derek Allard09de1852007-02-14 01:35:56 +00001192 $table = $this->ar_from[0];
1193 }
Derek Allard39b622d2008-01-16 21:10:09 +00001194 elseif (is_array($table))
Derek Allard41f60d42007-12-20 20:09:22 +00001195 {
1196 foreach($table as $single_table)
1197 {
Derek Allard39b622d2008-01-16 21:10:09 +00001198 $this->delete($single_table, $where, $limit, FALSE);
Derek Allard41f60d42007-12-20 20:09:22 +00001199 }
Derek Allard39b622d2008-01-16 21:10:09 +00001200
Derek Allard41f60d42007-12-20 20:09:22 +00001201 $this->_reset_write();
1202 return;
1203 }
Derek Allard39b622d2008-01-16 21:10:09 +00001204 else
1205 {
1206 $table = $this->_protect_identifiers($this->dbprefix.$table);
1207 }
Derek Allard41f60d42007-12-20 20:09:22 +00001208
Derek Allard09de1852007-02-14 01:35:56 +00001209 if ($where != '')
1210 {
1211 $this->where($where);
1212 }
1213
Derek Allarde77d77c2007-12-19 15:01:55 +00001214 if ($limit != NULL)
1215 {
1216 $this->limit($limit);
1217 }
1218
Derek Allard39b622d2008-01-16 21:10:09 +00001219 if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
Derek Allard09de1852007-02-14 01:35:56 +00001220 {
1221 if ($this->db_debug)
1222 {
1223 return $this->display_error('db_del_must_use_where');
1224 }
Derek Allard39b622d2008-01-16 21:10:09 +00001225
Derek Allard09de1852007-02-14 01:35:56 +00001226 return FALSE;
1227 }
Derek Allard41f60d42007-12-20 20:09:22 +00001228
Derek Allard39b622d2008-01-16 21:10:09 +00001229 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +00001230
Derek Allard41f60d42007-12-20 20:09:22 +00001231 if ($reset_data)
1232 {
1233 $this->_reset_write();
1234 }
Derek Allard39b622d2008-01-16 21:10:09 +00001235
Derek Allard09de1852007-02-14 01:35:56 +00001236 return $this->query($sql);
1237 }
Derek Allard15ddc9d2007-12-20 13:54:39 +00001238
Derek Allard09de1852007-02-14 01:35:56 +00001239 // --------------------------------------------------------------------
1240
1241 /**
1242 * Use Table - DEPRECATED
1243 *
1244 * @deprecated use $this->db->from instead
1245 */
1246 function use_table($table)
1247 {
1248 return $this->from($table);
1249 return $this;
1250 }
1251
1252 // --------------------------------------------------------------------
1253
1254 /**
Derek Allard09de1852007-02-14 01:35:56 +00001255 * Tests whether the string has an SQL operator
1256 *
1257 * @access private
1258 * @param string
1259 * @return bool
1260 */
1261 function _has_operator($str)
1262 {
1263 $str = trim($str);
1264 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
1265 {
1266 return FALSE;
1267 }
1268
1269 return TRUE;
1270 }
1271
1272 // --------------------------------------------------------------------
1273
1274 /**
Derek Allard5e128942007-12-28 21:33:03 +00001275 * Track Aliases
1276 *
1277 * Used to track SQL statements written with aliased tables.
1278 *
1279 * @access private
1280 * @param string The table to inspect
1281 * @return string
1282 */
1283 function _track_aliases($table)
1284 {
1285 // if a table alias is used we can recognize it by a space
1286 if (strpos($table, " ") !== FALSE)
1287 {
1288 // if the alias is written with the AS keyowrd, get it out
Derek Allard39b622d2008-01-16 21:10:09 +00001289 $table = preg_replace('/ AS /i', ' ', $table);
Derek Allard5e128942007-12-28 21:33:03 +00001290
Derek Allard39b622d2008-01-16 21:10:09 +00001291 $this->ar_aliased_tables[] = trim(strrchr($table, " "));
Derek Allard5e128942007-12-28 21:33:03 +00001292 }
1293
1294 return $this->dbprefix.$table;
1295 }
1296
1297 // --------------------------------------------------------------------
1298
1299 /**
1300 * Filter Table Aliases
1301 *
1302 * Intelligently removes database prefixes from aliased tables
1303 *
1304 * @access private
1305 * @param array An array of compiled SQL
1306 * @return array Cleaned up statement with aliases accounted for
1307 */
1308 function _filter_table_aliases($statements)
1309 {
Derek Allard39b622d2008-01-16 21:10:09 +00001310 $statements_without_aliases = array();
Derek Allard5e128942007-12-28 21:33:03 +00001311
Derek Allard39b622d2008-01-16 21:10:09 +00001312 foreach ($statements as $k => $v)
Derek Allard5e128942007-12-28 21:33:03 +00001313 {
Derek Allard39b622d2008-01-16 21:10:09 +00001314 foreach ($this->ar_aliased_tables as $table)
Derek Allard5e128942007-12-28 21:33:03 +00001315 {
Derek Allard39b622d2008-01-16 21:10:09 +00001316 $statement = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $v); // makes `table.field`
1317 $statement = str_replace(array($this->dbprefix.$table, '.'), array($table, $this->_protect_identifiers('.')), $statement);
Derek Allard5e128942007-12-28 21:33:03 +00001318 }
1319
Derek Allard39b622d2008-01-16 21:10:09 +00001320 $statements[$k] = $statement;
Derek Allard5e128942007-12-28 21:33:03 +00001321 }
1322
Derek Allard39b622d2008-01-16 21:10:09 +00001323 return $statements;
Derek Allard5e128942007-12-28 21:33:03 +00001324 }
1325
1326 // --------------------------------------------------------------------
1327
1328 /**
Derek Allard09de1852007-02-14 01:35:56 +00001329 * Compile the SELECT statement
1330 *
1331 * Generates a query string based on which functions were used.
1332 * Should not be called directly. The get() function calls it.
1333 *
1334 * @access private
1335 * @return string
1336 */
Derek Allard694b5b82007-12-18 15:58:03 +00001337 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001338 {
1339 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
1340
1341 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->ar_select);
1342
Derek Allard694b5b82007-12-18 15:58:03 +00001343 if ($select_override !== FALSE)
1344 {
1345 $sql = $select_override;
1346 }
1347
Derek Allard09de1852007-02-14 01:35:56 +00001348 if (count($this->ar_from) > 0)
1349 {
1350 $sql .= "\nFROM ";
Derek Allard15ddc9d2007-12-20 13:54:39 +00001351 $sql .= '(' . implode(', ', $this->ar_from) . ')';
Derek Allard09de1852007-02-14 01:35:56 +00001352 }
1353
1354 if (count($this->ar_join) > 0)
Derek Allard5e128942007-12-28 21:33:03 +00001355 {
Derek Allard09de1852007-02-14 01:35:56 +00001356 $sql .= "\n";
Derek Allard5e128942007-12-28 21:33:03 +00001357
1358 // special consideration for table aliases
1359 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1360 {
1361 $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
1362 }
1363 else
1364 {
1365 $sql .= implode("\n", $this->ar_join);
1366 }
1367
Derek Allard09de1852007-02-14 01:35:56 +00001368 }
1369
1370 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1371 {
1372 $sql .= "\nWHERE ";
1373 }
1374
1375 $sql .= implode("\n", $this->ar_where);
1376
1377 if (count($this->ar_like) > 0)
1378 {
1379 if (count($this->ar_where) > 0)
1380 {
1381 $sql .= " AND ";
1382 }
1383
1384 $sql .= implode("\n", $this->ar_like);
1385 }
1386
1387 if (count($this->ar_groupby) > 0)
1388 {
Derek Allard5e128942007-12-28 21:33:03 +00001389
Derek Allard09de1852007-02-14 01:35:56 +00001390 $sql .= "\nGROUP BY ";
Derek Allard5e128942007-12-28 21:33:03 +00001391
1392 // special consideration for table aliases
1393 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1394 {
1395 $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
1396 }
1397 else
1398 {
1399 $sql .= implode(', ', $this->ar_groupby);
1400 }
Derek Allard09de1852007-02-14 01:35:56 +00001401 }
1402
1403 if (count($this->ar_having) > 0)
1404 {
1405 $sql .= "\nHAVING ";
1406 $sql .= implode("\n", $this->ar_having);
1407 }
1408
1409 if (count($this->ar_orderby) > 0)
1410 {
1411 $sql .= "\nORDER BY ";
1412 $sql .= implode(', ', $this->ar_orderby);
1413
1414 if ($this->ar_order !== FALSE)
1415 {
1416 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1417 }
1418 }
1419
1420 if (is_numeric($this->ar_limit))
1421 {
1422 $sql .= "\n";
1423 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1424 }
1425
1426 return $sql;
1427 }
1428
1429 // --------------------------------------------------------------------
1430
1431 /**
1432 * Object to Array
1433 *
1434 * Takes an object as input and converts the class variables to array key/vals
1435 *
1436 * @access public
1437 * @param object
1438 * @return array
1439 */
1440 function _object_to_array($object)
1441 {
1442 if ( ! is_object($object))
1443 {
1444 return $object;
1445 }
1446
1447 $array = array();
1448 foreach (get_object_vars($object) as $key => $val)
1449 {
Derek Allard848b7762007-12-31 16:43:05 +00001450 // There are some built in keys we need to ignore for this conversion
1451 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
1452
Derek Allard09de1852007-02-14 01:35:56 +00001453 {
1454 $array[$key] = $val;
1455 }
1456 }
1457
1458 return $array;
1459 }
1460
1461 // --------------------------------------------------------------------
1462
1463 /**
1464 * Resets the active record values. Called by the get() function
1465 *
1466 * @access private
1467 * @return void
1468 */
1469 function _reset_select()
1470 {
1471 $this->ar_select = array();
1472 $this->ar_distinct = FALSE;
1473 $this->ar_from = array();
1474 $this->ar_join = array();
1475 $this->ar_where = array();
1476 $this->ar_like = array();
1477 $this->ar_groupby = array();
1478 $this->ar_having = array();
1479 $this->ar_limit = FALSE;
1480 $this->ar_offset = FALSE;
1481 $this->ar_order = FALSE;
1482 $this->ar_orderby = array();
Derek Allardc6935512007-12-19 14:23:19 +00001483 $this->ar_wherein = array();
Derek Allard5e128942007-12-28 21:33:03 +00001484 $this->ar_aliased_tables = array();
Derek Allard09de1852007-02-14 01:35:56 +00001485 }
1486
1487 // --------------------------------------------------------------------
1488
1489 /**
1490 * Resets the active record "write" values.
1491 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001492 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001493 *
1494 * @access private
1495 * @return void
1496 */
1497 function _reset_write()
1498 {
1499 $this->ar_set = array();
1500 $this->ar_from = array();
1501 $this->ar_where = array();
Derek Allard39b622d2008-01-16 21:10:09 +00001502 $this->ar_like = array();
Derek Allardda6d2402007-12-19 14:49:29 +00001503 $this->ar_limit = FALSE;
Derek Allard39b622d2008-01-16 21:10:09 +00001504 $this->ar_order = FALSE;
1505 $this->ar_orderby = array();
Derek Allard09de1852007-02-14 01:35:56 +00001506 }
1507
1508}
1509
adminac94f382006-09-24 20:28:12 +00001510?>