blob: de8cfc1ca5478a52332e34830439dd52a18ee50d [file] [log] [blame]
Andrey Andreev24276a32012-01-08 02:44:38 +02001<?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
WanWizard7219c072011-12-28 14:09:05 +01008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
WanWizard7219c072011-12-28 14:09:05 +010010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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
WanWizard7219c072011-12-28 14:09:05 +010043 protected $return_delete_sql = FALSE;
44 protected $reset_delete_data = FALSE;
45
Andrey Andreev24276a32012-01-08 02:44:38 +020046 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();
WanWizard7219c072011-12-28 14:09:05 +010061 protected $ar_aliased_tables = array();
Andrey Andreev24276a32012-01-08 02:44:38 +020062 protected $ar_store_array = array();
WanWizard7219c072011-12-28 14:09:05 +010063 protected $ar_where_group_started = FALSE;
64 protected $ar_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +020065
Derek Allard2067d1a2008-11-13 22:59:24 +000066 // Active Record Caching variables
WanWizard7219c072011-12-28 14:09:05 +010067 protected $ar_caching = FALSE;
68 protected $ar_cache_exists = array();
69 protected $ar_cache_select = array();
70 protected $ar_cache_from = array();
71 protected $ar_cache_join = array();
72 protected $ar_cache_where = array();
73 protected $ar_cache_like = array();
74 protected $ar_cache_groupby = array();
75 protected $ar_cache_having = array();
76 protected $ar_cache_orderby = array();
77 protected $ar_cache_set = array();
78
79 protected $ar_no_escape = array();
Andrey Andreev24276a32012-01-08 02:44:38 +020080 protected $ar_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000081
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 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 return $this;
116 }
117
118 // --------------------------------------------------------------------
119
120 /**
121 * Select Max
122 *
123 * Generates a SELECT MAX(field) portion of a query
124 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 * @param string the field
126 * @param string an alias
127 * @return object
128 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600129 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
131 return $this->_max_min_avg_sum($select, $alias, 'MAX');
132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 // --------------------------------------------------------------------
135
136 /**
137 * Select Min
138 *
139 * Generates a SELECT MIN(field) portion of a query
140 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 * @param string the field
142 * @param string an alias
143 * @return object
144 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600145 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
147 return $this->_max_min_avg_sum($select, $alias, 'MIN');
148 }
149
150 // --------------------------------------------------------------------
151
152 /**
153 * Select Average
154 *
155 * Generates a SELECT AVG(field) portion of a query
156 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 * @param string the field
158 * @param string an alias
159 * @return object
160 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600161 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
163 return $this->_max_min_avg_sum($select, $alias, 'AVG');
164 }
165
166 // --------------------------------------------------------------------
167
168 /**
169 * Select Sum
170 *
171 * Generates a SELECT SUM(field) portion of a query
172 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 * @param string the field
174 * @param string an alias
175 * @return object
176 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600177 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
179 return $this->_max_min_avg_sum($select, $alias, 'SUM');
180 }
181
182 // --------------------------------------------------------------------
183
184 /**
185 * Processing Function for the four functions above:
186 *
187 * select_max()
188 * select_min()
189 * select_avg()
Andrey Andreev24276a32012-01-08 02:44:38 +0200190 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200191 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 * @param string the field
193 * @param string an alias
194 * @return object
195 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600196 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
198 if ( ! is_string($select) OR $select == '')
199 {
200 $this->display_error('db_invalid_query');
201 }
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
206 {
207 show_error('Invalid function type: '.$type);
208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 if ($alias == '')
211 {
212 $alias = $this->_create_alias_from_table(trim($select));
213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200215 $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->protect_identifiers(trim($alias));
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 $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 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200239 $item = explode('.', $item);
240 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 return $item;
244 }
245
246 // --------------------------------------------------------------------
247
248 /**
249 * DISTINCT
250 *
251 * Sets a flag which tells the query string compiler to add DISTINCT
252 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 * @param bool
254 * @return object
255 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600256 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
258 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
259 return $this;
260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 // --------------------------------------------------------------------
263
264 /**
265 * From
266 *
267 * Generates the FROM portion of the query
268 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 * @param mixed can be a string or array
270 * @return object
271 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600272 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
274 foreach ((array)$from as $val)
275 {
276 if (strpos($val, ',') !== FALSE)
277 {
278 foreach (explode(',', $val) as $v)
279 {
280 $v = trim($v);
281 $this->_track_aliases($v);
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200282 $v = $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 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200286 $this->ar_cache_from[] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 $this->ar_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200288 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
291 else
292 {
293 $val = trim($val);
294
Andrey Andreev24276a32012-01-08 02:44:38 +0200295 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200296 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 $this->_track_aliases($val);
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200298 $this->ar_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 if ($this->ar_caching === TRUE)
301 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200302 $this->ar_cache_from[] = $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 $this->ar_cache_exists[] = 'from';
304 }
305 }
306 }
307
308 return $this;
309 }
310
311 // --------------------------------------------------------------------
312
313 /**
314 * Join
315 *
316 * Generates the JOIN portion of the query
317 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 * @param string
319 * @param string the join condition
320 * @param string the type of join
321 * @return object
322 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600323 public function join($table, $cond, $type = '')
Barry Mienydd671972010-10-04 16:33:58 +0200324 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 if ($type != '')
326 {
327 $type = strtoupper(trim($type));
328
329 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
330 {
331 $type = '';
332 }
333 else
334 {
335 $type .= ' ';
336 }
337 }
338
Andrey Andreev24276a32012-01-08 02:44:38 +0200339 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200340 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 $this->_track_aliases($table);
342
343 // Strip apart the condition and protect the identifiers
Hamza Bhattid3c1ccf2012-03-05 22:58:56 +0400344 if (preg_match('/([\[\w\.]+)([\W\s]+)(.+)/', $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200346 $cond = $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 // Assemble the JOIN statement
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200350 $this->ar_join[] = $join = $type.'JOIN '.$this->protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 if ($this->ar_caching === TRUE)
353 {
354 $this->ar_cache_join[] = $join;
355 $this->ar_cache_exists[] = 'join';
356 }
357
358 return $this;
359 }
360
361 // --------------------------------------------------------------------
362
363 /**
364 * Where
365 *
366 * Generates the WHERE portion of the query. Separates
367 * multiple calls with AND
368 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 * @param mixed
370 * @param mixed
371 * @return object
372 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600373 public function where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
375 return $this->_where($key, $value, 'AND ', $escape);
376 }
Barry Mienydd671972010-10-04 16:33:58 +0200377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 // --------------------------------------------------------------------
379
380 /**
381 * OR Where
382 *
383 * Generates the WHERE portion of the query. Separates
384 * multiple calls with OR
385 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 * @param mixed
387 * @param mixed
388 * @return object
389 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600390 public function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
392 return $this->_where($key, $value, 'OR ', $escape);
393 }
394
395 // --------------------------------------------------------------------
396
397 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 * Where
399 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600400 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 * @param mixed
403 * @param mixed
404 * @param string
405 * @return object
406 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600407 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
WanWizard7219c072011-12-28 14:09:05 +0100409 $type = $this->_group_get_type($type);
410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 if ( ! is_array($key))
412 {
413 $key = array($key => $value);
414 }
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 // If the escape value was not set will will base it on the global setting
417 if ( ! is_bool($escape))
418 {
419 $escape = $this->_protect_identifiers;
420 }
421
422 foreach ($key as $k => $v)
423 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200424 $prefix = (count($this->ar_where) === 0 AND count($this->ar_cache_where) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000425
426 if (is_null($v) && ! $this->_has_operator($k))
427 {
428 // value appears not to have been set, assign the test to IS NULL
429 $k .= ' IS NULL';
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 if ( ! is_null($v))
433 {
434 if ($escape === TRUE)
435 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200436 $k = $this->protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 $v = ' '.$this->escape($v);
438 }
WanWizard7219c072011-12-28 14:09:05 +0100439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 if ( ! $this->_has_operator($k))
441 {
Greg Akere156c6e2011-04-20 16:03:04 -0500442 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 }
444 }
445 else
446 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200447 $k = $this->protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
449
450 $this->ar_where[] = $prefix.$k.$v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 if ($this->ar_caching === TRUE)
452 {
453 $this->ar_cache_where[] = $prefix.$k.$v;
454 $this->ar_cache_exists[] = 'where';
455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 return $this;
460 }
461
462 // --------------------------------------------------------------------
463
464 /**
465 * Where_in
466 *
467 * Generates a WHERE field IN ('item', 'item') SQL query joined with
468 * AND if appropriate
469 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * @param string The field to search
471 * @param array The values searched on
472 * @return object
473 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600474 public function where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
476 return $this->_where_in($key, $values);
477 }
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 // --------------------------------------------------------------------
480
481 /**
482 * Where_in_or
483 *
484 * Generates a WHERE field IN ('item', 'item') SQL query joined with
485 * OR if appropriate
486 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 * @param string The field to search
488 * @param array The values searched on
489 * @return object
490 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600491 public function or_where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 {
493 return $this->_where_in($key, $values, FALSE, 'OR ');
494 }
495
496 // --------------------------------------------------------------------
497
498 /**
499 * Where_not_in
500 *
501 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
502 * with AND if appropriate
503 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 * @param string The field to search
505 * @param array The values searched on
506 * @return object
507 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600508 public function where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 {
510 return $this->_where_in($key, $values, TRUE);
511 }
Barry Mienydd671972010-10-04 16:33:58 +0200512
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 // --------------------------------------------------------------------
514
515 /**
516 * Where_not_in_or
517 *
518 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
519 * with OR if appropriate
520 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 * @param string The field to search
522 * @param array The values searched on
523 * @return object
524 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600525 public function or_where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 {
527 return $this->_where_in($key, $values, TRUE, 'OR ');
528 }
529
530 // --------------------------------------------------------------------
531
532 /**
533 * Where_in
534 *
535 * Called by where_in, where_in_or, where_not_in, where_not_in_or
536 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 * @param string The field to search
538 * @param array The values searched on
539 * @param boolean If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200540 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 * @return object
542 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600543 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 {
545 if ($key === NULL OR $values === NULL)
546 {
547 return;
548 }
Barry Mienydd671972010-10-04 16:33:58 +0200549
WanWizard7219c072011-12-28 14:09:05 +0100550 $type = $this->_group_get_type($type);
551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 if ( ! is_array($values))
553 {
554 $values = array($values);
555 }
Barry Mienydd671972010-10-04 16:33:58 +0200556
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 $not = ($not) ? ' NOT' : '';
558
559 foreach ($values as $value)
560 {
561 $this->ar_wherein[] = $this->escape($value);
562 }
563
Andrey Andreev24276a32012-01-08 02:44:38 +0200564 $prefix = (count($this->ar_where) === 0) ? '' : $type;
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200565 $this->ar_where[] = $where_in = $prefix.$this->protect_identifiers($key).$not.' IN ('.implode(', ', $this->ar_wherein).') ';
Barry Mienydd671972010-10-04 16:33:58 +0200566
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 if ($this->ar_caching === TRUE)
568 {
569 $this->ar_cache_where[] = $where_in;
570 $this->ar_cache_exists[] = 'where';
571 }
572
573 // reset the array for multiple calls
574 $this->ar_wherein = array();
575 return $this;
576 }
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 // --------------------------------------------------------------------
579
580 /**
581 * Like
582 *
583 * Generates a %LIKE% portion of the query. Separates
584 * multiple calls with AND
585 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 * @param mixed
587 * @param mixed
588 * @return object
589 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600590 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 {
592 return $this->_like($field, $match, 'AND ', $side);
593 }
594
595 // --------------------------------------------------------------------
596
597 /**
598 * Not Like
599 *
600 * Generates a NOT LIKE portion of the query. Separates
601 * multiple calls with AND
602 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 * @param mixed
604 * @param mixed
605 * @return object
606 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600607 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 {
609 return $this->_like($field, $match, 'AND ', $side, 'NOT');
610 }
Barry Mienydd671972010-10-04 16:33:58 +0200611
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 // --------------------------------------------------------------------
613
614 /**
615 * OR Like
616 *
617 * Generates a %LIKE% portion of the query. Separates
618 * multiple calls with OR
619 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 * @param mixed
621 * @param mixed
622 * @return object
623 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600624 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 {
626 return $this->_like($field, $match, 'OR ', $side);
627 }
628
629 // --------------------------------------------------------------------
630
631 /**
632 * OR Not Like
633 *
634 * Generates a NOT LIKE portion of the query. Separates
635 * multiple calls with OR
636 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 * @param mixed
638 * @param mixed
639 * @return object
640 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600641 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 {
643 return $this->_like($field, $match, 'OR ', $side, 'NOT');
644 }
Barry Mienydd671972010-10-04 16:33:58 +0200645
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 // --------------------------------------------------------------------
647
648 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 * Like
650 *
651 * Called by like() or orlike()
652 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 * @param mixed
654 * @param mixed
655 * @param string
656 * @return object
657 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600658 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 {
WanWizard7219c072011-12-28 14:09:05 +0100660 $type = $this->_group_get_type($type);
661
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 if ( ! is_array($field))
663 {
664 $field = array($field => $match);
665 }
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 foreach ($field as $k => $v)
668 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200669 $k = $this->protect_identifiers($k);
Andrey Andreev24276a32012-01-08 02:44:38 +0200670 $prefix = (count($this->ar_like) === 0) ? '' : $type;
Derek Jonese4ed5832009-02-20 21:44:59 +0000671 $v = $this->escape_like_str($v);
Kyle Farris0c147b32011-08-26 02:29:31 -0400672
Andrey Andreev24276a32012-01-08 02:44:38 +0200673 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400674 {
675 $like_statement = $prefix." $k $not LIKE '{$v}'";
676 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200677 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 {
679 $like_statement = $prefix." $k $not LIKE '%{$v}'";
680 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200681 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 {
683 $like_statement = $prefix." $k $not LIKE '{$v}%'";
684 }
685 else
686 {
687 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
688 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600689
Derek Jonese4ed5832009-02-20 21:44:59 +0000690 // some platforms require an escape sequence definition for LIKE wildcards
691 if ($this->_like_escape_str != '')
692 {
Greg Aker0d424892010-01-26 02:14:44 +0000693 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000694 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600695
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 $this->ar_like[] = $like_statement;
697 if ($this->ar_caching === TRUE)
698 {
699 $this->ar_cache_like[] = $like_statement;
700 $this->ar_cache_exists[] = 'like';
701 }
Barry Mienydd671972010-10-04 16:33:58 +0200702
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200704
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 return $this;
706 }
Barry Mienydd671972010-10-04 16:33:58 +0200707
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 // --------------------------------------------------------------------
709
710 /**
WanWizard7219c072011-12-28 14:09:05 +0100711 * Starts a query group.
712 *
713 * @param string (Internal use only)
714 * @param string (Internal use only)
715 * @return object
716 */
717 public function group_start($not = '', $type = 'AND ')
718 {
719 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100720 $this->ar_where_group_started = TRUE;
Andrey Andreev24276a32012-01-08 02:44:38 +0200721 $prefix = (count($this->ar_where) === 0 AND count($this->ar_cache_where) === 0) ? '' : $type;
722 $this->ar_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->ar_where_group_count).' (';
WanWizard7219c072011-12-28 14:09:05 +0100723
WanWizard7219c072011-12-28 14:09:05 +0100724 if ($this->ar_caching)
725 {
726 $this->ar_cache_where[] = $value;
727 }
728
729 return $this;
730 }
731
732 // --------------------------------------------------------------------
733
734 /**
735 * Starts a query group, but ORs the group
736 *
737 * @return object
738 */
739 public function or_group_start()
740 {
741 return $this->group_start('', 'OR ');
742 }
743
744 // --------------------------------------------------------------------
745
746 /**
747 * Starts a query group, but NOTs the group
748 *
749 * @return object
750 */
751 public function not_group_start()
752 {
753 return $this->group_start('NOT ', 'AND ');
754 }
755
756 // --------------------------------------------------------------------
757
758 /**
759 * Starts a query group, but OR NOTs the group
760 *
761 * @return object
762 */
763 public function or_not_group_start()
764 {
765 return $this->group_start('NOT ', 'OR ');
766 }
767
768 // --------------------------------------------------------------------
769
770 /**
771 * Ends a query group
772 *
773 * @return object
774 */
775 public function group_end()
776 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200777 $this->ar_where_group_started = FALSE;
778 $this->ar_where[] = $value = str_repeat(' ', $this->ar_where_group_count--) . ')';
WanWizard7219c072011-12-28 14:09:05 +0100779
WanWizard7219c072011-12-28 14:09:05 +0100780 if ($this->ar_caching)
781 {
782 $this->ar_cache_where[] = $value;
783 }
784
WanWizard7219c072011-12-28 14:09:05 +0100785 return $this;
786 }
787
788 // --------------------------------------------------------------------
789
790 /**
791 * Group_get_type
792 *
793 * Called by group_start(), _like(), _where() and _where_in()
794 *
795 * @param string
796 * @return string
797 */
798 protected function _group_get_type($type)
799 {
800 if ($this->ar_where_group_started)
801 {
802 $type = '';
803 $this->ar_where_group_started = FALSE;
804 }
805
806 return $type;
807 }
808
809 // --------------------------------------------------------------------
810
811 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 * GROUP BY
813 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 * @param string
815 * @return object
816 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600817 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 {
819 if (is_string($by))
820 {
821 $by = explode(',', $by);
822 }
Barry Mienydd671972010-10-04 16:33:58 +0200823
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 foreach ($by as $val)
825 {
826 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200827
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 if ($val != '')
829 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200830 $this->ar_groupby[] = $val = $this->protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200831
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 if ($this->ar_caching === TRUE)
833 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200834 $this->ar_cache_groupby[] = $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 $this->ar_cache_exists[] = 'groupby';
836 }
837 }
838 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200839
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 return $this;
841 }
842
843 // --------------------------------------------------------------------
844
845 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 * Sets the HAVING value
847 *
848 * Separates multiple calls with AND
849 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 * @param string
851 * @param string
852 * @return object
853 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600854 public function having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 {
856 return $this->_having($key, $value, 'AND ', $escape);
857 }
Barry Mienydd671972010-10-04 16:33:58 +0200858
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 // --------------------------------------------------------------------
860
861 /**
862 * Sets the OR HAVING value
863 *
864 * Separates multiple calls with OR
865 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 * @param string
867 * @param string
868 * @return object
869 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600870 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 {
872 return $this->_having($key, $value, 'OR ', $escape);
873 }
Barry Mienydd671972010-10-04 16:33:58 +0200874
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 // --------------------------------------------------------------------
876
877 /**
878 * Sets the HAVING values
879 *
880 * Called by having() or or_having()
881 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 * @param string
883 * @param string
884 * @return object
885 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600886 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 {
888 if ( ! is_array($key))
889 {
890 $key = array($key => $value);
891 }
Barry Mienydd671972010-10-04 16:33:58 +0200892
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 foreach ($key as $k => $v)
894 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200895 $prefix = (count($this->ar_having) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000896
897 if ($escape === TRUE)
898 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200899 $k = $this->protect_identifiers($k);
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 }
901
902 if ( ! $this->_has_operator($k))
903 {
904 $k .= ' = ';
905 }
906
907 if ($v != '')
908 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400909 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 }
Barry Mienydd671972010-10-04 16:33:58 +0200911
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 $this->ar_having[] = $prefix.$k.$v;
913 if ($this->ar_caching === TRUE)
914 {
915 $this->ar_cache_having[] = $prefix.$k.$v;
916 $this->ar_cache_exists[] = 'having';
917 }
918 }
Barry Mienydd671972010-10-04 16:33:58 +0200919
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 return $this;
921 }
Barry Mienydd671972010-10-04 16:33:58 +0200922
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 // --------------------------------------------------------------------
924
925 /**
926 * Sets the ORDER BY value
927 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 * @param string
929 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100930 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 * @return object
932 */
pporlan2c685fb2011-12-22 12:15:25 +0100933 public function order_by($orderby, $direction = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200935 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 {
937 $orderby = ''; // Random results want or don't need a field name
938 $direction = $this->_random_keyword;
939 }
940 elseif (trim($direction) != '')
941 {
942 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
943 }
Barry Mienydd671972010-10-04 16:33:58 +0200944
945
Andrey Andreev24276a32012-01-08 02:44:38 +0200946 if ((strpos($orderby, ',') !== FALSE) && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 {
948 $temp = array();
949 foreach (explode(',', $orderby) as $part)
950 {
951 $part = trim($part);
952 if ( ! in_array($part, $this->ar_aliased_tables))
953 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200954 $part = $this->protect_identifiers(trim($part));
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 }
Barry Mienydd671972010-10-04 16:33:58 +0200956
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 $temp[] = $part;
958 }
Barry Mienydd671972010-10-04 16:33:58 +0200959
960 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200962 elseif ($direction != $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
pporlan2c685fb2011-12-22 12:15:25 +0100964 if ($escape === TRUE)
965 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200966 $orderby = $this->protect_identifiers($orderby);
pporlan2c685fb2011-12-22 12:15:25 +0100967 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 }
Barry Mienydd671972010-10-04 16:33:58 +0200969
Andrey Andreev24276a32012-01-08 02:44:38 +0200970 $this->ar_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200971
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 if ($this->ar_caching === TRUE)
973 {
974 $this->ar_cache_orderby[] = $orderby_statement;
975 $this->ar_cache_exists[] = 'orderby';
976 }
977
978 return $this;
979 }
Barry Mienydd671972010-10-04 16:33:58 +0200980
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 // --------------------------------------------------------------------
982
983 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 * Sets the LIMIT value
985 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 * @param integer the limit value
987 * @param integer the offset value
988 * @return object
989 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200990 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 {
Kyle Farris76116012011-08-31 11:17:48 -0400992 $this->ar_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000993
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200994 if ( ! is_null($offset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 {
Kyle Farris76116012011-08-31 11:17:48 -0400996 $this->ar_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 }
Barry Mienydd671972010-10-04 16:33:58 +0200998
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 return $this;
1000 }
Barry Mienydd671972010-10-04 16:33:58 +02001001
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 // --------------------------------------------------------------------
1003
1004 /**
1005 * Sets the OFFSET value
1006 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 * @param integer the offset value
1008 * @return object
1009 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001010 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 {
kenjis0e857632011-09-02 08:41:17 +09001012 $this->ar_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 return $this;
1014 }
Barry Mienydd671972010-10-04 16:33:58 +02001015
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 // --------------------------------------------------------------------
1017
1018 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001019 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 * @param mixed
1022 * @param string
1023 * @param boolean
1024 * @return object
1025 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001026 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 {
1028 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001029
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 if ( ! is_array($key))
1031 {
1032 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001033 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001034
1035 foreach ($key as $k => $v)
1036 {
1037 if ($escape === FALSE)
1038 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001039 $this->ar_set[$this->protect_identifiers($k)] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 }
1041 else
1042 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001043 $this->ar_set[$this->protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 }
1045 }
Barry Mienydd671972010-10-04 16:33:58 +02001046
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 return $this;
1048 }
WanWizard7219c072011-12-28 14:09:05 +01001049
Kyle Farris0c147b32011-08-26 02:29:31 -04001050 // --------------------------------------------------------------------
1051
1052 /**
1053 * Get SELECT query string
1054 *
1055 * Compiles a SELECT query string and returns the sql.
1056 *
1057 * @access public
1058 * @param string the table name to select from (optional)
1059 * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone
1060 * @return string
1061 */
WanWizard7219c072011-12-28 14:09:05 +01001062 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001063 {
1064 if ($table != '')
kylefarris0a3176b2011-08-26 02:37:52 -04001065 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001066 $this->_track_aliases($table);
1067 $this->from($table);
1068 }
WanWizard7219c072011-12-28 14:09:05 +01001069
Kyle Farris0c147b32011-08-26 02:29:31 -04001070 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001071
Kyle Farris0c147b32011-08-26 02:29:31 -04001072 if ($reset === TRUE)
1073 {
1074 $this->_reset_select();
1075 }
WanWizard7219c072011-12-28 14:09:05 +01001076
Kyle Farris0c147b32011-08-26 02:29:31 -04001077 return $select;
1078 }
WanWizard7219c072011-12-28 14:09:05 +01001079
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 // --------------------------------------------------------------------
1081
1082 /**
1083 * Get
1084 *
1085 * Compiles the select statement based on the other functions called
1086 * and runs the query
1087 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 * @param string the table
1089 * @param string the limit clause
1090 * @param string the offset clause
1091 * @return object
1092 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001093 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 {
1095 if ($table != '')
1096 {
1097 $this->_track_aliases($table);
1098 $this->from($table);
1099 }
Barry Mienydd671972010-10-04 16:33:58 +02001100
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 if ( ! is_null($limit))
1102 {
1103 $this->limit($limit, $offset);
1104 }
Barry Mienydd671972010-10-04 16:33:58 +02001105
Andrey Andreev24276a32012-01-08 02:44:38 +02001106 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 $this->_reset_select();
1108 return $result;
1109 }
1110
1111 /**
1112 * "Count All Results" query
1113 *
Barry Mienydd671972010-10-04 16:33:58 +02001114 * Generates a platform-specific query string that counts all records
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 * returned by an Active Record query.
1116 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 * @param string
1118 * @return string
1119 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001120 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 {
1122 if ($table != '')
1123 {
1124 $this->_track_aliases($table);
1125 $this->from($table);
1126 }
Barry Mienydd671972010-10-04 16:33:58 +02001127
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001128 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001130
Purwandi1d160e72012-01-09 16:33:28 +07001131 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001133 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 }
1135
Purwandi1d160e72012-01-09 16:33:28 +07001136 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001137 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 // --------------------------------------------------------------------
1140
1141 /**
1142 * Get_Where
1143 *
1144 * Allows the where clause, limit and offset to be added directly
1145 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 * @param string the where clause
1147 * @param string the limit clause
1148 * @param string the offset clause
1149 * @return object
1150 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001151 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 {
1153 if ($table != '')
1154 {
1155 $this->from($table);
1156 }
1157
1158 if ( ! is_null($where))
1159 {
1160 $this->where($where);
1161 }
Barry Mienydd671972010-10-04 16:33:58 +02001162
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 if ( ! is_null($limit))
1164 {
1165 $this->limit($limit, $offset);
1166 }
Barry Mienydd671972010-10-04 16:33:58 +02001167
Andrey Andreev24276a32012-01-08 02:44:38 +02001168 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 $this->_reset_select();
1170 return $result;
1171 }
1172
1173 // --------------------------------------------------------------------
1174
1175 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001176 * Insert_Batch
1177 *
1178 * Compiles batch insert strings and runs the queries
1179 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001180 * @param string the table to retrieve the results from
1181 * @param array an associative array of insert values
1182 * @return object
1183 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001184 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001185 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001186 if ( ! is_null($set))
1187 {
1188 $this->set_insert_batch($set);
1189 }
Barry Mienydd671972010-10-04 16:33:58 +02001190
Andrey Andreev24276a32012-01-08 02:44:38 +02001191 if (count($this->ar_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001192 {
1193 if ($this->db_debug)
1194 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001195 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001196 return $this->display_error('db_must_use_set');
1197 }
1198 return FALSE;
1199 }
1200
1201 if ($table == '')
1202 {
1203 if ( ! isset($this->ar_from[0]))
1204 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001205 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001206 }
Barry Mienydd671972010-10-04 16:33:58 +02001207
Derek Jonesd10e8962010-03-02 17:10:36 -06001208 $table = $this->ar_from[0];
1209 }
1210
1211 // Batch this baby
Andrey Andreev24276a32012-01-08 02:44:38 +02001212 for ($i = 0, $total = count($this->ar_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001213 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001214 $this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001215 }
Barry Mienydd671972010-10-04 16:33:58 +02001216
Derek Jonesd10e8962010-03-02 17:10:36 -06001217 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001218 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001219 }
1220
1221 // --------------------------------------------------------------------
1222
1223 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001224 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001225 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001226 * @param mixed
1227 * @param string
1228 * @param boolean
1229 * @return object
1230 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001231 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001232 {
1233 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001234
Derek Jonesd10e8962010-03-02 17:10:36 -06001235 if ( ! is_array($key))
1236 {
1237 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001238 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001239
1240 $keys = array_keys(current($key));
1241 sort($keys);
1242
1243 foreach ($key as $row)
1244 {
Barry Mienydd671972010-10-04 16:33:58 +02001245 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1246 {
1247 // batch function above returns an error on an empty array
1248 $this->ar_set[] = array();
1249 return;
1250 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001251
Barry Mienydd671972010-10-04 16:33:58 +02001252 ksort($row); // puts $row in the same order as our keys
1253
Derek Jonesd10e8962010-03-02 17:10:36 -06001254 if ($escape === FALSE)
1255 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001256 $this->ar_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001257 }
1258 else
1259 {
1260 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001261 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001262 {
Barry Mienydd671972010-10-04 16:33:58 +02001263 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001264 }
1265
Derek Jones37f4b9c2011-07-01 17:56:50 -05001266 $this->ar_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001267 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001268 }
1269
1270 foreach ($keys as $k)
1271 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001272 $this->ar_keys[] = $this->protect_identifiers($k);
Derek Jonesd10e8962010-03-02 17:10:36 -06001273 }
Barry Mienydd671972010-10-04 16:33:58 +02001274
Derek Jonesd10e8962010-03-02 17:10:36 -06001275 return $this;
1276 }
WanWizard7219c072011-12-28 14:09:05 +01001277
Kyle Farris0c147b32011-08-26 02:29:31 -04001278 // --------------------------------------------------------------------
1279
1280 /**
1281 * Get INSERT query string
1282 *
1283 * Compiles an insert query and returns the sql
1284 *
1285 * @access public
1286 * @param string the table to insert into
1287 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1288 * @return string
1289 */
1290 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001291 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001292 if ($this->_validate_insert($table) === FALSE)
1293 {
1294 return FALSE;
1295 }
WanWizard7219c072011-12-28 14:09:05 +01001296
Kyle Farris76116012011-08-31 11:17:48 -04001297 $sql = $this->_insert(
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001298 $this->protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE),
WanWizard7219c072011-12-28 14:09:05 +01001299 array_keys($this->ar_set),
Kyle Farris76116012011-08-31 11:17:48 -04001300 array_values($this->ar_set)
1301 );
WanWizard7219c072011-12-28 14:09:05 +01001302
Kyle Farris0c147b32011-08-26 02:29:31 -04001303 if ($reset === TRUE)
1304 {
1305 $this->_reset_write();
1306 }
WanWizard7219c072011-12-28 14:09:05 +01001307
Kyle Farris0c147b32011-08-26 02:29:31 -04001308 return $sql;
1309 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001310
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 // --------------------------------------------------------------------
1312
1313 /**
1314 * Insert
1315 *
1316 * Compiles an insert string and runs the query
1317 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001318 * @access public
Phil Sturgeon9789f322011-07-15 15:14:05 -06001319 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001320 * @param array an associative array of insert values
1321 * @return object
1322 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001323 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001324 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 if ( ! is_null($set))
1326 {
1327 $this->set($set);
1328 }
WanWizard7219c072011-12-28 14:09:05 +01001329
Kyle Farris0c147b32011-08-26 02:29:31 -04001330 if ($this->_validate_insert($table) === FALSE)
1331 {
1332 return FALSE;
1333 }
WanWizard7219c072011-12-28 14:09:05 +01001334
Kyle Farris76116012011-08-31 11:17:48 -04001335 $sql = $this->_insert(
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001336 $this->protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE),
WanWizard7219c072011-12-28 14:09:05 +01001337 array_keys($this->ar_set),
Kyle Farris76116012011-08-31 11:17:48 -04001338 array_values($this->ar_set)
1339 );
Barry Mienydd671972010-10-04 16:33:58 +02001340
Kyle Farris0c147b32011-08-26 02:29:31 -04001341 $this->_reset_write();
1342 return $this->query($sql);
1343 }
WanWizard7219c072011-12-28 14:09:05 +01001344
Kyle Farris0c147b32011-08-26 02:29:31 -04001345 // --------------------------------------------------------------------
1346
1347 /**
1348 * Validate Insert
1349 *
1350 * This method is used by both insert() and get_compiled_insert() to
1351 * validate that the there data is actually being set and that table
1352 * has been chosen to be inserted into.
1353 *
1354 * @access public
1355 * @param string the table to insert data into
1356 * @return string
1357 */
WanWizard7219c072011-12-28 14:09:05 +01001358 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001359 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001360 if (count($this->ar_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001362 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 }
1364
1365 if ($table == '')
1366 {
1367 if ( ! isset($this->ar_from[0]))
1368 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001369 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001370 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001372 else
1373 {
1374 $this->ar_from[0] = $table;
1375 }
WanWizard7219c072011-12-28 14:09:05 +01001376
Kyle Farris0c147b32011-08-26 02:29:31 -04001377 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 }
Barry Mienydd671972010-10-04 16:33:58 +02001379
Phil Sturgeon9789f322011-07-15 15:14:05 -06001380 // --------------------------------------------------------------------
1381
1382 /**
1383 * Replace
1384 *
1385 * Compiles an replace into string and runs the query
1386 *
1387 * @param string the table to replace data into
1388 * @param array an associative array of insert values
1389 * @return object
1390 */
1391 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001392 {
1393 if ( ! is_null($set))
1394 {
1395 $this->set($set);
1396 }
Barry Mienydd671972010-10-04 16:33:58 +02001397
Andrey Andreev24276a32012-01-08 02:44:38 +02001398 if (count($this->ar_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001399 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001400 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001401 }
1402
1403 if ($table == '')
1404 {
1405 if ( ! isset($this->ar_from[0]))
1406 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001407 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001408 }
Barry Mienydd671972010-10-04 16:33:58 +02001409
Derek Jonesd10e8962010-03-02 17:10:36 -06001410 $table = $this->ar_from[0];
1411 }
1412
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001413 $sql = $this->_replace($this->protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
Derek Jonesd10e8962010-03-02 17:10:36 -06001414 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001415 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001416 }
WanWizard7219c072011-12-28 14:09:05 +01001417
Kyle Farris0c147b32011-08-26 02:29:31 -04001418 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001419
Kyle Farris0c147b32011-08-26 02:29:31 -04001420 /**
1421 * Get UPDATE query string
1422 *
1423 * Compiles an update query and returns the sql
1424 *
1425 * @access public
1426 * @param string the table to update
1427 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1428 * @return string
1429 */
1430 public function get_compiled_update($table = '', $reset = TRUE)
1431 {
1432 // Combine any cached components with the current statements
1433 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001434
Kyle Farris0c147b32011-08-26 02:29:31 -04001435 if ($this->_validate_update($table) === FALSE)
1436 {
1437 return FALSE;
1438 }
WanWizard7219c072011-12-28 14:09:05 +01001439
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001440 $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);
WanWizard7219c072011-12-28 14:09:05 +01001441
Kyle Farris0c147b32011-08-26 02:29:31 -04001442 if ($reset === TRUE)
1443 {
1444 $this->_reset_write();
1445 }
WanWizard7219c072011-12-28 14:09:05 +01001446
Kyle Farris0c147b32011-08-26 02:29:31 -04001447 return $sql;
1448 }
WanWizard7219c072011-12-28 14:09:05 +01001449
Derek Allard2067d1a2008-11-13 22:59:24 +00001450 // --------------------------------------------------------------------
1451
1452 /**
1453 * Update
1454 *
1455 * Compiles an update string and runs the query
1456 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001457 * @param string the table to retrieve the results from
1458 * @param array an associative array of update values
1459 * @param mixed the where clause
1460 * @return object
1461 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001462 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001463 {
1464 // Combine any cached components with the current statements
1465 $this->_merge_cache();
1466
1467 if ( ! is_null($set))
1468 {
1469 $this->set($set);
1470 }
Barry Mienydd671972010-10-04 16:33:58 +02001471
Kyle Farris0c147b32011-08-26 02:29:31 -04001472 if ($this->_validate_update($table) === FALSE)
1473 {
1474 return FALSE;
1475 }
1476
1477 if ($where != NULL)
1478 {
1479 $this->where($where);
1480 }
1481
1482 if ($limit != NULL)
1483 {
1484 $this->limit($limit);
1485 }
1486
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001487 $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 -04001488 $this->_reset_write();
1489 return $this->query($sql);
1490 }
WanWizard7219c072011-12-28 14:09:05 +01001491
Kyle Farris0c147b32011-08-26 02:29:31 -04001492 // --------------------------------------------------------------------
1493
1494 /**
1495 * Validate Update
1496 *
1497 * This method is used by both update() and get_compiled_update() to
1498 * validate that data is actually being set and that a table has been
1499 * chosen to be update.
1500 *
1501 * @access public
1502 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001503 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001504 */
1505 protected function _validate_update($table = '')
1506 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001507 if (count($this->ar_set) == 0)
1508 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001509 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001510 }
1511
1512 if ($table == '')
1513 {
1514 if ( ! isset($this->ar_from[0]))
1515 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001516 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001517 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001518 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001519 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001520 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001521 $this->ar_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001522 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001523
1524 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 }
WanWizard7219c072011-12-28 14:09:05 +01001526
Derek Jonesd10e8962010-03-02 17:10:36 -06001527 // --------------------------------------------------------------------
1528
1529 /**
1530 * Update_Batch
1531 *
1532 * Compiles an update string and runs the query
1533 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001534 * @param string the table to retrieve the results from
1535 * @param array an associative array of update values
1536 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001537 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001538 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001539 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001540 {
1541 // Combine any cached components with the current statements
1542 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001543
Derek Jonesd10e8962010-03-02 17:10:36 -06001544 if (is_null($index))
1545 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001546 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001547 }
1548
1549 if ( ! is_null($set))
1550 {
1551 $this->set_update_batch($set, $index);
1552 }
1553
Andrey Andreev24276a32012-01-08 02:44:38 +02001554 if (count($this->ar_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001555 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001556 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001557 }
1558
1559 if ($table == '')
1560 {
1561 if ( ! isset($this->ar_from[0]))
1562 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001563 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001564 }
Barry Mienydd671972010-10-04 16:33:58 +02001565
Derek Jonesd10e8962010-03-02 17:10:36 -06001566 $table = $this->ar_from[0];
1567 }
Barry Mienydd671972010-10-04 16:33:58 +02001568
Derek Jonesd10e8962010-03-02 17:10:36 -06001569 // Batch this baby
Andrey Andreev24276a32012-01-08 02:44:38 +02001570 for ($i = 0, $total = count($this->ar_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001571 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001572 $this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->ar_set, $i, 100), $this->protect_identifiers($index), $this->ar_where));
Derek Jonesd10e8962010-03-02 17:10:36 -06001573 }
Barry Mienydd671972010-10-04 16:33:58 +02001574
Derek Jonesd10e8962010-03-02 17:10:36 -06001575 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001576 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001577 }
1578
1579 // --------------------------------------------------------------------
1580
1581 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001582 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001583 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001584 * @param array
1585 * @param string
1586 * @param boolean
1587 * @return object
1588 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001589 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001590 {
1591 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001592
Derek Jonesd10e8962010-03-02 17:10:36 -06001593 if ( ! is_array($key))
1594 {
1595 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001596 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001597
1598 foreach ($key as $k => $v)
1599 {
1600 $index_set = FALSE;
1601 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001602 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001603 {
1604 if ($k2 == $index)
1605 {
1606 $index_set = TRUE;
1607 }
1608 else
1609 {
1610 $not[] = $k.'-'.$v;
1611 }
1612
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001613 $clean[$this->protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001614 }
1615
1616 if ($index_set == FALSE)
1617 {
1618 return $this->display_error('db_batch_missing_index');
1619 }
1620
1621 $this->ar_set[] = $clean;
1622 }
Barry Mienydd671972010-10-04 16:33:58 +02001623
Derek Jonesd10e8962010-03-02 17:10:36 -06001624 return $this;
1625 }
1626
Derek Allard2067d1a2008-11-13 22:59:24 +00001627 // --------------------------------------------------------------------
1628
1629 /**
1630 * Empty Table
1631 *
1632 * Compiles a delete string and runs "DELETE FROM table"
1633 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001634 * @param string the table to empty
1635 * @return object
1636 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001637 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001638 {
1639 if ($table == '')
1640 {
1641 if ( ! isset($this->ar_from[0]))
1642 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001643 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001644 }
1645
1646 $table = $this->ar_from[0];
1647 }
1648 else
1649 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001650 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001651 }
1652
1653 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001654 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001655 return $this->query($sql);
1656 }
1657
1658 // --------------------------------------------------------------------
1659
1660 /**
1661 * Truncate
1662 *
1663 * Compiles a truncate string and runs the query
1664 * If the database does not support the truncate() command
1665 * This function maps to "DELETE FROM table"
1666 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001667 * @param string the table to truncate
1668 * @return object
1669 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001670 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001671 {
1672 if ($table == '')
1673 {
1674 if ( ! isset($this->ar_from[0]))
1675 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001676 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001677 }
1678
1679 $table = $this->ar_from[0];
1680 }
1681 else
1682 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001683 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001684 }
1685
1686 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001687 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001688 return $this->query($sql);
1689 }
WanWizard7219c072011-12-28 14:09:05 +01001690
Kyle Farris0c147b32011-08-26 02:29:31 -04001691 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001692
Kyle Farris0c147b32011-08-26 02:29:31 -04001693 /**
1694 * Get DELETE query string
1695 *
1696 * Compiles a delete query string and returns the sql
1697 *
1698 * @access public
1699 * @param string the table to delete from
1700 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1701 * @return string
1702 */
1703 public function get_compiled_delete($table = '', $reset = TRUE)
1704 {
1705 $this->return_delete_sql = TRUE;
1706 $sql = $this->delete($table, '', NULL, $reset);
1707 $this->return_delete_sql = FALSE;
1708 return $sql;
1709 }
WanWizard7219c072011-12-28 14:09:05 +01001710
Derek Allard2067d1a2008-11-13 22:59:24 +00001711 // --------------------------------------------------------------------
1712
1713 /**
1714 * Delete
1715 *
1716 * Compiles a delete string and runs the query
1717 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001718 * @param mixed the table(s) to delete from. String or array
1719 * @param mixed the where clause
1720 * @param mixed the limit clause
1721 * @param boolean
1722 * @return object
1723 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001724 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001725 {
1726 // Combine any cached components with the current statements
1727 $this->_merge_cache();
1728
1729 if ($table == '')
1730 {
1731 if ( ! isset($this->ar_from[0]))
1732 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001733 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001734 }
1735
1736 $table = $this->ar_from[0];
1737 }
1738 elseif (is_array($table))
1739 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001740 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001741 {
1742 $this->delete($single_table, $where, $limit, FALSE);
1743 }
1744
1745 $this->_reset_write();
1746 return;
1747 }
1748 else
1749 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001750 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001751 }
1752
1753 if ($where != '')
1754 {
1755 $this->where($where);
1756 }
1757
1758 if ($limit != NULL)
1759 {
1760 $this->limit($limit);
1761 }
1762
Andrey Andreev24276a32012-01-08 02:44:38 +02001763 if (count($this->ar_where) === 0 && count($this->ar_wherein) === 0 && count($this->ar_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001764 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001765 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001766 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001767
1768 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001769 if ($reset_data)
1770 {
1771 $this->_reset_write();
1772 }
WanWizard7219c072011-12-28 14:09:05 +01001773
Andrey Andreev24276a32012-01-08 02:44:38 +02001774 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001775 }
WanWizard7219c072011-12-28 14:09:05 +01001776
Derek Allard2067d1a2008-11-13 22:59:24 +00001777 // --------------------------------------------------------------------
1778
1779 /**
1780 * DB Prefix
1781 *
1782 * Prepends a database prefix if one exists in configuration
1783 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001784 * @param string the table
1785 * @return string
1786 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001787 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 {
1789 if ($table == '')
1790 {
1791 $this->display_error('db_table_name_required');
1792 }
1793
1794 return $this->dbprefix.$table;
1795 }
1796
1797 // --------------------------------------------------------------------
1798
1799 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001800 * Set DB Prefix
1801 *
1802 * Set's the DB Prefix to something new without needing to reconnect
1803 *
1804 * @param string the prefix
1805 * @return string
1806 */
1807 public function set_dbprefix($prefix = '')
1808 {
1809 return $this->dbprefix = $prefix;
1810 }
1811
1812 // --------------------------------------------------------------------
1813
1814 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001815 * Track Aliases
1816 *
1817 * Used to track SQL statements written with aliased tables.
1818 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001819 * @param string The table to inspect
1820 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001821 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001822 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001823 {
1824 if (is_array($table))
1825 {
1826 foreach ($table as $t)
1827 {
1828 $this->_track_aliases($t);
1829 }
1830 return;
1831 }
Barry Mienydd671972010-10-04 16:33:58 +02001832
Derek Jones37f4b9c2011-07-01 17:56:50 -05001833 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001834 // the string into discreet statements
1835 if (strpos($table, ',') !== FALSE)
1836 {
1837 return $this->_track_aliases(explode(',', $table));
1838 }
Barry Mienydd671972010-10-04 16:33:58 +02001839
Derek Allard2067d1a2008-11-13 22:59:24 +00001840 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001841 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001842 {
1843 // if the alias is written with the AS keyword, remove it
1844 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001845
Derek Allard2067d1a2008-11-13 22:59:24 +00001846 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001847 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001848
Derek Allard2067d1a2008-11-13 22:59:24 +00001849 // Store the alias, if it doesn't already exist
1850 if ( ! in_array($table, $this->ar_aliased_tables))
1851 {
1852 $this->ar_aliased_tables[] = $table;
1853 }
1854 }
1855 }
WanWizard7219c072011-12-28 14:09:05 +01001856
Derek Allard2067d1a2008-11-13 22:59:24 +00001857 // --------------------------------------------------------------------
1858
1859 /**
1860 * Compile the SELECT statement
1861 *
1862 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001863 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001864 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001865 * @return string
1866 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001867 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001868 {
1869 // Combine any cached components with the current statements
1870 $this->_merge_cache();
1871
Derek Allard2067d1a2008-11-13 22:59:24 +00001872 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001873 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
Andrey Andreev24276a32012-01-08 02:44:38 +02001881 if (count($this->ar_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001882 {
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;
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001893 $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
Derek Allard2067d1a2008-11-13 22:59:24 +00001900 // Write the "FROM" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001901 if (count($this->ar_from) > 0)
1902 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001903 $sql .= "\nFROM ".$this->_from_tables($this->ar_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00001904 }
1905
Derek Allard2067d1a2008-11-13 22:59:24 +00001906 // Write the "JOIN" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001907 if (count($this->ar_join) > 0)
1908 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001909 $sql .= "\n".implode("\n", $this->ar_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00001910 }
1911
Derek Allard2067d1a2008-11-13 22:59:24 +00001912 // Write the "WHERE" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001913 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1914 {
Greg Akere156c6e2011-04-20 16:03:04 -05001915 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00001916 }
1917
1918 $sql .= implode("\n", $this->ar_where);
1919
Derek Allard2067d1a2008-11-13 22:59:24 +00001920 // Write the "LIKE" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001921 if (count($this->ar_like) > 0)
1922 {
1923 if (count($this->ar_where) > 0)
1924 {
1925 $sql .= "\nAND ";
1926 }
1927
1928 $sql .= implode("\n", $this->ar_like);
1929 }
1930
Derek Allard2067d1a2008-11-13 22:59:24 +00001931 // Write the "GROUP BY" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001932 if (count($this->ar_groupby) > 0)
1933 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001934 $sql .= "\nGROUP BY ".implode(', ', $this->ar_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00001935 }
1936
Derek Allard2067d1a2008-11-13 22:59:24 +00001937 // Write the "HAVING" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001938 if (count($this->ar_having) > 0)
1939 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001940 $sql .= "\nHAVING ".implode("\n", $this->ar_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00001941 }
1942
Derek Allard2067d1a2008-11-13 22:59:24 +00001943 // Write the "ORDER BY" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001944 if (count($this->ar_orderby) > 0)
1945 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001946 $sql .= "\nORDER BY ".implode(', ', $this->ar_orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00001947 if ($this->ar_order !== FALSE)
1948 {
1949 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02001950 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001951 }
1952
Derek Allard2067d1a2008-11-13 22:59:24 +00001953 // Write the "LIMIT" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001954 if (is_numeric($this->ar_limit))
1955 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001956 return $this->_limit($sql."\n", $this->ar_limit, $this->ar_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00001957 }
1958
1959 return $sql;
1960 }
1961
1962 // --------------------------------------------------------------------
1963
1964 /**
1965 * Object to Array
1966 *
1967 * Takes an object as input and converts the class variables to array key/vals
1968 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001969 * @param object
1970 * @return array
1971 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001972 public function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001973 {
1974 if ( ! is_object($object))
1975 {
1976 return $object;
1977 }
Barry Mienydd671972010-10-04 16:33:58 +02001978
Derek Allard2067d1a2008-11-13 22:59:24 +00001979 $array = array();
1980 foreach (get_object_vars($object) as $key => $val)
1981 {
1982 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06001983 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00001984 {
1985 $array[$key] = $val;
1986 }
1987 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001988
1989 return $array;
1990 }
Barry Mienydd671972010-10-04 16:33:58 +02001991
Derek Jonesd10e8962010-03-02 17:10:36 -06001992 // --------------------------------------------------------------------
1993
1994 /**
1995 * Object to Array
1996 *
1997 * Takes an object as input and converts the class variables to array key/vals
1998 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001999 * @param object
2000 * @return array
2001 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002002 public function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002003 {
2004 if ( ! is_object($object))
2005 {
2006 return $object;
2007 }
Barry Mienydd671972010-10-04 16:33:58 +02002008
Derek Jonesd10e8962010-03-02 17:10:36 -06002009 $array = array();
2010 $out = get_object_vars($object);
2011 $fields = array_keys($out);
2012
2013 foreach ($fields as $val)
2014 {
2015 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002016 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002017 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002018 $i = 0;
2019 foreach ($out[$val] as $data)
2020 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002021 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002022 }
2023 }
2024 }
2025
Derek Allard2067d1a2008-11-13 22:59:24 +00002026 return $array;
2027 }
Barry Mienydd671972010-10-04 16:33:58 +02002028
Derek Allard2067d1a2008-11-13 22:59:24 +00002029 // --------------------------------------------------------------------
2030
2031 /**
2032 * Start Cache
2033 *
2034 * Starts AR caching
2035 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002036 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002037 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002038 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002039 {
2040 $this->ar_caching = TRUE;
2041 }
2042
2043 // --------------------------------------------------------------------
2044
2045 /**
2046 * Stop Cache
2047 *
2048 * Stops AR caching
2049 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002050 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002051 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002052 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 {
2054 $this->ar_caching = FALSE;
2055 }
2056
2057 // --------------------------------------------------------------------
2058
2059 /**
2060 * Flush Cache
2061 *
2062 * Empties the AR cache
2063 *
2064 * @access public
2065 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002066 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002067 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002068 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002069 $this->_reset_run(array(
2070 'ar_cache_select' => array(),
2071 'ar_cache_from' => array(),
2072 'ar_cache_join' => array(),
2073 'ar_cache_where' => array(),
2074 'ar_cache_like' => array(),
2075 'ar_cache_groupby' => array(),
2076 'ar_cache_having' => array(),
2077 'ar_cache_orderby' => array(),
2078 'ar_cache_set' => array(),
2079 'ar_cache_exists' => array(),
2080 'ar_cache_no_escape' => array()
2081 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002082 }
2083
2084 // --------------------------------------------------------------------
2085
2086 /**
2087 * Merge Cache
2088 *
Barry Mienydd671972010-10-04 16:33:58 +02002089 * When called, this function merges any cached AR arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002090 * locally called ones.
2091 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002092 * @return void
2093 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002094 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002095 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002096 if (count($this->ar_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002097 {
2098 return;
2099 }
2100
2101 foreach ($this->ar_cache_exists as $val)
2102 {
2103 $ar_variable = 'ar_'.$val;
2104 $ar_cache_var = 'ar_cache_'.$val;
2105
Andrey Andreev24276a32012-01-08 02:44:38 +02002106 if (count($this->$ar_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002107 {
2108 continue;
2109 }
2110
2111 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
2112 }
2113
2114 // If we are "protecting identifiers" we need to examine the "from"
2115 // portion of the query to determine if there are any aliases
2116 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
2117 {
2118 $this->_track_aliases($this->ar_from);
2119 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002120
2121 $this->ar_no_escape = $this->ar_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002122 }
WanWizard7219c072011-12-28 14:09:05 +01002123
Kyle Farris0c147b32011-08-26 02:29:31 -04002124 // --------------------------------------------------------------------
2125
2126 /**
2127 * Reset Active Record values.
WanWizard7219c072011-12-28 14:09:05 +01002128 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002129 * Publicly-visible method to reset the AR values.
2130 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002131 * @return void
2132 */
2133 public function reset_query()
2134 {
2135 $this->_reset_select();
2136 $this->_reset_write();
2137 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002138
2139 // --------------------------------------------------------------------
2140
2141 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002142 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002143 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002144 * @param array An array of fields to reset
2145 * @return void
2146 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002147 protected function _reset_run($ar_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002148 {
2149 foreach ($ar_reset_items as $item => $default_value)
2150 {
2151 if ( ! in_array($item, $this->ar_store_array))
2152 {
2153 $this->$item = $default_value;
2154 }
2155 }
2156 }
2157
2158 // --------------------------------------------------------------------
2159
2160 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002161 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002162 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002163 * @return void
2164 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002165 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002166 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002167 $this->_reset_run(array(
2168 'ar_select' => array(),
2169 'ar_from' => array(),
2170 'ar_join' => array(),
2171 'ar_where' => array(),
2172 'ar_like' => array(),
2173 'ar_groupby' => array(),
2174 'ar_having' => array(),
2175 'ar_orderby' => array(),
2176 'ar_wherein' => array(),
2177 'ar_aliased_tables' => array(),
2178 'ar_no_escape' => array(),
2179 'ar_distinct' => FALSE,
2180 'ar_limit' => FALSE,
2181 'ar_offset' => FALSE,
2182 'ar_order' => FALSE
2183 )
2184 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002185 }
Barry Mienydd671972010-10-04 16:33:58 +02002186
Derek Allard2067d1a2008-11-13 22:59:24 +00002187 // --------------------------------------------------------------------
2188
2189 /**
2190 * Resets the active record "write" values.
2191 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002192 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002193 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002194 * @return void
2195 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002196 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002197 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002198 $this->_reset_run(array(
2199 'ar_set' => array(),
2200 'ar_from' => array(),
2201 'ar_where' => array(),
2202 'ar_like' => array(),
2203 'ar_orderby' => array(),
2204 'ar_keys' => array(),
2205 'ar_limit' => FALSE,
2206 'ar_order' => FALSE
2207 )
2208 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002209 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002210
Derek Allard2067d1a2008-11-13 22:59:24 +00002211}
2212
2213/* End of file DB_active_rec.php */
WanWizard7219c072011-12-28 14:09:05 +01002214/* Location: ./system/database/DB_active_rec.php */