blob: 4bf3098d9f7ddd64fabe4fe37c0896f0b220d088 [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
8 * @author Rick Ellis
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Allard6838f002007-10-04 19:29:59 +000010 * @license http://www.codeigniter.com/user_guide/license.html
Derek Allard09de1852007-02-14 01:35:56 +000011 * @link http://www.codeigniter.com
12 * @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
26 * @author Rick Ellis
27 * @link http://www.codeigniter.com/user_guide/database/
28 */
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
47
48 /**
49 * Select
50 *
51 * Generates the SELECT portion of the query
52 *
53 * @access public
54 * @param string
55 * @return object
56 */
57 function select($select = '*')
58 {
59 if (is_string($select))
60 {
61 $select = explode(',', $select);
62 }
63
64 foreach ($select as $val)
65 {
66 $val = trim($val);
67
68 if ($val != '')
69 $this->ar_select[] = $val;
70 }
71 return $this;
72 }
73
74 // --------------------------------------------------------------------
75
76 /**
77 * DISTINCT
78 *
79 * Sets a flag which tells the query string compiler to add DISTINCT
80 *
81 * @access public
82 * @param bool
83 * @return object
84 */
85 function distinct($val = TRUE)
86 {
87 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
88 return $this;
89 }
90
91 // --------------------------------------------------------------------
92
93 /**
94 * From
95 *
96 * Generates the FROM portion of the query
97 *
98 * @access public
99 * @param mixed can be a string or array
100 * @return object
101 */
102 function from($from)
103 {
104 foreach ((array)$from as $val)
105 {
Derek Allard5e128942007-12-28 21:33:03 +0000106 $this->_track_aliases($val);
Derek Allard09de1852007-02-14 01:35:56 +0000107 $this->ar_from[] = $this->dbprefix.$val;
108 }
Derek Allard5e128942007-12-28 21:33:03 +0000109
Derek Allard09de1852007-02-14 01:35:56 +0000110 return $this;
111 }
112
113 // --------------------------------------------------------------------
114
115 /**
116 * Join
117 *
118 * Generates the JOIN portion of the query
119 *
120 * @access public
121 * @param string
122 * @param string the join condition
123 * @param string the type of join
124 * @return object
125 */
126 function join($table, $cond, $type = '')
127 {
128 if ($type != '')
129 {
130 $type = strtoupper(trim($type));
131
132 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
133 {
134 $type = '';
135 }
136 else
137 {
138 $type .= ' ';
139 }
140 }
141
Derek Allard09de1852007-02-14 01:35:56 +0000142 // If a DB prefix is used we might need to add it to the column names
143 if ($this->dbprefix)
144 {
145 // First we remove any existing prefixes in the condition to avoid duplicates
146 $cond = preg_replace('|('.$this->dbprefix.')([\w\.]+)([\W\s]+)|', "$2$3", $cond);
147
148 // Next we add the prefixes to the condition
149 $cond = preg_replace('|([\w\.]+)([\W\s]+)(.+)|', $this->dbprefix . "$1$2" . $this->dbprefix . "$3", $cond);
Derek Allard5e128942007-12-28 21:33:03 +0000150
151 $this->_track_aliases($table);
152
153 }
154
Derek Allard09de1852007-02-14 01:35:56 +0000155 $this->ar_join[] = $type.'JOIN '.$this->dbprefix.$table.' ON '.$cond;
156 return $this;
157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Where
163 *
164 * Generates the WHERE portion of the query. Separates
165 * multiple calls with AND
166 *
167 * @access public
168 * @param mixed
169 * @param mixed
170 * @return object
171 */
172 function where($key, $value = NULL)
173 {
174 return $this->_where($key, $value, 'AND ');
175 }
176
177 // --------------------------------------------------------------------
178
179 /**
180 * OR Where
181 *
182 * Generates the WHERE portion of the query. Separates
183 * multiple calls with OR
184 *
185 * @access public
186 * @param mixed
187 * @param mixed
188 * @return object
189 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000190 function or_where($key, $value = NULL)
Derek Allard09de1852007-02-14 01:35:56 +0000191 {
192 return $this->_where($key, $value, 'OR ');
193 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000194
195 // --------------------------------------------------------------------
196
197 /**
198 * orwhere() is an alias of or_where()
199 * this function is here for backwards compatibility, as
200 * orwhere() has been deprecated
201 */
202 function orwhere($key, $value = NULL)
203 {
204 return $this->or_where($key, $value);
205 }
Derek Allard09de1852007-02-14 01:35:56 +0000206
207 // --------------------------------------------------------------------
208
209 /**
210 * Where
211 *
212 * Called by where() or orwhere()
213 *
214 * @access private
215 * @param mixed
216 * @param mixed
217 * @param string
218 * @return object
219 */
220 function _where($key, $value = NULL, $type = 'AND ')
221 {
222 if ( ! is_array($key))
223 {
224 $key = array($key => $value);
225 }
226
227 foreach ($key as $k => $v)
228 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000229
Derek Allard09de1852007-02-14 01:35:56 +0000230 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Derek Allard15ddc9d2007-12-20 13:54:39 +0000231
232 if (is_null($key[$k]))
233 {
234 // value appears not to have been set, assign the test to IS NULL
235 $k .= ' IS NULL';
236 }
Derek Allard09de1852007-02-14 01:35:56 +0000237
238 if ( ! is_null($v))
239 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000240
Derek Allard09de1852007-02-14 01:35:56 +0000241 if ( ! $this->_has_operator($k))
242 {
243 $k .= ' =';
244 }
Derek Allard15ddc9d2007-12-20 13:54:39 +0000245
Derek Allard09de1852007-02-14 01:35:56 +0000246 $v = ' '.$this->escape($v);
Derek Allard15ddc9d2007-12-20 13:54:39 +0000247
Derek Allard09de1852007-02-14 01:35:56 +0000248 }
249
250 $this->ar_where[] = $prefix.$k.$v;
251 }
252 return $this;
253 }
Derek Allard80dd7022007-12-18 23:55:06 +0000254
255 // --------------------------------------------------------------------
256
257 /**
258 * Where_in
259 *
Derek Allardc6935512007-12-19 14:23:19 +0000260 * Generates a WHERE field IN ('item', 'item') SQL query joined with
261 * AND if appropriate
Derek Allard80dd7022007-12-18 23:55:06 +0000262 *
263 * @access public
264 * @param string The field to search
265 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000266
267 * @return object
268 */
269 function where_in($key = NULL, $values = NULL)
270 {
271 return $this->_where_in($key, $values);
272 }
273
274 // --------------------------------------------------------------------
275
276 /**
277 * Where_in_or
278 *
279 * Generates a WHERE field IN ('item', 'item') SQL query joined with
280 * OR if appropriate
281 *
282 * @access public
283 * @param string The field to search
284 * @param array The values searched on
285
286 * @return object
287 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000288 function or_where_in($key = NULL, $values = NULL)
Derek Allardc6935512007-12-19 14:23:19 +0000289 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000290 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000291 }
292
293 // --------------------------------------------------------------------
294
295 /**
296 * Where_not_in
297 *
298 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
299 * with AND if appropriate
300 *
301 * @access public
302 * @param string The field to search
303 * @param array The values searched on
304
305 * @return object
306 */
307 function where_not_in($key = NULL, $values = NULL)
308 {
309 return $this->_where_in($key, $values, TRUE);
310 }
311
312 // --------------------------------------------------------------------
313
314 /**
315 * Where_not_in_or
316 *
317 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
318 * with OR if appropriate
319 *
320 * @access public
321 * @param string The field to search
322 * @param array The values searched on
323
324 * @return object
325 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000326 function or_where_not_in($key = NULL, $values = NULL)
Derek Allardc6935512007-12-19 14:23:19 +0000327 {
Derek Allard15ddc9d2007-12-20 13:54:39 +0000328 return $this->_where_in($key, $values, FALSE, 'OR ');
Derek Allardc6935512007-12-19 14:23:19 +0000329 }
330
331 // --------------------------------------------------------------------
332
333 /**
334 * Where_in
335 *
336 * Called by where_in, where_in_or, where_not_in, where_not_in_or
337 *
338 * @access public
339 * @param string The field to search
340 * @param array The values searched on
341 * @param boolean If the statement whould be IN or NOT IN
Derek Allard80dd7022007-12-18 23:55:06 +0000342 * @param string
343 * @return object
344 */
Derek Allard15ddc9d2007-12-20 13:54:39 +0000345 function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard80dd7022007-12-18 23:55:06 +0000346 {
347 if ($key === NULL || !is_array($values))
348 {
349 return;
350 }
351
Derek Allardc6935512007-12-19 14:23:19 +0000352 $not = ($not) ? ' NOT ' : '';
Derek Allard80dd7022007-12-18 23:55:06 +0000353
354 foreach ($values as $value)
355 {
356 $this->ar_wherein[] = $this->escape($value);
357 }
358
359 $prefix = (count($this->ar_where) == 0) ? '' : $type;
360
Derek Allardc6935512007-12-19 14:23:19 +0000361 $this->ar_where[] = $prefix.$key.$not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
Derek Allard80dd7022007-12-18 23:55:06 +0000362
363 return $this;
364 }
365
Derek Allard09de1852007-02-14 01:35:56 +0000366 // --------------------------------------------------------------------
367
368 /**
369 * Like
370 *
371 * Generates a %LIKE% portion of the query. Separates
372 * multiple calls with AND
373 *
374 * @access public
375 * @param mixed
376 * @param mixed
377 * @return object
378 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000379 function like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000380 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000381 return $this->_like($field, $match, 'AND ', $side);
Derek Allard09de1852007-02-14 01:35:56 +0000382 }
Derek Allarde54e3d22007-12-19 15:53:44 +0000383
384 // --------------------------------------------------------------------
385
386 /**
387 * Not Like
388 *
389 * Generates a NOT LIKE portion of the query. Separates
390 * multiple calls with AND
391 *
392 * @access public
393 * @param mixed
394 * @param mixed
395 * @return object
396 */
397 function not_like($field, $match = '', $side = 'both')
398 {
399 return $this->_like($field, $match, 'AND ', $side, ' NOT');
400 }
401
Derek Allard09de1852007-02-14 01:35:56 +0000402 // --------------------------------------------------------------------
403
404 /**
405 * OR Like
406 *
407 * Generates a %LIKE% portion of the query. Separates
408 * multiple calls with OR
409 *
410 * @access public
411 * @param mixed
412 * @param mixed
413 * @return object
414 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000415 function or_like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000416 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000417 return $this->_like($field, $match, 'OR ', $side);
418 }
419
420 // --------------------------------------------------------------------
421
422 /**
Derek Allarde54e3d22007-12-19 15:53:44 +0000423 * OR Not Like
424 *
425 * Generates a NOT LIKE portion of the query. Separates
426 * multiple calls with OR
427 *
428 * @access public
429 * @param mixed
430 * @param mixed
431 * @return object
432 */
433 function or_not_like($field, $match = '', $side = 'both')
434 {
435 return $this->_like($field, $match, 'OR ', $side, 'NOT ');
436 }
437
438 // --------------------------------------------------------------------
439
440 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000441 * orlike() is an alias of or_like()
442 * this function is here for backwards compatibility, as
443 * orlike() has been deprecated
444 */
445 function orlike($field, $match = '', $side = 'both')
446 {
447 return $this->orlike($field, $match, $side);
Derek Allard09de1852007-02-14 01:35:56 +0000448 }
449
450 // --------------------------------------------------------------------
451
452 /**
453 * Like
454 *
455 * Called by like() or orlike()
456 *
457 * @access private
458 * @param mixed
459 * @param mixed
460 * @param string
461 * @return object
462 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000463 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard09de1852007-02-14 01:35:56 +0000464 {
465 if ( ! is_array($field))
466 {
467 $field = array($field => $match);
468 }
469
470 foreach ($field as $k => $v)
Derek Allarde54e3d22007-12-19 15:53:44 +0000471 {
472
Derek Allard09de1852007-02-14 01:35:56 +0000473 $prefix = (count($this->ar_like) == 0) ? '' : $type;
Derek Allarde54e3d22007-12-19 15:53:44 +0000474
Derek Allard09de1852007-02-14 01:35:56 +0000475 $v = $this->escape_str($v);
Derek Allarde54e3d22007-12-19 15:53:44 +0000476
Derek Allard218e2bc2007-12-17 21:18:14 +0000477 if ($side == 'before')
478 {
Derek Allarde54e3d22007-12-19 15:53:44 +0000479 $this->ar_like[] = $prefix." $k $not LIKE '%{$v}'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000480 }
481 elseif ($side == 'after')
482 {
Derek Allarde54e3d22007-12-19 15:53:44 +0000483 $this->ar_like[] = $prefix." $k $not LIKE '{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000484 }
485 else
486 {
Derek Allarde54e3d22007-12-19 15:53:44 +0000487 $this->ar_like[] = $prefix." $k $not LIKE '%{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000488 }
Derek Allard09de1852007-02-14 01:35:56 +0000489 }
490 return $this;
491 }
492
493 // --------------------------------------------------------------------
494
495 /**
496 * GROUP BY
497 *
498 * @access public
499 * @param string
500 * @return object
501 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000502 function group_by($by)
Derek Allard09de1852007-02-14 01:35:56 +0000503 {
504 if (is_string($by))
505 {
506 $by = explode(',', $by);
507 }
508
509 foreach ($by as $val)
510 {
511 $val = trim($val);
512
513 if ($val != '')
Derek Allard5e128942007-12-28 21:33:03 +0000514 $this->ar_groupby[] = $this->dbprefix.$val;
Derek Allard09de1852007-02-14 01:35:56 +0000515 }
516 return $this;
517 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000518
519 // --------------------------------------------------------------------
520
521 /**
522 * groupby() is an alias of group_by()
523 * this function is here for backwards compatibility, as
524 * groupby() has been deprecated
525 */
526 function groupby($by)
527 {
528 return $this->group_by($by);
529 }
530
Derek Allard09de1852007-02-14 01:35:56 +0000531 // --------------------------------------------------------------------
532
533 /**
534 * Sets the HAVING value
535 *
536 * Separates multiple calls with AND
537 *
538 * @access public
539 * @param string
540 * @param string
541 * @return object
542 */
543 function having($key, $value = '')
544 {
545 return $this->_having($key, $value, 'AND ');
546 }
547
548 // --------------------------------------------------------------------
549
550 /**
551 * Sets the OR HAVING value
552 *
553 * Separates multiple calls with OR
554 *
555 * @access public
556 * @param string
557 * @param string
558 * @return object
559 */
560 function orhaving($key, $value = '')
561 {
562 return $this->_having($key, $value, 'OR ');
563 }
564
565 // --------------------------------------------------------------------
566
567 /**
568 * Sets the HAVING values
569 *
570 * Called by having() or orhaving()
571 *
572 * @access private
573 * @param string
574 * @param string
575 * @return object
576 */
577 function _having($key, $value = '', $type = 'AND ')
578 {
579 if ( ! is_array($key))
580 {
581 $key = array($key => $value);
582 }
583
584 foreach ($key as $k => $v)
585 {
586 $prefix = (count($this->ar_having) == 0) ? '' : $type;
587
588 if ($v != '')
589 {
590 $v = ' '.$this->escape($v);
591 }
592
593 $this->ar_having[] = $prefix.$k.$v;
594 }
595 return $this;
596 }
597
598 // --------------------------------------------------------------------
599
600 /**
601 * Sets the ORDER BY value
602 *
603 * @access public
604 * @param string
605 * @param string direction: asc or desc
606 * @return object
607 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000608 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000609 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000610 if (strtolower($direction) == 'random')
611 {
612 $orderby = ''; // Random results want or don't need a field name
613 $direction = $this->_random_keyword;
614 }
615 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000616 {
Derek Allard92782492007-08-10 11:26:01 +0000617 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000618 }
619
620 $this->ar_orderby[] = $orderby.$direction;
621 return $this;
622 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000623
Derek Allard218e2bc2007-12-17 21:18:14 +0000624 // --------------------------------------------------------------------
625
626 /**
627 * orderby() is an alias of order_by()
628 * this function is here for backwards compatibility, as
629 * orderby() has been deprecated
630 */
631 function orderby($orderby, $direction = '')
632 {
633 return $this->order_by($orderby, $direction);
634 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000635
Derek Allard09de1852007-02-14 01:35:56 +0000636 // --------------------------------------------------------------------
637
638 /**
639 * Sets the LIMIT value
640 *
641 * @access public
642 * @param integer the limit value
643 * @param integer the offset value
644 * @return object
645 */
646 function limit($value, $offset = '')
647 {
648 $this->ar_limit = $value;
649
650 if ($offset != '')
651 $this->ar_offset = $offset;
652
653 return $this;
654 }
655
656 // --------------------------------------------------------------------
657
658 /**
659 * Sets the OFFSET value
660 *
661 * @access public
662 * @param integer the offset value
663 * @return object
664 */
665 function offset($value)
666 {
667 $this->ar_offset = $value;
668 return $this;
669 }
670
671 // --------------------------------------------------------------------
672
673 /**
674 * The "set" function. Allows key/value pairs to be set for inserting or updating
675 *
676 * @access public
677 * @param mixed
678 * @param string
679 * @return object
680 */
681 function set($key, $value = '')
682 {
683 $key = $this->_object_to_array($key);
684
685 if ( ! is_array($key))
686 {
687 $key = array($key => $value);
688 }
689
690 foreach ($key as $k => $v)
691 {
692 $this->ar_set[$k] = $this->escape($v);
693 }
694
695 return $this;
696 }
697
698 // --------------------------------------------------------------------
699
700 /**
701 * Get
702 *
703 * Compiles the select statement based on the other functions called
704 * and runs the query
705 *
706 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +0000707 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +0000708 * @param string the limit clause
709 * @param string the offset clause
710 * @return object
711 */
712 function get($table = '', $limit = null, $offset = null)
713 {
714 if ($table != '')
715 {
Derek Allard5e128942007-12-28 21:33:03 +0000716 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +0000717 $this->from($table);
718 }
719
720 if ( ! is_null($limit))
721 {
722 $this->limit($limit, $offset);
723 }
724
725 $sql = $this->_compile_select();
726
727 $result = $this->query($sql);
728 $this->_reset_select();
729 return $result;
730 }
731
732 // --------------------------------------------------------------------
733
734 /**
Derek Allard694b5b82007-12-18 15:58:03 +0000735 * "Count All Results" query
736 *
737 * Generates a platform-specific query string that counts all records
738 * returned by an Active Record query.
739 *
740 * @access public
741 * @param string
742 * @return string
743 */
744 function count_all_results($table = '')
745 {
746 if ($table != '')
747 {
Derek Allard5e128942007-12-28 21:33:03 +0000748 $this->_track_aliases($table);
Derek Allard694b5b82007-12-18 15:58:03 +0000749 $this->from($table);
750 }
751
Derek Allard6ddb5a12007-12-18 17:22:50 +0000752 $sql = $this->_compile_select($this->_count_string);
Derek Allard694b5b82007-12-18 15:58:03 +0000753
754 $query = $this->query($sql);
755 $this->_reset_select();
756
757 if ($query->num_rows() == 0)
758 {
759 return '0';
760 }
761
762 $row = $query->row();
763 return $row->numrows;
764 }
765
766 // --------------------------------------------------------------------
767
768 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000769 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +0000770 *
771 * Allows the where clause, limit and offset to be added directly
772 *
773 * @access public
774 * @param string the where clause
775 * @param string the limit clause
776 * @param string the offset clause
777 * @return object
778 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000779 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +0000780 {
781 if ($table != '')
782 {
Derek Allard5e128942007-12-28 21:33:03 +0000783 $this->_track_aliases($table);
Derek Allard09de1852007-02-14 01:35:56 +0000784 $this->from($table);
785 }
786
787 if ( ! is_null($where))
788 {
789 $this->where($where);
790 }
791
792 if ( ! is_null($limit))
793 {
794 $this->limit($limit, $offset);
795 }
796
797 $sql = $this->_compile_select();
798
799 $result = $this->query($sql);
800 $this->_reset_select();
801 return $result;
802 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000803
804 // --------------------------------------------------------------------
805
806 /**
807 * getwhere() is an alias of get_where()
808 * this function is here for backwards compatibility, as
809 * getwhere() has been deprecated
810 */
811 function getwhere($table = '', $where = null, $limit = null, $offset = null)
812 {
813 return $this->get_where($table, $where, $limit, $offset);
814 }
Derek Allard09de1852007-02-14 01:35:56 +0000815
816 // --------------------------------------------------------------------
817
818 /**
819 * Insert
820 *
821 * Compiles an insert string and runs the query
822 *
823 * @access public
824 * @param string the table to retrieve the results from
825 * @param array an associative array of insert values
826 * @return object
827 */
828 function insert($table = '', $set = NULL)
829 {
830 if ( ! is_null($set))
831 {
832 $this->set($set);
833 }
834
835 if (count($this->ar_set) == 0)
836 {
837 if ($this->db_debug)
838 {
839 return $this->display_error('db_must_use_set');
840 }
841 return FALSE;
842 }
843
844 if ($table == '')
845 {
846 if ( ! isset($this->ar_from[0]))
847 {
848 if ($this->db_debug)
849 {
850 return $this->display_error('db_must_set_table');
851 }
852 return FALSE;
853 }
854
855 $table = $this->ar_from[0];
856 }
857
858 $sql = $this->_insert($this->dbprefix.$table, array_keys($this->ar_set), array_values($this->ar_set));
859
860 $this->_reset_write();
861 return $this->query($sql);
862 }
863
864 // --------------------------------------------------------------------
865
866 /**
867 * Update
868 *
869 * Compiles an update string and runs the query
870 *
871 * @access public
872 * @param string the table to retrieve the results from
873 * @param array an associative array of update values
874 * @param mixed the where clause
875 * @return object
876 */
Derek Allard5e128942007-12-28 21:33:03 +0000877 function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +0000878 {
879 if ( ! is_null($set))
880 {
881 $this->set($set);
882 }
883
884 if (count($this->ar_set) == 0)
885 {
886 if ($this->db_debug)
887 {
888 return $this->display_error('db_must_use_set');
889 }
890 return FALSE;
891 }
892
893 if ($table == '')
894 {
895 if ( ! isset($this->ar_from[0]))
896 {
897 if ($this->db_debug)
898 {
899 return $this->display_error('db_must_set_table');
900 }
901 return FALSE;
902 }
903
904 $table = $this->ar_from[0];
905 }
906
Derek Allarde77d77c2007-12-19 15:01:55 +0000907 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +0000908 {
909 $this->where($where);
910 }
Derek Allardda6d2402007-12-19 14:49:29 +0000911
Derek Allarde77d77c2007-12-19 15:01:55 +0000912 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +0000913 {
914 $this->limit($limit);
915 }
Derek Allard09de1852007-02-14 01:35:56 +0000916
Derek Allardda6d2402007-12-19 14:49:29 +0000917 $sql = $this->_update($this->dbprefix.$table, $this->ar_set, $this->ar_where, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +0000918
919 $this->_reset_write();
920 return $this->query($sql);
921 }
922
923 // --------------------------------------------------------------------
924
925 /**
926 * Delete
927 *
928 * Compiles a delete string and runs the query
929 *
930 * @access public
Derek Allard41f60d42007-12-20 20:09:22 +0000931 * @param mixed the table(s) to delete from. String or array
Derek Allard09de1852007-02-14 01:35:56 +0000932 * @param mixed the where clause
Derek Allard41f60d42007-12-20 20:09:22 +0000933 * @param mixed the limit clause
934 * @param boolean
Derek Allard09de1852007-02-14 01:35:56 +0000935 * @return object
936 */
Derek Allard41f60d42007-12-20 20:09:22 +0000937 function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard09de1852007-02-14 01:35:56 +0000938 {
939 if ($table == '')
940 {
941 if ( ! isset($this->ar_from[0]))
942 {
943 if ($this->db_debug)
944 {
945 return $this->display_error('db_must_set_table');
946 }
947 return FALSE;
948 }
949
950 $table = $this->ar_from[0];
951 }
952
Derek Allard41f60d42007-12-20 20:09:22 +0000953 if (is_array($table))
954 {
955 foreach($table as $single_table)
956 {
957 $this->delete($this->dbprefix.$single_table, $where, $limit, FALSE);
958 }
959 $this->_reset_write();
960 return;
961 }
962
Derek Allard09de1852007-02-14 01:35:56 +0000963 if ($where != '')
964 {
965 $this->where($where);
966 }
967
Derek Allarde77d77c2007-12-19 15:01:55 +0000968 if ($limit != NULL)
969 {
970 $this->limit($limit);
971 }
972
Derek Allard09de1852007-02-14 01:35:56 +0000973 if (count($this->ar_where) == 0)
974 {
975 if ($this->db_debug)
976 {
977 return $this->display_error('db_del_must_use_where');
978 }
979 return FALSE;
980 }
Derek Allard41f60d42007-12-20 20:09:22 +0000981
Derek Allarde77d77c2007-12-19 15:01:55 +0000982 $sql = $this->_delete($this->dbprefix.$table, $this->ar_where, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +0000983
Derek Allard41f60d42007-12-20 20:09:22 +0000984 if ($reset_data)
985 {
986 $this->_reset_write();
987 }
Derek Allard09de1852007-02-14 01:35:56 +0000988 return $this->query($sql);
989 }
Derek Allard15ddc9d2007-12-20 13:54:39 +0000990
Derek Allard09de1852007-02-14 01:35:56 +0000991 // --------------------------------------------------------------------
992
993 /**
994 * Use Table - DEPRECATED
995 *
996 * @deprecated use $this->db->from instead
997 */
998 function use_table($table)
999 {
1000 return $this->from($table);
1001 return $this;
1002 }
1003
1004 // --------------------------------------------------------------------
1005
1006 /**
Derek Allard09de1852007-02-14 01:35:56 +00001007 * Tests whether the string has an SQL operator
1008 *
1009 * @access private
1010 * @param string
1011 * @return bool
1012 */
1013 function _has_operator($str)
1014 {
1015 $str = trim($str);
1016 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
1017 {
1018 return FALSE;
1019 }
1020
1021 return TRUE;
1022 }
1023
1024 // --------------------------------------------------------------------
1025
1026 /**
Derek Allard5e128942007-12-28 21:33:03 +00001027 * Track Aliases
1028 *
1029 * Used to track SQL statements written with aliased tables.
1030 *
1031 * @access private
1032 * @param string The table to inspect
1033 * @return string
1034 */
1035 function _track_aliases($table)
1036 {
1037 // if a table alias is used we can recognize it by a space
1038 if (strpos($table, " ") !== FALSE)
1039 {
1040 // if the alias is written with the AS keyowrd, get it out
1041 $table = preg_replace('/AS/i', '', $table);
1042
1043 $this->ar_aliased_tables[] = trim(strrchr($table, " ") . '.');
1044 }
1045
1046 return $this->dbprefix.$table;
1047 }
1048
1049 // --------------------------------------------------------------------
1050
1051 /**
1052 * Filter Table Aliases
1053 *
1054 * Intelligently removes database prefixes from aliased tables
1055 *
1056 * @access private
1057 * @param array An array of compiled SQL
1058 * @return array Cleaned up statement with aliases accounted for
1059 */
1060 function _filter_table_aliases($statements)
1061 {
1062 $filter_tables_with_aliases = array();
1063
1064 foreach ($statements as $statement)
1065 {
1066 $tables_with_dbprefix = array();
1067
1068 foreach ($this->ar_aliased_tables as $k => $v)
1069 {
1070 $tables_with_dbprefix[$k] = '/'.$this->dbprefix.str_replace('.', '', $v).'\./';
1071 }
1072
1073 $statement = preg_replace($tables_with_dbprefix, $this->ar_aliased_tables, $statement);
1074
1075 $filter_tables_with_aliases[] = $statement;
1076 }
1077
1078 return $filter_tables_with_aliases;
1079 }
1080
1081 // --------------------------------------------------------------------
1082
1083 /**
Derek Allard09de1852007-02-14 01:35:56 +00001084 * Compile the SELECT statement
1085 *
1086 * Generates a query string based on which functions were used.
1087 * Should not be called directly. The get() function calls it.
1088 *
1089 * @access private
1090 * @return string
1091 */
Derek Allard694b5b82007-12-18 15:58:03 +00001092 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001093 {
1094 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
1095
1096 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->ar_select);
1097
Derek Allard694b5b82007-12-18 15:58:03 +00001098 if ($select_override !== FALSE)
1099 {
1100 $sql = $select_override;
1101 }
1102
Derek Allard09de1852007-02-14 01:35:56 +00001103 if (count($this->ar_from) > 0)
1104 {
1105 $sql .= "\nFROM ";
Derek Allard15ddc9d2007-12-20 13:54:39 +00001106 $sql .= '(' . implode(', ', $this->ar_from) . ')';
Derek Allard09de1852007-02-14 01:35:56 +00001107 }
1108
1109 if (count($this->ar_join) > 0)
Derek Allard5e128942007-12-28 21:33:03 +00001110 {
Derek Allard09de1852007-02-14 01:35:56 +00001111 $sql .= "\n";
Derek Allard5e128942007-12-28 21:33:03 +00001112
1113 // special consideration for table aliases
1114 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1115 {
1116 $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
1117 }
1118 else
1119 {
1120 $sql .= implode("\n", $this->ar_join);
1121 }
1122
Derek Allard09de1852007-02-14 01:35:56 +00001123 }
1124
1125 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1126 {
1127 $sql .= "\nWHERE ";
1128 }
1129
1130 $sql .= implode("\n", $this->ar_where);
1131
1132 if (count($this->ar_like) > 0)
1133 {
1134 if (count($this->ar_where) > 0)
1135 {
1136 $sql .= " AND ";
1137 }
1138
1139 $sql .= implode("\n", $this->ar_like);
1140 }
1141
1142 if (count($this->ar_groupby) > 0)
1143 {
Derek Allard5e128942007-12-28 21:33:03 +00001144
Derek Allard09de1852007-02-14 01:35:56 +00001145 $sql .= "\nGROUP BY ";
Derek Allard5e128942007-12-28 21:33:03 +00001146
1147 // special consideration for table aliases
1148 if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
1149 {
1150 $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
1151 }
1152 else
1153 {
1154 $sql .= implode(', ', $this->ar_groupby);
1155 }
Derek Allard09de1852007-02-14 01:35:56 +00001156 }
1157
1158 if (count($this->ar_having) > 0)
1159 {
1160 $sql .= "\nHAVING ";
1161 $sql .= implode("\n", $this->ar_having);
1162 }
1163
1164 if (count($this->ar_orderby) > 0)
1165 {
1166 $sql .= "\nORDER BY ";
1167 $sql .= implode(', ', $this->ar_orderby);
1168
1169 if ($this->ar_order !== FALSE)
1170 {
1171 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1172 }
1173 }
1174
1175 if (is_numeric($this->ar_limit))
1176 {
1177 $sql .= "\n";
1178 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1179 }
1180
1181 return $sql;
1182 }
1183
1184 // --------------------------------------------------------------------
1185
1186 /**
1187 * Object to Array
1188 *
1189 * Takes an object as input and converts the class variables to array key/vals
1190 *
1191 * @access public
1192 * @param object
1193 * @return array
1194 */
1195 function _object_to_array($object)
1196 {
1197 if ( ! is_object($object))
1198 {
1199 return $object;
1200 }
1201
1202 $array = array();
1203 foreach (get_object_vars($object) as $key => $val)
1204 {
1205 if ( ! is_object($val) AND ! is_array($val))
1206 {
1207 $array[$key] = $val;
1208 }
1209 }
1210
1211 return $array;
1212 }
1213
1214 // --------------------------------------------------------------------
1215
1216 /**
1217 * Resets the active record values. Called by the get() function
1218 *
1219 * @access private
1220 * @return void
1221 */
1222 function _reset_select()
1223 {
1224 $this->ar_select = array();
1225 $this->ar_distinct = FALSE;
1226 $this->ar_from = array();
1227 $this->ar_join = array();
1228 $this->ar_where = array();
1229 $this->ar_like = array();
1230 $this->ar_groupby = array();
1231 $this->ar_having = array();
1232 $this->ar_limit = FALSE;
1233 $this->ar_offset = FALSE;
1234 $this->ar_order = FALSE;
1235 $this->ar_orderby = array();
Derek Allardc6935512007-12-19 14:23:19 +00001236 $this->ar_wherein = array();
Derek Allard5e128942007-12-28 21:33:03 +00001237 $this->ar_aliased_tables = array();
Derek Allard09de1852007-02-14 01:35:56 +00001238 }
1239
1240 // --------------------------------------------------------------------
1241
1242 /**
1243 * Resets the active record "write" values.
1244 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001245 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001246 *
1247 * @access private
1248 * @return void
1249 */
1250 function _reset_write()
1251 {
1252 $this->ar_set = array();
1253 $this->ar_from = array();
1254 $this->ar_where = array();
Derek Allardda6d2402007-12-19 14:49:29 +00001255 $this->ar_limit = FALSE;
Derek Allard09de1852007-02-14 01:35:56 +00001256 }
1257
1258}
1259
adminac94f382006-09-24 20:28:12 +00001260?>