blob: 4631b1b92cdfff64b115b29d1b27eb6eb435621a [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 Andreevfe642da2012-06-16 03:47:33 +03001064 * The "set" function.
1065 *
1066 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 * @param mixed
1069 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001070 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 * @return object
1072 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001073 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 {
1075 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001076
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 if ( ! is_array($key))
1078 {
1079 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001080 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001081
Andrey Andreevfe642da2012-06-16 03:47:33 +03001082 is_bool($escape) OR $escape = $this->_protect_identifiers;
1083
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 foreach ($key as $k => $v)
1085 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001086 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1087 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 }
Barry Mienydd671972010-10-04 16:33:58 +02001089
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 return $this;
1091 }
WanWizard7219c072011-12-28 14:09:05 +01001092
Kyle Farris0c147b32011-08-26 02:29:31 -04001093 // --------------------------------------------------------------------
1094
1095 /**
1096 * Get SELECT query string
1097 *
1098 * Compiles a SELECT query string and returns the sql.
1099 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001100 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001101 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001102 * @return string
1103 */
WanWizard7219c072011-12-28 14:09:05 +01001104 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001105 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001106 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001107 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001108 $this->_track_aliases($table);
1109 $this->from($table);
1110 }
WanWizard7219c072011-12-28 14:09:05 +01001111
Andrey Andreev650b4c02012-06-11 12:07:15 +03001112 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001113
Kyle Farris0c147b32011-08-26 02:29:31 -04001114 if ($reset === TRUE)
1115 {
1116 $this->_reset_select();
1117 }
WanWizard7219c072011-12-28 14:09:05 +01001118
Kyle Farris0c147b32011-08-26 02:29:31 -04001119 return $select;
1120 }
WanWizard7219c072011-12-28 14:09:05 +01001121
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 // --------------------------------------------------------------------
1123
1124 /**
1125 * Get
1126 *
1127 * Compiles the select statement based on the other functions called
1128 * and runs the query
1129 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 * @param string the table
1131 * @param string the limit clause
1132 * @param string the offset clause
1133 * @return object
1134 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001135 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001137 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 {
1139 $this->_track_aliases($table);
1140 $this->from($table);
1141 }
Barry Mienydd671972010-10-04 16:33:58 +02001142
Andrey Andreev650b4c02012-06-11 12:07:15 +03001143 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 {
1145 $this->limit($limit, $offset);
1146 }
Barry Mienydd671972010-10-04 16:33:58 +02001147
Andrey Andreev24276a32012-01-08 02:44:38 +02001148 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 $this->_reset_select();
1150 return $result;
1151 }
1152
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001153 // --------------------------------------------------------------------
1154
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 /**
1156 * "Count All Results" query
1157 *
Barry Mienydd671972010-10-04 16:33:58 +02001158 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001159 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 * @param string
1162 * @return string
1163 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001164 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001166 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 {
1168 $this->_track_aliases($table);
1169 $this->from($table);
1170 }
Barry Mienydd671972010-10-04 16:33:58 +02001171
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001172 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001174
Purwandi1d160e72012-01-09 16:33:28 +07001175 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001177 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 }
1179
Purwandi1d160e72012-01-09 16:33:28 +07001180 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001181 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001183
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 // --------------------------------------------------------------------
1185
1186 /**
1187 * Get_Where
1188 *
1189 * Allows the where clause, limit and offset to be added directly
1190 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 * @param string the where clause
1192 * @param string the limit clause
1193 * @param string the offset clause
1194 * @return object
1195 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001196 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001198 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 {
1200 $this->from($table);
1201 }
1202
1203 if ( ! is_null($where))
1204 {
1205 $this->where($where);
1206 }
Barry Mienydd671972010-10-04 16:33:58 +02001207
Andrey Andreev650b4c02012-06-11 12:07:15 +03001208 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 {
1210 $this->limit($limit, $offset);
1211 }
Barry Mienydd671972010-10-04 16:33:58 +02001212
Andrey Andreev24276a32012-01-08 02:44:38 +02001213 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 $this->_reset_select();
1215 return $result;
1216 }
1217
1218 // --------------------------------------------------------------------
1219
1220 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001221 * Insert_Batch
1222 *
1223 * Compiles batch insert strings and runs the queries
1224 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001225 * @param string the table to retrieve the results from
1226 * @param array an associative array of insert values
1227 * @return object
1228 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001229 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001230 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001231 if ( ! is_null($set))
1232 {
1233 $this->set_insert_batch($set);
1234 }
Barry Mienydd671972010-10-04 16:33:58 +02001235
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001236 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001237 {
1238 if ($this->db_debug)
1239 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001240 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001241 return $this->display_error('db_must_use_set');
1242 }
1243 return FALSE;
1244 }
1245
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001246 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001247 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001248 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001249 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001250 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001251 }
Barry Mienydd671972010-10-04 16:33:58 +02001252
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001253 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001254 }
1255
1256 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001257 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001258 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001259 $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 -06001260 }
Barry Mienydd671972010-10-04 16:33:58 +02001261
Derek Jonesd10e8962010-03-02 17:10:36 -06001262 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001263 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001264 }
1265
1266 // --------------------------------------------------------------------
1267
1268 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001269 * Insert_batch statement
1270 *
1271 * Generates a platform-specific insert string from the supplied data.
1272 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001273 * @param string the table name
1274 * @param array the insert keys
1275 * @param array the insert values
1276 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001277 */
1278 protected function _insert_batch($table, $keys, $values)
1279 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001280 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001281 }
1282
1283 // --------------------------------------------------------------------
1284
1285 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001286 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001287 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001288 * @param mixed
1289 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001290 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001291 * @return object
1292 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001293 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001294 {
1295 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001296
Derek Jonesd10e8962010-03-02 17:10:36 -06001297 if ( ! is_array($key))
1298 {
1299 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001300 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001301
Andrey Andreevfe642da2012-06-16 03:47:33 +03001302 is_bool($escape) OR $escape = $this->_protect_identifiers;
1303
Iban Eguia3c0a4522012-04-15 13:30:44 +02001304 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001305 sort($keys);
1306
1307 foreach ($key as $row)
1308 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001309 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001310 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1311 {
1312 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001313 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001314 return;
1315 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001316
Barry Mienydd671972010-10-04 16:33:58 +02001317 ksort($row); // puts $row in the same order as our keys
1318
Andrey Andreev650b4c02012-06-11 12:07:15 +03001319 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001320 {
1321 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001322 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001323 {
Barry Mienydd671972010-10-04 16:33:58 +02001324 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001325 }
1326
Andrey Andreev650b4c02012-06-11 12:07:15 +03001327 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001328 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001329
1330 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001331 }
1332
1333 foreach ($keys as $k)
1334 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001335 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001336 }
Barry Mienydd671972010-10-04 16:33:58 +02001337
Derek Jonesd10e8962010-03-02 17:10:36 -06001338 return $this;
1339 }
WanWizard7219c072011-12-28 14:09:05 +01001340
Kyle Farris0c147b32011-08-26 02:29:31 -04001341 // --------------------------------------------------------------------
1342
1343 /**
1344 * Get INSERT query string
1345 *
1346 * Compiles an insert query and returns the sql
1347 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001348 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001349 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001350 * @return string
1351 */
1352 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001353 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001354 if ($this->_validate_insert($table) === FALSE)
1355 {
1356 return FALSE;
1357 }
WanWizard7219c072011-12-28 14:09:05 +01001358
Kyle Farris76116012011-08-31 11:17:48 -04001359 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001360 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001361 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001362 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001363 array_keys($this->qb_set),
1364 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001365 );
WanWizard7219c072011-12-28 14:09:05 +01001366
Kyle Farris0c147b32011-08-26 02:29:31 -04001367 if ($reset === TRUE)
1368 {
1369 $this->_reset_write();
1370 }
WanWizard7219c072011-12-28 14:09:05 +01001371
Kyle Farris0c147b32011-08-26 02:29:31 -04001372 return $sql;
1373 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001374
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 // --------------------------------------------------------------------
1376
1377 /**
1378 * Insert
1379 *
1380 * Compiles an insert string and runs the query
1381 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001382 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001383 * @param array an associative array of insert values
1384 * @return object
1385 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001386 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001387 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001388 if ( ! is_null($set))
1389 {
1390 $this->set($set);
1391 }
WanWizard7219c072011-12-28 14:09:05 +01001392
Kyle Farris0c147b32011-08-26 02:29:31 -04001393 if ($this->_validate_insert($table) === FALSE)
1394 {
1395 return FALSE;
1396 }
WanWizard7219c072011-12-28 14:09:05 +01001397
Kyle Farris76116012011-08-31 11:17:48 -04001398 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001399 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001400 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001401 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001402 array_keys($this->qb_set),
1403 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001404 );
Barry Mienydd671972010-10-04 16:33:58 +02001405
Kyle Farris0c147b32011-08-26 02:29:31 -04001406 $this->_reset_write();
1407 return $this->query($sql);
1408 }
WanWizard7219c072011-12-28 14:09:05 +01001409
Kyle Farris0c147b32011-08-26 02:29:31 -04001410 // --------------------------------------------------------------------
1411
1412 /**
Andrey Andreev65d537c2012-04-05 14:11:41 +03001413 * Insert statement
1414 *
1415 * Generates a platform-specific insert string from the supplied data
1416 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001417 * @param string the table name
1418 * @param array the insert keys
1419 * @param array the insert values
1420 * @return string
Andrey Andreev65d537c2012-04-05 14:11:41 +03001421 */
1422 protected function _insert($table, $keys, $values)
1423 {
1424 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1425 }
1426
1427 // --------------------------------------------------------------------
1428
1429 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001430 * Validate Insert
1431 *
1432 * This method is used by both insert() and get_compiled_insert() to
1433 * validate that the there data is actually being set and that table
1434 * has been chosen to be inserted into.
1435 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001436 * @param string the table to insert data into
1437 * @return string
1438 */
WanWizard7219c072011-12-28 14:09:05 +01001439 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001440 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001441 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001442 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001443 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001444 }
1445
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001446 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001447 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001448 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001449 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001450 elseif ( ! isset($this->qb_from[0]))
1451 {
1452 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1453 }
WanWizard7219c072011-12-28 14:09:05 +01001454
Kyle Farris0c147b32011-08-26 02:29:31 -04001455 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001456 }
Barry Mienydd671972010-10-04 16:33:58 +02001457
Phil Sturgeon9789f322011-07-15 15:14:05 -06001458 // --------------------------------------------------------------------
1459
1460 /**
1461 * Replace
1462 *
1463 * Compiles an replace into string and runs the query
1464 *
1465 * @param string the table to replace data into
1466 * @param array an associative array of insert values
1467 * @return object
1468 */
1469 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001470 {
1471 if ( ! is_null($set))
1472 {
1473 $this->set($set);
1474 }
Barry Mienydd671972010-10-04 16:33:58 +02001475
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001476 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001477 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001478 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001479 }
1480
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001481 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001482 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001483 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001484 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001485 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001486 }
Barry Mienydd671972010-10-04 16:33:58 +02001487
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001488 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001489 }
1490
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001491 $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 +00001492
Derek Jonesd10e8962010-03-02 17:10:36 -06001493 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001494 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001495 }
WanWizard7219c072011-12-28 14:09:05 +01001496
Kyle Farris0c147b32011-08-26 02:29:31 -04001497 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001498
Kyle Farris0c147b32011-08-26 02:29:31 -04001499 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001500 * Replace statement
1501 *
1502 * Generates a platform-specific replace string from the supplied data
1503 *
1504 * @param string the table name
1505 * @param array the insert keys
1506 * @param array the insert values
1507 * @return string
1508 */
1509 protected function _replace($table, $keys, $values)
1510 {
1511 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1512 }
1513
1514 // --------------------------------------------------------------------
1515
1516 /**
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001517 * From Tables
1518 *
1519 * This public function implicitly groups FROM tables so there is no confusion
1520 * about operator precedence in harmony with SQL standards
1521 *
1522 * @param array
1523 * @return string
1524 */
1525 protected function _from_tables($tables)
1526 {
1527 is_array($tables) OR $tables = array($tables);
1528
1529 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
1530 }
1531
1532 // --------------------------------------------------------------------
1533
1534 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001535 * Get UPDATE query string
1536 *
1537 * Compiles an update query and returns the sql
1538 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001539 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001540 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001541 * @return string
1542 */
1543 public function get_compiled_update($table = '', $reset = TRUE)
1544 {
1545 // Combine any cached components with the current statements
1546 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001547
Kyle Farris0c147b32011-08-26 02:29:31 -04001548 if ($this->_validate_update($table) === FALSE)
1549 {
1550 return FALSE;
1551 }
WanWizard7219c072011-12-28 14:09:05 +01001552
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001553 $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 +01001554
Kyle Farris0c147b32011-08-26 02:29:31 -04001555 if ($reset === TRUE)
1556 {
1557 $this->_reset_write();
1558 }
WanWizard7219c072011-12-28 14:09:05 +01001559
Kyle Farris0c147b32011-08-26 02:29:31 -04001560 return $sql;
1561 }
WanWizard7219c072011-12-28 14:09:05 +01001562
Derek Allard2067d1a2008-11-13 22:59:24 +00001563 // --------------------------------------------------------------------
1564
1565 /**
1566 * Update
1567 *
1568 * Compiles an update string and runs the query
1569 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001570 * @param string the table to retrieve the results from
1571 * @param array an associative array of update values
1572 * @param mixed the where clause
1573 * @return object
1574 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001575 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001576 {
1577 // Combine any cached components with the current statements
1578 $this->_merge_cache();
1579
1580 if ( ! is_null($set))
1581 {
1582 $this->set($set);
1583 }
Barry Mienydd671972010-10-04 16:33:58 +02001584
Kyle Farris0c147b32011-08-26 02:29:31 -04001585 if ($this->_validate_update($table) === FALSE)
1586 {
1587 return FALSE;
1588 }
1589
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001590 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001591 {
1592 $this->where($where);
1593 }
1594
Andrey Andreev650b4c02012-06-11 12:07:15 +03001595 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001596 {
1597 $this->limit($limit);
1598 }
1599
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001600 $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 +00001601
Kyle Farris0c147b32011-08-26 02:29:31 -04001602 $this->_reset_write();
1603 return $this->query($sql);
1604 }
WanWizard7219c072011-12-28 14:09:05 +01001605
Kyle Farris0c147b32011-08-26 02:29:31 -04001606 // --------------------------------------------------------------------
1607
1608 /**
Andrey Andreev975034d2012-04-05 15:09:55 +03001609 * Update statement
1610 *
1611 * Generates a platform-specific update string from the supplied data
1612 *
1613 * @param string the table name
1614 * @param array the update data
1615 * @param array the where clause
1616 * @param array the orderby clause
1617 * @param array the limit clause
Andrey Andreev00541ae2012-04-09 11:43:10 +03001618 * @param array the like clause
Andrey Andreev975034d2012-04-05 15:09:55 +03001619 * @return string
1620 */
Andrey Andreev00541ae2012-04-09 11:43:10 +03001621 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Andrey Andreev975034d2012-04-05 15:09:55 +03001622 {
1623 foreach ($values as $key => $val)
1624 {
1625 $valstr[] = $key.' = '.$val;
1626 }
1627
Andrey Andreev00541ae2012-04-09 11:43:10 +03001628 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
1629
1630 if ( ! empty($like))
1631 {
1632 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
1633 }
1634
Andrey Andreev975034d2012-04-05 15:09:55 +03001635 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev00541ae2012-04-09 11:43:10 +03001636 .$where
Andrey Andreev975034d2012-04-05 15:09:55 +03001637 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
1638 .($limit ? ' LIMIT '.$limit : '');
1639 }
1640
1641 // --------------------------------------------------------------------
1642
1643 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001644 * Validate Update
1645 *
1646 * This method is used by both update() and get_compiled_update() to
1647 * validate that data is actually being set and that a table has been
1648 * chosen to be update.
1649 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001650 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001651 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001652 */
1653 protected function _validate_update($table = '')
1654 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001655 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001656 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001657 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001658 }
1659
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001660 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001661 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001662 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001663 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001664 elseif ( ! isset($this->qb_from[0]))
1665 {
1666 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1667 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001668
1669 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001670 }
WanWizard7219c072011-12-28 14:09:05 +01001671
Derek Jonesd10e8962010-03-02 17:10:36 -06001672 // --------------------------------------------------------------------
1673
1674 /**
1675 * Update_Batch
1676 *
1677 * Compiles an update string and runs the query
1678 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001679 * @param string the table to retrieve the results from
1680 * @param array an associative array of update values
1681 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001682 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001683 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001684 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001685 {
1686 // Combine any cached components with the current statements
1687 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001688
Derek Jonesd10e8962010-03-02 17:10:36 -06001689 if (is_null($index))
1690 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001691 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001692 }
1693
1694 if ( ! is_null($set))
1695 {
1696 $this->set_update_batch($set, $index);
1697 }
1698
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001699 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001700 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001701 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001702 }
1703
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001704 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001705 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001706 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001707 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001708 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001709 }
Barry Mienydd671972010-10-04 16:33:58 +02001710
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001711 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001712 }
Barry Mienydd671972010-10-04 16:33:58 +02001713
Derek Jonesd10e8962010-03-02 17:10:36 -06001714 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001715 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001716 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001717 $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 -06001718 }
Barry Mienydd671972010-10-04 16:33:58 +02001719
Derek Jonesd10e8962010-03-02 17:10:36 -06001720 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001721 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001722 }
1723
1724 // --------------------------------------------------------------------
1725
1726 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001727 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001728 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001729 * @param array
1730 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001731 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001732 * @return object
1733 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001734 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001735 {
1736 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001737
Derek Jonesd10e8962010-03-02 17:10:36 -06001738 if ( ! is_array($key))
1739 {
1740 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001741 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001742
Andrey Andreevfe642da2012-06-16 03:47:33 +03001743 is_bool($escape) OR $escape = $this->_protect_identifiers;
1744
Derek Jonesd10e8962010-03-02 17:10:36 -06001745 foreach ($key as $k => $v)
1746 {
1747 $index_set = FALSE;
1748 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001749 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001750 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001751 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001752 {
1753 $index_set = TRUE;
1754 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001755
Andrey Andreevfe642da2012-06-16 03:47:33 +03001756 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001757 }
1758
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001759 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001760 {
1761 return $this->display_error('db_batch_missing_index');
1762 }
1763
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001764 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001765 }
Barry Mienydd671972010-10-04 16:33:58 +02001766
Derek Jonesd10e8962010-03-02 17:10:36 -06001767 return $this;
1768 }
1769
Derek Allard2067d1a2008-11-13 22:59:24 +00001770 // --------------------------------------------------------------------
1771
1772 /**
1773 * Empty Table
1774 *
1775 * Compiles a delete string and runs "DELETE FROM table"
1776 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001777 * @param string the table to empty
1778 * @return object
1779 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001780 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001781 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001782 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001783 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001784 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001785 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001786 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001787 }
1788
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001789 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001790 }
1791 else
1792 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001793 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001794 }
1795
1796 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001797 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001798 return $this->query($sql);
1799 }
1800
1801 // --------------------------------------------------------------------
1802
1803 /**
1804 * Truncate
1805 *
1806 * Compiles a truncate string and runs the query
1807 * If the database does not support the truncate() command
1808 * This function maps to "DELETE FROM table"
1809 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001810 * @param string the table to truncate
1811 * @return object
1812 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001813 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001815 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001816 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001817 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001818 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001819 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 }
1821
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001822 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001823 }
1824 else
1825 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001826 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 }
1828
1829 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001830 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001831 return $this->query($sql);
1832 }
WanWizard7219c072011-12-28 14:09:05 +01001833
Kyle Farris0c147b32011-08-26 02:29:31 -04001834 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001835
Kyle Farris0c147b32011-08-26 02:29:31 -04001836 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001837 * Truncate statement
1838 *
1839 * Generates a platform-specific truncate string from the supplied data
1840 *
1841 * If the database does not support the truncate() command,
1842 * then this method maps to 'DELETE FROM table'
1843 *
1844 * @param string the table name
1845 * @return string
1846 */
1847 protected function _truncate($table)
1848 {
1849 return 'TRUNCATE '.$table;
1850 }
1851
1852 // --------------------------------------------------------------------
1853
1854 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001855 * Get DELETE query string
1856 *
1857 * Compiles a delete query string and returns the sql
1858 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001859 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001860 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001861 * @return string
1862 */
1863 public function get_compiled_delete($table = '', $reset = TRUE)
1864 {
1865 $this->return_delete_sql = TRUE;
1866 $sql = $this->delete($table, '', NULL, $reset);
1867 $this->return_delete_sql = FALSE;
1868 return $sql;
1869 }
WanWizard7219c072011-12-28 14:09:05 +01001870
Derek Allard2067d1a2008-11-13 22:59:24 +00001871 // --------------------------------------------------------------------
1872
1873 /**
1874 * Delete
1875 *
1876 * Compiles a delete string and runs the query
1877 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001878 * @param mixed the table(s) to delete from. String or array
1879 * @param mixed the where clause
1880 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001881 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001882 * @return object
1883 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001884 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001885 {
1886 // Combine any cached components with the current statements
1887 $this->_merge_cache();
1888
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001889 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001890 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001891 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001892 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001893 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001894 }
1895
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001896 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001897 }
1898 elseif (is_array($table))
1899 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001900 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001901 {
1902 $this->delete($single_table, $where, $limit, FALSE);
1903 }
1904
1905 $this->_reset_write();
1906 return;
1907 }
1908 else
1909 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001910 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001911 }
1912
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001913 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001914 {
1915 $this->where($where);
1916 }
1917
Andrey Andreev650b4c02012-06-11 12:07:15 +03001918 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001919 {
1920 $this->limit($limit);
1921 }
1922
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001923 if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001924 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001925 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001926 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001927
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001928 $sql = $this->_delete($table, $this->qb_where, $this->qb_like, $this->qb_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001929 if ($reset_data)
1930 {
1931 $this->_reset_write();
1932 }
WanWizard7219c072011-12-28 14:09:05 +01001933
Andrey Andreev24276a32012-01-08 02:44:38 +02001934 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001935 }
WanWizard7219c072011-12-28 14:09:05 +01001936
Derek Allard2067d1a2008-11-13 22:59:24 +00001937 // --------------------------------------------------------------------
1938
1939 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001940 * Delete statement
1941 *
1942 * Generates a platform-specific delete string from the supplied data
1943 *
1944 * @param string the table name
1945 * @param array the where clause
1946 * @param array the like clause
1947 * @param string the limit clause
1948 * @return string
1949 */
1950 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
1951 {
1952 $conditions = array();
1953
1954 empty($where) OR $conditions[] = implode(' ', $where);
1955 empty($like) OR $conditions[] = implode(' ', $like);
1956
1957 return 'DELETE FROM '.$table
1958 .(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '')
Andrey Andreev650b4c02012-06-11 12:07:15 +03001959 .($limit ? ' LIMIT '.(int) $limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001960 }
1961
1962 // --------------------------------------------------------------------
1963
1964 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001965 * DB Prefix
1966 *
1967 * Prepends a database prefix if one exists in configuration
1968 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001969 * @param string the table
1970 * @return string
1971 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001972 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001973 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001974 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001975 {
1976 $this->display_error('db_table_name_required');
1977 }
1978
1979 return $this->dbprefix.$table;
1980 }
1981
1982 // --------------------------------------------------------------------
1983
1984 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001985 * Set DB Prefix
1986 *
1987 * Set's the DB Prefix to something new without needing to reconnect
1988 *
1989 * @param string the prefix
1990 * @return string
1991 */
1992 public function set_dbprefix($prefix = '')
1993 {
1994 return $this->dbprefix = $prefix;
1995 }
1996
1997 // --------------------------------------------------------------------
1998
1999 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002000 * Track Aliases
2001 *
2002 * Used to track SQL statements written with aliased tables.
2003 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002004 * @param string The table to inspect
2005 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002006 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002007 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002008 {
2009 if (is_array($table))
2010 {
2011 foreach ($table as $t)
2012 {
2013 $this->_track_aliases($t);
2014 }
2015 return;
2016 }
Barry Mienydd671972010-10-04 16:33:58 +02002017
Derek Jones37f4b9c2011-07-01 17:56:50 -05002018 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002019 // the string into discreet statements
2020 if (strpos($table, ',') !== FALSE)
2021 {
2022 return $this->_track_aliases(explode(',', $table));
2023 }
Barry Mienydd671972010-10-04 16:33:58 +02002024
Derek Allard2067d1a2008-11-13 22:59:24 +00002025 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002026 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002027 {
2028 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002029 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002030
Derek Allard2067d1a2008-11-13 22:59:24 +00002031 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002032 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002033
Derek Allard2067d1a2008-11-13 22:59:24 +00002034 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002035 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002036 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002037 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002038 }
2039 }
2040 }
WanWizard7219c072011-12-28 14:09:05 +01002041
Derek Allard2067d1a2008-11-13 22:59:24 +00002042 // --------------------------------------------------------------------
2043
2044 /**
2045 * Compile the SELECT statement
2046 *
2047 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05002048 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00002049 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002050 * @return string
2051 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002052 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 {
2054 // Combine any cached components with the current statements
2055 $this->_merge_cache();
2056
Derek Allard2067d1a2008-11-13 22:59:24 +00002057 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002058 if ($select_override !== FALSE)
2059 {
2060 $sql = $select_override;
2061 }
2062 else
2063 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002064 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002065
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002066 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002067 {
Barry Mienydd671972010-10-04 16:33:58 +02002068 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002069 }
2070 else
Barry Mienydd671972010-10-04 16:33:58 +02002071 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002072 // Cycle through the "select" portion of the query and prep each column name.
2073 // The reason we protect identifiers here rather then in the select() function
2074 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002075 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002076 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002077 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002078 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002079 }
Barry Mienydd671972010-10-04 16:33:58 +02002080
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002081 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002082 }
2083 }
2084
Derek Allard2067d1a2008-11-13 22:59:24 +00002085 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002086 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002087 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002088 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002089 }
2090
Derek Allard2067d1a2008-11-13 22:59:24 +00002091 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002092 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002093 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002094 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002095 }
2096
Derek Allard2067d1a2008-11-13 22:59:24 +00002097 // Write the "WHERE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002098 if (count($this->qb_where) > 0 OR count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002099 {
Greg Akere156c6e2011-04-20 16:03:04 -05002100 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00002101 }
2102
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002103 $sql .= implode("\n", $this->qb_where);
Derek Allard2067d1a2008-11-13 22:59:24 +00002104
Derek Allard2067d1a2008-11-13 22:59:24 +00002105 // Write the "LIKE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002106 if (count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002107 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002108 if (count($this->qb_where) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002109 {
2110 $sql .= "\nAND ";
2111 }
2112
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002113 $sql .= implode("\n", $this->qb_like);
Derek Allard2067d1a2008-11-13 22:59:24 +00002114 }
2115
Derek Allard2067d1a2008-11-13 22:59:24 +00002116 // Write the "GROUP BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002117 if (count($this->qb_groupby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002118 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002119 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002120 }
2121
Derek Allard2067d1a2008-11-13 22:59:24 +00002122 // Write the "HAVING" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002123 if (count($this->qb_having) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002124 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002125 $sql .= "\nHAVING ".implode("\n", $this->qb_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00002126 }
2127
Derek Allard2067d1a2008-11-13 22:59:24 +00002128 // Write the "ORDER BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002129 if (count($this->qb_orderby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002130 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002131 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002132 }
2133
Derek Allard2067d1a2008-11-13 22:59:24 +00002134 // Write the "LIMIT" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002135 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002136 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002137 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00002138 }
2139
2140 return $sql;
2141 }
2142
2143 // --------------------------------------------------------------------
2144
2145 /**
2146 * Object to Array
2147 *
2148 * Takes an object as input and converts the class variables to array key/vals
2149 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002150 * @param object
2151 * @return array
2152 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002153 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002154 {
2155 if ( ! is_object($object))
2156 {
2157 return $object;
2158 }
Barry Mienydd671972010-10-04 16:33:58 +02002159
Derek Allard2067d1a2008-11-13 22:59:24 +00002160 $array = array();
2161 foreach (get_object_vars($object) as $key => $val)
2162 {
2163 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002164 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002165 {
2166 $array[$key] = $val;
2167 }
2168 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002169
2170 return $array;
2171 }
Barry Mienydd671972010-10-04 16:33:58 +02002172
Derek Jonesd10e8962010-03-02 17:10:36 -06002173 // --------------------------------------------------------------------
2174
2175 /**
2176 * Object to Array
2177 *
2178 * Takes an object as input and converts the class variables to array key/vals
2179 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002180 * @param object
2181 * @return array
2182 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002183 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002184 {
2185 if ( ! is_object($object))
2186 {
2187 return $object;
2188 }
Barry Mienydd671972010-10-04 16:33:58 +02002189
Derek Jonesd10e8962010-03-02 17:10:36 -06002190 $array = array();
2191 $out = get_object_vars($object);
2192 $fields = array_keys($out);
2193
2194 foreach ($fields as $val)
2195 {
2196 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002197 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002198 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002199 $i = 0;
2200 foreach ($out[$val] as $data)
2201 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002202 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002203 }
2204 }
2205 }
2206
Derek Allard2067d1a2008-11-13 22:59:24 +00002207 return $array;
2208 }
Barry Mienydd671972010-10-04 16:33:58 +02002209
Derek Allard2067d1a2008-11-13 22:59:24 +00002210 // --------------------------------------------------------------------
2211
2212 /**
2213 * Start Cache
2214 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002215 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002216 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002217 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002218 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002219 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002220 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002221 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002222 }
2223
2224 // --------------------------------------------------------------------
2225
2226 /**
2227 * Stop Cache
2228 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002229 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002230 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002231 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002232 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002233 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002234 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002235 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002236 }
2237
2238 // --------------------------------------------------------------------
2239
2240 /**
2241 * Flush Cache
2242 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002243 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002244 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002245 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002246 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002247 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002248 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002249 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002250 'qb_cache_select' => array(),
2251 'qb_cache_from' => array(),
2252 'qb_cache_join' => array(),
2253 'qb_cache_where' => array(),
2254 'qb_cache_like' => array(),
2255 'qb_cache_groupby' => array(),
2256 'qb_cache_having' => array(),
2257 'qb_cache_orderby' => array(),
2258 'qb_cache_set' => array(),
2259 'qb_cache_exists' => array(),
2260 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002261 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002262 }
2263
2264 // --------------------------------------------------------------------
2265
2266 /**
2267 * Merge Cache
2268 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002269 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002270 * locally called ones.
2271 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002272 * @return void
2273 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002274 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002275 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002276 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002277 {
2278 return;
2279 }
2280
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002281 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002282 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002283 $qb_variable = 'qb_'.$val;
2284 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002285
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002286 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002287 {
2288 continue;
2289 }
2290
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002291 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002292 }
2293
2294 // If we are "protecting identifiers" we need to examine the "from"
2295 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002296 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002297 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002298 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002299 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002300
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002301 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002302 }
WanWizard7219c072011-12-28 14:09:05 +01002303
Kyle Farris0c147b32011-08-26 02:29:31 -04002304 // --------------------------------------------------------------------
2305
2306 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002307 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002308 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002309 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002310 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002311 * @return void
2312 */
2313 public function reset_query()
2314 {
2315 $this->_reset_select();
2316 $this->_reset_write();
2317 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002318
2319 // --------------------------------------------------------------------
2320
2321 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002322 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002323 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002324 * @param array An array of fields to reset
2325 * @return void
2326 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002327 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002328 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002329 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002330 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002331 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002332 {
2333 $this->$item = $default_value;
2334 }
2335 }
2336 }
2337
2338 // --------------------------------------------------------------------
2339
2340 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002341 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002342 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002343 * @return void
2344 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002345 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002346 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002347 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002348 'qb_select' => array(),
2349 'qb_from' => array(),
2350 'qb_join' => array(),
2351 'qb_where' => array(),
2352 'qb_like' => array(),
2353 'qb_groupby' => array(),
2354 'qb_having' => array(),
2355 'qb_orderby' => array(),
2356 'qb_wherein' => array(),
2357 'qb_aliased_tables' => array(),
2358 'qb_no_escape' => array(),
2359 'qb_distinct' => FALSE,
2360 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002361 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002362 )
2363 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002364 }
Barry Mienydd671972010-10-04 16:33:58 +02002365
Derek Allard2067d1a2008-11-13 22:59:24 +00002366 // --------------------------------------------------------------------
2367
2368 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002369 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002370 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002371 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002372 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002373 * @return void
2374 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002375 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002376 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002377 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002378 'qb_set' => array(),
2379 'qb_from' => array(),
2380 'qb_where' => array(),
2381 'qb_like' => array(),
2382 'qb_orderby' => array(),
2383 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002384 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002385 )
2386 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002387 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002388
Derek Allard2067d1a2008-11-13 22:59:24 +00002389}
2390
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002391/* End of file DB_query_builder.php */
Andrey Andreev58803fb2012-06-24 00:45:37 +03002392/* Location: ./system/database/DB_query_builder.php */