blob: b9d77f1fb422195ac420f64b9aaabfc9eccb5a32 [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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
Jamie Rumbelow7efad202012-02-19 12:37:00 +000029 * Query Builder Class
Derek Allard2067d1a2008-11-13 22:59:24 +000030 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +000031 * This is the platform-independent base Query Builder implementation class.
Derek Allard2067d1a2008-11-13 22:59:24 +000032 *
33 * @package CodeIgniter
34 * @subpackage Drivers
35 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/database/
38 */
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +010039
40abstract class CI_DB_query_builder extends CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000041
WanWizard7219c072011-12-28 14:09:05 +010042 protected $return_delete_sql = FALSE;
43 protected $reset_delete_data = FALSE;
44
Jamie Rumbelow7efad202012-02-19 12:37:00 +000045 protected $qb_select = array();
46 protected $qb_distinct = FALSE;
47 protected $qb_from = array();
48 protected $qb_join = array();
49 protected $qb_where = array();
50 protected $qb_like = array();
51 protected $qb_groupby = array();
52 protected $qb_having = array();
53 protected $qb_keys = array();
54 protected $qb_limit = FALSE;
55 protected $qb_offset = FALSE;
Jamie Rumbelow7efad202012-02-19 12:37:00 +000056 protected $qb_orderby = array();
57 protected $qb_set = array();
58 protected $qb_wherein = array();
59 protected $qb_aliased_tables = array();
60 protected $qb_store_array = array();
61 protected $qb_where_group_started = FALSE;
62 protected $qb_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +020063
Jamie Rumbelow7efad202012-02-19 12:37:00 +000064 // Query Builder Caching variables
65 protected $qb_caching = FALSE;
66 protected $qb_cache_exists = array();
67 protected $qb_cache_select = array();
68 protected $qb_cache_from = array();
69 protected $qb_cache_join = array();
70 protected $qb_cache_where = array();
71 protected $qb_cache_like = array();
72 protected $qb_cache_groupby = array();
73 protected $qb_cache_having = array();
74 protected $qb_cache_orderby = array();
75 protected $qb_cache_set = array();
WanWizard7219c072011-12-28 14:09:05 +010076
Jamie Rumbelow7efad202012-02-19 12:37:00 +000077 protected $qb_no_escape = array();
78 protected $qb_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000079
80 /**
81 * Select
82 *
83 * Generates the SELECT portion of the query
84 *
Derek Allard2067d1a2008-11-13 22:59:24 +000085 * @param string
86 * @return object
87 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060088 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
Derek Allard2067d1a2008-11-13 22:59:24 +000090 if (is_string($select))
91 {
92 $select = explode(',', $select);
93 }
94
95 foreach ($select as $val)
96 {
97 $val = trim($val);
98
Alex Bilbie48a2baf2012-06-02 11:09:54 +010099 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000101 $this->qb_select[] = $val;
102 $this->qb_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000103
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000104 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000106 $this->qb_cache_select[] = $val;
107 $this->qb_cache_exists[] = 'select';
108 $this->qb_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 }
110 }
111 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 return $this;
114 }
115
116 // --------------------------------------------------------------------
117
118 /**
119 * Select Max
120 *
121 * Generates a SELECT MAX(field) portion of a query
122 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * @param string the field
124 * @param string an alias
125 * @return object
126 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600127 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
129 return $this->_max_min_avg_sum($select, $alias, 'MAX');
130 }
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 // --------------------------------------------------------------------
133
134 /**
135 * Select Min
136 *
137 * Generates a SELECT MIN(field) portion of a query
138 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 * @param string the field
140 * @param string an alias
141 * @return object
142 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600143 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
145 return $this->_max_min_avg_sum($select, $alias, 'MIN');
146 }
147
148 // --------------------------------------------------------------------
149
150 /**
151 * Select Average
152 *
153 * Generates a SELECT AVG(field) portion of a query
154 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 * @param string the field
156 * @param string an alias
157 * @return object
158 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600159 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
161 return $this->_max_min_avg_sum($select, $alias, 'AVG');
162 }
163
164 // --------------------------------------------------------------------
165
166 /**
167 * Select Sum
168 *
169 * Generates a SELECT SUM(field) portion of a query
170 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 * @param string the field
172 * @param string an alias
173 * @return object
174 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600175 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 {
177 return $this->_max_min_avg_sum($select, $alias, 'SUM');
178 }
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Processing Function for the four functions above:
184 *
185 * select_max()
186 * select_min()
187 * select_avg()
Andrey Andreev24276a32012-01-08 02:44:38 +0200188 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200189 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 * @param string the field
191 * @param string an alias
192 * @return object
193 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600194 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100196 if ( ! is_string($select) OR $select === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
198 $this->display_error('db_invalid_query');
199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
204 {
205 show_error('Invalid function type: '.$type);
206 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100208 if ($alias === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
210 $alias = $this->_create_alias_from_table(trim($select));
211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300213 $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->escape_identifiers(trim($alias));
214
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000215 $this->qb_select[] = $sql;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100216 $this->qb_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200217
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000218 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000220 $this->qb_cache_select[] = $sql;
221 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 return $this;
225 }
226
227 // --------------------------------------------------------------------
228
229 /**
230 * Determines the alias name based on the table
231 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * @param string
233 * @return string
234 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600235 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
237 if (strpos($item, '.') !== FALSE)
238 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200239 $item = explode('.', $item);
240 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 return $item;
244 }
245
246 // --------------------------------------------------------------------
247
248 /**
249 * DISTINCT
250 *
251 * Sets a flag which tells the query string compiler to add DISTINCT
252 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 * @param bool
254 * @return object
255 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600256 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300258 $this->qb_distinct = is_bool($val) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 return $this;
260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 // --------------------------------------------------------------------
263
264 /**
265 * From
266 *
267 * Generates the FROM portion of the query
268 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 * @param mixed can be a string or array
270 * @return object
271 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600272 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300274 foreach ((array) $from as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
276 if (strpos($val, ',') !== FALSE)
277 {
278 foreach (explode(',', $val) as $v)
279 {
280 $v = trim($v);
281 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200282
George Petsagourakis193d4482012-04-28 11:16:18 +0300283 $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000284
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000285 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000287 $this->qb_cache_from[] = $v;
288 $this->qb_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
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000297 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200299
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000300 $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000301
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000302 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000304 $this->qb_cache_from[] = $val;
305 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 }
307 }
308 }
309
310 return $this;
311 }
312
313 // --------------------------------------------------------------------
314
315 /**
316 * Join
317 *
318 * Generates the JOIN portion of the query
319 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 * @param string
321 * @param string the join condition
322 * @param string the type of join
323 * @return object
324 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600325 public function join($table, $cond, $type = '')
Barry Mienydd671972010-10-04 16:33:58 +0200326 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100327 if ($type !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
329 $type = strtoupper(trim($type));
330
331 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
332 {
333 $type = '';
334 }
335 else
336 {
337 $type .= ' ';
338 }
339 }
340
Andrey Andreev24276a32012-01-08 02:44:38 +0200341 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000342 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 $this->_track_aliases($table);
344
345 // Strip apart the condition and protect the identifiers
Hamza Bhattid3c1ccf2012-03-05 22:58:56 +0400346 if (preg_match('/([\[\w\.]+)([\W\s]+)(.+)/', $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200348 $cond = $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 }
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // Assemble the JOIN statement
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000352 $this->qb_join[] = $join = $type.'JOIN '.$this->protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000353
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000354 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000356 $this->qb_cache_join[] = $join;
357 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 }
359
360 return $this;
361 }
362
363 // --------------------------------------------------------------------
364
365 /**
366 * Where
367 *
368 * Generates the WHERE portion of the query. Separates
369 * multiple calls with AND
370 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 * @param mixed
372 * @param mixed
373 * @return object
374 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600375 public function where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
377 return $this->_where($key, $value, 'AND ', $escape);
378 }
Barry Mienydd671972010-10-04 16:33:58 +0200379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 // --------------------------------------------------------------------
381
382 /**
383 * OR Where
384 *
385 * Generates the WHERE portion of the query. Separates
386 * multiple calls with OR
387 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 * @param mixed
389 * @param mixed
390 * @return object
391 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600392 public function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 {
394 return $this->_where($key, $value, 'OR ', $escape);
395 }
396
397 // --------------------------------------------------------------------
398
399 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 * Where
401 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600402 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 * @param mixed
405 * @param mixed
406 * @param string
407 * @return object
408 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600409 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
WanWizard7219c072011-12-28 14:09:05 +0100411 $type = $this->_group_get_type($type);
412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 if ( ! is_array($key))
414 {
415 $key = array($key => $value);
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // If the escape value was not set will will base it on the global setting
419 if ( ! is_bool($escape))
420 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +0000421 $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
423
424 foreach ($key as $k => $v)
425 {
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100426 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000427
Andrey Andreev9c14f652012-06-10 14:35:07 +0300428 $k = $this->_has_operator($k)
Andrey Andreev392b6ad2012-06-10 15:08:59 +0300429 ? $this->protect_identifiers(substr($k, 0, strpos(rtrim($k), ' ')), FALSE, $escape).strchr(rtrim($k), ' ')
Andrey Andreev9c14f652012-06-10 14:35:07 +0300430 : $this->protect_identifiers($k, FALSE, $escape);
431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 if (is_null($v) && ! $this->_has_operator($k))
433 {
434 // value appears not to have been set, assign the test to IS NULL
435 $k .= ' IS NULL';
436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 if ( ! is_null($v))
439 {
440 if ($escape === TRUE)
441 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 $v = ' '.$this->escape($v);
443 }
WanWizard7219c072011-12-28 14:09:05 +0100444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 if ( ! $this->_has_operator($k))
446 {
Greg Akere156c6e2011-04-20 16:03:04 -0500447 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
449 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000450
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000451 $this->qb_where[] = $prefix.$k.$v;
452 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000454 $this->qb_cache_where[] = $prefix.$k.$v;
455 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
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
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300540 * @param bool 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 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300548 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 }
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 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000562 $this->qb_wherein[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 }
564
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000565 $prefix = (count($this->qb_where) === 0) ? '' : $type;
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000566 $this->qb_where[] = $where_in = $prefix.$this->protect_identifiers($key).$not.' IN ('.implode(', ', $this->qb_wherein).') ';
Barry Mienydd671972010-10-04 16:33:58 +0200567
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000568 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000570 $this->qb_cache_where[] = $where_in;
571 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 }
573
574 // reset the array for multiple calls
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000575 $this->qb_wherein = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 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 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000670 $k = $this->protect_identifiers($k);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000671 $prefix = (count($this->qb_like) === 0) ? '' : $type;
Derek Jonese4ed5832009-02-20 21:44:59 +0000672 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300673
Andrey Andreev24276a32012-01-08 02:44:38 +0200674 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400675 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100676 $like_statement = "{$prefix} $k $not LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400677 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200678 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100680 $like_statement = "{$prefix} $k $not LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200682 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100684 $like_statement = "{$prefix} $k $not LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 }
686 else
687 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100688 $like_statement = "{$prefix} $k $not LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 }
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
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100692 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000693 {
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
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000697 $this->qb_like[] = $like_statement;
698 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000700 $this->qb_cache_like[] = $like_statement;
701 $this->qb_cache_exists[] = 'like';
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 }
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
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000722 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100723 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000724 $this->qb_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (';
WanWizard7219c072011-12-28 14:09:05 +0100725
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000726 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100727 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000728 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100729 }
730
731 return $this;
732 }
733
734 // --------------------------------------------------------------------
735
736 /**
737 * Starts a query group, but ORs the group
738 *
739 * @return object
740 */
741 public function or_group_start()
742 {
743 return $this->group_start('', 'OR ');
744 }
745
746 // --------------------------------------------------------------------
747
748 /**
749 * Starts a query group, but NOTs the group
750 *
751 * @return object
752 */
753 public function not_group_start()
754 {
755 return $this->group_start('NOT ', 'AND ');
756 }
757
758 // --------------------------------------------------------------------
759
760 /**
761 * Starts a query group, but OR NOTs the group
762 *
763 * @return object
764 */
765 public function or_not_group_start()
766 {
767 return $this->group_start('NOT ', 'OR ');
768 }
769
770 // --------------------------------------------------------------------
771
772 /**
773 * Ends a query group
774 *
775 * @return object
776 */
777 public function group_end()
778 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000779 $this->qb_where_group_started = FALSE;
780 $this->qb_where[] = $value = str_repeat(' ', $this->qb_where_group_count--) . ')';
WanWizard7219c072011-12-28 14:09:05 +0100781
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000782 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100783 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000784 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100785 }
786
WanWizard7219c072011-12-28 14:09:05 +0100787 return $this;
788 }
789
790 // --------------------------------------------------------------------
791
792 /**
793 * Group_get_type
794 *
795 * Called by group_start(), _like(), _where() and _where_in()
796 *
797 * @param string
798 * @return string
799 */
800 protected function _group_get_type($type)
801 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000802 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100803 {
804 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000805 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100806 }
807
808 return $type;
809 }
810
811 // --------------------------------------------------------------------
812
813 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 * GROUP BY
815 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 * @param string
817 * @return object
818 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600819 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 {
821 if (is_string($by))
822 {
823 $by = explode(',', $by);
824 }
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 foreach ($by as $val)
827 {
828 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200829
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100830 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000832 $this->qb_groupby[] = $val = $this->protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200833
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000834 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000836 $this->qb_cache_groupby[] = $val;
837 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 }
839 }
840 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200841
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 return $this;
843 }
844
845 // --------------------------------------------------------------------
846
847 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 * Sets the HAVING value
849 *
850 * Separates multiple calls with AND
851 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 * @param string
853 * @param string
854 * @return object
855 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600856 public function having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 {
858 return $this->_having($key, $value, 'AND ', $escape);
859 }
Barry Mienydd671972010-10-04 16:33:58 +0200860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 // --------------------------------------------------------------------
862
863 /**
864 * Sets the OR HAVING value
865 *
866 * Separates multiple calls with OR
867 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 * @param string
869 * @param string
870 * @return object
871 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600872 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 {
874 return $this->_having($key, $value, 'OR ', $escape);
875 }
Barry Mienydd671972010-10-04 16:33:58 +0200876
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 // --------------------------------------------------------------------
878
879 /**
880 * Sets the HAVING values
881 *
882 * Called by having() or or_having()
883 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 * @param string
885 * @param string
886 * @return object
887 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600888 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 {
890 if ( ! is_array($key))
891 {
892 $key = array($key => $value);
893 }
Barry Mienydd671972010-10-04 16:33:58 +0200894
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 foreach ($key as $k => $v)
896 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000897 $prefix = (count($this->qb_having) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000898
899 if ($escape === TRUE)
900 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200901 $k = $this->protect_identifiers($k);
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 }
903
904 if ( ! $this->_has_operator($k))
905 {
906 $k .= ' = ';
907 }
908
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100909 if ($v !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400911 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 }
Barry Mienydd671972010-10-04 16:33:58 +0200913
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000914 $this->qb_having[] = $prefix.$k.$v;
915 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000917 $this->qb_cache_having[] = $prefix.$k.$v;
918 $this->qb_cache_exists[] = 'having';
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 }
920 }
Barry Mienydd671972010-10-04 16:33:58 +0200921
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 return $this;
923 }
Barry Mienydd671972010-10-04 16:33:58 +0200924
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 // --------------------------------------------------------------------
926
927 /**
928 * Sets the ORDER BY value
929 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 * @param string
931 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100932 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 * @return object
934 */
pporlan2c685fb2011-12-22 12:15:25 +0100935 public function order_by($orderby, $direction = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200937 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 {
939 $orderby = ''; // Random results want or don't need a field name
940 $direction = $this->_random_keyword;
941 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100942 elseif (trim($direction) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 {
Andrey Andreev650b4c02012-06-11 12:07:15 +0300944 $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : ' ASC';
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 }
Barry Mienydd671972010-10-04 16:33:58 +0200946
947
Andrey Andreev24276a32012-01-08 02:44:38 +0200948 if ((strpos($orderby, ',') !== FALSE) && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 {
950 $temp = array();
951 foreach (explode(',', $orderby) as $part)
952 {
953 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000954 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200956 $part = $this->protect_identifiers(trim($part));
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 }
Barry Mienydd671972010-10-04 16:33:58 +0200958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 $temp[] = $part;
960 }
Barry Mienydd671972010-10-04 16:33:58 +0200961
962 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 }
Andrey Andreev650b4c02012-06-11 12:07:15 +0300964 elseif ($direction !== $this->_random_keyword && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 {
Andrey Andreev650b4c02012-06-11 12:07:15 +0300966 $orderby = $this->protect_identifiers($orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 }
Barry Mienydd671972010-10-04 16:33:58 +0200968
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000969 $this->qb_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200970
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000971 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000973 $this->qb_cache_orderby[] = $orderby_statement;
974 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 }
976
977 return $this;
978 }
Barry Mienydd671972010-10-04 16:33:58 +0200979
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 // --------------------------------------------------------------------
981
982 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 * Sets the LIMIT value
984 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300985 * @param int the limit value
986 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 * @return object
988 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200989 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000991 $this->qb_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000992
Andrey Andreev650b4c02012-06-11 12:07:15 +0300993 if ( ! empty($offset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000995 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 }
Barry Mienydd671972010-10-04 16:33:58 +0200997
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 return $this;
999 }
Barry Mienydd671972010-10-04 16:33:58 +02001000
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 // --------------------------------------------------------------------
1002
1003 /**
1004 * Sets the OFFSET value
1005 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001006 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 * @return object
1008 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001009 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001011 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 return $this;
1013 }
Barry Mienydd671972010-10-04 16:33:58 +02001014
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 // --------------------------------------------------------------------
1016
1017 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001018 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 * @param mixed
1021 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001022 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 * @return object
1024 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001025 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 {
1027 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001028
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 if ( ! is_array($key))
1030 {
1031 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001032 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001033
1034 foreach ($key as $k => $v)
1035 {
1036 if ($escape === FALSE)
1037 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001038 $this->qb_set[$this->protect_identifiers($k)] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 }
1040 else
1041 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001042 $this->qb_set[$this->protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 }
1044 }
Barry Mienydd671972010-10-04 16:33:58 +02001045
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 return $this;
1047 }
WanWizard7219c072011-12-28 14:09:05 +01001048
Kyle Farris0c147b32011-08-26 02:29:31 -04001049 // --------------------------------------------------------------------
1050
1051 /**
1052 * Get SELECT query string
1053 *
1054 * Compiles a SELECT query string and returns the sql.
1055 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001056 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001057 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001058 * @return string
1059 */
WanWizard7219c072011-12-28 14:09:05 +01001060 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001061 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001062 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001063 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001064 $this->_track_aliases($table);
1065 $this->from($table);
1066 }
WanWizard7219c072011-12-28 14:09:05 +01001067
Andrey Andreev650b4c02012-06-11 12:07:15 +03001068 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001069
Kyle Farris0c147b32011-08-26 02:29:31 -04001070 if ($reset === TRUE)
1071 {
1072 $this->_reset_select();
1073 }
WanWizard7219c072011-12-28 14:09:05 +01001074
Kyle Farris0c147b32011-08-26 02:29:31 -04001075 return $select;
1076 }
WanWizard7219c072011-12-28 14:09:05 +01001077
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 // --------------------------------------------------------------------
1079
1080 /**
1081 * Get
1082 *
1083 * Compiles the select statement based on the other functions called
1084 * and runs the query
1085 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 * @param string the table
1087 * @param string the limit clause
1088 * @param string the offset clause
1089 * @return object
1090 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001091 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001093 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 {
1095 $this->_track_aliases($table);
1096 $this->from($table);
1097 }
Barry Mienydd671972010-10-04 16:33:58 +02001098
Andrey Andreev650b4c02012-06-11 12:07:15 +03001099 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 {
1101 $this->limit($limit, $offset);
1102 }
Barry Mienydd671972010-10-04 16:33:58 +02001103
Andrey Andreev24276a32012-01-08 02:44:38 +02001104 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 $this->_reset_select();
1106 return $result;
1107 }
1108
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001109 // --------------------------------------------------------------------
1110
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 /**
1112 * "Count All Results" query
1113 *
Barry Mienydd671972010-10-04 16:33:58 +02001114 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001115 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 * @param string
1118 * @return string
1119 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001120 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001122 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 {
1124 $this->_track_aliases($table);
1125 $this->from($table);
1126 }
Barry Mienydd671972010-10-04 16:33:58 +02001127
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001128 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001130
Purwandi1d160e72012-01-09 16:33:28 +07001131 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001133 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 }
1135
Purwandi1d160e72012-01-09 16:33:28 +07001136 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001137 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001139
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 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001154 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 {
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
Andrey Andreev650b4c02012-06-11 12:07:15 +03001164 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 {
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
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001192 if (count($this->qb_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
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001202 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001203 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001204 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001205 {
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
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001209 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001210 }
1211
1212 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001213 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001214 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001215 $this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), $this->qb_keys, array_slice($this->qb_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 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001225 * Insert_batch statement
1226 *
1227 * Generates a platform-specific insert string from the supplied data.
1228 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001229 * @param string the table name
1230 * @param array the insert keys
1231 * @param array the insert values
1232 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001233 */
1234 protected function _insert_batch($table, $keys, $values)
1235 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001236 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001237 }
1238
1239 // --------------------------------------------------------------------
1240
1241 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001242 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001243 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001244 * @param mixed
1245 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001246 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001247 * @return object
1248 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001249 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001250 {
1251 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001252
Derek Jonesd10e8962010-03-02 17:10:36 -06001253 if ( ! is_array($key))
1254 {
1255 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001256 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001257
Iban Eguia3c0a4522012-04-15 13:30:44 +02001258 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001259 sort($keys);
1260
1261 foreach ($key as $row)
1262 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001263 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001264 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1265 {
1266 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001267 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001268 return;
1269 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001270
Barry Mienydd671972010-10-04 16:33:58 +02001271 ksort($row); // puts $row in the same order as our keys
1272
Andrey Andreev650b4c02012-06-11 12:07:15 +03001273 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001274 {
1275 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001276 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001277 {
Barry Mienydd671972010-10-04 16:33:58 +02001278 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001279 }
1280
Andrey Andreev650b4c02012-06-11 12:07:15 +03001281 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001282 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001283
1284 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001285 }
1286
1287 foreach ($keys as $k)
1288 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001289 $this->qb_keys[] = $this->protect_identifiers($k);
Derek Jonesd10e8962010-03-02 17:10:36 -06001290 }
Barry Mienydd671972010-10-04 16:33:58 +02001291
Derek Jonesd10e8962010-03-02 17:10:36 -06001292 return $this;
1293 }
WanWizard7219c072011-12-28 14:09:05 +01001294
Kyle Farris0c147b32011-08-26 02:29:31 -04001295 // --------------------------------------------------------------------
1296
1297 /**
1298 * Get INSERT query string
1299 *
1300 * Compiles an insert query and returns the sql
1301 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001302 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001303 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001304 * @return string
1305 */
1306 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001307 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001308 if ($this->_validate_insert($table) === FALSE)
1309 {
1310 return FALSE;
1311 }
WanWizard7219c072011-12-28 14:09:05 +01001312
Kyle Farris76116012011-08-31 11:17:48 -04001313 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001314 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001315 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001316 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001317 array_keys($this->qb_set),
1318 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001319 );
WanWizard7219c072011-12-28 14:09:05 +01001320
Kyle Farris0c147b32011-08-26 02:29:31 -04001321 if ($reset === TRUE)
1322 {
1323 $this->_reset_write();
1324 }
WanWizard7219c072011-12-28 14:09:05 +01001325
Kyle Farris0c147b32011-08-26 02:29:31 -04001326 return $sql;
1327 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001328
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 // --------------------------------------------------------------------
1330
1331 /**
1332 * Insert
1333 *
1334 * Compiles an insert string and runs the query
1335 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001336 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 * @param array an associative array of insert values
1338 * @return object
1339 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001340 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001341 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001342 if ( ! is_null($set))
1343 {
1344 $this->set($set);
1345 }
WanWizard7219c072011-12-28 14:09:05 +01001346
Kyle Farris0c147b32011-08-26 02:29:31 -04001347 if ($this->_validate_insert($table) === FALSE)
1348 {
1349 return FALSE;
1350 }
WanWizard7219c072011-12-28 14:09:05 +01001351
Kyle Farris76116012011-08-31 11:17:48 -04001352 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001353 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001354 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001355 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001356 array_keys($this->qb_set),
1357 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001358 );
Barry Mienydd671972010-10-04 16:33:58 +02001359
Kyle Farris0c147b32011-08-26 02:29:31 -04001360 $this->_reset_write();
1361 return $this->query($sql);
1362 }
WanWizard7219c072011-12-28 14:09:05 +01001363
Kyle Farris0c147b32011-08-26 02:29:31 -04001364 // --------------------------------------------------------------------
1365
1366 /**
Andrey Andreev65d537c2012-04-05 14:11:41 +03001367 * Insert statement
1368 *
1369 * Generates a platform-specific insert string from the supplied data
1370 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001371 * @param string the table name
1372 * @param array the insert keys
1373 * @param array the insert values
1374 * @return string
Andrey Andreev65d537c2012-04-05 14:11:41 +03001375 */
1376 protected function _insert($table, $keys, $values)
1377 {
1378 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1379 }
1380
1381 // --------------------------------------------------------------------
1382
1383 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001384 * Validate Insert
1385 *
1386 * This method is used by both insert() and get_compiled_insert() to
1387 * validate that the there data is actually being set and that table
1388 * has been chosen to be inserted into.
1389 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001390 * @param string the table to insert data into
1391 * @return string
1392 */
WanWizard7219c072011-12-28 14:09:05 +01001393 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001394 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001395 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001396 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001397 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001398 }
1399
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001400 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001401 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001402 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001403 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001404 elseif ( ! isset($this->qb_from[0]))
1405 {
1406 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1407 }
WanWizard7219c072011-12-28 14:09:05 +01001408
Kyle Farris0c147b32011-08-26 02:29:31 -04001409 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001410 }
Barry Mienydd671972010-10-04 16:33:58 +02001411
Phil Sturgeon9789f322011-07-15 15:14:05 -06001412 // --------------------------------------------------------------------
1413
1414 /**
1415 * Replace
1416 *
1417 * Compiles an replace into string and runs the query
1418 *
1419 * @param string the table to replace data into
1420 * @param array an associative array of insert values
1421 * @return object
1422 */
1423 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001424 {
1425 if ( ! is_null($set))
1426 {
1427 $this->set($set);
1428 }
Barry Mienydd671972010-10-04 16:33:58 +02001429
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001430 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001431 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001432 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001433 }
1434
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001435 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001436 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001437 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001438 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001439 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001440 }
Barry Mienydd671972010-10-04 16:33:58 +02001441
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001442 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001443 }
1444
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001445 $sql = $this->_replace($this->protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->qb_set), array_values($this->qb_set));
Jamie Rumbelow3b1355c2012-03-06 21:27:46 +00001446
Derek Jonesd10e8962010-03-02 17:10:36 -06001447 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001448 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001449 }
WanWizard7219c072011-12-28 14:09:05 +01001450
Kyle Farris0c147b32011-08-26 02:29:31 -04001451 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001452
Kyle Farris0c147b32011-08-26 02:29:31 -04001453 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001454 * Replace statement
1455 *
1456 * Generates a platform-specific replace string from the supplied data
1457 *
1458 * @param string the table name
1459 * @param array the insert keys
1460 * @param array the insert values
1461 * @return string
1462 */
1463 protected function _replace($table, $keys, $values)
1464 {
1465 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1466 }
1467
1468 // --------------------------------------------------------------------
1469
1470 /**
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001471 * From Tables
1472 *
1473 * This public function implicitly groups FROM tables so there is no confusion
1474 * about operator precedence in harmony with SQL standards
1475 *
1476 * @param array
1477 * @return string
1478 */
1479 protected function _from_tables($tables)
1480 {
1481 is_array($tables) OR $tables = array($tables);
1482
1483 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
1484 }
1485
1486 // --------------------------------------------------------------------
1487
1488 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001489 * Get UPDATE query string
1490 *
1491 * Compiles an update query and returns the sql
1492 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001493 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001494 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001495 * @return string
1496 */
1497 public function get_compiled_update($table = '', $reset = TRUE)
1498 {
1499 // Combine any cached components with the current statements
1500 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001501
Kyle Farris0c147b32011-08-26 02:29:31 -04001502 if ($this->_validate_update($table) === FALSE)
1503 {
1504 return FALSE;
1505 }
WanWizard7219c072011-12-28 14:09:05 +01001506
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001507 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set, $this->qb_where, $this->qb_orderby, $this->qb_limit);
WanWizard7219c072011-12-28 14:09:05 +01001508
Kyle Farris0c147b32011-08-26 02:29:31 -04001509 if ($reset === TRUE)
1510 {
1511 $this->_reset_write();
1512 }
WanWizard7219c072011-12-28 14:09:05 +01001513
Kyle Farris0c147b32011-08-26 02:29:31 -04001514 return $sql;
1515 }
WanWizard7219c072011-12-28 14:09:05 +01001516
Derek Allard2067d1a2008-11-13 22:59:24 +00001517 // --------------------------------------------------------------------
1518
1519 /**
1520 * Update
1521 *
1522 * Compiles an update string and runs the query
1523 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001524 * @param string the table to retrieve the results from
1525 * @param array an associative array of update values
1526 * @param mixed the where clause
1527 * @return object
1528 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001529 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001530 {
1531 // Combine any cached components with the current statements
1532 $this->_merge_cache();
1533
1534 if ( ! is_null($set))
1535 {
1536 $this->set($set);
1537 }
Barry Mienydd671972010-10-04 16:33:58 +02001538
Kyle Farris0c147b32011-08-26 02:29:31 -04001539 if ($this->_validate_update($table) === FALSE)
1540 {
1541 return FALSE;
1542 }
1543
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001544 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001545 {
1546 $this->where($where);
1547 }
1548
Andrey Andreev650b4c02012-06-11 12:07:15 +03001549 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001550 {
1551 $this->limit($limit);
1552 }
1553
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001554 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set, $this->qb_where, $this->qb_orderby, $this->qb_limit, $this->qb_like);
Jamie Rumbelow3b1355c2012-03-06 21:27:46 +00001555
Kyle Farris0c147b32011-08-26 02:29:31 -04001556 $this->_reset_write();
1557 return $this->query($sql);
1558 }
WanWizard7219c072011-12-28 14:09:05 +01001559
Kyle Farris0c147b32011-08-26 02:29:31 -04001560 // --------------------------------------------------------------------
1561
1562 /**
Andrey Andreev975034d2012-04-05 15:09:55 +03001563 * Update statement
1564 *
1565 * Generates a platform-specific update string from the supplied data
1566 *
1567 * @param string the table name
1568 * @param array the update data
1569 * @param array the where clause
1570 * @param array the orderby clause
1571 * @param array the limit clause
Andrey Andreev00541ae2012-04-09 11:43:10 +03001572 * @param array the like clause
Andrey Andreev975034d2012-04-05 15:09:55 +03001573 * @return string
1574 */
Andrey Andreev00541ae2012-04-09 11:43:10 +03001575 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Andrey Andreev975034d2012-04-05 15:09:55 +03001576 {
1577 foreach ($values as $key => $val)
1578 {
1579 $valstr[] = $key.' = '.$val;
1580 }
1581
Andrey Andreev00541ae2012-04-09 11:43:10 +03001582 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
1583
1584 if ( ! empty($like))
1585 {
1586 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
1587 }
1588
Andrey Andreev975034d2012-04-05 15:09:55 +03001589 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev00541ae2012-04-09 11:43:10 +03001590 .$where
Andrey Andreev975034d2012-04-05 15:09:55 +03001591 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
1592 .($limit ? ' LIMIT '.$limit : '');
1593 }
1594
1595 // --------------------------------------------------------------------
1596
1597 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001598 * Validate Update
1599 *
1600 * This method is used by both update() and get_compiled_update() to
1601 * validate that data is actually being set and that a table has been
1602 * chosen to be update.
1603 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001604 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001605 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001606 */
1607 protected function _validate_update($table = '')
1608 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001609 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001610 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001611 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001612 }
1613
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001614 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001615 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001616 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001617 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001618 elseif ( ! isset($this->qb_from[0]))
1619 {
1620 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1621 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001622
1623 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001624 }
WanWizard7219c072011-12-28 14:09:05 +01001625
Derek Jonesd10e8962010-03-02 17:10:36 -06001626 // --------------------------------------------------------------------
1627
1628 /**
1629 * Update_Batch
1630 *
1631 * Compiles an update string and runs the query
1632 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001633 * @param string the table to retrieve the results from
1634 * @param array an associative array of update values
1635 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001636 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001637 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001638 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001639 {
1640 // Combine any cached components with the current statements
1641 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001642
Derek Jonesd10e8962010-03-02 17:10:36 -06001643 if (is_null($index))
1644 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001645 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001646 }
1647
1648 if ( ! is_null($set))
1649 {
1650 $this->set_update_batch($set, $index);
1651 }
1652
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001653 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001654 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001655 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001656 }
1657
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001658 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001659 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001660 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001661 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001662 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001663 }
Barry Mienydd671972010-10-04 16:33:58 +02001664
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001665 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001666 }
Barry Mienydd671972010-10-04 16:33:58 +02001667
Derek Jonesd10e8962010-03-02 17:10:36 -06001668 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001669 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001670 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001671 $this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, 100), $this->protect_identifiers($index), $this->qb_where));
Derek Jonesd10e8962010-03-02 17:10:36 -06001672 }
Barry Mienydd671972010-10-04 16:33:58 +02001673
Derek Jonesd10e8962010-03-02 17:10:36 -06001674 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001675 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001676 }
1677
1678 // --------------------------------------------------------------------
1679
1680 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001681 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001682 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001683 * @param array
1684 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001685 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001686 * @return object
1687 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001688 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001689 {
1690 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001691
Derek Jonesd10e8962010-03-02 17:10:36 -06001692 if ( ! is_array($key))
1693 {
1694 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001695 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001696
1697 foreach ($key as $k => $v)
1698 {
1699 $index_set = FALSE;
1700 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001701 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001702 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001703 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001704 {
1705 $index_set = TRUE;
1706 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001707
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001708 $clean[$this->protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001709 }
1710
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001711 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001712 {
1713 return $this->display_error('db_batch_missing_index');
1714 }
1715
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001716 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001717 }
Barry Mienydd671972010-10-04 16:33:58 +02001718
Derek Jonesd10e8962010-03-02 17:10:36 -06001719 return $this;
1720 }
1721
Derek Allard2067d1a2008-11-13 22:59:24 +00001722 // --------------------------------------------------------------------
1723
1724 /**
1725 * Empty Table
1726 *
1727 * Compiles a delete string and runs "DELETE FROM table"
1728 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001729 * @param string the table to empty
1730 * @return object
1731 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001732 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001734 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001735 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001736 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001737 {
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
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001741 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 }
1743 else
1744 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001745 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001746 }
1747
1748 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001750 return $this->query($sql);
1751 }
1752
1753 // --------------------------------------------------------------------
1754
1755 /**
1756 * Truncate
1757 *
1758 * Compiles a truncate string and runs the query
1759 * If the database does not support the truncate() command
1760 * This function maps to "DELETE FROM table"
1761 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001762 * @param string the table to truncate
1763 * @return object
1764 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001765 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001766 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001767 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001768 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001769 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001770 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001771 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 }
1773
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001774 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001775 }
1776 else
1777 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001778 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001779 }
1780
1781 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001782 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001783 return $this->query($sql);
1784 }
WanWizard7219c072011-12-28 14:09:05 +01001785
Kyle Farris0c147b32011-08-26 02:29:31 -04001786 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001787
Kyle Farris0c147b32011-08-26 02:29:31 -04001788 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001789 * Truncate statement
1790 *
1791 * Generates a platform-specific truncate string from the supplied data
1792 *
1793 * If the database does not support the truncate() command,
1794 * then this method maps to 'DELETE FROM table'
1795 *
1796 * @param string the table name
1797 * @return string
1798 */
1799 protected function _truncate($table)
1800 {
1801 return 'TRUNCATE '.$table;
1802 }
1803
1804 // --------------------------------------------------------------------
1805
1806 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001807 * Get DELETE query string
1808 *
1809 * Compiles a delete query string and returns the sql
1810 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001811 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001812 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001813 * @return string
1814 */
1815 public function get_compiled_delete($table = '', $reset = TRUE)
1816 {
1817 $this->return_delete_sql = TRUE;
1818 $sql = $this->delete($table, '', NULL, $reset);
1819 $this->return_delete_sql = FALSE;
1820 return $sql;
1821 }
WanWizard7219c072011-12-28 14:09:05 +01001822
Derek Allard2067d1a2008-11-13 22:59:24 +00001823 // --------------------------------------------------------------------
1824
1825 /**
1826 * Delete
1827 *
1828 * Compiles a delete string and runs the query
1829 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001830 * @param mixed the table(s) to delete from. String or array
1831 * @param mixed the where clause
1832 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001833 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001834 * @return object
1835 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001836 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001837 {
1838 // Combine any cached components with the current statements
1839 $this->_merge_cache();
1840
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001841 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001842 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001843 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001844 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001845 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001846 }
1847
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001848 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001849 }
1850 elseif (is_array($table))
1851 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001852 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001853 {
1854 $this->delete($single_table, $where, $limit, FALSE);
1855 }
1856
1857 $this->_reset_write();
1858 return;
1859 }
1860 else
1861 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001862 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001863 }
1864
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001865 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001866 {
1867 $this->where($where);
1868 }
1869
Andrey Andreev650b4c02012-06-11 12:07:15 +03001870 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001871 {
1872 $this->limit($limit);
1873 }
1874
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001875 if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001876 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001877 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001878 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001879
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001880 $sql = $this->_delete($table, $this->qb_where, $this->qb_like, $this->qb_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001881 if ($reset_data)
1882 {
1883 $this->_reset_write();
1884 }
WanWizard7219c072011-12-28 14:09:05 +01001885
Andrey Andreev24276a32012-01-08 02:44:38 +02001886 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001887 }
WanWizard7219c072011-12-28 14:09:05 +01001888
Derek Allard2067d1a2008-11-13 22:59:24 +00001889 // --------------------------------------------------------------------
1890
1891 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001892 * Delete statement
1893 *
1894 * Generates a platform-specific delete string from the supplied data
1895 *
1896 * @param string the table name
1897 * @param array the where clause
1898 * @param array the like clause
1899 * @param string the limit clause
1900 * @return string
1901 */
1902 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
1903 {
1904 $conditions = array();
1905
1906 empty($where) OR $conditions[] = implode(' ', $where);
1907 empty($like) OR $conditions[] = implode(' ', $like);
1908
1909 return 'DELETE FROM '.$table
1910 .(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '')
Andrey Andreev650b4c02012-06-11 12:07:15 +03001911 .($limit ? ' LIMIT '.(int) $limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001912 }
1913
1914 // --------------------------------------------------------------------
1915
1916 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001917 * DB Prefix
1918 *
1919 * Prepends a database prefix if one exists in configuration
1920 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001921 * @param string the table
1922 * @return string
1923 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001924 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001925 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001926 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001927 {
1928 $this->display_error('db_table_name_required');
1929 }
1930
1931 return $this->dbprefix.$table;
1932 }
1933
1934 // --------------------------------------------------------------------
1935
1936 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001937 * Set DB Prefix
1938 *
1939 * Set's the DB Prefix to something new without needing to reconnect
1940 *
1941 * @param string the prefix
1942 * @return string
1943 */
1944 public function set_dbprefix($prefix = '')
1945 {
1946 return $this->dbprefix = $prefix;
1947 }
1948
1949 // --------------------------------------------------------------------
1950
1951 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001952 * Track Aliases
1953 *
1954 * Used to track SQL statements written with aliased tables.
1955 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001956 * @param string The table to inspect
1957 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001958 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001959 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001960 {
1961 if (is_array($table))
1962 {
1963 foreach ($table as $t)
1964 {
1965 $this->_track_aliases($t);
1966 }
1967 return;
1968 }
Barry Mienydd671972010-10-04 16:33:58 +02001969
Derek Jones37f4b9c2011-07-01 17:56:50 -05001970 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001971 // the string into discreet statements
1972 if (strpos($table, ',') !== FALSE)
1973 {
1974 return $this->_track_aliases(explode(',', $table));
1975 }
Barry Mienydd671972010-10-04 16:33:58 +02001976
Derek Allard2067d1a2008-11-13 22:59:24 +00001977 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001978 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001979 {
1980 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03001981 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001982
Derek Allard2067d1a2008-11-13 22:59:24 +00001983 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001984 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001985
Derek Allard2067d1a2008-11-13 22:59:24 +00001986 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001987 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001988 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001989 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001990 }
1991 }
1992 }
WanWizard7219c072011-12-28 14:09:05 +01001993
Derek Allard2067d1a2008-11-13 22:59:24 +00001994 // --------------------------------------------------------------------
1995
1996 /**
1997 * Compile the SELECT statement
1998 *
1999 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05002000 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00002001 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002002 * @return string
2003 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002004 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002005 {
2006 // Combine any cached components with the current statements
2007 $this->_merge_cache();
2008
Derek Allard2067d1a2008-11-13 22:59:24 +00002009 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002010 if ($select_override !== FALSE)
2011 {
2012 $sql = $select_override;
2013 }
2014 else
2015 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002016 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002017
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002018 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002019 {
Barry Mienydd671972010-10-04 16:33:58 +02002020 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002021 }
2022 else
Barry Mienydd671972010-10-04 16:33:58 +02002023 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002024 // Cycle through the "select" portion of the query and prep each column name.
2025 // The reason we protect identifiers here rather then in the select() function
2026 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002027 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002028 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002029 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002030 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002031 }
Barry Mienydd671972010-10-04 16:33:58 +02002032
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002033 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002034 }
2035 }
2036
Derek Allard2067d1a2008-11-13 22:59:24 +00002037 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002038 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002039 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002040 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002041 }
2042
Derek Allard2067d1a2008-11-13 22:59:24 +00002043 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002044 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002045 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002046 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002047 }
2048
Derek Allard2067d1a2008-11-13 22:59:24 +00002049 // Write the "WHERE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002050 if (count($this->qb_where) > 0 OR count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002051 {
Greg Akere156c6e2011-04-20 16:03:04 -05002052 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 }
2054
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002055 $sql .= implode("\n", $this->qb_where);
Derek Allard2067d1a2008-11-13 22:59:24 +00002056
Derek Allard2067d1a2008-11-13 22:59:24 +00002057 // Write the "LIKE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002058 if (count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002059 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002060 if (count($this->qb_where) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002061 {
2062 $sql .= "\nAND ";
2063 }
2064
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002065 $sql .= implode("\n", $this->qb_like);
Derek Allard2067d1a2008-11-13 22:59:24 +00002066 }
2067
Derek Allard2067d1a2008-11-13 22:59:24 +00002068 // Write the "GROUP BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002069 if (count($this->qb_groupby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002070 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002071 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002072 }
2073
Derek Allard2067d1a2008-11-13 22:59:24 +00002074 // Write the "HAVING" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002075 if (count($this->qb_having) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002076 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002077 $sql .= "\nHAVING ".implode("\n", $this->qb_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00002078 }
2079
Derek Allard2067d1a2008-11-13 22:59:24 +00002080 // Write the "ORDER BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002081 if (count($this->qb_orderby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002082 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002083 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002084 }
2085
Derek Allard2067d1a2008-11-13 22:59:24 +00002086 // Write the "LIMIT" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002087 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002088 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002089 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00002090 }
2091
2092 return $sql;
2093 }
2094
2095 // --------------------------------------------------------------------
2096
2097 /**
2098 * Object to Array
2099 *
2100 * Takes an object as input and converts the class variables to array key/vals
2101 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002102 * @param object
2103 * @return array
2104 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002105 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002106 {
2107 if ( ! is_object($object))
2108 {
2109 return $object;
2110 }
Barry Mienydd671972010-10-04 16:33:58 +02002111
Derek Allard2067d1a2008-11-13 22:59:24 +00002112 $array = array();
2113 foreach (get_object_vars($object) as $key => $val)
2114 {
2115 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002116 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002117 {
2118 $array[$key] = $val;
2119 }
2120 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002121
2122 return $array;
2123 }
Barry Mienydd671972010-10-04 16:33:58 +02002124
Derek Jonesd10e8962010-03-02 17:10:36 -06002125 // --------------------------------------------------------------------
2126
2127 /**
2128 * Object to Array
2129 *
2130 * Takes an object as input and converts the class variables to array key/vals
2131 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002132 * @param object
2133 * @return array
2134 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002135 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002136 {
2137 if ( ! is_object($object))
2138 {
2139 return $object;
2140 }
Barry Mienydd671972010-10-04 16:33:58 +02002141
Derek Jonesd10e8962010-03-02 17:10:36 -06002142 $array = array();
2143 $out = get_object_vars($object);
2144 $fields = array_keys($out);
2145
2146 foreach ($fields as $val)
2147 {
2148 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002149 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002150 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002151 $i = 0;
2152 foreach ($out[$val] as $data)
2153 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002154 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002155 }
2156 }
2157 }
2158
Derek Allard2067d1a2008-11-13 22:59:24 +00002159 return $array;
2160 }
Barry Mienydd671972010-10-04 16:33:58 +02002161
Derek Allard2067d1a2008-11-13 22:59:24 +00002162 // --------------------------------------------------------------------
2163
2164 /**
2165 * Start Cache
2166 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002167 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002168 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002169 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002170 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002171 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002172 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002173 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002174 }
2175
2176 // --------------------------------------------------------------------
2177
2178 /**
2179 * Stop Cache
2180 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002181 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002182 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002183 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002184 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002185 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002186 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002187 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002188 }
2189
2190 // --------------------------------------------------------------------
2191
2192 /**
2193 * Flush Cache
2194 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002195 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002196 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002197 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002198 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002199 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002200 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002201 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002202 'qb_cache_select' => array(),
2203 'qb_cache_from' => array(),
2204 'qb_cache_join' => array(),
2205 'qb_cache_where' => array(),
2206 'qb_cache_like' => array(),
2207 'qb_cache_groupby' => array(),
2208 'qb_cache_having' => array(),
2209 'qb_cache_orderby' => array(),
2210 'qb_cache_set' => array(),
2211 'qb_cache_exists' => array(),
2212 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002213 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002214 }
2215
2216 // --------------------------------------------------------------------
2217
2218 /**
2219 * Merge Cache
2220 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002221 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002222 * locally called ones.
2223 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002224 * @return void
2225 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002226 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002227 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002228 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002229 {
2230 return;
2231 }
2232
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002233 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002234 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002235 $qb_variable = 'qb_'.$val;
2236 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002237
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002238 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002239 {
2240 continue;
2241 }
2242
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002243 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002244 }
2245
2246 // If we are "protecting identifiers" we need to examine the "from"
2247 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002248 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002249 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002250 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002251 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002252
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002253 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002254 }
WanWizard7219c072011-12-28 14:09:05 +01002255
Kyle Farris0c147b32011-08-26 02:29:31 -04002256 // --------------------------------------------------------------------
2257
2258 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002259 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002260 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002261 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002262 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002263 * @return void
2264 */
2265 public function reset_query()
2266 {
2267 $this->_reset_select();
2268 $this->_reset_write();
2269 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002270
2271 // --------------------------------------------------------------------
2272
2273 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002274 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002275 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002276 * @param array An array of fields to reset
2277 * @return void
2278 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002279 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002280 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002281 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002282 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002283 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002284 {
2285 $this->$item = $default_value;
2286 }
2287 }
2288 }
2289
2290 // --------------------------------------------------------------------
2291
2292 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002293 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002294 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002295 * @return void
2296 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002297 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002298 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002299 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002300 'qb_select' => array(),
2301 'qb_from' => array(),
2302 'qb_join' => array(),
2303 'qb_where' => array(),
2304 'qb_like' => array(),
2305 'qb_groupby' => array(),
2306 'qb_having' => array(),
2307 'qb_orderby' => array(),
2308 'qb_wherein' => array(),
2309 'qb_aliased_tables' => array(),
2310 'qb_no_escape' => array(),
2311 'qb_distinct' => FALSE,
2312 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002313 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002314 )
2315 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002316 }
Barry Mienydd671972010-10-04 16:33:58 +02002317
Derek Allard2067d1a2008-11-13 22:59:24 +00002318 // --------------------------------------------------------------------
2319
2320 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002321 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002322 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002323 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002324 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002325 * @return void
2326 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002327 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002328 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002329 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002330 'qb_set' => array(),
2331 'qb_from' => array(),
2332 'qb_where' => array(),
2333 'qb_like' => array(),
2334 'qb_orderby' => array(),
2335 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002336 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002337 )
2338 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002339 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002340
Derek Allard2067d1a2008-11-13 22:59:24 +00002341}
2342
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002343/* End of file DB_query_builder.php */
Andrey Andreeve4c30192012-06-04 15:08:24 +03002344/* Location: ./system/database/DB_query_builder.php */