blob: 3b45bbada9838ec3a6effeb7b4ad74af4b490ca1 [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;
56 protected $qb_order = FALSE;
57 protected $qb_orderby = array();
58 protected $qb_set = array();
59 protected $qb_wherein = array();
60 protected $qb_aliased_tables = array();
61 protected $qb_store_array = array();
62 protected $qb_where_group_started = FALSE;
63 protected $qb_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +020064
Jamie Rumbelow7efad202012-02-19 12:37:00 +000065 // Query Builder Caching variables
66 protected $qb_caching = FALSE;
67 protected $qb_cache_exists = array();
68 protected $qb_cache_select = array();
69 protected $qb_cache_from = array();
70 protected $qb_cache_join = array();
71 protected $qb_cache_where = array();
72 protected $qb_cache_like = array();
73 protected $qb_cache_groupby = array();
74 protected $qb_cache_having = array();
75 protected $qb_cache_orderby = array();
76 protected $qb_cache_set = array();
WanWizard7219c072011-12-28 14:09:05 +010077
Jamie Rumbelow7efad202012-02-19 12:37:00 +000078 protected $qb_no_escape = array();
79 protected $qb_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000080
81 /**
82 * Select
83 *
84 * Generates the SELECT portion of the query
85 *
Derek Allard2067d1a2008-11-13 22:59:24 +000086 * @param string
87 * @return object
88 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060089 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000090 {
Derek Allard2067d1a2008-11-13 22:59:24 +000091 if (is_string($select))
92 {
93 $select = explode(',', $select);
94 }
95
96 foreach ($select as $val)
97 {
98 $val = trim($val);
99
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100100 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000102 $this->qb_select[] = $val;
103 $this->qb_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000104
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000105 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000107 $this->qb_cache_select[] = $val;
108 $this->qb_cache_exists[] = 'select';
109 $this->qb_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 }
111 }
112 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 return $this;
115 }
116
117 // --------------------------------------------------------------------
118
119 /**
120 * Select Max
121 *
122 * Generates a SELECT MAX(field) portion of a query
123 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 * @param string the field
125 * @param string an alias
126 * @return object
127 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600128 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
130 return $this->_max_min_avg_sum($select, $alias, 'MAX');
131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 // --------------------------------------------------------------------
134
135 /**
136 * Select Min
137 *
138 * Generates a SELECT MIN(field) portion of a query
139 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 * @param string the field
141 * @param string an alias
142 * @return object
143 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600144 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
146 return $this->_max_min_avg_sum($select, $alias, 'MIN');
147 }
148
149 // --------------------------------------------------------------------
150
151 /**
152 * Select Average
153 *
154 * Generates a SELECT AVG(field) portion of a query
155 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 * @param string the field
157 * @param string an alias
158 * @return object
159 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600160 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
162 return $this->_max_min_avg_sum($select, $alias, 'AVG');
163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Select Sum
169 *
170 * Generates a SELECT SUM(field) portion of a query
171 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 * @param string the field
173 * @param string an alias
174 * @return object
175 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600176 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
178 return $this->_max_min_avg_sum($select, $alias, 'SUM');
179 }
180
181 // --------------------------------------------------------------------
182
183 /**
184 * Processing Function for the four functions above:
185 *
186 * select_max()
187 * select_min()
188 * select_avg()
Andrey Andreev24276a32012-01-08 02:44:38 +0200189 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200190 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 * @param string the field
192 * @param string an alias
193 * @return object
194 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600195 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100197 if ( ! is_string($select) OR $select === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
199 $this->display_error('db_invalid_query');
200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
205 {
206 show_error('Invalid function type: '.$type);
207 }
Barry Mienydd671972010-10-04 16:33:58 +0200208
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100209 if ($alias === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
211 $alias = $this->_create_alias_from_table(trim($select));
212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300214 $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->escape_identifiers(trim($alias));
215
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000216 $this->qb_select[] = $sql;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100217 $this->qb_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200218
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000219 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000221 $this->qb_cache_select[] = $sql;
222 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 return $this;
226 }
227
228 // --------------------------------------------------------------------
229
230 /**
231 * Determines the alias name based on the table
232 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 * @param string
234 * @return string
235 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600236 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
238 if (strpos($item, '.') !== FALSE)
239 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200240 $item = explode('.', $item);
241 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 return $item;
245 }
246
247 // --------------------------------------------------------------------
248
249 /**
250 * DISTINCT
251 *
252 * Sets a flag which tells the query string compiler to add DISTINCT
253 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 * @param bool
255 * @return object
256 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600257 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300259 $this->qb_distinct = is_bool($val) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 return $this;
261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 // --------------------------------------------------------------------
264
265 /**
266 * From
267 *
268 * Generates the FROM portion of the query
269 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 * @param mixed can be a string or array
271 * @return object
272 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600273 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300275 foreach ((array) $from as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
277 if (strpos($val, ',') !== FALSE)
278 {
279 foreach (explode(',', $val) as $v)
280 {
281 $v = trim($v);
282 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200283
George Petsagourakis193d4482012-04-28 11:16:18 +0300284 $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000285
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000286 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000288 $this->qb_cache_from[] = $v;
289 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200290 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 }
293 else
294 {
295 $val = trim($val);
296
Andrey Andreev24276a32012-01-08 02:44:38 +0200297 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000298 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200300
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000301 $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000302
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000303 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000305 $this->qb_cache_from[] = $val;
306 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 }
308 }
309 }
310
311 return $this;
312 }
313
314 // --------------------------------------------------------------------
315
316 /**
317 * Join
318 *
319 * Generates the JOIN portion of the query
320 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 * @param string
322 * @param string the join condition
323 * @param string the type of join
324 * @return object
325 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600326 public function join($table, $cond, $type = '')
Barry Mienydd671972010-10-04 16:33:58 +0200327 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100328 if ($type !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
330 $type = strtoupper(trim($type));
331
332 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
333 {
334 $type = '';
335 }
336 else
337 {
338 $type .= ' ';
339 }
340 }
341
Andrey Andreev24276a32012-01-08 02:44:38 +0200342 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000343 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 $this->_track_aliases($table);
345
346 // Strip apart the condition and protect the identifiers
Hamza Bhattid3c1ccf2012-03-05 22:58:56 +0400347 if (preg_match('/([\[\w\.]+)([\W\s]+)(.+)/', $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200349 $cond = $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 // Assemble the JOIN statement
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000353 $this->qb_join[] = $join = $type.'JOIN '.$this->protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000354
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000355 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000357 $this->qb_cache_join[] = $join;
358 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
360
361 return $this;
362 }
363
364 // --------------------------------------------------------------------
365
366 /**
367 * Where
368 *
369 * Generates the WHERE portion of the query. Separates
370 * multiple calls with AND
371 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 * @param mixed
373 * @param mixed
374 * @return object
375 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600376 public function where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 return $this->_where($key, $value, 'AND ', $escape);
379 }
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 // --------------------------------------------------------------------
382
383 /**
384 * OR Where
385 *
386 * Generates the WHERE portion of the query. Separates
387 * multiple calls with OR
388 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 * @param mixed
390 * @param mixed
391 * @return object
392 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600393 public function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
395 return $this->_where($key, $value, 'OR ', $escape);
396 }
397
398 // --------------------------------------------------------------------
399
400 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 * Where
402 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600403 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * @param mixed
406 * @param mixed
407 * @param string
408 * @return object
409 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600410 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
WanWizard7219c072011-12-28 14:09:05 +0100412 $type = $this->_group_get_type($type);
413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 if ( ! is_array($key))
415 {
416 $key = array($key => $value);
417 }
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 // If the escape value was not set will will base it on the global setting
420 if ( ! is_bool($escape))
421 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +0000422 $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 }
424
425 foreach ($key as $k => $v)
426 {
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100427 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000428
429 if (is_null($v) && ! $this->_has_operator($k))
430 {
431 // value appears not to have been set, assign the test to IS NULL
432 $k .= ' IS NULL';
433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 if ( ! is_null($v))
436 {
437 if ($escape === TRUE)
438 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200439 $k = $this->protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 $v = ' '.$this->escape($v);
441 }
WanWizard7219c072011-12-28 14:09:05 +0100442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 if ( ! $this->_has_operator($k))
444 {
Greg Akere156c6e2011-04-20 16:03:04 -0500445 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
447 }
448 else
449 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200450 $k = $this->protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 }
452
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000453 $this->qb_where[] = $prefix.$k.$v;
454 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000456 $this->qb_cache_where[] = $prefix.$k.$v;
457 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 return $this;
463 }
464
465 // --------------------------------------------------------------------
466
467 /**
468 * Where_in
469 *
470 * Generates a WHERE field IN ('item', 'item') SQL query joined with
471 * AND if appropriate
472 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 * @param string The field to search
474 * @param array The values searched on
475 * @return object
476 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600477 public function where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 {
479 return $this->_where_in($key, $values);
480 }
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 // --------------------------------------------------------------------
483
484 /**
485 * Where_in_or
486 *
487 * Generates a WHERE field IN ('item', 'item') SQL query joined with
488 * OR if appropriate
489 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 * @param string The field to search
491 * @param array The values searched on
492 * @return object
493 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600494 public function or_where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
496 return $this->_where_in($key, $values, FALSE, 'OR ');
497 }
498
499 // --------------------------------------------------------------------
500
501 /**
502 * Where_not_in
503 *
504 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
505 * with AND if appropriate
506 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 * @param string The field to search
508 * @param array The values searched on
509 * @return object
510 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600511 public function where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 {
513 return $this->_where_in($key, $values, TRUE);
514 }
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 // --------------------------------------------------------------------
517
518 /**
519 * Where_not_in_or
520 *
521 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
522 * with OR if appropriate
523 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 * @param string The field to search
525 * @param array The values searched on
526 * @return object
527 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600528 public function or_where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 {
530 return $this->_where_in($key, $values, TRUE, 'OR ');
531 }
532
533 // --------------------------------------------------------------------
534
535 /**
536 * Where_in
537 *
538 * Called by where_in, where_in_or, where_not_in, where_not_in_or
539 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 * @param string The field to search
541 * @param array The values searched on
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300542 * @param bool If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200543 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 * @return object
545 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600546 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 {
548 if ($key === NULL OR $values === NULL)
549 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300550 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552
WanWizard7219c072011-12-28 14:09:05 +0100553 $type = $this->_group_get_type($type);
554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 if ( ! is_array($values))
556 {
557 $values = array($values);
558 }
Barry Mienydd671972010-10-04 16:33:58 +0200559
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 $not = ($not) ? ' NOT' : '';
561
562 foreach ($values as $value)
563 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000564 $this->qb_wherein[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 }
566
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000567 $prefix = (count($this->qb_where) === 0) ? '' : $type;
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000568 $this->qb_where[] = $where_in = $prefix.$this->protect_identifiers($key).$not.' IN ('.implode(', ', $this->qb_wherein).') ';
Barry Mienydd671972010-10-04 16:33:58 +0200569
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000570 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000572 $this->qb_cache_where[] = $where_in;
573 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 }
575
576 // reset the array for multiple calls
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000577 $this->qb_wherein = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 return $this;
579 }
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 // --------------------------------------------------------------------
582
583 /**
584 * Like
585 *
586 * Generates a %LIKE% portion of the query. Separates
587 * multiple calls with AND
588 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 * @param mixed
590 * @param mixed
591 * @return object
592 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600593 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
595 return $this->_like($field, $match, 'AND ', $side);
596 }
597
598 // --------------------------------------------------------------------
599
600 /**
601 * Not Like
602 *
603 * Generates a NOT LIKE portion of the query. Separates
604 * multiple calls with AND
605 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 * @param mixed
607 * @param mixed
608 * @return object
609 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600610 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 {
612 return $this->_like($field, $match, 'AND ', $side, 'NOT');
613 }
Barry Mienydd671972010-10-04 16:33:58 +0200614
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 // --------------------------------------------------------------------
616
617 /**
618 * OR Like
619 *
620 * Generates a %LIKE% portion of the query. Separates
621 * multiple calls with OR
622 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 * @param mixed
624 * @param mixed
625 * @return object
626 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600627 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 {
629 return $this->_like($field, $match, 'OR ', $side);
630 }
631
632 // --------------------------------------------------------------------
633
634 /**
635 * OR Not Like
636 *
637 * Generates a NOT LIKE portion of the query. Separates
638 * multiple calls with OR
639 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 * @param mixed
641 * @param mixed
642 * @return object
643 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600644 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 {
646 return $this->_like($field, $match, 'OR ', $side, 'NOT');
647 }
Barry Mienydd671972010-10-04 16:33:58 +0200648
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 // --------------------------------------------------------------------
650
651 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 * Like
653 *
654 * Called by like() or orlike()
655 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 * @param mixed
657 * @param mixed
658 * @param string
659 * @return object
660 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600661 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 {
WanWizard7219c072011-12-28 14:09:05 +0100663 $type = $this->_group_get_type($type);
664
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 if ( ! is_array($field))
666 {
667 $field = array($field => $match);
668 }
Barry Mienydd671972010-10-04 16:33:58 +0200669
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 foreach ($field as $k => $v)
671 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000672 $k = $this->protect_identifiers($k);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000673 $prefix = (count($this->qb_like) === 0) ? '' : $type;
Derek Jonese4ed5832009-02-20 21:44:59 +0000674 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300675
Andrey Andreev24276a32012-01-08 02:44:38 +0200676 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400677 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100678 $like_statement = "{$prefix} $k $not LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400679 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200680 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100682 $like_statement = "{$prefix} $k $not LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200684 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100686 $like_statement = "{$prefix} $k $not LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 }
688 else
689 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100690 $like_statement = "{$prefix} $k $not LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600692
Derek Jonese4ed5832009-02-20 21:44:59 +0000693 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100694 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000695 {
Greg Aker0d424892010-01-26 02:14:44 +0000696 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000697 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600698
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000699 $this->qb_like[] = $like_statement;
700 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000702 $this->qb_cache_like[] = $like_statement;
703 $this->qb_cache_exists[] = 'like';
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 }
Barry Mienydd671972010-10-04 16:33:58 +0200705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200707
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 return $this;
709 }
Barry Mienydd671972010-10-04 16:33:58 +0200710
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 // --------------------------------------------------------------------
712
713 /**
WanWizard7219c072011-12-28 14:09:05 +0100714 * Starts a query group.
715 *
716 * @param string (Internal use only)
717 * @param string (Internal use only)
718 * @return object
719 */
720 public function group_start($not = '', $type = 'AND ')
721 {
722 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100723
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000724 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100725 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000726 $this->qb_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (';
WanWizard7219c072011-12-28 14:09:05 +0100727
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000728 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100729 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000730 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100731 }
732
733 return $this;
734 }
735
736 // --------------------------------------------------------------------
737
738 /**
739 * Starts a query group, but ORs the group
740 *
741 * @return object
742 */
743 public function or_group_start()
744 {
745 return $this->group_start('', 'OR ');
746 }
747
748 // --------------------------------------------------------------------
749
750 /**
751 * Starts a query group, but NOTs the group
752 *
753 * @return object
754 */
755 public function not_group_start()
756 {
757 return $this->group_start('NOT ', 'AND ');
758 }
759
760 // --------------------------------------------------------------------
761
762 /**
763 * Starts a query group, but OR NOTs the group
764 *
765 * @return object
766 */
767 public function or_not_group_start()
768 {
769 return $this->group_start('NOT ', 'OR ');
770 }
771
772 // --------------------------------------------------------------------
773
774 /**
775 * Ends a query group
776 *
777 * @return object
778 */
779 public function group_end()
780 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000781 $this->qb_where_group_started = FALSE;
782 $this->qb_where[] = $value = str_repeat(' ', $this->qb_where_group_count--) . ')';
WanWizard7219c072011-12-28 14:09:05 +0100783
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000784 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100785 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000786 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100787 }
788
WanWizard7219c072011-12-28 14:09:05 +0100789 return $this;
790 }
791
792 // --------------------------------------------------------------------
793
794 /**
795 * Group_get_type
796 *
797 * Called by group_start(), _like(), _where() and _where_in()
798 *
799 * @param string
800 * @return string
801 */
802 protected function _group_get_type($type)
803 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000804 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100805 {
806 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000807 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100808 }
809
810 return $type;
811 }
812
813 // --------------------------------------------------------------------
814
815 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 * GROUP BY
817 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 * @param string
819 * @return object
820 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600821 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 {
823 if (is_string($by))
824 {
825 $by = explode(',', $by);
826 }
Barry Mienydd671972010-10-04 16:33:58 +0200827
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 foreach ($by as $val)
829 {
830 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200831
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100832 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000834 $this->qb_groupby[] = $val = $this->protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200835
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000836 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000838 $this->qb_cache_groupby[] = $val;
839 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 }
841 }
842 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200843
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 return $this;
845 }
846
847 // --------------------------------------------------------------------
848
849 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 * Sets the HAVING value
851 *
852 * Separates multiple calls with AND
853 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 * @param string
855 * @param string
856 * @return object
857 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600858 public function having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 {
860 return $this->_having($key, $value, 'AND ', $escape);
861 }
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 // --------------------------------------------------------------------
864
865 /**
866 * Sets the OR HAVING value
867 *
868 * Separates multiple calls with OR
869 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 * @param string
871 * @param string
872 * @return object
873 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600874 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 {
876 return $this->_having($key, $value, 'OR ', $escape);
877 }
Barry Mienydd671972010-10-04 16:33:58 +0200878
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 // --------------------------------------------------------------------
880
881 /**
882 * Sets the HAVING values
883 *
884 * Called by having() or or_having()
885 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 * @param string
887 * @param string
888 * @return object
889 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600890 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 {
892 if ( ! is_array($key))
893 {
894 $key = array($key => $value);
895 }
Barry Mienydd671972010-10-04 16:33:58 +0200896
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 foreach ($key as $k => $v)
898 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000899 $prefix = (count($this->qb_having) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000900
901 if ($escape === TRUE)
902 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200903 $k = $this->protect_identifiers($k);
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 }
905
906 if ( ! $this->_has_operator($k))
907 {
908 $k .= ' = ';
909 }
910
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100911 if ($v !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400913 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 }
Barry Mienydd671972010-10-04 16:33:58 +0200915
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000916 $this->qb_having[] = $prefix.$k.$v;
917 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000919 $this->qb_cache_having[] = $prefix.$k.$v;
920 $this->qb_cache_exists[] = 'having';
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 }
922 }
Barry Mienydd671972010-10-04 16:33:58 +0200923
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 return $this;
925 }
Barry Mienydd671972010-10-04 16:33:58 +0200926
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 // --------------------------------------------------------------------
928
929 /**
930 * Sets the ORDER BY value
931 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 * @param string
933 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100934 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 * @return object
936 */
pporlan2c685fb2011-12-22 12:15:25 +0100937 public function order_by($orderby, $direction = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200939 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 {
941 $orderby = ''; // Random results want or don't need a field name
942 $direction = $this->_random_keyword;
943 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100944 elseif (trim($direction) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 {
946 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
947 }
Barry Mienydd671972010-10-04 16:33:58 +0200948
949
Andrey Andreev24276a32012-01-08 02:44:38 +0200950 if ((strpos($orderby, ',') !== FALSE) && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 {
952 $temp = array();
953 foreach (explode(',', $orderby) as $part)
954 {
955 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000956 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200958 $part = $this->protect_identifiers(trim($part));
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 }
Barry Mienydd671972010-10-04 16:33:58 +0200960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 $temp[] = $part;
962 }
Barry Mienydd671972010-10-04 16:33:58 +0200963
964 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100966 elseif ($direction !== $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 {
pporlan2c685fb2011-12-22 12:15:25 +0100968 if ($escape === TRUE)
969 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200970 $orderby = $this->protect_identifiers($orderby);
pporlan2c685fb2011-12-22 12:15:25 +0100971 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 }
Barry Mienydd671972010-10-04 16:33:58 +0200973
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000974 $this->qb_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200975
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000976 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000978 $this->qb_cache_orderby[] = $orderby_statement;
979 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 }
981
982 return $this;
983 }
Barry Mienydd671972010-10-04 16:33:58 +0200984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 // --------------------------------------------------------------------
986
987 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 * Sets the LIMIT value
989 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300990 * @param int the limit value
991 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 * @return object
993 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200994 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000996 $this->qb_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000997
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200998 if ( ! is_null($offset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001000 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 }
Barry Mienydd671972010-10-04 16:33:58 +02001002
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 return $this;
1004 }
Barry Mienydd671972010-10-04 16:33:58 +02001005
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 // --------------------------------------------------------------------
1007
1008 /**
1009 * Sets the OFFSET value
1010 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001011 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 * @return object
1013 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001014 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001016 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 return $this;
1018 }
Barry Mienydd671972010-10-04 16:33:58 +02001019
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 // --------------------------------------------------------------------
1021
1022 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001023 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 * @param mixed
1026 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001027 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 * @return object
1029 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001030 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 {
1032 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001033
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 if ( ! is_array($key))
1035 {
1036 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001037 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001038
1039 foreach ($key as $k => $v)
1040 {
1041 if ($escape === FALSE)
1042 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001043 $this->qb_set[$this->protect_identifiers($k)] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 }
1045 else
1046 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001047 $this->qb_set[$this->protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 }
1049 }
Barry Mienydd671972010-10-04 16:33:58 +02001050
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 return $this;
1052 }
WanWizard7219c072011-12-28 14:09:05 +01001053
Kyle Farris0c147b32011-08-26 02:29:31 -04001054 // --------------------------------------------------------------------
1055
1056 /**
1057 * Get SELECT query string
1058 *
1059 * Compiles a SELECT query string and returns the sql.
1060 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001061 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001062 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001063 * @return string
1064 */
WanWizard7219c072011-12-28 14:09:05 +01001065 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001066 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001067 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001068 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001069 $this->_track_aliases($table);
1070 $this->from($table);
1071 }
WanWizard7219c072011-12-28 14:09:05 +01001072
Kyle Farris0c147b32011-08-26 02:29:31 -04001073 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001074
Kyle Farris0c147b32011-08-26 02:29:31 -04001075 if ($reset === TRUE)
1076 {
1077 $this->_reset_select();
1078 }
WanWizard7219c072011-12-28 14:09:05 +01001079
Kyle Farris0c147b32011-08-26 02:29:31 -04001080 return $select;
1081 }
WanWizard7219c072011-12-28 14:09:05 +01001082
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 // --------------------------------------------------------------------
1084
1085 /**
1086 * Get
1087 *
1088 * Compiles the select statement based on the other functions called
1089 * and runs the query
1090 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 * @param string the table
1092 * @param string the limit clause
1093 * @param string the offset clause
1094 * @return object
1095 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001096 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001098 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 {
1100 $this->_track_aliases($table);
1101 $this->from($table);
1102 }
Barry Mienydd671972010-10-04 16:33:58 +02001103
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 if ( ! is_null($limit))
1105 {
1106 $this->limit($limit, $offset);
1107 }
Barry Mienydd671972010-10-04 16:33:58 +02001108
Andrey Andreev24276a32012-01-08 02:44:38 +02001109 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 $this->_reset_select();
1111 return $result;
1112 }
1113
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001114 // --------------------------------------------------------------------
1115
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 /**
1117 * "Count All Results" query
1118 *
Barry Mienydd671972010-10-04 16:33:58 +02001119 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001120 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 * @param string
1123 * @return string
1124 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001125 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001127 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 {
1129 $this->_track_aliases($table);
1130 $this->from($table);
1131 }
Barry Mienydd671972010-10-04 16:33:58 +02001132
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001133 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001135
Purwandi1d160e72012-01-09 16:33:28 +07001136 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001138 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 }
1140
Purwandi1d160e72012-01-09 16:33:28 +07001141 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001142 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001144
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 // --------------------------------------------------------------------
1146
1147 /**
1148 * Get_Where
1149 *
1150 * Allows the where clause, limit and offset to be added directly
1151 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 * @param string the where clause
1153 * @param string the limit clause
1154 * @param string the offset clause
1155 * @return object
1156 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001157 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001159 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 {
1161 $this->from($table);
1162 }
1163
1164 if ( ! is_null($where))
1165 {
1166 $this->where($where);
1167 }
Barry Mienydd671972010-10-04 16:33:58 +02001168
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 if ( ! is_null($limit))
1170 {
1171 $this->limit($limit, $offset);
1172 }
Barry Mienydd671972010-10-04 16:33:58 +02001173
Andrey Andreev24276a32012-01-08 02:44:38 +02001174 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 $this->_reset_select();
1176 return $result;
1177 }
1178
1179 // --------------------------------------------------------------------
1180
1181 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001182 * Insert_Batch
1183 *
1184 * Compiles batch insert strings and runs the queries
1185 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001186 * @param string the table to retrieve the results from
1187 * @param array an associative array of insert values
1188 * @return object
1189 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001190 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001191 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001192 if ( ! is_null($set))
1193 {
1194 $this->set_insert_batch($set);
1195 }
Barry Mienydd671972010-10-04 16:33:58 +02001196
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001197 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001198 {
1199 if ($this->db_debug)
1200 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001201 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001202 return $this->display_error('db_must_use_set');
1203 }
1204 return FALSE;
1205 }
1206
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001207 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001208 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001209 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001210 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001211 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001212 }
Barry Mienydd671972010-10-04 16:33:58 +02001213
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001214 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001215 }
1216
1217 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001218 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001219 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001220 $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 -06001221 }
Barry Mienydd671972010-10-04 16:33:58 +02001222
Derek Jonesd10e8962010-03-02 17:10:36 -06001223 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001224 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001225 }
1226
1227 // --------------------------------------------------------------------
1228
1229 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001230 * Insert_batch statement
1231 *
1232 * Generates a platform-specific insert string from the supplied data.
1233 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001234 * @param string the table name
1235 * @param array the insert keys
1236 * @param array the insert values
1237 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001238 */
1239 protected function _insert_batch($table, $keys, $values)
1240 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001241 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001242 }
1243
1244 // --------------------------------------------------------------------
1245
1246 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001247 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001248 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001249 * @param mixed
1250 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001251 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001252 * @return object
1253 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001254 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001255 {
1256 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001257
Derek Jonesd10e8962010-03-02 17:10:36 -06001258 if ( ! is_array($key))
1259 {
1260 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001261 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001262
Iban Eguia3c0a4522012-04-15 13:30:44 +02001263 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001264 sort($keys);
1265
1266 foreach ($key as $row)
1267 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001268 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001269 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1270 {
1271 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001272 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001273 return;
1274 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001275
Barry Mienydd671972010-10-04 16:33:58 +02001276 ksort($row); // puts $row in the same order as our keys
1277
Derek Jonesd10e8962010-03-02 17:10:36 -06001278 if ($escape === FALSE)
1279 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001280 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001281 }
1282 else
1283 {
1284 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001285 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001286 {
Barry Mienydd671972010-10-04 16:33:58 +02001287 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001288 }
1289
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001290 $this->qb_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001291 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001292 }
1293
1294 foreach ($keys as $k)
1295 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001296 $this->qb_keys[] = $this->protect_identifiers($k);
Derek Jonesd10e8962010-03-02 17:10:36 -06001297 }
Barry Mienydd671972010-10-04 16:33:58 +02001298
Derek Jonesd10e8962010-03-02 17:10:36 -06001299 return $this;
1300 }
WanWizard7219c072011-12-28 14:09:05 +01001301
Kyle Farris0c147b32011-08-26 02:29:31 -04001302 // --------------------------------------------------------------------
1303
1304 /**
1305 * Get INSERT query string
1306 *
1307 * Compiles an insert query and returns the sql
1308 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001309 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001310 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001311 * @return string
1312 */
1313 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001314 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001315 if ($this->_validate_insert($table) === FALSE)
1316 {
1317 return FALSE;
1318 }
WanWizard7219c072011-12-28 14:09:05 +01001319
Kyle Farris76116012011-08-31 11:17:48 -04001320 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001321 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001322 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001323 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001324 array_keys($this->qb_set),
1325 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001326 );
WanWizard7219c072011-12-28 14:09:05 +01001327
Kyle Farris0c147b32011-08-26 02:29:31 -04001328 if ($reset === TRUE)
1329 {
1330 $this->_reset_write();
1331 }
WanWizard7219c072011-12-28 14:09:05 +01001332
Kyle Farris0c147b32011-08-26 02:29:31 -04001333 return $sql;
1334 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001335
Derek Allard2067d1a2008-11-13 22:59:24 +00001336 // --------------------------------------------------------------------
1337
1338 /**
1339 * Insert
1340 *
1341 * Compiles an insert string and runs the query
1342 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001343 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 * @param array an associative array of insert values
1345 * @return object
1346 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001347 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001348 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 if ( ! is_null($set))
1350 {
1351 $this->set($set);
1352 }
WanWizard7219c072011-12-28 14:09:05 +01001353
Kyle Farris0c147b32011-08-26 02:29:31 -04001354 if ($this->_validate_insert($table) === FALSE)
1355 {
1356 return FALSE;
1357 }
WanWizard7219c072011-12-28 14:09:05 +01001358
Kyle Farris76116012011-08-31 11:17:48 -04001359 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001360 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001361 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001362 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001363 array_keys($this->qb_set),
1364 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001365 );
Barry Mienydd671972010-10-04 16:33:58 +02001366
Kyle Farris0c147b32011-08-26 02:29:31 -04001367 $this->_reset_write();
1368 return $this->query($sql);
1369 }
WanWizard7219c072011-12-28 14:09:05 +01001370
Kyle Farris0c147b32011-08-26 02:29:31 -04001371 // --------------------------------------------------------------------
1372
1373 /**
Andrey Andreev65d537c2012-04-05 14:11:41 +03001374 * Insert statement
1375 *
1376 * Generates a platform-specific insert string from the supplied data
1377 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001378 * @param string the table name
1379 * @param array the insert keys
1380 * @param array the insert values
1381 * @return string
Andrey Andreev65d537c2012-04-05 14:11:41 +03001382 */
1383 protected function _insert($table, $keys, $values)
1384 {
1385 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1386 }
1387
1388 // --------------------------------------------------------------------
1389
1390 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001391 * Validate Insert
1392 *
1393 * This method is used by both insert() and get_compiled_insert() to
1394 * validate that the there data is actually being set and that table
1395 * has been chosen to be inserted into.
1396 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001397 * @param string the table to insert data into
1398 * @return string
1399 */
WanWizard7219c072011-12-28 14:09:05 +01001400 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001401 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001402 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001403 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001404 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001405 }
1406
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001407 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001408 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001409 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001410 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001411 elseif ( ! isset($this->qb_from[0]))
1412 {
1413 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1414 }
WanWizard7219c072011-12-28 14:09:05 +01001415
Kyle Farris0c147b32011-08-26 02:29:31 -04001416 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 }
Barry Mienydd671972010-10-04 16:33:58 +02001418
Phil Sturgeon9789f322011-07-15 15:14:05 -06001419 // --------------------------------------------------------------------
1420
1421 /**
1422 * Replace
1423 *
1424 * Compiles an replace into string and runs the query
1425 *
1426 * @param string the table to replace data into
1427 * @param array an associative array of insert values
1428 * @return object
1429 */
1430 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001431 {
1432 if ( ! is_null($set))
1433 {
1434 $this->set($set);
1435 }
Barry Mienydd671972010-10-04 16:33:58 +02001436
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001437 if (count($this->qb_set) === 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_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001440 }
1441
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001442 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001443 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001444 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001445 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001446 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001447 }
Barry Mienydd671972010-10-04 16:33:58 +02001448
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001449 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001450 }
1451
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001452 $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 +00001453
Derek Jonesd10e8962010-03-02 17:10:36 -06001454 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001455 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001456 }
WanWizard7219c072011-12-28 14:09:05 +01001457
Kyle Farris0c147b32011-08-26 02:29:31 -04001458 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001459
Kyle Farris0c147b32011-08-26 02:29:31 -04001460 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001461 * Replace statement
1462 *
1463 * Generates a platform-specific replace string from the supplied data
1464 *
1465 * @param string the table name
1466 * @param array the insert keys
1467 * @param array the insert values
1468 * @return string
1469 */
1470 protected function _replace($table, $keys, $values)
1471 {
1472 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1473 }
1474
1475 // --------------------------------------------------------------------
1476
1477 /**
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001478 * From Tables
1479 *
1480 * This public function implicitly groups FROM tables so there is no confusion
1481 * about operator precedence in harmony with SQL standards
1482 *
1483 * @param array
1484 * @return string
1485 */
1486 protected function _from_tables($tables)
1487 {
1488 is_array($tables) OR $tables = array($tables);
1489
1490 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
1491 }
1492
1493 // --------------------------------------------------------------------
1494
1495 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001496 * Get UPDATE query string
1497 *
1498 * Compiles an update query and returns the sql
1499 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001500 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001501 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001502 * @return string
1503 */
1504 public function get_compiled_update($table = '', $reset = TRUE)
1505 {
1506 // Combine any cached components with the current statements
1507 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001508
Kyle Farris0c147b32011-08-26 02:29:31 -04001509 if ($this->_validate_update($table) === FALSE)
1510 {
1511 return FALSE;
1512 }
WanWizard7219c072011-12-28 14:09:05 +01001513
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001514 $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 +01001515
Kyle Farris0c147b32011-08-26 02:29:31 -04001516 if ($reset === TRUE)
1517 {
1518 $this->_reset_write();
1519 }
WanWizard7219c072011-12-28 14:09:05 +01001520
Kyle Farris0c147b32011-08-26 02:29:31 -04001521 return $sql;
1522 }
WanWizard7219c072011-12-28 14:09:05 +01001523
Derek Allard2067d1a2008-11-13 22:59:24 +00001524 // --------------------------------------------------------------------
1525
1526 /**
1527 * Update
1528 *
1529 * Compiles an update string and runs the query
1530 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001531 * @param string the table to retrieve the results from
1532 * @param array an associative array of update values
1533 * @param mixed the where clause
1534 * @return object
1535 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001536 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 {
1538 // Combine any cached components with the current statements
1539 $this->_merge_cache();
1540
1541 if ( ! is_null($set))
1542 {
1543 $this->set($set);
1544 }
Barry Mienydd671972010-10-04 16:33:58 +02001545
Kyle Farris0c147b32011-08-26 02:29:31 -04001546 if ($this->_validate_update($table) === FALSE)
1547 {
1548 return FALSE;
1549 }
1550
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001551 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001552 {
1553 $this->where($where);
1554 }
1555
Andrey Andreeve4c30192012-06-04 15:08:24 +03001556 if ($limit != NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001557 {
1558 $this->limit($limit);
1559 }
1560
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001561 $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 +00001562
Kyle Farris0c147b32011-08-26 02:29:31 -04001563 $this->_reset_write();
1564 return $this->query($sql);
1565 }
WanWizard7219c072011-12-28 14:09:05 +01001566
Kyle Farris0c147b32011-08-26 02:29:31 -04001567 // --------------------------------------------------------------------
1568
1569 /**
Andrey Andreev975034d2012-04-05 15:09:55 +03001570 * Update statement
1571 *
1572 * Generates a platform-specific update string from the supplied data
1573 *
1574 * @param string the table name
1575 * @param array the update data
1576 * @param array the where clause
1577 * @param array the orderby clause
1578 * @param array the limit clause
Andrey Andreev00541ae2012-04-09 11:43:10 +03001579 * @param array the like clause
Andrey Andreev975034d2012-04-05 15:09:55 +03001580 * @return string
1581 */
Andrey Andreev00541ae2012-04-09 11:43:10 +03001582 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Andrey Andreev975034d2012-04-05 15:09:55 +03001583 {
1584 foreach ($values as $key => $val)
1585 {
1586 $valstr[] = $key.' = '.$val;
1587 }
1588
Andrey Andreev00541ae2012-04-09 11:43:10 +03001589 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
1590
1591 if ( ! empty($like))
1592 {
1593 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
1594 }
1595
Andrey Andreev975034d2012-04-05 15:09:55 +03001596 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev00541ae2012-04-09 11:43:10 +03001597 .$where
Andrey Andreev975034d2012-04-05 15:09:55 +03001598 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
1599 .($limit ? ' LIMIT '.$limit : '');
1600 }
1601
1602 // --------------------------------------------------------------------
1603
1604 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001605 * Validate Update
1606 *
1607 * This method is used by both update() and get_compiled_update() to
1608 * validate that data is actually being set and that a table has been
1609 * chosen to be update.
1610 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001611 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001612 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001613 */
1614 protected function _validate_update($table = '')
1615 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001616 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001617 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001618 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001619 }
1620
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001621 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001622 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001623 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001624 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001625 elseif ( ! isset($this->qb_from[0]))
1626 {
1627 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1628 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001629
1630 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001631 }
WanWizard7219c072011-12-28 14:09:05 +01001632
Derek Jonesd10e8962010-03-02 17:10:36 -06001633 // --------------------------------------------------------------------
1634
1635 /**
1636 * Update_Batch
1637 *
1638 * Compiles an update string and runs the query
1639 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001640 * @param string the table to retrieve the results from
1641 * @param array an associative array of update values
1642 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001643 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001644 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001645 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001646 {
1647 // Combine any cached components with the current statements
1648 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001649
Derek Jonesd10e8962010-03-02 17:10:36 -06001650 if (is_null($index))
1651 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001652 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001653 }
1654
1655 if ( ! is_null($set))
1656 {
1657 $this->set_update_batch($set, $index);
1658 }
1659
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001660 if (count($this->qb_set) === 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_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001663 }
1664
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001665 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001666 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001667 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001668 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001669 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001670 }
Barry Mienydd671972010-10-04 16:33:58 +02001671
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001672 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001673 }
Barry Mienydd671972010-10-04 16:33:58 +02001674
Derek Jonesd10e8962010-03-02 17:10:36 -06001675 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001676 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001677 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001678 $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 -06001679 }
Barry Mienydd671972010-10-04 16:33:58 +02001680
Derek Jonesd10e8962010-03-02 17:10:36 -06001681 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001682 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001683 }
1684
1685 // --------------------------------------------------------------------
1686
1687 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001688 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001689 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001690 * @param array
1691 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001692 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001693 * @return object
1694 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001695 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001696 {
1697 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001698
Derek Jonesd10e8962010-03-02 17:10:36 -06001699 if ( ! is_array($key))
1700 {
1701 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001702 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001703
1704 foreach ($key as $k => $v)
1705 {
1706 $index_set = FALSE;
1707 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001708 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001709 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001710 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001711 {
1712 $index_set = TRUE;
1713 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001714
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001715 $clean[$this->protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001716 }
1717
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001718 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001719 {
1720 return $this->display_error('db_batch_missing_index');
1721 }
1722
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001723 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001724 }
Barry Mienydd671972010-10-04 16:33:58 +02001725
Derek Jonesd10e8962010-03-02 17:10:36 -06001726 return $this;
1727 }
1728
Derek Allard2067d1a2008-11-13 22:59:24 +00001729 // --------------------------------------------------------------------
1730
1731 /**
1732 * Empty Table
1733 *
1734 * Compiles a delete string and runs "DELETE FROM table"
1735 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001736 * @param string the table to empty
1737 * @return object
1738 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001739 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001740 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001741 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001743 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001744 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001745 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001746 }
1747
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001748 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 }
1750 else
1751 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001752 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001753 }
1754
1755 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001756 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001757 return $this->query($sql);
1758 }
1759
1760 // --------------------------------------------------------------------
1761
1762 /**
1763 * Truncate
1764 *
1765 * Compiles a truncate string and runs the query
1766 * If the database does not support the truncate() command
1767 * This function maps to "DELETE FROM table"
1768 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001769 * @param string the table to truncate
1770 * @return object
1771 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001772 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001773 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001774 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001775 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001776 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001777 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001778 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001779 }
1780
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001781 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001782 }
1783 else
1784 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001785 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001786 }
1787
1788 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001790 return $this->query($sql);
1791 }
WanWizard7219c072011-12-28 14:09:05 +01001792
Kyle Farris0c147b32011-08-26 02:29:31 -04001793 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001794
Kyle Farris0c147b32011-08-26 02:29:31 -04001795 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001796 * Truncate statement
1797 *
1798 * Generates a platform-specific truncate string from the supplied data
1799 *
1800 * If the database does not support the truncate() command,
1801 * then this method maps to 'DELETE FROM table'
1802 *
1803 * @param string the table name
1804 * @return string
1805 */
1806 protected function _truncate($table)
1807 {
1808 return 'TRUNCATE '.$table;
1809 }
1810
1811 // --------------------------------------------------------------------
1812
1813 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001814 * Get DELETE query string
1815 *
1816 * Compiles a delete query string and returns the sql
1817 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001818 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001819 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001820 * @return string
1821 */
1822 public function get_compiled_delete($table = '', $reset = TRUE)
1823 {
1824 $this->return_delete_sql = TRUE;
1825 $sql = $this->delete($table, '', NULL, $reset);
1826 $this->return_delete_sql = FALSE;
1827 return $sql;
1828 }
WanWizard7219c072011-12-28 14:09:05 +01001829
Derek Allard2067d1a2008-11-13 22:59:24 +00001830 // --------------------------------------------------------------------
1831
1832 /**
1833 * Delete
1834 *
1835 * Compiles a delete string and runs the query
1836 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001837 * @param mixed the table(s) to delete from. String or array
1838 * @param mixed the where clause
1839 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001840 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001841 * @return object
1842 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001843 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001844 {
1845 // Combine any cached components with the current statements
1846 $this->_merge_cache();
1847
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001848 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001849 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001850 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001851 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001852 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001853 }
1854
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001855 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001856 }
1857 elseif (is_array($table))
1858 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001859 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001860 {
1861 $this->delete($single_table, $where, $limit, FALSE);
1862 }
1863
1864 $this->_reset_write();
1865 return;
1866 }
1867 else
1868 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001869 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001870 }
1871
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001872 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001873 {
1874 $this->where($where);
1875 }
1876
Andrey Andreeve4c30192012-06-04 15:08:24 +03001877 if ($limit != NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001878 {
1879 $this->limit($limit);
1880 }
1881
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001882 if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001883 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001884 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001885 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001886
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001887 $sql = $this->_delete($table, $this->qb_where, $this->qb_like, $this->qb_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001888 if ($reset_data)
1889 {
1890 $this->_reset_write();
1891 }
WanWizard7219c072011-12-28 14:09:05 +01001892
Andrey Andreev24276a32012-01-08 02:44:38 +02001893 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001894 }
WanWizard7219c072011-12-28 14:09:05 +01001895
Derek Allard2067d1a2008-11-13 22:59:24 +00001896 // --------------------------------------------------------------------
1897
1898 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001899 * Delete statement
1900 *
1901 * Generates a platform-specific delete string from the supplied data
1902 *
1903 * @param string the table name
1904 * @param array the where clause
1905 * @param array the like clause
1906 * @param string the limit clause
1907 * @return string
1908 */
1909 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
1910 {
1911 $conditions = array();
1912
1913 empty($where) OR $conditions[] = implode(' ', $where);
1914 empty($like) OR $conditions[] = implode(' ', $like);
1915
1916 return 'DELETE FROM '.$table
1917 .(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '')
1918 .($limit ? ' LIMIT '.$limit : '');
1919 }
1920
1921 // --------------------------------------------------------------------
1922
1923 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001924 * DB Prefix
1925 *
1926 * Prepends a database prefix if one exists in configuration
1927 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001928 * @param string the table
1929 * @return string
1930 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001931 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001932 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001933 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001934 {
1935 $this->display_error('db_table_name_required');
1936 }
1937
1938 return $this->dbprefix.$table;
1939 }
1940
1941 // --------------------------------------------------------------------
1942
1943 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001944 * Set DB Prefix
1945 *
1946 * Set's the DB Prefix to something new without needing to reconnect
1947 *
1948 * @param string the prefix
1949 * @return string
1950 */
1951 public function set_dbprefix($prefix = '')
1952 {
1953 return $this->dbprefix = $prefix;
1954 }
1955
1956 // --------------------------------------------------------------------
1957
1958 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001959 * Track Aliases
1960 *
1961 * Used to track SQL statements written with aliased tables.
1962 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001963 * @param string The table to inspect
1964 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001965 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001966 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001967 {
1968 if (is_array($table))
1969 {
1970 foreach ($table as $t)
1971 {
1972 $this->_track_aliases($t);
1973 }
1974 return;
1975 }
Barry Mienydd671972010-10-04 16:33:58 +02001976
Derek Jones37f4b9c2011-07-01 17:56:50 -05001977 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001978 // the string into discreet statements
1979 if (strpos($table, ',') !== FALSE)
1980 {
1981 return $this->_track_aliases(explode(',', $table));
1982 }
Barry Mienydd671972010-10-04 16:33:58 +02001983
Derek Allard2067d1a2008-11-13 22:59:24 +00001984 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001985 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001986 {
1987 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03001988 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001989
Derek Allard2067d1a2008-11-13 22:59:24 +00001990 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001991 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001992
Derek Allard2067d1a2008-11-13 22:59:24 +00001993 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001994 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001995 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001996 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001997 }
1998 }
1999 }
WanWizard7219c072011-12-28 14:09:05 +01002000
Derek Allard2067d1a2008-11-13 22:59:24 +00002001 // --------------------------------------------------------------------
2002
2003 /**
2004 * Compile the SELECT statement
2005 *
2006 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05002007 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00002008 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002009 * @return string
2010 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002011 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002012 {
2013 // Combine any cached components with the current statements
2014 $this->_merge_cache();
2015
Derek Allard2067d1a2008-11-13 22:59:24 +00002016 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002017 if ($select_override !== FALSE)
2018 {
2019 $sql = $select_override;
2020 }
2021 else
2022 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002023 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002024
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002025 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002026 {
Barry Mienydd671972010-10-04 16:33:58 +02002027 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002028 }
2029 else
Barry Mienydd671972010-10-04 16:33:58 +02002030 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002031 // Cycle through the "select" portion of the query and prep each column name.
2032 // The reason we protect identifiers here rather then in the select() function
2033 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002034 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002035 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002036 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002037 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002038 }
Barry Mienydd671972010-10-04 16:33:58 +02002039
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002040 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002041 }
2042 }
2043
Derek Allard2067d1a2008-11-13 22:59:24 +00002044 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002045 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002046 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002047 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002048 }
2049
Derek Allard2067d1a2008-11-13 22:59:24 +00002050 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002051 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002052 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002053 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002054 }
2055
Derek Allard2067d1a2008-11-13 22:59:24 +00002056 // Write the "WHERE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002057 if (count($this->qb_where) > 0 OR count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002058 {
Greg Akere156c6e2011-04-20 16:03:04 -05002059 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00002060 }
2061
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002062 $sql .= implode("\n", $this->qb_where);
Derek Allard2067d1a2008-11-13 22:59:24 +00002063
Derek Allard2067d1a2008-11-13 22:59:24 +00002064 // Write the "LIKE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002065 if (count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002066 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002067 if (count($this->qb_where) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002068 {
2069 $sql .= "\nAND ";
2070 }
2071
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002072 $sql .= implode("\n", $this->qb_like);
Derek Allard2067d1a2008-11-13 22:59:24 +00002073 }
2074
Derek Allard2067d1a2008-11-13 22:59:24 +00002075 // Write the "GROUP BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002076 if (count($this->qb_groupby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002077 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002078 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002079 }
2080
Derek Allard2067d1a2008-11-13 22:59:24 +00002081 // Write the "HAVING" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002082 if (count($this->qb_having) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002083 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002084 $sql .= "\nHAVING ".implode("\n", $this->qb_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00002085 }
2086
Derek Allard2067d1a2008-11-13 22:59:24 +00002087 // Write the "ORDER BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002088 if (count($this->qb_orderby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002089 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002090 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
2091 if ($this->qb_order !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002092 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002093 $sql .= ($this->qb_order === 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02002094 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002095 }
2096
Derek Allard2067d1a2008-11-13 22:59:24 +00002097 // Write the "LIMIT" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002098 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002099 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002100 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00002101 }
2102
2103 return $sql;
2104 }
2105
2106 // --------------------------------------------------------------------
2107
2108 /**
2109 * Object to Array
2110 *
2111 * Takes an object as input and converts the class variables to array key/vals
2112 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002113 * @param object
2114 * @return array
2115 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002116 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002117 {
2118 if ( ! is_object($object))
2119 {
2120 return $object;
2121 }
Barry Mienydd671972010-10-04 16:33:58 +02002122
Derek Allard2067d1a2008-11-13 22:59:24 +00002123 $array = array();
2124 foreach (get_object_vars($object) as $key => $val)
2125 {
2126 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002127 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002128 {
2129 $array[$key] = $val;
2130 }
2131 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002132
2133 return $array;
2134 }
Barry Mienydd671972010-10-04 16:33:58 +02002135
Derek Jonesd10e8962010-03-02 17:10:36 -06002136 // --------------------------------------------------------------------
2137
2138 /**
2139 * Object to Array
2140 *
2141 * Takes an object as input and converts the class variables to array key/vals
2142 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002143 * @param object
2144 * @return array
2145 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002146 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002147 {
2148 if ( ! is_object($object))
2149 {
2150 return $object;
2151 }
Barry Mienydd671972010-10-04 16:33:58 +02002152
Derek Jonesd10e8962010-03-02 17:10:36 -06002153 $array = array();
2154 $out = get_object_vars($object);
2155 $fields = array_keys($out);
2156
2157 foreach ($fields as $val)
2158 {
2159 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002160 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002161 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002162 $i = 0;
2163 foreach ($out[$val] as $data)
2164 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002165 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002166 }
2167 }
2168 }
2169
Derek Allard2067d1a2008-11-13 22:59:24 +00002170 return $array;
2171 }
Barry Mienydd671972010-10-04 16:33:58 +02002172
Derek Allard2067d1a2008-11-13 22:59:24 +00002173 // --------------------------------------------------------------------
2174
2175 /**
2176 * Start Cache
2177 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002178 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002179 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002180 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002181 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002182 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002183 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002184 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002185 }
2186
2187 // --------------------------------------------------------------------
2188
2189 /**
2190 * Stop Cache
2191 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002192 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002193 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002194 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002195 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002196 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002197 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002198 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002199 }
2200
2201 // --------------------------------------------------------------------
2202
2203 /**
2204 * Flush Cache
2205 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002206 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002207 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002208 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002209 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002210 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002211 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002212 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002213 'qb_cache_select' => array(),
2214 'qb_cache_from' => array(),
2215 'qb_cache_join' => array(),
2216 'qb_cache_where' => array(),
2217 'qb_cache_like' => array(),
2218 'qb_cache_groupby' => array(),
2219 'qb_cache_having' => array(),
2220 'qb_cache_orderby' => array(),
2221 'qb_cache_set' => array(),
2222 'qb_cache_exists' => array(),
2223 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002224 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002225 }
2226
2227 // --------------------------------------------------------------------
2228
2229 /**
2230 * Merge Cache
2231 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002232 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002233 * locally called ones.
2234 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002235 * @return void
2236 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002237 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002238 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002239 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002240 {
2241 return;
2242 }
2243
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002244 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002245 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002246 $qb_variable = 'qb_'.$val;
2247 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002248
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002249 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002250 {
2251 continue;
2252 }
2253
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002254 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002255 }
2256
2257 // If we are "protecting identifiers" we need to examine the "from"
2258 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002259 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002260 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002261 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002262 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002263
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002264 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002265 }
WanWizard7219c072011-12-28 14:09:05 +01002266
Kyle Farris0c147b32011-08-26 02:29:31 -04002267 // --------------------------------------------------------------------
2268
2269 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002270 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002271 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002272 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002273 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002274 * @return void
2275 */
2276 public function reset_query()
2277 {
2278 $this->_reset_select();
2279 $this->_reset_write();
2280 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002281
2282 // --------------------------------------------------------------------
2283
2284 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002285 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002286 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002287 * @param array An array of fields to reset
2288 * @return void
2289 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002290 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002291 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002292 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002293 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002294 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002295 {
2296 $this->$item = $default_value;
2297 }
2298 }
2299 }
2300
2301 // --------------------------------------------------------------------
2302
2303 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002304 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002305 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002306 * @return void
2307 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002308 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002309 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002310 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002311 'qb_select' => array(),
2312 'qb_from' => array(),
2313 'qb_join' => array(),
2314 'qb_where' => array(),
2315 'qb_like' => array(),
2316 'qb_groupby' => array(),
2317 'qb_having' => array(),
2318 'qb_orderby' => array(),
2319 'qb_wherein' => array(),
2320 'qb_aliased_tables' => array(),
2321 'qb_no_escape' => array(),
2322 'qb_distinct' => FALSE,
2323 'qb_limit' => FALSE,
2324 'qb_offset' => FALSE,
2325 'qb_order' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002326 )
2327 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002328 }
Barry Mienydd671972010-10-04 16:33:58 +02002329
Derek Allard2067d1a2008-11-13 22:59:24 +00002330 // --------------------------------------------------------------------
2331
2332 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002333 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002334 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002335 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002336 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002337 * @return void
2338 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002339 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002340 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002341 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002342 'qb_set' => array(),
2343 'qb_from' => array(),
2344 'qb_where' => array(),
2345 'qb_like' => array(),
2346 'qb_orderby' => array(),
2347 'qb_keys' => array(),
2348 'qb_limit' => FALSE,
2349 'qb_order' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002350 )
2351 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002352 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002353
Derek Allard2067d1a2008-11-13 22:59:24 +00002354}
2355
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002356/* End of file DB_query_builder.php */
Andrey Andreeve4c30192012-06-04 15:08:24 +03002357/* Location: ./system/database/DB_query_builder.php */