blob: 7490639ddd4452ad4710f634314f59985716da1e [file] [log] [blame]
Andrey Andreev24276a32012-01-08 02:44:38 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
WanWizard7219c072011-12-28 14:09:05 +01008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
WanWizard7219c072011-12-28 14:09:05 +010010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
Jamie Rumbelow7efad202012-02-19 12:37:00 +000029 * Query Builder Class
Derek Allard2067d1a2008-11-13 22:59:24 +000030 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +000031 * This is the platform-independent base Query Builder implementation class.
Derek Allard2067d1a2008-11-13 22:59:24 +000032 *
33 * @package CodeIgniter
34 * @subpackage Drivers
35 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/database/
38 */
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +010039
40abstract class CI_DB_query_builder extends CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000041
WanWizard7219c072011-12-28 14:09:05 +010042 protected $return_delete_sql = FALSE;
43 protected $reset_delete_data = FALSE;
44
Jamie Rumbelow7efad202012-02-19 12:37:00 +000045 protected $qb_select = array();
46 protected $qb_distinct = FALSE;
47 protected $qb_from = array();
48 protected $qb_join = array();
49 protected $qb_where = array();
50 protected $qb_like = array();
51 protected $qb_groupby = array();
52 protected $qb_having = array();
53 protected $qb_keys = array();
54 protected $qb_limit = FALSE;
55 protected $qb_offset = FALSE;
Jamie Rumbelow7efad202012-02-19 12:37:00 +000056 protected $qb_orderby = array();
57 protected $qb_set = array();
58 protected $qb_wherein = array();
59 protected $qb_aliased_tables = array();
60 protected $qb_store_array = array();
61 protected $qb_where_group_started = FALSE;
62 protected $qb_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +020063
Jamie Rumbelow7efad202012-02-19 12:37:00 +000064 // Query Builder Caching variables
65 protected $qb_caching = FALSE;
66 protected $qb_cache_exists = array();
67 protected $qb_cache_select = array();
68 protected $qb_cache_from = array();
69 protected $qb_cache_join = array();
70 protected $qb_cache_where = array();
71 protected $qb_cache_like = array();
72 protected $qb_cache_groupby = array();
73 protected $qb_cache_having = array();
74 protected $qb_cache_orderby = array();
75 protected $qb_cache_set = array();
WanWizard7219c072011-12-28 14:09:05 +010076
Jamie Rumbelow7efad202012-02-19 12:37:00 +000077 protected $qb_no_escape = array();
78 protected $qb_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000079
80 /**
81 * Select
82 *
83 * Generates the SELECT portion of the query
84 *
Derek Allard2067d1a2008-11-13 22:59:24 +000085 * @param string
86 * @return object
87 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060088 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
Derek Allard2067d1a2008-11-13 22:59:24 +000090 if (is_string($select))
91 {
92 $select = explode(',', $select);
93 }
94
95 foreach ($select as $val)
96 {
97 $val = trim($val);
98
Alex Bilbie48a2baf2012-06-02 11:09:54 +010099 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000101 $this->qb_select[] = $val;
102 $this->qb_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000103
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000104 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000106 $this->qb_cache_select[] = $val;
107 $this->qb_cache_exists[] = 'select';
108 $this->qb_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 }
110 }
111 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 return $this;
114 }
115
116 // --------------------------------------------------------------------
117
118 /**
119 * Select Max
120 *
121 * Generates a SELECT MAX(field) portion of a query
122 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * @param string the field
124 * @param string an alias
125 * @return object
126 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600127 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
129 return $this->_max_min_avg_sum($select, $alias, 'MAX');
130 }
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 // --------------------------------------------------------------------
133
134 /**
135 * Select Min
136 *
137 * Generates a SELECT MIN(field) portion of a query
138 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 * @param string the field
140 * @param string an alias
141 * @return object
142 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600143 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
145 return $this->_max_min_avg_sum($select, $alias, 'MIN');
146 }
147
148 // --------------------------------------------------------------------
149
150 /**
151 * Select Average
152 *
153 * Generates a SELECT AVG(field) portion of a query
154 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 * @param string the field
156 * @param string an alias
157 * @return object
158 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600159 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
161 return $this->_max_min_avg_sum($select, $alias, 'AVG');
162 }
163
164 // --------------------------------------------------------------------
165
166 /**
167 * Select Sum
168 *
169 * Generates a SELECT SUM(field) portion of a query
170 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 * @param string the field
172 * @param string an alias
173 * @return object
174 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600175 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 {
177 return $this->_max_min_avg_sum($select, $alias, 'SUM');
178 }
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Processing Function for the four functions above:
184 *
185 * select_max()
186 * select_min()
187 * select_avg()
Andrey Andreev24276a32012-01-08 02:44:38 +0200188 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200189 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 * @param string the field
191 * @param string an alias
192 * @return object
193 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600194 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100196 if ( ! is_string($select) OR $select === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
198 $this->display_error('db_invalid_query');
199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
204 {
205 show_error('Invalid function type: '.$type);
206 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100208 if ($alias === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
210 $alias = $this->_create_alias_from_table(trim($select));
211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300213 $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->escape_identifiers(trim($alias));
214
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000215 $this->qb_select[] = $sql;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100216 $this->qb_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200217
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000218 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000220 $this->qb_cache_select[] = $sql;
221 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 return $this;
225 }
226
227 // --------------------------------------------------------------------
228
229 /**
230 * Determines the alias name based on the table
231 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * @param string
233 * @return string
234 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600235 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
237 if (strpos($item, '.') !== FALSE)
238 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200239 $item = explode('.', $item);
240 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 return $item;
244 }
245
246 // --------------------------------------------------------------------
247
248 /**
249 * DISTINCT
250 *
251 * Sets a flag which tells the query string compiler to add DISTINCT
252 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 * @param bool
254 * @return object
255 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600256 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300258 $this->qb_distinct = is_bool($val) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 return $this;
260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 // --------------------------------------------------------------------
263
264 /**
265 * From
266 *
267 * Generates the FROM portion of the query
268 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 * @param mixed can be a string or array
270 * @return object
271 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600272 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300274 foreach ((array) $from as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
276 if (strpos($val, ',') !== FALSE)
277 {
278 foreach (explode(',', $val) as $v)
279 {
280 $v = trim($v);
281 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200282
George Petsagourakis193d4482012-04-28 11:16:18 +0300283 $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000284
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000285 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000287 $this->qb_cache_from[] = $v;
288 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200289 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
292 else
293 {
294 $val = trim($val);
295
Andrey Andreev24276a32012-01-08 02:44:38 +0200296 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000297 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200299
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000300 $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000301
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000302 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000304 $this->qb_cache_from[] = $val;
305 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 }
307 }
308 }
309
310 return $this;
311 }
312
313 // --------------------------------------------------------------------
314
315 /**
316 * Join
317 *
318 * Generates the JOIN portion of the query
319 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 * @param string
321 * @param string the join condition
322 * @param string the type of join
323 * @return object
324 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600325 public function join($table, $cond, $type = '')
Barry Mienydd671972010-10-04 16:33:58 +0200326 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100327 if ($type !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
329 $type = strtoupper(trim($type));
330
331 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
332 {
333 $type = '';
334 }
335 else
336 {
337 $type .= ' ';
338 }
339 }
340
Andrey Andreev24276a32012-01-08 02:44:38 +0200341 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000342 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 $this->_track_aliases($table);
344
345 // Strip apart the condition and protect the identifiers
Hamza Bhattid3c1ccf2012-03-05 22:58:56 +0400346 if (preg_match('/([\[\w\.]+)([\W\s]+)(.+)/', $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200348 $cond = $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 }
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // Assemble the JOIN statement
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000352 $this->qb_join[] = $join = $type.'JOIN '.$this->protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000353
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000354 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000356 $this->qb_cache_join[] = $join;
357 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 }
359
360 return $this;
361 }
362
363 // --------------------------------------------------------------------
364
365 /**
366 * Where
367 *
368 * Generates the WHERE portion of the query. Separates
369 * multiple calls with AND
370 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 * @param mixed
372 * @param mixed
373 * @return object
374 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600375 public function where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
377 return $this->_where($key, $value, 'AND ', $escape);
378 }
Barry Mienydd671972010-10-04 16:33:58 +0200379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 // --------------------------------------------------------------------
381
382 /**
383 * OR Where
384 *
385 * Generates the WHERE portion of the query. Separates
386 * multiple calls with OR
387 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 * @param mixed
389 * @param mixed
390 * @return object
391 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600392 public function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 {
394 return $this->_where($key, $value, 'OR ', $escape);
395 }
396
397 // --------------------------------------------------------------------
398
399 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 * Where
401 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600402 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 * @param mixed
405 * @param mixed
406 * @param string
407 * @return object
408 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600409 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
WanWizard7219c072011-12-28 14:09:05 +0100411 $type = $this->_group_get_type($type);
412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 if ( ! is_array($key))
414 {
415 $key = array($key => $value);
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // If the escape value was not set will will base it on the global setting
419 if ( ! is_bool($escape))
420 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +0000421 $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
423
424 foreach ($key as $k => $v)
425 {
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100426 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000427
Andrey Andreev9c14f652012-06-10 14:35:07 +0300428 $k = $this->_has_operator($k)
Andrey Andreev392b6ad2012-06-10 15:08:59 +0300429 ? $this->protect_identifiers(substr($k, 0, strpos(rtrim($k), ' ')), FALSE, $escape).strchr(rtrim($k), ' ')
Andrey Andreev9c14f652012-06-10 14:35:07 +0300430 : $this->protect_identifiers($k, FALSE, $escape);
431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 if (is_null($v) && ! $this->_has_operator($k))
433 {
434 // value appears not to have been set, assign the test to IS NULL
435 $k .= ' IS NULL';
436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 if ( ! is_null($v))
439 {
440 if ($escape === TRUE)
441 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 $v = ' '.$this->escape($v);
443 }
WanWizard7219c072011-12-28 14:09:05 +0100444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 if ( ! $this->_has_operator($k))
446 {
Greg Akere156c6e2011-04-20 16:03:04 -0500447 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
449 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000450
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000451 $this->qb_where[] = $prefix.$k.$v;
452 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000454 $this->qb_cache_where[] = $prefix.$k.$v;
455 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 return $this;
461 }
462
463 // --------------------------------------------------------------------
464
465 /**
466 * Where_in
467 *
468 * Generates a WHERE field IN ('item', 'item') SQL query joined with
469 * AND if appropriate
470 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 * @param string The field to search
472 * @param array The values searched on
473 * @return object
474 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600475 public function where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 {
477 return $this->_where_in($key, $values);
478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 // --------------------------------------------------------------------
481
482 /**
483 * Where_in_or
484 *
485 * Generates a WHERE field IN ('item', 'item') SQL query joined with
486 * OR if appropriate
487 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 * @param string The field to search
489 * @param array The values searched on
490 * @return object
491 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600492 public function or_where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
494 return $this->_where_in($key, $values, FALSE, 'OR ');
495 }
496
497 // --------------------------------------------------------------------
498
499 /**
500 * Where_not_in
501 *
502 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
503 * with AND if appropriate
504 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 * @param string The field to search
506 * @param array The values searched on
507 * @return object
508 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600509 public function where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 {
511 return $this->_where_in($key, $values, TRUE);
512 }
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 // --------------------------------------------------------------------
515
516 /**
517 * Where_not_in_or
518 *
519 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
520 * with OR if appropriate
521 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 * @param string The field to search
523 * @param array The values searched on
524 * @return object
525 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600526 public function or_where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 {
528 return $this->_where_in($key, $values, TRUE, 'OR ');
529 }
530
531 // --------------------------------------------------------------------
532
533 /**
534 * Where_in
535 *
536 * Called by where_in, where_in_or, where_not_in, where_not_in_or
537 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 * @param string The field to search
539 * @param array The values searched on
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300540 * @param bool If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200541 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 * @return object
543 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600544 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 {
546 if ($key === NULL OR $values === NULL)
547 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300548 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 }
Barry Mienydd671972010-10-04 16:33:58 +0200550
WanWizard7219c072011-12-28 14:09:05 +0100551 $type = $this->_group_get_type($type);
552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 if ( ! is_array($values))
554 {
555 $values = array($values);
556 }
Barry Mienydd671972010-10-04 16:33:58 +0200557
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 $not = ($not) ? ' NOT' : '';
559
560 foreach ($values as $value)
561 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000562 $this->qb_wherein[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 }
564
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000565 $prefix = (count($this->qb_where) === 0) ? '' : $type;
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000566 $this->qb_where[] = $where_in = $prefix.$this->protect_identifiers($key).$not.' IN ('.implode(', ', $this->qb_wherein).') ';
Barry Mienydd671972010-10-04 16:33:58 +0200567
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000568 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000570 $this->qb_cache_where[] = $where_in;
571 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 }
573
574 // reset the array for multiple calls
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000575 $this->qb_wherein = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 return $this;
577 }
Barry Mienydd671972010-10-04 16:33:58 +0200578
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 // --------------------------------------------------------------------
580
581 /**
582 * Like
583 *
584 * Generates a %LIKE% portion of the query. Separates
585 * multiple calls with AND
586 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 * @param mixed
588 * @param mixed
589 * @return object
590 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600591 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
593 return $this->_like($field, $match, 'AND ', $side);
594 }
595
596 // --------------------------------------------------------------------
597
598 /**
599 * Not Like
600 *
601 * Generates a NOT LIKE portion of the query. Separates
602 * multiple calls with AND
603 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 * @param mixed
605 * @param mixed
606 * @return object
607 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600608 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
610 return $this->_like($field, $match, 'AND ', $side, 'NOT');
611 }
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 // --------------------------------------------------------------------
614
615 /**
616 * OR Like
617 *
618 * Generates a %LIKE% portion of the query. Separates
619 * multiple calls with OR
620 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 * @param mixed
622 * @param mixed
623 * @return object
624 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600625 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 {
627 return $this->_like($field, $match, 'OR ', $side);
628 }
629
630 // --------------------------------------------------------------------
631
632 /**
633 * OR Not Like
634 *
635 * Generates a NOT LIKE portion of the query. Separates
636 * multiple calls with OR
637 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 * @param mixed
639 * @param mixed
640 * @return object
641 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600642 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
644 return $this->_like($field, $match, 'OR ', $side, 'NOT');
645 }
Barry Mienydd671972010-10-04 16:33:58 +0200646
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 // --------------------------------------------------------------------
648
649 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 * Like
651 *
652 * Called by like() or orlike()
653 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 * @param mixed
655 * @param mixed
656 * @param string
657 * @return object
658 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600659 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 {
WanWizard7219c072011-12-28 14:09:05 +0100661 $type = $this->_group_get_type($type);
662
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 if ( ! is_array($field))
664 {
665 $field = array($field => $match);
666 }
Barry Mienydd671972010-10-04 16:33:58 +0200667
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 foreach ($field as $k => $v)
669 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000670 $k = $this->protect_identifiers($k);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000671 $prefix = (count($this->qb_like) === 0) ? '' : $type;
Derek Jonese4ed5832009-02-20 21:44:59 +0000672 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300673
Andrey Andreev24276a32012-01-08 02:44:38 +0200674 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400675 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100676 $like_statement = "{$prefix} $k $not LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400677 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200678 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100680 $like_statement = "{$prefix} $k $not LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200682 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100684 $like_statement = "{$prefix} $k $not LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 }
686 else
687 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100688 $like_statement = "{$prefix} $k $not LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600690
Derek Jonese4ed5832009-02-20 21:44:59 +0000691 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100692 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000693 {
Greg Aker0d424892010-01-26 02:14:44 +0000694 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000695 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600696
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000697 $this->qb_like[] = $like_statement;
698 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000700 $this->qb_cache_like[] = $like_statement;
701 $this->qb_cache_exists[] = 'like';
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 }
Barry Mienydd671972010-10-04 16:33:58 +0200703
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 return $this;
707 }
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 // --------------------------------------------------------------------
710
711 /**
WanWizard7219c072011-12-28 14:09:05 +0100712 * Starts a query group.
713 *
714 * @param string (Internal use only)
715 * @param string (Internal use only)
716 * @return object
717 */
718 public function group_start($not = '', $type = 'AND ')
719 {
720 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100721
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000722 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100723 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000724 $this->qb_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (';
WanWizard7219c072011-12-28 14:09:05 +0100725
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000726 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100727 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000728 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100729 }
730
731 return $this;
732 }
733
734 // --------------------------------------------------------------------
735
736 /**
737 * Starts a query group, but ORs the group
738 *
739 * @return object
740 */
741 public function or_group_start()
742 {
743 return $this->group_start('', 'OR ');
744 }
745
746 // --------------------------------------------------------------------
747
748 /**
749 * Starts a query group, but NOTs the group
750 *
751 * @return object
752 */
753 public function not_group_start()
754 {
755 return $this->group_start('NOT ', 'AND ');
756 }
757
758 // --------------------------------------------------------------------
759
760 /**
761 * Starts a query group, but OR NOTs the group
762 *
763 * @return object
764 */
765 public function or_not_group_start()
766 {
767 return $this->group_start('NOT ', 'OR ');
768 }
769
770 // --------------------------------------------------------------------
771
772 /**
773 * Ends a query group
774 *
775 * @return object
776 */
777 public function group_end()
778 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000779 $this->qb_where_group_started = FALSE;
780 $this->qb_where[] = $value = str_repeat(' ', $this->qb_where_group_count--) . ')';
WanWizard7219c072011-12-28 14:09:05 +0100781
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000782 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100783 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000784 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100785 }
786
WanWizard7219c072011-12-28 14:09:05 +0100787 return $this;
788 }
789
790 // --------------------------------------------------------------------
791
792 /**
793 * Group_get_type
794 *
795 * Called by group_start(), _like(), _where() and _where_in()
796 *
797 * @param string
798 * @return string
799 */
800 protected function _group_get_type($type)
801 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000802 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100803 {
804 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000805 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100806 }
807
808 return $type;
809 }
810
811 // --------------------------------------------------------------------
812
813 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 * GROUP BY
815 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 * @param string
817 * @return object
818 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600819 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 {
821 if (is_string($by))
822 {
823 $by = explode(',', $by);
824 }
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 foreach ($by as $val)
827 {
828 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200829
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100830 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000832 $this->qb_groupby[] = $val = $this->protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200833
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000834 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000836 $this->qb_cache_groupby[] = $val;
837 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 }
839 }
840 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200841
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 return $this;
843 }
844
845 // --------------------------------------------------------------------
846
847 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 * Sets the HAVING value
849 *
850 * Separates multiple calls with AND
851 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 * @param string
853 * @param string
854 * @return object
855 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600856 public function having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 {
858 return $this->_having($key, $value, 'AND ', $escape);
859 }
Barry Mienydd671972010-10-04 16:33:58 +0200860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 // --------------------------------------------------------------------
862
863 /**
864 * Sets the OR HAVING value
865 *
866 * Separates multiple calls with OR
867 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 * @param string
869 * @param string
870 * @return object
871 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600872 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 {
874 return $this->_having($key, $value, 'OR ', $escape);
875 }
Barry Mienydd671972010-10-04 16:33:58 +0200876
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 // --------------------------------------------------------------------
878
879 /**
880 * Sets the HAVING values
881 *
882 * Called by having() or or_having()
883 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 * @param string
885 * @param string
886 * @return object
887 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600888 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 {
890 if ( ! is_array($key))
891 {
892 $key = array($key => $value);
893 }
Barry Mienydd671972010-10-04 16:33:58 +0200894
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 foreach ($key as $k => $v)
896 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000897 $prefix = (count($this->qb_having) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000898
899 if ($escape === TRUE)
900 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200901 $k = $this->protect_identifiers($k);
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 }
903
904 if ( ! $this->_has_operator($k))
905 {
906 $k .= ' = ';
907 }
908
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100909 if ($v !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400911 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 }
Barry Mienydd671972010-10-04 16:33:58 +0200913
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000914 $this->qb_having[] = $prefix.$k.$v;
915 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000917 $this->qb_cache_having[] = $prefix.$k.$v;
918 $this->qb_cache_exists[] = 'having';
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 }
920 }
Barry Mienydd671972010-10-04 16:33:58 +0200921
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 return $this;
923 }
Barry Mienydd671972010-10-04 16:33:58 +0200924
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 // --------------------------------------------------------------------
926
927 /**
928 * Sets the ORDER BY value
929 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 * @param string
931 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100932 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 * @return object
934 */
pporlan2c685fb2011-12-22 12:15:25 +0100935 public function order_by($orderby, $direction = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200937 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 {
939 $orderby = ''; // Random results want or don't need a field name
940 $direction = $this->_random_keyword;
941 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100942 elseif (trim($direction) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 {
Andrey Andreev650b4c02012-06-11 12:07:15 +0300944 $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : ' ASC';
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 }
Barry Mienydd671972010-10-04 16:33:58 +0200946
947
Andrey Andreev24276a32012-01-08 02:44:38 +0200948 if ((strpos($orderby, ',') !== FALSE) && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 {
950 $temp = array();
951 foreach (explode(',', $orderby) as $part)
952 {
953 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000954 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 {
Andrey Andreev88cb2782012-06-11 20:40:50 +0300956 $part = preg_match('/^(.+)\s+(ASC|DESC)$/i', $part, $matches)
957 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
958 : $this->protect_identifiers($part);
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 }
Barry Mienydd671972010-10-04 16:33:58 +0200960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 $temp[] = $part;
962 }
Barry Mienydd671972010-10-04 16:33:58 +0200963
964 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
Andrey Andreev650b4c02012-06-11 12:07:15 +0300966 elseif ($direction !== $this->_random_keyword && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 {
Andrey Andreev88cb2782012-06-11 20:40:50 +0300968 $part = preg_match('/^(.+)\s+(ASC|DESC)$/i', $orderby, $matches)
969 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
970 : $this->protect_identifiers($orderby);
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
Andrey Andreev650b4c02012-06-11 12:07:15 +0300997 if ( ! empty($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
Andrey Andreev650b4c02012-06-11 12:07:15 +03001072 $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 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001095 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
Andrey Andreev650b4c02012-06-11 12:07:15 +03001103 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 {
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
Andrey Andreev650b4c02012-06-11 12:07:15 +03001168 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 {
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
Andrey Andreev650b4c02012-06-11 12:07:15 +03001277 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001278 {
1279 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001280 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001281 {
Barry Mienydd671972010-10-04 16:33:58 +02001282 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001283 }
1284
Andrey Andreev650b4c02012-06-11 12:07:15 +03001285 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001286 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001287
1288 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001289 }
1290
1291 foreach ($keys as $k)
1292 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001293 $this->qb_keys[] = $this->protect_identifiers($k);
Derek Jonesd10e8962010-03-02 17:10:36 -06001294 }
Barry Mienydd671972010-10-04 16:33:58 +02001295
Derek Jonesd10e8962010-03-02 17:10:36 -06001296 return $this;
1297 }
WanWizard7219c072011-12-28 14:09:05 +01001298
Kyle Farris0c147b32011-08-26 02:29:31 -04001299 // --------------------------------------------------------------------
1300
1301 /**
1302 * Get INSERT query string
1303 *
1304 * Compiles an insert query and returns the sql
1305 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001306 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001307 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001308 * @return string
1309 */
1310 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001311 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001312 if ($this->_validate_insert($table) === FALSE)
1313 {
1314 return FALSE;
1315 }
WanWizard7219c072011-12-28 14:09:05 +01001316
Kyle Farris76116012011-08-31 11:17:48 -04001317 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001318 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001319 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001320 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001321 array_keys($this->qb_set),
1322 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001323 );
WanWizard7219c072011-12-28 14:09:05 +01001324
Kyle Farris0c147b32011-08-26 02:29:31 -04001325 if ($reset === TRUE)
1326 {
1327 $this->_reset_write();
1328 }
WanWizard7219c072011-12-28 14:09:05 +01001329
Kyle Farris0c147b32011-08-26 02:29:31 -04001330 return $sql;
1331 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001332
Derek Allard2067d1a2008-11-13 22:59:24 +00001333 // --------------------------------------------------------------------
1334
1335 /**
1336 * Insert
1337 *
1338 * Compiles an insert string and runs the query
1339 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001340 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 * @param array an associative array of insert values
1342 * @return object
1343 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001344 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001345 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 if ( ! is_null($set))
1347 {
1348 $this->set($set);
1349 }
WanWizard7219c072011-12-28 14:09:05 +01001350
Kyle Farris0c147b32011-08-26 02:29:31 -04001351 if ($this->_validate_insert($table) === FALSE)
1352 {
1353 return FALSE;
1354 }
WanWizard7219c072011-12-28 14:09:05 +01001355
Kyle Farris76116012011-08-31 11:17:48 -04001356 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001357 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001358 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001359 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001360 array_keys($this->qb_set),
1361 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001362 );
Barry Mienydd671972010-10-04 16:33:58 +02001363
Kyle Farris0c147b32011-08-26 02:29:31 -04001364 $this->_reset_write();
1365 return $this->query($sql);
1366 }
WanWizard7219c072011-12-28 14:09:05 +01001367
Kyle Farris0c147b32011-08-26 02:29:31 -04001368 // --------------------------------------------------------------------
1369
1370 /**
Andrey Andreev65d537c2012-04-05 14:11:41 +03001371 * Insert statement
1372 *
1373 * Generates a platform-specific insert string from the supplied data
1374 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001375 * @param string the table name
1376 * @param array the insert keys
1377 * @param array the insert values
1378 * @return string
Andrey Andreev65d537c2012-04-05 14:11:41 +03001379 */
1380 protected function _insert($table, $keys, $values)
1381 {
1382 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1383 }
1384
1385 // --------------------------------------------------------------------
1386
1387 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001388 * Validate Insert
1389 *
1390 * This method is used by both insert() and get_compiled_insert() to
1391 * validate that the there data is actually being set and that table
1392 * has been chosen to be inserted into.
1393 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001394 * @param string the table to insert data into
1395 * @return string
1396 */
WanWizard7219c072011-12-28 14:09:05 +01001397 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001398 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001399 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001400 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001401 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001402 }
1403
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001404 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001405 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001406 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001407 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001408 elseif ( ! isset($this->qb_from[0]))
1409 {
1410 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1411 }
WanWizard7219c072011-12-28 14:09:05 +01001412
Kyle Farris0c147b32011-08-26 02:29:31 -04001413 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001414 }
Barry Mienydd671972010-10-04 16:33:58 +02001415
Phil Sturgeon9789f322011-07-15 15:14:05 -06001416 // --------------------------------------------------------------------
1417
1418 /**
1419 * Replace
1420 *
1421 * Compiles an replace into string and runs the query
1422 *
1423 * @param string the table to replace data into
1424 * @param array an associative array of insert values
1425 * @return object
1426 */
1427 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001428 {
1429 if ( ! is_null($set))
1430 {
1431 $this->set($set);
1432 }
Barry Mienydd671972010-10-04 16:33:58 +02001433
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001434 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001435 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001436 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001437 }
1438
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001439 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001440 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001441 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001442 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001443 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001444 }
Barry Mienydd671972010-10-04 16:33:58 +02001445
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001446 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001447 }
1448
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001449 $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 +00001450
Derek Jonesd10e8962010-03-02 17:10:36 -06001451 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001452 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001453 }
WanWizard7219c072011-12-28 14:09:05 +01001454
Kyle Farris0c147b32011-08-26 02:29:31 -04001455 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001456
Kyle Farris0c147b32011-08-26 02:29:31 -04001457 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001458 * Replace statement
1459 *
1460 * Generates a platform-specific replace string from the supplied data
1461 *
1462 * @param string the table name
1463 * @param array the insert keys
1464 * @param array the insert values
1465 * @return string
1466 */
1467 protected function _replace($table, $keys, $values)
1468 {
1469 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1470 }
1471
1472 // --------------------------------------------------------------------
1473
1474 /**
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001475 * From Tables
1476 *
1477 * This public function implicitly groups FROM tables so there is no confusion
1478 * about operator precedence in harmony with SQL standards
1479 *
1480 * @param array
1481 * @return string
1482 */
1483 protected function _from_tables($tables)
1484 {
1485 is_array($tables) OR $tables = array($tables);
1486
1487 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
1488 }
1489
1490 // --------------------------------------------------------------------
1491
1492 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001493 * Get UPDATE query string
1494 *
1495 * Compiles an update query and returns the sql
1496 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001497 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001498 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001499 * @return string
1500 */
1501 public function get_compiled_update($table = '', $reset = TRUE)
1502 {
1503 // Combine any cached components with the current statements
1504 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001505
Kyle Farris0c147b32011-08-26 02:29:31 -04001506 if ($this->_validate_update($table) === FALSE)
1507 {
1508 return FALSE;
1509 }
WanWizard7219c072011-12-28 14:09:05 +01001510
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001511 $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 +01001512
Kyle Farris0c147b32011-08-26 02:29:31 -04001513 if ($reset === TRUE)
1514 {
1515 $this->_reset_write();
1516 }
WanWizard7219c072011-12-28 14:09:05 +01001517
Kyle Farris0c147b32011-08-26 02:29:31 -04001518 return $sql;
1519 }
WanWizard7219c072011-12-28 14:09:05 +01001520
Derek Allard2067d1a2008-11-13 22:59:24 +00001521 // --------------------------------------------------------------------
1522
1523 /**
1524 * Update
1525 *
1526 * Compiles an update string and runs the query
1527 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001528 * @param string the table to retrieve the results from
1529 * @param array an associative array of update values
1530 * @param mixed the where clause
1531 * @return object
1532 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001533 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001534 {
1535 // Combine any cached components with the current statements
1536 $this->_merge_cache();
1537
1538 if ( ! is_null($set))
1539 {
1540 $this->set($set);
1541 }
Barry Mienydd671972010-10-04 16:33:58 +02001542
Kyle Farris0c147b32011-08-26 02:29:31 -04001543 if ($this->_validate_update($table) === FALSE)
1544 {
1545 return FALSE;
1546 }
1547
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001548 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001549 {
1550 $this->where($where);
1551 }
1552
Andrey Andreev650b4c02012-06-11 12:07:15 +03001553 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001554 {
1555 $this->limit($limit);
1556 }
1557
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001558 $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 +00001559
Kyle Farris0c147b32011-08-26 02:29:31 -04001560 $this->_reset_write();
1561 return $this->query($sql);
1562 }
WanWizard7219c072011-12-28 14:09:05 +01001563
Kyle Farris0c147b32011-08-26 02:29:31 -04001564 // --------------------------------------------------------------------
1565
1566 /**
Andrey Andreev975034d2012-04-05 15:09:55 +03001567 * Update statement
1568 *
1569 * Generates a platform-specific update string from the supplied data
1570 *
1571 * @param string the table name
1572 * @param array the update data
1573 * @param array the where clause
1574 * @param array the orderby clause
1575 * @param array the limit clause
Andrey Andreev00541ae2012-04-09 11:43:10 +03001576 * @param array the like clause
Andrey Andreev975034d2012-04-05 15:09:55 +03001577 * @return string
1578 */
Andrey Andreev00541ae2012-04-09 11:43:10 +03001579 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Andrey Andreev975034d2012-04-05 15:09:55 +03001580 {
1581 foreach ($values as $key => $val)
1582 {
1583 $valstr[] = $key.' = '.$val;
1584 }
1585
Andrey Andreev00541ae2012-04-09 11:43:10 +03001586 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
1587
1588 if ( ! empty($like))
1589 {
1590 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
1591 }
1592
Andrey Andreev975034d2012-04-05 15:09:55 +03001593 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev00541ae2012-04-09 11:43:10 +03001594 .$where
Andrey Andreev975034d2012-04-05 15:09:55 +03001595 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
1596 .($limit ? ' LIMIT '.$limit : '');
1597 }
1598
1599 // --------------------------------------------------------------------
1600
1601 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001602 * Validate Update
1603 *
1604 * This method is used by both update() and get_compiled_update() to
1605 * validate that data is actually being set and that a table has been
1606 * chosen to be update.
1607 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001608 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001609 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001610 */
1611 protected function _validate_update($table = '')
1612 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001613 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001614 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001615 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001616 }
1617
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001618 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001619 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001620 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001621 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001622 elseif ( ! isset($this->qb_from[0]))
1623 {
1624 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1625 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001626
1627 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001628 }
WanWizard7219c072011-12-28 14:09:05 +01001629
Derek Jonesd10e8962010-03-02 17:10:36 -06001630 // --------------------------------------------------------------------
1631
1632 /**
1633 * Update_Batch
1634 *
1635 * Compiles an update string and runs the query
1636 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001637 * @param string the table to retrieve the results from
1638 * @param array an associative array of update values
1639 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001640 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001641 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001642 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001643 {
1644 // Combine any cached components with the current statements
1645 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001646
Derek Jonesd10e8962010-03-02 17:10:36 -06001647 if (is_null($index))
1648 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001649 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001650 }
1651
1652 if ( ! is_null($set))
1653 {
1654 $this->set_update_batch($set, $index);
1655 }
1656
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001657 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001658 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001659 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001660 }
1661
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001662 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001663 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001664 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001665 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001666 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001667 }
Barry Mienydd671972010-10-04 16:33:58 +02001668
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001669 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001670 }
Barry Mienydd671972010-10-04 16:33:58 +02001671
Derek Jonesd10e8962010-03-02 17:10:36 -06001672 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001673 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001674 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001675 $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 -06001676 }
Barry Mienydd671972010-10-04 16:33:58 +02001677
Derek Jonesd10e8962010-03-02 17:10:36 -06001678 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001679 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001680 }
1681
1682 // --------------------------------------------------------------------
1683
1684 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001685 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001686 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001687 * @param array
1688 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001689 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001690 * @return object
1691 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001692 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001693 {
1694 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001695
Derek Jonesd10e8962010-03-02 17:10:36 -06001696 if ( ! is_array($key))
1697 {
1698 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001699 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001700
1701 foreach ($key as $k => $v)
1702 {
1703 $index_set = FALSE;
1704 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001705 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001706 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001707 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001708 {
1709 $index_set = TRUE;
1710 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001711
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001712 $clean[$this->protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001713 }
1714
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001715 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001716 {
1717 return $this->display_error('db_batch_missing_index');
1718 }
1719
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001720 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001721 }
Barry Mienydd671972010-10-04 16:33:58 +02001722
Derek Jonesd10e8962010-03-02 17:10:36 -06001723 return $this;
1724 }
1725
Derek Allard2067d1a2008-11-13 22:59:24 +00001726 // --------------------------------------------------------------------
1727
1728 /**
1729 * Empty Table
1730 *
1731 * Compiles a delete string and runs "DELETE FROM table"
1732 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 * @param string the table to empty
1734 * @return object
1735 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001736 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001737 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001738 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001739 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001740 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001741 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001742 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001743 }
1744
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001745 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001746 }
1747 else
1748 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001749 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001750 }
1751
1752 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001753 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001754 return $this->query($sql);
1755 }
1756
1757 // --------------------------------------------------------------------
1758
1759 /**
1760 * Truncate
1761 *
1762 * Compiles a truncate string and runs the query
1763 * If the database does not support the truncate() command
1764 * This function maps to "DELETE FROM table"
1765 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001766 * @param string the table to truncate
1767 * @return object
1768 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001769 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001770 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001771 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001773 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001774 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001775 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001776 }
1777
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001778 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001779 }
1780 else
1781 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001782 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001783 }
1784
1785 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001786 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001787 return $this->query($sql);
1788 }
WanWizard7219c072011-12-28 14:09:05 +01001789
Kyle Farris0c147b32011-08-26 02:29:31 -04001790 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001791
Kyle Farris0c147b32011-08-26 02:29:31 -04001792 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001793 * Truncate statement
1794 *
1795 * Generates a platform-specific truncate string from the supplied data
1796 *
1797 * If the database does not support the truncate() command,
1798 * then this method maps to 'DELETE FROM table'
1799 *
1800 * @param string the table name
1801 * @return string
1802 */
1803 protected function _truncate($table)
1804 {
1805 return 'TRUNCATE '.$table;
1806 }
1807
1808 // --------------------------------------------------------------------
1809
1810 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001811 * Get DELETE query string
1812 *
1813 * Compiles a delete query string and returns the sql
1814 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001815 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001816 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001817 * @return string
1818 */
1819 public function get_compiled_delete($table = '', $reset = TRUE)
1820 {
1821 $this->return_delete_sql = TRUE;
1822 $sql = $this->delete($table, '', NULL, $reset);
1823 $this->return_delete_sql = FALSE;
1824 return $sql;
1825 }
WanWizard7219c072011-12-28 14:09:05 +01001826
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 // --------------------------------------------------------------------
1828
1829 /**
1830 * Delete
1831 *
1832 * Compiles a delete string and runs the query
1833 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001834 * @param mixed the table(s) to delete from. String or array
1835 * @param mixed the where clause
1836 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001837 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001838 * @return object
1839 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001840 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001841 {
1842 // Combine any cached components with the current statements
1843 $this->_merge_cache();
1844
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001845 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001846 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001847 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001848 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001849 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001850 }
1851
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001852 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001853 }
1854 elseif (is_array($table))
1855 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001856 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001857 {
1858 $this->delete($single_table, $where, $limit, FALSE);
1859 }
1860
1861 $this->_reset_write();
1862 return;
1863 }
1864 else
1865 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001866 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001867 }
1868
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001869 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001870 {
1871 $this->where($where);
1872 }
1873
Andrey Andreev650b4c02012-06-11 12:07:15 +03001874 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001875 {
1876 $this->limit($limit);
1877 }
1878
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001879 if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001880 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001881 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001882 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001883
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001884 $sql = $this->_delete($table, $this->qb_where, $this->qb_like, $this->qb_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001885 if ($reset_data)
1886 {
1887 $this->_reset_write();
1888 }
WanWizard7219c072011-12-28 14:09:05 +01001889
Andrey Andreev24276a32012-01-08 02:44:38 +02001890 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001891 }
WanWizard7219c072011-12-28 14:09:05 +01001892
Derek Allard2067d1a2008-11-13 22:59:24 +00001893 // --------------------------------------------------------------------
1894
1895 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001896 * Delete statement
1897 *
1898 * Generates a platform-specific delete string from the supplied data
1899 *
1900 * @param string the table name
1901 * @param array the where clause
1902 * @param array the like clause
1903 * @param string the limit clause
1904 * @return string
1905 */
1906 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
1907 {
1908 $conditions = array();
1909
1910 empty($where) OR $conditions[] = implode(' ', $where);
1911 empty($like) OR $conditions[] = implode(' ', $like);
1912
1913 return 'DELETE FROM '.$table
1914 .(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '')
Andrey Andreev650b4c02012-06-11 12:07:15 +03001915 .($limit ? ' LIMIT '.(int) $limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001916 }
1917
1918 // --------------------------------------------------------------------
1919
1920 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001921 * DB Prefix
1922 *
1923 * Prepends a database prefix if one exists in configuration
1924 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001925 * @param string the table
1926 * @return string
1927 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001928 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001929 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001930 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001931 {
1932 $this->display_error('db_table_name_required');
1933 }
1934
1935 return $this->dbprefix.$table;
1936 }
1937
1938 // --------------------------------------------------------------------
1939
1940 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001941 * Set DB Prefix
1942 *
1943 * Set's the DB Prefix to something new without needing to reconnect
1944 *
1945 * @param string the prefix
1946 * @return string
1947 */
1948 public function set_dbprefix($prefix = '')
1949 {
1950 return $this->dbprefix = $prefix;
1951 }
1952
1953 // --------------------------------------------------------------------
1954
1955 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001956 * Track Aliases
1957 *
1958 * Used to track SQL statements written with aliased tables.
1959 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001960 * @param string The table to inspect
1961 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001962 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001963 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001964 {
1965 if (is_array($table))
1966 {
1967 foreach ($table as $t)
1968 {
1969 $this->_track_aliases($t);
1970 }
1971 return;
1972 }
Barry Mienydd671972010-10-04 16:33:58 +02001973
Derek Jones37f4b9c2011-07-01 17:56:50 -05001974 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001975 // the string into discreet statements
1976 if (strpos($table, ',') !== FALSE)
1977 {
1978 return $this->_track_aliases(explode(',', $table));
1979 }
Barry Mienydd671972010-10-04 16:33:58 +02001980
Derek Allard2067d1a2008-11-13 22:59:24 +00001981 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001982 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001983 {
1984 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03001985 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001986
Derek Allard2067d1a2008-11-13 22:59:24 +00001987 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001988 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001989
Derek Allard2067d1a2008-11-13 22:59:24 +00001990 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001991 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001992 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001993 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001994 }
1995 }
1996 }
WanWizard7219c072011-12-28 14:09:05 +01001997
Derek Allard2067d1a2008-11-13 22:59:24 +00001998 // --------------------------------------------------------------------
1999
2000 /**
2001 * Compile the SELECT statement
2002 *
2003 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05002004 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00002005 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002006 * @return string
2007 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002008 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002009 {
2010 // Combine any cached components with the current statements
2011 $this->_merge_cache();
2012
Derek Allard2067d1a2008-11-13 22:59:24 +00002013 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002014 if ($select_override !== FALSE)
2015 {
2016 $sql = $select_override;
2017 }
2018 else
2019 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002020 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002021
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002022 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002023 {
Barry Mienydd671972010-10-04 16:33:58 +02002024 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002025 }
2026 else
Barry Mienydd671972010-10-04 16:33:58 +02002027 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002028 // Cycle through the "select" portion of the query and prep each column name.
2029 // The reason we protect identifiers here rather then in the select() function
2030 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002031 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002032 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002033 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002034 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002035 }
Barry Mienydd671972010-10-04 16:33:58 +02002036
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002037 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002038 }
2039 }
2040
Derek Allard2067d1a2008-11-13 22:59:24 +00002041 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002042 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002043 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002044 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002045 }
2046
Derek Allard2067d1a2008-11-13 22:59:24 +00002047 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002048 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002049 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002050 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002051 }
2052
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 // Write the "WHERE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002054 if (count($this->qb_where) > 0 OR count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002055 {
Greg Akere156c6e2011-04-20 16:03:04 -05002056 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00002057 }
2058
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002059 $sql .= implode("\n", $this->qb_where);
Derek Allard2067d1a2008-11-13 22:59:24 +00002060
Derek Allard2067d1a2008-11-13 22:59:24 +00002061 // Write the "LIKE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002062 if (count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002063 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002064 if (count($this->qb_where) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002065 {
2066 $sql .= "\nAND ";
2067 }
2068
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002069 $sql .= implode("\n", $this->qb_like);
Derek Allard2067d1a2008-11-13 22:59:24 +00002070 }
2071
Derek Allard2067d1a2008-11-13 22:59:24 +00002072 // Write the "GROUP BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002073 if (count($this->qb_groupby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002074 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002075 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002076 }
2077
Derek Allard2067d1a2008-11-13 22:59:24 +00002078 // Write the "HAVING" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002079 if (count($this->qb_having) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002080 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002081 $sql .= "\nHAVING ".implode("\n", $this->qb_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00002082 }
2083
Derek Allard2067d1a2008-11-13 22:59:24 +00002084 // Write the "ORDER BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002085 if (count($this->qb_orderby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002086 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002087 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002088 }
2089
Derek Allard2067d1a2008-11-13 22:59:24 +00002090 // Write the "LIMIT" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002091 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002092 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002093 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00002094 }
2095
2096 return $sql;
2097 }
2098
2099 // --------------------------------------------------------------------
2100
2101 /**
2102 * Object to Array
2103 *
2104 * Takes an object as input and converts the class variables to array key/vals
2105 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002106 * @param object
2107 * @return array
2108 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002109 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002110 {
2111 if ( ! is_object($object))
2112 {
2113 return $object;
2114 }
Barry Mienydd671972010-10-04 16:33:58 +02002115
Derek Allard2067d1a2008-11-13 22:59:24 +00002116 $array = array();
2117 foreach (get_object_vars($object) as $key => $val)
2118 {
2119 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002120 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002121 {
2122 $array[$key] = $val;
2123 }
2124 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002125
2126 return $array;
2127 }
Barry Mienydd671972010-10-04 16:33:58 +02002128
Derek Jonesd10e8962010-03-02 17:10:36 -06002129 // --------------------------------------------------------------------
2130
2131 /**
2132 * Object to Array
2133 *
2134 * Takes an object as input and converts the class variables to array key/vals
2135 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002136 * @param object
2137 * @return array
2138 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002139 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002140 {
2141 if ( ! is_object($object))
2142 {
2143 return $object;
2144 }
Barry Mienydd671972010-10-04 16:33:58 +02002145
Derek Jonesd10e8962010-03-02 17:10:36 -06002146 $array = array();
2147 $out = get_object_vars($object);
2148 $fields = array_keys($out);
2149
2150 foreach ($fields as $val)
2151 {
2152 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002153 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002154 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002155 $i = 0;
2156 foreach ($out[$val] as $data)
2157 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002158 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002159 }
2160 }
2161 }
2162
Derek Allard2067d1a2008-11-13 22:59:24 +00002163 return $array;
2164 }
Barry Mienydd671972010-10-04 16:33:58 +02002165
Derek Allard2067d1a2008-11-13 22:59:24 +00002166 // --------------------------------------------------------------------
2167
2168 /**
2169 * Start Cache
2170 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002171 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002172 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002173 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002174 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002175 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002176 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002177 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002178 }
2179
2180 // --------------------------------------------------------------------
2181
2182 /**
2183 * Stop Cache
2184 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002185 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002186 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002187 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002188 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002189 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002190 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002191 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002192 }
2193
2194 // --------------------------------------------------------------------
2195
2196 /**
2197 * Flush Cache
2198 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002199 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002200 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002201 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002202 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002203 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002204 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002205 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002206 'qb_cache_select' => array(),
2207 'qb_cache_from' => array(),
2208 'qb_cache_join' => array(),
2209 'qb_cache_where' => array(),
2210 'qb_cache_like' => array(),
2211 'qb_cache_groupby' => array(),
2212 'qb_cache_having' => array(),
2213 'qb_cache_orderby' => array(),
2214 'qb_cache_set' => array(),
2215 'qb_cache_exists' => array(),
2216 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002217 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002218 }
2219
2220 // --------------------------------------------------------------------
2221
2222 /**
2223 * Merge Cache
2224 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002225 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002226 * locally called ones.
2227 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002228 * @return void
2229 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002230 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002231 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002232 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002233 {
2234 return;
2235 }
2236
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002237 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002238 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002239 $qb_variable = 'qb_'.$val;
2240 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002241
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002242 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002243 {
2244 continue;
2245 }
2246
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002247 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002248 }
2249
2250 // If we are "protecting identifiers" we need to examine the "from"
2251 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002252 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002253 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002254 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002255 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002256
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002257 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002258 }
WanWizard7219c072011-12-28 14:09:05 +01002259
Kyle Farris0c147b32011-08-26 02:29:31 -04002260 // --------------------------------------------------------------------
2261
2262 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002263 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002264 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002265 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002266 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002267 * @return void
2268 */
2269 public function reset_query()
2270 {
2271 $this->_reset_select();
2272 $this->_reset_write();
2273 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002274
2275 // --------------------------------------------------------------------
2276
2277 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002278 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002279 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002280 * @param array An array of fields to reset
2281 * @return void
2282 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002283 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002284 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002285 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002286 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002287 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002288 {
2289 $this->$item = $default_value;
2290 }
2291 }
2292 }
2293
2294 // --------------------------------------------------------------------
2295
2296 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002297 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002298 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002299 * @return void
2300 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002301 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002302 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002303 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002304 'qb_select' => array(),
2305 'qb_from' => array(),
2306 'qb_join' => array(),
2307 'qb_where' => array(),
2308 'qb_like' => array(),
2309 'qb_groupby' => array(),
2310 'qb_having' => array(),
2311 'qb_orderby' => array(),
2312 'qb_wherein' => array(),
2313 'qb_aliased_tables' => array(),
2314 'qb_no_escape' => array(),
2315 'qb_distinct' => FALSE,
2316 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002317 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002318 )
2319 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002320 }
Barry Mienydd671972010-10-04 16:33:58 +02002321
Derek Allard2067d1a2008-11-13 22:59:24 +00002322 // --------------------------------------------------------------------
2323
2324 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002325 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002326 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002327 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002328 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002329 * @return void
2330 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002331 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002332 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002333 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002334 'qb_set' => array(),
2335 'qb_from' => array(),
2336 'qb_where' => array(),
2337 'qb_like' => array(),
2338 'qb_orderby' => array(),
2339 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002340 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002341 )
2342 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002343 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002344
Derek Allard2067d1a2008-11-13 22:59:24 +00002345}
2346
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002347/* End of file DB_query_builder.php */
Andrey Andreeve4c30192012-06-04 15:08:24 +03002348/* Location: ./system/database/DB_query_builder.php */