blob: 62e02129b06cddf869c0d8ef59427ae05481d0c2 [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 {
WanWizardbc69f362012-06-22 00:10:11 +0200466 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000467
Andrey Andreev929fd2d2012-06-17 17:29:57 +0300468 $k = (($op = $this->_get_operator($k)) !== FALSE)
469 ? $this->protect_identifiers(substr($k, 0, strpos($k, $op)), FALSE, $escape).strstr($k, $op)
Andrey Andreev9c14f652012-06-10 14:35:07 +0300470 : $this->protect_identifiers($k, FALSE, $escape);
471
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 if (is_null($v) && ! $this->_has_operator($k))
473 {
474 // value appears not to have been set, assign the test to IS NULL
475 $k .= ' IS NULL';
476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 if ( ! is_null($v))
479 {
480 if ($escape === TRUE)
481 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 $v = ' '.$this->escape($v);
483 }
WanWizard7219c072011-12-28 14:09:05 +0100484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 if ( ! $this->_has_operator($k))
486 {
Greg Akere156c6e2011-04-20 16:03:04 -0500487 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
489 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000490
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000491 $this->qb_where[] = $prefix.$k.$v;
492 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000494 $this->qb_cache_where[] = $prefix.$k.$v;
495 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 }
Barry Mienydd671972010-10-04 16:33:58 +0200497
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 }
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 return $this;
501 }
502
503 // --------------------------------------------------------------------
504
505 /**
506 * Where_in
507 *
508 * Generates a WHERE field IN ('item', 'item') SQL query joined with
509 * AND if appropriate
510 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 * @param string The field to search
512 * @param array The values searched on
513 * @return object
514 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300515 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300517 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 }
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 // --------------------------------------------------------------------
521
522 /**
523 * Where_in_or
524 *
525 * Generates a WHERE field IN ('item', 'item') SQL query joined with
526 * OR if appropriate
527 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 * @param string The field to search
529 * @param array The values searched on
530 * @return object
531 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300532 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300534 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
536
537 // --------------------------------------------------------------------
538
539 /**
540 * Where_not_in
541 *
542 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
543 * with AND if appropriate
544 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 * @param string The field to search
546 * @param array The values searched on
547 * @return object
548 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300549 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300551 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 }
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 // --------------------------------------------------------------------
555
556 /**
557 * Where_not_in_or
558 *
559 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
560 * with OR if appropriate
561 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 * @param string The field to search
563 * @param array The values searched on
564 * @return object
565 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300566 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300568 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 }
570
571 // --------------------------------------------------------------------
572
573 /**
574 * Where_in
575 *
576 * Called by where_in, where_in_or, where_not_in, where_not_in_or
577 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 * @param string The field to search
579 * @param array The values searched on
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300580 * @param bool If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200581 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 * @return object
583 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300584 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 {
586 if ($key === NULL OR $values === NULL)
587 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300588 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 }
Barry Mienydd671972010-10-04 16:33:58 +0200590
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 if ( ! is_array($values))
592 {
593 $values = array($values);
594 }
Barry Mienydd671972010-10-04 16:33:58 +0200595
Andrey Andreev498c1e02012-06-16 03:34:10 +0300596 is_bool($escape) OR $escape = $this->_protect_identifiers;
597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 $not = ($not) ? ' NOT' : '';
599
600 foreach ($values as $value)
601 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000602 $this->qb_wherein[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 }
604
WanWizardbc69f362012-06-22 00:10:11 +0200605 $prefix = (count($this->qb_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Andrey Andreev498c1e02012-06-16 03:34:10 +0300606 $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 +0200607
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000608 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000610 $this->qb_cache_where[] = $where_in;
611 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 }
613
614 // reset the array for multiple calls
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000615 $this->qb_wherein = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 return $this;
617 }
Barry Mienydd671972010-10-04 16:33:58 +0200618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 // --------------------------------------------------------------------
620
621 /**
622 * Like
623 *
624 * Generates a %LIKE% portion of the query. Separates
625 * multiple calls with AND
626 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 * @param mixed
628 * @param mixed
629 * @return object
630 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600631 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 {
633 return $this->_like($field, $match, 'AND ', $side);
634 }
635
636 // --------------------------------------------------------------------
637
638 /**
639 * Not Like
640 *
641 * Generates a NOT LIKE portion of the query. Separates
642 * multiple calls with AND
643 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 * @param mixed
645 * @param mixed
646 * @return object
647 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600648 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 {
650 return $this->_like($field, $match, 'AND ', $side, 'NOT');
651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 // --------------------------------------------------------------------
654
655 /**
656 * OR Like
657 *
658 * Generates a %LIKE% portion of the query. Separates
659 * multiple calls with OR
660 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 * @param mixed
662 * @param mixed
663 * @return object
664 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600665 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 {
667 return $this->_like($field, $match, 'OR ', $side);
668 }
669
670 // --------------------------------------------------------------------
671
672 /**
673 * OR Not Like
674 *
675 * Generates a NOT LIKE portion of the query. Separates
676 * multiple calls with OR
677 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 * @param mixed
679 * @param mixed
680 * @return object
681 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600682 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
684 return $this->_like($field, $match, 'OR ', $side, 'NOT');
685 }
Barry Mienydd671972010-10-04 16:33:58 +0200686
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 // --------------------------------------------------------------------
688
689 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 * Like
691 *
692 * Called by like() or orlike()
693 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 * @param mixed
695 * @param mixed
696 * @param string
697 * @return object
698 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600699 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 {
701 if ( ! is_array($field))
702 {
703 $field = array($field => $match);
704 }
Barry Mienydd671972010-10-04 16:33:58 +0200705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 foreach ($field as $k => $v)
707 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000708 $k = $this->protect_identifiers($k);
WanWizardbc69f362012-06-22 00:10:11 +0200709 $prefix = (count($this->qb_like) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Derek Jonese4ed5832009-02-20 21:44:59 +0000710 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300711
Andrey Andreev24276a32012-01-08 02:44:38 +0200712 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400713 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100714 $like_statement = "{$prefix} $k $not LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400715 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200716 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100718 $like_statement = "{$prefix} $k $not LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200720 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100722 $like_statement = "{$prefix} $k $not LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 }
724 else
725 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100726 $like_statement = "{$prefix} $k $not LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600728
Derek Jonese4ed5832009-02-20 21:44:59 +0000729 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100730 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000731 {
Greg Aker0d424892010-01-26 02:14:44 +0000732 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000733 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600734
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000735 $this->qb_like[] = $like_statement;
736 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000738 $this->qb_cache_like[] = $like_statement;
739 $this->qb_cache_exists[] = 'like';
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 }
Barry Mienydd671972010-10-04 16:33:58 +0200741
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200743
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 return $this;
745 }
Barry Mienydd671972010-10-04 16:33:58 +0200746
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 // --------------------------------------------------------------------
748
749 /**
WanWizard7219c072011-12-28 14:09:05 +0100750 * Starts a query group.
751 *
752 * @param string (Internal use only)
753 * @param string (Internal use only)
754 * @return object
755 */
756 public function group_start($not = '', $type = 'AND ')
757 {
758 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100759
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000760 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100761 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000762 $this->qb_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (';
WanWizard7219c072011-12-28 14:09:05 +0100763
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000764 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100765 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000766 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100767 }
768
769 return $this;
770 }
771
772 // --------------------------------------------------------------------
773
774 /**
775 * Starts a query group, but ORs the group
776 *
777 * @return object
778 */
779 public function or_group_start()
780 {
781 return $this->group_start('', 'OR ');
782 }
783
784 // --------------------------------------------------------------------
785
786 /**
787 * Starts a query group, but NOTs the group
788 *
789 * @return object
790 */
791 public function not_group_start()
792 {
793 return $this->group_start('NOT ', 'AND ');
794 }
795
796 // --------------------------------------------------------------------
797
798 /**
799 * Starts a query group, but OR NOTs the group
800 *
801 * @return object
802 */
803 public function or_not_group_start()
804 {
805 return $this->group_start('NOT ', 'OR ');
806 }
807
808 // --------------------------------------------------------------------
809
810 /**
811 * Ends a query group
812 *
813 * @return object
814 */
815 public function group_end()
816 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000817 $this->qb_where_group_started = FALSE;
818 $this->qb_where[] = $value = str_repeat(' ', $this->qb_where_group_count--) . ')';
WanWizard7219c072011-12-28 14:09:05 +0100819
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000820 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100821 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000822 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100823 }
824
WanWizard7219c072011-12-28 14:09:05 +0100825 return $this;
826 }
827
828 // --------------------------------------------------------------------
829
830 /**
831 * Group_get_type
832 *
833 * Called by group_start(), _like(), _where() and _where_in()
834 *
835 * @param string
836 * @return string
837 */
838 protected function _group_get_type($type)
839 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000840 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100841 {
842 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000843 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100844 }
845
846 return $type;
847 }
848
849 // --------------------------------------------------------------------
850
851 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 * GROUP BY
853 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 * @param string
855 * @return object
856 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600857 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 {
859 if (is_string($by))
860 {
861 $by = explode(',', $by);
862 }
Barry Mienydd671972010-10-04 16:33:58 +0200863
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 foreach ($by as $val)
865 {
866 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200867
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100868 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000870 $this->qb_groupby[] = $val = $this->protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200871
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000872 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000874 $this->qb_cache_groupby[] = $val;
875 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 }
877 }
878 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200879
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 return $this;
881 }
882
883 // --------------------------------------------------------------------
884
885 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 * Sets the HAVING value
887 *
888 * Separates multiple calls with AND
889 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 * @param string
891 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300892 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 * @return object
894 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300895 public function having($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 {
897 return $this->_having($key, $value, 'AND ', $escape);
898 }
Barry Mienydd671972010-10-04 16:33:58 +0200899
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 // --------------------------------------------------------------------
901
902 /**
903 * Sets the OR HAVING value
904 *
905 * Separates multiple calls with OR
906 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 * @param string
908 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300909 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 * @return object
911 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300912 public function or_having($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
914 return $this->_having($key, $value, 'OR ', $escape);
915 }
Barry Mienydd671972010-10-04 16:33:58 +0200916
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 // --------------------------------------------------------------------
918
919 /**
920 * Sets the HAVING values
921 *
922 * Called by having() or or_having()
923 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 * @param string
925 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300926 * @param string
927 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 * @return object
929 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300930 protected function _having($key, $value = '', $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 {
932 if ( ! is_array($key))
933 {
934 $key = array($key => $value);
935 }
Barry Mienydd671972010-10-04 16:33:58 +0200936
Andrey Andreevfe642da2012-06-16 03:47:33 +0300937 is_bool($escape) OR $escape = $this->_protect_identifiers;
938
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 foreach ($key as $k => $v)
940 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000941 $prefix = (count($this->qb_having) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000942
Andrey Andreev974c75b2012-06-15 12:30:02 +0300943 $k = $this->_has_operator($k)
944 ? $this->protect_identifiers(substr($k, 0, strpos(rtrim($k), ' ')), FALSE, $escape).strchr(rtrim($k), ' ')
945 : $this->protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000946
947 if ( ! $this->_has_operator($k))
948 {
949 $k .= ' = ';
950 }
951
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100952 if ($v !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400954 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 }
Barry Mienydd671972010-10-04 16:33:58 +0200956
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000957 $this->qb_having[] = $prefix.$k.$v;
958 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000960 $this->qb_cache_having[] = $prefix.$k.$v;
961 $this->qb_cache_exists[] = 'having';
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 }
963 }
Barry Mienydd671972010-10-04 16:33:58 +0200964
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 return $this;
966 }
Barry Mienydd671972010-10-04 16:33:58 +0200967
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 // --------------------------------------------------------------------
969
970 /**
971 * Sets the ORDER BY value
972 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 * @param string
974 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100975 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 * @return object
977 */
Andrey Andreevd24160c2012-06-16 03:21:20 +0300978 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200980 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 {
982 $orderby = ''; // Random results want or don't need a field name
983 $direction = $this->_random_keyword;
984 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100985 elseif (trim($direction) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 {
Andrey Andreev650b4c02012-06-11 12:07:15 +0300987 $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : ' ASC';
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 }
Barry Mienydd671972010-10-04 16:33:58 +0200989
Andrey Andreevd24160c2012-06-16 03:21:20 +0300990 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +0200991
Andrey Andreevd24160c2012-06-16 03:21:20 +0300992 if ($escape === TRUE && strpos($orderby, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 {
994 $temp = array();
995 foreach (explode(',', $orderby) as $part)
996 {
997 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000998 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 {
Andrey Andreev88cb2782012-06-11 20:40:50 +03001000 $part = preg_match('/^(.+)\s+(ASC|DESC)$/i', $part, $matches)
1001 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
1002 : $this->protect_identifiers($part);
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 }
Barry Mienydd671972010-10-04 16:33:58 +02001004
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 $temp[] = $part;
1006 }
Barry Mienydd671972010-10-04 16:33:58 +02001007
1008 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001010 elseif ($direction !== $this->_random_keyword && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 {
Andrey Andreev1d1d7ff2012-06-11 22:35:35 +03001012 $orderby = preg_match('/^(.+)\s+(ASC|DESC)$/i', $orderby, $matches)
Andrey Andreev88cb2782012-06-11 20:40:50 +03001013 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
1014 : $this->protect_identifiers($orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 }
Barry Mienydd671972010-10-04 16:33:58 +02001016
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001017 $this->qb_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +02001018
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001019 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001021 $this->qb_cache_orderby[] = $orderby_statement;
1022 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 }
1024
1025 return $this;
1026 }
Barry Mienydd671972010-10-04 16:33:58 +02001027
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 // --------------------------------------------------------------------
1029
1030 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 * Sets the LIMIT value
1032 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001033 * @param int the limit value
1034 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 * @return object
1036 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +02001037 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001039 is_null($value) OR $this->qb_limit = (int) $value;
1040 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001041
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 return $this;
1043 }
Barry Mienydd671972010-10-04 16:33:58 +02001044
Derek Allard2067d1a2008-11-13 22:59:24 +00001045 // --------------------------------------------------------------------
1046
1047 /**
1048 * Sets the OFFSET value
1049 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001050 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 * @return object
1052 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001053 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001055 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 return $this;
1057 }
Barry Mienydd671972010-10-04 16:33:58 +02001058
Derek Allard2067d1a2008-11-13 22:59:24 +00001059 // --------------------------------------------------------------------
1060
1061 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001062 * The "set" function.
1063 *
1064 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 * @param mixed
1067 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001068 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 * @return object
1070 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001071 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 {
1073 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001074
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 if ( ! is_array($key))
1076 {
1077 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001078 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001079
Andrey Andreevfe642da2012-06-16 03:47:33 +03001080 is_bool($escape) OR $escape = $this->_protect_identifiers;
1081
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 foreach ($key as $k => $v)
1083 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001084 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1085 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 }
Barry Mienydd671972010-10-04 16:33:58 +02001087
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 return $this;
1089 }
WanWizard7219c072011-12-28 14:09:05 +01001090
Kyle Farris0c147b32011-08-26 02:29:31 -04001091 // --------------------------------------------------------------------
1092
1093 /**
1094 * Get SELECT query string
1095 *
1096 * Compiles a SELECT query string and returns the sql.
1097 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001098 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001099 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001100 * @return string
1101 */
WanWizard7219c072011-12-28 14:09:05 +01001102 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001103 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001104 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001105 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001106 $this->_track_aliases($table);
1107 $this->from($table);
1108 }
WanWizard7219c072011-12-28 14:09:05 +01001109
Andrey Andreev650b4c02012-06-11 12:07:15 +03001110 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001111
Kyle Farris0c147b32011-08-26 02:29:31 -04001112 if ($reset === TRUE)
1113 {
1114 $this->_reset_select();
1115 }
WanWizard7219c072011-12-28 14:09:05 +01001116
Kyle Farris0c147b32011-08-26 02:29:31 -04001117 return $select;
1118 }
WanWizard7219c072011-12-28 14:09:05 +01001119
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 // --------------------------------------------------------------------
1121
1122 /**
1123 * Get
1124 *
1125 * Compiles the select statement based on the other functions called
1126 * and runs the query
1127 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 * @param string the table
1129 * @param string the limit clause
1130 * @param string the offset clause
1131 * @return object
1132 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001133 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001135 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 {
1137 $this->_track_aliases($table);
1138 $this->from($table);
1139 }
Barry Mienydd671972010-10-04 16:33:58 +02001140
Andrey Andreev650b4c02012-06-11 12:07:15 +03001141 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 {
1143 $this->limit($limit, $offset);
1144 }
Barry Mienydd671972010-10-04 16:33:58 +02001145
Andrey Andreev24276a32012-01-08 02:44:38 +02001146 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 $this->_reset_select();
1148 return $result;
1149 }
1150
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001151 // --------------------------------------------------------------------
1152
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 /**
1154 * "Count All Results" query
1155 *
Barry Mienydd671972010-10-04 16:33:58 +02001156 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001157 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 * @param string
1160 * @return string
1161 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001162 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001164 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 {
1166 $this->_track_aliases($table);
1167 $this->from($table);
1168 }
Barry Mienydd671972010-10-04 16:33:58 +02001169
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001170 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001172
Purwandi1d160e72012-01-09 16:33:28 +07001173 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001175 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 }
1177
Purwandi1d160e72012-01-09 16:33:28 +07001178 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001179 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001181
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 // --------------------------------------------------------------------
1183
1184 /**
1185 * Get_Where
1186 *
1187 * Allows the where clause, limit and offset to be added directly
1188 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 * @param string the where clause
1190 * @param string the limit clause
1191 * @param string the offset clause
1192 * @return object
1193 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001194 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001196 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 {
1198 $this->from($table);
1199 }
1200
1201 if ( ! is_null($where))
1202 {
1203 $this->where($where);
1204 }
Barry Mienydd671972010-10-04 16:33:58 +02001205
Andrey Andreev650b4c02012-06-11 12:07:15 +03001206 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 {
1208 $this->limit($limit, $offset);
1209 }
Barry Mienydd671972010-10-04 16:33:58 +02001210
Andrey Andreev24276a32012-01-08 02:44:38 +02001211 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 $this->_reset_select();
1213 return $result;
1214 }
1215
1216 // --------------------------------------------------------------------
1217
1218 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001219 * Insert_Batch
1220 *
1221 * Compiles batch insert strings and runs the queries
1222 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001223 * @param string the table to retrieve the results from
1224 * @param array an associative array of insert values
1225 * @return object
1226 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001227 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001228 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001229 if ( ! is_null($set))
1230 {
1231 $this->set_insert_batch($set);
1232 }
Barry Mienydd671972010-10-04 16:33:58 +02001233
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001234 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001235 {
1236 if ($this->db_debug)
1237 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001238 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001239 return $this->display_error('db_must_use_set');
1240 }
1241 return FALSE;
1242 }
1243
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001244 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001245 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001246 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001247 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001248 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001249 }
Barry Mienydd671972010-10-04 16:33:58 +02001250
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001251 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001252 }
1253
1254 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001255 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001256 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001257 $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 -06001258 }
Barry Mienydd671972010-10-04 16:33:58 +02001259
Derek Jonesd10e8962010-03-02 17:10:36 -06001260 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001261 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001262 }
1263
1264 // --------------------------------------------------------------------
1265
1266 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001267 * Insert_batch statement
1268 *
1269 * Generates a platform-specific insert string from the supplied data.
1270 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001271 * @param string the table name
1272 * @param array the insert keys
1273 * @param array the insert values
1274 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001275 */
1276 protected function _insert_batch($table, $keys, $values)
1277 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001278 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001279 }
1280
1281 // --------------------------------------------------------------------
1282
1283 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001284 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001285 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001286 * @param mixed
1287 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001288 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001289 * @return object
1290 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001291 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001292 {
1293 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001294
Derek Jonesd10e8962010-03-02 17:10:36 -06001295 if ( ! is_array($key))
1296 {
1297 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001298 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001299
Andrey Andreevfe642da2012-06-16 03:47:33 +03001300 is_bool($escape) OR $escape = $this->_protect_identifiers;
1301
Iban Eguia3c0a4522012-04-15 13:30:44 +02001302 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001303 sort($keys);
1304
1305 foreach ($key as $row)
1306 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001307 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001308 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1309 {
1310 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001311 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001312 return;
1313 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001314
Barry Mienydd671972010-10-04 16:33:58 +02001315 ksort($row); // puts $row in the same order as our keys
1316
Andrey Andreev650b4c02012-06-11 12:07:15 +03001317 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001318 {
1319 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001320 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001321 {
Barry Mienydd671972010-10-04 16:33:58 +02001322 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001323 }
1324
Andrey Andreev650b4c02012-06-11 12:07:15 +03001325 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001326 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001327
1328 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001329 }
1330
1331 foreach ($keys as $k)
1332 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001333 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001334 }
Barry Mienydd671972010-10-04 16:33:58 +02001335
Derek Jonesd10e8962010-03-02 17:10:36 -06001336 return $this;
1337 }
WanWizard7219c072011-12-28 14:09:05 +01001338
Kyle Farris0c147b32011-08-26 02:29:31 -04001339 // --------------------------------------------------------------------
1340
1341 /**
1342 * Get INSERT query string
1343 *
1344 * Compiles an insert query and returns the sql
1345 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001346 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001347 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001348 * @return string
1349 */
1350 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001351 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001352 if ($this->_validate_insert($table) === FALSE)
1353 {
1354 return FALSE;
1355 }
WanWizard7219c072011-12-28 14:09:05 +01001356
Kyle Farris76116012011-08-31 11:17:48 -04001357 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001358 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001359 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001360 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001361 array_keys($this->qb_set),
1362 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001363 );
WanWizard7219c072011-12-28 14:09:05 +01001364
Kyle Farris0c147b32011-08-26 02:29:31 -04001365 if ($reset === TRUE)
1366 {
1367 $this->_reset_write();
1368 }
WanWizard7219c072011-12-28 14:09:05 +01001369
Kyle Farris0c147b32011-08-26 02:29:31 -04001370 return $sql;
1371 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001372
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 // --------------------------------------------------------------------
1374
1375 /**
1376 * Insert
1377 *
1378 * Compiles an insert string and runs the query
1379 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001380 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 * @param array an associative array of insert values
1382 * @return object
1383 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001384 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001385 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001386 if ( ! is_null($set))
1387 {
1388 $this->set($set);
1389 }
WanWizard7219c072011-12-28 14:09:05 +01001390
Kyle Farris0c147b32011-08-26 02:29:31 -04001391 if ($this->_validate_insert($table) === FALSE)
1392 {
1393 return FALSE;
1394 }
WanWizard7219c072011-12-28 14:09:05 +01001395
Kyle Farris76116012011-08-31 11:17:48 -04001396 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001397 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001398 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001399 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001400 array_keys($this->qb_set),
1401 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001402 );
Barry Mienydd671972010-10-04 16:33:58 +02001403
Kyle Farris0c147b32011-08-26 02:29:31 -04001404 $this->_reset_write();
1405 return $this->query($sql);
1406 }
WanWizard7219c072011-12-28 14:09:05 +01001407
Kyle Farris0c147b32011-08-26 02:29:31 -04001408 // --------------------------------------------------------------------
1409
1410 /**
Andrey Andreev65d537c2012-04-05 14:11:41 +03001411 * Insert statement
1412 *
1413 * Generates a platform-specific insert string from the supplied data
1414 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001415 * @param string the table name
1416 * @param array the insert keys
1417 * @param array the insert values
1418 * @return string
Andrey Andreev65d537c2012-04-05 14:11:41 +03001419 */
1420 protected function _insert($table, $keys, $values)
1421 {
1422 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1423 }
1424
1425 // --------------------------------------------------------------------
1426
1427 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001428 * Validate Insert
1429 *
1430 * This method is used by both insert() and get_compiled_insert() to
1431 * validate that the there data is actually being set and that table
1432 * has been chosen to be inserted into.
1433 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001434 * @param string the table to insert data into
1435 * @return string
1436 */
WanWizard7219c072011-12-28 14:09:05 +01001437 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001438 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001439 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001440 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001441 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001442 }
1443
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001444 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001445 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001446 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001447 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001448 elseif ( ! isset($this->qb_from[0]))
1449 {
1450 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1451 }
WanWizard7219c072011-12-28 14:09:05 +01001452
Kyle Farris0c147b32011-08-26 02:29:31 -04001453 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001454 }
Barry Mienydd671972010-10-04 16:33:58 +02001455
Phil Sturgeon9789f322011-07-15 15:14:05 -06001456 // --------------------------------------------------------------------
1457
1458 /**
1459 * Replace
1460 *
1461 * Compiles an replace into string and runs the query
1462 *
1463 * @param string the table to replace data into
1464 * @param array an associative array of insert values
1465 * @return object
1466 */
1467 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001468 {
1469 if ( ! is_null($set))
1470 {
1471 $this->set($set);
1472 }
Barry Mienydd671972010-10-04 16:33:58 +02001473
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001474 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001475 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001476 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001477 }
1478
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001479 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001480 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001481 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001482 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001483 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001484 }
Barry Mienydd671972010-10-04 16:33:58 +02001485
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001486 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001487 }
1488
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001489 $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 +00001490
Derek Jonesd10e8962010-03-02 17:10:36 -06001491 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001492 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001493 }
WanWizard7219c072011-12-28 14:09:05 +01001494
Kyle Farris0c147b32011-08-26 02:29:31 -04001495 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001496
Kyle Farris0c147b32011-08-26 02:29:31 -04001497 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001498 * Replace statement
1499 *
1500 * Generates a platform-specific replace string from the supplied data
1501 *
1502 * @param string the table name
1503 * @param array the insert keys
1504 * @param array the insert values
1505 * @return string
1506 */
1507 protected function _replace($table, $keys, $values)
1508 {
1509 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1510 }
1511
1512 // --------------------------------------------------------------------
1513
1514 /**
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001515 * From Tables
1516 *
1517 * This public function implicitly groups FROM tables so there is no confusion
1518 * about operator precedence in harmony with SQL standards
1519 *
1520 * @param array
1521 * @return string
1522 */
1523 protected function _from_tables($tables)
1524 {
1525 is_array($tables) OR $tables = array($tables);
1526
1527 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
1528 }
1529
1530 // --------------------------------------------------------------------
1531
1532 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001533 * Get UPDATE query string
1534 *
1535 * Compiles an update query and returns the sql
1536 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001537 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001538 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001539 * @return string
1540 */
1541 public function get_compiled_update($table = '', $reset = TRUE)
1542 {
1543 // Combine any cached components with the current statements
1544 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001545
Kyle Farris0c147b32011-08-26 02:29:31 -04001546 if ($this->_validate_update($table) === FALSE)
1547 {
1548 return FALSE;
1549 }
WanWizard7219c072011-12-28 14:09:05 +01001550
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001551 $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 +01001552
Kyle Farris0c147b32011-08-26 02:29:31 -04001553 if ($reset === TRUE)
1554 {
1555 $this->_reset_write();
1556 }
WanWizard7219c072011-12-28 14:09:05 +01001557
Kyle Farris0c147b32011-08-26 02:29:31 -04001558 return $sql;
1559 }
WanWizard7219c072011-12-28 14:09:05 +01001560
Derek Allard2067d1a2008-11-13 22:59:24 +00001561 // --------------------------------------------------------------------
1562
1563 /**
1564 * Update
1565 *
1566 * Compiles an update string and runs the query
1567 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001568 * @param string the table to retrieve the results from
1569 * @param array an associative array of update values
1570 * @param mixed the where clause
1571 * @return object
1572 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001573 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001574 {
1575 // Combine any cached components with the current statements
1576 $this->_merge_cache();
1577
1578 if ( ! is_null($set))
1579 {
1580 $this->set($set);
1581 }
Barry Mienydd671972010-10-04 16:33:58 +02001582
Kyle Farris0c147b32011-08-26 02:29:31 -04001583 if ($this->_validate_update($table) === FALSE)
1584 {
1585 return FALSE;
1586 }
1587
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001588 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001589 {
1590 $this->where($where);
1591 }
1592
Andrey Andreev650b4c02012-06-11 12:07:15 +03001593 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001594 {
1595 $this->limit($limit);
1596 }
1597
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001598 $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 +00001599
Kyle Farris0c147b32011-08-26 02:29:31 -04001600 $this->_reset_write();
1601 return $this->query($sql);
1602 }
WanWizard7219c072011-12-28 14:09:05 +01001603
Kyle Farris0c147b32011-08-26 02:29:31 -04001604 // --------------------------------------------------------------------
1605
1606 /**
Andrey Andreev975034d2012-04-05 15:09:55 +03001607 * Update statement
1608 *
1609 * Generates a platform-specific update string from the supplied data
1610 *
1611 * @param string the table name
1612 * @param array the update data
1613 * @param array the where clause
1614 * @param array the orderby clause
1615 * @param array the limit clause
Andrey Andreev00541ae2012-04-09 11:43:10 +03001616 * @param array the like clause
Andrey Andreev975034d2012-04-05 15:09:55 +03001617 * @return string
1618 */
Andrey Andreev00541ae2012-04-09 11:43:10 +03001619 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Andrey Andreev975034d2012-04-05 15:09:55 +03001620 {
1621 foreach ($values as $key => $val)
1622 {
1623 $valstr[] = $key.' = '.$val;
1624 }
1625
Andrey Andreev00541ae2012-04-09 11:43:10 +03001626 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
1627
1628 if ( ! empty($like))
1629 {
1630 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
1631 }
1632
Andrey Andreev975034d2012-04-05 15:09:55 +03001633 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev00541ae2012-04-09 11:43:10 +03001634 .$where
Andrey Andreev975034d2012-04-05 15:09:55 +03001635 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
1636 .($limit ? ' LIMIT '.$limit : '');
1637 }
1638
1639 // --------------------------------------------------------------------
1640
1641 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001642 * Validate Update
1643 *
1644 * This method is used by both update() and get_compiled_update() to
1645 * validate that data is actually being set and that a table has been
1646 * chosen to be update.
1647 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001648 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001649 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001650 */
1651 protected function _validate_update($table = '')
1652 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001653 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001654 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001655 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001656 }
1657
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001658 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001659 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001660 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001661 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001662 elseif ( ! isset($this->qb_from[0]))
1663 {
1664 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1665 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001666
1667 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001668 }
WanWizard7219c072011-12-28 14:09:05 +01001669
Derek Jonesd10e8962010-03-02 17:10:36 -06001670 // --------------------------------------------------------------------
1671
1672 /**
1673 * Update_Batch
1674 *
1675 * Compiles an update string and runs the query
1676 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001677 * @param string the table to retrieve the results from
1678 * @param array an associative array of update values
1679 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001680 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001681 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001682 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001683 {
1684 // Combine any cached components with the current statements
1685 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001686
Derek Jonesd10e8962010-03-02 17:10:36 -06001687 if (is_null($index))
1688 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001689 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001690 }
1691
1692 if ( ! is_null($set))
1693 {
1694 $this->set_update_batch($set, $index);
1695 }
1696
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001697 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001698 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001699 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001700 }
1701
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001702 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001703 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001704 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001705 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001706 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001707 }
Barry Mienydd671972010-10-04 16:33:58 +02001708
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001709 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001710 }
Barry Mienydd671972010-10-04 16:33:58 +02001711
Derek Jonesd10e8962010-03-02 17:10:36 -06001712 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001713 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001714 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001715 $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 -06001716 }
Barry Mienydd671972010-10-04 16:33:58 +02001717
Derek Jonesd10e8962010-03-02 17:10:36 -06001718 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001719 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001720 }
1721
1722 // --------------------------------------------------------------------
1723
1724 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001725 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001726 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001727 * @param array
1728 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001729 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001730 * @return object
1731 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001732 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001733 {
1734 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001735
Derek Jonesd10e8962010-03-02 17:10:36 -06001736 if ( ! is_array($key))
1737 {
1738 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001739 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001740
Andrey Andreevfe642da2012-06-16 03:47:33 +03001741 is_bool($escape) OR $escape = $this->_protect_identifiers;
1742
Derek Jonesd10e8962010-03-02 17:10:36 -06001743 foreach ($key as $k => $v)
1744 {
1745 $index_set = FALSE;
1746 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001747 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001748 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001749 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001750 {
1751 $index_set = TRUE;
1752 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001753
Andrey Andreevfe642da2012-06-16 03:47:33 +03001754 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001755 }
1756
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001757 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001758 {
1759 return $this->display_error('db_batch_missing_index');
1760 }
1761
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001762 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001763 }
Barry Mienydd671972010-10-04 16:33:58 +02001764
Derek Jonesd10e8962010-03-02 17:10:36 -06001765 return $this;
1766 }
1767
Derek Allard2067d1a2008-11-13 22:59:24 +00001768 // --------------------------------------------------------------------
1769
1770 /**
1771 * Empty Table
1772 *
1773 * Compiles a delete string and runs "DELETE FROM table"
1774 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001775 * @param string the table to empty
1776 * @return object
1777 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001778 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001779 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001780 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001781 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001782 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001783 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001784 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001785 }
1786
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001787 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 }
1789 else
1790 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001791 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001792 }
1793
1794 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001795 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001796 return $this->query($sql);
1797 }
1798
1799 // --------------------------------------------------------------------
1800
1801 /**
1802 * Truncate
1803 *
1804 * Compiles a truncate string and runs the query
1805 * If the database does not support the truncate() command
1806 * This function maps to "DELETE FROM table"
1807 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001808 * @param string the table to truncate
1809 * @return object
1810 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001811 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001812 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001813 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001815 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001816 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001817 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001818 }
1819
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001820 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001821 }
1822 else
1823 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001824 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001825 }
1826
1827 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001828 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001829 return $this->query($sql);
1830 }
WanWizard7219c072011-12-28 14:09:05 +01001831
Kyle Farris0c147b32011-08-26 02:29:31 -04001832 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001833
Kyle Farris0c147b32011-08-26 02:29:31 -04001834 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001835 * Truncate statement
1836 *
1837 * Generates a platform-specific truncate string from the supplied data
1838 *
1839 * If the database does not support the truncate() command,
1840 * then this method maps to 'DELETE FROM table'
1841 *
1842 * @param string the table name
1843 * @return string
1844 */
1845 protected function _truncate($table)
1846 {
1847 return 'TRUNCATE '.$table;
1848 }
1849
1850 // --------------------------------------------------------------------
1851
1852 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001853 * Get DELETE query string
1854 *
1855 * Compiles a delete query string and returns the sql
1856 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001857 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001858 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001859 * @return string
1860 */
1861 public function get_compiled_delete($table = '', $reset = TRUE)
1862 {
1863 $this->return_delete_sql = TRUE;
1864 $sql = $this->delete($table, '', NULL, $reset);
1865 $this->return_delete_sql = FALSE;
1866 return $sql;
1867 }
WanWizard7219c072011-12-28 14:09:05 +01001868
Derek Allard2067d1a2008-11-13 22:59:24 +00001869 // --------------------------------------------------------------------
1870
1871 /**
1872 * Delete
1873 *
1874 * Compiles a delete string and runs the query
1875 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001876 * @param mixed the table(s) to delete from. String or array
1877 * @param mixed the where clause
1878 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001879 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001880 * @return object
1881 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001882 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001883 {
1884 // Combine any cached components with the current statements
1885 $this->_merge_cache();
1886
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001887 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001888 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001889 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001890 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001891 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001892 }
1893
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001894 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001895 }
1896 elseif (is_array($table))
1897 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001898 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001899 {
1900 $this->delete($single_table, $where, $limit, FALSE);
1901 }
1902
1903 $this->_reset_write();
1904 return;
1905 }
1906 else
1907 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001908 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001909 }
1910
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001911 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001912 {
1913 $this->where($where);
1914 }
1915
Andrey Andreev650b4c02012-06-11 12:07:15 +03001916 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001917 {
1918 $this->limit($limit);
1919 }
1920
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001921 if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001922 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001923 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001924 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001925
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001926 $sql = $this->_delete($table, $this->qb_where, $this->qb_like, $this->qb_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001927 if ($reset_data)
1928 {
1929 $this->_reset_write();
1930 }
WanWizard7219c072011-12-28 14:09:05 +01001931
Andrey Andreev24276a32012-01-08 02:44:38 +02001932 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001933 }
WanWizard7219c072011-12-28 14:09:05 +01001934
Derek Allard2067d1a2008-11-13 22:59:24 +00001935 // --------------------------------------------------------------------
1936
1937 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001938 * Delete statement
1939 *
1940 * Generates a platform-specific delete string from the supplied data
1941 *
1942 * @param string the table name
1943 * @param array the where clause
1944 * @param array the like clause
1945 * @param string the limit clause
1946 * @return string
1947 */
1948 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
1949 {
1950 $conditions = array();
1951
1952 empty($where) OR $conditions[] = implode(' ', $where);
1953 empty($like) OR $conditions[] = implode(' ', $like);
1954
1955 return 'DELETE FROM '.$table
1956 .(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '')
Andrey Andreev650b4c02012-06-11 12:07:15 +03001957 .($limit ? ' LIMIT '.(int) $limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001958 }
1959
1960 // --------------------------------------------------------------------
1961
1962 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001963 * DB Prefix
1964 *
1965 * Prepends a database prefix if one exists in configuration
1966 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001967 * @param string the table
1968 * @return string
1969 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001970 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001971 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001972 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001973 {
1974 $this->display_error('db_table_name_required');
1975 }
1976
1977 return $this->dbprefix.$table;
1978 }
1979
1980 // --------------------------------------------------------------------
1981
1982 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001983 * Set DB Prefix
1984 *
1985 * Set's the DB Prefix to something new without needing to reconnect
1986 *
1987 * @param string the prefix
1988 * @return string
1989 */
1990 public function set_dbprefix($prefix = '')
1991 {
1992 return $this->dbprefix = $prefix;
1993 }
1994
1995 // --------------------------------------------------------------------
1996
1997 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001998 * Track Aliases
1999 *
2000 * Used to track SQL statements written with aliased tables.
2001 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002002 * @param string The table to inspect
2003 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002004 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002005 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002006 {
2007 if (is_array($table))
2008 {
2009 foreach ($table as $t)
2010 {
2011 $this->_track_aliases($t);
2012 }
2013 return;
2014 }
Barry Mienydd671972010-10-04 16:33:58 +02002015
Derek Jones37f4b9c2011-07-01 17:56:50 -05002016 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002017 // the string into discreet statements
2018 if (strpos($table, ',') !== FALSE)
2019 {
2020 return $this->_track_aliases(explode(',', $table));
2021 }
Barry Mienydd671972010-10-04 16:33:58 +02002022
Derek Allard2067d1a2008-11-13 22:59:24 +00002023 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002024 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002025 {
2026 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002027 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002028
Derek Allard2067d1a2008-11-13 22:59:24 +00002029 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002030 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002031
Derek Allard2067d1a2008-11-13 22:59:24 +00002032 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002033 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002034 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002035 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002036 }
2037 }
2038 }
WanWizard7219c072011-12-28 14:09:05 +01002039
Derek Allard2067d1a2008-11-13 22:59:24 +00002040 // --------------------------------------------------------------------
2041
2042 /**
2043 * Compile the SELECT statement
2044 *
2045 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05002046 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00002047 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002048 * @return string
2049 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002050 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002051 {
2052 // Combine any cached components with the current statements
2053 $this->_merge_cache();
2054
Derek Allard2067d1a2008-11-13 22:59:24 +00002055 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002056 if ($select_override !== FALSE)
2057 {
2058 $sql = $select_override;
2059 }
2060 else
2061 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002062 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002063
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002064 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002065 {
Barry Mienydd671972010-10-04 16:33:58 +02002066 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002067 }
2068 else
Barry Mienydd671972010-10-04 16:33:58 +02002069 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002070 // Cycle through the "select" portion of the query and prep each column name.
2071 // The reason we protect identifiers here rather then in the select() function
2072 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002073 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002074 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002075 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002076 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002077 }
Barry Mienydd671972010-10-04 16:33:58 +02002078
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002079 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002080 }
2081 }
2082
Derek Allard2067d1a2008-11-13 22:59:24 +00002083 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002084 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002085 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002086 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002087 }
2088
Derek Allard2067d1a2008-11-13 22:59:24 +00002089 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002090 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002091 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002092 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002093 }
2094
Derek Allard2067d1a2008-11-13 22:59:24 +00002095 // Write the "WHERE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002096 if (count($this->qb_where) > 0 OR count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002097 {
Greg Akere156c6e2011-04-20 16:03:04 -05002098 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00002099 }
2100
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002101 $sql .= implode("\n", $this->qb_where);
Derek Allard2067d1a2008-11-13 22:59:24 +00002102
Derek Allard2067d1a2008-11-13 22:59:24 +00002103 // Write the "LIKE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002104 if (count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002105 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002106 if (count($this->qb_where) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002107 {
2108 $sql .= "\nAND ";
2109 }
2110
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002111 $sql .= implode("\n", $this->qb_like);
Derek Allard2067d1a2008-11-13 22:59:24 +00002112 }
2113
Derek Allard2067d1a2008-11-13 22:59:24 +00002114 // Write the "GROUP BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002115 if (count($this->qb_groupby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002116 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002117 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002118 }
2119
Derek Allard2067d1a2008-11-13 22:59:24 +00002120 // Write the "HAVING" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002121 if (count($this->qb_having) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002122 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002123 $sql .= "\nHAVING ".implode("\n", $this->qb_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00002124 }
2125
Derek Allard2067d1a2008-11-13 22:59:24 +00002126 // Write the "ORDER BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002127 if (count($this->qb_orderby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002128 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002129 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002130 }
2131
Derek Allard2067d1a2008-11-13 22:59:24 +00002132 // Write the "LIMIT" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002133 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002134 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002135 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00002136 }
2137
2138 return $sql;
2139 }
2140
2141 // --------------------------------------------------------------------
2142
2143 /**
2144 * Object to Array
2145 *
2146 * Takes an object as input and converts the class variables to array key/vals
2147 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002148 * @param object
2149 * @return array
2150 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002151 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002152 {
2153 if ( ! is_object($object))
2154 {
2155 return $object;
2156 }
Barry Mienydd671972010-10-04 16:33:58 +02002157
Derek Allard2067d1a2008-11-13 22:59:24 +00002158 $array = array();
2159 foreach (get_object_vars($object) as $key => $val)
2160 {
2161 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002162 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002163 {
2164 $array[$key] = $val;
2165 }
2166 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002167
2168 return $array;
2169 }
Barry Mienydd671972010-10-04 16:33:58 +02002170
Derek Jonesd10e8962010-03-02 17:10:36 -06002171 // --------------------------------------------------------------------
2172
2173 /**
2174 * Object to Array
2175 *
2176 * Takes an object as input and converts the class variables to array key/vals
2177 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002178 * @param object
2179 * @return array
2180 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002181 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002182 {
2183 if ( ! is_object($object))
2184 {
2185 return $object;
2186 }
Barry Mienydd671972010-10-04 16:33:58 +02002187
Derek Jonesd10e8962010-03-02 17:10:36 -06002188 $array = array();
2189 $out = get_object_vars($object);
2190 $fields = array_keys($out);
2191
2192 foreach ($fields as $val)
2193 {
2194 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002195 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002196 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002197 $i = 0;
2198 foreach ($out[$val] as $data)
2199 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002200 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002201 }
2202 }
2203 }
2204
Derek Allard2067d1a2008-11-13 22:59:24 +00002205 return $array;
2206 }
Barry Mienydd671972010-10-04 16:33:58 +02002207
Derek Allard2067d1a2008-11-13 22:59:24 +00002208 // --------------------------------------------------------------------
2209
2210 /**
2211 * Start Cache
2212 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002213 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002214 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002215 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002216 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002217 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002218 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002219 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002220 }
2221
2222 // --------------------------------------------------------------------
2223
2224 /**
2225 * Stop Cache
2226 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002227 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002228 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002229 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002230 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002231 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002232 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002233 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002234 }
2235
2236 // --------------------------------------------------------------------
2237
2238 /**
2239 * Flush Cache
2240 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002241 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002242 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002243 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002244 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002245 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002246 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002247 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002248 'qb_cache_select' => array(),
2249 'qb_cache_from' => array(),
2250 'qb_cache_join' => array(),
2251 'qb_cache_where' => array(),
2252 'qb_cache_like' => array(),
2253 'qb_cache_groupby' => array(),
2254 'qb_cache_having' => array(),
2255 'qb_cache_orderby' => array(),
2256 'qb_cache_set' => array(),
2257 'qb_cache_exists' => array(),
2258 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002259 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002260 }
2261
2262 // --------------------------------------------------------------------
2263
2264 /**
2265 * Merge Cache
2266 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002267 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002268 * locally called ones.
2269 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002270 * @return void
2271 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002272 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002273 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002274 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002275 {
2276 return;
2277 }
2278
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002279 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002280 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002281 $qb_variable = 'qb_'.$val;
2282 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002283
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002284 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002285 {
2286 continue;
2287 }
2288
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002289 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002290 }
2291
2292 // If we are "protecting identifiers" we need to examine the "from"
2293 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002294 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002295 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002296 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002297 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002298
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002299 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002300 }
WanWizard7219c072011-12-28 14:09:05 +01002301
Kyle Farris0c147b32011-08-26 02:29:31 -04002302 // --------------------------------------------------------------------
2303
2304 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002305 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002306 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002307 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002308 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002309 * @return void
2310 */
2311 public function reset_query()
2312 {
2313 $this->_reset_select();
2314 $this->_reset_write();
2315 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002316
2317 // --------------------------------------------------------------------
2318
2319 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002320 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002321 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002322 * @param array An array of fields to reset
2323 * @return void
2324 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002325 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002326 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002327 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002328 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002329 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002330 {
2331 $this->$item = $default_value;
2332 }
2333 }
2334 }
2335
2336 // --------------------------------------------------------------------
2337
2338 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002339 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002340 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002341 * @return void
2342 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002343 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002344 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002345 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002346 'qb_select' => array(),
2347 'qb_from' => array(),
2348 'qb_join' => array(),
2349 'qb_where' => array(),
2350 'qb_like' => array(),
2351 'qb_groupby' => array(),
2352 'qb_having' => array(),
2353 'qb_orderby' => array(),
2354 'qb_wherein' => array(),
2355 'qb_aliased_tables' => array(),
2356 'qb_no_escape' => array(),
2357 'qb_distinct' => FALSE,
2358 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002359 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002360 )
2361 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002362 }
Barry Mienydd671972010-10-04 16:33:58 +02002363
Derek Allard2067d1a2008-11-13 22:59:24 +00002364 // --------------------------------------------------------------------
2365
2366 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002367 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002368 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002369 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002370 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002371 * @return void
2372 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002373 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002374 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002375 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002376 'qb_set' => array(),
2377 'qb_from' => array(),
2378 'qb_where' => array(),
2379 'qb_like' => array(),
2380 'qb_orderby' => array(),
2381 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002382 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002383 )
2384 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002385 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002386
Derek Allard2067d1a2008-11-13 22:59:24 +00002387}
2388
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002389/* End of file DB_query_builder.php */
WanWizardbc69f362012-06-22 00:10:11 +02002390/* Location: ./system/database/DB_query_builder.php */