blob: d21f15066d3af01fc88b8694b617576c8ea55a0e [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 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001045 is_null($value) OR $this->qb_limit = (int) $value;
1046 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001047
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 return $this;
1049 }
Barry Mienydd671972010-10-04 16:33:58 +02001050
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 // --------------------------------------------------------------------
1052
1053 /**
1054 * Sets the OFFSET value
1055 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001056 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 * @return object
1058 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001059 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001061 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 return $this;
1063 }
Barry Mienydd671972010-10-04 16:33:58 +02001064
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 // --------------------------------------------------------------------
1066
1067 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001068 * The "set" function.
1069 *
1070 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 * @param mixed
1073 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001074 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 * @return object
1076 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001077 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 {
1079 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001080
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 if ( ! is_array($key))
1082 {
1083 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001084 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001085
Andrey Andreevfe642da2012-06-16 03:47:33 +03001086 is_bool($escape) OR $escape = $this->_protect_identifiers;
1087
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 foreach ($key as $k => $v)
1089 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001090 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1091 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 }
Barry Mienydd671972010-10-04 16:33:58 +02001093
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 return $this;
1095 }
WanWizard7219c072011-12-28 14:09:05 +01001096
Kyle Farris0c147b32011-08-26 02:29:31 -04001097 // --------------------------------------------------------------------
1098
1099 /**
1100 * Get SELECT query string
1101 *
1102 * Compiles a SELECT query string and returns the sql.
1103 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001104 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001105 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001106 * @return string
1107 */
WanWizard7219c072011-12-28 14:09:05 +01001108 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001109 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001110 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001111 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001112 $this->_track_aliases($table);
1113 $this->from($table);
1114 }
WanWizard7219c072011-12-28 14:09:05 +01001115
Andrey Andreev650b4c02012-06-11 12:07:15 +03001116 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001117
Kyle Farris0c147b32011-08-26 02:29:31 -04001118 if ($reset === TRUE)
1119 {
1120 $this->_reset_select();
1121 }
WanWizard7219c072011-12-28 14:09:05 +01001122
Kyle Farris0c147b32011-08-26 02:29:31 -04001123 return $select;
1124 }
WanWizard7219c072011-12-28 14:09:05 +01001125
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 // --------------------------------------------------------------------
1127
1128 /**
1129 * Get
1130 *
1131 * Compiles the select statement based on the other functions called
1132 * and runs the query
1133 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 * @param string the table
1135 * @param string the limit clause
1136 * @param string the offset clause
1137 * @return object
1138 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001139 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001141 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 {
1143 $this->_track_aliases($table);
1144 $this->from($table);
1145 }
Barry Mienydd671972010-10-04 16:33:58 +02001146
Andrey Andreev650b4c02012-06-11 12:07:15 +03001147 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 {
1149 $this->limit($limit, $offset);
1150 }
Barry Mienydd671972010-10-04 16:33:58 +02001151
Andrey Andreev24276a32012-01-08 02:44:38 +02001152 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 $this->_reset_select();
1154 return $result;
1155 }
1156
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001157 // --------------------------------------------------------------------
1158
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 /**
1160 * "Count All Results" query
1161 *
Barry Mienydd671972010-10-04 16:33:58 +02001162 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001163 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 * @param string
1166 * @return string
1167 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001168 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001170 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 {
1172 $this->_track_aliases($table);
1173 $this->from($table);
1174 }
Barry Mienydd671972010-10-04 16:33:58 +02001175
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001176 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001178
Purwandi1d160e72012-01-09 16:33:28 +07001179 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001181 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 }
1183
Purwandi1d160e72012-01-09 16:33:28 +07001184 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001185 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001187
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 // --------------------------------------------------------------------
1189
1190 /**
1191 * Get_Where
1192 *
1193 * Allows the where clause, limit and offset to be added directly
1194 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 * @param string the where clause
1196 * @param string the limit clause
1197 * @param string the offset clause
1198 * @return object
1199 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001200 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001202 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 {
1204 $this->from($table);
1205 }
1206
1207 if ( ! is_null($where))
1208 {
1209 $this->where($where);
1210 }
Barry Mienydd671972010-10-04 16:33:58 +02001211
Andrey Andreev650b4c02012-06-11 12:07:15 +03001212 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 {
1214 $this->limit($limit, $offset);
1215 }
Barry Mienydd671972010-10-04 16:33:58 +02001216
Andrey Andreev24276a32012-01-08 02:44:38 +02001217 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001218 $this->_reset_select();
1219 return $result;
1220 }
1221
1222 // --------------------------------------------------------------------
1223
1224 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001225 * Insert_Batch
1226 *
1227 * Compiles batch insert strings and runs the queries
1228 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001229 * @param string the table to retrieve the results from
1230 * @param array an associative array of insert values
1231 * @return object
1232 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001233 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001234 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001235 if ( ! is_null($set))
1236 {
1237 $this->set_insert_batch($set);
1238 }
Barry Mienydd671972010-10-04 16:33:58 +02001239
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001240 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001241 {
1242 if ($this->db_debug)
1243 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001244 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001245 return $this->display_error('db_must_use_set');
1246 }
1247 return FALSE;
1248 }
1249
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001250 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001251 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001252 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001253 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001254 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001255 }
Barry Mienydd671972010-10-04 16:33:58 +02001256
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001257 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001258 }
1259
1260 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001261 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001262 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001263 $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 -06001264 }
Barry Mienydd671972010-10-04 16:33:58 +02001265
Derek Jonesd10e8962010-03-02 17:10:36 -06001266 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001267 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001268 }
1269
1270 // --------------------------------------------------------------------
1271
1272 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001273 * Insert_batch statement
1274 *
1275 * Generates a platform-specific insert string from the supplied data.
1276 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001277 * @param string the table name
1278 * @param array the insert keys
1279 * @param array the insert values
1280 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001281 */
1282 protected function _insert_batch($table, $keys, $values)
1283 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001284 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001285 }
1286
1287 // --------------------------------------------------------------------
1288
1289 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001290 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001291 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001292 * @param mixed
1293 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001294 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001295 * @return object
1296 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001297 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001298 {
1299 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001300
Derek Jonesd10e8962010-03-02 17:10:36 -06001301 if ( ! is_array($key))
1302 {
1303 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001304 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001305
Andrey Andreevfe642da2012-06-16 03:47:33 +03001306 is_bool($escape) OR $escape = $this->_protect_identifiers;
1307
Iban Eguia3c0a4522012-04-15 13:30:44 +02001308 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001309 sort($keys);
1310
1311 foreach ($key as $row)
1312 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001313 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001314 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1315 {
1316 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001317 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001318 return;
1319 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001320
Barry Mienydd671972010-10-04 16:33:58 +02001321 ksort($row); // puts $row in the same order as our keys
1322
Andrey Andreev650b4c02012-06-11 12:07:15 +03001323 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001324 {
1325 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001326 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001327 {
Barry Mienydd671972010-10-04 16:33:58 +02001328 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001329 }
1330
Andrey Andreev650b4c02012-06-11 12:07:15 +03001331 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001332 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001333
1334 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001335 }
1336
1337 foreach ($keys as $k)
1338 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001339 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001340 }
Barry Mienydd671972010-10-04 16:33:58 +02001341
Derek Jonesd10e8962010-03-02 17:10:36 -06001342 return $this;
1343 }
WanWizard7219c072011-12-28 14:09:05 +01001344
Kyle Farris0c147b32011-08-26 02:29:31 -04001345 // --------------------------------------------------------------------
1346
1347 /**
1348 * Get INSERT query string
1349 *
1350 * Compiles an insert query and returns the sql
1351 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001352 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001353 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001354 * @return string
1355 */
1356 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001357 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001358 if ($this->_validate_insert($table) === FALSE)
1359 {
1360 return FALSE;
1361 }
WanWizard7219c072011-12-28 14:09:05 +01001362
Kyle Farris76116012011-08-31 11:17:48 -04001363 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001364 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001365 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001366 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001367 array_keys($this->qb_set),
1368 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001369 );
WanWizard7219c072011-12-28 14:09:05 +01001370
Kyle Farris0c147b32011-08-26 02:29:31 -04001371 if ($reset === TRUE)
1372 {
1373 $this->_reset_write();
1374 }
WanWizard7219c072011-12-28 14:09:05 +01001375
Kyle Farris0c147b32011-08-26 02:29:31 -04001376 return $sql;
1377 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001378
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 // --------------------------------------------------------------------
1380
1381 /**
1382 * Insert
1383 *
1384 * Compiles an insert string and runs the query
1385 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001386 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001387 * @param array an associative array of insert values
1388 * @return object
1389 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001390 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001391 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001392 if ( ! is_null($set))
1393 {
1394 $this->set($set);
1395 }
WanWizard7219c072011-12-28 14:09:05 +01001396
Kyle Farris0c147b32011-08-26 02:29:31 -04001397 if ($this->_validate_insert($table) === FALSE)
1398 {
1399 return FALSE;
1400 }
WanWizard7219c072011-12-28 14:09:05 +01001401
Kyle Farris76116012011-08-31 11:17:48 -04001402 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001403 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001404 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001405 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001406 array_keys($this->qb_set),
1407 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001408 );
Barry Mienydd671972010-10-04 16:33:58 +02001409
Kyle Farris0c147b32011-08-26 02:29:31 -04001410 $this->_reset_write();
1411 return $this->query($sql);
1412 }
WanWizard7219c072011-12-28 14:09:05 +01001413
Kyle Farris0c147b32011-08-26 02:29:31 -04001414 // --------------------------------------------------------------------
1415
1416 /**
Andrey Andreev65d537c2012-04-05 14:11:41 +03001417 * Insert statement
1418 *
1419 * Generates a platform-specific insert string from the supplied data
1420 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001421 * @param string the table name
1422 * @param array the insert keys
1423 * @param array the insert values
1424 * @return string
Andrey Andreev65d537c2012-04-05 14:11:41 +03001425 */
1426 protected function _insert($table, $keys, $values)
1427 {
1428 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1429 }
1430
1431 // --------------------------------------------------------------------
1432
1433 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001434 * Validate Insert
1435 *
1436 * This method is used by both insert() and get_compiled_insert() to
1437 * validate that the there data is actually being set and that table
1438 * has been chosen to be inserted into.
1439 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001440 * @param string the table to insert data into
1441 * @return string
1442 */
WanWizard7219c072011-12-28 14:09:05 +01001443 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001444 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001445 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001446 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001447 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 }
1449
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001450 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001451 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001452 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001453 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001454 elseif ( ! isset($this->qb_from[0]))
1455 {
1456 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1457 }
WanWizard7219c072011-12-28 14:09:05 +01001458
Kyle Farris0c147b32011-08-26 02:29:31 -04001459 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001460 }
Barry Mienydd671972010-10-04 16:33:58 +02001461
Phil Sturgeon9789f322011-07-15 15:14:05 -06001462 // --------------------------------------------------------------------
1463
1464 /**
1465 * Replace
1466 *
1467 * Compiles an replace into string and runs the query
1468 *
1469 * @param string the table to replace data into
1470 * @param array an associative array of insert values
1471 * @return object
1472 */
1473 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001474 {
1475 if ( ! is_null($set))
1476 {
1477 $this->set($set);
1478 }
Barry Mienydd671972010-10-04 16:33:58 +02001479
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001480 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001481 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001482 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001483 }
1484
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001485 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001486 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001487 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001488 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001489 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001490 }
Barry Mienydd671972010-10-04 16:33:58 +02001491
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001492 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001493 }
1494
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001495 $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 +00001496
Derek Jonesd10e8962010-03-02 17:10:36 -06001497 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001498 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001499 }
WanWizard7219c072011-12-28 14:09:05 +01001500
Kyle Farris0c147b32011-08-26 02:29:31 -04001501 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001502
Kyle Farris0c147b32011-08-26 02:29:31 -04001503 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001504 * Replace statement
1505 *
1506 * Generates a platform-specific replace string from the supplied data
1507 *
1508 * @param string the table name
1509 * @param array the insert keys
1510 * @param array the insert values
1511 * @return string
1512 */
1513 protected function _replace($table, $keys, $values)
1514 {
1515 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1516 }
1517
1518 // --------------------------------------------------------------------
1519
1520 /**
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001521 * From Tables
1522 *
1523 * This public function implicitly groups FROM tables so there is no confusion
1524 * about operator precedence in harmony with SQL standards
1525 *
1526 * @param array
1527 * @return string
1528 */
1529 protected function _from_tables($tables)
1530 {
1531 is_array($tables) OR $tables = array($tables);
1532
1533 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
1534 }
1535
1536 // --------------------------------------------------------------------
1537
1538 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001539 * Get UPDATE query string
1540 *
1541 * Compiles an update query and returns the sql
1542 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001543 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001544 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001545 * @return string
1546 */
1547 public function get_compiled_update($table = '', $reset = TRUE)
1548 {
1549 // Combine any cached components with the current statements
1550 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001551
Kyle Farris0c147b32011-08-26 02:29:31 -04001552 if ($this->_validate_update($table) === FALSE)
1553 {
1554 return FALSE;
1555 }
WanWizard7219c072011-12-28 14:09:05 +01001556
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001557 $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 +01001558
Kyle Farris0c147b32011-08-26 02:29:31 -04001559 if ($reset === TRUE)
1560 {
1561 $this->_reset_write();
1562 }
WanWizard7219c072011-12-28 14:09:05 +01001563
Kyle Farris0c147b32011-08-26 02:29:31 -04001564 return $sql;
1565 }
WanWizard7219c072011-12-28 14:09:05 +01001566
Derek Allard2067d1a2008-11-13 22:59:24 +00001567 // --------------------------------------------------------------------
1568
1569 /**
1570 * Update
1571 *
1572 * Compiles an update string and runs the query
1573 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001574 * @param string the table to retrieve the results from
1575 * @param array an associative array of update values
1576 * @param mixed the where clause
1577 * @return object
1578 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001579 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001580 {
1581 // Combine any cached components with the current statements
1582 $this->_merge_cache();
1583
1584 if ( ! is_null($set))
1585 {
1586 $this->set($set);
1587 }
Barry Mienydd671972010-10-04 16:33:58 +02001588
Kyle Farris0c147b32011-08-26 02:29:31 -04001589 if ($this->_validate_update($table) === FALSE)
1590 {
1591 return FALSE;
1592 }
1593
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001594 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001595 {
1596 $this->where($where);
1597 }
1598
Andrey Andreev650b4c02012-06-11 12:07:15 +03001599 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001600 {
1601 $this->limit($limit);
1602 }
1603
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001604 $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 +00001605
Kyle Farris0c147b32011-08-26 02:29:31 -04001606 $this->_reset_write();
1607 return $this->query($sql);
1608 }
WanWizard7219c072011-12-28 14:09:05 +01001609
Kyle Farris0c147b32011-08-26 02:29:31 -04001610 // --------------------------------------------------------------------
1611
1612 /**
Andrey Andreev975034d2012-04-05 15:09:55 +03001613 * Update statement
1614 *
1615 * Generates a platform-specific update string from the supplied data
1616 *
1617 * @param string the table name
1618 * @param array the update data
1619 * @param array the where clause
1620 * @param array the orderby clause
1621 * @param array the limit clause
Andrey Andreev00541ae2012-04-09 11:43:10 +03001622 * @param array the like clause
Andrey Andreev975034d2012-04-05 15:09:55 +03001623 * @return string
1624 */
Andrey Andreev00541ae2012-04-09 11:43:10 +03001625 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Andrey Andreev975034d2012-04-05 15:09:55 +03001626 {
1627 foreach ($values as $key => $val)
1628 {
1629 $valstr[] = $key.' = '.$val;
1630 }
1631
Andrey Andreev00541ae2012-04-09 11:43:10 +03001632 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
1633
1634 if ( ! empty($like))
1635 {
1636 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
1637 }
1638
Andrey Andreev975034d2012-04-05 15:09:55 +03001639 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev00541ae2012-04-09 11:43:10 +03001640 .$where
Andrey Andreev975034d2012-04-05 15:09:55 +03001641 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
1642 .($limit ? ' LIMIT '.$limit : '');
1643 }
1644
1645 // --------------------------------------------------------------------
1646
1647 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001648 * Validate Update
1649 *
1650 * This method is used by both update() and get_compiled_update() to
1651 * validate that data is actually being set and that a table has been
1652 * chosen to be update.
1653 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001654 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001655 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001656 */
1657 protected function _validate_update($table = '')
1658 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001659 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001660 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001661 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001662 }
1663
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001664 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001665 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001666 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001667 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001668 elseif ( ! isset($this->qb_from[0]))
1669 {
1670 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1671 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001672
1673 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001674 }
WanWizard7219c072011-12-28 14:09:05 +01001675
Derek Jonesd10e8962010-03-02 17:10:36 -06001676 // --------------------------------------------------------------------
1677
1678 /**
1679 * Update_Batch
1680 *
1681 * Compiles an update string and runs the query
1682 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001683 * @param string the table to retrieve the results from
1684 * @param array an associative array of update values
1685 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001686 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001687 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001688 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001689 {
1690 // Combine any cached components with the current statements
1691 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001692
Derek Jonesd10e8962010-03-02 17:10:36 -06001693 if (is_null($index))
1694 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001695 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001696 }
1697
1698 if ( ! is_null($set))
1699 {
1700 $this->set_update_batch($set, $index);
1701 }
1702
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001703 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001704 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001705 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001706 }
1707
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001708 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001709 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001710 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001711 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001712 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001713 }
Barry Mienydd671972010-10-04 16:33:58 +02001714
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001715 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001716 }
Barry Mienydd671972010-10-04 16:33:58 +02001717
Derek Jonesd10e8962010-03-02 17:10:36 -06001718 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001719 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001720 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001721 $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 -06001722 }
Barry Mienydd671972010-10-04 16:33:58 +02001723
Derek Jonesd10e8962010-03-02 17:10:36 -06001724 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001725 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001726 }
1727
1728 // --------------------------------------------------------------------
1729
1730 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001731 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001732 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001733 * @param array
1734 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001735 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001736 * @return object
1737 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001738 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001739 {
1740 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001741
Derek Jonesd10e8962010-03-02 17:10:36 -06001742 if ( ! is_array($key))
1743 {
1744 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001745 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001746
Andrey Andreevfe642da2012-06-16 03:47:33 +03001747 is_bool($escape) OR $escape = $this->_protect_identifiers;
1748
Derek Jonesd10e8962010-03-02 17:10:36 -06001749 foreach ($key as $k => $v)
1750 {
1751 $index_set = FALSE;
1752 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001753 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001754 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001755 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001756 {
1757 $index_set = TRUE;
1758 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001759
Andrey Andreevfe642da2012-06-16 03:47:33 +03001760 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001761 }
1762
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001763 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001764 {
1765 return $this->display_error('db_batch_missing_index');
1766 }
1767
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001768 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001769 }
Barry Mienydd671972010-10-04 16:33:58 +02001770
Derek Jonesd10e8962010-03-02 17:10:36 -06001771 return $this;
1772 }
1773
Derek Allard2067d1a2008-11-13 22:59:24 +00001774 // --------------------------------------------------------------------
1775
1776 /**
1777 * Empty Table
1778 *
1779 * Compiles a delete string and runs "DELETE FROM table"
1780 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001781 * @param string the table to empty
1782 * @return object
1783 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001784 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001785 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001786 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001787 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001788 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001790 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 }
1792
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001793 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001794 }
1795 else
1796 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001797 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001798 }
1799
1800 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001801 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001802 return $this->query($sql);
1803 }
1804
1805 // --------------------------------------------------------------------
1806
1807 /**
1808 * Truncate
1809 *
1810 * Compiles a truncate string and runs the query
1811 * If the database does not support the truncate() command
1812 * This function maps to "DELETE FROM table"
1813 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 * @param string the table to truncate
1815 * @return object
1816 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001817 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001818 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001819 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001821 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001822 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001823 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001824 }
1825
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001826 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 }
1828 else
1829 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001830 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001831 }
1832
1833 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001834 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001835 return $this->query($sql);
1836 }
WanWizard7219c072011-12-28 14:09:05 +01001837
Kyle Farris0c147b32011-08-26 02:29:31 -04001838 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001839
Kyle Farris0c147b32011-08-26 02:29:31 -04001840 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001841 * Truncate statement
1842 *
1843 * Generates a platform-specific truncate string from the supplied data
1844 *
1845 * If the database does not support the truncate() command,
1846 * then this method maps to 'DELETE FROM table'
1847 *
1848 * @param string the table name
1849 * @return string
1850 */
1851 protected function _truncate($table)
1852 {
1853 return 'TRUNCATE '.$table;
1854 }
1855
1856 // --------------------------------------------------------------------
1857
1858 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001859 * Get DELETE query string
1860 *
1861 * Compiles a delete query string and returns the sql
1862 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001863 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001864 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001865 * @return string
1866 */
1867 public function get_compiled_delete($table = '', $reset = TRUE)
1868 {
1869 $this->return_delete_sql = TRUE;
1870 $sql = $this->delete($table, '', NULL, $reset);
1871 $this->return_delete_sql = FALSE;
1872 return $sql;
1873 }
WanWizard7219c072011-12-28 14:09:05 +01001874
Derek Allard2067d1a2008-11-13 22:59:24 +00001875 // --------------------------------------------------------------------
1876
1877 /**
1878 * Delete
1879 *
1880 * Compiles a delete string and runs the query
1881 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001882 * @param mixed the table(s) to delete from. String or array
1883 * @param mixed the where clause
1884 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001885 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001886 * @return object
1887 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001888 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001889 {
1890 // Combine any cached components with the current statements
1891 $this->_merge_cache();
1892
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001893 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001894 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001895 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001896 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001897 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001898 }
1899
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001900 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001901 }
1902 elseif (is_array($table))
1903 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001904 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001905 {
1906 $this->delete($single_table, $where, $limit, FALSE);
1907 }
1908
1909 $this->_reset_write();
1910 return;
1911 }
1912 else
1913 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001914 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001915 }
1916
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001917 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001918 {
1919 $this->where($where);
1920 }
1921
Andrey Andreev650b4c02012-06-11 12:07:15 +03001922 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001923 {
1924 $this->limit($limit);
1925 }
1926
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001927 if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001928 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001929 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001930 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001931
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001932 $sql = $this->_delete($table, $this->qb_where, $this->qb_like, $this->qb_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001933 if ($reset_data)
1934 {
1935 $this->_reset_write();
1936 }
WanWizard7219c072011-12-28 14:09:05 +01001937
Andrey Andreev24276a32012-01-08 02:44:38 +02001938 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001939 }
WanWizard7219c072011-12-28 14:09:05 +01001940
Derek Allard2067d1a2008-11-13 22:59:24 +00001941 // --------------------------------------------------------------------
1942
1943 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001944 * Delete statement
1945 *
1946 * Generates a platform-specific delete string from the supplied data
1947 *
1948 * @param string the table name
1949 * @param array the where clause
1950 * @param array the like clause
1951 * @param string the limit clause
1952 * @return string
1953 */
1954 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
1955 {
1956 $conditions = array();
1957
1958 empty($where) OR $conditions[] = implode(' ', $where);
1959 empty($like) OR $conditions[] = implode(' ', $like);
1960
1961 return 'DELETE FROM '.$table
1962 .(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '')
Andrey Andreev650b4c02012-06-11 12:07:15 +03001963 .($limit ? ' LIMIT '.(int) $limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001964 }
1965
1966 // --------------------------------------------------------------------
1967
1968 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001969 * DB Prefix
1970 *
1971 * Prepends a database prefix if one exists in configuration
1972 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001973 * @param string the table
1974 * @return string
1975 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001976 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001977 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001978 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001979 {
1980 $this->display_error('db_table_name_required');
1981 }
1982
1983 return $this->dbprefix.$table;
1984 }
1985
1986 // --------------------------------------------------------------------
1987
1988 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001989 * Set DB Prefix
1990 *
1991 * Set's the DB Prefix to something new without needing to reconnect
1992 *
1993 * @param string the prefix
1994 * @return string
1995 */
1996 public function set_dbprefix($prefix = '')
1997 {
1998 return $this->dbprefix = $prefix;
1999 }
2000
2001 // --------------------------------------------------------------------
2002
2003 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002004 * Track Aliases
2005 *
2006 * Used to track SQL statements written with aliased tables.
2007 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002008 * @param string The table to inspect
2009 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002010 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002011 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002012 {
2013 if (is_array($table))
2014 {
2015 foreach ($table as $t)
2016 {
2017 $this->_track_aliases($t);
2018 }
2019 return;
2020 }
Barry Mienydd671972010-10-04 16:33:58 +02002021
Derek Jones37f4b9c2011-07-01 17:56:50 -05002022 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002023 // the string into discreet statements
2024 if (strpos($table, ',') !== FALSE)
2025 {
2026 return $this->_track_aliases(explode(',', $table));
2027 }
Barry Mienydd671972010-10-04 16:33:58 +02002028
Derek Allard2067d1a2008-11-13 22:59:24 +00002029 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002030 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002031 {
2032 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002033 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002034
Derek Allard2067d1a2008-11-13 22:59:24 +00002035 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002036 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002037
Derek Allard2067d1a2008-11-13 22:59:24 +00002038 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002039 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002040 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002041 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002042 }
2043 }
2044 }
WanWizard7219c072011-12-28 14:09:05 +01002045
Derek Allard2067d1a2008-11-13 22:59:24 +00002046 // --------------------------------------------------------------------
2047
2048 /**
2049 * Compile the SELECT statement
2050 *
2051 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05002052 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002054 * @return string
2055 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002056 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002057 {
2058 // Combine any cached components with the current statements
2059 $this->_merge_cache();
2060
Derek Allard2067d1a2008-11-13 22:59:24 +00002061 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002062 if ($select_override !== FALSE)
2063 {
2064 $sql = $select_override;
2065 }
2066 else
2067 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002068 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002069
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002070 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002071 {
Barry Mienydd671972010-10-04 16:33:58 +02002072 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002073 }
2074 else
Barry Mienydd671972010-10-04 16:33:58 +02002075 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002076 // Cycle through the "select" portion of the query and prep each column name.
2077 // The reason we protect identifiers here rather then in the select() function
2078 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002079 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002080 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002081 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002082 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002083 }
Barry Mienydd671972010-10-04 16:33:58 +02002084
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002085 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002086 }
2087 }
2088
Derek Allard2067d1a2008-11-13 22:59:24 +00002089 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002090 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002091 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002092 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002093 }
2094
Derek Allard2067d1a2008-11-13 22:59:24 +00002095 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002096 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002097 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002098 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002099 }
2100
Derek Allard2067d1a2008-11-13 22:59:24 +00002101 // Write the "WHERE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002102 if (count($this->qb_where) > 0 OR count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002103 {
Greg Akere156c6e2011-04-20 16:03:04 -05002104 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00002105 }
2106
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002107 $sql .= implode("\n", $this->qb_where);
Derek Allard2067d1a2008-11-13 22:59:24 +00002108
Derek Allard2067d1a2008-11-13 22:59:24 +00002109 // Write the "LIKE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002110 if (count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002111 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002112 if (count($this->qb_where) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002113 {
2114 $sql .= "\nAND ";
2115 }
2116
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002117 $sql .= implode("\n", $this->qb_like);
Derek Allard2067d1a2008-11-13 22:59:24 +00002118 }
2119
Derek Allard2067d1a2008-11-13 22:59:24 +00002120 // Write the "GROUP BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002121 if (count($this->qb_groupby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002122 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002123 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002124 }
2125
Derek Allard2067d1a2008-11-13 22:59:24 +00002126 // Write the "HAVING" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002127 if (count($this->qb_having) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002128 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002129 $sql .= "\nHAVING ".implode("\n", $this->qb_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00002130 }
2131
Derek Allard2067d1a2008-11-13 22:59:24 +00002132 // Write the "ORDER BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002133 if (count($this->qb_orderby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002134 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002135 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002136 }
2137
Derek Allard2067d1a2008-11-13 22:59:24 +00002138 // Write the "LIMIT" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002139 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002140 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002141 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00002142 }
2143
2144 return $sql;
2145 }
2146
2147 // --------------------------------------------------------------------
2148
2149 /**
2150 * Object to Array
2151 *
2152 * Takes an object as input and converts the class variables to array key/vals
2153 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002154 * @param object
2155 * @return array
2156 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002157 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002158 {
2159 if ( ! is_object($object))
2160 {
2161 return $object;
2162 }
Barry Mienydd671972010-10-04 16:33:58 +02002163
Derek Allard2067d1a2008-11-13 22:59:24 +00002164 $array = array();
2165 foreach (get_object_vars($object) as $key => $val)
2166 {
2167 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002168 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002169 {
2170 $array[$key] = $val;
2171 }
2172 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002173
2174 return $array;
2175 }
Barry Mienydd671972010-10-04 16:33:58 +02002176
Derek Jonesd10e8962010-03-02 17:10:36 -06002177 // --------------------------------------------------------------------
2178
2179 /**
2180 * Object to Array
2181 *
2182 * Takes an object as input and converts the class variables to array key/vals
2183 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002184 * @param object
2185 * @return array
2186 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002187 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002188 {
2189 if ( ! is_object($object))
2190 {
2191 return $object;
2192 }
Barry Mienydd671972010-10-04 16:33:58 +02002193
Derek Jonesd10e8962010-03-02 17:10:36 -06002194 $array = array();
2195 $out = get_object_vars($object);
2196 $fields = array_keys($out);
2197
2198 foreach ($fields as $val)
2199 {
2200 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002201 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002202 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002203 $i = 0;
2204 foreach ($out[$val] as $data)
2205 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002206 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002207 }
2208 }
2209 }
2210
Derek Allard2067d1a2008-11-13 22:59:24 +00002211 return $array;
2212 }
Barry Mienydd671972010-10-04 16:33:58 +02002213
Derek Allard2067d1a2008-11-13 22:59:24 +00002214 // --------------------------------------------------------------------
2215
2216 /**
2217 * Start Cache
2218 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002219 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002220 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002221 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002222 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002223 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002224 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002225 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002226 }
2227
2228 // --------------------------------------------------------------------
2229
2230 /**
2231 * Stop Cache
2232 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002233 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002234 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002235 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002236 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002237 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002238 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002239 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002240 }
2241
2242 // --------------------------------------------------------------------
2243
2244 /**
2245 * Flush Cache
2246 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002247 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002248 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002249 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002250 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002251 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002252 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002253 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002254 'qb_cache_select' => array(),
2255 'qb_cache_from' => array(),
2256 'qb_cache_join' => array(),
2257 'qb_cache_where' => array(),
2258 'qb_cache_like' => array(),
2259 'qb_cache_groupby' => array(),
2260 'qb_cache_having' => array(),
2261 'qb_cache_orderby' => array(),
2262 'qb_cache_set' => array(),
2263 'qb_cache_exists' => array(),
2264 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002265 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002266 }
2267
2268 // --------------------------------------------------------------------
2269
2270 /**
2271 * Merge Cache
2272 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002273 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002274 * locally called ones.
2275 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002276 * @return void
2277 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002278 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002279 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002280 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002281 {
2282 return;
2283 }
2284
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002285 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002286 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002287 $qb_variable = 'qb_'.$val;
2288 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002289
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002290 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002291 {
2292 continue;
2293 }
2294
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002295 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002296 }
2297
2298 // If we are "protecting identifiers" we need to examine the "from"
2299 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002300 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002301 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002302 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002303 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002304
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002305 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002306 }
WanWizard7219c072011-12-28 14:09:05 +01002307
Kyle Farris0c147b32011-08-26 02:29:31 -04002308 // --------------------------------------------------------------------
2309
2310 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002311 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002312 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002313 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002314 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002315 * @return void
2316 */
2317 public function reset_query()
2318 {
2319 $this->_reset_select();
2320 $this->_reset_write();
2321 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002322
2323 // --------------------------------------------------------------------
2324
2325 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002326 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002327 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002328 * @param array An array of fields to reset
2329 * @return void
2330 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002331 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002332 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002333 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002334 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002335 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002336 {
2337 $this->$item = $default_value;
2338 }
2339 }
2340 }
2341
2342 // --------------------------------------------------------------------
2343
2344 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002345 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002346 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002347 * @return void
2348 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002349 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002350 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002351 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002352 'qb_select' => array(),
2353 'qb_from' => array(),
2354 'qb_join' => array(),
2355 'qb_where' => array(),
2356 'qb_like' => array(),
2357 'qb_groupby' => array(),
2358 'qb_having' => array(),
2359 'qb_orderby' => array(),
2360 'qb_wherein' => array(),
2361 'qb_aliased_tables' => array(),
2362 'qb_no_escape' => array(),
2363 'qb_distinct' => FALSE,
2364 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002365 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002366 )
2367 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002368 }
Barry Mienydd671972010-10-04 16:33:58 +02002369
Derek Allard2067d1a2008-11-13 22:59:24 +00002370 // --------------------------------------------------------------------
2371
2372 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002373 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002374 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002375 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002376 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002377 * @return void
2378 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002379 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002380 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002381 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002382 'qb_set' => array(),
2383 'qb_from' => array(),
2384 'qb_where' => array(),
2385 'qb_like' => array(),
2386 'qb_orderby' => array(),
2387 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002388 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002389 )
2390 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002391 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002392
Derek Allard2067d1a2008-11-13 22:59:24 +00002393}
2394
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002395/* End of file DB_query_builder.php */
Andrey Andreeve4c30192012-06-04 15:08:24 +03002396/* Location: ./system/database/DB_query_builder.php */