blob: 412febfccf120f3fca2dea20e80cc0bee5c521a9 [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
pporlan2c685fb2011-12-22 12:15:25 +0100833 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 * @return object
835 */
pporlan2c685fb2011-12-22 12:15:25 +0100836 public function order_by($orderby, $direction = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 {
838 if (strtolower($direction) == 'random')
839 {
840 $orderby = ''; // Random results want or don't need a field name
841 $direction = $this->_random_keyword;
842 }
843 elseif (trim($direction) != '')
844 {
845 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
846 }
Barry Mienydd671972010-10-04 16:33:58 +0200847
848
pporlan2c685fb2011-12-22 12:15:25 +0100849 if ((strpos($orderby, ',') !== FALSE) && ($escape === TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 {
851 $temp = array();
852 foreach (explode(',', $orderby) as $part)
853 {
854 $part = trim($part);
855 if ( ! in_array($part, $this->ar_aliased_tables))
856 {
857 $part = $this->_protect_identifiers(trim($part));
858 }
Barry Mienydd671972010-10-04 16:33:58 +0200859
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 $temp[] = $part;
861 }
Barry Mienydd671972010-10-04 16:33:58 +0200862
863 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 }
Derek Allarde37ab382009-02-03 16:13:57 +0000865 else if ($direction != $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 {
pporlan2c685fb2011-12-22 12:15:25 +0100867 if ($escape === TRUE)
868 {
869 $orderby = $this->_protect_identifiers($orderby);
870 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 }
Barry Mienydd671972010-10-04 16:33:58 +0200872
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200874
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 $this->ar_orderby[] = $orderby_statement;
876 if ($this->ar_caching === TRUE)
877 {
878 $this->ar_cache_orderby[] = $orderby_statement;
879 $this->ar_cache_exists[] = 'orderby';
880 }
881
882 return $this;
883 }
Barry Mienydd671972010-10-04 16:33:58 +0200884
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 // --------------------------------------------------------------------
886
887 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 * Sets the LIMIT value
889 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 * @param integer the limit value
891 * @param integer the offset value
892 * @return object
893 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200894 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 {
Kyle Farris76116012011-08-31 11:17:48 -0400896 $this->ar_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000897
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200898 if ( ! is_null($offset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 {
Kyle Farris76116012011-08-31 11:17:48 -0400900 $this->ar_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 }
Barry Mienydd671972010-10-04 16:33:58 +0200902
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 return $this;
904 }
Barry Mienydd671972010-10-04 16:33:58 +0200905
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 // --------------------------------------------------------------------
907
908 /**
909 * Sets the OFFSET value
910 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 * @param integer the offset value
912 * @return object
913 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600914 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 {
kenjis0e857632011-09-02 08:41:17 +0900916 $this->ar_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 return $this;
918 }
Barry Mienydd671972010-10-04 16:33:58 +0200919
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 // --------------------------------------------------------------------
921
922 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500923 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 * @param mixed
926 * @param string
927 * @param boolean
928 * @return object
929 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600930 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 {
932 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +0200933
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 if ( ! is_array($key))
935 {
936 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +0200937 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000938
939 foreach ($key as $k => $v)
940 {
941 if ($escape === FALSE)
942 {
943 $this->ar_set[$this->_protect_identifiers($k)] = $v;
944 }
945 else
946 {
Phil Sturgeond0ac1a22011-02-15 22:54:08 +0000947 $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 }
949 }
Barry Mienydd671972010-10-04 16:33:58 +0200950
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 return $this;
952 }
Kyle Farris0c147b32011-08-26 02:29:31 -0400953
Kyle Farris0c147b32011-08-26 02:29:31 -0400954 // --------------------------------------------------------------------
955
956 /**
957 * Get SELECT query string
958 *
959 * Compiles a SELECT query string and returns the sql.
960 *
961 * @access public
962 * @param string the table name to select from (optional)
963 * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone
964 * @return string
965 */
966 public function get_compiled_select($table = '', $reset = TRUE)
967 {
968 if ($table != '')
kylefarris0a3176b2011-08-26 02:37:52 -0400969 {
Kyle Farris0c147b32011-08-26 02:29:31 -0400970 $this->_track_aliases($table);
971 $this->from($table);
972 }
973
974 $select = $this->_compile_select();
975
976 if ($reset === TRUE)
977 {
978 $this->_reset_select();
979 }
980
981 return $select;
982 }
983
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 // --------------------------------------------------------------------
985
986 /**
987 * Get
988 *
989 * Compiles the select statement based on the other functions called
990 * and runs the query
991 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 * @param string the table
993 * @param string the limit clause
994 * @param string the offset clause
995 * @return object
996 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600997 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 {
999 if ($table != '')
1000 {
1001 $this->_track_aliases($table);
1002 $this->from($table);
1003 }
Barry Mienydd671972010-10-04 16:33:58 +02001004
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 if ( ! is_null($limit))
1006 {
1007 $this->limit($limit, $offset);
1008 }
Barry Mienydd671972010-10-04 16:33:58 +02001009
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 $sql = $this->_compile_select();
1011
1012 $result = $this->query($sql);
1013 $this->_reset_select();
1014 return $result;
1015 }
1016
1017 /**
1018 * "Count All Results" query
1019 *
Barry Mienydd671972010-10-04 16:33:58 +02001020 * Generates a platform-specific query string that counts all records
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 * returned by an Active Record query.
1022 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 * @param string
1024 * @return string
1025 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001026 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 {
1028 if ($table != '')
1029 {
1030 $this->_track_aliases($table);
1031 $this->from($table);
1032 }
Barry Mienydd671972010-10-04 16:33:58 +02001033
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
1035
1036 $query = $this->query($sql);
1037 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001038
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 if ($query->num_rows() == 0)
1040 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001041 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 }
1043
1044 $row = $query->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001045 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 }
1047
1048 // --------------------------------------------------------------------
1049
1050 /**
1051 * Get_Where
1052 *
1053 * Allows the where clause, limit and offset to be added directly
1054 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 * @param string the where clause
1056 * @param string the limit clause
1057 * @param string the offset clause
1058 * @return object
1059 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001060 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 {
1062 if ($table != '')
1063 {
1064 $this->from($table);
1065 }
1066
1067 if ( ! is_null($where))
1068 {
1069 $this->where($where);
1070 }
Barry Mienydd671972010-10-04 16:33:58 +02001071
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 if ( ! is_null($limit))
1073 {
1074 $this->limit($limit, $offset);
1075 }
Barry Mienydd671972010-10-04 16:33:58 +02001076
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 $sql = $this->_compile_select();
1078
1079 $result = $this->query($sql);
1080 $this->_reset_select();
1081 return $result;
1082 }
1083
1084 // --------------------------------------------------------------------
1085
1086 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001087 * Insert_Batch
1088 *
1089 * Compiles batch insert strings and runs the queries
1090 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001091 * @param string the table to retrieve the results from
1092 * @param array an associative array of insert values
1093 * @return object
1094 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001095 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001096 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001097 if ( ! is_null($set))
1098 {
1099 $this->set_insert_batch($set);
1100 }
Barry Mienydd671972010-10-04 16:33:58 +02001101
Derek Jonesd10e8962010-03-02 17:10:36 -06001102 if (count($this->ar_set) == 0)
1103 {
1104 if ($this->db_debug)
1105 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001106 //No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001107 return $this->display_error('db_must_use_set');
1108 }
1109 return FALSE;
1110 }
1111
1112 if ($table == '')
1113 {
1114 if ( ! isset($this->ar_from[0]))
1115 {
1116 if ($this->db_debug)
1117 {
1118 return $this->display_error('db_must_set_table');
1119 }
1120 return FALSE;
1121 }
Barry Mienydd671972010-10-04 16:33:58 +02001122
Derek Jonesd10e8962010-03-02 17:10:36 -06001123 $table = $this->ar_from[0];
1124 }
1125
1126 // Batch this baby
1127 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1128 {
Barry Mienydd671972010-10-04 16:33:58 +02001129
Derek Jonesd10e8962010-03-02 17:10:36 -06001130 $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
1131
1132 //echo $sql;
1133
1134 $this->query($sql);
1135 }
Barry Mienydd671972010-10-04 16:33:58 +02001136
Derek Jonesd10e8962010-03-02 17:10:36 -06001137 $this->_reset_write();
1138
1139
Barry Mienydd671972010-10-04 16:33:58 +02001140 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001141 }
1142
1143 // --------------------------------------------------------------------
1144
1145 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001146 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001147 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001148 * @param mixed
1149 * @param string
1150 * @param boolean
1151 * @return object
1152 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001153 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001154 {
1155 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001156
Derek Jonesd10e8962010-03-02 17:10:36 -06001157 if ( ! is_array($key))
1158 {
1159 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001160 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001161
1162 $keys = array_keys(current($key));
1163 sort($keys);
1164
1165 foreach ($key as $row)
1166 {
Barry Mienydd671972010-10-04 16:33:58 +02001167 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1168 {
1169 // batch function above returns an error on an empty array
1170 $this->ar_set[] = array();
1171 return;
1172 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001173
Barry Mienydd671972010-10-04 16:33:58 +02001174 ksort($row); // puts $row in the same order as our keys
1175
Derek Jonesd10e8962010-03-02 17:10:36 -06001176 if ($escape === FALSE)
1177 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001178 $this->ar_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001179 }
1180 else
1181 {
1182 $clean = array();
1183
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001184 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001185 {
Barry Mienydd671972010-10-04 16:33:58 +02001186 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001187 }
1188
Derek Jones37f4b9c2011-07-01 17:56:50 -05001189 $this->ar_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001190 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001191 }
1192
1193 foreach ($keys as $k)
1194 {
1195 $this->ar_keys[] = $this->_protect_identifiers($k);
1196 }
Barry Mienydd671972010-10-04 16:33:58 +02001197
Derek Jonesd10e8962010-03-02 17:10:36 -06001198 return $this;
1199 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001200
1201 // --------------------------------------------------------------------
1202
1203 /**
1204 * Get INSERT query string
1205 *
1206 * Compiles an insert query and returns the sql
1207 *
1208 * @access public
1209 * @param string the table to insert into
1210 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1211 * @return string
1212 */
1213 public function get_compiled_insert($table = '', $reset = TRUE)
1214 {
1215 if ($this->_validate_insert($table) === FALSE)
1216 {
1217 return FALSE;
1218 }
1219
Kyle Farris76116012011-08-31 11:17:48 -04001220 $sql = $this->_insert(
1221 $this->_protect_identifiers(
1222 $this->ar_from[0], TRUE, NULL, FALSE
1223 ),
1224 array_keys($this->ar_set),
1225 array_values($this->ar_set)
1226 );
Kyle Farris0c147b32011-08-26 02:29:31 -04001227
1228 if ($reset === TRUE)
1229 {
1230 $this->_reset_write();
1231 }
1232
1233 return $sql;
1234 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001235
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 // --------------------------------------------------------------------
1237
1238 /**
1239 * Insert
1240 *
1241 * Compiles an insert string and runs the query
1242 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001243 * @access public
Phil Sturgeon9789f322011-07-15 15:14:05 -06001244 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 * @param array an associative array of insert values
1246 * @return object
1247 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001248 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001249 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 if ( ! is_null($set))
1251 {
1252 $this->set($set);
1253 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001254
1255 if ($this->_validate_insert($table) === FALSE)
1256 {
1257 return FALSE;
1258 }
1259
Kyle Farris76116012011-08-31 11:17:48 -04001260 $sql = $this->_insert(
1261 $this->_protect_identifiers(
1262 $this->ar_from[0], TRUE, NULL, FALSE
1263 ),
1264 array_keys($this->ar_set),
1265 array_values($this->ar_set)
1266 );
Barry Mienydd671972010-10-04 16:33:58 +02001267
Kyle Farris0c147b32011-08-26 02:29:31 -04001268 $this->_reset_write();
1269 return $this->query($sql);
1270 }
1271
Kyle Farris0c147b32011-08-26 02:29:31 -04001272 // --------------------------------------------------------------------
1273
1274 /**
1275 * Validate Insert
1276 *
1277 * This method is used by both insert() and get_compiled_insert() to
1278 * validate that the there data is actually being set and that table
1279 * has been chosen to be inserted into.
1280 *
1281 * @access public
1282 * @param string the table to insert data into
1283 * @return string
1284 */
1285 protected function _validate_insert($table = '')
1286 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001287 if (count($this->ar_set) == 0)
1288 {
1289 if ($this->db_debug)
1290 {
1291 return $this->display_error('db_must_use_set');
1292 }
1293 return FALSE;
1294 }
1295
1296 if ($table == '')
1297 {
1298 if ( ! isset($this->ar_from[0]))
1299 {
1300 if ($this->db_debug)
1301 {
1302 return $this->display_error('db_must_set_table');
1303 }
1304 return FALSE;
1305 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001306 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001307 else
1308 {
1309 $this->ar_from[0] = $table;
1310 }
1311
1312 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 }
Barry Mienydd671972010-10-04 16:33:58 +02001314
Phil Sturgeon9789f322011-07-15 15:14:05 -06001315 // --------------------------------------------------------------------
1316
1317 /**
1318 * Replace
1319 *
1320 * Compiles an replace into string and runs the query
1321 *
1322 * @param string the table to replace data into
1323 * @param array an associative array of insert values
1324 * @return object
1325 */
1326 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001327 {
1328 if ( ! is_null($set))
1329 {
1330 $this->set($set);
1331 }
Barry Mienydd671972010-10-04 16:33:58 +02001332
Derek Jonesd10e8962010-03-02 17:10:36 -06001333 if (count($this->ar_set) == 0)
1334 {
1335 if ($this->db_debug)
1336 {
1337 return $this->display_error('db_must_use_set');
1338 }
1339 return FALSE;
1340 }
1341
1342 if ($table == '')
1343 {
1344 if ( ! isset($this->ar_from[0]))
1345 {
1346 if ($this->db_debug)
1347 {
1348 return $this->display_error('db_must_set_table');
1349 }
1350 return FALSE;
1351 }
Barry Mienydd671972010-10-04 16:33:58 +02001352
Derek Jonesd10e8962010-03-02 17:10:36 -06001353 $table = $this->ar_from[0];
1354 }
1355
1356 $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 +02001357
Derek Jonesd10e8962010-03-02 17:10:36 -06001358 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001359 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001360 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001361
Kyle Farris0c147b32011-08-26 02:29:31 -04001362 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001363
Kyle Farris0c147b32011-08-26 02:29:31 -04001364 /**
1365 * Get UPDATE query string
1366 *
1367 * Compiles an update query and returns the sql
1368 *
1369 * @access public
1370 * @param string the table to update
1371 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1372 * @return string
1373 */
1374 public function get_compiled_update($table = '', $reset = TRUE)
1375 {
1376 // Combine any cached components with the current statements
1377 $this->_merge_cache();
1378
1379 if ($this->_validate_update($table) === FALSE)
1380 {
1381 return FALSE;
1382 }
1383
1384 $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);
1385
1386 if ($reset === TRUE)
1387 {
1388 $this->_reset_write();
1389 }
1390
1391 return $sql;
1392 }
1393
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 // --------------------------------------------------------------------
1395
1396 /**
1397 * Update
1398 *
1399 * Compiles an update string and runs the query
1400 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 * @param string the table to retrieve the results from
1402 * @param array an associative array of update values
1403 * @param mixed the where clause
1404 * @return object
1405 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001406 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001407 {
1408 // Combine any cached components with the current statements
1409 $this->_merge_cache();
1410
1411 if ( ! is_null($set))
1412 {
1413 $this->set($set);
1414 }
Barry Mienydd671972010-10-04 16:33:58 +02001415
Kyle Farris0c147b32011-08-26 02:29:31 -04001416 if ($this->_validate_update($table) === FALSE)
1417 {
1418 return FALSE;
1419 }
1420
1421 if ($where != NULL)
1422 {
1423 $this->where($where);
1424 }
1425
1426 if ($limit != NULL)
1427 {
1428 $this->limit($limit);
1429 }
1430
Mancy0d91fd22011-12-20 13:13:14 +03001431 $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, $this->ar_like);
Kyle Farris0c147b32011-08-26 02:29:31 -04001432
1433 $this->_reset_write();
1434 return $this->query($sql);
1435 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001436
1437 // --------------------------------------------------------------------
1438
1439 /**
1440 * Validate Update
1441 *
1442 * This method is used by both update() and get_compiled_update() to
1443 * validate that data is actually being set and that a table has been
1444 * chosen to be update.
1445 *
1446 * @access public
1447 * @param string the table to update data on
1448 * @return string
1449 */
1450 protected function _validate_update($table = '')
1451 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001452 if (count($this->ar_set) == 0)
1453 {
1454 if ($this->db_debug)
1455 {
1456 return $this->display_error('db_must_use_set');
1457 }
1458 return FALSE;
1459 }
1460
1461 if ($table == '')
1462 {
1463 if ( ! isset($this->ar_from[0]))
1464 {
1465 if ($this->db_debug)
1466 {
1467 return $this->display_error('db_must_set_table');
1468 }
1469 return FALSE;
1470 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001471 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001472 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001473 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001474 $this->ar_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001475 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001476 }
Kyle Farris76116012011-08-31 11:17:48 -04001477
Derek Jonesd10e8962010-03-02 17:10:36 -06001478 // --------------------------------------------------------------------
1479
1480 /**
1481 * Update_Batch
1482 *
1483 * Compiles an update string and runs the query
1484 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001485 * @param string the table to retrieve the results from
1486 * @param array an associative array of update values
1487 * @param string the where key
1488 * @return object
1489 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001490 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001491 {
1492 // Combine any cached components with the current statements
1493 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001494
Derek Jonesd10e8962010-03-02 17:10:36 -06001495 if (is_null($index))
1496 {
1497 if ($this->db_debug)
1498 {
Kyle Farris2de2fa02011-08-31 11:52:20 -04001499 return $this->display_error('db_must_use_index');
Derek Jonesd10e8962010-03-02 17:10:36 -06001500 }
1501
1502 return FALSE;
1503 }
1504
1505 if ( ! is_null($set))
1506 {
1507 $this->set_update_batch($set, $index);
1508 }
1509
1510 if (count($this->ar_set) == 0)
1511 {
1512 if ($this->db_debug)
1513 {
1514 return $this->display_error('db_must_use_set');
1515 }
1516
1517 return FALSE;
1518 }
1519
1520 if ($table == '')
1521 {
1522 if ( ! isset($this->ar_from[0]))
1523 {
1524 if ($this->db_debug)
1525 {
1526 return $this->display_error('db_must_set_table');
1527 }
1528 return FALSE;
1529 }
Barry Mienydd671972010-10-04 16:33:58 +02001530
Derek Jonesd10e8962010-03-02 17:10:36 -06001531 $table = $this->ar_from[0];
1532 }
Barry Mienydd671972010-10-04 16:33:58 +02001533
Derek Jonesd10e8962010-03-02 17:10:36 -06001534 // Batch this baby
1535 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1536 {
1537 $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);
1538
1539 $this->query($sql);
1540 }
Barry Mienydd671972010-10-04 16:33:58 +02001541
Derek Jonesd10e8962010-03-02 17:10:36 -06001542 $this->_reset_write();
1543 }
1544
1545 // --------------------------------------------------------------------
1546
1547 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001548 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001549 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001550 * @param array
1551 * @param string
1552 * @param boolean
1553 * @return object
1554 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001555 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001556 {
1557 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001558
Derek Jonesd10e8962010-03-02 17:10:36 -06001559 if ( ! is_array($key))
1560 {
1561 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001562 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001563
1564 foreach ($key as $k => $v)
1565 {
1566 $index_set = FALSE;
1567 $clean = array();
1568
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001569 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001570 {
1571 if ($k2 == $index)
1572 {
1573 $index_set = TRUE;
1574 }
1575 else
1576 {
1577 $not[] = $k.'-'.$v;
1578 }
1579
1580 if ($escape === FALSE)
1581 {
1582 $clean[$this->_protect_identifiers($k2)] = $v2;
1583 }
1584 else
1585 {
Barry Mienydd671972010-10-04 16:33:58 +02001586 $clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001587 }
1588 }
1589
1590 if ($index_set == FALSE)
1591 {
1592 return $this->display_error('db_batch_missing_index');
1593 }
1594
1595 $this->ar_set[] = $clean;
1596 }
Barry Mienydd671972010-10-04 16:33:58 +02001597
Derek Jonesd10e8962010-03-02 17:10:36 -06001598 return $this;
1599 }
1600
Derek Allard2067d1a2008-11-13 22:59:24 +00001601 // --------------------------------------------------------------------
1602
1603 /**
1604 * Empty Table
1605 *
1606 * Compiles a delete string and runs "DELETE FROM table"
1607 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001608 * @param string the table to empty
1609 * @return object
1610 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001611 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001612 {
1613 if ($table == '')
1614 {
1615 if ( ! isset($this->ar_from[0]))
1616 {
1617 if ($this->db_debug)
1618 {
1619 return $this->display_error('db_must_set_table');
1620 }
1621 return FALSE;
1622 }
1623
1624 $table = $this->ar_from[0];
1625 }
1626 else
1627 {
1628 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1629 }
1630
1631 $sql = $this->_delete($table);
1632
1633 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001634
Derek Allard2067d1a2008-11-13 22:59:24 +00001635 return $this->query($sql);
1636 }
1637
1638 // --------------------------------------------------------------------
1639
1640 /**
1641 * Truncate
1642 *
1643 * Compiles a truncate string and runs the query
1644 * If the database does not support the truncate() command
1645 * This function maps to "DELETE FROM table"
1646 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001647 * @param string the table to truncate
1648 * @return object
1649 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001650 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001651 {
1652 if ($table == '')
1653 {
1654 if ( ! isset($this->ar_from[0]))
1655 {
1656 if ($this->db_debug)
1657 {
1658 return $this->display_error('db_must_set_table');
1659 }
1660 return FALSE;
1661 }
1662
1663 $table = $this->ar_from[0];
1664 }
1665 else
1666 {
1667 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1668 }
1669
1670 $sql = $this->_truncate($table);
1671
1672 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001673
Derek Allard2067d1a2008-11-13 22:59:24 +00001674 return $this->query($sql);
1675 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001676
1677 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001678
Kyle Farris0c147b32011-08-26 02:29:31 -04001679 /**
1680 * Get DELETE query string
1681 *
1682 * Compiles a delete query string and returns the sql
1683 *
1684 * @access public
1685 * @param string the table to delete from
1686 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1687 * @return string
1688 */
1689 public function get_compiled_delete($table = '', $reset = TRUE)
1690 {
1691 $this->return_delete_sql = TRUE;
1692 $sql = $this->delete($table, '', NULL, $reset);
1693 $this->return_delete_sql = FALSE;
1694 return $sql;
1695 }
1696
Derek Allard2067d1a2008-11-13 22:59:24 +00001697 // --------------------------------------------------------------------
1698
1699 /**
1700 * Delete
1701 *
1702 * Compiles a delete string and runs the query
1703 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001704 * @param mixed the table(s) to delete from. String or array
1705 * @param mixed the where clause
1706 * @param mixed the limit clause
1707 * @param boolean
1708 * @return object
1709 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001710 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001711 {
1712 // Combine any cached components with the current statements
1713 $this->_merge_cache();
1714
1715 if ($table == '')
1716 {
1717 if ( ! isset($this->ar_from[0]))
1718 {
1719 if ($this->db_debug)
1720 {
1721 return $this->display_error('db_must_set_table');
1722 }
1723 return FALSE;
1724 }
1725
1726 $table = $this->ar_from[0];
1727 }
1728 elseif (is_array($table))
1729 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001730 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 {
1732 $this->delete($single_table, $where, $limit, FALSE);
1733 }
1734
1735 $this->_reset_write();
1736 return;
1737 }
1738 else
1739 {
1740 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1741 }
1742
1743 if ($where != '')
1744 {
1745 $this->where($where);
1746 }
1747
1748 if ($limit != NULL)
1749 {
1750 $this->limit($limit);
1751 }
1752
Derek Allard03d783b2009-05-03 19:45:45 +00001753 if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001754 {
1755 if ($this->db_debug)
1756 {
1757 return $this->display_error('db_del_must_use_where');
1758 }
1759
1760 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001761 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001762
1763 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
1764
1765 if ($reset_data)
1766 {
1767 $this->_reset_write();
1768 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001769
1770 if ($this->return_delete_sql === true)
1771 {
1772 return $sql;
1773 }
Barry Mienydd671972010-10-04 16:33:58 +02001774
Derek Allard2067d1a2008-11-13 22:59:24 +00001775 return $this->query($sql);
1776 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001777
Derek Allard2067d1a2008-11-13 22:59:24 +00001778 // --------------------------------------------------------------------
1779
1780 /**
1781 * DB Prefix
1782 *
1783 * Prepends a database prefix if one exists in configuration
1784 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001785 * @param string the table
1786 * @return string
1787 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001788 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 {
1790 if ($table == '')
1791 {
1792 $this->display_error('db_table_name_required');
1793 }
1794
1795 return $this->dbprefix.$table;
1796 }
1797
1798 // --------------------------------------------------------------------
1799
1800 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001801 * Set DB Prefix
1802 *
1803 * Set's the DB Prefix to something new without needing to reconnect
1804 *
1805 * @param string the prefix
1806 * @return string
1807 */
1808 public function set_dbprefix($prefix = '')
1809 {
1810 return $this->dbprefix = $prefix;
1811 }
1812
1813 // --------------------------------------------------------------------
1814
1815 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001816 * Track Aliases
1817 *
1818 * Used to track SQL statements written with aliased tables.
1819 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 * @param string The table to inspect
1821 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001822 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001823 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001824 {
1825 if (is_array($table))
1826 {
1827 foreach ($table as $t)
1828 {
1829 $this->_track_aliases($t);
1830 }
1831 return;
1832 }
Barry Mienydd671972010-10-04 16:33:58 +02001833
Derek Jones37f4b9c2011-07-01 17:56:50 -05001834 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001835 // the string into discreet statements
1836 if (strpos($table, ',') !== FALSE)
1837 {
1838 return $this->_track_aliases(explode(',', $table));
1839 }
Barry Mienydd671972010-10-04 16:33:58 +02001840
Derek Allard2067d1a2008-11-13 22:59:24 +00001841 // if a table alias is used we can recognize it by a space
1842 if (strpos($table, " ") !== FALSE)
1843 {
1844 // if the alias is written with the AS keyword, remove it
1845 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001846
Derek Allard2067d1a2008-11-13 22:59:24 +00001847 // Grab the alias
1848 $table = trim(strrchr($table, " "));
Barry Mienydd671972010-10-04 16:33:58 +02001849
Derek Allard2067d1a2008-11-13 22:59:24 +00001850 // Store the alias, if it doesn't already exist
1851 if ( ! in_array($table, $this->ar_aliased_tables))
1852 {
1853 $this->ar_aliased_tables[] = $table;
1854 }
1855 }
1856 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001857
Derek Allard2067d1a2008-11-13 22:59:24 +00001858 // --------------------------------------------------------------------
1859
1860 /**
1861 * Compile the SELECT statement
1862 *
1863 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001864 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001865 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001866 * @return string
1867 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001868 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001869 {
1870 // Combine any cached components with the current statements
1871 $this->_merge_cache();
1872
1873 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001874
Derek Allard2067d1a2008-11-13 22:59:24 +00001875 // Write the "select" portion of the query
1876
1877 if ($select_override !== FALSE)
1878 {
1879 $sql = $select_override;
1880 }
1881 else
1882 {
1883 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001884
Derek Allard2067d1a2008-11-13 22:59:24 +00001885 if (count($this->ar_select) == 0)
1886 {
Barry Mienydd671972010-10-04 16:33:58 +02001887 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001888 }
1889 else
Barry Mienydd671972010-10-04 16:33:58 +02001890 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001891 // Cycle through the "select" portion of the query and prep each column name.
1892 // The reason we protect identifiers here rather then in the select() function
1893 // is because until the user calls the from() function we don't know if there are aliases
1894 foreach ($this->ar_select as $key => $val)
1895 {
Phil Sturgeon77cc0282011-08-09 16:03:49 -06001896 $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL;
1897 $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001898 }
Barry Mienydd671972010-10-04 16:33:58 +02001899
Derek Allard2067d1a2008-11-13 22:59:24 +00001900 $sql .= implode(', ', $this->ar_select);
1901 }
1902 }
1903
1904 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001905
Derek Allard2067d1a2008-11-13 22:59:24 +00001906 // Write the "FROM" portion of the query
1907
1908 if (count($this->ar_from) > 0)
1909 {
1910 $sql .= "\nFROM ";
1911
1912 $sql .= $this->_from_tables($this->ar_from);
1913 }
1914
1915 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001916
Derek Allard2067d1a2008-11-13 22:59:24 +00001917 // Write the "JOIN" portion of the query
1918
1919 if (count($this->ar_join) > 0)
1920 {
1921 $sql .= "\n";
1922
1923 $sql .= implode("\n", $this->ar_join);
1924 }
1925
1926 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001927
Derek Allard2067d1a2008-11-13 22:59:24 +00001928 // Write the "WHERE" portion of the query
1929
1930 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1931 {
Greg Akere156c6e2011-04-20 16:03:04 -05001932 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00001933 }
1934
1935 $sql .= implode("\n", $this->ar_where);
1936
1937 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001938
Derek Allard2067d1a2008-11-13 22:59:24 +00001939 // Write the "LIKE" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001940
Derek Allard2067d1a2008-11-13 22:59:24 +00001941 if (count($this->ar_like) > 0)
1942 {
1943 if (count($this->ar_where) > 0)
1944 {
1945 $sql .= "\nAND ";
1946 }
1947
1948 $sql .= implode("\n", $this->ar_like);
1949 }
1950
1951 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001952
Derek Allard2067d1a2008-11-13 22:59:24 +00001953 // Write the "GROUP BY" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001954
Derek Allard2067d1a2008-11-13 22:59:24 +00001955 if (count($this->ar_groupby) > 0)
1956 {
1957 $sql .= "\nGROUP BY ";
Barry Mienydd671972010-10-04 16:33:58 +02001958
Derek Allard2067d1a2008-11-13 22:59:24 +00001959 $sql .= implode(', ', $this->ar_groupby);
1960 }
1961
1962 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001963
Derek Allard2067d1a2008-11-13 22:59:24 +00001964 // Write the "HAVING" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001965
Derek Allard2067d1a2008-11-13 22:59:24 +00001966 if (count($this->ar_having) > 0)
1967 {
1968 $sql .= "\nHAVING ";
1969 $sql .= implode("\n", $this->ar_having);
1970 }
1971
1972 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001973
Derek Allard2067d1a2008-11-13 22:59:24 +00001974 // Write the "ORDER BY" portion of the query
1975
1976 if (count($this->ar_orderby) > 0)
1977 {
1978 $sql .= "\nORDER BY ";
1979 $sql .= implode(', ', $this->ar_orderby);
Barry Mienydd671972010-10-04 16:33:58 +02001980
Derek Allard2067d1a2008-11-13 22:59:24 +00001981 if ($this->ar_order !== FALSE)
1982 {
1983 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02001984 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001985 }
1986
1987 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001988
Derek Allard2067d1a2008-11-13 22:59:24 +00001989 // Write the "LIMIT" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001990
Derek Allard2067d1a2008-11-13 22:59:24 +00001991 if (is_numeric($this->ar_limit))
1992 {
1993 $sql .= "\n";
1994 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1995 }
1996
1997 return $sql;
1998 }
1999
2000 // --------------------------------------------------------------------
2001
2002 /**
2003 * Object to Array
2004 *
2005 * Takes an object as input and converts the class variables to array key/vals
2006 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002007 * @param object
2008 * @return array
2009 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002010 public function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002011 {
2012 if ( ! is_object($object))
2013 {
2014 return $object;
2015 }
Barry Mienydd671972010-10-04 16:33:58 +02002016
Derek Allard2067d1a2008-11-13 22:59:24 +00002017 $array = array();
2018 foreach (get_object_vars($object) as $key => $val)
2019 {
2020 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06002021 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002022 {
2023 $array[$key] = $val;
2024 }
2025 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002026
2027 return $array;
2028 }
Barry Mienydd671972010-10-04 16:33:58 +02002029
Derek Jonesd10e8962010-03-02 17:10:36 -06002030 // --------------------------------------------------------------------
2031
2032 /**
2033 * Object to Array
2034 *
2035 * Takes an object as input and converts the class variables to array key/vals
2036 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002037 * @param object
2038 * @return array
2039 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002040 public function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002041 {
2042 if ( ! is_object($object))
2043 {
2044 return $object;
2045 }
Barry Mienydd671972010-10-04 16:33:58 +02002046
Derek Jonesd10e8962010-03-02 17:10:36 -06002047 $array = array();
2048 $out = get_object_vars($object);
2049 $fields = array_keys($out);
2050
2051 foreach ($fields as $val)
2052 {
2053 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06002054 if ($val != '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002055 {
2056
2057 $i = 0;
2058 foreach ($out[$val] as $data)
2059 {
2060 $array[$i][$val] = $data;
2061 $i++;
2062 }
2063 }
2064 }
2065
Derek Allard2067d1a2008-11-13 22:59:24 +00002066 return $array;
2067 }
Barry Mienydd671972010-10-04 16:33:58 +02002068
Derek Allard2067d1a2008-11-13 22:59:24 +00002069 // --------------------------------------------------------------------
2070
2071 /**
2072 * Start Cache
2073 *
2074 * Starts AR caching
2075 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002076 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002077 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002078 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002079 {
2080 $this->ar_caching = TRUE;
2081 }
2082
2083 // --------------------------------------------------------------------
2084
2085 /**
2086 * Stop Cache
2087 *
2088 * Stops AR caching
2089 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002090 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002091 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002092 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002093 {
2094 $this->ar_caching = FALSE;
2095 }
2096
2097 // --------------------------------------------------------------------
2098
2099 /**
2100 * Flush Cache
2101 *
2102 * Empties the AR cache
2103 *
2104 * @access public
2105 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002106 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002107 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002108 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002109 $this->_reset_run(array(
2110 'ar_cache_select' => array(),
2111 'ar_cache_from' => array(),
2112 'ar_cache_join' => array(),
2113 'ar_cache_where' => array(),
2114 'ar_cache_like' => array(),
2115 'ar_cache_groupby' => array(),
2116 'ar_cache_having' => array(),
2117 'ar_cache_orderby' => array(),
2118 'ar_cache_set' => array(),
2119 'ar_cache_exists' => array(),
2120 'ar_cache_no_escape' => array()
2121 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002122 }
2123
2124 // --------------------------------------------------------------------
2125
2126 /**
2127 * Merge Cache
2128 *
Barry Mienydd671972010-10-04 16:33:58 +02002129 * When called, this function merges any cached AR arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002130 * locally called ones.
2131 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002132 * @return void
2133 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002134 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002135 {
2136 if (count($this->ar_cache_exists) == 0)
2137 {
2138 return;
2139 }
2140
2141 foreach ($this->ar_cache_exists as $val)
2142 {
2143 $ar_variable = 'ar_'.$val;
2144 $ar_cache_var = 'ar_cache_'.$val;
2145
2146 if (count($this->$ar_cache_var) == 0)
2147 {
2148 continue;
2149 }
2150
2151 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
2152 }
2153
2154 // If we are "protecting identifiers" we need to examine the "from"
2155 // portion of the query to determine if there are any aliases
2156 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
2157 {
2158 $this->_track_aliases($this->ar_from);
2159 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002160
2161 $this->ar_no_escape = $this->ar_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002162 }
Kyle Farris0c147b32011-08-26 02:29:31 -04002163
2164 // --------------------------------------------------------------------
2165
2166 /**
2167 * Reset Active Record values.
2168 *
2169 * Publicly-visible method to reset the AR values.
2170 *
2171 * @access public
2172 * @return void
2173 */
2174 public function reset_query()
2175 {
2176 $this->_reset_select();
2177 $this->_reset_write();
2178 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002179
2180 // --------------------------------------------------------------------
2181
2182 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002183 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002184 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002185 * @param array An array of fields to reset
2186 * @return void
2187 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002188 protected function _reset_run($ar_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002189 {
2190 foreach ($ar_reset_items as $item => $default_value)
2191 {
2192 if ( ! in_array($item, $this->ar_store_array))
2193 {
2194 $this->$item = $default_value;
2195 }
2196 }
2197 }
2198
2199 // --------------------------------------------------------------------
2200
2201 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002202 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002203 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002204 * @return void
2205 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002206 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002207 {
2208 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06002209 'ar_select' => array(),
2210 'ar_from' => array(),
2211 'ar_join' => array(),
2212 'ar_where' => array(),
2213 'ar_like' => array(),
2214 'ar_groupby' => array(),
2215 'ar_having' => array(),
2216 'ar_orderby' => array(),
2217 'ar_wherein' => array(),
2218 'ar_aliased_tables' => array(),
2219 'ar_no_escape' => array(),
2220 'ar_distinct' => FALSE,
2221 'ar_limit' => FALSE,
2222 'ar_offset' => FALSE,
2223 'ar_order' => FALSE,
2224 );
Barry Mienydd671972010-10-04 16:33:58 +02002225
Derek Allard2067d1a2008-11-13 22:59:24 +00002226 $this->_reset_run($ar_reset_items);
2227 }
Barry Mienydd671972010-10-04 16:33:58 +02002228
Derek Allard2067d1a2008-11-13 22:59:24 +00002229 // --------------------------------------------------------------------
2230
2231 /**
2232 * Resets the active record "write" values.
2233 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002234 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002235 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002236 * @return void
2237 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002238 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002239 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002240 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06002241 'ar_set' => array(),
2242 'ar_from' => array(),
2243 'ar_where' => array(),
2244 'ar_like' => array(),
2245 'ar_orderby' => array(),
2246 'ar_keys' => array(),
2247 'ar_limit' => FALSE,
2248 'ar_order' => FALSE
2249 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002250
2251 $this->_reset_run($ar_reset_items);
2252 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002253}
2254
2255/* End of file DB_active_rec.php */
Kyle Farris2de2fa02011-08-31 11:52:20 -04002256/* Location: ./system/database/DB_active_rec.php */