blob: 85dd77da9f31bcd30fd279c8349b50d974e48584 [file] [log] [blame]
Andrey Andreev24276a32012-01-08 02:44:38 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
WanWizard7219c072011-12-28 14:09:05 +01008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
WanWizard7219c072011-12-28 14:09:05 +010010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
Jamie Rumbelow7efad202012-02-19 12:37:00 +000029 * Query Builder Class
Derek Allard2067d1a2008-11-13 22:59:24 +000030 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +000031 * This is the platform-independent base Query Builder implementation class.
Derek Allard2067d1a2008-11-13 22:59:24 +000032 *
33 * @package CodeIgniter
34 * @subpackage Drivers
35 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/database/
38 */
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +010039
40abstract class CI_DB_query_builder extends CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000041
WanWizard7219c072011-12-28 14:09:05 +010042 protected $return_delete_sql = FALSE;
43 protected $reset_delete_data = FALSE;
44
Jamie Rumbelow7efad202012-02-19 12:37:00 +000045 protected $qb_select = array();
46 protected $qb_distinct = FALSE;
47 protected $qb_from = array();
48 protected $qb_join = array();
49 protected $qb_where = array();
50 protected $qb_like = array();
51 protected $qb_groupby = array();
52 protected $qb_having = array();
53 protected $qb_keys = array();
54 protected $qb_limit = FALSE;
55 protected $qb_offset = FALSE;
Jamie Rumbelow7efad202012-02-19 12:37:00 +000056 protected $qb_orderby = array();
57 protected $qb_set = array();
58 protected $qb_wherein = array();
59 protected $qb_aliased_tables = array();
60 protected $qb_store_array = array();
61 protected $qb_where_group_started = FALSE;
62 protected $qb_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +020063
Jamie Rumbelow7efad202012-02-19 12:37:00 +000064 // Query Builder Caching variables
65 protected $qb_caching = FALSE;
66 protected $qb_cache_exists = array();
67 protected $qb_cache_select = array();
68 protected $qb_cache_from = array();
69 protected $qb_cache_join = array();
70 protected $qb_cache_where = array();
71 protected $qb_cache_like = array();
72 protected $qb_cache_groupby = array();
73 protected $qb_cache_having = array();
74 protected $qb_cache_orderby = array();
75 protected $qb_cache_set = array();
WanWizard7219c072011-12-28 14:09:05 +010076
Jamie Rumbelow7efad202012-02-19 12:37:00 +000077 protected $qb_no_escape = array();
78 protected $qb_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000079
80 /**
81 * Select
82 *
83 * Generates the SELECT portion of the query
84 *
Derek Allard2067d1a2008-11-13 22:59:24 +000085 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +030086 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +000087 * @return object
88 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060089 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000090 {
Derek Allard2067d1a2008-11-13 22:59:24 +000091 if (is_string($select))
92 {
93 $select = explode(',', $select);
94 }
95
Andrey Andreev42870232012-06-12 01:30:20 +030096 // If the escape value was not set will will base it on the global setting
97 is_bool($escape) OR $escape = $this->_protect_identifiers;
98
Derek Allard2067d1a2008-11-13 22:59:24 +000099 foreach ($select as $val)
100 {
101 $val = trim($val);
102
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100103 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000105 $this->qb_select[] = $val;
106 $this->qb_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000107
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000108 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000110 $this->qb_cache_select[] = $val;
111 $this->qb_cache_exists[] = 'select';
112 $this->qb_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
114 }
115 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 return $this;
118 }
119
120 // --------------------------------------------------------------------
121
122 /**
123 * Select Max
124 *
125 * Generates a SELECT MAX(field) portion of a query
126 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 * @param string the field
128 * @param string an alias
129 * @return object
130 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600131 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 {
133 return $this->_max_min_avg_sum($select, $alias, 'MAX');
134 }
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 // --------------------------------------------------------------------
137
138 /**
139 * Select Min
140 *
141 * Generates a SELECT MIN(field) portion of a query
142 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 * @param string the field
144 * @param string an alias
145 * @return object
146 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600147 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 {
149 return $this->_max_min_avg_sum($select, $alias, 'MIN');
150 }
151
152 // --------------------------------------------------------------------
153
154 /**
155 * Select Average
156 *
157 * Generates a SELECT AVG(field) portion of a query
158 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 * @param string the field
160 * @param string an alias
161 * @return object
162 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600163 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
165 return $this->_max_min_avg_sum($select, $alias, 'AVG');
166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Select Sum
172 *
173 * Generates a SELECT SUM(field) portion of a query
174 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 * @param string the field
176 * @param string an alias
177 * @return object
178 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600179 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
181 return $this->_max_min_avg_sum($select, $alias, 'SUM');
182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Processing Function for the four functions above:
188 *
189 * select_max()
190 * select_min()
191 * select_avg()
Andrey Andreev24276a32012-01-08 02:44:38 +0200192 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200193 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 * @param string the field
195 * @param string an alias
196 * @return object
197 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600198 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100200 if ( ! is_string($select) OR $select === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
202 $this->display_error('db_invalid_query');
203 }
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
208 {
209 show_error('Invalid function type: '.$type);
210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100212 if ($alias === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
214 $alias = $this->_create_alias_from_table(trim($select));
215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300217 $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->escape_identifiers(trim($alias));
218
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000219 $this->qb_select[] = $sql;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100220 $this->qb_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200221
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000222 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000224 $this->qb_cache_select[] = $sql;
225 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 return $this;
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Determines the alias name based on the table
235 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 * @param string
237 * @return string
238 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600239 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
241 if (strpos($item, '.') !== FALSE)
242 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200243 $item = explode('.', $item);
244 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 return $item;
248 }
249
250 // --------------------------------------------------------------------
251
252 /**
253 * DISTINCT
254 *
255 * Sets a flag which tells the query string compiler to add DISTINCT
256 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 * @param bool
258 * @return object
259 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600260 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300262 $this->qb_distinct = is_bool($val) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 return $this;
264 }
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 // --------------------------------------------------------------------
267
268 /**
269 * From
270 *
271 * Generates the FROM portion of the query
272 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * @param mixed can be a string or array
274 * @return object
275 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600276 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300278 foreach ((array) $from as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 if (strpos($val, ',') !== FALSE)
281 {
282 foreach (explode(',', $val) as $v)
283 {
284 $v = trim($v);
285 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200286
George Petsagourakis193d4482012-04-28 11:16:18 +0300287 $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000288
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000289 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000291 $this->qb_cache_from[] = $v;
292 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200293 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
296 else
297 {
298 $val = trim($val);
299
Andrey Andreev24276a32012-01-08 02:44:38 +0200300 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000301 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200303
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000304 $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000305
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000306 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000308 $this->qb_cache_from[] = $val;
309 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 }
311 }
312 }
313
314 return $this;
315 }
316
317 // --------------------------------------------------------------------
318
319 /**
320 * Join
321 *
322 * Generates the JOIN portion of the query
323 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * @param string
325 * @param string the join condition
326 * @param string the type of join
Andrey Andreev42870232012-06-12 01:30:20 +0300327 * @param string wether not to try to escape identifiers
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 * @return object
329 */
Andrey Andreev42870232012-06-12 01:30:20 +0300330 public function join($table, $cond, $type = '', $escape = TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200331 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100332 if ($type !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 $type = strtoupper(trim($type));
335
Andrey Andreev42870232012-06-12 01:30:20 +0300336 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 $type = '';
339 }
340 else
341 {
342 $type .= ' ';
343 }
344 }
345
Andrey Andreev24276a32012-01-08 02:44:38 +0200346 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000347 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 $this->_track_aliases($table);
349
Andrey Andreev42870232012-06-12 01:30:20 +0300350 // Split multiple conditions
351 if ($escape === TRUE && preg_match_all('/\sAND\s|\sOR\s/i', $cond, $m, PREG_SET_ORDER | PREG_OFFSET_CAPTURE))
352 {
353 $newcond = '';
354 $m[0][] = array('', strlen($cond));
355
356 for ($i = 0, $c = count($m[0]), $s = 0;
357 $i < $c;
358 $s += $m[0][$i][1] + strlen($m[0][$i][0]), $i++)
359 {
360 $temp = substr($cond, $s, $m[0][$i][1]);
361
362 $newcond .= preg_match('/([\[\w\.-]+)([\W\s]+)(.+)/i', $temp, $match)
363 ? $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3])
364 : $temp;
365
366 $newcond .= $m[0][$i][0];
367 }
368
369 $cond = $newcond;
370 }
371 // Split apart the condition and protect the identifiers
372 elseif ($escape === TRUE && preg_match('/([\[\w\.-]+)([\W\s]+)(.+)/i', $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200374 $cond = $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
Andrey Andreev42870232012-06-12 01:30:20 +0300377 // Do we want to escape the table name?
378 if ($escape === TRUE)
379 {
380 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
381 }
382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 // Assemble the JOIN statement
Andrey Andreev8295c842012-06-15 03:42:25 +0300384 $this->qb_join[] = $join = $type.'JOIN '.$table.' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000385
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000386 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000388 $this->qb_cache_join[] = $join;
389 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 }
391
392 return $this;
393 }
394
395 // --------------------------------------------------------------------
396
397 /**
398 * Where
399 *
400 * Generates the WHERE portion of the query. Separates
401 * multiple calls with AND
402 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 * @param mixed
404 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300405 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 * @return object
407 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300408 public function where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
410 return $this->_where($key, $value, 'AND ', $escape);
411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 // --------------------------------------------------------------------
414
415 /**
416 * OR Where
417 *
418 * Generates the WHERE portion of the query. Separates
419 * multiple calls with OR
420 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 * @param mixed
422 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300423 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 * @return object
425 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300426 public function or_where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
428 return $this->_where($key, $value, 'OR ', $escape);
429 }
430
431 // --------------------------------------------------------------------
432
433 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 * Where
435 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600436 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 * @param mixed
439 * @param mixed
440 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300441 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 * @return object
443 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600444 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 {
WanWizard7219c072011-12-28 14:09:05 +0100446 $type = $this->_group_get_type($type);
447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 if ( ! is_array($key))
449 {
450 $key = array($key => $value);
451 }
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 // If the escape value was not set will will base it on the global setting
Andrey Andreeve10fb792012-06-15 12:07:04 +0300454 is_bool($escape) OR $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000455
456 foreach ($key as $k => $v)
457 {
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100458 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000459
Andrey Andreev9c14f652012-06-10 14:35:07 +0300460 $k = $this->_has_operator($k)
Andrey Andreev392b6ad2012-06-10 15:08:59 +0300461 ? $this->protect_identifiers(substr($k, 0, strpos(rtrim($k), ' ')), FALSE, $escape).strchr(rtrim($k), ' ')
Andrey Andreev9c14f652012-06-10 14:35:07 +0300462 : $this->protect_identifiers($k, FALSE, $escape);
463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 if (is_null($v) && ! $this->_has_operator($k))
465 {
466 // value appears not to have been set, assign the test to IS NULL
467 $k .= ' IS NULL';
468 }
Barry Mienydd671972010-10-04 16:33:58 +0200469
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 if ( ! is_null($v))
471 {
472 if ($escape === TRUE)
473 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 $v = ' '.$this->escape($v);
475 }
WanWizard7219c072011-12-28 14:09:05 +0100476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 if ( ! $this->_has_operator($k))
478 {
Greg Akere156c6e2011-04-20 16:03:04 -0500479 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 }
481 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000482
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000483 $this->qb_where[] = $prefix.$k.$v;
484 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000486 $this->qb_cache_where[] = $prefix.$k.$v;
487 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 }
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 return $this;
493 }
494
495 // --------------------------------------------------------------------
496
497 /**
498 * Where_in
499 *
500 * Generates a WHERE field IN ('item', 'item') SQL query joined with
501 * AND if appropriate
502 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 * @param string The field to search
504 * @param array The values searched on
505 * @return object
506 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300507 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300509 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 }
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 // --------------------------------------------------------------------
513
514 /**
515 * Where_in_or
516 *
517 * Generates a WHERE field IN ('item', 'item') SQL query joined with
518 * OR if appropriate
519 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 * @param string The field to search
521 * @param array The values searched on
522 * @return object
523 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300524 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300526 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 }
528
529 // --------------------------------------------------------------------
530
531 /**
532 * Where_not_in
533 *
534 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
535 * with AND if appropriate
536 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 * @param string The field to search
538 * @param array The values searched on
539 * @return object
540 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300541 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300543 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 }
Barry Mienydd671972010-10-04 16:33:58 +0200545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 // --------------------------------------------------------------------
547
548 /**
549 * Where_not_in_or
550 *
551 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
552 * with OR if appropriate
553 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 * @param string The field to search
555 * @param array The values searched on
556 * @return object
557 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300558 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300560 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 }
562
563 // --------------------------------------------------------------------
564
565 /**
566 * Where_in
567 *
568 * Called by where_in, where_in_or, where_not_in, where_not_in_or
569 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 * @param string The field to search
571 * @param array The values searched on
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300572 * @param bool If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200573 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 * @return object
575 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300576 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 {
578 if ($key === NULL OR $values === NULL)
579 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300580 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 }
Barry Mienydd671972010-10-04 16:33:58 +0200582
WanWizard7219c072011-12-28 14:09:05 +0100583 $type = $this->_group_get_type($type);
584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 if ( ! is_array($values))
586 {
587 $values = array($values);
588 }
Barry Mienydd671972010-10-04 16:33:58 +0200589
Andrey Andreev498c1e02012-06-16 03:34:10 +0300590 is_bool($escape) OR $escape = $this->_protect_identifiers;
591
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 $not = ($not) ? ' NOT' : '';
593
594 foreach ($values as $value)
595 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000596 $this->qb_wherein[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 }
598
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000599 $prefix = (count($this->qb_where) === 0) ? '' : $type;
Andrey Andreev498c1e02012-06-16 03:34:10 +0300600 $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 +0200601
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000602 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000604 $this->qb_cache_where[] = $where_in;
605 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 }
607
608 // reset the array for multiple calls
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000609 $this->qb_wherein = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 return $this;
611 }
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 // --------------------------------------------------------------------
614
615 /**
616 * Like
617 *
618 * Generates a %LIKE% portion of the query. Separates
619 * multiple calls with AND
620 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 * @param mixed
622 * @param mixed
623 * @return object
624 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600625 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 {
627 return $this->_like($field, $match, 'AND ', $side);
628 }
629
630 // --------------------------------------------------------------------
631
632 /**
633 * Not Like
634 *
635 * Generates a NOT LIKE portion of the query. Separates
636 * multiple calls with AND
637 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 * @param mixed
639 * @param mixed
640 * @return object
641 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600642 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
644 return $this->_like($field, $match, 'AND ', $side, 'NOT');
645 }
Barry Mienydd671972010-10-04 16:33:58 +0200646
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 // --------------------------------------------------------------------
648
649 /**
650 * OR Like
651 *
652 * Generates a %LIKE% portion of the query. Separates
653 * multiple calls with OR
654 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 * @param mixed
656 * @param mixed
657 * @return object
658 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600659 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 {
661 return $this->_like($field, $match, 'OR ', $side);
662 }
663
664 // --------------------------------------------------------------------
665
666 /**
667 * OR Not Like
668 *
669 * Generates a NOT LIKE portion of the query. Separates
670 * multiple calls with OR
671 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 * @param mixed
673 * @param mixed
674 * @return object
675 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600676 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 {
678 return $this->_like($field, $match, 'OR ', $side, 'NOT');
679 }
Barry Mienydd671972010-10-04 16:33:58 +0200680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 // --------------------------------------------------------------------
682
683 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 * Like
685 *
686 * Called by like() or orlike()
687 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 * @param mixed
689 * @param mixed
690 * @param string
691 * @return object
692 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600693 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 {
WanWizard7219c072011-12-28 14:09:05 +0100695 $type = $this->_group_get_type($type);
696
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 if ( ! is_array($field))
698 {
699 $field = array($field => $match);
700 }
Barry Mienydd671972010-10-04 16:33:58 +0200701
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 foreach ($field as $k => $v)
703 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000704 $k = $this->protect_identifiers($k);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000705 $prefix = (count($this->qb_like) === 0) ? '' : $type;
Derek Jonese4ed5832009-02-20 21:44:59 +0000706 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300707
Andrey Andreev24276a32012-01-08 02:44:38 +0200708 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400709 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100710 $like_statement = "{$prefix} $k $not LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400711 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200712 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100714 $like_statement = "{$prefix} $k $not LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200716 elseif ($side === 'after')
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 }
720 else
721 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100722 $like_statement = "{$prefix} $k $not LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600724
Derek Jonese4ed5832009-02-20 21:44:59 +0000725 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100726 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000727 {
Greg Aker0d424892010-01-26 02:14:44 +0000728 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000729 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600730
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000731 $this->qb_like[] = $like_statement;
732 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000734 $this->qb_cache_like[] = $like_statement;
735 $this->qb_cache_exists[] = 'like';
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 }
Barry Mienydd671972010-10-04 16:33:58 +0200737
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200739
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 return $this;
741 }
Barry Mienydd671972010-10-04 16:33:58 +0200742
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 // --------------------------------------------------------------------
744
745 /**
WanWizard7219c072011-12-28 14:09:05 +0100746 * Starts a query group.
747 *
748 * @param string (Internal use only)
749 * @param string (Internal use only)
750 * @return object
751 */
752 public function group_start($not = '', $type = 'AND ')
753 {
754 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100755
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000756 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100757 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000758 $this->qb_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (';
WanWizard7219c072011-12-28 14:09:05 +0100759
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000760 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100761 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000762 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100763 }
764
765 return $this;
766 }
767
768 // --------------------------------------------------------------------
769
770 /**
771 * Starts a query group, but ORs the group
772 *
773 * @return object
774 */
775 public function or_group_start()
776 {
777 return $this->group_start('', 'OR ');
778 }
779
780 // --------------------------------------------------------------------
781
782 /**
783 * Starts a query group, but NOTs the group
784 *
785 * @return object
786 */
787 public function not_group_start()
788 {
789 return $this->group_start('NOT ', 'AND ');
790 }
791
792 // --------------------------------------------------------------------
793
794 /**
795 * Starts a query group, but OR NOTs the group
796 *
797 * @return object
798 */
799 public function or_not_group_start()
800 {
801 return $this->group_start('NOT ', 'OR ');
802 }
803
804 // --------------------------------------------------------------------
805
806 /**
807 * Ends a query group
808 *
809 * @return object
810 */
811 public function group_end()
812 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000813 $this->qb_where_group_started = FALSE;
814 $this->qb_where[] = $value = str_repeat(' ', $this->qb_where_group_count--) . ')';
WanWizard7219c072011-12-28 14:09:05 +0100815
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000816 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100817 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000818 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100819 }
820
WanWizard7219c072011-12-28 14:09:05 +0100821 return $this;
822 }
823
824 // --------------------------------------------------------------------
825
826 /**
827 * Group_get_type
828 *
829 * Called by group_start(), _like(), _where() and _where_in()
830 *
831 * @param string
832 * @return string
833 */
834 protected function _group_get_type($type)
835 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000836 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100837 {
838 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000839 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100840 }
841
842 return $type;
843 }
844
845 // --------------------------------------------------------------------
846
847 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 * GROUP BY
849 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 * @param string
851 * @return object
852 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600853 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 {
855 if (is_string($by))
856 {
857 $by = explode(',', $by);
858 }
Barry Mienydd671972010-10-04 16:33:58 +0200859
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 foreach ($by as $val)
861 {
862 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200863
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100864 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000866 $this->qb_groupby[] = $val = $this->protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200867
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000868 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000870 $this->qb_cache_groupby[] = $val;
871 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 }
873 }
874 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200875
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 return $this;
877 }
878
879 // --------------------------------------------------------------------
880
881 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 * Sets the HAVING value
883 *
884 * Separates multiple calls with AND
885 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 * @param string
887 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300888 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 * @return object
890 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600891 public function having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 {
893 return $this->_having($key, $value, 'AND ', $escape);
894 }
Barry Mienydd671972010-10-04 16:33:58 +0200895
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 // --------------------------------------------------------------------
897
898 /**
899 * Sets the OR HAVING value
900 *
901 * Separates multiple calls with OR
902 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 * @param string
904 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300905 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 * @return object
907 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600908 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 {
910 return $this->_having($key, $value, 'OR ', $escape);
911 }
Barry Mienydd671972010-10-04 16:33:58 +0200912
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 // --------------------------------------------------------------------
914
915 /**
916 * Sets the HAVING values
917 *
918 * Called by having() or or_having()
919 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 * @param string
921 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300922 * @param string
923 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 * @return object
925 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600926 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 {
928 if ( ! is_array($key))
929 {
930 $key = array($key => $value);
931 }
Barry Mienydd671972010-10-04 16:33:58 +0200932
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 foreach ($key as $k => $v)
934 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000935 $prefix = (count($this->qb_having) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000936
Andrey Andreev974c75b2012-06-15 12:30:02 +0300937 $k = $this->_has_operator($k)
938 ? $this->protect_identifiers(substr($k, 0, strpos(rtrim($k), ' ')), FALSE, $escape).strchr(rtrim($k), ' ')
939 : $this->protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000940
941 if ( ! $this->_has_operator($k))
942 {
943 $k .= ' = ';
944 }
945
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100946 if ($v !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400948 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 }
Barry Mienydd671972010-10-04 16:33:58 +0200950
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000951 $this->qb_having[] = $prefix.$k.$v;
952 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000954 $this->qb_cache_having[] = $prefix.$k.$v;
955 $this->qb_cache_exists[] = 'having';
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 }
957 }
Barry Mienydd671972010-10-04 16:33:58 +0200958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 return $this;
960 }
Barry Mienydd671972010-10-04 16:33:58 +0200961
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 // --------------------------------------------------------------------
963
964 /**
965 * Sets the ORDER BY value
966 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 * @param string
968 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100969 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 * @return object
971 */
Andrey Andreevd24160c2012-06-16 03:21:20 +0300972 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200974 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 {
976 $orderby = ''; // Random results want or don't need a field name
977 $direction = $this->_random_keyword;
978 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100979 elseif (trim($direction) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 {
Andrey Andreev650b4c02012-06-11 12:07:15 +0300981 $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : ' ASC';
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 }
Barry Mienydd671972010-10-04 16:33:58 +0200983
Andrey Andreevd24160c2012-06-16 03:21:20 +0300984 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +0200985
Andrey Andreevd24160c2012-06-16 03:21:20 +0300986 if ($escape === TRUE && strpos($orderby, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 {
988 $temp = array();
989 foreach (explode(',', $orderby) as $part)
990 {
991 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000992 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 {
Andrey Andreev88cb2782012-06-11 20:40:50 +0300994 $part = preg_match('/^(.+)\s+(ASC|DESC)$/i', $part, $matches)
995 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
996 : $this->protect_identifiers($part);
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 }
Barry Mienydd671972010-10-04 16:33:58 +0200998
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 $temp[] = $part;
1000 }
Barry Mienydd671972010-10-04 16:33:58 +02001001
1002 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001004 elseif ($direction !== $this->_random_keyword && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 {
Andrey Andreev1d1d7ff2012-06-11 22:35:35 +03001006 $orderby = preg_match('/^(.+)\s+(ASC|DESC)$/i', $orderby, $matches)
Andrey Andreev88cb2782012-06-11 20:40:50 +03001007 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
1008 : $this->protect_identifiers($orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 }
Barry Mienydd671972010-10-04 16:33:58 +02001010
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001011 $this->qb_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +02001012
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001013 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001015 $this->qb_cache_orderby[] = $orderby_statement;
1016 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 }
1018
1019 return $this;
1020 }
Barry Mienydd671972010-10-04 16:33:58 +02001021
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 // --------------------------------------------------------------------
1023
1024 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 * Sets the LIMIT value
1026 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001027 * @param int the limit value
1028 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 * @return object
1030 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +02001031 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001033 $this->qb_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +00001034
Andrey Andreev650b4c02012-06-11 12:07:15 +03001035 if ( ! empty($offset))
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001037 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 }
Barry Mienydd671972010-10-04 16:33:58 +02001039
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 return $this;
1041 }
Barry Mienydd671972010-10-04 16:33:58 +02001042
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 // --------------------------------------------------------------------
1044
1045 /**
1046 * Sets the OFFSET value
1047 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001048 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 * @return object
1050 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001051 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001053 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 return $this;
1055 }
Barry Mienydd671972010-10-04 16:33:58 +02001056
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 // --------------------------------------------------------------------
1058
1059 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001060 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 * @param mixed
1063 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001064 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 * @return object
1066 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001067 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 {
1069 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001070
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 if ( ! is_array($key))
1072 {
1073 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001074 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001075
1076 foreach ($key as $k => $v)
1077 {
1078 if ($escape === FALSE)
1079 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001080 $this->qb_set[$this->protect_identifiers($k)] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 }
1082 else
1083 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001084 $this->qb_set[$this->protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 }
1086 }
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 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001291 public function set_insert_batch($key, $value = '', $escape = TRUE)
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
Iban Eguia3c0a4522012-04-15 13:30:44 +02001300 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001301 sort($keys);
1302
1303 foreach ($key as $row)
1304 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001305 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001306 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1307 {
1308 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001309 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001310 return;
1311 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001312
Barry Mienydd671972010-10-04 16:33:58 +02001313 ksort($row); // puts $row in the same order as our keys
1314
Andrey Andreev650b4c02012-06-11 12:07:15 +03001315 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001316 {
1317 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001318 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001319 {
Barry Mienydd671972010-10-04 16:33:58 +02001320 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001321 }
1322
Andrey Andreev650b4c02012-06-11 12:07:15 +03001323 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001324 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001325
1326 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001327 }
1328
1329 foreach ($keys as $k)
1330 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001331 $this->qb_keys[] = $this->protect_identifiers($k);
Derek Jonesd10e8962010-03-02 17:10:36 -06001332 }
Barry Mienydd671972010-10-04 16:33:58 +02001333
Derek Jonesd10e8962010-03-02 17:10:36 -06001334 return $this;
1335 }
WanWizard7219c072011-12-28 14:09:05 +01001336
Kyle Farris0c147b32011-08-26 02:29:31 -04001337 // --------------------------------------------------------------------
1338
1339 /**
1340 * Get INSERT query string
1341 *
1342 * Compiles an insert query and returns the sql
1343 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001344 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001345 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001346 * @return string
1347 */
1348 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001349 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001350 if ($this->_validate_insert($table) === FALSE)
1351 {
1352 return FALSE;
1353 }
WanWizard7219c072011-12-28 14:09:05 +01001354
Kyle Farris76116012011-08-31 11:17:48 -04001355 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001356 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001357 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001358 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001359 array_keys($this->qb_set),
1360 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001361 );
WanWizard7219c072011-12-28 14:09:05 +01001362
Kyle Farris0c147b32011-08-26 02:29:31 -04001363 if ($reset === TRUE)
1364 {
1365 $this->_reset_write();
1366 }
WanWizard7219c072011-12-28 14:09:05 +01001367
Kyle Farris0c147b32011-08-26 02:29:31 -04001368 return $sql;
1369 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001370
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 // --------------------------------------------------------------------
1372
1373 /**
1374 * Insert
1375 *
1376 * Compiles an insert string and runs the query
1377 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001378 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 * @param array an associative array of insert values
1380 * @return object
1381 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001382 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001383 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 if ( ! is_null($set))
1385 {
1386 $this->set($set);
1387 }
WanWizard7219c072011-12-28 14:09:05 +01001388
Kyle Farris0c147b32011-08-26 02:29:31 -04001389 if ($this->_validate_insert($table) === FALSE)
1390 {
1391 return FALSE;
1392 }
WanWizard7219c072011-12-28 14:09:05 +01001393
Kyle Farris76116012011-08-31 11:17:48 -04001394 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001395 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001396 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001397 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001398 array_keys($this->qb_set),
1399 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001400 );
Barry Mienydd671972010-10-04 16:33:58 +02001401
Kyle Farris0c147b32011-08-26 02:29:31 -04001402 $this->_reset_write();
1403 return $this->query($sql);
1404 }
WanWizard7219c072011-12-28 14:09:05 +01001405
Kyle Farris0c147b32011-08-26 02:29:31 -04001406 // --------------------------------------------------------------------
1407
1408 /**
Andrey Andreev65d537c2012-04-05 14:11:41 +03001409 * Insert statement
1410 *
1411 * Generates a platform-specific insert string from the supplied data
1412 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001413 * @param string the table name
1414 * @param array the insert keys
1415 * @param array the insert values
1416 * @return string
Andrey Andreev65d537c2012-04-05 14:11:41 +03001417 */
1418 protected function _insert($table, $keys, $values)
1419 {
1420 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1421 }
1422
1423 // --------------------------------------------------------------------
1424
1425 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001426 * Validate Insert
1427 *
1428 * This method is used by both insert() and get_compiled_insert() to
1429 * validate that the there data is actually being set and that table
1430 * has been chosen to be inserted into.
1431 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001432 * @param string the table to insert data into
1433 * @return string
1434 */
WanWizard7219c072011-12-28 14:09:05 +01001435 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001436 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001437 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001438 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001439 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001440 }
1441
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001442 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001443 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001444 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001445 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001446 elseif ( ! isset($this->qb_from[0]))
1447 {
1448 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1449 }
WanWizard7219c072011-12-28 14:09:05 +01001450
Kyle Farris0c147b32011-08-26 02:29:31 -04001451 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001452 }
Barry Mienydd671972010-10-04 16:33:58 +02001453
Phil Sturgeon9789f322011-07-15 15:14:05 -06001454 // --------------------------------------------------------------------
1455
1456 /**
1457 * Replace
1458 *
1459 * Compiles an replace into string and runs the query
1460 *
1461 * @param string the table to replace data into
1462 * @param array an associative array of insert values
1463 * @return object
1464 */
1465 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001466 {
1467 if ( ! is_null($set))
1468 {
1469 $this->set($set);
1470 }
Barry Mienydd671972010-10-04 16:33:58 +02001471
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001472 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001473 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001474 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001475 }
1476
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001477 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001478 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001479 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001480 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001481 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001482 }
Barry Mienydd671972010-10-04 16:33:58 +02001483
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001484 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001485 }
1486
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001487 $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 +00001488
Derek Jonesd10e8962010-03-02 17:10:36 -06001489 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001490 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001491 }
WanWizard7219c072011-12-28 14:09:05 +01001492
Kyle Farris0c147b32011-08-26 02:29:31 -04001493 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001494
Kyle Farris0c147b32011-08-26 02:29:31 -04001495 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001496 * Replace statement
1497 *
1498 * Generates a platform-specific replace string from the supplied data
1499 *
1500 * @param string the table name
1501 * @param array the insert keys
1502 * @param array the insert values
1503 * @return string
1504 */
1505 protected function _replace($table, $keys, $values)
1506 {
1507 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1508 }
1509
1510 // --------------------------------------------------------------------
1511
1512 /**
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001513 * From Tables
1514 *
1515 * This public function implicitly groups FROM tables so there is no confusion
1516 * about operator precedence in harmony with SQL standards
1517 *
1518 * @param array
1519 * @return string
1520 */
1521 protected function _from_tables($tables)
1522 {
1523 is_array($tables) OR $tables = array($tables);
1524
1525 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
1526 }
1527
1528 // --------------------------------------------------------------------
1529
1530 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001531 * Get UPDATE query string
1532 *
1533 * Compiles an update query and returns the sql
1534 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001535 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001536 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001537 * @return string
1538 */
1539 public function get_compiled_update($table = '', $reset = TRUE)
1540 {
1541 // Combine any cached components with the current statements
1542 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001543
Kyle Farris0c147b32011-08-26 02:29:31 -04001544 if ($this->_validate_update($table) === FALSE)
1545 {
1546 return FALSE;
1547 }
WanWizard7219c072011-12-28 14:09:05 +01001548
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001549 $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 +01001550
Kyle Farris0c147b32011-08-26 02:29:31 -04001551 if ($reset === TRUE)
1552 {
1553 $this->_reset_write();
1554 }
WanWizard7219c072011-12-28 14:09:05 +01001555
Kyle Farris0c147b32011-08-26 02:29:31 -04001556 return $sql;
1557 }
WanWizard7219c072011-12-28 14:09:05 +01001558
Derek Allard2067d1a2008-11-13 22:59:24 +00001559 // --------------------------------------------------------------------
1560
1561 /**
1562 * Update
1563 *
1564 * Compiles an update string and runs the query
1565 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001566 * @param string the table to retrieve the results from
1567 * @param array an associative array of update values
1568 * @param mixed the where clause
1569 * @return object
1570 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001571 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001572 {
1573 // Combine any cached components with the current statements
1574 $this->_merge_cache();
1575
1576 if ( ! is_null($set))
1577 {
1578 $this->set($set);
1579 }
Barry Mienydd671972010-10-04 16:33:58 +02001580
Kyle Farris0c147b32011-08-26 02:29:31 -04001581 if ($this->_validate_update($table) === FALSE)
1582 {
1583 return FALSE;
1584 }
1585
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001586 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001587 {
1588 $this->where($where);
1589 }
1590
Andrey Andreev650b4c02012-06-11 12:07:15 +03001591 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001592 {
1593 $this->limit($limit);
1594 }
1595
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001596 $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 +00001597
Kyle Farris0c147b32011-08-26 02:29:31 -04001598 $this->_reset_write();
1599 return $this->query($sql);
1600 }
WanWizard7219c072011-12-28 14:09:05 +01001601
Kyle Farris0c147b32011-08-26 02:29:31 -04001602 // --------------------------------------------------------------------
1603
1604 /**
Andrey Andreev975034d2012-04-05 15:09:55 +03001605 * Update statement
1606 *
1607 * Generates a platform-specific update string from the supplied data
1608 *
1609 * @param string the table name
1610 * @param array the update data
1611 * @param array the where clause
1612 * @param array the orderby clause
1613 * @param array the limit clause
Andrey Andreev00541ae2012-04-09 11:43:10 +03001614 * @param array the like clause
Andrey Andreev975034d2012-04-05 15:09:55 +03001615 * @return string
1616 */
Andrey Andreev00541ae2012-04-09 11:43:10 +03001617 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Andrey Andreev975034d2012-04-05 15:09:55 +03001618 {
1619 foreach ($values as $key => $val)
1620 {
1621 $valstr[] = $key.' = '.$val;
1622 }
1623
Andrey Andreev00541ae2012-04-09 11:43:10 +03001624 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
1625
1626 if ( ! empty($like))
1627 {
1628 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
1629 }
1630
Andrey Andreev975034d2012-04-05 15:09:55 +03001631 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev00541ae2012-04-09 11:43:10 +03001632 .$where
Andrey Andreev975034d2012-04-05 15:09:55 +03001633 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
1634 .($limit ? ' LIMIT '.$limit : '');
1635 }
1636
1637 // --------------------------------------------------------------------
1638
1639 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001640 * Validate Update
1641 *
1642 * This method is used by both update() and get_compiled_update() to
1643 * validate that data is actually being set and that a table has been
1644 * chosen to be update.
1645 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001646 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001647 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001648 */
1649 protected function _validate_update($table = '')
1650 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001651 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001652 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001653 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001654 }
1655
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001656 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001657 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001658 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001659 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001660 elseif ( ! isset($this->qb_from[0]))
1661 {
1662 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1663 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001664
1665 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001666 }
WanWizard7219c072011-12-28 14:09:05 +01001667
Derek Jonesd10e8962010-03-02 17:10:36 -06001668 // --------------------------------------------------------------------
1669
1670 /**
1671 * Update_Batch
1672 *
1673 * Compiles an update string and runs the query
1674 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001675 * @param string the table to retrieve the results from
1676 * @param array an associative array of update values
1677 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001678 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001679 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001680 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001681 {
1682 // Combine any cached components with the current statements
1683 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001684
Derek Jonesd10e8962010-03-02 17:10:36 -06001685 if (is_null($index))
1686 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001687 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001688 }
1689
1690 if ( ! is_null($set))
1691 {
1692 $this->set_update_batch($set, $index);
1693 }
1694
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001695 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001696 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001697 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001698 }
1699
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001700 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001701 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001702 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001703 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001704 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001705 }
Barry Mienydd671972010-10-04 16:33:58 +02001706
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001707 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001708 }
Barry Mienydd671972010-10-04 16:33:58 +02001709
Derek Jonesd10e8962010-03-02 17:10:36 -06001710 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001711 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001712 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001713 $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 -06001714 }
Barry Mienydd671972010-10-04 16:33:58 +02001715
Derek Jonesd10e8962010-03-02 17:10:36 -06001716 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001717 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001718 }
1719
1720 // --------------------------------------------------------------------
1721
1722 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001723 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001724 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001725 * @param array
1726 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001727 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001728 * @return object
1729 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001730 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001731 {
1732 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001733
Derek Jonesd10e8962010-03-02 17:10:36 -06001734 if ( ! is_array($key))
1735 {
1736 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001737 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001738
1739 foreach ($key as $k => $v)
1740 {
1741 $index_set = FALSE;
1742 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001743 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001744 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001745 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001746 {
1747 $index_set = TRUE;
1748 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001749
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001750 $clean[$this->protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001751 }
1752
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001753 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001754 {
1755 return $this->display_error('db_batch_missing_index');
1756 }
1757
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001758 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001759 }
Barry Mienydd671972010-10-04 16:33:58 +02001760
Derek Jonesd10e8962010-03-02 17:10:36 -06001761 return $this;
1762 }
1763
Derek Allard2067d1a2008-11-13 22:59:24 +00001764 // --------------------------------------------------------------------
1765
1766 /**
1767 * Empty Table
1768 *
1769 * Compiles a delete string and runs "DELETE FROM table"
1770 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001771 * @param string the table to empty
1772 * @return object
1773 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001774 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001775 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001776 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001777 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001778 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001779 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001780 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001781 }
1782
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001783 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001784 }
1785 else
1786 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001787 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 }
1789
1790 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001792 return $this->query($sql);
1793 }
1794
1795 // --------------------------------------------------------------------
1796
1797 /**
1798 * Truncate
1799 *
1800 * Compiles a truncate string and runs the query
1801 * If the database does not support the truncate() command
1802 * This function maps to "DELETE FROM table"
1803 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001804 * @param string the table to truncate
1805 * @return object
1806 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001807 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001808 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001809 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001810 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001811 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001812 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001813 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 }
1815
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001816 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001817 }
1818 else
1819 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001820 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001821 }
1822
1823 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001824 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001825 return $this->query($sql);
1826 }
WanWizard7219c072011-12-28 14:09:05 +01001827
Kyle Farris0c147b32011-08-26 02:29:31 -04001828 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001829
Kyle Farris0c147b32011-08-26 02:29:31 -04001830 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001831 * Truncate statement
1832 *
1833 * Generates a platform-specific truncate string from the supplied data
1834 *
1835 * If the database does not support the truncate() command,
1836 * then this method maps to 'DELETE FROM table'
1837 *
1838 * @param string the table name
1839 * @return string
1840 */
1841 protected function _truncate($table)
1842 {
1843 return 'TRUNCATE '.$table;
1844 }
1845
1846 // --------------------------------------------------------------------
1847
1848 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001849 * Get DELETE query string
1850 *
1851 * Compiles a delete query string and returns the sql
1852 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001853 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001854 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001855 * @return string
1856 */
1857 public function get_compiled_delete($table = '', $reset = TRUE)
1858 {
1859 $this->return_delete_sql = TRUE;
1860 $sql = $this->delete($table, '', NULL, $reset);
1861 $this->return_delete_sql = FALSE;
1862 return $sql;
1863 }
WanWizard7219c072011-12-28 14:09:05 +01001864
Derek Allard2067d1a2008-11-13 22:59:24 +00001865 // --------------------------------------------------------------------
1866
1867 /**
1868 * Delete
1869 *
1870 * Compiles a delete string and runs the query
1871 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001872 * @param mixed the table(s) to delete from. String or array
1873 * @param mixed the where clause
1874 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001875 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001876 * @return object
1877 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001878 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001879 {
1880 // Combine any cached components with the current statements
1881 $this->_merge_cache();
1882
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001883 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001884 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001885 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001886 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001887 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001888 }
1889
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001890 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001891 }
1892 elseif (is_array($table))
1893 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001894 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001895 {
1896 $this->delete($single_table, $where, $limit, FALSE);
1897 }
1898
1899 $this->_reset_write();
1900 return;
1901 }
1902 else
1903 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001904 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001905 }
1906
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001907 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001908 {
1909 $this->where($where);
1910 }
1911
Andrey Andreev650b4c02012-06-11 12:07:15 +03001912 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001913 {
1914 $this->limit($limit);
1915 }
1916
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001917 if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001918 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001919 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001920 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001921
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001922 $sql = $this->_delete($table, $this->qb_where, $this->qb_like, $this->qb_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001923 if ($reset_data)
1924 {
1925 $this->_reset_write();
1926 }
WanWizard7219c072011-12-28 14:09:05 +01001927
Andrey Andreev24276a32012-01-08 02:44:38 +02001928 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001929 }
WanWizard7219c072011-12-28 14:09:05 +01001930
Derek Allard2067d1a2008-11-13 22:59:24 +00001931 // --------------------------------------------------------------------
1932
1933 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001934 * Delete statement
1935 *
1936 * Generates a platform-specific delete string from the supplied data
1937 *
1938 * @param string the table name
1939 * @param array the where clause
1940 * @param array the like clause
1941 * @param string the limit clause
1942 * @return string
1943 */
1944 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
1945 {
1946 $conditions = array();
1947
1948 empty($where) OR $conditions[] = implode(' ', $where);
1949 empty($like) OR $conditions[] = implode(' ', $like);
1950
1951 return 'DELETE FROM '.$table
1952 .(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '')
Andrey Andreev650b4c02012-06-11 12:07:15 +03001953 .($limit ? ' LIMIT '.(int) $limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001954 }
1955
1956 // --------------------------------------------------------------------
1957
1958 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001959 * DB Prefix
1960 *
1961 * Prepends a database prefix if one exists in configuration
1962 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001963 * @param string the table
1964 * @return string
1965 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001966 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001967 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001968 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001969 {
1970 $this->display_error('db_table_name_required');
1971 }
1972
1973 return $this->dbprefix.$table;
1974 }
1975
1976 // --------------------------------------------------------------------
1977
1978 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001979 * Set DB Prefix
1980 *
1981 * Set's the DB Prefix to something new without needing to reconnect
1982 *
1983 * @param string the prefix
1984 * @return string
1985 */
1986 public function set_dbprefix($prefix = '')
1987 {
1988 return $this->dbprefix = $prefix;
1989 }
1990
1991 // --------------------------------------------------------------------
1992
1993 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001994 * Track Aliases
1995 *
1996 * Used to track SQL statements written with aliased tables.
1997 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001998 * @param string The table to inspect
1999 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002000 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002001 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002002 {
2003 if (is_array($table))
2004 {
2005 foreach ($table as $t)
2006 {
2007 $this->_track_aliases($t);
2008 }
2009 return;
2010 }
Barry Mienydd671972010-10-04 16:33:58 +02002011
Derek Jones37f4b9c2011-07-01 17:56:50 -05002012 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002013 // the string into discreet statements
2014 if (strpos($table, ',') !== FALSE)
2015 {
2016 return $this->_track_aliases(explode(',', $table));
2017 }
Barry Mienydd671972010-10-04 16:33:58 +02002018
Derek Allard2067d1a2008-11-13 22:59:24 +00002019 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002020 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002021 {
2022 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002023 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002024
Derek Allard2067d1a2008-11-13 22:59:24 +00002025 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002026 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002027
Derek Allard2067d1a2008-11-13 22:59:24 +00002028 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002029 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002030 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002031 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002032 }
2033 }
2034 }
WanWizard7219c072011-12-28 14:09:05 +01002035
Derek Allard2067d1a2008-11-13 22:59:24 +00002036 // --------------------------------------------------------------------
2037
2038 /**
2039 * Compile the SELECT statement
2040 *
2041 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05002042 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00002043 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002044 * @return string
2045 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002046 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002047 {
2048 // Combine any cached components with the current statements
2049 $this->_merge_cache();
2050
Derek Allard2067d1a2008-11-13 22:59:24 +00002051 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002052 if ($select_override !== FALSE)
2053 {
2054 $sql = $select_override;
2055 }
2056 else
2057 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002058 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002059
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002060 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002061 {
Barry Mienydd671972010-10-04 16:33:58 +02002062 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002063 }
2064 else
Barry Mienydd671972010-10-04 16:33:58 +02002065 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002066 // Cycle through the "select" portion of the query and prep each column name.
2067 // The reason we protect identifiers here rather then in the select() function
2068 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002069 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002070 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002071 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002072 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002073 }
Barry Mienydd671972010-10-04 16:33:58 +02002074
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002075 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002076 }
2077 }
2078
Derek Allard2067d1a2008-11-13 22:59:24 +00002079 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002080 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002081 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002082 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002083 }
2084
Derek Allard2067d1a2008-11-13 22:59:24 +00002085 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002086 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002087 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002088 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002089 }
2090
Derek Allard2067d1a2008-11-13 22:59:24 +00002091 // Write the "WHERE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002092 if (count($this->qb_where) > 0 OR count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002093 {
Greg Akere156c6e2011-04-20 16:03:04 -05002094 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00002095 }
2096
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002097 $sql .= implode("\n", $this->qb_where);
Derek Allard2067d1a2008-11-13 22:59:24 +00002098
Derek Allard2067d1a2008-11-13 22:59:24 +00002099 // Write the "LIKE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002100 if (count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002101 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002102 if (count($this->qb_where) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002103 {
2104 $sql .= "\nAND ";
2105 }
2106
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002107 $sql .= implode("\n", $this->qb_like);
Derek Allard2067d1a2008-11-13 22:59:24 +00002108 }
2109
Derek Allard2067d1a2008-11-13 22:59:24 +00002110 // Write the "GROUP BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002111 if (count($this->qb_groupby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002112 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002113 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002114 }
2115
Derek Allard2067d1a2008-11-13 22:59:24 +00002116 // Write the "HAVING" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002117 if (count($this->qb_having) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002118 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002119 $sql .= "\nHAVING ".implode("\n", $this->qb_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00002120 }
2121
Derek Allard2067d1a2008-11-13 22:59:24 +00002122 // Write the "ORDER BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002123 if (count($this->qb_orderby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002124 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002125 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002126 }
2127
Derek Allard2067d1a2008-11-13 22:59:24 +00002128 // Write the "LIMIT" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002129 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002130 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002131 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00002132 }
2133
2134 return $sql;
2135 }
2136
2137 // --------------------------------------------------------------------
2138
2139 /**
2140 * Object to Array
2141 *
2142 * Takes an object as input and converts the class variables to array key/vals
2143 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002144 * @param object
2145 * @return array
2146 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002147 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002148 {
2149 if ( ! is_object($object))
2150 {
2151 return $object;
2152 }
Barry Mienydd671972010-10-04 16:33:58 +02002153
Derek Allard2067d1a2008-11-13 22:59:24 +00002154 $array = array();
2155 foreach (get_object_vars($object) as $key => $val)
2156 {
2157 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002158 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002159 {
2160 $array[$key] = $val;
2161 }
2162 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002163
2164 return $array;
2165 }
Barry Mienydd671972010-10-04 16:33:58 +02002166
Derek Jonesd10e8962010-03-02 17:10:36 -06002167 // --------------------------------------------------------------------
2168
2169 /**
2170 * Object to Array
2171 *
2172 * Takes an object as input and converts the class variables to array key/vals
2173 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002174 * @param object
2175 * @return array
2176 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002177 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002178 {
2179 if ( ! is_object($object))
2180 {
2181 return $object;
2182 }
Barry Mienydd671972010-10-04 16:33:58 +02002183
Derek Jonesd10e8962010-03-02 17:10:36 -06002184 $array = array();
2185 $out = get_object_vars($object);
2186 $fields = array_keys($out);
2187
2188 foreach ($fields as $val)
2189 {
2190 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002191 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002192 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002193 $i = 0;
2194 foreach ($out[$val] as $data)
2195 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002196 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002197 }
2198 }
2199 }
2200
Derek Allard2067d1a2008-11-13 22:59:24 +00002201 return $array;
2202 }
Barry Mienydd671972010-10-04 16:33:58 +02002203
Derek Allard2067d1a2008-11-13 22:59:24 +00002204 // --------------------------------------------------------------------
2205
2206 /**
2207 * Start Cache
2208 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002209 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002210 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002211 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002212 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002213 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002214 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002215 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002216 }
2217
2218 // --------------------------------------------------------------------
2219
2220 /**
2221 * Stop Cache
2222 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002223 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002224 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002225 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002226 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002227 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002228 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002229 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002230 }
2231
2232 // --------------------------------------------------------------------
2233
2234 /**
2235 * Flush Cache
2236 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002237 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002238 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002239 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002240 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002241 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002242 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002243 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002244 'qb_cache_select' => array(),
2245 'qb_cache_from' => array(),
2246 'qb_cache_join' => array(),
2247 'qb_cache_where' => array(),
2248 'qb_cache_like' => array(),
2249 'qb_cache_groupby' => array(),
2250 'qb_cache_having' => array(),
2251 'qb_cache_orderby' => array(),
2252 'qb_cache_set' => array(),
2253 'qb_cache_exists' => array(),
2254 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002255 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002256 }
2257
2258 // --------------------------------------------------------------------
2259
2260 /**
2261 * Merge Cache
2262 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002263 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002264 * locally called ones.
2265 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002266 * @return void
2267 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002268 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002269 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002270 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002271 {
2272 return;
2273 }
2274
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002275 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002276 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002277 $qb_variable = 'qb_'.$val;
2278 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002279
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002280 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002281 {
2282 continue;
2283 }
2284
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002285 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002286 }
2287
2288 // If we are "protecting identifiers" we need to examine the "from"
2289 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002290 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002291 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002292 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002293 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002294
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002295 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002296 }
WanWizard7219c072011-12-28 14:09:05 +01002297
Kyle Farris0c147b32011-08-26 02:29:31 -04002298 // --------------------------------------------------------------------
2299
2300 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002301 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002302 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002303 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002304 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002305 * @return void
2306 */
2307 public function reset_query()
2308 {
2309 $this->_reset_select();
2310 $this->_reset_write();
2311 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002312
2313 // --------------------------------------------------------------------
2314
2315 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002316 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002317 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002318 * @param array An array of fields to reset
2319 * @return void
2320 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002321 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002322 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002323 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002324 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002325 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002326 {
2327 $this->$item = $default_value;
2328 }
2329 }
2330 }
2331
2332 // --------------------------------------------------------------------
2333
2334 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002335 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002336 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002337 * @return void
2338 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002339 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002340 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002341 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002342 'qb_select' => array(),
2343 'qb_from' => array(),
2344 'qb_join' => array(),
2345 'qb_where' => array(),
2346 'qb_like' => array(),
2347 'qb_groupby' => array(),
2348 'qb_having' => array(),
2349 'qb_orderby' => array(),
2350 'qb_wherein' => array(),
2351 'qb_aliased_tables' => array(),
2352 'qb_no_escape' => array(),
2353 'qb_distinct' => FALSE,
2354 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002355 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002356 )
2357 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002358 }
Barry Mienydd671972010-10-04 16:33:58 +02002359
Derek Allard2067d1a2008-11-13 22:59:24 +00002360 // --------------------------------------------------------------------
2361
2362 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002363 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002364 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002365 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002366 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002367 * @return void
2368 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002369 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002370 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002371 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002372 'qb_set' => array(),
2373 'qb_from' => array(),
2374 'qb_where' => array(),
2375 'qb_like' => array(),
2376 'qb_orderby' => array(),
2377 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002378 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002379 )
2380 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002381 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002382
Derek Allard2067d1a2008-11-13 22:59:24 +00002383}
2384
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002385/* End of file DB_query_builder.php */
Andrey Andreeve4c30192012-06-04 15:08:24 +03002386/* Location: ./system/database/DB_query_builder.php */