blob: 34a77c5519a9212f8f476dbc50c4597e0908b3c3 [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();
Jamie Rumbelow7efad202012-02-19 12:37:00 +000050 protected $qb_groupby = array();
51 protected $qb_having = array();
52 protected $qb_keys = array();
53 protected $qb_limit = FALSE;
54 protected $qb_offset = FALSE;
Jamie Rumbelow7efad202012-02-19 12:37:00 +000055 protected $qb_orderby = array();
56 protected $qb_set = array();
57 protected $qb_wherein = array();
58 protected $qb_aliased_tables = array();
59 protected $qb_store_array = array();
60 protected $qb_where_group_started = FALSE;
61 protected $qb_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +020062
Jamie Rumbelow7efad202012-02-19 12:37:00 +000063 // Query Builder Caching variables
64 protected $qb_caching = FALSE;
65 protected $qb_cache_exists = array();
66 protected $qb_cache_select = array();
67 protected $qb_cache_from = array();
68 protected $qb_cache_join = array();
69 protected $qb_cache_where = array();
70 protected $qb_cache_like = array();
71 protected $qb_cache_groupby = array();
72 protected $qb_cache_having = array();
73 protected $qb_cache_orderby = array();
74 protected $qb_cache_set = array();
WanWizard7219c072011-12-28 14:09:05 +010075
Jamie Rumbelow7efad202012-02-19 12:37:00 +000076 protected $qb_no_escape = array();
77 protected $qb_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000078
79 /**
80 * Select
81 *
82 * Generates the SELECT portion of the query
83 *
Derek Allard2067d1a2008-11-13 22:59:24 +000084 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +030085 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +000086 * @return object
87 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060088 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
Derek Allard2067d1a2008-11-13 22:59:24 +000090 if (is_string($select))
91 {
92 $select = explode(',', $select);
93 }
94
Andrey Andreev42870232012-06-12 01:30:20 +030095 // If the escape value was not set will will base it on the global setting
96 is_bool($escape) OR $escape = $this->_protect_identifiers;
97
Derek Allard2067d1a2008-11-13 22:59:24 +000098 foreach ($select as $val)
99 {
100 $val = trim($val);
101
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100102 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000104 $this->qb_select[] = $val;
105 $this->qb_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000106
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000107 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000109 $this->qb_cache_select[] = $val;
110 $this->qb_cache_exists[] = 'select';
111 $this->qb_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 }
113 }
114 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 return $this;
117 }
118
119 // --------------------------------------------------------------------
120
121 /**
122 * Select Max
123 *
124 * Generates a SELECT MAX(field) portion of a query
125 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 * @param string the field
127 * @param string an alias
128 * @return object
129 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600130 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
132 return $this->_max_min_avg_sum($select, $alias, 'MAX');
133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 // --------------------------------------------------------------------
136
137 /**
138 * Select Min
139 *
140 * Generates a SELECT MIN(field) portion of a query
141 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 * @param string the field
143 * @param string an alias
144 * @return object
145 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600146 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
148 return $this->_max_min_avg_sum($select, $alias, 'MIN');
149 }
150
151 // --------------------------------------------------------------------
152
153 /**
154 * Select Average
155 *
156 * Generates a SELECT AVG(field) portion of a query
157 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 * @param string the field
159 * @param string an alias
160 * @return object
161 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600162 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
164 return $this->_max_min_avg_sum($select, $alias, 'AVG');
165 }
166
167 // --------------------------------------------------------------------
168
169 /**
170 * Select Sum
171 *
172 * Generates a SELECT SUM(field) portion of a query
173 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 * @param string the field
175 * @param string an alias
176 * @return object
177 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600178 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
180 return $this->_max_min_avg_sum($select, $alias, 'SUM');
181 }
182
183 // --------------------------------------------------------------------
184
185 /**
186 * Processing Function for the four functions above:
187 *
188 * select_max()
189 * select_min()
190 * select_avg()
Andrey Andreev24276a32012-01-08 02:44:38 +0200191 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200192 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 * @param string the field
194 * @param string an alias
195 * @return object
196 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600197 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100199 if ( ! is_string($select) OR $select === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
201 $this->display_error('db_invalid_query');
202 }
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
207 {
208 show_error('Invalid function type: '.$type);
209 }
Barry Mienydd671972010-10-04 16:33:58 +0200210
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100211 if ($alias === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
213 $alias = $this->_create_alias_from_table(trim($select));
214 }
Barry Mienydd671972010-10-04 16:33:58 +0200215
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300216 $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->escape_identifiers(trim($alias));
217
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000218 $this->qb_select[] = $sql;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100219 $this->qb_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200220
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000221 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000223 $this->qb_cache_select[] = $sql;
224 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 return $this;
228 }
229
230 // --------------------------------------------------------------------
231
232 /**
233 * Determines the alias name based on the table
234 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 * @param string
236 * @return string
237 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600238 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
240 if (strpos($item, '.') !== FALSE)
241 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200242 $item = explode('.', $item);
243 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 return $item;
247 }
248
249 // --------------------------------------------------------------------
250
251 /**
252 * DISTINCT
253 *
254 * Sets a flag which tells the query string compiler to add DISTINCT
255 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 * @param bool
257 * @return object
258 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600259 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300261 $this->qb_distinct = is_bool($val) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 return $this;
263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 // --------------------------------------------------------------------
266
267 /**
268 * From
269 *
270 * Generates the FROM portion of the query
271 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 * @param mixed can be a string or array
273 * @return object
274 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600275 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300277 foreach ((array) $from as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
279 if (strpos($val, ',') !== FALSE)
280 {
281 foreach (explode(',', $val) as $v)
282 {
283 $v = trim($v);
284 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200285
George Petsagourakis193d4482012-04-28 11:16:18 +0300286 $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000287
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000288 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000290 $this->qb_cache_from[] = $v;
291 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200292 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
295 else
296 {
297 $val = trim($val);
298
Andrey Andreev24276a32012-01-08 02:44:38 +0200299 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000300 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200302
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000303 $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000304
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000305 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000307 $this->qb_cache_from[] = $val;
308 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
310 }
311 }
312
313 return $this;
314 }
315
316 // --------------------------------------------------------------------
317
318 /**
319 * Join
320 *
321 * Generates the JOIN portion of the query
322 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 * @param string
324 * @param string the join condition
325 * @param string the type of join
Alex Bilbief512b732012-06-16 11:15:19 +0100326 * @param string whether not to try to escape identifiers
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 * @return object
328 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300329 public function join($table, $cond, $type = '', $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200330 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100331 if ($type !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 {
333 $type = strtoupper(trim($type));
334
Andrey Andreev42870232012-06-12 01:30:20 +0300335 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
337 $type = '';
338 }
339 else
340 {
341 $type .= ' ';
342 }
343 }
344
Andrey Andreev24276a32012-01-08 02:44:38 +0200345 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000346 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 $this->_track_aliases($table);
348
Andrey Andreevfe642da2012-06-16 03:47:33 +0300349 is_bool($escape) OR $escape = $this->_protect_identifiers;
350
Andrey Andreev42870232012-06-12 01:30:20 +0300351 // Split multiple conditions
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300352 if ($escape === TRUE && preg_match_all('/\sAND\s|\sOR\s/i', $cond, $m, PREG_OFFSET_CAPTURE))
Andrey Andreev42870232012-06-12 01:30:20 +0300353 {
354 $newcond = '';
355 $m[0][] = array('', strlen($cond));
356
357 for ($i = 0, $c = count($m[0]), $s = 0;
358 $i < $c;
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300359 $s = $m[0][$i][1] + strlen($m[0][$i][0]), $i++)
Andrey Andreev42870232012-06-12 01:30:20 +0300360 {
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300361 $temp = substr($cond, $s, ($m[0][$i][1] - $s));
Andrey Andreev42870232012-06-12 01:30:20 +0300362
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300363 $newcond .= preg_match("/([\[\]\w\.'-]+)(\s*[^\"\[`'\w]+\s*)(.+)/i", $temp, $match)
Andrey Andreev42870232012-06-12 01:30:20 +0300364 ? $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3])
365 : $temp;
366
367 $newcond .= $m[0][$i][0];
368 }
369
Andrey Andreev3751f932012-06-17 18:07:48 +0300370 $cond = ' ON '.$newcond;
Andrey Andreev42870232012-06-12 01:30:20 +0300371 }
372 // Split apart the condition and protect the identifiers
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300373 elseif ($escape === TRUE && preg_match("/([\[\]\w\.'-]+)(\s*[^\"\[`'\w]+\s*)(.+)/i", $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
Andrey Andreev3751f932012-06-17 18:07:48 +0300375 $cond = ' ON '.$this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
376 }
377 elseif ( ! $this->_has_operator($cond))
378 {
379 $cond = ' USING ('.($escape ? $this->escape_identifiers($cond) : $cond).')';
380 }
381 else
382 {
383 $cond = ' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Andrey Andreev42870232012-06-12 01:30:20 +0300386 // Do we want to escape the table name?
387 if ($escape === TRUE)
388 {
389 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
390 }
391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 // Assemble the JOIN statement
Andrey Andreev3751f932012-06-17 18:07:48 +0300393 $this->qb_join[] = $join = $type.'JOIN '.$table.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000394
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000395 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000397 $this->qb_cache_join[] = $join;
398 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 }
400
401 return $this;
402 }
403
404 // --------------------------------------------------------------------
405
406 /**
407 * Where
408 *
409 * Generates the WHERE portion of the query. Separates
410 * multiple calls with AND
411 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 * @param mixed
413 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300414 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 * @return object
416 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300417 public function where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300419 return $this->_wh('qb_where', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 }
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 // --------------------------------------------------------------------
423
424 /**
425 * OR Where
426 *
427 * Generates the WHERE portion of the query. Separates
428 * multiple calls with OR
429 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 * @param mixed
431 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300432 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 * @return object
434 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300435 public function or_where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300437 return $this->_wh('qb_where', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 }
439
440 // --------------------------------------------------------------------
441
442 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +0300443 * WHERE, HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300445 * Called by where(), or_where(), having(), or_having()
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300447 * @param string 'qb_where' or 'qb_having'
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 * @param mixed
449 * @param mixed
450 * @param string
Andrey Andreevb0478652012-07-18 15:34:46 +0300451 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 * @return object
453 */
Andrey Andreevd40459d2012-07-18 16:46:39 +0300454 protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300456 $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where';
457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 if ( ! is_array($key))
459 {
460 $key = array($key => $value);
461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 // If the escape value was not set will will base it on the global setting
Andrey Andreeve10fb792012-06-15 12:07:04 +0300464 is_bool($escape) OR $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000465
466 foreach ($key as $k => $v)
467 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300468 $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0)
Andrey Andreev58803fb2012-06-24 00:45:37 +0300469 ? $this->_group_get_type('')
470 : $this->_group_get_type($type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000471
472 if (is_null($v) && ! $this->_has_operator($k))
473 {
474 // value appears not to have been set, assign the test to IS NULL
475 $k .= ' IS NULL';
476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 if ( ! is_null($v))
479 {
480 if ($escape === TRUE)
481 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300482 $v = ' '.(is_int($v) ? $v : $this->escape($v));
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 }
WanWizard7219c072011-12-28 14:09:05 +0100484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 if ( ! $this->_has_operator($k))
486 {
Greg Akere156c6e2011-04-20 16:03:04 -0500487 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
489 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000490
Andrey Andreevd40459d2012-07-18 16:46:39 +0300491 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000492 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300494 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
495 $this->qb_cache_exists[] = substr($qb_key, 3);
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 }
Barry Mienydd671972010-10-04 16:33:58 +0200497
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 }
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 return $this;
501 }
502
503 // --------------------------------------------------------------------
504
505 /**
506 * Where_in
507 *
508 * Generates a WHERE field IN ('item', 'item') SQL query joined with
509 * 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 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300515 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300517 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 }
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 // --------------------------------------------------------------------
521
522 /**
523 * Where_in_or
524 *
525 * Generates a WHERE field IN ('item', 'item') SQL query joined with
526 * 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 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300532 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300534 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
536
537 // --------------------------------------------------------------------
538
539 /**
540 * Where_not_in
541 *
542 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
543 * with AND if appropriate
544 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 * @param string The field to search
546 * @param array The values searched on
547 * @return object
548 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300549 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300551 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 }
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 // --------------------------------------------------------------------
555
556 /**
557 * Where_not_in_or
558 *
559 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
560 * with OR if appropriate
561 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 * @param string The field to search
563 * @param array The values searched on
564 * @return object
565 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300566 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300568 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 }
570
571 // --------------------------------------------------------------------
572
573 /**
574 * Where_in
575 *
576 * Called by where_in, where_in_or, where_not_in, where_not_in_or
577 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 * @param string The field to search
579 * @param array The values searched on
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300580 * @param bool If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200581 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 * @return object
583 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300584 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 {
586 if ($key === NULL OR $values === NULL)
587 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300588 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 }
Barry Mienydd671972010-10-04 16:33:58 +0200590
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 if ( ! is_array($values))
592 {
593 $values = array($values);
594 }
Barry Mienydd671972010-10-04 16:33:58 +0200595
Andrey Andreev498c1e02012-06-16 03:34:10 +0300596 is_bool($escape) OR $escape = $this->_protect_identifiers;
597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 $not = ($not) ? ' NOT' : '';
599
600 foreach ($values as $value)
601 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000602 $this->qb_wherein[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 }
604
WanWizardbc69f362012-06-22 00:10:11 +0200605 $prefix = (count($this->qb_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Andrey Andreev6e704752012-07-18 00:46:33 +0300606 $where_in = array(
607 'condition' => $prefix.$key.$not.' IN('.implode(', ', $this->qb_wherein).')',
608 'escape' => $escape
609 );
Barry Mienydd671972010-10-04 16:33:58 +0200610
Andrey Andreev6e704752012-07-18 00:46:33 +0300611 $this->qb_where[] = $where_in;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000612 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000614 $this->qb_cache_where[] = $where_in;
615 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 }
617
618 // reset the array for multiple calls
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000619 $this->qb_wherein = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 return $this;
621 }
Barry Mienydd671972010-10-04 16:33:58 +0200622
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 // --------------------------------------------------------------------
624
625 /**
626 * Like
627 *
628 * Generates a %LIKE% portion of the query. Separates
629 * multiple calls with AND
630 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300632 * @param string
633 * @param string
634 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 * @return object
636 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300637 public function like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300639 return $this->_like($field, $match, 'AND ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 }
641
642 // --------------------------------------------------------------------
643
644 /**
645 * Not Like
646 *
647 * Generates a NOT LIKE portion of the query. Separates
648 * multiple calls with AND
649 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300651 * @param string
652 * @param string
653 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 * @return object
655 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300656 public function not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300658 return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
Barry Mienydd671972010-10-04 16:33:58 +0200660
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 // --------------------------------------------------------------------
662
663 /**
664 * OR Like
665 *
666 * Generates a %LIKE% portion of the query. Separates
667 * multiple calls with OR
668 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300670 * @param string
671 * @param string
672 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 * @return object
674 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300675 public function or_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300677 return $this->_like($field, $match, 'OR ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 }
679
680 // --------------------------------------------------------------------
681
682 /**
683 * OR Not Like
684 *
685 * Generates a NOT LIKE portion of the query. Separates
686 * multiple calls with OR
687 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300689 * @param string
690 * @param string
691 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 * @return object
693 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300694 public function or_not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300696 return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 }
Barry Mienydd671972010-10-04 16:33:58 +0200698
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 // --------------------------------------------------------------------
700
701 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 * Like
703 *
Andrey Andreevb0478652012-07-18 15:34:46 +0300704 * Called by like(), or_like(), not_like, or_not_like()
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 * @param string
Andrey Andreevb0478652012-07-18 15:34:46 +0300708 * @param string
709 * @param string
710 * @param string
711 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 * @return object
713 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300714 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 {
716 if ( ! is_array($field))
717 {
718 $field = array($field => $match);
719 }
Barry Mienydd671972010-10-04 16:33:58 +0200720
Andrey Andreevb0478652012-07-18 15:34:46 +0300721 is_bool($escape) OR $escape = $this->_protect_identifiers;
722 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
723 ? $this->_group_get_type('') : $this->_group_get_type($type);
724
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 foreach ($field as $k => $v)
726 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000727 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300728
Andrey Andreev24276a32012-01-08 02:44:38 +0200729 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400730 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300731 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400732 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200733 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300735 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200737 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300739 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 }
741 else
742 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300743 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600745
Derek Jonese4ed5832009-02-20 21:44:59 +0000746 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100747 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000748 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300749 $like_statement .= sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000750 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600751
Andrey Andreevb0478652012-07-18 15:34:46 +0300752 $this->qb_where[] = array('condition' => $like_statement, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000753 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 {
Andrey Andreevededc4a2012-07-18 01:16:15 +0300755 $this->qb_cache_where[] = $like_statement;
756 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200759
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 return $this;
761 }
Barry Mienydd671972010-10-04 16:33:58 +0200762
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 // --------------------------------------------------------------------
764
765 /**
WanWizard7219c072011-12-28 14:09:05 +0100766 * Starts a query group.
767 *
768 * @param string (Internal use only)
769 * @param string (Internal use only)
770 * @return object
771 */
772 public function group_start($not = '', $type = 'AND ')
773 {
774 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100775
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000776 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100777 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Andrey Andreev6e704752012-07-18 00:46:33 +0300778 $where = array(
779 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (',
780 'escape' => FALSE
781 );
WanWizard7219c072011-12-28 14:09:05 +0100782
Andrey Andreev6e704752012-07-18 00:46:33 +0300783 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000784 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100785 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300786 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +0100787 }
788
789 return $this;
790 }
791
792 // --------------------------------------------------------------------
793
794 /**
795 * Starts a query group, but ORs the group
796 *
797 * @return object
798 */
799 public function or_group_start()
800 {
801 return $this->group_start('', 'OR ');
802 }
803
804 // --------------------------------------------------------------------
805
806 /**
807 * Starts a query group, but NOTs the group
808 *
809 * @return object
810 */
811 public function not_group_start()
812 {
813 return $this->group_start('NOT ', 'AND ');
814 }
815
816 // --------------------------------------------------------------------
817
818 /**
819 * Starts a query group, but OR NOTs the group
820 *
821 * @return object
822 */
823 public function or_not_group_start()
824 {
825 return $this->group_start('NOT ', 'OR ');
826 }
827
828 // --------------------------------------------------------------------
829
830 /**
831 * Ends a query group
832 *
833 * @return object
834 */
835 public function group_end()
836 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000837 $this->qb_where_group_started = FALSE;
Andrey Andreev6e704752012-07-18 00:46:33 +0300838 $where = array(
839 'condition' => str_repeat(' ', $this->qb_where_group_count--).')',
840 'escape' => FALSE
841 );
WanWizard7219c072011-12-28 14:09:05 +0100842
Andrey Andreev6e704752012-07-18 00:46:33 +0300843 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000844 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100845 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300846 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +0100847 }
848
WanWizard7219c072011-12-28 14:09:05 +0100849 return $this;
850 }
851
852 // --------------------------------------------------------------------
853
854 /**
855 * Group_get_type
856 *
857 * Called by group_start(), _like(), _where() and _where_in()
858 *
859 * @param string
860 * @return string
861 */
862 protected function _group_get_type($type)
863 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000864 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100865 {
866 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000867 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100868 }
869
870 return $type;
871 }
872
873 // --------------------------------------------------------------------
874
875 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 * GROUP BY
877 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 * @param string
879 * @return object
880 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600881 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 {
883 if (is_string($by))
884 {
885 $by = explode(',', $by);
886 }
Barry Mienydd671972010-10-04 16:33:58 +0200887
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 foreach ($by as $val)
889 {
890 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200891
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100892 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000894 $this->qb_groupby[] = $val = $this->protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200895
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000896 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000898 $this->qb_cache_groupby[] = $val;
899 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 }
901 }
902 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200903
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 return $this;
905 }
906
907 // --------------------------------------------------------------------
908
909 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 * Sets the HAVING value
911 *
912 * Separates multiple calls with AND
913 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 * @param string
915 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300916 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 * @return object
918 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300919 public function having($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300921 return $this->_wh('qb_having', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 }
Barry Mienydd671972010-10-04 16:33:58 +0200923
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 // --------------------------------------------------------------------
925
926 /**
927 * Sets the OR HAVING value
928 *
929 * Separates multiple calls with OR
930 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 * @param string
932 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300933 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 * @return object
935 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300936 public function or_having($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300938 return $this->_wh('qb_having', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 }
Barry Mienydd671972010-10-04 16:33:58 +0200940
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 // --------------------------------------------------------------------
942
943 /**
944 * Sets the ORDER BY value
945 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 * @param string
947 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100948 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 * @return object
950 */
Andrey Andreevd24160c2012-06-16 03:21:20 +0300951 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200953 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 {
955 $orderby = ''; // Random results want or don't need a field name
956 $direction = $this->_random_keyword;
957 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100958 elseif (trim($direction) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 {
Andrey Andreev650b4c02012-06-11 12:07:15 +0300960 $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : ' ASC';
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 }
Barry Mienydd671972010-10-04 16:33:58 +0200962
Andrey Andreevd24160c2012-06-16 03:21:20 +0300963 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +0200964
Andrey Andreevd24160c2012-06-16 03:21:20 +0300965 if ($escape === TRUE && strpos($orderby, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 {
967 $temp = array();
968 foreach (explode(',', $orderby) as $part)
969 {
970 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000971 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 {
Andrey Andreev88cb2782012-06-11 20:40:50 +0300973 $part = preg_match('/^(.+)\s+(ASC|DESC)$/i', $part, $matches)
974 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
975 : $this->protect_identifiers($part);
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 }
Barry Mienydd671972010-10-04 16:33:58 +0200977
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 $temp[] = $part;
979 }
Barry Mienydd671972010-10-04 16:33:58 +0200980
981 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 }
Andrey Andreev650b4c02012-06-11 12:07:15 +0300983 elseif ($direction !== $this->_random_keyword && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 {
Andrey Andreev1d1d7ff2012-06-11 22:35:35 +0300985 $orderby = preg_match('/^(.+)\s+(ASC|DESC)$/i', $orderby, $matches)
Andrey Andreev88cb2782012-06-11 20:40:50 +0300986 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
987 : $this->protect_identifiers($orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 }
Barry Mienydd671972010-10-04 16:33:58 +0200989
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000990 $this->qb_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200991
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000992 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000994 $this->qb_cache_orderby[] = $orderby_statement;
995 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 }
997
998 return $this;
999 }
Barry Mienydd671972010-10-04 16:33:58 +02001000
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 // --------------------------------------------------------------------
1002
1003 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 * Sets the LIMIT value
1005 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001006 * @param int the limit value
1007 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 * @return object
1009 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +02001010 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001012 is_null($value) OR $this->qb_limit = (int) $value;
1013 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001014
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 return $this;
1016 }
Barry Mienydd671972010-10-04 16:33:58 +02001017
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 // --------------------------------------------------------------------
1019
1020 /**
1021 * Sets the OFFSET value
1022 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001023 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 * @return object
1025 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001026 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001028 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 return $this;
1030 }
Barry Mienydd671972010-10-04 16:33:58 +02001031
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 // --------------------------------------------------------------------
1033
1034 /**
Andrey Andreev2c35b642012-06-24 03:05:26 +03001035 * Limit string
1036 *
1037 * Generates a platform-specific LIMIT clause
1038 *
1039 * @param string the sql query string
1040 * @param int the number of rows to limit the query to
1041 * @param int the offset value
1042 * @return string
1043 */
1044 protected function _limit($sql, $limit, $offset)
1045 {
1046 return $sql.' LIMIT '.($offset ? $offset.', ' : '').$limit;
1047 }
1048
1049 // --------------------------------------------------------------------
1050
1051 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001052 * The "set" function.
1053 *
1054 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 * @param mixed
1057 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001058 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 * @return object
1060 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001061 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 {
1063 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001064
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 if ( ! is_array($key))
1066 {
1067 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001068 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001069
Andrey Andreevfe642da2012-06-16 03:47:33 +03001070 is_bool($escape) OR $escape = $this->_protect_identifiers;
1071
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 foreach ($key as $k => $v)
1073 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001074 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1075 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 }
Barry Mienydd671972010-10-04 16:33:58 +02001077
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 return $this;
1079 }
WanWizard7219c072011-12-28 14:09:05 +01001080
Kyle Farris0c147b32011-08-26 02:29:31 -04001081 // --------------------------------------------------------------------
1082
1083 /**
1084 * Get SELECT query string
1085 *
1086 * Compiles a SELECT query string and returns the sql.
1087 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001088 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001089 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001090 * @return string
1091 */
WanWizard7219c072011-12-28 14:09:05 +01001092 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001093 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001094 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001095 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001096 $this->_track_aliases($table);
1097 $this->from($table);
1098 }
WanWizard7219c072011-12-28 14:09:05 +01001099
Andrey Andreev650b4c02012-06-11 12:07:15 +03001100 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001101
Kyle Farris0c147b32011-08-26 02:29:31 -04001102 if ($reset === TRUE)
1103 {
1104 $this->_reset_select();
1105 }
WanWizard7219c072011-12-28 14:09:05 +01001106
Kyle Farris0c147b32011-08-26 02:29:31 -04001107 return $select;
1108 }
WanWizard7219c072011-12-28 14:09:05 +01001109
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 // --------------------------------------------------------------------
1111
1112 /**
1113 * Get
1114 *
1115 * Compiles the select statement based on the other functions called
1116 * and runs the query
1117 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 * @param string the table
1119 * @param string the limit clause
1120 * @param string the offset clause
1121 * @return object
1122 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001123 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001125 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 {
1127 $this->_track_aliases($table);
1128 $this->from($table);
1129 }
Barry Mienydd671972010-10-04 16:33:58 +02001130
Andrey Andreev650b4c02012-06-11 12:07:15 +03001131 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 {
1133 $this->limit($limit, $offset);
1134 }
Barry Mienydd671972010-10-04 16:33:58 +02001135
Andrey Andreev24276a32012-01-08 02:44:38 +02001136 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 $this->_reset_select();
1138 return $result;
1139 }
1140
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001141 // --------------------------------------------------------------------
1142
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 /**
1144 * "Count All Results" query
1145 *
Barry Mienydd671972010-10-04 16:33:58 +02001146 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001147 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 * @param string
1150 * @return string
1151 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001152 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001154 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 {
1156 $this->_track_aliases($table);
1157 $this->from($table);
1158 }
Barry Mienydd671972010-10-04 16:33:58 +02001159
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001160 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001162
Purwandi1d160e72012-01-09 16:33:28 +07001163 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001165 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 }
1167
Purwandi1d160e72012-01-09 16:33:28 +07001168 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001169 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001171
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 // --------------------------------------------------------------------
1173
1174 /**
1175 * Get_Where
1176 *
1177 * Allows the where clause, limit and offset to be added directly
1178 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 * @param string the where clause
1180 * @param string the limit clause
1181 * @param string the offset clause
1182 * @return object
1183 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001184 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001186 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 {
1188 $this->from($table);
1189 }
1190
1191 if ( ! is_null($where))
1192 {
1193 $this->where($where);
1194 }
Barry Mienydd671972010-10-04 16:33:58 +02001195
Andrey Andreev650b4c02012-06-11 12:07:15 +03001196 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 {
1198 $this->limit($limit, $offset);
1199 }
Barry Mienydd671972010-10-04 16:33:58 +02001200
Andrey Andreev24276a32012-01-08 02:44:38 +02001201 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 $this->_reset_select();
1203 return $result;
1204 }
1205
1206 // --------------------------------------------------------------------
1207
1208 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001209 * Insert_Batch
1210 *
1211 * Compiles batch insert strings and runs the queries
1212 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001213 * @param string the table to retrieve the results from
1214 * @param array an associative array of insert values
1215 * @return object
1216 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001217 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001218 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001219 if ( ! is_null($set))
1220 {
1221 $this->set_insert_batch($set);
1222 }
Barry Mienydd671972010-10-04 16:33:58 +02001223
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001224 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001225 {
1226 if ($this->db_debug)
1227 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001228 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001229 return $this->display_error('db_must_use_set');
1230 }
1231 return FALSE;
1232 }
1233
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001234 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001235 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001236 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001237 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001238 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001239 }
Barry Mienydd671972010-10-04 16:33:58 +02001240
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001241 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001242 }
1243
1244 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001245 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001246 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001247 $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 -06001248 }
Barry Mienydd671972010-10-04 16:33:58 +02001249
Derek Jonesd10e8962010-03-02 17:10:36 -06001250 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001251 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001252 }
1253
1254 // --------------------------------------------------------------------
1255
1256 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001257 * Insert_batch statement
1258 *
1259 * Generates a platform-specific insert string from the supplied data.
1260 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001261 * @param string the table name
1262 * @param array the insert keys
1263 * @param array the insert values
1264 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001265 */
1266 protected function _insert_batch($table, $keys, $values)
1267 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001268 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001269 }
1270
1271 // --------------------------------------------------------------------
1272
1273 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001274 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001275 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001276 * @param mixed
1277 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001278 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001279 * @return object
1280 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001281 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001282 {
1283 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001284
Derek Jonesd10e8962010-03-02 17:10:36 -06001285 if ( ! is_array($key))
1286 {
1287 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001288 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001289
Andrey Andreevfe642da2012-06-16 03:47:33 +03001290 is_bool($escape) OR $escape = $this->_protect_identifiers;
1291
Iban Eguia3c0a4522012-04-15 13:30:44 +02001292 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001293 sort($keys);
1294
1295 foreach ($key as $row)
1296 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001297 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001298 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1299 {
1300 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001301 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001302 return;
1303 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001304
Barry Mienydd671972010-10-04 16:33:58 +02001305 ksort($row); // puts $row in the same order as our keys
1306
Andrey Andreev650b4c02012-06-11 12:07:15 +03001307 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001308 {
1309 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001310 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001311 {
Barry Mienydd671972010-10-04 16:33:58 +02001312 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001313 }
1314
Andrey Andreev650b4c02012-06-11 12:07:15 +03001315 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001316 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001317
1318 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001319 }
1320
1321 foreach ($keys as $k)
1322 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001323 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001324 }
Barry Mienydd671972010-10-04 16:33:58 +02001325
Derek Jonesd10e8962010-03-02 17:10:36 -06001326 return $this;
1327 }
WanWizard7219c072011-12-28 14:09:05 +01001328
Kyle Farris0c147b32011-08-26 02:29:31 -04001329 // --------------------------------------------------------------------
1330
1331 /**
1332 * Get INSERT query string
1333 *
1334 * Compiles an insert query and returns the sql
1335 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001336 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001337 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001338 * @return string
1339 */
1340 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001341 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001342 if ($this->_validate_insert($table) === FALSE)
1343 {
1344 return FALSE;
1345 }
WanWizard7219c072011-12-28 14:09:05 +01001346
Kyle Farris76116012011-08-31 11:17:48 -04001347 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001348 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001349 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001350 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001351 array_keys($this->qb_set),
1352 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001353 );
WanWizard7219c072011-12-28 14:09:05 +01001354
Kyle Farris0c147b32011-08-26 02:29:31 -04001355 if ($reset === TRUE)
1356 {
1357 $this->_reset_write();
1358 }
WanWizard7219c072011-12-28 14:09:05 +01001359
Kyle Farris0c147b32011-08-26 02:29:31 -04001360 return $sql;
1361 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001362
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 // --------------------------------------------------------------------
1364
1365 /**
1366 * Insert
1367 *
1368 * Compiles an insert string and runs the query
1369 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001370 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 * @param array an associative array of insert values
1372 * @return object
1373 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001374 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001375 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 if ( ! is_null($set))
1377 {
1378 $this->set($set);
1379 }
WanWizard7219c072011-12-28 14:09:05 +01001380
Kyle Farris0c147b32011-08-26 02:29:31 -04001381 if ($this->_validate_insert($table) === FALSE)
1382 {
1383 return FALSE;
1384 }
WanWizard7219c072011-12-28 14:09:05 +01001385
Kyle Farris76116012011-08-31 11:17:48 -04001386 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001387 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001388 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001389 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001390 array_keys($this->qb_set),
1391 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001392 );
Barry Mienydd671972010-10-04 16:33:58 +02001393
Kyle Farris0c147b32011-08-26 02:29:31 -04001394 $this->_reset_write();
1395 return $this->query($sql);
1396 }
WanWizard7219c072011-12-28 14:09:05 +01001397
Kyle Farris0c147b32011-08-26 02:29:31 -04001398 // --------------------------------------------------------------------
1399
1400 /**
1401 * Validate Insert
1402 *
1403 * This method is used by both insert() and get_compiled_insert() to
1404 * validate that the there data is actually being set and that table
1405 * has been chosen to be inserted into.
1406 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001407 * @param string the table to insert data into
1408 * @return string
1409 */
WanWizard7219c072011-12-28 14:09:05 +01001410 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001411 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001412 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001414 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001415 }
1416
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001417 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001418 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001419 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001420 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001421 elseif ( ! isset($this->qb_from[0]))
1422 {
1423 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1424 }
WanWizard7219c072011-12-28 14:09:05 +01001425
Kyle Farris0c147b32011-08-26 02:29:31 -04001426 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 }
Barry Mienydd671972010-10-04 16:33:58 +02001428
Phil Sturgeon9789f322011-07-15 15:14:05 -06001429 // --------------------------------------------------------------------
1430
1431 /**
1432 * Replace
1433 *
1434 * Compiles an replace into string and runs the query
1435 *
1436 * @param string the table to replace data into
1437 * @param array an associative array of insert values
1438 * @return object
1439 */
1440 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001441 {
1442 if ( ! is_null($set))
1443 {
1444 $this->set($set);
1445 }
Barry Mienydd671972010-10-04 16:33:58 +02001446
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001447 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001448 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001449 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001450 }
1451
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001452 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001453 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001454 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001455 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001456 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001457 }
Barry Mienydd671972010-10-04 16:33:58 +02001458
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001459 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001460 }
1461
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001462 $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 +00001463
Derek Jonesd10e8962010-03-02 17:10:36 -06001464 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001465 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001466 }
WanWizard7219c072011-12-28 14:09:05 +01001467
Kyle Farris0c147b32011-08-26 02:29:31 -04001468 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001469
Kyle Farris0c147b32011-08-26 02:29:31 -04001470 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001471 * Replace statement
1472 *
1473 * Generates a platform-specific replace string from the supplied data
1474 *
1475 * @param string the table name
1476 * @param array the insert keys
1477 * @param array the insert values
1478 * @return string
1479 */
1480 protected function _replace($table, $keys, $values)
1481 {
1482 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1483 }
1484
1485 // --------------------------------------------------------------------
1486
1487 /**
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001488 * From Tables
1489 *
1490 * This public function implicitly groups FROM tables so there is no confusion
1491 * about operator precedence in harmony with SQL standards
1492 *
1493 * @param array
1494 * @return string
1495 */
1496 protected function _from_tables($tables)
1497 {
1498 is_array($tables) OR $tables = array($tables);
1499
1500 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
1501 }
1502
1503 // --------------------------------------------------------------------
1504
1505 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001506 * Get UPDATE query string
1507 *
1508 * Compiles an update query and returns the sql
1509 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001510 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001511 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001512 * @return string
1513 */
1514 public function get_compiled_update($table = '', $reset = TRUE)
1515 {
1516 // Combine any cached components with the current statements
1517 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001518
Kyle Farris0c147b32011-08-26 02:29:31 -04001519 if ($this->_validate_update($table) === FALSE)
1520 {
1521 return FALSE;
1522 }
WanWizard7219c072011-12-28 14:09:05 +01001523
Andrey Andreevb0478652012-07-18 15:34:46 +03001524 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001525
Kyle Farris0c147b32011-08-26 02:29:31 -04001526 if ($reset === TRUE)
1527 {
1528 $this->_reset_write();
1529 }
WanWizard7219c072011-12-28 14:09:05 +01001530
Kyle Farris0c147b32011-08-26 02:29:31 -04001531 return $sql;
1532 }
WanWizard7219c072011-12-28 14:09:05 +01001533
Derek Allard2067d1a2008-11-13 22:59:24 +00001534 // --------------------------------------------------------------------
1535
1536 /**
1537 * Update
1538 *
1539 * Compiles an update string and runs the query
1540 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001541 * @param string the table to retrieve the results from
1542 * @param array an associative array of update values
1543 * @param mixed the where clause
1544 * @return object
1545 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001546 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001547 {
1548 // Combine any cached components with the current statements
1549 $this->_merge_cache();
1550
1551 if ( ! is_null($set))
1552 {
1553 $this->set($set);
1554 }
Barry Mienydd671972010-10-04 16:33:58 +02001555
Kyle Farris0c147b32011-08-26 02:29:31 -04001556 if ($this->_validate_update($table) === FALSE)
1557 {
1558 return FALSE;
1559 }
1560
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001561 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001562 {
1563 $this->where($where);
1564 }
1565
Andrey Andreev650b4c02012-06-11 12:07:15 +03001566 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001567 {
1568 $this->limit($limit);
1569 }
1570
Andrey Andreevb0478652012-07-18 15:34:46 +03001571 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
Jamie Rumbelow3b1355c2012-03-06 21:27:46 +00001572
Kyle Farris0c147b32011-08-26 02:29:31 -04001573 $this->_reset_write();
1574 return $this->query($sql);
1575 }
WanWizard7219c072011-12-28 14:09:05 +01001576
Kyle Farris0c147b32011-08-26 02:29:31 -04001577 // --------------------------------------------------------------------
1578
1579 /**
1580 * Validate Update
1581 *
1582 * This method is used by both update() and get_compiled_update() to
1583 * validate that data is actually being set and that a table has been
1584 * chosen to be update.
1585 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001586 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001587 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001588 */
1589 protected function _validate_update($table = '')
1590 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001591 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001592 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001593 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001594 }
1595
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001596 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001597 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001598 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001599 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001600 elseif ( ! isset($this->qb_from[0]))
1601 {
1602 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1603 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001604
1605 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001606 }
WanWizard7219c072011-12-28 14:09:05 +01001607
Derek Jonesd10e8962010-03-02 17:10:36 -06001608 // --------------------------------------------------------------------
1609
1610 /**
1611 * Update_Batch
1612 *
1613 * Compiles an update string and runs the query
1614 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001615 * @param string the table to retrieve the results from
1616 * @param array an associative array of update values
1617 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001618 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001619 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001620 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001621 {
1622 // Combine any cached components with the current statements
1623 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001624
Derek Jonesd10e8962010-03-02 17:10:36 -06001625 if (is_null($index))
1626 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001627 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001628 }
1629
1630 if ( ! is_null($set))
1631 {
1632 $this->set_update_batch($set, $index);
1633 }
1634
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001635 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001636 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001637 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001638 }
1639
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001640 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001641 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001642 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001643 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001644 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001645 }
Barry Mienydd671972010-10-04 16:33:58 +02001646
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001647 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001648 }
Barry Mienydd671972010-10-04 16:33:58 +02001649
Derek Jonesd10e8962010-03-02 17:10:36 -06001650 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001651 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001652 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001653 $this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, 100), $this->protect_identifiers($index)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001654 }
Barry Mienydd671972010-10-04 16:33:58 +02001655
Derek Jonesd10e8962010-03-02 17:10:36 -06001656 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001657 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001658 }
1659
1660 // --------------------------------------------------------------------
1661
1662 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001663 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001664 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001665 * @param array
1666 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001667 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001668 * @return object
1669 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001670 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001671 {
1672 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001673
Derek Jonesd10e8962010-03-02 17:10:36 -06001674 if ( ! is_array($key))
1675 {
1676 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001677 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001678
Andrey Andreevfe642da2012-06-16 03:47:33 +03001679 is_bool($escape) OR $escape = $this->_protect_identifiers;
1680
Derek Jonesd10e8962010-03-02 17:10:36 -06001681 foreach ($key as $k => $v)
1682 {
1683 $index_set = FALSE;
1684 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001685 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001686 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001687 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001688 {
1689 $index_set = TRUE;
1690 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001691
Andrey Andreevfe642da2012-06-16 03:47:33 +03001692 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001693 }
1694
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001695 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001696 {
1697 return $this->display_error('db_batch_missing_index');
1698 }
1699
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001700 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001701 }
Barry Mienydd671972010-10-04 16:33:58 +02001702
Derek Jonesd10e8962010-03-02 17:10:36 -06001703 return $this;
1704 }
1705
Derek Allard2067d1a2008-11-13 22:59:24 +00001706 // --------------------------------------------------------------------
1707
1708 /**
1709 * Empty Table
1710 *
1711 * Compiles a delete string and runs "DELETE FROM table"
1712 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001713 * @param string the table to empty
1714 * @return object
1715 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001716 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001717 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001718 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001719 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001720 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001721 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001722 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001723 }
1724
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001725 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001726 }
1727 else
1728 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001729 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001730 }
1731
1732 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001734 return $this->query($sql);
1735 }
1736
1737 // --------------------------------------------------------------------
1738
1739 /**
1740 * Truncate
1741 *
1742 * Compiles a truncate string and runs the query
1743 * If the database does not support the truncate() command
1744 * This function maps to "DELETE FROM table"
1745 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001746 * @param string the table to truncate
1747 * @return object
1748 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001749 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001750 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001751 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001752 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001753 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001754 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001755 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001756 }
1757
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001758 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001759 }
1760 else
1761 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001762 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001763 }
1764
1765 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001766 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001767 return $this->query($sql);
1768 }
WanWizard7219c072011-12-28 14:09:05 +01001769
Kyle Farris0c147b32011-08-26 02:29:31 -04001770 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001771
Kyle Farris0c147b32011-08-26 02:29:31 -04001772 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001773 * Truncate statement
1774 *
1775 * Generates a platform-specific truncate string from the supplied data
1776 *
1777 * If the database does not support the truncate() command,
1778 * then this method maps to 'DELETE FROM table'
1779 *
1780 * @param string the table name
1781 * @return string
1782 */
1783 protected function _truncate($table)
1784 {
1785 return 'TRUNCATE '.$table;
1786 }
1787
1788 // --------------------------------------------------------------------
1789
1790 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001791 * Get DELETE query string
1792 *
1793 * Compiles a delete query string and returns the sql
1794 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001795 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001796 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001797 * @return string
1798 */
1799 public function get_compiled_delete($table = '', $reset = TRUE)
1800 {
1801 $this->return_delete_sql = TRUE;
1802 $sql = $this->delete($table, '', NULL, $reset);
1803 $this->return_delete_sql = FALSE;
1804 return $sql;
1805 }
WanWizard7219c072011-12-28 14:09:05 +01001806
Derek Allard2067d1a2008-11-13 22:59:24 +00001807 // --------------------------------------------------------------------
1808
1809 /**
1810 * Delete
1811 *
1812 * Compiles a delete string and runs the query
1813 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 * @param mixed the table(s) to delete from. String or array
1815 * @param mixed the where clause
1816 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001817 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001818 * @return object
1819 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001820 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001821 {
1822 // Combine any cached components with the current statements
1823 $this->_merge_cache();
1824
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001825 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001826 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001827 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001828 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001829 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001830 }
1831
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001832 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001833 }
1834 elseif (is_array($table))
1835 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001836 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001837 {
1838 $this->delete($single_table, $where, $limit, FALSE);
1839 }
1840
1841 $this->_reset_write();
1842 return;
1843 }
1844 else
1845 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001846 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001847 }
1848
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001849 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001850 {
1851 $this->where($where);
1852 }
1853
Andrey Andreev650b4c02012-06-11 12:07:15 +03001854 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001855 {
1856 $this->limit($limit);
1857 }
1858
Andrey Andreevb0478652012-07-18 15:34:46 +03001859 if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001860 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001861 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001862 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001863
Andrey Andreevb0478652012-07-18 15:34:46 +03001864 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001865 if ($reset_data)
1866 {
1867 $this->_reset_write();
1868 }
WanWizard7219c072011-12-28 14:09:05 +01001869
Andrey Andreev24276a32012-01-08 02:44:38 +02001870 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001871 }
WanWizard7219c072011-12-28 14:09:05 +01001872
Derek Allard2067d1a2008-11-13 22:59:24 +00001873 // --------------------------------------------------------------------
1874
1875 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001876 * Delete statement
1877 *
1878 * Generates a platform-specific delete string from the supplied data
1879 *
1880 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03001881 * @return string
1882 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001883 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03001884 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001885 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevb0478652012-07-18 15:34:46 +03001886 .($this->qb_limit ? ' LIMIT '.(int) $this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001887 }
1888
1889 // --------------------------------------------------------------------
1890
1891 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001892 * DB Prefix
1893 *
1894 * Prepends a database prefix if one exists in configuration
1895 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001896 * @param string the table
1897 * @return string
1898 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001899 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001900 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001901 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001902 {
1903 $this->display_error('db_table_name_required');
1904 }
1905
1906 return $this->dbprefix.$table;
1907 }
1908
1909 // --------------------------------------------------------------------
1910
1911 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001912 * Set DB Prefix
1913 *
1914 * Set's the DB Prefix to something new without needing to reconnect
1915 *
1916 * @param string the prefix
1917 * @return string
1918 */
1919 public function set_dbprefix($prefix = '')
1920 {
1921 return $this->dbprefix = $prefix;
1922 }
1923
1924 // --------------------------------------------------------------------
1925
1926 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001927 * Track Aliases
1928 *
1929 * Used to track SQL statements written with aliased tables.
1930 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001931 * @param string The table to inspect
1932 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001933 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001934 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001935 {
1936 if (is_array($table))
1937 {
1938 foreach ($table as $t)
1939 {
1940 $this->_track_aliases($t);
1941 }
1942 return;
1943 }
Barry Mienydd671972010-10-04 16:33:58 +02001944
Derek Jones37f4b9c2011-07-01 17:56:50 -05001945 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001946 // the string into discreet statements
1947 if (strpos($table, ',') !== FALSE)
1948 {
1949 return $this->_track_aliases(explode(',', $table));
1950 }
Barry Mienydd671972010-10-04 16:33:58 +02001951
Derek Allard2067d1a2008-11-13 22:59:24 +00001952 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001953 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001954 {
1955 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03001956 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001957
Derek Allard2067d1a2008-11-13 22:59:24 +00001958 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001959 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001960
Derek Allard2067d1a2008-11-13 22:59:24 +00001961 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001962 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001963 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001964 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001965 }
1966 }
1967 }
WanWizard7219c072011-12-28 14:09:05 +01001968
Derek Allard2067d1a2008-11-13 22:59:24 +00001969 // --------------------------------------------------------------------
1970
1971 /**
1972 * Compile the SELECT statement
1973 *
1974 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001975 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001976 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001977 * @return string
1978 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001979 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001980 {
1981 // Combine any cached components with the current statements
1982 $this->_merge_cache();
1983
Derek Allard2067d1a2008-11-13 22:59:24 +00001984 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001985 if ($select_override !== FALSE)
1986 {
1987 $sql = $select_override;
1988 }
1989 else
1990 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001991 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001992
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001993 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001994 {
Barry Mienydd671972010-10-04 16:33:58 +02001995 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001996 }
1997 else
Barry Mienydd671972010-10-04 16:33:58 +02001998 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001999 // Cycle through the "select" portion of the query and prep each column name.
2000 // The reason we protect identifiers here rather then in the select() function
2001 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002002 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002003 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002004 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002005 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002006 }
Barry Mienydd671972010-10-04 16:33:58 +02002007
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002008 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002009 }
2010 }
2011
Derek Allard2067d1a2008-11-13 22:59:24 +00002012 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002013 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002014 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002015 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002016 }
2017
Derek Allard2067d1a2008-11-13 22:59:24 +00002018 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002019 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002020 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002021 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002022 }
2023
Andrey Andreevd40459d2012-07-18 16:46:39 +03002024 // WHERE
2025 $sql .= $this->_compile_wh('qb_where');
Derek Allard2067d1a2008-11-13 22:59:24 +00002026
Andrey Andreevb0478652012-07-18 15:34:46 +03002027 // GROUP BY
2028 if (count($this->qb_groupby) > 0)
2029 {
2030 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
2031 }
2032
2033 // HAVING
Andrey Andreevd40459d2012-07-18 16:46:39 +03002034 $sql .= $this->_compile_wh('qb_having');
Andrey Andreevb0478652012-07-18 15:34:46 +03002035
2036 // ORDER BY
2037 if (count($this->qb_orderby) > 0)
2038 {
2039 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
2040 }
2041
Andrey Andreevd40459d2012-07-18 16:46:39 +03002042 // LIMIT
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002043 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002044 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002045 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00002046 }
2047
2048 return $sql;
2049 }
2050
2051 // --------------------------------------------------------------------
2052
2053 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002054 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002055 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002056 * Escapes identifiers in WHERE and HAVING statements at execution time.
2057 *
Andrey Andreevb0478652012-07-18 15:34:46 +03002058 * Required so that aliases are tracked properly, regardless of wether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002059 * where(), or_where(), having(), or_having are called prior to from(),
2060 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002061 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002062 * @param string 'qb_where' or 'qb_having'
2063 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002064 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002065 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002066 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002067 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002068 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002069 $sql = ($qb_key === 'qb_having') ? "\nHAVING " : "\nWHERE ";
Andrey Andreev6e704752012-07-18 00:46:33 +03002070
Andrey Andreevd40459d2012-07-18 16:46:39 +03002071 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002072 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002073 if ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002074 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002075 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002076 continue;
2077 }
2078
Andrey Andreevd40459d2012-07-18 16:46:39 +03002079 $op = preg_quote($this->_get_operator($this->{$qb_key}[$i]['condition']));
2080 if ( ! preg_match('/^(\s*(?:AND|OR)\s+)?(\(?)(.*)('.$op.')(.*(?<!\)))?(\)?)$/i', $this->{$qb_key}[$i]['condition'], $matches))
Andrey Andreev6e704752012-07-18 00:46:33 +03002081 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002082 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002083 continue;
2084 }
2085
2086 // $matches = array(
2087 // 0 => 'OR (test <= foo)', /* the whole thing */
2088 // 1 => 'OR ', /* optional */
2089 // 2 => '(', /* optional */
2090 // 3 => 'test', /* the field name */
2091 // 4 => ' <= ', /* $op */
2092 // 5 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2093 // 6 => ')' /* optional */
2094 // );
2095 empty($matches[5]) OR $matches[5] = ' '.$this->protect_identifiers(trim($matches[5]));
Andrey Andreevd40459d2012-07-18 16:46:39 +03002096 $this->{$qb_key}[$i] = $matches[1].$matches[2].$this->protect_identifiers(trim($matches[3]))
Andrey Andreev6e704752012-07-18 00:46:33 +03002097 .' '.trim($matches[4]).$matches[5].$matches[6];
2098 }
2099
Andrey Andreevd40459d2012-07-18 16:46:39 +03002100 return implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002101 }
2102
Andrey Andreevb0478652012-07-18 15:34:46 +03002103 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002104 }
2105
2106 // --------------------------------------------------------------------
2107
2108 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002109 * Object to Array
2110 *
2111 * Takes an object as input and converts the class variables to array key/vals
2112 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002113 * @param object
2114 * @return array
2115 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002116 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002117 {
2118 if ( ! is_object($object))
2119 {
2120 return $object;
2121 }
Barry Mienydd671972010-10-04 16:33:58 +02002122
Derek Allard2067d1a2008-11-13 22:59:24 +00002123 $array = array();
2124 foreach (get_object_vars($object) as $key => $val)
2125 {
2126 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002127 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002128 {
2129 $array[$key] = $val;
2130 }
2131 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002132
2133 return $array;
2134 }
Barry Mienydd671972010-10-04 16:33:58 +02002135
Derek Jonesd10e8962010-03-02 17:10:36 -06002136 // --------------------------------------------------------------------
2137
2138 /**
2139 * Object to Array
2140 *
2141 * Takes an object as input and converts the class variables to array key/vals
2142 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002143 * @param object
2144 * @return array
2145 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002146 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002147 {
2148 if ( ! is_object($object))
2149 {
2150 return $object;
2151 }
Barry Mienydd671972010-10-04 16:33:58 +02002152
Derek Jonesd10e8962010-03-02 17:10:36 -06002153 $array = array();
2154 $out = get_object_vars($object);
2155 $fields = array_keys($out);
2156
2157 foreach ($fields as $val)
2158 {
2159 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002160 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002161 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002162 $i = 0;
2163 foreach ($out[$val] as $data)
2164 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002165 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002166 }
2167 }
2168 }
2169
Derek Allard2067d1a2008-11-13 22:59:24 +00002170 return $array;
2171 }
Barry Mienydd671972010-10-04 16:33:58 +02002172
Derek Allard2067d1a2008-11-13 22:59:24 +00002173 // --------------------------------------------------------------------
2174
2175 /**
2176 * Start Cache
2177 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002178 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002179 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002180 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002181 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002182 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002183 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002184 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002185 }
2186
2187 // --------------------------------------------------------------------
2188
2189 /**
2190 * Stop Cache
2191 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002192 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002193 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002194 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002195 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002196 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002197 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002198 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002199 }
2200
2201 // --------------------------------------------------------------------
2202
2203 /**
2204 * Flush Cache
2205 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002206 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002207 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002208 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002209 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002210 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002211 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002212 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002213 'qb_cache_select' => array(),
2214 'qb_cache_from' => array(),
2215 'qb_cache_join' => array(),
2216 'qb_cache_where' => array(),
2217 'qb_cache_like' => array(),
2218 'qb_cache_groupby' => array(),
2219 'qb_cache_having' => array(),
2220 'qb_cache_orderby' => array(),
2221 'qb_cache_set' => array(),
2222 'qb_cache_exists' => array(),
2223 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002224 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002225 }
2226
2227 // --------------------------------------------------------------------
2228
2229 /**
2230 * Merge Cache
2231 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002232 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002233 * locally called ones.
2234 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002235 * @return void
2236 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002237 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002238 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002239 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002240 {
2241 return;
2242 }
2243
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002244 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002245 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002246 $qb_variable = 'qb_'.$val;
2247 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002248
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002249 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002250 {
2251 continue;
2252 }
2253
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002254 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002255 }
2256
2257 // If we are "protecting identifiers" we need to examine the "from"
2258 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002259 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002260 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002261 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002262 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002263
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002264 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002265 }
WanWizard7219c072011-12-28 14:09:05 +01002266
Kyle Farris0c147b32011-08-26 02:29:31 -04002267 // --------------------------------------------------------------------
2268
2269 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002270 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002271 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002272 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002273 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002274 * @return void
2275 */
2276 public function reset_query()
2277 {
2278 $this->_reset_select();
2279 $this->_reset_write();
2280 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002281
2282 // --------------------------------------------------------------------
2283
2284 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002285 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002286 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002287 * @param array An array of fields to reset
2288 * @return void
2289 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002290 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002291 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002292 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002293 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002294 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002295 {
2296 $this->$item = $default_value;
2297 }
2298 }
2299 }
2300
2301 // --------------------------------------------------------------------
2302
2303 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002304 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002305 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002306 * @return void
2307 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002308 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002309 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002310 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002311 'qb_select' => array(),
2312 'qb_from' => array(),
2313 'qb_join' => array(),
2314 'qb_where' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002315 'qb_groupby' => array(),
2316 'qb_having' => array(),
2317 'qb_orderby' => array(),
2318 'qb_wherein' => array(),
2319 'qb_aliased_tables' => array(),
2320 'qb_no_escape' => array(),
2321 'qb_distinct' => FALSE,
2322 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002323 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002324 )
2325 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002326 }
Barry Mienydd671972010-10-04 16:33:58 +02002327
Derek Allard2067d1a2008-11-13 22:59:24 +00002328 // --------------------------------------------------------------------
2329
2330 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002331 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002332 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002333 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002334 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002335 * @return void
2336 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002337 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002338 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002339 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002340 'qb_set' => array(),
2341 'qb_from' => array(),
2342 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002343 'qb_orderby' => array(),
2344 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002345 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002346 )
2347 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002348 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002349
Derek Allard2067d1a2008-11-13 22:59:24 +00002350}
2351
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002352/* End of file DB_query_builder.php */
Andrey Andreev58803fb2012-06-24 00:45:37 +03002353/* Location: ./system/database/DB_query_builder.php */