blob: 479b7f24a9bbf35a6838c3f3115d01578329cafd [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
Andrey Andreev35443c62012-06-25 15:34:30 +0300470 if ($escape === TRUE)
471 {
472 $k = (($op = $this->_get_operator($k)) !== FALSE)
Andrey Andreev40f14042012-06-25 17:54:22 +0300473 ? $this->escape_identifiers(trim(substr($k, 0, strpos($k, $op)))).' '.strstr($k, $op)
474 : $this->escape_identifiers(trim($k));
Andrey Andreev35443c62012-06-25 15:34:30 +0300475 }
Andrey Andreev9c14f652012-06-10 14:35:07 +0300476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 if (is_null($v) && ! $this->_has_operator($k))
478 {
479 // value appears not to have been set, assign the test to IS NULL
480 $k .= ' IS NULL';
481 }
Barry Mienydd671972010-10-04 16:33:58 +0200482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 if ( ! is_null($v))
484 {
485 if ($escape === TRUE)
486 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 $v = ' '.$this->escape($v);
488 }
WanWizard7219c072011-12-28 14:09:05 +0100489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 if ( ! $this->_has_operator($k))
491 {
Greg Akere156c6e2011-04-20 16:03:04 -0500492 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 }
494 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000495
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000496 $this->qb_where[] = $prefix.$k.$v;
497 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000499 $this->qb_cache_where[] = $prefix.$k.$v;
500 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 return $this;
506 }
507
508 // --------------------------------------------------------------------
509
510 /**
511 * Where_in
512 *
513 * Generates a WHERE field IN ('item', 'item') SQL query joined with
514 * AND if appropriate
515 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 * @param string The field to search
517 * @param array The values searched on
518 * @return object
519 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300520 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300522 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 }
Barry Mienydd671972010-10-04 16:33:58 +0200524
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 // --------------------------------------------------------------------
526
527 /**
528 * Where_in_or
529 *
530 * Generates a WHERE field IN ('item', 'item') SQL query joined with
531 * OR if appropriate
532 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 * @param string The field to search
534 * @param array The values searched on
535 * @return object
536 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300537 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300539 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 }
541
542 // --------------------------------------------------------------------
543
544 /**
545 * Where_not_in
546 *
547 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
548 * with AND if appropriate
549 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 * @param string The field to search
551 * @param array The values searched on
552 * @return object
553 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300554 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300556 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 }
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 // --------------------------------------------------------------------
560
561 /**
562 * Where_not_in_or
563 *
564 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
565 * with OR if appropriate
566 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 * @param string The field to search
568 * @param array The values searched on
569 * @return object
570 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300571 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300573 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 }
575
576 // --------------------------------------------------------------------
577
578 /**
579 * Where_in
580 *
581 * Called by where_in, where_in_or, where_not_in, where_not_in_or
582 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 * @param string The field to search
584 * @param array The values searched on
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300585 * @param bool If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200586 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 * @return object
588 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300589 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 {
591 if ($key === NULL OR $values === NULL)
592 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300593 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 }
Barry Mienydd671972010-10-04 16:33:58 +0200595
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 if ( ! is_array($values))
597 {
598 $values = array($values);
599 }
Barry Mienydd671972010-10-04 16:33:58 +0200600
Andrey Andreev498c1e02012-06-16 03:34:10 +0300601 is_bool($escape) OR $escape = $this->_protect_identifiers;
602
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 $not = ($not) ? ' NOT' : '';
604
605 foreach ($values as $value)
606 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000607 $this->qb_wherein[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 }
609
Andrey Andreev35443c62012-06-25 15:34:30 +0300610 if ($escape === TRUE)
611 {
Andrey Andreev40f14042012-06-25 17:54:22 +0300612 $key = $this->escape_identifiers(trim($key));
Andrey Andreev35443c62012-06-25 15:34:30 +0300613 }
614
WanWizardbc69f362012-06-22 00:10:11 +0200615 $prefix = (count($this->qb_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Andrey Andreev35443c62012-06-25 15:34:30 +0300616 $this->qb_where[] = $where_in = $prefix.$key.$not.' IN ('.implode(', ', $this->qb_wherein).') ';
Barry Mienydd671972010-10-04 16:33:58 +0200617
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000618 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000620 $this->qb_cache_where[] = $where_in;
621 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 }
623
624 // reset the array for multiple calls
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000625 $this->qb_wherein = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 return $this;
627 }
Barry Mienydd671972010-10-04 16:33:58 +0200628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 // --------------------------------------------------------------------
630
631 /**
632 * Like
633 *
634 * Generates a %LIKE% portion of the query. Separates
635 * multiple calls with AND
636 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 * @param mixed
638 * @param mixed
639 * @return object
640 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600641 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 {
643 return $this->_like($field, $match, 'AND ', $side);
644 }
645
646 // --------------------------------------------------------------------
647
648 /**
649 * Not Like
650 *
651 * Generates a NOT LIKE portion of the query. Separates
652 * multiple calls with AND
653 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 * @param mixed
655 * @param mixed
656 * @return object
657 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600658 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 {
660 return $this->_like($field, $match, 'AND ', $side, 'NOT');
661 }
Barry Mienydd671972010-10-04 16:33:58 +0200662
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 // --------------------------------------------------------------------
664
665 /**
666 * OR Like
667 *
668 * Generates a %LIKE% portion of the query. Separates
669 * multiple calls with OR
670 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 * @param mixed
672 * @param mixed
673 * @return object
674 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600675 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 {
677 return $this->_like($field, $match, 'OR ', $side);
678 }
679
680 // --------------------------------------------------------------------
681
682 /**
683 * OR Not Like
684 *
685 * Generates a NOT LIKE portion of the query. Separates
686 * multiple calls with OR
687 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 * @param mixed
689 * @param mixed
690 * @return object
691 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600692 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 {
694 return $this->_like($field, $match, 'OR ', $side, 'NOT');
695 }
Barry Mienydd671972010-10-04 16:33:58 +0200696
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 // --------------------------------------------------------------------
698
699 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 * Like
701 *
702 * Called by like() or orlike()
703 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 * @param mixed
705 * @param mixed
706 * @param string
707 * @return object
708 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600709 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 {
711 if ( ! is_array($field))
712 {
713 $field = array($field => $match);
714 }
Barry Mienydd671972010-10-04 16:33:58 +0200715
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 foreach ($field as $k => $v)
717 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000718 $k = $this->protect_identifiers($k);
WanWizardbc69f362012-06-22 00:10:11 +0200719 $prefix = (count($this->qb_like) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Derek Jonese4ed5832009-02-20 21:44:59 +0000720 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300721
Andrey Andreev24276a32012-01-08 02:44:38 +0200722 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400723 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100724 $like_statement = "{$prefix} $k $not LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400725 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200726 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100728 $like_statement = "{$prefix} $k $not LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200730 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100732 $like_statement = "{$prefix} $k $not LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 }
734 else
735 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100736 $like_statement = "{$prefix} $k $not LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600738
Derek Jonese4ed5832009-02-20 21:44:59 +0000739 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100740 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000741 {
Greg Aker0d424892010-01-26 02:14:44 +0000742 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000743 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600744
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000745 $this->qb_like[] = $like_statement;
746 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000748 $this->qb_cache_like[] = $like_statement;
749 $this->qb_cache_exists[] = 'like';
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 }
Barry Mienydd671972010-10-04 16:33:58 +0200751
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200753
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 return $this;
755 }
Barry Mienydd671972010-10-04 16:33:58 +0200756
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 // --------------------------------------------------------------------
758
759 /**
WanWizard7219c072011-12-28 14:09:05 +0100760 * Starts a query group.
761 *
762 * @param string (Internal use only)
763 * @param string (Internal use only)
764 * @return object
765 */
766 public function group_start($not = '', $type = 'AND ')
767 {
768 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100769
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000770 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100771 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000772 $this->qb_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (';
WanWizard7219c072011-12-28 14:09:05 +0100773
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000774 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100775 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000776 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100777 }
778
779 return $this;
780 }
781
782 // --------------------------------------------------------------------
783
784 /**
785 * Starts a query group, but ORs the group
786 *
787 * @return object
788 */
789 public function or_group_start()
790 {
791 return $this->group_start('', 'OR ');
792 }
793
794 // --------------------------------------------------------------------
795
796 /**
797 * Starts a query group, but NOTs the group
798 *
799 * @return object
800 */
801 public function not_group_start()
802 {
803 return $this->group_start('NOT ', 'AND ');
804 }
805
806 // --------------------------------------------------------------------
807
808 /**
809 * Starts a query group, but OR NOTs the group
810 *
811 * @return object
812 */
813 public function or_not_group_start()
814 {
815 return $this->group_start('NOT ', 'OR ');
816 }
817
818 // --------------------------------------------------------------------
819
820 /**
821 * Ends a query group
822 *
823 * @return object
824 */
825 public function group_end()
826 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000827 $this->qb_where_group_started = FALSE;
828 $this->qb_where[] = $value = str_repeat(' ', $this->qb_where_group_count--) . ')';
WanWizard7219c072011-12-28 14:09:05 +0100829
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000830 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100831 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000832 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100833 }
834
WanWizard7219c072011-12-28 14:09:05 +0100835 return $this;
836 }
837
838 // --------------------------------------------------------------------
839
840 /**
841 * Group_get_type
842 *
843 * Called by group_start(), _like(), _where() and _where_in()
844 *
845 * @param string
846 * @return string
847 */
848 protected function _group_get_type($type)
849 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000850 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100851 {
852 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000853 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100854 }
855
856 return $type;
857 }
858
859 // --------------------------------------------------------------------
860
861 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 * GROUP BY
863 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 * @param string
865 * @return object
866 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600867 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 {
869 if (is_string($by))
870 {
871 $by = explode(',', $by);
872 }
Barry Mienydd671972010-10-04 16:33:58 +0200873
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 foreach ($by as $val)
875 {
876 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200877
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100878 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000880 $this->qb_groupby[] = $val = $this->protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200881
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000882 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000884 $this->qb_cache_groupby[] = $val;
885 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 }
887 }
888 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200889
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 return $this;
891 }
892
893 // --------------------------------------------------------------------
894
895 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 * Sets the HAVING value
897 *
898 * Separates multiple calls with AND
899 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 * @param string
901 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300902 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 * @return object
904 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300905 public function having($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 {
907 return $this->_having($key, $value, 'AND ', $escape);
908 }
Barry Mienydd671972010-10-04 16:33:58 +0200909
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 // --------------------------------------------------------------------
911
912 /**
913 * Sets the OR HAVING value
914 *
915 * Separates multiple calls with OR
916 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 * @param string
918 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300919 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 * @return object
921 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300922 public function or_having($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 {
924 return $this->_having($key, $value, 'OR ', $escape);
925 }
Barry Mienydd671972010-10-04 16:33:58 +0200926
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 // --------------------------------------------------------------------
928
929 /**
930 * Sets the HAVING values
931 *
932 * Called by having() or or_having()
933 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 * @param string
935 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300936 * @param string
937 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 * @return object
939 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300940 protected function _having($key, $value = '', $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 {
942 if ( ! is_array($key))
943 {
944 $key = array($key => $value);
945 }
Barry Mienydd671972010-10-04 16:33:58 +0200946
Andrey Andreevfe642da2012-06-16 03:47:33 +0300947 is_bool($escape) OR $escape = $this->_protect_identifiers;
948
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 foreach ($key as $k => $v)
950 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000951 $prefix = (count($this->qb_having) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000952
Andrey Andreev974c75b2012-06-15 12:30:02 +0300953 $k = $this->_has_operator($k)
954 ? $this->protect_identifiers(substr($k, 0, strpos(rtrim($k), ' ')), FALSE, $escape).strchr(rtrim($k), ' ')
955 : $this->protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000956
957 if ( ! $this->_has_operator($k))
958 {
959 $k .= ' = ';
960 }
961
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100962 if ($v !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400964 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
Barry Mienydd671972010-10-04 16:33:58 +0200966
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000967 $this->qb_having[] = $prefix.$k.$v;
968 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000970 $this->qb_cache_having[] = $prefix.$k.$v;
971 $this->qb_cache_exists[] = 'having';
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 }
973 }
Barry Mienydd671972010-10-04 16:33:58 +0200974
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 return $this;
976 }
Barry Mienydd671972010-10-04 16:33:58 +0200977
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 // --------------------------------------------------------------------
979
980 /**
981 * Sets the ORDER BY value
982 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 * @param string
984 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100985 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 * @return object
987 */
Andrey Andreevd24160c2012-06-16 03:21:20 +0300988 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200990 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 {
992 $orderby = ''; // Random results want or don't need a field name
993 $direction = $this->_random_keyword;
994 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100995 elseif (trim($direction) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 {
Andrey Andreev650b4c02012-06-11 12:07:15 +0300997 $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : ' ASC';
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 }
Barry Mienydd671972010-10-04 16:33:58 +0200999
Andrey Andreevd24160c2012-06-16 03:21:20 +03001000 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +02001001
Andrey Andreevd24160c2012-06-16 03:21:20 +03001002 if ($escape === TRUE && strpos($orderby, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 {
1004 $temp = array();
1005 foreach (explode(',', $orderby) as $part)
1006 {
1007 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001008 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 {
Andrey Andreev88cb2782012-06-11 20:40:50 +03001010 $part = preg_match('/^(.+)\s+(ASC|DESC)$/i', $part, $matches)
1011 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
1012 : $this->protect_identifiers($part);
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 }
Barry Mienydd671972010-10-04 16:33:58 +02001014
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 $temp[] = $part;
1016 }
Barry Mienydd671972010-10-04 16:33:58 +02001017
1018 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001020 elseif ($direction !== $this->_random_keyword && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 {
Andrey Andreev1d1d7ff2012-06-11 22:35:35 +03001022 $orderby = preg_match('/^(.+)\s+(ASC|DESC)$/i', $orderby, $matches)
Andrey Andreev88cb2782012-06-11 20:40:50 +03001023 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
1024 : $this->protect_identifiers($orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 }
Barry Mienydd671972010-10-04 16:33:58 +02001026
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001027 $this->qb_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +02001028
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001029 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001031 $this->qb_cache_orderby[] = $orderby_statement;
1032 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 }
1034
1035 return $this;
1036 }
Barry Mienydd671972010-10-04 16:33:58 +02001037
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 // --------------------------------------------------------------------
1039
1040 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 * Sets the LIMIT value
1042 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001043 * @param int the limit value
1044 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 * @return object
1046 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +02001047 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001049 is_null($value) OR $this->qb_limit = (int) $value;
1050 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001051
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 return $this;
1053 }
Barry Mienydd671972010-10-04 16:33:58 +02001054
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 // --------------------------------------------------------------------
1056
1057 /**
1058 * Sets the OFFSET value
1059 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001060 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 * @return object
1062 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001063 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001065 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 return $this;
1067 }
Barry Mienydd671972010-10-04 16:33:58 +02001068
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 // --------------------------------------------------------------------
1070
1071 /**
Andrey Andreev2c35b642012-06-24 03:05:26 +03001072 * Limit string
1073 *
1074 * Generates a platform-specific LIMIT clause
1075 *
1076 * @param string the sql query string
1077 * @param int the number of rows to limit the query to
1078 * @param int the offset value
1079 * @return string
1080 */
1081 protected function _limit($sql, $limit, $offset)
1082 {
1083 return $sql.' LIMIT '.($offset ? $offset.', ' : '').$limit;
1084 }
1085
1086 // --------------------------------------------------------------------
1087
1088 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001089 * The "set" function.
1090 *
1091 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 * @param mixed
1094 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001095 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 * @return object
1097 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001098 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 {
1100 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001101
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 if ( ! is_array($key))
1103 {
1104 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001105 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001106
Andrey Andreevfe642da2012-06-16 03:47:33 +03001107 is_bool($escape) OR $escape = $this->_protect_identifiers;
1108
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 foreach ($key as $k => $v)
1110 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001111 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1112 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 }
Barry Mienydd671972010-10-04 16:33:58 +02001114
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 return $this;
1116 }
WanWizard7219c072011-12-28 14:09:05 +01001117
Kyle Farris0c147b32011-08-26 02:29:31 -04001118 // --------------------------------------------------------------------
1119
1120 /**
1121 * Get SELECT query string
1122 *
1123 * Compiles a SELECT query string and returns the sql.
1124 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001125 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001126 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001127 * @return string
1128 */
WanWizard7219c072011-12-28 14:09:05 +01001129 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001130 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001131 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001132 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001133 $this->_track_aliases($table);
1134 $this->from($table);
1135 }
WanWizard7219c072011-12-28 14:09:05 +01001136
Andrey Andreev650b4c02012-06-11 12:07:15 +03001137 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001138
Kyle Farris0c147b32011-08-26 02:29:31 -04001139 if ($reset === TRUE)
1140 {
1141 $this->_reset_select();
1142 }
WanWizard7219c072011-12-28 14:09:05 +01001143
Kyle Farris0c147b32011-08-26 02:29:31 -04001144 return $select;
1145 }
WanWizard7219c072011-12-28 14:09:05 +01001146
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 // --------------------------------------------------------------------
1148
1149 /**
1150 * Get
1151 *
1152 * Compiles the select statement based on the other functions called
1153 * and runs the query
1154 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 * @param string the table
1156 * @param string the limit clause
1157 * @param string the offset clause
1158 * @return object
1159 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001160 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001162 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 {
1164 $this->_track_aliases($table);
1165 $this->from($table);
1166 }
Barry Mienydd671972010-10-04 16:33:58 +02001167
Andrey Andreev650b4c02012-06-11 12:07:15 +03001168 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 {
1170 $this->limit($limit, $offset);
1171 }
Barry Mienydd671972010-10-04 16:33:58 +02001172
Andrey Andreev24276a32012-01-08 02:44:38 +02001173 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 $this->_reset_select();
1175 return $result;
1176 }
1177
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001178 // --------------------------------------------------------------------
1179
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 /**
1181 * "Count All Results" query
1182 *
Barry Mienydd671972010-10-04 16:33:58 +02001183 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001184 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 * @param string
1187 * @return string
1188 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001189 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001191 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 {
1193 $this->_track_aliases($table);
1194 $this->from($table);
1195 }
Barry Mienydd671972010-10-04 16:33:58 +02001196
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001197 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001199
Purwandi1d160e72012-01-09 16:33:28 +07001200 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001202 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 }
1204
Purwandi1d160e72012-01-09 16:33:28 +07001205 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001206 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001208
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 // --------------------------------------------------------------------
1210
1211 /**
1212 * Get_Where
1213 *
1214 * Allows the where clause, limit and offset to be added directly
1215 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 * @param string the where clause
1217 * @param string the limit clause
1218 * @param string the offset clause
1219 * @return object
1220 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001221 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001223 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 {
1225 $this->from($table);
1226 }
1227
1228 if ( ! is_null($where))
1229 {
1230 $this->where($where);
1231 }
Barry Mienydd671972010-10-04 16:33:58 +02001232
Andrey Andreev650b4c02012-06-11 12:07:15 +03001233 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 {
1235 $this->limit($limit, $offset);
1236 }
Barry Mienydd671972010-10-04 16:33:58 +02001237
Andrey Andreev24276a32012-01-08 02:44:38 +02001238 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 $this->_reset_select();
1240 return $result;
1241 }
1242
1243 // --------------------------------------------------------------------
1244
1245 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001246 * Insert_Batch
1247 *
1248 * Compiles batch insert strings and runs the queries
1249 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001250 * @param string the table to retrieve the results from
1251 * @param array an associative array of insert values
1252 * @return object
1253 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001254 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001255 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001256 if ( ! is_null($set))
1257 {
1258 $this->set_insert_batch($set);
1259 }
Barry Mienydd671972010-10-04 16:33:58 +02001260
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001261 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001262 {
1263 if ($this->db_debug)
1264 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001265 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001266 return $this->display_error('db_must_use_set');
1267 }
1268 return FALSE;
1269 }
1270
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001271 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001272 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001273 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001274 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001275 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001276 }
Barry Mienydd671972010-10-04 16:33:58 +02001277
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001278 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001279 }
1280
1281 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001282 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001283 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001284 $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 -06001285 }
Barry Mienydd671972010-10-04 16:33:58 +02001286
Derek Jonesd10e8962010-03-02 17:10:36 -06001287 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001288 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001289 }
1290
1291 // --------------------------------------------------------------------
1292
1293 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001294 * Insert_batch statement
1295 *
1296 * Generates a platform-specific insert string from the supplied data.
1297 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001298 * @param string the table name
1299 * @param array the insert keys
1300 * @param array the insert values
1301 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001302 */
1303 protected function _insert_batch($table, $keys, $values)
1304 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001305 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001306 }
1307
1308 // --------------------------------------------------------------------
1309
1310 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001311 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001312 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001313 * @param mixed
1314 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001315 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001316 * @return object
1317 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001318 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001319 {
1320 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001321
Derek Jonesd10e8962010-03-02 17:10:36 -06001322 if ( ! is_array($key))
1323 {
1324 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001325 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001326
Andrey Andreevfe642da2012-06-16 03:47:33 +03001327 is_bool($escape) OR $escape = $this->_protect_identifiers;
1328
Iban Eguia3c0a4522012-04-15 13:30:44 +02001329 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001330 sort($keys);
1331
1332 foreach ($key as $row)
1333 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001334 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001335 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1336 {
1337 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001338 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001339 return;
1340 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001341
Barry Mienydd671972010-10-04 16:33:58 +02001342 ksort($row); // puts $row in the same order as our keys
1343
Andrey Andreev650b4c02012-06-11 12:07:15 +03001344 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001345 {
1346 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001347 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001348 {
Barry Mienydd671972010-10-04 16:33:58 +02001349 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001350 }
1351
Andrey Andreev650b4c02012-06-11 12:07:15 +03001352 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001353 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001354
1355 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001356 }
1357
1358 foreach ($keys as $k)
1359 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001360 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001361 }
Barry Mienydd671972010-10-04 16:33:58 +02001362
Derek Jonesd10e8962010-03-02 17:10:36 -06001363 return $this;
1364 }
WanWizard7219c072011-12-28 14:09:05 +01001365
Kyle Farris0c147b32011-08-26 02:29:31 -04001366 // --------------------------------------------------------------------
1367
1368 /**
1369 * Get INSERT query string
1370 *
1371 * Compiles an insert query and returns the sql
1372 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001373 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001374 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001375 * @return string
1376 */
1377 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001378 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001379 if ($this->_validate_insert($table) === FALSE)
1380 {
1381 return FALSE;
1382 }
WanWizard7219c072011-12-28 14:09:05 +01001383
Kyle Farris76116012011-08-31 11:17:48 -04001384 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001385 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001386 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001387 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001388 array_keys($this->qb_set),
1389 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001390 );
WanWizard7219c072011-12-28 14:09:05 +01001391
Kyle Farris0c147b32011-08-26 02:29:31 -04001392 if ($reset === TRUE)
1393 {
1394 $this->_reset_write();
1395 }
WanWizard7219c072011-12-28 14:09:05 +01001396
Kyle Farris0c147b32011-08-26 02:29:31 -04001397 return $sql;
1398 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001399
Derek Allard2067d1a2008-11-13 22:59:24 +00001400 // --------------------------------------------------------------------
1401
1402 /**
1403 * Insert
1404 *
1405 * Compiles an insert string and runs the query
1406 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001407 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001408 * @param array an associative array of insert values
1409 * @return object
1410 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001411 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001412 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 if ( ! is_null($set))
1414 {
1415 $this->set($set);
1416 }
WanWizard7219c072011-12-28 14:09:05 +01001417
Kyle Farris0c147b32011-08-26 02:29:31 -04001418 if ($this->_validate_insert($table) === FALSE)
1419 {
1420 return FALSE;
1421 }
WanWizard7219c072011-12-28 14:09:05 +01001422
Kyle Farris76116012011-08-31 11:17:48 -04001423 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001424 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001425 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001426 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001427 array_keys($this->qb_set),
1428 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001429 );
Barry Mienydd671972010-10-04 16:33:58 +02001430
Kyle Farris0c147b32011-08-26 02:29:31 -04001431 $this->_reset_write();
1432 return $this->query($sql);
1433 }
WanWizard7219c072011-12-28 14:09:05 +01001434
Kyle Farris0c147b32011-08-26 02:29:31 -04001435 // --------------------------------------------------------------------
1436
1437 /**
1438 * Validate Insert
1439 *
1440 * This method is used by both insert() and get_compiled_insert() to
1441 * validate that the there data is actually being set and that table
1442 * has been chosen to be inserted into.
1443 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001444 * @param string the table to insert data into
1445 * @return string
1446 */
WanWizard7219c072011-12-28 14:09:05 +01001447 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001448 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001449 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001450 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001451 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001452 }
1453
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001454 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001455 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001456 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001457 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001458 elseif ( ! isset($this->qb_from[0]))
1459 {
1460 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1461 }
WanWizard7219c072011-12-28 14:09:05 +01001462
Kyle Farris0c147b32011-08-26 02:29:31 -04001463 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001464 }
Barry Mienydd671972010-10-04 16:33:58 +02001465
Phil Sturgeon9789f322011-07-15 15:14:05 -06001466 // --------------------------------------------------------------------
1467
1468 /**
1469 * Replace
1470 *
1471 * Compiles an replace into string and runs the query
1472 *
1473 * @param string the table to replace data into
1474 * @param array an associative array of insert values
1475 * @return object
1476 */
1477 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001478 {
1479 if ( ! is_null($set))
1480 {
1481 $this->set($set);
1482 }
Barry Mienydd671972010-10-04 16:33:58 +02001483
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001484 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001485 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001486 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001487 }
1488
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001489 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001490 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001491 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001492 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001493 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001494 }
Barry Mienydd671972010-10-04 16:33:58 +02001495
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001496 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001497 }
1498
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001499 $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 +00001500
Derek Jonesd10e8962010-03-02 17:10:36 -06001501 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001502 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001503 }
WanWizard7219c072011-12-28 14:09:05 +01001504
Kyle Farris0c147b32011-08-26 02:29:31 -04001505 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001506
Kyle Farris0c147b32011-08-26 02:29:31 -04001507 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001508 * Replace statement
1509 *
1510 * Generates a platform-specific replace string from the supplied data
1511 *
1512 * @param string the table name
1513 * @param array the insert keys
1514 * @param array the insert values
1515 * @return string
1516 */
1517 protected function _replace($table, $keys, $values)
1518 {
1519 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1520 }
1521
1522 // --------------------------------------------------------------------
1523
1524 /**
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001525 * From Tables
1526 *
1527 * This public function implicitly groups FROM tables so there is no confusion
1528 * about operator precedence in harmony with SQL standards
1529 *
1530 * @param array
1531 * @return string
1532 */
1533 protected function _from_tables($tables)
1534 {
1535 is_array($tables) OR $tables = array($tables);
1536
1537 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
1538 }
1539
1540 // --------------------------------------------------------------------
1541
1542 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001543 * Get UPDATE query string
1544 *
1545 * Compiles an update query and returns the sql
1546 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001547 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001548 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001549 * @return string
1550 */
1551 public function get_compiled_update($table = '', $reset = TRUE)
1552 {
1553 // Combine any cached components with the current statements
1554 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001555
Kyle Farris0c147b32011-08-26 02:29:31 -04001556 if ($this->_validate_update($table) === FALSE)
1557 {
1558 return FALSE;
1559 }
WanWizard7219c072011-12-28 14:09:05 +01001560
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001561 $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 +01001562
Kyle Farris0c147b32011-08-26 02:29:31 -04001563 if ($reset === TRUE)
1564 {
1565 $this->_reset_write();
1566 }
WanWizard7219c072011-12-28 14:09:05 +01001567
Kyle Farris0c147b32011-08-26 02:29:31 -04001568 return $sql;
1569 }
WanWizard7219c072011-12-28 14:09:05 +01001570
Derek Allard2067d1a2008-11-13 22:59:24 +00001571 // --------------------------------------------------------------------
1572
1573 /**
1574 * Update
1575 *
1576 * Compiles an update string and runs the query
1577 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001578 * @param string the table to retrieve the results from
1579 * @param array an associative array of update values
1580 * @param mixed the where clause
1581 * @return object
1582 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001583 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001584 {
1585 // Combine any cached components with the current statements
1586 $this->_merge_cache();
1587
1588 if ( ! is_null($set))
1589 {
1590 $this->set($set);
1591 }
Barry Mienydd671972010-10-04 16:33:58 +02001592
Kyle Farris0c147b32011-08-26 02:29:31 -04001593 if ($this->_validate_update($table) === FALSE)
1594 {
1595 return FALSE;
1596 }
1597
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001598 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001599 {
1600 $this->where($where);
1601 }
1602
Andrey Andreev650b4c02012-06-11 12:07:15 +03001603 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001604 {
1605 $this->limit($limit);
1606 }
1607
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001608 $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 +00001609
Kyle Farris0c147b32011-08-26 02:29:31 -04001610 $this->_reset_write();
1611 return $this->query($sql);
1612 }
WanWizard7219c072011-12-28 14:09:05 +01001613
Kyle Farris0c147b32011-08-26 02:29:31 -04001614 // --------------------------------------------------------------------
1615
1616 /**
1617 * Validate Update
1618 *
1619 * This method is used by both update() and get_compiled_update() to
1620 * validate that data is actually being set and that a table has been
1621 * chosen to be update.
1622 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001623 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001624 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001625 */
1626 protected function _validate_update($table = '')
1627 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001628 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001629 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001630 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001631 }
1632
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001633 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001634 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001635 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001636 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001637 elseif ( ! isset($this->qb_from[0]))
1638 {
1639 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1640 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001641
1642 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001643 }
WanWizard7219c072011-12-28 14:09:05 +01001644
Derek Jonesd10e8962010-03-02 17:10:36 -06001645 // --------------------------------------------------------------------
1646
1647 /**
1648 * Update_Batch
1649 *
1650 * Compiles an update string and runs the query
1651 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001652 * @param string the table to retrieve the results from
1653 * @param array an associative array of update values
1654 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001655 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001656 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001657 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001658 {
1659 // Combine any cached components with the current statements
1660 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001661
Derek Jonesd10e8962010-03-02 17:10:36 -06001662 if (is_null($index))
1663 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001664 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001665 }
1666
1667 if ( ! is_null($set))
1668 {
1669 $this->set_update_batch($set, $index);
1670 }
1671
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001672 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001673 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001674 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001675 }
1676
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001677 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001678 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001679 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001680 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001681 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001682 }
Barry Mienydd671972010-10-04 16:33:58 +02001683
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001684 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001685 }
Barry Mienydd671972010-10-04 16:33:58 +02001686
Derek Jonesd10e8962010-03-02 17:10:36 -06001687 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001688 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001689 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001690 $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 -06001691 }
Barry Mienydd671972010-10-04 16:33:58 +02001692
Derek Jonesd10e8962010-03-02 17:10:36 -06001693 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001694 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001695 }
1696
1697 // --------------------------------------------------------------------
1698
1699 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001700 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001701 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001702 * @param array
1703 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001704 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001705 * @return object
1706 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001707 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001708 {
1709 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001710
Derek Jonesd10e8962010-03-02 17:10:36 -06001711 if ( ! is_array($key))
1712 {
1713 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001714 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001715
Andrey Andreevfe642da2012-06-16 03:47:33 +03001716 is_bool($escape) OR $escape = $this->_protect_identifiers;
1717
Derek Jonesd10e8962010-03-02 17:10:36 -06001718 foreach ($key as $k => $v)
1719 {
1720 $index_set = FALSE;
1721 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001722 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001723 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001724 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001725 {
1726 $index_set = TRUE;
1727 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001728
Andrey Andreevfe642da2012-06-16 03:47:33 +03001729 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001730 }
1731
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001732 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001733 {
1734 return $this->display_error('db_batch_missing_index');
1735 }
1736
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001737 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001738 }
Barry Mienydd671972010-10-04 16:33:58 +02001739
Derek Jonesd10e8962010-03-02 17:10:36 -06001740 return $this;
1741 }
1742
Derek Allard2067d1a2008-11-13 22:59:24 +00001743 // --------------------------------------------------------------------
1744
1745 /**
1746 * Empty Table
1747 *
1748 * Compiles a delete string and runs "DELETE FROM table"
1749 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001750 * @param string the table to empty
1751 * @return object
1752 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001753 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001754 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001755 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001756 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001757 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001758 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001759 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001760 }
1761
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001762 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001763 }
1764 else
1765 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001766 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001767 }
1768
1769 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001770 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001771 return $this->query($sql);
1772 }
1773
1774 // --------------------------------------------------------------------
1775
1776 /**
1777 * Truncate
1778 *
1779 * Compiles a truncate string and runs the query
1780 * If the database does not support the truncate() command
1781 * This function maps to "DELETE FROM table"
1782 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001783 * @param string the table to truncate
1784 * @return object
1785 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001786 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001787 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001788 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001790 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001792 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 }
1794
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001795 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001796 }
1797 else
1798 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001799 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001800 }
1801
1802 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001803 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001804 return $this->query($sql);
1805 }
WanWizard7219c072011-12-28 14:09:05 +01001806
Kyle Farris0c147b32011-08-26 02:29:31 -04001807 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001808
Kyle Farris0c147b32011-08-26 02:29:31 -04001809 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001810 * Truncate statement
1811 *
1812 * Generates a platform-specific truncate string from the supplied data
1813 *
1814 * If the database does not support the truncate() command,
1815 * then this method maps to 'DELETE FROM table'
1816 *
1817 * @param string the table name
1818 * @return string
1819 */
1820 protected function _truncate($table)
1821 {
1822 return 'TRUNCATE '.$table;
1823 }
1824
1825 // --------------------------------------------------------------------
1826
1827 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001828 * Get DELETE query string
1829 *
1830 * Compiles a delete query string and returns the sql
1831 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001832 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001833 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001834 * @return string
1835 */
1836 public function get_compiled_delete($table = '', $reset = TRUE)
1837 {
1838 $this->return_delete_sql = TRUE;
1839 $sql = $this->delete($table, '', NULL, $reset);
1840 $this->return_delete_sql = FALSE;
1841 return $sql;
1842 }
WanWizard7219c072011-12-28 14:09:05 +01001843
Derek Allard2067d1a2008-11-13 22:59:24 +00001844 // --------------------------------------------------------------------
1845
1846 /**
1847 * Delete
1848 *
1849 * Compiles a delete string and runs the query
1850 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001851 * @param mixed the table(s) to delete from. String or array
1852 * @param mixed the where clause
1853 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001854 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001855 * @return object
1856 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001857 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001858 {
1859 // Combine any cached components with the current statements
1860 $this->_merge_cache();
1861
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001862 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001863 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001864 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001865 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001866 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001867 }
1868
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001869 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001870 }
1871 elseif (is_array($table))
1872 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001873 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001874 {
1875 $this->delete($single_table, $where, $limit, FALSE);
1876 }
1877
1878 $this->_reset_write();
1879 return;
1880 }
1881 else
1882 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001883 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001884 }
1885
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001886 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001887 {
1888 $this->where($where);
1889 }
1890
Andrey Andreev650b4c02012-06-11 12:07:15 +03001891 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001892 {
1893 $this->limit($limit);
1894 }
1895
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001896 if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001897 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001898 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001899 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001900
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001901 $sql = $this->_delete($table, $this->qb_where, $this->qb_like, $this->qb_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001902 if ($reset_data)
1903 {
1904 $this->_reset_write();
1905 }
WanWizard7219c072011-12-28 14:09:05 +01001906
Andrey Andreev24276a32012-01-08 02:44:38 +02001907 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001908 }
WanWizard7219c072011-12-28 14:09:05 +01001909
Derek Allard2067d1a2008-11-13 22:59:24 +00001910 // --------------------------------------------------------------------
1911
1912 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001913 * Delete statement
1914 *
1915 * Generates a platform-specific delete string from the supplied data
1916 *
1917 * @param string the table name
1918 * @param array the where clause
1919 * @param array the like clause
1920 * @param string the limit clause
1921 * @return string
1922 */
1923 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
1924 {
1925 $conditions = array();
1926
1927 empty($where) OR $conditions[] = implode(' ', $where);
1928 empty($like) OR $conditions[] = implode(' ', $like);
1929
1930 return 'DELETE FROM '.$table
1931 .(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '')
Andrey Andreev650b4c02012-06-11 12:07:15 +03001932 .($limit ? ' LIMIT '.(int) $limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001933 }
1934
1935 // --------------------------------------------------------------------
1936
1937 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001938 * DB Prefix
1939 *
1940 * Prepends a database prefix if one exists in configuration
1941 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001942 * @param string the table
1943 * @return string
1944 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001945 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001946 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001947 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001948 {
1949 $this->display_error('db_table_name_required');
1950 }
1951
1952 return $this->dbprefix.$table;
1953 }
1954
1955 // --------------------------------------------------------------------
1956
1957 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001958 * Set DB Prefix
1959 *
1960 * Set's the DB Prefix to something new without needing to reconnect
1961 *
1962 * @param string the prefix
1963 * @return string
1964 */
1965 public function set_dbprefix($prefix = '')
1966 {
1967 return $this->dbprefix = $prefix;
1968 }
1969
1970 // --------------------------------------------------------------------
1971
1972 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001973 * Track Aliases
1974 *
1975 * Used to track SQL statements written with aliased tables.
1976 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001977 * @param string The table to inspect
1978 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001979 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001980 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001981 {
1982 if (is_array($table))
1983 {
1984 foreach ($table as $t)
1985 {
1986 $this->_track_aliases($t);
1987 }
1988 return;
1989 }
Barry Mienydd671972010-10-04 16:33:58 +02001990
Derek Jones37f4b9c2011-07-01 17:56:50 -05001991 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001992 // the string into discreet statements
1993 if (strpos($table, ',') !== FALSE)
1994 {
1995 return $this->_track_aliases(explode(',', $table));
1996 }
Barry Mienydd671972010-10-04 16:33:58 +02001997
Derek Allard2067d1a2008-11-13 22:59:24 +00001998 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001999 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002000 {
2001 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002002 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002003
Derek Allard2067d1a2008-11-13 22:59:24 +00002004 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002005 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002006
Derek Allard2067d1a2008-11-13 22:59:24 +00002007 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002008 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002009 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002010 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002011 }
2012 }
2013 }
WanWizard7219c072011-12-28 14:09:05 +01002014
Derek Allard2067d1a2008-11-13 22:59:24 +00002015 // --------------------------------------------------------------------
2016
2017 /**
2018 * Compile the SELECT statement
2019 *
2020 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05002021 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00002022 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002023 * @return string
2024 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002025 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002026 {
2027 // Combine any cached components with the current statements
2028 $this->_merge_cache();
2029
Derek Allard2067d1a2008-11-13 22:59:24 +00002030 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002031 if ($select_override !== FALSE)
2032 {
2033 $sql = $select_override;
2034 }
2035 else
2036 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002037 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002038
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002039 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002040 {
Barry Mienydd671972010-10-04 16:33:58 +02002041 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002042 }
2043 else
Barry Mienydd671972010-10-04 16:33:58 +02002044 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002045 // Cycle through the "select" portion of the query and prep each column name.
2046 // The reason we protect identifiers here rather then in the select() function
2047 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002048 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002049 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002050 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002051 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002052 }
Barry Mienydd671972010-10-04 16:33:58 +02002053
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002054 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002055 }
2056 }
2057
Derek Allard2067d1a2008-11-13 22:59:24 +00002058 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002059 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002060 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002061 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002062 }
2063
Derek Allard2067d1a2008-11-13 22:59:24 +00002064 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002065 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002066 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002067 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002068 }
2069
Derek Allard2067d1a2008-11-13 22:59:24 +00002070 // Write the "WHERE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002071 if (count($this->qb_where) > 0 OR count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002072 {
Greg Akere156c6e2011-04-20 16:03:04 -05002073 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00002074 }
2075
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002076 $sql .= implode("\n", $this->qb_where);
Derek Allard2067d1a2008-11-13 22:59:24 +00002077
Derek Allard2067d1a2008-11-13 22:59:24 +00002078 // Write the "LIKE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002079 if (count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002080 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002081 if (count($this->qb_where) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002082 {
2083 $sql .= "\nAND ";
2084 }
2085
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002086 $sql .= implode("\n", $this->qb_like);
Derek Allard2067d1a2008-11-13 22:59:24 +00002087 }
2088
Derek Allard2067d1a2008-11-13 22:59:24 +00002089 // Write the "GROUP BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002090 if (count($this->qb_groupby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002091 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002092 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002093 }
2094
Derek Allard2067d1a2008-11-13 22:59:24 +00002095 // Write the "HAVING" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002096 if (count($this->qb_having) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002097 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002098 $sql .= "\nHAVING ".implode("\n", $this->qb_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00002099 }
2100
Derek Allard2067d1a2008-11-13 22:59:24 +00002101 // Write the "ORDER BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002102 if (count($this->qb_orderby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002103 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002104 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002105 }
2106
Derek Allard2067d1a2008-11-13 22:59:24 +00002107 // Write the "LIMIT" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002108 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002109 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002110 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00002111 }
2112
2113 return $sql;
2114 }
2115
2116 // --------------------------------------------------------------------
2117
2118 /**
2119 * Object to Array
2120 *
2121 * Takes an object as input and converts the class variables to array key/vals
2122 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002123 * @param object
2124 * @return array
2125 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002126 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002127 {
2128 if ( ! is_object($object))
2129 {
2130 return $object;
2131 }
Barry Mienydd671972010-10-04 16:33:58 +02002132
Derek Allard2067d1a2008-11-13 22:59:24 +00002133 $array = array();
2134 foreach (get_object_vars($object) as $key => $val)
2135 {
2136 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002137 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002138 {
2139 $array[$key] = $val;
2140 }
2141 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002142
2143 return $array;
2144 }
Barry Mienydd671972010-10-04 16:33:58 +02002145
Derek Jonesd10e8962010-03-02 17:10:36 -06002146 // --------------------------------------------------------------------
2147
2148 /**
2149 * Object to Array
2150 *
2151 * Takes an object as input and converts the class variables to array key/vals
2152 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002153 * @param object
2154 * @return array
2155 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002156 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002157 {
2158 if ( ! is_object($object))
2159 {
2160 return $object;
2161 }
Barry Mienydd671972010-10-04 16:33:58 +02002162
Derek Jonesd10e8962010-03-02 17:10:36 -06002163 $array = array();
2164 $out = get_object_vars($object);
2165 $fields = array_keys($out);
2166
2167 foreach ($fields as $val)
2168 {
2169 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002170 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002171 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002172 $i = 0;
2173 foreach ($out[$val] as $data)
2174 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002175 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002176 }
2177 }
2178 }
2179
Derek Allard2067d1a2008-11-13 22:59:24 +00002180 return $array;
2181 }
Barry Mienydd671972010-10-04 16:33:58 +02002182
Derek Allard2067d1a2008-11-13 22:59:24 +00002183 // --------------------------------------------------------------------
2184
2185 /**
2186 * Start Cache
2187 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002188 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002189 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002190 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002191 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002192 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002193 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002194 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002195 }
2196
2197 // --------------------------------------------------------------------
2198
2199 /**
2200 * Stop Cache
2201 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002202 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002203 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002204 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002205 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002206 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002207 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002208 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002209 }
2210
2211 // --------------------------------------------------------------------
2212
2213 /**
2214 * Flush Cache
2215 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002216 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002217 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002218 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002219 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002220 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002221 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002222 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002223 'qb_cache_select' => array(),
2224 'qb_cache_from' => array(),
2225 'qb_cache_join' => array(),
2226 'qb_cache_where' => array(),
2227 'qb_cache_like' => array(),
2228 'qb_cache_groupby' => array(),
2229 'qb_cache_having' => array(),
2230 'qb_cache_orderby' => array(),
2231 'qb_cache_set' => array(),
2232 'qb_cache_exists' => array(),
2233 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002234 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002235 }
2236
2237 // --------------------------------------------------------------------
2238
2239 /**
2240 * Merge Cache
2241 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002242 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002243 * locally called ones.
2244 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002245 * @return void
2246 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002247 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002248 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002249 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002250 {
2251 return;
2252 }
2253
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002254 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002255 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002256 $qb_variable = 'qb_'.$val;
2257 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002258
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002259 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002260 {
2261 continue;
2262 }
2263
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002264 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002265 }
2266
2267 // If we are "protecting identifiers" we need to examine the "from"
2268 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002269 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002270 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002271 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002272 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002273
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002274 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002275 }
WanWizard7219c072011-12-28 14:09:05 +01002276
Kyle Farris0c147b32011-08-26 02:29:31 -04002277 // --------------------------------------------------------------------
2278
2279 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002280 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002281 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002282 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002283 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002284 * @return void
2285 */
2286 public function reset_query()
2287 {
2288 $this->_reset_select();
2289 $this->_reset_write();
2290 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002291
2292 // --------------------------------------------------------------------
2293
2294 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002295 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002296 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002297 * @param array An array of fields to reset
2298 * @return void
2299 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002300 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002301 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002302 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002303 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002304 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002305 {
2306 $this->$item = $default_value;
2307 }
2308 }
2309 }
2310
2311 // --------------------------------------------------------------------
2312
2313 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002314 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002315 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002316 * @return void
2317 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002318 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002319 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002320 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002321 'qb_select' => array(),
2322 'qb_from' => array(),
2323 'qb_join' => array(),
2324 'qb_where' => array(),
2325 'qb_like' => array(),
2326 'qb_groupby' => array(),
2327 'qb_having' => array(),
2328 'qb_orderby' => array(),
2329 'qb_wherein' => array(),
2330 'qb_aliased_tables' => array(),
2331 'qb_no_escape' => array(),
2332 'qb_distinct' => FALSE,
2333 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002334 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002335 )
2336 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002337 }
Barry Mienydd671972010-10-04 16:33:58 +02002338
Derek Allard2067d1a2008-11-13 22:59:24 +00002339 // --------------------------------------------------------------------
2340
2341 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002342 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002343 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002344 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002345 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002346 * @return void
2347 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002348 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002349 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002350 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002351 'qb_set' => array(),
2352 'qb_from' => array(),
2353 'qb_where' => array(),
2354 'qb_like' => array(),
2355 'qb_orderby' => array(),
2356 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002357 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002358 )
2359 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002360 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002361
Derek Allard2067d1a2008-11-13 22:59:24 +00002362}
2363
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002364/* End of file DB_query_builder.php */
Andrey Andreev58803fb2012-06-24 00:45:37 +03002365/* Location: ./system/database/DB_query_builder.php */