blob: 7b0565df987affa04ed73fe00503e734eddddf3c [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();
Jamie Rumbelow7efad202012-02-19 12:37:00 +000050 protected $qb_groupby = array();
51 protected $qb_having = array();
52 protected $qb_keys = array();
53 protected $qb_limit = FALSE;
54 protected $qb_offset = FALSE;
Jamie Rumbelow7efad202012-02-19 12:37:00 +000055 protected $qb_orderby = array();
56 protected $qb_set = array();
Jamie Rumbelow7efad202012-02-19 12:37:00 +000057 protected $qb_aliased_tables = array();
58 protected $qb_store_array = array();
59 protected $qb_where_group_started = FALSE;
60 protected $qb_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +020061
Jamie Rumbelow7efad202012-02-19 12:37:00 +000062 // Query Builder Caching variables
63 protected $qb_caching = FALSE;
64 protected $qb_cache_exists = array();
65 protected $qb_cache_select = array();
66 protected $qb_cache_from = array();
67 protected $qb_cache_join = array();
68 protected $qb_cache_where = array();
69 protected $qb_cache_like = array();
70 protected $qb_cache_groupby = array();
71 protected $qb_cache_having = array();
72 protected $qb_cache_orderby = array();
73 protected $qb_cache_set = array();
WanWizard7219c072011-12-28 14:09:05 +010074
Jamie Rumbelow7efad202012-02-19 12:37:00 +000075 protected $qb_no_escape = array();
76 protected $qb_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000077
78 /**
79 * Select
80 *
81 * Generates the SELECT portion of the query
82 *
Derek Allard2067d1a2008-11-13 22:59:24 +000083 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +030084 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +000085 * @return object
86 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060087 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000088 {
Derek Allard2067d1a2008-11-13 22:59:24 +000089 if (is_string($select))
90 {
91 $select = explode(',', $select);
92 }
93
Andrey Andreev42870232012-06-12 01:30:20 +030094 // If the escape value was not set will will base it on the global setting
95 is_bool($escape) OR $escape = $this->_protect_identifiers;
96
Derek Allard2067d1a2008-11-13 22:59:24 +000097 foreach ($select as $val)
98 {
99 $val = trim($val);
100
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100101 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000103 $this->qb_select[] = $val;
104 $this->qb_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000105
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000106 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000108 $this->qb_cache_select[] = $val;
109 $this->qb_cache_exists[] = 'select';
110 $this->qb_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
112 }
113 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 return $this;
116 }
117
118 // --------------------------------------------------------------------
119
120 /**
121 * Select Max
122 *
123 * Generates a SELECT MAX(field) portion of a query
124 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 * @param string the field
126 * @param string an alias
127 * @return object
128 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600129 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
131 return $this->_max_min_avg_sum($select, $alias, 'MAX');
132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 // --------------------------------------------------------------------
135
136 /**
137 * Select Min
138 *
139 * Generates a SELECT MIN(field) portion of a query
140 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 * @param string the field
142 * @param string an alias
143 * @return object
144 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600145 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
147 return $this->_max_min_avg_sum($select, $alias, 'MIN');
148 }
149
150 // --------------------------------------------------------------------
151
152 /**
153 * Select Average
154 *
155 * Generates a SELECT AVG(field) portion of a query
156 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 * @param string the field
158 * @param string an alias
159 * @return object
160 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600161 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
163 return $this->_max_min_avg_sum($select, $alias, 'AVG');
164 }
165
166 // --------------------------------------------------------------------
167
168 /**
169 * Select Sum
170 *
171 * Generates a SELECT SUM(field) portion of a query
172 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 * @param string the field
174 * @param string an alias
175 * @return object
176 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600177 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
179 return $this->_max_min_avg_sum($select, $alias, 'SUM');
180 }
181
182 // --------------------------------------------------------------------
183
184 /**
185 * Processing Function for the four functions above:
186 *
187 * select_max()
188 * select_min()
189 * select_avg()
Andrey Andreev24276a32012-01-08 02:44:38 +0200190 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200191 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 * @param string the field
193 * @param string an alias
194 * @return object
195 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600196 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100198 if ( ! is_string($select) OR $select === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
200 $this->display_error('db_invalid_query');
201 }
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
206 {
207 show_error('Invalid function type: '.$type);
208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100210 if ($alias === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
212 $alias = $this->_create_alias_from_table(trim($select));
213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300215 $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->escape_identifiers(trim($alias));
216
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000217 $this->qb_select[] = $sql;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100218 $this->qb_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200219
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000220 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000222 $this->qb_cache_select[] = $sql;
223 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 return $this;
227 }
228
229 // --------------------------------------------------------------------
230
231 /**
232 * Determines the alias name based on the table
233 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 * @param string
235 * @return string
236 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600237 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
239 if (strpos($item, '.') !== FALSE)
240 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200241 $item = explode('.', $item);
242 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 }
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 return $item;
246 }
247
248 // --------------------------------------------------------------------
249
250 /**
251 * DISTINCT
252 *
253 * Sets a flag which tells the query string compiler to add DISTINCT
254 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 * @param bool
256 * @return object
257 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600258 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300260 $this->qb_distinct = is_bool($val) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 return $this;
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 // --------------------------------------------------------------------
265
266 /**
267 * From
268 *
269 * Generates the FROM portion of the query
270 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 * @param mixed can be a string or array
272 * @return object
273 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600274 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300276 foreach ((array) $from as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
278 if (strpos($val, ',') !== FALSE)
279 {
280 foreach (explode(',', $val) as $v)
281 {
282 $v = trim($v);
283 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200284
George Petsagourakis193d4482012-04-28 11:16:18 +0300285 $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000286
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000287 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000289 $this->qb_cache_from[] = $v;
290 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200291 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
294 else
295 {
296 $val = trim($val);
297
Andrey Andreev24276a32012-01-08 02:44:38 +0200298 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000299 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200301
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000302 $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000303
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000304 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000306 $this->qb_cache_from[] = $val;
307 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
309 }
310 }
311
312 return $this;
313 }
314
315 // --------------------------------------------------------------------
316
317 /**
318 * Join
319 *
320 * Generates the JOIN portion of the query
321 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 * @param string
323 * @param string the join condition
324 * @param string the type of join
Alex Bilbief512b732012-06-16 11:15:19 +0100325 * @param string whether not to try to escape identifiers
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 * @return object
327 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300328 public function join($table, $cond, $type = '', $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200329 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100330 if ($type !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
332 $type = strtoupper(trim($type));
333
Andrey Andreev42870232012-06-12 01:30:20 +0300334 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
336 $type = '';
337 }
338 else
339 {
340 $type .= ' ';
341 }
342 }
343
Andrey Andreev24276a32012-01-08 02:44:38 +0200344 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000345 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 $this->_track_aliases($table);
347
Andrey Andreevfe642da2012-06-16 03:47:33 +0300348 is_bool($escape) OR $escape = $this->_protect_identifiers;
349
Andrey Andreev42870232012-06-12 01:30:20 +0300350 // Split multiple conditions
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300351 if ($escape === TRUE && preg_match_all('/\sAND\s|\sOR\s/i', $cond, $m, PREG_OFFSET_CAPTURE))
Andrey Andreev42870232012-06-12 01:30:20 +0300352 {
353 $newcond = '';
354 $m[0][] = array('', strlen($cond));
355
356 for ($i = 0, $c = count($m[0]), $s = 0;
357 $i < $c;
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300358 $s = $m[0][$i][1] + strlen($m[0][$i][0]), $i++)
Andrey Andreev42870232012-06-12 01:30:20 +0300359 {
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300360 $temp = substr($cond, $s, ($m[0][$i][1] - $s));
Andrey Andreev42870232012-06-12 01:30:20 +0300361
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300362 $newcond .= preg_match("/([\[\]\w\.'-]+)(\s*[^\"\[`'\w]+\s*)(.+)/i", $temp, $match)
Andrey Andreev42870232012-06-12 01:30:20 +0300363 ? $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3])
364 : $temp;
365
366 $newcond .= $m[0][$i][0];
367 }
368
Andrey Andreev3751f932012-06-17 18:07:48 +0300369 $cond = ' ON '.$newcond;
Andrey Andreev42870232012-06-12 01:30:20 +0300370 }
371 // Split apart the condition and protect the identifiers
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300372 elseif ($escape === TRUE && preg_match("/([\[\]\w\.'-]+)(\s*[^\"\[`'\w]+\s*)(.+)/i", $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
Andrey Andreev3751f932012-06-17 18:07:48 +0300374 $cond = ' ON '.$this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
375 }
376 elseif ( ! $this->_has_operator($cond))
377 {
378 $cond = ' USING ('.($escape ? $this->escape_identifiers($cond) : $cond).')';
379 }
380 else
381 {
382 $cond = ' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Andrey Andreev42870232012-06-12 01:30:20 +0300385 // Do we want to escape the table name?
386 if ($escape === TRUE)
387 {
388 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
389 }
390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 // Assemble the JOIN statement
Andrey Andreev3751f932012-06-17 18:07:48 +0300392 $this->qb_join[] = $join = $type.'JOIN '.$table.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000393
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000394 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000396 $this->qb_cache_join[] = $join;
397 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
399
400 return $this;
401 }
402
403 // --------------------------------------------------------------------
404
405 /**
406 * Where
407 *
408 * Generates the WHERE portion of the query. Separates
409 * multiple calls with AND
410 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 * @param mixed
412 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300413 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 * @return object
415 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300416 public function where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300418 return $this->_wh('qb_where', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 // --------------------------------------------------------------------
422
423 /**
424 * OR Where
425 *
426 * Generates the WHERE portion of the query. Separates
427 * multiple calls with OR
428 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 * @param mixed
430 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300431 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 * @return object
433 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300434 public function or_where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300436 return $this->_wh('qb_where', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
438
439 // --------------------------------------------------------------------
440
441 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +0300442 * WHERE, HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300444 * Called by where(), or_where(), having(), or_having()
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300446 * @param string 'qb_where' or 'qb_having'
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 * @param mixed
448 * @param mixed
449 * @param string
Andrey Andreevb0478652012-07-18 15:34:46 +0300450 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 * @return object
452 */
Andrey Andreevd40459d2012-07-18 16:46:39 +0300453 protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300455 $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where';
456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 if ( ! is_array($key))
458 {
459 $key = array($key => $value);
460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 // If the escape value was not set will will base it on the global setting
Andrey Andreeve10fb792012-06-15 12:07:04 +0300463 is_bool($escape) OR $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000464
465 foreach ($key as $k => $v)
466 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300467 $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0)
Andrey Andreev58803fb2012-06-24 00:45:37 +0300468 ? $this->_group_get_type('')
469 : $this->_group_get_type($type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000470
471 if (is_null($v) && ! $this->_has_operator($k))
472 {
473 // value appears not to have been set, assign the test to IS NULL
474 $k .= ' IS NULL';
475 }
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 if ( ! is_null($v))
478 {
479 if ($escape === TRUE)
480 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300481 $v = ' '.(is_int($v) ? $v : $this->escape($v));
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
WanWizard7219c072011-12-28 14:09:05 +0100483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 if ( ! $this->_has_operator($k))
485 {
Greg Akere156c6e2011-04-20 16:03:04 -0500486 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
488 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000489
Andrey Andreevd40459d2012-07-18 16:46:39 +0300490 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000491 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300493 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
494 $this->qb_cache_exists[] = substr($qb_key, 3);
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 return $this;
500 }
501
502 // --------------------------------------------------------------------
503
504 /**
505 * Where_in
506 *
507 * Generates a WHERE field IN ('item', 'item') SQL query joined with
508 * AND if appropriate
509 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 * @param string The field to search
511 * @param array The values searched on
512 * @return object
513 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300514 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300516 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 }
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 // --------------------------------------------------------------------
520
521 /**
522 * Where_in_or
523 *
524 * Generates a WHERE field IN ('item', 'item') SQL query joined with
525 * OR if appropriate
526 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 * @param string The field to search
528 * @param array The values searched on
529 * @return object
530 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300531 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300533 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 }
535
536 // --------------------------------------------------------------------
537
538 /**
539 * Where_not_in
540 *
541 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
542 * with AND if appropriate
543 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 * @param string The field to search
545 * @param array The values searched on
546 * @return object
547 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300548 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300550 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 // --------------------------------------------------------------------
554
555 /**
556 * Where_not_in_or
557 *
558 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
559 * with OR if appropriate
560 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 * @param string The field to search
562 * @param array The values searched on
563 * @return object
564 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300565 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300567 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 }
569
570 // --------------------------------------------------------------------
571
572 /**
573 * Where_in
574 *
575 * Called by where_in, where_in_or, where_not_in, where_not_in_or
576 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 * @param string The field to search
578 * @param array The values searched on
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300579 * @param bool If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200580 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 * @return object
582 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300583 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 {
585 if ($key === NULL OR $values === NULL)
586 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300587 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 }
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 if ( ! is_array($values))
591 {
592 $values = array($values);
593 }
Barry Mienydd671972010-10-04 16:33:58 +0200594
Andrey Andreev498c1e02012-06-16 03:34:10 +0300595 is_bool($escape) OR $escape = $this->_protect_identifiers;
596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 $not = ($not) ? ' NOT' : '';
598
Andrey Andreev94611df2012-07-19 12:29:54 +0300599 $where_in = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 foreach ($values as $value)
601 {
Andrey Andreev94611df2012-07-19 12:29:54 +0300602 $wherein[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 }
604
WanWizardbc69f362012-06-22 00:10:11 +0200605 $prefix = (count($this->qb_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Andrey Andreev6e704752012-07-18 00:46:33 +0300606 $where_in = array(
Andrey Andreev94611df2012-07-19 12:29:54 +0300607 'condition' => $prefix.$key.$not.' IN('.implode(', ', $where_in).')',
Andrey Andreev6e704752012-07-18 00:46:33 +0300608 'escape' => $escape
609 );
Barry Mienydd671972010-10-04 16:33:58 +0200610
Andrey Andreev6e704752012-07-18 00:46:33 +0300611 $this->qb_where[] = $where_in;
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 return $this;
619 }
Barry Mienydd671972010-10-04 16:33:58 +0200620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 // --------------------------------------------------------------------
622
623 /**
624 * Like
625 *
626 * Generates a %LIKE% portion of the query. Separates
627 * multiple calls with AND
628 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300630 * @param string
631 * @param string
632 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 * @return object
634 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300635 public function like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300637 return $this->_like($field, $match, 'AND ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 }
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
Andrey Andreevb0478652012-07-18 15:34:46 +0300649 * @param string
650 * @param string
651 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 * @return object
653 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300654 public function not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300656 return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 }
Barry Mienydd671972010-10-04 16:33:58 +0200658
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 // --------------------------------------------------------------------
660
661 /**
662 * OR Like
663 *
664 * Generates a %LIKE% portion of the query. Separates
665 * multiple calls with OR
666 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300668 * @param string
669 * @param string
670 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 * @return object
672 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300673 public function or_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300675 return $this->_like($field, $match, 'OR ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 }
677
678 // --------------------------------------------------------------------
679
680 /**
681 * OR Not Like
682 *
683 * Generates a NOT LIKE portion of the query. Separates
684 * multiple calls with OR
685 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300687 * @param string
688 * @param string
689 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 * @return object
691 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300692 public function or_not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300694 return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 }
Barry Mienydd671972010-10-04 16:33:58 +0200696
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 // --------------------------------------------------------------------
698
699 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 * Like
701 *
Andrey Andreevb0478652012-07-18 15:34:46 +0300702 * Called by like(), or_like(), not_like, or_not_like()
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 * @param string
Andrey Andreevb0478652012-07-18 15:34:46 +0300706 * @param string
707 * @param string
708 * @param string
709 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 * @return object
711 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300712 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 {
714 if ( ! is_array($field))
715 {
716 $field = array($field => $match);
717 }
Barry Mienydd671972010-10-04 16:33:58 +0200718
Andrey Andreevb0478652012-07-18 15:34:46 +0300719 is_bool($escape) OR $escape = $this->_protect_identifiers;
720 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
721 ? $this->_group_get_type('') : $this->_group_get_type($type);
722
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 foreach ($field as $k => $v)
724 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000725 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300726
Andrey Andreev24276a32012-01-08 02:44:38 +0200727 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400728 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300729 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400730 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200731 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300733 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200735 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300737 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 }
739 else
740 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300741 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600743
Derek Jonese4ed5832009-02-20 21:44:59 +0000744 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100745 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000746 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300747 $like_statement .= sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000748 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600749
Andrey Andreevb0478652012-07-18 15:34:46 +0300750 $this->qb_where[] = array('condition' => $like_statement, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000751 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 {
Andrey Andreevededc4a2012-07-18 01:16:15 +0300753 $this->qb_cache_where[] = $like_statement;
754 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200757
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 return $this;
759 }
Barry Mienydd671972010-10-04 16:33:58 +0200760
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 // --------------------------------------------------------------------
762
763 /**
WanWizard7219c072011-12-28 14:09:05 +0100764 * Starts a query group.
765 *
766 * @param string (Internal use only)
767 * @param string (Internal use only)
768 * @return object
769 */
770 public function group_start($not = '', $type = 'AND ')
771 {
772 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100773
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000774 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100775 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Andrey Andreev6e704752012-07-18 00:46:33 +0300776 $where = array(
777 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (',
778 'escape' => FALSE
779 );
WanWizard7219c072011-12-28 14:09:05 +0100780
Andrey Andreev6e704752012-07-18 00:46:33 +0300781 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000782 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100783 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300784 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +0100785 }
786
787 return $this;
788 }
789
790 // --------------------------------------------------------------------
791
792 /**
793 * Starts a query group, but ORs the group
794 *
795 * @return object
796 */
797 public function or_group_start()
798 {
799 return $this->group_start('', 'OR ');
800 }
801
802 // --------------------------------------------------------------------
803
804 /**
805 * Starts a query group, but NOTs the group
806 *
807 * @return object
808 */
809 public function not_group_start()
810 {
811 return $this->group_start('NOT ', 'AND ');
812 }
813
814 // --------------------------------------------------------------------
815
816 /**
817 * Starts a query group, but OR NOTs the group
818 *
819 * @return object
820 */
821 public function or_not_group_start()
822 {
823 return $this->group_start('NOT ', 'OR ');
824 }
825
826 // --------------------------------------------------------------------
827
828 /**
829 * Ends a query group
830 *
831 * @return object
832 */
833 public function group_end()
834 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000835 $this->qb_where_group_started = FALSE;
Andrey Andreev6e704752012-07-18 00:46:33 +0300836 $where = array(
837 'condition' => str_repeat(' ', $this->qb_where_group_count--).')',
838 'escape' => FALSE
839 );
WanWizard7219c072011-12-28 14:09:05 +0100840
Andrey Andreev6e704752012-07-18 00:46:33 +0300841 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000842 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100843 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300844 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +0100845 }
846
WanWizard7219c072011-12-28 14:09:05 +0100847 return $this;
848 }
849
850 // --------------------------------------------------------------------
851
852 /**
853 * Group_get_type
854 *
855 * Called by group_start(), _like(), _where() and _where_in()
856 *
857 * @param string
858 * @return string
859 */
860 protected function _group_get_type($type)
861 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000862 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100863 {
864 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000865 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100866 }
867
868 return $type;
869 }
870
871 // --------------------------------------------------------------------
872
873 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 * GROUP BY
875 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 * @param string
877 * @return object
878 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600879 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 {
881 if (is_string($by))
882 {
883 $by = explode(',', $by);
884 }
Barry Mienydd671972010-10-04 16:33:58 +0200885
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 foreach ($by as $val)
887 {
888 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200889
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100890 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000892 $this->qb_groupby[] = $val = $this->protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200893
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000894 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000896 $this->qb_cache_groupby[] = $val;
897 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 }
899 }
900 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200901
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 return $this;
903 }
904
905 // --------------------------------------------------------------------
906
907 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 * Sets the HAVING value
909 *
910 * Separates multiple calls with AND
911 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 * @param string
913 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300914 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 * @return object
916 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300917 public function having($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300919 return $this->_wh('qb_having', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 }
Barry Mienydd671972010-10-04 16:33:58 +0200921
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 // --------------------------------------------------------------------
923
924 /**
925 * Sets the OR HAVING value
926 *
927 * Separates multiple calls with OR
928 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 * @param string
930 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300931 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 * @return object
933 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300934 public function or_having($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300936 return $this->_wh('qb_having', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 }
Barry Mienydd671972010-10-04 16:33:58 +0200938
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 // --------------------------------------------------------------------
940
941 /**
942 * Sets the ORDER BY value
943 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 * @param string
945 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100946 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 * @return object
948 */
Andrey Andreevd24160c2012-06-16 03:21:20 +0300949 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200951 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 {
953 $orderby = ''; // Random results want or don't need a field name
954 $direction = $this->_random_keyword;
955 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100956 elseif (trim($direction) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 {
Andrey Andreev650b4c02012-06-11 12:07:15 +0300958 $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : ' ASC';
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 }
Barry Mienydd671972010-10-04 16:33:58 +0200960
Andrey Andreevd24160c2012-06-16 03:21:20 +0300961 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +0200962
Andrey Andreevd24160c2012-06-16 03:21:20 +0300963 if ($escape === TRUE && strpos($orderby, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 {
965 $temp = array();
966 foreach (explode(',', $orderby) as $part)
967 {
968 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000969 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 {
Andrey Andreev88cb2782012-06-11 20:40:50 +0300971 $part = preg_match('/^(.+)\s+(ASC|DESC)$/i', $part, $matches)
972 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
973 : $this->protect_identifiers($part);
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 }
Barry Mienydd671972010-10-04 16:33:58 +0200975
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 $temp[] = $part;
977 }
Barry Mienydd671972010-10-04 16:33:58 +0200978
979 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 }
Andrey Andreev650b4c02012-06-11 12:07:15 +0300981 elseif ($direction !== $this->_random_keyword && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 {
Andrey Andreev1d1d7ff2012-06-11 22:35:35 +0300983 $orderby = preg_match('/^(.+)\s+(ASC|DESC)$/i', $orderby, $matches)
Andrey Andreev88cb2782012-06-11 20:40:50 +0300984 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
985 : $this->protect_identifiers($orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 }
Barry Mienydd671972010-10-04 16:33:58 +0200987
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000988 $this->qb_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200989
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000990 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000992 $this->qb_cache_orderby[] = $orderby_statement;
993 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 }
995
996 return $this;
997 }
Barry Mienydd671972010-10-04 16:33:58 +0200998
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 // --------------------------------------------------------------------
1000
1001 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 * Sets the LIMIT value
1003 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001004 * @param int the limit value
1005 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 * @return object
1007 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +02001008 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001010 is_null($value) OR $this->qb_limit = (int) $value;
1011 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001012
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 return $this;
1014 }
Barry Mienydd671972010-10-04 16:33:58 +02001015
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 // --------------------------------------------------------------------
1017
1018 /**
1019 * Sets the OFFSET value
1020 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001021 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 * @return object
1023 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001024 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001026 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 return $this;
1028 }
Barry Mienydd671972010-10-04 16:33:58 +02001029
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 // --------------------------------------------------------------------
1031
1032 /**
Andrey Andreev2c35b642012-06-24 03:05:26 +03001033 * Limit string
1034 *
1035 * Generates a platform-specific LIMIT clause
1036 *
1037 * @param string the sql query string
1038 * @param int the number of rows to limit the query to
1039 * @param int the offset value
1040 * @return string
1041 */
1042 protected function _limit($sql, $limit, $offset)
1043 {
1044 return $sql.' LIMIT '.($offset ? $offset.', ' : '').$limit;
1045 }
1046
1047 // --------------------------------------------------------------------
1048
1049 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001050 * The "set" function.
1051 *
1052 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 * @param mixed
1055 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001056 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 * @return object
1058 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001059 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 {
1061 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001062
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 if ( ! is_array($key))
1064 {
1065 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001066 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001067
Andrey Andreevfe642da2012-06-16 03:47:33 +03001068 is_bool($escape) OR $escape = $this->_protect_identifiers;
1069
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 foreach ($key as $k => $v)
1071 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001072 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1073 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 }
Barry Mienydd671972010-10-04 16:33:58 +02001075
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 return $this;
1077 }
WanWizard7219c072011-12-28 14:09:05 +01001078
Kyle Farris0c147b32011-08-26 02:29:31 -04001079 // --------------------------------------------------------------------
1080
1081 /**
1082 * Get SELECT query string
1083 *
1084 * Compiles a SELECT query string and returns the sql.
1085 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001086 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001087 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001088 * @return string
1089 */
WanWizard7219c072011-12-28 14:09:05 +01001090 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001091 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001092 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001093 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001094 $this->_track_aliases($table);
1095 $this->from($table);
1096 }
WanWizard7219c072011-12-28 14:09:05 +01001097
Andrey Andreev650b4c02012-06-11 12:07:15 +03001098 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001099
Kyle Farris0c147b32011-08-26 02:29:31 -04001100 if ($reset === TRUE)
1101 {
1102 $this->_reset_select();
1103 }
WanWizard7219c072011-12-28 14:09:05 +01001104
Kyle Farris0c147b32011-08-26 02:29:31 -04001105 return $select;
1106 }
WanWizard7219c072011-12-28 14:09:05 +01001107
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 // --------------------------------------------------------------------
1109
1110 /**
1111 * Get
1112 *
1113 * Compiles the select statement based on the other functions called
1114 * and runs the query
1115 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 * @param string the table
1117 * @param string the limit clause
1118 * @param string the offset clause
1119 * @return object
1120 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001121 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001123 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 {
1125 $this->_track_aliases($table);
1126 $this->from($table);
1127 }
Barry Mienydd671972010-10-04 16:33:58 +02001128
Andrey Andreev650b4c02012-06-11 12:07:15 +03001129 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 {
1131 $this->limit($limit, $offset);
1132 }
Barry Mienydd671972010-10-04 16:33:58 +02001133
Andrey Andreev24276a32012-01-08 02:44:38 +02001134 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 $this->_reset_select();
1136 return $result;
1137 }
1138
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001139 // --------------------------------------------------------------------
1140
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 /**
1142 * "Count All Results" query
1143 *
Barry Mienydd671972010-10-04 16:33:58 +02001144 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001145 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 * @param string
1148 * @return string
1149 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001150 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001152 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 {
1154 $this->_track_aliases($table);
1155 $this->from($table);
1156 }
Barry Mienydd671972010-10-04 16:33:58 +02001157
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001158 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001160
Purwandi1d160e72012-01-09 16:33:28 +07001161 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001163 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 }
1165
Purwandi1d160e72012-01-09 16:33:28 +07001166 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001167 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001169
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 // --------------------------------------------------------------------
1171
1172 /**
1173 * Get_Where
1174 *
1175 * Allows the where clause, limit and offset to be added directly
1176 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 * @param string the where clause
1178 * @param string the limit clause
1179 * @param string the offset clause
1180 * @return object
1181 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001182 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001184 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 {
1186 $this->from($table);
1187 }
1188
1189 if ( ! is_null($where))
1190 {
1191 $this->where($where);
1192 }
Barry Mienydd671972010-10-04 16:33:58 +02001193
Andrey Andreev650b4c02012-06-11 12:07:15 +03001194 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 {
1196 $this->limit($limit, $offset);
1197 }
Barry Mienydd671972010-10-04 16:33:58 +02001198
Andrey Andreev24276a32012-01-08 02:44:38 +02001199 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 $this->_reset_select();
1201 return $result;
1202 }
1203
1204 // --------------------------------------------------------------------
1205
1206 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001207 * Insert_Batch
1208 *
1209 * Compiles batch insert strings and runs the queries
1210 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001211 * @param string the table to retrieve the results from
1212 * @param array an associative array of insert values
1213 * @return object
1214 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001215 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001216 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001217 if ( ! is_null($set))
1218 {
1219 $this->set_insert_batch($set);
1220 }
Barry Mienydd671972010-10-04 16:33:58 +02001221
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001222 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001223 {
1224 if ($this->db_debug)
1225 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001226 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001227 return $this->display_error('db_must_use_set');
1228 }
1229 return FALSE;
1230 }
1231
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001232 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001233 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001234 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001235 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001236 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001237 }
Barry Mienydd671972010-10-04 16:33:58 +02001238
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001239 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001240 }
1241
1242 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001243 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001244 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001245 $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 -06001246 }
Barry Mienydd671972010-10-04 16:33:58 +02001247
Derek Jonesd10e8962010-03-02 17:10:36 -06001248 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001249 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001250 }
1251
1252 // --------------------------------------------------------------------
1253
1254 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001255 * Insert_batch statement
1256 *
1257 * Generates a platform-specific insert string from the supplied data.
1258 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001259 * @param string the table name
1260 * @param array the insert keys
1261 * @param array the insert values
1262 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001263 */
1264 protected function _insert_batch($table, $keys, $values)
1265 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001266 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001267 }
1268
1269 // --------------------------------------------------------------------
1270
1271 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001272 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001273 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001274 * @param mixed
1275 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001276 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001277 * @return object
1278 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001279 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001280 {
1281 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001282
Derek Jonesd10e8962010-03-02 17:10:36 -06001283 if ( ! is_array($key))
1284 {
1285 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001286 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001287
Andrey Andreevfe642da2012-06-16 03:47:33 +03001288 is_bool($escape) OR $escape = $this->_protect_identifiers;
1289
Iban Eguia3c0a4522012-04-15 13:30:44 +02001290 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001291 sort($keys);
1292
1293 foreach ($key as $row)
1294 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001295 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001296 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1297 {
1298 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001299 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001300 return;
1301 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001302
Barry Mienydd671972010-10-04 16:33:58 +02001303 ksort($row); // puts $row in the same order as our keys
1304
Andrey Andreev650b4c02012-06-11 12:07:15 +03001305 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001306 {
1307 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001308 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001309 {
Barry Mienydd671972010-10-04 16:33:58 +02001310 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001311 }
1312
Andrey Andreev650b4c02012-06-11 12:07:15 +03001313 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001314 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001315
1316 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001317 }
1318
1319 foreach ($keys as $k)
1320 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001321 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001322 }
Barry Mienydd671972010-10-04 16:33:58 +02001323
Derek Jonesd10e8962010-03-02 17:10:36 -06001324 return $this;
1325 }
WanWizard7219c072011-12-28 14:09:05 +01001326
Kyle Farris0c147b32011-08-26 02:29:31 -04001327 // --------------------------------------------------------------------
1328
1329 /**
1330 * Get INSERT query string
1331 *
1332 * Compiles an insert query and returns the sql
1333 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001334 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001335 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001336 * @return string
1337 */
1338 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001339 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001340 if ($this->_validate_insert($table) === FALSE)
1341 {
1342 return FALSE;
1343 }
WanWizard7219c072011-12-28 14:09:05 +01001344
Kyle Farris76116012011-08-31 11:17:48 -04001345 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001346 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001347 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001348 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001349 array_keys($this->qb_set),
1350 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001351 );
WanWizard7219c072011-12-28 14:09:05 +01001352
Kyle Farris0c147b32011-08-26 02:29:31 -04001353 if ($reset === TRUE)
1354 {
1355 $this->_reset_write();
1356 }
WanWizard7219c072011-12-28 14:09:05 +01001357
Kyle Farris0c147b32011-08-26 02:29:31 -04001358 return $sql;
1359 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001360
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 // --------------------------------------------------------------------
1362
1363 /**
1364 * Insert
1365 *
1366 * Compiles an insert string and runs the query
1367 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001368 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001369 * @param array an associative array of insert values
1370 * @return object
1371 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001372 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001373 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001374 if ( ! is_null($set))
1375 {
1376 $this->set($set);
1377 }
WanWizard7219c072011-12-28 14:09:05 +01001378
Kyle Farris0c147b32011-08-26 02:29:31 -04001379 if ($this->_validate_insert($table) === FALSE)
1380 {
1381 return FALSE;
1382 }
WanWizard7219c072011-12-28 14:09:05 +01001383
Kyle Farris76116012011-08-31 11:17:48 -04001384 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001385 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001386 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001387 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001388 array_keys($this->qb_set),
1389 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001390 );
Barry Mienydd671972010-10-04 16:33:58 +02001391
Kyle Farris0c147b32011-08-26 02:29:31 -04001392 $this->_reset_write();
1393 return $this->query($sql);
1394 }
WanWizard7219c072011-12-28 14:09:05 +01001395
Kyle Farris0c147b32011-08-26 02:29:31 -04001396 // --------------------------------------------------------------------
1397
1398 /**
1399 * Validate Insert
1400 *
1401 * This method is used by both insert() and get_compiled_insert() to
1402 * validate that the there data is actually being set and that table
1403 * has been chosen to be inserted into.
1404 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001405 * @param string the table to insert data into
1406 * @return string
1407 */
WanWizard7219c072011-12-28 14:09:05 +01001408 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001409 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001410 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001411 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001412 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 }
1414
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001415 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001416 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001417 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001418 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001419 elseif ( ! isset($this->qb_from[0]))
1420 {
1421 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1422 }
WanWizard7219c072011-12-28 14:09:05 +01001423
Kyle Farris0c147b32011-08-26 02:29:31 -04001424 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001425 }
Barry Mienydd671972010-10-04 16:33:58 +02001426
Phil Sturgeon9789f322011-07-15 15:14:05 -06001427 // --------------------------------------------------------------------
1428
1429 /**
1430 * Replace
1431 *
1432 * Compiles an replace into string and runs the query
1433 *
1434 * @param string the table to replace data into
1435 * @param array an associative array of insert values
1436 * @return object
1437 */
1438 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001439 {
1440 if ( ! is_null($set))
1441 {
1442 $this->set($set);
1443 }
Barry Mienydd671972010-10-04 16:33:58 +02001444
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001445 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001446 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001447 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001448 }
1449
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001450 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001451 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001452 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001453 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001454 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001455 }
Barry Mienydd671972010-10-04 16:33:58 +02001456
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001457 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001458 }
1459
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001460 $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 +00001461
Derek Jonesd10e8962010-03-02 17:10:36 -06001462 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001463 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001464 }
WanWizard7219c072011-12-28 14:09:05 +01001465
Kyle Farris0c147b32011-08-26 02:29:31 -04001466 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001467
Kyle Farris0c147b32011-08-26 02:29:31 -04001468 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001469 * Replace statement
1470 *
1471 * Generates a platform-specific replace string from the supplied data
1472 *
1473 * @param string the table name
1474 * @param array the insert keys
1475 * @param array the insert values
1476 * @return string
1477 */
1478 protected function _replace($table, $keys, $values)
1479 {
1480 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1481 }
1482
1483 // --------------------------------------------------------------------
1484
1485 /**
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001486 * From Tables
1487 *
1488 * This public function implicitly groups FROM tables so there is no confusion
1489 * about operator precedence in harmony with SQL standards
1490 *
1491 * @param array
1492 * @return string
1493 */
1494 protected function _from_tables($tables)
1495 {
1496 is_array($tables) OR $tables = array($tables);
1497
1498 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
1499 }
1500
1501 // --------------------------------------------------------------------
1502
1503 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001504 * Get UPDATE query string
1505 *
1506 * Compiles an update query and returns the sql
1507 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001508 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001509 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001510 * @return string
1511 */
1512 public function get_compiled_update($table = '', $reset = TRUE)
1513 {
1514 // Combine any cached components with the current statements
1515 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001516
Kyle Farris0c147b32011-08-26 02:29:31 -04001517 if ($this->_validate_update($table) === FALSE)
1518 {
1519 return FALSE;
1520 }
WanWizard7219c072011-12-28 14:09:05 +01001521
Andrey Andreevb0478652012-07-18 15:34:46 +03001522 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001523
Kyle Farris0c147b32011-08-26 02:29:31 -04001524 if ($reset === TRUE)
1525 {
1526 $this->_reset_write();
1527 }
WanWizard7219c072011-12-28 14:09:05 +01001528
Kyle Farris0c147b32011-08-26 02:29:31 -04001529 return $sql;
1530 }
WanWizard7219c072011-12-28 14:09:05 +01001531
Derek Allard2067d1a2008-11-13 22:59:24 +00001532 // --------------------------------------------------------------------
1533
1534 /**
1535 * Update
1536 *
1537 * Compiles an update string and runs the query
1538 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001539 * @param string the table to retrieve the results from
1540 * @param array an associative array of update values
1541 * @param mixed the where clause
1542 * @return object
1543 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001544 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001545 {
1546 // Combine any cached components with the current statements
1547 $this->_merge_cache();
1548
1549 if ( ! is_null($set))
1550 {
1551 $this->set($set);
1552 }
Barry Mienydd671972010-10-04 16:33:58 +02001553
Kyle Farris0c147b32011-08-26 02:29:31 -04001554 if ($this->_validate_update($table) === FALSE)
1555 {
1556 return FALSE;
1557 }
1558
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001559 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001560 {
1561 $this->where($where);
1562 }
1563
Andrey Andreev650b4c02012-06-11 12:07:15 +03001564 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001565 {
1566 $this->limit($limit);
1567 }
1568
Andrey Andreevb0478652012-07-18 15:34:46 +03001569 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
Jamie Rumbelow3b1355c2012-03-06 21:27:46 +00001570
Kyle Farris0c147b32011-08-26 02:29:31 -04001571 $this->_reset_write();
1572 return $this->query($sql);
1573 }
WanWizard7219c072011-12-28 14:09:05 +01001574
Kyle Farris0c147b32011-08-26 02:29:31 -04001575 // --------------------------------------------------------------------
1576
1577 /**
1578 * Validate Update
1579 *
1580 * This method is used by both update() and get_compiled_update() to
1581 * validate that data is actually being set and that a table has been
1582 * chosen to be update.
1583 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001584 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001585 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001586 */
1587 protected function _validate_update($table = '')
1588 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001589 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001590 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001591 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001592 }
1593
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001594 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001595 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001596 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001597 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001598 elseif ( ! isset($this->qb_from[0]))
1599 {
1600 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1601 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001602
1603 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001604 }
WanWizard7219c072011-12-28 14:09:05 +01001605
Derek Jonesd10e8962010-03-02 17:10:36 -06001606 // --------------------------------------------------------------------
1607
1608 /**
1609 * Update_Batch
1610 *
1611 * Compiles an update string and runs the query
1612 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001613 * @param string the table to retrieve the results from
1614 * @param array an associative array of update values
1615 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001616 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001617 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001618 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001619 {
1620 // Combine any cached components with the current statements
1621 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001622
Derek Jonesd10e8962010-03-02 17:10:36 -06001623 if (is_null($index))
1624 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001625 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001626 }
1627
1628 if ( ! is_null($set))
1629 {
1630 $this->set_update_batch($set, $index);
1631 }
1632
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001633 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001634 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001635 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001636 }
1637
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001638 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001639 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001640 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001641 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001642 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001643 }
Barry Mienydd671972010-10-04 16:33:58 +02001644
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001645 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001646 }
Barry Mienydd671972010-10-04 16:33:58 +02001647
Derek Jonesd10e8962010-03-02 17:10:36 -06001648 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001649 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001650 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001651 $this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, 100), $this->protect_identifiers($index)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001652 }
Barry Mienydd671972010-10-04 16:33:58 +02001653
Derek Jonesd10e8962010-03-02 17:10:36 -06001654 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001655 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001656 }
1657
1658 // --------------------------------------------------------------------
1659
1660 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001661 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001662 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001663 * @param array
1664 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001665 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001666 * @return object
1667 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001668 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001669 {
1670 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001671
Derek Jonesd10e8962010-03-02 17:10:36 -06001672 if ( ! is_array($key))
1673 {
1674 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001675 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001676
Andrey Andreevfe642da2012-06-16 03:47:33 +03001677 is_bool($escape) OR $escape = $this->_protect_identifiers;
1678
Derek Jonesd10e8962010-03-02 17:10:36 -06001679 foreach ($key as $k => $v)
1680 {
1681 $index_set = FALSE;
1682 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001683 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001684 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001685 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001686 {
1687 $index_set = TRUE;
1688 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001689
Andrey Andreevfe642da2012-06-16 03:47:33 +03001690 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001691 }
1692
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001693 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001694 {
1695 return $this->display_error('db_batch_missing_index');
1696 }
1697
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001698 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001699 }
Barry Mienydd671972010-10-04 16:33:58 +02001700
Derek Jonesd10e8962010-03-02 17:10:36 -06001701 return $this;
1702 }
1703
Derek Allard2067d1a2008-11-13 22:59:24 +00001704 // --------------------------------------------------------------------
1705
1706 /**
1707 * Empty Table
1708 *
1709 * Compiles a delete string and runs "DELETE FROM table"
1710 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001711 * @param string the table to empty
1712 * @return object
1713 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001714 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001715 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001716 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001717 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001718 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001719 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001720 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001721 }
1722
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001723 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001724 }
1725 else
1726 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001727 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001728 }
1729
1730 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001732 return $this->query($sql);
1733 }
1734
1735 // --------------------------------------------------------------------
1736
1737 /**
1738 * Truncate
1739 *
1740 * Compiles a truncate string and runs the query
1741 * If the database does not support the truncate() command
1742 * This function maps to "DELETE FROM table"
1743 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001744 * @param string the table to truncate
1745 * @return object
1746 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001747 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001748 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001749 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001750 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001751 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001752 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001753 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001754 }
1755
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001756 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001757 }
1758 else
1759 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001760 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001761 }
1762
1763 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001764 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001765 return $this->query($sql);
1766 }
WanWizard7219c072011-12-28 14:09:05 +01001767
Kyle Farris0c147b32011-08-26 02:29:31 -04001768 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001769
Kyle Farris0c147b32011-08-26 02:29:31 -04001770 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001771 * Truncate statement
1772 *
1773 * Generates a platform-specific truncate string from the supplied data
1774 *
1775 * If the database does not support the truncate() command,
1776 * then this method maps to 'DELETE FROM table'
1777 *
1778 * @param string the table name
1779 * @return string
1780 */
1781 protected function _truncate($table)
1782 {
1783 return 'TRUNCATE '.$table;
1784 }
1785
1786 // --------------------------------------------------------------------
1787
1788 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001789 * Get DELETE query string
1790 *
1791 * Compiles a delete query string and returns the sql
1792 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001793 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001794 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001795 * @return string
1796 */
1797 public function get_compiled_delete($table = '', $reset = TRUE)
1798 {
1799 $this->return_delete_sql = TRUE;
1800 $sql = $this->delete($table, '', NULL, $reset);
1801 $this->return_delete_sql = FALSE;
1802 return $sql;
1803 }
WanWizard7219c072011-12-28 14:09:05 +01001804
Derek Allard2067d1a2008-11-13 22:59:24 +00001805 // --------------------------------------------------------------------
1806
1807 /**
1808 * Delete
1809 *
1810 * Compiles a delete string and runs the query
1811 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001812 * @param mixed the table(s) to delete from. String or array
1813 * @param mixed the where clause
1814 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001815 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001816 * @return object
1817 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001818 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001819 {
1820 // Combine any cached components with the current statements
1821 $this->_merge_cache();
1822
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001823 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001824 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001825 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001826 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001827 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001828 }
1829
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001830 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001831 }
1832 elseif (is_array($table))
1833 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001834 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001835 {
1836 $this->delete($single_table, $where, $limit, FALSE);
1837 }
1838
1839 $this->_reset_write();
1840 return;
1841 }
1842 else
1843 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001844 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001845 }
1846
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001847 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001848 {
1849 $this->where($where);
1850 }
1851
Andrey Andreev650b4c02012-06-11 12:07:15 +03001852 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001853 {
1854 $this->limit($limit);
1855 }
1856
Andrey Andreev94611df2012-07-19 12:29:54 +03001857 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001858 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001859 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001860 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001861
Andrey Andreevb0478652012-07-18 15:34:46 +03001862 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001863 if ($reset_data)
1864 {
1865 $this->_reset_write();
1866 }
WanWizard7219c072011-12-28 14:09:05 +01001867
Andrey Andreev24276a32012-01-08 02:44:38 +02001868 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001869 }
WanWizard7219c072011-12-28 14:09:05 +01001870
Derek Allard2067d1a2008-11-13 22:59:24 +00001871 // --------------------------------------------------------------------
1872
1873 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001874 * Delete statement
1875 *
1876 * Generates a platform-specific delete string from the supplied data
1877 *
1878 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03001879 * @return string
1880 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001881 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03001882 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001883 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevb0478652012-07-18 15:34:46 +03001884 .($this->qb_limit ? ' LIMIT '.(int) $this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001885 }
1886
1887 // --------------------------------------------------------------------
1888
1889 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001890 * DB Prefix
1891 *
1892 * Prepends a database prefix if one exists in configuration
1893 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001894 * @param string the table
1895 * @return string
1896 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001897 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001898 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001899 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001900 {
1901 $this->display_error('db_table_name_required');
1902 }
1903
1904 return $this->dbprefix.$table;
1905 }
1906
1907 // --------------------------------------------------------------------
1908
1909 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001910 * Set DB Prefix
1911 *
1912 * Set's the DB Prefix to something new without needing to reconnect
1913 *
1914 * @param string the prefix
1915 * @return string
1916 */
1917 public function set_dbprefix($prefix = '')
1918 {
1919 return $this->dbprefix = $prefix;
1920 }
1921
1922 // --------------------------------------------------------------------
1923
1924 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001925 * Track Aliases
1926 *
1927 * Used to track SQL statements written with aliased tables.
1928 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001929 * @param string The table to inspect
1930 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001931 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001932 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001933 {
1934 if (is_array($table))
1935 {
1936 foreach ($table as $t)
1937 {
1938 $this->_track_aliases($t);
1939 }
1940 return;
1941 }
Barry Mienydd671972010-10-04 16:33:58 +02001942
Derek Jones37f4b9c2011-07-01 17:56:50 -05001943 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001944 // the string into discreet statements
1945 if (strpos($table, ',') !== FALSE)
1946 {
1947 return $this->_track_aliases(explode(',', $table));
1948 }
Barry Mienydd671972010-10-04 16:33:58 +02001949
Derek Allard2067d1a2008-11-13 22:59:24 +00001950 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001951 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001952 {
1953 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03001954 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001955
Derek Allard2067d1a2008-11-13 22:59:24 +00001956 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001957 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001958
Derek Allard2067d1a2008-11-13 22:59:24 +00001959 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001960 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001961 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001962 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001963 }
1964 }
1965 }
WanWizard7219c072011-12-28 14:09:05 +01001966
Derek Allard2067d1a2008-11-13 22:59:24 +00001967 // --------------------------------------------------------------------
1968
1969 /**
1970 * Compile the SELECT statement
1971 *
1972 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001973 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001974 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001975 * @return string
1976 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001977 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001978 {
1979 // Combine any cached components with the current statements
1980 $this->_merge_cache();
1981
Derek Allard2067d1a2008-11-13 22:59:24 +00001982 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001983 if ($select_override !== FALSE)
1984 {
1985 $sql = $select_override;
1986 }
1987 else
1988 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001989 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001990
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001991 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001992 {
Barry Mienydd671972010-10-04 16:33:58 +02001993 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001994 }
1995 else
Barry Mienydd671972010-10-04 16:33:58 +02001996 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001997 // Cycle through the "select" portion of the query and prep each column name.
1998 // The reason we protect identifiers here rather then in the select() function
1999 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002000 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002001 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002002 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002003 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002004 }
Barry Mienydd671972010-10-04 16:33:58 +02002005
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002006 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002007 }
2008 }
2009
Derek Allard2067d1a2008-11-13 22:59:24 +00002010 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002011 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002012 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002013 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002014 }
2015
Derek Allard2067d1a2008-11-13 22:59:24 +00002016 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002017 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002018 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002019 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002020 }
2021
Andrey Andreevd40459d2012-07-18 16:46:39 +03002022 // WHERE
2023 $sql .= $this->_compile_wh('qb_where');
Derek Allard2067d1a2008-11-13 22:59:24 +00002024
Andrey Andreevb0478652012-07-18 15:34:46 +03002025 // GROUP BY
2026 if (count($this->qb_groupby) > 0)
2027 {
2028 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
2029 }
2030
2031 // HAVING
Andrey Andreevd40459d2012-07-18 16:46:39 +03002032 $sql .= $this->_compile_wh('qb_having');
Andrey Andreevb0478652012-07-18 15:34:46 +03002033
2034 // ORDER BY
2035 if (count($this->qb_orderby) > 0)
2036 {
2037 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
2038 }
2039
Andrey Andreevd40459d2012-07-18 16:46:39 +03002040 // LIMIT
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002041 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002042 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002043 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00002044 }
2045
2046 return $sql;
2047 }
2048
2049 // --------------------------------------------------------------------
2050
2051 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002052 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002053 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002054 * Escapes identifiers in WHERE and HAVING statements at execution time.
2055 *
Andrey Andreevb0478652012-07-18 15:34:46 +03002056 * Required so that aliases are tracked properly, regardless of wether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002057 * where(), or_where(), having(), or_having are called prior to from(),
2058 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002059 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002060 * @param string 'qb_where' or 'qb_having'
2061 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002062 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002063 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002064 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002065 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002066 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002067 $sql = ($qb_key === 'qb_having') ? "\nHAVING " : "\nWHERE ";
Andrey Andreev6e704752012-07-18 00:46:33 +03002068
Andrey Andreevd40459d2012-07-18 16:46:39 +03002069 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002070 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002071 if ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002072 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002073 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002074 continue;
2075 }
2076
Andrey Andreevd40459d2012-07-18 16:46:39 +03002077 $op = preg_quote($this->_get_operator($this->{$qb_key}[$i]['condition']));
2078 if ( ! preg_match('/^(\s*(?:AND|OR)\s+)?(\(?)(.*)('.$op.')(.*(?<!\)))?(\)?)$/i', $this->{$qb_key}[$i]['condition'], $matches))
Andrey Andreev6e704752012-07-18 00:46:33 +03002079 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002080 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002081 continue;
2082 }
2083
2084 // $matches = array(
2085 // 0 => 'OR (test <= foo)', /* the whole thing */
2086 // 1 => 'OR ', /* optional */
2087 // 2 => '(', /* optional */
2088 // 3 => 'test', /* the field name */
2089 // 4 => ' <= ', /* $op */
2090 // 5 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2091 // 6 => ')' /* optional */
2092 // );
2093 empty($matches[5]) OR $matches[5] = ' '.$this->protect_identifiers(trim($matches[5]));
Andrey Andreevd40459d2012-07-18 16:46:39 +03002094 $this->{$qb_key}[$i] = $matches[1].$matches[2].$this->protect_identifiers(trim($matches[3]))
Andrey Andreev6e704752012-07-18 00:46:33 +03002095 .' '.trim($matches[4]).$matches[5].$matches[6];
2096 }
2097
Andrey Andreevd40459d2012-07-18 16:46:39 +03002098 return implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002099 }
2100
Andrey Andreevb0478652012-07-18 15:34:46 +03002101 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002102 }
2103
2104 // --------------------------------------------------------------------
2105
2106 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002107 * Object to Array
2108 *
2109 * Takes an object as input and converts the class variables to array key/vals
2110 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002111 * @param object
2112 * @return array
2113 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002114 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002115 {
2116 if ( ! is_object($object))
2117 {
2118 return $object;
2119 }
Barry Mienydd671972010-10-04 16:33:58 +02002120
Derek Allard2067d1a2008-11-13 22:59:24 +00002121 $array = array();
2122 foreach (get_object_vars($object) as $key => $val)
2123 {
2124 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002125 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002126 {
2127 $array[$key] = $val;
2128 }
2129 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002130
2131 return $array;
2132 }
Barry Mienydd671972010-10-04 16:33:58 +02002133
Derek Jonesd10e8962010-03-02 17:10:36 -06002134 // --------------------------------------------------------------------
2135
2136 /**
2137 * Object to Array
2138 *
2139 * Takes an object as input and converts the class variables to array key/vals
2140 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002141 * @param object
2142 * @return array
2143 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002144 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002145 {
2146 if ( ! is_object($object))
2147 {
2148 return $object;
2149 }
Barry Mienydd671972010-10-04 16:33:58 +02002150
Derek Jonesd10e8962010-03-02 17:10:36 -06002151 $array = array();
2152 $out = get_object_vars($object);
2153 $fields = array_keys($out);
2154
2155 foreach ($fields as $val)
2156 {
2157 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002158 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002159 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002160 $i = 0;
2161 foreach ($out[$val] as $data)
2162 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002163 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002164 }
2165 }
2166 }
2167
Derek Allard2067d1a2008-11-13 22:59:24 +00002168 return $array;
2169 }
Barry Mienydd671972010-10-04 16:33:58 +02002170
Derek Allard2067d1a2008-11-13 22:59:24 +00002171 // --------------------------------------------------------------------
2172
2173 /**
2174 * Start Cache
2175 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002176 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002177 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002178 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002179 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002180 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002181 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002182 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002183 }
2184
2185 // --------------------------------------------------------------------
2186
2187 /**
2188 * Stop Cache
2189 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002190 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002191 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002192 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002193 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002194 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002195 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002196 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002197 }
2198
2199 // --------------------------------------------------------------------
2200
2201 /**
2202 * Flush Cache
2203 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002204 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002205 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002206 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002207 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002208 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002209 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002210 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002211 'qb_cache_select' => array(),
2212 'qb_cache_from' => array(),
2213 'qb_cache_join' => array(),
2214 'qb_cache_where' => array(),
2215 'qb_cache_like' => array(),
2216 'qb_cache_groupby' => array(),
2217 'qb_cache_having' => array(),
2218 'qb_cache_orderby' => array(),
2219 'qb_cache_set' => array(),
2220 'qb_cache_exists' => array(),
2221 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002222 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002223 }
2224
2225 // --------------------------------------------------------------------
2226
2227 /**
2228 * Merge Cache
2229 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002230 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002231 * locally called ones.
2232 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002233 * @return void
2234 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002235 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002236 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002237 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002238 {
2239 return;
2240 }
2241
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002242 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002243 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002244 $qb_variable = 'qb_'.$val;
2245 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002246
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002247 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002248 {
2249 continue;
2250 }
2251
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002252 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002253 }
2254
2255 // If we are "protecting identifiers" we need to examine the "from"
2256 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002257 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002258 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002259 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002260 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002261
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002262 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002263 }
WanWizard7219c072011-12-28 14:09:05 +01002264
Kyle Farris0c147b32011-08-26 02:29:31 -04002265 // --------------------------------------------------------------------
2266
2267 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002268 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002269 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002270 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002271 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002272 * @return void
2273 */
2274 public function reset_query()
2275 {
2276 $this->_reset_select();
2277 $this->_reset_write();
2278 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002279
2280 // --------------------------------------------------------------------
2281
2282 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002283 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002284 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002285 * @param array An array of fields to reset
2286 * @return void
2287 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002288 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002289 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002290 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002291 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002292 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002293 {
2294 $this->$item = $default_value;
2295 }
2296 }
2297 }
2298
2299 // --------------------------------------------------------------------
2300
2301 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002302 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002303 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002304 * @return void
2305 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002306 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002307 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002308 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002309 'qb_select' => array(),
2310 'qb_from' => array(),
2311 'qb_join' => array(),
2312 'qb_where' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002313 'qb_groupby' => array(),
2314 'qb_having' => array(),
2315 'qb_orderby' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002316 'qb_aliased_tables' => array(),
2317 'qb_no_escape' => array(),
2318 'qb_distinct' => FALSE,
2319 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002320 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002321 )
2322 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002323 }
Barry Mienydd671972010-10-04 16:33:58 +02002324
Derek Allard2067d1a2008-11-13 22:59:24 +00002325 // --------------------------------------------------------------------
2326
2327 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002328 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002329 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002330 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002331 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002332 * @return void
2333 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002334 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002335 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002336 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002337 'qb_set' => array(),
2338 'qb_from' => array(),
2339 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002340 'qb_orderby' => array(),
2341 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002342 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002343 )
2344 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002345 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002346
Derek Allard2067d1a2008-11-13 22:59:24 +00002347}
2348
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002349/* End of file DB_query_builder.php */
Andrey Andreev58803fb2012-06-24 00:45:37 +03002350/* Location: ./system/database/DB_query_builder.php */