blob: 0a4327b426d230fe2f5dd4459369803db18ff49f [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();
45
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 {
106 $this->ar_from[] = $this->dbprefix.$val;
107 }
108 return $this;
109 }
110
111 // --------------------------------------------------------------------
112
113 /**
114 * Join
115 *
116 * Generates the JOIN portion of the query
117 *
118 * @access public
119 * @param string
120 * @param string the join condition
121 * @param string the type of join
122 * @return object
123 */
124 function join($table, $cond, $type = '')
125 {
126 if ($type != '')
127 {
128 $type = strtoupper(trim($type));
129
130 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
131 {
132 $type = '';
133 }
134 else
135 {
136 $type .= ' ';
137 }
138 }
139
140 if ($this->dbprefix)
141 {
142 $cond = preg_replace('|([\w\.]+)([\W\s]+)(.+)|', $this->dbprefix . "$1$2" . $this->dbprefix . "$3", $cond);
143 }
144
145 // If a DB prefix is used we might need to add it to the column names
146 if ($this->dbprefix)
147 {
148 // First we remove any existing prefixes in the condition to avoid duplicates
149 $cond = preg_replace('|('.$this->dbprefix.')([\w\.]+)([\W\s]+)|', "$2$3", $cond);
150
151 // Next we add the prefixes to the condition
152 $cond = preg_replace('|([\w\.]+)([\W\s]+)(.+)|', $this->dbprefix . "$1$2" . $this->dbprefix . "$3", $cond);
153 }
154
155 $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 {
229 $prefix = (count($this->ar_where) == 0) ? '' : $type;
230
231 if ( ! is_null($v))
232 {
233 if ( ! $this->_has_operator($k))
234 {
235 $k .= ' =';
236 }
237
238 $v = ' '.$this->escape($v);
239 }
240
241 $this->ar_where[] = $prefix.$k.$v;
242 }
243 return $this;
244 }
Derek Allard80dd7022007-12-18 23:55:06 +0000245
246 // --------------------------------------------------------------------
247
248 /**
249 * Where_in
250 *
Derek Allardc6935512007-12-19 14:23:19 +0000251 * Generates a WHERE field IN ('item', 'item') SQL query joined with
252 * AND if appropriate
Derek Allard80dd7022007-12-18 23:55:06 +0000253 *
254 * @access public
255 * @param string The field to search
256 * @param array The values searched on
Derek Allardc6935512007-12-19 14:23:19 +0000257
258 * @return object
259 */
260 function where_in($key = NULL, $values = NULL)
261 {
262 return $this->_where_in($key, $values);
263 }
264
265 // --------------------------------------------------------------------
266
267 /**
268 * Where_in_or
269 *
270 * Generates a WHERE field IN ('item', 'item') SQL query joined with
271 * OR if appropriate
272 *
273 * @access public
274 * @param string The field to search
275 * @param array The values searched on
276
277 * @return object
278 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000279 function or_where_in($key = NULL, $values = NULL)
Derek Allardc6935512007-12-19 14:23:19 +0000280 {
281 return $this->_where_in($key, $values, FALSE, 'or');
282 }
283
284 // --------------------------------------------------------------------
285
286 /**
287 * Where_not_in
288 *
289 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
290 * with AND if appropriate
291 *
292 * @access public
293 * @param string The field to search
294 * @param array The values searched on
295
296 * @return object
297 */
298 function where_not_in($key = NULL, $values = NULL)
299 {
300 return $this->_where_in($key, $values, TRUE);
301 }
302
303 // --------------------------------------------------------------------
304
305 /**
306 * Where_not_in_or
307 *
308 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
309 * with OR if appropriate
310 *
311 * @access public
312 * @param string The field to search
313 * @param array The values searched on
314
315 * @return object
316 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000317 function or_where_not_in($key = NULL, $values = NULL)
Derek Allardc6935512007-12-19 14:23:19 +0000318 {
319 return $this->_where_in($key, $values, FALSE, 'or');
320 }
321
322 // --------------------------------------------------------------------
323
324 /**
325 * Where_in
326 *
327 * Called by where_in, where_in_or, where_not_in, where_not_in_or
328 *
329 * @access public
330 * @param string The field to search
331 * @param array The values searched on
332 * @param boolean If the statement whould be IN or NOT IN
Derek Allard80dd7022007-12-18 23:55:06 +0000333 * @param string
334 * @return object
335 */
Derek Allardc6935512007-12-19 14:23:19 +0000336 function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'and')
Derek Allard80dd7022007-12-18 23:55:06 +0000337 {
338 if ($key === NULL || !is_array($values))
339 {
340 return;
341 }
342
Derek Allardc6935512007-12-19 14:23:19 +0000343 $not = ($not) ? ' NOT ' : '';
Derek Allard80dd7022007-12-18 23:55:06 +0000344 $type = (strtolower($type) == 'or') ? ' OR ' : ' AND ';
345
346 foreach ($values as $value)
347 {
348 $this->ar_wherein[] = $this->escape($value);
349 }
350
351 $prefix = (count($this->ar_where) == 0) ? '' : $type;
352
Derek Allardc6935512007-12-19 14:23:19 +0000353 $this->ar_where[] = $prefix.$key.$not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
Derek Allard80dd7022007-12-18 23:55:06 +0000354
355 return $this;
356 }
357
Derek Allard09de1852007-02-14 01:35:56 +0000358 // --------------------------------------------------------------------
359
360 /**
361 * Like
362 *
363 * Generates a %LIKE% portion of the query. Separates
364 * multiple calls with AND
365 *
366 * @access public
367 * @param mixed
368 * @param mixed
369 * @return object
370 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000371 function like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000372 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000373 return $this->_like($field, $match, 'AND ', $side);
Derek Allard09de1852007-02-14 01:35:56 +0000374 }
Derek Allarde54e3d22007-12-19 15:53:44 +0000375
376 // --------------------------------------------------------------------
377
378 /**
379 * Not Like
380 *
381 * Generates a NOT LIKE portion of the query. Separates
382 * multiple calls with AND
383 *
384 * @access public
385 * @param mixed
386 * @param mixed
387 * @return object
388 */
389 function not_like($field, $match = '', $side = 'both')
390 {
391 return $this->_like($field, $match, 'AND ', $side, ' NOT');
392 }
393
Derek Allard09de1852007-02-14 01:35:56 +0000394 // --------------------------------------------------------------------
395
396 /**
397 * OR Like
398 *
399 * Generates a %LIKE% portion of the query. Separates
400 * multiple calls with OR
401 *
402 * @access public
403 * @param mixed
404 * @param mixed
405 * @return object
406 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000407 function or_like($field, $match = '', $side = 'both')
Derek Allard09de1852007-02-14 01:35:56 +0000408 {
Derek Allard218e2bc2007-12-17 21:18:14 +0000409 return $this->_like($field, $match, 'OR ', $side);
410 }
411
412 // --------------------------------------------------------------------
413
414 /**
Derek Allarde54e3d22007-12-19 15:53:44 +0000415 * OR Not Like
416 *
417 * Generates a NOT LIKE portion of the query. Separates
418 * multiple calls with OR
419 *
420 * @access public
421 * @param mixed
422 * @param mixed
423 * @return object
424 */
425 function or_not_like($field, $match = '', $side = 'both')
426 {
427 return $this->_like($field, $match, 'OR ', $side, 'NOT ');
428 }
429
430 // --------------------------------------------------------------------
431
432 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000433 * orlike() is an alias of or_like()
434 * this function is here for backwards compatibility, as
435 * orlike() has been deprecated
436 */
437 function orlike($field, $match = '', $side = 'both')
438 {
439 return $this->orlike($field, $match, $side);
Derek Allard09de1852007-02-14 01:35:56 +0000440 }
441
442 // --------------------------------------------------------------------
443
444 /**
445 * Like
446 *
447 * Called by like() or orlike()
448 *
449 * @access private
450 * @param mixed
451 * @param mixed
452 * @param string
453 * @return object
454 */
Derek Allarde54e3d22007-12-19 15:53:44 +0000455 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard09de1852007-02-14 01:35:56 +0000456 {
457 if ( ! is_array($field))
458 {
459 $field = array($field => $match);
460 }
461
462 foreach ($field as $k => $v)
Derek Allarde54e3d22007-12-19 15:53:44 +0000463 {
464
Derek Allard09de1852007-02-14 01:35:56 +0000465 $prefix = (count($this->ar_like) == 0) ? '' : $type;
Derek Allarde54e3d22007-12-19 15:53:44 +0000466
Derek Allard09de1852007-02-14 01:35:56 +0000467 $v = $this->escape_str($v);
Derek Allarde54e3d22007-12-19 15:53:44 +0000468
Derek Allard218e2bc2007-12-17 21:18:14 +0000469 if ($side == 'before')
470 {
Derek Allarde54e3d22007-12-19 15:53:44 +0000471 $this->ar_like[] = $prefix." $k $not LIKE '%{$v}'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000472 }
473 elseif ($side == 'after')
474 {
Derek Allarde54e3d22007-12-19 15:53:44 +0000475 $this->ar_like[] = $prefix." $k $not LIKE '{$v}%'";
Derek Allard218e2bc2007-12-17 21:18:14 +0000476 }
477 else
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 }
Derek Allard09de1852007-02-14 01:35:56 +0000481 }
482 return $this;
483 }
484
485 // --------------------------------------------------------------------
486
487 /**
488 * GROUP BY
489 *
490 * @access public
491 * @param string
492 * @return object
493 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000494 function group_by($by)
Derek Allard09de1852007-02-14 01:35:56 +0000495 {
496 if (is_string($by))
497 {
498 $by = explode(',', $by);
499 }
500
501 foreach ($by as $val)
502 {
503 $val = trim($val);
504
505 if ($val != '')
506 $this->ar_groupby[] = $val;
507 }
508 return $this;
509 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000510
511 // --------------------------------------------------------------------
512
513 /**
514 * groupby() is an alias of group_by()
515 * this function is here for backwards compatibility, as
516 * groupby() has been deprecated
517 */
518 function groupby($by)
519 {
520 return $this->group_by($by);
521 }
522
Derek Allard09de1852007-02-14 01:35:56 +0000523 // --------------------------------------------------------------------
524
525 /**
526 * Sets the HAVING value
527 *
528 * Separates multiple calls with AND
529 *
530 * @access public
531 * @param string
532 * @param string
533 * @return object
534 */
535 function having($key, $value = '')
536 {
537 return $this->_having($key, $value, 'AND ');
538 }
539
540 // --------------------------------------------------------------------
541
542 /**
543 * Sets the OR HAVING value
544 *
545 * Separates multiple calls with OR
546 *
547 * @access public
548 * @param string
549 * @param string
550 * @return object
551 */
552 function orhaving($key, $value = '')
553 {
554 return $this->_having($key, $value, 'OR ');
555 }
556
557 // --------------------------------------------------------------------
558
559 /**
560 * Sets the HAVING values
561 *
562 * Called by having() or orhaving()
563 *
564 * @access private
565 * @param string
566 * @param string
567 * @return object
568 */
569 function _having($key, $value = '', $type = 'AND ')
570 {
571 if ( ! is_array($key))
572 {
573 $key = array($key => $value);
574 }
575
576 foreach ($key as $k => $v)
577 {
578 $prefix = (count($this->ar_having) == 0) ? '' : $type;
579
580 if ($v != '')
581 {
582 $v = ' '.$this->escape($v);
583 }
584
585 $this->ar_having[] = $prefix.$k.$v;
586 }
587 return $this;
588 }
589
590 // --------------------------------------------------------------------
591
592 /**
593 * Sets the ORDER BY value
594 *
595 * @access public
596 * @param string
597 * @param string direction: asc or desc
598 * @return object
599 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000600 function order_by($orderby, $direction = '')
Derek Allard09de1852007-02-14 01:35:56 +0000601 {
Derek Allard6ddb5a12007-12-18 17:22:50 +0000602 if (strtolower($direction) == 'random')
603 {
604 $orderby = ''; // Random results want or don't need a field name
605 $direction = $this->_random_keyword;
606 }
607 elseif (trim($direction) != '')
Derek Allard09de1852007-02-14 01:35:56 +0000608 {
Derek Allard92782492007-08-10 11:26:01 +0000609 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
Derek Allard09de1852007-02-14 01:35:56 +0000610 }
611
612 $this->ar_orderby[] = $orderby.$direction;
613 return $this;
614 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000615
Derek Allard218e2bc2007-12-17 21:18:14 +0000616 // --------------------------------------------------------------------
617
618 /**
619 * orderby() is an alias of order_by()
620 * this function is here for backwards compatibility, as
621 * orderby() has been deprecated
622 */
623 function orderby($orderby, $direction = '')
624 {
625 return $this->order_by($orderby, $direction);
626 }
Derek Allard6ddb5a12007-12-18 17:22:50 +0000627
Derek Allard09de1852007-02-14 01:35:56 +0000628 // --------------------------------------------------------------------
629
630 /**
631 * Sets the LIMIT value
632 *
633 * @access public
634 * @param integer the limit value
635 * @param integer the offset value
636 * @return object
637 */
638 function limit($value, $offset = '')
639 {
640 $this->ar_limit = $value;
641
642 if ($offset != '')
643 $this->ar_offset = $offset;
644
645 return $this;
646 }
647
648 // --------------------------------------------------------------------
649
650 /**
651 * Sets the OFFSET value
652 *
653 * @access public
654 * @param integer the offset value
655 * @return object
656 */
657 function offset($value)
658 {
659 $this->ar_offset = $value;
660 return $this;
661 }
662
663 // --------------------------------------------------------------------
664
665 /**
666 * The "set" function. Allows key/value pairs to be set for inserting or updating
667 *
668 * @access public
669 * @param mixed
670 * @param string
671 * @return object
672 */
673 function set($key, $value = '')
674 {
675 $key = $this->_object_to_array($key);
676
677 if ( ! is_array($key))
678 {
679 $key = array($key => $value);
680 }
681
682 foreach ($key as $k => $v)
683 {
684 $this->ar_set[$k] = $this->escape($v);
685 }
686
687 return $this;
688 }
689
690 // --------------------------------------------------------------------
691
692 /**
693 * Get
694 *
695 * Compiles the select statement based on the other functions called
696 * and runs the query
697 *
698 * @access public
Derek Allard694b5b82007-12-18 15:58:03 +0000699 * @param string the table
Derek Allard09de1852007-02-14 01:35:56 +0000700 * @param string the limit clause
701 * @param string the offset clause
702 * @return object
703 */
704 function get($table = '', $limit = null, $offset = null)
705 {
706 if ($table != '')
707 {
708 $this->from($table);
709 }
710
711 if ( ! is_null($limit))
712 {
713 $this->limit($limit, $offset);
714 }
715
716 $sql = $this->_compile_select();
717
718 $result = $this->query($sql);
719 $this->_reset_select();
720 return $result;
721 }
722
723 // --------------------------------------------------------------------
724
725 /**
Derek Allard694b5b82007-12-18 15:58:03 +0000726 * "Count All Results" query
727 *
728 * Generates a platform-specific query string that counts all records
729 * returned by an Active Record query.
730 *
731 * @access public
732 * @param string
733 * @return string
734 */
735 function count_all_results($table = '')
736 {
737 if ($table != '')
738 {
739 $this->from($table);
740 }
741
Derek Allard6ddb5a12007-12-18 17:22:50 +0000742 $sql = $this->_compile_select($this->_count_string);
Derek Allard694b5b82007-12-18 15:58:03 +0000743
744 $query = $this->query($sql);
745 $this->_reset_select();
746
747 if ($query->num_rows() == 0)
748 {
749 return '0';
750 }
751
752 $row = $query->row();
753 return $row->numrows;
754 }
755
756 // --------------------------------------------------------------------
757
758 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000759 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +0000760 *
761 * Allows the where clause, limit and offset to be added directly
762 *
763 * @access public
764 * @param string the where clause
765 * @param string the limit clause
766 * @param string the offset clause
767 * @return object
768 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000769 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +0000770 {
771 if ($table != '')
772 {
773 $this->from($table);
774 }
775
776 if ( ! is_null($where))
777 {
778 $this->where($where);
779 }
780
781 if ( ! is_null($limit))
782 {
783 $this->limit($limit, $offset);
784 }
785
786 $sql = $this->_compile_select();
787
788 $result = $this->query($sql);
789 $this->_reset_select();
790 return $result;
791 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000792
793 // --------------------------------------------------------------------
794
795 /**
796 * getwhere() is an alias of get_where()
797 * this function is here for backwards compatibility, as
798 * getwhere() has been deprecated
799 */
800 function getwhere($table = '', $where = null, $limit = null, $offset = null)
801 {
802 return $this->get_where($table, $where, $limit, $offset);
803 }
Derek Allard09de1852007-02-14 01:35:56 +0000804
805 // --------------------------------------------------------------------
806
807 /**
808 * Insert
809 *
810 * Compiles an insert string and runs the query
811 *
812 * @access public
813 * @param string the table to retrieve the results from
814 * @param array an associative array of insert values
815 * @return object
816 */
817 function insert($table = '', $set = NULL)
818 {
819 if ( ! is_null($set))
820 {
821 $this->set($set);
822 }
823
824 if (count($this->ar_set) == 0)
825 {
826 if ($this->db_debug)
827 {
828 return $this->display_error('db_must_use_set');
829 }
830 return FALSE;
831 }
832
833 if ($table == '')
834 {
835 if ( ! isset($this->ar_from[0]))
836 {
837 if ($this->db_debug)
838 {
839 return $this->display_error('db_must_set_table');
840 }
841 return FALSE;
842 }
843
844 $table = $this->ar_from[0];
845 }
846
847 $sql = $this->_insert($this->dbprefix.$table, array_keys($this->ar_set), array_values($this->ar_set));
848
849 $this->_reset_write();
850 return $this->query($sql);
851 }
852
853 // --------------------------------------------------------------------
854
855 /**
856 * Update
857 *
858 * Compiles an update string and runs the query
859 *
860 * @access public
861 * @param string the table to retrieve the results from
862 * @param array an associative array of update values
863 * @param mixed the where clause
864 * @return object
865 */
Derek Allardda6d2402007-12-19 14:49:29 +0000866 function update($table = '', $set = NULL, $where = null, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +0000867 {
868 if ( ! is_null($set))
869 {
870 $this->set($set);
871 }
872
873 if (count($this->ar_set) == 0)
874 {
875 if ($this->db_debug)
876 {
877 return $this->display_error('db_must_use_set');
878 }
879 return FALSE;
880 }
881
882 if ($table == '')
883 {
884 if ( ! isset($this->ar_from[0]))
885 {
886 if ($this->db_debug)
887 {
888 return $this->display_error('db_must_set_table');
889 }
890 return FALSE;
891 }
892
893 $table = $this->ar_from[0];
894 }
895
Derek Allarde77d77c2007-12-19 15:01:55 +0000896 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +0000897 {
898 $this->where($where);
899 }
Derek Allardda6d2402007-12-19 14:49:29 +0000900
Derek Allarde77d77c2007-12-19 15:01:55 +0000901 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +0000902 {
903 $this->limit($limit);
904 }
Derek Allard09de1852007-02-14 01:35:56 +0000905
Derek Allardda6d2402007-12-19 14:49:29 +0000906 $sql = $this->_update($this->dbprefix.$table, $this->ar_set, $this->ar_where, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +0000907
908 $this->_reset_write();
909 return $this->query($sql);
910 }
911
912 // --------------------------------------------------------------------
913
914 /**
915 * Delete
916 *
917 * Compiles a delete string and runs the query
918 *
919 * @access public
920 * @param string the table to retrieve the results from
921 * @param mixed the where clause
922 * @return object
923 */
Derek Allarde77d77c2007-12-19 15:01:55 +0000924 function delete($table = '', $where = '', $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +0000925 {
926 if ($table == '')
927 {
928 if ( ! isset($this->ar_from[0]))
929 {
930 if ($this->db_debug)
931 {
932 return $this->display_error('db_must_set_table');
933 }
934 return FALSE;
935 }
936
937 $table = $this->ar_from[0];
938 }
939
940 if ($where != '')
941 {
942 $this->where($where);
943 }
944
Derek Allarde77d77c2007-12-19 15:01:55 +0000945 if ($limit != NULL)
946 {
947 $this->limit($limit);
948 }
949
Derek Allard09de1852007-02-14 01:35:56 +0000950 if (count($this->ar_where) == 0)
951 {
952 if ($this->db_debug)
953 {
954 return $this->display_error('db_del_must_use_where');
955 }
956 return FALSE;
957 }
958
Derek Allarde77d77c2007-12-19 15:01:55 +0000959 $sql = $this->_delete($this->dbprefix.$table, $this->ar_where, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +0000960
961 $this->_reset_write();
962 return $this->query($sql);
963 }
964
965 // --------------------------------------------------------------------
966
967 /**
968 * Use Table - DEPRECATED
969 *
970 * @deprecated use $this->db->from instead
971 */
972 function use_table($table)
973 {
974 return $this->from($table);
975 return $this;
976 }
977
978 // --------------------------------------------------------------------
979
980 /**
Derek Allard09de1852007-02-14 01:35:56 +0000981 * Tests whether the string has an SQL operator
982 *
983 * @access private
984 * @param string
985 * @return bool
986 */
987 function _has_operator($str)
988 {
989 $str = trim($str);
990 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
991 {
992 return FALSE;
993 }
994
995 return TRUE;
996 }
997
998 // --------------------------------------------------------------------
999
1000 /**
1001 * Compile the SELECT statement
1002 *
1003 * Generates a query string based on which functions were used.
1004 * Should not be called directly. The get() function calls it.
1005 *
1006 * @access private
1007 * @return string
1008 */
Derek Allard694b5b82007-12-18 15:58:03 +00001009 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001010 {
1011 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
1012
1013 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->ar_select);
1014
Derek Allard694b5b82007-12-18 15:58:03 +00001015 if ($select_override !== FALSE)
1016 {
1017 $sql = $select_override;
1018 }
1019
Derek Allard09de1852007-02-14 01:35:56 +00001020 if (count($this->ar_from) > 0)
1021 {
1022 $sql .= "\nFROM ";
1023 $sql .= implode(', ', $this->ar_from);
1024 }
1025
1026 if (count($this->ar_join) > 0)
1027 {
1028 $sql .= "\n";
1029 $sql .= implode("\n", $this->ar_join);
1030 }
1031
1032 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1033 {
1034 $sql .= "\nWHERE ";
1035 }
1036
1037 $sql .= implode("\n", $this->ar_where);
1038
1039 if (count($this->ar_like) > 0)
1040 {
1041 if (count($this->ar_where) > 0)
1042 {
1043 $sql .= " AND ";
1044 }
1045
1046 $sql .= implode("\n", $this->ar_like);
1047 }
1048
1049 if (count($this->ar_groupby) > 0)
1050 {
1051 $sql .= "\nGROUP BY ";
1052 $sql .= implode(', ', $this->ar_groupby);
1053 }
1054
1055 if (count($this->ar_having) > 0)
1056 {
1057 $sql .= "\nHAVING ";
1058 $sql .= implode("\n", $this->ar_having);
1059 }
1060
1061 if (count($this->ar_orderby) > 0)
1062 {
1063 $sql .= "\nORDER BY ";
1064 $sql .= implode(', ', $this->ar_orderby);
1065
1066 if ($this->ar_order !== FALSE)
1067 {
1068 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1069 }
1070 }
1071
1072 if (is_numeric($this->ar_limit))
1073 {
1074 $sql .= "\n";
1075 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1076 }
1077
1078 return $sql;
1079 }
1080
1081 // --------------------------------------------------------------------
1082
1083 /**
1084 * Object to Array
1085 *
1086 * Takes an object as input and converts the class variables to array key/vals
1087 *
1088 * @access public
1089 * @param object
1090 * @return array
1091 */
1092 function _object_to_array($object)
1093 {
1094 if ( ! is_object($object))
1095 {
1096 return $object;
1097 }
1098
1099 $array = array();
1100 foreach (get_object_vars($object) as $key => $val)
1101 {
1102 if ( ! is_object($val) AND ! is_array($val))
1103 {
1104 $array[$key] = $val;
1105 }
1106 }
1107
1108 return $array;
1109 }
1110
1111 // --------------------------------------------------------------------
1112
1113 /**
1114 * Resets the active record values. Called by the get() function
1115 *
1116 * @access private
1117 * @return void
1118 */
1119 function _reset_select()
1120 {
1121 $this->ar_select = array();
1122 $this->ar_distinct = FALSE;
1123 $this->ar_from = array();
1124 $this->ar_join = array();
1125 $this->ar_where = array();
1126 $this->ar_like = array();
1127 $this->ar_groupby = array();
1128 $this->ar_having = array();
1129 $this->ar_limit = FALSE;
1130 $this->ar_offset = FALSE;
1131 $this->ar_order = FALSE;
1132 $this->ar_orderby = array();
Derek Allardc6935512007-12-19 14:23:19 +00001133 $this->ar_wherein = array();
Derek Allard09de1852007-02-14 01:35:56 +00001134 }
1135
1136 // --------------------------------------------------------------------
1137
1138 /**
1139 * Resets the active record "write" values.
1140 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001141 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001142 *
1143 * @access private
1144 * @return void
1145 */
1146 function _reset_write()
1147 {
1148 $this->ar_set = array();
1149 $this->ar_from = array();
1150 $this->ar_where = array();
Derek Allardda6d2402007-12-19 14:49:29 +00001151 $this->ar_limit = FALSE;
Derek Allard09de1852007-02-14 01:35:56 +00001152 }
1153
1154}
1155
adminac94f382006-09-24 20:28:12 +00001156?>