blob: 46202224b2a86f0a47240aede80c02f5844ef922 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://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 ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/database/
28 */
29class CI_DB_active_record extends CI_DB_driver {
30
Kyle Farris76116012011-08-31 11:17:48 -040031 protected $return_delete_sql = FALSE;
32 protected $reset_delete_data = FALSE;
Kyle Farris0c147b32011-08-26 02:29:31 -040033
Kyle Farris76116012011-08-31 11:17:48 -040034 protected $ar_select = array();
35 protected $ar_distinct = FALSE;
36 protected $ar_from = array();
37 protected $ar_join = array();
38 protected $ar_where = array();
39 protected $ar_like = array();
40 protected $ar_groupby = array();
41 protected $ar_having = array();
42 protected $ar_keys = array();
43 protected $ar_limit = FALSE;
44 protected $ar_offset = FALSE;
45 protected $ar_order = FALSE;
46 protected $ar_orderby = array();
47 protected $ar_set = array();
48 protected $ar_wherein = array();
49 protected $ar_aliased_tables = array();
50 protected $ar_store_array = array();
Barry Mienydd671972010-10-04 16:33:58 +020051
Derek Allard2067d1a2008-11-13 22:59:24 +000052 // Active Record Caching variables
Kyle Farris76116012011-08-31 11:17:48 -040053 protected $ar_caching = FALSE;
54 protected $ar_cache_exists = array();
55 protected $ar_cache_select = array();
56 protected $ar_cache_from = array();
57 protected $ar_cache_join = array();
58 protected $ar_cache_where = array();
59 protected $ar_cache_like = array();
60 protected $ar_cache_groupby = array();
61 protected $ar_cache_having = array();
62 protected $ar_cache_orderby = array();
63 protected $ar_cache_set = array();
Derek Jones37f4b9c2011-07-01 17:56:50 -050064
Kyle Farris76116012011-08-31 11:17:48 -040065 protected $ar_no_escape = array();
66 protected $ar_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000067
68 // --------------------------------------------------------------------
69
70 /**
71 * Select
72 *
73 * Generates the SELECT portion of the query
74 *
Derek Allard2067d1a2008-11-13 22:59:24 +000075 * @param string
76 * @return object
77 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060078 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000079 {
Derek Allard2067d1a2008-11-13 22:59:24 +000080 if (is_string($select))
81 {
82 $select = explode(',', $select);
83 }
84
85 foreach ($select as $val)
86 {
87 $val = trim($val);
88
89 if ($val != '')
90 {
91 $this->ar_select[] = $val;
Greg Akere156c6e2011-04-20 16:03:04 -050092 $this->ar_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +000093
94 if ($this->ar_caching === TRUE)
95 {
96 $this->ar_cache_select[] = $val;
97 $this->ar_cache_exists[] = 'select';
Greg Aker2e1837a2011-05-06 12:17:04 -050098 $this->ar_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +000099 }
100 }
101 }
102 return $this;
103 }
104
105 // --------------------------------------------------------------------
106
107 /**
108 * Select Max
109 *
110 * Generates a SELECT MAX(field) portion of a query
111 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 * @param string the field
113 * @param string an alias
114 * @return object
115 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600116 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 {
118 return $this->_max_min_avg_sum($select, $alias, 'MAX');
119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 // --------------------------------------------------------------------
122
123 /**
124 * Select Min
125 *
126 * Generates a SELECT MIN(field) portion of a query
127 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 * @param string the field
129 * @param string an alias
130 * @return object
131 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600132 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
134 return $this->_max_min_avg_sum($select, $alias, 'MIN');
135 }
136
137 // --------------------------------------------------------------------
138
139 /**
140 * Select Average
141 *
142 * Generates a SELECT AVG(field) portion of a query
143 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 * @param string the field
145 * @param string an alias
146 * @return object
147 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600148 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
150 return $this->_max_min_avg_sum($select, $alias, 'AVG');
151 }
152
153 // --------------------------------------------------------------------
154
155 /**
156 * Select Sum
157 *
158 * Generates a SELECT SUM(field) portion of a query
159 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 * @param string the field
161 * @param string an alias
162 * @return object
163 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600164 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
166 return $this->_max_min_avg_sum($select, $alias, 'SUM');
167 }
168
169 // --------------------------------------------------------------------
170
171 /**
172 * Processing Function for the four functions above:
173 *
174 * select_max()
175 * select_min()
176 * select_avg()
Derek Jones37f4b9c2011-07-01 17:56:50 -0500177 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200178 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 * @param string the field
180 * @param string an alias
181 * @return object
182 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600183 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 {
185 if ( ! is_string($select) OR $select == '')
186 {
187 $this->display_error('db_invalid_query');
188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
193 {
194 show_error('Invalid function type: '.$type);
195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 if ($alias == '')
198 {
199 $alias = $this->_create_alias_from_table(trim($select));
200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Kyle Farris0c147b32011-08-26 02:29:31 -0400202 $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$alias;
Derek Allard2067d1a2008-11-13 22:59:24 +0000203
204 $this->ar_select[] = $sql;
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 if ($this->ar_caching === TRUE)
207 {
208 $this->ar_cache_select[] = $sql;
209 $this->ar_cache_exists[] = 'select';
210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 return $this;
213 }
214
215 // --------------------------------------------------------------------
216
217 /**
218 * Determines the alias name based on the table
219 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 * @param string
221 * @return string
222 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600223 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
225 if (strpos($item, '.') !== FALSE)
226 {
227 return end(explode('.', $item));
228 }
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 return $item;
231 }
232
233 // --------------------------------------------------------------------
234
235 /**
236 * DISTINCT
237 *
238 * Sets a flag which tells the query string compiler to add DISTINCT
239 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 * @param bool
241 * @return object
242 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600243 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
245 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
246 return $this;
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 // --------------------------------------------------------------------
250
251 /**
252 * From
253 *
254 * Generates the FROM portion of the query
255 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 * @param mixed can be a string or array
257 * @return object
258 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600259 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
261 foreach ((array)$from as $val)
262 {
263 if (strpos($val, ',') !== FALSE)
264 {
265 foreach (explode(',', $val) as $v)
266 {
267 $v = trim($v);
268 $this->_track_aliases($v);
269
270 $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 if ($this->ar_caching === TRUE)
273 {
274 $this->ar_cache_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
275 $this->ar_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200276 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 }
278
279 }
280 else
281 {
282 $val = trim($val);
283
Derek Jones37f4b9c2011-07-01 17:56:50 -0500284 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200285 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 if ($this->ar_caching === TRUE)
291 {
292 $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
293 $this->ar_cache_exists[] = 'from';
294 }
295 }
296 }
297
298 return $this;
299 }
300
301 // --------------------------------------------------------------------
302
303 /**
304 * Join
305 *
306 * Generates the JOIN portion of the query
307 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 * @param string
309 * @param string the join condition
310 * @param string the type of join
311 * @return object
312 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600313 public function join($table, $cond, $type = '')
Barry Mienydd671972010-10-04 16:33:58 +0200314 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 if ($type != '')
316 {
317 $type = strtoupper(trim($type));
318
319 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
320 {
321 $type = '';
322 }
323 else
324 {
325 $type .= ' ';
326 }
327 }
328
Derek Jones37f4b9c2011-07-01 17:56:50 -0500329 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200330 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 $this->_track_aliases($table);
332
333 // Strip apart the condition and protect the identifiers
334 if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
335 {
336 $match[1] = $this->_protect_identifiers($match[1]);
337 $match[3] = $this->_protect_identifiers($match[3]);
Barry Mienydd671972010-10-04 16:33:58 +0200338
339 $cond = $match[1].$match[2].$match[3];
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 // Assemble the JOIN statement
343 $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
344
345 $this->ar_join[] = $join;
346 if ($this->ar_caching === TRUE)
347 {
348 $this->ar_cache_join[] = $join;
349 $this->ar_cache_exists[] = 'join';
350 }
351
352 return $this;
353 }
354
355 // --------------------------------------------------------------------
356
357 /**
358 * Where
359 *
360 * Generates the WHERE portion of the query. Separates
361 * multiple calls with AND
362 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 * @param mixed
364 * @param mixed
365 * @return object
366 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600367 public function where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
369 return $this->_where($key, $value, 'AND ', $escape);
370 }
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 // --------------------------------------------------------------------
373
374 /**
375 * OR Where
376 *
377 * Generates the WHERE portion of the query. Separates
378 * multiple calls with OR
379 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 * @param mixed
381 * @param mixed
382 * @return object
383 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600384 public function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
386 return $this->_where($key, $value, 'OR ', $escape);
387 }
388
389 // --------------------------------------------------------------------
390
391 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 * Where
393 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600394 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 * @param mixed
397 * @param mixed
398 * @param string
399 * @return object
400 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600401 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
403 if ( ! is_array($key))
404 {
405 $key = array($key => $value);
406 }
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 // If the escape value was not set will will base it on the global setting
409 if ( ! is_bool($escape))
410 {
411 $escape = $this->_protect_identifiers;
412 }
413
414 foreach ($key as $k => $v)
415 {
416 $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type;
417
418 if (is_null($v) && ! $this->_has_operator($k))
419 {
420 // value appears not to have been set, assign the test to IS NULL
421 $k .= ' IS NULL';
422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 if ( ! is_null($v))
425 {
426 if ($escape === TRUE)
427 {
428 $k = $this->_protect_identifiers($k, FALSE, $escape);
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 $v = ' '.$this->escape($v);
431 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 if ( ! $this->_has_operator($k))
434 {
Greg Akere156c6e2011-04-20 16:03:04 -0500435 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
437 }
438 else
439 {
Barry Mienydd671972010-10-04 16:33:58 +0200440 $k = $this->_protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 }
442
443 $this->ar_where[] = $prefix.$k.$v;
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 if ($this->ar_caching === TRUE)
446 {
447 $this->ar_cache_where[] = $prefix.$k.$v;
448 $this->ar_cache_exists[] = 'where';
449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 }
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 return $this;
454 }
455
456 // --------------------------------------------------------------------
457
458 /**
459 * Where_in
460 *
461 * Generates a WHERE field IN ('item', 'item') SQL query joined with
462 * AND if appropriate
463 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 * @param string The field to search
465 * @param array The values searched on
466 * @return object
467 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600468 public function where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 {
470 return $this->_where_in($key, $values);
471 }
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 // --------------------------------------------------------------------
474
475 /**
476 * Where_in_or
477 *
478 * Generates a WHERE field IN ('item', 'item') SQL query joined with
479 * OR if appropriate
480 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 * @param string The field to search
482 * @param array The values searched on
483 * @return object
484 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600485 public function or_where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
487 return $this->_where_in($key, $values, FALSE, 'OR ');
488 }
489
490 // --------------------------------------------------------------------
491
492 /**
493 * Where_not_in
494 *
495 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
496 * with AND if appropriate
497 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 * @param string The field to search
499 * @param array The values searched on
500 * @return object
501 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600502 public function where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 {
504 return $this->_where_in($key, $values, TRUE);
505 }
Barry Mienydd671972010-10-04 16:33:58 +0200506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 // --------------------------------------------------------------------
508
509 /**
510 * Where_not_in_or
511 *
512 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
513 * with OR if appropriate
514 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 * @param string The field to search
516 * @param array The values searched on
517 * @return object
518 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600519 public function or_where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
521 return $this->_where_in($key, $values, TRUE, 'OR ');
522 }
523
524 // --------------------------------------------------------------------
525
526 /**
527 * Where_in
528 *
529 * Called by where_in, where_in_or, where_not_in, where_not_in_or
530 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 * @param string The field to search
532 * @param array The values searched on
533 * @param boolean If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200534 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 * @return object
536 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600537 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
539 if ($key === NULL OR $values === NULL)
540 {
541 return;
542 }
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 if ( ! is_array($values))
545 {
546 $values = array($values);
547 }
Barry Mienydd671972010-10-04 16:33:58 +0200548
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 $not = ($not) ? ' NOT' : '';
550
551 foreach ($values as $value)
552 {
553 $this->ar_wherein[] = $this->escape($value);
554 }
555
556 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Barry Mienydd671972010-10-04 16:33:58 +0200557
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
559
560 $this->ar_where[] = $where_in;
561 if ($this->ar_caching === TRUE)
562 {
563 $this->ar_cache_where[] = $where_in;
564 $this->ar_cache_exists[] = 'where';
565 }
566
567 // reset the array for multiple calls
568 $this->ar_wherein = array();
569 return $this;
570 }
Barry Mienydd671972010-10-04 16:33:58 +0200571
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 // --------------------------------------------------------------------
573
574 /**
575 * Like
576 *
577 * Generates a %LIKE% portion of the query. Separates
578 * multiple calls with AND
579 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 * @param mixed
581 * @param mixed
582 * @return object
583 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600584 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 {
586 return $this->_like($field, $match, 'AND ', $side);
587 }
588
589 // --------------------------------------------------------------------
590
591 /**
592 * Not Like
593 *
594 * Generates a NOT LIKE portion of the query. Separates
595 * multiple calls with AND
596 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 * @param mixed
598 * @param mixed
599 * @return object
600 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600601 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 {
603 return $this->_like($field, $match, 'AND ', $side, 'NOT');
604 }
Barry Mienydd671972010-10-04 16:33:58 +0200605
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 // --------------------------------------------------------------------
607
608 /**
609 * OR Like
610 *
611 * Generates a %LIKE% portion of the query. Separates
612 * multiple calls with OR
613 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 * @param mixed
615 * @param mixed
616 * @return object
617 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600618 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 {
620 return $this->_like($field, $match, 'OR ', $side);
621 }
622
623 // --------------------------------------------------------------------
624
625 /**
626 * OR Not Like
627 *
628 * Generates a NOT LIKE portion of the query. Separates
629 * multiple calls with OR
630 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 * @param mixed
632 * @param mixed
633 * @return object
634 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600635 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
637 return $this->_like($field, $match, 'OR ', $side, 'NOT');
638 }
Barry Mienydd671972010-10-04 16:33:58 +0200639
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 // --------------------------------------------------------------------
641
642 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 * Like
644 *
645 * Called by like() or orlike()
646 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 * @param mixed
648 * @param mixed
649 * @param string
650 * @return object
651 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600652 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 {
654 if ( ! is_array($field))
655 {
656 $field = array($field => $match);
657 }
Barry Mienydd671972010-10-04 16:33:58 +0200658
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 foreach ($field as $k => $v)
660 {
661 $k = $this->_protect_identifiers($k);
662
663 $prefix = (count($this->ar_like) == 0) ? '' : $type;
664
Derek Jonese4ed5832009-02-20 21:44:59 +0000665 $v = $this->escape_like_str($v);
Kyle Farris0c147b32011-08-26 02:29:31 -0400666
667 if ($side == 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 {
669 $like_statement = $prefix." $k $not LIKE '%{$v}'";
670 }
671 elseif ($side == 'after')
672 {
673 $like_statement = $prefix." $k $not LIKE '{$v}%'";
674 }
675 else
676 {
677 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
678 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600679
Derek Jonese4ed5832009-02-20 21:44:59 +0000680 // some platforms require an escape sequence definition for LIKE wildcards
681 if ($this->_like_escape_str != '')
682 {
Greg Aker0d424892010-01-26 02:14:44 +0000683 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000684 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600685
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 $this->ar_like[] = $like_statement;
687 if ($this->ar_caching === TRUE)
688 {
689 $this->ar_cache_like[] = $like_statement;
690 $this->ar_cache_exists[] = 'like';
691 }
Barry Mienydd671972010-10-04 16:33:58 +0200692
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 }
694 return $this;
695 }
Barry Mienydd671972010-10-04 16:33:58 +0200696
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 // --------------------------------------------------------------------
698
699 /**
700 * GROUP BY
701 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 * @param string
703 * @return object
704 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600705 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 {
707 if (is_string($by))
708 {
709 $by = explode(',', $by);
710 }
Barry Mienydd671972010-10-04 16:33:58 +0200711
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 foreach ($by as $val)
713 {
714 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200715
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 if ($val != '')
717 {
718 $this->ar_groupby[] = $this->_protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200719
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 if ($this->ar_caching === TRUE)
721 {
722 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
723 $this->ar_cache_exists[] = 'groupby';
724 }
725 }
726 }
727 return $this;
728 }
729
730 // --------------------------------------------------------------------
731
732 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 * Sets the HAVING value
734 *
735 * Separates multiple calls with AND
736 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 * @param string
738 * @param string
739 * @return object
740 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600741 public function having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 {
743 return $this->_having($key, $value, 'AND ', $escape);
744 }
Barry Mienydd671972010-10-04 16:33:58 +0200745
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 // --------------------------------------------------------------------
747
748 /**
749 * Sets the OR HAVING value
750 *
751 * Separates multiple calls with OR
752 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 * @param string
754 * @param string
755 * @return object
756 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600757 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 {
759 return $this->_having($key, $value, 'OR ', $escape);
760 }
Barry Mienydd671972010-10-04 16:33:58 +0200761
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 // --------------------------------------------------------------------
763
764 /**
765 * Sets the HAVING values
766 *
767 * Called by having() or or_having()
768 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 * @param string
770 * @param string
771 * @return object
772 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600773 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 {
775 if ( ! is_array($key))
776 {
777 $key = array($key => $value);
778 }
Barry Mienydd671972010-10-04 16:33:58 +0200779
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 foreach ($key as $k => $v)
781 {
782 $prefix = (count($this->ar_having) == 0) ? '' : $type;
783
784 if ($escape === TRUE)
785 {
786 $k = $this->_protect_identifiers($k);
787 }
788
789 if ( ! $this->_has_operator($k))
790 {
791 $k .= ' = ';
792 }
793
794 if ($v != '')
795 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400796 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 }
Barry Mienydd671972010-10-04 16:33:58 +0200798
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 $this->ar_having[] = $prefix.$k.$v;
800 if ($this->ar_caching === TRUE)
801 {
802 $this->ar_cache_having[] = $prefix.$k.$v;
803 $this->ar_cache_exists[] = 'having';
804 }
805 }
Barry Mienydd671972010-10-04 16:33:58 +0200806
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 return $this;
808 }
Barry Mienydd671972010-10-04 16:33:58 +0200809
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 // --------------------------------------------------------------------
811
812 /**
813 * Sets the ORDER BY value
814 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 * @param string
816 * @param string direction: asc or desc
817 * @return object
818 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600819 public function order_by($orderby, $direction = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 {
821 if (strtolower($direction) == 'random')
822 {
823 $orderby = ''; // Random results want or don't need a field name
824 $direction = $this->_random_keyword;
825 }
826 elseif (trim($direction) != '')
827 {
828 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
829 }
Barry Mienydd671972010-10-04 16:33:58 +0200830
831
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 if (strpos($orderby, ',') !== FALSE)
833 {
834 $temp = array();
835 foreach (explode(',', $orderby) as $part)
836 {
837 $part = trim($part);
838 if ( ! in_array($part, $this->ar_aliased_tables))
839 {
840 $part = $this->_protect_identifiers(trim($part));
841 }
Barry Mienydd671972010-10-04 16:33:58 +0200842
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 $temp[] = $part;
844 }
Barry Mienydd671972010-10-04 16:33:58 +0200845
846 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 }
Derek Allarde37ab382009-02-03 16:13:57 +0000848 else if ($direction != $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 {
850 $orderby = $this->_protect_identifiers($orderby);
851 }
Barry Mienydd671972010-10-04 16:33:58 +0200852
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200854
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 $this->ar_orderby[] = $orderby_statement;
856 if ($this->ar_caching === TRUE)
857 {
858 $this->ar_cache_orderby[] = $orderby_statement;
859 $this->ar_cache_exists[] = 'orderby';
860 }
861
862 return $this;
863 }
Barry Mienydd671972010-10-04 16:33:58 +0200864
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 // --------------------------------------------------------------------
866
867 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 * Sets the LIMIT value
869 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 * @param integer the limit value
871 * @param integer the offset value
872 * @return object
873 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600874 public function limit($value, $offset = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 {
Kyle Farris76116012011-08-31 11:17:48 -0400876 $this->ar_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000877
878 if ($offset != '')
879 {
Kyle Farris76116012011-08-31 11:17:48 -0400880 $this->ar_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 }
Barry Mienydd671972010-10-04 16:33:58 +0200882
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 return $this;
884 }
Barry Mienydd671972010-10-04 16:33:58 +0200885
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 // --------------------------------------------------------------------
887
888 /**
889 * Sets the OFFSET value
890 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 * @param integer the offset value
892 * @return object
893 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600894 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 {
896 $this->ar_offset = $offset;
897 return $this;
898 }
Barry Mienydd671972010-10-04 16:33:58 +0200899
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 // --------------------------------------------------------------------
901
902 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500903 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 * @param mixed
906 * @param string
907 * @param boolean
908 * @return object
909 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600910 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 {
912 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +0200913
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 if ( ! is_array($key))
915 {
916 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +0200917 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000918
919 foreach ($key as $k => $v)
920 {
921 if ($escape === FALSE)
922 {
923 $this->ar_set[$this->_protect_identifiers($k)] = $v;
924 }
925 else
926 {
Phil Sturgeond0ac1a22011-02-15 22:54:08 +0000927 $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 }
929 }
Barry Mienydd671972010-10-04 16:33:58 +0200930
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 return $this;
932 }
Kyle Farris0c147b32011-08-26 02:29:31 -0400933
Kyle Farris0c147b32011-08-26 02:29:31 -0400934 // --------------------------------------------------------------------
935
936 /**
937 * Get SELECT query string
938 *
939 * Compiles a SELECT query string and returns the sql.
940 *
941 * @access public
942 * @param string the table name to select from (optional)
943 * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone
944 * @return string
945 */
946 public function get_compiled_select($table = '', $reset = TRUE)
947 {
948 if ($table != '')
kylefarris0a3176b2011-08-26 02:37:52 -0400949 {
Kyle Farris0c147b32011-08-26 02:29:31 -0400950 $this->_track_aliases($table);
951 $this->from($table);
952 }
953
954 $select = $this->_compile_select();
955
956 if ($reset === TRUE)
957 {
958 $this->_reset_select();
959 }
960
961 return $select;
962 }
963
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 // --------------------------------------------------------------------
965
966 /**
967 * Get
968 *
969 * Compiles the select statement based on the other functions called
970 * and runs the query
971 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 * @param string the table
973 * @param string the limit clause
974 * @param string the offset clause
975 * @return object
976 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600977 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 {
979 if ($table != '')
980 {
981 $this->_track_aliases($table);
982 $this->from($table);
983 }
Barry Mienydd671972010-10-04 16:33:58 +0200984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 if ( ! is_null($limit))
986 {
987 $this->limit($limit, $offset);
988 }
Barry Mienydd671972010-10-04 16:33:58 +0200989
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 $sql = $this->_compile_select();
991
992 $result = $this->query($sql);
993 $this->_reset_select();
994 return $result;
995 }
996
997 /**
998 * "Count All Results" query
999 *
Barry Mienydd671972010-10-04 16:33:58 +02001000 * Generates a platform-specific query string that counts all records
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 * returned by an Active Record query.
1002 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 * @param string
1004 * @return string
1005 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001006 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 {
1008 if ($table != '')
1009 {
1010 $this->_track_aliases($table);
1011 $this->from($table);
1012 }
Barry Mienydd671972010-10-04 16:33:58 +02001013
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
1015
1016 $query = $this->query($sql);
1017 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001018
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 if ($query->num_rows() == 0)
1020 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001021 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 }
1023
1024 $row = $query->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001025 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 }
1027
1028 // --------------------------------------------------------------------
1029
1030 /**
1031 * Get_Where
1032 *
1033 * Allows the where clause, limit and offset to be added directly
1034 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 * @param string the where clause
1036 * @param string the limit clause
1037 * @param string the offset clause
1038 * @return object
1039 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001040 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 {
1042 if ($table != '')
1043 {
1044 $this->from($table);
1045 }
1046
1047 if ( ! is_null($where))
1048 {
1049 $this->where($where);
1050 }
Barry Mienydd671972010-10-04 16:33:58 +02001051
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 if ( ! is_null($limit))
1053 {
1054 $this->limit($limit, $offset);
1055 }
Barry Mienydd671972010-10-04 16:33:58 +02001056
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 $sql = $this->_compile_select();
1058
1059 $result = $this->query($sql);
1060 $this->_reset_select();
1061 return $result;
1062 }
1063
1064 // --------------------------------------------------------------------
1065
1066 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001067 * Insert_Batch
1068 *
1069 * Compiles batch insert strings and runs the queries
1070 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001071 * @param string the table to retrieve the results from
1072 * @param array an associative array of insert values
1073 * @return object
1074 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001075 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001076 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001077 if ( ! is_null($set))
1078 {
1079 $this->set_insert_batch($set);
1080 }
Barry Mienydd671972010-10-04 16:33:58 +02001081
Derek Jonesd10e8962010-03-02 17:10:36 -06001082 if (count($this->ar_set) == 0)
1083 {
1084 if ($this->db_debug)
1085 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001086 //No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001087 return $this->display_error('db_must_use_set');
1088 }
1089 return FALSE;
1090 }
1091
1092 if ($table == '')
1093 {
1094 if ( ! isset($this->ar_from[0]))
1095 {
1096 if ($this->db_debug)
1097 {
1098 return $this->display_error('db_must_set_table');
1099 }
1100 return FALSE;
1101 }
Barry Mienydd671972010-10-04 16:33:58 +02001102
Derek Jonesd10e8962010-03-02 17:10:36 -06001103 $table = $this->ar_from[0];
1104 }
1105
1106 // Batch this baby
1107 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1108 {
Barry Mienydd671972010-10-04 16:33:58 +02001109
Derek Jonesd10e8962010-03-02 17:10:36 -06001110 $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
1111
1112 //echo $sql;
1113
1114 $this->query($sql);
1115 }
Barry Mienydd671972010-10-04 16:33:58 +02001116
Derek Jonesd10e8962010-03-02 17:10:36 -06001117 $this->_reset_write();
1118
1119
Barry Mienydd671972010-10-04 16:33:58 +02001120 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001121 }
1122
1123 // --------------------------------------------------------------------
1124
1125 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001126 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001127 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001128 * @param mixed
1129 * @param string
1130 * @param boolean
1131 * @return object
1132 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001133 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001134 {
1135 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001136
Derek Jonesd10e8962010-03-02 17:10:36 -06001137 if ( ! is_array($key))
1138 {
1139 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001140 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001141
1142 $keys = array_keys(current($key));
1143 sort($keys);
1144
1145 foreach ($key as $row)
1146 {
Barry Mienydd671972010-10-04 16:33:58 +02001147 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1148 {
1149 // batch function above returns an error on an empty array
1150 $this->ar_set[] = array();
1151 return;
1152 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001153
Barry Mienydd671972010-10-04 16:33:58 +02001154 ksort($row); // puts $row in the same order as our keys
1155
Derek Jonesd10e8962010-03-02 17:10:36 -06001156 if ($escape === FALSE)
1157 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001158 $this->ar_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001159 }
1160 else
1161 {
1162 $clean = array();
1163
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001164 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001165 {
Barry Mienydd671972010-10-04 16:33:58 +02001166 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001167 }
1168
Derek Jones37f4b9c2011-07-01 17:56:50 -05001169 $this->ar_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001170 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001171 }
1172
1173 foreach ($keys as $k)
1174 {
1175 $this->ar_keys[] = $this->_protect_identifiers($k);
1176 }
Barry Mienydd671972010-10-04 16:33:58 +02001177
Derek Jonesd10e8962010-03-02 17:10:36 -06001178 return $this;
1179 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001180
1181 // --------------------------------------------------------------------
1182
1183 /**
1184 * Get INSERT query string
1185 *
1186 * Compiles an insert query and returns the sql
1187 *
1188 * @access public
1189 * @param string the table to insert into
1190 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1191 * @return string
1192 */
1193 public function get_compiled_insert($table = '', $reset = TRUE)
1194 {
1195 if ($this->_validate_insert($table) === FALSE)
1196 {
1197 return FALSE;
1198 }
1199
Kyle Farris76116012011-08-31 11:17:48 -04001200 $sql = $this->_insert(
1201 $this->_protect_identifiers(
1202 $this->ar_from[0], TRUE, NULL, FALSE
1203 ),
1204 array_keys($this->ar_set),
1205 array_values($this->ar_set)
1206 );
Kyle Farris0c147b32011-08-26 02:29:31 -04001207
1208 if ($reset === TRUE)
1209 {
1210 $this->_reset_write();
1211 }
1212
1213 return $sql;
1214 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001215
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 // --------------------------------------------------------------------
1217
1218 /**
1219 * Insert
1220 *
1221 * Compiles an insert string and runs the query
1222 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001223 * @access public
Phil Sturgeon9789f322011-07-15 15:14:05 -06001224 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 * @param array an associative array of insert values
1226 * @return object
1227 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001228 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001229 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 if ( ! is_null($set))
1231 {
1232 $this->set($set);
1233 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001234
1235 if ($this->_validate_insert($table) === FALSE)
1236 {
1237 return FALSE;
1238 }
1239
Kyle Farris76116012011-08-31 11:17:48 -04001240 $sql = $this->_insert(
1241 $this->_protect_identifiers(
1242 $this->ar_from[0], TRUE, NULL, FALSE
1243 ),
1244 array_keys($this->ar_set),
1245 array_values($this->ar_set)
1246 );
Barry Mienydd671972010-10-04 16:33:58 +02001247
Kyle Farris0c147b32011-08-26 02:29:31 -04001248 $this->_reset_write();
1249 return $this->query($sql);
1250 }
1251
Kyle Farris0c147b32011-08-26 02:29:31 -04001252 // --------------------------------------------------------------------
1253
1254 /**
1255 * Validate Insert
1256 *
1257 * This method is used by both insert() and get_compiled_insert() to
1258 * validate that the there data is actually being set and that table
1259 * has been chosen to be inserted into.
1260 *
1261 * @access public
1262 * @param string the table to insert data into
1263 * @return string
1264 */
1265 protected function _validate_insert($table = '')
1266 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 if (count($this->ar_set) == 0)
1268 {
1269 if ($this->db_debug)
1270 {
1271 return $this->display_error('db_must_use_set');
1272 }
1273 return FALSE;
1274 }
1275
1276 if ($table == '')
1277 {
1278 if ( ! isset($this->ar_from[0]))
1279 {
1280 if ($this->db_debug)
1281 {
1282 return $this->display_error('db_must_set_table');
1283 }
1284 return FALSE;
1285 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001287 else
1288 {
1289 $this->ar_from[0] = $table;
1290 }
1291
1292 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 }
Barry Mienydd671972010-10-04 16:33:58 +02001294
Phil Sturgeon9789f322011-07-15 15:14:05 -06001295 // --------------------------------------------------------------------
1296
1297 /**
1298 * Replace
1299 *
1300 * Compiles an replace into string and runs the query
1301 *
1302 * @param string the table to replace data into
1303 * @param array an associative array of insert values
1304 * @return object
1305 */
1306 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001307 {
1308 if ( ! is_null($set))
1309 {
1310 $this->set($set);
1311 }
Barry Mienydd671972010-10-04 16:33:58 +02001312
Derek Jonesd10e8962010-03-02 17:10:36 -06001313 if (count($this->ar_set) == 0)
1314 {
1315 if ($this->db_debug)
1316 {
1317 return $this->display_error('db_must_use_set');
1318 }
1319 return FALSE;
1320 }
1321
1322 if ($table == '')
1323 {
1324 if ( ! isset($this->ar_from[0]))
1325 {
1326 if ($this->db_debug)
1327 {
1328 return $this->display_error('db_must_set_table');
1329 }
1330 return FALSE;
1331 }
Barry Mienydd671972010-10-04 16:33:58 +02001332
Derek Jonesd10e8962010-03-02 17:10:36 -06001333 $table = $this->ar_from[0];
1334 }
1335
1336 $sql = $this->_replace($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
Barry Mienydd671972010-10-04 16:33:58 +02001337
Derek Jonesd10e8962010-03-02 17:10:36 -06001338 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001339 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001340 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001341
Kyle Farris0c147b32011-08-26 02:29:31 -04001342 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001343
Kyle Farris0c147b32011-08-26 02:29:31 -04001344 /**
1345 * Get UPDATE query string
1346 *
1347 * Compiles an update query and returns the sql
1348 *
1349 * @access public
1350 * @param string the table to update
1351 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1352 * @return string
1353 */
1354 public function get_compiled_update($table = '', $reset = TRUE)
1355 {
1356 // Combine any cached components with the current statements
1357 $this->_merge_cache();
1358
1359 if ($this->_validate_update($table) === FALSE)
1360 {
1361 return FALSE;
1362 }
1363
1364 $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
1365
1366 if ($reset === TRUE)
1367 {
1368 $this->_reset_write();
1369 }
1370
1371 return $sql;
1372 }
1373
Derek Allard2067d1a2008-11-13 22:59:24 +00001374 // --------------------------------------------------------------------
1375
1376 /**
1377 * Update
1378 *
1379 * Compiles an update string and runs the query
1380 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 * @param string the table to retrieve the results from
1382 * @param array an associative array of update values
1383 * @param mixed the where clause
1384 * @return object
1385 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001386 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001387 {
1388 // Combine any cached components with the current statements
1389 $this->_merge_cache();
1390
1391 if ( ! is_null($set))
1392 {
1393 $this->set($set);
1394 }
Barry Mienydd671972010-10-04 16:33:58 +02001395
Kyle Farris0c147b32011-08-26 02:29:31 -04001396 if ($this->_validate_update($table) === FALSE)
1397 {
1398 return FALSE;
1399 }
1400
1401 if ($where != NULL)
1402 {
1403 $this->where($where);
1404 }
1405
1406 if ($limit != NULL)
1407 {
1408 $this->limit($limit);
1409 }
1410
1411 $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
1412
1413 $this->_reset_write();
1414 return $this->query($sql);
1415 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001416
1417 // --------------------------------------------------------------------
1418
1419 /**
1420 * Validate Update
1421 *
1422 * This method is used by both update() and get_compiled_update() to
1423 * validate that data is actually being set and that a table has been
1424 * chosen to be update.
1425 *
1426 * @access public
1427 * @param string the table to update data on
1428 * @return string
1429 */
1430 protected function _validate_update($table = '')
1431 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001432 if (count($this->ar_set) == 0)
1433 {
1434 if ($this->db_debug)
1435 {
1436 return $this->display_error('db_must_use_set');
1437 }
1438 return FALSE;
1439 }
1440
1441 if ($table == '')
1442 {
1443 if ( ! isset($this->ar_from[0]))
1444 {
1445 if ($this->db_debug)
1446 {
1447 return $this->display_error('db_must_set_table');
1448 }
1449 return FALSE;
1450 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001451 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001452 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001453 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001454 $this->ar_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001455 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001456 }
Kyle Farris76116012011-08-31 11:17:48 -04001457
Derek Jonesd10e8962010-03-02 17:10:36 -06001458 // --------------------------------------------------------------------
1459
1460 /**
1461 * Update_Batch
1462 *
1463 * Compiles an update string and runs the query
1464 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001465 * @param string the table to retrieve the results from
1466 * @param array an associative array of update values
1467 * @param string the where key
1468 * @return object
1469 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001470 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001471 {
1472 // Combine any cached components with the current statements
1473 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001474
Derek Jonesd10e8962010-03-02 17:10:36 -06001475 if (is_null($index))
1476 {
1477 if ($this->db_debug)
1478 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001479 return $this->display_error('db_myst_use_index');
Derek Jonesd10e8962010-03-02 17:10:36 -06001480 }
1481
1482 return FALSE;
1483 }
1484
1485 if ( ! is_null($set))
1486 {
1487 $this->set_update_batch($set, $index);
1488 }
1489
1490 if (count($this->ar_set) == 0)
1491 {
1492 if ($this->db_debug)
1493 {
1494 return $this->display_error('db_must_use_set');
1495 }
1496
1497 return FALSE;
1498 }
1499
1500 if ($table == '')
1501 {
1502 if ( ! isset($this->ar_from[0]))
1503 {
1504 if ($this->db_debug)
1505 {
1506 return $this->display_error('db_must_set_table');
1507 }
1508 return FALSE;
1509 }
Barry Mienydd671972010-10-04 16:33:58 +02001510
Derek Jonesd10e8962010-03-02 17:10:36 -06001511 $table = $this->ar_from[0];
1512 }
Barry Mienydd671972010-10-04 16:33:58 +02001513
Derek Jonesd10e8962010-03-02 17:10:36 -06001514 // Batch this baby
1515 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1516 {
1517 $sql = $this->_update_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->ar_set, $i, 100), $this->_protect_identifiers($index), $this->ar_where);
1518
1519 $this->query($sql);
1520 }
Barry Mienydd671972010-10-04 16:33:58 +02001521
Derek Jonesd10e8962010-03-02 17:10:36 -06001522 $this->_reset_write();
1523 }
1524
1525 // --------------------------------------------------------------------
1526
1527 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001528 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001529 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001530 * @param array
1531 * @param string
1532 * @param boolean
1533 * @return object
1534 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001535 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001536 {
1537 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001538
Derek Jonesd10e8962010-03-02 17:10:36 -06001539 if ( ! is_array($key))
1540 {
1541 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001542 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001543
1544 foreach ($key as $k => $v)
1545 {
1546 $index_set = FALSE;
1547 $clean = array();
1548
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001549 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001550 {
1551 if ($k2 == $index)
1552 {
1553 $index_set = TRUE;
1554 }
1555 else
1556 {
1557 $not[] = $k.'-'.$v;
1558 }
1559
1560 if ($escape === FALSE)
1561 {
1562 $clean[$this->_protect_identifiers($k2)] = $v2;
1563 }
1564 else
1565 {
Barry Mienydd671972010-10-04 16:33:58 +02001566 $clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001567 }
1568 }
1569
1570 if ($index_set == FALSE)
1571 {
1572 return $this->display_error('db_batch_missing_index');
1573 }
1574
1575 $this->ar_set[] = $clean;
1576 }
Barry Mienydd671972010-10-04 16:33:58 +02001577
Derek Jonesd10e8962010-03-02 17:10:36 -06001578 return $this;
1579 }
1580
Derek Allard2067d1a2008-11-13 22:59:24 +00001581 // --------------------------------------------------------------------
1582
1583 /**
1584 * Empty Table
1585 *
1586 * Compiles a delete string and runs "DELETE FROM table"
1587 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001588 * @param string the table to empty
1589 * @return object
1590 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001591 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001592 {
1593 if ($table == '')
1594 {
1595 if ( ! isset($this->ar_from[0]))
1596 {
1597 if ($this->db_debug)
1598 {
1599 return $this->display_error('db_must_set_table');
1600 }
1601 return FALSE;
1602 }
1603
1604 $table = $this->ar_from[0];
1605 }
1606 else
1607 {
1608 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1609 }
1610
1611 $sql = $this->_delete($table);
1612
1613 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001614
Derek Allard2067d1a2008-11-13 22:59:24 +00001615 return $this->query($sql);
1616 }
1617
1618 // --------------------------------------------------------------------
1619
1620 /**
1621 * Truncate
1622 *
1623 * Compiles a truncate string and runs the query
1624 * If the database does not support the truncate() command
1625 * This function maps to "DELETE FROM table"
1626 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001627 * @param string the table to truncate
1628 * @return object
1629 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001630 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001631 {
1632 if ($table == '')
1633 {
1634 if ( ! isset($this->ar_from[0]))
1635 {
1636 if ($this->db_debug)
1637 {
1638 return $this->display_error('db_must_set_table');
1639 }
1640 return FALSE;
1641 }
1642
1643 $table = $this->ar_from[0];
1644 }
1645 else
1646 {
1647 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1648 }
1649
1650 $sql = $this->_truncate($table);
1651
1652 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001653
Derek Allard2067d1a2008-11-13 22:59:24 +00001654 return $this->query($sql);
1655 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001656
1657 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001658
Kyle Farris0c147b32011-08-26 02:29:31 -04001659 /**
1660 * Get DELETE query string
1661 *
1662 * Compiles a delete query string and returns the sql
1663 *
1664 * @access public
1665 * @param string the table to delete from
1666 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1667 * @return string
1668 */
1669 public function get_compiled_delete($table = '', $reset = TRUE)
1670 {
1671 $this->return_delete_sql = TRUE;
1672 $sql = $this->delete($table, '', NULL, $reset);
1673 $this->return_delete_sql = FALSE;
1674 return $sql;
1675 }
1676
Derek Allard2067d1a2008-11-13 22:59:24 +00001677 // --------------------------------------------------------------------
1678
1679 /**
1680 * Delete
1681 *
1682 * Compiles a delete string and runs the query
1683 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001684 * @param mixed the table(s) to delete from. String or array
1685 * @param mixed the where clause
1686 * @param mixed the limit clause
1687 * @param boolean
1688 * @return object
1689 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001690 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 {
1692 // Combine any cached components with the current statements
1693 $this->_merge_cache();
1694
1695 if ($table == '')
1696 {
1697 if ( ! isset($this->ar_from[0]))
1698 {
1699 if ($this->db_debug)
1700 {
1701 return $this->display_error('db_must_set_table');
1702 }
1703 return FALSE;
1704 }
1705
1706 $table = $this->ar_from[0];
1707 }
1708 elseif (is_array($table))
1709 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001710 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001711 {
1712 $this->delete($single_table, $where, $limit, FALSE);
1713 }
1714
1715 $this->_reset_write();
1716 return;
1717 }
1718 else
1719 {
1720 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1721 }
1722
1723 if ($where != '')
1724 {
1725 $this->where($where);
1726 }
1727
1728 if ($limit != NULL)
1729 {
1730 $this->limit($limit);
1731 }
1732
Derek Allard03d783b2009-05-03 19:45:45 +00001733 if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001734 {
1735 if ($this->db_debug)
1736 {
1737 return $this->display_error('db_del_must_use_where');
1738 }
1739
1740 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001741 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001742
1743 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
1744
1745 if ($reset_data)
1746 {
1747 $this->_reset_write();
1748 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001749
1750 if ($this->return_delete_sql === true)
1751 {
1752 return $sql;
1753 }
Barry Mienydd671972010-10-04 16:33:58 +02001754
Derek Allard2067d1a2008-11-13 22:59:24 +00001755 return $this->query($sql);
1756 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001757
Derek Allard2067d1a2008-11-13 22:59:24 +00001758 // --------------------------------------------------------------------
1759
1760 /**
1761 * DB Prefix
1762 *
1763 * Prepends a database prefix if one exists in configuration
1764 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001765 * @param string the table
1766 * @return string
1767 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001768 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001769 {
1770 if ($table == '')
1771 {
1772 $this->display_error('db_table_name_required');
1773 }
1774
1775 return $this->dbprefix.$table;
1776 }
1777
1778 // --------------------------------------------------------------------
1779
1780 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001781 * Set DB Prefix
1782 *
1783 * Set's the DB Prefix to something new without needing to reconnect
1784 *
1785 * @param string the prefix
1786 * @return string
1787 */
1788 public function set_dbprefix($prefix = '')
1789 {
1790 return $this->dbprefix = $prefix;
1791 }
1792
1793 // --------------------------------------------------------------------
1794
1795 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001796 * Track Aliases
1797 *
1798 * Used to track SQL statements written with aliased tables.
1799 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001800 * @param string The table to inspect
1801 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001802 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001803 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001804 {
1805 if (is_array($table))
1806 {
1807 foreach ($table as $t)
1808 {
1809 $this->_track_aliases($t);
1810 }
1811 return;
1812 }
Barry Mienydd671972010-10-04 16:33:58 +02001813
Derek Jones37f4b9c2011-07-01 17:56:50 -05001814 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001815 // the string into discreet statements
1816 if (strpos($table, ',') !== FALSE)
1817 {
1818 return $this->_track_aliases(explode(',', $table));
1819 }
Barry Mienydd671972010-10-04 16:33:58 +02001820
Derek Allard2067d1a2008-11-13 22:59:24 +00001821 // if a table alias is used we can recognize it by a space
1822 if (strpos($table, " ") !== FALSE)
1823 {
1824 // if the alias is written with the AS keyword, remove it
1825 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001826
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 // Grab the alias
1828 $table = trim(strrchr($table, " "));
Barry Mienydd671972010-10-04 16:33:58 +02001829
Derek Allard2067d1a2008-11-13 22:59:24 +00001830 // Store the alias, if it doesn't already exist
1831 if ( ! in_array($table, $this->ar_aliased_tables))
1832 {
1833 $this->ar_aliased_tables[] = $table;
1834 }
1835 }
1836 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001837
Derek Allard2067d1a2008-11-13 22:59:24 +00001838 // --------------------------------------------------------------------
1839
1840 /**
1841 * Compile the SELECT statement
1842 *
1843 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001844 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001845 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001846 * @return string
1847 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001848 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001849 {
1850 // Combine any cached components with the current statements
1851 $this->_merge_cache();
1852
1853 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001854
Derek Allard2067d1a2008-11-13 22:59:24 +00001855 // Write the "select" portion of the query
1856
1857 if ($select_override !== FALSE)
1858 {
1859 $sql = $select_override;
1860 }
1861 else
1862 {
1863 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001864
Derek Allard2067d1a2008-11-13 22:59:24 +00001865 if (count($this->ar_select) == 0)
1866 {
Barry Mienydd671972010-10-04 16:33:58 +02001867 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001868 }
1869 else
Barry Mienydd671972010-10-04 16:33:58 +02001870 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001871 // Cycle through the "select" portion of the query and prep each column name.
1872 // The reason we protect identifiers here rather then in the select() function
1873 // is because until the user calls the from() function we don't know if there are aliases
1874 foreach ($this->ar_select as $key => $val)
1875 {
Phil Sturgeon77cc0282011-08-09 16:03:49 -06001876 $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL;
1877 $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001878 }
Barry Mienydd671972010-10-04 16:33:58 +02001879
Derek Allard2067d1a2008-11-13 22:59:24 +00001880 $sql .= implode(', ', $this->ar_select);
1881 }
1882 }
1883
1884 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001885
Derek Allard2067d1a2008-11-13 22:59:24 +00001886 // Write the "FROM" portion of the query
1887
1888 if (count($this->ar_from) > 0)
1889 {
1890 $sql .= "\nFROM ";
1891
1892 $sql .= $this->_from_tables($this->ar_from);
1893 }
1894
1895 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001896
Derek Allard2067d1a2008-11-13 22:59:24 +00001897 // Write the "JOIN" portion of the query
1898
1899 if (count($this->ar_join) > 0)
1900 {
1901 $sql .= "\n";
1902
1903 $sql .= implode("\n", $this->ar_join);
1904 }
1905
1906 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001907
Derek Allard2067d1a2008-11-13 22:59:24 +00001908 // Write the "WHERE" portion of the query
1909
1910 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1911 {
Greg Akere156c6e2011-04-20 16:03:04 -05001912 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00001913 }
1914
1915 $sql .= implode("\n", $this->ar_where);
1916
1917 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001918
Derek Allard2067d1a2008-11-13 22:59:24 +00001919 // Write the "LIKE" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001920
Derek Allard2067d1a2008-11-13 22:59:24 +00001921 if (count($this->ar_like) > 0)
1922 {
1923 if (count($this->ar_where) > 0)
1924 {
1925 $sql .= "\nAND ";
1926 }
1927
1928 $sql .= implode("\n", $this->ar_like);
1929 }
1930
1931 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001932
Derek Allard2067d1a2008-11-13 22:59:24 +00001933 // Write the "GROUP BY" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001934
Derek Allard2067d1a2008-11-13 22:59:24 +00001935 if (count($this->ar_groupby) > 0)
1936 {
1937 $sql .= "\nGROUP BY ";
Barry Mienydd671972010-10-04 16:33:58 +02001938
Derek Allard2067d1a2008-11-13 22:59:24 +00001939 $sql .= implode(', ', $this->ar_groupby);
1940 }
1941
1942 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001943
Derek Allard2067d1a2008-11-13 22:59:24 +00001944 // Write the "HAVING" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001945
Derek Allard2067d1a2008-11-13 22:59:24 +00001946 if (count($this->ar_having) > 0)
1947 {
1948 $sql .= "\nHAVING ";
1949 $sql .= implode("\n", $this->ar_having);
1950 }
1951
1952 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001953
Derek Allard2067d1a2008-11-13 22:59:24 +00001954 // Write the "ORDER BY" portion of the query
1955
1956 if (count($this->ar_orderby) > 0)
1957 {
1958 $sql .= "\nORDER BY ";
1959 $sql .= implode(', ', $this->ar_orderby);
Barry Mienydd671972010-10-04 16:33:58 +02001960
Derek Allard2067d1a2008-11-13 22:59:24 +00001961 if ($this->ar_order !== FALSE)
1962 {
1963 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02001964 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001965 }
1966
1967 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001968
Derek Allard2067d1a2008-11-13 22:59:24 +00001969 // Write the "LIMIT" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001970
Derek Allard2067d1a2008-11-13 22:59:24 +00001971 if (is_numeric($this->ar_limit))
1972 {
1973 $sql .= "\n";
1974 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1975 }
1976
1977 return $sql;
1978 }
1979
1980 // --------------------------------------------------------------------
1981
1982 /**
1983 * Object to Array
1984 *
1985 * Takes an object as input and converts the class variables to array key/vals
1986 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001987 * @param object
1988 * @return array
1989 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001990 public function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001991 {
1992 if ( ! is_object($object))
1993 {
1994 return $object;
1995 }
Barry Mienydd671972010-10-04 16:33:58 +02001996
Derek Allard2067d1a2008-11-13 22:59:24 +00001997 $array = array();
1998 foreach (get_object_vars($object) as $key => $val)
1999 {
2000 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06002001 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002002 {
2003 $array[$key] = $val;
2004 }
2005 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002006
2007 return $array;
2008 }
Barry Mienydd671972010-10-04 16:33:58 +02002009
Derek Jonesd10e8962010-03-02 17:10:36 -06002010 // --------------------------------------------------------------------
2011
2012 /**
2013 * Object to Array
2014 *
2015 * Takes an object as input and converts the class variables to array key/vals
2016 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002017 * @param object
2018 * @return array
2019 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002020 public function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002021 {
2022 if ( ! is_object($object))
2023 {
2024 return $object;
2025 }
Barry Mienydd671972010-10-04 16:33:58 +02002026
Derek Jonesd10e8962010-03-02 17:10:36 -06002027 $array = array();
2028 $out = get_object_vars($object);
2029 $fields = array_keys($out);
2030
2031 foreach ($fields as $val)
2032 {
2033 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06002034 if ($val != '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002035 {
2036
2037 $i = 0;
2038 foreach ($out[$val] as $data)
2039 {
2040 $array[$i][$val] = $data;
2041 $i++;
2042 }
2043 }
2044 }
2045
Derek Allard2067d1a2008-11-13 22:59:24 +00002046 return $array;
2047 }
Barry Mienydd671972010-10-04 16:33:58 +02002048
Derek Allard2067d1a2008-11-13 22:59:24 +00002049 // --------------------------------------------------------------------
2050
2051 /**
2052 * Start Cache
2053 *
2054 * Starts AR caching
2055 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002056 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002057 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002058 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002059 {
2060 $this->ar_caching = TRUE;
2061 }
2062
2063 // --------------------------------------------------------------------
2064
2065 /**
2066 * Stop Cache
2067 *
2068 * Stops AR caching
2069 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002070 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002071 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002072 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002073 {
2074 $this->ar_caching = FALSE;
2075 }
2076
2077 // --------------------------------------------------------------------
2078
2079 /**
2080 * Flush Cache
2081 *
2082 * Empties the AR cache
2083 *
2084 * @access public
2085 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002086 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002087 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002088 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002089 $this->_reset_run(array(
2090 'ar_cache_select' => array(),
2091 'ar_cache_from' => array(),
2092 'ar_cache_join' => array(),
2093 'ar_cache_where' => array(),
2094 'ar_cache_like' => array(),
2095 'ar_cache_groupby' => array(),
2096 'ar_cache_having' => array(),
2097 'ar_cache_orderby' => array(),
2098 'ar_cache_set' => array(),
2099 'ar_cache_exists' => array(),
2100 'ar_cache_no_escape' => array()
2101 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002102 }
2103
2104 // --------------------------------------------------------------------
2105
2106 /**
2107 * Merge Cache
2108 *
Barry Mienydd671972010-10-04 16:33:58 +02002109 * When called, this function merges any cached AR arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002110 * locally called ones.
2111 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002112 * @return void
2113 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002114 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002115 {
2116 if (count($this->ar_cache_exists) == 0)
2117 {
2118 return;
2119 }
2120
2121 foreach ($this->ar_cache_exists as $val)
2122 {
2123 $ar_variable = 'ar_'.$val;
2124 $ar_cache_var = 'ar_cache_'.$val;
2125
2126 if (count($this->$ar_cache_var) == 0)
2127 {
2128 continue;
2129 }
2130
2131 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
2132 }
2133
2134 // If we are "protecting identifiers" we need to examine the "from"
2135 // portion of the query to determine if there are any aliases
2136 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
2137 {
2138 $this->_track_aliases($this->ar_from);
2139 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002140
2141 $this->ar_no_escape = $this->ar_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002142 }
Kyle Farris0c147b32011-08-26 02:29:31 -04002143
2144 // --------------------------------------------------------------------
2145
2146 /**
2147 * Reset Active Record values.
2148 *
2149 * Publicly-visible method to reset the AR values.
2150 *
2151 * @access public
2152 * @return void
2153 */
2154 public function reset_query()
2155 {
2156 $this->_reset_select();
2157 $this->_reset_write();
2158 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002159
2160 // --------------------------------------------------------------------
2161
2162 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002163 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002164 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002165 * @param array An array of fields to reset
2166 * @return void
2167 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002168 protected function _reset_run($ar_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002169 {
2170 foreach ($ar_reset_items as $item => $default_value)
2171 {
2172 if ( ! in_array($item, $this->ar_store_array))
2173 {
2174 $this->$item = $default_value;
2175 }
2176 }
2177 }
2178
2179 // --------------------------------------------------------------------
2180
2181 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002182 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002183 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002184 * @return void
2185 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002186 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002187 {
2188 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06002189 'ar_select' => array(),
2190 'ar_from' => array(),
2191 'ar_join' => array(),
2192 'ar_where' => array(),
2193 'ar_like' => array(),
2194 'ar_groupby' => array(),
2195 'ar_having' => array(),
2196 'ar_orderby' => array(),
2197 'ar_wherein' => array(),
2198 'ar_aliased_tables' => array(),
2199 'ar_no_escape' => array(),
2200 'ar_distinct' => FALSE,
2201 'ar_limit' => FALSE,
2202 'ar_offset' => FALSE,
2203 'ar_order' => FALSE,
2204 );
Barry Mienydd671972010-10-04 16:33:58 +02002205
Derek Allard2067d1a2008-11-13 22:59:24 +00002206 $this->_reset_run($ar_reset_items);
2207 }
Barry Mienydd671972010-10-04 16:33:58 +02002208
Derek Allard2067d1a2008-11-13 22:59:24 +00002209 // --------------------------------------------------------------------
2210
2211 /**
2212 * Resets the active record "write" values.
2213 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002214 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002215 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002216 * @return void
2217 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002218 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002219 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002220 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06002221 'ar_set' => array(),
2222 'ar_from' => array(),
2223 'ar_where' => array(),
2224 'ar_like' => array(),
2225 'ar_orderby' => array(),
2226 'ar_keys' => array(),
2227 'ar_limit' => FALSE,
2228 'ar_order' => FALSE
2229 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002230
2231 $this->_reset_run($ar_reset_items);
2232 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002233}
2234
2235/* End of file DB_active_rec.php */
Kyle Farris0c147b32011-08-26 02:29:31 -04002236/* Location: ./system/database/DB_active_rec.php */