blob: 5eb6bbb4e0e5a205f1f591f08cb46ce9bd4df428 [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
Andrey Andreev42870232012-06-12 01:30:20 +0300327 * @param string wether not to try to escape identifiers
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 * @return object
329 */
Andrey Andreev42870232012-06-12 01:30:20 +0300330 public function join($table, $cond, $type = '', $escape = TRUE)
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 Andreev42870232012-06-12 01:30:20 +0300350 // Split multiple conditions
351 if ($escape === TRUE && preg_match_all('/\sAND\s|\sOR\s/i', $cond, $m, PREG_SET_ORDER | PREG_OFFSET_CAPTURE))
352 {
353 $newcond = '';
354 $m[0][] = array('', strlen($cond));
355
356 for ($i = 0, $c = count($m[0]), $s = 0;
357 $i < $c;
358 $s += $m[0][$i][1] + strlen($m[0][$i][0]), $i++)
359 {
360 $temp = substr($cond, $s, $m[0][$i][1]);
361
362 $newcond .= preg_match('/([\[\w\.-]+)([\W\s]+)(.+)/i', $temp, $match)
363 ? $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3])
364 : $temp;
365
366 $newcond .= $m[0][$i][0];
367 }
368
369 $cond = $newcond;
370 }
371 // Split apart the condition and protect the identifiers
372 elseif ($escape === TRUE && preg_match('/([\[\w\.-]+)([\W\s]+)(.+)/i', $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200374 $cond = $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
Andrey Andreev42870232012-06-12 01:30:20 +0300377 // Do we want to escape the table name?
378 if ($escape === TRUE)
379 {
380 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
381 }
382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 // Assemble the JOIN statement
Andrey Andreev8295c842012-06-15 03:42:25 +0300384 $this->qb_join[] = $join = $type.'JOIN '.$table.' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000385
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000386 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000388 $this->qb_cache_join[] = $join;
389 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 }
391
392 return $this;
393 }
394
395 // --------------------------------------------------------------------
396
397 /**
398 * Where
399 *
400 * Generates the WHERE portion of the query. Separates
401 * multiple calls with AND
402 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 * @param mixed
404 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300405 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 * @return object
407 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600408 public function where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
410 return $this->_where($key, $value, 'AND ', $escape);
411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 // --------------------------------------------------------------------
414
415 /**
416 * OR Where
417 *
418 * Generates the WHERE portion of the query. Separates
419 * multiple calls with OR
420 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 * @param mixed
422 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300423 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 * @return object
425 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600426 public function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
428 return $this->_where($key, $value, 'OR ', $escape);
429 }
430
431 // --------------------------------------------------------------------
432
433 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 * Where
435 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600436 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 * @param mixed
439 * @param mixed
440 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300441 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 * @return object
443 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600444 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 {
WanWizard7219c072011-12-28 14:09:05 +0100446 $type = $this->_group_get_type($type);
447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 if ( ! is_array($key))
449 {
450 $key = array($key => $value);
451 }
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 // If the escape value was not set will will base it on the global setting
Andrey Andreeve10fb792012-06-15 12:07:04 +0300454 is_bool($escape) OR $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000455
456 foreach ($key as $k => $v)
457 {
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100458 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000459
Andrey Andreev9c14f652012-06-10 14:35:07 +0300460 $k = $this->_has_operator($k)
Andrey Andreev392b6ad2012-06-10 15:08:59 +0300461 ? $this->protect_identifiers(substr($k, 0, strpos(rtrim($k), ' ')), FALSE, $escape).strchr(rtrim($k), ' ')
Andrey Andreev9c14f652012-06-10 14:35:07 +0300462 : $this->protect_identifiers($k, FALSE, $escape);
463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 if (is_null($v) && ! $this->_has_operator($k))
465 {
466 // value appears not to have been set, assign the test to IS NULL
467 $k .= ' IS NULL';
468 }
Barry Mienydd671972010-10-04 16:33:58 +0200469
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 if ( ! is_null($v))
471 {
472 if ($escape === TRUE)
473 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 $v = ' '.$this->escape($v);
475 }
WanWizard7219c072011-12-28 14:09:05 +0100476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 if ( ! $this->_has_operator($k))
478 {
Greg Akere156c6e2011-04-20 16:03:04 -0500479 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 }
481 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000482
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000483 $this->qb_where[] = $prefix.$k.$v;
484 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000486 $this->qb_cache_where[] = $prefix.$k.$v;
487 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 }
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 return $this;
493 }
494
495 // --------------------------------------------------------------------
496
497 /**
498 * Where_in
499 *
500 * Generates a WHERE field IN ('item', 'item') SQL query joined with
501 * AND if appropriate
502 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 * @param string The field to search
504 * @param array The values searched on
505 * @return object
506 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600507 public function where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
509 return $this->_where_in($key, $values);
510 }
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 // --------------------------------------------------------------------
513
514 /**
515 * Where_in_or
516 *
517 * Generates a WHERE field IN ('item', 'item') SQL query joined with
518 * OR if appropriate
519 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 * @param string The field to search
521 * @param array The values searched on
522 * @return object
523 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600524 public function or_where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
526 return $this->_where_in($key, $values, FALSE, 'OR ');
527 }
528
529 // --------------------------------------------------------------------
530
531 /**
532 * Where_not_in
533 *
534 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
535 * with AND if appropriate
536 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 * @param string The field to search
538 * @param array The values searched on
539 * @return object
540 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600541 public function where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 {
543 return $this->_where_in($key, $values, TRUE);
544 }
Barry Mienydd671972010-10-04 16:33:58 +0200545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 // --------------------------------------------------------------------
547
548 /**
549 * Where_not_in_or
550 *
551 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
552 * with OR if appropriate
553 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 * @param string The field to search
555 * @param array The values searched on
556 * @return object
557 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600558 public function or_where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 {
560 return $this->_where_in($key, $values, TRUE, 'OR ');
561 }
562
563 // --------------------------------------------------------------------
564
565 /**
566 * Where_in
567 *
568 * Called by where_in, where_in_or, where_not_in, where_not_in_or
569 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 * @param string The field to search
571 * @param array The values searched on
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300572 * @param bool If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200573 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 * @return object
575 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600576 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 {
578 if ($key === NULL OR $values === NULL)
579 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300580 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 }
Barry Mienydd671972010-10-04 16:33:58 +0200582
WanWizard7219c072011-12-28 14:09:05 +0100583 $type = $this->_group_get_type($type);
584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 if ( ! is_array($values))
586 {
587 $values = array($values);
588 }
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 $not = ($not) ? ' NOT' : '';
591
592 foreach ($values as $value)
593 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000594 $this->qb_wherein[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 }
596
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000597 $prefix = (count($this->qb_where) === 0) ? '' : $type;
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000598 $this->qb_where[] = $where_in = $prefix.$this->protect_identifiers($key).$not.' IN ('.implode(', ', $this->qb_wherein).') ';
Barry Mienydd671972010-10-04 16:33:58 +0200599
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000600 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000602 $this->qb_cache_where[] = $where_in;
603 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 }
605
606 // reset the array for multiple calls
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000607 $this->qb_wherein = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 return $this;
609 }
Barry Mienydd671972010-10-04 16:33:58 +0200610
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 // --------------------------------------------------------------------
612
613 /**
614 * Like
615 *
616 * Generates a %LIKE% portion of the query. Separates
617 * multiple calls with AND
618 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 * @param mixed
620 * @param mixed
621 * @return object
622 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600623 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 {
625 return $this->_like($field, $match, 'AND ', $side);
626 }
627
628 // --------------------------------------------------------------------
629
630 /**
631 * Not Like
632 *
633 * Generates a NOT LIKE portion of the query. Separates
634 * multiple calls with AND
635 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 * @param mixed
637 * @param mixed
638 * @return object
639 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600640 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 {
642 return $this->_like($field, $match, 'AND ', $side, 'NOT');
643 }
Barry Mienydd671972010-10-04 16:33:58 +0200644
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 // --------------------------------------------------------------------
646
647 /**
648 * OR Like
649 *
650 * Generates a %LIKE% portion of the query. Separates
651 * multiple calls with OR
652 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 * @param mixed
654 * @param mixed
655 * @return object
656 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600657 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 {
659 return $this->_like($field, $match, 'OR ', $side);
660 }
661
662 // --------------------------------------------------------------------
663
664 /**
665 * OR Not Like
666 *
667 * Generates a NOT LIKE portion of the query. Separates
668 * multiple calls with OR
669 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 * @param mixed
671 * @param mixed
672 * @return object
673 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600674 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 {
676 return $this->_like($field, $match, 'OR ', $side, 'NOT');
677 }
Barry Mienydd671972010-10-04 16:33:58 +0200678
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 // --------------------------------------------------------------------
680
681 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 * Like
683 *
684 * Called by like() or orlike()
685 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 * @param mixed
687 * @param mixed
688 * @param string
689 * @return object
690 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600691 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
WanWizard7219c072011-12-28 14:09:05 +0100693 $type = $this->_group_get_type($type);
694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 if ( ! is_array($field))
696 {
697 $field = array($field => $match);
698 }
Barry Mienydd671972010-10-04 16:33:58 +0200699
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 foreach ($field as $k => $v)
701 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000702 $k = $this->protect_identifiers($k);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000703 $prefix = (count($this->qb_like) === 0) ? '' : $type;
Derek Jonese4ed5832009-02-20 21:44:59 +0000704 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300705
Andrey Andreev24276a32012-01-08 02:44:38 +0200706 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400707 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100708 $like_statement = "{$prefix} $k $not LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400709 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200710 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100712 $like_statement = "{$prefix} $k $not LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200714 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100716 $like_statement = "{$prefix} $k $not LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 }
718 else
719 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100720 $like_statement = "{$prefix} $k $not LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600722
Derek Jonese4ed5832009-02-20 21:44:59 +0000723 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100724 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000725 {
Greg Aker0d424892010-01-26 02:14:44 +0000726 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000727 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600728
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000729 $this->qb_like[] = $like_statement;
730 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000732 $this->qb_cache_like[] = $like_statement;
733 $this->qb_cache_exists[] = 'like';
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 }
Barry Mienydd671972010-10-04 16:33:58 +0200735
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200737
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 return $this;
739 }
Barry Mienydd671972010-10-04 16:33:58 +0200740
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 // --------------------------------------------------------------------
742
743 /**
WanWizard7219c072011-12-28 14:09:05 +0100744 * Starts a query group.
745 *
746 * @param string (Internal use only)
747 * @param string (Internal use only)
748 * @return object
749 */
750 public function group_start($not = '', $type = 'AND ')
751 {
752 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100753
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000754 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100755 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000756 $this->qb_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (';
WanWizard7219c072011-12-28 14:09:05 +0100757
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000758 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100759 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000760 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100761 }
762
763 return $this;
764 }
765
766 // --------------------------------------------------------------------
767
768 /**
769 * Starts a query group, but ORs the group
770 *
771 * @return object
772 */
773 public function or_group_start()
774 {
775 return $this->group_start('', 'OR ');
776 }
777
778 // --------------------------------------------------------------------
779
780 /**
781 * Starts a query group, but NOTs the group
782 *
783 * @return object
784 */
785 public function not_group_start()
786 {
787 return $this->group_start('NOT ', 'AND ');
788 }
789
790 // --------------------------------------------------------------------
791
792 /**
793 * Starts a query group, but OR NOTs the group
794 *
795 * @return object
796 */
797 public function or_not_group_start()
798 {
799 return $this->group_start('NOT ', 'OR ');
800 }
801
802 // --------------------------------------------------------------------
803
804 /**
805 * Ends a query group
806 *
807 * @return object
808 */
809 public function group_end()
810 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000811 $this->qb_where_group_started = FALSE;
812 $this->qb_where[] = $value = str_repeat(' ', $this->qb_where_group_count--) . ')';
WanWizard7219c072011-12-28 14:09:05 +0100813
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000814 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100815 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000816 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100817 }
818
WanWizard7219c072011-12-28 14:09:05 +0100819 return $this;
820 }
821
822 // --------------------------------------------------------------------
823
824 /**
825 * Group_get_type
826 *
827 * Called by group_start(), _like(), _where() and _where_in()
828 *
829 * @param string
830 * @return string
831 */
832 protected function _group_get_type($type)
833 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000834 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100835 {
836 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000837 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100838 }
839
840 return $type;
841 }
842
843 // --------------------------------------------------------------------
844
845 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 * GROUP BY
847 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 * @param string
849 * @return object
850 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600851 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 {
853 if (is_string($by))
854 {
855 $by = explode(',', $by);
856 }
Barry Mienydd671972010-10-04 16:33:58 +0200857
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 foreach ($by as $val)
859 {
860 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200861
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100862 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000864 $this->qb_groupby[] = $val = $this->protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200865
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000866 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000868 $this->qb_cache_groupby[] = $val;
869 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 }
871 }
872 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200873
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 return $this;
875 }
876
877 // --------------------------------------------------------------------
878
879 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 * Sets the HAVING value
881 *
882 * Separates multiple calls with AND
883 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 * @param string
885 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300886 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 * @return object
888 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600889 public function having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 {
891 return $this->_having($key, $value, 'AND ', $escape);
892 }
Barry Mienydd671972010-10-04 16:33:58 +0200893
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 // --------------------------------------------------------------------
895
896 /**
897 * Sets the OR HAVING value
898 *
899 * Separates multiple calls with OR
900 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 * @param string
902 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300903 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 * @return object
905 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600906 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 {
908 return $this->_having($key, $value, 'OR ', $escape);
909 }
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 // --------------------------------------------------------------------
912
913 /**
914 * Sets the HAVING values
915 *
916 * Called by having() or or_having()
917 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 * @param string
919 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300920 * @param string
921 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 * @return object
923 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600924 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 {
926 if ( ! is_array($key))
927 {
928 $key = array($key => $value);
929 }
Barry Mienydd671972010-10-04 16:33:58 +0200930
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 foreach ($key as $k => $v)
932 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000933 $prefix = (count($this->qb_having) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000934
Andrey Andreev974c75b2012-06-15 12:30:02 +0300935 $k = $this->_has_operator($k)
936 ? $this->protect_identifiers(substr($k, 0, strpos(rtrim($k), ' ')), FALSE, $escape).strchr(rtrim($k), ' ')
937 : $this->protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000938
939 if ( ! $this->_has_operator($k))
940 {
941 $k .= ' = ';
942 }
943
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100944 if ($v !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400946 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 }
Barry Mienydd671972010-10-04 16:33:58 +0200948
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000949 $this->qb_having[] = $prefix.$k.$v;
950 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000952 $this->qb_cache_having[] = $prefix.$k.$v;
953 $this->qb_cache_exists[] = 'having';
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 }
955 }
Barry Mienydd671972010-10-04 16:33:58 +0200956
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 return $this;
958 }
Barry Mienydd671972010-10-04 16:33:58 +0200959
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 // --------------------------------------------------------------------
961
962 /**
963 * Sets the ORDER BY value
964 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 * @param string
966 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100967 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 * @return object
969 */
Andrey Andreevd24160c2012-06-16 03:21:20 +0300970 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200972 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 {
974 $orderby = ''; // Random results want or don't need a field name
975 $direction = $this->_random_keyword;
976 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100977 elseif (trim($direction) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 {
Andrey Andreev650b4c02012-06-11 12:07:15 +0300979 $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : ' ASC';
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 }
Barry Mienydd671972010-10-04 16:33:58 +0200981
Andrey Andreevd24160c2012-06-16 03:21:20 +0300982 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +0200983
Andrey Andreevd24160c2012-06-16 03:21:20 +0300984 if ($escape === TRUE && strpos($orderby, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 {
986 $temp = array();
987 foreach (explode(',', $orderby) as $part)
988 {
989 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000990 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 {
Andrey Andreev88cb2782012-06-11 20:40:50 +0300992 $part = preg_match('/^(.+)\s+(ASC|DESC)$/i', $part, $matches)
993 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
994 : $this->protect_identifiers($part);
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 }
Barry Mienydd671972010-10-04 16:33:58 +0200996
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 $temp[] = $part;
998 }
Barry Mienydd671972010-10-04 16:33:58 +0200999
1000 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001002 elseif ($direction !== $this->_random_keyword && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 {
Andrey Andreev1d1d7ff2012-06-11 22:35:35 +03001004 $orderby = preg_match('/^(.+)\s+(ASC|DESC)$/i', $orderby, $matches)
Andrey Andreev88cb2782012-06-11 20:40:50 +03001005 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
1006 : $this->protect_identifiers($orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 }
Barry Mienydd671972010-10-04 16:33:58 +02001008
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001009 $this->qb_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +02001010
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001011 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001013 $this->qb_cache_orderby[] = $orderby_statement;
1014 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 }
1016
1017 return $this;
1018 }
Barry Mienydd671972010-10-04 16:33:58 +02001019
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 // --------------------------------------------------------------------
1021
1022 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 * Sets the LIMIT value
1024 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001025 * @param int the limit value
1026 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 * @return object
1028 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +02001029 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001031 $this->qb_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +00001032
Andrey Andreev650b4c02012-06-11 12:07:15 +03001033 if ( ! empty($offset))
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001035 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 }
Barry Mienydd671972010-10-04 16:33:58 +02001037
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 return $this;
1039 }
Barry Mienydd671972010-10-04 16:33:58 +02001040
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 // --------------------------------------------------------------------
1042
1043 /**
1044 * Sets the OFFSET value
1045 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001046 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 * @return object
1048 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001049 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001051 $this->qb_offset = (int) $offset;
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 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001058 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 * @param mixed
1061 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001062 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 * @return object
1064 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001065 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 {
1067 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001068
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 if ( ! is_array($key))
1070 {
1071 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001072 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001073
1074 foreach ($key as $k => $v)
1075 {
1076 if ($escape === FALSE)
1077 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001078 $this->qb_set[$this->protect_identifiers($k)] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 }
1080 else
1081 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001082 $this->qb_set[$this->protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 }
1084 }
Barry Mienydd671972010-10-04 16:33:58 +02001085
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 return $this;
1087 }
WanWizard7219c072011-12-28 14:09:05 +01001088
Kyle Farris0c147b32011-08-26 02:29:31 -04001089 // --------------------------------------------------------------------
1090
1091 /**
1092 * Get SELECT query string
1093 *
1094 * Compiles a SELECT query string and returns the sql.
1095 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001096 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001097 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001098 * @return string
1099 */
WanWizard7219c072011-12-28 14:09:05 +01001100 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001101 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001102 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001103 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001104 $this->_track_aliases($table);
1105 $this->from($table);
1106 }
WanWizard7219c072011-12-28 14:09:05 +01001107
Andrey Andreev650b4c02012-06-11 12:07:15 +03001108 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001109
Kyle Farris0c147b32011-08-26 02:29:31 -04001110 if ($reset === TRUE)
1111 {
1112 $this->_reset_select();
1113 }
WanWizard7219c072011-12-28 14:09:05 +01001114
Kyle Farris0c147b32011-08-26 02:29:31 -04001115 return $select;
1116 }
WanWizard7219c072011-12-28 14:09:05 +01001117
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 // --------------------------------------------------------------------
1119
1120 /**
1121 * Get
1122 *
1123 * Compiles the select statement based on the other functions called
1124 * and runs the query
1125 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 * @param string the table
1127 * @param string the limit clause
1128 * @param string the offset clause
1129 * @return object
1130 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001131 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001133 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 {
1135 $this->_track_aliases($table);
1136 $this->from($table);
1137 }
Barry Mienydd671972010-10-04 16:33:58 +02001138
Andrey Andreev650b4c02012-06-11 12:07:15 +03001139 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 {
1141 $this->limit($limit, $offset);
1142 }
Barry Mienydd671972010-10-04 16:33:58 +02001143
Andrey Andreev24276a32012-01-08 02:44:38 +02001144 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 $this->_reset_select();
1146 return $result;
1147 }
1148
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001149 // --------------------------------------------------------------------
1150
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 /**
1152 * "Count All Results" query
1153 *
Barry Mienydd671972010-10-04 16:33:58 +02001154 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001155 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 * @param string
1158 * @return string
1159 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001160 public function count_all_results($table = '')
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 Andreev032e7ea2012-03-06 19:48:35 +02001168 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001170
Purwandi1d160e72012-01-09 16:33:28 +07001171 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001173 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 }
1175
Purwandi1d160e72012-01-09 16:33:28 +07001176 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001177 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001179
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 // --------------------------------------------------------------------
1181
1182 /**
1183 * Get_Where
1184 *
1185 * Allows the where clause, limit and offset to be added directly
1186 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 * @param string the where clause
1188 * @param string the limit clause
1189 * @param string the offset clause
1190 * @return object
1191 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001192 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001194 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 {
1196 $this->from($table);
1197 }
1198
1199 if ( ! is_null($where))
1200 {
1201 $this->where($where);
1202 }
Barry Mienydd671972010-10-04 16:33:58 +02001203
Andrey Andreev650b4c02012-06-11 12:07:15 +03001204 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 {
1206 $this->limit($limit, $offset);
1207 }
Barry Mienydd671972010-10-04 16:33:58 +02001208
Andrey Andreev24276a32012-01-08 02:44:38 +02001209 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 $this->_reset_select();
1211 return $result;
1212 }
1213
1214 // --------------------------------------------------------------------
1215
1216 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001217 * Insert_Batch
1218 *
1219 * Compiles batch insert strings and runs the queries
1220 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001221 * @param string the table to retrieve the results from
1222 * @param array an associative array of insert values
1223 * @return object
1224 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001225 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001226 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001227 if ( ! is_null($set))
1228 {
1229 $this->set_insert_batch($set);
1230 }
Barry Mienydd671972010-10-04 16:33:58 +02001231
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001232 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001233 {
1234 if ($this->db_debug)
1235 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001236 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001237 return $this->display_error('db_must_use_set');
1238 }
1239 return FALSE;
1240 }
1241
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001242 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001243 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001244 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001245 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001246 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001247 }
Barry Mienydd671972010-10-04 16:33:58 +02001248
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001249 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001250 }
1251
1252 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001253 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001254 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001255 $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 -06001256 }
Barry Mienydd671972010-10-04 16:33:58 +02001257
Derek Jonesd10e8962010-03-02 17:10:36 -06001258 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001259 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001260 }
1261
1262 // --------------------------------------------------------------------
1263
1264 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001265 * Insert_batch statement
1266 *
1267 * Generates a platform-specific insert string from the supplied data.
1268 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001269 * @param string the table name
1270 * @param array the insert keys
1271 * @param array the insert values
1272 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001273 */
1274 protected function _insert_batch($table, $keys, $values)
1275 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001276 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001277 }
1278
1279 // --------------------------------------------------------------------
1280
1281 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001282 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001283 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001284 * @param mixed
1285 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001286 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001287 * @return object
1288 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001289 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001290 {
1291 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001292
Derek Jonesd10e8962010-03-02 17:10:36 -06001293 if ( ! is_array($key))
1294 {
1295 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001296 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001297
Iban Eguia3c0a4522012-04-15 13:30:44 +02001298 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001299 sort($keys);
1300
1301 foreach ($key as $row)
1302 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001303 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001304 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1305 {
1306 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001307 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001308 return;
1309 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001310
Barry Mienydd671972010-10-04 16:33:58 +02001311 ksort($row); // puts $row in the same order as our keys
1312
Andrey Andreev650b4c02012-06-11 12:07:15 +03001313 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001314 {
1315 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001316 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001317 {
Barry Mienydd671972010-10-04 16:33:58 +02001318 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001319 }
1320
Andrey Andreev650b4c02012-06-11 12:07:15 +03001321 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001322 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001323
1324 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001325 }
1326
1327 foreach ($keys as $k)
1328 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001329 $this->qb_keys[] = $this->protect_identifiers($k);
Derek Jonesd10e8962010-03-02 17:10:36 -06001330 }
Barry Mienydd671972010-10-04 16:33:58 +02001331
Derek Jonesd10e8962010-03-02 17:10:36 -06001332 return $this;
1333 }
WanWizard7219c072011-12-28 14:09:05 +01001334
Kyle Farris0c147b32011-08-26 02:29:31 -04001335 // --------------------------------------------------------------------
1336
1337 /**
1338 * Get INSERT query string
1339 *
1340 * Compiles an insert query and returns the sql
1341 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001342 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001343 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001344 * @return string
1345 */
1346 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001347 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001348 if ($this->_validate_insert($table) === FALSE)
1349 {
1350 return FALSE;
1351 }
WanWizard7219c072011-12-28 14:09:05 +01001352
Kyle Farris76116012011-08-31 11:17:48 -04001353 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001354 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001355 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001356 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001357 array_keys($this->qb_set),
1358 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001359 );
WanWizard7219c072011-12-28 14:09:05 +01001360
Kyle Farris0c147b32011-08-26 02:29:31 -04001361 if ($reset === TRUE)
1362 {
1363 $this->_reset_write();
1364 }
WanWizard7219c072011-12-28 14:09:05 +01001365
Kyle Farris0c147b32011-08-26 02:29:31 -04001366 return $sql;
1367 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001368
Derek Allard2067d1a2008-11-13 22:59:24 +00001369 // --------------------------------------------------------------------
1370
1371 /**
1372 * Insert
1373 *
1374 * Compiles an insert string and runs the query
1375 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001376 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001377 * @param array an associative array of insert values
1378 * @return object
1379 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001380 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001381 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001382 if ( ! is_null($set))
1383 {
1384 $this->set($set);
1385 }
WanWizard7219c072011-12-28 14:09:05 +01001386
Kyle Farris0c147b32011-08-26 02:29:31 -04001387 if ($this->_validate_insert($table) === FALSE)
1388 {
1389 return FALSE;
1390 }
WanWizard7219c072011-12-28 14:09:05 +01001391
Kyle Farris76116012011-08-31 11:17:48 -04001392 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001393 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001394 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001395 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001396 array_keys($this->qb_set),
1397 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001398 );
Barry Mienydd671972010-10-04 16:33:58 +02001399
Kyle Farris0c147b32011-08-26 02:29:31 -04001400 $this->_reset_write();
1401 return $this->query($sql);
1402 }
WanWizard7219c072011-12-28 14:09:05 +01001403
Kyle Farris0c147b32011-08-26 02:29:31 -04001404 // --------------------------------------------------------------------
1405
1406 /**
Andrey Andreev65d537c2012-04-05 14:11:41 +03001407 * Insert statement
1408 *
1409 * Generates a platform-specific insert string from the supplied data
1410 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001411 * @param string the table name
1412 * @param array the insert keys
1413 * @param array the insert values
1414 * @return string
Andrey Andreev65d537c2012-04-05 14:11:41 +03001415 */
1416 protected function _insert($table, $keys, $values)
1417 {
1418 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1419 }
1420
1421 // --------------------------------------------------------------------
1422
1423 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001424 * Validate Insert
1425 *
1426 * This method is used by both insert() and get_compiled_insert() to
1427 * validate that the there data is actually being set and that table
1428 * has been chosen to be inserted into.
1429 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001430 * @param string the table to insert data into
1431 * @return string
1432 */
WanWizard7219c072011-12-28 14:09:05 +01001433 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001434 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001435 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001436 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001437 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001438 }
1439
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001440 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001441 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001442 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001443 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001444 elseif ( ! isset($this->qb_from[0]))
1445 {
1446 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1447 }
WanWizard7219c072011-12-28 14:09:05 +01001448
Kyle Farris0c147b32011-08-26 02:29:31 -04001449 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001450 }
Barry Mienydd671972010-10-04 16:33:58 +02001451
Phil Sturgeon9789f322011-07-15 15:14:05 -06001452 // --------------------------------------------------------------------
1453
1454 /**
1455 * Replace
1456 *
1457 * Compiles an replace into string and runs the query
1458 *
1459 * @param string the table to replace data into
1460 * @param array an associative array of insert values
1461 * @return object
1462 */
1463 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001464 {
1465 if ( ! is_null($set))
1466 {
1467 $this->set($set);
1468 }
Barry Mienydd671972010-10-04 16:33:58 +02001469
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001470 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001471 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001472 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001473 }
1474
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001475 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001476 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001477 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001478 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001479 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001480 }
Barry Mienydd671972010-10-04 16:33:58 +02001481
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001482 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001483 }
1484
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001485 $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 +00001486
Derek Jonesd10e8962010-03-02 17:10:36 -06001487 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001488 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001489 }
WanWizard7219c072011-12-28 14:09:05 +01001490
Kyle Farris0c147b32011-08-26 02:29:31 -04001491 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001492
Kyle Farris0c147b32011-08-26 02:29:31 -04001493 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001494 * Replace statement
1495 *
1496 * Generates a platform-specific replace string from the supplied data
1497 *
1498 * @param string the table name
1499 * @param array the insert keys
1500 * @param array the insert values
1501 * @return string
1502 */
1503 protected function _replace($table, $keys, $values)
1504 {
1505 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1506 }
1507
1508 // --------------------------------------------------------------------
1509
1510 /**
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001511 * From Tables
1512 *
1513 * This public function implicitly groups FROM tables so there is no confusion
1514 * about operator precedence in harmony with SQL standards
1515 *
1516 * @param array
1517 * @return string
1518 */
1519 protected function _from_tables($tables)
1520 {
1521 is_array($tables) OR $tables = array($tables);
1522
1523 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
1524 }
1525
1526 // --------------------------------------------------------------------
1527
1528 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001529 * Get UPDATE query string
1530 *
1531 * Compiles an update query and returns the sql
1532 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001533 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001534 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001535 * @return string
1536 */
1537 public function get_compiled_update($table = '', $reset = TRUE)
1538 {
1539 // Combine any cached components with the current statements
1540 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001541
Kyle Farris0c147b32011-08-26 02:29:31 -04001542 if ($this->_validate_update($table) === FALSE)
1543 {
1544 return FALSE;
1545 }
WanWizard7219c072011-12-28 14:09:05 +01001546
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001547 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set, $this->qb_where, $this->qb_orderby, $this->qb_limit);
WanWizard7219c072011-12-28 14:09:05 +01001548
Kyle Farris0c147b32011-08-26 02:29:31 -04001549 if ($reset === TRUE)
1550 {
1551 $this->_reset_write();
1552 }
WanWizard7219c072011-12-28 14:09:05 +01001553
Kyle Farris0c147b32011-08-26 02:29:31 -04001554 return $sql;
1555 }
WanWizard7219c072011-12-28 14:09:05 +01001556
Derek Allard2067d1a2008-11-13 22:59:24 +00001557 // --------------------------------------------------------------------
1558
1559 /**
1560 * Update
1561 *
1562 * Compiles an update string and runs the query
1563 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001564 * @param string the table to retrieve the results from
1565 * @param array an associative array of update values
1566 * @param mixed the where clause
1567 * @return object
1568 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001569 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001570 {
1571 // Combine any cached components with the current statements
1572 $this->_merge_cache();
1573
1574 if ( ! is_null($set))
1575 {
1576 $this->set($set);
1577 }
Barry Mienydd671972010-10-04 16:33:58 +02001578
Kyle Farris0c147b32011-08-26 02:29:31 -04001579 if ($this->_validate_update($table) === FALSE)
1580 {
1581 return FALSE;
1582 }
1583
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001584 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001585 {
1586 $this->where($where);
1587 }
1588
Andrey Andreev650b4c02012-06-11 12:07:15 +03001589 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001590 {
1591 $this->limit($limit);
1592 }
1593
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001594 $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 +00001595
Kyle Farris0c147b32011-08-26 02:29:31 -04001596 $this->_reset_write();
1597 return $this->query($sql);
1598 }
WanWizard7219c072011-12-28 14:09:05 +01001599
Kyle Farris0c147b32011-08-26 02:29:31 -04001600 // --------------------------------------------------------------------
1601
1602 /**
Andrey Andreev975034d2012-04-05 15:09:55 +03001603 * Update statement
1604 *
1605 * Generates a platform-specific update string from the supplied data
1606 *
1607 * @param string the table name
1608 * @param array the update data
1609 * @param array the where clause
1610 * @param array the orderby clause
1611 * @param array the limit clause
Andrey Andreev00541ae2012-04-09 11:43:10 +03001612 * @param array the like clause
Andrey Andreev975034d2012-04-05 15:09:55 +03001613 * @return string
1614 */
Andrey Andreev00541ae2012-04-09 11:43:10 +03001615 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Andrey Andreev975034d2012-04-05 15:09:55 +03001616 {
1617 foreach ($values as $key => $val)
1618 {
1619 $valstr[] = $key.' = '.$val;
1620 }
1621
Andrey Andreev00541ae2012-04-09 11:43:10 +03001622 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
1623
1624 if ( ! empty($like))
1625 {
1626 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
1627 }
1628
Andrey Andreev975034d2012-04-05 15:09:55 +03001629 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev00541ae2012-04-09 11:43:10 +03001630 .$where
Andrey Andreev975034d2012-04-05 15:09:55 +03001631 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
1632 .($limit ? ' LIMIT '.$limit : '');
1633 }
1634
1635 // --------------------------------------------------------------------
1636
1637 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001638 * Validate Update
1639 *
1640 * This method is used by both update() and get_compiled_update() to
1641 * validate that data is actually being set and that a table has been
1642 * chosen to be update.
1643 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001644 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001645 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001646 */
1647 protected function _validate_update($table = '')
1648 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001649 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001650 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001651 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001652 }
1653
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001654 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001655 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001656 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001657 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001658 elseif ( ! isset($this->qb_from[0]))
1659 {
1660 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1661 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001662
1663 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001664 }
WanWizard7219c072011-12-28 14:09:05 +01001665
Derek Jonesd10e8962010-03-02 17:10:36 -06001666 // --------------------------------------------------------------------
1667
1668 /**
1669 * Update_Batch
1670 *
1671 * Compiles an update string and runs the query
1672 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001673 * @param string the table to retrieve the results from
1674 * @param array an associative array of update values
1675 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001676 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001677 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001678 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001679 {
1680 // Combine any cached components with the current statements
1681 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001682
Derek Jonesd10e8962010-03-02 17:10:36 -06001683 if (is_null($index))
1684 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001685 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001686 }
1687
1688 if ( ! is_null($set))
1689 {
1690 $this->set_update_batch($set, $index);
1691 }
1692
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001693 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001694 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001695 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001696 }
1697
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001698 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001699 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001700 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001701 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001702 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001703 }
Barry Mienydd671972010-10-04 16:33:58 +02001704
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001705 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001706 }
Barry Mienydd671972010-10-04 16:33:58 +02001707
Derek Jonesd10e8962010-03-02 17:10:36 -06001708 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001709 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001710 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001711 $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 -06001712 }
Barry Mienydd671972010-10-04 16:33:58 +02001713
Derek Jonesd10e8962010-03-02 17:10:36 -06001714 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001715 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001716 }
1717
1718 // --------------------------------------------------------------------
1719
1720 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001721 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001722 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001723 * @param array
1724 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001725 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001726 * @return object
1727 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001728 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001729 {
1730 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001731
Derek Jonesd10e8962010-03-02 17:10:36 -06001732 if ( ! is_array($key))
1733 {
1734 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001735 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001736
1737 foreach ($key as $k => $v)
1738 {
1739 $index_set = FALSE;
1740 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001741 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001742 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001743 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001744 {
1745 $index_set = TRUE;
1746 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001747
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001748 $clean[$this->protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001749 }
1750
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001751 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001752 {
1753 return $this->display_error('db_batch_missing_index');
1754 }
1755
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001756 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001757 }
Barry Mienydd671972010-10-04 16:33:58 +02001758
Derek Jonesd10e8962010-03-02 17:10:36 -06001759 return $this;
1760 }
1761
Derek Allard2067d1a2008-11-13 22:59:24 +00001762 // --------------------------------------------------------------------
1763
1764 /**
1765 * Empty Table
1766 *
1767 * Compiles a delete string and runs "DELETE FROM table"
1768 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001769 * @param string the table to empty
1770 * @return object
1771 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001772 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001773 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001774 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001775 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001776 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001777 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001778 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001779 }
1780
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001781 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001782 }
1783 else
1784 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001785 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001786 }
1787
1788 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001790 return $this->query($sql);
1791 }
1792
1793 // --------------------------------------------------------------------
1794
1795 /**
1796 * Truncate
1797 *
1798 * Compiles a truncate string and runs the query
1799 * If the database does not support the truncate() command
1800 * This function maps to "DELETE FROM table"
1801 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001802 * @param string the table to truncate
1803 * @return object
1804 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001805 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001806 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001807 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001808 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001809 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001810 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001811 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001812 }
1813
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001814 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001815 }
1816 else
1817 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001818 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001819 }
1820
1821 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001822 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001823 return $this->query($sql);
1824 }
WanWizard7219c072011-12-28 14:09:05 +01001825
Kyle Farris0c147b32011-08-26 02:29:31 -04001826 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001827
Kyle Farris0c147b32011-08-26 02:29:31 -04001828 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001829 * Truncate statement
1830 *
1831 * Generates a platform-specific truncate string from the supplied data
1832 *
1833 * If the database does not support the truncate() command,
1834 * then this method maps to 'DELETE FROM table'
1835 *
1836 * @param string the table name
1837 * @return string
1838 */
1839 protected function _truncate($table)
1840 {
1841 return 'TRUNCATE '.$table;
1842 }
1843
1844 // --------------------------------------------------------------------
1845
1846 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001847 * Get DELETE query string
1848 *
1849 * Compiles a delete query string and returns the sql
1850 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001851 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001852 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001853 * @return string
1854 */
1855 public function get_compiled_delete($table = '', $reset = TRUE)
1856 {
1857 $this->return_delete_sql = TRUE;
1858 $sql = $this->delete($table, '', NULL, $reset);
1859 $this->return_delete_sql = FALSE;
1860 return $sql;
1861 }
WanWizard7219c072011-12-28 14:09:05 +01001862
Derek Allard2067d1a2008-11-13 22:59:24 +00001863 // --------------------------------------------------------------------
1864
1865 /**
1866 * Delete
1867 *
1868 * Compiles a delete string and runs the query
1869 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001870 * @param mixed the table(s) to delete from. String or array
1871 * @param mixed the where clause
1872 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001873 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001874 * @return object
1875 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001876 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001877 {
1878 // Combine any cached components with the current statements
1879 $this->_merge_cache();
1880
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001881 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001882 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001883 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001884 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001885 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001886 }
1887
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001888 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001889 }
1890 elseif (is_array($table))
1891 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001892 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001893 {
1894 $this->delete($single_table, $where, $limit, FALSE);
1895 }
1896
1897 $this->_reset_write();
1898 return;
1899 }
1900 else
1901 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001902 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001903 }
1904
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001905 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001906 {
1907 $this->where($where);
1908 }
1909
Andrey Andreev650b4c02012-06-11 12:07:15 +03001910 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001911 {
1912 $this->limit($limit);
1913 }
1914
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001915 if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001916 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001917 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001918 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001919
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001920 $sql = $this->_delete($table, $this->qb_where, $this->qb_like, $this->qb_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001921 if ($reset_data)
1922 {
1923 $this->_reset_write();
1924 }
WanWizard7219c072011-12-28 14:09:05 +01001925
Andrey Andreev24276a32012-01-08 02:44:38 +02001926 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001927 }
WanWizard7219c072011-12-28 14:09:05 +01001928
Derek Allard2067d1a2008-11-13 22:59:24 +00001929 // --------------------------------------------------------------------
1930
1931 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001932 * Delete statement
1933 *
1934 * Generates a platform-specific delete string from the supplied data
1935 *
1936 * @param string the table name
1937 * @param array the where clause
1938 * @param array the like clause
1939 * @param string the limit clause
1940 * @return string
1941 */
1942 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
1943 {
1944 $conditions = array();
1945
1946 empty($where) OR $conditions[] = implode(' ', $where);
1947 empty($like) OR $conditions[] = implode(' ', $like);
1948
1949 return 'DELETE FROM '.$table
1950 .(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '')
Andrey Andreev650b4c02012-06-11 12:07:15 +03001951 .($limit ? ' LIMIT '.(int) $limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001952 }
1953
1954 // --------------------------------------------------------------------
1955
1956 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001957 * DB Prefix
1958 *
1959 * Prepends a database prefix if one exists in configuration
1960 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001961 * @param string the table
1962 * @return string
1963 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001964 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001965 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001966 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001967 {
1968 $this->display_error('db_table_name_required');
1969 }
1970
1971 return $this->dbprefix.$table;
1972 }
1973
1974 // --------------------------------------------------------------------
1975
1976 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001977 * Set DB Prefix
1978 *
1979 * Set's the DB Prefix to something new without needing to reconnect
1980 *
1981 * @param string the prefix
1982 * @return string
1983 */
1984 public function set_dbprefix($prefix = '')
1985 {
1986 return $this->dbprefix = $prefix;
1987 }
1988
1989 // --------------------------------------------------------------------
1990
1991 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001992 * Track Aliases
1993 *
1994 * Used to track SQL statements written with aliased tables.
1995 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001996 * @param string The table to inspect
1997 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001998 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001999 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002000 {
2001 if (is_array($table))
2002 {
2003 foreach ($table as $t)
2004 {
2005 $this->_track_aliases($t);
2006 }
2007 return;
2008 }
Barry Mienydd671972010-10-04 16:33:58 +02002009
Derek Jones37f4b9c2011-07-01 17:56:50 -05002010 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002011 // the string into discreet statements
2012 if (strpos($table, ',') !== FALSE)
2013 {
2014 return $this->_track_aliases(explode(',', $table));
2015 }
Barry Mienydd671972010-10-04 16:33:58 +02002016
Derek Allard2067d1a2008-11-13 22:59:24 +00002017 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002018 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002019 {
2020 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002021 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002022
Derek Allard2067d1a2008-11-13 22:59:24 +00002023 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002024 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002025
Derek Allard2067d1a2008-11-13 22:59:24 +00002026 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002027 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002028 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002029 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002030 }
2031 }
2032 }
WanWizard7219c072011-12-28 14:09:05 +01002033
Derek Allard2067d1a2008-11-13 22:59:24 +00002034 // --------------------------------------------------------------------
2035
2036 /**
2037 * Compile the SELECT statement
2038 *
2039 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05002040 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00002041 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002042 * @return string
2043 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002044 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002045 {
2046 // Combine any cached components with the current statements
2047 $this->_merge_cache();
2048
Derek Allard2067d1a2008-11-13 22:59:24 +00002049 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002050 if ($select_override !== FALSE)
2051 {
2052 $sql = $select_override;
2053 }
2054 else
2055 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002056 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002057
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002058 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002059 {
Barry Mienydd671972010-10-04 16:33:58 +02002060 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002061 }
2062 else
Barry Mienydd671972010-10-04 16:33:58 +02002063 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002064 // Cycle through the "select" portion of the query and prep each column name.
2065 // The reason we protect identifiers here rather then in the select() function
2066 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002067 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002068 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002069 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002070 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002071 }
Barry Mienydd671972010-10-04 16:33:58 +02002072
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002073 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002074 }
2075 }
2076
Derek Allard2067d1a2008-11-13 22:59:24 +00002077 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002078 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002079 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002080 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002081 }
2082
Derek Allard2067d1a2008-11-13 22:59:24 +00002083 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002084 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002085 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002086 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002087 }
2088
Derek Allard2067d1a2008-11-13 22:59:24 +00002089 // Write the "WHERE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002090 if (count($this->qb_where) > 0 OR count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002091 {
Greg Akere156c6e2011-04-20 16:03:04 -05002092 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00002093 }
2094
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002095 $sql .= implode("\n", $this->qb_where);
Derek Allard2067d1a2008-11-13 22:59:24 +00002096
Derek Allard2067d1a2008-11-13 22:59:24 +00002097 // Write the "LIKE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002098 if (count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002099 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002100 if (count($this->qb_where) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002101 {
2102 $sql .= "\nAND ";
2103 }
2104
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002105 $sql .= implode("\n", $this->qb_like);
Derek Allard2067d1a2008-11-13 22:59:24 +00002106 }
2107
Derek Allard2067d1a2008-11-13 22:59:24 +00002108 // Write the "GROUP BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002109 if (count($this->qb_groupby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002110 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002111 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002112 }
2113
Derek Allard2067d1a2008-11-13 22:59:24 +00002114 // Write the "HAVING" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002115 if (count($this->qb_having) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002116 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002117 $sql .= "\nHAVING ".implode("\n", $this->qb_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00002118 }
2119
Derek Allard2067d1a2008-11-13 22:59:24 +00002120 // Write the "ORDER BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002121 if (count($this->qb_orderby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002122 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002123 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002124 }
2125
Derek Allard2067d1a2008-11-13 22:59:24 +00002126 // Write the "LIMIT" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002127 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002128 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002129 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00002130 }
2131
2132 return $sql;
2133 }
2134
2135 // --------------------------------------------------------------------
2136
2137 /**
2138 * Object to Array
2139 *
2140 * Takes an object as input and converts the class variables to array key/vals
2141 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002142 * @param object
2143 * @return array
2144 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002145 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002146 {
2147 if ( ! is_object($object))
2148 {
2149 return $object;
2150 }
Barry Mienydd671972010-10-04 16:33:58 +02002151
Derek Allard2067d1a2008-11-13 22:59:24 +00002152 $array = array();
2153 foreach (get_object_vars($object) as $key => $val)
2154 {
2155 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002156 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002157 {
2158 $array[$key] = $val;
2159 }
2160 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002161
2162 return $array;
2163 }
Barry Mienydd671972010-10-04 16:33:58 +02002164
Derek Jonesd10e8962010-03-02 17:10:36 -06002165 // --------------------------------------------------------------------
2166
2167 /**
2168 * Object to Array
2169 *
2170 * Takes an object as input and converts the class variables to array key/vals
2171 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002172 * @param object
2173 * @return array
2174 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002175 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002176 {
2177 if ( ! is_object($object))
2178 {
2179 return $object;
2180 }
Barry Mienydd671972010-10-04 16:33:58 +02002181
Derek Jonesd10e8962010-03-02 17:10:36 -06002182 $array = array();
2183 $out = get_object_vars($object);
2184 $fields = array_keys($out);
2185
2186 foreach ($fields as $val)
2187 {
2188 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002189 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002190 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002191 $i = 0;
2192 foreach ($out[$val] as $data)
2193 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002194 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002195 }
2196 }
2197 }
2198
Derek Allard2067d1a2008-11-13 22:59:24 +00002199 return $array;
2200 }
Barry Mienydd671972010-10-04 16:33:58 +02002201
Derek Allard2067d1a2008-11-13 22:59:24 +00002202 // --------------------------------------------------------------------
2203
2204 /**
2205 * Start Cache
2206 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002207 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002208 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002209 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002210 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002211 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002212 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002213 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002214 }
2215
2216 // --------------------------------------------------------------------
2217
2218 /**
2219 * Stop Cache
2220 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002221 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002222 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002223 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002224 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002225 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002226 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002227 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002228 }
2229
2230 // --------------------------------------------------------------------
2231
2232 /**
2233 * Flush Cache
2234 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002235 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002236 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002237 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002238 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002239 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002240 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002241 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002242 'qb_cache_select' => array(),
2243 'qb_cache_from' => array(),
2244 'qb_cache_join' => array(),
2245 'qb_cache_where' => array(),
2246 'qb_cache_like' => array(),
2247 'qb_cache_groupby' => array(),
2248 'qb_cache_having' => array(),
2249 'qb_cache_orderby' => array(),
2250 'qb_cache_set' => array(),
2251 'qb_cache_exists' => array(),
2252 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002253 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002254 }
2255
2256 // --------------------------------------------------------------------
2257
2258 /**
2259 * Merge Cache
2260 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002261 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002262 * locally called ones.
2263 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002264 * @return void
2265 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002266 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002267 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002268 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002269 {
2270 return;
2271 }
2272
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002273 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002274 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002275 $qb_variable = 'qb_'.$val;
2276 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002277
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002278 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002279 {
2280 continue;
2281 }
2282
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002283 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002284 }
2285
2286 // If we are "protecting identifiers" we need to examine the "from"
2287 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002288 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002289 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002290 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002291 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002292
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002293 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002294 }
WanWizard7219c072011-12-28 14:09:05 +01002295
Kyle Farris0c147b32011-08-26 02:29:31 -04002296 // --------------------------------------------------------------------
2297
2298 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002299 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002300 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002301 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002302 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002303 * @return void
2304 */
2305 public function reset_query()
2306 {
2307 $this->_reset_select();
2308 $this->_reset_write();
2309 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002310
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 * @param array An array of fields to reset
2317 * @return void
2318 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002319 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002320 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002321 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002322 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002323 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002324 {
2325 $this->$item = $default_value;
2326 }
2327 }
2328 }
2329
2330 // --------------------------------------------------------------------
2331
2332 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002333 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002334 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002335 * @return void
2336 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002337 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002338 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002339 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002340 'qb_select' => array(),
2341 'qb_from' => array(),
2342 'qb_join' => array(),
2343 'qb_where' => array(),
2344 'qb_like' => array(),
2345 'qb_groupby' => array(),
2346 'qb_having' => array(),
2347 'qb_orderby' => array(),
2348 'qb_wherein' => array(),
2349 'qb_aliased_tables' => array(),
2350 'qb_no_escape' => array(),
2351 'qb_distinct' => FALSE,
2352 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002353 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002354 )
2355 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002356 }
Barry Mienydd671972010-10-04 16:33:58 +02002357
Derek Allard2067d1a2008-11-13 22:59:24 +00002358 // --------------------------------------------------------------------
2359
2360 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002361 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002362 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002363 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002364 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002365 * @return void
2366 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002367 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002368 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002369 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002370 'qb_set' => array(),
2371 'qb_from' => array(),
2372 'qb_where' => array(),
2373 'qb_like' => array(),
2374 'qb_orderby' => array(),
2375 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002376 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002377 )
2378 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002379 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002380
Derek Allard2067d1a2008-11-13 22:59:24 +00002381}
2382
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002383/* End of file DB_query_builder.php */
Andrey Andreeve4c30192012-06-04 15:08:24 +03002384/* Location: ./system/database/DB_query_builder.php */