blob: 575f097dd2a95d41ac3308cd9572e90dfb7eb4ba [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
Kyle Farris76116012011-08-31 11:17:48 -040046 protected $ar_select = array();
47 protected $ar_distinct = FALSE;
Andrey Andreev24276a32012-01-08 02:44:38 +020048 protected $ar_from = array();
49 protected $ar_join = array();
50 protected $ar_where = array();
51 protected $ar_like = array();
Kyle Farris76116012011-08-31 11:17:48 -040052 protected $ar_groupby = array();
53 protected $ar_having = array();
Andrey Andreev24276a32012-01-08 02:44:38 +020054 protected $ar_keys = array();
55 protected $ar_limit = FALSE;
Kyle Farris76116012011-08-31 11:17:48 -040056 protected $ar_offset = FALSE;
Andrey Andreev24276a32012-01-08 02:44:38 +020057 protected $ar_order = FALSE;
Kyle Farris76116012011-08-31 11:17:48 -040058 protected $ar_orderby = array();
Andrey Andreev24276a32012-01-08 02:44:38 +020059 protected $ar_set = array();
Kyle Farris76116012011-08-31 11:17:48 -040060 protected $ar_wherein = array();
WanWizard7219c072011-12-28 14:09:05 +010061 protected $ar_aliased_tables = array();
Kyle Farris76116012011-08-31 11:17:48 -040062 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();
Derek Allard2067d1a2008-11-13 22:59:24 +000078
WanWizard7219c072011-12-28 14:09:05 +010079 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
Juan José González8b4d83b2011-09-27 17:21:14 -0500215 $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;
Greg Aker827f3de2011-05-06 11:29:57 -0500217 $this->ar_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 if ($this->ar_caching === TRUE)
220 {
221 $this->ar_cache_select[] = $sql;
222 $this->ar_cache_exists[] = 'select';
223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 return $this;
226 }
227
228 // --------------------------------------------------------------------
229
230 /**
231 * Determines the alias name based on the table
232 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 * @param string
234 * @return string
235 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600236 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
238 if (strpos($item, '.') !== FALSE)
239 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200240 $item = explode('.', $item);
241 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 return $item;
245 }
246
247 // --------------------------------------------------------------------
248
249 /**
250 * DISTINCT
251 *
252 * Sets a flag which tells the query string compiler to add DISTINCT
253 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 * @param bool
255 * @return object
256 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600257 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
259 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
260 return $this;
261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 // --------------------------------------------------------------------
264
265 /**
266 * From
267 *
268 * Generates the FROM portion of the query
269 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 * @param mixed can be a string or array
271 * @return object
272 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600273 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 {
275 foreach ((array)$from as $val)
276 {
277 if (strpos($val, ',') !== FALSE)
278 {
279 foreach (explode(',', $val) as $v)
280 {
281 $v = trim($v);
282 $this->_track_aliases($v);
Andrey Andreev0f7decc2012-01-08 04:40:29 +0200283 $v = $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 if ($this->ar_caching === TRUE)
286 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200287 $this->ar_cache_from[] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 $this->ar_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200289 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
292 else
293 {
294 $val = trim($val);
295
Andrey Andreev24276a32012-01-08 02:44:38 +0200296 // 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);
Andrey Andreev24276a32012-01-08 02:44:38 +0200299 $this->ar_from[] = $val = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 if ($this->ar_caching === TRUE)
302 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200303 $this->ar_cache_from[] = $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 $this->ar_cache_exists[] = 'from';
305 }
306 }
307 }
308
309 return $this;
310 }
311
312 // --------------------------------------------------------------------
313
314 /**
315 * Join
316 *
317 * Generates the JOIN portion of the query
318 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 * @param string
320 * @param string the join condition
321 * @param string the type of join
322 * @return object
323 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600324 public function join($table, $cond, $type = '')
Barry Mienydd671972010-10-04 16:33:58 +0200325 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 if ($type != '')
327 {
328 $type = strtoupper(trim($type));
329
330 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
331 {
332 $type = '';
333 }
334 else
335 {
336 $type .= ' ';
337 }
338 }
339
Andrey Andreev24276a32012-01-08 02:44:38 +0200340 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200341 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 $this->_track_aliases($table);
343
344 // Strip apart the condition and protect the identifiers
345 if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
346 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200347 $cond = $this->_protect_identifiers($match[1]).$match[2].$this->_protect_identifiers($match[3]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 // Assemble the JOIN statement
Andrey Andreev24276a32012-01-08 02:44:38 +0200351 $this->ar_join[] = $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 if ($this->ar_caching === TRUE)
354 {
355 $this->ar_cache_join[] = $join;
356 $this->ar_cache_exists[] = 'join';
357 }
358
359 return $this;
360 }
361
362 // --------------------------------------------------------------------
363
364 /**
365 * Where
366 *
367 * Generates the WHERE portion of the query. Separates
368 * multiple calls with AND
369 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 * @param mixed
371 * @param mixed
372 * @return object
373 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600374 public function where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
376 return $this->_where($key, $value, 'AND ', $escape);
377 }
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 // --------------------------------------------------------------------
380
381 /**
382 * OR Where
383 *
384 * Generates the WHERE portion of the query. Separates
385 * multiple calls with OR
386 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 * @param mixed
388 * @param mixed
389 * @return object
390 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600391 public function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
393 return $this->_where($key, $value, 'OR ', $escape);
394 }
395
396 // --------------------------------------------------------------------
397
398 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 * Where
400 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600401 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 * @param mixed
404 * @param mixed
405 * @param string
406 * @return object
407 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600408 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
WanWizard7219c072011-12-28 14:09:05 +0100410 $type = $this->_group_get_type($type);
411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 if ( ! is_array($key))
413 {
414 $key = array($key => $value);
415 }
Barry Mienydd671972010-10-04 16:33:58 +0200416
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 // If the escape value was not set will will base it on the global setting
418 if ( ! is_bool($escape))
419 {
420 $escape = $this->_protect_identifiers;
421 }
422
423 foreach ($key as $k => $v)
424 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200425 $prefix = (count($this->ar_where) === 0 AND count($this->ar_cache_where) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000426
427 if (is_null($v) && ! $this->_has_operator($k))
428 {
429 // value appears not to have been set, assign the test to IS NULL
430 $k .= ' IS NULL';
431 }
Barry Mienydd671972010-10-04 16:33:58 +0200432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 if ( ! is_null($v))
434 {
435 if ($escape === TRUE)
436 {
437 $k = $this->_protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 $v = ' '.$this->escape($v);
439 }
WanWizard7219c072011-12-28 14:09:05 +0100440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 if ( ! $this->_has_operator($k))
442 {
Greg Akere156c6e2011-04-20 16:03:04 -0500443 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 }
445 }
446 else
447 {
Barry Mienydd671972010-10-04 16:33:58 +0200448 $k = $this->_protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
450
451 $this->ar_where[] = $prefix.$k.$v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 if ($this->ar_caching === TRUE)
453 {
454 $this->ar_cache_where[] = $prefix.$k.$v;
455 $this->ar_cache_exists[] = 'where';
456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 return $this;
461 }
462
463 // --------------------------------------------------------------------
464
465 /**
466 * Where_in
467 *
468 * Generates a WHERE field IN ('item', 'item') SQL query joined with
469 * AND if appropriate
470 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 * @param string The field to search
472 * @param array The values searched on
473 * @return object
474 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600475 public function where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 {
477 return $this->_where_in($key, $values);
478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 // --------------------------------------------------------------------
481
482 /**
483 * Where_in_or
484 *
485 * Generates a WHERE field IN ('item', 'item') SQL query joined with
486 * OR if appropriate
487 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 * @param string The field to search
489 * @param array The values searched on
490 * @return object
491 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600492 public function or_where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
494 return $this->_where_in($key, $values, FALSE, 'OR ');
495 }
496
497 // --------------------------------------------------------------------
498
499 /**
500 * Where_not_in
501 *
502 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
503 * with AND if appropriate
504 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 * @param string The field to search
506 * @param array The values searched on
507 * @return object
508 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600509 public function where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 {
511 return $this->_where_in($key, $values, TRUE);
512 }
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 // --------------------------------------------------------------------
515
516 /**
517 * Where_not_in_or
518 *
519 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
520 * with OR if appropriate
521 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 * @param string The field to search
523 * @param array The values searched on
524 * @return object
525 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600526 public function or_where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 {
528 return $this->_where_in($key, $values, TRUE, 'OR ');
529 }
530
531 // --------------------------------------------------------------------
532
533 /**
534 * Where_in
535 *
536 * Called by where_in, where_in_or, where_not_in, where_not_in_or
537 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 * @param string The field to search
539 * @param array The values searched on
540 * @param boolean If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200541 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 * @return object
543 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600544 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 {
546 if ($key === NULL OR $values === NULL)
547 {
548 return;
549 }
Barry Mienydd671972010-10-04 16:33:58 +0200550
WanWizard7219c072011-12-28 14:09:05 +0100551 $type = $this->_group_get_type($type);
552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 if ( ! is_array($values))
554 {
555 $values = array($values);
556 }
Barry Mienydd671972010-10-04 16:33:58 +0200557
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 $not = ($not) ? ' NOT' : '';
559
560 foreach ($values as $value)
561 {
562 $this->ar_wherein[] = $this->escape($value);
563 }
564
Andrey Andreev24276a32012-01-08 02:44:38 +0200565 $prefix = (count($this->ar_where) === 0) ? '' : $type;
566 $this->ar_where[] = $where_in = $prefix.$this->_protect_identifiers($key).$not.' IN ('.implode(', ', $this->ar_wherein).') ';
Barry Mienydd671972010-10-04 16:33:58 +0200567
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 if ($this->ar_caching === TRUE)
569 {
570 $this->ar_cache_where[] = $where_in;
571 $this->ar_cache_exists[] = 'where';
572 }
573
574 // reset the array for multiple calls
575 $this->ar_wherein = array();
576 return $this;
577 }
Barry Mienydd671972010-10-04 16:33:58 +0200578
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 // --------------------------------------------------------------------
580
581 /**
582 * Like
583 *
584 * Generates a %LIKE% portion of the query. Separates
585 * multiple calls with AND
586 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 * @param mixed
588 * @param mixed
589 * @return object
590 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600591 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
593 return $this->_like($field, $match, 'AND ', $side);
594 }
595
596 // --------------------------------------------------------------------
597
598 /**
599 * Not Like
600 *
601 * Generates a NOT LIKE portion of the query. Separates
602 * multiple calls with AND
603 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 * @param mixed
605 * @param mixed
606 * @return object
607 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600608 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
610 return $this->_like($field, $match, 'AND ', $side, 'NOT');
611 }
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 // --------------------------------------------------------------------
614
615 /**
616 * OR Like
617 *
618 * Generates a %LIKE% portion of the query. Separates
619 * multiple calls with OR
620 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 * @param mixed
622 * @param mixed
623 * @return object
624 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600625 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 {
627 return $this->_like($field, $match, 'OR ', $side);
628 }
629
630 // --------------------------------------------------------------------
631
632 /**
633 * OR Not Like
634 *
635 * Generates a NOT LIKE portion of the query. Separates
636 * multiple calls with OR
637 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 * @param mixed
639 * @param mixed
640 * @return object
641 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600642 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
644 return $this->_like($field, $match, 'OR ', $side, 'NOT');
645 }
Barry Mienydd671972010-10-04 16:33:58 +0200646
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 // --------------------------------------------------------------------
648
649 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 * Like
651 *
652 * Called by like() or orlike()
653 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 * @param mixed
655 * @param mixed
656 * @param string
657 * @return object
658 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600659 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 {
WanWizard7219c072011-12-28 14:09:05 +0100661 $type = $this->_group_get_type($type);
662
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 if ( ! is_array($field))
664 {
665 $field = array($field => $match);
666 }
Barry Mienydd671972010-10-04 16:33:58 +0200667
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 foreach ($field as $k => $v)
669 {
670 $k = $this->_protect_identifiers($k);
Andrey Andreev24276a32012-01-08 02:44:38 +0200671 $prefix = (count($this->ar_like) === 0) ? '' : $type;
Derek Jonese4ed5832009-02-20 21:44:59 +0000672 $v = $this->escape_like_str($v);
Kyle Farris0c147b32011-08-26 02:29:31 -0400673
Andrey Andreev24276a32012-01-08 02:44:38 +0200674 if ($side === 'none')
Nithinea4ad9b2011-08-21 01:23:47 -0300675 {
676 $like_statement = $prefix." $k $not LIKE '{$v}'";
677 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200678 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 {
680 $like_statement = $prefix." $k $not LIKE '%{$v}'";
681 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200682 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
684 $like_statement = $prefix." $k $not LIKE '{$v}%'";
685 }
686 else
687 {
688 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
689 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600690
Derek Jonese4ed5832009-02-20 21:44:59 +0000691 // some platforms require an escape sequence definition for LIKE wildcards
692 if ($this->_like_escape_str != '')
693 {
Greg Aker0d424892010-01-26 02:14:44 +0000694 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000695 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600696
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 $this->ar_like[] = $like_statement;
698 if ($this->ar_caching === TRUE)
699 {
700 $this->ar_cache_like[] = $like_statement;
701 $this->ar_cache_exists[] = 'like';
702 }
Barry Mienydd671972010-10-04 16:33:58 +0200703
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 return $this;
707 }
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 // --------------------------------------------------------------------
710
711 /**
WanWizard7219c072011-12-28 14:09:05 +0100712 * Starts a query group.
713 *
714 * @param string (Internal use only)
715 * @param string (Internal use only)
716 * @return object
717 */
718 public function group_start($not = '', $type = 'AND ')
719 {
720 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100721 $this->ar_where_group_started = TRUE;
Andrey Andreev24276a32012-01-08 02:44:38 +0200722 $prefix = (count($this->ar_where) === 0 AND count($this->ar_cache_where) === 0) ? '' : $type;
723 $this->ar_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->ar_where_group_count).' (';
WanWizard7219c072011-12-28 14:09:05 +0100724
WanWizard7219c072011-12-28 14:09:05 +0100725 if ($this->ar_caching)
726 {
727 $this->ar_cache_where[] = $value;
728 }
729
730 return $this;
731 }
732
733 // --------------------------------------------------------------------
734
735 /**
736 * Starts a query group, but ORs the group
737 *
738 * @return object
739 */
740 public function or_group_start()
741 {
742 return $this->group_start('', 'OR ');
743 }
744
745 // --------------------------------------------------------------------
746
747 /**
748 * Starts a query group, but NOTs the group
749 *
750 * @return object
751 */
752 public function not_group_start()
753 {
754 return $this->group_start('NOT ', 'AND ');
755 }
756
757 // --------------------------------------------------------------------
758
759 /**
760 * Starts a query group, but OR NOTs the group
761 *
762 * @return object
763 */
764 public function or_not_group_start()
765 {
766 return $this->group_start('NOT ', 'OR ');
767 }
768
769 // --------------------------------------------------------------------
770
771 /**
772 * Ends a query group
773 *
774 * @return object
775 */
776 public function group_end()
777 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200778 $this->ar_where_group_started = FALSE;
779 $this->ar_where[] = $value = str_repeat(' ', $this->ar_where_group_count--) . ')';
WanWizard7219c072011-12-28 14:09:05 +0100780
WanWizard7219c072011-12-28 14:09:05 +0100781 if ($this->ar_caching)
782 {
783 $this->ar_cache_where[] = $value;
784 }
785
WanWizard7219c072011-12-28 14:09:05 +0100786 return $this;
787 }
788
789 // --------------------------------------------------------------------
790
791 /**
792 * Group_get_type
793 *
794 * Called by group_start(), _like(), _where() and _where_in()
795 *
796 * @param string
797 * @return string
798 */
799 protected function _group_get_type($type)
800 {
801 if ($this->ar_where_group_started)
802 {
803 $type = '';
804 $this->ar_where_group_started = FALSE;
805 }
806
807 return $type;
808 }
809
810 // --------------------------------------------------------------------
811
812 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 * GROUP BY
814 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 * @param string
816 * @return object
817 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600818 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 {
820 if (is_string($by))
821 {
822 $by = explode(',', $by);
823 }
Barry Mienydd671972010-10-04 16:33:58 +0200824
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 foreach ($by as $val)
826 {
827 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200828
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 if ($val != '')
830 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200831 $this->ar_groupby[] = $val = $this->_protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200832
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 if ($this->ar_caching === TRUE)
834 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200835 $this->ar_cache_groupby[] = $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 $this->ar_cache_exists[] = 'groupby';
837 }
838 }
839 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200840
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 return $this;
842 }
843
844 // --------------------------------------------------------------------
845
846 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 * Sets the HAVING value
848 *
849 * Separates multiple calls with AND
850 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 * @param string
852 * @param string
853 * @return object
854 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600855 public function having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 {
857 return $this->_having($key, $value, 'AND ', $escape);
858 }
Barry Mienydd671972010-10-04 16:33:58 +0200859
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 // --------------------------------------------------------------------
861
862 /**
863 * Sets the OR HAVING value
864 *
865 * Separates multiple calls with OR
866 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 * @param string
868 * @param string
869 * @return object
870 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600871 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 {
873 return $this->_having($key, $value, 'OR ', $escape);
874 }
Barry Mienydd671972010-10-04 16:33:58 +0200875
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 // --------------------------------------------------------------------
877
878 /**
879 * Sets the HAVING values
880 *
881 * Called by having() or or_having()
882 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 * @param string
884 * @param string
885 * @return object
886 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600887 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 {
889 if ( ! is_array($key))
890 {
891 $key = array($key => $value);
892 }
Barry Mienydd671972010-10-04 16:33:58 +0200893
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 foreach ($key as $k => $v)
895 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200896 $prefix = (count($this->ar_having) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000897
898 if ($escape === TRUE)
899 {
900 $k = $this->_protect_identifiers($k);
901 }
902
903 if ( ! $this->_has_operator($k))
904 {
905 $k .= ' = ';
906 }
907
908 if ($v != '')
909 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400910 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 }
Barry Mienydd671972010-10-04 16:33:58 +0200912
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 $this->ar_having[] = $prefix.$k.$v;
914 if ($this->ar_caching === TRUE)
915 {
916 $this->ar_cache_having[] = $prefix.$k.$v;
917 $this->ar_cache_exists[] = 'having';
918 }
919 }
Barry Mienydd671972010-10-04 16:33:58 +0200920
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 return $this;
922 }
Barry Mienydd671972010-10-04 16:33:58 +0200923
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 // --------------------------------------------------------------------
925
926 /**
927 * Sets the ORDER BY value
928 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 * @param string
930 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100931 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 * @return object
933 */
pporlan2c685fb2011-12-22 12:15:25 +0100934 public function order_by($orderby, $direction = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200936 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 {
938 $orderby = ''; // Random results want or don't need a field name
939 $direction = $this->_random_keyword;
940 }
941 elseif (trim($direction) != '')
942 {
943 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
944 }
Barry Mienydd671972010-10-04 16:33:58 +0200945
946
Andrey Andreev24276a32012-01-08 02:44:38 +0200947 if ((strpos($orderby, ',') !== FALSE) && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 {
949 $temp = array();
950 foreach (explode(',', $orderby) as $part)
951 {
952 $part = trim($part);
953 if ( ! in_array($part, $this->ar_aliased_tables))
954 {
955 $part = $this->_protect_identifiers(trim($part));
956 }
Barry Mienydd671972010-10-04 16:33:58 +0200957
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 $temp[] = $part;
959 }
Barry Mienydd671972010-10-04 16:33:58 +0200960
961 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200963 elseif ($direction != $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 {
pporlan2c685fb2011-12-22 12:15:25 +0100965 if ($escape === TRUE)
966 {
967 $orderby = $this->_protect_identifiers($orderby);
968 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 }
Barry Mienydd671972010-10-04 16:33:58 +0200970
Andrey Andreev24276a32012-01-08 02:44:38 +0200971 $this->ar_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200972
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 if ($this->ar_caching === TRUE)
974 {
975 $this->ar_cache_orderby[] = $orderby_statement;
976 $this->ar_cache_exists[] = 'orderby';
977 }
978
979 return $this;
980 }
Barry Mienydd671972010-10-04 16:33:58 +0200981
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 // --------------------------------------------------------------------
983
984 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 * Sets the LIMIT value
986 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 * @param integer the limit value
988 * @param integer the offset value
989 * @return object
990 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200991 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 {
Phil Sturgeonb0eae5f2011-08-10 09:00:52 -0600993 $this->ar_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000994
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200995 if ( ! is_null($offset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 {
Phil Sturgeonb0eae5f2011-08-10 09:00:52 -0600997 $this->ar_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 }
Barry Mienydd671972010-10-04 16:33:58 +0200999
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 return $this;
1001 }
Barry Mienydd671972010-10-04 16:33:58 +02001002
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 // --------------------------------------------------------------------
1004
1005 /**
1006 * Sets the OFFSET value
1007 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 * @param integer the offset value
1009 * @return object
1010 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001011 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 {
kenjis0e857632011-09-02 08:41:17 +09001013 $this->ar_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 return $this;
1015 }
Barry Mienydd671972010-10-04 16:33:58 +02001016
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 // --------------------------------------------------------------------
1018
1019 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001020 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 * @param mixed
1023 * @param string
1024 * @param boolean
1025 * @return object
1026 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001027 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 {
1029 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001030
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 if ( ! is_array($key))
1032 {
1033 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001034 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001035
1036 foreach ($key as $k => $v)
1037 {
1038 if ($escape === FALSE)
1039 {
1040 $this->ar_set[$this->_protect_identifiers($k)] = $v;
1041 }
1042 else
1043 {
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001044 $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 }
1046 }
Barry Mienydd671972010-10-04 16:33:58 +02001047
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 return $this;
1049 }
WanWizard7219c072011-12-28 14:09:05 +01001050
Kyle Farris0c147b32011-08-26 02:29:31 -04001051 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001052
Kyle Farris0c147b32011-08-26 02:29:31 -04001053 /**
1054 * Get SELECT query string
1055 *
1056 * Compiles a SELECT query string and returns the sql.
1057 *
1058 * @access public
1059 * @param string the table name to select from (optional)
1060 * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone
1061 * @return string
1062 */
WanWizard7219c072011-12-28 14:09:05 +01001063 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001064 {
1065 if ($table != '')
kylefarris0a3176b2011-08-26 02:37:52 -04001066 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001067 $this->_track_aliases($table);
1068 $this->from($table);
1069 }
WanWizard7219c072011-12-28 14:09:05 +01001070
Kyle Farris0c147b32011-08-26 02:29:31 -04001071 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001072
Kyle Farris0c147b32011-08-26 02:29:31 -04001073 if ($reset === TRUE)
1074 {
1075 $this->_reset_select();
1076 }
WanWizard7219c072011-12-28 14:09:05 +01001077
Kyle Farris0c147b32011-08-26 02:29:31 -04001078 return $select;
1079 }
WanWizard7219c072011-12-28 14:09:05 +01001080
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 // --------------------------------------------------------------------
1082
1083 /**
1084 * Get
1085 *
1086 * Compiles the select statement based on the other functions called
1087 * and runs the query
1088 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001089 * @param string the table
1090 * @param string the limit clause
1091 * @param string the offset clause
1092 * @return object
1093 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001094 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 {
1096 if ($table != '')
1097 {
1098 $this->_track_aliases($table);
1099 $this->from($table);
1100 }
Barry Mienydd671972010-10-04 16:33:58 +02001101
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 if ( ! is_null($limit))
1103 {
1104 $this->limit($limit, $offset);
1105 }
Barry Mienydd671972010-10-04 16:33:58 +02001106
Andrey Andreev24276a32012-01-08 02:44:38 +02001107 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 $this->_reset_select();
1109 return $result;
1110 }
1111
1112 /**
1113 * "Count All Results" query
1114 *
Barry Mienydd671972010-10-04 16:33:58 +02001115 * Generates a platform-specific query string that counts all records
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 * returned by an Active Record query.
1117 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 * @param string
1119 * @return string
1120 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001121 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 {
1123 if ($table != '')
1124 {
1125 $this->_track_aliases($table);
1126 $this->from($table);
1127 }
Barry Mienydd671972010-10-04 16:33:58 +02001128
Andrey Andreev24276a32012-01-08 02:44:38 +02001129 $result = $this->query($this->_compile_select($this->_count_string.$this->_protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001131
Purwandi1d160e72012-01-09 16:33:28 +07001132 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001134 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 }
1136
Purwandi1d160e72012-01-09 16:33:28 +07001137 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001138 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 // --------------------------------------------------------------------
1141
1142 /**
1143 * Get_Where
1144 *
1145 * Allows the where clause, limit and offset to be added directly
1146 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 * @param string the where clause
1148 * @param string the limit clause
1149 * @param string the offset clause
1150 * @return object
1151 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001152 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 {
1154 if ($table != '')
1155 {
1156 $this->from($table);
1157 }
1158
1159 if ( ! is_null($where))
1160 {
1161 $this->where($where);
1162 }
Barry Mienydd671972010-10-04 16:33:58 +02001163
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 if ( ! is_null($limit))
1165 {
1166 $this->limit($limit, $offset);
1167 }
Barry Mienydd671972010-10-04 16:33:58 +02001168
Andrey Andreev24276a32012-01-08 02:44:38 +02001169 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 $this->_reset_select();
1171 return $result;
1172 }
1173
1174 // --------------------------------------------------------------------
1175
1176 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001177 * Insert_Batch
1178 *
1179 * Compiles batch insert strings and runs the queries
1180 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001181 * @param string the table to retrieve the results from
1182 * @param array an associative array of insert values
1183 * @return object
1184 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001185 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001186 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001187 if ( ! is_null($set))
1188 {
1189 $this->set_insert_batch($set);
1190 }
Barry Mienydd671972010-10-04 16:33:58 +02001191
Andrey Andreev24276a32012-01-08 02:44:38 +02001192 if (count($this->ar_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001193 {
1194 if ($this->db_debug)
1195 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001196 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001197 return $this->display_error('db_must_use_set');
1198 }
1199 return FALSE;
1200 }
1201
1202 if ($table == '')
1203 {
1204 if ( ! isset($this->ar_from[0]))
1205 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001206 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001207 }
Barry Mienydd671972010-10-04 16:33:58 +02001208
Derek Jonesd10e8962010-03-02 17:10:36 -06001209 $table = $this->ar_from[0];
1210 }
1211
1212 // Batch this baby
Andrey Andreev24276a32012-01-08 02:44:38 +02001213 for ($i = 0, $total = count($this->ar_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001214 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001215 $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 -06001216 }
Barry Mienydd671972010-10-04 16:33:58 +02001217
Derek Jonesd10e8962010-03-02 17:10:36 -06001218 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001219 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001220 }
1221
1222 // --------------------------------------------------------------------
1223
1224 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001225 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001226 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001227 * @param mixed
1228 * @param string
1229 * @param boolean
1230 * @return object
1231 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001232 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001233 {
1234 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001235
Derek Jonesd10e8962010-03-02 17:10:36 -06001236 if ( ! is_array($key))
1237 {
1238 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001239 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001240
1241 $keys = array_keys(current($key));
1242 sort($keys);
1243
1244 foreach ($key as $row)
1245 {
Barry Mienydd671972010-10-04 16:33:58 +02001246 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1247 {
1248 // batch function above returns an error on an empty array
1249 $this->ar_set[] = array();
1250 return;
1251 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001252
Barry Mienydd671972010-10-04 16:33:58 +02001253 ksort($row); // puts $row in the same order as our keys
1254
Derek Jonesd10e8962010-03-02 17:10:36 -06001255 if ($escape === FALSE)
1256 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001257 $this->ar_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001258 }
1259 else
1260 {
1261 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001262 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001263 {
Barry Mienydd671972010-10-04 16:33:58 +02001264 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001265 }
1266
Derek Jones37f4b9c2011-07-01 17:56:50 -05001267 $this->ar_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001268 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001269 }
1270
1271 foreach ($keys as $k)
1272 {
1273 $this->ar_keys[] = $this->_protect_identifiers($k);
1274 }
Barry Mienydd671972010-10-04 16:33:58 +02001275
Derek Jonesd10e8962010-03-02 17:10:36 -06001276 return $this;
1277 }
WanWizard7219c072011-12-28 14:09:05 +01001278
Kyle Farris0c147b32011-08-26 02:29:31 -04001279 // --------------------------------------------------------------------
1280
1281 /**
1282 * Get INSERT query string
1283 *
1284 * Compiles an insert query and returns the sql
1285 *
1286 * @access public
1287 * @param string the table to insert into
1288 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1289 * @return string
1290 */
1291 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001292 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001293 if ($this->_validate_insert($table) === FALSE)
1294 {
1295 return FALSE;
1296 }
WanWizard7219c072011-12-28 14:09:05 +01001297
Kyle Farris76116012011-08-31 11:17:48 -04001298 $sql = $this->_insert(
1299 $this->_protect_identifiers(
1300 $this->ar_from[0], TRUE, NULL, FALSE
1301 ),
WanWizard7219c072011-12-28 14:09:05 +01001302 array_keys($this->ar_set),
Kyle Farris76116012011-08-31 11:17:48 -04001303 array_values($this->ar_set)
1304 );
WanWizard7219c072011-12-28 14:09:05 +01001305
Kyle Farris0c147b32011-08-26 02:29:31 -04001306 if ($reset === TRUE)
1307 {
1308 $this->_reset_write();
1309 }
WanWizard7219c072011-12-28 14:09:05 +01001310
Kyle Farris0c147b32011-08-26 02:29:31 -04001311 return $sql;
1312 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001313
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 // --------------------------------------------------------------------
1315
1316 /**
1317 * Insert
1318 *
1319 * Compiles an insert string and runs the query
1320 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001321 * @access public
Phil Sturgeon9789f322011-07-15 15:14:05 -06001322 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 * @param array an associative array of insert values
1324 * @return object
1325 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001326 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001327 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 if ( ! is_null($set))
1329 {
1330 $this->set($set);
1331 }
WanWizard7219c072011-12-28 14:09:05 +01001332
Kyle Farris0c147b32011-08-26 02:29:31 -04001333 if ($this->_validate_insert($table) === FALSE)
1334 {
1335 return FALSE;
1336 }
WanWizard7219c072011-12-28 14:09:05 +01001337
Kyle Farris76116012011-08-31 11:17:48 -04001338 $sql = $this->_insert(
1339 $this->_protect_identifiers(
1340 $this->ar_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001341 ),
1342 array_keys($this->ar_set),
Kyle Farris76116012011-08-31 11:17:48 -04001343 array_values($this->ar_set)
1344 );
Barry Mienydd671972010-10-04 16:33:58 +02001345
Kyle Farris0c147b32011-08-26 02:29:31 -04001346 $this->_reset_write();
1347 return $this->query($sql);
1348 }
WanWizard7219c072011-12-28 14:09:05 +01001349
Kyle Farris0c147b32011-08-26 02:29:31 -04001350 // --------------------------------------------------------------------
1351
1352 /**
1353 * Validate Insert
1354 *
1355 * This method is used by both insert() and get_compiled_insert() to
1356 * validate that the there data is actually being set and that table
1357 * has been chosen to be inserted into.
1358 *
1359 * @access public
1360 * @param string the table to insert data into
1361 * @return string
1362 */
WanWizard7219c072011-12-28 14:09:05 +01001363 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001364 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001365 if (count($this->ar_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001366 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001367 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 }
1369
1370 if ($table == '')
1371 {
1372 if ( ! isset($this->ar_from[0]))
1373 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001374 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001377 else
1378 {
1379 $this->ar_from[0] = $table;
1380 }
WanWizard7219c072011-12-28 14:09:05 +01001381
Kyle Farris0c147b32011-08-26 02:29:31 -04001382 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001383 }
Barry Mienydd671972010-10-04 16:33:58 +02001384
Phil Sturgeon9789f322011-07-15 15:14:05 -06001385 // --------------------------------------------------------------------
1386
1387 /**
1388 * Replace
1389 *
1390 * Compiles an replace into string and runs the query
1391 *
1392 * @param string the table to replace data into
1393 * @param array an associative array of insert values
1394 * @return object
1395 */
1396 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001397 {
1398 if ( ! is_null($set))
1399 {
1400 $this->set($set);
1401 }
Barry Mienydd671972010-10-04 16:33:58 +02001402
Andrey Andreev24276a32012-01-08 02:44:38 +02001403 if (count($this->ar_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001404 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001405 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001406 }
1407
1408 if ($table == '')
1409 {
1410 if ( ! isset($this->ar_from[0]))
1411 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001412 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001413 }
Barry Mienydd671972010-10-04 16:33:58 +02001414
Derek Jonesd10e8962010-03-02 17:10:36 -06001415 $table = $this->ar_from[0];
1416 }
1417
1418 $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 -06001419 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001420 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001421 }
WanWizard7219c072011-12-28 14:09:05 +01001422
Kyle Farris0c147b32011-08-26 02:29:31 -04001423 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001424
Kyle Farris0c147b32011-08-26 02:29:31 -04001425 /**
1426 * Get UPDATE query string
1427 *
1428 * Compiles an update query and returns the sql
1429 *
1430 * @access public
1431 * @param string the table to update
1432 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1433 * @return string
1434 */
1435 public function get_compiled_update($table = '', $reset = TRUE)
1436 {
1437 // Combine any cached components with the current statements
1438 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001439
Kyle Farris0c147b32011-08-26 02:29:31 -04001440 if ($this->_validate_update($table) === FALSE)
1441 {
1442 return FALSE;
1443 }
WanWizard7219c072011-12-28 14:09:05 +01001444
Kyle Farris0c147b32011-08-26 02:29:31 -04001445 $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 +01001446
Kyle Farris0c147b32011-08-26 02:29:31 -04001447 if ($reset === TRUE)
1448 {
1449 $this->_reset_write();
1450 }
WanWizard7219c072011-12-28 14:09:05 +01001451
Kyle Farris0c147b32011-08-26 02:29:31 -04001452 return $sql;
1453 }
WanWizard7219c072011-12-28 14:09:05 +01001454
Derek Allard2067d1a2008-11-13 22:59:24 +00001455 // --------------------------------------------------------------------
1456
1457 /**
1458 * Update
1459 *
1460 * Compiles an update string and runs the query
1461 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001462 * @param string the table to retrieve the results from
1463 * @param array an associative array of update values
1464 * @param mixed the where clause
1465 * @return object
1466 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001467 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001468 {
1469 // Combine any cached components with the current statements
1470 $this->_merge_cache();
1471
1472 if ( ! is_null($set))
1473 {
1474 $this->set($set);
1475 }
Barry Mienydd671972010-10-04 16:33:58 +02001476
Kyle Farris0c147b32011-08-26 02:29:31 -04001477 if ($this->_validate_update($table) === FALSE)
1478 {
1479 return FALSE;
1480 }
1481
1482 if ($where != NULL)
1483 {
1484 $this->where($where);
1485 }
1486
1487 if ($limit != NULL)
1488 {
1489 $this->limit($limit);
1490 }
1491
Mancy0d91fd22011-12-20 13:13:14 +03001492 $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 -04001493 $this->_reset_write();
1494 return $this->query($sql);
1495 }
WanWizard7219c072011-12-28 14:09:05 +01001496
Kyle Farris0c147b32011-08-26 02:29:31 -04001497 // --------------------------------------------------------------------
1498
1499 /**
1500 * Validate Update
1501 *
1502 * This method is used by both update() and get_compiled_update() to
1503 * validate that data is actually being set and that a table has been
1504 * chosen to be update.
1505 *
1506 * @access public
1507 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001508 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001509 */
1510 protected function _validate_update($table = '')
1511 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001512 if (count($this->ar_set) == 0)
1513 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001514 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001515 }
1516
1517 if ($table == '')
1518 {
1519 if ( ! isset($this->ar_from[0]))
1520 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001521 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001522 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001524 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001526 $this->ar_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001527 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001528
1529 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001530 }
WanWizard7219c072011-12-28 14:09:05 +01001531
Derek Jonesd10e8962010-03-02 17:10:36 -06001532 // --------------------------------------------------------------------
1533
1534 /**
1535 * Update_Batch
1536 *
1537 * Compiles an update string and runs the query
1538 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001539 * @param string the table to retrieve the results from
1540 * @param array an associative array of update values
1541 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001542 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001543 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001544 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001545 {
1546 // Combine any cached components with the current statements
1547 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001548
Derek Jonesd10e8962010-03-02 17:10:36 -06001549 if (is_null($index))
1550 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001551 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001552 }
1553
1554 if ( ! is_null($set))
1555 {
1556 $this->set_update_batch($set, $index);
1557 }
1558
Andrey Andreev24276a32012-01-08 02:44:38 +02001559 if (count($this->ar_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001560 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001561 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001562 }
1563
1564 if ($table == '')
1565 {
1566 if ( ! isset($this->ar_from[0]))
1567 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001568 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001569 }
Barry Mienydd671972010-10-04 16:33:58 +02001570
Derek Jonesd10e8962010-03-02 17:10:36 -06001571 $table = $this->ar_from[0];
1572 }
Barry Mienydd671972010-10-04 16:33:58 +02001573
Derek Jonesd10e8962010-03-02 17:10:36 -06001574 // Batch this baby
Andrey Andreev24276a32012-01-08 02:44:38 +02001575 for ($i = 0, $total = count($this->ar_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001576 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001577 $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 -06001578 }
Barry Mienydd671972010-10-04 16:33:58 +02001579
Derek Jonesd10e8962010-03-02 17:10:36 -06001580 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001581 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001582 }
1583
1584 // --------------------------------------------------------------------
1585
1586 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001587 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001588 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001589 * @param array
1590 * @param string
1591 * @param boolean
1592 * @return object
1593 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001594 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001595 {
1596 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001597
Derek Jonesd10e8962010-03-02 17:10:36 -06001598 if ( ! is_array($key))
1599 {
1600 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001601 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001602
1603 foreach ($key as $k => $v)
1604 {
1605 $index_set = FALSE;
1606 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001607 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001608 {
1609 if ($k2 == $index)
1610 {
1611 $index_set = TRUE;
1612 }
1613 else
1614 {
1615 $not[] = $k.'-'.$v;
1616 }
1617
Andrey Andreev24276a32012-01-08 02:44:38 +02001618 $clean[$this->_protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001619 }
1620
1621 if ($index_set == FALSE)
1622 {
1623 return $this->display_error('db_batch_missing_index');
1624 }
1625
1626 $this->ar_set[] = $clean;
1627 }
Barry Mienydd671972010-10-04 16:33:58 +02001628
Derek Jonesd10e8962010-03-02 17:10:36 -06001629 return $this;
1630 }
1631
Derek Allard2067d1a2008-11-13 22:59:24 +00001632 // --------------------------------------------------------------------
1633
1634 /**
1635 * Empty Table
1636 *
1637 * Compiles a delete string and runs "DELETE FROM table"
1638 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001639 * @param string the table to empty
1640 * @return object
1641 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001642 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001643 {
1644 if ($table == '')
1645 {
1646 if ( ! isset($this->ar_from[0]))
1647 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001648 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001649 }
1650
1651 $table = $this->ar_from[0];
1652 }
1653 else
1654 {
1655 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1656 }
1657
1658 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001659 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001660 return $this->query($sql);
1661 }
1662
1663 // --------------------------------------------------------------------
1664
1665 /**
1666 * Truncate
1667 *
1668 * Compiles a truncate string and runs the query
1669 * If the database does not support the truncate() command
1670 * This function maps to "DELETE FROM table"
1671 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001672 * @param string the table to truncate
1673 * @return object
1674 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001675 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001676 {
1677 if ($table == '')
1678 {
1679 if ( ! isset($this->ar_from[0]))
1680 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001681 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001682 }
1683
1684 $table = $this->ar_from[0];
1685 }
1686 else
1687 {
1688 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1689 }
1690
1691 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001692 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001693 return $this->query($sql);
1694 }
WanWizard7219c072011-12-28 14:09:05 +01001695
Kyle Farris0c147b32011-08-26 02:29:31 -04001696 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001697
Kyle Farris0c147b32011-08-26 02:29:31 -04001698 /**
1699 * Get DELETE query string
1700 *
1701 * Compiles a delete query string and returns the sql
1702 *
1703 * @access public
1704 * @param string the table to delete from
1705 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1706 * @return string
1707 */
1708 public function get_compiled_delete($table = '', $reset = TRUE)
1709 {
1710 $this->return_delete_sql = TRUE;
1711 $sql = $this->delete($table, '', NULL, $reset);
1712 $this->return_delete_sql = FALSE;
1713 return $sql;
1714 }
WanWizard7219c072011-12-28 14:09:05 +01001715
Derek Allard2067d1a2008-11-13 22:59:24 +00001716 // --------------------------------------------------------------------
1717
1718 /**
1719 * Delete
1720 *
1721 * Compiles a delete string and runs the query
1722 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001723 * @param mixed the table(s) to delete from. String or array
1724 * @param mixed the where clause
1725 * @param mixed the limit clause
1726 * @param boolean
1727 * @return object
1728 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001729 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001730 {
1731 // Combine any cached components with the current statements
1732 $this->_merge_cache();
1733
1734 if ($table == '')
1735 {
1736 if ( ! isset($this->ar_from[0]))
1737 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001738 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001739 }
1740
1741 $table = $this->ar_from[0];
1742 }
1743 elseif (is_array($table))
1744 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001745 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001746 {
1747 $this->delete($single_table, $where, $limit, FALSE);
1748 }
1749
1750 $this->_reset_write();
1751 return;
1752 }
1753 else
1754 {
1755 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1756 }
1757
1758 if ($where != '')
1759 {
1760 $this->where($where);
1761 }
1762
1763 if ($limit != NULL)
1764 {
1765 $this->limit($limit);
1766 }
1767
Andrey Andreev24276a32012-01-08 02:44:38 +02001768 if (count($this->ar_where) === 0 && count($this->ar_wherein) === 0 && count($this->ar_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001769 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001770 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001771 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001772
1773 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001774 if ($reset_data)
1775 {
1776 $this->_reset_write();
1777 }
Barry Mienydd671972010-10-04 16:33:58 +02001778
Andrey Andreev24276a32012-01-08 02:44:38 +02001779 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001780 }
WanWizard7219c072011-12-28 14:09:05 +01001781
Derek Allard2067d1a2008-11-13 22:59:24 +00001782 // --------------------------------------------------------------------
1783
1784 /**
1785 * DB Prefix
1786 *
1787 * Prepends a database prefix if one exists in configuration
1788 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 * @param string the table
1790 * @return string
1791 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001792 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 {
1794 if ($table == '')
1795 {
1796 $this->display_error('db_table_name_required');
1797 }
1798
1799 return $this->dbprefix.$table;
1800 }
1801
1802 // --------------------------------------------------------------------
1803
1804 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001805 * Set DB Prefix
1806 *
1807 * Set's the DB Prefix to something new without needing to reconnect
1808 *
1809 * @param string the prefix
1810 * @return string
1811 */
1812 public function set_dbprefix($prefix = '')
1813 {
1814 return $this->dbprefix = $prefix;
1815 }
1816
1817 // --------------------------------------------------------------------
1818
1819 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 * Track Aliases
1821 *
1822 * Used to track SQL statements written with aliased tables.
1823 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001824 * @param string The table to inspect
1825 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001826 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001827 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001828 {
1829 if (is_array($table))
1830 {
1831 foreach ($table as $t)
1832 {
1833 $this->_track_aliases($t);
1834 }
1835 return;
1836 }
Barry Mienydd671972010-10-04 16:33:58 +02001837
Derek Jones37f4b9c2011-07-01 17:56:50 -05001838 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001839 // the string into discreet statements
1840 if (strpos($table, ',') !== FALSE)
1841 {
1842 return $this->_track_aliases(explode(',', $table));
1843 }
Barry Mienydd671972010-10-04 16:33:58 +02001844
Derek Allard2067d1a2008-11-13 22:59:24 +00001845 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001846 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001847 {
1848 // if the alias is written with the AS keyword, remove it
1849 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001850
Derek Allard2067d1a2008-11-13 22:59:24 +00001851 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001852 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001853
Derek Allard2067d1a2008-11-13 22:59:24 +00001854 // Store the alias, if it doesn't already exist
1855 if ( ! in_array($table, $this->ar_aliased_tables))
1856 {
1857 $this->ar_aliased_tables[] = $table;
1858 }
1859 }
1860 }
WanWizard7219c072011-12-28 14:09:05 +01001861
Derek Allard2067d1a2008-11-13 22:59:24 +00001862 // --------------------------------------------------------------------
1863
1864 /**
1865 * Compile the SELECT statement
1866 *
1867 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001868 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001869 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001870 * @return string
1871 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001872 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001873 {
1874 // Combine any cached components with the current statements
1875 $this->_merge_cache();
1876
Derek Allard2067d1a2008-11-13 22:59:24 +00001877 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001878 if ($select_override !== FALSE)
1879 {
1880 $sql = $select_override;
1881 }
1882 else
1883 {
1884 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001885
Andrey Andreev24276a32012-01-08 02:44:38 +02001886 if (count($this->ar_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001887 {
Barry Mienydd671972010-10-04 16:33:58 +02001888 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001889 }
1890 else
Barry Mienydd671972010-10-04 16:33:58 +02001891 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001892 // Cycle through the "select" portion of the query and prep each column name.
1893 // The reason we protect identifiers here rather then in the select() function
1894 // is because until the user calls the from() function we don't know if there are aliases
1895 foreach ($this->ar_select as $key => $val)
1896 {
Phil Sturgeon77cc0282011-08-09 16:03:49 -06001897 $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL;
1898 $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001899 }
Barry Mienydd671972010-10-04 16:33:58 +02001900
Derek Allard2067d1a2008-11-13 22:59:24 +00001901 $sql .= implode(', ', $this->ar_select);
1902 }
1903 }
1904
Derek Allard2067d1a2008-11-13 22:59:24 +00001905 // Write the "FROM" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001906 if (count($this->ar_from) > 0)
1907 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001908 $sql .= "\nFROM ".$this->_from_tables($this->ar_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00001909 }
1910
Derek Allard2067d1a2008-11-13 22:59:24 +00001911 // Write the "JOIN" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001912 if (count($this->ar_join) > 0)
1913 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001914 $sql .= "\n".implode("\n", $this->ar_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00001915 }
1916
Derek Allard2067d1a2008-11-13 22:59:24 +00001917 // Write the "WHERE" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001918 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1919 {
Greg Akere156c6e2011-04-20 16:03:04 -05001920 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00001921 }
1922
1923 $sql .= implode("\n", $this->ar_where);
1924
Derek Allard2067d1a2008-11-13 22:59:24 +00001925 // Write the "LIKE" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001926 if (count($this->ar_like) > 0)
1927 {
1928 if (count($this->ar_where) > 0)
1929 {
1930 $sql .= "\nAND ";
1931 }
1932
1933 $sql .= implode("\n", $this->ar_like);
1934 }
1935
Derek Allard2067d1a2008-11-13 22:59:24 +00001936 // Write the "GROUP BY" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001937 if (count($this->ar_groupby) > 0)
1938 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001939 $sql .= "\nGROUP BY ".implode(', ', $this->ar_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00001940 }
1941
Derek Allard2067d1a2008-11-13 22:59:24 +00001942 // Write the "HAVING" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001943 if (count($this->ar_having) > 0)
1944 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001945 $sql .= "\nHAVING ".implode("\n", $this->ar_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00001946 }
1947
Derek Allard2067d1a2008-11-13 22:59:24 +00001948 // Write the "ORDER BY" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001949 if (count($this->ar_orderby) > 0)
1950 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001951 $sql .= "\nORDER BY ".implode(', ', $this->ar_orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00001952 if ($this->ar_order !== FALSE)
1953 {
1954 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02001955 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001956 }
1957
Derek Allard2067d1a2008-11-13 22:59:24 +00001958 // Write the "LIMIT" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001959 if (is_numeric($this->ar_limit))
1960 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001961 return $this->_limit($sql."\n", $this->ar_limit, $this->ar_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00001962 }
1963
1964 return $sql;
1965 }
1966
1967 // --------------------------------------------------------------------
1968
1969 /**
1970 * Object to Array
1971 *
1972 * Takes an object as input and converts the class variables to array key/vals
1973 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001974 * @param object
1975 * @return array
1976 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001977 public function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001978 {
1979 if ( ! is_object($object))
1980 {
1981 return $object;
1982 }
Barry Mienydd671972010-10-04 16:33:58 +02001983
Derek Allard2067d1a2008-11-13 22:59:24 +00001984 $array = array();
1985 foreach (get_object_vars($object) as $key => $val)
1986 {
1987 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06001988 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00001989 {
1990 $array[$key] = $val;
1991 }
1992 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001993
1994 return $array;
1995 }
Barry Mienydd671972010-10-04 16:33:58 +02001996
Derek Jonesd10e8962010-03-02 17:10:36 -06001997 // --------------------------------------------------------------------
1998
1999 /**
2000 * Object to Array
2001 *
2002 * Takes an object as input and converts the class variables to array key/vals
2003 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002004 * @param object
2005 * @return array
2006 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002007 public function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002008 {
2009 if ( ! is_object($object))
2010 {
2011 return $object;
2012 }
Barry Mienydd671972010-10-04 16:33:58 +02002013
Derek Jonesd10e8962010-03-02 17:10:36 -06002014 $array = array();
2015 $out = get_object_vars($object);
2016 $fields = array_keys($out);
2017
2018 foreach ($fields as $val)
2019 {
2020 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002021 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002022 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002023 $i = 0;
2024 foreach ($out[$val] as $data)
2025 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002026 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002027 }
2028 }
2029 }
2030
Derek Allard2067d1a2008-11-13 22:59:24 +00002031 return $array;
2032 }
Barry Mienydd671972010-10-04 16:33:58 +02002033
Derek Allard2067d1a2008-11-13 22:59:24 +00002034 // --------------------------------------------------------------------
2035
2036 /**
2037 * Start Cache
2038 *
2039 * Starts AR caching
2040 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002041 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002042 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002043 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002044 {
2045 $this->ar_caching = TRUE;
2046 }
2047
2048 // --------------------------------------------------------------------
2049
2050 /**
2051 * Stop Cache
2052 *
2053 * Stops AR caching
2054 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002055 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002056 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002057 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002058 {
2059 $this->ar_caching = FALSE;
2060 }
2061
2062 // --------------------------------------------------------------------
2063
2064 /**
2065 * Flush Cache
2066 *
2067 * Empties the AR cache
2068 *
2069 * @access public
2070 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002071 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002072 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002073 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002074 $this->_reset_run(array(
2075 'ar_cache_select' => array(),
2076 'ar_cache_from' => array(),
2077 'ar_cache_join' => array(),
2078 'ar_cache_where' => array(),
2079 'ar_cache_like' => array(),
2080 'ar_cache_groupby' => array(),
2081 'ar_cache_having' => array(),
2082 'ar_cache_orderby' => array(),
2083 'ar_cache_set' => array(),
2084 'ar_cache_exists' => array(),
2085 'ar_cache_no_escape' => array()
2086 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002087 }
2088
2089 // --------------------------------------------------------------------
2090
2091 /**
2092 * Merge Cache
2093 *
Barry Mienydd671972010-10-04 16:33:58 +02002094 * When called, this function merges any cached AR arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002095 * locally called ones.
2096 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002097 * @return void
2098 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002099 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002100 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002101 if (count($this->ar_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002102 {
2103 return;
2104 }
2105
2106 foreach ($this->ar_cache_exists as $val)
2107 {
2108 $ar_variable = 'ar_'.$val;
2109 $ar_cache_var = 'ar_cache_'.$val;
2110
Andrey Andreev24276a32012-01-08 02:44:38 +02002111 if (count($this->$ar_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002112 {
2113 continue;
2114 }
2115
2116 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
2117 }
2118
2119 // If we are "protecting identifiers" we need to examine the "from"
2120 // portion of the query to determine if there are any aliases
2121 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
2122 {
2123 $this->_track_aliases($this->ar_from);
2124 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002125
2126 $this->ar_no_escape = $this->ar_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002127 }
WanWizard7219c072011-12-28 14:09:05 +01002128
Kyle Farris0c147b32011-08-26 02:29:31 -04002129 // --------------------------------------------------------------------
2130
2131 /**
2132 * Reset Active Record values.
WanWizard7219c072011-12-28 14:09:05 +01002133 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002134 * Publicly-visible method to reset the AR values.
2135 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002136 * @return void
2137 */
2138 public function reset_query()
2139 {
2140 $this->_reset_select();
2141 $this->_reset_write();
2142 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002143
2144 // --------------------------------------------------------------------
2145
2146 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002147 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002148 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002149 * @param array An array of fields to reset
2150 * @return void
2151 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002152 protected function _reset_run($ar_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002153 {
2154 foreach ($ar_reset_items as $item => $default_value)
2155 {
2156 if ( ! in_array($item, $this->ar_store_array))
2157 {
2158 $this->$item = $default_value;
2159 }
2160 }
2161 }
2162
2163 // --------------------------------------------------------------------
2164
2165 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002166 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002167 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002168 * @return void
2169 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002170 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002171 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002172 $this->_reset_run(array(
2173 'ar_select' => array(),
2174 'ar_from' => array(),
2175 'ar_join' => array(),
2176 'ar_where' => array(),
2177 'ar_like' => array(),
2178 'ar_groupby' => array(),
2179 'ar_having' => array(),
2180 'ar_orderby' => array(),
2181 'ar_wherein' => array(),
2182 'ar_aliased_tables' => array(),
2183 'ar_no_escape' => array(),
2184 'ar_distinct' => FALSE,
2185 'ar_limit' => FALSE,
2186 'ar_offset' => FALSE,
2187 'ar_order' => FALSE
2188 )
2189 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002190 }
Barry Mienydd671972010-10-04 16:33:58 +02002191
Derek Allard2067d1a2008-11-13 22:59:24 +00002192 // --------------------------------------------------------------------
2193
2194 /**
2195 * Resets the active record "write" values.
2196 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002197 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002198 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002199 * @return void
2200 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002201 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002202 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002203 $this->_reset_run(array(
2204 'ar_set' => array(),
2205 'ar_from' => array(),
2206 'ar_where' => array(),
2207 'ar_like' => array(),
2208 'ar_orderby' => array(),
2209 'ar_keys' => array(),
2210 'ar_limit' => FALSE,
2211 'ar_order' => FALSE
2212 )
2213 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002214 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002215
Derek Allard2067d1a2008-11-13 22:59:24 +00002216}
2217
2218/* End of file DB_active_rec.php */
WanWizard7219c072011-12-28 14:09:05 +01002219/* Location: ./system/database/DB_active_rec.php */