blob: 4c54b1c0a40d1958d780bc7456de3f08940a55ca [file] [log] [blame]
Andrey Andreev24276a32012-01-08 02:44:38 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
WanWizard7219c072011-12-28 14:09:05 +01008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
WanWizard7219c072011-12-28 14:09:05 +010010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
Jamie Rumbelow7efad202012-02-19 12:37:00 +000029 * Query Builder Class
Derek Allard2067d1a2008-11-13 22:59:24 +000030 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +000031 * This is the platform-independent base Query Builder implementation class.
Derek Allard2067d1a2008-11-13 22:59:24 +000032 *
33 * @package CodeIgniter
34 * @subpackage Drivers
35 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/database/
38 */
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +010039
40abstract class CI_DB_query_builder extends CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000041
WanWizard7219c072011-12-28 14:09:05 +010042 protected $return_delete_sql = FALSE;
43 protected $reset_delete_data = FALSE;
44
Jamie Rumbelow7efad202012-02-19 12:37:00 +000045 protected $qb_select = array();
46 protected $qb_distinct = FALSE;
47 protected $qb_from = array();
48 protected $qb_join = array();
49 protected $qb_where = array();
50 protected $qb_like = array();
51 protected $qb_groupby = array();
52 protected $qb_having = array();
53 protected $qb_keys = array();
54 protected $qb_limit = FALSE;
55 protected $qb_offset = FALSE;
Jamie Rumbelow7efad202012-02-19 12:37:00 +000056 protected $qb_orderby = array();
57 protected $qb_set = array();
58 protected $qb_wherein = array();
59 protected $qb_aliased_tables = array();
60 protected $qb_store_array = array();
61 protected $qb_where_group_started = FALSE;
62 protected $qb_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +020063
Jamie Rumbelow7efad202012-02-19 12:37:00 +000064 // Query Builder Caching variables
65 protected $qb_caching = FALSE;
66 protected $qb_cache_exists = array();
67 protected $qb_cache_select = array();
68 protected $qb_cache_from = array();
69 protected $qb_cache_join = array();
70 protected $qb_cache_where = array();
71 protected $qb_cache_like = array();
72 protected $qb_cache_groupby = array();
73 protected $qb_cache_having = array();
74 protected $qb_cache_orderby = array();
75 protected $qb_cache_set = array();
WanWizard7219c072011-12-28 14:09:05 +010076
Jamie Rumbelow7efad202012-02-19 12:37:00 +000077 protected $qb_no_escape = array();
78 protected $qb_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000079
80 /**
81 * Select
82 *
83 * Generates the SELECT portion of the query
84 *
Derek Allard2067d1a2008-11-13 22:59:24 +000085 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +030086 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +000087 * @return object
88 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060089 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000090 {
Derek Allard2067d1a2008-11-13 22:59:24 +000091 if (is_string($select))
92 {
93 $select = explode(',', $select);
94 }
95
Andrey Andreev42870232012-06-12 01:30:20 +030096 // If the escape value was not set will will base it on the global setting
97 is_bool($escape) OR $escape = $this->_protect_identifiers;
98
Derek Allard2067d1a2008-11-13 22:59:24 +000099 foreach ($select as $val)
100 {
101 $val = trim($val);
102
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100103 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000105 $this->qb_select[] = $val;
106 $this->qb_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000107
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000108 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000110 $this->qb_cache_select[] = $val;
111 $this->qb_cache_exists[] = 'select';
112 $this->qb_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
114 }
115 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 return $this;
118 }
119
120 // --------------------------------------------------------------------
121
122 /**
123 * Select Max
124 *
125 * Generates a SELECT MAX(field) portion of a query
126 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 * @param string the field
128 * @param string an alias
129 * @return object
130 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600131 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 {
133 return $this->_max_min_avg_sum($select, $alias, 'MAX');
134 }
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 // --------------------------------------------------------------------
137
138 /**
139 * Select Min
140 *
141 * Generates a SELECT MIN(field) portion of a query
142 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 * @param string the field
144 * @param string an alias
145 * @return object
146 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600147 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 {
149 return $this->_max_min_avg_sum($select, $alias, 'MIN');
150 }
151
152 // --------------------------------------------------------------------
153
154 /**
155 * Select Average
156 *
157 * Generates a SELECT AVG(field) portion of a query
158 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 * @param string the field
160 * @param string an alias
161 * @return object
162 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600163 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
165 return $this->_max_min_avg_sum($select, $alias, 'AVG');
166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Select Sum
172 *
173 * Generates a SELECT SUM(field) portion of a query
174 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 * @param string the field
176 * @param string an alias
177 * @return object
178 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600179 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
181 return $this->_max_min_avg_sum($select, $alias, 'SUM');
182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Processing Function for the four functions above:
188 *
189 * select_max()
190 * select_min()
191 * select_avg()
Andrey Andreev24276a32012-01-08 02:44:38 +0200192 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200193 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 * @param string the field
195 * @param string an alias
196 * @return object
197 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600198 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100200 if ( ! is_string($select) OR $select === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
202 $this->display_error('db_invalid_query');
203 }
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
208 {
209 show_error('Invalid function type: '.$type);
210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100212 if ($alias === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
214 $alias = $this->_create_alias_from_table(trim($select));
215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300217 $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->escape_identifiers(trim($alias));
218
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000219 $this->qb_select[] = $sql;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100220 $this->qb_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200221
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000222 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000224 $this->qb_cache_select[] = $sql;
225 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 return $this;
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Determines the alias name based on the table
235 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 * @param string
237 * @return string
238 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600239 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
241 if (strpos($item, '.') !== FALSE)
242 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200243 $item = explode('.', $item);
244 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 return $item;
248 }
249
250 // --------------------------------------------------------------------
251
252 /**
253 * DISTINCT
254 *
255 * Sets a flag which tells the query string compiler to add DISTINCT
256 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 * @param bool
258 * @return object
259 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600260 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300262 $this->qb_distinct = is_bool($val) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 return $this;
264 }
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 // --------------------------------------------------------------------
267
268 /**
269 * From
270 *
271 * Generates the FROM portion of the query
272 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * @param mixed can be a string or array
274 * @return object
275 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600276 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300278 foreach ((array) $from as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 if (strpos($val, ',') !== FALSE)
281 {
282 foreach (explode(',', $val) as $v)
283 {
284 $v = trim($v);
285 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200286
George Petsagourakis193d4482012-04-28 11:16:18 +0300287 $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000288
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000289 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000291 $this->qb_cache_from[] = $v;
292 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200293 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
296 else
297 {
298 $val = trim($val);
299
Andrey Andreev24276a32012-01-08 02:44:38 +0200300 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000301 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200303
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000304 $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000305
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000306 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000308 $this->qb_cache_from[] = $val;
309 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 }
311 }
312 }
313
314 return $this;
315 }
316
317 // --------------------------------------------------------------------
318
319 /**
320 * Join
321 *
322 * Generates the JOIN portion of the query
323 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * @param string
325 * @param string the join condition
326 * @param string the type of join
Alex Bilbief512b732012-06-16 11:15:19 +0100327 * @param string whether not to try to escape identifiers
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 * @return object
329 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300330 public function join($table, $cond, $type = '', $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200331 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100332 if ($type !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 $type = strtoupper(trim($type));
335
Andrey Andreev42870232012-06-12 01:30:20 +0300336 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 $type = '';
339 }
340 else
341 {
342 $type .= ' ';
343 }
344 }
345
Andrey Andreev24276a32012-01-08 02:44:38 +0200346 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000347 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 $this->_track_aliases($table);
349
Andrey Andreevfe642da2012-06-16 03:47:33 +0300350 is_bool($escape) OR $escape = $this->_protect_identifiers;
351
Andrey Andreev42870232012-06-12 01:30:20 +0300352 // Split multiple conditions
353 if ($escape === TRUE && preg_match_all('/\sAND\s|\sOR\s/i', $cond, $m, PREG_SET_ORDER | PREG_OFFSET_CAPTURE))
354 {
355 $newcond = '';
356 $m[0][] = array('', strlen($cond));
357
358 for ($i = 0, $c = count($m[0]), $s = 0;
359 $i < $c;
360 $s += $m[0][$i][1] + strlen($m[0][$i][0]), $i++)
361 {
362 $temp = substr($cond, $s, $m[0][$i][1]);
363
364 $newcond .= preg_match('/([\[\w\.-]+)([\W\s]+)(.+)/i', $temp, $match)
365 ? $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3])
366 : $temp;
367
368 $newcond .= $m[0][$i][0];
369 }
370
Andrey Andreev3751f932012-06-17 18:07:48 +0300371 $cond = ' ON '.$newcond;
Andrey Andreev42870232012-06-12 01:30:20 +0300372 }
373 // Split apart the condition and protect the identifiers
374 elseif ($escape === TRUE && preg_match('/([\[\w\.-]+)([\W\s]+)(.+)/i', $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
Andrey Andreev3751f932012-06-17 18:07:48 +0300376 $cond = ' ON '.$this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
377 }
378 elseif ( ! $this->_has_operator($cond))
379 {
380 $cond = ' USING ('.($escape ? $this->escape_identifiers($cond) : $cond).')';
381 }
382 else
383 {
384 $cond = ' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Andrey Andreev42870232012-06-12 01:30:20 +0300387 // Do we want to escape the table name?
388 if ($escape === TRUE)
389 {
390 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
391 }
392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 // Assemble the JOIN statement
Andrey Andreev3751f932012-06-17 18:07:48 +0300394 $this->qb_join[] = $join = $type.'JOIN '.$table.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000395
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000396 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000398 $this->qb_cache_join[] = $join;
399 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 }
401
402 return $this;
403 }
404
405 // --------------------------------------------------------------------
406
407 /**
408 * Where
409 *
410 * Generates the WHERE portion of the query. Separates
411 * multiple calls with AND
412 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 * @param mixed
414 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300415 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 * @return object
417 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300418 public function where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
420 return $this->_where($key, $value, 'AND ', $escape);
421 }
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 // --------------------------------------------------------------------
424
425 /**
426 * OR Where
427 *
428 * Generates the WHERE portion of the query. Separates
429 * multiple calls with OR
430 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 * @param mixed
432 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300433 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 * @return object
435 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300436 public function or_where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
438 return $this->_where($key, $value, 'OR ', $escape);
439 }
440
441 // --------------------------------------------------------------------
442
443 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 * Where
445 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600446 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 * @param mixed
449 * @param mixed
450 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300451 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 * @return object
453 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600454 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 {
WanWizard7219c072011-12-28 14:09:05 +0100456 $type = $this->_group_get_type($type);
457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 if ( ! is_array($key))
459 {
460 $key = array($key => $value);
461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 // If the escape value was not set will will base it on the global setting
Andrey Andreeve10fb792012-06-15 12:07:04 +0300464 is_bool($escape) OR $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000465
466 foreach ($key as $k => $v)
467 {
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100468 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000469
Andrey Andreev929fd2d2012-06-17 17:29:57 +0300470 $k = (($op = $this->_get_operator($k)) !== FALSE)
471 ? $this->protect_identifiers(substr($k, 0, strpos($k, $op)), FALSE, $escape).strstr($k, $op)
Andrey Andreev9c14f652012-06-10 14:35:07 +0300472 : $this->protect_identifiers($k, FALSE, $escape);
473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 if (is_null($v) && ! $this->_has_operator($k))
475 {
476 // value appears not to have been set, assign the test to IS NULL
477 $k .= ' IS NULL';
478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 if ( ! is_null($v))
481 {
482 if ($escape === TRUE)
483 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 $v = ' '.$this->escape($v);
485 }
WanWizard7219c072011-12-28 14:09:05 +0100486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 if ( ! $this->_has_operator($k))
488 {
Greg Akere156c6e2011-04-20 16:03:04 -0500489 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 }
491 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000492
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000493 $this->qb_where[] = $prefix.$k.$v;
494 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000496 $this->qb_cache_where[] = $prefix.$k.$v;
497 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 }
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
Barry Mienydd671972010-10-04 16:33:58 +0200501
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 return $this;
503 }
504
505 // --------------------------------------------------------------------
506
507 /**
508 * Where_in
509 *
510 * Generates a WHERE field IN ('item', 'item') SQL query joined with
511 * AND if appropriate
512 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 * @param string The field to search
514 * @param array The values searched on
515 * @return object
516 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300517 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300519 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 }
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 // --------------------------------------------------------------------
523
524 /**
525 * Where_in_or
526 *
527 * Generates a WHERE field IN ('item', 'item') SQL query joined with
528 * OR if appropriate
529 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 * @param string The field to search
531 * @param array The values searched on
532 * @return object
533 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300534 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300536 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 }
538
539 // --------------------------------------------------------------------
540
541 /**
542 * Where_not_in
543 *
544 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
545 * with AND if appropriate
546 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 * @param string The field to search
548 * @param array The values searched on
549 * @return object
550 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300551 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300553 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 }
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 // --------------------------------------------------------------------
557
558 /**
559 * Where_not_in_or
560 *
561 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
562 * with OR if appropriate
563 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 * @param string The field to search
565 * @param array The values searched on
566 * @return object
567 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300568 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300570 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 }
572
573 // --------------------------------------------------------------------
574
575 /**
576 * Where_in
577 *
578 * Called by where_in, where_in_or, where_not_in, where_not_in_or
579 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 * @param string The field to search
581 * @param array The values searched on
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300582 * @param bool If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200583 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 * @return object
585 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300586 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
588 if ($key === NULL OR $values === NULL)
589 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300590 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 }
Barry Mienydd671972010-10-04 16:33:58 +0200592
WanWizard7219c072011-12-28 14:09:05 +0100593 $type = $this->_group_get_type($type);
594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 if ( ! is_array($values))
596 {
597 $values = array($values);
598 }
Barry Mienydd671972010-10-04 16:33:58 +0200599
Andrey Andreev498c1e02012-06-16 03:34:10 +0300600 is_bool($escape) OR $escape = $this->_protect_identifiers;
601
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 $not = ($not) ? ' NOT' : '';
603
604 foreach ($values as $value)
605 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000606 $this->qb_wherein[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 }
608
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000609 $prefix = (count($this->qb_where) === 0) ? '' : $type;
Andrey Andreev498c1e02012-06-16 03:34:10 +0300610 $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 +0200611
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000612 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000614 $this->qb_cache_where[] = $where_in;
615 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 }
617
618 // reset the array for multiple calls
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000619 $this->qb_wherein = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 return $this;
621 }
Barry Mienydd671972010-10-04 16:33:58 +0200622
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 // --------------------------------------------------------------------
624
625 /**
626 * Like
627 *
628 * Generates a %LIKE% portion of the query. Separates
629 * multiple calls with AND
630 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 * @param mixed
632 * @param mixed
633 * @return object
634 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600635 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
637 return $this->_like($field, $match, 'AND ', $side);
638 }
639
640 // --------------------------------------------------------------------
641
642 /**
643 * Not Like
644 *
645 * Generates a NOT LIKE portion of the query. Separates
646 * multiple calls with AND
647 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 * @param mixed
649 * @param mixed
650 * @return object
651 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600652 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 {
654 return $this->_like($field, $match, 'AND ', $side, 'NOT');
655 }
Barry Mienydd671972010-10-04 16:33:58 +0200656
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 // --------------------------------------------------------------------
658
659 /**
660 * OR Like
661 *
662 * Generates a %LIKE% portion of the query. Separates
663 * multiple calls with OR
664 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 * @param mixed
666 * @param mixed
667 * @return object
668 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600669 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 {
671 return $this->_like($field, $match, 'OR ', $side);
672 }
673
674 // --------------------------------------------------------------------
675
676 /**
677 * OR Not Like
678 *
679 * Generates a NOT LIKE portion of the query. Separates
680 * multiple calls with OR
681 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 * @param mixed
683 * @param mixed
684 * @return object
685 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600686 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 {
688 return $this->_like($field, $match, 'OR ', $side, 'NOT');
689 }
Barry Mienydd671972010-10-04 16:33:58 +0200690
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 // --------------------------------------------------------------------
692
693 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 * Like
695 *
696 * Called by like() or orlike()
697 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 * @param mixed
699 * @param mixed
700 * @param string
701 * @return object
702 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600703 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 {
WanWizard7219c072011-12-28 14:09:05 +0100705 $type = $this->_group_get_type($type);
706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 if ( ! is_array($field))
708 {
709 $field = array($field => $match);
710 }
Barry Mienydd671972010-10-04 16:33:58 +0200711
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 foreach ($field as $k => $v)
713 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000714 $k = $this->protect_identifiers($k);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000715 $prefix = (count($this->qb_like) === 0) ? '' : $type;
Derek Jonese4ed5832009-02-20 21:44:59 +0000716 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300717
Andrey Andreev24276a32012-01-08 02:44:38 +0200718 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400719 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100720 $like_statement = "{$prefix} $k $not LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400721 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200722 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100724 $like_statement = "{$prefix} $k $not LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200726 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100728 $like_statement = "{$prefix} $k $not LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 }
730 else
731 {
Phil Sturgeonf777d3d2012-05-23 18:47:19 +0100732 $like_statement = "{$prefix} $k $not LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600734
Derek Jonese4ed5832009-02-20 21:44:59 +0000735 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100736 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000737 {
Greg Aker0d424892010-01-26 02:14:44 +0000738 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000739 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600740
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000741 $this->qb_like[] = $like_statement;
742 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000744 $this->qb_cache_like[] = $like_statement;
745 $this->qb_cache_exists[] = 'like';
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 }
Barry Mienydd671972010-10-04 16:33:58 +0200747
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200749
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 return $this;
751 }
Barry Mienydd671972010-10-04 16:33:58 +0200752
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 // --------------------------------------------------------------------
754
755 /**
WanWizard7219c072011-12-28 14:09:05 +0100756 * Starts a query group.
757 *
758 * @param string (Internal use only)
759 * @param string (Internal use only)
760 * @return object
761 */
762 public function group_start($not = '', $type = 'AND ')
763 {
764 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100765
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000766 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100767 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000768 $this->qb_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (';
WanWizard7219c072011-12-28 14:09:05 +0100769
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000770 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100771 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000772 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100773 }
774
775 return $this;
776 }
777
778 // --------------------------------------------------------------------
779
780 /**
781 * Starts a query group, but ORs the group
782 *
783 * @return object
784 */
785 public function or_group_start()
786 {
787 return $this->group_start('', 'OR ');
788 }
789
790 // --------------------------------------------------------------------
791
792 /**
793 * Starts a query group, but NOTs the group
794 *
795 * @return object
796 */
797 public function not_group_start()
798 {
799 return $this->group_start('NOT ', 'AND ');
800 }
801
802 // --------------------------------------------------------------------
803
804 /**
805 * Starts a query group, but OR NOTs the group
806 *
807 * @return object
808 */
809 public function or_not_group_start()
810 {
811 return $this->group_start('NOT ', 'OR ');
812 }
813
814 // --------------------------------------------------------------------
815
816 /**
817 * Ends a query group
818 *
819 * @return object
820 */
821 public function group_end()
822 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000823 $this->qb_where_group_started = FALSE;
824 $this->qb_where[] = $value = str_repeat(' ', $this->qb_where_group_count--) . ')';
WanWizard7219c072011-12-28 14:09:05 +0100825
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000826 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100827 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000828 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100829 }
830
WanWizard7219c072011-12-28 14:09:05 +0100831 return $this;
832 }
833
834 // --------------------------------------------------------------------
835
836 /**
837 * Group_get_type
838 *
839 * Called by group_start(), _like(), _where() and _where_in()
840 *
841 * @param string
842 * @return string
843 */
844 protected function _group_get_type($type)
845 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000846 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100847 {
848 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000849 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100850 }
851
852 return $type;
853 }
854
855 // --------------------------------------------------------------------
856
857 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 * GROUP BY
859 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 * @param string
861 * @return object
862 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600863 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 {
865 if (is_string($by))
866 {
867 $by = explode(',', $by);
868 }
Barry Mienydd671972010-10-04 16:33:58 +0200869
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 foreach ($by as $val)
871 {
872 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200873
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100874 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000876 $this->qb_groupby[] = $val = $this->protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200877
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000878 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000880 $this->qb_cache_groupby[] = $val;
881 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 }
883 }
884 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200885
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 return $this;
887 }
888
889 // --------------------------------------------------------------------
890
891 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 * Sets the HAVING value
893 *
894 * Separates multiple calls with AND
895 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 * @param string
897 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300898 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 * @return object
900 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300901 public function having($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 {
903 return $this->_having($key, $value, 'AND ', $escape);
904 }
Barry Mienydd671972010-10-04 16:33:58 +0200905
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 // --------------------------------------------------------------------
907
908 /**
909 * Sets the OR HAVING value
910 *
911 * Separates multiple calls with OR
912 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 * @param string
914 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300915 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 * @return object
917 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300918 public function or_having($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 {
920 return $this->_having($key, $value, 'OR ', $escape);
921 }
Barry Mienydd671972010-10-04 16:33:58 +0200922
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 // --------------------------------------------------------------------
924
925 /**
926 * Sets the HAVING values
927 *
928 * Called by having() or or_having()
929 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 * @param string
931 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300932 * @param string
933 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 * @return object
935 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300936 protected function _having($key, $value = '', $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 {
938 if ( ! is_array($key))
939 {
940 $key = array($key => $value);
941 }
Barry Mienydd671972010-10-04 16:33:58 +0200942
Andrey Andreevfe642da2012-06-16 03:47:33 +0300943 is_bool($escape) OR $escape = $this->_protect_identifiers;
944
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 foreach ($key as $k => $v)
946 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000947 $prefix = (count($this->qb_having) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000948
Andrey Andreev974c75b2012-06-15 12:30:02 +0300949 $k = $this->_has_operator($k)
950 ? $this->protect_identifiers(substr($k, 0, strpos(rtrim($k), ' ')), FALSE, $escape).strchr(rtrim($k), ' ')
951 : $this->protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000952
953 if ( ! $this->_has_operator($k))
954 {
955 $k .= ' = ';
956 }
957
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100958 if ($v !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400960 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 }
Barry Mienydd671972010-10-04 16:33:58 +0200962
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000963 $this->qb_having[] = $prefix.$k.$v;
964 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000966 $this->qb_cache_having[] = $prefix.$k.$v;
967 $this->qb_cache_exists[] = 'having';
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 }
969 }
Barry Mienydd671972010-10-04 16:33:58 +0200970
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 return $this;
972 }
Barry Mienydd671972010-10-04 16:33:58 +0200973
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 // --------------------------------------------------------------------
975
976 /**
977 * Sets the ORDER BY value
978 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 * @param string
980 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100981 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 * @return object
983 */
Andrey Andreevd24160c2012-06-16 03:21:20 +0300984 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200986 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 {
988 $orderby = ''; // Random results want or don't need a field name
989 $direction = $this->_random_keyword;
990 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100991 elseif (trim($direction) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 {
Andrey Andreev650b4c02012-06-11 12:07:15 +0300993 $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : ' ASC';
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 }
Barry Mienydd671972010-10-04 16:33:58 +0200995
Andrey Andreevd24160c2012-06-16 03:21:20 +0300996 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +0200997
Andrey Andreevd24160c2012-06-16 03:21:20 +0300998 if ($escape === TRUE && strpos($orderby, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 {
1000 $temp = array();
1001 foreach (explode(',', $orderby) as $part)
1002 {
1003 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001004 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 {
Andrey Andreev88cb2782012-06-11 20:40:50 +03001006 $part = preg_match('/^(.+)\s+(ASC|DESC)$/i', $part, $matches)
1007 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
1008 : $this->protect_identifiers($part);
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 }
Barry Mienydd671972010-10-04 16:33:58 +02001010
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 $temp[] = $part;
1012 }
Barry Mienydd671972010-10-04 16:33:58 +02001013
1014 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001016 elseif ($direction !== $this->_random_keyword && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 {
Andrey Andreev1d1d7ff2012-06-11 22:35:35 +03001018 $orderby = preg_match('/^(.+)\s+(ASC|DESC)$/i', $orderby, $matches)
Andrey Andreev88cb2782012-06-11 20:40:50 +03001019 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
1020 : $this->protect_identifiers($orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00001021 }
Barry Mienydd671972010-10-04 16:33:58 +02001022
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001023 $this->qb_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +02001024
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001025 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001027 $this->qb_cache_orderby[] = $orderby_statement;
1028 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 }
1030
1031 return $this;
1032 }
Barry Mienydd671972010-10-04 16:33:58 +02001033
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 // --------------------------------------------------------------------
1035
1036 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 * Sets the LIMIT value
1038 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001039 * @param int the limit value
1040 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 * @return object
1042 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +02001043 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001045 $this->qb_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +00001046
Andrey Andreev650b4c02012-06-11 12:07:15 +03001047 if ( ! empty($offset))
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001049 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 }
Barry Mienydd671972010-10-04 16:33:58 +02001051
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 return $this;
1053 }
Barry Mienydd671972010-10-04 16:33:58 +02001054
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 // --------------------------------------------------------------------
1056
1057 /**
1058 * Sets the OFFSET value
1059 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001060 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 * @return object
1062 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001063 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001065 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 return $this;
1067 }
Barry Mienydd671972010-10-04 16:33:58 +02001068
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 // --------------------------------------------------------------------
1070
1071 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001072 * The "set" function.
1073 *
1074 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 * @param mixed
1077 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001078 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 * @return object
1080 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001081 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 {
1083 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001084
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 if ( ! is_array($key))
1086 {
1087 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001088 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001089
Andrey Andreevfe642da2012-06-16 03:47:33 +03001090 is_bool($escape) OR $escape = $this->_protect_identifiers;
1091
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 foreach ($key as $k => $v)
1093 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001094 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1095 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 }
Barry Mienydd671972010-10-04 16:33:58 +02001097
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 return $this;
1099 }
WanWizard7219c072011-12-28 14:09:05 +01001100
Kyle Farris0c147b32011-08-26 02:29:31 -04001101 // --------------------------------------------------------------------
1102
1103 /**
1104 * Get SELECT query string
1105 *
1106 * Compiles a SELECT query string and returns the sql.
1107 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001108 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001109 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001110 * @return string
1111 */
WanWizard7219c072011-12-28 14:09:05 +01001112 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001113 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001114 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001115 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001116 $this->_track_aliases($table);
1117 $this->from($table);
1118 }
WanWizard7219c072011-12-28 14:09:05 +01001119
Andrey Andreev650b4c02012-06-11 12:07:15 +03001120 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001121
Kyle Farris0c147b32011-08-26 02:29:31 -04001122 if ($reset === TRUE)
1123 {
1124 $this->_reset_select();
1125 }
WanWizard7219c072011-12-28 14:09:05 +01001126
Kyle Farris0c147b32011-08-26 02:29:31 -04001127 return $select;
1128 }
WanWizard7219c072011-12-28 14:09:05 +01001129
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 // --------------------------------------------------------------------
1131
1132 /**
1133 * Get
1134 *
1135 * Compiles the select statement based on the other functions called
1136 * and runs the query
1137 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 * @param string the table
1139 * @param string the limit clause
1140 * @param string the offset clause
1141 * @return object
1142 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001143 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001145 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 {
1147 $this->_track_aliases($table);
1148 $this->from($table);
1149 }
Barry Mienydd671972010-10-04 16:33:58 +02001150
Andrey Andreev650b4c02012-06-11 12:07:15 +03001151 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 {
1153 $this->limit($limit, $offset);
1154 }
Barry Mienydd671972010-10-04 16:33:58 +02001155
Andrey Andreev24276a32012-01-08 02:44:38 +02001156 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 $this->_reset_select();
1158 return $result;
1159 }
1160
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001161 // --------------------------------------------------------------------
1162
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 /**
1164 * "Count All Results" query
1165 *
Barry Mienydd671972010-10-04 16:33:58 +02001166 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001167 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 * @param string
1170 * @return string
1171 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001172 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001174 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 {
1176 $this->_track_aliases($table);
1177 $this->from($table);
1178 }
Barry Mienydd671972010-10-04 16:33:58 +02001179
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001180 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001182
Purwandi1d160e72012-01-09 16:33:28 +07001183 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001185 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 }
1187
Purwandi1d160e72012-01-09 16:33:28 +07001188 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001189 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001191
Derek Allard2067d1a2008-11-13 22:59:24 +00001192 // --------------------------------------------------------------------
1193
1194 /**
1195 * Get_Where
1196 *
1197 * Allows the where clause, limit and offset to be added directly
1198 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 * @param string the where clause
1200 * @param string the limit clause
1201 * @param string the offset clause
1202 * @return object
1203 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001204 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001206 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 {
1208 $this->from($table);
1209 }
1210
1211 if ( ! is_null($where))
1212 {
1213 $this->where($where);
1214 }
Barry Mienydd671972010-10-04 16:33:58 +02001215
Andrey Andreev650b4c02012-06-11 12:07:15 +03001216 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 {
1218 $this->limit($limit, $offset);
1219 }
Barry Mienydd671972010-10-04 16:33:58 +02001220
Andrey Andreev24276a32012-01-08 02:44:38 +02001221 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 $this->_reset_select();
1223 return $result;
1224 }
1225
1226 // --------------------------------------------------------------------
1227
1228 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001229 * Insert_Batch
1230 *
1231 * Compiles batch insert strings and runs the queries
1232 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001233 * @param string the table to retrieve the results from
1234 * @param array an associative array of insert values
1235 * @return object
1236 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001237 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001238 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001239 if ( ! is_null($set))
1240 {
1241 $this->set_insert_batch($set);
1242 }
Barry Mienydd671972010-10-04 16:33:58 +02001243
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001244 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001245 {
1246 if ($this->db_debug)
1247 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001248 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001249 return $this->display_error('db_must_use_set');
1250 }
1251 return FALSE;
1252 }
1253
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001254 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001255 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001256 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001257 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001258 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001259 }
Barry Mienydd671972010-10-04 16:33:58 +02001260
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001261 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001262 }
1263
1264 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001265 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001266 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001267 $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 -06001268 }
Barry Mienydd671972010-10-04 16:33:58 +02001269
Derek Jonesd10e8962010-03-02 17:10:36 -06001270 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001271 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001272 }
1273
1274 // --------------------------------------------------------------------
1275
1276 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001277 * Insert_batch statement
1278 *
1279 * Generates a platform-specific insert string from the supplied data.
1280 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001281 * @param string the table name
1282 * @param array the insert keys
1283 * @param array the insert values
1284 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001285 */
1286 protected function _insert_batch($table, $keys, $values)
1287 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001288 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001289 }
1290
1291 // --------------------------------------------------------------------
1292
1293 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001294 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001295 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001296 * @param mixed
1297 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001298 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001299 * @return object
1300 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001301 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001302 {
1303 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001304
Derek Jonesd10e8962010-03-02 17:10:36 -06001305 if ( ! is_array($key))
1306 {
1307 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001308 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001309
Andrey Andreevfe642da2012-06-16 03:47:33 +03001310 is_bool($escape) OR $escape = $this->_protect_identifiers;
1311
Iban Eguia3c0a4522012-04-15 13:30:44 +02001312 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001313 sort($keys);
1314
1315 foreach ($key as $row)
1316 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001317 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001318 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1319 {
1320 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001321 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001322 return;
1323 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001324
Barry Mienydd671972010-10-04 16:33:58 +02001325 ksort($row); // puts $row in the same order as our keys
1326
Andrey Andreev650b4c02012-06-11 12:07:15 +03001327 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001328 {
1329 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001330 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001331 {
Barry Mienydd671972010-10-04 16:33:58 +02001332 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001333 }
1334
Andrey Andreev650b4c02012-06-11 12:07:15 +03001335 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001336 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001337
1338 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001339 }
1340
1341 foreach ($keys as $k)
1342 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001343 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001344 }
Barry Mienydd671972010-10-04 16:33:58 +02001345
Derek Jonesd10e8962010-03-02 17:10:36 -06001346 return $this;
1347 }
WanWizard7219c072011-12-28 14:09:05 +01001348
Kyle Farris0c147b32011-08-26 02:29:31 -04001349 // --------------------------------------------------------------------
1350
1351 /**
1352 * Get INSERT query string
1353 *
1354 * Compiles an insert query and returns the sql
1355 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001356 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001357 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001358 * @return string
1359 */
1360 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001361 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001362 if ($this->_validate_insert($table) === FALSE)
1363 {
1364 return FALSE;
1365 }
WanWizard7219c072011-12-28 14:09:05 +01001366
Kyle Farris76116012011-08-31 11:17:48 -04001367 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001368 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001369 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001370 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001371 array_keys($this->qb_set),
1372 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001373 );
WanWizard7219c072011-12-28 14:09:05 +01001374
Kyle Farris0c147b32011-08-26 02:29:31 -04001375 if ($reset === TRUE)
1376 {
1377 $this->_reset_write();
1378 }
WanWizard7219c072011-12-28 14:09:05 +01001379
Kyle Farris0c147b32011-08-26 02:29:31 -04001380 return $sql;
1381 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001382
Derek Allard2067d1a2008-11-13 22:59:24 +00001383 // --------------------------------------------------------------------
1384
1385 /**
1386 * Insert
1387 *
1388 * Compiles an insert string and runs the query
1389 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001390 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001391 * @param array an associative array of insert values
1392 * @return object
1393 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001394 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001395 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001396 if ( ! is_null($set))
1397 {
1398 $this->set($set);
1399 }
WanWizard7219c072011-12-28 14:09:05 +01001400
Kyle Farris0c147b32011-08-26 02:29:31 -04001401 if ($this->_validate_insert($table) === FALSE)
1402 {
1403 return FALSE;
1404 }
WanWizard7219c072011-12-28 14:09:05 +01001405
Kyle Farris76116012011-08-31 11:17:48 -04001406 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001407 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001408 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001409 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001410 array_keys($this->qb_set),
1411 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001412 );
Barry Mienydd671972010-10-04 16:33:58 +02001413
Kyle Farris0c147b32011-08-26 02:29:31 -04001414 $this->_reset_write();
1415 return $this->query($sql);
1416 }
WanWizard7219c072011-12-28 14:09:05 +01001417
Kyle Farris0c147b32011-08-26 02:29:31 -04001418 // --------------------------------------------------------------------
1419
1420 /**
Andrey Andreev65d537c2012-04-05 14:11:41 +03001421 * Insert statement
1422 *
1423 * Generates a platform-specific insert string from the supplied data
1424 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001425 * @param string the table name
1426 * @param array the insert keys
1427 * @param array the insert values
1428 * @return string
Andrey Andreev65d537c2012-04-05 14:11:41 +03001429 */
1430 protected function _insert($table, $keys, $values)
1431 {
1432 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1433 }
1434
1435 // --------------------------------------------------------------------
1436
1437 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001438 * Validate Insert
1439 *
1440 * This method is used by both insert() and get_compiled_insert() to
1441 * validate that the there data is actually being set and that table
1442 * has been chosen to be inserted into.
1443 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001444 * @param string the table to insert data into
1445 * @return string
1446 */
WanWizard7219c072011-12-28 14:09:05 +01001447 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001448 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001449 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001450 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001451 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001452 }
1453
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001454 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001455 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001456 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001457 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001458 elseif ( ! isset($this->qb_from[0]))
1459 {
1460 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1461 }
WanWizard7219c072011-12-28 14:09:05 +01001462
Kyle Farris0c147b32011-08-26 02:29:31 -04001463 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001464 }
Barry Mienydd671972010-10-04 16:33:58 +02001465
Phil Sturgeon9789f322011-07-15 15:14:05 -06001466 // --------------------------------------------------------------------
1467
1468 /**
1469 * Replace
1470 *
1471 * Compiles an replace into string and runs the query
1472 *
1473 * @param string the table to replace data into
1474 * @param array an associative array of insert values
1475 * @return object
1476 */
1477 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001478 {
1479 if ( ! is_null($set))
1480 {
1481 $this->set($set);
1482 }
Barry Mienydd671972010-10-04 16:33:58 +02001483
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001484 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001485 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001486 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001487 }
1488
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001489 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001490 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001491 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001492 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001493 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001494 }
Barry Mienydd671972010-10-04 16:33:58 +02001495
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001496 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001497 }
1498
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001499 $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 +00001500
Derek Jonesd10e8962010-03-02 17:10:36 -06001501 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001502 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001503 }
WanWizard7219c072011-12-28 14:09:05 +01001504
Kyle Farris0c147b32011-08-26 02:29:31 -04001505 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001506
Kyle Farris0c147b32011-08-26 02:29:31 -04001507 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001508 * Replace statement
1509 *
1510 * Generates a platform-specific replace string from the supplied data
1511 *
1512 * @param string the table name
1513 * @param array the insert keys
1514 * @param array the insert values
1515 * @return string
1516 */
1517 protected function _replace($table, $keys, $values)
1518 {
1519 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1520 }
1521
1522 // --------------------------------------------------------------------
1523
1524 /**
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001525 * From Tables
1526 *
1527 * This public function implicitly groups FROM tables so there is no confusion
1528 * about operator precedence in harmony with SQL standards
1529 *
1530 * @param array
1531 * @return string
1532 */
1533 protected function _from_tables($tables)
1534 {
1535 is_array($tables) OR $tables = array($tables);
1536
1537 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
1538 }
1539
1540 // --------------------------------------------------------------------
1541
1542 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001543 * Get UPDATE query string
1544 *
1545 * Compiles an update query and returns the sql
1546 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001547 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001548 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001549 * @return string
1550 */
1551 public function get_compiled_update($table = '', $reset = TRUE)
1552 {
1553 // Combine any cached components with the current statements
1554 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001555
Kyle Farris0c147b32011-08-26 02:29:31 -04001556 if ($this->_validate_update($table) === FALSE)
1557 {
1558 return FALSE;
1559 }
WanWizard7219c072011-12-28 14:09:05 +01001560
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001561 $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 +01001562
Kyle Farris0c147b32011-08-26 02:29:31 -04001563 if ($reset === TRUE)
1564 {
1565 $this->_reset_write();
1566 }
WanWizard7219c072011-12-28 14:09:05 +01001567
Kyle Farris0c147b32011-08-26 02:29:31 -04001568 return $sql;
1569 }
WanWizard7219c072011-12-28 14:09:05 +01001570
Derek Allard2067d1a2008-11-13 22:59:24 +00001571 // --------------------------------------------------------------------
1572
1573 /**
1574 * Update
1575 *
1576 * Compiles an update string and runs the query
1577 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001578 * @param string the table to retrieve the results from
1579 * @param array an associative array of update values
1580 * @param mixed the where clause
1581 * @return object
1582 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001583 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001584 {
1585 // Combine any cached components with the current statements
1586 $this->_merge_cache();
1587
1588 if ( ! is_null($set))
1589 {
1590 $this->set($set);
1591 }
Barry Mienydd671972010-10-04 16:33:58 +02001592
Kyle Farris0c147b32011-08-26 02:29:31 -04001593 if ($this->_validate_update($table) === FALSE)
1594 {
1595 return FALSE;
1596 }
1597
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001598 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001599 {
1600 $this->where($where);
1601 }
1602
Andrey Andreev650b4c02012-06-11 12:07:15 +03001603 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001604 {
1605 $this->limit($limit);
1606 }
1607
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001608 $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 +00001609
Kyle Farris0c147b32011-08-26 02:29:31 -04001610 $this->_reset_write();
1611 return $this->query($sql);
1612 }
WanWizard7219c072011-12-28 14:09:05 +01001613
Kyle Farris0c147b32011-08-26 02:29:31 -04001614 // --------------------------------------------------------------------
1615
1616 /**
Andrey Andreev975034d2012-04-05 15:09:55 +03001617 * Update statement
1618 *
1619 * Generates a platform-specific update string from the supplied data
1620 *
1621 * @param string the table name
1622 * @param array the update data
1623 * @param array the where clause
1624 * @param array the orderby clause
1625 * @param array the limit clause
Andrey Andreev00541ae2012-04-09 11:43:10 +03001626 * @param array the like clause
Andrey Andreev975034d2012-04-05 15:09:55 +03001627 * @return string
1628 */
Andrey Andreev00541ae2012-04-09 11:43:10 +03001629 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Andrey Andreev975034d2012-04-05 15:09:55 +03001630 {
1631 foreach ($values as $key => $val)
1632 {
1633 $valstr[] = $key.' = '.$val;
1634 }
1635
Andrey Andreev00541ae2012-04-09 11:43:10 +03001636 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
1637
1638 if ( ! empty($like))
1639 {
1640 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
1641 }
1642
Andrey Andreev975034d2012-04-05 15:09:55 +03001643 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev00541ae2012-04-09 11:43:10 +03001644 .$where
Andrey Andreev975034d2012-04-05 15:09:55 +03001645 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
1646 .($limit ? ' LIMIT '.$limit : '');
1647 }
1648
1649 // --------------------------------------------------------------------
1650
1651 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001652 * Validate Update
1653 *
1654 * This method is used by both update() and get_compiled_update() to
1655 * validate that data is actually being set and that a table has been
1656 * chosen to be update.
1657 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001658 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001659 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001660 */
1661 protected function _validate_update($table = '')
1662 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001663 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001664 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001665 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001666 }
1667
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001668 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001669 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001670 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001671 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001672 elseif ( ! isset($this->qb_from[0]))
1673 {
1674 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1675 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001676
1677 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001678 }
WanWizard7219c072011-12-28 14:09:05 +01001679
Derek Jonesd10e8962010-03-02 17:10:36 -06001680 // --------------------------------------------------------------------
1681
1682 /**
1683 * Update_Batch
1684 *
1685 * Compiles an update string and runs the query
1686 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001687 * @param string the table to retrieve the results from
1688 * @param array an associative array of update values
1689 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001690 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001691 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001692 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001693 {
1694 // Combine any cached components with the current statements
1695 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001696
Derek Jonesd10e8962010-03-02 17:10:36 -06001697 if (is_null($index))
1698 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001699 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001700 }
1701
1702 if ( ! is_null($set))
1703 {
1704 $this->set_update_batch($set, $index);
1705 }
1706
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001707 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001708 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001709 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001710 }
1711
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001712 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001713 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001714 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001715 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001716 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001717 }
Barry Mienydd671972010-10-04 16:33:58 +02001718
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001719 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001720 }
Barry Mienydd671972010-10-04 16:33:58 +02001721
Derek Jonesd10e8962010-03-02 17:10:36 -06001722 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001723 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001724 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001725 $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 -06001726 }
Barry Mienydd671972010-10-04 16:33:58 +02001727
Derek Jonesd10e8962010-03-02 17:10:36 -06001728 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001729 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001730 }
1731
1732 // --------------------------------------------------------------------
1733
1734 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001735 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001736 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001737 * @param array
1738 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001739 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001740 * @return object
1741 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001742 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001743 {
1744 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001745
Derek Jonesd10e8962010-03-02 17:10:36 -06001746 if ( ! is_array($key))
1747 {
1748 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001749 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001750
Andrey Andreevfe642da2012-06-16 03:47:33 +03001751 is_bool($escape) OR $escape = $this->_protect_identifiers;
1752
Derek Jonesd10e8962010-03-02 17:10:36 -06001753 foreach ($key as $k => $v)
1754 {
1755 $index_set = FALSE;
1756 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001757 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001758 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001759 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001760 {
1761 $index_set = TRUE;
1762 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001763
Andrey Andreevfe642da2012-06-16 03:47:33 +03001764 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001765 }
1766
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001767 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001768 {
1769 return $this->display_error('db_batch_missing_index');
1770 }
1771
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001772 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001773 }
Barry Mienydd671972010-10-04 16:33:58 +02001774
Derek Jonesd10e8962010-03-02 17:10:36 -06001775 return $this;
1776 }
1777
Derek Allard2067d1a2008-11-13 22:59:24 +00001778 // --------------------------------------------------------------------
1779
1780 /**
1781 * Empty Table
1782 *
1783 * Compiles a delete string and runs "DELETE FROM table"
1784 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001785 * @param string the table to empty
1786 * @return object
1787 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001788 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001790 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001792 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001794 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001795 }
1796
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001797 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001798 }
1799 else
1800 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001801 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001802 }
1803
1804 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001805 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001806 return $this->query($sql);
1807 }
1808
1809 // --------------------------------------------------------------------
1810
1811 /**
1812 * Truncate
1813 *
1814 * Compiles a truncate string and runs the query
1815 * If the database does not support the truncate() command
1816 * This function maps to "DELETE FROM table"
1817 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001818 * @param string the table to truncate
1819 * @return object
1820 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001821 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001822 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001823 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001824 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001825 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001826 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001827 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001828 }
1829
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001830 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001831 }
1832 else
1833 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001834 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001835 }
1836
1837 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001838 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001839 return $this->query($sql);
1840 }
WanWizard7219c072011-12-28 14:09:05 +01001841
Kyle Farris0c147b32011-08-26 02:29:31 -04001842 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001843
Kyle Farris0c147b32011-08-26 02:29:31 -04001844 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001845 * Truncate statement
1846 *
1847 * Generates a platform-specific truncate string from the supplied data
1848 *
1849 * If the database does not support the truncate() command,
1850 * then this method maps to 'DELETE FROM table'
1851 *
1852 * @param string the table name
1853 * @return string
1854 */
1855 protected function _truncate($table)
1856 {
1857 return 'TRUNCATE '.$table;
1858 }
1859
1860 // --------------------------------------------------------------------
1861
1862 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001863 * Get DELETE query string
1864 *
1865 * Compiles a delete query string and returns the sql
1866 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001867 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001868 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001869 * @return string
1870 */
1871 public function get_compiled_delete($table = '', $reset = TRUE)
1872 {
1873 $this->return_delete_sql = TRUE;
1874 $sql = $this->delete($table, '', NULL, $reset);
1875 $this->return_delete_sql = FALSE;
1876 return $sql;
1877 }
WanWizard7219c072011-12-28 14:09:05 +01001878
Derek Allard2067d1a2008-11-13 22:59:24 +00001879 // --------------------------------------------------------------------
1880
1881 /**
1882 * Delete
1883 *
1884 * Compiles a delete string and runs the query
1885 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001886 * @param mixed the table(s) to delete from. String or array
1887 * @param mixed the where clause
1888 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001889 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001890 * @return object
1891 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001892 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001893 {
1894 // Combine any cached components with the current statements
1895 $this->_merge_cache();
1896
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001897 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001898 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001899 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001900 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001901 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001902 }
1903
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001904 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001905 }
1906 elseif (is_array($table))
1907 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001908 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001909 {
1910 $this->delete($single_table, $where, $limit, FALSE);
1911 }
1912
1913 $this->_reset_write();
1914 return;
1915 }
1916 else
1917 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001918 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001919 }
1920
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001921 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001922 {
1923 $this->where($where);
1924 }
1925
Andrey Andreev650b4c02012-06-11 12:07:15 +03001926 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001927 {
1928 $this->limit($limit);
1929 }
1930
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001931 if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001932 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001933 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001934 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001935
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001936 $sql = $this->_delete($table, $this->qb_where, $this->qb_like, $this->qb_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001937 if ($reset_data)
1938 {
1939 $this->_reset_write();
1940 }
WanWizard7219c072011-12-28 14:09:05 +01001941
Andrey Andreev24276a32012-01-08 02:44:38 +02001942 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001943 }
WanWizard7219c072011-12-28 14:09:05 +01001944
Derek Allard2067d1a2008-11-13 22:59:24 +00001945 // --------------------------------------------------------------------
1946
1947 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001948 * Delete statement
1949 *
1950 * Generates a platform-specific delete string from the supplied data
1951 *
1952 * @param string the table name
1953 * @param array the where clause
1954 * @param array the like clause
1955 * @param string the limit clause
1956 * @return string
1957 */
1958 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
1959 {
1960 $conditions = array();
1961
1962 empty($where) OR $conditions[] = implode(' ', $where);
1963 empty($like) OR $conditions[] = implode(' ', $like);
1964
1965 return 'DELETE FROM '.$table
1966 .(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '')
Andrey Andreev650b4c02012-06-11 12:07:15 +03001967 .($limit ? ' LIMIT '.(int) $limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001968 }
1969
1970 // --------------------------------------------------------------------
1971
1972 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001973 * DB Prefix
1974 *
1975 * Prepends a database prefix if one exists in configuration
1976 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001977 * @param string the table
1978 * @return string
1979 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001980 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001981 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001982 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001983 {
1984 $this->display_error('db_table_name_required');
1985 }
1986
1987 return $this->dbprefix.$table;
1988 }
1989
1990 // --------------------------------------------------------------------
1991
1992 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001993 * Set DB Prefix
1994 *
1995 * Set's the DB Prefix to something new without needing to reconnect
1996 *
1997 * @param string the prefix
1998 * @return string
1999 */
2000 public function set_dbprefix($prefix = '')
2001 {
2002 return $this->dbprefix = $prefix;
2003 }
2004
2005 // --------------------------------------------------------------------
2006
2007 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002008 * Track Aliases
2009 *
2010 * Used to track SQL statements written with aliased tables.
2011 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002012 * @param string The table to inspect
2013 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002014 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002015 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002016 {
2017 if (is_array($table))
2018 {
2019 foreach ($table as $t)
2020 {
2021 $this->_track_aliases($t);
2022 }
2023 return;
2024 }
Barry Mienydd671972010-10-04 16:33:58 +02002025
Derek Jones37f4b9c2011-07-01 17:56:50 -05002026 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002027 // the string into discreet statements
2028 if (strpos($table, ',') !== FALSE)
2029 {
2030 return $this->_track_aliases(explode(',', $table));
2031 }
Barry Mienydd671972010-10-04 16:33:58 +02002032
Derek Allard2067d1a2008-11-13 22:59:24 +00002033 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002034 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002035 {
2036 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002037 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002038
Derek Allard2067d1a2008-11-13 22:59:24 +00002039 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002040 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002041
Derek Allard2067d1a2008-11-13 22:59:24 +00002042 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002043 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002044 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002045 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002046 }
2047 }
2048 }
WanWizard7219c072011-12-28 14:09:05 +01002049
Derek Allard2067d1a2008-11-13 22:59:24 +00002050 // --------------------------------------------------------------------
2051
2052 /**
2053 * Compile the SELECT statement
2054 *
2055 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05002056 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00002057 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002058 * @return string
2059 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002060 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002061 {
2062 // Combine any cached components with the current statements
2063 $this->_merge_cache();
2064
Derek Allard2067d1a2008-11-13 22:59:24 +00002065 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002066 if ($select_override !== FALSE)
2067 {
2068 $sql = $select_override;
2069 }
2070 else
2071 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002072 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002073
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002074 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002075 {
Barry Mienydd671972010-10-04 16:33:58 +02002076 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002077 }
2078 else
Barry Mienydd671972010-10-04 16:33:58 +02002079 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002080 // Cycle through the "select" portion of the query and prep each column name.
2081 // The reason we protect identifiers here rather then in the select() function
2082 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002083 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002084 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002085 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002086 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002087 }
Barry Mienydd671972010-10-04 16:33:58 +02002088
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002089 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002090 }
2091 }
2092
Derek Allard2067d1a2008-11-13 22:59:24 +00002093 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002094 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002095 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002096 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002097 }
2098
Derek Allard2067d1a2008-11-13 22:59:24 +00002099 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002100 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002101 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002102 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002103 }
2104
Derek Allard2067d1a2008-11-13 22:59:24 +00002105 // Write the "WHERE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002106 if (count($this->qb_where) > 0 OR count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002107 {
Greg Akere156c6e2011-04-20 16:03:04 -05002108 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00002109 }
2110
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002111 $sql .= implode("\n", $this->qb_where);
Derek Allard2067d1a2008-11-13 22:59:24 +00002112
Derek Allard2067d1a2008-11-13 22:59:24 +00002113 // Write the "LIKE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002114 if (count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002115 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002116 if (count($this->qb_where) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002117 {
2118 $sql .= "\nAND ";
2119 }
2120
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002121 $sql .= implode("\n", $this->qb_like);
Derek Allard2067d1a2008-11-13 22:59:24 +00002122 }
2123
Derek Allard2067d1a2008-11-13 22:59:24 +00002124 // Write the "GROUP BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002125 if (count($this->qb_groupby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002126 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002127 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002128 }
2129
Derek Allard2067d1a2008-11-13 22:59:24 +00002130 // Write the "HAVING" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002131 if (count($this->qb_having) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002132 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002133 $sql .= "\nHAVING ".implode("\n", $this->qb_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00002134 }
2135
Derek Allard2067d1a2008-11-13 22:59:24 +00002136 // Write the "ORDER BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002137 if (count($this->qb_orderby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002138 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002139 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002140 }
2141
Derek Allard2067d1a2008-11-13 22:59:24 +00002142 // Write the "LIMIT" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002143 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002144 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002145 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00002146 }
2147
2148 return $sql;
2149 }
2150
2151 // --------------------------------------------------------------------
2152
2153 /**
2154 * Object to Array
2155 *
2156 * Takes an object as input and converts the class variables to array key/vals
2157 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002158 * @param object
2159 * @return array
2160 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002161 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002162 {
2163 if ( ! is_object($object))
2164 {
2165 return $object;
2166 }
Barry Mienydd671972010-10-04 16:33:58 +02002167
Derek Allard2067d1a2008-11-13 22:59:24 +00002168 $array = array();
2169 foreach (get_object_vars($object) as $key => $val)
2170 {
2171 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002172 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002173 {
2174 $array[$key] = $val;
2175 }
2176 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002177
2178 return $array;
2179 }
Barry Mienydd671972010-10-04 16:33:58 +02002180
Derek Jonesd10e8962010-03-02 17:10:36 -06002181 // --------------------------------------------------------------------
2182
2183 /**
2184 * Object to Array
2185 *
2186 * Takes an object as input and converts the class variables to array key/vals
2187 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002188 * @param object
2189 * @return array
2190 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002191 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002192 {
2193 if ( ! is_object($object))
2194 {
2195 return $object;
2196 }
Barry Mienydd671972010-10-04 16:33:58 +02002197
Derek Jonesd10e8962010-03-02 17:10:36 -06002198 $array = array();
2199 $out = get_object_vars($object);
2200 $fields = array_keys($out);
2201
2202 foreach ($fields as $val)
2203 {
2204 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002205 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002206 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002207 $i = 0;
2208 foreach ($out[$val] as $data)
2209 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002210 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002211 }
2212 }
2213 }
2214
Derek Allard2067d1a2008-11-13 22:59:24 +00002215 return $array;
2216 }
Barry Mienydd671972010-10-04 16:33:58 +02002217
Derek Allard2067d1a2008-11-13 22:59:24 +00002218 // --------------------------------------------------------------------
2219
2220 /**
2221 * Start Cache
2222 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002223 * Starts 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 start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002228 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002229 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002230 }
2231
2232 // --------------------------------------------------------------------
2233
2234 /**
2235 * Stop Cache
2236 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002237 * Stops QB caching
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 stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002242 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002243 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002244 }
2245
2246 // --------------------------------------------------------------------
2247
2248 /**
2249 * Flush Cache
2250 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002251 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002252 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002253 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002254 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002255 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002256 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002257 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002258 'qb_cache_select' => array(),
2259 'qb_cache_from' => array(),
2260 'qb_cache_join' => array(),
2261 'qb_cache_where' => array(),
2262 'qb_cache_like' => array(),
2263 'qb_cache_groupby' => array(),
2264 'qb_cache_having' => array(),
2265 'qb_cache_orderby' => array(),
2266 'qb_cache_set' => array(),
2267 'qb_cache_exists' => array(),
2268 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002269 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002270 }
2271
2272 // --------------------------------------------------------------------
2273
2274 /**
2275 * Merge Cache
2276 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002277 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002278 * locally called ones.
2279 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002280 * @return void
2281 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002282 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002283 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002284 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002285 {
2286 return;
2287 }
2288
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002289 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002290 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002291 $qb_variable = 'qb_'.$val;
2292 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002293
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002294 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002295 {
2296 continue;
2297 }
2298
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002299 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002300 }
2301
2302 // If we are "protecting identifiers" we need to examine the "from"
2303 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002304 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002305 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002306 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002307 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002308
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002309 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002310 }
WanWizard7219c072011-12-28 14:09:05 +01002311
Kyle Farris0c147b32011-08-26 02:29:31 -04002312 // --------------------------------------------------------------------
2313
2314 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002315 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002316 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002317 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002318 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002319 * @return void
2320 */
2321 public function reset_query()
2322 {
2323 $this->_reset_select();
2324 $this->_reset_write();
2325 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002326
2327 // --------------------------------------------------------------------
2328
2329 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002330 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002331 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002332 * @param array An array of fields to reset
2333 * @return void
2334 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002335 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002336 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002337 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002338 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002339 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002340 {
2341 $this->$item = $default_value;
2342 }
2343 }
2344 }
2345
2346 // --------------------------------------------------------------------
2347
2348 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002349 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002350 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002351 * @return void
2352 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002353 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002354 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002355 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002356 'qb_select' => array(),
2357 'qb_from' => array(),
2358 'qb_join' => array(),
2359 'qb_where' => array(),
2360 'qb_like' => array(),
2361 'qb_groupby' => array(),
2362 'qb_having' => array(),
2363 'qb_orderby' => array(),
2364 'qb_wherein' => array(),
2365 'qb_aliased_tables' => array(),
2366 'qb_no_escape' => array(),
2367 'qb_distinct' => FALSE,
2368 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002369 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002370 )
2371 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002372 }
Barry Mienydd671972010-10-04 16:33:58 +02002373
Derek Allard2067d1a2008-11-13 22:59:24 +00002374 // --------------------------------------------------------------------
2375
2376 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002377 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002378 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002379 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002380 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002381 * @return void
2382 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002383 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002384 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002385 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002386 'qb_set' => array(),
2387 'qb_from' => array(),
2388 'qb_where' => array(),
2389 'qb_like' => array(),
2390 'qb_orderby' => array(),
2391 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002392 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002393 )
2394 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002395 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002396
Derek Allard2067d1a2008-11-13 22:59:24 +00002397}
2398
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002399/* End of file DB_query_builder.php */
Andrey Andreeve4c30192012-06-04 15:08:24 +03002400/* Location: ./system/database/DB_query_builder.php */