blob: 92cb8c1d57823f34dba38a73bd0c73275a733a00 [file] [log] [blame]
Andrey Andreev24276a32012-01-08 02:44:38 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
WanWizard7219c072011-12-28 14:09:05 +01008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
WanWizard7219c072011-12-28 14:09:05 +010010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
Jamie Rumbelow7efad202012-02-19 12:37:00 +000029 * Query Builder Class
Derek Allard2067d1a2008-11-13 22:59:24 +000030 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +000031 * This is the platform-independent base Query Builder implementation class.
Derek Allard2067d1a2008-11-13 22:59:24 +000032 *
33 * @package CodeIgniter
34 * @subpackage Drivers
35 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/database/
38 */
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +010039
40abstract class CI_DB_query_builder extends CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000041
WanWizard7219c072011-12-28 14:09:05 +010042 protected $return_delete_sql = FALSE;
43 protected $reset_delete_data = FALSE;
44
Jamie Rumbelow7efad202012-02-19 12:37:00 +000045 protected $qb_select = array();
46 protected $qb_distinct = FALSE;
47 protected $qb_from = array();
48 protected $qb_join = array();
49 protected $qb_where = array();
50 protected $qb_like = array();
51 protected $qb_groupby = array();
52 protected $qb_having = array();
53 protected $qb_keys = array();
54 protected $qb_limit = FALSE;
55 protected $qb_offset = FALSE;
Jamie Rumbelow7efad202012-02-19 12:37:00 +000056 protected $qb_orderby = array();
57 protected $qb_set = array();
58 protected $qb_wherein = array();
59 protected $qb_aliased_tables = array();
60 protected $qb_store_array = array();
61 protected $qb_where_group_started = FALSE;
62 protected $qb_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +020063
Jamie Rumbelow7efad202012-02-19 12:37:00 +000064 // Query Builder Caching variables
65 protected $qb_caching = FALSE;
66 protected $qb_cache_exists = array();
67 protected $qb_cache_select = array();
68 protected $qb_cache_from = array();
69 protected $qb_cache_join = array();
70 protected $qb_cache_where = array();
71 protected $qb_cache_like = array();
72 protected $qb_cache_groupby = array();
73 protected $qb_cache_having = array();
74 protected $qb_cache_orderby = array();
75 protected $qb_cache_set = array();
WanWizard7219c072011-12-28 14:09:05 +010076
Jamie Rumbelow7efad202012-02-19 12:37:00 +000077 protected $qb_no_escape = array();
78 protected $qb_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000079
80 /**
81 * Select
82 *
83 * Generates the SELECT portion of the query
84 *
Derek Allard2067d1a2008-11-13 22:59:24 +000085 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +030086 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +000087 * @return object
88 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060089 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000090 {
Derek Allard2067d1a2008-11-13 22:59:24 +000091 if (is_string($select))
92 {
93 $select = explode(',', $select);
94 }
95
Andrey Andreev42870232012-06-12 01:30:20 +030096 // If the escape value was not set will will base it on the global setting
97 is_bool($escape) OR $escape = $this->_protect_identifiers;
98
Derek Allard2067d1a2008-11-13 22:59:24 +000099 foreach ($select as $val)
100 {
101 $val = trim($val);
102
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100103 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000105 $this->qb_select[] = $val;
106 $this->qb_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000107
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000108 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000110 $this->qb_cache_select[] = $val;
111 $this->qb_cache_exists[] = 'select';
112 $this->qb_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
114 }
115 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 return $this;
118 }
119
120 // --------------------------------------------------------------------
121
122 /**
123 * Select Max
124 *
125 * Generates a SELECT MAX(field) portion of a query
126 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 * @param string the field
128 * @param string an alias
129 * @return object
130 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600131 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 {
133 return $this->_max_min_avg_sum($select, $alias, 'MAX');
134 }
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 // --------------------------------------------------------------------
137
138 /**
139 * Select Min
140 *
141 * Generates a SELECT MIN(field) portion of a query
142 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 * @param string the field
144 * @param string an alias
145 * @return object
146 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600147 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 {
149 return $this->_max_min_avg_sum($select, $alias, 'MIN');
150 }
151
152 // --------------------------------------------------------------------
153
154 /**
155 * Select Average
156 *
157 * Generates a SELECT AVG(field) portion of a query
158 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 * @param string the field
160 * @param string an alias
161 * @return object
162 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600163 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
165 return $this->_max_min_avg_sum($select, $alias, 'AVG');
166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Select Sum
172 *
173 * Generates a SELECT SUM(field) portion of a query
174 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 * @param string the field
176 * @param string an alias
177 * @return object
178 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600179 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
181 return $this->_max_min_avg_sum($select, $alias, 'SUM');
182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Processing Function for the four functions above:
188 *
189 * select_max()
190 * select_min()
191 * select_avg()
Andrey Andreev24276a32012-01-08 02:44:38 +0200192 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200193 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 * @param string the field
195 * @param string an alias
196 * @return object
197 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600198 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100200 if ( ! is_string($select) OR $select === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
202 $this->display_error('db_invalid_query');
203 }
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
208 {
209 show_error('Invalid function type: '.$type);
210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100212 if ($alias === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
214 $alias = $this->_create_alias_from_table(trim($select));
215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300217 $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->escape_identifiers(trim($alias));
218
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000219 $this->qb_select[] = $sql;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100220 $this->qb_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200221
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000222 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000224 $this->qb_cache_select[] = $sql;
225 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 return $this;
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Determines the alias name based on the table
235 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 * @param string
237 * @return string
238 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600239 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
241 if (strpos($item, '.') !== FALSE)
242 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200243 $item = explode('.', $item);
244 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 return $item;
248 }
249
250 // --------------------------------------------------------------------
251
252 /**
253 * DISTINCT
254 *
255 * Sets a flag which tells the query string compiler to add DISTINCT
256 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 * @param bool
258 * @return object
259 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600260 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300262 $this->qb_distinct = is_bool($val) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 return $this;
264 }
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 // --------------------------------------------------------------------
267
268 /**
269 * From
270 *
271 * Generates the FROM portion of the query
272 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * @param mixed can be a string or array
274 * @return object
275 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600276 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300278 foreach ((array) $from as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 if (strpos($val, ',') !== FALSE)
281 {
282 foreach (explode(',', $val) as $v)
283 {
284 $v = trim($v);
285 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200286
George Petsagourakis193d4482012-04-28 11:16:18 +0300287 $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000288
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000289 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000291 $this->qb_cache_from[] = $v;
292 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200293 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
296 else
297 {
298 $val = trim($val);
299
Andrey Andreev24276a32012-01-08 02:44:38 +0200300 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000301 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200303
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000304 $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000305
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000306 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000308 $this->qb_cache_from[] = $val;
309 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 }
311 }
312 }
313
314 return $this;
315 }
316
317 // --------------------------------------------------------------------
318
319 /**
320 * Join
321 *
322 * Generates the JOIN portion of the query
323 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * @param string
325 * @param string the join condition
326 * @param string the type of join
Alex Bilbief512b732012-06-16 11:15:19 +0100327 * @param string whether not to try to escape identifiers
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 * @return object
329 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300330 public function join($table, $cond, $type = '', $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200331 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100332 if ($type !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 $type = strtoupper(trim($type));
335
Andrey Andreev42870232012-06-12 01:30:20 +0300336 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 $type = '';
339 }
340 else
341 {
342 $type .= ' ';
343 }
344 }
345
Andrey Andreev24276a32012-01-08 02:44:38 +0200346 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000347 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 $this->_track_aliases($table);
349
Andrey Andreevfe642da2012-06-16 03:47:33 +0300350 is_bool($escape) OR $escape = $this->_protect_identifiers;
351
Andrey Andreev42870232012-06-12 01:30:20 +0300352 // Split multiple conditions
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300353 if ($escape === TRUE && preg_match_all('/\sAND\s|\sOR\s/i', $cond, $m, PREG_OFFSET_CAPTURE))
Andrey Andreev42870232012-06-12 01:30:20 +0300354 {
355 $newcond = '';
356 $m[0][] = array('', strlen($cond));
357
358 for ($i = 0, $c = count($m[0]), $s = 0;
359 $i < $c;
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300360 $s = $m[0][$i][1] + strlen($m[0][$i][0]), $i++)
Andrey Andreev42870232012-06-12 01:30:20 +0300361 {
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300362 $temp = substr($cond, $s, ($m[0][$i][1] - $s));
Andrey Andreev42870232012-06-12 01:30:20 +0300363
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300364 $newcond .= preg_match("/([\[\]\w\.'-]+)(\s*[^\"\[`'\w]+\s*)(.+)/i", $temp, $match)
Andrey Andreev42870232012-06-12 01:30:20 +0300365 ? $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3])
366 : $temp;
367
368 $newcond .= $m[0][$i][0];
369 }
370
Andrey Andreev3751f932012-06-17 18:07:48 +0300371 $cond = ' ON '.$newcond;
Andrey Andreev42870232012-06-12 01:30:20 +0300372 }
373 // Split apart the condition and protect the identifiers
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300374 elseif ($escape === TRUE && preg_match("/([\[\]\w\.'-]+)(\s*[^\"\[`'\w]+\s*)(.+)/i", $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
Andrey Andreev3751f932012-06-17 18:07:48 +0300376 $cond = ' ON '.$this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
377 }
378 elseif ( ! $this->_has_operator($cond))
379 {
380 $cond = ' USING ('.($escape ? $this->escape_identifiers($cond) : $cond).')';
381 }
382 else
383 {
384 $cond = ' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Andrey Andreev42870232012-06-12 01:30:20 +0300387 // Do we want to escape the table name?
388 if ($escape === TRUE)
389 {
390 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
391 }
392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 // Assemble the JOIN statement
Andrey Andreev3751f932012-06-17 18:07:48 +0300394 $this->qb_join[] = $join = $type.'JOIN '.$table.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000395
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000396 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000398 $this->qb_cache_join[] = $join;
399 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 }
401
402 return $this;
403 }
404
405 // --------------------------------------------------------------------
406
407 /**
408 * Where
409 *
410 * Generates the WHERE portion of the query. Separates
411 * multiple calls with AND
412 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 * @param mixed
414 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300415 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 * @return object
417 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300418 public function where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
420 return $this->_where($key, $value, 'AND ', $escape);
421 }
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 // --------------------------------------------------------------------
424
425 /**
426 * OR Where
427 *
428 * Generates the WHERE portion of the query. Separates
429 * multiple calls with OR
430 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 * @param mixed
432 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300433 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 * @return object
435 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300436 public function or_where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
438 return $this->_where($key, $value, 'OR ', $escape);
439 }
440
441 // --------------------------------------------------------------------
442
443 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 * Where
445 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600446 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 * @param mixed
449 * @param mixed
450 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300451 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 * @return object
453 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600454 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 {
456 if ( ! is_array($key))
457 {
458 $key = array($key => $value);
459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 // If the escape value was not set will will base it on the global setting
Andrey Andreeve10fb792012-06-15 12:07:04 +0300462 is_bool($escape) OR $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000463
464 foreach ($key as $k => $v)
465 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300466 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
467 ? $this->_group_get_type('')
468 : $this->_group_get_type($type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000469
470 if (is_null($v) && ! $this->_has_operator($k))
471 {
472 // value appears not to have been set, assign the test to IS NULL
473 $k .= ' IS NULL';
474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 if ( ! is_null($v))
477 {
478 if ($escape === TRUE)
479 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 $v = ' '.$this->escape($v);
481 }
WanWizard7219c072011-12-28 14:09:05 +0100482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 if ( ! $this->_has_operator($k))
484 {
Greg Akere156c6e2011-04-20 16:03:04 -0500485 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 }
487 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000488
Andrey Andreev6e704752012-07-18 00:46:33 +0300489 $this->qb_where[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000490 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300492 // check this shit
493 $this->qb_cache_where[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000494 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 return $this;
500 }
501
502 // --------------------------------------------------------------------
503
504 /**
505 * Where_in
506 *
507 * Generates a WHERE field IN ('item', 'item') SQL query joined with
508 * AND if appropriate
509 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 * @param string The field to search
511 * @param array The values searched on
512 * @return object
513 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300514 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300516 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 }
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 // --------------------------------------------------------------------
520
521 /**
522 * Where_in_or
523 *
524 * Generates a WHERE field IN ('item', 'item') SQL query joined with
525 * OR if appropriate
526 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 * @param string The field to search
528 * @param array The values searched on
529 * @return object
530 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300531 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300533 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 }
535
536 // --------------------------------------------------------------------
537
538 /**
539 * Where_not_in
540 *
541 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
542 * with AND if appropriate
543 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 * @param string The field to search
545 * @param array The values searched on
546 * @return object
547 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300548 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300550 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 // --------------------------------------------------------------------
554
555 /**
556 * Where_not_in_or
557 *
558 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
559 * with OR if appropriate
560 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 * @param string The field to search
562 * @param array The values searched on
563 * @return object
564 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300565 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300567 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 }
569
570 // --------------------------------------------------------------------
571
572 /**
573 * Where_in
574 *
575 * Called by where_in, where_in_or, where_not_in, where_not_in_or
576 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 * @param string The field to search
578 * @param array The values searched on
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300579 * @param bool If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200580 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 * @return object
582 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300583 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 {
585 if ($key === NULL OR $values === NULL)
586 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300587 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 }
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 if ( ! is_array($values))
591 {
592 $values = array($values);
593 }
Barry Mienydd671972010-10-04 16:33:58 +0200594
Andrey Andreev498c1e02012-06-16 03:34:10 +0300595 is_bool($escape) OR $escape = $this->_protect_identifiers;
596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 $not = ($not) ? ' NOT' : '';
598
599 foreach ($values as $value)
600 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000601 $this->qb_wherein[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 }
603
WanWizardbc69f362012-06-22 00:10:11 +0200604 $prefix = (count($this->qb_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Andrey Andreev6e704752012-07-18 00:46:33 +0300605 $where_in = array(
606 'condition' => $prefix.$key.$not.' IN('.implode(', ', $this->qb_wherein).')',
607 'escape' => $escape
608 );
Barry Mienydd671972010-10-04 16:33:58 +0200609
Andrey Andreev6e704752012-07-18 00:46:33 +0300610 $this->qb_where[] = $where_in;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000611 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000613 $this->qb_cache_where[] = $where_in;
614 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 }
616
617 // reset the array for multiple calls
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000618 $this->qb_wherein = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 return $this;
620 }
Barry Mienydd671972010-10-04 16:33:58 +0200621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 // --------------------------------------------------------------------
623
624 /**
625 * Like
626 *
627 * Generates a %LIKE% portion of the query. Separates
628 * multiple calls with AND
629 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 * @param mixed
631 * @param mixed
632 * @return object
633 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600634 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
636 return $this->_like($field, $match, 'AND ', $side);
637 }
638
639 // --------------------------------------------------------------------
640
641 /**
642 * Not Like
643 *
644 * Generates a NOT LIKE portion of the query. Separates
645 * multiple calls with AND
646 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 * @param mixed
648 * @param mixed
649 * @return object
650 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600651 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 {
653 return $this->_like($field, $match, 'AND ', $side, 'NOT');
654 }
Barry Mienydd671972010-10-04 16:33:58 +0200655
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 // --------------------------------------------------------------------
657
658 /**
659 * OR Like
660 *
661 * Generates a %LIKE% portion of the query. Separates
662 * multiple calls with OR
663 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 * @param mixed
665 * @param mixed
666 * @return object
667 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600668 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 {
670 return $this->_like($field, $match, 'OR ', $side);
671 }
672
673 // --------------------------------------------------------------------
674
675 /**
676 * OR Not Like
677 *
678 * Generates a NOT LIKE portion of the query. Separates
679 * multiple calls with OR
680 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 * @param mixed
682 * @param mixed
683 * @return object
684 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600685 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 {
687 return $this->_like($field, $match, 'OR ', $side, 'NOT');
688 }
Barry Mienydd671972010-10-04 16:33:58 +0200689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 // --------------------------------------------------------------------
691
692 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 * Like
694 *
695 * Called by like() or orlike()
696 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 * @param mixed
698 * @param mixed
699 * @param string
700 * @return object
701 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600702 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 {
704 if ( ! is_array($field))
705 {
706 $field = array($field => $match);
707 }
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 foreach ($field as $k => $v)
710 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000711 $k = $this->protect_identifiers($k);
WanWizardbc69f362012-06-22 00:10:11 +0200712 $prefix = (count($this->qb_like) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Derek Jonese4ed5832009-02-20 21:44:59 +0000713 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300714
Andrey Andreev24276a32012-01-08 02:44:38 +0200715 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400716 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100717 $like_statement = "{$prefix} $k $not LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400718 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200719 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100721 $like_statement = "{$prefix} $k $not LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200723 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100725 $like_statement = "{$prefix} $k $not LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 }
727 else
728 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100729 $like_statement = "{$prefix} $k $not LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600731
Derek Jonese4ed5832009-02-20 21:44:59 +0000732 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100733 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000734 {
Greg Aker0d424892010-01-26 02:14:44 +0000735 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000736 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600737
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000738 $this->qb_like[] = $like_statement;
739 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000741 $this->qb_cache_like[] = $like_statement;
742 $this->qb_cache_exists[] = 'like';
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 }
Barry Mienydd671972010-10-04 16:33:58 +0200744
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200746
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 return $this;
748 }
Barry Mienydd671972010-10-04 16:33:58 +0200749
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 // --------------------------------------------------------------------
751
752 /**
WanWizard7219c072011-12-28 14:09:05 +0100753 * Starts a query group.
754 *
755 * @param string (Internal use only)
756 * @param string (Internal use only)
757 * @return object
758 */
759 public function group_start($not = '', $type = 'AND ')
760 {
761 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100762
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000763 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100764 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Andrey Andreev6e704752012-07-18 00:46:33 +0300765 $where = array(
766 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (',
767 'escape' => FALSE
768 );
WanWizard7219c072011-12-28 14:09:05 +0100769
Andrey Andreev6e704752012-07-18 00:46:33 +0300770 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000771 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100772 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300773 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +0100774 }
775
776 return $this;
777 }
778
779 // --------------------------------------------------------------------
780
781 /**
782 * Starts a query group, but ORs the group
783 *
784 * @return object
785 */
786 public function or_group_start()
787 {
788 return $this->group_start('', 'OR ');
789 }
790
791 // --------------------------------------------------------------------
792
793 /**
794 * Starts a query group, but NOTs the group
795 *
796 * @return object
797 */
798 public function not_group_start()
799 {
800 return $this->group_start('NOT ', 'AND ');
801 }
802
803 // --------------------------------------------------------------------
804
805 /**
806 * Starts a query group, but OR NOTs the group
807 *
808 * @return object
809 */
810 public function or_not_group_start()
811 {
812 return $this->group_start('NOT ', 'OR ');
813 }
814
815 // --------------------------------------------------------------------
816
817 /**
818 * Ends a query group
819 *
820 * @return object
821 */
822 public function group_end()
823 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000824 $this->qb_where_group_started = FALSE;
Andrey Andreev6e704752012-07-18 00:46:33 +0300825 $where = array(
826 'condition' => str_repeat(' ', $this->qb_where_group_count--).')',
827 'escape' => FALSE
828 );
WanWizard7219c072011-12-28 14:09:05 +0100829
Andrey Andreev6e704752012-07-18 00:46:33 +0300830 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000831 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100832 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300833 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +0100834 }
835
WanWizard7219c072011-12-28 14:09:05 +0100836 return $this;
837 }
838
839 // --------------------------------------------------------------------
840
841 /**
842 * Group_get_type
843 *
844 * Called by group_start(), _like(), _where() and _where_in()
845 *
846 * @param string
847 * @return string
848 */
849 protected function _group_get_type($type)
850 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000851 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100852 {
853 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000854 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100855 }
856
857 return $type;
858 }
859
860 // --------------------------------------------------------------------
861
862 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 * GROUP BY
864 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 * @param string
866 * @return object
867 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600868 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 {
870 if (is_string($by))
871 {
872 $by = explode(',', $by);
873 }
Barry Mienydd671972010-10-04 16:33:58 +0200874
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 foreach ($by as $val)
876 {
877 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200878
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100879 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000881 $this->qb_groupby[] = $val = $this->protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200882
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000883 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000885 $this->qb_cache_groupby[] = $val;
886 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 }
888 }
889 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200890
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 return $this;
892 }
893
894 // --------------------------------------------------------------------
895
896 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 * Sets the HAVING value
898 *
899 * Separates multiple calls with AND
900 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 * @param string
902 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300903 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 * @return object
905 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300906 public function having($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 {
908 return $this->_having($key, $value, 'AND ', $escape);
909 }
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 // --------------------------------------------------------------------
912
913 /**
914 * Sets the OR HAVING value
915 *
916 * Separates multiple calls with OR
917 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 * @param string
919 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300920 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 * @return object
922 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300923 public function or_having($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 {
925 return $this->_having($key, $value, 'OR ', $escape);
926 }
Barry Mienydd671972010-10-04 16:33:58 +0200927
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 // --------------------------------------------------------------------
929
930 /**
931 * Sets the HAVING values
932 *
933 * Called by having() or or_having()
934 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 * @param string
936 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300937 * @param string
938 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 * @return object
940 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300941 protected function _having($key, $value = '', $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 {
943 if ( ! is_array($key))
944 {
945 $key = array($key => $value);
946 }
Barry Mienydd671972010-10-04 16:33:58 +0200947
Andrey Andreevfe642da2012-06-16 03:47:33 +0300948 is_bool($escape) OR $escape = $this->_protect_identifiers;
949
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 foreach ($key as $k => $v)
951 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000952 $prefix = (count($this->qb_having) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000953
Andrey Andreev974c75b2012-06-15 12:30:02 +0300954 $k = $this->_has_operator($k)
955 ? $this->protect_identifiers(substr($k, 0, strpos(rtrim($k), ' ')), FALSE, $escape).strchr(rtrim($k), ' ')
956 : $this->protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000957
958 if ( ! $this->_has_operator($k))
959 {
960 $k .= ' = ';
961 }
962
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100963 if ($v !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400965 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 }
Barry Mienydd671972010-10-04 16:33:58 +0200967
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000968 $this->qb_having[] = $prefix.$k.$v;
969 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000971 $this->qb_cache_having[] = $prefix.$k.$v;
972 $this->qb_cache_exists[] = 'having';
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 }
974 }
Barry Mienydd671972010-10-04 16:33:58 +0200975
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 return $this;
977 }
Barry Mienydd671972010-10-04 16:33:58 +0200978
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 // --------------------------------------------------------------------
980
981 /**
982 * Sets the ORDER BY value
983 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 * @param string
985 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100986 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 * @return object
988 */
Andrey Andreevd24160c2012-06-16 03:21:20 +0300989 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200991 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 {
993 $orderby = ''; // Random results want or don't need a field name
994 $direction = $this->_random_keyword;
995 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100996 elseif (trim($direction) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 {
Andrey Andreev650b4c02012-06-11 12:07:15 +0300998 $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : ' ASC';
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 }
Barry Mienydd671972010-10-04 16:33:58 +02001000
Andrey Andreevd24160c2012-06-16 03:21:20 +03001001 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +02001002
Andrey Andreevd24160c2012-06-16 03:21:20 +03001003 if ($escape === TRUE && strpos($orderby, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 {
1005 $temp = array();
1006 foreach (explode(',', $orderby) as $part)
1007 {
1008 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001009 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 {
Andrey Andreev88cb2782012-06-11 20:40:50 +03001011 $part = preg_match('/^(.+)\s+(ASC|DESC)$/i', $part, $matches)
1012 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
1013 : $this->protect_identifiers($part);
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 }
Barry Mienydd671972010-10-04 16:33:58 +02001015
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 $temp[] = $part;
1017 }
Barry Mienydd671972010-10-04 16:33:58 +02001018
1019 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001021 elseif ($direction !== $this->_random_keyword && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 {
Andrey Andreev1d1d7ff2012-06-11 22:35:35 +03001023 $orderby = preg_match('/^(.+)\s+(ASC|DESC)$/i', $orderby, $matches)
Andrey Andreev88cb2782012-06-11 20:40:50 +03001024 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
1025 : $this->protect_identifiers($orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 }
Barry Mienydd671972010-10-04 16:33:58 +02001027
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001028 $this->qb_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +02001029
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001030 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001032 $this->qb_cache_orderby[] = $orderby_statement;
1033 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 }
1035
1036 return $this;
1037 }
Barry Mienydd671972010-10-04 16:33:58 +02001038
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 // --------------------------------------------------------------------
1040
1041 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 * Sets the LIMIT value
1043 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001044 * @param int the limit value
1045 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 * @return object
1047 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +02001048 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001050 is_null($value) OR $this->qb_limit = (int) $value;
1051 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001052
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 return $this;
1054 }
Barry Mienydd671972010-10-04 16:33:58 +02001055
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 // --------------------------------------------------------------------
1057
1058 /**
1059 * Sets the OFFSET value
1060 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001061 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 * @return object
1063 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001064 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001066 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 return $this;
1068 }
Barry Mienydd671972010-10-04 16:33:58 +02001069
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 // --------------------------------------------------------------------
1071
1072 /**
Andrey Andreev2c35b642012-06-24 03:05:26 +03001073 * Limit string
1074 *
1075 * Generates a platform-specific LIMIT clause
1076 *
1077 * @param string the sql query string
1078 * @param int the number of rows to limit the query to
1079 * @param int the offset value
1080 * @return string
1081 */
1082 protected function _limit($sql, $limit, $offset)
1083 {
1084 return $sql.' LIMIT '.($offset ? $offset.', ' : '').$limit;
1085 }
1086
1087 // --------------------------------------------------------------------
1088
1089 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001090 * The "set" function.
1091 *
1092 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 * @param mixed
1095 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001096 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 * @return object
1098 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001099 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 {
1101 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001102
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 if ( ! is_array($key))
1104 {
1105 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001106 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001107
Andrey Andreevfe642da2012-06-16 03:47:33 +03001108 is_bool($escape) OR $escape = $this->_protect_identifiers;
1109
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 foreach ($key as $k => $v)
1111 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001112 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1113 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 }
Barry Mienydd671972010-10-04 16:33:58 +02001115
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 return $this;
1117 }
WanWizard7219c072011-12-28 14:09:05 +01001118
Kyle Farris0c147b32011-08-26 02:29:31 -04001119 // --------------------------------------------------------------------
1120
1121 /**
1122 * Get SELECT query string
1123 *
1124 * Compiles a SELECT query string and returns the sql.
1125 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001126 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001127 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001128 * @return string
1129 */
WanWizard7219c072011-12-28 14:09:05 +01001130 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001131 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001132 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001133 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001134 $this->_track_aliases($table);
1135 $this->from($table);
1136 }
WanWizard7219c072011-12-28 14:09:05 +01001137
Andrey Andreev650b4c02012-06-11 12:07:15 +03001138 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001139
Kyle Farris0c147b32011-08-26 02:29:31 -04001140 if ($reset === TRUE)
1141 {
1142 $this->_reset_select();
1143 }
WanWizard7219c072011-12-28 14:09:05 +01001144
Kyle Farris0c147b32011-08-26 02:29:31 -04001145 return $select;
1146 }
WanWizard7219c072011-12-28 14:09:05 +01001147
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 // --------------------------------------------------------------------
1149
1150 /**
1151 * Get
1152 *
1153 * Compiles the select statement based on the other functions called
1154 * and runs the query
1155 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 * @param string the table
1157 * @param string the limit clause
1158 * @param string the offset clause
1159 * @return object
1160 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001161 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001163 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 {
1165 $this->_track_aliases($table);
1166 $this->from($table);
1167 }
Barry Mienydd671972010-10-04 16:33:58 +02001168
Andrey Andreev650b4c02012-06-11 12:07:15 +03001169 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 {
1171 $this->limit($limit, $offset);
1172 }
Barry Mienydd671972010-10-04 16:33:58 +02001173
Andrey Andreev24276a32012-01-08 02:44:38 +02001174 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 $this->_reset_select();
1176 return $result;
1177 }
1178
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001179 // --------------------------------------------------------------------
1180
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 /**
1182 * "Count All Results" query
1183 *
Barry Mienydd671972010-10-04 16:33:58 +02001184 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001185 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 * @param string
1188 * @return string
1189 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001190 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001192 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 {
1194 $this->_track_aliases($table);
1195 $this->from($table);
1196 }
Barry Mienydd671972010-10-04 16:33:58 +02001197
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001198 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001200
Purwandi1d160e72012-01-09 16:33:28 +07001201 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001203 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 }
1205
Purwandi1d160e72012-01-09 16:33:28 +07001206 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001207 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001209
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 // --------------------------------------------------------------------
1211
1212 /**
1213 * Get_Where
1214 *
1215 * Allows the where clause, limit and offset to be added directly
1216 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 * @param string the where clause
1218 * @param string the limit clause
1219 * @param string the offset clause
1220 * @return object
1221 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001222 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001224 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 {
1226 $this->from($table);
1227 }
1228
1229 if ( ! is_null($where))
1230 {
1231 $this->where($where);
1232 }
Barry Mienydd671972010-10-04 16:33:58 +02001233
Andrey Andreev650b4c02012-06-11 12:07:15 +03001234 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001235 {
1236 $this->limit($limit, $offset);
1237 }
Barry Mienydd671972010-10-04 16:33:58 +02001238
Andrey Andreev24276a32012-01-08 02:44:38 +02001239 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 $this->_reset_select();
1241 return $result;
1242 }
1243
1244 // --------------------------------------------------------------------
1245
1246 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001247 * Insert_Batch
1248 *
1249 * Compiles batch insert strings and runs the queries
1250 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001251 * @param string the table to retrieve the results from
1252 * @param array an associative array of insert values
1253 * @return object
1254 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001255 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001256 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001257 if ( ! is_null($set))
1258 {
1259 $this->set_insert_batch($set);
1260 }
Barry Mienydd671972010-10-04 16:33:58 +02001261
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001262 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001263 {
1264 if ($this->db_debug)
1265 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001266 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001267 return $this->display_error('db_must_use_set');
1268 }
1269 return FALSE;
1270 }
1271
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001272 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001273 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001274 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001275 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001276 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001277 }
Barry Mienydd671972010-10-04 16:33:58 +02001278
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001279 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001280 }
1281
1282 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001283 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001284 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001285 $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 -06001286 }
Barry Mienydd671972010-10-04 16:33:58 +02001287
Derek Jonesd10e8962010-03-02 17:10:36 -06001288 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001289 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001290 }
1291
1292 // --------------------------------------------------------------------
1293
1294 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001295 * Insert_batch statement
1296 *
1297 * Generates a platform-specific insert string from the supplied data.
1298 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001299 * @param string the table name
1300 * @param array the insert keys
1301 * @param array the insert values
1302 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001303 */
1304 protected function _insert_batch($table, $keys, $values)
1305 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001306 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001307 }
1308
1309 // --------------------------------------------------------------------
1310
1311 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001312 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001313 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001314 * @param mixed
1315 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001316 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001317 * @return object
1318 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001319 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001320 {
1321 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001322
Derek Jonesd10e8962010-03-02 17:10:36 -06001323 if ( ! is_array($key))
1324 {
1325 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001326 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001327
Andrey Andreevfe642da2012-06-16 03:47:33 +03001328 is_bool($escape) OR $escape = $this->_protect_identifiers;
1329
Iban Eguia3c0a4522012-04-15 13:30:44 +02001330 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001331 sort($keys);
1332
1333 foreach ($key as $row)
1334 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001335 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001336 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1337 {
1338 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001339 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001340 return;
1341 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001342
Barry Mienydd671972010-10-04 16:33:58 +02001343 ksort($row); // puts $row in the same order as our keys
1344
Andrey Andreev650b4c02012-06-11 12:07:15 +03001345 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001346 {
1347 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001348 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001349 {
Barry Mienydd671972010-10-04 16:33:58 +02001350 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001351 }
1352
Andrey Andreev650b4c02012-06-11 12:07:15 +03001353 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001354 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001355
1356 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001357 }
1358
1359 foreach ($keys as $k)
1360 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001361 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001362 }
Barry Mienydd671972010-10-04 16:33:58 +02001363
Derek Jonesd10e8962010-03-02 17:10:36 -06001364 return $this;
1365 }
WanWizard7219c072011-12-28 14:09:05 +01001366
Kyle Farris0c147b32011-08-26 02:29:31 -04001367 // --------------------------------------------------------------------
1368
1369 /**
1370 * Get INSERT query string
1371 *
1372 * Compiles an insert query and returns the sql
1373 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001374 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001375 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001376 * @return string
1377 */
1378 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001379 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001380 if ($this->_validate_insert($table) === FALSE)
1381 {
1382 return FALSE;
1383 }
WanWizard7219c072011-12-28 14:09:05 +01001384
Kyle Farris76116012011-08-31 11:17:48 -04001385 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001386 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001387 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001388 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001389 array_keys($this->qb_set),
1390 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001391 );
WanWizard7219c072011-12-28 14:09:05 +01001392
Kyle Farris0c147b32011-08-26 02:29:31 -04001393 if ($reset === TRUE)
1394 {
1395 $this->_reset_write();
1396 }
WanWizard7219c072011-12-28 14:09:05 +01001397
Kyle Farris0c147b32011-08-26 02:29:31 -04001398 return $sql;
1399 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001400
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 // --------------------------------------------------------------------
1402
1403 /**
1404 * Insert
1405 *
1406 * Compiles an insert string and runs the query
1407 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001408 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001409 * @param array an associative array of insert values
1410 * @return object
1411 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001412 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001413 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001414 if ( ! is_null($set))
1415 {
1416 $this->set($set);
1417 }
WanWizard7219c072011-12-28 14:09:05 +01001418
Kyle Farris0c147b32011-08-26 02:29:31 -04001419 if ($this->_validate_insert($table) === FALSE)
1420 {
1421 return FALSE;
1422 }
WanWizard7219c072011-12-28 14:09:05 +01001423
Kyle Farris76116012011-08-31 11:17:48 -04001424 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001425 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001426 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001427 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001428 array_keys($this->qb_set),
1429 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001430 );
Barry Mienydd671972010-10-04 16:33:58 +02001431
Kyle Farris0c147b32011-08-26 02:29:31 -04001432 $this->_reset_write();
1433 return $this->query($sql);
1434 }
WanWizard7219c072011-12-28 14:09:05 +01001435
Kyle Farris0c147b32011-08-26 02:29:31 -04001436 // --------------------------------------------------------------------
1437
1438 /**
1439 * Validate Insert
1440 *
1441 * This method is used by both insert() and get_compiled_insert() to
1442 * validate that the there data is actually being set and that table
1443 * has been chosen to be inserted into.
1444 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001445 * @param string the table to insert data into
1446 * @return string
1447 */
WanWizard7219c072011-12-28 14:09:05 +01001448 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001449 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001450 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001451 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001452 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001453 }
1454
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001455 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001456 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001457 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001458 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001459 elseif ( ! isset($this->qb_from[0]))
1460 {
1461 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1462 }
WanWizard7219c072011-12-28 14:09:05 +01001463
Kyle Farris0c147b32011-08-26 02:29:31 -04001464 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001465 }
Barry Mienydd671972010-10-04 16:33:58 +02001466
Phil Sturgeon9789f322011-07-15 15:14:05 -06001467 // --------------------------------------------------------------------
1468
1469 /**
1470 * Replace
1471 *
1472 * Compiles an replace into string and runs the query
1473 *
1474 * @param string the table to replace data into
1475 * @param array an associative array of insert values
1476 * @return object
1477 */
1478 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001479 {
1480 if ( ! is_null($set))
1481 {
1482 $this->set($set);
1483 }
Barry Mienydd671972010-10-04 16:33:58 +02001484
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001485 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001486 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001487 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001488 }
1489
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001490 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001491 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001492 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001493 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001494 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001495 }
Barry Mienydd671972010-10-04 16:33:58 +02001496
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001497 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001498 }
1499
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001500 $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 +00001501
Derek Jonesd10e8962010-03-02 17:10:36 -06001502 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001503 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001504 }
WanWizard7219c072011-12-28 14:09:05 +01001505
Kyle Farris0c147b32011-08-26 02:29:31 -04001506 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001507
Kyle Farris0c147b32011-08-26 02:29:31 -04001508 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001509 * Replace statement
1510 *
1511 * Generates a platform-specific replace string from the supplied data
1512 *
1513 * @param string the table name
1514 * @param array the insert keys
1515 * @param array the insert values
1516 * @return string
1517 */
1518 protected function _replace($table, $keys, $values)
1519 {
1520 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1521 }
1522
1523 // --------------------------------------------------------------------
1524
1525 /**
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001526 * From Tables
1527 *
1528 * This public function implicitly groups FROM tables so there is no confusion
1529 * about operator precedence in harmony with SQL standards
1530 *
1531 * @param array
1532 * @return string
1533 */
1534 protected function _from_tables($tables)
1535 {
1536 is_array($tables) OR $tables = array($tables);
1537
1538 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
1539 }
1540
1541 // --------------------------------------------------------------------
1542
1543 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001544 * Get UPDATE query string
1545 *
1546 * Compiles an update query and returns the sql
1547 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001548 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001549 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001550 * @return string
1551 */
1552 public function get_compiled_update($table = '', $reset = TRUE)
1553 {
1554 // Combine any cached components with the current statements
1555 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001556
Kyle Farris0c147b32011-08-26 02:29:31 -04001557 if ($this->_validate_update($table) === FALSE)
1558 {
1559 return FALSE;
1560 }
WanWizard7219c072011-12-28 14:09:05 +01001561
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001562 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set, $this->qb_where, $this->qb_orderby, $this->qb_limit);
WanWizard7219c072011-12-28 14:09:05 +01001563
Kyle Farris0c147b32011-08-26 02:29:31 -04001564 if ($reset === TRUE)
1565 {
1566 $this->_reset_write();
1567 }
WanWizard7219c072011-12-28 14:09:05 +01001568
Kyle Farris0c147b32011-08-26 02:29:31 -04001569 return $sql;
1570 }
WanWizard7219c072011-12-28 14:09:05 +01001571
Derek Allard2067d1a2008-11-13 22:59:24 +00001572 // --------------------------------------------------------------------
1573
1574 /**
1575 * Update
1576 *
1577 * Compiles an update string and runs the query
1578 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001579 * @param string the table to retrieve the results from
1580 * @param array an associative array of update values
1581 * @param mixed the where clause
1582 * @return object
1583 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001584 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001585 {
1586 // Combine any cached components with the current statements
1587 $this->_merge_cache();
1588
1589 if ( ! is_null($set))
1590 {
1591 $this->set($set);
1592 }
Barry Mienydd671972010-10-04 16:33:58 +02001593
Kyle Farris0c147b32011-08-26 02:29:31 -04001594 if ($this->_validate_update($table) === FALSE)
1595 {
1596 return FALSE;
1597 }
1598
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001599 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001600 {
1601 $this->where($where);
1602 }
1603
Andrey Andreev650b4c02012-06-11 12:07:15 +03001604 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001605 {
1606 $this->limit($limit);
1607 }
1608
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001609 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set, $this->qb_where, $this->qb_orderby, $this->qb_limit, $this->qb_like);
Jamie Rumbelow3b1355c2012-03-06 21:27:46 +00001610
Kyle Farris0c147b32011-08-26 02:29:31 -04001611 $this->_reset_write();
1612 return $this->query($sql);
1613 }
WanWizard7219c072011-12-28 14:09:05 +01001614
Kyle Farris0c147b32011-08-26 02:29:31 -04001615 // --------------------------------------------------------------------
1616
1617 /**
1618 * Validate Update
1619 *
1620 * This method is used by both update() and get_compiled_update() to
1621 * validate that data is actually being set and that a table has been
1622 * chosen to be update.
1623 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001624 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001625 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001626 */
1627 protected function _validate_update($table = '')
1628 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001629 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001630 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001631 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001632 }
1633
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001634 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001635 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001636 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001637 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001638 elseif ( ! isset($this->qb_from[0]))
1639 {
1640 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1641 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001642
1643 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001644 }
WanWizard7219c072011-12-28 14:09:05 +01001645
Derek Jonesd10e8962010-03-02 17:10:36 -06001646 // --------------------------------------------------------------------
1647
1648 /**
1649 * Update_Batch
1650 *
1651 * Compiles an update string and runs the query
1652 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001653 * @param string the table to retrieve the results from
1654 * @param array an associative array of update values
1655 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001656 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001657 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001658 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001659 {
1660 // Combine any cached components with the current statements
1661 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001662
Derek Jonesd10e8962010-03-02 17:10:36 -06001663 if (is_null($index))
1664 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001665 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001666 }
1667
1668 if ( ! is_null($set))
1669 {
1670 $this->set_update_batch($set, $index);
1671 }
1672
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001673 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001674 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001675 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001676 }
1677
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001678 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001679 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001680 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001681 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001682 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001683 }
Barry Mienydd671972010-10-04 16:33:58 +02001684
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001685 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001686 }
Barry Mienydd671972010-10-04 16:33:58 +02001687
Derek Jonesd10e8962010-03-02 17:10:36 -06001688 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001689 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001690 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001691 $this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, 100), $this->protect_identifiers($index), $this->qb_where));
Derek Jonesd10e8962010-03-02 17:10:36 -06001692 }
Barry Mienydd671972010-10-04 16:33:58 +02001693
Derek Jonesd10e8962010-03-02 17:10:36 -06001694 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001695 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001696 }
1697
1698 // --------------------------------------------------------------------
1699
1700 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001701 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001702 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001703 * @param array
1704 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001705 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001706 * @return object
1707 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001708 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001709 {
1710 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001711
Derek Jonesd10e8962010-03-02 17:10:36 -06001712 if ( ! is_array($key))
1713 {
1714 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001715 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001716
Andrey Andreevfe642da2012-06-16 03:47:33 +03001717 is_bool($escape) OR $escape = $this->_protect_identifiers;
1718
Derek Jonesd10e8962010-03-02 17:10:36 -06001719 foreach ($key as $k => $v)
1720 {
1721 $index_set = FALSE;
1722 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001723 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001724 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001725 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001726 {
1727 $index_set = TRUE;
1728 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001729
Andrey Andreevfe642da2012-06-16 03:47:33 +03001730 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001731 }
1732
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001733 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001734 {
1735 return $this->display_error('db_batch_missing_index');
1736 }
1737
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001738 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001739 }
Barry Mienydd671972010-10-04 16:33:58 +02001740
Derek Jonesd10e8962010-03-02 17:10:36 -06001741 return $this;
1742 }
1743
Derek Allard2067d1a2008-11-13 22:59:24 +00001744 // --------------------------------------------------------------------
1745
1746 /**
1747 * Empty Table
1748 *
1749 * Compiles a delete string and runs "DELETE FROM table"
1750 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001751 * @param string the table to empty
1752 * @return object
1753 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001754 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001755 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001756 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001757 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001758 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001759 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001760 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001761 }
1762
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001763 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001764 }
1765 else
1766 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001767 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001768 }
1769
1770 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001771 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 return $this->query($sql);
1773 }
1774
1775 // --------------------------------------------------------------------
1776
1777 /**
1778 * Truncate
1779 *
1780 * Compiles a truncate string and runs the query
1781 * If the database does not support the truncate() command
1782 * This function maps to "DELETE FROM table"
1783 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001784 * @param string the table to truncate
1785 * @return object
1786 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001787 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001789 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001790 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001791 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001792 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001793 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001794 }
1795
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001796 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001797 }
1798 else
1799 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001800 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001801 }
1802
1803 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001804 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001805 return $this->query($sql);
1806 }
WanWizard7219c072011-12-28 14:09:05 +01001807
Kyle Farris0c147b32011-08-26 02:29:31 -04001808 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001809
Kyle Farris0c147b32011-08-26 02:29:31 -04001810 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001811 * Truncate statement
1812 *
1813 * Generates a platform-specific truncate string from the supplied data
1814 *
1815 * If the database does not support the truncate() command,
1816 * then this method maps to 'DELETE FROM table'
1817 *
1818 * @param string the table name
1819 * @return string
1820 */
1821 protected function _truncate($table)
1822 {
1823 return 'TRUNCATE '.$table;
1824 }
1825
1826 // --------------------------------------------------------------------
1827
1828 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001829 * Get DELETE query string
1830 *
1831 * Compiles a delete query string and returns the sql
1832 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001833 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001834 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001835 * @return string
1836 */
1837 public function get_compiled_delete($table = '', $reset = TRUE)
1838 {
1839 $this->return_delete_sql = TRUE;
1840 $sql = $this->delete($table, '', NULL, $reset);
1841 $this->return_delete_sql = FALSE;
1842 return $sql;
1843 }
WanWizard7219c072011-12-28 14:09:05 +01001844
Derek Allard2067d1a2008-11-13 22:59:24 +00001845 // --------------------------------------------------------------------
1846
1847 /**
1848 * Delete
1849 *
1850 * Compiles a delete string and runs the query
1851 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001852 * @param mixed the table(s) to delete from. String or array
1853 * @param mixed the where clause
1854 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001855 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001856 * @return object
1857 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001858 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001859 {
1860 // Combine any cached components with the current statements
1861 $this->_merge_cache();
1862
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001863 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001864 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001865 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001866 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001867 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001868 }
1869
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001870 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001871 }
1872 elseif (is_array($table))
1873 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001874 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001875 {
1876 $this->delete($single_table, $where, $limit, FALSE);
1877 }
1878
1879 $this->_reset_write();
1880 return;
1881 }
1882 else
1883 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001884 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001885 }
1886
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001887 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001888 {
1889 $this->where($where);
1890 }
1891
Andrey Andreev650b4c02012-06-11 12:07:15 +03001892 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001893 {
1894 $this->limit($limit);
1895 }
1896
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001897 if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001898 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001899 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001900 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001901
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001902 $sql = $this->_delete($table, $this->qb_where, $this->qb_like, $this->qb_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001903 if ($reset_data)
1904 {
1905 $this->_reset_write();
1906 }
WanWizard7219c072011-12-28 14:09:05 +01001907
Andrey Andreev24276a32012-01-08 02:44:38 +02001908 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001909 }
WanWizard7219c072011-12-28 14:09:05 +01001910
Derek Allard2067d1a2008-11-13 22:59:24 +00001911 // --------------------------------------------------------------------
1912
1913 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001914 * Delete statement
1915 *
1916 * Generates a platform-specific delete string from the supplied data
1917 *
1918 * @param string the table name
1919 * @param array the where clause
1920 * @param array the like clause
1921 * @param string the limit clause
1922 * @return string
1923 */
1924 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
1925 {
1926 $conditions = array();
1927
1928 empty($where) OR $conditions[] = implode(' ', $where);
1929 empty($like) OR $conditions[] = implode(' ', $like);
1930
1931 return 'DELETE FROM '.$table
1932 .(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '')
Andrey Andreev650b4c02012-06-11 12:07:15 +03001933 .($limit ? ' LIMIT '.(int) $limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001934 }
1935
1936 // --------------------------------------------------------------------
1937
1938 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001939 * DB Prefix
1940 *
1941 * Prepends a database prefix if one exists in configuration
1942 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001943 * @param string the table
1944 * @return string
1945 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001946 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001947 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001948 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001949 {
1950 $this->display_error('db_table_name_required');
1951 }
1952
1953 return $this->dbprefix.$table;
1954 }
1955
1956 // --------------------------------------------------------------------
1957
1958 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001959 * Set DB Prefix
1960 *
1961 * Set's the DB Prefix to something new without needing to reconnect
1962 *
1963 * @param string the prefix
1964 * @return string
1965 */
1966 public function set_dbprefix($prefix = '')
1967 {
1968 return $this->dbprefix = $prefix;
1969 }
1970
1971 // --------------------------------------------------------------------
1972
1973 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001974 * Track Aliases
1975 *
1976 * Used to track SQL statements written with aliased tables.
1977 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001978 * @param string The table to inspect
1979 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001980 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001981 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001982 {
1983 if (is_array($table))
1984 {
1985 foreach ($table as $t)
1986 {
1987 $this->_track_aliases($t);
1988 }
1989 return;
1990 }
Barry Mienydd671972010-10-04 16:33:58 +02001991
Derek Jones37f4b9c2011-07-01 17:56:50 -05001992 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001993 // the string into discreet statements
1994 if (strpos($table, ',') !== FALSE)
1995 {
1996 return $this->_track_aliases(explode(',', $table));
1997 }
Barry Mienydd671972010-10-04 16:33:58 +02001998
Derek Allard2067d1a2008-11-13 22:59:24 +00001999 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002000 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002001 {
2002 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002003 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002004
Derek Allard2067d1a2008-11-13 22:59:24 +00002005 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002006 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002007
Derek Allard2067d1a2008-11-13 22:59:24 +00002008 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002009 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002010 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002011 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002012 }
2013 }
2014 }
WanWizard7219c072011-12-28 14:09:05 +01002015
Derek Allard2067d1a2008-11-13 22:59:24 +00002016 // --------------------------------------------------------------------
2017
2018 /**
2019 * Compile the SELECT statement
2020 *
2021 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05002022 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00002023 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002024 * @return string
2025 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002026 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002027 {
2028 // Combine any cached components with the current statements
2029 $this->_merge_cache();
2030
Derek Allard2067d1a2008-11-13 22:59:24 +00002031 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002032 if ($select_override !== FALSE)
2033 {
2034 $sql = $select_override;
2035 }
2036 else
2037 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002038 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002039
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002040 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002041 {
Barry Mienydd671972010-10-04 16:33:58 +02002042 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002043 }
2044 else
Barry Mienydd671972010-10-04 16:33:58 +02002045 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002046 // Cycle through the "select" portion of the query and prep each column name.
2047 // The reason we protect identifiers here rather then in the select() function
2048 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002049 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002050 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002051 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002052 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 }
Barry Mienydd671972010-10-04 16:33:58 +02002054
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002055 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002056 }
2057 }
2058
Derek Allard2067d1a2008-11-13 22:59:24 +00002059 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002060 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002061 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002062 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002063 }
2064
Derek Allard2067d1a2008-11-13 22:59:24 +00002065 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002066 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002067 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002068 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002069 }
2070
Andrey Andreev6e704752012-07-18 00:46:33 +03002071 $sql .= $this->_compile_conditions();
Derek Allard2067d1a2008-11-13 22:59:24 +00002072
Derek Allard2067d1a2008-11-13 22:59:24 +00002073 // Write the "LIMIT" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002074 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002075 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002076 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00002077 }
2078
2079 return $sql;
2080 }
2081
2082 // --------------------------------------------------------------------
2083
2084 /**
Andrey Andreev6e704752012-07-18 00:46:33 +03002085 * Compile WHERE statement
2086 *
2087 * Escapes identifiers in WHERE, LIKE, HAVING, GROUP BY, ORDER BY
2088 * statements at execution time. Required so that aliases are tracked
2089 * properly, regardless of wether e.g. where() is called prior to
2090 * join() and dbprefix is added only if needed.
2091 *
2092 * @return string
2093 */
2094 protected function _compile_conditions()
2095 {
2096 // WHERE
2097 if (count($this->qb_where) > 0)
2098 {
2099 $sql = "\nWHERE ";
2100
2101 for ($i = 0, $c = count($this->qb_where); $i < $c; $i++)
2102 {
2103 if ($this->qb_where[$i]['escape'] === FALSE)
2104 {
2105 $this->qb_where[$i] = $this->qb_where[$i]['condition'];
2106 continue;
2107 }
2108
2109 $op = preg_quote($this->_get_operator($this->qb_where[$i]['condition']));
2110 if ( ! preg_match('/^(\s*(?:AND|OR)\s+)?(\(?)(.*)('.$op.')(.*(?<!\)))?(\)?)$/i', $this->qb_where[$i]['condition'], $matches))
2111 {
2112 $this->qb_where[$i] = $this->qb_where[$i]['condition'];
2113 continue;
2114 }
2115
2116 // $matches = array(
2117 // 0 => 'OR (test <= foo)', /* the whole thing */
2118 // 1 => 'OR ', /* optional */
2119 // 2 => '(', /* optional */
2120 // 3 => 'test', /* the field name */
2121 // 4 => ' <= ', /* $op */
2122 // 5 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2123 // 6 => ')' /* optional */
2124 // );
2125 empty($matches[5]) OR $matches[5] = ' '.$this->protect_identifiers(trim($matches[5]));
2126 $this->qb_where[$i] = $matches[1].$matches[2].$this->protect_identifiers(trim($matches[3]))
2127 .' '.trim($matches[4]).$matches[5].$matches[6];
2128 }
2129
2130 $sql .= implode("\n", $this->qb_where);
2131 }
2132 else
2133 {
2134 $sql = '';
2135 }
2136
2137 // LIKE
2138 if (count($this->qb_like) > 0)
2139 {
2140 $sql .= ($sql === '') ? "\nWHERE " : "\nAND ";
2141 $sql .= implode("\n", $this->qb_like);
2142 }
2143
2144 // GROUP BY
2145 if (count($this->qb_groupby) > 0)
2146 {
2147 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
2148 }
2149
2150 // HAVING
2151 if (count($this->qb_having) > 0)
2152 {
2153 $sql .= "\nHAVING ".implode("\n", $this->qb_having);
2154 }
2155
2156 // ORDER BY
2157 if (count($this->qb_orderby) > 0)
2158 {
2159 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
2160 }
2161
2162 return $sql;
2163 }
2164
2165 // --------------------------------------------------------------------
2166
2167 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002168 * Object to Array
2169 *
2170 * Takes an object as input and converts the class variables to array key/vals
2171 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002172 * @param object
2173 * @return array
2174 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002175 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002176 {
2177 if ( ! is_object($object))
2178 {
2179 return $object;
2180 }
Barry Mienydd671972010-10-04 16:33:58 +02002181
Derek Allard2067d1a2008-11-13 22:59:24 +00002182 $array = array();
2183 foreach (get_object_vars($object) as $key => $val)
2184 {
2185 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002186 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002187 {
2188 $array[$key] = $val;
2189 }
2190 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002191
2192 return $array;
2193 }
Barry Mienydd671972010-10-04 16:33:58 +02002194
Derek Jonesd10e8962010-03-02 17:10:36 -06002195 // --------------------------------------------------------------------
2196
2197 /**
2198 * Object to Array
2199 *
2200 * Takes an object as input and converts the class variables to array key/vals
2201 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002202 * @param object
2203 * @return array
2204 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002205 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002206 {
2207 if ( ! is_object($object))
2208 {
2209 return $object;
2210 }
Barry Mienydd671972010-10-04 16:33:58 +02002211
Derek Jonesd10e8962010-03-02 17:10:36 -06002212 $array = array();
2213 $out = get_object_vars($object);
2214 $fields = array_keys($out);
2215
2216 foreach ($fields as $val)
2217 {
2218 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002219 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002220 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002221 $i = 0;
2222 foreach ($out[$val] as $data)
2223 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002224 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002225 }
2226 }
2227 }
2228
Derek Allard2067d1a2008-11-13 22:59:24 +00002229 return $array;
2230 }
Barry Mienydd671972010-10-04 16:33:58 +02002231
Derek Allard2067d1a2008-11-13 22:59:24 +00002232 // --------------------------------------------------------------------
2233
2234 /**
2235 * Start Cache
2236 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002237 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002238 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002239 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002240 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002241 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002242 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002243 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002244 }
2245
2246 // --------------------------------------------------------------------
2247
2248 /**
2249 * Stop Cache
2250 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002251 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002252 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002253 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002254 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002255 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002256 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002257 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002258 }
2259
2260 // --------------------------------------------------------------------
2261
2262 /**
2263 * Flush Cache
2264 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002265 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002266 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002267 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002268 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002269 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002270 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002271 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002272 'qb_cache_select' => array(),
2273 'qb_cache_from' => array(),
2274 'qb_cache_join' => array(),
2275 'qb_cache_where' => array(),
2276 'qb_cache_like' => array(),
2277 'qb_cache_groupby' => array(),
2278 'qb_cache_having' => array(),
2279 'qb_cache_orderby' => array(),
2280 'qb_cache_set' => array(),
2281 'qb_cache_exists' => array(),
2282 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002283 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002284 }
2285
2286 // --------------------------------------------------------------------
2287
2288 /**
2289 * Merge Cache
2290 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002291 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002292 * locally called ones.
2293 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002294 * @return void
2295 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002296 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002297 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002298 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002299 {
2300 return;
2301 }
2302
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002303 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002304 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002305 $qb_variable = 'qb_'.$val;
2306 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002307
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002308 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002309 {
2310 continue;
2311 }
2312
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002313 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002314 }
2315
2316 // If we are "protecting identifiers" we need to examine the "from"
2317 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002318 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002319 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002320 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002321 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002322
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002323 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002324 }
WanWizard7219c072011-12-28 14:09:05 +01002325
Kyle Farris0c147b32011-08-26 02:29:31 -04002326 // --------------------------------------------------------------------
2327
2328 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002329 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002330 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002331 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002332 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002333 * @return void
2334 */
2335 public function reset_query()
2336 {
2337 $this->_reset_select();
2338 $this->_reset_write();
2339 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002340
2341 // --------------------------------------------------------------------
2342
2343 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002344 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002345 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002346 * @param array An array of fields to reset
2347 * @return void
2348 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002349 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002350 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002351 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002352 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002353 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002354 {
2355 $this->$item = $default_value;
2356 }
2357 }
2358 }
2359
2360 // --------------------------------------------------------------------
2361
2362 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002363 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002364 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002365 * @return void
2366 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002367 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002368 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002369 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002370 'qb_select' => array(),
2371 'qb_from' => array(),
2372 'qb_join' => array(),
2373 'qb_where' => array(),
2374 'qb_like' => array(),
2375 'qb_groupby' => array(),
2376 'qb_having' => array(),
2377 'qb_orderby' => array(),
2378 'qb_wherein' => array(),
2379 'qb_aliased_tables' => array(),
2380 'qb_no_escape' => array(),
2381 'qb_distinct' => FALSE,
2382 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002383 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002384 )
2385 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002386 }
Barry Mienydd671972010-10-04 16:33:58 +02002387
Derek Allard2067d1a2008-11-13 22:59:24 +00002388 // --------------------------------------------------------------------
2389
2390 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002391 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002392 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002393 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002394 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002395 * @return void
2396 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002397 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002398 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002399 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002400 'qb_set' => array(),
2401 'qb_from' => array(),
2402 'qb_where' => array(),
2403 'qb_like' => array(),
2404 'qb_orderby' => array(),
2405 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002406 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002407 )
2408 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002409 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002410
Derek Allard2067d1a2008-11-13 22:59:24 +00002411}
2412
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002413/* End of file DB_query_builder.php */
Andrey Andreev58803fb2012-06-24 00:45:37 +03002414/* Location: ./system/database/DB_query_builder.php */