blob: a3585586e06742cc9593065aa51bbb8a0ec69a72 [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 /**
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300185 * Processing Function for the following functions:
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 *
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 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300192 *
193 * @param string $select = '' field name
194 * @param string $alias = ''
195 * @param string $type = 'MAX'
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 * @return object
197 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600198 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100200 if ( ! is_string($select) OR $select === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
202 $this->display_error('db_invalid_query');
203 }
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
208 {
209 show_error('Invalid function type: '.$type);
210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100212 if ($alias === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
214 $alias = $this->_create_alias_from_table(trim($select));
215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300217 $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->escape_identifiers(trim($alias));
218
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000219 $this->qb_select[] = $sql;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100220 $this->qb_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200221
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000222 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000224 $this->qb_cache_select[] = $sql;
225 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 return $this;
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Determines the alias name based on the table
235 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 * @param string
237 * @return string
238 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600239 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
241 if (strpos($item, '.') !== FALSE)
242 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200243 $item = explode('.', $item);
244 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 return $item;
248 }
249
250 // --------------------------------------------------------------------
251
252 /**
253 * DISTINCT
254 *
255 * Sets a flag which tells the query string compiler to add DISTINCT
256 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 * @param bool
258 * @return object
259 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600260 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300262 $this->qb_distinct = is_bool($val) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 return $this;
264 }
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 // --------------------------------------------------------------------
267
268 /**
269 * From
270 *
271 * Generates the FROM portion of the query
272 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * @param mixed can be a string or array
274 * @return object
275 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600276 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300278 foreach ((array) $from as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 if (strpos($val, ',') !== FALSE)
281 {
282 foreach (explode(',', $val) as $v)
283 {
284 $v = trim($v);
285 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200286
George Petsagourakis193d4482012-04-28 11:16:18 +0300287 $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000288
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000289 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000291 $this->qb_cache_from[] = $v;
292 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200293 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
296 else
297 {
298 $val = trim($val);
299
Andrey Andreev24276a32012-01-08 02:44:38 +0200300 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000301 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200303
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000304 $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000305
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000306 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000308 $this->qb_cache_from[] = $val;
309 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 }
311 }
312 }
313
314 return $this;
315 }
316
317 // --------------------------------------------------------------------
318
319 /**
320 * Join
321 *
322 * Generates the JOIN portion of the query
323 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * @param string
325 * @param string the join condition
326 * @param string the type of join
Alex Bilbief512b732012-06-16 11:15:19 +0100327 * @param string whether not to try to escape identifiers
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 * @return object
329 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300330 public function join($table, $cond, $type = '', $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200331 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100332 if ($type !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 $type = strtoupper(trim($type));
335
Andrey Andreev42870232012-06-12 01:30:20 +0300336 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 $type = '';
339 }
340 else
341 {
342 $type .= ' ';
343 }
344 }
345
Andrey Andreev24276a32012-01-08 02:44:38 +0200346 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000347 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 $this->_track_aliases($table);
349
Andrey Andreevfe642da2012-06-16 03:47:33 +0300350 is_bool($escape) OR $escape = $this->_protect_identifiers;
351
Andrey Andreev42870232012-06-12 01:30:20 +0300352 // Split multiple conditions
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300353 if ($escape === TRUE && preg_match_all('/\sAND\s|\sOR\s/i', $cond, $m, PREG_OFFSET_CAPTURE))
Andrey Andreev42870232012-06-12 01:30:20 +0300354 {
355 $newcond = '';
356 $m[0][] = array('', strlen($cond));
357
358 for ($i = 0, $c = count($m[0]), $s = 0;
359 $i < $c;
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300360 $s = $m[0][$i][1] + strlen($m[0][$i][0]), $i++)
Andrey Andreev42870232012-06-12 01:30:20 +0300361 {
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300362 $temp = substr($cond, $s, ($m[0][$i][1] - $s));
Andrey Andreev42870232012-06-12 01:30:20 +0300363
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300364 $newcond .= preg_match("/([\[\]\w\.'-]+)(\s*[^\"\[`'\w]+\s*)(.+)/i", $temp, $match)
Andrey Andreev42870232012-06-12 01:30:20 +0300365 ? $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3])
366 : $temp;
367
368 $newcond .= $m[0][$i][0];
369 }
370
Andrey Andreev3751f932012-06-17 18:07:48 +0300371 $cond = ' ON '.$newcond;
Andrey Andreev42870232012-06-12 01:30:20 +0300372 }
373 // Split apart the condition and protect the identifiers
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300374 elseif ($escape === TRUE && preg_match("/([\[\]\w\.'-]+)(\s*[^\"\[`'\w]+\s*)(.+)/i", $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
Andrey Andreev3751f932012-06-17 18:07:48 +0300376 $cond = ' ON '.$this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
377 }
378 elseif ( ! $this->_has_operator($cond))
379 {
380 $cond = ' USING ('.($escape ? $this->escape_identifiers($cond) : $cond).')';
381 }
382 else
383 {
384 $cond = ' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Andrey Andreev42870232012-06-12 01:30:20 +0300387 // Do we want to escape the table name?
388 if ($escape === TRUE)
389 {
390 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
391 }
392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 // Assemble the JOIN statement
Andrey Andreev3751f932012-06-17 18:07:48 +0300394 $this->qb_join[] = $join = $type.'JOIN '.$table.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000395
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000396 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000398 $this->qb_cache_join[] = $join;
399 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 }
401
402 return $this;
403 }
404
405 // --------------------------------------------------------------------
406
407 /**
408 * Where
409 *
410 * Generates the WHERE portion of the query. Separates
411 * multiple calls with AND
412 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 * @param mixed
414 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300415 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 * @return object
417 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300418 public function where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300420 return $this->_wh('qb_where', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 }
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 // --------------------------------------------------------------------
424
425 /**
426 * OR Where
427 *
428 * Generates the WHERE portion of the query. Separates
429 * multiple calls with OR
430 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 * @param mixed
432 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300433 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 * @return object
435 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300436 public function or_where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300438 return $this->_wh('qb_where', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
440
441 // --------------------------------------------------------------------
442
443 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +0300444 * WHERE, HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300446 * Called by where(), or_where(), having(), or_having()
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300448 * @param string 'qb_where' or 'qb_having'
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 * @param mixed
450 * @param mixed
451 * @param string
Andrey Andreevb0478652012-07-18 15:34:46 +0300452 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 * @return object
454 */
Andrey Andreevd40459d2012-07-18 16:46:39 +0300455 protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300457 $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where';
458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 if ( ! is_array($key))
460 {
461 $key = array($key => $value);
462 }
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 // If the escape value was not set will will base it on the global setting
Andrey Andreeve10fb792012-06-15 12:07:04 +0300465 is_bool($escape) OR $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000466
467 foreach ($key as $k => $v)
468 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300469 $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0)
Andrey Andreev58803fb2012-06-24 00:45:37 +0300470 ? $this->_group_get_type('')
471 : $this->_group_get_type($type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000472
473 if (is_null($v) && ! $this->_has_operator($k))
474 {
475 // value appears not to have been set, assign the test to IS NULL
476 $k .= ' IS NULL';
477 }
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 if ( ! is_null($v))
480 {
481 if ($escape === TRUE)
482 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300483 $v = ' '.(is_int($v) ? $v : $this->escape($v));
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
WanWizard7219c072011-12-28 14:09:05 +0100485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 if ( ! $this->_has_operator($k))
487 {
Greg Akere156c6e2011-04-20 16:03:04 -0500488 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
490 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000491
Andrey Andreevd40459d2012-07-18 16:46:39 +0300492 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000493 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300495 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
496 $this->qb_cache_exists[] = substr($qb_key, 3);
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 }
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 return $this;
502 }
503
504 // --------------------------------------------------------------------
505
506 /**
507 * Where_in
508 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300509 * Generates a WHERE field IN('item', 'item') SQL query joined with
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 * AND if appropriate
511 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300512 * @param string $key = NULL The field to search
513 * @param array $values = NULL The values searched on
514 * @param bool $escape = NULL
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 * @return object
516 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300517 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300519 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 }
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 // --------------------------------------------------------------------
523
524 /**
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300525 * Or_where_in
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300527 * Generates a WHERE field IN('item', 'item') SQL query joined with
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 * OR if appropriate
529 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300530 * @param string $key = NULL The field to search
531 * @param array $values = NULL The values searched on
532 * @param bool $escape = NULL
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 * @return object
534 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300535 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300537 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 }
539
540 // --------------------------------------------------------------------
541
542 /**
543 * Where_not_in
544 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300545 * Generates a WHERE field NOT IN('item', 'item') SQL query joined
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 * with AND if appropriate
547 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300548 * @param string $key = NULL The field to search
549 * @param array $values = NULL The values searched on
550 * @param bool $escape = NULL
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 * @return object
552 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300553 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300555 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 }
Barry Mienydd671972010-10-04 16:33:58 +0200557
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 // --------------------------------------------------------------------
559
560 /**
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300561 * Or_where_not_in
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300563 * Generates a WHERE field NOT IN('item', 'item') SQL query joined
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 * with OR if appropriate
565 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300566 * @param string $key = NULL The field to search
567 * @param array $values = NULL The values searched on
568 * @param bool $escape = NULL
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 * @return object
570 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300571 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300573 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 }
575
576 // --------------------------------------------------------------------
577
578 /**
579 * Where_in
580 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300581 * Called by where_in(), or_where_in(), where_not_in(), or_where_not_in()
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300583 * @param string $key = NULL The field to search
584 * @param array $values = NULL The values searched on
585 * @param bool $not = FALSE If the statement would be IN or NOT IN
586 * @param string $type = 'AND '
587 * @param bool $escape = NULL
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 * @return object
589 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300590 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 {
592 if ($key === NULL OR $values === NULL)
593 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300594 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 }
Barry Mienydd671972010-10-04 16:33:58 +0200596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 if ( ! is_array($values))
598 {
599 $values = array($values);
600 }
Barry Mienydd671972010-10-04 16:33:58 +0200601
Andrey Andreev498c1e02012-06-16 03:34:10 +0300602 is_bool($escape) OR $escape = $this->_protect_identifiers;
603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 $not = ($not) ? ' NOT' : '';
605
Andrey Andreev94611df2012-07-19 12:29:54 +0300606 $where_in = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 foreach ($values as $value)
608 {
Andrey Andreevcc02db92012-10-12 14:30:10 +0300609 $where_in[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 }
611
WanWizardbc69f362012-06-22 00:10:11 +0200612 $prefix = (count($this->qb_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Andrey Andreev6e704752012-07-18 00:46:33 +0300613 $where_in = array(
Andrey Andreev94611df2012-07-19 12:29:54 +0300614 'condition' => $prefix.$key.$not.' IN('.implode(', ', $where_in).')',
Andrey Andreev6e704752012-07-18 00:46:33 +0300615 'escape' => $escape
616 );
Barry Mienydd671972010-10-04 16:33:58 +0200617
Andrey Andreev6e704752012-07-18 00:46:33 +0300618 $this->qb_where[] = $where_in;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000619 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000621 $this->qb_cache_where[] = $where_in;
622 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 }
624
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 return $this;
626 }
Barry Mienydd671972010-10-04 16:33:58 +0200627
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 // --------------------------------------------------------------------
629
630 /**
631 * Like
632 *
633 * Generates a %LIKE% portion of the query. Separates
634 * multiple calls with AND
635 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300637 * @param string
638 * @param string
639 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 * @return object
641 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300642 public function like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300644 return $this->_like($field, $match, 'AND ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 }
646
647 // --------------------------------------------------------------------
648
649 /**
650 * Not Like
651 *
652 * Generates a NOT LIKE portion of the query. Separates
653 * multiple calls with AND
654 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300656 * @param string
657 * @param string
658 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 * @return object
660 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300661 public function not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300663 return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 }
Barry Mienydd671972010-10-04 16:33:58 +0200665
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 // --------------------------------------------------------------------
667
668 /**
669 * OR Like
670 *
671 * Generates a %LIKE% portion of the query. Separates
672 * multiple calls with OR
673 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300675 * @param string
676 * @param string
677 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 * @return object
679 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300680 public function or_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300682 return $this->_like($field, $match, 'OR ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 }
684
685 // --------------------------------------------------------------------
686
687 /**
688 * OR Not Like
689 *
690 * Generates a NOT LIKE portion of the query. Separates
691 * multiple calls with OR
692 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300694 * @param string
695 * @param string
696 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 * @return object
698 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300699 public function or_not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300701 return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 }
Barry Mienydd671972010-10-04 16:33:58 +0200703
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 // --------------------------------------------------------------------
705
706 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 * Like
708 *
Andrey Andreevb0478652012-07-18 15:34:46 +0300709 * Called by like(), or_like(), not_like, or_not_like()
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 * @param string
Andrey Andreevb0478652012-07-18 15:34:46 +0300713 * @param string
714 * @param string
715 * @param string
716 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 * @return object
718 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300719 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 {
721 if ( ! is_array($field))
722 {
723 $field = array($field => $match);
724 }
Barry Mienydd671972010-10-04 16:33:58 +0200725
Andrey Andreevb0478652012-07-18 15:34:46 +0300726 is_bool($escape) OR $escape = $this->_protect_identifiers;
727 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
728 ? $this->_group_get_type('') : $this->_group_get_type($type);
729
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 foreach ($field as $k => $v)
731 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000732 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300733
Andrey Andreev24276a32012-01-08 02:44:38 +0200734 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400735 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300736 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400737 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200738 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300740 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200742 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300744 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 }
746 else
747 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300748 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600750
Derek Jonese4ed5832009-02-20 21:44:59 +0000751 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100752 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000753 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300754 $like_statement .= sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000755 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600756
Andrey Andreevb0478652012-07-18 15:34:46 +0300757 $this->qb_where[] = array('condition' => $like_statement, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000758 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 {
Andrey Andreevededc4a2012-07-18 01:16:15 +0300760 $this->qb_cache_where[] = $like_statement;
761 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200764
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 return $this;
766 }
Barry Mienydd671972010-10-04 16:33:58 +0200767
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 // --------------------------------------------------------------------
769
770 /**
WanWizard7219c072011-12-28 14:09:05 +0100771 * Starts a query group.
772 *
773 * @param string (Internal use only)
774 * @param string (Internal use only)
775 * @return object
776 */
777 public function group_start($not = '', $type = 'AND ')
778 {
779 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100780
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000781 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100782 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Andrey Andreev6e704752012-07-18 00:46:33 +0300783 $where = array(
784 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (',
785 'escape' => FALSE
786 );
WanWizard7219c072011-12-28 14:09:05 +0100787
Andrey Andreev6e704752012-07-18 00:46:33 +0300788 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000789 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100790 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300791 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +0100792 }
793
794 return $this;
795 }
796
797 // --------------------------------------------------------------------
798
799 /**
800 * Starts a query group, but ORs the group
801 *
802 * @return object
803 */
804 public function or_group_start()
805 {
806 return $this->group_start('', 'OR ');
807 }
808
809 // --------------------------------------------------------------------
810
811 /**
812 * Starts a query group, but NOTs the group
813 *
814 * @return object
815 */
816 public function not_group_start()
817 {
818 return $this->group_start('NOT ', 'AND ');
819 }
820
821 // --------------------------------------------------------------------
822
823 /**
824 * Starts a query group, but OR NOTs the group
825 *
826 * @return object
827 */
828 public function or_not_group_start()
829 {
830 return $this->group_start('NOT ', 'OR ');
831 }
832
833 // --------------------------------------------------------------------
834
835 /**
836 * Ends a query group
837 *
838 * @return object
839 */
840 public function group_end()
841 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000842 $this->qb_where_group_started = FALSE;
Andrey Andreev6e704752012-07-18 00:46:33 +0300843 $where = array(
844 'condition' => str_repeat(' ', $this->qb_where_group_count--).')',
845 'escape' => FALSE
846 );
WanWizard7219c072011-12-28 14:09:05 +0100847
Andrey Andreev6e704752012-07-18 00:46:33 +0300848 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000849 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100850 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300851 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +0100852 }
853
WanWizard7219c072011-12-28 14:09:05 +0100854 return $this;
855 }
856
857 // --------------------------------------------------------------------
858
859 /**
860 * Group_get_type
861 *
862 * Called by group_start(), _like(), _where() and _where_in()
863 *
864 * @param string
865 * @return string
866 */
867 protected function _group_get_type($type)
868 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000869 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100870 {
871 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000872 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100873 }
874
875 return $type;
876 }
877
878 // --------------------------------------------------------------------
879
880 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 * GROUP BY
882 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 * @param string
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300884 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 * @return object
886 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300887 public function group_by($by, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 {
Andrey Andreev96feb582012-07-19 13:12:34 +0300889 is_bool($escape) OR $escape = $this->_protect_identifiers;
890
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 if (is_string($by))
892 {
Andrey Andreev96feb582012-07-19 13:12:34 +0300893 $by = ($escape === TRUE)
894 ? explode(',', $by)
895 : array($by);
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 }
Barry Mienydd671972010-10-04 16:33:58 +0200897
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 foreach ($by as $val)
899 {
900 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200901
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100902 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 {
Andrey Andreev96feb582012-07-19 13:12:34 +0300904 $val = array('field' => $val, 'escape' => $escape);
Barry Mienydd671972010-10-04 16:33:58 +0200905
Andrey Andreev96feb582012-07-19 13:12:34 +0300906 $this->qb_groupby[] = $val;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000907 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000909 $this->qb_cache_groupby[] = $val;
910 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 }
912 }
913 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200914
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 return $this;
916 }
917
918 // --------------------------------------------------------------------
919
920 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 * Sets the HAVING value
922 *
923 * Separates multiple calls with AND
924 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 * @param string
926 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300927 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 * @return object
929 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +0300930 public function having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300932 return $this->_wh('qb_having', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 }
Barry Mienydd671972010-10-04 16:33:58 +0200934
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 // --------------------------------------------------------------------
936
937 /**
938 * Sets the OR HAVING value
939 *
940 * Separates multiple calls with OR
941 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 * @param string
943 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300944 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 * @return object
946 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +0300947 public function or_having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300949 return $this->_wh('qb_having', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 }
Barry Mienydd671972010-10-04 16:33:58 +0200951
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 // --------------------------------------------------------------------
953
954 /**
955 * Sets the ORDER BY value
956 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 * @param string
Andrey Andreev2d486232012-07-19 14:46:51 +0300958 * @param string direction: ASC or DESC
pporlan2c685fb2011-12-22 12:15:25 +0100959 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 * @return object
961 */
Andrey Andreevd24160c2012-06-16 03:21:20 +0300962 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300964 $direction = trim($direction);
965
966 if (strtolower($direction) === 'random' OR $orderby === $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300968 // Random ordered results don't need a field name
969 $orderby = $this->_random_keyword;
970 $direction = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 }
Andrey Andreev2d486232012-07-19 14:46:51 +0300972 elseif (empty($orderby))
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300974 return $this;
975 }
976 elseif ($direction !== '')
977 {
978 $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 }
Barry Mienydd671972010-10-04 16:33:58 +0200980
Andrey Andreevd24160c2012-06-16 03:21:20 +0300981 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +0200982
Andrey Andreev2d486232012-07-19 14:46:51 +0300983 if ($escape === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 {
Andrey Andreev93dd2f22012-10-24 10:09:18 +0300985 $qb_orderby[] = array('field' => $orderby, 'direction' => $direction, 'escape' => FALSE);
Andrey Andreev2d486232012-07-19 14:46:51 +0300986 }
987 else
988 {
989 $qb_orderby = array();
990 foreach (explode(',', $orderby) as $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300992 $qb_orderby[] = ($direction === '' && preg_match('/\s+(ASC|DESC)$/i', rtrim($field), $match, PREG_OFFSET_CAPTURE))
993 ? array('field' => ltrim(substr($field, 0, $match[0][1])), 'direction' => ' '.$match[1][0], 'escape' => TRUE)
994 : array('field' => trim($field), 'direction' => $direction, 'escape' => TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 }
Barry Mienydd671972010-10-04 16:33:58 +0200997
Andrey Andreev2d486232012-07-19 14:46:51 +0300998 $this->qb_orderby = array_merge($this->qb_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000999 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001001 $this->qb_cache_orderby = array_merge($this->qb_cache_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001002 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 }
1004
1005 return $this;
1006 }
Barry Mienydd671972010-10-04 16:33:58 +02001007
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 // --------------------------------------------------------------------
1009
1010 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 * Sets the LIMIT value
1012 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001013 * @param int the limit value
1014 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 * @return object
1016 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001017 public function limit($value, $offset = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001019 is_null($value) OR $this->qb_limit = (int) $value;
1020 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001021
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 return $this;
1023 }
Barry Mienydd671972010-10-04 16:33:58 +02001024
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 // --------------------------------------------------------------------
1026
1027 /**
1028 * Sets the OFFSET value
1029 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001030 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 * @return object
1032 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001033 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001035 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 return $this;
1037 }
Barry Mienydd671972010-10-04 16:33:58 +02001038
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 // --------------------------------------------------------------------
1040
1041 /**
Andrey Andreev2c35b642012-06-24 03:05:26 +03001042 * Limit string
1043 *
1044 * Generates a platform-specific LIMIT clause
1045 *
1046 * @param string the sql query string
Andrey Andreev2c35b642012-06-24 03:05:26 +03001047 * @return string
1048 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001049 protected function _limit($sql)
Andrey Andreev2c35b642012-06-24 03:05:26 +03001050 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001051 return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').$this->qb_limit;
Andrey Andreev2c35b642012-06-24 03:05:26 +03001052 }
1053
1054 // --------------------------------------------------------------------
1055
1056 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001057 * The "set" function.
1058 *
1059 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 * @param mixed
1062 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001063 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 * @return object
1065 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001066 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 {
1068 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001069
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 if ( ! is_array($key))
1071 {
1072 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001073 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001074
Andrey Andreevfe642da2012-06-16 03:47:33 +03001075 is_bool($escape) OR $escape = $this->_protect_identifiers;
1076
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 foreach ($key as $k => $v)
1078 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001079 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1080 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 }
Barry Mienydd671972010-10-04 16:33:58 +02001082
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 return $this;
1084 }
WanWizard7219c072011-12-28 14:09:05 +01001085
Kyle Farris0c147b32011-08-26 02:29:31 -04001086 // --------------------------------------------------------------------
1087
1088 /**
1089 * Get SELECT query string
1090 *
1091 * Compiles a SELECT query string and returns the sql.
1092 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001093 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001094 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001095 * @return string
1096 */
WanWizard7219c072011-12-28 14:09:05 +01001097 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001098 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001099 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001100 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001101 $this->_track_aliases($table);
1102 $this->from($table);
1103 }
WanWizard7219c072011-12-28 14:09:05 +01001104
Andrey Andreev650b4c02012-06-11 12:07:15 +03001105 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001106
Kyle Farris0c147b32011-08-26 02:29:31 -04001107 if ($reset === TRUE)
1108 {
1109 $this->_reset_select();
1110 }
WanWizard7219c072011-12-28 14:09:05 +01001111
Kyle Farris0c147b32011-08-26 02:29:31 -04001112 return $select;
1113 }
WanWizard7219c072011-12-28 14:09:05 +01001114
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 // --------------------------------------------------------------------
1116
1117 /**
1118 * Get
1119 *
1120 * Compiles the select statement based on the other functions called
1121 * and runs the query
1122 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 * @param string the table
1124 * @param string the limit clause
1125 * @param string the offset clause
1126 * @return object
1127 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001128 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001130 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 {
1132 $this->_track_aliases($table);
1133 $this->from($table);
1134 }
Barry Mienydd671972010-10-04 16:33:58 +02001135
Andrey Andreev650b4c02012-06-11 12:07:15 +03001136 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 {
1138 $this->limit($limit, $offset);
1139 }
Barry Mienydd671972010-10-04 16:33:58 +02001140
Andrey Andreev24276a32012-01-08 02:44:38 +02001141 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 $this->_reset_select();
1143 return $result;
1144 }
1145
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001146 // --------------------------------------------------------------------
1147
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 /**
1149 * "Count All Results" query
1150 *
Barry Mienydd671972010-10-04 16:33:58 +02001151 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001152 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 * @param string
1155 * @return string
1156 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001157 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001159 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 {
1161 $this->_track_aliases($table);
1162 $this->from($table);
1163 }
Barry Mienydd671972010-10-04 16:33:58 +02001164
Andrey Andreevb05f5062012-10-26 12:01:02 +03001165 $result = ($this->qb_distinct === TRUE)
1166 ? $this->query($this->_count_string.$this->protect_identifiers('numrows')."\nFROM (\n".$this->_compile_select()."\n) CI_count_all_results")
1167 : $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001169
Purwandi1d160e72012-01-09 16:33:28 +07001170 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001172 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 }
1174
Purwandi1d160e72012-01-09 16:33:28 +07001175 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001176 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001178
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 // --------------------------------------------------------------------
1180
1181 /**
1182 * Get_Where
1183 *
1184 * Allows the where clause, limit and offset to be added directly
1185 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001186 * @param string $table = ''
1187 * @param string $where = NULL
1188 * @param int $limit = NULL
1189 * @param int $offset = NULL
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 * @return object
1191 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001192 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001194 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 {
1196 $this->from($table);
1197 }
1198
1199 if ( ! is_null($where))
1200 {
1201 $this->where($where);
1202 }
Barry Mienydd671972010-10-04 16:33:58 +02001203
Andrey Andreev650b4c02012-06-11 12:07:15 +03001204 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 {
1206 $this->limit($limit, $offset);
1207 }
Barry Mienydd671972010-10-04 16:33:58 +02001208
Andrey Andreev24276a32012-01-08 02:44:38 +02001209 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 $this->_reset_select();
1211 return $result;
1212 }
1213
1214 // --------------------------------------------------------------------
1215
1216 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001217 * Insert_Batch
1218 *
1219 * Compiles batch insert strings and runs the queries
1220 *
Andrey Andreev9f808b02012-10-24 17:38:48 +03001221 * @param string $table = '' table to insert into
1222 * @param array $set an associative array of insert values
1223 * @return int number of rows inserted or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001224 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001225 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001226 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001227 if ( ! is_null($set))
1228 {
1229 $this->set_insert_batch($set);
1230 }
Barry Mienydd671972010-10-04 16:33:58 +02001231
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001232 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001233 {
Andrey Andreev9f808b02012-10-24 17:38:48 +03001234 // No valid data array. Folds in cases where keys and values did not match up
1235 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001236 }
1237
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001238 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001239 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001240 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001241 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001242 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001243 }
Barry Mienydd671972010-10-04 16:33:58 +02001244
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001245 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001246 }
1247
1248 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001249 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001250 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001251 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001252 $this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, 100)));
Andrey Andreev9f808b02012-10-24 17:38:48 +03001253 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001254 }
Barry Mienydd671972010-10-04 16:33:58 +02001255
Derek Jonesd10e8962010-03-02 17:10:36 -06001256 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001257 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001258 }
1259
1260 // --------------------------------------------------------------------
1261
1262 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001263 * Insert_batch statement
1264 *
1265 * Generates a platform-specific insert string from the supplied data.
1266 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001267 * @param string the table name
1268 * @param array the insert keys
1269 * @param array the insert values
1270 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001271 */
1272 protected function _insert_batch($table, $keys, $values)
1273 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001274 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001275 }
1276
1277 // --------------------------------------------------------------------
1278
1279 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001280 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001281 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001282 * @param mixed
1283 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001284 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001285 * @return object
1286 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001287 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001288 {
1289 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001290
Derek Jonesd10e8962010-03-02 17:10:36 -06001291 if ( ! is_array($key))
1292 {
1293 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001294 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001295
Andrey Andreevfe642da2012-06-16 03:47:33 +03001296 is_bool($escape) OR $escape = $this->_protect_identifiers;
1297
Iban Eguia3c0a4522012-04-15 13:30:44 +02001298 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001299 sort($keys);
1300
1301 foreach ($key as $row)
1302 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001303 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001304 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1305 {
1306 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001307 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001308 return;
1309 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001310
Barry Mienydd671972010-10-04 16:33:58 +02001311 ksort($row); // puts $row in the same order as our keys
1312
Andrey Andreev650b4c02012-06-11 12:07:15 +03001313 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001314 {
1315 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001316 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001317 {
Barry Mienydd671972010-10-04 16:33:58 +02001318 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001319 }
1320
Andrey Andreev650b4c02012-06-11 12:07:15 +03001321 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001322 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001323
1324 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001325 }
1326
1327 foreach ($keys as $k)
1328 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001329 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001330 }
Barry Mienydd671972010-10-04 16:33:58 +02001331
Derek Jonesd10e8962010-03-02 17:10:36 -06001332 return $this;
1333 }
WanWizard7219c072011-12-28 14:09:05 +01001334
Kyle Farris0c147b32011-08-26 02:29:31 -04001335 // --------------------------------------------------------------------
1336
1337 /**
1338 * Get INSERT query string
1339 *
1340 * Compiles an insert query and returns the sql
1341 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001342 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001343 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001344 * @return string
1345 */
1346 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001347 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001348 if ($this->_validate_insert($table) === FALSE)
1349 {
1350 return FALSE;
1351 }
WanWizard7219c072011-12-28 14:09:05 +01001352
Kyle Farris76116012011-08-31 11:17:48 -04001353 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001354 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001355 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001356 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001357 array_keys($this->qb_set),
1358 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001359 );
WanWizard7219c072011-12-28 14:09:05 +01001360
Kyle Farris0c147b32011-08-26 02:29:31 -04001361 if ($reset === TRUE)
1362 {
1363 $this->_reset_write();
1364 }
WanWizard7219c072011-12-28 14:09:05 +01001365
Kyle Farris0c147b32011-08-26 02:29:31 -04001366 return $sql;
1367 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001368
Derek Allard2067d1a2008-11-13 22:59:24 +00001369 // --------------------------------------------------------------------
1370
1371 /**
1372 * Insert
1373 *
1374 * Compiles an insert string and runs the query
1375 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001376 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001377 * @param array an associative array of insert values
1378 * @return object
1379 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001380 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001381 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001382 if ( ! is_null($set))
1383 {
1384 $this->set($set);
1385 }
WanWizard7219c072011-12-28 14:09:05 +01001386
Kyle Farris0c147b32011-08-26 02:29:31 -04001387 if ($this->_validate_insert($table) === FALSE)
1388 {
1389 return FALSE;
1390 }
WanWizard7219c072011-12-28 14:09:05 +01001391
Kyle Farris76116012011-08-31 11:17:48 -04001392 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001393 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001394 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001395 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001396 array_keys($this->qb_set),
1397 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001398 );
Barry Mienydd671972010-10-04 16:33:58 +02001399
Kyle Farris0c147b32011-08-26 02:29:31 -04001400 $this->_reset_write();
1401 return $this->query($sql);
1402 }
WanWizard7219c072011-12-28 14:09:05 +01001403
Kyle Farris0c147b32011-08-26 02:29:31 -04001404 // --------------------------------------------------------------------
1405
1406 /**
1407 * Validate Insert
1408 *
1409 * This method is used by both insert() and get_compiled_insert() to
1410 * validate that the there data is actually being set and that table
1411 * has been chosen to be inserted into.
1412 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001413 * @param string the table to insert data into
1414 * @return string
1415 */
WanWizard7219c072011-12-28 14:09:05 +01001416 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001417 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001418 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001419 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001420 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001421 }
1422
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001423 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001424 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001425 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001426 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001427 elseif ( ! isset($this->qb_from[0]))
1428 {
1429 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1430 }
WanWizard7219c072011-12-28 14:09:05 +01001431
Kyle Farris0c147b32011-08-26 02:29:31 -04001432 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001433 }
Barry Mienydd671972010-10-04 16:33:58 +02001434
Phil Sturgeon9789f322011-07-15 15:14:05 -06001435 // --------------------------------------------------------------------
1436
1437 /**
1438 * Replace
1439 *
1440 * Compiles an replace into string and runs the query
1441 *
1442 * @param string the table to replace data into
1443 * @param array an associative array of insert values
1444 * @return object
1445 */
1446 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001447 {
1448 if ( ! is_null($set))
1449 {
1450 $this->set($set);
1451 }
Barry Mienydd671972010-10-04 16:33:58 +02001452
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001453 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001454 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001455 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001456 }
1457
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001458 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001459 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001460 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001461 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001462 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001463 }
Barry Mienydd671972010-10-04 16:33:58 +02001464
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001465 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001466 }
1467
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001468 $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 +00001469
Derek Jonesd10e8962010-03-02 17:10:36 -06001470 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001471 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001472 }
WanWizard7219c072011-12-28 14:09:05 +01001473
Kyle Farris0c147b32011-08-26 02:29:31 -04001474 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001475
Kyle Farris0c147b32011-08-26 02:29:31 -04001476 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001477 * Replace statement
1478 *
1479 * Generates a platform-specific replace string from the supplied data
1480 *
1481 * @param string the table name
1482 * @param array the insert keys
1483 * @param array the insert values
1484 * @return string
1485 */
1486 protected function _replace($table, $keys, $values)
1487 {
1488 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1489 }
1490
1491 // --------------------------------------------------------------------
1492
1493 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001494 * FROM tables
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001495 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001496 * Groups tables in FROM clauses if needed, so there is no confusion
1497 * about operator precedence.
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001498 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001499 * Note: This is only used (and overriden) by MySQL and CUBRID.
1500 *
1501 * @return string
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001502 */
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001503 protected function _from_tables()
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001504 {
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001505 return implode(', ', $this->qb_from);
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001506 }
1507
1508 // --------------------------------------------------------------------
1509
1510 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001511 * Get UPDATE query string
1512 *
1513 * Compiles an update query and returns the sql
1514 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001515 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001516 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001517 * @return string
1518 */
1519 public function get_compiled_update($table = '', $reset = TRUE)
1520 {
1521 // Combine any cached components with the current statements
1522 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001523
Kyle Farris0c147b32011-08-26 02:29:31 -04001524 if ($this->_validate_update($table) === FALSE)
1525 {
1526 return FALSE;
1527 }
WanWizard7219c072011-12-28 14:09:05 +01001528
Andrey Andreevb0478652012-07-18 15:34:46 +03001529 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001530
Kyle Farris0c147b32011-08-26 02:29:31 -04001531 if ($reset === TRUE)
1532 {
1533 $this->_reset_write();
1534 }
WanWizard7219c072011-12-28 14:09:05 +01001535
Kyle Farris0c147b32011-08-26 02:29:31 -04001536 return $sql;
1537 }
WanWizard7219c072011-12-28 14:09:05 +01001538
Derek Allard2067d1a2008-11-13 22:59:24 +00001539 // --------------------------------------------------------------------
1540
1541 /**
1542 * Update
1543 *
1544 * Compiles an update string and runs the query
1545 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001546 * @param string $table = ''
1547 * @param array $set = NULL an associative array of update values
1548 * @param mixed $where = NULL
1549 * @param int $limit = NULL
Derek Allard2067d1a2008-11-13 22:59:24 +00001550 * @return object
1551 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001552 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001553 {
1554 // Combine any cached components with the current statements
1555 $this->_merge_cache();
1556
1557 if ( ! is_null($set))
1558 {
1559 $this->set($set);
1560 }
Barry Mienydd671972010-10-04 16:33:58 +02001561
Kyle Farris0c147b32011-08-26 02:29:31 -04001562 if ($this->_validate_update($table) === FALSE)
1563 {
1564 return FALSE;
1565 }
1566
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001567 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001568 {
1569 $this->where($where);
1570 }
1571
Andrey Andreev650b4c02012-06-11 12:07:15 +03001572 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001573 {
1574 $this->limit($limit);
1575 }
1576
Andrey Andreevb0478652012-07-18 15:34:46 +03001577 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
Jamie Rumbelow3b1355c2012-03-06 21:27:46 +00001578
Kyle Farris0c147b32011-08-26 02:29:31 -04001579 $this->_reset_write();
1580 return $this->query($sql);
1581 }
WanWizard7219c072011-12-28 14:09:05 +01001582
Kyle Farris0c147b32011-08-26 02:29:31 -04001583 // --------------------------------------------------------------------
1584
1585 /**
1586 * Validate Update
1587 *
1588 * This method is used by both update() and get_compiled_update() to
1589 * validate that data is actually being set and that a table has been
1590 * chosen to be update.
1591 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001592 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001593 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001594 */
1595 protected function _validate_update($table = '')
1596 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001597 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001598 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001599 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001600 }
1601
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001602 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001603 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001604 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001605 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001606 elseif ( ! isset($this->qb_from[0]))
1607 {
1608 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1609 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001610
1611 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001612 }
WanWizard7219c072011-12-28 14:09:05 +01001613
Derek Jonesd10e8962010-03-02 17:10:36 -06001614 // --------------------------------------------------------------------
1615
1616 /**
1617 * Update_Batch
1618 *
1619 * Compiles an update string and runs the query
1620 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001621 * @param string the table to retrieve the results from
1622 * @param array an associative array of update values
1623 * @param string the where key
Andrey Andreev9f808b02012-10-24 17:38:48 +03001624 * @return int number of rows affected or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001625 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001626 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001627 {
1628 // Combine any cached components with the current statements
1629 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001630
Derek Jonesd10e8962010-03-02 17:10:36 -06001631 if (is_null($index))
1632 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001633 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001634 }
1635
1636 if ( ! is_null($set))
1637 {
1638 $this->set_update_batch($set, $index);
1639 }
1640
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001641 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001642 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001643 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001644 }
1645
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001646 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001647 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001648 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001649 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001650 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001651 }
Barry Mienydd671972010-10-04 16:33:58 +02001652
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001653 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001654 }
Barry Mienydd671972010-10-04 16:33:58 +02001655
Derek Jonesd10e8962010-03-02 17:10:36 -06001656 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001657 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001658 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001659 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001660 $this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, 100), $this->protect_identifiers($index)));
Andrey Andreev9f808b02012-10-24 17:38:48 +03001661 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001662 }
Barry Mienydd671972010-10-04 16:33:58 +02001663
Derek Jonesd10e8962010-03-02 17:10:36 -06001664 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001665 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001666 }
1667
1668 // --------------------------------------------------------------------
1669
1670 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001671 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001672 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001673 * @param array
1674 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001675 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001676 * @return object
1677 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001678 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001679 {
1680 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001681
Derek Jonesd10e8962010-03-02 17:10:36 -06001682 if ( ! is_array($key))
1683 {
1684 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001685 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001686
Andrey Andreevfe642da2012-06-16 03:47:33 +03001687 is_bool($escape) OR $escape = $this->_protect_identifiers;
1688
Derek Jonesd10e8962010-03-02 17:10:36 -06001689 foreach ($key as $k => $v)
1690 {
1691 $index_set = FALSE;
1692 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001693 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001694 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001695 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001696 {
1697 $index_set = TRUE;
1698 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001699
Andrey Andreevfe642da2012-06-16 03:47:33 +03001700 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001701 }
1702
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001703 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001704 {
1705 return $this->display_error('db_batch_missing_index');
1706 }
1707
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001708 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001709 }
Barry Mienydd671972010-10-04 16:33:58 +02001710
Derek Jonesd10e8962010-03-02 17:10:36 -06001711 return $this;
1712 }
1713
Derek Allard2067d1a2008-11-13 22:59:24 +00001714 // --------------------------------------------------------------------
1715
1716 /**
1717 * Empty Table
1718 *
1719 * Compiles a delete string and runs "DELETE FROM table"
1720 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001721 * @param string the table to empty
1722 * @return object
1723 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001724 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001725 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001726 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001727 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001728 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001729 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001730 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 }
1732
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001733 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001734 }
1735 else
1736 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001737 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001738 }
1739
1740 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001741 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 return $this->query($sql);
1743 }
1744
1745 // --------------------------------------------------------------------
1746
1747 /**
1748 * Truncate
1749 *
1750 * Compiles a truncate string and runs the query
1751 * If the database does not support the truncate() command
1752 * This function maps to "DELETE FROM table"
1753 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001754 * @param string the table to truncate
1755 * @return object
1756 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001757 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001758 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001759 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001760 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001761 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001762 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001763 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001764 }
1765
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001766 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001767 }
1768 else
1769 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001770 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001771 }
1772
1773 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001774 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001775 return $this->query($sql);
1776 }
WanWizard7219c072011-12-28 14:09:05 +01001777
Kyle Farris0c147b32011-08-26 02:29:31 -04001778 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001779
Kyle Farris0c147b32011-08-26 02:29:31 -04001780 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001781 * Truncate statement
1782 *
1783 * Generates a platform-specific truncate string from the supplied data
1784 *
1785 * If the database does not support the truncate() command,
1786 * then this method maps to 'DELETE FROM table'
1787 *
1788 * @param string the table name
1789 * @return string
1790 */
1791 protected function _truncate($table)
1792 {
1793 return 'TRUNCATE '.$table;
1794 }
1795
1796 // --------------------------------------------------------------------
1797
1798 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001799 * Get DELETE query string
1800 *
1801 * Compiles a delete query string and returns the sql
1802 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001803 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001804 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001805 * @return string
1806 */
1807 public function get_compiled_delete($table = '', $reset = TRUE)
1808 {
1809 $this->return_delete_sql = TRUE;
1810 $sql = $this->delete($table, '', NULL, $reset);
1811 $this->return_delete_sql = FALSE;
1812 return $sql;
1813 }
WanWizard7219c072011-12-28 14:09:05 +01001814
Derek Allard2067d1a2008-11-13 22:59:24 +00001815 // --------------------------------------------------------------------
1816
1817 /**
1818 * Delete
1819 *
1820 * Compiles a delete string and runs the query
1821 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001822 * @param mixed the table(s) to delete from. String or array
1823 * @param mixed the where clause
1824 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001825 * @param bool
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001826 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001828 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001829 {
1830 // Combine any cached components with the current statements
1831 $this->_merge_cache();
1832
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001833 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001834 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001835 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001836 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001837 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001838 }
1839
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001840 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001841 }
1842 elseif (is_array($table))
1843 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001844 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001845 {
Andrey Andreev13f50542012-10-12 12:31:02 +03001846 $this->delete($single_table, $where, $limit, $reset_data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001847 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001848 return;
1849 }
1850 else
1851 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001852 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001853 }
1854
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001855 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001856 {
1857 $this->where($where);
1858 }
1859
Andrey Andreev650b4c02012-06-11 12:07:15 +03001860 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001861 {
1862 $this->limit($limit);
1863 }
1864
Andrey Andreev94611df2012-07-19 12:29:54 +03001865 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001866 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001867 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001868 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001869
Andrey Andreevb0478652012-07-18 15:34:46 +03001870 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001871 if ($reset_data)
1872 {
1873 $this->_reset_write();
1874 }
WanWizard7219c072011-12-28 14:09:05 +01001875
Andrey Andreev24276a32012-01-08 02:44:38 +02001876 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001877 }
WanWizard7219c072011-12-28 14:09:05 +01001878
Derek Allard2067d1a2008-11-13 22:59:24 +00001879 // --------------------------------------------------------------------
1880
1881 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001882 * Delete statement
1883 *
1884 * Generates a platform-specific delete string from the supplied data
1885 *
1886 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03001887 * @return string
1888 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001889 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03001890 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001891 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001892 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001893 }
1894
1895 // --------------------------------------------------------------------
1896
1897 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001898 * DB Prefix
1899 *
1900 * Prepends a database prefix if one exists in configuration
1901 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001902 * @param string the table
1903 * @return string
1904 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001905 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001906 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001907 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001908 {
1909 $this->display_error('db_table_name_required');
1910 }
1911
1912 return $this->dbprefix.$table;
1913 }
1914
1915 // --------------------------------------------------------------------
1916
1917 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001918 * Set DB Prefix
1919 *
1920 * Set's the DB Prefix to something new without needing to reconnect
1921 *
1922 * @param string the prefix
1923 * @return string
1924 */
1925 public function set_dbprefix($prefix = '')
1926 {
1927 return $this->dbprefix = $prefix;
1928 }
1929
1930 // --------------------------------------------------------------------
1931
1932 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001933 * Track Aliases
1934 *
1935 * Used to track SQL statements written with aliased tables.
1936 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001937 * @param string The table to inspect
1938 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001939 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001940 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001941 {
1942 if (is_array($table))
1943 {
1944 foreach ($table as $t)
1945 {
1946 $this->_track_aliases($t);
1947 }
1948 return;
1949 }
Barry Mienydd671972010-10-04 16:33:58 +02001950
Derek Jones37f4b9c2011-07-01 17:56:50 -05001951 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001952 // the string into discreet statements
1953 if (strpos($table, ',') !== FALSE)
1954 {
1955 return $this->_track_aliases(explode(',', $table));
1956 }
Barry Mienydd671972010-10-04 16:33:58 +02001957
Derek Allard2067d1a2008-11-13 22:59:24 +00001958 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001959 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001960 {
1961 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03001962 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001963
Derek Allard2067d1a2008-11-13 22:59:24 +00001964 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001965 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001966
Derek Allard2067d1a2008-11-13 22:59:24 +00001967 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001968 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001969 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001970 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001971 }
1972 }
1973 }
WanWizard7219c072011-12-28 14:09:05 +01001974
Derek Allard2067d1a2008-11-13 22:59:24 +00001975 // --------------------------------------------------------------------
1976
1977 /**
1978 * Compile the SELECT statement
1979 *
1980 * Generates a query string based on which functions were used.
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001981 * Should not be called directly.
Derek Allard2067d1a2008-11-13 22:59:24 +00001982 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001983 * @param bool $select_override = FALSE
Derek Allard2067d1a2008-11-13 22:59:24 +00001984 * @return string
1985 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001986 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001987 {
1988 // Combine any cached components with the current statements
1989 $this->_merge_cache();
1990
Derek Allard2067d1a2008-11-13 22:59:24 +00001991 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001992 if ($select_override !== FALSE)
1993 {
1994 $sql = $select_override;
1995 }
1996 else
1997 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001998 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001999
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002000 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002001 {
Barry Mienydd671972010-10-04 16:33:58 +02002002 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002003 }
2004 else
Barry Mienydd671972010-10-04 16:33:58 +02002005 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002006 // Cycle through the "select" portion of the query and prep each column name.
2007 // The reason we protect identifiers here rather then in the select() function
2008 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002009 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002010 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002011 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002012 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002013 }
Barry Mienydd671972010-10-04 16:33:58 +02002014
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002015 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002016 }
2017 }
2018
Derek Allard2067d1a2008-11-13 22:59:24 +00002019 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002020 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002021 {
Andrey Andreeve78f8152012-10-09 11:38:38 +03002022 $sql .= "\nFROM ".$this->_from_tables();
Derek Allard2067d1a2008-11-13 22:59:24 +00002023 }
2024
Derek Allard2067d1a2008-11-13 22:59:24 +00002025 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002026 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002027 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002028 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002029 }
2030
Andrey Andreev2d486232012-07-19 14:46:51 +03002031 $sql .= $this->_compile_wh('qb_where')
2032 .$this->_compile_group_by()
2033 .$this->_compile_wh('qb_having')
2034 .$this->_compile_order_by(); // ORDER BY
Andrey Andreevb0478652012-07-18 15:34:46 +03002035
Andrey Andreevd40459d2012-07-18 16:46:39 +03002036 // LIMIT
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002037 if ($this->qb_limit)
Derek Allard2067d1a2008-11-13 22:59:24 +00002038 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002039 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002040 }
2041
2042 return $sql;
2043 }
2044
2045 // --------------------------------------------------------------------
2046
2047 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002048 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002049 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002050 * Escapes identifiers in WHERE and HAVING statements at execution time.
2051 *
Andrey Andreevb0478652012-07-18 15:34:46 +03002052 * Required so that aliases are tracked properly, regardless of wether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002053 * where(), or_where(), having(), or_having are called prior to from(),
2054 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002055 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002056 * @param string 'qb_where' or 'qb_having'
2057 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002058 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002059 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002060 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002061 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002062 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002063 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002064 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002065 if ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002066 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002067 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002068 continue;
2069 }
2070
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002071 // Split multiple conditions
2072 $conditions = preg_split(
2073 '/(\s*AND\s+|\s*OR\s+)/i',
2074 $this->{$qb_key}[$i]['condition'],
2075 -1,
2076 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
2077 );
2078
2079 for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002080 {
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002081 if (($op = $this->_get_operator($conditions[$ci])) === FALSE
Andrey Andreeve4742582012-10-25 13:25:13 +03002082 OR ! preg_match('/^(\(?)(.*)('.preg_quote($op, '/').')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches))
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002083 {
2084 continue;
2085 }
2086
2087 // $matches = array(
2088 // 0 => '(test <= foo)', /* the whole thing */
2089 // 1 => '(', /* optional */
2090 // 2 => 'test', /* the field name */
2091 // 3 => ' <= ', /* $op */
2092 // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2093 // 5 => ')' /* optional */
2094 // );
Andrey Andreev082aa402012-10-22 19:41:55 +03002095
2096 if ( ! empty($matches[4]))
2097 {
2098 $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4]));
2099 $matches[4] = ' '.$matches[4];
2100 }
2101
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002102 $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2]))
2103 .' '.trim($matches[3]).$matches[4].$matches[5];
Andrey Andreev6e704752012-07-18 00:46:33 +03002104 }
2105
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002106 $this->{$qb_key}[$i] = implode('', $conditions);
Andrey Andreev6e704752012-07-18 00:46:33 +03002107 }
2108
Andrey Andreev9d3aa1b2012-10-12 12:14:09 +03002109 return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ")
2110 .implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002111 }
2112
Andrey Andreevb0478652012-07-18 15:34:46 +03002113 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002114 }
2115
2116 // --------------------------------------------------------------------
2117
2118 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002119 * Compile GROUP BY
2120 *
2121 * Escapes identifiers in GROUP BY statements at execution time.
2122 *
2123 * Required so that aliases are tracked properly, regardless of wether
2124 * group_by() is called prior to from(), join() and dbprefix is added
2125 * only if needed.
2126 *
2127 * @return string SQL statement
2128 */
2129 protected function _compile_group_by()
2130 {
2131 if (count($this->qb_groupby) > 0)
2132 {
Andrey Andreev96feb582012-07-19 13:12:34 +03002133 for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
2134 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002135 $this->qb_groupby[$i] = ($this->qb_groupby[$i]['escape'] === FALSE OR $this->_is_literal($this->qb_groupby[$i]['field']))
Andrey Andreev96feb582012-07-19 13:12:34 +03002136 ? $this->qb_groupby[$i]['field']
Andrey Andreev13f50542012-10-12 12:31:02 +03002137 : $this->protect_identifiers($this->qb_groupby[$i]['field']);
Andrey Andreev96feb582012-07-19 13:12:34 +03002138 }
2139
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002140 return "\nGROUP BY ".implode(', ', $this->qb_groupby);
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002141 }
2142
2143 return '';
2144 }
2145
2146 // --------------------------------------------------------------------
2147
2148 /**
Andrey Andreev2d486232012-07-19 14:46:51 +03002149 * Compile ORDER BY
2150 *
2151 * Escapes identifiers in ORDER BY statements at execution time.
2152 *
2153 * Required so that aliases are tracked properly, regardless of wether
2154 * order_by() is called prior to from(), join() and dbprefix is added
2155 * only if needed.
2156 *
2157 * @return string SQL statement
2158 */
2159 protected function _compile_order_by()
2160 {
Andrey Andreeva53ea842012-10-23 12:44:09 +03002161 if (is_array($this->qb_orderby) && count($this->qb_orderby) > 0)
Andrey Andreev2d486232012-07-19 14:46:51 +03002162 {
Andrey Andreev2d486232012-07-19 14:46:51 +03002163 for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++)
2164 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002165 if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field']))
Andrey Andreev2d486232012-07-19 14:46:51 +03002166 {
Andrey Andreevfc043b32012-10-12 14:46:14 +03002167 $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']);
Andrey Andreev2d486232012-07-19 14:46:51 +03002168 }
2169
2170 $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction'];
2171 }
2172
Andrey Andreeva53ea842012-10-23 12:44:09 +03002173 return $this->qb_orderby = "\nORDER BY ".implode(', ', $this->qb_orderby);
2174 }
2175 elseif (is_string($this->qb_orderby))
2176 {
2177 return $this->qb_orderby;
Andrey Andreev2d486232012-07-19 14:46:51 +03002178 }
2179
2180 return '';
2181 }
2182
2183 // --------------------------------------------------------------------
2184
2185 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002186 * Object to Array
2187 *
2188 * Takes an object as input and converts the class variables to array key/vals
2189 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002190 * @param object
2191 * @return array
2192 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002193 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002194 {
2195 if ( ! is_object($object))
2196 {
2197 return $object;
2198 }
Barry Mienydd671972010-10-04 16:33:58 +02002199
Derek Allard2067d1a2008-11-13 22:59:24 +00002200 $array = array();
2201 foreach (get_object_vars($object) as $key => $val)
2202 {
2203 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002204 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002205 {
2206 $array[$key] = $val;
2207 }
2208 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002209
2210 return $array;
2211 }
Barry Mienydd671972010-10-04 16:33:58 +02002212
Derek Jonesd10e8962010-03-02 17:10:36 -06002213 // --------------------------------------------------------------------
2214
2215 /**
2216 * Object to Array
2217 *
2218 * Takes an object as input and converts the class variables to array key/vals
2219 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002220 * @param object
2221 * @return array
2222 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002223 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002224 {
2225 if ( ! is_object($object))
2226 {
2227 return $object;
2228 }
Barry Mienydd671972010-10-04 16:33:58 +02002229
Derek Jonesd10e8962010-03-02 17:10:36 -06002230 $array = array();
2231 $out = get_object_vars($object);
2232 $fields = array_keys($out);
2233
2234 foreach ($fields as $val)
2235 {
2236 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002237 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002238 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002239 $i = 0;
2240 foreach ($out[$val] as $data)
2241 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002242 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002243 }
2244 }
2245 }
2246
Derek Allard2067d1a2008-11-13 22:59:24 +00002247 return $array;
2248 }
Barry Mienydd671972010-10-04 16:33:58 +02002249
Derek Allard2067d1a2008-11-13 22:59:24 +00002250 // --------------------------------------------------------------------
2251
2252 /**
2253 * Start Cache
2254 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002255 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002256 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002257 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002258 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002259 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002260 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002261 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002262 }
2263
2264 // --------------------------------------------------------------------
2265
2266 /**
2267 * Stop Cache
2268 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002269 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002270 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002271 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002272 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002273 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002274 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002275 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002276 }
2277
2278 // --------------------------------------------------------------------
2279
2280 /**
2281 * Flush Cache
2282 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002283 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002284 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002285 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002286 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002287 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002288 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002289 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002290 'qb_cache_select' => array(),
2291 'qb_cache_from' => array(),
2292 'qb_cache_join' => array(),
2293 'qb_cache_where' => array(),
2294 'qb_cache_like' => array(),
2295 'qb_cache_groupby' => array(),
2296 'qb_cache_having' => array(),
2297 'qb_cache_orderby' => array(),
2298 'qb_cache_set' => array(),
2299 'qb_cache_exists' => array(),
2300 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002301 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002302 }
2303
2304 // --------------------------------------------------------------------
2305
2306 /**
2307 * Merge Cache
2308 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002309 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002310 * locally called ones.
2311 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002312 * @return void
2313 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002314 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002315 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002316 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002317 {
2318 return;
2319 }
2320
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002321 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002322 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002323 $qb_variable = 'qb_'.$val;
2324 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002325
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002326 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002327 {
2328 continue;
2329 }
2330
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002331 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002332 }
2333
2334 // If we are "protecting identifiers" we need to examine the "from"
2335 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002336 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002337 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002338 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002339 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002340
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002341 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002342 }
WanWizard7219c072011-12-28 14:09:05 +01002343
Kyle Farris0c147b32011-08-26 02:29:31 -04002344 // --------------------------------------------------------------------
2345
2346 /**
Andrey Andreev082aa402012-10-22 19:41:55 +03002347 * Is literal
2348 *
2349 * Determines if a string represents a literal value or a field name
2350 *
2351 * @param string
2352 * @return bool
2353 */
2354 protected function _is_literal($str)
2355 {
2356 $str = trim($str);
2357
2358 if (empty($str))
2359 {
2360 return TRUE;
2361 }
2362
2363 static $_str;
2364
2365 if (empty($_str))
2366 {
2367 $_str = ($this->_escape_char !== '"')
2368 ? array('"', "'") : array("'");
2369 }
2370
2371 return (ctype_digit($str) OR in_array($str[0], $_str, TRUE));
2372 }
2373
2374 // --------------------------------------------------------------------
2375
2376 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002377 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002378 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002379 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002380 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002381 * @return void
2382 */
2383 public function reset_query()
2384 {
2385 $this->_reset_select();
2386 $this->_reset_write();
2387 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002388
2389 // --------------------------------------------------------------------
2390
2391 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002392 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002393 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002394 * @param array An array of fields to reset
2395 * @return void
2396 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002397 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002398 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002399 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002400 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002401 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002402 {
2403 $this->$item = $default_value;
2404 }
2405 }
2406 }
2407
2408 // --------------------------------------------------------------------
2409
2410 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002411 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002412 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002413 * @return void
2414 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002415 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002416 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002417 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002418 'qb_select' => array(),
2419 'qb_from' => array(),
2420 'qb_join' => array(),
2421 'qb_where' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002422 'qb_groupby' => array(),
2423 'qb_having' => array(),
2424 'qb_orderby' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002425 'qb_aliased_tables' => array(),
2426 'qb_no_escape' => array(),
2427 'qb_distinct' => FALSE,
2428 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002429 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002430 )
2431 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002432 }
Barry Mienydd671972010-10-04 16:33:58 +02002433
Derek Allard2067d1a2008-11-13 22:59:24 +00002434 // --------------------------------------------------------------------
2435
2436 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002437 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002438 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002439 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002440 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002441 * @return void
2442 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002443 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002444 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002445 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002446 'qb_set' => array(),
2447 'qb_from' => array(),
2448 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002449 'qb_orderby' => array(),
2450 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002451 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002452 )
2453 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002454 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002455
Derek Allard2067d1a2008-11-13 22:59:24 +00002456}
2457
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002458/* End of file DB_query_builder.php */
Andrey Andreev58803fb2012-06-24 00:45:37 +03002459/* Location: ./system/database/DB_query_builder.php */