blob: 8c801cd62ade51b2599bd06f9b586c875a77564f [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 Farris0c147b32011-08-26 02:29:31 -040031 private $return_delete_sql = FALSE;
32 private $reset_delete_data = FALSE;
33
Derek Allard2067d1a2008-11-13 22:59:24 +000034 var $ar_select = array();
35 var $ar_distinct = FALSE;
36 var $ar_from = array();
37 var $ar_join = array();
38 var $ar_where = array();
39 var $ar_like = array();
40 var $ar_groupby = array();
41 var $ar_having = array();
Robin Sowell43753fd2010-09-16 12:52:07 -040042 var $ar_keys = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000043 var $ar_limit = FALSE;
44 var $ar_offset = FALSE;
45 var $ar_order = FALSE;
46 var $ar_orderby = array();
Barry Mienydd671972010-10-04 16:33:58 +020047 var $ar_set = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000048 var $ar_wherein = array();
49 var $ar_aliased_tables = array();
50 var $ar_store_array = array();
Barry Mienydd671972010-10-04 16:33:58 +020051
Derek Allard2067d1a2008-11-13 22:59:24 +000052 // Active Record Caching variables
Barry Mienydd671972010-10-04 16:33:58 +020053 var $ar_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000054 var $ar_cache_exists = array();
55 var $ar_cache_select = array();
56 var $ar_cache_from = array();
57 var $ar_cache_join = array();
58 var $ar_cache_where = array();
59 var $ar_cache_like = array();
60 var $ar_cache_groupby = array();
61 var $ar_cache_having = array();
62 var $ar_cache_orderby = array();
Barry Mienydd671972010-10-04 16:33:58 +020063 var $ar_cache_set = array();
Derek Jones37f4b9c2011-07-01 17:56:50 -050064
Greg Akere156c6e2011-04-20 16:03:04 -050065 var $ar_no_escape = array();
Greg Aker2e1837a2011-05-06 12:17:04 -050066 var $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 Farris0c147b32011-08-26 02:29:31 -0400876 $this->ar_limit = $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000877
878 if ($offset != '')
879 {
Kyle Farris0c147b32011-08-26 02:29:31 -0400880 $this->ar_offset = $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
934
935 // --------------------------------------------------------------------
936
937 /**
938 * Get SELECT query string
939 *
940 * Compiles a SELECT query string and returns the sql.
941 *
942 * @access public
943 * @param string the table name to select from (optional)
944 * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone
945 * @return string
946 */
947 public function get_compiled_select($table = '', $reset = TRUE)
948 {
949 if ($table != '')
950 {
951 $this->_track_aliases($table);
952 $this->from($table);
953 }
954
955 $select = $this->_compile_select();
956
957 if ($reset === TRUE)
958 {
959 $this->_reset_select();
960 }
961
962 return $select;
963 }
964
Barry Mienydd671972010-10-04 16:33:58 +0200965
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 // --------------------------------------------------------------------
967
968 /**
969 * Get
970 *
971 * Compiles the select statement based on the other functions called
972 * and runs the query
973 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 * @param string the table
975 * @param string the limit clause
976 * @param string the offset clause
977 * @return object
978 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600979 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 {
981 if ($table != '')
982 {
983 $this->_track_aliases($table);
984 $this->from($table);
985 }
Barry Mienydd671972010-10-04 16:33:58 +0200986
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 if ( ! is_null($limit))
988 {
989 $this->limit($limit, $offset);
990 }
Barry Mienydd671972010-10-04 16:33:58 +0200991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 $sql = $this->_compile_select();
993
994 $result = $this->query($sql);
995 $this->_reset_select();
996 return $result;
997 }
998
999 /**
1000 * "Count All Results" query
1001 *
Barry Mienydd671972010-10-04 16:33:58 +02001002 * Generates a platform-specific query string that counts all records
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 * returned by an Active Record query.
1004 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 * @param string
1006 * @return string
1007 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001008 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 {
1010 if ($table != '')
1011 {
1012 $this->_track_aliases($table);
1013 $this->from($table);
1014 }
Barry Mienydd671972010-10-04 16:33:58 +02001015
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
1017
1018 $query = $this->query($sql);
1019 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001020
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 if ($query->num_rows() == 0)
1022 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001023 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 }
1025
1026 $row = $query->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001027 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 }
1029
1030 // --------------------------------------------------------------------
1031
1032 /**
1033 * Get_Where
1034 *
1035 * Allows the where clause, limit and offset to be added directly
1036 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 * @param string the where clause
1038 * @param string the limit clause
1039 * @param string the offset clause
1040 * @return object
1041 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001042 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 {
1044 if ($table != '')
1045 {
1046 $this->from($table);
1047 }
1048
1049 if ( ! is_null($where))
1050 {
1051 $this->where($where);
1052 }
Barry Mienydd671972010-10-04 16:33:58 +02001053
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 if ( ! is_null($limit))
1055 {
1056 $this->limit($limit, $offset);
1057 }
Barry Mienydd671972010-10-04 16:33:58 +02001058
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 $sql = $this->_compile_select();
1060
1061 $result = $this->query($sql);
1062 $this->_reset_select();
1063 return $result;
1064 }
1065
1066 // --------------------------------------------------------------------
1067
1068 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001069 * Insert_Batch
1070 *
1071 * Compiles batch insert strings and runs the queries
1072 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001073 * @param string the table to retrieve the results from
1074 * @param array an associative array of insert values
1075 * @return object
1076 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001077 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001078 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001079 if ( ! is_null($set))
1080 {
1081 $this->set_insert_batch($set);
1082 }
Barry Mienydd671972010-10-04 16:33:58 +02001083
Derek Jonesd10e8962010-03-02 17:10:36 -06001084 if (count($this->ar_set) == 0)
1085 {
1086 if ($this->db_debug)
1087 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001088 //No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001089 return $this->display_error('db_must_use_set');
1090 }
1091 return FALSE;
1092 }
1093
1094 if ($table == '')
1095 {
1096 if ( ! isset($this->ar_from[0]))
1097 {
1098 if ($this->db_debug)
1099 {
1100 return $this->display_error('db_must_set_table');
1101 }
1102 return FALSE;
1103 }
Barry Mienydd671972010-10-04 16:33:58 +02001104
Derek Jonesd10e8962010-03-02 17:10:36 -06001105 $table = $this->ar_from[0];
1106 }
1107
1108 // Batch this baby
1109 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1110 {
Barry Mienydd671972010-10-04 16:33:58 +02001111
Derek Jonesd10e8962010-03-02 17:10:36 -06001112 $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
1113
1114 //echo $sql;
1115
1116 $this->query($sql);
1117 }
Barry Mienydd671972010-10-04 16:33:58 +02001118
Derek Jonesd10e8962010-03-02 17:10:36 -06001119 $this->_reset_write();
1120
1121
Barry Mienydd671972010-10-04 16:33:58 +02001122 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001123 }
1124
1125 // --------------------------------------------------------------------
1126
1127 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001128 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001129 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001130 * @param mixed
1131 * @param string
1132 * @param boolean
1133 * @return object
1134 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001135 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001136 {
1137 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001138
Derek Jonesd10e8962010-03-02 17:10:36 -06001139 if ( ! is_array($key))
1140 {
1141 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001142 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001143
1144 $keys = array_keys(current($key));
1145 sort($keys);
1146
1147 foreach ($key as $row)
1148 {
Barry Mienydd671972010-10-04 16:33:58 +02001149 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1150 {
1151 // batch function above returns an error on an empty array
1152 $this->ar_set[] = array();
1153 return;
1154 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001155
Barry Mienydd671972010-10-04 16:33:58 +02001156 ksort($row); // puts $row in the same order as our keys
1157
Derek Jonesd10e8962010-03-02 17:10:36 -06001158 if ($escape === FALSE)
1159 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001160 $this->ar_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001161 }
1162 else
1163 {
1164 $clean = array();
1165
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001166 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001167 {
Barry Mienydd671972010-10-04 16:33:58 +02001168 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001169 }
1170
Derek Jones37f4b9c2011-07-01 17:56:50 -05001171 $this->ar_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001172 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001173 }
1174
1175 foreach ($keys as $k)
1176 {
1177 $this->ar_keys[] = $this->_protect_identifiers($k);
1178 }
Barry Mienydd671972010-10-04 16:33:58 +02001179
Derek Jonesd10e8962010-03-02 17:10:36 -06001180 return $this;
1181 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001182
1183 // --------------------------------------------------------------------
1184
1185 /**
1186 * Get INSERT query string
1187 *
1188 * Compiles an insert query and returns the sql
1189 *
1190 * @access public
1191 * @param string the table to insert into
1192 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1193 * @return string
1194 */
1195 public function get_compiled_insert($table = '', $reset = TRUE)
1196 {
1197 if ($this->_validate_insert($table) === FALSE)
1198 {
1199 return FALSE;
1200 }
1201
1202 $sql = $this->_insert($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
1203
1204 if ($reset === TRUE)
1205 {
1206 $this->_reset_write();
1207 }
1208
1209 return $sql;
1210 }
1211
Derek Jonesd10e8962010-03-02 17:10:36 -06001212
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 // --------------------------------------------------------------------
1214
1215 /**
1216 * Insert
1217 *
1218 * Compiles an insert string and runs the query
1219 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001220 * @access public
Phil Sturgeon9789f322011-07-15 15:14:05 -06001221 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 * @param array an associative array of insert values
1223 * @return object
1224 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001225 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001226 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 if ( ! is_null($set))
1228 {
1229 $this->set($set);
1230 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001231
1232 if ($this->_validate_insert($table) === FALSE)
1233 {
1234 return FALSE;
1235 }
1236
1237 $sql = $this->_insert($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
Barry Mienydd671972010-10-04 16:33:58 +02001238
Kyle Farris0c147b32011-08-26 02:29:31 -04001239 $this->_reset_write();
1240 return $this->query($sql);
1241 }
1242
1243
1244 // --------------------------------------------------------------------
1245
1246 /**
1247 * Validate Insert
1248 *
1249 * This method is used by both insert() and get_compiled_insert() to
1250 * validate that the there data is actually being set and that table
1251 * has been chosen to be inserted into.
1252 *
1253 * @access public
1254 * @param string the table to insert data into
1255 * @return string
1256 */
1257 protected function _validate_insert($table = '')
1258 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 if (count($this->ar_set) == 0)
1260 {
1261 if ($this->db_debug)
1262 {
1263 return $this->display_error('db_must_use_set');
1264 }
1265 return FALSE;
1266 }
1267
1268 if ($table == '')
1269 {
1270 if ( ! isset($this->ar_from[0]))
1271 {
1272 if ($this->db_debug)
1273 {
1274 return $this->display_error('db_must_set_table');
1275 }
1276 return FALSE;
1277 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001279 else
1280 {
1281 $this->ar_from[0] = $table;
1282 }
1283
1284 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 }
Barry Mienydd671972010-10-04 16:33:58 +02001286
Phil Sturgeon9789f322011-07-15 15:14:05 -06001287 // --------------------------------------------------------------------
1288
1289 /**
1290 * Replace
1291 *
1292 * Compiles an replace into string and runs the query
1293 *
1294 * @param string the table to replace data into
1295 * @param array an associative array of insert values
1296 * @return object
1297 */
1298 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001299 {
1300 if ( ! is_null($set))
1301 {
1302 $this->set($set);
1303 }
Barry Mienydd671972010-10-04 16:33:58 +02001304
Derek Jonesd10e8962010-03-02 17:10:36 -06001305 if (count($this->ar_set) == 0)
1306 {
1307 if ($this->db_debug)
1308 {
1309 return $this->display_error('db_must_use_set');
1310 }
1311 return FALSE;
1312 }
1313
1314 if ($table == '')
1315 {
1316 if ( ! isset($this->ar_from[0]))
1317 {
1318 if ($this->db_debug)
1319 {
1320 return $this->display_error('db_must_set_table');
1321 }
1322 return FALSE;
1323 }
Barry Mienydd671972010-10-04 16:33:58 +02001324
Derek Jonesd10e8962010-03-02 17:10:36 -06001325 $table = $this->ar_from[0];
1326 }
1327
1328 $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 +02001329
Derek Jonesd10e8962010-03-02 17:10:36 -06001330 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001331 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001332 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001333
1334
1335 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001336
Kyle Farris0c147b32011-08-26 02:29:31 -04001337 /**
1338 * Get UPDATE query string
1339 *
1340 * Compiles an update query and returns the sql
1341 *
1342 * @access public
1343 * @param string the table to update
1344 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1345 * @return string
1346 */
1347 public function get_compiled_update($table = '', $reset = TRUE)
1348 {
1349 // Combine any cached components with the current statements
1350 $this->_merge_cache();
1351
1352 if ($this->_validate_update($table) === FALSE)
1353 {
1354 return FALSE;
1355 }
1356
1357 $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);
1358
1359 if ($reset === TRUE)
1360 {
1361 $this->_reset_write();
1362 }
1363
1364 return $sql;
1365 }
1366
1367
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 // --------------------------------------------------------------------
1369
1370 /**
1371 * Update
1372 *
1373 * Compiles an update string and runs the query
1374 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 * @param string the table to retrieve the results from
1376 * @param array an associative array of update values
1377 * @param mixed the where clause
1378 * @return object
1379 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001380 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 {
1382 // Combine any cached components with the current statements
1383 $this->_merge_cache();
1384
1385 if ( ! is_null($set))
1386 {
1387 $this->set($set);
1388 }
Barry Mienydd671972010-10-04 16:33:58 +02001389
Kyle Farris0c147b32011-08-26 02:29:31 -04001390 if ($this->_validate_update($table) === FALSE)
1391 {
1392 return FALSE;
1393 }
1394
1395 if ($where != NULL)
1396 {
1397 $this->where($where);
1398 }
1399
1400 if ($limit != NULL)
1401 {
1402 $this->limit($limit);
1403 }
1404
1405 $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);
1406
1407 $this->_reset_write();
1408 return $this->query($sql);
1409 }
1410
1411
1412 // --------------------------------------------------------------------
1413
1414 /**
1415 * Validate Update
1416 *
1417 * This method is used by both update() and get_compiled_update() to
1418 * validate that data is actually being set and that a table has been
1419 * chosen to be update.
1420 *
1421 * @access public
1422 * @param string the table to update data on
1423 * @return string
1424 */
1425 protected function _validate_update($table = '')
1426 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 if (count($this->ar_set) == 0)
1428 {
1429 if ($this->db_debug)
1430 {
1431 return $this->display_error('db_must_use_set');
1432 }
1433 return FALSE;
1434 }
1435
1436 if ($table == '')
1437 {
1438 if ( ! isset($this->ar_from[0]))
1439 {
1440 if ($this->db_debug)
1441 {
1442 return $this->display_error('db_must_set_table');
1443 }
1444 return FALSE;
1445 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001446 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001447 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001449 $this->ar_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001450 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001451 }
1452
Derek Jonesd10e8962010-03-02 17:10:36 -06001453
1454 // --------------------------------------------------------------------
1455
1456 /**
1457 * Update_Batch
1458 *
1459 * Compiles an update string and runs the query
1460 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001461 * @param string the table to retrieve the results from
1462 * @param array an associative array of update values
1463 * @param string the where key
1464 * @return object
1465 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001466 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001467 {
1468 // Combine any cached components with the current statements
1469 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001470
Derek Jonesd10e8962010-03-02 17:10:36 -06001471 if (is_null($index))
1472 {
1473 if ($this->db_debug)
1474 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001475 return $this->display_error('db_myst_use_index');
Derek Jonesd10e8962010-03-02 17:10:36 -06001476 }
1477
1478 return FALSE;
1479 }
1480
1481 if ( ! is_null($set))
1482 {
1483 $this->set_update_batch($set, $index);
1484 }
1485
1486 if (count($this->ar_set) == 0)
1487 {
1488 if ($this->db_debug)
1489 {
1490 return $this->display_error('db_must_use_set');
1491 }
1492
1493 return FALSE;
1494 }
1495
1496 if ($table == '')
1497 {
1498 if ( ! isset($this->ar_from[0]))
1499 {
1500 if ($this->db_debug)
1501 {
1502 return $this->display_error('db_must_set_table');
1503 }
1504 return FALSE;
1505 }
Barry Mienydd671972010-10-04 16:33:58 +02001506
Derek Jonesd10e8962010-03-02 17:10:36 -06001507 $table = $this->ar_from[0];
1508 }
Barry Mienydd671972010-10-04 16:33:58 +02001509
Derek Jonesd10e8962010-03-02 17:10:36 -06001510 // Batch this baby
1511 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1512 {
1513 $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);
1514
1515 $this->query($sql);
1516 }
Barry Mienydd671972010-10-04 16:33:58 +02001517
Derek Jonesd10e8962010-03-02 17:10:36 -06001518 $this->_reset_write();
1519 }
1520
1521 // --------------------------------------------------------------------
1522
1523 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001524 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001525 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001526 * @param array
1527 * @param string
1528 * @param boolean
1529 * @return object
1530 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001531 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001532 {
1533 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001534
Derek Jonesd10e8962010-03-02 17:10:36 -06001535 if ( ! is_array($key))
1536 {
1537 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001538 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001539
1540 foreach ($key as $k => $v)
1541 {
1542 $index_set = FALSE;
1543 $clean = array();
1544
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001545 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001546 {
1547 if ($k2 == $index)
1548 {
1549 $index_set = TRUE;
1550 }
1551 else
1552 {
1553 $not[] = $k.'-'.$v;
1554 }
1555
1556 if ($escape === FALSE)
1557 {
1558 $clean[$this->_protect_identifiers($k2)] = $v2;
1559 }
1560 else
1561 {
Barry Mienydd671972010-10-04 16:33:58 +02001562 $clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001563 }
1564 }
1565
1566 if ($index_set == FALSE)
1567 {
1568 return $this->display_error('db_batch_missing_index');
1569 }
1570
1571 $this->ar_set[] = $clean;
1572 }
Barry Mienydd671972010-10-04 16:33:58 +02001573
Derek Jonesd10e8962010-03-02 17:10:36 -06001574 return $this;
1575 }
1576
Derek Allard2067d1a2008-11-13 22:59:24 +00001577 // --------------------------------------------------------------------
1578
1579 /**
1580 * Empty Table
1581 *
1582 * Compiles a delete string and runs "DELETE FROM table"
1583 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001584 * @param string the table to empty
1585 * @return object
1586 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001587 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001588 {
1589 if ($table == '')
1590 {
1591 if ( ! isset($this->ar_from[0]))
1592 {
1593 if ($this->db_debug)
1594 {
1595 return $this->display_error('db_must_set_table');
1596 }
1597 return FALSE;
1598 }
1599
1600 $table = $this->ar_from[0];
1601 }
1602 else
1603 {
1604 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1605 }
1606
1607 $sql = $this->_delete($table);
1608
1609 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001610
Derek Allard2067d1a2008-11-13 22:59:24 +00001611 return $this->query($sql);
1612 }
1613
1614 // --------------------------------------------------------------------
1615
1616 /**
1617 * Truncate
1618 *
1619 * Compiles a truncate string and runs the query
1620 * If the database does not support the truncate() command
1621 * This function maps to "DELETE FROM table"
1622 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001623 * @param string the table to truncate
1624 * @return object
1625 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001626 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001627 {
1628 if ($table == '')
1629 {
1630 if ( ! isset($this->ar_from[0]))
1631 {
1632 if ($this->db_debug)
1633 {
1634 return $this->display_error('db_must_set_table');
1635 }
1636 return FALSE;
1637 }
1638
1639 $table = $this->ar_from[0];
1640 }
1641 else
1642 {
1643 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1644 }
1645
1646 $sql = $this->_truncate($table);
1647
1648 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001649
Derek Allard2067d1a2008-11-13 22:59:24 +00001650 return $this->query($sql);
1651 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001652
1653 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001654
Kyle Farris0c147b32011-08-26 02:29:31 -04001655 /**
1656 * Get DELETE query string
1657 *
1658 * Compiles a delete query string and returns the sql
1659 *
1660 * @access public
1661 * @param string the table to delete from
1662 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1663 * @return string
1664 */
1665 public function get_compiled_delete($table = '', $reset = TRUE)
1666 {
1667 $this->return_delete_sql = TRUE;
1668 $sql = $this->delete($table, '', NULL, $reset);
1669 $this->return_delete_sql = FALSE;
1670 return $sql;
1671 }
1672
Derek Allard2067d1a2008-11-13 22:59:24 +00001673 // --------------------------------------------------------------------
1674
1675 /**
1676 * Delete
1677 *
1678 * Compiles a delete string and runs the query
1679 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001680 * @param mixed the table(s) to delete from. String or array
1681 * @param mixed the where clause
1682 * @param mixed the limit clause
1683 * @param boolean
1684 * @return object
1685 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001686 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001687 {
1688 // Combine any cached components with the current statements
1689 $this->_merge_cache();
1690
1691 if ($table == '')
1692 {
1693 if ( ! isset($this->ar_from[0]))
1694 {
1695 if ($this->db_debug)
1696 {
1697 return $this->display_error('db_must_set_table');
1698 }
1699 return FALSE;
1700 }
1701
1702 $table = $this->ar_from[0];
1703 }
1704 elseif (is_array($table))
1705 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001706 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001707 {
1708 $this->delete($single_table, $where, $limit, FALSE);
1709 }
1710
1711 $this->_reset_write();
1712 return;
1713 }
1714 else
1715 {
1716 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1717 }
1718
1719 if ($where != '')
1720 {
1721 $this->where($where);
1722 }
1723
1724 if ($limit != NULL)
1725 {
1726 $this->limit($limit);
1727 }
1728
Derek Allard03d783b2009-05-03 19:45:45 +00001729 if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001730 {
1731 if ($this->db_debug)
1732 {
1733 return $this->display_error('db_del_must_use_where');
1734 }
1735
1736 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001737 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001738
1739 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
1740
1741 if ($reset_data)
1742 {
1743 $this->_reset_write();
1744 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001745
1746 if ($this->return_delete_sql === true)
1747 {
1748 return $sql;
1749 }
Barry Mienydd671972010-10-04 16:33:58 +02001750
Derek Allard2067d1a2008-11-13 22:59:24 +00001751 return $this->query($sql);
1752 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001753
Derek Allard2067d1a2008-11-13 22:59:24 +00001754
1755 // --------------------------------------------------------------------
1756
1757 /**
1758 * DB Prefix
1759 *
1760 * Prepends a database prefix if one exists in configuration
1761 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001762 * @param string the table
1763 * @return string
1764 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001765 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001766 {
1767 if ($table == '')
1768 {
1769 $this->display_error('db_table_name_required');
1770 }
1771
1772 return $this->dbprefix.$table;
1773 }
1774
1775 // --------------------------------------------------------------------
1776
1777 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001778 * Set DB Prefix
1779 *
1780 * Set's the DB Prefix to something new without needing to reconnect
1781 *
1782 * @param string the prefix
1783 * @return string
1784 */
1785 public function set_dbprefix($prefix = '')
1786 {
1787 return $this->dbprefix = $prefix;
1788 }
1789
1790 // --------------------------------------------------------------------
1791
1792 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 * Track Aliases
1794 *
1795 * Used to track SQL statements written with aliased tables.
1796 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001797 * @param string The table to inspect
1798 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001799 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001800 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001801 {
1802 if (is_array($table))
1803 {
1804 foreach ($table as $t)
1805 {
1806 $this->_track_aliases($t);
1807 }
1808 return;
1809 }
Barry Mienydd671972010-10-04 16:33:58 +02001810
Derek Jones37f4b9c2011-07-01 17:56:50 -05001811 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001812 // the string into discreet statements
1813 if (strpos($table, ',') !== FALSE)
1814 {
1815 return $this->_track_aliases(explode(',', $table));
1816 }
Barry Mienydd671972010-10-04 16:33:58 +02001817
Derek Allard2067d1a2008-11-13 22:59:24 +00001818 // if a table alias is used we can recognize it by a space
1819 if (strpos($table, " ") !== FALSE)
1820 {
1821 // if the alias is written with the AS keyword, remove it
1822 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001823
Derek Allard2067d1a2008-11-13 22:59:24 +00001824 // Grab the alias
1825 $table = trim(strrchr($table, " "));
Barry Mienydd671972010-10-04 16:33:58 +02001826
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 // Store the alias, if it doesn't already exist
1828 if ( ! in_array($table, $this->ar_aliased_tables))
1829 {
1830 $this->ar_aliased_tables[] = $table;
1831 }
1832 }
1833 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001834
Derek Allard2067d1a2008-11-13 22:59:24 +00001835
1836 // --------------------------------------------------------------------
1837
1838 /**
1839 * Compile the SELECT statement
1840 *
1841 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001842 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001843 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001844 * @return string
1845 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001846 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001847 {
1848 // Combine any cached components with the current statements
1849 $this->_merge_cache();
1850
1851 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001852
Derek Allard2067d1a2008-11-13 22:59:24 +00001853 // Write the "select" portion of the query
1854
1855 if ($select_override !== FALSE)
1856 {
1857 $sql = $select_override;
1858 }
1859 else
1860 {
1861 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001862
Derek Allard2067d1a2008-11-13 22:59:24 +00001863 if (count($this->ar_select) == 0)
1864 {
Barry Mienydd671972010-10-04 16:33:58 +02001865 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001866 }
1867 else
Barry Mienydd671972010-10-04 16:33:58 +02001868 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001869 // Cycle through the "select" portion of the query and prep each column name.
1870 // The reason we protect identifiers here rather then in the select() function
1871 // is because until the user calls the from() function we don't know if there are aliases
1872 foreach ($this->ar_select as $key => $val)
1873 {
Phil Sturgeon77cc0282011-08-09 16:03:49 -06001874 $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL;
1875 $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001876 }
Barry Mienydd671972010-10-04 16:33:58 +02001877
Derek Allard2067d1a2008-11-13 22:59:24 +00001878 $sql .= implode(', ', $this->ar_select);
1879 }
1880 }
1881
1882 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001883
Derek Allard2067d1a2008-11-13 22:59:24 +00001884 // Write the "FROM" portion of the query
1885
1886 if (count($this->ar_from) > 0)
1887 {
1888 $sql .= "\nFROM ";
1889
1890 $sql .= $this->_from_tables($this->ar_from);
1891 }
1892
1893 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001894
Derek Allard2067d1a2008-11-13 22:59:24 +00001895 // Write the "JOIN" portion of the query
1896
1897 if (count($this->ar_join) > 0)
1898 {
1899 $sql .= "\n";
1900
1901 $sql .= implode("\n", $this->ar_join);
1902 }
1903
1904 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001905
Derek Allard2067d1a2008-11-13 22:59:24 +00001906 // Write the "WHERE" portion of the query
1907
1908 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1909 {
Greg Akere156c6e2011-04-20 16:03:04 -05001910 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00001911 }
1912
1913 $sql .= implode("\n", $this->ar_where);
1914
1915 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001916
Derek Allard2067d1a2008-11-13 22:59:24 +00001917 // Write the "LIKE" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001918
Derek Allard2067d1a2008-11-13 22:59:24 +00001919 if (count($this->ar_like) > 0)
1920 {
1921 if (count($this->ar_where) > 0)
1922 {
1923 $sql .= "\nAND ";
1924 }
1925
1926 $sql .= implode("\n", $this->ar_like);
1927 }
1928
1929 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001930
Derek Allard2067d1a2008-11-13 22:59:24 +00001931 // Write the "GROUP BY" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001932
Derek Allard2067d1a2008-11-13 22:59:24 +00001933 if (count($this->ar_groupby) > 0)
1934 {
1935 $sql .= "\nGROUP BY ";
Barry Mienydd671972010-10-04 16:33:58 +02001936
Derek Allard2067d1a2008-11-13 22:59:24 +00001937 $sql .= implode(', ', $this->ar_groupby);
1938 }
1939
1940 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001941
Derek Allard2067d1a2008-11-13 22:59:24 +00001942 // Write the "HAVING" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001943
Derek Allard2067d1a2008-11-13 22:59:24 +00001944 if (count($this->ar_having) > 0)
1945 {
1946 $sql .= "\nHAVING ";
1947 $sql .= implode("\n", $this->ar_having);
1948 }
1949
1950 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001951
Derek Allard2067d1a2008-11-13 22:59:24 +00001952 // Write the "ORDER BY" portion of the query
1953
1954 if (count($this->ar_orderby) > 0)
1955 {
1956 $sql .= "\nORDER BY ";
1957 $sql .= implode(', ', $this->ar_orderby);
Barry Mienydd671972010-10-04 16:33:58 +02001958
Derek Allard2067d1a2008-11-13 22:59:24 +00001959 if ($this->ar_order !== FALSE)
1960 {
1961 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02001962 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001963 }
1964
1965 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001966
Derek Allard2067d1a2008-11-13 22:59:24 +00001967 // Write the "LIMIT" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001968
Derek Allard2067d1a2008-11-13 22:59:24 +00001969 if (is_numeric($this->ar_limit))
1970 {
1971 $sql .= "\n";
1972 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1973 }
1974
1975 return $sql;
1976 }
1977
1978 // --------------------------------------------------------------------
1979
1980 /**
1981 * Object to Array
1982 *
1983 * Takes an object as input and converts the class variables to array key/vals
1984 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001985 * @param object
1986 * @return array
1987 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001988 public function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001989 {
1990 if ( ! is_object($object))
1991 {
1992 return $object;
1993 }
Barry Mienydd671972010-10-04 16:33:58 +02001994
Derek Allard2067d1a2008-11-13 22:59:24 +00001995 $array = array();
1996 foreach (get_object_vars($object) as $key => $val)
1997 {
1998 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06001999 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002000 {
2001 $array[$key] = $val;
2002 }
2003 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002004
2005 return $array;
2006 }
Barry Mienydd671972010-10-04 16:33:58 +02002007
Derek Jonesd10e8962010-03-02 17:10:36 -06002008 // --------------------------------------------------------------------
2009
2010 /**
2011 * Object to Array
2012 *
2013 * Takes an object as input and converts the class variables to array key/vals
2014 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002015 * @param object
2016 * @return array
2017 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002018 public function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002019 {
2020 if ( ! is_object($object))
2021 {
2022 return $object;
2023 }
Barry Mienydd671972010-10-04 16:33:58 +02002024
Derek Jonesd10e8962010-03-02 17:10:36 -06002025 $array = array();
2026 $out = get_object_vars($object);
2027 $fields = array_keys($out);
2028
2029 foreach ($fields as $val)
2030 {
2031 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06002032 if ($val != '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002033 {
2034
2035 $i = 0;
2036 foreach ($out[$val] as $data)
2037 {
2038 $array[$i][$val] = $data;
2039 $i++;
2040 }
2041 }
2042 }
2043
Derek Allard2067d1a2008-11-13 22:59:24 +00002044 return $array;
2045 }
Barry Mienydd671972010-10-04 16:33:58 +02002046
Derek Allard2067d1a2008-11-13 22:59:24 +00002047 // --------------------------------------------------------------------
2048
2049 /**
2050 * Start Cache
2051 *
2052 * Starts AR caching
2053 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002054 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002055 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002056 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002057 {
2058 $this->ar_caching = TRUE;
2059 }
2060
2061 // --------------------------------------------------------------------
2062
2063 /**
2064 * Stop Cache
2065 *
2066 * Stops AR caching
2067 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002068 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002069 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002070 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002071 {
2072 $this->ar_caching = FALSE;
2073 }
2074
2075 // --------------------------------------------------------------------
2076
2077 /**
2078 * Flush Cache
2079 *
2080 * Empties the AR cache
2081 *
2082 * @access public
2083 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002084 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002085 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002086 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002087 $this->_reset_run(array(
2088 'ar_cache_select' => array(),
2089 'ar_cache_from' => array(),
2090 'ar_cache_join' => array(),
2091 'ar_cache_where' => array(),
2092 'ar_cache_like' => array(),
2093 'ar_cache_groupby' => array(),
2094 'ar_cache_having' => array(),
2095 'ar_cache_orderby' => array(),
2096 'ar_cache_set' => array(),
2097 'ar_cache_exists' => array(),
2098 'ar_cache_no_escape' => array()
2099 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002100 }
2101
2102 // --------------------------------------------------------------------
2103
2104 /**
2105 * Merge Cache
2106 *
Barry Mienydd671972010-10-04 16:33:58 +02002107 * When called, this function merges any cached AR arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002108 * locally called ones.
2109 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002110 * @return void
2111 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002112 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002113 {
2114 if (count($this->ar_cache_exists) == 0)
2115 {
2116 return;
2117 }
2118
2119 foreach ($this->ar_cache_exists as $val)
2120 {
2121 $ar_variable = 'ar_'.$val;
2122 $ar_cache_var = 'ar_cache_'.$val;
2123
2124 if (count($this->$ar_cache_var) == 0)
2125 {
2126 continue;
2127 }
2128
2129 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
2130 }
2131
2132 // If we are "protecting identifiers" we need to examine the "from"
2133 // portion of the query to determine if there are any aliases
2134 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
2135 {
2136 $this->_track_aliases($this->ar_from);
2137 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002138
2139 $this->ar_no_escape = $this->ar_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002140 }
Kyle Farris0c147b32011-08-26 02:29:31 -04002141
2142 // --------------------------------------------------------------------
2143
2144 /**
2145 * Reset Active Record values.
2146 *
2147 * Publicly-visible method to reset the AR values.
2148 *
2149 * @access public
2150 * @return void
2151 */
2152 public function reset_query()
2153 {
2154 $this->_reset_select();
2155 $this->_reset_write();
2156 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002157
2158 // --------------------------------------------------------------------
2159
2160 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002161 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002162 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002163 * @param array An array of fields to reset
2164 * @return void
2165 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002166 protected function _reset_run($ar_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002167 {
2168 foreach ($ar_reset_items as $item => $default_value)
2169 {
2170 if ( ! in_array($item, $this->ar_store_array))
2171 {
2172 $this->$item = $default_value;
2173 }
2174 }
2175 }
2176
2177 // --------------------------------------------------------------------
2178
2179 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002180 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002181 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002182 * @return void
2183 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002184 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002185 {
2186 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06002187 'ar_select' => array(),
2188 'ar_from' => array(),
2189 'ar_join' => array(),
2190 'ar_where' => array(),
2191 'ar_like' => array(),
2192 'ar_groupby' => array(),
2193 'ar_having' => array(),
2194 'ar_orderby' => array(),
2195 'ar_wherein' => array(),
2196 'ar_aliased_tables' => array(),
2197 'ar_no_escape' => array(),
2198 'ar_distinct' => FALSE,
2199 'ar_limit' => FALSE,
2200 'ar_offset' => FALSE,
2201 'ar_order' => FALSE,
2202 );
Barry Mienydd671972010-10-04 16:33:58 +02002203
Derek Allard2067d1a2008-11-13 22:59:24 +00002204 $this->_reset_run($ar_reset_items);
2205 }
Barry Mienydd671972010-10-04 16:33:58 +02002206
Derek Allard2067d1a2008-11-13 22:59:24 +00002207 // --------------------------------------------------------------------
2208
2209 /**
2210 * Resets the active record "write" values.
2211 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002212 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002213 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002214 * @return void
2215 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002216 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002217 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002218 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06002219 'ar_set' => array(),
2220 'ar_from' => array(),
2221 'ar_where' => array(),
2222 'ar_like' => array(),
2223 'ar_orderby' => array(),
2224 'ar_keys' => array(),
2225 'ar_limit' => FALSE,
2226 'ar_order' => FALSE
2227 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002228
2229 $this->_reset_run($ar_reset_items);
2230 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002231}
2232
2233/* End of file DB_active_rec.php */
Kyle Farris0c147b32011-08-26 02:29:31 -04002234/* Location: ./system/database/DB_active_rec.php */