blob: 8fa67ea0633073c06f3dffe3487aadf9f2f2bf7a [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
Soesapto Joeni Hantorob01d96f2012-05-11 11:18:38 +0700435 if ( ! is_null($v))
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 {
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 }
Soesapto Joeni Hantorob01d96f2012-05-11 11:18:38 +0700442 else if (is_bool($v))
443 {
444 $v = ' '.($v ? 'TRUE' : 'FALSE');
445 }
WanWizard7219c072011-12-28 14:09:05 +0100446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 if ( ! $this->_has_operator($k))
448 {
Greg Akere156c6e2011-04-20 16:03:04 -0500449 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 }
451 }
452 else
453 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200454 $k = $this->protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
456
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000457 $this->qb_where[] = $prefix.$k.$v;
458 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000460 $this->qb_cache_where[] = $prefix.$k.$v;
461 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 }
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 return $this;
467 }
468
469 // --------------------------------------------------------------------
470
471 /**
472 * Where_in
473 *
474 * Generates a WHERE field IN ('item', 'item') SQL query joined with
475 * AND if appropriate
476 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 * @param string The field to search
478 * @param array The values searched on
479 * @return object
480 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600481 public function where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
483 return $this->_where_in($key, $values);
484 }
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 // --------------------------------------------------------------------
487
488 /**
489 * Where_in_or
490 *
491 * Generates a WHERE field IN ('item', 'item') SQL query joined with
492 * OR if appropriate
493 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 * @param string The field to search
495 * @param array The values searched on
496 * @return object
497 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600498 public function or_where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 {
500 return $this->_where_in($key, $values, FALSE, 'OR ');
501 }
502
503 // --------------------------------------------------------------------
504
505 /**
506 * Where_not_in
507 *
508 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
509 * with AND if appropriate
510 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 * @param string The field to search
512 * @param array The values searched on
513 * @return object
514 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600515 public function where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 {
517 return $this->_where_in($key, $values, TRUE);
518 }
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 // --------------------------------------------------------------------
521
522 /**
523 * Where_not_in_or
524 *
525 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
526 * with OR if appropriate
527 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 * @param string The field to search
529 * @param array The values searched on
530 * @return object
531 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600532 public function or_where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
534 return $this->_where_in($key, $values, TRUE, 'OR ');
535 }
536
537 // --------------------------------------------------------------------
538
539 /**
540 * Where_in
541 *
542 * Called by where_in, where_in_or, where_not_in, where_not_in_or
543 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 * @param string The field to search
545 * @param array The values searched on
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300546 * @param bool If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200547 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 * @return object
549 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600550 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 {
552 if ($key === NULL OR $values === NULL)
553 {
554 return;
555 }
Barry Mienydd671972010-10-04 16:33:58 +0200556
WanWizard7219c072011-12-28 14:09:05 +0100557 $type = $this->_group_get_type($type);
558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 if ( ! is_array($values))
560 {
561 $values = array($values);
562 }
Barry Mienydd671972010-10-04 16:33:58 +0200563
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 $not = ($not) ? ' NOT' : '';
565
566 foreach ($values as $value)
567 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000568 $this->qb_wherein[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 }
570
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000571 $prefix = (count($this->qb_where) === 0) ? '' : $type;
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000572 $this->qb_where[] = $where_in = $prefix.$this->protect_identifiers($key).$not.' IN ('.implode(', ', $this->qb_wherein).') ';
Barry Mienydd671972010-10-04 16:33:58 +0200573
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000574 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000576 $this->qb_cache_where[] = $where_in;
577 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 }
579
580 // reset the array for multiple calls
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000581 $this->qb_wherein = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 return $this;
583 }
Barry Mienydd671972010-10-04 16:33:58 +0200584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 // --------------------------------------------------------------------
586
587 /**
588 * Like
589 *
590 * Generates a %LIKE% portion of the query. Separates
591 * multiple calls with AND
592 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 * @param mixed
594 * @param mixed
595 * @return object
596 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600597 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
599 return $this->_like($field, $match, 'AND ', $side);
600 }
601
602 // --------------------------------------------------------------------
603
604 /**
605 * Not Like
606 *
607 * Generates a NOT LIKE portion of the query. Separates
608 * multiple calls with AND
609 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 * @param mixed
611 * @param mixed
612 * @return object
613 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600614 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 {
616 return $this->_like($field, $match, 'AND ', $side, 'NOT');
617 }
Barry Mienydd671972010-10-04 16:33:58 +0200618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 // --------------------------------------------------------------------
620
621 /**
622 * OR Like
623 *
624 * Generates a %LIKE% portion of the query. Separates
625 * multiple calls with OR
626 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 * @param mixed
628 * @param mixed
629 * @return object
630 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600631 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 {
633 return $this->_like($field, $match, 'OR ', $side);
634 }
635
636 // --------------------------------------------------------------------
637
638 /**
639 * OR Not Like
640 *
641 * Generates a NOT LIKE portion of the query. Separates
642 * multiple calls with OR
643 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 * @param mixed
645 * @param mixed
646 * @return object
647 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600648 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 {
650 return $this->_like($field, $match, 'OR ', $side, 'NOT');
651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 // --------------------------------------------------------------------
654
655 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 * Like
657 *
658 * Called by like() or orlike()
659 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 * @param mixed
661 * @param mixed
662 * @param string
663 * @return object
664 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600665 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 {
WanWizard7219c072011-12-28 14:09:05 +0100667 $type = $this->_group_get_type($type);
668
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 if ( ! is_array($field))
670 {
671 $field = array($field => $match);
672 }
Barry Mienydd671972010-10-04 16:33:58 +0200673
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 foreach ($field as $k => $v)
675 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000676 $k = $this->protect_identifiers($k);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000677 $prefix = (count($this->qb_like) === 0) ? '' : $type;
Derek Jonese4ed5832009-02-20 21:44:59 +0000678 $v = $this->escape_like_str($v);
Kyle Farris0c147b32011-08-26 02:29:31 -0400679
Andrey Andreev24276a32012-01-08 02:44:38 +0200680 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400681 {
682 $like_statement = $prefix." $k $not LIKE '{$v}'";
683 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200684 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 {
686 $like_statement = $prefix." $k $not LIKE '%{$v}'";
687 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200688 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 {
690 $like_statement = $prefix." $k $not LIKE '{$v}%'";
691 }
692 else
693 {
694 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
695 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600696
Derek Jonese4ed5832009-02-20 21:44:59 +0000697 // some platforms require an escape sequence definition for LIKE wildcards
698 if ($this->_like_escape_str != '')
699 {
Greg Aker0d424892010-01-26 02:14:44 +0000700 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000701 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600702
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000703 $this->qb_like[] = $like_statement;
704 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000706 $this->qb_cache_like[] = $like_statement;
707 $this->qb_cache_exists[] = 'like';
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 }
Barry Mienydd671972010-10-04 16:33:58 +0200709
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200711
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 return $this;
713 }
Barry Mienydd671972010-10-04 16:33:58 +0200714
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 // --------------------------------------------------------------------
716
717 /**
WanWizard7219c072011-12-28 14:09:05 +0100718 * Starts a query group.
719 *
720 * @param string (Internal use only)
721 * @param string (Internal use only)
722 * @return object
723 */
724 public function group_start($not = '', $type = 'AND ')
725 {
726 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100727
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000728 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100729 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000730 $this->qb_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (';
WanWizard7219c072011-12-28 14:09:05 +0100731
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000732 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100733 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000734 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100735 }
736
737 return $this;
738 }
739
740 // --------------------------------------------------------------------
741
742 /**
743 * Starts a query group, but ORs the group
744 *
745 * @return object
746 */
747 public function or_group_start()
748 {
749 return $this->group_start('', 'OR ');
750 }
751
752 // --------------------------------------------------------------------
753
754 /**
755 * Starts a query group, but NOTs the group
756 *
757 * @return object
758 */
759 public function not_group_start()
760 {
761 return $this->group_start('NOT ', 'AND ');
762 }
763
764 // --------------------------------------------------------------------
765
766 /**
767 * Starts a query group, but OR NOTs the group
768 *
769 * @return object
770 */
771 public function or_not_group_start()
772 {
773 return $this->group_start('NOT ', 'OR ');
774 }
775
776 // --------------------------------------------------------------------
777
778 /**
779 * Ends a query group
780 *
781 * @return object
782 */
783 public function group_end()
784 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000785 $this->qb_where_group_started = FALSE;
786 $this->qb_where[] = $value = str_repeat(' ', $this->qb_where_group_count--) . ')';
WanWizard7219c072011-12-28 14:09:05 +0100787
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000788 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100789 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000790 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100791 }
792
WanWizard7219c072011-12-28 14:09:05 +0100793 return $this;
794 }
795
796 // --------------------------------------------------------------------
797
798 /**
799 * Group_get_type
800 *
801 * Called by group_start(), _like(), _where() and _where_in()
802 *
803 * @param string
804 * @return string
805 */
806 protected function _group_get_type($type)
807 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000808 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100809 {
810 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000811 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100812 }
813
814 return $type;
815 }
816
817 // --------------------------------------------------------------------
818
819 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 * GROUP BY
821 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 * @param string
823 * @return object
824 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600825 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 {
827 if (is_string($by))
828 {
829 $by = explode(',', $by);
830 }
Barry Mienydd671972010-10-04 16:33:58 +0200831
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 foreach ($by as $val)
833 {
834 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200835
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 if ($val != '')
837 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000838 $this->qb_groupby[] = $val = $this->protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200839
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000840 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000842 $this->qb_cache_groupby[] = $val;
843 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 }
845 }
846 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200847
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 return $this;
849 }
850
851 // --------------------------------------------------------------------
852
853 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 * Sets the HAVING value
855 *
856 * Separates multiple calls with AND
857 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 * @param string
859 * @param string
860 * @return object
861 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600862 public function having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 {
864 return $this->_having($key, $value, 'AND ', $escape);
865 }
Barry Mienydd671972010-10-04 16:33:58 +0200866
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 // --------------------------------------------------------------------
868
869 /**
870 * Sets the OR HAVING value
871 *
872 * Separates multiple calls with OR
873 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 * @param string
875 * @param string
876 * @return object
877 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600878 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
880 return $this->_having($key, $value, 'OR ', $escape);
881 }
Barry Mienydd671972010-10-04 16:33:58 +0200882
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 // --------------------------------------------------------------------
884
885 /**
886 * Sets the HAVING values
887 *
888 * Called by having() or or_having()
889 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 * @param string
891 * @param string
892 * @return object
893 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600894 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 {
896 if ( ! is_array($key))
897 {
898 $key = array($key => $value);
899 }
Barry Mienydd671972010-10-04 16:33:58 +0200900
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 foreach ($key as $k => $v)
902 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000903 $prefix = (count($this->qb_having) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000904
905 if ($escape === TRUE)
906 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200907 $k = $this->protect_identifiers($k);
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 }
909
910 if ( ! $this->_has_operator($k))
911 {
912 $k .= ' = ';
913 }
914
915 if ($v != '')
916 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400917 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 }
Barry Mienydd671972010-10-04 16:33:58 +0200919
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000920 $this->qb_having[] = $prefix.$k.$v;
921 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000923 $this->qb_cache_having[] = $prefix.$k.$v;
924 $this->qb_cache_exists[] = 'having';
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 }
926 }
Barry Mienydd671972010-10-04 16:33:58 +0200927
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 return $this;
929 }
Barry Mienydd671972010-10-04 16:33:58 +0200930
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 // --------------------------------------------------------------------
932
933 /**
934 * Sets the ORDER BY value
935 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 * @param string
937 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100938 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 * @return object
940 */
pporlan2c685fb2011-12-22 12:15:25 +0100941 public function order_by($orderby, $direction = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200943 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 {
945 $orderby = ''; // Random results want or don't need a field name
946 $direction = $this->_random_keyword;
947 }
948 elseif (trim($direction) != '')
949 {
950 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
951 }
Barry Mienydd671972010-10-04 16:33:58 +0200952
953
Andrey Andreev24276a32012-01-08 02:44:38 +0200954 if ((strpos($orderby, ',') !== FALSE) && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 {
956 $temp = array();
957 foreach (explode(',', $orderby) as $part)
958 {
959 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000960 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200962 $part = $this->protect_identifiers(trim($part));
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 }
Barry Mienydd671972010-10-04 16:33:58 +0200964
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 $temp[] = $part;
966 }
Barry Mienydd671972010-10-04 16:33:58 +0200967
968 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200970 elseif ($direction != $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 {
pporlan2c685fb2011-12-22 12:15:25 +0100972 if ($escape === TRUE)
973 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200974 $orderby = $this->protect_identifiers($orderby);
pporlan2c685fb2011-12-22 12:15:25 +0100975 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 }
Barry Mienydd671972010-10-04 16:33:58 +0200977
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000978 $this->qb_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200979
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000980 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000982 $this->qb_cache_orderby[] = $orderby_statement;
983 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 }
985
986 return $this;
987 }
Barry Mienydd671972010-10-04 16:33:58 +0200988
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 // --------------------------------------------------------------------
990
991 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 * Sets the LIMIT value
993 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300994 * @param int the limit value
995 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 * @return object
997 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200998 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001000 $this->qb_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +00001001
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +02001002 if ( ! is_null($offset))
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001004 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 }
Barry Mienydd671972010-10-04 16:33:58 +02001006
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 return $this;
1008 }
Barry Mienydd671972010-10-04 16:33:58 +02001009
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 // --------------------------------------------------------------------
1011
1012 /**
1013 * Sets the OFFSET value
1014 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001015 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 * @return object
1017 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001018 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001020 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 return $this;
1022 }
Barry Mienydd671972010-10-04 16:33:58 +02001023
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 // --------------------------------------------------------------------
1025
1026 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001027 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 * @param mixed
1030 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001031 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 * @return object
1033 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001034 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 {
1036 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001037
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 if ( ! is_array($key))
1039 {
1040 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001041 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001042
1043 foreach ($key as $k => $v)
1044 {
1045 if ($escape === FALSE)
1046 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001047 $this->qb_set[$this->protect_identifiers($k)] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 }
1049 else
1050 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001051 $this->qb_set[$this->protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 }
1053 }
Barry Mienydd671972010-10-04 16:33:58 +02001054
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 return $this;
1056 }
WanWizard7219c072011-12-28 14:09:05 +01001057
Kyle Farris0c147b32011-08-26 02:29:31 -04001058 // --------------------------------------------------------------------
1059
1060 /**
1061 * Get SELECT query string
1062 *
1063 * Compiles a SELECT query string and returns the sql.
1064 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001065 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001066 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001067 * @return string
1068 */
WanWizard7219c072011-12-28 14:09:05 +01001069 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001070 {
1071 if ($table != '')
kylefarris0a3176b2011-08-26 02:37:52 -04001072 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001073 $this->_track_aliases($table);
1074 $this->from($table);
1075 }
WanWizard7219c072011-12-28 14:09:05 +01001076
Kyle Farris0c147b32011-08-26 02:29:31 -04001077 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001078
Kyle Farris0c147b32011-08-26 02:29:31 -04001079 if ($reset === TRUE)
1080 {
1081 $this->_reset_select();
1082 }
WanWizard7219c072011-12-28 14:09:05 +01001083
Kyle Farris0c147b32011-08-26 02:29:31 -04001084 return $select;
1085 }
WanWizard7219c072011-12-28 14:09:05 +01001086
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 // --------------------------------------------------------------------
1088
1089 /**
1090 * Get
1091 *
1092 * Compiles the select statement based on the other functions called
1093 * and runs the query
1094 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 * @param string the table
1096 * @param string the limit clause
1097 * @param string the offset clause
1098 * @return object
1099 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001100 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 {
1102 if ($table != '')
1103 {
1104 $this->_track_aliases($table);
1105 $this->from($table);
1106 }
Barry Mienydd671972010-10-04 16:33:58 +02001107
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 if ( ! is_null($limit))
1109 {
1110 $this->limit($limit, $offset);
1111 }
Barry Mienydd671972010-10-04 16:33:58 +02001112
Andrey Andreev24276a32012-01-08 02:44:38 +02001113 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 $this->_reset_select();
1115 return $result;
1116 }
1117
1118 /**
1119 * "Count All Results" query
1120 *
Barry Mienydd671972010-10-04 16:33:58 +02001121 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001122 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 * @param string
1125 * @return string
1126 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001127 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 {
1129 if ($table != '')
1130 {
1131 $this->_track_aliases($table);
1132 $this->from($table);
1133 }
Barry Mienydd671972010-10-04 16:33:58 +02001134
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001135 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001137
Purwandi1d160e72012-01-09 16:33:28 +07001138 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001140 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 }
1142
Purwandi1d160e72012-01-09 16:33:28 +07001143 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001144 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 // --------------------------------------------------------------------
1147
1148 /**
1149 * Get_Where
1150 *
1151 * Allows the where clause, limit and offset to be added directly
1152 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 * @param string the where clause
1154 * @param string the limit clause
1155 * @param string the offset clause
1156 * @return object
1157 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001158 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 {
1160 if ($table != '')
1161 {
1162 $this->from($table);
1163 }
1164
1165 if ( ! is_null($where))
1166 {
1167 $this->where($where);
1168 }
Barry Mienydd671972010-10-04 16:33:58 +02001169
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 if ( ! is_null($limit))
1171 {
1172 $this->limit($limit, $offset);
1173 }
Barry Mienydd671972010-10-04 16:33:58 +02001174
Andrey Andreev24276a32012-01-08 02:44:38 +02001175 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 $this->_reset_select();
1177 return $result;
1178 }
1179
1180 // --------------------------------------------------------------------
1181
1182 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001183 * Insert_Batch
1184 *
1185 * Compiles batch insert strings and runs the queries
1186 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001187 * @param string the table to retrieve the results from
1188 * @param array an associative array of insert values
1189 * @return object
1190 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001191 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001192 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001193 if ( ! is_null($set))
1194 {
1195 $this->set_insert_batch($set);
1196 }
Barry Mienydd671972010-10-04 16:33:58 +02001197
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001198 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001199 {
1200 if ($this->db_debug)
1201 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001202 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001203 return $this->display_error('db_must_use_set');
1204 }
1205 return FALSE;
1206 }
1207
1208 if ($table == '')
1209 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001210 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001211 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001212 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001213 }
Barry Mienydd671972010-10-04 16:33:58 +02001214
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001215 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001216 }
1217
1218 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001219 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001220 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001221 $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 -06001222 }
Barry Mienydd671972010-10-04 16:33:58 +02001223
Derek Jonesd10e8962010-03-02 17:10:36 -06001224 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001225 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001226 }
1227
1228 // --------------------------------------------------------------------
1229
1230 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001231 * Insert_batch statement
1232 *
1233 * Generates a platform-specific insert string from the supplied data.
1234 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001235 * @param string the table name
1236 * @param array the insert keys
1237 * @param array the insert values
1238 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001239 */
1240 protected function _insert_batch($table, $keys, $values)
1241 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001242 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001243 }
1244
1245 // --------------------------------------------------------------------
1246
1247 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001248 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001249 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001250 * @param mixed
1251 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001252 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001253 * @return object
1254 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001255 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001256 {
1257 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001258
Derek Jonesd10e8962010-03-02 17:10:36 -06001259 if ( ! is_array($key))
1260 {
1261 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001262 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001263
Iban Eguia3c0a4522012-04-15 13:30:44 +02001264 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001265 sort($keys);
1266
1267 foreach ($key as $row)
1268 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001269 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001270 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1271 {
1272 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001273 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001274 return;
1275 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001276
Barry Mienydd671972010-10-04 16:33:58 +02001277 ksort($row); // puts $row in the same order as our keys
1278
Derek Jonesd10e8962010-03-02 17:10:36 -06001279 if ($escape === FALSE)
1280 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001281 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001282 }
1283 else
1284 {
1285 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001286 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001287 {
Barry Mienydd671972010-10-04 16:33:58 +02001288 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001289 }
1290
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001291 $this->qb_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001292 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001293 }
1294
1295 foreach ($keys as $k)
1296 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001297 $this->qb_keys[] = $this->protect_identifiers($k);
Derek Jonesd10e8962010-03-02 17:10:36 -06001298 }
Barry Mienydd671972010-10-04 16:33:58 +02001299
Derek Jonesd10e8962010-03-02 17:10:36 -06001300 return $this;
1301 }
WanWizard7219c072011-12-28 14:09:05 +01001302
Kyle Farris0c147b32011-08-26 02:29:31 -04001303 // --------------------------------------------------------------------
1304
1305 /**
1306 * Get INSERT query string
1307 *
1308 * Compiles an insert query and returns the sql
1309 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001310 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001311 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001312 * @return string
1313 */
1314 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001315 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001316 if ($this->_validate_insert($table) === FALSE)
1317 {
1318 return FALSE;
1319 }
WanWizard7219c072011-12-28 14:09:05 +01001320
Kyle Farris76116012011-08-31 11:17:48 -04001321 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001322 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001323 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001324 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001325 array_keys($this->qb_set),
1326 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001327 );
WanWizard7219c072011-12-28 14:09:05 +01001328
Kyle Farris0c147b32011-08-26 02:29:31 -04001329 if ($reset === TRUE)
1330 {
1331 $this->_reset_write();
1332 }
WanWizard7219c072011-12-28 14:09:05 +01001333
Kyle Farris0c147b32011-08-26 02:29:31 -04001334 return $sql;
1335 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001336
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 // --------------------------------------------------------------------
1338
1339 /**
1340 * Insert
1341 *
1342 * Compiles an insert string and runs the query
1343 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001344 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 * @param array an associative array of insert values
1346 * @return object
1347 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001348 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001349 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 if ( ! is_null($set))
1351 {
1352 $this->set($set);
1353 }
WanWizard7219c072011-12-28 14:09:05 +01001354
Kyle Farris0c147b32011-08-26 02:29:31 -04001355 if ($this->_validate_insert($table) === FALSE)
1356 {
1357 return FALSE;
1358 }
WanWizard7219c072011-12-28 14:09:05 +01001359
Kyle Farris76116012011-08-31 11:17:48 -04001360 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001361 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001362 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001363 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001364 array_keys($this->qb_set),
1365 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001366 );
Barry Mienydd671972010-10-04 16:33:58 +02001367
Kyle Farris0c147b32011-08-26 02:29:31 -04001368 $this->_reset_write();
1369 return $this->query($sql);
1370 }
WanWizard7219c072011-12-28 14:09:05 +01001371
Kyle Farris0c147b32011-08-26 02:29:31 -04001372 // --------------------------------------------------------------------
1373
1374 /**
Andrey Andreev65d537c2012-04-05 14:11:41 +03001375 * Insert statement
1376 *
1377 * Generates a platform-specific insert string from the supplied data
1378 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001379 * @param string the table name
1380 * @param array the insert keys
1381 * @param array the insert values
1382 * @return string
Andrey Andreev65d537c2012-04-05 14:11:41 +03001383 */
1384 protected function _insert($table, $keys, $values)
1385 {
1386 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1387 }
1388
1389 // --------------------------------------------------------------------
1390
1391 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001392 * Validate Insert
1393 *
1394 * This method is used by both insert() and get_compiled_insert() to
1395 * validate that the there data is actually being set and that table
1396 * has been chosen to be inserted into.
1397 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001398 * @param string the table to insert data into
1399 * @return string
1400 */
WanWizard7219c072011-12-28 14:09:05 +01001401 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001402 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001403 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001404 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001405 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001406 }
1407
1408 if ($table == '')
1409 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001410 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001411 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001412 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001414 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001415 else
1416 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001417 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001418 }
WanWizard7219c072011-12-28 14:09:05 +01001419
Kyle Farris0c147b32011-08-26 02:29:31 -04001420 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001421 }
Barry Mienydd671972010-10-04 16:33:58 +02001422
Phil Sturgeon9789f322011-07-15 15:14:05 -06001423 // --------------------------------------------------------------------
1424
1425 /**
1426 * Replace
1427 *
1428 * Compiles an replace into string and runs the query
1429 *
1430 * @param string the table to replace data into
1431 * @param array an associative array of insert values
1432 * @return object
1433 */
1434 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001435 {
1436 if ( ! is_null($set))
1437 {
1438 $this->set($set);
1439 }
Barry Mienydd671972010-10-04 16:33:58 +02001440
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001441 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001442 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001443 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001444 }
1445
1446 if ($table == '')
1447 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001448 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001449 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001450 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001451 }
Barry Mienydd671972010-10-04 16:33:58 +02001452
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001453 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001454 }
1455
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001456 $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 +00001457
Derek Jonesd10e8962010-03-02 17:10:36 -06001458 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001459 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001460 }
WanWizard7219c072011-12-28 14:09:05 +01001461
Kyle Farris0c147b32011-08-26 02:29:31 -04001462 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001463
Kyle Farris0c147b32011-08-26 02:29:31 -04001464 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001465 * Replace statement
1466 *
1467 * Generates a platform-specific replace string from the supplied data
1468 *
1469 * @param string the table name
1470 * @param array the insert keys
1471 * @param array the insert values
1472 * @return string
1473 */
1474 protected function _replace($table, $keys, $values)
1475 {
1476 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1477 }
1478
1479 // --------------------------------------------------------------------
1480
1481 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001482 * Get UPDATE query string
1483 *
1484 * Compiles an update query and returns the sql
1485 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001486 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001487 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001488 * @return string
1489 */
1490 public function get_compiled_update($table = '', $reset = TRUE)
1491 {
1492 // Combine any cached components with the current statements
1493 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001494
Kyle Farris0c147b32011-08-26 02:29:31 -04001495 if ($this->_validate_update($table) === FALSE)
1496 {
1497 return FALSE;
1498 }
WanWizard7219c072011-12-28 14:09:05 +01001499
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001500 $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 +01001501
Kyle Farris0c147b32011-08-26 02:29:31 -04001502 if ($reset === TRUE)
1503 {
1504 $this->_reset_write();
1505 }
WanWizard7219c072011-12-28 14:09:05 +01001506
Kyle Farris0c147b32011-08-26 02:29:31 -04001507 return $sql;
1508 }
WanWizard7219c072011-12-28 14:09:05 +01001509
Derek Allard2067d1a2008-11-13 22:59:24 +00001510 // --------------------------------------------------------------------
1511
1512 /**
1513 * Update
1514 *
1515 * Compiles an update string and runs the query
1516 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001517 * @param string the table to retrieve the results from
1518 * @param array an associative array of update values
1519 * @param mixed the where clause
1520 * @return object
1521 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001522 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 {
1524 // Combine any cached components with the current statements
1525 $this->_merge_cache();
1526
1527 if ( ! is_null($set))
1528 {
1529 $this->set($set);
1530 }
Barry Mienydd671972010-10-04 16:33:58 +02001531
Kyle Farris0c147b32011-08-26 02:29:31 -04001532 if ($this->_validate_update($table) === FALSE)
1533 {
1534 return FALSE;
1535 }
1536
1537 if ($where != NULL)
1538 {
1539 $this->where($where);
1540 }
1541
1542 if ($limit != NULL)
1543 {
1544 $this->limit($limit);
1545 }
1546
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001547 $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 +00001548
Kyle Farris0c147b32011-08-26 02:29:31 -04001549 $this->_reset_write();
1550 return $this->query($sql);
1551 }
WanWizard7219c072011-12-28 14:09:05 +01001552
Kyle Farris0c147b32011-08-26 02:29:31 -04001553 // --------------------------------------------------------------------
1554
1555 /**
Andrey Andreev975034d2012-04-05 15:09:55 +03001556 * Update statement
1557 *
1558 * Generates a platform-specific update string from the supplied data
1559 *
1560 * @param string the table name
1561 * @param array the update data
1562 * @param array the where clause
1563 * @param array the orderby clause
1564 * @param array the limit clause
Andrey Andreev00541ae2012-04-09 11:43:10 +03001565 * @param array the like clause
Andrey Andreev975034d2012-04-05 15:09:55 +03001566 * @return string
1567 */
Andrey Andreev00541ae2012-04-09 11:43:10 +03001568 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Andrey Andreev975034d2012-04-05 15:09:55 +03001569 {
1570 foreach ($values as $key => $val)
1571 {
1572 $valstr[] = $key.' = '.$val;
1573 }
1574
Andrey Andreev00541ae2012-04-09 11:43:10 +03001575 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
1576
1577 if ( ! empty($like))
1578 {
1579 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
1580 }
1581
Andrey Andreev975034d2012-04-05 15:09:55 +03001582 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev00541ae2012-04-09 11:43:10 +03001583 .$where
Andrey Andreev975034d2012-04-05 15:09:55 +03001584 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
1585 .($limit ? ' LIMIT '.$limit : '');
1586 }
1587
1588 // --------------------------------------------------------------------
1589
1590 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001591 * Validate Update
1592 *
1593 * This method is used by both update() and get_compiled_update() to
1594 * validate that data is actually being set and that a table has been
1595 * chosen to be update.
1596 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001597 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001598 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001599 */
1600 protected function _validate_update($table = '')
1601 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001602 if (count($this->qb_set) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001603 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001604 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001605 }
1606
1607 if ($table == '')
1608 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001609 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001610 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001611 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001612 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001613 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001614 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001615 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001616 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001617 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001618
1619 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001620 }
WanWizard7219c072011-12-28 14:09:05 +01001621
Derek Jonesd10e8962010-03-02 17:10:36 -06001622 // --------------------------------------------------------------------
1623
1624 /**
1625 * Update_Batch
1626 *
1627 * Compiles an update string and runs the query
1628 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001629 * @param string the table to retrieve the results from
1630 * @param array an associative array of update values
1631 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001632 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001633 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001634 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001635 {
1636 // Combine any cached components with the current statements
1637 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001638
Derek Jonesd10e8962010-03-02 17:10:36 -06001639 if (is_null($index))
1640 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001641 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001642 }
1643
1644 if ( ! is_null($set))
1645 {
1646 $this->set_update_batch($set, $index);
1647 }
1648
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001649 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001650 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001651 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001652 }
1653
1654 if ($table == '')
1655 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001656 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001657 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001658 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001659 }
Barry Mienydd671972010-10-04 16:33:58 +02001660
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001661 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001662 }
Barry Mienydd671972010-10-04 16:33:58 +02001663
Derek Jonesd10e8962010-03-02 17:10:36 -06001664 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001665 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001666 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001667 $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 -06001668 }
Barry Mienydd671972010-10-04 16:33:58 +02001669
Derek Jonesd10e8962010-03-02 17:10:36 -06001670 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001671 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001672 }
1673
1674 // --------------------------------------------------------------------
1675
1676 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001677 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001678 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001679 * @param array
1680 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001681 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001682 * @return object
1683 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001684 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001685 {
1686 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001687
Derek Jonesd10e8962010-03-02 17:10:36 -06001688 if ( ! is_array($key))
1689 {
1690 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001691 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001692
1693 foreach ($key as $k => $v)
1694 {
1695 $index_set = FALSE;
1696 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001697 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001698 {
1699 if ($k2 == $index)
1700 {
1701 $index_set = TRUE;
1702 }
1703 else
1704 {
1705 $not[] = $k.'-'.$v;
1706 }
1707
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001708 $clean[$this->protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001709 }
1710
1711 if ($index_set == FALSE)
1712 {
1713 return $this->display_error('db_batch_missing_index');
1714 }
1715
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001716 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001717 }
Barry Mienydd671972010-10-04 16:33:58 +02001718
Derek Jonesd10e8962010-03-02 17:10:36 -06001719 return $this;
1720 }
1721
Derek Allard2067d1a2008-11-13 22:59:24 +00001722 // --------------------------------------------------------------------
1723
1724 /**
1725 * Empty Table
1726 *
1727 * Compiles a delete string and runs "DELETE FROM table"
1728 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001729 * @param string the table to empty
1730 * @return object
1731 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001732 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 {
1734 if ($table == '')
1735 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001736 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001737 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001738 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001739 }
1740
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001741 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 }
1743 else
1744 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001745 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001746 }
1747
1748 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001750 return $this->query($sql);
1751 }
1752
1753 // --------------------------------------------------------------------
1754
1755 /**
1756 * Truncate
1757 *
1758 * Compiles a truncate string and runs the query
1759 * If the database does not support the truncate() command
1760 * This function maps to "DELETE FROM table"
1761 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001762 * @param string the table to truncate
1763 * @return object
1764 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001765 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001766 {
1767 if ($table == '')
1768 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001769 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001770 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001771 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 }
1773
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001774 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001775 }
1776 else
1777 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001778 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001779 }
1780
1781 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001782 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001783 return $this->query($sql);
1784 }
WanWizard7219c072011-12-28 14:09:05 +01001785
Kyle Farris0c147b32011-08-26 02:29:31 -04001786 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001787
Kyle Farris0c147b32011-08-26 02:29:31 -04001788 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001789 * Truncate statement
1790 *
1791 * Generates a platform-specific truncate string from the supplied data
1792 *
1793 * If the database does not support the truncate() command,
1794 * then this method maps to 'DELETE FROM table'
1795 *
1796 * @param string the table name
1797 * @return string
1798 */
1799 protected function _truncate($table)
1800 {
1801 return 'TRUNCATE '.$table;
1802 }
1803
1804 // --------------------------------------------------------------------
1805
1806 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001807 * Get DELETE query string
1808 *
1809 * Compiles a delete query string and returns the sql
1810 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001811 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001812 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001813 * @return string
1814 */
1815 public function get_compiled_delete($table = '', $reset = TRUE)
1816 {
1817 $this->return_delete_sql = TRUE;
1818 $sql = $this->delete($table, '', NULL, $reset);
1819 $this->return_delete_sql = FALSE;
1820 return $sql;
1821 }
WanWizard7219c072011-12-28 14:09:05 +01001822
Derek Allard2067d1a2008-11-13 22:59:24 +00001823 // --------------------------------------------------------------------
1824
1825 /**
1826 * Delete
1827 *
1828 * Compiles a delete string and runs the query
1829 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001830 * @param mixed the table(s) to delete from. String or array
1831 * @param mixed the where clause
1832 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001833 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001834 * @return object
1835 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001836 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001837 {
1838 // Combine any cached components with the current statements
1839 $this->_merge_cache();
1840
1841 if ($table == '')
1842 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001843 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001844 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001845 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001846 }
1847
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001848 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001849 }
1850 elseif (is_array($table))
1851 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001852 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001853 {
1854 $this->delete($single_table, $where, $limit, FALSE);
1855 }
1856
1857 $this->_reset_write();
1858 return;
1859 }
1860 else
1861 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001862 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001863 }
1864
1865 if ($where != '')
1866 {
1867 $this->where($where);
1868 }
1869
1870 if ($limit != NULL)
1871 {
1872 $this->limit($limit);
1873 }
1874
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001875 if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001876 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001877 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001878 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001879
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001880 $sql = $this->_delete($table, $this->qb_where, $this->qb_like, $this->qb_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001881 if ($reset_data)
1882 {
1883 $this->_reset_write();
1884 }
WanWizard7219c072011-12-28 14:09:05 +01001885
Andrey Andreev24276a32012-01-08 02:44:38 +02001886 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001887 }
WanWizard7219c072011-12-28 14:09:05 +01001888
Derek Allard2067d1a2008-11-13 22:59:24 +00001889 // --------------------------------------------------------------------
1890
1891 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001892 * Delete statement
1893 *
1894 * Generates a platform-specific delete string from the supplied data
1895 *
1896 * @param string the table name
1897 * @param array the where clause
1898 * @param array the like clause
1899 * @param string the limit clause
1900 * @return string
1901 */
1902 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
1903 {
1904 $conditions = array();
1905
1906 empty($where) OR $conditions[] = implode(' ', $where);
1907 empty($like) OR $conditions[] = implode(' ', $like);
1908
1909 return 'DELETE FROM '.$table
1910 .(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '')
1911 .($limit ? ' LIMIT '.$limit : '');
1912 }
1913
1914 // --------------------------------------------------------------------
1915
1916 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001917 * DB Prefix
1918 *
1919 * Prepends a database prefix if one exists in configuration
1920 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001921 * @param string the table
1922 * @return string
1923 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001924 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001925 {
1926 if ($table == '')
1927 {
1928 $this->display_error('db_table_name_required');
1929 }
1930
1931 return $this->dbprefix.$table;
1932 }
1933
1934 // --------------------------------------------------------------------
1935
1936 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001937 * Set DB Prefix
1938 *
1939 * Set's the DB Prefix to something new without needing to reconnect
1940 *
1941 * @param string the prefix
1942 * @return string
1943 */
1944 public function set_dbprefix($prefix = '')
1945 {
1946 return $this->dbprefix = $prefix;
1947 }
1948
1949 // --------------------------------------------------------------------
1950
1951 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001952 * Track Aliases
1953 *
1954 * Used to track SQL statements written with aliased tables.
1955 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001956 * @param string The table to inspect
1957 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001958 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001959 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001960 {
1961 if (is_array($table))
1962 {
1963 foreach ($table as $t)
1964 {
1965 $this->_track_aliases($t);
1966 }
1967 return;
1968 }
Barry Mienydd671972010-10-04 16:33:58 +02001969
Derek Jones37f4b9c2011-07-01 17:56:50 -05001970 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001971 // the string into discreet statements
1972 if (strpos($table, ',') !== FALSE)
1973 {
1974 return $this->_track_aliases(explode(',', $table));
1975 }
Barry Mienydd671972010-10-04 16:33:58 +02001976
Derek Allard2067d1a2008-11-13 22:59:24 +00001977 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001978 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001979 {
1980 // if the alias is written with the AS keyword, remove it
1981 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001982
Derek Allard2067d1a2008-11-13 22:59:24 +00001983 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001984 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001985
Derek Allard2067d1a2008-11-13 22:59:24 +00001986 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001987 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001988 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001989 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001990 }
1991 }
1992 }
WanWizard7219c072011-12-28 14:09:05 +01001993
Derek Allard2067d1a2008-11-13 22:59:24 +00001994 // --------------------------------------------------------------------
1995
1996 /**
1997 * Compile the SELECT statement
1998 *
1999 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05002000 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00002001 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002002 * @return string
2003 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002004 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002005 {
2006 // Combine any cached components with the current statements
2007 $this->_merge_cache();
2008
Derek Allard2067d1a2008-11-13 22:59:24 +00002009 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002010 if ($select_override !== FALSE)
2011 {
2012 $sql = $select_override;
2013 }
2014 else
2015 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002016 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002017
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002018 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002019 {
Barry Mienydd671972010-10-04 16:33:58 +02002020 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002021 }
2022 else
Barry Mienydd671972010-10-04 16:33:58 +02002023 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002024 // Cycle through the "select" portion of the query and prep each column name.
2025 // The reason we protect identifiers here rather then in the select() function
2026 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002027 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002028 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002029 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002030 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002031 }
Barry Mienydd671972010-10-04 16:33:58 +02002032
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002033 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002034 }
2035 }
2036
Derek Allard2067d1a2008-11-13 22:59:24 +00002037 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002038 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002039 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002040 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002041 }
2042
Derek Allard2067d1a2008-11-13 22:59:24 +00002043 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002044 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002045 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002046 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002047 }
2048
Derek Allard2067d1a2008-11-13 22:59:24 +00002049 // Write the "WHERE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002050 if (count($this->qb_where) > 0 OR count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002051 {
Greg Akere156c6e2011-04-20 16:03:04 -05002052 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 }
2054
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002055 $sql .= implode("\n", $this->qb_where);
Derek Allard2067d1a2008-11-13 22:59:24 +00002056
Derek Allard2067d1a2008-11-13 22:59:24 +00002057 // Write the "LIKE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002058 if (count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002059 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002060 if (count($this->qb_where) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002061 {
2062 $sql .= "\nAND ";
2063 }
2064
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002065 $sql .= implode("\n", $this->qb_like);
Derek Allard2067d1a2008-11-13 22:59:24 +00002066 }
2067
Derek Allard2067d1a2008-11-13 22:59:24 +00002068 // Write the "GROUP BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002069 if (count($this->qb_groupby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002070 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002071 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002072 }
2073
Derek Allard2067d1a2008-11-13 22:59:24 +00002074 // Write the "HAVING" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002075 if (count($this->qb_having) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002076 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002077 $sql .= "\nHAVING ".implode("\n", $this->qb_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00002078 }
2079
Derek Allard2067d1a2008-11-13 22:59:24 +00002080 // Write the "ORDER BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002081 if (count($this->qb_orderby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002082 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002083 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
2084 if ($this->qb_order !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002085 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002086 $sql .= ($this->qb_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02002087 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002088 }
2089
Derek Allard2067d1a2008-11-13 22:59:24 +00002090 // Write the "LIMIT" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002091 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002092 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002093 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00002094 }
2095
2096 return $sql;
2097 }
2098
2099 // --------------------------------------------------------------------
2100
2101 /**
2102 * Object to Array
2103 *
2104 * Takes an object as input and converts the class variables to array key/vals
2105 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002106 * @param object
2107 * @return array
2108 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002109 public function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002110 {
2111 if ( ! is_object($object))
2112 {
2113 return $object;
2114 }
Barry Mienydd671972010-10-04 16:33:58 +02002115
Derek Allard2067d1a2008-11-13 22:59:24 +00002116 $array = array();
2117 foreach (get_object_vars($object) as $key => $val)
2118 {
2119 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06002120 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002121 {
2122 $array[$key] = $val;
2123 }
2124 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002125
2126 return $array;
2127 }
Barry Mienydd671972010-10-04 16:33:58 +02002128
Derek Jonesd10e8962010-03-02 17:10:36 -06002129 // --------------------------------------------------------------------
2130
2131 /**
2132 * Object to Array
2133 *
2134 * Takes an object as input and converts the class variables to array key/vals
2135 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002136 * @param object
2137 * @return array
2138 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002139 public function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002140 {
2141 if ( ! is_object($object))
2142 {
2143 return $object;
2144 }
Barry Mienydd671972010-10-04 16:33:58 +02002145
Derek Jonesd10e8962010-03-02 17:10:36 -06002146 $array = array();
2147 $out = get_object_vars($object);
2148 $fields = array_keys($out);
2149
2150 foreach ($fields as $val)
2151 {
2152 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002153 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002154 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002155 $i = 0;
2156 foreach ($out[$val] as $data)
2157 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002158 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002159 }
2160 }
2161 }
2162
Derek Allard2067d1a2008-11-13 22:59:24 +00002163 return $array;
2164 }
Barry Mienydd671972010-10-04 16:33:58 +02002165
Derek Allard2067d1a2008-11-13 22:59:24 +00002166 // --------------------------------------------------------------------
2167
2168 /**
2169 * Start Cache
2170 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002171 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002172 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002173 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002174 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002175 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002176 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002177 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002178 }
2179
2180 // --------------------------------------------------------------------
2181
2182 /**
2183 * Stop Cache
2184 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002185 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002186 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002187 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002188 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002189 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002190 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002191 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002192 }
2193
2194 // --------------------------------------------------------------------
2195
2196 /**
2197 * Flush Cache
2198 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002199 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002200 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002201 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002202 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002203 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002204 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002205 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002206 'qb_cache_select' => array(),
2207 'qb_cache_from' => array(),
2208 'qb_cache_join' => array(),
2209 'qb_cache_where' => array(),
2210 'qb_cache_like' => array(),
2211 'qb_cache_groupby' => array(),
2212 'qb_cache_having' => array(),
2213 'qb_cache_orderby' => array(),
2214 'qb_cache_set' => array(),
2215 'qb_cache_exists' => array(),
2216 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002217 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002218 }
2219
2220 // --------------------------------------------------------------------
2221
2222 /**
2223 * Merge Cache
2224 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002225 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002226 * locally called ones.
2227 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002228 * @return void
2229 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002230 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002231 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002232 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002233 {
2234 return;
2235 }
2236
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002237 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002238 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002239 $qb_variable = 'qb_'.$val;
2240 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002241
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002242 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002243 {
2244 continue;
2245 }
2246
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002247 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002248 }
2249
2250 // If we are "protecting identifiers" we need to examine the "from"
2251 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002252 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002253 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002254 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002255 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002256
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002257 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002258 }
WanWizard7219c072011-12-28 14:09:05 +01002259
Kyle Farris0c147b32011-08-26 02:29:31 -04002260 // --------------------------------------------------------------------
2261
2262 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002263 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002264 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002265 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002266 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002267 * @return void
2268 */
2269 public function reset_query()
2270 {
2271 $this->_reset_select();
2272 $this->_reset_write();
2273 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002274
2275 // --------------------------------------------------------------------
2276
2277 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002278 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002279 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002280 * @param array An array of fields to reset
2281 * @return void
2282 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002283 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002284 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002285 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002286 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002287 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002288 {
2289 $this->$item = $default_value;
2290 }
2291 }
2292 }
2293
2294 // --------------------------------------------------------------------
2295
2296 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002297 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002298 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002299 * @return void
2300 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002301 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002302 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002303 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002304 'qb_select' => array(),
2305 'qb_from' => array(),
2306 'qb_join' => array(),
2307 'qb_where' => array(),
2308 'qb_like' => array(),
2309 'qb_groupby' => array(),
2310 'qb_having' => array(),
2311 'qb_orderby' => array(),
2312 'qb_wherein' => array(),
2313 'qb_aliased_tables' => array(),
2314 'qb_no_escape' => array(),
2315 'qb_distinct' => FALSE,
2316 'qb_limit' => FALSE,
2317 'qb_offset' => FALSE,
2318 'qb_order' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002319 )
2320 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002321 }
Barry Mienydd671972010-10-04 16:33:58 +02002322
Derek Allard2067d1a2008-11-13 22:59:24 +00002323 // --------------------------------------------------------------------
2324
2325 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002326 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002327 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002328 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002329 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002330 * @return void
2331 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002332 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002333 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002334 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002335 'qb_set' => array(),
2336 'qb_from' => array(),
2337 'qb_where' => array(),
2338 'qb_like' => array(),
2339 'qb_orderby' => array(),
2340 'qb_keys' => array(),
2341 'qb_limit' => FALSE,
2342 'qb_order' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002343 )
2344 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002345 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002346
Derek Allard2067d1a2008-11-13 22:59:24 +00002347}
2348
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002349/* End of file DB_query_builder.php */
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002350/* Location: ./system/database/DB_query_builder.php */