blob: d0af66de193da82521c70aa37074539b1a40a738 [file] [log] [blame]
Andrey Andreev24276a32012-01-08 02:44:38 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
WanWizard7219c072011-12-28 14:09:05 +01008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
WanWizard7219c072011-12-28 14:09:05 +010010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
Jamie Rumbelow7efad202012-02-19 12:37:00 +000029 * Query Builder Class
Derek Allard2067d1a2008-11-13 22:59:24 +000030 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +000031 * This is the platform-independent base Query Builder implementation class.
Derek Allard2067d1a2008-11-13 22:59:24 +000032 *
33 * @package CodeIgniter
34 * @subpackage Drivers
35 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/database/
38 */
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +010039
40abstract class CI_DB_query_builder extends CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000041
WanWizard7219c072011-12-28 14:09:05 +010042 protected $return_delete_sql = FALSE;
43 protected $reset_delete_data = FALSE;
44
Jamie Rumbelow7efad202012-02-19 12:37:00 +000045 protected $qb_select = array();
46 protected $qb_distinct = FALSE;
47 protected $qb_from = array();
48 protected $qb_join = array();
49 protected $qb_where = array();
50 protected $qb_like = array();
51 protected $qb_groupby = array();
52 protected $qb_having = array();
53 protected $qb_keys = array();
54 protected $qb_limit = FALSE;
55 protected $qb_offset = FALSE;
56 protected $qb_order = FALSE;
57 protected $qb_orderby = array();
58 protected $qb_set = array();
59 protected $qb_wherein = array();
60 protected $qb_aliased_tables = array();
61 protected $qb_store_array = array();
62 protected $qb_where_group_started = FALSE;
63 protected $qb_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +020064
Jamie Rumbelow7efad202012-02-19 12:37:00 +000065 // Query Builder Caching variables
66 protected $qb_caching = FALSE;
67 protected $qb_cache_exists = array();
68 protected $qb_cache_select = array();
69 protected $qb_cache_from = array();
70 protected $qb_cache_join = array();
71 protected $qb_cache_where = array();
72 protected $qb_cache_like = array();
73 protected $qb_cache_groupby = array();
74 protected $qb_cache_having = array();
75 protected $qb_cache_orderby = array();
76 protected $qb_cache_set = array();
WanWizard7219c072011-12-28 14:09:05 +010077
Jamie Rumbelow7efad202012-02-19 12:37:00 +000078 protected $qb_no_escape = array();
79 protected $qb_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000080
81 /**
82 * Select
83 *
84 * Generates the SELECT portion of the query
85 *
Derek Allard2067d1a2008-11-13 22:59:24 +000086 * @param string
87 * @return object
88 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060089 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000090 {
Derek Allard2067d1a2008-11-13 22:59:24 +000091 if (is_string($select))
92 {
93 $select = explode(',', $select);
94 }
95
96 foreach ($select as $val)
97 {
98 $val = trim($val);
99
100 if ($val != '')
101 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000102 $this->qb_select[] = $val;
103 $this->qb_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000104
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000105 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000107 $this->qb_cache_select[] = $val;
108 $this->qb_cache_exists[] = 'select';
109 $this->qb_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 }
111 }
112 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 return $this;
115 }
116
117 // --------------------------------------------------------------------
118
119 /**
120 * Select Max
121 *
122 * Generates a SELECT MAX(field) portion of a query
123 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 * @param string the field
125 * @param string an alias
126 * @return object
127 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600128 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
130 return $this->_max_min_avg_sum($select, $alias, 'MAX');
131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 // --------------------------------------------------------------------
134
135 /**
136 * Select Min
137 *
138 * Generates a SELECT MIN(field) portion of a query
139 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 * @param string the field
141 * @param string an alias
142 * @return object
143 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600144 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
146 return $this->_max_min_avg_sum($select, $alias, 'MIN');
147 }
148
149 // --------------------------------------------------------------------
150
151 /**
152 * Select Average
153 *
154 * Generates a SELECT AVG(field) portion of a query
155 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 * @param string the field
157 * @param string an alias
158 * @return object
159 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600160 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
162 return $this->_max_min_avg_sum($select, $alias, 'AVG');
163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Select Sum
169 *
170 * Generates a SELECT SUM(field) portion of a query
171 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 * @param string the field
173 * @param string an alias
174 * @return object
175 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600176 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
178 return $this->_max_min_avg_sum($select, $alias, 'SUM');
179 }
180
181 // --------------------------------------------------------------------
182
183 /**
184 * Processing Function for the four functions above:
185 *
186 * select_max()
187 * select_min()
188 * select_avg()
Andrey Andreev24276a32012-01-08 02:44:38 +0200189 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200190 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 * @param string the field
192 * @param string an alias
193 * @return object
194 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600195 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
197 if ( ! is_string($select) OR $select == '')
198 {
199 $this->display_error('db_invalid_query');
200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
205 {
206 show_error('Invalid function type: '.$type);
207 }
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 if ($alias == '')
210 {
211 $alias = $this->_create_alias_from_table(trim($select));
212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000214 $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->protect_identifiers(trim($alias));
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100215
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000216 $this->qb_select[] = $sql;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100217 $this->qb_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200218
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000219 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000221 $this->qb_cache_select[] = $sql;
222 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 return $this;
226 }
227
228 // --------------------------------------------------------------------
229
230 /**
231 * Determines the alias name based on the table
232 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 * @param string
234 * @return string
235 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600236 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
238 if (strpos($item, '.') !== FALSE)
239 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200240 $item = explode('.', $item);
241 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 return $item;
245 }
246
247 // --------------------------------------------------------------------
248
249 /**
250 * DISTINCT
251 *
252 * Sets a flag which tells the query string compiler to add DISTINCT
253 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 * @param bool
255 * @return object
256 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600257 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000259 $this->qb_distinct = (is_bool($val)) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 return $this;
261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 // --------------------------------------------------------------------
264
265 /**
266 * From
267 *
268 * Generates the FROM portion of the query
269 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 * @param mixed can be a string or array
271 * @return object
272 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600273 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 {
275 foreach ((array)$from as $val)
276 {
277 if (strpos($val, ',') !== FALSE)
278 {
279 foreach (explode(',', $val) as $v)
280 {
281 $v = trim($v);
282 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200283
George Petsagourakis193d4482012-04-28 11:16:18 +0300284 $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000285
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000286 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000288 $this->qb_cache_from[] = $v;
289 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200290 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 }
293 else
294 {
295 $val = trim($val);
296
Andrey Andreev24276a32012-01-08 02:44:38 +0200297 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000298 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200300
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000301 $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000302
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000303 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000305 $this->qb_cache_from[] = $val;
306 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 }
308 }
309 }
310
311 return $this;
312 }
313
314 // --------------------------------------------------------------------
315
316 /**
317 * Join
318 *
319 * Generates the JOIN portion of the query
320 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 * @param string
322 * @param string the join condition
323 * @param string the type of join
324 * @return object
325 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600326 public function join($table, $cond, $type = '')
Barry Mienydd671972010-10-04 16:33:58 +0200327 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 if ($type != '')
329 {
330 $type = strtoupper(trim($type));
331
332 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
333 {
334 $type = '';
335 }
336 else
337 {
338 $type .= ' ';
339 }
340 }
341
Andrey Andreev24276a32012-01-08 02:44:38 +0200342 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000343 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 $this->_track_aliases($table);
345
346 // Strip apart the condition and protect the identifiers
Hamza Bhattid3c1ccf2012-03-05 22:58:56 +0400347 if (preg_match('/([\[\w\.]+)([\W\s]+)(.+)/', $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200349 $cond = $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 // Assemble the JOIN statement
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000353 $this->qb_join[] = $join = $type.'JOIN '.$this->protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000354
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000355 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000357 $this->qb_cache_join[] = $join;
358 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
360
361 return $this;
362 }
363
364 // --------------------------------------------------------------------
365
366 /**
367 * Where
368 *
369 * Generates the WHERE portion of the query. Separates
370 * multiple calls with AND
371 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 * @param mixed
373 * @param mixed
374 * @return object
375 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600376 public function where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 return $this->_where($key, $value, 'AND ', $escape);
379 }
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 // --------------------------------------------------------------------
382
383 /**
384 * OR Where
385 *
386 * Generates the WHERE portion of the query. Separates
387 * multiple calls with OR
388 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 * @param mixed
390 * @param mixed
391 * @return object
392 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600393 public function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
395 return $this->_where($key, $value, 'OR ', $escape);
396 }
397
398 // --------------------------------------------------------------------
399
400 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 * Where
402 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600403 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * @param mixed
406 * @param mixed
407 * @param string
408 * @return object
409 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600410 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
WanWizard7219c072011-12-28 14:09:05 +0100412 $type = $this->_group_get_type($type);
413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 if ( ! is_array($key))
415 {
416 $key = array($key => $value);
417 }
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 // If the escape value was not set will will base it on the global setting
420 if ( ! is_bool($escape))
421 {
Jamie Rumbelow0cd8c792012-03-08 12:52:24 +0000422 $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 }
424
425 foreach ($key as $k => $v)
426 {
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100427 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000428
429 if (is_null($v) && ! $this->_has_operator($k))
430 {
431 // value appears not to have been set, assign the test to IS NULL
432 $k .= ' IS NULL';
433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 if ( ! is_null($v))
436 {
437 if ($escape === TRUE)
438 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200439 $k = $this->protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 $v = ' '.$this->escape($v);
441 }
WanWizard7219c072011-12-28 14:09:05 +0100442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 if ( ! $this->_has_operator($k))
444 {
Greg Akere156c6e2011-04-20 16:03:04 -0500445 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
447 }
448 else
449 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200450 $k = $this->protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 }
452
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000453 $this->qb_where[] = $prefix.$k.$v;
454 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000456 $this->qb_cache_where[] = $prefix.$k.$v;
457 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 return $this;
463 }
464
465 // --------------------------------------------------------------------
466
467 /**
468 * Where_in
469 *
470 * Generates a WHERE field IN ('item', 'item') SQL query joined with
471 * AND if appropriate
472 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 * @param string The field to search
474 * @param array The values searched on
475 * @return object
476 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600477 public function where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 {
479 return $this->_where_in($key, $values);
480 }
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 // --------------------------------------------------------------------
483
484 /**
485 * Where_in_or
486 *
487 * Generates a WHERE field IN ('item', 'item') SQL query joined with
488 * OR if appropriate
489 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 * @param string The field to search
491 * @param array The values searched on
492 * @return object
493 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600494 public function or_where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
496 return $this->_where_in($key, $values, FALSE, 'OR ');
497 }
498
499 // --------------------------------------------------------------------
500
501 /**
502 * Where_not_in
503 *
504 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
505 * with AND if appropriate
506 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 * @param string The field to search
508 * @param array The values searched on
509 * @return object
510 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600511 public function where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 {
513 return $this->_where_in($key, $values, TRUE);
514 }
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 // --------------------------------------------------------------------
517
518 /**
519 * Where_not_in_or
520 *
521 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
522 * with OR if appropriate
523 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 * @param string The field to search
525 * @param array The values searched on
526 * @return object
527 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600528 public function or_where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 {
530 return $this->_where_in($key, $values, TRUE, 'OR ');
531 }
532
533 // --------------------------------------------------------------------
534
535 /**
536 * Where_in
537 *
538 * Called by where_in, where_in_or, where_not_in, where_not_in_or
539 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 * @param string The field to search
541 * @param array The values searched on
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300542 * @param bool If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200543 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 * @return object
545 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600546 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 {
548 if ($key === NULL OR $values === NULL)
549 {
550 return;
551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552
WanWizard7219c072011-12-28 14:09:05 +0100553 $type = $this->_group_get_type($type);
554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 if ( ! is_array($values))
556 {
557 $values = array($values);
558 }
Barry Mienydd671972010-10-04 16:33:58 +0200559
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 $not = ($not) ? ' NOT' : '';
561
562 foreach ($values as $value)
563 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000564 $this->qb_wherein[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 }
566
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000567 $prefix = (count($this->qb_where) === 0) ? '' : $type;
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000568 $this->qb_where[] = $where_in = $prefix.$this->protect_identifiers($key).$not.' IN ('.implode(', ', $this->qb_wherein).') ';
Barry Mienydd671972010-10-04 16:33:58 +0200569
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000570 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000572 $this->qb_cache_where[] = $where_in;
573 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 }
575
576 // reset the array for multiple calls
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000577 $this->qb_wherein = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 return $this;
579 }
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 // --------------------------------------------------------------------
582
583 /**
584 * Like
585 *
586 * Generates a %LIKE% portion of the query. Separates
587 * multiple calls with AND
588 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 * @param mixed
590 * @param mixed
591 * @return object
592 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600593 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
595 return $this->_like($field, $match, 'AND ', $side);
596 }
597
598 // --------------------------------------------------------------------
599
600 /**
601 * Not Like
602 *
603 * Generates a NOT LIKE portion of the query. Separates
604 * multiple calls with AND
605 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 * @param mixed
607 * @param mixed
608 * @return object
609 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600610 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 {
612 return $this->_like($field, $match, 'AND ', $side, 'NOT');
613 }
Barry Mienydd671972010-10-04 16:33:58 +0200614
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 // --------------------------------------------------------------------
616
617 /**
618 * OR Like
619 *
620 * Generates a %LIKE% portion of the query. Separates
621 * multiple calls with OR
622 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 * @param mixed
624 * @param mixed
625 * @return object
626 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600627 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 {
629 return $this->_like($field, $match, 'OR ', $side);
630 }
631
632 // --------------------------------------------------------------------
633
634 /**
635 * OR Not Like
636 *
637 * Generates a NOT LIKE portion of the query. Separates
638 * multiple calls with OR
639 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 * @param mixed
641 * @param mixed
642 * @return object
643 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600644 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 {
646 return $this->_like($field, $match, 'OR ', $side, 'NOT');
647 }
Barry Mienydd671972010-10-04 16:33:58 +0200648
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 // --------------------------------------------------------------------
650
651 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 * Like
653 *
654 * Called by like() or orlike()
655 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 * @param mixed
657 * @param mixed
658 * @param string
659 * @return object
660 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600661 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 {
WanWizard7219c072011-12-28 14:09:05 +0100663 $type = $this->_group_get_type($type);
664
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 if ( ! is_array($field))
666 {
667 $field = array($field => $match);
668 }
Barry Mienydd671972010-10-04 16:33:58 +0200669
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 foreach ($field as $k => $v)
671 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000672 $k = $this->protect_identifiers($k);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000673 $prefix = (count($this->qb_like) === 0) ? '' : $type;
Derek Jonese4ed5832009-02-20 21:44:59 +0000674 $v = $this->escape_like_str($v);
Kyle Farris0c147b32011-08-26 02:29:31 -0400675
Andrey Andreev24276a32012-01-08 02:44:38 +0200676 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400677 {
678 $like_statement = $prefix." $k $not LIKE '{$v}'";
679 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200680 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 {
682 $like_statement = $prefix." $k $not LIKE '%{$v}'";
683 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200684 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 {
686 $like_statement = $prefix." $k $not LIKE '{$v}%'";
687 }
688 else
689 {
690 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
691 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600692
Derek Jonese4ed5832009-02-20 21:44:59 +0000693 // some platforms require an escape sequence definition for LIKE wildcards
694 if ($this->_like_escape_str != '')
695 {
Greg Aker0d424892010-01-26 02:14:44 +0000696 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000697 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600698
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000699 $this->qb_like[] = $like_statement;
700 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000702 $this->qb_cache_like[] = $like_statement;
703 $this->qb_cache_exists[] = 'like';
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 }
Barry Mienydd671972010-10-04 16:33:58 +0200705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200707
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 return $this;
709 }
Barry Mienydd671972010-10-04 16:33:58 +0200710
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 // --------------------------------------------------------------------
712
713 /**
WanWizard7219c072011-12-28 14:09:05 +0100714 * Starts a query group.
715 *
716 * @param string (Internal use only)
717 * @param string (Internal use only)
718 * @return object
719 */
720 public function group_start($not = '', $type = 'AND ')
721 {
722 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100723
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000724 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100725 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000726 $this->qb_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (';
WanWizard7219c072011-12-28 14:09:05 +0100727
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000728 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100729 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000730 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100731 }
732
733 return $this;
734 }
735
736 // --------------------------------------------------------------------
737
738 /**
739 * Starts a query group, but ORs the group
740 *
741 * @return object
742 */
743 public function or_group_start()
744 {
745 return $this->group_start('', 'OR ');
746 }
747
748 // --------------------------------------------------------------------
749
750 /**
751 * Starts a query group, but NOTs the group
752 *
753 * @return object
754 */
755 public function not_group_start()
756 {
757 return $this->group_start('NOT ', 'AND ');
758 }
759
760 // --------------------------------------------------------------------
761
762 /**
763 * Starts a query group, but OR NOTs the group
764 *
765 * @return object
766 */
767 public function or_not_group_start()
768 {
769 return $this->group_start('NOT ', 'OR ');
770 }
771
772 // --------------------------------------------------------------------
773
774 /**
775 * Ends a query group
776 *
777 * @return object
778 */
779 public function group_end()
780 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000781 $this->qb_where_group_started = FALSE;
782 $this->qb_where[] = $value = str_repeat(' ', $this->qb_where_group_count--) . ')';
WanWizard7219c072011-12-28 14:09:05 +0100783
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000784 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100785 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000786 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100787 }
788
WanWizard7219c072011-12-28 14:09:05 +0100789 return $this;
790 }
791
792 // --------------------------------------------------------------------
793
794 /**
795 * Group_get_type
796 *
797 * Called by group_start(), _like(), _where() and _where_in()
798 *
799 * @param string
800 * @return string
801 */
802 protected function _group_get_type($type)
803 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000804 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100805 {
806 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000807 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100808 }
809
810 return $type;
811 }
812
813 // --------------------------------------------------------------------
814
815 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 * GROUP BY
817 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 * @param string
819 * @return object
820 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600821 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 {
823 if (is_string($by))
824 {
825 $by = explode(',', $by);
826 }
Barry Mienydd671972010-10-04 16:33:58 +0200827
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 foreach ($by as $val)
829 {
830 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200831
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 if ($val != '')
833 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000834 $this->qb_groupby[] = $val = $this->protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200835
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000836 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000838 $this->qb_cache_groupby[] = $val;
839 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 }
841 }
842 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200843
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 return $this;
845 }
846
847 // --------------------------------------------------------------------
848
849 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 * Sets the HAVING value
851 *
852 * Separates multiple calls with AND
853 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 * @param string
855 * @param string
856 * @return object
857 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600858 public function having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 {
860 return $this->_having($key, $value, 'AND ', $escape);
861 }
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 // --------------------------------------------------------------------
864
865 /**
866 * Sets the OR HAVING value
867 *
868 * Separates multiple calls with OR
869 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 * @param string
871 * @param string
872 * @return object
873 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600874 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 {
876 return $this->_having($key, $value, 'OR ', $escape);
877 }
Barry Mienydd671972010-10-04 16:33:58 +0200878
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 // --------------------------------------------------------------------
880
881 /**
882 * Sets the HAVING values
883 *
884 * Called by having() or or_having()
885 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 * @param string
887 * @param string
888 * @return object
889 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600890 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 {
892 if ( ! is_array($key))
893 {
894 $key = array($key => $value);
895 }
Barry Mienydd671972010-10-04 16:33:58 +0200896
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 foreach ($key as $k => $v)
898 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000899 $prefix = (count($this->qb_having) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000900
901 if ($escape === TRUE)
902 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200903 $k = $this->protect_identifiers($k);
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 }
905
906 if ( ! $this->_has_operator($k))
907 {
908 $k .= ' = ';
909 }
910
911 if ($v != '')
912 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400913 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 }
Barry Mienydd671972010-10-04 16:33:58 +0200915
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000916 $this->qb_having[] = $prefix.$k.$v;
917 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000919 $this->qb_cache_having[] = $prefix.$k.$v;
920 $this->qb_cache_exists[] = 'having';
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 }
922 }
Barry Mienydd671972010-10-04 16:33:58 +0200923
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 return $this;
925 }
Barry Mienydd671972010-10-04 16:33:58 +0200926
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 // --------------------------------------------------------------------
928
929 /**
930 * Sets the ORDER BY value
931 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 * @param string
933 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100934 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 * @return object
936 */
pporlan2c685fb2011-12-22 12:15:25 +0100937 public function order_by($orderby, $direction = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200939 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 {
941 $orderby = ''; // Random results want or don't need a field name
942 $direction = $this->_random_keyword;
943 }
944 elseif (trim($direction) != '')
945 {
946 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
947 }
Barry Mienydd671972010-10-04 16:33:58 +0200948
949
Andrey Andreev24276a32012-01-08 02:44:38 +0200950 if ((strpos($orderby, ',') !== FALSE) && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 {
952 $temp = array();
953 foreach (explode(',', $orderby) as $part)
954 {
955 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000956 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200958 $part = $this->protect_identifiers(trim($part));
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 }
Barry Mienydd671972010-10-04 16:33:58 +0200960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 $temp[] = $part;
962 }
Barry Mienydd671972010-10-04 16:33:58 +0200963
964 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200966 elseif ($direction != $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 {
pporlan2c685fb2011-12-22 12:15:25 +0100968 if ($escape === TRUE)
969 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200970 $orderby = $this->protect_identifiers($orderby);
pporlan2c685fb2011-12-22 12:15:25 +0100971 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 }
Barry Mienydd671972010-10-04 16:33:58 +0200973
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000974 $this->qb_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200975
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000976 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000978 $this->qb_cache_orderby[] = $orderby_statement;
979 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 }
981
982 return $this;
983 }
Barry Mienydd671972010-10-04 16:33:58 +0200984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 // --------------------------------------------------------------------
986
987 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 * Sets the LIMIT value
989 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300990 * @param int the limit value
991 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 * @return object
993 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200994 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000996 $this->qb_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000997
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200998 if ( ! is_null($offset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001000 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 }
Barry Mienydd671972010-10-04 16:33:58 +02001002
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 return $this;
1004 }
Barry Mienydd671972010-10-04 16:33:58 +02001005
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 // --------------------------------------------------------------------
1007
1008 /**
1009 * Sets the OFFSET value
1010 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001011 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 * @return object
1013 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001014 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001016 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 return $this;
1018 }
Barry Mienydd671972010-10-04 16:33:58 +02001019
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 // --------------------------------------------------------------------
1021
1022 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001023 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 * @param mixed
1026 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001027 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 * @return object
1029 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001030 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 {
1032 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001033
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 if ( ! is_array($key))
1035 {
1036 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001037 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001038
1039 foreach ($key as $k => $v)
1040 {
1041 if ($escape === FALSE)
1042 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001043 $this->qb_set[$this->protect_identifiers($k)] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 }
1045 else
1046 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001047 $this->qb_set[$this->protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 }
1049 }
Barry Mienydd671972010-10-04 16:33:58 +02001050
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 return $this;
1052 }
WanWizard7219c072011-12-28 14:09:05 +01001053
Kyle Farris0c147b32011-08-26 02:29:31 -04001054 // --------------------------------------------------------------------
1055
1056 /**
1057 * Get SELECT query string
1058 *
1059 * Compiles a SELECT query string and returns the sql.
1060 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001061 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001062 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001063 * @return string
1064 */
WanWizard7219c072011-12-28 14:09:05 +01001065 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001066 {
1067 if ($table != '')
kylefarris0a3176b2011-08-26 02:37:52 -04001068 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001069 $this->_track_aliases($table);
1070 $this->from($table);
1071 }
WanWizard7219c072011-12-28 14:09:05 +01001072
Kyle Farris0c147b32011-08-26 02:29:31 -04001073 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001074
Kyle Farris0c147b32011-08-26 02:29:31 -04001075 if ($reset === TRUE)
1076 {
1077 $this->_reset_select();
1078 }
WanWizard7219c072011-12-28 14:09:05 +01001079
Kyle Farris0c147b32011-08-26 02:29:31 -04001080 return $select;
1081 }
WanWizard7219c072011-12-28 14:09:05 +01001082
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 // --------------------------------------------------------------------
1084
1085 /**
1086 * Get
1087 *
1088 * Compiles the select statement based on the other functions called
1089 * and runs the query
1090 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 * @param string the table
1092 * @param string the limit clause
1093 * @param string the offset clause
1094 * @return object
1095 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001096 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 {
1098 if ($table != '')
1099 {
1100 $this->_track_aliases($table);
1101 $this->from($table);
1102 }
Barry Mienydd671972010-10-04 16:33:58 +02001103
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 if ( ! is_null($limit))
1105 {
1106 $this->limit($limit, $offset);
1107 }
Barry Mienydd671972010-10-04 16:33:58 +02001108
Andrey Andreev24276a32012-01-08 02:44:38 +02001109 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 $this->_reset_select();
1111 return $result;
1112 }
1113
1114 /**
1115 * "Count All Results" query
1116 *
Barry Mienydd671972010-10-04 16:33:58 +02001117 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001118 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 * @param string
1121 * @return string
1122 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001123 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 {
1125 if ($table != '')
1126 {
1127 $this->_track_aliases($table);
1128 $this->from($table);
1129 }
Barry Mienydd671972010-10-04 16:33:58 +02001130
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001131 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001133
Purwandi1d160e72012-01-09 16:33:28 +07001134 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001136 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 }
1138
Purwandi1d160e72012-01-09 16:33:28 +07001139 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001140 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 // --------------------------------------------------------------------
1143
1144 /**
1145 * Get_Where
1146 *
1147 * Allows the where clause, limit and offset to be added directly
1148 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 * @param string the where clause
1150 * @param string the limit clause
1151 * @param string the offset clause
1152 * @return object
1153 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001154 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 {
1156 if ($table != '')
1157 {
1158 $this->from($table);
1159 }
1160
1161 if ( ! is_null($where))
1162 {
1163 $this->where($where);
1164 }
Barry Mienydd671972010-10-04 16:33:58 +02001165
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 if ( ! is_null($limit))
1167 {
1168 $this->limit($limit, $offset);
1169 }
Barry Mienydd671972010-10-04 16:33:58 +02001170
Andrey Andreev24276a32012-01-08 02:44:38 +02001171 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 $this->_reset_select();
1173 return $result;
1174 }
1175
1176 // --------------------------------------------------------------------
1177
1178 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001179 * Insert_Batch
1180 *
1181 * Compiles batch insert strings and runs the queries
1182 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001183 * @param string the table to retrieve the results from
1184 * @param array an associative array of insert values
1185 * @return object
1186 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001187 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001188 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001189 if ( ! is_null($set))
1190 {
1191 $this->set_insert_batch($set);
1192 }
Barry Mienydd671972010-10-04 16:33:58 +02001193
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001194 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001195 {
1196 if ($this->db_debug)
1197 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001198 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001199 return $this->display_error('db_must_use_set');
1200 }
1201 return FALSE;
1202 }
1203
1204 if ($table == '')
1205 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001206 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001207 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001208 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001209 }
Barry Mienydd671972010-10-04 16:33:58 +02001210
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001211 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001212 }
1213
1214 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001215 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001216 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001217 $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 -06001218 }
Barry Mienydd671972010-10-04 16:33:58 +02001219
Derek Jonesd10e8962010-03-02 17:10:36 -06001220 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001221 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001222 }
1223
1224 // --------------------------------------------------------------------
1225
1226 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001227 * Insert_batch statement
1228 *
1229 * Generates a platform-specific insert string from the supplied data.
1230 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001231 * @param string the table name
1232 * @param array the insert keys
1233 * @param array the insert values
1234 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001235 */
1236 protected function _insert_batch($table, $keys, $values)
1237 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001238 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001239 }
1240
1241 // --------------------------------------------------------------------
1242
1243 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001244 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001245 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001246 * @param mixed
1247 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001248 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001249 * @return object
1250 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001251 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001252 {
1253 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001254
Derek Jonesd10e8962010-03-02 17:10:36 -06001255 if ( ! is_array($key))
1256 {
1257 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001258 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001259
Iban Eguia3c0a4522012-04-15 13:30:44 +02001260 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001261 sort($keys);
1262
1263 foreach ($key as $row)
1264 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001265 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001266 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1267 {
1268 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001269 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001270 return;
1271 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001272
Barry Mienydd671972010-10-04 16:33:58 +02001273 ksort($row); // puts $row in the same order as our keys
1274
Derek Jonesd10e8962010-03-02 17:10:36 -06001275 if ($escape === FALSE)
1276 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001277 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001278 }
1279 else
1280 {
1281 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001282 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001283 {
Barry Mienydd671972010-10-04 16:33:58 +02001284 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001285 }
1286
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001287 $this->qb_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001288 }
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
1404 if ($table == '')
1405 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001406 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001407 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001408 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001409 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001410 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001411 else
1412 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001413 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001414 }
WanWizard7219c072011-12-28 14:09:05 +01001415
Kyle Farris0c147b32011-08-26 02:29:31 -04001416 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 }
Barry Mienydd671972010-10-04 16:33:58 +02001418
Phil Sturgeon9789f322011-07-15 15:14:05 -06001419 // --------------------------------------------------------------------
1420
1421 /**
1422 * Replace
1423 *
1424 * Compiles an replace into string and runs the query
1425 *
1426 * @param string the table to replace data into
1427 * @param array an associative array of insert values
1428 * @return object
1429 */
1430 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001431 {
1432 if ( ! is_null($set))
1433 {
1434 $this->set($set);
1435 }
Barry Mienydd671972010-10-04 16:33:58 +02001436
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001437 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001438 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001439 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001440 }
1441
1442 if ($table == '')
1443 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001444 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001445 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001446 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001447 }
Barry Mienydd671972010-10-04 16:33:58 +02001448
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001449 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001450 }
1451
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001452 $sql = $this->_replace($this->protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->qb_set), array_values($this->qb_set));
Jamie Rumbelow3b1355c2012-03-06 21:27:46 +00001453
Derek Jonesd10e8962010-03-02 17:10:36 -06001454 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001455 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001456 }
WanWizard7219c072011-12-28 14:09:05 +01001457
Kyle Farris0c147b32011-08-26 02:29:31 -04001458 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001459
Kyle Farris0c147b32011-08-26 02:29:31 -04001460 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001461 * Replace statement
1462 *
1463 * Generates a platform-specific replace string from the supplied data
1464 *
1465 * @param string the table name
1466 * @param array the insert keys
1467 * @param array the insert values
1468 * @return string
1469 */
1470 protected function _replace($table, $keys, $values)
1471 {
1472 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1473 }
1474
1475 // --------------------------------------------------------------------
1476
1477 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001478 * Get UPDATE query string
1479 *
1480 * Compiles an update query and returns the sql
1481 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001482 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001483 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001484 * @return string
1485 */
1486 public function get_compiled_update($table = '', $reset = TRUE)
1487 {
1488 // Combine any cached components with the current statements
1489 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001490
Kyle Farris0c147b32011-08-26 02:29:31 -04001491 if ($this->_validate_update($table) === FALSE)
1492 {
1493 return FALSE;
1494 }
WanWizard7219c072011-12-28 14:09:05 +01001495
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001496 $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 +01001497
Kyle Farris0c147b32011-08-26 02:29:31 -04001498 if ($reset === TRUE)
1499 {
1500 $this->_reset_write();
1501 }
WanWizard7219c072011-12-28 14:09:05 +01001502
Kyle Farris0c147b32011-08-26 02:29:31 -04001503 return $sql;
1504 }
WanWizard7219c072011-12-28 14:09:05 +01001505
Derek Allard2067d1a2008-11-13 22:59:24 +00001506 // --------------------------------------------------------------------
1507
1508 /**
1509 * Update
1510 *
1511 * Compiles an update string and runs the query
1512 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 * @param string the table to retrieve the results from
1514 * @param array an associative array of update values
1515 * @param mixed the where clause
1516 * @return object
1517 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001518 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001519 {
1520 // Combine any cached components with the current statements
1521 $this->_merge_cache();
1522
1523 if ( ! is_null($set))
1524 {
1525 $this->set($set);
1526 }
Barry Mienydd671972010-10-04 16:33:58 +02001527
Kyle Farris0c147b32011-08-26 02:29:31 -04001528 if ($this->_validate_update($table) === FALSE)
1529 {
1530 return FALSE;
1531 }
1532
1533 if ($where != NULL)
1534 {
1535 $this->where($where);
1536 }
1537
1538 if ($limit != NULL)
1539 {
1540 $this->limit($limit);
1541 }
1542
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001543 $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 +00001544
Kyle Farris0c147b32011-08-26 02:29:31 -04001545 $this->_reset_write();
1546 return $this->query($sql);
1547 }
WanWizard7219c072011-12-28 14:09:05 +01001548
Kyle Farris0c147b32011-08-26 02:29:31 -04001549 // --------------------------------------------------------------------
1550
1551 /**
Andrey Andreev975034d2012-04-05 15:09:55 +03001552 * Update statement
1553 *
1554 * Generates a platform-specific update string from the supplied data
1555 *
1556 * @param string the table name
1557 * @param array the update data
1558 * @param array the where clause
1559 * @param array the orderby clause
1560 * @param array the limit clause
Andrey Andreev00541ae2012-04-09 11:43:10 +03001561 * @param array the like clause
Andrey Andreev975034d2012-04-05 15:09:55 +03001562 * @return string
1563 */
Andrey Andreev00541ae2012-04-09 11:43:10 +03001564 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Andrey Andreev975034d2012-04-05 15:09:55 +03001565 {
1566 foreach ($values as $key => $val)
1567 {
1568 $valstr[] = $key.' = '.$val;
1569 }
1570
Andrey Andreev00541ae2012-04-09 11:43:10 +03001571 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
1572
1573 if ( ! empty($like))
1574 {
1575 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
1576 }
1577
Andrey Andreev975034d2012-04-05 15:09:55 +03001578 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev00541ae2012-04-09 11:43:10 +03001579 .$where
Andrey Andreev975034d2012-04-05 15:09:55 +03001580 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
1581 .($limit ? ' LIMIT '.$limit : '');
1582 }
1583
1584 // --------------------------------------------------------------------
1585
1586 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001587 * Validate Update
1588 *
1589 * This method is used by both update() and get_compiled_update() to
1590 * validate that data is actually being set and that a table has been
1591 * chosen to be update.
1592 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001593 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001594 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001595 */
1596 protected function _validate_update($table = '')
1597 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001598 if (count($this->qb_set) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001599 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001600 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001601 }
1602
1603 if ($table == '')
1604 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001605 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001606 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001607 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001608 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001609 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001610 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001611 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001612 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001613 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001614
1615 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001616 }
WanWizard7219c072011-12-28 14:09:05 +01001617
Derek Jonesd10e8962010-03-02 17:10:36 -06001618 // --------------------------------------------------------------------
1619
1620 /**
1621 * Update_Batch
1622 *
1623 * Compiles an update string and runs the query
1624 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001625 * @param string the table to retrieve the results from
1626 * @param array an associative array of update values
1627 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001628 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001629 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001630 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001631 {
1632 // Combine any cached components with the current statements
1633 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001634
Derek Jonesd10e8962010-03-02 17:10:36 -06001635 if (is_null($index))
1636 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001637 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001638 }
1639
1640 if ( ! is_null($set))
1641 {
1642 $this->set_update_batch($set, $index);
1643 }
1644
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001645 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001646 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001647 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001648 }
1649
1650 if ($table == '')
1651 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001652 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001653 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001654 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001655 }
Barry Mienydd671972010-10-04 16:33:58 +02001656
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001657 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001658 }
Barry Mienydd671972010-10-04 16:33:58 +02001659
Derek Jonesd10e8962010-03-02 17:10:36 -06001660 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001661 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001662 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001663 $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 -06001664 }
Barry Mienydd671972010-10-04 16:33:58 +02001665
Derek Jonesd10e8962010-03-02 17:10:36 -06001666 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001667 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001668 }
1669
1670 // --------------------------------------------------------------------
1671
1672 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001673 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001674 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001675 * @param array
1676 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001677 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001678 * @return object
1679 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001680 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001681 {
1682 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001683
Derek Jonesd10e8962010-03-02 17:10:36 -06001684 if ( ! is_array($key))
1685 {
1686 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001687 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001688
1689 foreach ($key as $k => $v)
1690 {
1691 $index_set = FALSE;
1692 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001693 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001694 {
1695 if ($k2 == $index)
1696 {
1697 $index_set = TRUE;
1698 }
1699 else
1700 {
1701 $not[] = $k.'-'.$v;
1702 }
1703
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001704 $clean[$this->protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001705 }
1706
1707 if ($index_set == FALSE)
1708 {
1709 return $this->display_error('db_batch_missing_index');
1710 }
1711
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001712 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001713 }
Barry Mienydd671972010-10-04 16:33:58 +02001714
Derek Jonesd10e8962010-03-02 17:10:36 -06001715 return $this;
1716 }
1717
Derek Allard2067d1a2008-11-13 22:59:24 +00001718 // --------------------------------------------------------------------
1719
1720 /**
1721 * Empty Table
1722 *
1723 * Compiles a delete string and runs "DELETE FROM table"
1724 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001725 * @param string the table to empty
1726 * @return object
1727 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001728 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001729 {
1730 if ($table == '')
1731 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001732 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001734 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001735 }
1736
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001737 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001738 }
1739 else
1740 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001741 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 }
1743
1744 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001745 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001746 return $this->query($sql);
1747 }
1748
1749 // --------------------------------------------------------------------
1750
1751 /**
1752 * Truncate
1753 *
1754 * Compiles a truncate string and runs the query
1755 * If the database does not support the truncate() command
1756 * This function maps to "DELETE FROM table"
1757 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001758 * @param string the table to truncate
1759 * @return object
1760 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001761 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001762 {
1763 if ($table == '')
1764 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001765 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001766 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001767 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001768 }
1769
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001770 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001771 }
1772 else
1773 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001774 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001775 }
1776
1777 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001778 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001779 return $this->query($sql);
1780 }
WanWizard7219c072011-12-28 14:09:05 +01001781
Kyle Farris0c147b32011-08-26 02:29:31 -04001782 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001783
Kyle Farris0c147b32011-08-26 02:29:31 -04001784 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001785 * Truncate statement
1786 *
1787 * Generates a platform-specific truncate string from the supplied data
1788 *
1789 * If the database does not support the truncate() command,
1790 * then this method maps to 'DELETE FROM table'
1791 *
1792 * @param string the table name
1793 * @return string
1794 */
1795 protected function _truncate($table)
1796 {
1797 return 'TRUNCATE '.$table;
1798 }
1799
1800 // --------------------------------------------------------------------
1801
1802 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001803 * Get DELETE query string
1804 *
1805 * Compiles a delete query string and returns the sql
1806 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001807 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001808 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001809 * @return string
1810 */
1811 public function get_compiled_delete($table = '', $reset = TRUE)
1812 {
1813 $this->return_delete_sql = TRUE;
1814 $sql = $this->delete($table, '', NULL, $reset);
1815 $this->return_delete_sql = FALSE;
1816 return $sql;
1817 }
WanWizard7219c072011-12-28 14:09:05 +01001818
Derek Allard2067d1a2008-11-13 22:59:24 +00001819 // --------------------------------------------------------------------
1820
1821 /**
1822 * Delete
1823 *
1824 * Compiles a delete string and runs the query
1825 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001826 * @param mixed the table(s) to delete from. String or array
1827 * @param mixed the where clause
1828 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001829 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001830 * @return object
1831 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001832 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001833 {
1834 // Combine any cached components with the current statements
1835 $this->_merge_cache();
1836
1837 if ($table == '')
1838 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001839 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001840 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001841 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001842 }
1843
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001844 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001845 }
1846 elseif (is_array($table))
1847 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001848 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001849 {
1850 $this->delete($single_table, $where, $limit, FALSE);
1851 }
1852
1853 $this->_reset_write();
1854 return;
1855 }
1856 else
1857 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001858 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001859 }
1860
1861 if ($where != '')
1862 {
1863 $this->where($where);
1864 }
1865
1866 if ($limit != NULL)
1867 {
1868 $this->limit($limit);
1869 }
1870
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001871 if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001872 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001873 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001874 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001875
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001876 $sql = $this->_delete($table, $this->qb_where, $this->qb_like, $this->qb_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001877 if ($reset_data)
1878 {
1879 $this->_reset_write();
1880 }
WanWizard7219c072011-12-28 14:09:05 +01001881
Andrey Andreev24276a32012-01-08 02:44:38 +02001882 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001883 }
WanWizard7219c072011-12-28 14:09:05 +01001884
Derek Allard2067d1a2008-11-13 22:59:24 +00001885 // --------------------------------------------------------------------
1886
1887 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001888 * Delete statement
1889 *
1890 * Generates a platform-specific delete string from the supplied data
1891 *
1892 * @param string the table name
1893 * @param array the where clause
1894 * @param array the like clause
1895 * @param string the limit clause
1896 * @return string
1897 */
1898 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
1899 {
1900 $conditions = array();
1901
1902 empty($where) OR $conditions[] = implode(' ', $where);
1903 empty($like) OR $conditions[] = implode(' ', $like);
1904
1905 return 'DELETE FROM '.$table
1906 .(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '')
1907 .($limit ? ' LIMIT '.$limit : '');
1908 }
1909
1910 // --------------------------------------------------------------------
1911
1912 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001913 * DB Prefix
1914 *
1915 * Prepends a database prefix if one exists in configuration
1916 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001917 * @param string the table
1918 * @return string
1919 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001920 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001921 {
1922 if ($table == '')
1923 {
1924 $this->display_error('db_table_name_required');
1925 }
1926
1927 return $this->dbprefix.$table;
1928 }
1929
1930 // --------------------------------------------------------------------
1931
1932 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001933 * Set DB Prefix
1934 *
1935 * Set's the DB Prefix to something new without needing to reconnect
1936 *
1937 * @param string the prefix
1938 * @return string
1939 */
1940 public function set_dbprefix($prefix = '')
1941 {
1942 return $this->dbprefix = $prefix;
1943 }
1944
1945 // --------------------------------------------------------------------
1946
1947 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001948 * Track Aliases
1949 *
1950 * Used to track SQL statements written with aliased tables.
1951 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001952 * @param string The table to inspect
1953 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001954 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001955 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001956 {
1957 if (is_array($table))
1958 {
1959 foreach ($table as $t)
1960 {
1961 $this->_track_aliases($t);
1962 }
1963 return;
1964 }
Barry Mienydd671972010-10-04 16:33:58 +02001965
Derek Jones37f4b9c2011-07-01 17:56:50 -05001966 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001967 // the string into discreet statements
1968 if (strpos($table, ',') !== FALSE)
1969 {
1970 return $this->_track_aliases(explode(',', $table));
1971 }
Barry Mienydd671972010-10-04 16:33:58 +02001972
Derek Allard2067d1a2008-11-13 22:59:24 +00001973 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001974 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001975 {
1976 // if the alias is written with the AS keyword, remove it
1977 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001978
Derek Allard2067d1a2008-11-13 22:59:24 +00001979 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001980 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001981
Derek Allard2067d1a2008-11-13 22:59:24 +00001982 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001983 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001984 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001985 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001986 }
1987 }
1988 }
WanWizard7219c072011-12-28 14:09:05 +01001989
Derek Allard2067d1a2008-11-13 22:59:24 +00001990 // --------------------------------------------------------------------
1991
1992 /**
1993 * Compile the SELECT statement
1994 *
1995 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001996 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001997 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001998 * @return string
1999 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002000 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002001 {
2002 // Combine any cached components with the current statements
2003 $this->_merge_cache();
2004
Derek Allard2067d1a2008-11-13 22:59:24 +00002005 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002006 if ($select_override !== FALSE)
2007 {
2008 $sql = $select_override;
2009 }
2010 else
2011 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002012 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002013
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002014 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002015 {
Barry Mienydd671972010-10-04 16:33:58 +02002016 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002017 }
2018 else
Barry Mienydd671972010-10-04 16:33:58 +02002019 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002020 // Cycle through the "select" portion of the query and prep each column name.
2021 // The reason we protect identifiers here rather then in the select() function
2022 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002023 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002024 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002025 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002026 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002027 }
Barry Mienydd671972010-10-04 16:33:58 +02002028
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002029 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002030 }
2031 }
2032
Derek Allard2067d1a2008-11-13 22:59:24 +00002033 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002034 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002035 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002036 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002037 }
2038
Derek Allard2067d1a2008-11-13 22:59:24 +00002039 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002040 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002041 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002042 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002043 }
2044
Derek Allard2067d1a2008-11-13 22:59:24 +00002045 // Write the "WHERE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002046 if (count($this->qb_where) > 0 OR count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002047 {
Greg Akere156c6e2011-04-20 16:03:04 -05002048 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00002049 }
2050
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002051 $sql .= implode("\n", $this->qb_where);
Derek Allard2067d1a2008-11-13 22:59:24 +00002052
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 // Write the "LIKE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002054 if (count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002055 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002056 if (count($this->qb_where) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002057 {
2058 $sql .= "\nAND ";
2059 }
2060
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002061 $sql .= implode("\n", $this->qb_like);
Derek Allard2067d1a2008-11-13 22:59:24 +00002062 }
2063
Derek Allard2067d1a2008-11-13 22:59:24 +00002064 // Write the "GROUP BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002065 if (count($this->qb_groupby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002066 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002067 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002068 }
2069
Derek Allard2067d1a2008-11-13 22:59:24 +00002070 // Write the "HAVING" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002071 if (count($this->qb_having) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002072 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002073 $sql .= "\nHAVING ".implode("\n", $this->qb_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00002074 }
2075
Derek Allard2067d1a2008-11-13 22:59:24 +00002076 // Write the "ORDER BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002077 if (count($this->qb_orderby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002078 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002079 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
2080 if ($this->qb_order !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002081 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002082 $sql .= ($this->qb_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02002083 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002084 }
2085
Derek Allard2067d1a2008-11-13 22:59:24 +00002086 // Write the "LIMIT" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002087 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002088 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002089 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00002090 }
2091
2092 return $sql;
2093 }
2094
2095 // --------------------------------------------------------------------
2096
2097 /**
2098 * Object to Array
2099 *
2100 * Takes an object as input and converts the class variables to array key/vals
2101 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002102 * @param object
2103 * @return array
2104 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002105 public function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002106 {
2107 if ( ! is_object($object))
2108 {
2109 return $object;
2110 }
Barry Mienydd671972010-10-04 16:33:58 +02002111
Derek Allard2067d1a2008-11-13 22:59:24 +00002112 $array = array();
2113 foreach (get_object_vars($object) as $key => $val)
2114 {
2115 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06002116 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002117 {
2118 $array[$key] = $val;
2119 }
2120 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002121
2122 return $array;
2123 }
Barry Mienydd671972010-10-04 16:33:58 +02002124
Derek Jonesd10e8962010-03-02 17:10:36 -06002125 // --------------------------------------------------------------------
2126
2127 /**
2128 * Object to Array
2129 *
2130 * Takes an object as input and converts the class variables to array key/vals
2131 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002132 * @param object
2133 * @return array
2134 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002135 public function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002136 {
2137 if ( ! is_object($object))
2138 {
2139 return $object;
2140 }
Barry Mienydd671972010-10-04 16:33:58 +02002141
Derek Jonesd10e8962010-03-02 17:10:36 -06002142 $array = array();
2143 $out = get_object_vars($object);
2144 $fields = array_keys($out);
2145
2146 foreach ($fields as $val)
2147 {
2148 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002149 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002150 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002151 $i = 0;
2152 foreach ($out[$val] as $data)
2153 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002154 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002155 }
2156 }
2157 }
2158
Derek Allard2067d1a2008-11-13 22:59:24 +00002159 return $array;
2160 }
Barry Mienydd671972010-10-04 16:33:58 +02002161
Derek Allard2067d1a2008-11-13 22:59:24 +00002162 // --------------------------------------------------------------------
2163
2164 /**
2165 * Start Cache
2166 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002167 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002168 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002169 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002170 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002171 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002172 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002173 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002174 }
2175
2176 // --------------------------------------------------------------------
2177
2178 /**
2179 * Stop Cache
2180 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002181 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002182 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002183 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002184 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002185 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002186 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002187 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002188 }
2189
2190 // --------------------------------------------------------------------
2191
2192 /**
2193 * Flush Cache
2194 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002195 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002196 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002197 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002198 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002199 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002200 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002201 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002202 'qb_cache_select' => array(),
2203 'qb_cache_from' => array(),
2204 'qb_cache_join' => array(),
2205 'qb_cache_where' => array(),
2206 'qb_cache_like' => array(),
2207 'qb_cache_groupby' => array(),
2208 'qb_cache_having' => array(),
2209 'qb_cache_orderby' => array(),
2210 'qb_cache_set' => array(),
2211 'qb_cache_exists' => array(),
2212 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002213 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002214 }
2215
2216 // --------------------------------------------------------------------
2217
2218 /**
2219 * Merge Cache
2220 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002221 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002222 * locally called ones.
2223 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002224 * @return void
2225 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002226 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002227 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002228 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002229 {
2230 return;
2231 }
2232
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002233 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002234 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002235 $qb_variable = 'qb_'.$val;
2236 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002237
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002238 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002239 {
2240 continue;
2241 }
2242
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002243 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002244 }
2245
2246 // If we are "protecting identifiers" we need to examine the "from"
2247 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002248 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002249 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002250 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002251 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002252
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002253 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002254 }
WanWizard7219c072011-12-28 14:09:05 +01002255
Kyle Farris0c147b32011-08-26 02:29:31 -04002256 // --------------------------------------------------------------------
2257
2258 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002259 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002260 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002261 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002262 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002263 * @return void
2264 */
2265 public function reset_query()
2266 {
2267 $this->_reset_select();
2268 $this->_reset_write();
2269 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002270
2271 // --------------------------------------------------------------------
2272
2273 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002274 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002275 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002276 * @param array An array of fields to reset
2277 * @return void
2278 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002279 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002280 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002281 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002282 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002283 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002284 {
2285 $this->$item = $default_value;
2286 }
2287 }
2288 }
2289
2290 // --------------------------------------------------------------------
2291
2292 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002293 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002294 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002295 * @return void
2296 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002297 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002298 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002299 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002300 'qb_select' => array(),
2301 'qb_from' => array(),
2302 'qb_join' => array(),
2303 'qb_where' => array(),
2304 'qb_like' => array(),
2305 'qb_groupby' => array(),
2306 'qb_having' => array(),
2307 'qb_orderby' => array(),
2308 'qb_wherein' => array(),
2309 'qb_aliased_tables' => array(),
2310 'qb_no_escape' => array(),
2311 'qb_distinct' => FALSE,
2312 'qb_limit' => FALSE,
2313 'qb_offset' => FALSE,
2314 'qb_order' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002315 )
2316 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002317 }
Barry Mienydd671972010-10-04 16:33:58 +02002318
Derek Allard2067d1a2008-11-13 22:59:24 +00002319 // --------------------------------------------------------------------
2320
2321 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002322 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002323 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002324 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002325 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002326 * @return void
2327 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002328 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002329 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002330 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002331 'qb_set' => array(),
2332 'qb_from' => array(),
2333 'qb_where' => array(),
2334 'qb_like' => array(),
2335 'qb_orderby' => array(),
2336 'qb_keys' => array(),
2337 'qb_limit' => FALSE,
2338 'qb_order' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002339 )
2340 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002341 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002342
Derek Allard2067d1a2008-11-13 22:59:24 +00002343}
2344
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002345/* End of file DB_query_builder.php */
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002346/* Location: ./system/database/DB_query_builder.php */