blob: 35164a79c1c1da31d4f07d0606e3778538cea366 [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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;
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 Andreev032e7ea2012-03-06 19:48:35 +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 Andreev032e7ea2012-03-06 19:48:35 +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
Hamza Bhattid3c1ccf2012-03-05 22:58:56 +0400345 if (preg_match('/([\[\w\.]+)([\W\s]+)(.+)/', $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +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 Andreev032e7ea2012-03-06 19:48:35 +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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200437 $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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +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;
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200566 $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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200670 $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')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400675 {
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 Andreev032e7ea2012-03-06 19:48:35 +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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200900 $k = $this->protect_identifiers($k);
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 }
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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200955 $part = $this->protect_identifiers(trim($part));
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 }
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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200967 $orderby = $this->protect_identifiers($orderby);
pporlan2c685fb2011-12-22 12:15:25 +0100968 }
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 {
Kyle Farris76116012011-08-31 11:17:48 -0400993 $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 {
Kyle Farris76116012011-08-31 11:17:48 -0400997 $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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001040 $this->ar_set[$this->protect_identifiers($k)] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 }
1042 else
1043 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001044 $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 // --------------------------------------------------------------------
1052
1053 /**
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 Andreev032e7ea2012-03-06 19:48:35 +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 Andreev032e7ea2012-03-06 19:48:35 +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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001273 $this->ar_keys[] = $this->protect_identifiers($k);
Derek Jonesd10e8962010-03-02 17:10:36 -06001274 }
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(
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001299 $this->protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE),
WanWizard7219c072011-12-28 14:09:05 +01001300 array_keys($this->ar_set),
Kyle Farris76116012011-08-31 11:17:48 -04001301 array_values($this->ar_set)
1302 );
WanWizard7219c072011-12-28 14:09:05 +01001303
Kyle Farris0c147b32011-08-26 02:29:31 -04001304 if ($reset === TRUE)
1305 {
1306 $this->_reset_write();
1307 }
WanWizard7219c072011-12-28 14:09:05 +01001308
Kyle Farris0c147b32011-08-26 02:29:31 -04001309 return $sql;
1310 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001311
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 // --------------------------------------------------------------------
1313
1314 /**
1315 * Insert
1316 *
1317 * Compiles an insert string and runs the query
1318 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001319 * @access public
Phil Sturgeon9789f322011-07-15 15:14:05 -06001320 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 * @param array an associative array of insert values
1322 * @return object
1323 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001324 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001325 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 if ( ! is_null($set))
1327 {
1328 $this->set($set);
1329 }
WanWizard7219c072011-12-28 14:09:05 +01001330
Kyle Farris0c147b32011-08-26 02:29:31 -04001331 if ($this->_validate_insert($table) === FALSE)
1332 {
1333 return FALSE;
1334 }
WanWizard7219c072011-12-28 14:09:05 +01001335
Kyle Farris76116012011-08-31 11:17:48 -04001336 $sql = $this->_insert(
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001337 $this->protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE),
WanWizard7219c072011-12-28 14:09:05 +01001338 array_keys($this->ar_set),
Kyle Farris76116012011-08-31 11:17:48 -04001339 array_values($this->ar_set)
1340 );
Barry Mienydd671972010-10-04 16:33:58 +02001341
Kyle Farris0c147b32011-08-26 02:29:31 -04001342 $this->_reset_write();
1343 return $this->query($sql);
1344 }
WanWizard7219c072011-12-28 14:09:05 +01001345
Kyle Farris0c147b32011-08-26 02:29:31 -04001346 // --------------------------------------------------------------------
1347
1348 /**
1349 * Validate Insert
1350 *
1351 * This method is used by both insert() and get_compiled_insert() to
1352 * validate that the there data is actually being set and that table
1353 * has been chosen to be inserted into.
1354 *
1355 * @access public
1356 * @param string the table to insert data into
1357 * @return string
1358 */
WanWizard7219c072011-12-28 14:09:05 +01001359 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001360 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001361 if (count($this->ar_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001363 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001364 }
1365
1366 if ($table == '')
1367 {
1368 if ( ! isset($this->ar_from[0]))
1369 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001370 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001372 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001373 else
1374 {
1375 $this->ar_from[0] = $table;
1376 }
WanWizard7219c072011-12-28 14:09:05 +01001377
Kyle Farris0c147b32011-08-26 02:29:31 -04001378 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 }
Barry Mienydd671972010-10-04 16:33:58 +02001380
Phil Sturgeon9789f322011-07-15 15:14:05 -06001381 // --------------------------------------------------------------------
1382
1383 /**
1384 * Replace
1385 *
1386 * Compiles an replace into string and runs the query
1387 *
1388 * @param string the table to replace data into
1389 * @param array an associative array of insert values
1390 * @return object
1391 */
1392 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001393 {
1394 if ( ! is_null($set))
1395 {
1396 $this->set($set);
1397 }
Barry Mienydd671972010-10-04 16:33:58 +02001398
Andrey Andreev24276a32012-01-08 02:44:38 +02001399 if (count($this->ar_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001400 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001401 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001402 }
1403
1404 if ($table == '')
1405 {
1406 if ( ! isset($this->ar_from[0]))
1407 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001408 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001409 }
Barry Mienydd671972010-10-04 16:33:58 +02001410
Derek Jonesd10e8962010-03-02 17:10:36 -06001411 $table = $this->ar_from[0];
1412 }
1413
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001414 $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 -06001415 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001416 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001417 }
WanWizard7219c072011-12-28 14:09:05 +01001418
Kyle Farris0c147b32011-08-26 02:29:31 -04001419 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001420
Kyle Farris0c147b32011-08-26 02:29:31 -04001421 /**
1422 * Get UPDATE query string
1423 *
1424 * Compiles an update query and returns the sql
1425 *
1426 * @access public
1427 * @param string the table to update
1428 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1429 * @return string
1430 */
1431 public function get_compiled_update($table = '', $reset = TRUE)
1432 {
1433 // Combine any cached components with the current statements
1434 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001435
Kyle Farris0c147b32011-08-26 02:29:31 -04001436 if ($this->_validate_update($table) === FALSE)
1437 {
1438 return FALSE;
1439 }
WanWizard7219c072011-12-28 14:09:05 +01001440
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001441 $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 +01001442
Kyle Farris0c147b32011-08-26 02:29:31 -04001443 if ($reset === TRUE)
1444 {
1445 $this->_reset_write();
1446 }
WanWizard7219c072011-12-28 14:09:05 +01001447
Kyle Farris0c147b32011-08-26 02:29:31 -04001448 return $sql;
1449 }
WanWizard7219c072011-12-28 14:09:05 +01001450
Derek Allard2067d1a2008-11-13 22:59:24 +00001451 // --------------------------------------------------------------------
1452
1453 /**
1454 * Update
1455 *
1456 * Compiles an update string and runs the query
1457 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001458 * @param string the table to retrieve the results from
1459 * @param array an associative array of update values
1460 * @param mixed the where clause
1461 * @return object
1462 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001463 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001464 {
1465 // Combine any cached components with the current statements
1466 $this->_merge_cache();
1467
1468 if ( ! is_null($set))
1469 {
1470 $this->set($set);
1471 }
Barry Mienydd671972010-10-04 16:33:58 +02001472
Kyle Farris0c147b32011-08-26 02:29:31 -04001473 if ($this->_validate_update($table) === FALSE)
1474 {
1475 return FALSE;
1476 }
1477
1478 if ($where != NULL)
1479 {
1480 $this->where($where);
1481 }
1482
1483 if ($limit != NULL)
1484 {
1485 $this->limit($limit);
1486 }
1487
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001488 $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 -04001489 $this->_reset_write();
1490 return $this->query($sql);
1491 }
WanWizard7219c072011-12-28 14:09:05 +01001492
Kyle Farris0c147b32011-08-26 02:29:31 -04001493 // --------------------------------------------------------------------
1494
1495 /**
1496 * Validate Update
1497 *
1498 * This method is used by both update() and get_compiled_update() to
1499 * validate that data is actually being set and that a table has been
1500 * chosen to be update.
1501 *
1502 * @access public
1503 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001504 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001505 */
1506 protected function _validate_update($table = '')
1507 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001508 if (count($this->ar_set) == 0)
1509 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001510 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001511 }
1512
1513 if ($table == '')
1514 {
1515 if ( ! isset($this->ar_from[0]))
1516 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001517 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001518 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001519 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001520 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001521 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001522 $this->ar_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001524
1525 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001526 }
WanWizard7219c072011-12-28 14:09:05 +01001527
Derek Jonesd10e8962010-03-02 17:10:36 -06001528 // --------------------------------------------------------------------
1529
1530 /**
1531 * Update_Batch
1532 *
1533 * Compiles an update string and runs the query
1534 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001535 * @param string the table to retrieve the results from
1536 * @param array an associative array of update values
1537 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001538 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001539 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001540 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001541 {
1542 // Combine any cached components with the current statements
1543 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001544
Derek Jonesd10e8962010-03-02 17:10:36 -06001545 if (is_null($index))
1546 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001547 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001548 }
1549
1550 if ( ! is_null($set))
1551 {
1552 $this->set_update_batch($set, $index);
1553 }
1554
Andrey Andreev24276a32012-01-08 02:44:38 +02001555 if (count($this->ar_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001556 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001557 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001558 }
1559
1560 if ($table == '')
1561 {
1562 if ( ! isset($this->ar_from[0]))
1563 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001564 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001565 }
Barry Mienydd671972010-10-04 16:33:58 +02001566
Derek Jonesd10e8962010-03-02 17:10:36 -06001567 $table = $this->ar_from[0];
1568 }
Barry Mienydd671972010-10-04 16:33:58 +02001569
Derek Jonesd10e8962010-03-02 17:10:36 -06001570 // Batch this baby
Andrey Andreev24276a32012-01-08 02:44:38 +02001571 for ($i = 0, $total = count($this->ar_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001572 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001573 $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 -06001574 }
Barry Mienydd671972010-10-04 16:33:58 +02001575
Derek Jonesd10e8962010-03-02 17:10:36 -06001576 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001577 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001578 }
1579
1580 // --------------------------------------------------------------------
1581
1582 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001583 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001584 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001585 * @param array
1586 * @param string
1587 * @param boolean
1588 * @return object
1589 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001590 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001591 {
1592 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001593
Derek Jonesd10e8962010-03-02 17:10:36 -06001594 if ( ! is_array($key))
1595 {
1596 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001597 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001598
1599 foreach ($key as $k => $v)
1600 {
1601 $index_set = FALSE;
1602 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001603 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001604 {
1605 if ($k2 == $index)
1606 {
1607 $index_set = TRUE;
1608 }
1609 else
1610 {
1611 $not[] = $k.'-'.$v;
1612 }
1613
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001614 $clean[$this->protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001615 }
1616
1617 if ($index_set == FALSE)
1618 {
1619 return $this->display_error('db_batch_missing_index');
1620 }
1621
1622 $this->ar_set[] = $clean;
1623 }
Barry Mienydd671972010-10-04 16:33:58 +02001624
Derek Jonesd10e8962010-03-02 17:10:36 -06001625 return $this;
1626 }
1627
Derek Allard2067d1a2008-11-13 22:59:24 +00001628 // --------------------------------------------------------------------
1629
1630 /**
1631 * Empty Table
1632 *
1633 * Compiles a delete string and runs "DELETE FROM table"
1634 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001635 * @param string the table to empty
1636 * @return object
1637 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001638 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001639 {
1640 if ($table == '')
1641 {
1642 if ( ! isset($this->ar_from[0]))
1643 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001644 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001645 }
1646
1647 $table = $this->ar_from[0];
1648 }
1649 else
1650 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001651 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001652 }
1653
1654 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001655 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001656 return $this->query($sql);
1657 }
1658
1659 // --------------------------------------------------------------------
1660
1661 /**
1662 * Truncate
1663 *
1664 * Compiles a truncate string and runs the query
1665 * If the database does not support the truncate() command
1666 * This function maps to "DELETE FROM table"
1667 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001668 * @param string the table to truncate
1669 * @return object
1670 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001671 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001672 {
1673 if ($table == '')
1674 {
1675 if ( ! isset($this->ar_from[0]))
1676 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001677 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001678 }
1679
1680 $table = $this->ar_from[0];
1681 }
1682 else
1683 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001684 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001685 }
1686
1687 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001688 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001689 return $this->query($sql);
1690 }
WanWizard7219c072011-12-28 14:09:05 +01001691
Kyle Farris0c147b32011-08-26 02:29:31 -04001692 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001693
Kyle Farris0c147b32011-08-26 02:29:31 -04001694 /**
1695 * Get DELETE query string
1696 *
1697 * Compiles a delete query string and returns the sql
1698 *
1699 * @access public
1700 * @param string the table to delete from
1701 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1702 * @return string
1703 */
1704 public function get_compiled_delete($table = '', $reset = TRUE)
1705 {
1706 $this->return_delete_sql = TRUE;
1707 $sql = $this->delete($table, '', NULL, $reset);
1708 $this->return_delete_sql = FALSE;
1709 return $sql;
1710 }
WanWizard7219c072011-12-28 14:09:05 +01001711
Derek Allard2067d1a2008-11-13 22:59:24 +00001712 // --------------------------------------------------------------------
1713
1714 /**
1715 * Delete
1716 *
1717 * Compiles a delete string and runs the query
1718 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001719 * @param mixed the table(s) to delete from. String or array
1720 * @param mixed the where clause
1721 * @param mixed the limit clause
1722 * @param boolean
1723 * @return object
1724 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001725 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001726 {
1727 // Combine any cached components with the current statements
1728 $this->_merge_cache();
1729
1730 if ($table == '')
1731 {
1732 if ( ! isset($this->ar_from[0]))
1733 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001734 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001735 }
1736
1737 $table = $this->ar_from[0];
1738 }
1739 elseif (is_array($table))
1740 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001741 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 {
1743 $this->delete($single_table, $where, $limit, FALSE);
1744 }
1745
1746 $this->_reset_write();
1747 return;
1748 }
1749 else
1750 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001751 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001752 }
1753
1754 if ($where != '')
1755 {
1756 $this->where($where);
1757 }
1758
1759 if ($limit != NULL)
1760 {
1761 $this->limit($limit);
1762 }
1763
Andrey Andreev24276a32012-01-08 02:44:38 +02001764 if (count($this->ar_where) === 0 && count($this->ar_wherein) === 0 && count($this->ar_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001765 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001766 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001767 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001768
1769 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001770 if ($reset_data)
1771 {
1772 $this->_reset_write();
1773 }
WanWizard7219c072011-12-28 14:09:05 +01001774
Andrey Andreev24276a32012-01-08 02:44:38 +02001775 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001776 }
WanWizard7219c072011-12-28 14:09:05 +01001777
Derek Allard2067d1a2008-11-13 22:59:24 +00001778 // --------------------------------------------------------------------
1779
1780 /**
1781 * DB Prefix
1782 *
1783 * Prepends a database prefix if one exists in configuration
1784 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001785 * @param string the table
1786 * @return string
1787 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001788 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 {
1790 if ($table == '')
1791 {
1792 $this->display_error('db_table_name_required');
1793 }
1794
1795 return $this->dbprefix.$table;
1796 }
1797
1798 // --------------------------------------------------------------------
1799
1800 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001801 * Set DB Prefix
1802 *
1803 * Set's the DB Prefix to something new without needing to reconnect
1804 *
1805 * @param string the prefix
1806 * @return string
1807 */
1808 public function set_dbprefix($prefix = '')
1809 {
1810 return $this->dbprefix = $prefix;
1811 }
1812
1813 // --------------------------------------------------------------------
1814
1815 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001816 * Track Aliases
1817 *
1818 * Used to track SQL statements written with aliased tables.
1819 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 * @param string The table to inspect
1821 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001822 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001823 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001824 {
1825 if (is_array($table))
1826 {
1827 foreach ($table as $t)
1828 {
1829 $this->_track_aliases($t);
1830 }
1831 return;
1832 }
Barry Mienydd671972010-10-04 16:33:58 +02001833
Derek Jones37f4b9c2011-07-01 17:56:50 -05001834 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001835 // the string into discreet statements
1836 if (strpos($table, ',') !== FALSE)
1837 {
1838 return $this->_track_aliases(explode(',', $table));
1839 }
Barry Mienydd671972010-10-04 16:33:58 +02001840
Derek Allard2067d1a2008-11-13 22:59:24 +00001841 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001842 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001843 {
1844 // if the alias is written with the AS keyword, remove it
1845 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001846
Derek Allard2067d1a2008-11-13 22:59:24 +00001847 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001848 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001849
Derek Allard2067d1a2008-11-13 22:59:24 +00001850 // Store the alias, if it doesn't already exist
1851 if ( ! in_array($table, $this->ar_aliased_tables))
1852 {
1853 $this->ar_aliased_tables[] = $table;
1854 }
1855 }
1856 }
WanWizard7219c072011-12-28 14:09:05 +01001857
Derek Allard2067d1a2008-11-13 22:59:24 +00001858 // --------------------------------------------------------------------
1859
1860 /**
1861 * Compile the SELECT statement
1862 *
1863 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001864 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001865 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001866 * @return string
1867 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001868 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001869 {
1870 // Combine any cached components with the current statements
1871 $this->_merge_cache();
1872
Derek Allard2067d1a2008-11-13 22:59:24 +00001873 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001874 if ($select_override !== FALSE)
1875 {
1876 $sql = $select_override;
1877 }
1878 else
1879 {
1880 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001881
Andrey Andreev24276a32012-01-08 02:44:38 +02001882 if (count($this->ar_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001883 {
Barry Mienydd671972010-10-04 16:33:58 +02001884 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001885 }
1886 else
Barry Mienydd671972010-10-04 16:33:58 +02001887 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001888 // Cycle through the "select" portion of the query and prep each column name.
1889 // The reason we protect identifiers here rather then in the select() function
1890 // is because until the user calls the from() function we don't know if there are aliases
1891 foreach ($this->ar_select as $key => $val)
1892 {
Phil Sturgeon77cc0282011-08-09 16:03:49 -06001893 $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL;
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001894 $this->ar_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001895 }
Barry Mienydd671972010-10-04 16:33:58 +02001896
Derek Allard2067d1a2008-11-13 22:59:24 +00001897 $sql .= implode(', ', $this->ar_select);
1898 }
1899 }
1900
Derek Allard2067d1a2008-11-13 22:59:24 +00001901 // Write the "FROM" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001902 if (count($this->ar_from) > 0)
1903 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001904 $sql .= "\nFROM ".$this->_from_tables($this->ar_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00001905 }
1906
Derek Allard2067d1a2008-11-13 22:59:24 +00001907 // Write the "JOIN" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001908 if (count($this->ar_join) > 0)
1909 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001910 $sql .= "\n".implode("\n", $this->ar_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00001911 }
1912
Derek Allard2067d1a2008-11-13 22:59:24 +00001913 // Write the "WHERE" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001914 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1915 {
Greg Akere156c6e2011-04-20 16:03:04 -05001916 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00001917 }
1918
1919 $sql .= implode("\n", $this->ar_where);
1920
Derek Allard2067d1a2008-11-13 22:59:24 +00001921 // Write the "LIKE" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001922 if (count($this->ar_like) > 0)
1923 {
1924 if (count($this->ar_where) > 0)
1925 {
1926 $sql .= "\nAND ";
1927 }
1928
1929 $sql .= implode("\n", $this->ar_like);
1930 }
1931
Derek Allard2067d1a2008-11-13 22:59:24 +00001932 // Write the "GROUP BY" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001933 if (count($this->ar_groupby) > 0)
1934 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001935 $sql .= "\nGROUP BY ".implode(', ', $this->ar_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00001936 }
1937
Derek Allard2067d1a2008-11-13 22:59:24 +00001938 // Write the "HAVING" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001939 if (count($this->ar_having) > 0)
1940 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001941 $sql .= "\nHAVING ".implode("\n", $this->ar_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00001942 }
1943
Derek Allard2067d1a2008-11-13 22:59:24 +00001944 // Write the "ORDER BY" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001945 if (count($this->ar_orderby) > 0)
1946 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001947 $sql .= "\nORDER BY ".implode(', ', $this->ar_orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00001948 if ($this->ar_order !== FALSE)
1949 {
1950 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02001951 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001952 }
1953
Derek Allard2067d1a2008-11-13 22:59:24 +00001954 // Write the "LIMIT" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001955 if (is_numeric($this->ar_limit))
1956 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001957 return $this->_limit($sql."\n", $this->ar_limit, $this->ar_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00001958 }
1959
1960 return $sql;
1961 }
1962
1963 // --------------------------------------------------------------------
1964
1965 /**
1966 * Object to Array
1967 *
1968 * Takes an object as input and converts the class variables to array key/vals
1969 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001970 * @param object
1971 * @return array
1972 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001973 public function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001974 {
1975 if ( ! is_object($object))
1976 {
1977 return $object;
1978 }
Barry Mienydd671972010-10-04 16:33:58 +02001979
Derek Allard2067d1a2008-11-13 22:59:24 +00001980 $array = array();
1981 foreach (get_object_vars($object) as $key => $val)
1982 {
1983 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06001984 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00001985 {
1986 $array[$key] = $val;
1987 }
1988 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001989
1990 return $array;
1991 }
Barry Mienydd671972010-10-04 16:33:58 +02001992
Derek Jonesd10e8962010-03-02 17:10:36 -06001993 // --------------------------------------------------------------------
1994
1995 /**
1996 * Object to Array
1997 *
1998 * Takes an object as input and converts the class variables to array key/vals
1999 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002000 * @param object
2001 * @return array
2002 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002003 public function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002004 {
2005 if ( ! is_object($object))
2006 {
2007 return $object;
2008 }
Barry Mienydd671972010-10-04 16:33:58 +02002009
Derek Jonesd10e8962010-03-02 17:10:36 -06002010 $array = array();
2011 $out = get_object_vars($object);
2012 $fields = array_keys($out);
2013
2014 foreach ($fields as $val)
2015 {
2016 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002017 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002018 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002019 $i = 0;
2020 foreach ($out[$val] as $data)
2021 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002022 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002023 }
2024 }
2025 }
2026
Derek Allard2067d1a2008-11-13 22:59:24 +00002027 return $array;
2028 }
Barry Mienydd671972010-10-04 16:33:58 +02002029
Derek Allard2067d1a2008-11-13 22:59:24 +00002030 // --------------------------------------------------------------------
2031
2032 /**
2033 * Start Cache
2034 *
2035 * Starts AR caching
2036 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002037 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002038 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002039 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002040 {
2041 $this->ar_caching = TRUE;
2042 }
2043
2044 // --------------------------------------------------------------------
2045
2046 /**
2047 * Stop Cache
2048 *
2049 * Stops AR caching
2050 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002051 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002052 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002053 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002054 {
2055 $this->ar_caching = FALSE;
2056 }
2057
2058 // --------------------------------------------------------------------
2059
2060 /**
2061 * Flush Cache
2062 *
2063 * Empties the AR cache
2064 *
2065 * @access public
2066 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002067 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002068 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002069 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002070 $this->_reset_run(array(
2071 'ar_cache_select' => array(),
2072 'ar_cache_from' => array(),
2073 'ar_cache_join' => array(),
2074 'ar_cache_where' => array(),
2075 'ar_cache_like' => array(),
2076 'ar_cache_groupby' => array(),
2077 'ar_cache_having' => array(),
2078 'ar_cache_orderby' => array(),
2079 'ar_cache_set' => array(),
2080 'ar_cache_exists' => array(),
2081 'ar_cache_no_escape' => array()
2082 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002083 }
2084
2085 // --------------------------------------------------------------------
2086
2087 /**
2088 * Merge Cache
2089 *
Barry Mienydd671972010-10-04 16:33:58 +02002090 * When called, this function merges any cached AR arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002091 * locally called ones.
2092 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002093 * @return void
2094 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002095 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002096 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002097 if (count($this->ar_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002098 {
2099 return;
2100 }
2101
2102 foreach ($this->ar_cache_exists as $val)
2103 {
2104 $ar_variable = 'ar_'.$val;
2105 $ar_cache_var = 'ar_cache_'.$val;
2106
Andrey Andreev24276a32012-01-08 02:44:38 +02002107 if (count($this->$ar_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002108 {
2109 continue;
2110 }
2111
2112 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
2113 }
2114
2115 // If we are "protecting identifiers" we need to examine the "from"
2116 // portion of the query to determine if there are any aliases
2117 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
2118 {
2119 $this->_track_aliases($this->ar_from);
2120 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002121
2122 $this->ar_no_escape = $this->ar_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002123 }
WanWizard7219c072011-12-28 14:09:05 +01002124
Kyle Farris0c147b32011-08-26 02:29:31 -04002125 // --------------------------------------------------------------------
2126
2127 /**
2128 * Reset Active Record values.
WanWizard7219c072011-12-28 14:09:05 +01002129 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002130 * Publicly-visible method to reset the AR values.
2131 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002132 * @return void
2133 */
2134 public function reset_query()
2135 {
2136 $this->_reset_select();
2137 $this->_reset_write();
2138 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002139
2140 // --------------------------------------------------------------------
2141
2142 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002143 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002144 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002145 * @param array An array of fields to reset
2146 * @return void
2147 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002148 protected function _reset_run($ar_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002149 {
2150 foreach ($ar_reset_items as $item => $default_value)
2151 {
2152 if ( ! in_array($item, $this->ar_store_array))
2153 {
2154 $this->$item = $default_value;
2155 }
2156 }
2157 }
2158
2159 // --------------------------------------------------------------------
2160
2161 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002162 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002163 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002164 * @return void
2165 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002166 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002167 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002168 $this->_reset_run(array(
2169 'ar_select' => array(),
2170 'ar_from' => array(),
2171 'ar_join' => array(),
2172 'ar_where' => array(),
2173 'ar_like' => array(),
2174 'ar_groupby' => array(),
2175 'ar_having' => array(),
2176 'ar_orderby' => array(),
2177 'ar_wherein' => array(),
2178 'ar_aliased_tables' => array(),
2179 'ar_no_escape' => array(),
2180 'ar_distinct' => FALSE,
2181 'ar_limit' => FALSE,
2182 'ar_offset' => FALSE,
2183 'ar_order' => FALSE
2184 )
2185 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002186 }
Barry Mienydd671972010-10-04 16:33:58 +02002187
Derek Allard2067d1a2008-11-13 22:59:24 +00002188 // --------------------------------------------------------------------
2189
2190 /**
2191 * Resets the active record "write" values.
2192 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002193 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002194 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002195 * @return void
2196 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002197 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002198 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002199 $this->_reset_run(array(
2200 'ar_set' => array(),
2201 'ar_from' => array(),
2202 'ar_where' => array(),
2203 'ar_like' => array(),
2204 'ar_orderby' => array(),
2205 'ar_keys' => array(),
2206 'ar_limit' => FALSE,
2207 'ar_order' => FALSE
2208 )
2209 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002210 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002211
Derek Allard2067d1a2008-11-13 22:59:24 +00002212}
2213
2214/* End of file DB_active_rec.php */
WanWizard7219c072011-12-28 14:09:05 +01002215/* Location: ./system/database/DB_active_rec.php */