blob: dad1df116a3fef7878c53cf5d5ff517192d260b8 [file] [log] [blame]
Andrey Andreev24276a32012-01-08 02:44:38 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
WanWizard7219c072011-12-28 14:09:05 +01008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
WanWizard7219c072011-12-28 14:09:05 +010010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
Jamie Rumbelow7efad202012-02-19 12:37:00 +000029 * Query Builder Class
Derek Allard2067d1a2008-11-13 22:59:24 +000030 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +000031 * This is the platform-independent base Query Builder implementation class.
Derek Allard2067d1a2008-11-13 22:59:24 +000032 *
33 * @package CodeIgniter
34 * @subpackage Drivers
35 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/database/
38 */
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +010039
40abstract class CI_DB_query_builder extends CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000041
WanWizard7219c072011-12-28 14:09:05 +010042 protected $return_delete_sql = FALSE;
43 protected $reset_delete_data = FALSE;
44
Jamie Rumbelow7efad202012-02-19 12:37:00 +000045 protected $qb_select = array();
46 protected $qb_distinct = FALSE;
47 protected $qb_from = array();
48 protected $qb_join = array();
49 protected $qb_where = array();
50 protected $qb_like = array();
51 protected $qb_groupby = array();
52 protected $qb_having = array();
53 protected $qb_keys = array();
54 protected $qb_limit = FALSE;
55 protected $qb_offset = FALSE;
Jamie Rumbelow7efad202012-02-19 12:37:00 +000056 protected $qb_orderby = array();
57 protected $qb_set = array();
58 protected $qb_wherein = array();
59 protected $qb_aliased_tables = array();
60 protected $qb_store_array = array();
61 protected $qb_where_group_started = FALSE;
62 protected $qb_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +020063
Jamie Rumbelow7efad202012-02-19 12:37:00 +000064 // Query Builder Caching variables
65 protected $qb_caching = FALSE;
66 protected $qb_cache_exists = array();
67 protected $qb_cache_select = array();
68 protected $qb_cache_from = array();
69 protected $qb_cache_join = array();
70 protected $qb_cache_where = array();
71 protected $qb_cache_like = array();
72 protected $qb_cache_groupby = array();
73 protected $qb_cache_having = array();
74 protected $qb_cache_orderby = array();
75 protected $qb_cache_set = array();
WanWizard7219c072011-12-28 14:09:05 +010076
Jamie Rumbelow7efad202012-02-19 12:37:00 +000077 protected $qb_no_escape = array();
78 protected $qb_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000079
80 /**
81 * Select
82 *
83 * Generates the SELECT portion of the query
84 *
Derek Allard2067d1a2008-11-13 22:59:24 +000085 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +030086 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +000087 * @return object
88 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060089 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000090 {
Derek Allard2067d1a2008-11-13 22:59:24 +000091 if (is_string($select))
92 {
93 $select = explode(',', $select);
94 }
95
Andrey Andreev42870232012-06-12 01:30:20 +030096 // If the escape value was not set will will base it on the global setting
97 is_bool($escape) OR $escape = $this->_protect_identifiers;
98
Derek Allard2067d1a2008-11-13 22:59:24 +000099 foreach ($select as $val)
100 {
101 $val = trim($val);
102
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100103 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000105 $this->qb_select[] = $val;
106 $this->qb_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000107
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000108 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000110 $this->qb_cache_select[] = $val;
111 $this->qb_cache_exists[] = 'select';
112 $this->qb_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
114 }
115 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 return $this;
118 }
119
120 // --------------------------------------------------------------------
121
122 /**
123 * Select Max
124 *
125 * Generates a SELECT MAX(field) portion of a query
126 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 * @param string the field
128 * @param string an alias
129 * @return object
130 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600131 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 {
133 return $this->_max_min_avg_sum($select, $alias, 'MAX');
134 }
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 // --------------------------------------------------------------------
137
138 /**
139 * Select Min
140 *
141 * Generates a SELECT MIN(field) portion of a query
142 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 * @param string the field
144 * @param string an alias
145 * @return object
146 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600147 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 {
149 return $this->_max_min_avg_sum($select, $alias, 'MIN');
150 }
151
152 // --------------------------------------------------------------------
153
154 /**
155 * Select Average
156 *
157 * Generates a SELECT AVG(field) portion of a query
158 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 * @param string the field
160 * @param string an alias
161 * @return object
162 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600163 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
165 return $this->_max_min_avg_sum($select, $alias, 'AVG');
166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Select Sum
172 *
173 * Generates a SELECT SUM(field) portion of a query
174 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 * @param string the field
176 * @param string an alias
177 * @return object
178 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600179 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
181 return $this->_max_min_avg_sum($select, $alias, 'SUM');
182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Processing Function for the four functions above:
188 *
189 * select_max()
190 * select_min()
191 * select_avg()
Andrey Andreev24276a32012-01-08 02:44:38 +0200192 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200193 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 * @param string the field
195 * @param string an alias
196 * @return object
197 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600198 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100200 if ( ! is_string($select) OR $select === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
202 $this->display_error('db_invalid_query');
203 }
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
208 {
209 show_error('Invalid function type: '.$type);
210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100212 if ($alias === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
214 $alias = $this->_create_alias_from_table(trim($select));
215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300217 $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->escape_identifiers(trim($alias));
218
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000219 $this->qb_select[] = $sql;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100220 $this->qb_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200221
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000222 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000224 $this->qb_cache_select[] = $sql;
225 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 return $this;
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Determines the alias name based on the table
235 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 * @param string
237 * @return string
238 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600239 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
241 if (strpos($item, '.') !== FALSE)
242 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200243 $item = explode('.', $item);
244 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 return $item;
248 }
249
250 // --------------------------------------------------------------------
251
252 /**
253 * DISTINCT
254 *
255 * Sets a flag which tells the query string compiler to add DISTINCT
256 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 * @param bool
258 * @return object
259 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600260 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300262 $this->qb_distinct = is_bool($val) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 return $this;
264 }
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 // --------------------------------------------------------------------
267
268 /**
269 * From
270 *
271 * Generates the FROM portion of the query
272 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * @param mixed can be a string or array
274 * @return object
275 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600276 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300278 foreach ((array) $from as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 if (strpos($val, ',') !== FALSE)
281 {
282 foreach (explode(',', $val) as $v)
283 {
284 $v = trim($v);
285 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200286
George Petsagourakis193d4482012-04-28 11:16:18 +0300287 $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000288
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000289 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000291 $this->qb_cache_from[] = $v;
292 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200293 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
296 else
297 {
298 $val = trim($val);
299
Andrey Andreev24276a32012-01-08 02:44:38 +0200300 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000301 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200303
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000304 $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000305
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000306 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000308 $this->qb_cache_from[] = $val;
309 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 }
311 }
312 }
313
314 return $this;
315 }
316
317 // --------------------------------------------------------------------
318
319 /**
320 * Join
321 *
322 * Generates the JOIN portion of the query
323 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * @param string
325 * @param string the join condition
326 * @param string the type of join
Alex Bilbief512b732012-06-16 11:15:19 +0100327 * @param string whether not to try to escape identifiers
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 * @return object
329 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300330 public function join($table, $cond, $type = '', $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200331 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100332 if ($type !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 $type = strtoupper(trim($type));
335
Andrey Andreev42870232012-06-12 01:30:20 +0300336 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 $type = '';
339 }
340 else
341 {
342 $type .= ' ';
343 }
344 }
345
Andrey Andreev24276a32012-01-08 02:44:38 +0200346 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000347 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 $this->_track_aliases($table);
349
Andrey Andreevfe642da2012-06-16 03:47:33 +0300350 is_bool($escape) OR $escape = $this->_protect_identifiers;
351
Andrey Andreev42870232012-06-12 01:30:20 +0300352 // Split multiple conditions
353 if ($escape === TRUE && preg_match_all('/\sAND\s|\sOR\s/i', $cond, $m, PREG_SET_ORDER | PREG_OFFSET_CAPTURE))
354 {
355 $newcond = '';
356 $m[0][] = array('', strlen($cond));
357
358 for ($i = 0, $c = count($m[0]), $s = 0;
359 $i < $c;
360 $s += $m[0][$i][1] + strlen($m[0][$i][0]), $i++)
361 {
362 $temp = substr($cond, $s, $m[0][$i][1]);
363
364 $newcond .= preg_match('/([\[\w\.-]+)([\W\s]+)(.+)/i', $temp, $match)
365 ? $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3])
366 : $temp;
367
368 $newcond .= $m[0][$i][0];
369 }
370
Andrey Andreev3751f932012-06-17 18:07:48 +0300371 $cond = ' ON '.$newcond;
Andrey Andreev42870232012-06-12 01:30:20 +0300372 }
373 // Split apart the condition and protect the identifiers
374 elseif ($escape === TRUE && preg_match('/([\[\w\.-]+)([\W\s]+)(.+)/i', $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
Andrey Andreev3751f932012-06-17 18:07:48 +0300376 $cond = ' ON '.$this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
377 }
378 elseif ( ! $this->_has_operator($cond))
379 {
380 $cond = ' USING ('.($escape ? $this->escape_identifiers($cond) : $cond).')';
381 }
382 else
383 {
384 $cond = ' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Andrey Andreev42870232012-06-12 01:30:20 +0300387 // Do we want to escape the table name?
388 if ($escape === TRUE)
389 {
390 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
391 }
392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 // Assemble the JOIN statement
Andrey Andreev3751f932012-06-17 18:07:48 +0300394 $this->qb_join[] = $join = $type.'JOIN '.$table.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000395
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000396 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000398 $this->qb_cache_join[] = $join;
399 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 }
401
402 return $this;
403 }
404
405 // --------------------------------------------------------------------
406
407 /**
408 * Where
409 *
410 * Generates the WHERE portion of the query. Separates
411 * multiple calls with AND
412 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 * @param mixed
414 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300415 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 * @return object
417 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300418 public function where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
420 return $this->_where($key, $value, 'AND ', $escape);
421 }
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 // --------------------------------------------------------------------
424
425 /**
426 * OR Where
427 *
428 * Generates the WHERE portion of the query. Separates
429 * multiple calls with OR
430 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 * @param mixed
432 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300433 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 * @return object
435 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300436 public function or_where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
438 return $this->_where($key, $value, 'OR ', $escape);
439 }
440
441 // --------------------------------------------------------------------
442
443 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 * Where
445 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600446 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 * @param mixed
449 * @param mixed
450 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300451 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 * @return object
453 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600454 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 {
456 if ( ! is_array($key))
457 {
458 $key = array($key => $value);
459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 // If the escape value was not set will will base it on the global setting
Andrey Andreeve10fb792012-06-15 12:07:04 +0300462 is_bool($escape) OR $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000463
464 foreach ($key as $k => $v)
465 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300466 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
467 ? $this->_group_get_type('')
468 : $this->_group_get_type($type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000469
Andrey Andreev929fd2d2012-06-17 17:29:57 +0300470 $k = (($op = $this->_get_operator($k)) !== FALSE)
471 ? $this->protect_identifiers(substr($k, 0, strpos($k, $op)), FALSE, $escape).strstr($k, $op)
Andrey Andreev9c14f652012-06-10 14:35:07 +0300472 : $this->protect_identifiers($k, FALSE, $escape);
473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 if (is_null($v) && ! $this->_has_operator($k))
475 {
476 // value appears not to have been set, assign the test to IS NULL
477 $k .= ' IS NULL';
478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 if ( ! is_null($v))
481 {
482 if ($escape === TRUE)
483 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 $v = ' '.$this->escape($v);
485 }
WanWizard7219c072011-12-28 14:09:05 +0100486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 if ( ! $this->_has_operator($k))
488 {
Greg Akere156c6e2011-04-20 16:03:04 -0500489 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 }
491 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000492
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000493 $this->qb_where[] = $prefix.$k.$v;
494 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000496 $this->qb_cache_where[] = $prefix.$k.$v;
497 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 }
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
Barry Mienydd671972010-10-04 16:33:58 +0200501
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 return $this;
503 }
504
505 // --------------------------------------------------------------------
506
507 /**
508 * Where_in
509 *
510 * Generates a WHERE field IN ('item', 'item') SQL query joined with
511 * AND if appropriate
512 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 * @param string The field to search
514 * @param array The values searched on
515 * @return object
516 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300517 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300519 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 }
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 // --------------------------------------------------------------------
523
524 /**
525 * Where_in_or
526 *
527 * Generates a WHERE field IN ('item', 'item') SQL query joined with
528 * OR if appropriate
529 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 * @param string The field to search
531 * @param array The values searched on
532 * @return object
533 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300534 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300536 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 }
538
539 // --------------------------------------------------------------------
540
541 /**
542 * Where_not_in
543 *
544 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
545 * with AND if appropriate
546 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 * @param string The field to search
548 * @param array The values searched on
549 * @return object
550 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300551 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300553 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 }
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 // --------------------------------------------------------------------
557
558 /**
559 * Where_not_in_or
560 *
561 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
562 * with OR if appropriate
563 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 * @param string The field to search
565 * @param array The values searched on
566 * @return object
567 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300568 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300570 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 }
572
573 // --------------------------------------------------------------------
574
575 /**
576 * Where_in
577 *
578 * Called by where_in, where_in_or, where_not_in, where_not_in_or
579 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 * @param string The field to search
581 * @param array The values searched on
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300582 * @param bool If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200583 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 * @return object
585 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300586 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
588 if ($key === NULL OR $values === NULL)
589 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300590 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 }
Barry Mienydd671972010-10-04 16:33:58 +0200592
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 if ( ! is_array($values))
594 {
595 $values = array($values);
596 }
Barry Mienydd671972010-10-04 16:33:58 +0200597
Andrey Andreev498c1e02012-06-16 03:34:10 +0300598 is_bool($escape) OR $escape = $this->_protect_identifiers;
599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 $not = ($not) ? ' NOT' : '';
601
602 foreach ($values as $value)
603 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000604 $this->qb_wherein[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 }
606
WanWizardbc69f362012-06-22 00:10:11 +0200607 $prefix = (count($this->qb_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Andrey Andreev498c1e02012-06-16 03:34:10 +0300608 $this->qb_where[] = $where_in = $prefix.$this->protect_identifiers($key, FALSE, $escape).$not.' IN ('.implode(', ', $this->qb_wherein).') ';
Barry Mienydd671972010-10-04 16:33:58 +0200609
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000610 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000612 $this->qb_cache_where[] = $where_in;
613 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 }
615
616 // reset the array for multiple calls
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000617 $this->qb_wherein = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 return $this;
619 }
Barry Mienydd671972010-10-04 16:33:58 +0200620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 // --------------------------------------------------------------------
622
623 /**
624 * Like
625 *
626 * Generates a %LIKE% portion of the query. Separates
627 * multiple calls with AND
628 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 * @param mixed
630 * @param mixed
631 * @return object
632 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600633 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
635 return $this->_like($field, $match, 'AND ', $side);
636 }
637
638 // --------------------------------------------------------------------
639
640 /**
641 * Not Like
642 *
643 * Generates a NOT LIKE portion of the query. Separates
644 * multiple calls with AND
645 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 * @param mixed
647 * @param mixed
648 * @return object
649 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600650 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 {
652 return $this->_like($field, $match, 'AND ', $side, 'NOT');
653 }
Barry Mienydd671972010-10-04 16:33:58 +0200654
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 // --------------------------------------------------------------------
656
657 /**
658 * OR Like
659 *
660 * Generates a %LIKE% portion of the query. Separates
661 * multiple calls with OR
662 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 * @param mixed
664 * @param mixed
665 * @return object
666 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600667 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 {
669 return $this->_like($field, $match, 'OR ', $side);
670 }
671
672 // --------------------------------------------------------------------
673
674 /**
675 * OR Not Like
676 *
677 * Generates a NOT LIKE portion of the query. Separates
678 * multiple calls with OR
679 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 * @param mixed
681 * @param mixed
682 * @return object
683 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600684 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 {
686 return $this->_like($field, $match, 'OR ', $side, 'NOT');
687 }
Barry Mienydd671972010-10-04 16:33:58 +0200688
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 // --------------------------------------------------------------------
690
691 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 * Like
693 *
694 * Called by like() or orlike()
695 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 * @param mixed
697 * @param mixed
698 * @param string
699 * @return object
700 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600701 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 {
703 if ( ! is_array($field))
704 {
705 $field = array($field => $match);
706 }
Barry Mienydd671972010-10-04 16:33:58 +0200707
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 foreach ($field as $k => $v)
709 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000710 $k = $this->protect_identifiers($k);
WanWizardbc69f362012-06-22 00:10:11 +0200711 $prefix = (count($this->qb_like) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Derek Jonese4ed5832009-02-20 21:44:59 +0000712 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300713
Andrey Andreev24276a32012-01-08 02:44:38 +0200714 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400715 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100716 $like_statement = "{$prefix} $k $not LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400717 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200718 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100720 $like_statement = "{$prefix} $k $not LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200722 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100724 $like_statement = "{$prefix} $k $not LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 }
726 else
727 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100728 $like_statement = "{$prefix} $k $not LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600730
Derek Jonese4ed5832009-02-20 21:44:59 +0000731 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100732 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000733 {
Greg Aker0d424892010-01-26 02:14:44 +0000734 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000735 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600736
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000737 $this->qb_like[] = $like_statement;
738 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000740 $this->qb_cache_like[] = $like_statement;
741 $this->qb_cache_exists[] = 'like';
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 }
Barry Mienydd671972010-10-04 16:33:58 +0200743
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200745
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 return $this;
747 }
Barry Mienydd671972010-10-04 16:33:58 +0200748
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 // --------------------------------------------------------------------
750
751 /**
WanWizard7219c072011-12-28 14:09:05 +0100752 * Starts a query group.
753 *
754 * @param string (Internal use only)
755 * @param string (Internal use only)
756 * @return object
757 */
758 public function group_start($not = '', $type = 'AND ')
759 {
760 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100761
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000762 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100763 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000764 $this->qb_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (';
WanWizard7219c072011-12-28 14:09:05 +0100765
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000766 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100767 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000768 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100769 }
770
771 return $this;
772 }
773
774 // --------------------------------------------------------------------
775
776 /**
777 * Starts a query group, but ORs the group
778 *
779 * @return object
780 */
781 public function or_group_start()
782 {
783 return $this->group_start('', 'OR ');
784 }
785
786 // --------------------------------------------------------------------
787
788 /**
789 * Starts a query group, but NOTs the group
790 *
791 * @return object
792 */
793 public function not_group_start()
794 {
795 return $this->group_start('NOT ', 'AND ');
796 }
797
798 // --------------------------------------------------------------------
799
800 /**
801 * Starts a query group, but OR NOTs the group
802 *
803 * @return object
804 */
805 public function or_not_group_start()
806 {
807 return $this->group_start('NOT ', 'OR ');
808 }
809
810 // --------------------------------------------------------------------
811
812 /**
813 * Ends a query group
814 *
815 * @return object
816 */
817 public function group_end()
818 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000819 $this->qb_where_group_started = FALSE;
820 $this->qb_where[] = $value = str_repeat(' ', $this->qb_where_group_count--) . ')';
WanWizard7219c072011-12-28 14:09:05 +0100821
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000822 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100823 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000824 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100825 }
826
WanWizard7219c072011-12-28 14:09:05 +0100827 return $this;
828 }
829
830 // --------------------------------------------------------------------
831
832 /**
833 * Group_get_type
834 *
835 * Called by group_start(), _like(), _where() and _where_in()
836 *
837 * @param string
838 * @return string
839 */
840 protected function _group_get_type($type)
841 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000842 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100843 {
844 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000845 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100846 }
847
848 return $type;
849 }
850
851 // --------------------------------------------------------------------
852
853 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 * GROUP BY
855 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 * @param string
857 * @return object
858 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600859 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 {
861 if (is_string($by))
862 {
863 $by = explode(',', $by);
864 }
Barry Mienydd671972010-10-04 16:33:58 +0200865
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 foreach ($by as $val)
867 {
868 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200869
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100870 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000872 $this->qb_groupby[] = $val = $this->protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200873
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000874 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000876 $this->qb_cache_groupby[] = $val;
877 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 }
879 }
880 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200881
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 return $this;
883 }
884
885 // --------------------------------------------------------------------
886
887 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 * Sets the HAVING value
889 *
890 * Separates multiple calls with AND
891 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 * @param string
893 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300894 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 * @return object
896 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300897 public function having($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 {
899 return $this->_having($key, $value, 'AND ', $escape);
900 }
Barry Mienydd671972010-10-04 16:33:58 +0200901
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 // --------------------------------------------------------------------
903
904 /**
905 * Sets the OR HAVING value
906 *
907 * Separates multiple calls with OR
908 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 * @param string
910 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300911 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 * @return object
913 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300914 public function or_having($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 {
916 return $this->_having($key, $value, 'OR ', $escape);
917 }
Barry Mienydd671972010-10-04 16:33:58 +0200918
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 // --------------------------------------------------------------------
920
921 /**
922 * Sets the HAVING values
923 *
924 * Called by having() or or_having()
925 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 * @param string
927 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300928 * @param string
929 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 * @return object
931 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300932 protected function _having($key, $value = '', $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 {
934 if ( ! is_array($key))
935 {
936 $key = array($key => $value);
937 }
Barry Mienydd671972010-10-04 16:33:58 +0200938
Andrey Andreevfe642da2012-06-16 03:47:33 +0300939 is_bool($escape) OR $escape = $this->_protect_identifiers;
940
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 foreach ($key as $k => $v)
942 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000943 $prefix = (count($this->qb_having) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000944
Andrey Andreev974c75b2012-06-15 12:30:02 +0300945 $k = $this->_has_operator($k)
946 ? $this->protect_identifiers(substr($k, 0, strpos(rtrim($k), ' ')), FALSE, $escape).strchr(rtrim($k), ' ')
947 : $this->protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000948
949 if ( ! $this->_has_operator($k))
950 {
951 $k .= ' = ';
952 }
953
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100954 if ($v !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400956 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 }
Barry Mienydd671972010-10-04 16:33:58 +0200958
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000959 $this->qb_having[] = $prefix.$k.$v;
960 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000962 $this->qb_cache_having[] = $prefix.$k.$v;
963 $this->qb_cache_exists[] = 'having';
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 }
965 }
Barry Mienydd671972010-10-04 16:33:58 +0200966
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 return $this;
968 }
Barry Mienydd671972010-10-04 16:33:58 +0200969
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 // --------------------------------------------------------------------
971
972 /**
973 * Sets the ORDER BY value
974 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 * @param string
976 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100977 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 * @return object
979 */
Andrey Andreevd24160c2012-06-16 03:21:20 +0300980 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200982 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 {
984 $orderby = ''; // Random results want or don't need a field name
985 $direction = $this->_random_keyword;
986 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100987 elseif (trim($direction) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 {
Andrey Andreev650b4c02012-06-11 12:07:15 +0300989 $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : ' ASC';
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 }
Barry Mienydd671972010-10-04 16:33:58 +0200991
Andrey Andreevd24160c2012-06-16 03:21:20 +0300992 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +0200993
Andrey Andreevd24160c2012-06-16 03:21:20 +0300994 if ($escape === TRUE && strpos($orderby, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 {
996 $temp = array();
997 foreach (explode(',', $orderby) as $part)
998 {
999 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001000 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 {
Andrey Andreev88cb2782012-06-11 20:40:50 +03001002 $part = preg_match('/^(.+)\s+(ASC|DESC)$/i', $part, $matches)
1003 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
1004 : $this->protect_identifiers($part);
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 }
Barry Mienydd671972010-10-04 16:33:58 +02001006
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 $temp[] = $part;
1008 }
Barry Mienydd671972010-10-04 16:33:58 +02001009
1010 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001012 elseif ($direction !== $this->_random_keyword && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 {
Andrey Andreev1d1d7ff2012-06-11 22:35:35 +03001014 $orderby = preg_match('/^(.+)\s+(ASC|DESC)$/i', $orderby, $matches)
Andrey Andreev88cb2782012-06-11 20:40:50 +03001015 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
1016 : $this->protect_identifiers($orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 }
Barry Mienydd671972010-10-04 16:33:58 +02001018
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001019 $this->qb_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +02001020
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001021 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001023 $this->qb_cache_orderby[] = $orderby_statement;
1024 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 }
1026
1027 return $this;
1028 }
Barry Mienydd671972010-10-04 16:33:58 +02001029
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 // --------------------------------------------------------------------
1031
1032 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 * Sets the LIMIT value
1034 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001035 * @param int the limit value
1036 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 * @return object
1038 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +02001039 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001041 is_null($value) OR $this->qb_limit = (int) $value;
1042 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001043
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 return $this;
1045 }
Barry Mienydd671972010-10-04 16:33:58 +02001046
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 // --------------------------------------------------------------------
1048
1049 /**
1050 * Sets the OFFSET value
1051 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001052 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 * @return object
1054 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001055 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001057 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 return $this;
1059 }
Barry Mienydd671972010-10-04 16:33:58 +02001060
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 // --------------------------------------------------------------------
1062
1063 /**
Andrey Andreev2c35b642012-06-24 03:05:26 +03001064 * Limit string
1065 *
1066 * Generates a platform-specific LIMIT clause
1067 *
1068 * @param string the sql query string
1069 * @param int the number of rows to limit the query to
1070 * @param int the offset value
1071 * @return string
1072 */
1073 protected function _limit($sql, $limit, $offset)
1074 {
1075 return $sql.' LIMIT '.($offset ? $offset.', ' : '').$limit;
1076 }
1077
1078 // --------------------------------------------------------------------
1079
1080 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001081 * The "set" function.
1082 *
1083 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 * @param mixed
1086 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001087 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 * @return object
1089 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001090 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 {
1092 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001093
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 if ( ! is_array($key))
1095 {
1096 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001097 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001098
Andrey Andreevfe642da2012-06-16 03:47:33 +03001099 is_bool($escape) OR $escape = $this->_protect_identifiers;
1100
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 foreach ($key as $k => $v)
1102 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001103 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1104 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 }
Barry Mienydd671972010-10-04 16:33:58 +02001106
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 return $this;
1108 }
WanWizard7219c072011-12-28 14:09:05 +01001109
Kyle Farris0c147b32011-08-26 02:29:31 -04001110 // --------------------------------------------------------------------
1111
1112 /**
1113 * Get SELECT query string
1114 *
1115 * Compiles a SELECT query string and returns the sql.
1116 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001117 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001118 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001119 * @return string
1120 */
WanWizard7219c072011-12-28 14:09:05 +01001121 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001122 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001123 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001124 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001125 $this->_track_aliases($table);
1126 $this->from($table);
1127 }
WanWizard7219c072011-12-28 14:09:05 +01001128
Andrey Andreev650b4c02012-06-11 12:07:15 +03001129 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001130
Kyle Farris0c147b32011-08-26 02:29:31 -04001131 if ($reset === TRUE)
1132 {
1133 $this->_reset_select();
1134 }
WanWizard7219c072011-12-28 14:09:05 +01001135
Kyle Farris0c147b32011-08-26 02:29:31 -04001136 return $select;
1137 }
WanWizard7219c072011-12-28 14:09:05 +01001138
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 // --------------------------------------------------------------------
1140
1141 /**
1142 * Get
1143 *
1144 * Compiles the select statement based on the other functions called
1145 * and runs the query
1146 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 * @param string the table
1148 * @param string the limit clause
1149 * @param string the offset clause
1150 * @return object
1151 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001152 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001154 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 {
1156 $this->_track_aliases($table);
1157 $this->from($table);
1158 }
Barry Mienydd671972010-10-04 16:33:58 +02001159
Andrey Andreev650b4c02012-06-11 12:07:15 +03001160 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 {
1162 $this->limit($limit, $offset);
1163 }
Barry Mienydd671972010-10-04 16:33:58 +02001164
Andrey Andreev24276a32012-01-08 02:44:38 +02001165 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 $this->_reset_select();
1167 return $result;
1168 }
1169
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001170 // --------------------------------------------------------------------
1171
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 /**
1173 * "Count All Results" query
1174 *
Barry Mienydd671972010-10-04 16:33:58 +02001175 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001176 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 * @param string
1179 * @return string
1180 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001181 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001183 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 {
1185 $this->_track_aliases($table);
1186 $this->from($table);
1187 }
Barry Mienydd671972010-10-04 16:33:58 +02001188
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001189 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001191
Purwandi1d160e72012-01-09 16:33:28 +07001192 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001194 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 }
1196
Purwandi1d160e72012-01-09 16:33:28 +07001197 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001198 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001200
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 // --------------------------------------------------------------------
1202
1203 /**
1204 * Get_Where
1205 *
1206 * Allows the where clause, limit and offset to be added directly
1207 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 * @param string the where clause
1209 * @param string the limit clause
1210 * @param string the offset clause
1211 * @return object
1212 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001213 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001215 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 {
1217 $this->from($table);
1218 }
1219
1220 if ( ! is_null($where))
1221 {
1222 $this->where($where);
1223 }
Barry Mienydd671972010-10-04 16:33:58 +02001224
Andrey Andreev650b4c02012-06-11 12:07:15 +03001225 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 {
1227 $this->limit($limit, $offset);
1228 }
Barry Mienydd671972010-10-04 16:33:58 +02001229
Andrey Andreev24276a32012-01-08 02:44:38 +02001230 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 $this->_reset_select();
1232 return $result;
1233 }
1234
1235 // --------------------------------------------------------------------
1236
1237 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001238 * Insert_Batch
1239 *
1240 * Compiles batch insert strings and runs the queries
1241 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001242 * @param string the table to retrieve the results from
1243 * @param array an associative array of insert values
1244 * @return object
1245 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001246 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001247 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001248 if ( ! is_null($set))
1249 {
1250 $this->set_insert_batch($set);
1251 }
Barry Mienydd671972010-10-04 16:33:58 +02001252
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001253 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001254 {
1255 if ($this->db_debug)
1256 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001257 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001258 return $this->display_error('db_must_use_set');
1259 }
1260 return FALSE;
1261 }
1262
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001263 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001264 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001265 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001266 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001267 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001268 }
Barry Mienydd671972010-10-04 16:33:58 +02001269
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001270 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001271 }
1272
1273 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001274 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001275 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001276 $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 -06001277 }
Barry Mienydd671972010-10-04 16:33:58 +02001278
Derek Jonesd10e8962010-03-02 17:10:36 -06001279 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001280 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001281 }
1282
1283 // --------------------------------------------------------------------
1284
1285 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001286 * Insert_batch statement
1287 *
1288 * Generates a platform-specific insert string from the supplied data.
1289 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001290 * @param string the table name
1291 * @param array the insert keys
1292 * @param array the insert values
1293 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001294 */
1295 protected function _insert_batch($table, $keys, $values)
1296 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001297 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001298 }
1299
1300 // --------------------------------------------------------------------
1301
1302 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001303 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001304 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001305 * @param mixed
1306 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001307 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001308 * @return object
1309 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001310 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001311 {
1312 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001313
Derek Jonesd10e8962010-03-02 17:10:36 -06001314 if ( ! is_array($key))
1315 {
1316 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001317 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001318
Andrey Andreevfe642da2012-06-16 03:47:33 +03001319 is_bool($escape) OR $escape = $this->_protect_identifiers;
1320
Iban Eguia3c0a4522012-04-15 13:30:44 +02001321 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001322 sort($keys);
1323
1324 foreach ($key as $row)
1325 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001326 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001327 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1328 {
1329 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001330 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001331 return;
1332 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001333
Barry Mienydd671972010-10-04 16:33:58 +02001334 ksort($row); // puts $row in the same order as our keys
1335
Andrey Andreev650b4c02012-06-11 12:07:15 +03001336 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001337 {
1338 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001339 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001340 {
Barry Mienydd671972010-10-04 16:33:58 +02001341 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001342 }
1343
Andrey Andreev650b4c02012-06-11 12:07:15 +03001344 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001345 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001346
1347 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001348 }
1349
1350 foreach ($keys as $k)
1351 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001352 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001353 }
Barry Mienydd671972010-10-04 16:33:58 +02001354
Derek Jonesd10e8962010-03-02 17:10:36 -06001355 return $this;
1356 }
WanWizard7219c072011-12-28 14:09:05 +01001357
Kyle Farris0c147b32011-08-26 02:29:31 -04001358 // --------------------------------------------------------------------
1359
1360 /**
1361 * Get INSERT query string
1362 *
1363 * Compiles an insert query and returns the sql
1364 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001365 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001366 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001367 * @return string
1368 */
1369 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001370 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001371 if ($this->_validate_insert($table) === FALSE)
1372 {
1373 return FALSE;
1374 }
WanWizard7219c072011-12-28 14:09:05 +01001375
Kyle Farris76116012011-08-31 11:17:48 -04001376 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001377 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001378 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001379 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001380 array_keys($this->qb_set),
1381 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001382 );
WanWizard7219c072011-12-28 14:09:05 +01001383
Kyle Farris0c147b32011-08-26 02:29:31 -04001384 if ($reset === TRUE)
1385 {
1386 $this->_reset_write();
1387 }
WanWizard7219c072011-12-28 14:09:05 +01001388
Kyle Farris0c147b32011-08-26 02:29:31 -04001389 return $sql;
1390 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001391
Derek Allard2067d1a2008-11-13 22:59:24 +00001392 // --------------------------------------------------------------------
1393
1394 /**
1395 * Insert
1396 *
1397 * Compiles an insert string and runs the query
1398 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001399 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001400 * @param array an associative array of insert values
1401 * @return object
1402 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001403 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001404 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001405 if ( ! is_null($set))
1406 {
1407 $this->set($set);
1408 }
WanWizard7219c072011-12-28 14:09:05 +01001409
Kyle Farris0c147b32011-08-26 02:29:31 -04001410 if ($this->_validate_insert($table) === FALSE)
1411 {
1412 return FALSE;
1413 }
WanWizard7219c072011-12-28 14:09:05 +01001414
Kyle Farris76116012011-08-31 11:17:48 -04001415 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001416 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001417 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001418 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001419 array_keys($this->qb_set),
1420 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001421 );
Barry Mienydd671972010-10-04 16:33:58 +02001422
Kyle Farris0c147b32011-08-26 02:29:31 -04001423 $this->_reset_write();
1424 return $this->query($sql);
1425 }
WanWizard7219c072011-12-28 14:09:05 +01001426
Kyle Farris0c147b32011-08-26 02:29:31 -04001427 // --------------------------------------------------------------------
1428
1429 /**
Andrey Andreev65d537c2012-04-05 14:11:41 +03001430 * Insert statement
1431 *
1432 * Generates a platform-specific insert string from the supplied data
1433 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001434 * @param string the table name
1435 * @param array the insert keys
1436 * @param array the insert values
1437 * @return string
Andrey Andreev65d537c2012-04-05 14:11:41 +03001438 */
1439 protected function _insert($table, $keys, $values)
1440 {
1441 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1442 }
1443
1444 // --------------------------------------------------------------------
1445
1446 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001447 * Validate Insert
1448 *
1449 * This method is used by both insert() and get_compiled_insert() to
1450 * validate that the there data is actually being set and that table
1451 * has been chosen to be inserted into.
1452 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001453 * @param string the table to insert data into
1454 * @return string
1455 */
WanWizard7219c072011-12-28 14:09:05 +01001456 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001457 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001458 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001460 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001461 }
1462
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001463 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001464 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001465 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001466 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001467 elseif ( ! isset($this->qb_from[0]))
1468 {
1469 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1470 }
WanWizard7219c072011-12-28 14:09:05 +01001471
Kyle Farris0c147b32011-08-26 02:29:31 -04001472 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001473 }
Barry Mienydd671972010-10-04 16:33:58 +02001474
Phil Sturgeon9789f322011-07-15 15:14:05 -06001475 // --------------------------------------------------------------------
1476
1477 /**
1478 * Replace
1479 *
1480 * Compiles an replace into string and runs the query
1481 *
1482 * @param string the table to replace data into
1483 * @param array an associative array of insert values
1484 * @return object
1485 */
1486 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001487 {
1488 if ( ! is_null($set))
1489 {
1490 $this->set($set);
1491 }
Barry Mienydd671972010-10-04 16:33:58 +02001492
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001493 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001494 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001495 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001496 }
1497
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001498 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001499 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001500 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001501 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001502 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001503 }
Barry Mienydd671972010-10-04 16:33:58 +02001504
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001505 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001506 }
1507
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001508 $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 +00001509
Derek Jonesd10e8962010-03-02 17:10:36 -06001510 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001511 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001512 }
WanWizard7219c072011-12-28 14:09:05 +01001513
Kyle Farris0c147b32011-08-26 02:29:31 -04001514 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001515
Kyle Farris0c147b32011-08-26 02:29:31 -04001516 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001517 * Replace statement
1518 *
1519 * Generates a platform-specific replace string from the supplied data
1520 *
1521 * @param string the table name
1522 * @param array the insert keys
1523 * @param array the insert values
1524 * @return string
1525 */
1526 protected function _replace($table, $keys, $values)
1527 {
1528 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1529 }
1530
1531 // --------------------------------------------------------------------
1532
1533 /**
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001534 * From Tables
1535 *
1536 * This public function implicitly groups FROM tables so there is no confusion
1537 * about operator precedence in harmony with SQL standards
1538 *
1539 * @param array
1540 * @return string
1541 */
1542 protected function _from_tables($tables)
1543 {
1544 is_array($tables) OR $tables = array($tables);
1545
1546 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
1547 }
1548
1549 // --------------------------------------------------------------------
1550
1551 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001552 * Get UPDATE query string
1553 *
1554 * Compiles an update query and returns the sql
1555 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001556 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001557 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001558 * @return string
1559 */
1560 public function get_compiled_update($table = '', $reset = TRUE)
1561 {
1562 // Combine any cached components with the current statements
1563 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001564
Kyle Farris0c147b32011-08-26 02:29:31 -04001565 if ($this->_validate_update($table) === FALSE)
1566 {
1567 return FALSE;
1568 }
WanWizard7219c072011-12-28 14:09:05 +01001569
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001570 $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 +01001571
Kyle Farris0c147b32011-08-26 02:29:31 -04001572 if ($reset === TRUE)
1573 {
1574 $this->_reset_write();
1575 }
WanWizard7219c072011-12-28 14:09:05 +01001576
Kyle Farris0c147b32011-08-26 02:29:31 -04001577 return $sql;
1578 }
WanWizard7219c072011-12-28 14:09:05 +01001579
Derek Allard2067d1a2008-11-13 22:59:24 +00001580 // --------------------------------------------------------------------
1581
1582 /**
1583 * Update
1584 *
1585 * Compiles an update string and runs the query
1586 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001587 * @param string the table to retrieve the results from
1588 * @param array an associative array of update values
1589 * @param mixed the where clause
1590 * @return object
1591 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001592 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001593 {
1594 // Combine any cached components with the current statements
1595 $this->_merge_cache();
1596
1597 if ( ! is_null($set))
1598 {
1599 $this->set($set);
1600 }
Barry Mienydd671972010-10-04 16:33:58 +02001601
Kyle Farris0c147b32011-08-26 02:29:31 -04001602 if ($this->_validate_update($table) === FALSE)
1603 {
1604 return FALSE;
1605 }
1606
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001607 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001608 {
1609 $this->where($where);
1610 }
1611
Andrey Andreev650b4c02012-06-11 12:07:15 +03001612 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001613 {
1614 $this->limit($limit);
1615 }
1616
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001617 $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 +00001618
Kyle Farris0c147b32011-08-26 02:29:31 -04001619 $this->_reset_write();
1620 return $this->query($sql);
1621 }
WanWizard7219c072011-12-28 14:09:05 +01001622
Kyle Farris0c147b32011-08-26 02:29:31 -04001623 // --------------------------------------------------------------------
1624
1625 /**
Andrey Andreev975034d2012-04-05 15:09:55 +03001626 * Update statement
1627 *
1628 * Generates a platform-specific update string from the supplied data
1629 *
1630 * @param string the table name
1631 * @param array the update data
1632 * @param array the where clause
1633 * @param array the orderby clause
1634 * @param array the limit clause
Andrey Andreev00541ae2012-04-09 11:43:10 +03001635 * @param array the like clause
Andrey Andreev975034d2012-04-05 15:09:55 +03001636 * @return string
1637 */
Andrey Andreev00541ae2012-04-09 11:43:10 +03001638 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Andrey Andreev975034d2012-04-05 15:09:55 +03001639 {
1640 foreach ($values as $key => $val)
1641 {
1642 $valstr[] = $key.' = '.$val;
1643 }
1644
Andrey Andreev00541ae2012-04-09 11:43:10 +03001645 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
1646
1647 if ( ! empty($like))
1648 {
1649 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
1650 }
1651
Andrey Andreev975034d2012-04-05 15:09:55 +03001652 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev00541ae2012-04-09 11:43:10 +03001653 .$where
Andrey Andreev975034d2012-04-05 15:09:55 +03001654 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
1655 .($limit ? ' LIMIT '.$limit : '');
1656 }
1657
1658 // --------------------------------------------------------------------
1659
1660 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001661 * Validate Update
1662 *
1663 * This method is used by both update() and get_compiled_update() to
1664 * validate that data is actually being set and that a table has been
1665 * chosen to be update.
1666 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001667 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001668 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001669 */
1670 protected function _validate_update($table = '')
1671 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001672 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001673 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001674 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001675 }
1676
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001677 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001678 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001679 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001680 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001681 elseif ( ! isset($this->qb_from[0]))
1682 {
1683 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1684 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001685
1686 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001687 }
WanWizard7219c072011-12-28 14:09:05 +01001688
Derek Jonesd10e8962010-03-02 17:10:36 -06001689 // --------------------------------------------------------------------
1690
1691 /**
1692 * Update_Batch
1693 *
1694 * Compiles an update string and runs the query
1695 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001696 * @param string the table to retrieve the results from
1697 * @param array an associative array of update values
1698 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001699 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001700 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001701 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001702 {
1703 // Combine any cached components with the current statements
1704 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001705
Derek Jonesd10e8962010-03-02 17:10:36 -06001706 if (is_null($index))
1707 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001708 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001709 }
1710
1711 if ( ! is_null($set))
1712 {
1713 $this->set_update_batch($set, $index);
1714 }
1715
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001716 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001717 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001718 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001719 }
1720
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001721 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001722 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001723 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001724 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001725 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001726 }
Barry Mienydd671972010-10-04 16:33:58 +02001727
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001728 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001729 }
Barry Mienydd671972010-10-04 16:33:58 +02001730
Derek Jonesd10e8962010-03-02 17:10:36 -06001731 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001732 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001733 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001734 $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 -06001735 }
Barry Mienydd671972010-10-04 16:33:58 +02001736
Derek Jonesd10e8962010-03-02 17:10:36 -06001737 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001738 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001739 }
1740
1741 // --------------------------------------------------------------------
1742
1743 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001744 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001745 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001746 * @param array
1747 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001748 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001749 * @return object
1750 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001751 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001752 {
1753 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001754
Derek Jonesd10e8962010-03-02 17:10:36 -06001755 if ( ! is_array($key))
1756 {
1757 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001758 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001759
Andrey Andreevfe642da2012-06-16 03:47:33 +03001760 is_bool($escape) OR $escape = $this->_protect_identifiers;
1761
Derek Jonesd10e8962010-03-02 17:10:36 -06001762 foreach ($key as $k => $v)
1763 {
1764 $index_set = FALSE;
1765 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001766 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001767 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001768 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001769 {
1770 $index_set = TRUE;
1771 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001772
Andrey Andreevfe642da2012-06-16 03:47:33 +03001773 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001774 }
1775
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001776 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001777 {
1778 return $this->display_error('db_batch_missing_index');
1779 }
1780
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001781 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001782 }
Barry Mienydd671972010-10-04 16:33:58 +02001783
Derek Jonesd10e8962010-03-02 17:10:36 -06001784 return $this;
1785 }
1786
Derek Allard2067d1a2008-11-13 22:59:24 +00001787 // --------------------------------------------------------------------
1788
1789 /**
1790 * Empty Table
1791 *
1792 * Compiles a delete string and runs "DELETE FROM table"
1793 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001794 * @param string the table to empty
1795 * @return object
1796 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001797 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001798 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001799 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001800 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001801 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001802 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001803 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001804 }
1805
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001806 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001807 }
1808 else
1809 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001810 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001811 }
1812
1813 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001815 return $this->query($sql);
1816 }
1817
1818 // --------------------------------------------------------------------
1819
1820 /**
1821 * Truncate
1822 *
1823 * Compiles a truncate string and runs the query
1824 * If the database does not support the truncate() command
1825 * This function maps to "DELETE FROM table"
1826 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 * @param string the table to truncate
1828 * @return object
1829 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001830 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001831 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001832 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001833 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001834 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001835 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001836 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001837 }
1838
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001839 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001840 }
1841 else
1842 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001843 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001844 }
1845
1846 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001847 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001848 return $this->query($sql);
1849 }
WanWizard7219c072011-12-28 14:09:05 +01001850
Kyle Farris0c147b32011-08-26 02:29:31 -04001851 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001852
Kyle Farris0c147b32011-08-26 02:29:31 -04001853 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001854 * Truncate statement
1855 *
1856 * Generates a platform-specific truncate string from the supplied data
1857 *
1858 * If the database does not support the truncate() command,
1859 * then this method maps to 'DELETE FROM table'
1860 *
1861 * @param string the table name
1862 * @return string
1863 */
1864 protected function _truncate($table)
1865 {
1866 return 'TRUNCATE '.$table;
1867 }
1868
1869 // --------------------------------------------------------------------
1870
1871 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001872 * Get DELETE query string
1873 *
1874 * Compiles a delete query string and returns the sql
1875 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001876 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001877 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001878 * @return string
1879 */
1880 public function get_compiled_delete($table = '', $reset = TRUE)
1881 {
1882 $this->return_delete_sql = TRUE;
1883 $sql = $this->delete($table, '', NULL, $reset);
1884 $this->return_delete_sql = FALSE;
1885 return $sql;
1886 }
WanWizard7219c072011-12-28 14:09:05 +01001887
Derek Allard2067d1a2008-11-13 22:59:24 +00001888 // --------------------------------------------------------------------
1889
1890 /**
1891 * Delete
1892 *
1893 * Compiles a delete string and runs the query
1894 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001895 * @param mixed the table(s) to delete from. String or array
1896 * @param mixed the where clause
1897 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001898 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001899 * @return object
1900 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001901 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001902 {
1903 // Combine any cached components with the current statements
1904 $this->_merge_cache();
1905
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001906 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001907 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001908 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001909 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001910 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001911 }
1912
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001913 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001914 }
1915 elseif (is_array($table))
1916 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001917 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001918 {
1919 $this->delete($single_table, $where, $limit, FALSE);
1920 }
1921
1922 $this->_reset_write();
1923 return;
1924 }
1925 else
1926 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001927 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001928 }
1929
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001930 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001931 {
1932 $this->where($where);
1933 }
1934
Andrey Andreev650b4c02012-06-11 12:07:15 +03001935 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001936 {
1937 $this->limit($limit);
1938 }
1939
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001940 if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001941 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001942 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001943 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001944
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001945 $sql = $this->_delete($table, $this->qb_where, $this->qb_like, $this->qb_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001946 if ($reset_data)
1947 {
1948 $this->_reset_write();
1949 }
WanWizard7219c072011-12-28 14:09:05 +01001950
Andrey Andreev24276a32012-01-08 02:44:38 +02001951 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001952 }
WanWizard7219c072011-12-28 14:09:05 +01001953
Derek Allard2067d1a2008-11-13 22:59:24 +00001954 // --------------------------------------------------------------------
1955
1956 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001957 * Delete statement
1958 *
1959 * Generates a platform-specific delete string from the supplied data
1960 *
1961 * @param string the table name
1962 * @param array the where clause
1963 * @param array the like clause
1964 * @param string the limit clause
1965 * @return string
1966 */
1967 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
1968 {
1969 $conditions = array();
1970
1971 empty($where) OR $conditions[] = implode(' ', $where);
1972 empty($like) OR $conditions[] = implode(' ', $like);
1973
1974 return 'DELETE FROM '.$table
1975 .(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '')
Andrey Andreev650b4c02012-06-11 12:07:15 +03001976 .($limit ? ' LIMIT '.(int) $limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001977 }
1978
1979 // --------------------------------------------------------------------
1980
1981 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001982 * DB Prefix
1983 *
1984 * Prepends a database prefix if one exists in configuration
1985 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001986 * @param string the table
1987 * @return string
1988 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001989 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001990 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001991 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001992 {
1993 $this->display_error('db_table_name_required');
1994 }
1995
1996 return $this->dbprefix.$table;
1997 }
1998
1999 // --------------------------------------------------------------------
2000
2001 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06002002 * Set DB Prefix
2003 *
2004 * Set's the DB Prefix to something new without needing to reconnect
2005 *
2006 * @param string the prefix
2007 * @return string
2008 */
2009 public function set_dbprefix($prefix = '')
2010 {
2011 return $this->dbprefix = $prefix;
2012 }
2013
2014 // --------------------------------------------------------------------
2015
2016 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002017 * Track Aliases
2018 *
2019 * Used to track SQL statements written with aliased tables.
2020 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002021 * @param string The table to inspect
2022 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002023 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002024 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002025 {
2026 if (is_array($table))
2027 {
2028 foreach ($table as $t)
2029 {
2030 $this->_track_aliases($t);
2031 }
2032 return;
2033 }
Barry Mienydd671972010-10-04 16:33:58 +02002034
Derek Jones37f4b9c2011-07-01 17:56:50 -05002035 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002036 // the string into discreet statements
2037 if (strpos($table, ',') !== FALSE)
2038 {
2039 return $this->_track_aliases(explode(',', $table));
2040 }
Barry Mienydd671972010-10-04 16:33:58 +02002041
Derek Allard2067d1a2008-11-13 22:59:24 +00002042 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002043 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002044 {
2045 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002046 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002047
Derek Allard2067d1a2008-11-13 22:59:24 +00002048 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002049 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002050
Derek Allard2067d1a2008-11-13 22:59:24 +00002051 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002052 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002054 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002055 }
2056 }
2057 }
WanWizard7219c072011-12-28 14:09:05 +01002058
Derek Allard2067d1a2008-11-13 22:59:24 +00002059 // --------------------------------------------------------------------
2060
2061 /**
2062 * Compile the SELECT statement
2063 *
2064 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05002065 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00002066 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002067 * @return string
2068 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002069 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002070 {
2071 // Combine any cached components with the current statements
2072 $this->_merge_cache();
2073
Derek Allard2067d1a2008-11-13 22:59:24 +00002074 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002075 if ($select_override !== FALSE)
2076 {
2077 $sql = $select_override;
2078 }
2079 else
2080 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002081 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002082
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002083 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002084 {
Barry Mienydd671972010-10-04 16:33:58 +02002085 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002086 }
2087 else
Barry Mienydd671972010-10-04 16:33:58 +02002088 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002089 // Cycle through the "select" portion of the query and prep each column name.
2090 // The reason we protect identifiers here rather then in the select() function
2091 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002092 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002093 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002094 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002095 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002096 }
Barry Mienydd671972010-10-04 16:33:58 +02002097
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002098 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002099 }
2100 }
2101
Derek Allard2067d1a2008-11-13 22:59:24 +00002102 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002103 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002104 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002105 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002106 }
2107
Derek Allard2067d1a2008-11-13 22:59:24 +00002108 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002109 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002110 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002111 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002112 }
2113
Derek Allard2067d1a2008-11-13 22:59:24 +00002114 // Write the "WHERE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002115 if (count($this->qb_where) > 0 OR count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002116 {
Greg Akere156c6e2011-04-20 16:03:04 -05002117 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00002118 }
2119
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002120 $sql .= implode("\n", $this->qb_where);
Derek Allard2067d1a2008-11-13 22:59:24 +00002121
Derek Allard2067d1a2008-11-13 22:59:24 +00002122 // Write the "LIKE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002123 if (count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002124 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002125 if (count($this->qb_where) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002126 {
2127 $sql .= "\nAND ";
2128 }
2129
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002130 $sql .= implode("\n", $this->qb_like);
Derek Allard2067d1a2008-11-13 22:59:24 +00002131 }
2132
Derek Allard2067d1a2008-11-13 22:59:24 +00002133 // Write the "GROUP BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002134 if (count($this->qb_groupby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002135 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002136 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002137 }
2138
Derek Allard2067d1a2008-11-13 22:59:24 +00002139 // Write the "HAVING" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002140 if (count($this->qb_having) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002141 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002142 $sql .= "\nHAVING ".implode("\n", $this->qb_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00002143 }
2144
Derek Allard2067d1a2008-11-13 22:59:24 +00002145 // Write the "ORDER BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002146 if (count($this->qb_orderby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002147 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002148 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002149 }
2150
Derek Allard2067d1a2008-11-13 22:59:24 +00002151 // Write the "LIMIT" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002152 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002153 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002154 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00002155 }
2156
2157 return $sql;
2158 }
2159
2160 // --------------------------------------------------------------------
2161
2162 /**
2163 * Object to Array
2164 *
2165 * Takes an object as input and converts the class variables to array key/vals
2166 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002167 * @param object
2168 * @return array
2169 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002170 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002171 {
2172 if ( ! is_object($object))
2173 {
2174 return $object;
2175 }
Barry Mienydd671972010-10-04 16:33:58 +02002176
Derek Allard2067d1a2008-11-13 22:59:24 +00002177 $array = array();
2178 foreach (get_object_vars($object) as $key => $val)
2179 {
2180 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002181 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002182 {
2183 $array[$key] = $val;
2184 }
2185 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002186
2187 return $array;
2188 }
Barry Mienydd671972010-10-04 16:33:58 +02002189
Derek Jonesd10e8962010-03-02 17:10:36 -06002190 // --------------------------------------------------------------------
2191
2192 /**
2193 * Object to Array
2194 *
2195 * Takes an object as input and converts the class variables to array key/vals
2196 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002197 * @param object
2198 * @return array
2199 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002200 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002201 {
2202 if ( ! is_object($object))
2203 {
2204 return $object;
2205 }
Barry Mienydd671972010-10-04 16:33:58 +02002206
Derek Jonesd10e8962010-03-02 17:10:36 -06002207 $array = array();
2208 $out = get_object_vars($object);
2209 $fields = array_keys($out);
2210
2211 foreach ($fields as $val)
2212 {
2213 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002214 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002215 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002216 $i = 0;
2217 foreach ($out[$val] as $data)
2218 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002219 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002220 }
2221 }
2222 }
2223
Derek Allard2067d1a2008-11-13 22:59:24 +00002224 return $array;
2225 }
Barry Mienydd671972010-10-04 16:33:58 +02002226
Derek Allard2067d1a2008-11-13 22:59:24 +00002227 // --------------------------------------------------------------------
2228
2229 /**
2230 * Start Cache
2231 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002232 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002233 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002234 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002235 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002236 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002237 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002238 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002239 }
2240
2241 // --------------------------------------------------------------------
2242
2243 /**
2244 * Stop Cache
2245 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002246 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002247 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002248 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002249 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002250 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002251 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002252 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002253 }
2254
2255 // --------------------------------------------------------------------
2256
2257 /**
2258 * Flush Cache
2259 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002260 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002261 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002262 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002263 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002264 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002265 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002266 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002267 'qb_cache_select' => array(),
2268 'qb_cache_from' => array(),
2269 'qb_cache_join' => array(),
2270 'qb_cache_where' => array(),
2271 'qb_cache_like' => array(),
2272 'qb_cache_groupby' => array(),
2273 'qb_cache_having' => array(),
2274 'qb_cache_orderby' => array(),
2275 'qb_cache_set' => array(),
2276 'qb_cache_exists' => array(),
2277 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002278 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002279 }
2280
2281 // --------------------------------------------------------------------
2282
2283 /**
2284 * Merge Cache
2285 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002286 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002287 * locally called ones.
2288 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002289 * @return void
2290 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002291 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002292 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002293 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002294 {
2295 return;
2296 }
2297
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002298 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002299 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002300 $qb_variable = 'qb_'.$val;
2301 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002302
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002303 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002304 {
2305 continue;
2306 }
2307
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002308 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002309 }
2310
2311 // If we are "protecting identifiers" we need to examine the "from"
2312 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002313 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002314 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002315 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002316 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002317
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002318 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002319 }
WanWizard7219c072011-12-28 14:09:05 +01002320
Kyle Farris0c147b32011-08-26 02:29:31 -04002321 // --------------------------------------------------------------------
2322
2323 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002324 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002325 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002326 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002327 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002328 * @return void
2329 */
2330 public function reset_query()
2331 {
2332 $this->_reset_select();
2333 $this->_reset_write();
2334 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002335
2336 // --------------------------------------------------------------------
2337
2338 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002339 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002340 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002341 * @param array An array of fields to reset
2342 * @return void
2343 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002344 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002345 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002346 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002347 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002348 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002349 {
2350 $this->$item = $default_value;
2351 }
2352 }
2353 }
2354
2355 // --------------------------------------------------------------------
2356
2357 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002358 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002359 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002360 * @return void
2361 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002362 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002363 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002364 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002365 'qb_select' => array(),
2366 'qb_from' => array(),
2367 'qb_join' => array(),
2368 'qb_where' => array(),
2369 'qb_like' => array(),
2370 'qb_groupby' => array(),
2371 'qb_having' => array(),
2372 'qb_orderby' => array(),
2373 'qb_wherein' => array(),
2374 'qb_aliased_tables' => array(),
2375 'qb_no_escape' => array(),
2376 'qb_distinct' => FALSE,
2377 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002378 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002379 )
2380 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002381 }
Barry Mienydd671972010-10-04 16:33:58 +02002382
Derek Allard2067d1a2008-11-13 22:59:24 +00002383 // --------------------------------------------------------------------
2384
2385 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002386 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002387 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002388 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002389 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002390 * @return void
2391 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002392 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002393 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002394 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002395 'qb_set' => array(),
2396 'qb_from' => array(),
2397 'qb_where' => array(),
2398 'qb_like' => array(),
2399 'qb_orderby' => array(),
2400 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002401 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002402 )
2403 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002404 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002405
Derek Allard2067d1a2008-11-13 22:59:24 +00002406}
2407
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002408/* End of file DB_query_builder.php */
Andrey Andreev58803fb2012-06-24 00:45:37 +03002409/* Location: ./system/database/DB_query_builder.php */