blob: 43920772a35d7b3af6dce6ec42a60db2df605dd7 [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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Active Record Class
32 *
33 * This is the platform-independent base Active Record implementation class.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_active_record extends CI_DB_driver {
42
Kyle Farris76116012011-08-31 11:17:48 -040043 protected $return_delete_sql = FALSE;
44 protected $reset_delete_data = FALSE;
Kyle Farris0c147b32011-08-26 02:29:31 -040045
Kyle Farris76116012011-08-31 11:17:48 -040046 protected $ar_select = array();
47 protected $ar_distinct = FALSE;
48 protected $ar_from = array();
49 protected $ar_join = array();
50 protected $ar_where = array();
51 protected $ar_like = array();
52 protected $ar_groupby = array();
53 protected $ar_having = array();
54 protected $ar_keys = array();
55 protected $ar_limit = FALSE;
56 protected $ar_offset = FALSE;
57 protected $ar_order = FALSE;
58 protected $ar_orderby = array();
59 protected $ar_set = array();
60 protected $ar_wherein = array();
61 protected $ar_aliased_tables = array();
62 protected $ar_store_array = array();
Barry Mienydd671972010-10-04 16:33:58 +020063
Derek Allard2067d1a2008-11-13 22:59:24 +000064 // Active Record Caching variables
Kyle Farris76116012011-08-31 11:17:48 -040065 protected $ar_caching = FALSE;
66 protected $ar_cache_exists = array();
67 protected $ar_cache_select = array();
68 protected $ar_cache_from = array();
69 protected $ar_cache_join = array();
70 protected $ar_cache_where = array();
71 protected $ar_cache_like = array();
72 protected $ar_cache_groupby = array();
73 protected $ar_cache_having = array();
74 protected $ar_cache_orderby = array();
75 protected $ar_cache_set = array();
Derek Jones37f4b9c2011-07-01 17:56:50 -050076
Kyle Farris76116012011-08-31 11:17:48 -040077 protected $ar_no_escape = array();
78 protected $ar_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000079
80 // --------------------------------------------------------------------
81
82 /**
83 * Select
84 *
85 * Generates the SELECT portion of the query
86 *
Derek Allard2067d1a2008-11-13 22:59:24 +000087 * @param string
88 * @return object
89 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060090 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Derek Allard2067d1a2008-11-13 22:59:24 +000092 if (is_string($select))
93 {
94 $select = explode(',', $select);
95 }
96
97 foreach ($select as $val)
98 {
99 $val = trim($val);
100
101 if ($val != '')
102 {
103 $this->ar_select[] = $val;
Greg Akere156c6e2011-04-20 16:03:04 -0500104 $this->ar_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000105
106 if ($this->ar_caching === TRUE)
107 {
108 $this->ar_cache_select[] = $val;
109 $this->ar_cache_exists[] = 'select';
Greg Aker2e1837a2011-05-06 12:17:04 -0500110 $this->ar_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
112 }
113 }
114 return $this;
115 }
116
117 // --------------------------------------------------------------------
118
119 /**
120 * Select Max
121 *
122 * Generates a SELECT MAX(field) portion of a query
123 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 * @param string the field
125 * @param string an alias
126 * @return object
127 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600128 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
130 return $this->_max_min_avg_sum($select, $alias, 'MAX');
131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 // --------------------------------------------------------------------
134
135 /**
136 * Select Min
137 *
138 * Generates a SELECT MIN(field) portion of a query
139 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 * @param string the field
141 * @param string an alias
142 * @return object
143 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600144 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
146 return $this->_max_min_avg_sum($select, $alias, 'MIN');
147 }
148
149 // --------------------------------------------------------------------
150
151 /**
152 * Select Average
153 *
154 * Generates a SELECT AVG(field) portion of a query
155 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 * @param string the field
157 * @param string an alias
158 * @return object
159 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600160 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
162 return $this->_max_min_avg_sum($select, $alias, 'AVG');
163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Select Sum
169 *
170 * Generates a SELECT SUM(field) portion of a query
171 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 * @param string the field
173 * @param string an alias
174 * @return object
175 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600176 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
178 return $this->_max_min_avg_sum($select, $alias, 'SUM');
179 }
180
181 // --------------------------------------------------------------------
182
183 /**
184 * Processing Function for the four functions above:
185 *
186 * select_max()
187 * select_min()
188 * select_avg()
Derek Jones37f4b9c2011-07-01 17:56:50 -0500189 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200190 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 * @param string the field
192 * @param string an alias
193 * @return object
194 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600195 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
197 if ( ! is_string($select) OR $select == '')
198 {
199 $this->display_error('db_invalid_query');
200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
205 {
206 show_error('Invalid function type: '.$type);
207 }
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 if ($alias == '')
210 {
211 $alias = $this->_create_alias_from_table(trim($select));
212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Juan José González8b4d83b2011-09-27 17:21:14 -0500214 $sql = $this->_protect_identifiers($type.'('.trim($select).')').' AS '.$this->_protect_identifiers(trim($alias));
Derek Allard2067d1a2008-11-13 22:59:24 +0000215
216 $this->ar_select[] = $sql;
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 if ($this->ar_caching === TRUE)
219 {
220 $this->ar_cache_select[] = $sql;
221 $this->ar_cache_exists[] = 'select';
222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 return $this;
225 }
226
227 // --------------------------------------------------------------------
228
229 /**
230 * Determines the alias name based on the table
231 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * @param string
233 * @return string
234 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600235 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
237 if (strpos($item, '.') !== FALSE)
238 {
239 return end(explode('.', $item));
240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 return $item;
243 }
244
245 // --------------------------------------------------------------------
246
247 /**
248 * DISTINCT
249 *
250 * Sets a flag which tells the query string compiler to add DISTINCT
251 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 * @param bool
253 * @return object
254 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600255 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
257 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
258 return $this;
259 }
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 // --------------------------------------------------------------------
262
263 /**
264 * From
265 *
266 * Generates the FROM portion of the query
267 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 * @param mixed can be a string or array
269 * @return object
270 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600271 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
273 foreach ((array)$from as $val)
274 {
275 if (strpos($val, ',') !== FALSE)
276 {
277 foreach (explode(',', $val) as $v)
278 {
279 $v = trim($v);
280 $this->_track_aliases($v);
281
282 $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 if ($this->ar_caching === TRUE)
285 {
286 $this->ar_cache_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
287 $this->ar_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200288 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
290
291 }
292 else
293 {
294 $val = trim($val);
295
Derek Jones37f4b9c2011-07-01 17:56:50 -0500296 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200297 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 if ($this->ar_caching === TRUE)
303 {
304 $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
305 $this->ar_cache_exists[] = 'from';
306 }
307 }
308 }
309
310 return $this;
311 }
312
313 // --------------------------------------------------------------------
314
315 /**
316 * Join
317 *
318 * Generates the JOIN portion of the query
319 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 * @param string
321 * @param string the join condition
322 * @param string the type of join
323 * @return object
324 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600325 public function join($table, $cond, $type = '')
Barry Mienydd671972010-10-04 16:33:58 +0200326 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 if ($type != '')
328 {
329 $type = strtoupper(trim($type));
330
331 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
332 {
333 $type = '';
334 }
335 else
336 {
337 $type .= ' ';
338 }
339 }
340
Derek Jones37f4b9c2011-07-01 17:56:50 -0500341 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200342 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 $this->_track_aliases($table);
344
345 // Strip apart the condition and protect the identifiers
346 if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
347 {
348 $match[1] = $this->_protect_identifiers($match[1]);
349 $match[3] = $this->_protect_identifiers($match[3]);
Barry Mienydd671972010-10-04 16:33:58 +0200350
351 $cond = $match[1].$match[2].$match[3];
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 }
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 // Assemble the JOIN statement
355 $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
356
357 $this->ar_join[] = $join;
358 if ($this->ar_caching === TRUE)
359 {
360 $this->ar_cache_join[] = $join;
361 $this->ar_cache_exists[] = 'join';
362 }
363
364 return $this;
365 }
366
367 // --------------------------------------------------------------------
368
369 /**
370 * Where
371 *
372 * Generates the WHERE portion of the query. Separates
373 * multiple calls with AND
374 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 * @param mixed
376 * @param mixed
377 * @return object
378 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600379 public function where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
381 return $this->_where($key, $value, 'AND ', $escape);
382 }
Barry Mienydd671972010-10-04 16:33:58 +0200383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 // --------------------------------------------------------------------
385
386 /**
387 * OR Where
388 *
389 * Generates the WHERE portion of the query. Separates
390 * multiple calls with OR
391 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 * @param mixed
393 * @param mixed
394 * @return object
395 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600396 public function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
398 return $this->_where($key, $value, 'OR ', $escape);
399 }
400
401 // --------------------------------------------------------------------
402
403 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 * Where
405 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600406 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 * @param mixed
409 * @param mixed
410 * @param string
411 * @return object
412 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600413 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
415 if ( ! is_array($key))
416 {
417 $key = array($key => $value);
418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 // If the escape value was not set will will base it on the global setting
421 if ( ! is_bool($escape))
422 {
423 $escape = $this->_protect_identifiers;
424 }
425
426 foreach ($key as $k => $v)
427 {
428 $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type;
429
430 if (is_null($v) && ! $this->_has_operator($k))
431 {
432 // value appears not to have been set, assign the test to IS NULL
433 $k .= ' IS NULL';
434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 if ( ! is_null($v))
437 {
438 if ($escape === TRUE)
439 {
440 $k = $this->_protect_identifiers($k, FALSE, $escape);
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 $v = ' '.$this->escape($v);
443 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 if ( ! $this->_has_operator($k))
446 {
Greg Akere156c6e2011-04-20 16:03:04 -0500447 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
449 }
450 else
451 {
Barry Mienydd671972010-10-04 16:33:58 +0200452 $k = $this->_protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 }
454
455 $this->ar_where[] = $prefix.$k.$v;
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 if ($this->ar_caching === TRUE)
458 {
459 $this->ar_cache_where[] = $prefix.$k.$v;
460 $this->ar_cache_exists[] = 'where';
461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 }
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 return $this;
466 }
467
468 // --------------------------------------------------------------------
469
470 /**
471 * Where_in
472 *
473 * Generates a WHERE field IN ('item', 'item') SQL query joined with
474 * AND if appropriate
475 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 * @param string The field to search
477 * @param array The values searched on
478 * @return object
479 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600480 public function where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
482 return $this->_where_in($key, $values);
483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 // --------------------------------------------------------------------
486
487 /**
488 * Where_in_or
489 *
490 * Generates a WHERE field IN ('item', 'item') SQL query joined with
491 * OR if appropriate
492 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 * @param string The field to search
494 * @param array The values searched on
495 * @return object
496 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600497 public function or_where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
499 return $this->_where_in($key, $values, FALSE, 'OR ');
500 }
501
502 // --------------------------------------------------------------------
503
504 /**
505 * Where_not_in
506 *
507 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
508 * with AND if appropriate
509 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 * @param string The field to search
511 * @param array The values searched on
512 * @return object
513 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600514 public function where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
516 return $this->_where_in($key, $values, TRUE);
517 }
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 // --------------------------------------------------------------------
520
521 /**
522 * Where_not_in_or
523 *
524 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
525 * with OR if appropriate
526 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 * @param string The field to search
528 * @param array The values searched on
529 * @return object
530 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600531 public function or_where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 {
533 return $this->_where_in($key, $values, TRUE, 'OR ');
534 }
535
536 // --------------------------------------------------------------------
537
538 /**
539 * Where_in
540 *
541 * Called by where_in, where_in_or, where_not_in, where_not_in_or
542 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 * @param string The field to search
544 * @param array The values searched on
545 * @param boolean If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200546 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 * @return object
548 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600549 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 {
551 if ($key === NULL OR $values === NULL)
552 {
553 return;
554 }
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 if ( ! is_array($values))
557 {
558 $values = array($values);
559 }
Barry Mienydd671972010-10-04 16:33:58 +0200560
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 $not = ($not) ? ' NOT' : '';
562
563 foreach ($values as $value)
564 {
565 $this->ar_wherein[] = $this->escape($value);
566 }
567
568 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Barry Mienydd671972010-10-04 16:33:58 +0200569
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
571
572 $this->ar_where[] = $where_in;
573 if ($this->ar_caching === TRUE)
574 {
575 $this->ar_cache_where[] = $where_in;
576 $this->ar_cache_exists[] = 'where';
577 }
578
579 // reset the array for multiple calls
580 $this->ar_wherein = array();
581 return $this;
582 }
Barry Mienydd671972010-10-04 16:33:58 +0200583
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 // --------------------------------------------------------------------
585
586 /**
587 * Like
588 *
589 * Generates a %LIKE% portion of the query. Separates
590 * multiple calls with AND
591 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 * @param mixed
593 * @param mixed
594 * @return object
595 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600596 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 {
598 return $this->_like($field, $match, 'AND ', $side);
599 }
600
601 // --------------------------------------------------------------------
602
603 /**
604 * Not Like
605 *
606 * Generates a NOT LIKE portion of the query. Separates
607 * multiple calls with AND
608 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 * @param mixed
610 * @param mixed
611 * @return object
612 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600613 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 {
615 return $this->_like($field, $match, 'AND ', $side, 'NOT');
616 }
Barry Mienydd671972010-10-04 16:33:58 +0200617
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 // --------------------------------------------------------------------
619
620 /**
621 * OR Like
622 *
623 * Generates a %LIKE% portion of the query. Separates
624 * multiple calls with OR
625 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 * @param mixed
627 * @param mixed
628 * @return object
629 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600630 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 {
632 return $this->_like($field, $match, 'OR ', $side);
633 }
634
635 // --------------------------------------------------------------------
636
637 /**
638 * OR Not Like
639 *
640 * Generates a NOT LIKE portion of the query. Separates
641 * multiple calls with OR
642 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 * @param mixed
644 * @param mixed
645 * @return object
646 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600647 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 {
649 return $this->_like($field, $match, 'OR ', $side, 'NOT');
650 }
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 // --------------------------------------------------------------------
653
654 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 * Like
656 *
657 * Called by like() or orlike()
658 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 * @param mixed
660 * @param mixed
661 * @param string
662 * @return object
663 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600664 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 {
666 if ( ! is_array($field))
667 {
668 $field = array($field => $match);
669 }
Barry Mienydd671972010-10-04 16:33:58 +0200670
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 foreach ($field as $k => $v)
672 {
673 $k = $this->_protect_identifiers($k);
674
675 $prefix = (count($this->ar_like) == 0) ? '' : $type;
676
Derek Jonese4ed5832009-02-20 21:44:59 +0000677 $v = $this->escape_like_str($v);
Kyle Farris0c147b32011-08-26 02:29:31 -0400678
Kyle Farris81ef70f2011-08-31 11:59:12 -0400679 if ($side == 'none')
680 {
681 $like_statement = $prefix." $k $not LIKE '{$v}'";
682 }
683 elseif ($side == 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 {
685 $like_statement = $prefix." $k $not LIKE '%{$v}'";
686 }
687 elseif ($side == 'after')
688 {
689 $like_statement = $prefix." $k $not LIKE '{$v}%'";
690 }
691 else
692 {
693 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
694 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600695
Derek Jonese4ed5832009-02-20 21:44:59 +0000696 // some platforms require an escape sequence definition for LIKE wildcards
697 if ($this->_like_escape_str != '')
698 {
Greg Aker0d424892010-01-26 02:14:44 +0000699 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000700 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600701
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 $this->ar_like[] = $like_statement;
703 if ($this->ar_caching === TRUE)
704 {
705 $this->ar_cache_like[] = $like_statement;
706 $this->ar_cache_exists[] = 'like';
707 }
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 }
710 return $this;
711 }
Barry Mienydd671972010-10-04 16:33:58 +0200712
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 // --------------------------------------------------------------------
714
715 /**
716 * GROUP BY
717 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 * @param string
719 * @return object
720 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600721 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 {
723 if (is_string($by))
724 {
725 $by = explode(',', $by);
726 }
Barry Mienydd671972010-10-04 16:33:58 +0200727
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 foreach ($by as $val)
729 {
730 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200731
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 if ($val != '')
733 {
734 $this->ar_groupby[] = $this->_protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200735
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 if ($this->ar_caching === TRUE)
737 {
738 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
739 $this->ar_cache_exists[] = 'groupby';
740 }
741 }
742 }
743 return $this;
744 }
745
746 // --------------------------------------------------------------------
747
748 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 * Sets the HAVING value
750 *
751 * Separates multiple calls with AND
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 having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 {
759 return $this->_having($key, $value, 'AND ', $escape);
760 }
Barry Mienydd671972010-10-04 16:33:58 +0200761
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 // --------------------------------------------------------------------
763
764 /**
765 * Sets the OR HAVING value
766 *
767 * Separates multiple calls with OR
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 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 {
775 return $this->_having($key, $value, 'OR ', $escape);
776 }
Barry Mienydd671972010-10-04 16:33:58 +0200777
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 // --------------------------------------------------------------------
779
780 /**
781 * Sets the HAVING values
782 *
783 * Called by having() or or_having()
784 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 * @param string
786 * @param string
787 * @return object
788 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600789 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 {
791 if ( ! is_array($key))
792 {
793 $key = array($key => $value);
794 }
Barry Mienydd671972010-10-04 16:33:58 +0200795
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 foreach ($key as $k => $v)
797 {
798 $prefix = (count($this->ar_having) == 0) ? '' : $type;
799
800 if ($escape === TRUE)
801 {
802 $k = $this->_protect_identifiers($k);
803 }
804
805 if ( ! $this->_has_operator($k))
806 {
807 $k .= ' = ';
808 }
809
810 if ($v != '')
811 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400812 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 }
Barry Mienydd671972010-10-04 16:33:58 +0200814
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 $this->ar_having[] = $prefix.$k.$v;
816 if ($this->ar_caching === TRUE)
817 {
818 $this->ar_cache_having[] = $prefix.$k.$v;
819 $this->ar_cache_exists[] = 'having';
820 }
821 }
Barry Mienydd671972010-10-04 16:33:58 +0200822
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 return $this;
824 }
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 // --------------------------------------------------------------------
827
828 /**
829 * Sets the ORDER BY value
830 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 * @param string
832 * @param string direction: asc or desc
833 * @return object
834 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600835 public function order_by($orderby, $direction = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 {
837 if (strtolower($direction) == 'random')
838 {
839 $orderby = ''; // Random results want or don't need a field name
840 $direction = $this->_random_keyword;
841 }
842 elseif (trim($direction) != '')
843 {
844 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
845 }
Barry Mienydd671972010-10-04 16:33:58 +0200846
847
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 if (strpos($orderby, ',') !== FALSE)
849 {
850 $temp = array();
851 foreach (explode(',', $orderby) as $part)
852 {
853 $part = trim($part);
854 if ( ! in_array($part, $this->ar_aliased_tables))
855 {
856 $part = $this->_protect_identifiers(trim($part));
857 }
Barry Mienydd671972010-10-04 16:33:58 +0200858
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 $temp[] = $part;
860 }
Barry Mienydd671972010-10-04 16:33:58 +0200861
862 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 }
Derek Allarde37ab382009-02-03 16:13:57 +0000864 else if ($direction != $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 {
866 $orderby = $this->_protect_identifiers($orderby);
867 }
Barry Mienydd671972010-10-04 16:33:58 +0200868
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200870
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 $this->ar_orderby[] = $orderby_statement;
872 if ($this->ar_caching === TRUE)
873 {
874 $this->ar_cache_orderby[] = $orderby_statement;
875 $this->ar_cache_exists[] = 'orderby';
876 }
877
878 return $this;
879 }
Barry Mienydd671972010-10-04 16:33:58 +0200880
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 // --------------------------------------------------------------------
882
883 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 * Sets the LIMIT value
885 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 * @param integer the limit value
887 * @param integer the offset value
888 * @return object
889 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200890 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 {
Kyle Farris76116012011-08-31 11:17:48 -0400892 $this->ar_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000893
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200894 if ( ! is_null($offset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 {
Kyle Farris76116012011-08-31 11:17:48 -0400896 $this->ar_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 }
Barry Mienydd671972010-10-04 16:33:58 +0200898
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 return $this;
900 }
Barry Mienydd671972010-10-04 16:33:58 +0200901
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 // --------------------------------------------------------------------
903
904 /**
905 * Sets the OFFSET value
906 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 * @param integer the offset value
908 * @return object
909 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600910 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 {
kenjis0e857632011-09-02 08:41:17 +0900912 $this->ar_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 return $this;
914 }
Barry Mienydd671972010-10-04 16:33:58 +0200915
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 // --------------------------------------------------------------------
917
918 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500919 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 * @param mixed
922 * @param string
923 * @param boolean
924 * @return object
925 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600926 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 {
928 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +0200929
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 if ( ! is_array($key))
931 {
932 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +0200933 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000934
935 foreach ($key as $k => $v)
936 {
937 if ($escape === FALSE)
938 {
939 $this->ar_set[$this->_protect_identifiers($k)] = $v;
940 }
941 else
942 {
Phil Sturgeond0ac1a22011-02-15 22:54:08 +0000943 $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 }
945 }
Barry Mienydd671972010-10-04 16:33:58 +0200946
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 return $this;
948 }
Kyle Farris0c147b32011-08-26 02:29:31 -0400949
Kyle Farris0c147b32011-08-26 02:29:31 -0400950 // --------------------------------------------------------------------
951
952 /**
953 * Get SELECT query string
954 *
955 * Compiles a SELECT query string and returns the sql.
956 *
957 * @access public
958 * @param string the table name to select from (optional)
959 * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone
960 * @return string
961 */
962 public function get_compiled_select($table = '', $reset = TRUE)
963 {
964 if ($table != '')
kylefarris0a3176b2011-08-26 02:37:52 -0400965 {
Kyle Farris0c147b32011-08-26 02:29:31 -0400966 $this->_track_aliases($table);
967 $this->from($table);
968 }
969
970 $select = $this->_compile_select();
971
972 if ($reset === TRUE)
973 {
974 $this->_reset_select();
975 }
976
977 return $select;
978 }
979
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 // --------------------------------------------------------------------
981
982 /**
983 * Get
984 *
985 * Compiles the select statement based on the other functions called
986 * and runs the query
987 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 * @param string the table
989 * @param string the limit clause
990 * @param string the offset clause
991 * @return object
992 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600993 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 {
995 if ($table != '')
996 {
997 $this->_track_aliases($table);
998 $this->from($table);
999 }
Barry Mienydd671972010-10-04 16:33:58 +02001000
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 if ( ! is_null($limit))
1002 {
1003 $this->limit($limit, $offset);
1004 }
Barry Mienydd671972010-10-04 16:33:58 +02001005
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 $sql = $this->_compile_select();
1007
1008 $result = $this->query($sql);
1009 $this->_reset_select();
1010 return $result;
1011 }
1012
1013 /**
1014 * "Count All Results" query
1015 *
Barry Mienydd671972010-10-04 16:33:58 +02001016 * Generates a platform-specific query string that counts all records
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 * returned by an Active Record query.
1018 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 * @param string
1020 * @return string
1021 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001022 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 {
1024 if ($table != '')
1025 {
1026 $this->_track_aliases($table);
1027 $this->from($table);
1028 }
Barry Mienydd671972010-10-04 16:33:58 +02001029
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
1031
1032 $query = $this->query($sql);
1033 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001034
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 if ($query->num_rows() == 0)
1036 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001037 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 }
1039
1040 $row = $query->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001041 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 }
1043
1044 // --------------------------------------------------------------------
1045
1046 /**
1047 * Get_Where
1048 *
1049 * Allows the where clause, limit and offset to be added directly
1050 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 * @param string the where clause
1052 * @param string the limit clause
1053 * @param string the offset clause
1054 * @return object
1055 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001056 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 {
1058 if ($table != '')
1059 {
1060 $this->from($table);
1061 }
1062
1063 if ( ! is_null($where))
1064 {
1065 $this->where($where);
1066 }
Barry Mienydd671972010-10-04 16:33:58 +02001067
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 if ( ! is_null($limit))
1069 {
1070 $this->limit($limit, $offset);
1071 }
Barry Mienydd671972010-10-04 16:33:58 +02001072
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 $sql = $this->_compile_select();
1074
1075 $result = $this->query($sql);
1076 $this->_reset_select();
1077 return $result;
1078 }
1079
1080 // --------------------------------------------------------------------
1081
1082 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001083 * Insert_Batch
1084 *
1085 * Compiles batch insert strings and runs the queries
1086 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001087 * @param string the table to retrieve the results from
1088 * @param array an associative array of insert values
1089 * @return object
1090 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001091 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001092 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001093 if ( ! is_null($set))
1094 {
1095 $this->set_insert_batch($set);
1096 }
Barry Mienydd671972010-10-04 16:33:58 +02001097
Derek Jonesd10e8962010-03-02 17:10:36 -06001098 if (count($this->ar_set) == 0)
1099 {
1100 if ($this->db_debug)
1101 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001102 //No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001103 return $this->display_error('db_must_use_set');
1104 }
1105 return FALSE;
1106 }
1107
1108 if ($table == '')
1109 {
1110 if ( ! isset($this->ar_from[0]))
1111 {
1112 if ($this->db_debug)
1113 {
1114 return $this->display_error('db_must_set_table');
1115 }
1116 return FALSE;
1117 }
Barry Mienydd671972010-10-04 16:33:58 +02001118
Derek Jonesd10e8962010-03-02 17:10:36 -06001119 $table = $this->ar_from[0];
1120 }
1121
1122 // Batch this baby
1123 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1124 {
Barry Mienydd671972010-10-04 16:33:58 +02001125
Derek Jonesd10e8962010-03-02 17:10:36 -06001126 $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
1127
1128 //echo $sql;
1129
1130 $this->query($sql);
1131 }
Barry Mienydd671972010-10-04 16:33:58 +02001132
Derek Jonesd10e8962010-03-02 17:10:36 -06001133 $this->_reset_write();
1134
1135
Barry Mienydd671972010-10-04 16:33:58 +02001136 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001137 }
1138
1139 // --------------------------------------------------------------------
1140
1141 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001142 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001143 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001144 * @param mixed
1145 * @param string
1146 * @param boolean
1147 * @return object
1148 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001149 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001150 {
1151 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001152
Derek Jonesd10e8962010-03-02 17:10:36 -06001153 if ( ! is_array($key))
1154 {
1155 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001156 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001157
1158 $keys = array_keys(current($key));
1159 sort($keys);
1160
1161 foreach ($key as $row)
1162 {
Barry Mienydd671972010-10-04 16:33:58 +02001163 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1164 {
1165 // batch function above returns an error on an empty array
1166 $this->ar_set[] = array();
1167 return;
1168 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001169
Barry Mienydd671972010-10-04 16:33:58 +02001170 ksort($row); // puts $row in the same order as our keys
1171
Derek Jonesd10e8962010-03-02 17:10:36 -06001172 if ($escape === FALSE)
1173 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001174 $this->ar_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001175 }
1176 else
1177 {
1178 $clean = array();
1179
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001180 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001181 {
Barry Mienydd671972010-10-04 16:33:58 +02001182 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001183 }
1184
Derek Jones37f4b9c2011-07-01 17:56:50 -05001185 $this->ar_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001186 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001187 }
1188
1189 foreach ($keys as $k)
1190 {
1191 $this->ar_keys[] = $this->_protect_identifiers($k);
1192 }
Barry Mienydd671972010-10-04 16:33:58 +02001193
Derek Jonesd10e8962010-03-02 17:10:36 -06001194 return $this;
1195 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001196
1197 // --------------------------------------------------------------------
1198
1199 /**
1200 * Get INSERT query string
1201 *
1202 * Compiles an insert query and returns the sql
1203 *
1204 * @access public
1205 * @param string the table to insert into
1206 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1207 * @return string
1208 */
1209 public function get_compiled_insert($table = '', $reset = TRUE)
1210 {
1211 if ($this->_validate_insert($table) === FALSE)
1212 {
1213 return FALSE;
1214 }
1215
Kyle Farris76116012011-08-31 11:17:48 -04001216 $sql = $this->_insert(
1217 $this->_protect_identifiers(
1218 $this->ar_from[0], TRUE, NULL, FALSE
1219 ),
1220 array_keys($this->ar_set),
1221 array_values($this->ar_set)
1222 );
Kyle Farris0c147b32011-08-26 02:29:31 -04001223
1224 if ($reset === TRUE)
1225 {
1226 $this->_reset_write();
1227 }
1228
1229 return $sql;
1230 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001231
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 // --------------------------------------------------------------------
1233
1234 /**
1235 * Insert
1236 *
1237 * Compiles an insert string and runs the query
1238 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001239 * @access public
Phil Sturgeon9789f322011-07-15 15:14:05 -06001240 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 * @param array an associative array of insert values
1242 * @return object
1243 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001244 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001245 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 if ( ! is_null($set))
1247 {
1248 $this->set($set);
1249 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001250
1251 if ($this->_validate_insert($table) === FALSE)
1252 {
1253 return FALSE;
1254 }
1255
Kyle Farris76116012011-08-31 11:17:48 -04001256 $sql = $this->_insert(
1257 $this->_protect_identifiers(
1258 $this->ar_from[0], TRUE, NULL, FALSE
1259 ),
1260 array_keys($this->ar_set),
1261 array_values($this->ar_set)
1262 );
Barry Mienydd671972010-10-04 16:33:58 +02001263
Kyle Farris0c147b32011-08-26 02:29:31 -04001264 $this->_reset_write();
1265 return $this->query($sql);
1266 }
1267
Kyle Farris0c147b32011-08-26 02:29:31 -04001268 // --------------------------------------------------------------------
1269
1270 /**
1271 * Validate Insert
1272 *
1273 * This method is used by both insert() and get_compiled_insert() to
1274 * validate that the there data is actually being set and that table
1275 * has been chosen to be inserted into.
1276 *
1277 * @access public
1278 * @param string the table to insert data into
1279 * @return string
1280 */
1281 protected function _validate_insert($table = '')
1282 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 if (count($this->ar_set) == 0)
1284 {
1285 if ($this->db_debug)
1286 {
1287 return $this->display_error('db_must_use_set');
1288 }
1289 return FALSE;
1290 }
1291
1292 if ($table == '')
1293 {
1294 if ( ! isset($this->ar_from[0]))
1295 {
1296 if ($this->db_debug)
1297 {
1298 return $this->display_error('db_must_set_table');
1299 }
1300 return FALSE;
1301 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001303 else
1304 {
1305 $this->ar_from[0] = $table;
1306 }
1307
1308 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 }
Barry Mienydd671972010-10-04 16:33:58 +02001310
Phil Sturgeon9789f322011-07-15 15:14:05 -06001311 // --------------------------------------------------------------------
1312
1313 /**
1314 * Replace
1315 *
1316 * Compiles an replace into string and runs the query
1317 *
1318 * @param string the table to replace data into
1319 * @param array an associative array of insert values
1320 * @return object
1321 */
1322 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001323 {
1324 if ( ! is_null($set))
1325 {
1326 $this->set($set);
1327 }
Barry Mienydd671972010-10-04 16:33:58 +02001328
Derek Jonesd10e8962010-03-02 17:10:36 -06001329 if (count($this->ar_set) == 0)
1330 {
1331 if ($this->db_debug)
1332 {
1333 return $this->display_error('db_must_use_set');
1334 }
1335 return FALSE;
1336 }
1337
1338 if ($table == '')
1339 {
1340 if ( ! isset($this->ar_from[0]))
1341 {
1342 if ($this->db_debug)
1343 {
1344 return $this->display_error('db_must_set_table');
1345 }
1346 return FALSE;
1347 }
Barry Mienydd671972010-10-04 16:33:58 +02001348
Derek Jonesd10e8962010-03-02 17:10:36 -06001349 $table = $this->ar_from[0];
1350 }
1351
1352 $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 +02001353
Derek Jonesd10e8962010-03-02 17:10:36 -06001354 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001355 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001356 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001357
Kyle Farris0c147b32011-08-26 02:29:31 -04001358 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001359
Kyle Farris0c147b32011-08-26 02:29:31 -04001360 /**
1361 * Get UPDATE query string
1362 *
1363 * Compiles an update query and returns the sql
1364 *
1365 * @access public
1366 * @param string the table to update
1367 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1368 * @return string
1369 */
1370 public function get_compiled_update($table = '', $reset = TRUE)
1371 {
1372 // Combine any cached components with the current statements
1373 $this->_merge_cache();
1374
1375 if ($this->_validate_update($table) === FALSE)
1376 {
1377 return FALSE;
1378 }
1379
1380 $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);
1381
1382 if ($reset === TRUE)
1383 {
1384 $this->_reset_write();
1385 }
1386
1387 return $sql;
1388 }
1389
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 // --------------------------------------------------------------------
1391
1392 /**
1393 * Update
1394 *
1395 * Compiles an update string and runs the query
1396 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001397 * @param string the table to retrieve the results from
1398 * @param array an associative array of update values
1399 * @param mixed the where clause
1400 * @return object
1401 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001402 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001403 {
1404 // Combine any cached components with the current statements
1405 $this->_merge_cache();
1406
1407 if ( ! is_null($set))
1408 {
1409 $this->set($set);
1410 }
Barry Mienydd671972010-10-04 16:33:58 +02001411
Kyle Farris0c147b32011-08-26 02:29:31 -04001412 if ($this->_validate_update($table) === FALSE)
1413 {
1414 return FALSE;
1415 }
1416
1417 if ($where != NULL)
1418 {
1419 $this->where($where);
1420 }
1421
1422 if ($limit != NULL)
1423 {
1424 $this->limit($limit);
1425 }
1426
1427 $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);
1428
1429 $this->_reset_write();
1430 return $this->query($sql);
1431 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001432
1433 // --------------------------------------------------------------------
1434
1435 /**
1436 * Validate Update
1437 *
1438 * This method is used by both update() and get_compiled_update() to
1439 * validate that data is actually being set and that a table has been
1440 * chosen to be update.
1441 *
1442 * @access public
1443 * @param string the table to update data on
1444 * @return string
1445 */
1446 protected function _validate_update($table = '')
1447 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 if (count($this->ar_set) == 0)
1449 {
1450 if ($this->db_debug)
1451 {
1452 return $this->display_error('db_must_use_set');
1453 }
1454 return FALSE;
1455 }
1456
1457 if ($table == '')
1458 {
1459 if ( ! isset($this->ar_from[0]))
1460 {
1461 if ($this->db_debug)
1462 {
1463 return $this->display_error('db_must_set_table');
1464 }
1465 return FALSE;
1466 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001467 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001468 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001469 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001470 $this->ar_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001471 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001472 }
Kyle Farris76116012011-08-31 11:17:48 -04001473
Derek Jonesd10e8962010-03-02 17:10:36 -06001474 // --------------------------------------------------------------------
1475
1476 /**
1477 * Update_Batch
1478 *
1479 * Compiles an update string and runs the query
1480 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001481 * @param string the table to retrieve the results from
1482 * @param array an associative array of update values
1483 * @param string the where key
1484 * @return object
1485 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001486 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001487 {
1488 // Combine any cached components with the current statements
1489 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001490
Derek Jonesd10e8962010-03-02 17:10:36 -06001491 if (is_null($index))
1492 {
1493 if ($this->db_debug)
1494 {
Kyle Farris2de2fa02011-08-31 11:52:20 -04001495 return $this->display_error('db_must_use_index');
Derek Jonesd10e8962010-03-02 17:10:36 -06001496 }
1497
1498 return FALSE;
1499 }
1500
1501 if ( ! is_null($set))
1502 {
1503 $this->set_update_batch($set, $index);
1504 }
1505
1506 if (count($this->ar_set) == 0)
1507 {
1508 if ($this->db_debug)
1509 {
1510 return $this->display_error('db_must_use_set');
1511 }
1512
1513 return FALSE;
1514 }
1515
1516 if ($table == '')
1517 {
1518 if ( ! isset($this->ar_from[0]))
1519 {
1520 if ($this->db_debug)
1521 {
1522 return $this->display_error('db_must_set_table');
1523 }
1524 return FALSE;
1525 }
Barry Mienydd671972010-10-04 16:33:58 +02001526
Derek Jonesd10e8962010-03-02 17:10:36 -06001527 $table = $this->ar_from[0];
1528 }
Barry Mienydd671972010-10-04 16:33:58 +02001529
Derek Jonesd10e8962010-03-02 17:10:36 -06001530 // Batch this baby
1531 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1532 {
1533 $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);
1534
1535 $this->query($sql);
1536 }
Barry Mienydd671972010-10-04 16:33:58 +02001537
Derek Jonesd10e8962010-03-02 17:10:36 -06001538 $this->_reset_write();
1539 }
1540
1541 // --------------------------------------------------------------------
1542
1543 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001544 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001545 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001546 * @param array
1547 * @param string
1548 * @param boolean
1549 * @return object
1550 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001551 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001552 {
1553 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001554
Derek Jonesd10e8962010-03-02 17:10:36 -06001555 if ( ! is_array($key))
1556 {
1557 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001558 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001559
1560 foreach ($key as $k => $v)
1561 {
1562 $index_set = FALSE;
1563 $clean = array();
1564
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001565 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001566 {
1567 if ($k2 == $index)
1568 {
1569 $index_set = TRUE;
1570 }
1571 else
1572 {
1573 $not[] = $k.'-'.$v;
1574 }
1575
1576 if ($escape === FALSE)
1577 {
1578 $clean[$this->_protect_identifiers($k2)] = $v2;
1579 }
1580 else
1581 {
Barry Mienydd671972010-10-04 16:33:58 +02001582 $clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001583 }
1584 }
1585
1586 if ($index_set == FALSE)
1587 {
1588 return $this->display_error('db_batch_missing_index');
1589 }
1590
1591 $this->ar_set[] = $clean;
1592 }
Barry Mienydd671972010-10-04 16:33:58 +02001593
Derek Jonesd10e8962010-03-02 17:10:36 -06001594 return $this;
1595 }
1596
Derek Allard2067d1a2008-11-13 22:59:24 +00001597 // --------------------------------------------------------------------
1598
1599 /**
1600 * Empty Table
1601 *
1602 * Compiles a delete string and runs "DELETE FROM table"
1603 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001604 * @param string the table to empty
1605 * @return object
1606 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001607 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001608 {
1609 if ($table == '')
1610 {
1611 if ( ! isset($this->ar_from[0]))
1612 {
1613 if ($this->db_debug)
1614 {
1615 return $this->display_error('db_must_set_table');
1616 }
1617 return FALSE;
1618 }
1619
1620 $table = $this->ar_from[0];
1621 }
1622 else
1623 {
1624 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1625 }
1626
1627 $sql = $this->_delete($table);
1628
1629 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001630
Derek Allard2067d1a2008-11-13 22:59:24 +00001631 return $this->query($sql);
1632 }
1633
1634 // --------------------------------------------------------------------
1635
1636 /**
1637 * Truncate
1638 *
1639 * Compiles a truncate string and runs the query
1640 * If the database does not support the truncate() command
1641 * This function maps to "DELETE FROM table"
1642 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001643 * @param string the table to truncate
1644 * @return object
1645 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001646 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001647 {
1648 if ($table == '')
1649 {
1650 if ( ! isset($this->ar_from[0]))
1651 {
1652 if ($this->db_debug)
1653 {
1654 return $this->display_error('db_must_set_table');
1655 }
1656 return FALSE;
1657 }
1658
1659 $table = $this->ar_from[0];
1660 }
1661 else
1662 {
1663 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1664 }
1665
1666 $sql = $this->_truncate($table);
1667
1668 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001669
Derek Allard2067d1a2008-11-13 22:59:24 +00001670 return $this->query($sql);
1671 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001672
1673 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001674
Kyle Farris0c147b32011-08-26 02:29:31 -04001675 /**
1676 * Get DELETE query string
1677 *
1678 * Compiles a delete query string and returns the sql
1679 *
1680 * @access public
1681 * @param string the table to delete from
1682 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1683 * @return string
1684 */
1685 public function get_compiled_delete($table = '', $reset = TRUE)
1686 {
1687 $this->return_delete_sql = TRUE;
1688 $sql = $this->delete($table, '', NULL, $reset);
1689 $this->return_delete_sql = FALSE;
1690 return $sql;
1691 }
1692
Derek Allard2067d1a2008-11-13 22:59:24 +00001693 // --------------------------------------------------------------------
1694
1695 /**
1696 * Delete
1697 *
1698 * Compiles a delete string and runs the query
1699 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001700 * @param mixed the table(s) to delete from. String or array
1701 * @param mixed the where clause
1702 * @param mixed the limit clause
1703 * @param boolean
1704 * @return object
1705 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001706 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001707 {
1708 // Combine any cached components with the current statements
1709 $this->_merge_cache();
1710
1711 if ($table == '')
1712 {
1713 if ( ! isset($this->ar_from[0]))
1714 {
1715 if ($this->db_debug)
1716 {
1717 return $this->display_error('db_must_set_table');
1718 }
1719 return FALSE;
1720 }
1721
1722 $table = $this->ar_from[0];
1723 }
1724 elseif (is_array($table))
1725 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001726 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001727 {
1728 $this->delete($single_table, $where, $limit, FALSE);
1729 }
1730
1731 $this->_reset_write();
1732 return;
1733 }
1734 else
1735 {
1736 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1737 }
1738
1739 if ($where != '')
1740 {
1741 $this->where($where);
1742 }
1743
1744 if ($limit != NULL)
1745 {
1746 $this->limit($limit);
1747 }
1748
Derek Allard03d783b2009-05-03 19:45:45 +00001749 if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001750 {
1751 if ($this->db_debug)
1752 {
1753 return $this->display_error('db_del_must_use_where');
1754 }
1755
1756 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001757 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001758
1759 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
1760
1761 if ($reset_data)
1762 {
1763 $this->_reset_write();
1764 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001765
1766 if ($this->return_delete_sql === true)
1767 {
1768 return $sql;
1769 }
Barry Mienydd671972010-10-04 16:33:58 +02001770
Derek Allard2067d1a2008-11-13 22:59:24 +00001771 return $this->query($sql);
1772 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001773
Derek Allard2067d1a2008-11-13 22:59:24 +00001774 // --------------------------------------------------------------------
1775
1776 /**
1777 * DB Prefix
1778 *
1779 * Prepends a database prefix if one exists in configuration
1780 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001781 * @param string the table
1782 * @return string
1783 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001784 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001785 {
1786 if ($table == '')
1787 {
1788 $this->display_error('db_table_name_required');
1789 }
1790
1791 return $this->dbprefix.$table;
1792 }
1793
1794 // --------------------------------------------------------------------
1795
1796 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001797 * Set DB Prefix
1798 *
1799 * Set's the DB Prefix to something new without needing to reconnect
1800 *
1801 * @param string the prefix
1802 * @return string
1803 */
1804 public function set_dbprefix($prefix = '')
1805 {
1806 return $this->dbprefix = $prefix;
1807 }
1808
1809 // --------------------------------------------------------------------
1810
1811 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001812 * Track Aliases
1813 *
1814 * Used to track SQL statements written with aliased tables.
1815 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001816 * @param string The table to inspect
1817 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001818 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001819 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 {
1821 if (is_array($table))
1822 {
1823 foreach ($table as $t)
1824 {
1825 $this->_track_aliases($t);
1826 }
1827 return;
1828 }
Barry Mienydd671972010-10-04 16:33:58 +02001829
Derek Jones37f4b9c2011-07-01 17:56:50 -05001830 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001831 // the string into discreet statements
1832 if (strpos($table, ',') !== FALSE)
1833 {
1834 return $this->_track_aliases(explode(',', $table));
1835 }
Barry Mienydd671972010-10-04 16:33:58 +02001836
Derek Allard2067d1a2008-11-13 22:59:24 +00001837 // if a table alias is used we can recognize it by a space
1838 if (strpos($table, " ") !== FALSE)
1839 {
1840 // if the alias is written with the AS keyword, remove it
1841 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001842
Derek Allard2067d1a2008-11-13 22:59:24 +00001843 // Grab the alias
1844 $table = trim(strrchr($table, " "));
Barry Mienydd671972010-10-04 16:33:58 +02001845
Derek Allard2067d1a2008-11-13 22:59:24 +00001846 // Store the alias, if it doesn't already exist
1847 if ( ! in_array($table, $this->ar_aliased_tables))
1848 {
1849 $this->ar_aliased_tables[] = $table;
1850 }
1851 }
1852 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001853
Derek Allard2067d1a2008-11-13 22:59:24 +00001854 // --------------------------------------------------------------------
1855
1856 /**
1857 * Compile the SELECT statement
1858 *
1859 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001860 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001861 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001862 * @return string
1863 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001864 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001865 {
1866 // Combine any cached components with the current statements
1867 $this->_merge_cache();
1868
1869 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001870
Derek Allard2067d1a2008-11-13 22:59:24 +00001871 // Write the "select" portion of the query
1872
1873 if ($select_override !== FALSE)
1874 {
1875 $sql = $select_override;
1876 }
1877 else
1878 {
1879 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001880
Derek Allard2067d1a2008-11-13 22:59:24 +00001881 if (count($this->ar_select) == 0)
1882 {
Barry Mienydd671972010-10-04 16:33:58 +02001883 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001884 }
1885 else
Barry Mienydd671972010-10-04 16:33:58 +02001886 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001887 // Cycle through the "select" portion of the query and prep each column name.
1888 // The reason we protect identifiers here rather then in the select() function
1889 // is because until the user calls the from() function we don't know if there are aliases
1890 foreach ($this->ar_select as $key => $val)
1891 {
Phil Sturgeon77cc0282011-08-09 16:03:49 -06001892 $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL;
1893 $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001894 }
Barry Mienydd671972010-10-04 16:33:58 +02001895
Derek Allard2067d1a2008-11-13 22:59:24 +00001896 $sql .= implode(', ', $this->ar_select);
1897 }
1898 }
1899
1900 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001901
Derek Allard2067d1a2008-11-13 22:59:24 +00001902 // Write the "FROM" portion of the query
1903
1904 if (count($this->ar_from) > 0)
1905 {
1906 $sql .= "\nFROM ";
1907
1908 $sql .= $this->_from_tables($this->ar_from);
1909 }
1910
1911 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001912
Derek Allard2067d1a2008-11-13 22:59:24 +00001913 // Write the "JOIN" portion of the query
1914
1915 if (count($this->ar_join) > 0)
1916 {
1917 $sql .= "\n";
1918
1919 $sql .= implode("\n", $this->ar_join);
1920 }
1921
1922 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001923
Derek Allard2067d1a2008-11-13 22:59:24 +00001924 // Write the "WHERE" portion of the query
1925
1926 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1927 {
Greg Akere156c6e2011-04-20 16:03:04 -05001928 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00001929 }
1930
1931 $sql .= implode("\n", $this->ar_where);
1932
1933 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001934
Derek Allard2067d1a2008-11-13 22:59:24 +00001935 // Write the "LIKE" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001936
Derek Allard2067d1a2008-11-13 22:59:24 +00001937 if (count($this->ar_like) > 0)
1938 {
1939 if (count($this->ar_where) > 0)
1940 {
1941 $sql .= "\nAND ";
1942 }
1943
1944 $sql .= implode("\n", $this->ar_like);
1945 }
1946
1947 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001948
Derek Allard2067d1a2008-11-13 22:59:24 +00001949 // Write the "GROUP BY" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001950
Derek Allard2067d1a2008-11-13 22:59:24 +00001951 if (count($this->ar_groupby) > 0)
1952 {
1953 $sql .= "\nGROUP BY ";
Barry Mienydd671972010-10-04 16:33:58 +02001954
Derek Allard2067d1a2008-11-13 22:59:24 +00001955 $sql .= implode(', ', $this->ar_groupby);
1956 }
1957
1958 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001959
Derek Allard2067d1a2008-11-13 22:59:24 +00001960 // Write the "HAVING" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001961
Derek Allard2067d1a2008-11-13 22:59:24 +00001962 if (count($this->ar_having) > 0)
1963 {
1964 $sql .= "\nHAVING ";
1965 $sql .= implode("\n", $this->ar_having);
1966 }
1967
1968 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001969
Derek Allard2067d1a2008-11-13 22:59:24 +00001970 // Write the "ORDER BY" portion of the query
1971
1972 if (count($this->ar_orderby) > 0)
1973 {
1974 $sql .= "\nORDER BY ";
1975 $sql .= implode(', ', $this->ar_orderby);
Barry Mienydd671972010-10-04 16:33:58 +02001976
Derek Allard2067d1a2008-11-13 22:59:24 +00001977 if ($this->ar_order !== FALSE)
1978 {
1979 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02001980 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001981 }
1982
1983 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001984
Derek Allard2067d1a2008-11-13 22:59:24 +00001985 // Write the "LIMIT" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001986
Derek Allard2067d1a2008-11-13 22:59:24 +00001987 if (is_numeric($this->ar_limit))
1988 {
1989 $sql .= "\n";
1990 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1991 }
1992
1993 return $sql;
1994 }
1995
1996 // --------------------------------------------------------------------
1997
1998 /**
1999 * Object to Array
2000 *
2001 * Takes an object as input and converts the class variables to array key/vals
2002 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002003 * @param object
2004 * @return array
2005 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002006 public function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002007 {
2008 if ( ! is_object($object))
2009 {
2010 return $object;
2011 }
Barry Mienydd671972010-10-04 16:33:58 +02002012
Derek Allard2067d1a2008-11-13 22:59:24 +00002013 $array = array();
2014 foreach (get_object_vars($object) as $key => $val)
2015 {
2016 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06002017 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002018 {
2019 $array[$key] = $val;
2020 }
2021 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002022
2023 return $array;
2024 }
Barry Mienydd671972010-10-04 16:33:58 +02002025
Derek Jonesd10e8962010-03-02 17:10:36 -06002026 // --------------------------------------------------------------------
2027
2028 /**
2029 * Object to Array
2030 *
2031 * Takes an object as input and converts the class variables to array key/vals
2032 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002033 * @param object
2034 * @return array
2035 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002036 public function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002037 {
2038 if ( ! is_object($object))
2039 {
2040 return $object;
2041 }
Barry Mienydd671972010-10-04 16:33:58 +02002042
Derek Jonesd10e8962010-03-02 17:10:36 -06002043 $array = array();
2044 $out = get_object_vars($object);
2045 $fields = array_keys($out);
2046
2047 foreach ($fields as $val)
2048 {
2049 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06002050 if ($val != '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002051 {
2052
2053 $i = 0;
2054 foreach ($out[$val] as $data)
2055 {
2056 $array[$i][$val] = $data;
2057 $i++;
2058 }
2059 }
2060 }
2061
Derek Allard2067d1a2008-11-13 22:59:24 +00002062 return $array;
2063 }
Barry Mienydd671972010-10-04 16:33:58 +02002064
Derek Allard2067d1a2008-11-13 22:59:24 +00002065 // --------------------------------------------------------------------
2066
2067 /**
2068 * Start Cache
2069 *
2070 * Starts AR caching
2071 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002072 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002073 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002074 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002075 {
2076 $this->ar_caching = TRUE;
2077 }
2078
2079 // --------------------------------------------------------------------
2080
2081 /**
2082 * Stop Cache
2083 *
2084 * Stops AR caching
2085 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002086 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002087 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002088 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002089 {
2090 $this->ar_caching = FALSE;
2091 }
2092
2093 // --------------------------------------------------------------------
2094
2095 /**
2096 * Flush Cache
2097 *
2098 * Empties the AR cache
2099 *
2100 * @access public
2101 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002102 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002103 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002104 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002105 $this->_reset_run(array(
2106 'ar_cache_select' => array(),
2107 'ar_cache_from' => array(),
2108 'ar_cache_join' => array(),
2109 'ar_cache_where' => array(),
2110 'ar_cache_like' => array(),
2111 'ar_cache_groupby' => array(),
2112 'ar_cache_having' => array(),
2113 'ar_cache_orderby' => array(),
2114 'ar_cache_set' => array(),
2115 'ar_cache_exists' => array(),
2116 'ar_cache_no_escape' => array()
2117 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002118 }
2119
2120 // --------------------------------------------------------------------
2121
2122 /**
2123 * Merge Cache
2124 *
Barry Mienydd671972010-10-04 16:33:58 +02002125 * When called, this function merges any cached AR arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002126 * locally called ones.
2127 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002128 * @return void
2129 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002130 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002131 {
2132 if (count($this->ar_cache_exists) == 0)
2133 {
2134 return;
2135 }
2136
2137 foreach ($this->ar_cache_exists as $val)
2138 {
2139 $ar_variable = 'ar_'.$val;
2140 $ar_cache_var = 'ar_cache_'.$val;
2141
2142 if (count($this->$ar_cache_var) == 0)
2143 {
2144 continue;
2145 }
2146
2147 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
2148 }
2149
2150 // If we are "protecting identifiers" we need to examine the "from"
2151 // portion of the query to determine if there are any aliases
2152 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
2153 {
2154 $this->_track_aliases($this->ar_from);
2155 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002156
2157 $this->ar_no_escape = $this->ar_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002158 }
Kyle Farris0c147b32011-08-26 02:29:31 -04002159
2160 // --------------------------------------------------------------------
2161
2162 /**
2163 * Reset Active Record values.
2164 *
2165 * Publicly-visible method to reset the AR values.
2166 *
2167 * @access public
2168 * @return void
2169 */
2170 public function reset_query()
2171 {
2172 $this->_reset_select();
2173 $this->_reset_write();
2174 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002175
2176 // --------------------------------------------------------------------
2177
2178 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002179 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002180 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002181 * @param array An array of fields to reset
2182 * @return void
2183 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002184 protected function _reset_run($ar_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002185 {
2186 foreach ($ar_reset_items as $item => $default_value)
2187 {
2188 if ( ! in_array($item, $this->ar_store_array))
2189 {
2190 $this->$item = $default_value;
2191 }
2192 }
2193 }
2194
2195 // --------------------------------------------------------------------
2196
2197 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002198 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002199 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002200 * @return void
2201 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002202 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002203 {
2204 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06002205 'ar_select' => array(),
2206 'ar_from' => array(),
2207 'ar_join' => array(),
2208 'ar_where' => array(),
2209 'ar_like' => array(),
2210 'ar_groupby' => array(),
2211 'ar_having' => array(),
2212 'ar_orderby' => array(),
2213 'ar_wherein' => array(),
2214 'ar_aliased_tables' => array(),
2215 'ar_no_escape' => array(),
2216 'ar_distinct' => FALSE,
2217 'ar_limit' => FALSE,
2218 'ar_offset' => FALSE,
2219 'ar_order' => FALSE,
2220 );
Barry Mienydd671972010-10-04 16:33:58 +02002221
Derek Allard2067d1a2008-11-13 22:59:24 +00002222 $this->_reset_run($ar_reset_items);
2223 }
Barry Mienydd671972010-10-04 16:33:58 +02002224
Derek Allard2067d1a2008-11-13 22:59:24 +00002225 // --------------------------------------------------------------------
2226
2227 /**
2228 * Resets the active record "write" values.
2229 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002230 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002231 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002232 * @return void
2233 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002234 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002235 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002236 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06002237 'ar_set' => array(),
2238 'ar_from' => array(),
2239 'ar_where' => array(),
2240 'ar_like' => array(),
2241 'ar_orderby' => array(),
2242 'ar_keys' => array(),
2243 'ar_limit' => FALSE,
2244 'ar_order' => FALSE
2245 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002246
2247 $this->_reset_run($ar_reset_items);
2248 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002249}
2250
2251/* End of file DB_active_rec.php */
Kyle Farris2de2fa02011-08-31 11:52:20 -04002252/* Location: ./system/database/DB_active_rec.php */