blob: 5ea9643fe859fe7319afd360b7b6d4b5c3f8bbc5 [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 Andreev032e7ea2012-03-06 19:48:35 +02001165 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001167
Purwandi1d160e72012-01-09 16:33:28 +07001168 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001170 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 }
1172
Purwandi1d160e72012-01-09 16:33:28 +07001173 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001174 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001176
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 // --------------------------------------------------------------------
1178
1179 /**
1180 * Get_Where
1181 *
1182 * Allows the where clause, limit and offset to be added directly
1183 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001184 * @param string $table = ''
1185 * @param string $where = NULL
1186 * @param int $limit = NULL
1187 * @param int $offset = NULL
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 * @return object
1189 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001190 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001192 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 {
1194 $this->from($table);
1195 }
1196
1197 if ( ! is_null($where))
1198 {
1199 $this->where($where);
1200 }
Barry Mienydd671972010-10-04 16:33:58 +02001201
Andrey Andreev650b4c02012-06-11 12:07:15 +03001202 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 {
1204 $this->limit($limit, $offset);
1205 }
Barry Mienydd671972010-10-04 16:33:58 +02001206
Andrey Andreev24276a32012-01-08 02:44:38 +02001207 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 $this->_reset_select();
1209 return $result;
1210 }
1211
1212 // --------------------------------------------------------------------
1213
1214 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001215 * Insert_Batch
1216 *
1217 * Compiles batch insert strings and runs the queries
1218 *
Andrey Andreev9f808b02012-10-24 17:38:48 +03001219 * @param string $table = '' table to insert into
1220 * @param array $set an associative array of insert values
1221 * @return int number of rows inserted or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001222 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001223 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001224 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001225 if ( ! is_null($set))
1226 {
1227 $this->set_insert_batch($set);
1228 }
Barry Mienydd671972010-10-04 16:33:58 +02001229
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001230 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001231 {
Andrey Andreev9f808b02012-10-24 17:38:48 +03001232 // No valid data array. Folds in cases where keys and values did not match up
1233 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001234 }
1235
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001236 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001237 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001238 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001239 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001240 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001241 }
Barry Mienydd671972010-10-04 16:33:58 +02001242
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001243 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001244 }
1245
1246 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001247 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001248 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001249 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001250 $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 +03001251 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001252 }
Barry Mienydd671972010-10-04 16:33:58 +02001253
Derek Jonesd10e8962010-03-02 17:10:36 -06001254 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001255 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001256 }
1257
1258 // --------------------------------------------------------------------
1259
1260 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001261 * Insert_batch statement
1262 *
1263 * Generates a platform-specific insert string from the supplied data.
1264 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001265 * @param string the table name
1266 * @param array the insert keys
1267 * @param array the insert values
1268 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001269 */
1270 protected function _insert_batch($table, $keys, $values)
1271 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001272 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001273 }
1274
1275 // --------------------------------------------------------------------
1276
1277 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001278 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001279 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001280 * @param mixed
1281 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001282 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001283 * @return object
1284 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001285 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001286 {
1287 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001288
Derek Jonesd10e8962010-03-02 17:10:36 -06001289 if ( ! is_array($key))
1290 {
1291 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001292 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001293
Andrey Andreevfe642da2012-06-16 03:47:33 +03001294 is_bool($escape) OR $escape = $this->_protect_identifiers;
1295
Iban Eguia3c0a4522012-04-15 13:30:44 +02001296 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001297 sort($keys);
1298
1299 foreach ($key as $row)
1300 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001301 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001302 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1303 {
1304 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001305 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001306 return;
1307 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001308
Barry Mienydd671972010-10-04 16:33:58 +02001309 ksort($row); // puts $row in the same order as our keys
1310
Andrey Andreev650b4c02012-06-11 12:07:15 +03001311 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001312 {
1313 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001314 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001315 {
Barry Mienydd671972010-10-04 16:33:58 +02001316 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001317 }
1318
Andrey Andreev650b4c02012-06-11 12:07:15 +03001319 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001320 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001321
1322 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001323 }
1324
1325 foreach ($keys as $k)
1326 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001327 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001328 }
Barry Mienydd671972010-10-04 16:33:58 +02001329
Derek Jonesd10e8962010-03-02 17:10:36 -06001330 return $this;
1331 }
WanWizard7219c072011-12-28 14:09:05 +01001332
Kyle Farris0c147b32011-08-26 02:29:31 -04001333 // --------------------------------------------------------------------
1334
1335 /**
1336 * Get INSERT query string
1337 *
1338 * Compiles an insert query and returns the sql
1339 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001340 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001341 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001342 * @return string
1343 */
1344 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001345 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001346 if ($this->_validate_insert($table) === FALSE)
1347 {
1348 return FALSE;
1349 }
WanWizard7219c072011-12-28 14:09:05 +01001350
Kyle Farris76116012011-08-31 11:17:48 -04001351 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001352 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001353 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001354 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001355 array_keys($this->qb_set),
1356 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001357 );
WanWizard7219c072011-12-28 14:09:05 +01001358
Kyle Farris0c147b32011-08-26 02:29:31 -04001359 if ($reset === TRUE)
1360 {
1361 $this->_reset_write();
1362 }
WanWizard7219c072011-12-28 14:09:05 +01001363
Kyle Farris0c147b32011-08-26 02:29:31 -04001364 return $sql;
1365 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001366
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 // --------------------------------------------------------------------
1368
1369 /**
1370 * Insert
1371 *
1372 * Compiles an insert string and runs the query
1373 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001374 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 * @param array an associative array of insert values
1376 * @return object
1377 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001378 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001379 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001380 if ( ! is_null($set))
1381 {
1382 $this->set($set);
1383 }
WanWizard7219c072011-12-28 14:09:05 +01001384
Kyle Farris0c147b32011-08-26 02:29:31 -04001385 if ($this->_validate_insert($table) === FALSE)
1386 {
1387 return FALSE;
1388 }
WanWizard7219c072011-12-28 14:09:05 +01001389
Kyle Farris76116012011-08-31 11:17:48 -04001390 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001391 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001392 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001393 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001394 array_keys($this->qb_set),
1395 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001396 );
Barry Mienydd671972010-10-04 16:33:58 +02001397
Kyle Farris0c147b32011-08-26 02:29:31 -04001398 $this->_reset_write();
1399 return $this->query($sql);
1400 }
WanWizard7219c072011-12-28 14:09:05 +01001401
Kyle Farris0c147b32011-08-26 02:29:31 -04001402 // --------------------------------------------------------------------
1403
1404 /**
1405 * Validate Insert
1406 *
1407 * This method is used by both insert() and get_compiled_insert() to
1408 * validate that the there data is actually being set and that table
1409 * has been chosen to be inserted into.
1410 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001411 * @param string the table to insert data into
1412 * @return string
1413 */
WanWizard7219c072011-12-28 14:09:05 +01001414 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001415 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001416 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001418 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001419 }
1420
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001421 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001422 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001423 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001424 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001425 elseif ( ! isset($this->qb_from[0]))
1426 {
1427 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1428 }
WanWizard7219c072011-12-28 14:09:05 +01001429
Kyle Farris0c147b32011-08-26 02:29:31 -04001430 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001431 }
Barry Mienydd671972010-10-04 16:33:58 +02001432
Phil Sturgeon9789f322011-07-15 15:14:05 -06001433 // --------------------------------------------------------------------
1434
1435 /**
1436 * Replace
1437 *
1438 * Compiles an replace into string and runs the query
1439 *
1440 * @param string the table to replace data into
1441 * @param array an associative array of insert values
1442 * @return object
1443 */
1444 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001445 {
1446 if ( ! is_null($set))
1447 {
1448 $this->set($set);
1449 }
Barry Mienydd671972010-10-04 16:33:58 +02001450
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001451 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001452 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001453 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001454 }
1455
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001456 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001457 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001458 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001459 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001460 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001461 }
Barry Mienydd671972010-10-04 16:33:58 +02001462
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001463 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001464 }
1465
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001466 $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 +00001467
Derek Jonesd10e8962010-03-02 17:10:36 -06001468 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001469 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001470 }
WanWizard7219c072011-12-28 14:09:05 +01001471
Kyle Farris0c147b32011-08-26 02:29:31 -04001472 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001473
Kyle Farris0c147b32011-08-26 02:29:31 -04001474 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001475 * Replace statement
1476 *
1477 * Generates a platform-specific replace string from the supplied data
1478 *
1479 * @param string the table name
1480 * @param array the insert keys
1481 * @param array the insert values
1482 * @return string
1483 */
1484 protected function _replace($table, $keys, $values)
1485 {
1486 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1487 }
1488
1489 // --------------------------------------------------------------------
1490
1491 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001492 * FROM tables
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001493 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001494 * Groups tables in FROM clauses if needed, so there is no confusion
1495 * about operator precedence.
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001496 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001497 * Note: This is only used (and overriden) by MySQL and CUBRID.
1498 *
1499 * @return string
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001500 */
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001501 protected function _from_tables()
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001502 {
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001503 return implode(', ', $this->qb_from);
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001504 }
1505
1506 // --------------------------------------------------------------------
1507
1508 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001509 * Get UPDATE query string
1510 *
1511 * Compiles an update query and returns the sql
1512 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001513 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001514 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001515 * @return string
1516 */
1517 public function get_compiled_update($table = '', $reset = TRUE)
1518 {
1519 // Combine any cached components with the current statements
1520 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001521
Kyle Farris0c147b32011-08-26 02:29:31 -04001522 if ($this->_validate_update($table) === FALSE)
1523 {
1524 return FALSE;
1525 }
WanWizard7219c072011-12-28 14:09:05 +01001526
Andrey Andreevb0478652012-07-18 15:34:46 +03001527 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001528
Kyle Farris0c147b32011-08-26 02:29:31 -04001529 if ($reset === TRUE)
1530 {
1531 $this->_reset_write();
1532 }
WanWizard7219c072011-12-28 14:09:05 +01001533
Kyle Farris0c147b32011-08-26 02:29:31 -04001534 return $sql;
1535 }
WanWizard7219c072011-12-28 14:09:05 +01001536
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 // --------------------------------------------------------------------
1538
1539 /**
1540 * Update
1541 *
1542 * Compiles an update string and runs the query
1543 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001544 * @param string $table = ''
1545 * @param array $set = NULL an associative array of update values
1546 * @param mixed $where = NULL
1547 * @param int $limit = NULL
Derek Allard2067d1a2008-11-13 22:59:24 +00001548 * @return object
1549 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001550 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001551 {
1552 // Combine any cached components with the current statements
1553 $this->_merge_cache();
1554
1555 if ( ! is_null($set))
1556 {
1557 $this->set($set);
1558 }
Barry Mienydd671972010-10-04 16:33:58 +02001559
Kyle Farris0c147b32011-08-26 02:29:31 -04001560 if ($this->_validate_update($table) === FALSE)
1561 {
1562 return FALSE;
1563 }
1564
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001565 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001566 {
1567 $this->where($where);
1568 }
1569
Andrey Andreev650b4c02012-06-11 12:07:15 +03001570 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001571 {
1572 $this->limit($limit);
1573 }
1574
Andrey Andreevb0478652012-07-18 15:34:46 +03001575 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
Jamie Rumbelow3b1355c2012-03-06 21:27:46 +00001576
Kyle Farris0c147b32011-08-26 02:29:31 -04001577 $this->_reset_write();
1578 return $this->query($sql);
1579 }
WanWizard7219c072011-12-28 14:09:05 +01001580
Kyle Farris0c147b32011-08-26 02:29:31 -04001581 // --------------------------------------------------------------------
1582
1583 /**
1584 * Validate Update
1585 *
1586 * This method is used by both update() and get_compiled_update() to
1587 * validate that data is actually being set and that a table has been
1588 * chosen to be update.
1589 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001590 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001591 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001592 */
1593 protected function _validate_update($table = '')
1594 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001595 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001596 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001597 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001598 }
1599
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001600 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001601 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001602 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001603 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001604 elseif ( ! isset($this->qb_from[0]))
1605 {
1606 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1607 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001608
1609 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001610 }
WanWizard7219c072011-12-28 14:09:05 +01001611
Derek Jonesd10e8962010-03-02 17:10:36 -06001612 // --------------------------------------------------------------------
1613
1614 /**
1615 * Update_Batch
1616 *
1617 * Compiles an update string and runs the query
1618 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001619 * @param string the table to retrieve the results from
1620 * @param array an associative array of update values
1621 * @param string the where key
Andrey Andreev9f808b02012-10-24 17:38:48 +03001622 * @return int number of rows affected or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001623 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001624 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001625 {
1626 // Combine any cached components with the current statements
1627 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001628
Derek Jonesd10e8962010-03-02 17:10:36 -06001629 if (is_null($index))
1630 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001631 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001632 }
1633
1634 if ( ! is_null($set))
1635 {
1636 $this->set_update_batch($set, $index);
1637 }
1638
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001639 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001640 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001641 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001642 }
1643
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001644 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001645 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001646 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001647 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001648 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001649 }
Barry Mienydd671972010-10-04 16:33:58 +02001650
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001651 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001652 }
Barry Mienydd671972010-10-04 16:33:58 +02001653
Derek Jonesd10e8962010-03-02 17:10:36 -06001654 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001655 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001656 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001657 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001658 $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 +03001659 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001660 }
Barry Mienydd671972010-10-04 16:33:58 +02001661
Derek Jonesd10e8962010-03-02 17:10:36 -06001662 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001663 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001664 }
1665
1666 // --------------------------------------------------------------------
1667
1668 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001669 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001670 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001671 * @param array
1672 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001673 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001674 * @return object
1675 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001676 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001677 {
1678 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001679
Derek Jonesd10e8962010-03-02 17:10:36 -06001680 if ( ! is_array($key))
1681 {
1682 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001683 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001684
Andrey Andreevfe642da2012-06-16 03:47:33 +03001685 is_bool($escape) OR $escape = $this->_protect_identifiers;
1686
Derek Jonesd10e8962010-03-02 17:10:36 -06001687 foreach ($key as $k => $v)
1688 {
1689 $index_set = FALSE;
1690 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001691 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001692 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001693 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001694 {
1695 $index_set = TRUE;
1696 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001697
Andrey Andreevfe642da2012-06-16 03:47:33 +03001698 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001699 }
1700
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001701 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001702 {
1703 return $this->display_error('db_batch_missing_index');
1704 }
1705
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001706 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001707 }
Barry Mienydd671972010-10-04 16:33:58 +02001708
Derek Jonesd10e8962010-03-02 17:10:36 -06001709 return $this;
1710 }
1711
Derek Allard2067d1a2008-11-13 22:59:24 +00001712 // --------------------------------------------------------------------
1713
1714 /**
1715 * Empty Table
1716 *
1717 * Compiles a delete string and runs "DELETE FROM table"
1718 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001719 * @param string the table to empty
1720 * @return object
1721 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001722 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001723 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001724 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001725 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001726 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001727 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001728 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001729 }
1730
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001731 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001732 }
1733 else
1734 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001735 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001736 }
1737
1738 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001739 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001740 return $this->query($sql);
1741 }
1742
1743 // --------------------------------------------------------------------
1744
1745 /**
1746 * Truncate
1747 *
1748 * Compiles a truncate string and runs the query
1749 * If the database does not support the truncate() command
1750 * This function maps to "DELETE FROM table"
1751 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001752 * @param string the table to truncate
1753 * @return object
1754 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001755 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001756 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001757 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001758 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001759 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001760 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001761 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001762 }
1763
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001764 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001765 }
1766 else
1767 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001768 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001769 }
1770
1771 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001773 return $this->query($sql);
1774 }
WanWizard7219c072011-12-28 14:09:05 +01001775
Kyle Farris0c147b32011-08-26 02:29:31 -04001776 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001777
Kyle Farris0c147b32011-08-26 02:29:31 -04001778 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001779 * Truncate statement
1780 *
1781 * Generates a platform-specific truncate string from the supplied data
1782 *
1783 * If the database does not support the truncate() command,
1784 * then this method maps to 'DELETE FROM table'
1785 *
1786 * @param string the table name
1787 * @return string
1788 */
1789 protected function _truncate($table)
1790 {
1791 return 'TRUNCATE '.$table;
1792 }
1793
1794 // --------------------------------------------------------------------
1795
1796 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001797 * Get DELETE query string
1798 *
1799 * Compiles a delete query string and returns the sql
1800 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001801 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001802 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001803 * @return string
1804 */
1805 public function get_compiled_delete($table = '', $reset = TRUE)
1806 {
1807 $this->return_delete_sql = TRUE;
1808 $sql = $this->delete($table, '', NULL, $reset);
1809 $this->return_delete_sql = FALSE;
1810 return $sql;
1811 }
WanWizard7219c072011-12-28 14:09:05 +01001812
Derek Allard2067d1a2008-11-13 22:59:24 +00001813 // --------------------------------------------------------------------
1814
1815 /**
1816 * Delete
1817 *
1818 * Compiles a delete string and runs the query
1819 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 * @param mixed the table(s) to delete from. String or array
1821 * @param mixed the where clause
1822 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001823 * @param bool
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001824 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00001825 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001826 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 {
1828 // Combine any cached components with the current statements
1829 $this->_merge_cache();
1830
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001831 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001832 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001833 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001834 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001835 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001836 }
1837
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001838 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001839 }
1840 elseif (is_array($table))
1841 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001842 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001843 {
Andrey Andreev13f50542012-10-12 12:31:02 +03001844 $this->delete($single_table, $where, $limit, $reset_data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001845 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001846 return;
1847 }
1848 else
1849 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001850 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001851 }
1852
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001853 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001854 {
1855 $this->where($where);
1856 }
1857
Andrey Andreev650b4c02012-06-11 12:07:15 +03001858 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001859 {
1860 $this->limit($limit);
1861 }
1862
Andrey Andreev94611df2012-07-19 12:29:54 +03001863 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001864 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001865 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001866 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001867
Andrey Andreevb0478652012-07-18 15:34:46 +03001868 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001869 if ($reset_data)
1870 {
1871 $this->_reset_write();
1872 }
WanWizard7219c072011-12-28 14:09:05 +01001873
Andrey Andreev24276a32012-01-08 02:44:38 +02001874 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001875 }
WanWizard7219c072011-12-28 14:09:05 +01001876
Derek Allard2067d1a2008-11-13 22:59:24 +00001877 // --------------------------------------------------------------------
1878
1879 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001880 * Delete statement
1881 *
1882 * Generates a platform-specific delete string from the supplied data
1883 *
1884 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03001885 * @return string
1886 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001887 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03001888 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001889 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001890 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001891 }
1892
1893 // --------------------------------------------------------------------
1894
1895 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001896 * DB Prefix
1897 *
1898 * Prepends a database prefix if one exists in configuration
1899 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001900 * @param string the table
1901 * @return string
1902 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001903 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001904 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001905 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001906 {
1907 $this->display_error('db_table_name_required');
1908 }
1909
1910 return $this->dbprefix.$table;
1911 }
1912
1913 // --------------------------------------------------------------------
1914
1915 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001916 * Set DB Prefix
1917 *
1918 * Set's the DB Prefix to something new without needing to reconnect
1919 *
1920 * @param string the prefix
1921 * @return string
1922 */
1923 public function set_dbprefix($prefix = '')
1924 {
1925 return $this->dbprefix = $prefix;
1926 }
1927
1928 // --------------------------------------------------------------------
1929
1930 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001931 * Track Aliases
1932 *
1933 * Used to track SQL statements written with aliased tables.
1934 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001935 * @param string The table to inspect
1936 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001937 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001938 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001939 {
1940 if (is_array($table))
1941 {
1942 foreach ($table as $t)
1943 {
1944 $this->_track_aliases($t);
1945 }
1946 return;
1947 }
Barry Mienydd671972010-10-04 16:33:58 +02001948
Derek Jones37f4b9c2011-07-01 17:56:50 -05001949 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001950 // the string into discreet statements
1951 if (strpos($table, ',') !== FALSE)
1952 {
1953 return $this->_track_aliases(explode(',', $table));
1954 }
Barry Mienydd671972010-10-04 16:33:58 +02001955
Derek Allard2067d1a2008-11-13 22:59:24 +00001956 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001957 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001958 {
1959 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03001960 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001961
Derek Allard2067d1a2008-11-13 22:59:24 +00001962 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001963 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001964
Derek Allard2067d1a2008-11-13 22:59:24 +00001965 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001966 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001967 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001968 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001969 }
1970 }
1971 }
WanWizard7219c072011-12-28 14:09:05 +01001972
Derek Allard2067d1a2008-11-13 22:59:24 +00001973 // --------------------------------------------------------------------
1974
1975 /**
1976 * Compile the SELECT statement
1977 *
1978 * Generates a query string based on which functions were used.
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001979 * Should not be called directly.
Derek Allard2067d1a2008-11-13 22:59:24 +00001980 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001981 * @param bool $select_override = FALSE
Derek Allard2067d1a2008-11-13 22:59:24 +00001982 * @return string
1983 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001984 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001985 {
1986 // Combine any cached components with the current statements
1987 $this->_merge_cache();
1988
Derek Allard2067d1a2008-11-13 22:59:24 +00001989 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001990 if ($select_override !== FALSE)
1991 {
1992 $sql = $select_override;
1993 }
1994 else
1995 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001996 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001997
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001998 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001999 {
Barry Mienydd671972010-10-04 16:33:58 +02002000 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002001 }
2002 else
Barry Mienydd671972010-10-04 16:33:58 +02002003 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002004 // Cycle through the "select" portion of the query and prep each column name.
2005 // The reason we protect identifiers here rather then in the select() function
2006 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002007 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002008 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002009 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002010 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002011 }
Barry Mienydd671972010-10-04 16:33:58 +02002012
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002013 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002014 }
2015 }
2016
Derek Allard2067d1a2008-11-13 22:59:24 +00002017 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002018 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002019 {
Andrey Andreeve78f8152012-10-09 11:38:38 +03002020 $sql .= "\nFROM ".$this->_from_tables();
Derek Allard2067d1a2008-11-13 22:59:24 +00002021 }
2022
Derek Allard2067d1a2008-11-13 22:59:24 +00002023 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002024 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002025 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002026 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002027 }
2028
Andrey Andreev2d486232012-07-19 14:46:51 +03002029 $sql .= $this->_compile_wh('qb_where')
2030 .$this->_compile_group_by()
2031 .$this->_compile_wh('qb_having')
2032 .$this->_compile_order_by(); // ORDER BY
Andrey Andreevb0478652012-07-18 15:34:46 +03002033
Andrey Andreevd40459d2012-07-18 16:46:39 +03002034 // LIMIT
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002035 if ($this->qb_limit)
Derek Allard2067d1a2008-11-13 22:59:24 +00002036 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002037 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002038 }
2039
2040 return $sql;
2041 }
2042
2043 // --------------------------------------------------------------------
2044
2045 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002046 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002047 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002048 * Escapes identifiers in WHERE and HAVING statements at execution time.
2049 *
Andrey Andreevb0478652012-07-18 15:34:46 +03002050 * Required so that aliases are tracked properly, regardless of wether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002051 * where(), or_where(), having(), or_having are called prior to from(),
2052 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002053 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002054 * @param string 'qb_where' or 'qb_having'
2055 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002056 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002057 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002058 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002059 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002060 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002061 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002062 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002063 if ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002064 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002065 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002066 continue;
2067 }
2068
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002069 // Split multiple conditions
2070 $conditions = preg_split(
2071 '/(\s*AND\s+|\s*OR\s+)/i',
2072 $this->{$qb_key}[$i]['condition'],
2073 -1,
2074 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
2075 );
2076
2077 for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002078 {
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002079 if (($op = $this->_get_operator($conditions[$ci])) === FALSE
Andrey Andreev082aa402012-10-22 19:41:55 +03002080 OR ! preg_match('/^(\(?)(.*)('.preg_quote($op).')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches))
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002081 {
2082 continue;
2083 }
2084
2085 // $matches = array(
2086 // 0 => '(test <= foo)', /* the whole thing */
2087 // 1 => '(', /* optional */
2088 // 2 => 'test', /* the field name */
2089 // 3 => ' <= ', /* $op */
2090 // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2091 // 5 => ')' /* optional */
2092 // );
Andrey Andreev082aa402012-10-22 19:41:55 +03002093
2094 if ( ! empty($matches[4]))
2095 {
2096 $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4]));
2097 $matches[4] = ' '.$matches[4];
2098 }
2099
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002100 $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2]))
2101 .' '.trim($matches[3]).$matches[4].$matches[5];
Andrey Andreev6e704752012-07-18 00:46:33 +03002102 }
2103
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002104 $this->{$qb_key}[$i] = implode('', $conditions);
Andrey Andreev6e704752012-07-18 00:46:33 +03002105 }
2106
Andrey Andreev9d3aa1b2012-10-12 12:14:09 +03002107 return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ")
2108 .implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002109 }
2110
Andrey Andreevb0478652012-07-18 15:34:46 +03002111 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002112 }
2113
2114 // --------------------------------------------------------------------
2115
2116 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002117 * Compile GROUP BY
2118 *
2119 * Escapes identifiers in GROUP BY statements at execution time.
2120 *
2121 * Required so that aliases are tracked properly, regardless of wether
2122 * group_by() is called prior to from(), join() and dbprefix is added
2123 * only if needed.
2124 *
2125 * @return string SQL statement
2126 */
2127 protected function _compile_group_by()
2128 {
2129 if (count($this->qb_groupby) > 0)
2130 {
Andrey Andreev96feb582012-07-19 13:12:34 +03002131 for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
2132 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002133 $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 +03002134 ? $this->qb_groupby[$i]['field']
Andrey Andreev13f50542012-10-12 12:31:02 +03002135 : $this->protect_identifiers($this->qb_groupby[$i]['field']);
Andrey Andreev96feb582012-07-19 13:12:34 +03002136 }
2137
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002138 return "\nGROUP BY ".implode(', ', $this->qb_groupby);
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002139 }
2140
2141 return '';
2142 }
2143
2144 // --------------------------------------------------------------------
2145
2146 /**
Andrey Andreev2d486232012-07-19 14:46:51 +03002147 * Compile ORDER BY
2148 *
2149 * Escapes identifiers in ORDER BY statements at execution time.
2150 *
2151 * Required so that aliases are tracked properly, regardless of wether
2152 * order_by() is called prior to from(), join() and dbprefix is added
2153 * only if needed.
2154 *
2155 * @return string SQL statement
2156 */
2157 protected function _compile_order_by()
2158 {
Andrey Andreeva53ea842012-10-23 12:44:09 +03002159 if (is_array($this->qb_orderby) && count($this->qb_orderby) > 0)
Andrey Andreev2d486232012-07-19 14:46:51 +03002160 {
Andrey Andreev2d486232012-07-19 14:46:51 +03002161 for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++)
2162 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002163 if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field']))
Andrey Andreev2d486232012-07-19 14:46:51 +03002164 {
Andrey Andreevfc043b32012-10-12 14:46:14 +03002165 $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']);
Andrey Andreev2d486232012-07-19 14:46:51 +03002166 }
2167
2168 $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction'];
2169 }
2170
Andrey Andreeva53ea842012-10-23 12:44:09 +03002171 return $this->qb_orderby = "\nORDER BY ".implode(', ', $this->qb_orderby);
2172 }
2173 elseif (is_string($this->qb_orderby))
2174 {
2175 return $this->qb_orderby;
Andrey Andreev2d486232012-07-19 14:46:51 +03002176 }
2177
2178 return '';
2179 }
2180
2181 // --------------------------------------------------------------------
2182
2183 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002184 * Object to Array
2185 *
2186 * Takes an object as input and converts the class variables to array key/vals
2187 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002188 * @param object
2189 * @return array
2190 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002191 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002192 {
2193 if ( ! is_object($object))
2194 {
2195 return $object;
2196 }
Barry Mienydd671972010-10-04 16:33:58 +02002197
Derek Allard2067d1a2008-11-13 22:59:24 +00002198 $array = array();
2199 foreach (get_object_vars($object) as $key => $val)
2200 {
2201 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002202 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002203 {
2204 $array[$key] = $val;
2205 }
2206 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002207
2208 return $array;
2209 }
Barry Mienydd671972010-10-04 16:33:58 +02002210
Derek Jonesd10e8962010-03-02 17:10:36 -06002211 // --------------------------------------------------------------------
2212
2213 /**
2214 * Object to Array
2215 *
2216 * Takes an object as input and converts the class variables to array key/vals
2217 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002218 * @param object
2219 * @return array
2220 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002221 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002222 {
2223 if ( ! is_object($object))
2224 {
2225 return $object;
2226 }
Barry Mienydd671972010-10-04 16:33:58 +02002227
Derek Jonesd10e8962010-03-02 17:10:36 -06002228 $array = array();
2229 $out = get_object_vars($object);
2230 $fields = array_keys($out);
2231
2232 foreach ($fields as $val)
2233 {
2234 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002235 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002236 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002237 $i = 0;
2238 foreach ($out[$val] as $data)
2239 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002240 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002241 }
2242 }
2243 }
2244
Derek Allard2067d1a2008-11-13 22:59:24 +00002245 return $array;
2246 }
Barry Mienydd671972010-10-04 16:33:58 +02002247
Derek Allard2067d1a2008-11-13 22:59:24 +00002248 // --------------------------------------------------------------------
2249
2250 /**
2251 * Start Cache
2252 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002253 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002254 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002255 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002256 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002257 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002258 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002259 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002260 }
2261
2262 // --------------------------------------------------------------------
2263
2264 /**
2265 * Stop Cache
2266 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002267 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002268 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002269 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002270 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002271 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002272 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002273 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002274 }
2275
2276 // --------------------------------------------------------------------
2277
2278 /**
2279 * Flush Cache
2280 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002281 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002282 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002283 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002284 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002285 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002286 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002287 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002288 'qb_cache_select' => array(),
2289 'qb_cache_from' => array(),
2290 'qb_cache_join' => array(),
2291 'qb_cache_where' => array(),
2292 'qb_cache_like' => array(),
2293 'qb_cache_groupby' => array(),
2294 'qb_cache_having' => array(),
2295 'qb_cache_orderby' => array(),
2296 'qb_cache_set' => array(),
2297 'qb_cache_exists' => array(),
2298 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002299 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002300 }
2301
2302 // --------------------------------------------------------------------
2303
2304 /**
2305 * Merge Cache
2306 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002307 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002308 * locally called ones.
2309 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002310 * @return void
2311 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002312 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002313 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002314 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002315 {
2316 return;
2317 }
2318
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002319 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002320 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002321 $qb_variable = 'qb_'.$val;
2322 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002323
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002324 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002325 {
2326 continue;
2327 }
2328
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002329 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002330 }
2331
2332 // If we are "protecting identifiers" we need to examine the "from"
2333 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002334 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002335 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002336 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002337 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002338
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002339 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002340 }
WanWizard7219c072011-12-28 14:09:05 +01002341
Kyle Farris0c147b32011-08-26 02:29:31 -04002342 // --------------------------------------------------------------------
2343
2344 /**
Andrey Andreev082aa402012-10-22 19:41:55 +03002345 * Is literal
2346 *
2347 * Determines if a string represents a literal value or a field name
2348 *
2349 * @param string
2350 * @return bool
2351 */
2352 protected function _is_literal($str)
2353 {
2354 $str = trim($str);
2355
2356 if (empty($str))
2357 {
2358 return TRUE;
2359 }
2360
2361 static $_str;
2362
2363 if (empty($_str))
2364 {
2365 $_str = ($this->_escape_char !== '"')
2366 ? array('"', "'") : array("'");
2367 }
2368
2369 return (ctype_digit($str) OR in_array($str[0], $_str, TRUE));
2370 }
2371
2372 // --------------------------------------------------------------------
2373
2374 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002375 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002376 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002377 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002378 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002379 * @return void
2380 */
2381 public function reset_query()
2382 {
2383 $this->_reset_select();
2384 $this->_reset_write();
2385 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002386
2387 // --------------------------------------------------------------------
2388
2389 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002390 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002391 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002392 * @param array An array of fields to reset
2393 * @return void
2394 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002395 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002396 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002397 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002398 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002399 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002400 {
2401 $this->$item = $default_value;
2402 }
2403 }
2404 }
2405
2406 // --------------------------------------------------------------------
2407
2408 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002409 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002410 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002411 * @return void
2412 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002413 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002414 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002415 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002416 'qb_select' => array(),
2417 'qb_from' => array(),
2418 'qb_join' => array(),
2419 'qb_where' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002420 'qb_groupby' => array(),
2421 'qb_having' => array(),
2422 'qb_orderby' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002423 'qb_aliased_tables' => array(),
2424 'qb_no_escape' => array(),
2425 'qb_distinct' => FALSE,
2426 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002427 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002428 )
2429 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002430 }
Barry Mienydd671972010-10-04 16:33:58 +02002431
Derek Allard2067d1a2008-11-13 22:59:24 +00002432 // --------------------------------------------------------------------
2433
2434 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002435 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002436 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002437 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002438 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002439 * @return void
2440 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002441 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002442 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002443 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002444 'qb_set' => array(),
2445 'qb_from' => array(),
2446 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002447 'qb_orderby' => array(),
2448 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002449 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002450 )
2451 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002452 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002453
Derek Allard2067d1a2008-11-13 22:59:24 +00002454}
2455
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002456/* End of file DB_query_builder.php */
Andrey Andreev58803fb2012-06-24 00:45:37 +03002457/* Location: ./system/database/DB_query_builder.php */