blob: dfa0a3efbd7e6e320d5b597f43606c323957314c [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 {
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 != '')
514 $this->ar_groupby[] = $val;
515 }
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 {
716 $this->from($table);
717 }
718
719 if ( ! is_null($limit))
720 {
721 $this->limit($limit, $offset);
722 }
723
724 $sql = $this->_compile_select();
725
726 $result = $this->query($sql);
727 $this->_reset_select();
728 return $result;
729 }
730
731 // --------------------------------------------------------------------
732
733 /**
Derek Allard694b5b82007-12-18 15:58:03 +0000734 * "Count All Results" query
735 *
736 * Generates a platform-specific query string that counts all records
737 * returned by an Active Record query.
738 *
739 * @access public
740 * @param string
741 * @return string
742 */
743 function count_all_results($table = '')
744 {
745 if ($table != '')
746 {
747 $this->from($table);
748 }
749
Derek Allard6ddb5a12007-12-18 17:22:50 +0000750 $sql = $this->_compile_select($this->_count_string);
Derek Allard694b5b82007-12-18 15:58:03 +0000751
752 $query = $this->query($sql);
753 $this->_reset_select();
754
755 if ($query->num_rows() == 0)
756 {
757 return '0';
758 }
759
760 $row = $query->row();
761 return $row->numrows;
762 }
763
764 // --------------------------------------------------------------------
765
766 /**
Derek Allard218e2bc2007-12-17 21:18:14 +0000767 * Get_Where
Derek Allard09de1852007-02-14 01:35:56 +0000768 *
769 * Allows the where clause, limit and offset to be added directly
770 *
771 * @access public
772 * @param string the where clause
773 * @param string the limit clause
774 * @param string the offset clause
775 * @return object
776 */
Derek Allard218e2bc2007-12-17 21:18:14 +0000777 function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard09de1852007-02-14 01:35:56 +0000778 {
779 if ($table != '')
780 {
781 $this->from($table);
782 }
783
784 if ( ! is_null($where))
785 {
786 $this->where($where);
787 }
788
789 if ( ! is_null($limit))
790 {
791 $this->limit($limit, $offset);
792 }
793
794 $sql = $this->_compile_select();
795
796 $result = $this->query($sql);
797 $this->_reset_select();
798 return $result;
799 }
Derek Allard218e2bc2007-12-17 21:18:14 +0000800
801 // --------------------------------------------------------------------
802
803 /**
804 * getwhere() is an alias of get_where()
805 * this function is here for backwards compatibility, as
806 * getwhere() has been deprecated
807 */
808 function getwhere($table = '', $where = null, $limit = null, $offset = null)
809 {
810 return $this->get_where($table, $where, $limit, $offset);
811 }
Derek Allard09de1852007-02-14 01:35:56 +0000812
813 // --------------------------------------------------------------------
814
815 /**
816 * Insert
817 *
818 * Compiles an insert string and runs the query
819 *
820 * @access public
821 * @param string the table to retrieve the results from
822 * @param array an associative array of insert values
823 * @return object
824 */
825 function insert($table = '', $set = NULL)
826 {
827 if ( ! is_null($set))
828 {
829 $this->set($set);
830 }
831
832 if (count($this->ar_set) == 0)
833 {
834 if ($this->db_debug)
835 {
836 return $this->display_error('db_must_use_set');
837 }
838 return FALSE;
839 }
840
841 if ($table == '')
842 {
843 if ( ! isset($this->ar_from[0]))
844 {
845 if ($this->db_debug)
846 {
847 return $this->display_error('db_must_set_table');
848 }
849 return FALSE;
850 }
851
852 $table = $this->ar_from[0];
853 }
854
855 $sql = $this->_insert($this->dbprefix.$table, array_keys($this->ar_set), array_values($this->ar_set));
856
857 $this->_reset_write();
858 return $this->query($sql);
859 }
860
861 // --------------------------------------------------------------------
862
863 /**
864 * Update
865 *
866 * Compiles an update string and runs the query
867 *
868 * @access public
869 * @param string the table to retrieve the results from
870 * @param array an associative array of update values
871 * @param mixed the where clause
872 * @return object
873 */
Derek Allardda6d2402007-12-19 14:49:29 +0000874 function update($table = '', $set = NULL, $where = null, $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +0000875 {
876 if ( ! is_null($set))
877 {
878 $this->set($set);
879 }
880
881 if (count($this->ar_set) == 0)
882 {
883 if ($this->db_debug)
884 {
885 return $this->display_error('db_must_use_set');
886 }
887 return FALSE;
888 }
889
890 if ($table == '')
891 {
892 if ( ! isset($this->ar_from[0]))
893 {
894 if ($this->db_debug)
895 {
896 return $this->display_error('db_must_set_table');
897 }
898 return FALSE;
899 }
900
901 $table = $this->ar_from[0];
902 }
903
Derek Allarde77d77c2007-12-19 15:01:55 +0000904 if ($where != NULL)
Derek Allard09de1852007-02-14 01:35:56 +0000905 {
906 $this->where($where);
907 }
Derek Allardda6d2402007-12-19 14:49:29 +0000908
Derek Allarde77d77c2007-12-19 15:01:55 +0000909 if ($limit != NULL)
Derek Allardda6d2402007-12-19 14:49:29 +0000910 {
911 $this->limit($limit);
912 }
Derek Allard09de1852007-02-14 01:35:56 +0000913
Derek Allardda6d2402007-12-19 14:49:29 +0000914 $sql = $this->_update($this->dbprefix.$table, $this->ar_set, $this->ar_where, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +0000915
916 $this->_reset_write();
917 return $this->query($sql);
918 }
919
920 // --------------------------------------------------------------------
921
922 /**
923 * Delete
924 *
925 * Compiles a delete string and runs the query
926 *
927 * @access public
928 * @param string the table to retrieve the results from
929 * @param mixed the where clause
930 * @return object
931 */
Derek Allarde77d77c2007-12-19 15:01:55 +0000932 function delete($table = '', $where = '', $limit = NULL)
Derek Allard09de1852007-02-14 01:35:56 +0000933 {
934 if ($table == '')
935 {
936 if ( ! isset($this->ar_from[0]))
937 {
938 if ($this->db_debug)
939 {
940 return $this->display_error('db_must_set_table');
941 }
942 return FALSE;
943 }
944
945 $table = $this->ar_from[0];
946 }
947
948 if ($where != '')
949 {
950 $this->where($where);
951 }
952
Derek Allarde77d77c2007-12-19 15:01:55 +0000953 if ($limit != NULL)
954 {
955 $this->limit($limit);
956 }
957
Derek Allard09de1852007-02-14 01:35:56 +0000958 if (count($this->ar_where) == 0)
959 {
960 if ($this->db_debug)
961 {
962 return $this->display_error('db_del_must_use_where');
963 }
964 return FALSE;
965 }
Derek Allard15ddc9d2007-12-20 13:54:39 +0000966
Derek Allarde77d77c2007-12-19 15:01:55 +0000967 $sql = $this->_delete($this->dbprefix.$table, $this->ar_where, $this->ar_limit);
Derek Allard09de1852007-02-14 01:35:56 +0000968
969 $this->_reset_write();
970 return $this->query($sql);
971 }
Derek Allard15ddc9d2007-12-20 13:54:39 +0000972
Derek Allard09de1852007-02-14 01:35:56 +0000973 // --------------------------------------------------------------------
974
975 /**
976 * Use Table - DEPRECATED
977 *
978 * @deprecated use $this->db->from instead
979 */
980 function use_table($table)
981 {
982 return $this->from($table);
983 return $this;
984 }
985
986 // --------------------------------------------------------------------
987
988 /**
Derek Allard09de1852007-02-14 01:35:56 +0000989 * Tests whether the string has an SQL operator
990 *
991 * @access private
992 * @param string
993 * @return bool
994 */
995 function _has_operator($str)
996 {
997 $str = trim($str);
998 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
999 {
1000 return FALSE;
1001 }
1002
1003 return TRUE;
1004 }
1005
1006 // --------------------------------------------------------------------
1007
1008 /**
1009 * Compile the SELECT statement
1010 *
1011 * Generates a query string based on which functions were used.
1012 * Should not be called directly. The get() function calls it.
1013 *
1014 * @access private
1015 * @return string
1016 */
Derek Allard694b5b82007-12-18 15:58:03 +00001017 function _compile_select($select_override = FALSE)
Derek Allard09de1852007-02-14 01:35:56 +00001018 {
1019 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
1020
1021 $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->ar_select);
1022
Derek Allard694b5b82007-12-18 15:58:03 +00001023 if ($select_override !== FALSE)
1024 {
1025 $sql = $select_override;
1026 }
1027
Derek Allard09de1852007-02-14 01:35:56 +00001028 if (count($this->ar_from) > 0)
1029 {
1030 $sql .= "\nFROM ";
Derek Allard15ddc9d2007-12-20 13:54:39 +00001031 $sql .= '(' . implode(', ', $this->ar_from) . ')';
Derek Allard09de1852007-02-14 01:35:56 +00001032 }
1033
1034 if (count($this->ar_join) > 0)
1035 {
1036 $sql .= "\n";
1037 $sql .= implode("\n", $this->ar_join);
1038 }
1039
1040 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1041 {
1042 $sql .= "\nWHERE ";
1043 }
1044
1045 $sql .= implode("\n", $this->ar_where);
1046
1047 if (count($this->ar_like) > 0)
1048 {
1049 if (count($this->ar_where) > 0)
1050 {
1051 $sql .= " AND ";
1052 }
1053
1054 $sql .= implode("\n", $this->ar_like);
1055 }
1056
1057 if (count($this->ar_groupby) > 0)
1058 {
1059 $sql .= "\nGROUP BY ";
1060 $sql .= implode(', ', $this->ar_groupby);
1061 }
1062
1063 if (count($this->ar_having) > 0)
1064 {
1065 $sql .= "\nHAVING ";
1066 $sql .= implode("\n", $this->ar_having);
1067 }
1068
1069 if (count($this->ar_orderby) > 0)
1070 {
1071 $sql .= "\nORDER BY ";
1072 $sql .= implode(', ', $this->ar_orderby);
1073
1074 if ($this->ar_order !== FALSE)
1075 {
1076 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
1077 }
1078 }
1079
1080 if (is_numeric($this->ar_limit))
1081 {
1082 $sql .= "\n";
1083 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1084 }
1085
1086 return $sql;
1087 }
1088
1089 // --------------------------------------------------------------------
1090
1091 /**
1092 * Object to Array
1093 *
1094 * Takes an object as input and converts the class variables to array key/vals
1095 *
1096 * @access public
1097 * @param object
1098 * @return array
1099 */
1100 function _object_to_array($object)
1101 {
1102 if ( ! is_object($object))
1103 {
1104 return $object;
1105 }
1106
1107 $array = array();
1108 foreach (get_object_vars($object) as $key => $val)
1109 {
1110 if ( ! is_object($val) AND ! is_array($val))
1111 {
1112 $array[$key] = $val;
1113 }
1114 }
1115
1116 return $array;
1117 }
1118
1119 // --------------------------------------------------------------------
1120
1121 /**
1122 * Resets the active record values. Called by the get() function
1123 *
1124 * @access private
1125 * @return void
1126 */
1127 function _reset_select()
1128 {
1129 $this->ar_select = array();
1130 $this->ar_distinct = FALSE;
1131 $this->ar_from = array();
1132 $this->ar_join = array();
1133 $this->ar_where = array();
1134 $this->ar_like = array();
1135 $this->ar_groupby = array();
1136 $this->ar_having = array();
1137 $this->ar_limit = FALSE;
1138 $this->ar_offset = FALSE;
1139 $this->ar_order = FALSE;
1140 $this->ar_orderby = array();
Derek Allardc6935512007-12-19 14:23:19 +00001141 $this->ar_wherein = array();
Derek Allard09de1852007-02-14 01:35:56 +00001142 }
1143
1144 // --------------------------------------------------------------------
1145
1146 /**
1147 * Resets the active record "write" values.
1148 *
Derek Allarde77d77c2007-12-19 15:01:55 +00001149 * Called by the insert() update() and delete() functions
Derek Allard09de1852007-02-14 01:35:56 +00001150 *
1151 * @access private
1152 * @return void
1153 */
1154 function _reset_write()
1155 {
1156 $this->ar_set = array();
1157 $this->ar_from = array();
1158 $this->ar_where = array();
Derek Allardda6d2402007-12-19 14:49:29 +00001159 $this->ar_limit = FALSE;
Derek Allard09de1852007-02-14 01:35:56 +00001160 }
1161
1162}
1163
adminac94f382006-09-24 20:28:12 +00001164?>