blob: 7a54fce487262c47bd10635a2a84dea42bd56147 [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
Andrey Andreev9c14f652012-06-10 14:35:07 +0300429 $k = $this->_has_operator($k)
430 ? $this->protect_identifiers(substr($k, 0, strrpos(rtrim($k), ' ')), FALSE, $escape).strrchr(rtrim($k), ' ')
431 : $this->protect_identifiers($k, FALSE, $escape);
432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 if (is_null($v) && ! $this->_has_operator($k))
434 {
435 // value appears not to have been set, assign the test to IS NULL
436 $k .= ' IS NULL';
437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 if ( ! is_null($v))
440 {
441 if ($escape === TRUE)
442 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 $v = ' '.$this->escape($v);
444 }
WanWizard7219c072011-12-28 14:09:05 +0100445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 if ( ! $this->_has_operator($k))
447 {
Greg Akere156c6e2011-04-20 16:03:04 -0500448 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
450 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000451
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000452 $this->qb_where[] = $prefix.$k.$v;
453 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000455 $this->qb_cache_where[] = $prefix.$k.$v;
456 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 return $this;
462 }
463
464 // --------------------------------------------------------------------
465
466 /**
467 * Where_in
468 *
469 * Generates a WHERE field IN ('item', 'item') SQL query joined with
470 * AND if appropriate
471 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 * @param string The field to search
473 * @param array The values searched on
474 * @return object
475 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600476 public function where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
478 return $this->_where_in($key, $values);
479 }
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 // --------------------------------------------------------------------
482
483 /**
484 * Where_in_or
485 *
486 * Generates a WHERE field IN ('item', 'item') SQL query joined with
487 * OR if appropriate
488 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 * @param string The field to search
490 * @param array The values searched on
491 * @return object
492 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600493 public function or_where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 {
495 return $this->_where_in($key, $values, FALSE, 'OR ');
496 }
497
498 // --------------------------------------------------------------------
499
500 /**
501 * Where_not_in
502 *
503 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
504 * with AND if appropriate
505 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 * @param string The field to search
507 * @param array The values searched on
508 * @return object
509 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600510 public function where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 {
512 return $this->_where_in($key, $values, TRUE);
513 }
Barry Mienydd671972010-10-04 16:33:58 +0200514
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 // --------------------------------------------------------------------
516
517 /**
518 * Where_not_in_or
519 *
520 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
521 * with OR if appropriate
522 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 * @param string The field to search
524 * @param array The values searched on
525 * @return object
526 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600527 public function or_where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 {
529 return $this->_where_in($key, $values, TRUE, 'OR ');
530 }
531
532 // --------------------------------------------------------------------
533
534 /**
535 * Where_in
536 *
537 * Called by where_in, where_in_or, where_not_in, where_not_in_or
538 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 * @param string The field to search
540 * @param array The values searched on
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300541 * @param bool If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200542 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 * @return object
544 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600545 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 {
547 if ($key === NULL OR $values === NULL)
548 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300549 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 }
Barry Mienydd671972010-10-04 16:33:58 +0200551
WanWizard7219c072011-12-28 14:09:05 +0100552 $type = $this->_group_get_type($type);
553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 if ( ! is_array($values))
555 {
556 $values = array($values);
557 }
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 $not = ($not) ? ' NOT' : '';
560
561 foreach ($values as $value)
562 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000563 $this->qb_wherein[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 }
565
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000566 $prefix = (count($this->qb_where) === 0) ? '' : $type;
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000567 $this->qb_where[] = $where_in = $prefix.$this->protect_identifiers($key).$not.' IN ('.implode(', ', $this->qb_wherein).') ';
Barry Mienydd671972010-10-04 16:33:58 +0200568
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000569 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000571 $this->qb_cache_where[] = $where_in;
572 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 }
574
575 // reset the array for multiple calls
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000576 $this->qb_wherein = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 return $this;
578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 // --------------------------------------------------------------------
581
582 /**
583 * Like
584 *
585 * Generates a %LIKE% portion of the query. Separates
586 * multiple calls with AND
587 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 * @param mixed
589 * @param mixed
590 * @return object
591 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600592 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 {
594 return $this->_like($field, $match, 'AND ', $side);
595 }
596
597 // --------------------------------------------------------------------
598
599 /**
600 * Not Like
601 *
602 * Generates a NOT LIKE portion of the query. Separates
603 * multiple calls with AND
604 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 * @param mixed
606 * @param mixed
607 * @return object
608 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600609 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 {
611 return $this->_like($field, $match, 'AND ', $side, 'NOT');
612 }
Barry Mienydd671972010-10-04 16:33:58 +0200613
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 // --------------------------------------------------------------------
615
616 /**
617 * OR Like
618 *
619 * Generates a %LIKE% portion of the query. Separates
620 * multiple calls with OR
621 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 * @param mixed
623 * @param mixed
624 * @return object
625 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600626 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 {
628 return $this->_like($field, $match, 'OR ', $side);
629 }
630
631 // --------------------------------------------------------------------
632
633 /**
634 * OR Not Like
635 *
636 * Generates a NOT LIKE portion of the query. Separates
637 * multiple calls with OR
638 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 * @param mixed
640 * @param mixed
641 * @return object
642 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600643 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 {
645 return $this->_like($field, $match, 'OR ', $side, 'NOT');
646 }
Barry Mienydd671972010-10-04 16:33:58 +0200647
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 // --------------------------------------------------------------------
649
650 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 * Like
652 *
653 * Called by like() or orlike()
654 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 * @param mixed
656 * @param mixed
657 * @param string
658 * @return object
659 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600660 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 {
WanWizard7219c072011-12-28 14:09:05 +0100662 $type = $this->_group_get_type($type);
663
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 if ( ! is_array($field))
665 {
666 $field = array($field => $match);
667 }
Barry Mienydd671972010-10-04 16:33:58 +0200668
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 foreach ($field as $k => $v)
670 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000671 $k = $this->protect_identifiers($k);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000672 $prefix = (count($this->qb_like) === 0) ? '' : $type;
Derek Jonese4ed5832009-02-20 21:44:59 +0000673 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300674
Andrey Andreev24276a32012-01-08 02:44:38 +0200675 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400676 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100677 $like_statement = "{$prefix} $k $not LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400678 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200679 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100681 $like_statement = "{$prefix} $k $not LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200683 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100685 $like_statement = "{$prefix} $k $not LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 }
687 else
688 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100689 $like_statement = "{$prefix} $k $not LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600691
Derek Jonese4ed5832009-02-20 21:44:59 +0000692 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100693 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000694 {
Greg Aker0d424892010-01-26 02:14:44 +0000695 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000696 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600697
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000698 $this->qb_like[] = $like_statement;
699 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000701 $this->qb_cache_like[] = $like_statement;
702 $this->qb_cache_exists[] = 'like';
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 }
Barry Mienydd671972010-10-04 16:33:58 +0200704
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 return $this;
708 }
Barry Mienydd671972010-10-04 16:33:58 +0200709
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 // --------------------------------------------------------------------
711
712 /**
WanWizard7219c072011-12-28 14:09:05 +0100713 * Starts a query group.
714 *
715 * @param string (Internal use only)
716 * @param string (Internal use only)
717 * @return object
718 */
719 public function group_start($not = '', $type = 'AND ')
720 {
721 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100722
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000723 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100724 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000725 $this->qb_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (';
WanWizard7219c072011-12-28 14:09:05 +0100726
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000727 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100728 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000729 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100730 }
731
732 return $this;
733 }
734
735 // --------------------------------------------------------------------
736
737 /**
738 * Starts a query group, but ORs the group
739 *
740 * @return object
741 */
742 public function or_group_start()
743 {
744 return $this->group_start('', 'OR ');
745 }
746
747 // --------------------------------------------------------------------
748
749 /**
750 * Starts a query group, but NOTs the group
751 *
752 * @return object
753 */
754 public function not_group_start()
755 {
756 return $this->group_start('NOT ', 'AND ');
757 }
758
759 // --------------------------------------------------------------------
760
761 /**
762 * Starts a query group, but OR NOTs the group
763 *
764 * @return object
765 */
766 public function or_not_group_start()
767 {
768 return $this->group_start('NOT ', 'OR ');
769 }
770
771 // --------------------------------------------------------------------
772
773 /**
774 * Ends a query group
775 *
776 * @return object
777 */
778 public function group_end()
779 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000780 $this->qb_where_group_started = FALSE;
781 $this->qb_where[] = $value = str_repeat(' ', $this->qb_where_group_count--) . ')';
WanWizard7219c072011-12-28 14:09:05 +0100782
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000783 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100784 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000785 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100786 }
787
WanWizard7219c072011-12-28 14:09:05 +0100788 return $this;
789 }
790
791 // --------------------------------------------------------------------
792
793 /**
794 * Group_get_type
795 *
796 * Called by group_start(), _like(), _where() and _where_in()
797 *
798 * @param string
799 * @return string
800 */
801 protected function _group_get_type($type)
802 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000803 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100804 {
805 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000806 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100807 }
808
809 return $type;
810 }
811
812 // --------------------------------------------------------------------
813
814 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 * GROUP BY
816 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 * @param string
818 * @return object
819 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600820 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 {
822 if (is_string($by))
823 {
824 $by = explode(',', $by);
825 }
Barry Mienydd671972010-10-04 16:33:58 +0200826
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 foreach ($by as $val)
828 {
829 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200830
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100831 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000833 $this->qb_groupby[] = $val = $this->protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200834
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000835 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000837 $this->qb_cache_groupby[] = $val;
838 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 }
840 }
841 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200842
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 return $this;
844 }
845
846 // --------------------------------------------------------------------
847
848 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 * Sets the HAVING value
850 *
851 * Separates multiple calls with AND
852 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 * @param string
854 * @param string
855 * @return object
856 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600857 public function having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 {
859 return $this->_having($key, $value, 'AND ', $escape);
860 }
Barry Mienydd671972010-10-04 16:33:58 +0200861
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 // --------------------------------------------------------------------
863
864 /**
865 * Sets the OR HAVING value
866 *
867 * Separates multiple calls with OR
868 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 * @param string
870 * @param string
871 * @return object
872 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600873 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 {
875 return $this->_having($key, $value, 'OR ', $escape);
876 }
Barry Mienydd671972010-10-04 16:33:58 +0200877
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 // --------------------------------------------------------------------
879
880 /**
881 * Sets the HAVING values
882 *
883 * Called by having() or or_having()
884 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 * @param string
886 * @param string
887 * @return object
888 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600889 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 {
891 if ( ! is_array($key))
892 {
893 $key = array($key => $value);
894 }
Barry Mienydd671972010-10-04 16:33:58 +0200895
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 foreach ($key as $k => $v)
897 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000898 $prefix = (count($this->qb_having) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000899
900 if ($escape === TRUE)
901 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200902 $k = $this->protect_identifiers($k);
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 }
904
905 if ( ! $this->_has_operator($k))
906 {
907 $k .= ' = ';
908 }
909
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100910 if ($v !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400912 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 }
Barry Mienydd671972010-10-04 16:33:58 +0200914
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000915 $this->qb_having[] = $prefix.$k.$v;
916 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000918 $this->qb_cache_having[] = $prefix.$k.$v;
919 $this->qb_cache_exists[] = 'having';
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 }
921 }
Barry Mienydd671972010-10-04 16:33:58 +0200922
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 return $this;
924 }
Barry Mienydd671972010-10-04 16:33:58 +0200925
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 // --------------------------------------------------------------------
927
928 /**
929 * Sets the ORDER BY value
930 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 * @param string
932 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100933 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 * @return object
935 */
pporlan2c685fb2011-12-22 12:15:25 +0100936 public function order_by($orderby, $direction = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200938 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 {
940 $orderby = ''; // Random results want or don't need a field name
941 $direction = $this->_random_keyword;
942 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100943 elseif (trim($direction) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 {
945 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
946 }
Barry Mienydd671972010-10-04 16:33:58 +0200947
948
Andrey Andreev24276a32012-01-08 02:44:38 +0200949 if ((strpos($orderby, ',') !== FALSE) && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 {
951 $temp = array();
952 foreach (explode(',', $orderby) as $part)
953 {
954 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000955 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200957 $part = $this->protect_identifiers(trim($part));
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 }
Barry Mienydd671972010-10-04 16:33:58 +0200959
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 $temp[] = $part;
961 }
Barry Mienydd671972010-10-04 16:33:58 +0200962
963 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100965 elseif ($direction !== $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 {
pporlan2c685fb2011-12-22 12:15:25 +0100967 if ($escape === TRUE)
968 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200969 $orderby = $this->protect_identifiers($orderby);
pporlan2c685fb2011-12-22 12:15:25 +0100970 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 }
Barry Mienydd671972010-10-04 16:33:58 +0200972
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000973 $this->qb_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200974
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000975 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000977 $this->qb_cache_orderby[] = $orderby_statement;
978 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 }
980
981 return $this;
982 }
Barry Mienydd671972010-10-04 16:33:58 +0200983
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 // --------------------------------------------------------------------
985
986 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 * Sets the LIMIT value
988 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300989 * @param int the limit value
990 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 * @return object
992 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200993 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000995 $this->qb_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000996
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200997 if ( ! is_null($offset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000999 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 }
Barry Mienydd671972010-10-04 16:33:58 +02001001
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 return $this;
1003 }
Barry Mienydd671972010-10-04 16:33:58 +02001004
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 // --------------------------------------------------------------------
1006
1007 /**
1008 * Sets the OFFSET value
1009 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001010 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 * @return object
1012 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001013 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001015 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 return $this;
1017 }
Barry Mienydd671972010-10-04 16:33:58 +02001018
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 // --------------------------------------------------------------------
1020
1021 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001022 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 * @param mixed
1025 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001026 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 * @return object
1028 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001029 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 {
1031 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001032
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 if ( ! is_array($key))
1034 {
1035 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001036 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001037
1038 foreach ($key as $k => $v)
1039 {
1040 if ($escape === FALSE)
1041 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001042 $this->qb_set[$this->protect_identifiers($k)] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 }
1044 else
1045 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001046 $this->qb_set[$this->protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 }
1048 }
Barry Mienydd671972010-10-04 16:33:58 +02001049
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 return $this;
1051 }
WanWizard7219c072011-12-28 14:09:05 +01001052
Kyle Farris0c147b32011-08-26 02:29:31 -04001053 // --------------------------------------------------------------------
1054
1055 /**
1056 * Get SELECT query string
1057 *
1058 * Compiles a SELECT query string and returns the sql.
1059 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001060 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001061 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001062 * @return string
1063 */
WanWizard7219c072011-12-28 14:09:05 +01001064 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001065 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001066 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001067 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001068 $this->_track_aliases($table);
1069 $this->from($table);
1070 }
WanWizard7219c072011-12-28 14:09:05 +01001071
Kyle Farris0c147b32011-08-26 02:29:31 -04001072 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001073
Kyle Farris0c147b32011-08-26 02:29:31 -04001074 if ($reset === TRUE)
1075 {
1076 $this->_reset_select();
1077 }
WanWizard7219c072011-12-28 14:09:05 +01001078
Kyle Farris0c147b32011-08-26 02:29:31 -04001079 return $select;
1080 }
WanWizard7219c072011-12-28 14:09:05 +01001081
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 // --------------------------------------------------------------------
1083
1084 /**
1085 * Get
1086 *
1087 * Compiles the select statement based on the other functions called
1088 * and runs the query
1089 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 * @param string the table
1091 * @param string the limit clause
1092 * @param string the offset clause
1093 * @return object
1094 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001095 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001097 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 {
1099 $this->_track_aliases($table);
1100 $this->from($table);
1101 }
Barry Mienydd671972010-10-04 16:33:58 +02001102
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 if ( ! is_null($limit))
1104 {
1105 $this->limit($limit, $offset);
1106 }
Barry Mienydd671972010-10-04 16:33:58 +02001107
Andrey Andreev24276a32012-01-08 02:44:38 +02001108 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 $this->_reset_select();
1110 return $result;
1111 }
1112
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001113 // --------------------------------------------------------------------
1114
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 /**
1116 * "Count All Results" query
1117 *
Barry Mienydd671972010-10-04 16:33:58 +02001118 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001119 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 * @param string
1122 * @return string
1123 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001124 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001126 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 {
1128 $this->_track_aliases($table);
1129 $this->from($table);
1130 }
Barry Mienydd671972010-10-04 16:33:58 +02001131
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001132 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001134
Purwandi1d160e72012-01-09 16:33:28 +07001135 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001137 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 }
1139
Purwandi1d160e72012-01-09 16:33:28 +07001140 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001141 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001143
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 // --------------------------------------------------------------------
1145
1146 /**
1147 * Get_Where
1148 *
1149 * Allows the where clause, limit and offset to be added directly
1150 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 * @param string the where clause
1152 * @param string the limit clause
1153 * @param string the offset clause
1154 * @return object
1155 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001156 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001158 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 {
1160 $this->from($table);
1161 }
1162
1163 if ( ! is_null($where))
1164 {
1165 $this->where($where);
1166 }
Barry Mienydd671972010-10-04 16:33:58 +02001167
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 if ( ! is_null($limit))
1169 {
1170 $this->limit($limit, $offset);
1171 }
Barry Mienydd671972010-10-04 16:33:58 +02001172
Andrey Andreev24276a32012-01-08 02:44:38 +02001173 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 $this->_reset_select();
1175 return $result;
1176 }
1177
1178 // --------------------------------------------------------------------
1179
1180 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001181 * Insert_Batch
1182 *
1183 * Compiles batch insert strings and runs the queries
1184 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001185 * @param string the table to retrieve the results from
1186 * @param array an associative array of insert values
1187 * @return object
1188 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001189 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001190 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001191 if ( ! is_null($set))
1192 {
1193 $this->set_insert_batch($set);
1194 }
Barry Mienydd671972010-10-04 16:33:58 +02001195
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001196 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001197 {
1198 if ($this->db_debug)
1199 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001200 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001201 return $this->display_error('db_must_use_set');
1202 }
1203 return FALSE;
1204 }
1205
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001206 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001207 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001208 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001209 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001210 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001211 }
Barry Mienydd671972010-10-04 16:33:58 +02001212
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001213 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001214 }
1215
1216 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001217 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001218 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001219 $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 -06001220 }
Barry Mienydd671972010-10-04 16:33:58 +02001221
Derek Jonesd10e8962010-03-02 17:10:36 -06001222 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001223 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001224 }
1225
1226 // --------------------------------------------------------------------
1227
1228 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001229 * Insert_batch statement
1230 *
1231 * Generates a platform-specific insert string from the supplied data.
1232 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001233 * @param string the table name
1234 * @param array the insert keys
1235 * @param array the insert values
1236 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001237 */
1238 protected function _insert_batch($table, $keys, $values)
1239 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001240 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001241 }
1242
1243 // --------------------------------------------------------------------
1244
1245 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001246 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001247 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001248 * @param mixed
1249 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001250 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001251 * @return object
1252 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001253 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001254 {
1255 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001256
Derek Jonesd10e8962010-03-02 17:10:36 -06001257 if ( ! is_array($key))
1258 {
1259 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001260 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001261
Iban Eguia3c0a4522012-04-15 13:30:44 +02001262 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001263 sort($keys);
1264
1265 foreach ($key as $row)
1266 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001267 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001268 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1269 {
1270 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001271 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001272 return;
1273 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001274
Barry Mienydd671972010-10-04 16:33:58 +02001275 ksort($row); // puts $row in the same order as our keys
1276
Derek Jonesd10e8962010-03-02 17:10:36 -06001277 if ($escape === FALSE)
1278 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001279 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001280 }
1281 else
1282 {
1283 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001284 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001285 {
Barry Mienydd671972010-10-04 16:33:58 +02001286 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001287 }
1288
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001289 $this->qb_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001290 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001291 }
1292
1293 foreach ($keys as $k)
1294 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001295 $this->qb_keys[] = $this->protect_identifiers($k);
Derek Jonesd10e8962010-03-02 17:10:36 -06001296 }
Barry Mienydd671972010-10-04 16:33:58 +02001297
Derek Jonesd10e8962010-03-02 17:10:36 -06001298 return $this;
1299 }
WanWizard7219c072011-12-28 14:09:05 +01001300
Kyle Farris0c147b32011-08-26 02:29:31 -04001301 // --------------------------------------------------------------------
1302
1303 /**
1304 * Get INSERT query string
1305 *
1306 * Compiles an insert query and returns the sql
1307 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001308 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001309 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001310 * @return string
1311 */
1312 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001313 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001314 if ($this->_validate_insert($table) === FALSE)
1315 {
1316 return FALSE;
1317 }
WanWizard7219c072011-12-28 14:09:05 +01001318
Kyle Farris76116012011-08-31 11:17:48 -04001319 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001320 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001321 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001322 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001323 array_keys($this->qb_set),
1324 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001325 );
WanWizard7219c072011-12-28 14:09:05 +01001326
Kyle Farris0c147b32011-08-26 02:29:31 -04001327 if ($reset === TRUE)
1328 {
1329 $this->_reset_write();
1330 }
WanWizard7219c072011-12-28 14:09:05 +01001331
Kyle Farris0c147b32011-08-26 02:29:31 -04001332 return $sql;
1333 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001334
Derek Allard2067d1a2008-11-13 22:59:24 +00001335 // --------------------------------------------------------------------
1336
1337 /**
1338 * Insert
1339 *
1340 * Compiles an insert string and runs the query
1341 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001342 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 * @param array an associative array of insert values
1344 * @return object
1345 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001346 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001347 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 if ( ! is_null($set))
1349 {
1350 $this->set($set);
1351 }
WanWizard7219c072011-12-28 14:09:05 +01001352
Kyle Farris0c147b32011-08-26 02:29:31 -04001353 if ($this->_validate_insert($table) === FALSE)
1354 {
1355 return FALSE;
1356 }
WanWizard7219c072011-12-28 14:09:05 +01001357
Kyle Farris76116012011-08-31 11:17:48 -04001358 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001359 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001360 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001361 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001362 array_keys($this->qb_set),
1363 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001364 );
Barry Mienydd671972010-10-04 16:33:58 +02001365
Kyle Farris0c147b32011-08-26 02:29:31 -04001366 $this->_reset_write();
1367 return $this->query($sql);
1368 }
WanWizard7219c072011-12-28 14:09:05 +01001369
Kyle Farris0c147b32011-08-26 02:29:31 -04001370 // --------------------------------------------------------------------
1371
1372 /**
Andrey Andreev65d537c2012-04-05 14:11:41 +03001373 * Insert statement
1374 *
1375 * Generates a platform-specific insert string from the supplied data
1376 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001377 * @param string the table name
1378 * @param array the insert keys
1379 * @param array the insert values
1380 * @return string
Andrey Andreev65d537c2012-04-05 14:11:41 +03001381 */
1382 protected function _insert($table, $keys, $values)
1383 {
1384 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1385 }
1386
1387 // --------------------------------------------------------------------
1388
1389 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001390 * Validate Insert
1391 *
1392 * This method is used by both insert() and get_compiled_insert() to
1393 * validate that the there data is actually being set and that table
1394 * has been chosen to be inserted into.
1395 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001396 * @param string the table to insert data into
1397 * @return string
1398 */
WanWizard7219c072011-12-28 14:09:05 +01001399 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001400 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001401 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001402 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001403 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001404 }
1405
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001406 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001407 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001408 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001409 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001410 elseif ( ! isset($this->qb_from[0]))
1411 {
1412 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1413 }
WanWizard7219c072011-12-28 14:09:05 +01001414
Kyle Farris0c147b32011-08-26 02:29:31 -04001415 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001416 }
Barry Mienydd671972010-10-04 16:33:58 +02001417
Phil Sturgeon9789f322011-07-15 15:14:05 -06001418 // --------------------------------------------------------------------
1419
1420 /**
1421 * Replace
1422 *
1423 * Compiles an replace into string and runs the query
1424 *
1425 * @param string the table to replace data into
1426 * @param array an associative array of insert values
1427 * @return object
1428 */
1429 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001430 {
1431 if ( ! is_null($set))
1432 {
1433 $this->set($set);
1434 }
Barry Mienydd671972010-10-04 16:33:58 +02001435
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001436 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001437 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001438 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001439 }
1440
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001441 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001442 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001443 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001444 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001445 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001446 }
Barry Mienydd671972010-10-04 16:33:58 +02001447
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001448 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001449 }
1450
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001451 $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 +00001452
Derek Jonesd10e8962010-03-02 17:10:36 -06001453 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001454 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001455 }
WanWizard7219c072011-12-28 14:09:05 +01001456
Kyle Farris0c147b32011-08-26 02:29:31 -04001457 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001458
Kyle Farris0c147b32011-08-26 02:29:31 -04001459 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001460 * Replace statement
1461 *
1462 * Generates a platform-specific replace string from the supplied data
1463 *
1464 * @param string the table name
1465 * @param array the insert keys
1466 * @param array the insert values
1467 * @return string
1468 */
1469 protected function _replace($table, $keys, $values)
1470 {
1471 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1472 }
1473
1474 // --------------------------------------------------------------------
1475
1476 /**
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001477 * From Tables
1478 *
1479 * This public function implicitly groups FROM tables so there is no confusion
1480 * about operator precedence in harmony with SQL standards
1481 *
1482 * @param array
1483 * @return string
1484 */
1485 protected function _from_tables($tables)
1486 {
1487 is_array($tables) OR $tables = array($tables);
1488
1489 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
1490 }
1491
1492 // --------------------------------------------------------------------
1493
1494 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001495 * Get UPDATE query string
1496 *
1497 * Compiles an update query and returns the sql
1498 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001499 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001500 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001501 * @return string
1502 */
1503 public function get_compiled_update($table = '', $reset = TRUE)
1504 {
1505 // Combine any cached components with the current statements
1506 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001507
Kyle Farris0c147b32011-08-26 02:29:31 -04001508 if ($this->_validate_update($table) === FALSE)
1509 {
1510 return FALSE;
1511 }
WanWizard7219c072011-12-28 14:09:05 +01001512
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001513 $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 +01001514
Kyle Farris0c147b32011-08-26 02:29:31 -04001515 if ($reset === TRUE)
1516 {
1517 $this->_reset_write();
1518 }
WanWizard7219c072011-12-28 14:09:05 +01001519
Kyle Farris0c147b32011-08-26 02:29:31 -04001520 return $sql;
1521 }
WanWizard7219c072011-12-28 14:09:05 +01001522
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 // --------------------------------------------------------------------
1524
1525 /**
1526 * Update
1527 *
1528 * Compiles an update string and runs the query
1529 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001530 * @param string the table to retrieve the results from
1531 * @param array an associative array of update values
1532 * @param mixed the where clause
1533 * @return object
1534 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001535 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001536 {
1537 // Combine any cached components with the current statements
1538 $this->_merge_cache();
1539
1540 if ( ! is_null($set))
1541 {
1542 $this->set($set);
1543 }
Barry Mienydd671972010-10-04 16:33:58 +02001544
Kyle Farris0c147b32011-08-26 02:29:31 -04001545 if ($this->_validate_update($table) === FALSE)
1546 {
1547 return FALSE;
1548 }
1549
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001550 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001551 {
1552 $this->where($where);
1553 }
1554
Andrey Andreeve4c30192012-06-04 15:08:24 +03001555 if ($limit != NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001556 {
1557 $this->limit($limit);
1558 }
1559
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001560 $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 +00001561
Kyle Farris0c147b32011-08-26 02:29:31 -04001562 $this->_reset_write();
1563 return $this->query($sql);
1564 }
WanWizard7219c072011-12-28 14:09:05 +01001565
Kyle Farris0c147b32011-08-26 02:29:31 -04001566 // --------------------------------------------------------------------
1567
1568 /**
Andrey Andreev975034d2012-04-05 15:09:55 +03001569 * Update statement
1570 *
1571 * Generates a platform-specific update string from the supplied data
1572 *
1573 * @param string the table name
1574 * @param array the update data
1575 * @param array the where clause
1576 * @param array the orderby clause
1577 * @param array the limit clause
Andrey Andreev00541ae2012-04-09 11:43:10 +03001578 * @param array the like clause
Andrey Andreev975034d2012-04-05 15:09:55 +03001579 * @return string
1580 */
Andrey Andreev00541ae2012-04-09 11:43:10 +03001581 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Andrey Andreev975034d2012-04-05 15:09:55 +03001582 {
1583 foreach ($values as $key => $val)
1584 {
1585 $valstr[] = $key.' = '.$val;
1586 }
1587
Andrey Andreev00541ae2012-04-09 11:43:10 +03001588 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
1589
1590 if ( ! empty($like))
1591 {
1592 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
1593 }
1594
Andrey Andreev975034d2012-04-05 15:09:55 +03001595 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev00541ae2012-04-09 11:43:10 +03001596 .$where
Andrey Andreev975034d2012-04-05 15:09:55 +03001597 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
1598 .($limit ? ' LIMIT '.$limit : '');
1599 }
1600
1601 // --------------------------------------------------------------------
1602
1603 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001604 * Validate Update
1605 *
1606 * This method is used by both update() and get_compiled_update() to
1607 * validate that data is actually being set and that a table has been
1608 * chosen to be update.
1609 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001610 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001611 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001612 */
1613 protected function _validate_update($table = '')
1614 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001615 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001616 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001617 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001618 }
1619
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001620 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001621 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001622 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001623 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001624 elseif ( ! isset($this->qb_from[0]))
1625 {
1626 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1627 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001628
1629 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001630 }
WanWizard7219c072011-12-28 14:09:05 +01001631
Derek Jonesd10e8962010-03-02 17:10:36 -06001632 // --------------------------------------------------------------------
1633
1634 /**
1635 * Update_Batch
1636 *
1637 * Compiles an update string and runs the query
1638 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001639 * @param string the table to retrieve the results from
1640 * @param array an associative array of update values
1641 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001642 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001643 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001644 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001645 {
1646 // Combine any cached components with the current statements
1647 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001648
Derek Jonesd10e8962010-03-02 17:10:36 -06001649 if (is_null($index))
1650 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001651 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001652 }
1653
1654 if ( ! is_null($set))
1655 {
1656 $this->set_update_batch($set, $index);
1657 }
1658
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001659 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001660 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001661 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001662 }
1663
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001664 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001665 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001666 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001667 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001668 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001669 }
Barry Mienydd671972010-10-04 16:33:58 +02001670
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001671 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001672 }
Barry Mienydd671972010-10-04 16:33:58 +02001673
Derek Jonesd10e8962010-03-02 17:10:36 -06001674 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001675 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001676 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001677 $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 -06001678 }
Barry Mienydd671972010-10-04 16:33:58 +02001679
Derek Jonesd10e8962010-03-02 17:10:36 -06001680 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001681 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001682 }
1683
1684 // --------------------------------------------------------------------
1685
1686 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001687 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001688 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001689 * @param array
1690 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001691 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001692 * @return object
1693 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001694 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001695 {
1696 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001697
Derek Jonesd10e8962010-03-02 17:10:36 -06001698 if ( ! is_array($key))
1699 {
1700 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001701 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001702
1703 foreach ($key as $k => $v)
1704 {
1705 $index_set = FALSE;
1706 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001707 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001708 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001709 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001710 {
1711 $index_set = TRUE;
1712 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001713
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001714 $clean[$this->protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001715 }
1716
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001717 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001718 {
1719 return $this->display_error('db_batch_missing_index');
1720 }
1721
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001722 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001723 }
Barry Mienydd671972010-10-04 16:33:58 +02001724
Derek Jonesd10e8962010-03-02 17:10:36 -06001725 return $this;
1726 }
1727
Derek Allard2067d1a2008-11-13 22:59:24 +00001728 // --------------------------------------------------------------------
1729
1730 /**
1731 * Empty Table
1732 *
1733 * Compiles a delete string and runs "DELETE FROM table"
1734 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001735 * @param string the table to empty
1736 * @return object
1737 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001738 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001739 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001740 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001741 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001742 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001743 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001744 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001745 }
1746
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001747 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001748 }
1749 else
1750 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001751 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001752 }
1753
1754 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001755 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001756 return $this->query($sql);
1757 }
1758
1759 // --------------------------------------------------------------------
1760
1761 /**
1762 * Truncate
1763 *
1764 * Compiles a truncate string and runs the query
1765 * If the database does not support the truncate() command
1766 * This function maps to "DELETE FROM table"
1767 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001768 * @param string the table to truncate
1769 * @return object
1770 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001771 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001773 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001774 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001775 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001776 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001777 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001778 }
1779
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001780 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001781 }
1782 else
1783 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001784 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001785 }
1786
1787 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 return $this->query($sql);
1790 }
WanWizard7219c072011-12-28 14:09:05 +01001791
Kyle Farris0c147b32011-08-26 02:29:31 -04001792 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001793
Kyle Farris0c147b32011-08-26 02:29:31 -04001794 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001795 * Truncate statement
1796 *
1797 * Generates a platform-specific truncate string from the supplied data
1798 *
1799 * If the database does not support the truncate() command,
1800 * then this method maps to 'DELETE FROM table'
1801 *
1802 * @param string the table name
1803 * @return string
1804 */
1805 protected function _truncate($table)
1806 {
1807 return 'TRUNCATE '.$table;
1808 }
1809
1810 // --------------------------------------------------------------------
1811
1812 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001813 * Get DELETE query string
1814 *
1815 * Compiles a delete query string and returns the sql
1816 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001817 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001818 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001819 * @return string
1820 */
1821 public function get_compiled_delete($table = '', $reset = TRUE)
1822 {
1823 $this->return_delete_sql = TRUE;
1824 $sql = $this->delete($table, '', NULL, $reset);
1825 $this->return_delete_sql = FALSE;
1826 return $sql;
1827 }
WanWizard7219c072011-12-28 14:09:05 +01001828
Derek Allard2067d1a2008-11-13 22:59:24 +00001829 // --------------------------------------------------------------------
1830
1831 /**
1832 * Delete
1833 *
1834 * Compiles a delete string and runs the query
1835 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001836 * @param mixed the table(s) to delete from. String or array
1837 * @param mixed the where clause
1838 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001839 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001840 * @return object
1841 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001842 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001843 {
1844 // Combine any cached components with the current statements
1845 $this->_merge_cache();
1846
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001847 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001848 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001849 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001850 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001851 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001852 }
1853
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001854 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001855 }
1856 elseif (is_array($table))
1857 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001858 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001859 {
1860 $this->delete($single_table, $where, $limit, FALSE);
1861 }
1862
1863 $this->_reset_write();
1864 return;
1865 }
1866 else
1867 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001868 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001869 }
1870
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001871 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001872 {
1873 $this->where($where);
1874 }
1875
Andrey Andreeve4c30192012-06-04 15:08:24 +03001876 if ($limit != NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001877 {
1878 $this->limit($limit);
1879 }
1880
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001881 if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001882 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001883 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001884 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001885
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001886 $sql = $this->_delete($table, $this->qb_where, $this->qb_like, $this->qb_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001887 if ($reset_data)
1888 {
1889 $this->_reset_write();
1890 }
WanWizard7219c072011-12-28 14:09:05 +01001891
Andrey Andreev24276a32012-01-08 02:44:38 +02001892 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001893 }
WanWizard7219c072011-12-28 14:09:05 +01001894
Derek Allard2067d1a2008-11-13 22:59:24 +00001895 // --------------------------------------------------------------------
1896
1897 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001898 * Delete statement
1899 *
1900 * Generates a platform-specific delete string from the supplied data
1901 *
1902 * @param string the table name
1903 * @param array the where clause
1904 * @param array the like clause
1905 * @param string the limit clause
1906 * @return string
1907 */
1908 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
1909 {
1910 $conditions = array();
1911
1912 empty($where) OR $conditions[] = implode(' ', $where);
1913 empty($like) OR $conditions[] = implode(' ', $like);
1914
1915 return 'DELETE FROM '.$table
1916 .(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '')
1917 .($limit ? ' LIMIT '.$limit : '');
1918 }
1919
1920 // --------------------------------------------------------------------
1921
1922 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001923 * DB Prefix
1924 *
1925 * Prepends a database prefix if one exists in configuration
1926 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001927 * @param string the table
1928 * @return string
1929 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001930 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001931 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001932 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001933 {
1934 $this->display_error('db_table_name_required');
1935 }
1936
1937 return $this->dbprefix.$table;
1938 }
1939
1940 // --------------------------------------------------------------------
1941
1942 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001943 * Set DB Prefix
1944 *
1945 * Set's the DB Prefix to something new without needing to reconnect
1946 *
1947 * @param string the prefix
1948 * @return string
1949 */
1950 public function set_dbprefix($prefix = '')
1951 {
1952 return $this->dbprefix = $prefix;
1953 }
1954
1955 // --------------------------------------------------------------------
1956
1957 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001958 * Track Aliases
1959 *
1960 * Used to track SQL statements written with aliased tables.
1961 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001962 * @param string The table to inspect
1963 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001964 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001965 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001966 {
1967 if (is_array($table))
1968 {
1969 foreach ($table as $t)
1970 {
1971 $this->_track_aliases($t);
1972 }
1973 return;
1974 }
Barry Mienydd671972010-10-04 16:33:58 +02001975
Derek Jones37f4b9c2011-07-01 17:56:50 -05001976 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001977 // the string into discreet statements
1978 if (strpos($table, ',') !== FALSE)
1979 {
1980 return $this->_track_aliases(explode(',', $table));
1981 }
Barry Mienydd671972010-10-04 16:33:58 +02001982
Derek Allard2067d1a2008-11-13 22:59:24 +00001983 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001984 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001985 {
1986 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03001987 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001988
Derek Allard2067d1a2008-11-13 22:59:24 +00001989 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001990 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001991
Derek Allard2067d1a2008-11-13 22:59:24 +00001992 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001993 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001994 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001995 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001996 }
1997 }
1998 }
WanWizard7219c072011-12-28 14:09:05 +01001999
Derek Allard2067d1a2008-11-13 22:59:24 +00002000 // --------------------------------------------------------------------
2001
2002 /**
2003 * Compile the SELECT statement
2004 *
2005 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05002006 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00002007 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002008 * @return string
2009 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002010 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002011 {
2012 // Combine any cached components with the current statements
2013 $this->_merge_cache();
2014
Derek Allard2067d1a2008-11-13 22:59:24 +00002015 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002016 if ($select_override !== FALSE)
2017 {
2018 $sql = $select_override;
2019 }
2020 else
2021 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002022 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002023
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002024 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002025 {
Barry Mienydd671972010-10-04 16:33:58 +02002026 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002027 }
2028 else
Barry Mienydd671972010-10-04 16:33:58 +02002029 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002030 // Cycle through the "select" portion of the query and prep each column name.
2031 // The reason we protect identifiers here rather then in the select() function
2032 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002033 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002034 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002035 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002036 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002037 }
Barry Mienydd671972010-10-04 16:33:58 +02002038
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002039 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002040 }
2041 }
2042
Derek Allard2067d1a2008-11-13 22:59:24 +00002043 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002044 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002045 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002046 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002047 }
2048
Derek Allard2067d1a2008-11-13 22:59:24 +00002049 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002050 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002051 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002052 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 }
2054
Derek Allard2067d1a2008-11-13 22:59:24 +00002055 // Write the "WHERE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002056 if (count($this->qb_where) > 0 OR count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002057 {
Greg Akere156c6e2011-04-20 16:03:04 -05002058 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00002059 }
2060
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002061 $sql .= implode("\n", $this->qb_where);
Derek Allard2067d1a2008-11-13 22:59:24 +00002062
Derek Allard2067d1a2008-11-13 22:59:24 +00002063 // Write the "LIKE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002064 if (count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002065 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002066 if (count($this->qb_where) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002067 {
2068 $sql .= "\nAND ";
2069 }
2070
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002071 $sql .= implode("\n", $this->qb_like);
Derek Allard2067d1a2008-11-13 22:59:24 +00002072 }
2073
Derek Allard2067d1a2008-11-13 22:59:24 +00002074 // Write the "GROUP BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002075 if (count($this->qb_groupby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002076 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002077 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002078 }
2079
Derek Allard2067d1a2008-11-13 22:59:24 +00002080 // Write the "HAVING" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002081 if (count($this->qb_having) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002082 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002083 $sql .= "\nHAVING ".implode("\n", $this->qb_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00002084 }
2085
Derek Allard2067d1a2008-11-13 22:59:24 +00002086 // Write the "ORDER BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002087 if (count($this->qb_orderby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002088 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002089 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
2090 if ($this->qb_order !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002091 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002092 $sql .= ($this->qb_order === 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02002093 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002094 }
2095
Derek Allard2067d1a2008-11-13 22:59:24 +00002096 // Write the "LIMIT" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002097 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002098 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002099 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00002100 }
2101
2102 return $sql;
2103 }
2104
2105 // --------------------------------------------------------------------
2106
2107 /**
2108 * Object to Array
2109 *
2110 * Takes an object as input and converts the class variables to array key/vals
2111 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002112 * @param object
2113 * @return array
2114 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002115 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002116 {
2117 if ( ! is_object($object))
2118 {
2119 return $object;
2120 }
Barry Mienydd671972010-10-04 16:33:58 +02002121
Derek Allard2067d1a2008-11-13 22:59:24 +00002122 $array = array();
2123 foreach (get_object_vars($object) as $key => $val)
2124 {
2125 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002126 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002127 {
2128 $array[$key] = $val;
2129 }
2130 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002131
2132 return $array;
2133 }
Barry Mienydd671972010-10-04 16:33:58 +02002134
Derek Jonesd10e8962010-03-02 17:10:36 -06002135 // --------------------------------------------------------------------
2136
2137 /**
2138 * Object to Array
2139 *
2140 * Takes an object as input and converts the class variables to array key/vals
2141 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002142 * @param object
2143 * @return array
2144 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002145 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002146 {
2147 if ( ! is_object($object))
2148 {
2149 return $object;
2150 }
Barry Mienydd671972010-10-04 16:33:58 +02002151
Derek Jonesd10e8962010-03-02 17:10:36 -06002152 $array = array();
2153 $out = get_object_vars($object);
2154 $fields = array_keys($out);
2155
2156 foreach ($fields as $val)
2157 {
2158 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002159 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002160 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002161 $i = 0;
2162 foreach ($out[$val] as $data)
2163 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002164 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002165 }
2166 }
2167 }
2168
Derek Allard2067d1a2008-11-13 22:59:24 +00002169 return $array;
2170 }
Barry Mienydd671972010-10-04 16:33:58 +02002171
Derek Allard2067d1a2008-11-13 22:59:24 +00002172 // --------------------------------------------------------------------
2173
2174 /**
2175 * Start Cache
2176 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002177 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002178 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002179 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002180 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002181 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002182 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002183 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002184 }
2185
2186 // --------------------------------------------------------------------
2187
2188 /**
2189 * Stop Cache
2190 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002191 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002192 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002193 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002194 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002195 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002196 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002197 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002198 }
2199
2200 // --------------------------------------------------------------------
2201
2202 /**
2203 * Flush Cache
2204 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002205 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002206 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002207 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002208 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002209 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002210 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002211 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002212 'qb_cache_select' => array(),
2213 'qb_cache_from' => array(),
2214 'qb_cache_join' => array(),
2215 'qb_cache_where' => array(),
2216 'qb_cache_like' => array(),
2217 'qb_cache_groupby' => array(),
2218 'qb_cache_having' => array(),
2219 'qb_cache_orderby' => array(),
2220 'qb_cache_set' => array(),
2221 'qb_cache_exists' => array(),
2222 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002223 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002224 }
2225
2226 // --------------------------------------------------------------------
2227
2228 /**
2229 * Merge Cache
2230 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002231 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002232 * locally called ones.
2233 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002234 * @return void
2235 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002236 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002237 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002238 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002239 {
2240 return;
2241 }
2242
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002243 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002244 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002245 $qb_variable = 'qb_'.$val;
2246 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002247
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002248 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002249 {
2250 continue;
2251 }
2252
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002253 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002254 }
2255
2256 // If we are "protecting identifiers" we need to examine the "from"
2257 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002258 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002259 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002260 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002261 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002262
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002263 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002264 }
WanWizard7219c072011-12-28 14:09:05 +01002265
Kyle Farris0c147b32011-08-26 02:29:31 -04002266 // --------------------------------------------------------------------
2267
2268 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002269 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002270 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002271 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002272 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002273 * @return void
2274 */
2275 public function reset_query()
2276 {
2277 $this->_reset_select();
2278 $this->_reset_write();
2279 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002280
2281 // --------------------------------------------------------------------
2282
2283 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002284 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002285 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002286 * @param array An array of fields to reset
2287 * @return void
2288 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002289 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002290 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002291 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002292 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002293 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002294 {
2295 $this->$item = $default_value;
2296 }
2297 }
2298 }
2299
2300 // --------------------------------------------------------------------
2301
2302 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002303 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002304 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002305 * @return void
2306 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002307 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002308 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002309 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002310 'qb_select' => array(),
2311 'qb_from' => array(),
2312 'qb_join' => array(),
2313 'qb_where' => array(),
2314 'qb_like' => array(),
2315 'qb_groupby' => array(),
2316 'qb_having' => array(),
2317 'qb_orderby' => array(),
2318 'qb_wherein' => array(),
2319 'qb_aliased_tables' => array(),
2320 'qb_no_escape' => array(),
2321 'qb_distinct' => FALSE,
2322 'qb_limit' => FALSE,
2323 'qb_offset' => FALSE,
2324 'qb_order' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002325 )
2326 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002327 }
Barry Mienydd671972010-10-04 16:33:58 +02002328
Derek Allard2067d1a2008-11-13 22:59:24 +00002329 // --------------------------------------------------------------------
2330
2331 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002332 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002333 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002334 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002335 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002336 * @return void
2337 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002338 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002339 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002340 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002341 'qb_set' => array(),
2342 'qb_from' => array(),
2343 'qb_where' => array(),
2344 'qb_like' => array(),
2345 'qb_orderby' => array(),
2346 'qb_keys' => array(),
2347 'qb_limit' => FALSE,
2348 'qb_order' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002349 )
2350 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002351 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002352
Derek Allard2067d1a2008-11-13 22:59:24 +00002353}
2354
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002355/* End of file DB_query_builder.php */
Andrey Andreeve4c30192012-06-04 15:08:24 +03002356/* Location: ./system/database/DB_query_builder.php */