blob: 59cd1972cdfa8bed9704607598b4c8750d3ea401 [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 Farris2de2fa02011-08-31 11:52:20 -0400202 $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($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
Kyle Farris81ef70f2011-08-31 11:59:12 -0400667 if ($side == 'none')
668 {
669 $like_statement = $prefix." $k $not LIKE '{$v}'";
670 }
671 elseif ($side == 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 {
673 $like_statement = $prefix." $k $not LIKE '%{$v}'";
674 }
675 elseif ($side == 'after')
676 {
677 $like_statement = $prefix." $k $not LIKE '{$v}%'";
678 }
679 else
680 {
681 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
682 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600683
Derek Jonese4ed5832009-02-20 21:44:59 +0000684 // some platforms require an escape sequence definition for LIKE wildcards
685 if ($this->_like_escape_str != '')
686 {
Greg Aker0d424892010-01-26 02:14:44 +0000687 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000688 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 $this->ar_like[] = $like_statement;
691 if ($this->ar_caching === TRUE)
692 {
693 $this->ar_cache_like[] = $like_statement;
694 $this->ar_cache_exists[] = 'like';
695 }
Barry Mienydd671972010-10-04 16:33:58 +0200696
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 }
698 return $this;
699 }
Barry Mienydd671972010-10-04 16:33:58 +0200700
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 // --------------------------------------------------------------------
702
703 /**
704 * GROUP BY
705 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 * @param string
707 * @return object
708 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600709 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 {
711 if (is_string($by))
712 {
713 $by = explode(',', $by);
714 }
Barry Mienydd671972010-10-04 16:33:58 +0200715
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 foreach ($by as $val)
717 {
718 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200719
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 if ($val != '')
721 {
722 $this->ar_groupby[] = $this->_protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200723
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 if ($this->ar_caching === TRUE)
725 {
726 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
727 $this->ar_cache_exists[] = 'groupby';
728 }
729 }
730 }
731 return $this;
732 }
733
734 // --------------------------------------------------------------------
735
736 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 * Sets the HAVING value
738 *
739 * Separates multiple calls with AND
740 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 * @param string
742 * @param string
743 * @return object
744 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600745 public function having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 {
747 return $this->_having($key, $value, 'AND ', $escape);
748 }
Barry Mienydd671972010-10-04 16:33:58 +0200749
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 // --------------------------------------------------------------------
751
752 /**
753 * Sets the OR HAVING value
754 *
755 * Separates multiple calls with OR
756 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 * @param string
758 * @param string
759 * @return object
760 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600761 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 {
763 return $this->_having($key, $value, 'OR ', $escape);
764 }
Barry Mienydd671972010-10-04 16:33:58 +0200765
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 // --------------------------------------------------------------------
767
768 /**
769 * Sets the HAVING values
770 *
771 * Called by having() or or_having()
772 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 * @param string
774 * @param string
775 * @return object
776 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600777 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 {
779 if ( ! is_array($key))
780 {
781 $key = array($key => $value);
782 }
Barry Mienydd671972010-10-04 16:33:58 +0200783
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 foreach ($key as $k => $v)
785 {
786 $prefix = (count($this->ar_having) == 0) ? '' : $type;
787
788 if ($escape === TRUE)
789 {
790 $k = $this->_protect_identifiers($k);
791 }
792
793 if ( ! $this->_has_operator($k))
794 {
795 $k .= ' = ';
796 }
797
798 if ($v != '')
799 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400800 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 }
Barry Mienydd671972010-10-04 16:33:58 +0200802
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 $this->ar_having[] = $prefix.$k.$v;
804 if ($this->ar_caching === TRUE)
805 {
806 $this->ar_cache_having[] = $prefix.$k.$v;
807 $this->ar_cache_exists[] = 'having';
808 }
809 }
Barry Mienydd671972010-10-04 16:33:58 +0200810
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 return $this;
812 }
Barry Mienydd671972010-10-04 16:33:58 +0200813
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 // --------------------------------------------------------------------
815
816 /**
817 * Sets the ORDER BY value
818 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 * @param string
820 * @param string direction: asc or desc
821 * @return object
822 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600823 public function order_by($orderby, $direction = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 {
825 if (strtolower($direction) == 'random')
826 {
827 $orderby = ''; // Random results want or don't need a field name
828 $direction = $this->_random_keyword;
829 }
830 elseif (trim($direction) != '')
831 {
832 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
833 }
Barry Mienydd671972010-10-04 16:33:58 +0200834
835
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 if (strpos($orderby, ',') !== FALSE)
837 {
838 $temp = array();
839 foreach (explode(',', $orderby) as $part)
840 {
841 $part = trim($part);
842 if ( ! in_array($part, $this->ar_aliased_tables))
843 {
844 $part = $this->_protect_identifiers(trim($part));
845 }
Barry Mienydd671972010-10-04 16:33:58 +0200846
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 $temp[] = $part;
848 }
Barry Mienydd671972010-10-04 16:33:58 +0200849
850 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 }
Derek Allarde37ab382009-02-03 16:13:57 +0000852 else if ($direction != $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 {
854 $orderby = $this->_protect_identifiers($orderby);
855 }
Barry Mienydd671972010-10-04 16:33:58 +0200856
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200858
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 $this->ar_orderby[] = $orderby_statement;
860 if ($this->ar_caching === TRUE)
861 {
862 $this->ar_cache_orderby[] = $orderby_statement;
863 $this->ar_cache_exists[] = 'orderby';
864 }
865
866 return $this;
867 }
Barry Mienydd671972010-10-04 16:33:58 +0200868
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 // --------------------------------------------------------------------
870
871 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 * Sets the LIMIT value
873 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 * @param integer the limit value
875 * @param integer the offset value
876 * @return object
877 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600878 public function limit($value, $offset = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
Kyle Farris76116012011-08-31 11:17:48 -0400880 $this->ar_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000881
882 if ($offset != '')
883 {
Kyle Farris76116012011-08-31 11:17:48 -0400884 $this->ar_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 }
Barry Mienydd671972010-10-04 16:33:58 +0200886
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 return $this;
888 }
Barry Mienydd671972010-10-04 16:33:58 +0200889
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 // --------------------------------------------------------------------
891
892 /**
893 * Sets the OFFSET value
894 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 * @param integer the offset value
896 * @return object
897 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600898 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 {
900 $this->ar_offset = $offset;
901 return $this;
902 }
Barry Mienydd671972010-10-04 16:33:58 +0200903
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 // --------------------------------------------------------------------
905
906 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500907 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 * @param mixed
910 * @param string
911 * @param boolean
912 * @return object
913 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600914 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 {
916 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +0200917
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 if ( ! is_array($key))
919 {
920 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +0200921 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000922
923 foreach ($key as $k => $v)
924 {
925 if ($escape === FALSE)
926 {
927 $this->ar_set[$this->_protect_identifiers($k)] = $v;
928 }
929 else
930 {
Phil Sturgeond0ac1a22011-02-15 22:54:08 +0000931 $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 }
933 }
Barry Mienydd671972010-10-04 16:33:58 +0200934
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 return $this;
936 }
Kyle Farris0c147b32011-08-26 02:29:31 -0400937
Kyle Farris0c147b32011-08-26 02:29:31 -0400938 // --------------------------------------------------------------------
939
940 /**
941 * Get SELECT query string
942 *
943 * Compiles a SELECT query string and returns the sql.
944 *
945 * @access public
946 * @param string the table name to select from (optional)
947 * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone
948 * @return string
949 */
950 public function get_compiled_select($table = '', $reset = TRUE)
951 {
952 if ($table != '')
kylefarris0a3176b2011-08-26 02:37:52 -0400953 {
Kyle Farris0c147b32011-08-26 02:29:31 -0400954 $this->_track_aliases($table);
955 $this->from($table);
956 }
957
958 $select = $this->_compile_select();
959
960 if ($reset === TRUE)
961 {
962 $this->_reset_select();
963 }
964
965 return $select;
966 }
967
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 // --------------------------------------------------------------------
969
970 /**
971 * Get
972 *
973 * Compiles the select statement based on the other functions called
974 * and runs the query
975 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 * @param string the table
977 * @param string the limit clause
978 * @param string the offset clause
979 * @return object
980 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600981 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 {
983 if ($table != '')
984 {
985 $this->_track_aliases($table);
986 $this->from($table);
987 }
Barry Mienydd671972010-10-04 16:33:58 +0200988
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 if ( ! is_null($limit))
990 {
991 $this->limit($limit, $offset);
992 }
Barry Mienydd671972010-10-04 16:33:58 +0200993
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 $sql = $this->_compile_select();
995
996 $result = $this->query($sql);
997 $this->_reset_select();
998 return $result;
999 }
1000
1001 /**
1002 * "Count All Results" query
1003 *
Barry Mienydd671972010-10-04 16:33:58 +02001004 * Generates a platform-specific query string that counts all records
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 * returned by an Active Record query.
1006 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 * @param string
1008 * @return string
1009 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001010 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 {
1012 if ($table != '')
1013 {
1014 $this->_track_aliases($table);
1015 $this->from($table);
1016 }
Barry Mienydd671972010-10-04 16:33:58 +02001017
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
1019
1020 $query = $this->query($sql);
1021 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001022
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 if ($query->num_rows() == 0)
1024 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001025 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 }
1027
1028 $row = $query->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001029 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 }
1031
1032 // --------------------------------------------------------------------
1033
1034 /**
1035 * Get_Where
1036 *
1037 * Allows the where clause, limit and offset to be added directly
1038 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 * @param string the where clause
1040 * @param string the limit clause
1041 * @param string the offset clause
1042 * @return object
1043 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001044 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 {
1046 if ($table != '')
1047 {
1048 $this->from($table);
1049 }
1050
1051 if ( ! is_null($where))
1052 {
1053 $this->where($where);
1054 }
Barry Mienydd671972010-10-04 16:33:58 +02001055
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 if ( ! is_null($limit))
1057 {
1058 $this->limit($limit, $offset);
1059 }
Barry Mienydd671972010-10-04 16:33:58 +02001060
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 $sql = $this->_compile_select();
1062
1063 $result = $this->query($sql);
1064 $this->_reset_select();
1065 return $result;
1066 }
1067
1068 // --------------------------------------------------------------------
1069
1070 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001071 * Insert_Batch
1072 *
1073 * Compiles batch insert strings and runs the queries
1074 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001075 * @param string the table to retrieve the results from
1076 * @param array an associative array of insert values
1077 * @return object
1078 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001079 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001080 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001081 if ( ! is_null($set))
1082 {
1083 $this->set_insert_batch($set);
1084 }
Barry Mienydd671972010-10-04 16:33:58 +02001085
Derek Jonesd10e8962010-03-02 17:10:36 -06001086 if (count($this->ar_set) == 0)
1087 {
1088 if ($this->db_debug)
1089 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001090 //No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001091 return $this->display_error('db_must_use_set');
1092 }
1093 return FALSE;
1094 }
1095
1096 if ($table == '')
1097 {
1098 if ( ! isset($this->ar_from[0]))
1099 {
1100 if ($this->db_debug)
1101 {
1102 return $this->display_error('db_must_set_table');
1103 }
1104 return FALSE;
1105 }
Barry Mienydd671972010-10-04 16:33:58 +02001106
Derek Jonesd10e8962010-03-02 17:10:36 -06001107 $table = $this->ar_from[0];
1108 }
1109
1110 // Batch this baby
1111 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1112 {
Barry Mienydd671972010-10-04 16:33:58 +02001113
Derek Jonesd10e8962010-03-02 17:10:36 -06001114 $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
1115
1116 //echo $sql;
1117
1118 $this->query($sql);
1119 }
Barry Mienydd671972010-10-04 16:33:58 +02001120
Derek Jonesd10e8962010-03-02 17:10:36 -06001121 $this->_reset_write();
1122
1123
Barry Mienydd671972010-10-04 16:33:58 +02001124 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001125 }
1126
1127 // --------------------------------------------------------------------
1128
1129 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001130 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001131 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001132 * @param mixed
1133 * @param string
1134 * @param boolean
1135 * @return object
1136 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001137 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001138 {
1139 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001140
Derek Jonesd10e8962010-03-02 17:10:36 -06001141 if ( ! is_array($key))
1142 {
1143 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001144 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001145
1146 $keys = array_keys(current($key));
1147 sort($keys);
1148
1149 foreach ($key as $row)
1150 {
Barry Mienydd671972010-10-04 16:33:58 +02001151 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1152 {
1153 // batch function above returns an error on an empty array
1154 $this->ar_set[] = array();
1155 return;
1156 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001157
Barry Mienydd671972010-10-04 16:33:58 +02001158 ksort($row); // puts $row in the same order as our keys
1159
Derek Jonesd10e8962010-03-02 17:10:36 -06001160 if ($escape === FALSE)
1161 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001162 $this->ar_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001163 }
1164 else
1165 {
1166 $clean = array();
1167
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001168 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001169 {
Barry Mienydd671972010-10-04 16:33:58 +02001170 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001171 }
1172
Derek Jones37f4b9c2011-07-01 17:56:50 -05001173 $this->ar_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001174 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001175 }
1176
1177 foreach ($keys as $k)
1178 {
1179 $this->ar_keys[] = $this->_protect_identifiers($k);
1180 }
Barry Mienydd671972010-10-04 16:33:58 +02001181
Derek Jonesd10e8962010-03-02 17:10:36 -06001182 return $this;
1183 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001184
1185 // --------------------------------------------------------------------
1186
1187 /**
1188 * Get INSERT query string
1189 *
1190 * Compiles an insert query and returns the sql
1191 *
1192 * @access public
1193 * @param string the table to insert into
1194 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1195 * @return string
1196 */
1197 public function get_compiled_insert($table = '', $reset = TRUE)
1198 {
1199 if ($this->_validate_insert($table) === FALSE)
1200 {
1201 return FALSE;
1202 }
1203
Kyle Farris76116012011-08-31 11:17:48 -04001204 $sql = $this->_insert(
1205 $this->_protect_identifiers(
1206 $this->ar_from[0], TRUE, NULL, FALSE
1207 ),
1208 array_keys($this->ar_set),
1209 array_values($this->ar_set)
1210 );
Kyle Farris0c147b32011-08-26 02:29:31 -04001211
1212 if ($reset === TRUE)
1213 {
1214 $this->_reset_write();
1215 }
1216
1217 return $sql;
1218 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001219
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 // --------------------------------------------------------------------
1221
1222 /**
1223 * Insert
1224 *
1225 * Compiles an insert string and runs the query
1226 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001227 * @access public
Phil Sturgeon9789f322011-07-15 15:14:05 -06001228 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 * @param array an associative array of insert values
1230 * @return object
1231 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001232 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001233 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 if ( ! is_null($set))
1235 {
1236 $this->set($set);
1237 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001238
1239 if ($this->_validate_insert($table) === FALSE)
1240 {
1241 return FALSE;
1242 }
1243
Kyle Farris76116012011-08-31 11:17:48 -04001244 $sql = $this->_insert(
1245 $this->_protect_identifiers(
1246 $this->ar_from[0], TRUE, NULL, FALSE
1247 ),
1248 array_keys($this->ar_set),
1249 array_values($this->ar_set)
1250 );
Barry Mienydd671972010-10-04 16:33:58 +02001251
Kyle Farris0c147b32011-08-26 02:29:31 -04001252 $this->_reset_write();
1253 return $this->query($sql);
1254 }
1255
Kyle Farris0c147b32011-08-26 02:29:31 -04001256 // --------------------------------------------------------------------
1257
1258 /**
1259 * Validate Insert
1260 *
1261 * This method is used by both insert() and get_compiled_insert() to
1262 * validate that the there data is actually being set and that table
1263 * has been chosen to be inserted into.
1264 *
1265 * @access public
1266 * @param string the table to insert data into
1267 * @return string
1268 */
1269 protected function _validate_insert($table = '')
1270 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 if (count($this->ar_set) == 0)
1272 {
1273 if ($this->db_debug)
1274 {
1275 return $this->display_error('db_must_use_set');
1276 }
1277 return FALSE;
1278 }
1279
1280 if ($table == '')
1281 {
1282 if ( ! isset($this->ar_from[0]))
1283 {
1284 if ($this->db_debug)
1285 {
1286 return $this->display_error('db_must_set_table');
1287 }
1288 return FALSE;
1289 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001291 else
1292 {
1293 $this->ar_from[0] = $table;
1294 }
1295
1296 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 }
Barry Mienydd671972010-10-04 16:33:58 +02001298
Phil Sturgeon9789f322011-07-15 15:14:05 -06001299 // --------------------------------------------------------------------
1300
1301 /**
1302 * Replace
1303 *
1304 * Compiles an replace into string and runs the query
1305 *
1306 * @param string the table to replace data into
1307 * @param array an associative array of insert values
1308 * @return object
1309 */
1310 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001311 {
1312 if ( ! is_null($set))
1313 {
1314 $this->set($set);
1315 }
Barry Mienydd671972010-10-04 16:33:58 +02001316
Derek Jonesd10e8962010-03-02 17:10:36 -06001317 if (count($this->ar_set) == 0)
1318 {
1319 if ($this->db_debug)
1320 {
1321 return $this->display_error('db_must_use_set');
1322 }
1323 return FALSE;
1324 }
1325
1326 if ($table == '')
1327 {
1328 if ( ! isset($this->ar_from[0]))
1329 {
1330 if ($this->db_debug)
1331 {
1332 return $this->display_error('db_must_set_table');
1333 }
1334 return FALSE;
1335 }
Barry Mienydd671972010-10-04 16:33:58 +02001336
Derek Jonesd10e8962010-03-02 17:10:36 -06001337 $table = $this->ar_from[0];
1338 }
1339
1340 $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 +02001341
Derek Jonesd10e8962010-03-02 17:10:36 -06001342 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001343 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001344 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001345
Kyle Farris0c147b32011-08-26 02:29:31 -04001346 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001347
Kyle Farris0c147b32011-08-26 02:29:31 -04001348 /**
1349 * Get UPDATE query string
1350 *
1351 * Compiles an update query and returns the sql
1352 *
1353 * @access public
1354 * @param string the table to update
1355 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1356 * @return string
1357 */
1358 public function get_compiled_update($table = '', $reset = TRUE)
1359 {
1360 // Combine any cached components with the current statements
1361 $this->_merge_cache();
1362
1363 if ($this->_validate_update($table) === FALSE)
1364 {
1365 return FALSE;
1366 }
1367
1368 $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);
1369
1370 if ($reset === TRUE)
1371 {
1372 $this->_reset_write();
1373 }
1374
1375 return $sql;
1376 }
1377
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 // --------------------------------------------------------------------
1379
1380 /**
1381 * Update
1382 *
1383 * Compiles an update string and runs the query
1384 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001385 * @param string the table to retrieve the results from
1386 * @param array an associative array of update values
1387 * @param mixed the where clause
1388 * @return object
1389 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001390 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001391 {
1392 // Combine any cached components with the current statements
1393 $this->_merge_cache();
1394
1395 if ( ! is_null($set))
1396 {
1397 $this->set($set);
1398 }
Barry Mienydd671972010-10-04 16:33:58 +02001399
Kyle Farris0c147b32011-08-26 02:29:31 -04001400 if ($this->_validate_update($table) === FALSE)
1401 {
1402 return FALSE;
1403 }
1404
1405 if ($where != NULL)
1406 {
1407 $this->where($where);
1408 }
1409
1410 if ($limit != NULL)
1411 {
1412 $this->limit($limit);
1413 }
1414
1415 $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);
1416
1417 $this->_reset_write();
1418 return $this->query($sql);
1419 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001420
1421 // --------------------------------------------------------------------
1422
1423 /**
1424 * Validate Update
1425 *
1426 * This method is used by both update() and get_compiled_update() to
1427 * validate that data is actually being set and that a table has been
1428 * chosen to be update.
1429 *
1430 * @access public
1431 * @param string the table to update data on
1432 * @return string
1433 */
1434 protected function _validate_update($table = '')
1435 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001436 if (count($this->ar_set) == 0)
1437 {
1438 if ($this->db_debug)
1439 {
1440 return $this->display_error('db_must_use_set');
1441 }
1442 return FALSE;
1443 }
1444
1445 if ($table == '')
1446 {
1447 if ( ! isset($this->ar_from[0]))
1448 {
1449 if ($this->db_debug)
1450 {
1451 return $this->display_error('db_must_set_table');
1452 }
1453 return FALSE;
1454 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001455 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001456 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001457 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001458 $this->ar_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001460 }
Kyle Farris76116012011-08-31 11:17:48 -04001461
Derek Jonesd10e8962010-03-02 17:10:36 -06001462 // --------------------------------------------------------------------
1463
1464 /**
1465 * Update_Batch
1466 *
1467 * Compiles an update string and runs the query
1468 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001469 * @param string the table to retrieve the results from
1470 * @param array an associative array of update values
1471 * @param string the where key
1472 * @return object
1473 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001474 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001475 {
1476 // Combine any cached components with the current statements
1477 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001478
Derek Jonesd10e8962010-03-02 17:10:36 -06001479 if (is_null($index))
1480 {
1481 if ($this->db_debug)
1482 {
Kyle Farris2de2fa02011-08-31 11:52:20 -04001483 return $this->display_error('db_must_use_index');
Derek Jonesd10e8962010-03-02 17:10:36 -06001484 }
1485
1486 return FALSE;
1487 }
1488
1489 if ( ! is_null($set))
1490 {
1491 $this->set_update_batch($set, $index);
1492 }
1493
1494 if (count($this->ar_set) == 0)
1495 {
1496 if ($this->db_debug)
1497 {
1498 return $this->display_error('db_must_use_set');
1499 }
1500
1501 return FALSE;
1502 }
1503
1504 if ($table == '')
1505 {
1506 if ( ! isset($this->ar_from[0]))
1507 {
1508 if ($this->db_debug)
1509 {
1510 return $this->display_error('db_must_set_table');
1511 }
1512 return FALSE;
1513 }
Barry Mienydd671972010-10-04 16:33:58 +02001514
Derek Jonesd10e8962010-03-02 17:10:36 -06001515 $table = $this->ar_from[0];
1516 }
Barry Mienydd671972010-10-04 16:33:58 +02001517
Derek Jonesd10e8962010-03-02 17:10:36 -06001518 // Batch this baby
1519 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1520 {
1521 $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);
1522
1523 $this->query($sql);
1524 }
Barry Mienydd671972010-10-04 16:33:58 +02001525
Derek Jonesd10e8962010-03-02 17:10:36 -06001526 $this->_reset_write();
1527 }
1528
1529 // --------------------------------------------------------------------
1530
1531 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001532 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001533 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001534 * @param array
1535 * @param string
1536 * @param boolean
1537 * @return object
1538 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001539 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001540 {
1541 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001542
Derek Jonesd10e8962010-03-02 17:10:36 -06001543 if ( ! is_array($key))
1544 {
1545 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001546 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001547
1548 foreach ($key as $k => $v)
1549 {
1550 $index_set = FALSE;
1551 $clean = array();
1552
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001553 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001554 {
1555 if ($k2 == $index)
1556 {
1557 $index_set = TRUE;
1558 }
1559 else
1560 {
1561 $not[] = $k.'-'.$v;
1562 }
1563
1564 if ($escape === FALSE)
1565 {
1566 $clean[$this->_protect_identifiers($k2)] = $v2;
1567 }
1568 else
1569 {
Barry Mienydd671972010-10-04 16:33:58 +02001570 $clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001571 }
1572 }
1573
1574 if ($index_set == FALSE)
1575 {
1576 return $this->display_error('db_batch_missing_index');
1577 }
1578
1579 $this->ar_set[] = $clean;
1580 }
Barry Mienydd671972010-10-04 16:33:58 +02001581
Derek Jonesd10e8962010-03-02 17:10:36 -06001582 return $this;
1583 }
1584
Derek Allard2067d1a2008-11-13 22:59:24 +00001585 // --------------------------------------------------------------------
1586
1587 /**
1588 * Empty Table
1589 *
1590 * Compiles a delete string and runs "DELETE FROM table"
1591 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001592 * @param string the table to empty
1593 * @return object
1594 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001595 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001596 {
1597 if ($table == '')
1598 {
1599 if ( ! isset($this->ar_from[0]))
1600 {
1601 if ($this->db_debug)
1602 {
1603 return $this->display_error('db_must_set_table');
1604 }
1605 return FALSE;
1606 }
1607
1608 $table = $this->ar_from[0];
1609 }
1610 else
1611 {
1612 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1613 }
1614
1615 $sql = $this->_delete($table);
1616
1617 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001618
Derek Allard2067d1a2008-11-13 22:59:24 +00001619 return $this->query($sql);
1620 }
1621
1622 // --------------------------------------------------------------------
1623
1624 /**
1625 * Truncate
1626 *
1627 * Compiles a truncate string and runs the query
1628 * If the database does not support the truncate() command
1629 * This function maps to "DELETE FROM table"
1630 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001631 * @param string the table to truncate
1632 * @return object
1633 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001634 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001635 {
1636 if ($table == '')
1637 {
1638 if ( ! isset($this->ar_from[0]))
1639 {
1640 if ($this->db_debug)
1641 {
1642 return $this->display_error('db_must_set_table');
1643 }
1644 return FALSE;
1645 }
1646
1647 $table = $this->ar_from[0];
1648 }
1649 else
1650 {
1651 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1652 }
1653
1654 $sql = $this->_truncate($table);
1655
1656 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001657
Derek Allard2067d1a2008-11-13 22:59:24 +00001658 return $this->query($sql);
1659 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001660
1661 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001662
Kyle Farris0c147b32011-08-26 02:29:31 -04001663 /**
1664 * Get DELETE query string
1665 *
1666 * Compiles a delete query string and returns the sql
1667 *
1668 * @access public
1669 * @param string the table to delete from
1670 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1671 * @return string
1672 */
1673 public function get_compiled_delete($table = '', $reset = TRUE)
1674 {
1675 $this->return_delete_sql = TRUE;
1676 $sql = $this->delete($table, '', NULL, $reset);
1677 $this->return_delete_sql = FALSE;
1678 return $sql;
1679 }
1680
Derek Allard2067d1a2008-11-13 22:59:24 +00001681 // --------------------------------------------------------------------
1682
1683 /**
1684 * Delete
1685 *
1686 * Compiles a delete string and runs the query
1687 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001688 * @param mixed the table(s) to delete from. String or array
1689 * @param mixed the where clause
1690 * @param mixed the limit clause
1691 * @param boolean
1692 * @return object
1693 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001694 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001695 {
1696 // Combine any cached components with the current statements
1697 $this->_merge_cache();
1698
1699 if ($table == '')
1700 {
1701 if ( ! isset($this->ar_from[0]))
1702 {
1703 if ($this->db_debug)
1704 {
1705 return $this->display_error('db_must_set_table');
1706 }
1707 return FALSE;
1708 }
1709
1710 $table = $this->ar_from[0];
1711 }
1712 elseif (is_array($table))
1713 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001714 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001715 {
1716 $this->delete($single_table, $where, $limit, FALSE);
1717 }
1718
1719 $this->_reset_write();
1720 return;
1721 }
1722 else
1723 {
1724 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1725 }
1726
1727 if ($where != '')
1728 {
1729 $this->where($where);
1730 }
1731
1732 if ($limit != NULL)
1733 {
1734 $this->limit($limit);
1735 }
1736
Derek Allard03d783b2009-05-03 19:45:45 +00001737 if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001738 {
1739 if ($this->db_debug)
1740 {
1741 return $this->display_error('db_del_must_use_where');
1742 }
1743
1744 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001745 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001746
1747 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
1748
1749 if ($reset_data)
1750 {
1751 $this->_reset_write();
1752 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001753
1754 if ($this->return_delete_sql === true)
1755 {
1756 return $sql;
1757 }
Barry Mienydd671972010-10-04 16:33:58 +02001758
Derek Allard2067d1a2008-11-13 22:59:24 +00001759 return $this->query($sql);
1760 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001761
Derek Allard2067d1a2008-11-13 22:59:24 +00001762 // --------------------------------------------------------------------
1763
1764 /**
1765 * DB Prefix
1766 *
1767 * Prepends a database prefix if one exists in configuration
1768 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001769 * @param string the table
1770 * @return string
1771 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001772 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001773 {
1774 if ($table == '')
1775 {
1776 $this->display_error('db_table_name_required');
1777 }
1778
1779 return $this->dbprefix.$table;
1780 }
1781
1782 // --------------------------------------------------------------------
1783
1784 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001785 * Set DB Prefix
1786 *
1787 * Set's the DB Prefix to something new without needing to reconnect
1788 *
1789 * @param string the prefix
1790 * @return string
1791 */
1792 public function set_dbprefix($prefix = '')
1793 {
1794 return $this->dbprefix = $prefix;
1795 }
1796
1797 // --------------------------------------------------------------------
1798
1799 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001800 * Track Aliases
1801 *
1802 * Used to track SQL statements written with aliased tables.
1803 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001804 * @param string The table to inspect
1805 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001806 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001807 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001808 {
1809 if (is_array($table))
1810 {
1811 foreach ($table as $t)
1812 {
1813 $this->_track_aliases($t);
1814 }
1815 return;
1816 }
Barry Mienydd671972010-10-04 16:33:58 +02001817
Derek Jones37f4b9c2011-07-01 17:56:50 -05001818 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001819 // the string into discreet statements
1820 if (strpos($table, ',') !== FALSE)
1821 {
1822 return $this->_track_aliases(explode(',', $table));
1823 }
Barry Mienydd671972010-10-04 16:33:58 +02001824
Derek Allard2067d1a2008-11-13 22:59:24 +00001825 // if a table alias is used we can recognize it by a space
1826 if (strpos($table, " ") !== FALSE)
1827 {
1828 // if the alias is written with the AS keyword, remove it
1829 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001830
Derek Allard2067d1a2008-11-13 22:59:24 +00001831 // Grab the alias
1832 $table = trim(strrchr($table, " "));
Barry Mienydd671972010-10-04 16:33:58 +02001833
Derek Allard2067d1a2008-11-13 22:59:24 +00001834 // Store the alias, if it doesn't already exist
1835 if ( ! in_array($table, $this->ar_aliased_tables))
1836 {
1837 $this->ar_aliased_tables[] = $table;
1838 }
1839 }
1840 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001841
Derek Allard2067d1a2008-11-13 22:59:24 +00001842 // --------------------------------------------------------------------
1843
1844 /**
1845 * Compile the SELECT statement
1846 *
1847 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001848 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001849 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001850 * @return string
1851 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001852 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001853 {
1854 // Combine any cached components with the current statements
1855 $this->_merge_cache();
1856
1857 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001858
Derek Allard2067d1a2008-11-13 22:59:24 +00001859 // Write the "select" portion of the query
1860
1861 if ($select_override !== FALSE)
1862 {
1863 $sql = $select_override;
1864 }
1865 else
1866 {
1867 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001868
Derek Allard2067d1a2008-11-13 22:59:24 +00001869 if (count($this->ar_select) == 0)
1870 {
Barry Mienydd671972010-10-04 16:33:58 +02001871 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001872 }
1873 else
Barry Mienydd671972010-10-04 16:33:58 +02001874 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001875 // Cycle through the "select" portion of the query and prep each column name.
1876 // The reason we protect identifiers here rather then in the select() function
1877 // is because until the user calls the from() function we don't know if there are aliases
1878 foreach ($this->ar_select as $key => $val)
1879 {
Phil Sturgeon77cc0282011-08-09 16:03:49 -06001880 $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL;
1881 $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001882 }
Barry Mienydd671972010-10-04 16:33:58 +02001883
Derek Allard2067d1a2008-11-13 22:59:24 +00001884 $sql .= implode(', ', $this->ar_select);
1885 }
1886 }
1887
1888 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001889
Derek Allard2067d1a2008-11-13 22:59:24 +00001890 // Write the "FROM" portion of the query
1891
1892 if (count($this->ar_from) > 0)
1893 {
1894 $sql .= "\nFROM ";
1895
1896 $sql .= $this->_from_tables($this->ar_from);
1897 }
1898
1899 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001900
Derek Allard2067d1a2008-11-13 22:59:24 +00001901 // Write the "JOIN" portion of the query
1902
1903 if (count($this->ar_join) > 0)
1904 {
1905 $sql .= "\n";
1906
1907 $sql .= implode("\n", $this->ar_join);
1908 }
1909
1910 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001911
Derek Allard2067d1a2008-11-13 22:59:24 +00001912 // Write the "WHERE" portion of the query
1913
1914 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1915 {
Greg Akere156c6e2011-04-20 16:03:04 -05001916 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00001917 }
1918
1919 $sql .= implode("\n", $this->ar_where);
1920
1921 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001922
Derek Allard2067d1a2008-11-13 22:59:24 +00001923 // Write the "LIKE" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001924
Derek Allard2067d1a2008-11-13 22:59:24 +00001925 if (count($this->ar_like) > 0)
1926 {
1927 if (count($this->ar_where) > 0)
1928 {
1929 $sql .= "\nAND ";
1930 }
1931
1932 $sql .= implode("\n", $this->ar_like);
1933 }
1934
1935 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001936
Derek Allard2067d1a2008-11-13 22:59:24 +00001937 // Write the "GROUP BY" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001938
Derek Allard2067d1a2008-11-13 22:59:24 +00001939 if (count($this->ar_groupby) > 0)
1940 {
1941 $sql .= "\nGROUP BY ";
Barry Mienydd671972010-10-04 16:33:58 +02001942
Derek Allard2067d1a2008-11-13 22:59:24 +00001943 $sql .= implode(', ', $this->ar_groupby);
1944 }
1945
1946 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001947
Derek Allard2067d1a2008-11-13 22:59:24 +00001948 // Write the "HAVING" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001949
Derek Allard2067d1a2008-11-13 22:59:24 +00001950 if (count($this->ar_having) > 0)
1951 {
1952 $sql .= "\nHAVING ";
1953 $sql .= implode("\n", $this->ar_having);
1954 }
1955
1956 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001957
Derek Allard2067d1a2008-11-13 22:59:24 +00001958 // Write the "ORDER BY" portion of the query
1959
1960 if (count($this->ar_orderby) > 0)
1961 {
1962 $sql .= "\nORDER BY ";
1963 $sql .= implode(', ', $this->ar_orderby);
Barry Mienydd671972010-10-04 16:33:58 +02001964
Derek Allard2067d1a2008-11-13 22:59:24 +00001965 if ($this->ar_order !== FALSE)
1966 {
1967 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02001968 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001969 }
1970
1971 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001972
Derek Allard2067d1a2008-11-13 22:59:24 +00001973 // Write the "LIMIT" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001974
Derek Allard2067d1a2008-11-13 22:59:24 +00001975 if (is_numeric($this->ar_limit))
1976 {
1977 $sql .= "\n";
1978 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1979 }
1980
1981 return $sql;
1982 }
1983
1984 // --------------------------------------------------------------------
1985
1986 /**
1987 * Object to Array
1988 *
1989 * Takes an object as input and converts the class variables to array key/vals
1990 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001991 * @param object
1992 * @return array
1993 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001994 public function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001995 {
1996 if ( ! is_object($object))
1997 {
1998 return $object;
1999 }
Barry Mienydd671972010-10-04 16:33:58 +02002000
Derek Allard2067d1a2008-11-13 22:59:24 +00002001 $array = array();
2002 foreach (get_object_vars($object) as $key => $val)
2003 {
2004 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06002005 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002006 {
2007 $array[$key] = $val;
2008 }
2009 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002010
2011 return $array;
2012 }
Barry Mienydd671972010-10-04 16:33:58 +02002013
Derek Jonesd10e8962010-03-02 17:10:36 -06002014 // --------------------------------------------------------------------
2015
2016 /**
2017 * Object to Array
2018 *
2019 * Takes an object as input and converts the class variables to array key/vals
2020 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002021 * @param object
2022 * @return array
2023 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002024 public function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002025 {
2026 if ( ! is_object($object))
2027 {
2028 return $object;
2029 }
Barry Mienydd671972010-10-04 16:33:58 +02002030
Derek Jonesd10e8962010-03-02 17:10:36 -06002031 $array = array();
2032 $out = get_object_vars($object);
2033 $fields = array_keys($out);
2034
2035 foreach ($fields as $val)
2036 {
2037 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06002038 if ($val != '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002039 {
2040
2041 $i = 0;
2042 foreach ($out[$val] as $data)
2043 {
2044 $array[$i][$val] = $data;
2045 $i++;
2046 }
2047 }
2048 }
2049
Derek Allard2067d1a2008-11-13 22:59:24 +00002050 return $array;
2051 }
Barry Mienydd671972010-10-04 16:33:58 +02002052
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 // --------------------------------------------------------------------
2054
2055 /**
2056 * Start Cache
2057 *
2058 * Starts AR caching
2059 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002060 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002061 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002062 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002063 {
2064 $this->ar_caching = TRUE;
2065 }
2066
2067 // --------------------------------------------------------------------
2068
2069 /**
2070 * Stop Cache
2071 *
2072 * Stops AR caching
2073 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002074 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002075 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002076 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002077 {
2078 $this->ar_caching = FALSE;
2079 }
2080
2081 // --------------------------------------------------------------------
2082
2083 /**
2084 * Flush Cache
2085 *
2086 * Empties the AR cache
2087 *
2088 * @access public
2089 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002090 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002091 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002092 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002093 $this->_reset_run(array(
2094 'ar_cache_select' => array(),
2095 'ar_cache_from' => array(),
2096 'ar_cache_join' => array(),
2097 'ar_cache_where' => array(),
2098 'ar_cache_like' => array(),
2099 'ar_cache_groupby' => array(),
2100 'ar_cache_having' => array(),
2101 'ar_cache_orderby' => array(),
2102 'ar_cache_set' => array(),
2103 'ar_cache_exists' => array(),
2104 'ar_cache_no_escape' => array()
2105 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002106 }
2107
2108 // --------------------------------------------------------------------
2109
2110 /**
2111 * Merge Cache
2112 *
Barry Mienydd671972010-10-04 16:33:58 +02002113 * When called, this function merges any cached AR arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002114 * locally called ones.
2115 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002116 * @return void
2117 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002118 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002119 {
2120 if (count($this->ar_cache_exists) == 0)
2121 {
2122 return;
2123 }
2124
2125 foreach ($this->ar_cache_exists as $val)
2126 {
2127 $ar_variable = 'ar_'.$val;
2128 $ar_cache_var = 'ar_cache_'.$val;
2129
2130 if (count($this->$ar_cache_var) == 0)
2131 {
2132 continue;
2133 }
2134
2135 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
2136 }
2137
2138 // If we are "protecting identifiers" we need to examine the "from"
2139 // portion of the query to determine if there are any aliases
2140 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
2141 {
2142 $this->_track_aliases($this->ar_from);
2143 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002144
2145 $this->ar_no_escape = $this->ar_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002146 }
Kyle Farris0c147b32011-08-26 02:29:31 -04002147
2148 // --------------------------------------------------------------------
2149
2150 /**
2151 * Reset Active Record values.
2152 *
2153 * Publicly-visible method to reset the AR values.
2154 *
2155 * @access public
2156 * @return void
2157 */
2158 public function reset_query()
2159 {
2160 $this->_reset_select();
2161 $this->_reset_write();
2162 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002163
2164 // --------------------------------------------------------------------
2165
2166 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002167 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002168 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002169 * @param array An array of fields to reset
2170 * @return void
2171 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002172 protected function _reset_run($ar_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002173 {
2174 foreach ($ar_reset_items as $item => $default_value)
2175 {
2176 if ( ! in_array($item, $this->ar_store_array))
2177 {
2178 $this->$item = $default_value;
2179 }
2180 }
2181 }
2182
2183 // --------------------------------------------------------------------
2184
2185 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002186 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002187 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002188 * @return void
2189 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002190 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002191 {
2192 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06002193 'ar_select' => array(),
2194 'ar_from' => array(),
2195 'ar_join' => array(),
2196 'ar_where' => array(),
2197 'ar_like' => array(),
2198 'ar_groupby' => array(),
2199 'ar_having' => array(),
2200 'ar_orderby' => array(),
2201 'ar_wherein' => array(),
2202 'ar_aliased_tables' => array(),
2203 'ar_no_escape' => array(),
2204 'ar_distinct' => FALSE,
2205 'ar_limit' => FALSE,
2206 'ar_offset' => FALSE,
2207 'ar_order' => FALSE,
2208 );
Barry Mienydd671972010-10-04 16:33:58 +02002209
Derek Allard2067d1a2008-11-13 22:59:24 +00002210 $this->_reset_run($ar_reset_items);
2211 }
Barry Mienydd671972010-10-04 16:33:58 +02002212
Derek Allard2067d1a2008-11-13 22:59:24 +00002213 // --------------------------------------------------------------------
2214
2215 /**
2216 * Resets the active record "write" values.
2217 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002218 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002219 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002220 * @return void
2221 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002222 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002223 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002224 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06002225 'ar_set' => array(),
2226 'ar_from' => array(),
2227 'ar_where' => array(),
2228 'ar_like' => array(),
2229 'ar_orderby' => array(),
2230 'ar_keys' => array(),
2231 'ar_limit' => FALSE,
2232 'ar_order' => FALSE
2233 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002234
2235 $this->_reset_run($ar_reset_items);
2236 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002237}
2238
2239/* End of file DB_active_rec.php */
Kyle Farris2de2fa02011-08-31 11:52:20 -04002240/* Location: ./system/database/DB_active_rec.php */