blob: 55b97bb3f847e7c4574df81f63be84076af0c9e6 [file] [log] [blame]
Andrey Andreev24276a32012-01-08 02:44:38 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
WanWizard7219c072011-12-28 14:09:05 +01008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
WanWizard7219c072011-12-28 14:09:05 +010010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
Jamie Rumbelow7efad202012-02-19 12:37:00 +000029 * Query Builder Class
Derek Allard2067d1a2008-11-13 22:59:24 +000030 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +000031 * This is the platform-independent base Query Builder implementation class.
Derek Allard2067d1a2008-11-13 22:59:24 +000032 *
33 * @package CodeIgniter
34 * @subpackage Drivers
35 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/database/
38 */
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +010039
40abstract class CI_DB_query_builder extends CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000041
WanWizard7219c072011-12-28 14:09:05 +010042 protected $return_delete_sql = FALSE;
43 protected $reset_delete_data = FALSE;
44
Jamie Rumbelow7efad202012-02-19 12:37:00 +000045 protected $qb_select = array();
46 protected $qb_distinct = FALSE;
47 protected $qb_from = array();
48 protected $qb_join = array();
49 protected $qb_where = array();
Jamie Rumbelow7efad202012-02-19 12:37:00 +000050 protected $qb_groupby = array();
51 protected $qb_having = array();
52 protected $qb_keys = array();
53 protected $qb_limit = FALSE;
54 protected $qb_offset = FALSE;
Jamie Rumbelow7efad202012-02-19 12:37:00 +000055 protected $qb_orderby = array();
56 protected $qb_set = array();
Jamie Rumbelow7efad202012-02-19 12:37:00 +000057 protected $qb_aliased_tables = array();
58 protected $qb_store_array = array();
59 protected $qb_where_group_started = FALSE;
60 protected $qb_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +020061
Jamie Rumbelow7efad202012-02-19 12:37:00 +000062 // Query Builder Caching variables
63 protected $qb_caching = FALSE;
64 protected $qb_cache_exists = array();
65 protected $qb_cache_select = array();
66 protected $qb_cache_from = array();
67 protected $qb_cache_join = array();
68 protected $qb_cache_where = array();
69 protected $qb_cache_like = array();
70 protected $qb_cache_groupby = array();
71 protected $qb_cache_having = array();
72 protected $qb_cache_orderby = array();
73 protected $qb_cache_set = array();
WanWizard7219c072011-12-28 14:09:05 +010074
Jamie Rumbelow7efad202012-02-19 12:37:00 +000075 protected $qb_no_escape = array();
76 protected $qb_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000077
78 /**
79 * Select
80 *
81 * Generates the SELECT portion of the query
82 *
Derek Allard2067d1a2008-11-13 22:59:24 +000083 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +030084 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +000085 * @return object
86 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060087 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000088 {
Derek Allard2067d1a2008-11-13 22:59:24 +000089 if (is_string($select))
90 {
91 $select = explode(',', $select);
92 }
93
Andrey Andreev42870232012-06-12 01:30:20 +030094 // If the escape value was not set will will base it on the global setting
95 is_bool($escape) OR $escape = $this->_protect_identifiers;
96
Derek Allard2067d1a2008-11-13 22:59:24 +000097 foreach ($select as $val)
98 {
99 $val = trim($val);
100
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100101 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000103 $this->qb_select[] = $val;
104 $this->qb_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000105
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000106 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000108 $this->qb_cache_select[] = $val;
109 $this->qb_cache_exists[] = 'select';
110 $this->qb_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
112 }
113 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 return $this;
116 }
117
118 // --------------------------------------------------------------------
119
120 /**
121 * Select Max
122 *
123 * Generates a SELECT MAX(field) portion of a query
124 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 * @param string the field
126 * @param string an alias
127 * @return object
128 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600129 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
131 return $this->_max_min_avg_sum($select, $alias, 'MAX');
132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 // --------------------------------------------------------------------
135
136 /**
137 * Select Min
138 *
139 * Generates a SELECT MIN(field) portion of a query
140 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 * @param string the field
142 * @param string an alias
143 * @return object
144 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600145 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
147 return $this->_max_min_avg_sum($select, $alias, 'MIN');
148 }
149
150 // --------------------------------------------------------------------
151
152 /**
153 * Select Average
154 *
155 * Generates a SELECT AVG(field) portion of a query
156 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 * @param string the field
158 * @param string an alias
159 * @return object
160 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600161 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
163 return $this->_max_min_avg_sum($select, $alias, 'AVG');
164 }
165
166 // --------------------------------------------------------------------
167
168 /**
169 * Select Sum
170 *
171 * Generates a SELECT SUM(field) portion of a query
172 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 * @param string the field
174 * @param string an alias
175 * @return object
176 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600177 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
179 return $this->_max_min_avg_sum($select, $alias, 'SUM');
180 }
181
182 // --------------------------------------------------------------------
183
184 /**
185 * Processing Function for the four functions above:
186 *
187 * select_max()
188 * select_min()
189 * select_avg()
Andrey Andreev24276a32012-01-08 02:44:38 +0200190 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200191 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 * @param string the field
193 * @param string an alias
194 * @return object
195 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600196 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100198 if ( ! is_string($select) OR $select === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
200 $this->display_error('db_invalid_query');
201 }
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
206 {
207 show_error('Invalid function type: '.$type);
208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100210 if ($alias === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
212 $alias = $this->_create_alias_from_table(trim($select));
213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300215 $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->escape_identifiers(trim($alias));
216
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000217 $this->qb_select[] = $sql;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100218 $this->qb_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200219
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000220 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000222 $this->qb_cache_select[] = $sql;
223 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 return $this;
227 }
228
229 // --------------------------------------------------------------------
230
231 /**
232 * Determines the alias name based on the table
233 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 * @param string
235 * @return string
236 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600237 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
239 if (strpos($item, '.') !== FALSE)
240 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200241 $item = explode('.', $item);
242 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 }
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 return $item;
246 }
247
248 // --------------------------------------------------------------------
249
250 /**
251 * DISTINCT
252 *
253 * Sets a flag which tells the query string compiler to add DISTINCT
254 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 * @param bool
256 * @return object
257 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600258 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300260 $this->qb_distinct = is_bool($val) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 return $this;
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 // --------------------------------------------------------------------
265
266 /**
267 * From
268 *
269 * Generates the FROM portion of the query
270 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 * @param mixed can be a string or array
272 * @return object
273 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600274 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300276 foreach ((array) $from as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
278 if (strpos($val, ',') !== FALSE)
279 {
280 foreach (explode(',', $val) as $v)
281 {
282 $v = trim($v);
283 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200284
George Petsagourakis193d4482012-04-28 11:16:18 +0300285 $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000286
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000287 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000289 $this->qb_cache_from[] = $v;
290 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200291 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
294 else
295 {
296 $val = trim($val);
297
Andrey Andreev24276a32012-01-08 02:44:38 +0200298 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000299 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200301
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000302 $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000303
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000304 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000306 $this->qb_cache_from[] = $val;
307 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
309 }
310 }
311
312 return $this;
313 }
314
315 // --------------------------------------------------------------------
316
317 /**
318 * Join
319 *
320 * Generates the JOIN portion of the query
321 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 * @param string
323 * @param string the join condition
324 * @param string the type of join
Alex Bilbief512b732012-06-16 11:15:19 +0100325 * @param string whether not to try to escape identifiers
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 * @return object
327 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300328 public function join($table, $cond, $type = '', $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200329 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100330 if ($type !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
332 $type = strtoupper(trim($type));
333
Andrey Andreev42870232012-06-12 01:30:20 +0300334 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
336 $type = '';
337 }
338 else
339 {
340 $type .= ' ';
341 }
342 }
343
Andrey Andreev24276a32012-01-08 02:44:38 +0200344 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000345 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 $this->_track_aliases($table);
347
Andrey Andreevfe642da2012-06-16 03:47:33 +0300348 is_bool($escape) OR $escape = $this->_protect_identifiers;
349
Andrey Andreev42870232012-06-12 01:30:20 +0300350 // Split multiple conditions
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300351 if ($escape === TRUE && preg_match_all('/\sAND\s|\sOR\s/i', $cond, $m, PREG_OFFSET_CAPTURE))
Andrey Andreev42870232012-06-12 01:30:20 +0300352 {
353 $newcond = '';
354 $m[0][] = array('', strlen($cond));
355
356 for ($i = 0, $c = count($m[0]), $s = 0;
357 $i < $c;
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300358 $s = $m[0][$i][1] + strlen($m[0][$i][0]), $i++)
Andrey Andreev42870232012-06-12 01:30:20 +0300359 {
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300360 $temp = substr($cond, $s, ($m[0][$i][1] - $s));
Andrey Andreev42870232012-06-12 01:30:20 +0300361
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300362 $newcond .= preg_match("/([\[\]\w\.'-]+)(\s*[^\"\[`'\w]+\s*)(.+)/i", $temp, $match)
Andrey Andreev42870232012-06-12 01:30:20 +0300363 ? $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3])
364 : $temp;
365
366 $newcond .= $m[0][$i][0];
367 }
368
Andrey Andreev3751f932012-06-17 18:07:48 +0300369 $cond = ' ON '.$newcond;
Andrey Andreev42870232012-06-12 01:30:20 +0300370 }
371 // Split apart the condition and protect the identifiers
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300372 elseif ($escape === TRUE && preg_match("/([\[\]\w\.'-]+)(\s*[^\"\[`'\w]+\s*)(.+)/i", $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
Andrey Andreev3751f932012-06-17 18:07:48 +0300374 $cond = ' ON '.$this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
375 }
376 elseif ( ! $this->_has_operator($cond))
377 {
378 $cond = ' USING ('.($escape ? $this->escape_identifiers($cond) : $cond).')';
379 }
380 else
381 {
382 $cond = ' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Andrey Andreev42870232012-06-12 01:30:20 +0300385 // Do we want to escape the table name?
386 if ($escape === TRUE)
387 {
388 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
389 }
390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 // Assemble the JOIN statement
Andrey Andreev3751f932012-06-17 18:07:48 +0300392 $this->qb_join[] = $join = $type.'JOIN '.$table.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000393
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000394 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000396 $this->qb_cache_join[] = $join;
397 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
399
400 return $this;
401 }
402
403 // --------------------------------------------------------------------
404
405 /**
406 * Where
407 *
408 * Generates the WHERE portion of the query. Separates
409 * multiple calls with AND
410 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 * @param mixed
412 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300413 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 * @return object
415 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300416 public function where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300418 return $this->_wh('qb_where', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 // --------------------------------------------------------------------
422
423 /**
424 * OR Where
425 *
426 * Generates the WHERE portion of the query. Separates
427 * multiple calls with OR
428 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 * @param mixed
430 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300431 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 * @return object
433 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300434 public function or_where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300436 return $this->_wh('qb_where', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
438
439 // --------------------------------------------------------------------
440
441 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +0300442 * WHERE, HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300444 * Called by where(), or_where(), having(), or_having()
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300446 * @param string 'qb_where' or 'qb_having'
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 * @param mixed
448 * @param mixed
449 * @param string
Andrey Andreevb0478652012-07-18 15:34:46 +0300450 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 * @return object
452 */
Andrey Andreevd40459d2012-07-18 16:46:39 +0300453 protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300455 $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where';
456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 if ( ! is_array($key))
458 {
459 $key = array($key => $value);
460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 // If the escape value was not set will will base it on the global setting
Andrey Andreeve10fb792012-06-15 12:07:04 +0300463 is_bool($escape) OR $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000464
465 foreach ($key as $k => $v)
466 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300467 $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0)
Andrey Andreev58803fb2012-06-24 00:45:37 +0300468 ? $this->_group_get_type('')
469 : $this->_group_get_type($type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000470
471 if (is_null($v) && ! $this->_has_operator($k))
472 {
473 // value appears not to have been set, assign the test to IS NULL
474 $k .= ' IS NULL';
475 }
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 if ( ! is_null($v))
478 {
479 if ($escape === TRUE)
480 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300481 $v = ' '.(is_int($v) ? $v : $this->escape($v));
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
WanWizard7219c072011-12-28 14:09:05 +0100483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 if ( ! $this->_has_operator($k))
485 {
Greg Akere156c6e2011-04-20 16:03:04 -0500486 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
488 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000489
Andrey Andreevd40459d2012-07-18 16:46:39 +0300490 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000491 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300493 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
494 $this->qb_cache_exists[] = substr($qb_key, 3);
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 return $this;
500 }
501
502 // --------------------------------------------------------------------
503
504 /**
505 * Where_in
506 *
507 * Generates a WHERE field IN ('item', 'item') SQL query joined with
508 * AND if appropriate
509 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 * @param string The field to search
511 * @param array The values searched on
512 * @return object
513 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300514 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300516 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 }
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 // --------------------------------------------------------------------
520
521 /**
522 * Where_in_or
523 *
524 * Generates a WHERE field IN ('item', 'item') SQL query joined with
525 * OR if appropriate
526 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 * @param string The field to search
528 * @param array The values searched on
529 * @return object
530 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300531 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300533 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 }
535
536 // --------------------------------------------------------------------
537
538 /**
539 * Where_not_in
540 *
541 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
542 * with AND if appropriate
543 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 * @param string The field to search
545 * @param array The values searched on
546 * @return object
547 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300548 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300550 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 // --------------------------------------------------------------------
554
555 /**
556 * Where_not_in_or
557 *
558 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
559 * with OR if appropriate
560 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 * @param string The field to search
562 * @param array The values searched on
563 * @return object
564 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300565 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300567 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 }
569
570 // --------------------------------------------------------------------
571
572 /**
573 * Where_in
574 *
575 * Called by where_in, where_in_or, where_not_in, where_not_in_or
576 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 * @param string The field to search
578 * @param array The values searched on
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300579 * @param bool If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200580 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 * @return object
582 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300583 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 {
585 if ($key === NULL OR $values === NULL)
586 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300587 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 }
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 if ( ! is_array($values))
591 {
592 $values = array($values);
593 }
Barry Mienydd671972010-10-04 16:33:58 +0200594
Andrey Andreev498c1e02012-06-16 03:34:10 +0300595 is_bool($escape) OR $escape = $this->_protect_identifiers;
596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 $not = ($not) ? ' NOT' : '';
598
Andrey Andreev94611df2012-07-19 12:29:54 +0300599 $where_in = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 foreach ($values as $value)
601 {
Andrey Andreev94611df2012-07-19 12:29:54 +0300602 $wherein[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 }
604
WanWizardbc69f362012-06-22 00:10:11 +0200605 $prefix = (count($this->qb_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Andrey Andreev6e704752012-07-18 00:46:33 +0300606 $where_in = array(
Andrey Andreev94611df2012-07-19 12:29:54 +0300607 'condition' => $prefix.$key.$not.' IN('.implode(', ', $where_in).')',
Andrey Andreev6e704752012-07-18 00:46:33 +0300608 'escape' => $escape
609 );
Barry Mienydd671972010-10-04 16:33:58 +0200610
Andrey Andreev6e704752012-07-18 00:46:33 +0300611 $this->qb_where[] = $where_in;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000612 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000614 $this->qb_cache_where[] = $where_in;
615 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 }
617
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 return $this;
619 }
Barry Mienydd671972010-10-04 16:33:58 +0200620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 // --------------------------------------------------------------------
622
623 /**
624 * Like
625 *
626 * Generates a %LIKE% portion of the query. Separates
627 * multiple calls with AND
628 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300630 * @param string
631 * @param string
632 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 * @return object
634 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300635 public function like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300637 return $this->_like($field, $match, 'AND ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 }
639
640 // --------------------------------------------------------------------
641
642 /**
643 * Not Like
644 *
645 * Generates a NOT LIKE portion of the query. Separates
646 * multiple calls with AND
647 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300649 * @param string
650 * @param string
651 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 * @return object
653 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300654 public function not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300656 return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 }
Barry Mienydd671972010-10-04 16:33:58 +0200658
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 // --------------------------------------------------------------------
660
661 /**
662 * OR Like
663 *
664 * Generates a %LIKE% portion of the query. Separates
665 * multiple calls with OR
666 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300668 * @param string
669 * @param string
670 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 * @return object
672 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300673 public function or_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300675 return $this->_like($field, $match, 'OR ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 }
677
678 // --------------------------------------------------------------------
679
680 /**
681 * OR Not Like
682 *
683 * Generates a NOT LIKE portion of the query. Separates
684 * multiple calls with OR
685 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300687 * @param string
688 * @param string
689 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 * @return object
691 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300692 public function or_not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300694 return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 }
Barry Mienydd671972010-10-04 16:33:58 +0200696
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 // --------------------------------------------------------------------
698
699 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 * Like
701 *
Andrey Andreevb0478652012-07-18 15:34:46 +0300702 * Called by like(), or_like(), not_like, or_not_like()
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 * @param string
Andrey Andreevb0478652012-07-18 15:34:46 +0300706 * @param string
707 * @param string
708 * @param string
709 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 * @return object
711 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300712 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 {
714 if ( ! is_array($field))
715 {
716 $field = array($field => $match);
717 }
Barry Mienydd671972010-10-04 16:33:58 +0200718
Andrey Andreevb0478652012-07-18 15:34:46 +0300719 is_bool($escape) OR $escape = $this->_protect_identifiers;
720 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
721 ? $this->_group_get_type('') : $this->_group_get_type($type);
722
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 foreach ($field as $k => $v)
724 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000725 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300726
Andrey Andreev24276a32012-01-08 02:44:38 +0200727 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400728 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300729 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400730 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200731 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300733 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200735 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300737 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 }
739 else
740 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300741 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600743
Derek Jonese4ed5832009-02-20 21:44:59 +0000744 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100745 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000746 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300747 $like_statement .= sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000748 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600749
Andrey Andreevb0478652012-07-18 15:34:46 +0300750 $this->qb_where[] = array('condition' => $like_statement, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000751 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 {
Andrey Andreevededc4a2012-07-18 01:16:15 +0300753 $this->qb_cache_where[] = $like_statement;
754 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200757
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 return $this;
759 }
Barry Mienydd671972010-10-04 16:33:58 +0200760
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 // --------------------------------------------------------------------
762
763 /**
WanWizard7219c072011-12-28 14:09:05 +0100764 * Starts a query group.
765 *
766 * @param string (Internal use only)
767 * @param string (Internal use only)
768 * @return object
769 */
770 public function group_start($not = '', $type = 'AND ')
771 {
772 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100773
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000774 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100775 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Andrey Andreev6e704752012-07-18 00:46:33 +0300776 $where = array(
777 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (',
778 'escape' => FALSE
779 );
WanWizard7219c072011-12-28 14:09:05 +0100780
Andrey Andreev6e704752012-07-18 00:46:33 +0300781 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000782 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100783 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300784 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +0100785 }
786
787 return $this;
788 }
789
790 // --------------------------------------------------------------------
791
792 /**
793 * Starts a query group, but ORs the group
794 *
795 * @return object
796 */
797 public function or_group_start()
798 {
799 return $this->group_start('', 'OR ');
800 }
801
802 // --------------------------------------------------------------------
803
804 /**
805 * Starts a query group, but NOTs the group
806 *
807 * @return object
808 */
809 public function not_group_start()
810 {
811 return $this->group_start('NOT ', 'AND ');
812 }
813
814 // --------------------------------------------------------------------
815
816 /**
817 * Starts a query group, but OR NOTs the group
818 *
819 * @return object
820 */
821 public function or_not_group_start()
822 {
823 return $this->group_start('NOT ', 'OR ');
824 }
825
826 // --------------------------------------------------------------------
827
828 /**
829 * Ends a query group
830 *
831 * @return object
832 */
833 public function group_end()
834 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000835 $this->qb_where_group_started = FALSE;
Andrey Andreev6e704752012-07-18 00:46:33 +0300836 $where = array(
837 'condition' => str_repeat(' ', $this->qb_where_group_count--).')',
838 'escape' => FALSE
839 );
WanWizard7219c072011-12-28 14:09:05 +0100840
Andrey Andreev6e704752012-07-18 00:46:33 +0300841 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000842 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100843 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300844 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +0100845 }
846
WanWizard7219c072011-12-28 14:09:05 +0100847 return $this;
848 }
849
850 // --------------------------------------------------------------------
851
852 /**
853 * Group_get_type
854 *
855 * Called by group_start(), _like(), _where() and _where_in()
856 *
857 * @param string
858 * @return string
859 */
860 protected function _group_get_type($type)
861 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000862 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100863 {
864 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000865 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100866 }
867
868 return $type;
869 }
870
871 // --------------------------------------------------------------------
872
873 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 * GROUP BY
875 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 * @param string
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300877 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 * @return object
879 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300880 public function group_by($by, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 {
882 if (is_string($by))
883 {
884 $by = explode(',', $by);
885 }
Barry Mienydd671972010-10-04 16:33:58 +0200886
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300887 is_bool($escape) OR $escape = $this->_protect_identifiers;
888
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 foreach ($by as $val)
890 {
891 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200892
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100893 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000895 $this->qb_groupby[] = $val = $this->protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200896
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000897 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000899 $this->qb_cache_groupby[] = $val;
900 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 }
902 }
903 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200904
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 return $this;
906 }
907
908 // --------------------------------------------------------------------
909
910 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 * Sets the HAVING value
912 *
913 * Separates multiple calls with AND
914 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 * @param string
916 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300917 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 * @return object
919 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300920 public function having($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300922 return $this->_wh('qb_having', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 }
Barry Mienydd671972010-10-04 16:33:58 +0200924
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 // --------------------------------------------------------------------
926
927 /**
928 * Sets the OR HAVING value
929 *
930 * Separates multiple calls with OR
931 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 * @param string
933 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300934 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 * @return object
936 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300937 public function or_having($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300939 return $this->_wh('qb_having', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 }
Barry Mienydd671972010-10-04 16:33:58 +0200941
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 // --------------------------------------------------------------------
943
944 /**
945 * Sets the ORDER BY value
946 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 * @param string
948 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100949 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 * @return object
951 */
Andrey Andreevd24160c2012-06-16 03:21:20 +0300952 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200954 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 {
956 $orderby = ''; // Random results want or don't need a field name
957 $direction = $this->_random_keyword;
958 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100959 elseif (trim($direction) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 {
Andrey Andreev650b4c02012-06-11 12:07:15 +0300961 $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : ' ASC';
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 }
Barry Mienydd671972010-10-04 16:33:58 +0200963
Andrey Andreevd24160c2012-06-16 03:21:20 +0300964 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +0200965
Andrey Andreevd24160c2012-06-16 03:21:20 +0300966 if ($escape === TRUE && strpos($orderby, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 {
968 $temp = array();
969 foreach (explode(',', $orderby) as $part)
970 {
971 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000972 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 {
Andrey Andreev88cb2782012-06-11 20:40:50 +0300974 $part = preg_match('/^(.+)\s+(ASC|DESC)$/i', $part, $matches)
975 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
976 : $this->protect_identifiers($part);
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 }
Barry Mienydd671972010-10-04 16:33:58 +0200978
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 $temp[] = $part;
980 }
Barry Mienydd671972010-10-04 16:33:58 +0200981
982 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 }
Andrey Andreev650b4c02012-06-11 12:07:15 +0300984 elseif ($direction !== $this->_random_keyword && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 {
Andrey Andreev1d1d7ff2012-06-11 22:35:35 +0300986 $orderby = preg_match('/^(.+)\s+(ASC|DESC)$/i', $orderby, $matches)
Andrey Andreev88cb2782012-06-11 20:40:50 +0300987 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
988 : $this->protect_identifiers($orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 }
Barry Mienydd671972010-10-04 16:33:58 +0200990
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000991 $this->qb_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200992
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000993 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000995 $this->qb_cache_orderby[] = $orderby_statement;
996 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 }
998
999 return $this;
1000 }
Barry Mienydd671972010-10-04 16:33:58 +02001001
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 // --------------------------------------------------------------------
1003
1004 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 * Sets the LIMIT value
1006 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001007 * @param int the limit value
1008 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 * @return object
1010 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001011 public function limit($value, $offset = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001013 is_null($value) OR $this->qb_limit = (int) $value;
1014 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001015
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 return $this;
1017 }
Barry Mienydd671972010-10-04 16:33:58 +02001018
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 // --------------------------------------------------------------------
1020
1021 /**
1022 * Sets the OFFSET value
1023 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001024 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 * @return object
1026 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001027 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001029 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 return $this;
1031 }
Barry Mienydd671972010-10-04 16:33:58 +02001032
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 // --------------------------------------------------------------------
1034
1035 /**
Andrey Andreev2c35b642012-06-24 03:05:26 +03001036 * Limit string
1037 *
1038 * Generates a platform-specific LIMIT clause
1039 *
1040 * @param string the sql query string
Andrey Andreev2c35b642012-06-24 03:05:26 +03001041 * @return string
1042 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001043 protected function _limit($sql)
Andrey Andreev2c35b642012-06-24 03:05:26 +03001044 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001045 return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').$this->qb_limit;
Andrey Andreev2c35b642012-06-24 03:05:26 +03001046 }
1047
1048 // --------------------------------------------------------------------
1049
1050 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001051 * The "set" function.
1052 *
1053 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 * @param mixed
1056 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001057 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 * @return object
1059 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001060 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 {
1062 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001063
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 if ( ! is_array($key))
1065 {
1066 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001067 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001068
Andrey Andreevfe642da2012-06-16 03:47:33 +03001069 is_bool($escape) OR $escape = $this->_protect_identifiers;
1070
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 foreach ($key as $k => $v)
1072 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001073 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1074 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 }
Barry Mienydd671972010-10-04 16:33:58 +02001076
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 return $this;
1078 }
WanWizard7219c072011-12-28 14:09:05 +01001079
Kyle Farris0c147b32011-08-26 02:29:31 -04001080 // --------------------------------------------------------------------
1081
1082 /**
1083 * Get SELECT query string
1084 *
1085 * Compiles a SELECT query string and returns the sql.
1086 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001087 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001088 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001089 * @return string
1090 */
WanWizard7219c072011-12-28 14:09:05 +01001091 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001092 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001093 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001094 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001095 $this->_track_aliases($table);
1096 $this->from($table);
1097 }
WanWizard7219c072011-12-28 14:09:05 +01001098
Andrey Andreev650b4c02012-06-11 12:07:15 +03001099 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001100
Kyle Farris0c147b32011-08-26 02:29:31 -04001101 if ($reset === TRUE)
1102 {
1103 $this->_reset_select();
1104 }
WanWizard7219c072011-12-28 14:09:05 +01001105
Kyle Farris0c147b32011-08-26 02:29:31 -04001106 return $select;
1107 }
WanWizard7219c072011-12-28 14:09:05 +01001108
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 // --------------------------------------------------------------------
1110
1111 /**
1112 * Get
1113 *
1114 * Compiles the select statement based on the other functions called
1115 * and runs the query
1116 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 * @param string the table
1118 * @param string the limit clause
1119 * @param string the offset clause
1120 * @return object
1121 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001122 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001124 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 {
1126 $this->_track_aliases($table);
1127 $this->from($table);
1128 }
Barry Mienydd671972010-10-04 16:33:58 +02001129
Andrey Andreev650b4c02012-06-11 12:07:15 +03001130 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 {
1132 $this->limit($limit, $offset);
1133 }
Barry Mienydd671972010-10-04 16:33:58 +02001134
Andrey Andreev24276a32012-01-08 02:44:38 +02001135 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 $this->_reset_select();
1137 return $result;
1138 }
1139
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001140 // --------------------------------------------------------------------
1141
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 /**
1143 * "Count All Results" query
1144 *
Barry Mienydd671972010-10-04 16:33:58 +02001145 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001146 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 * @param string
1149 * @return string
1150 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001151 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001153 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 {
1155 $this->_track_aliases($table);
1156 $this->from($table);
1157 }
Barry Mienydd671972010-10-04 16:33:58 +02001158
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001159 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001161
Purwandi1d160e72012-01-09 16:33:28 +07001162 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001164 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 }
1166
Purwandi1d160e72012-01-09 16:33:28 +07001167 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001168 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001170
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 // --------------------------------------------------------------------
1172
1173 /**
1174 * Get_Where
1175 *
1176 * Allows the where clause, limit and offset to be added directly
1177 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 * @param string the where clause
1179 * @param string the limit clause
1180 * @param string the offset clause
1181 * @return object
1182 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001183 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001185 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 {
1187 $this->from($table);
1188 }
1189
1190 if ( ! is_null($where))
1191 {
1192 $this->where($where);
1193 }
Barry Mienydd671972010-10-04 16:33:58 +02001194
Andrey Andreev650b4c02012-06-11 12:07:15 +03001195 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 {
1197 $this->limit($limit, $offset);
1198 }
Barry Mienydd671972010-10-04 16:33:58 +02001199
Andrey Andreev24276a32012-01-08 02:44:38 +02001200 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 $this->_reset_select();
1202 return $result;
1203 }
1204
1205 // --------------------------------------------------------------------
1206
1207 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001208 * Insert_Batch
1209 *
1210 * Compiles batch insert strings and runs the queries
1211 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001212 * @param string the table to retrieve the results from
1213 * @param array an associative array of insert values
1214 * @return object
1215 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001216 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001217 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001218 if ( ! is_null($set))
1219 {
1220 $this->set_insert_batch($set);
1221 }
Barry Mienydd671972010-10-04 16:33:58 +02001222
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001223 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001224 {
1225 if ($this->db_debug)
1226 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001227 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001228 return $this->display_error('db_must_use_set');
1229 }
1230 return FALSE;
1231 }
1232
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001233 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001234 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001235 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001236 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001237 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001238 }
Barry Mienydd671972010-10-04 16:33:58 +02001239
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001240 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001241 }
1242
1243 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001244 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001245 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001246 $this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, 100)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001247 }
Barry Mienydd671972010-10-04 16:33:58 +02001248
Derek Jonesd10e8962010-03-02 17:10:36 -06001249 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001250 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001251 }
1252
1253 // --------------------------------------------------------------------
1254
1255 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001256 * Insert_batch statement
1257 *
1258 * Generates a platform-specific insert string from the supplied data.
1259 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001260 * @param string the table name
1261 * @param array the insert keys
1262 * @param array the insert values
1263 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001264 */
1265 protected function _insert_batch($table, $keys, $values)
1266 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001267 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001268 }
1269
1270 // --------------------------------------------------------------------
1271
1272 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001273 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001274 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001275 * @param mixed
1276 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001277 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001278 * @return object
1279 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001280 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001281 {
1282 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001283
Derek Jonesd10e8962010-03-02 17:10:36 -06001284 if ( ! is_array($key))
1285 {
1286 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001287 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001288
Andrey Andreevfe642da2012-06-16 03:47:33 +03001289 is_bool($escape) OR $escape = $this->_protect_identifiers;
1290
Iban Eguia3c0a4522012-04-15 13:30:44 +02001291 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001292 sort($keys);
1293
1294 foreach ($key as $row)
1295 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001296 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001297 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1298 {
1299 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001300 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001301 return;
1302 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001303
Barry Mienydd671972010-10-04 16:33:58 +02001304 ksort($row); // puts $row in the same order as our keys
1305
Andrey Andreev650b4c02012-06-11 12:07:15 +03001306 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001307 {
1308 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001309 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001310 {
Barry Mienydd671972010-10-04 16:33:58 +02001311 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001312 }
1313
Andrey Andreev650b4c02012-06-11 12:07:15 +03001314 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001315 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001316
1317 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001318 }
1319
1320 foreach ($keys as $k)
1321 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001322 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001323 }
Barry Mienydd671972010-10-04 16:33:58 +02001324
Derek Jonesd10e8962010-03-02 17:10:36 -06001325 return $this;
1326 }
WanWizard7219c072011-12-28 14:09:05 +01001327
Kyle Farris0c147b32011-08-26 02:29:31 -04001328 // --------------------------------------------------------------------
1329
1330 /**
1331 * Get INSERT query string
1332 *
1333 * Compiles an insert query and returns the sql
1334 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001335 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001336 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001337 * @return string
1338 */
1339 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001340 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001341 if ($this->_validate_insert($table) === FALSE)
1342 {
1343 return FALSE;
1344 }
WanWizard7219c072011-12-28 14:09:05 +01001345
Kyle Farris76116012011-08-31 11:17:48 -04001346 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001347 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001348 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001349 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001350 array_keys($this->qb_set),
1351 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001352 );
WanWizard7219c072011-12-28 14:09:05 +01001353
Kyle Farris0c147b32011-08-26 02:29:31 -04001354 if ($reset === TRUE)
1355 {
1356 $this->_reset_write();
1357 }
WanWizard7219c072011-12-28 14:09:05 +01001358
Kyle Farris0c147b32011-08-26 02:29:31 -04001359 return $sql;
1360 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001361
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 // --------------------------------------------------------------------
1363
1364 /**
1365 * Insert
1366 *
1367 * Compiles an insert string and runs the query
1368 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001369 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001370 * @param array an associative array of insert values
1371 * @return object
1372 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001373 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001374 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 if ( ! is_null($set))
1376 {
1377 $this->set($set);
1378 }
WanWizard7219c072011-12-28 14:09:05 +01001379
Kyle Farris0c147b32011-08-26 02:29:31 -04001380 if ($this->_validate_insert($table) === FALSE)
1381 {
1382 return FALSE;
1383 }
WanWizard7219c072011-12-28 14:09:05 +01001384
Kyle Farris76116012011-08-31 11:17:48 -04001385 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001386 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001387 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001388 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001389 array_keys($this->qb_set),
1390 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001391 );
Barry Mienydd671972010-10-04 16:33:58 +02001392
Kyle Farris0c147b32011-08-26 02:29:31 -04001393 $this->_reset_write();
1394 return $this->query($sql);
1395 }
WanWizard7219c072011-12-28 14:09:05 +01001396
Kyle Farris0c147b32011-08-26 02:29:31 -04001397 // --------------------------------------------------------------------
1398
1399 /**
1400 * Validate Insert
1401 *
1402 * This method is used by both insert() and get_compiled_insert() to
1403 * validate that the there data is actually being set and that table
1404 * has been chosen to be inserted into.
1405 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001406 * @param string the table to insert data into
1407 * @return string
1408 */
WanWizard7219c072011-12-28 14:09:05 +01001409 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001410 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001411 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001412 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001413 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001414 }
1415
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001416 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001417 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001418 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001419 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001420 elseif ( ! isset($this->qb_from[0]))
1421 {
1422 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1423 }
WanWizard7219c072011-12-28 14:09:05 +01001424
Kyle Farris0c147b32011-08-26 02:29:31 -04001425 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001426 }
Barry Mienydd671972010-10-04 16:33:58 +02001427
Phil Sturgeon9789f322011-07-15 15:14:05 -06001428 // --------------------------------------------------------------------
1429
1430 /**
1431 * Replace
1432 *
1433 * Compiles an replace into string and runs the query
1434 *
1435 * @param string the table to replace data into
1436 * @param array an associative array of insert values
1437 * @return object
1438 */
1439 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001440 {
1441 if ( ! is_null($set))
1442 {
1443 $this->set($set);
1444 }
Barry Mienydd671972010-10-04 16:33:58 +02001445
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001446 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001447 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001448 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001449 }
1450
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001451 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001452 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001453 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001454 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001455 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001456 }
Barry Mienydd671972010-10-04 16:33:58 +02001457
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001458 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001459 }
1460
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001461 $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 +00001462
Derek Jonesd10e8962010-03-02 17:10:36 -06001463 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001464 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001465 }
WanWizard7219c072011-12-28 14:09:05 +01001466
Kyle Farris0c147b32011-08-26 02:29:31 -04001467 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001468
Kyle Farris0c147b32011-08-26 02:29:31 -04001469 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001470 * Replace statement
1471 *
1472 * Generates a platform-specific replace string from the supplied data
1473 *
1474 * @param string the table name
1475 * @param array the insert keys
1476 * @param array the insert values
1477 * @return string
1478 */
1479 protected function _replace($table, $keys, $values)
1480 {
1481 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1482 }
1483
1484 // --------------------------------------------------------------------
1485
1486 /**
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001487 * From Tables
1488 *
1489 * This public function implicitly groups FROM tables so there is no confusion
1490 * about operator precedence in harmony with SQL standards
1491 *
1492 * @param array
1493 * @return string
1494 */
1495 protected function _from_tables($tables)
1496 {
1497 is_array($tables) OR $tables = array($tables);
1498
1499 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
1500 }
1501
1502 // --------------------------------------------------------------------
1503
1504 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001505 * Get UPDATE query string
1506 *
1507 * Compiles an update query and returns the sql
1508 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001509 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001510 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001511 * @return string
1512 */
1513 public function get_compiled_update($table = '', $reset = TRUE)
1514 {
1515 // Combine any cached components with the current statements
1516 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001517
Kyle Farris0c147b32011-08-26 02:29:31 -04001518 if ($this->_validate_update($table) === FALSE)
1519 {
1520 return FALSE;
1521 }
WanWizard7219c072011-12-28 14:09:05 +01001522
Andrey Andreevb0478652012-07-18 15:34:46 +03001523 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001524
Kyle Farris0c147b32011-08-26 02:29:31 -04001525 if ($reset === TRUE)
1526 {
1527 $this->_reset_write();
1528 }
WanWizard7219c072011-12-28 14:09:05 +01001529
Kyle Farris0c147b32011-08-26 02:29:31 -04001530 return $sql;
1531 }
WanWizard7219c072011-12-28 14:09:05 +01001532
Derek Allard2067d1a2008-11-13 22:59:24 +00001533 // --------------------------------------------------------------------
1534
1535 /**
1536 * Update
1537 *
1538 * Compiles an update string and runs the query
1539 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001540 * @param string the table to retrieve the results from
1541 * @param array an associative array of update values
1542 * @param mixed the where clause
1543 * @return object
1544 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001545 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001546 {
1547 // Combine any cached components with the current statements
1548 $this->_merge_cache();
1549
1550 if ( ! is_null($set))
1551 {
1552 $this->set($set);
1553 }
Barry Mienydd671972010-10-04 16:33:58 +02001554
Kyle Farris0c147b32011-08-26 02:29:31 -04001555 if ($this->_validate_update($table) === FALSE)
1556 {
1557 return FALSE;
1558 }
1559
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001560 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001561 {
1562 $this->where($where);
1563 }
1564
Andrey Andreev650b4c02012-06-11 12:07:15 +03001565 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001566 {
1567 $this->limit($limit);
1568 }
1569
Andrey Andreevb0478652012-07-18 15:34:46 +03001570 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
Jamie Rumbelow3b1355c2012-03-06 21:27:46 +00001571
Kyle Farris0c147b32011-08-26 02:29:31 -04001572 $this->_reset_write();
1573 return $this->query($sql);
1574 }
WanWizard7219c072011-12-28 14:09:05 +01001575
Kyle Farris0c147b32011-08-26 02:29:31 -04001576 // --------------------------------------------------------------------
1577
1578 /**
1579 * Validate Update
1580 *
1581 * This method is used by both update() and get_compiled_update() to
1582 * validate that data is actually being set and that a table has been
1583 * chosen to be update.
1584 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001585 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001586 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001587 */
1588 protected function _validate_update($table = '')
1589 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001590 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001591 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001592 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001593 }
1594
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001595 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001596 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001597 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001598 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001599 elseif ( ! isset($this->qb_from[0]))
1600 {
1601 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1602 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001603
1604 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001605 }
WanWizard7219c072011-12-28 14:09:05 +01001606
Derek Jonesd10e8962010-03-02 17:10:36 -06001607 // --------------------------------------------------------------------
1608
1609 /**
1610 * Update_Batch
1611 *
1612 * Compiles an update string and runs the query
1613 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001614 * @param string the table to retrieve the results from
1615 * @param array an associative array of update values
1616 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001617 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001618 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001619 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001620 {
1621 // Combine any cached components with the current statements
1622 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001623
Derek Jonesd10e8962010-03-02 17:10:36 -06001624 if (is_null($index))
1625 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001626 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001627 }
1628
1629 if ( ! is_null($set))
1630 {
1631 $this->set_update_batch($set, $index);
1632 }
1633
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001634 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001635 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001636 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001637 }
1638
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001639 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001640 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001641 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001642 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001643 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001644 }
Barry Mienydd671972010-10-04 16:33:58 +02001645
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001646 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001647 }
Barry Mienydd671972010-10-04 16:33:58 +02001648
Derek Jonesd10e8962010-03-02 17:10:36 -06001649 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001650 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001651 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001652 $this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, 100), $this->protect_identifiers($index)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001653 }
Barry Mienydd671972010-10-04 16:33:58 +02001654
Derek Jonesd10e8962010-03-02 17:10:36 -06001655 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001656 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001657 }
1658
1659 // --------------------------------------------------------------------
1660
1661 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001662 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001663 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001664 * @param array
1665 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001666 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001667 * @return object
1668 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001669 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001670 {
1671 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001672
Derek Jonesd10e8962010-03-02 17:10:36 -06001673 if ( ! is_array($key))
1674 {
1675 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001676 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001677
Andrey Andreevfe642da2012-06-16 03:47:33 +03001678 is_bool($escape) OR $escape = $this->_protect_identifiers;
1679
Derek Jonesd10e8962010-03-02 17:10:36 -06001680 foreach ($key as $k => $v)
1681 {
1682 $index_set = FALSE;
1683 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001684 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001685 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001686 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001687 {
1688 $index_set = TRUE;
1689 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001690
Andrey Andreevfe642da2012-06-16 03:47:33 +03001691 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001692 }
1693
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001694 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001695 {
1696 return $this->display_error('db_batch_missing_index');
1697 }
1698
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001699 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001700 }
Barry Mienydd671972010-10-04 16:33:58 +02001701
Derek Jonesd10e8962010-03-02 17:10:36 -06001702 return $this;
1703 }
1704
Derek Allard2067d1a2008-11-13 22:59:24 +00001705 // --------------------------------------------------------------------
1706
1707 /**
1708 * Empty Table
1709 *
1710 * Compiles a delete string and runs "DELETE FROM table"
1711 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001712 * @param string the table to empty
1713 * @return object
1714 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001715 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001716 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001717 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001718 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001719 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001720 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001721 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001722 }
1723
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001724 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001725 }
1726 else
1727 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001728 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001729 }
1730
1731 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001732 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 return $this->query($sql);
1734 }
1735
1736 // --------------------------------------------------------------------
1737
1738 /**
1739 * Truncate
1740 *
1741 * Compiles a truncate string and runs the query
1742 * If the database does not support the truncate() command
1743 * This function maps to "DELETE FROM table"
1744 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001745 * @param string the table to truncate
1746 * @return object
1747 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001748 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001750 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001751 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001752 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001753 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001754 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001755 }
1756
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001757 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001758 }
1759 else
1760 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001761 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001762 }
1763
1764 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001765 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001766 return $this->query($sql);
1767 }
WanWizard7219c072011-12-28 14:09:05 +01001768
Kyle Farris0c147b32011-08-26 02:29:31 -04001769 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001770
Kyle Farris0c147b32011-08-26 02:29:31 -04001771 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001772 * Truncate statement
1773 *
1774 * Generates a platform-specific truncate string from the supplied data
1775 *
1776 * If the database does not support the truncate() command,
1777 * then this method maps to 'DELETE FROM table'
1778 *
1779 * @param string the table name
1780 * @return string
1781 */
1782 protected function _truncate($table)
1783 {
1784 return 'TRUNCATE '.$table;
1785 }
1786
1787 // --------------------------------------------------------------------
1788
1789 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001790 * Get DELETE query string
1791 *
1792 * Compiles a delete query string and returns the sql
1793 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001794 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001795 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001796 * @return string
1797 */
1798 public function get_compiled_delete($table = '', $reset = TRUE)
1799 {
1800 $this->return_delete_sql = TRUE;
1801 $sql = $this->delete($table, '', NULL, $reset);
1802 $this->return_delete_sql = FALSE;
1803 return $sql;
1804 }
WanWizard7219c072011-12-28 14:09:05 +01001805
Derek Allard2067d1a2008-11-13 22:59:24 +00001806 // --------------------------------------------------------------------
1807
1808 /**
1809 * Delete
1810 *
1811 * Compiles a delete string and runs the query
1812 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001813 * @param mixed the table(s) to delete from. String or array
1814 * @param mixed the where clause
1815 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001816 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001817 * @return object
1818 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001819 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 {
1821 // Combine any cached components with the current statements
1822 $this->_merge_cache();
1823
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001824 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001825 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001826 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001828 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001829 }
1830
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001831 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001832 }
1833 elseif (is_array($table))
1834 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001835 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001836 {
1837 $this->delete($single_table, $where, $limit, FALSE);
1838 }
1839
1840 $this->_reset_write();
1841 return;
1842 }
1843 else
1844 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001845 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001846 }
1847
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001848 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001849 {
1850 $this->where($where);
1851 }
1852
Andrey Andreev650b4c02012-06-11 12:07:15 +03001853 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001854 {
1855 $this->limit($limit);
1856 }
1857
Andrey Andreev94611df2012-07-19 12:29:54 +03001858 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001859 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001860 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001861 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001862
Andrey Andreevb0478652012-07-18 15:34:46 +03001863 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001864 if ($reset_data)
1865 {
1866 $this->_reset_write();
1867 }
WanWizard7219c072011-12-28 14:09:05 +01001868
Andrey Andreev24276a32012-01-08 02:44:38 +02001869 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001870 }
WanWizard7219c072011-12-28 14:09:05 +01001871
Derek Allard2067d1a2008-11-13 22:59:24 +00001872 // --------------------------------------------------------------------
1873
1874 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001875 * Delete statement
1876 *
1877 * Generates a platform-specific delete string from the supplied data
1878 *
1879 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03001880 * @return string
1881 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001882 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03001883 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001884 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001885 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001886 }
1887
1888 // --------------------------------------------------------------------
1889
1890 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001891 * DB Prefix
1892 *
1893 * Prepends a database prefix if one exists in configuration
1894 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001895 * @param string the table
1896 * @return string
1897 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001898 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001899 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001900 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001901 {
1902 $this->display_error('db_table_name_required');
1903 }
1904
1905 return $this->dbprefix.$table;
1906 }
1907
1908 // --------------------------------------------------------------------
1909
1910 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001911 * Set DB Prefix
1912 *
1913 * Set's the DB Prefix to something new without needing to reconnect
1914 *
1915 * @param string the prefix
1916 * @return string
1917 */
1918 public function set_dbprefix($prefix = '')
1919 {
1920 return $this->dbprefix = $prefix;
1921 }
1922
1923 // --------------------------------------------------------------------
1924
1925 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001926 * Track Aliases
1927 *
1928 * Used to track SQL statements written with aliased tables.
1929 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001930 * @param string The table to inspect
1931 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001932 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001933 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001934 {
1935 if (is_array($table))
1936 {
1937 foreach ($table as $t)
1938 {
1939 $this->_track_aliases($t);
1940 }
1941 return;
1942 }
Barry Mienydd671972010-10-04 16:33:58 +02001943
Derek Jones37f4b9c2011-07-01 17:56:50 -05001944 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001945 // the string into discreet statements
1946 if (strpos($table, ',') !== FALSE)
1947 {
1948 return $this->_track_aliases(explode(',', $table));
1949 }
Barry Mienydd671972010-10-04 16:33:58 +02001950
Derek Allard2067d1a2008-11-13 22:59:24 +00001951 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001952 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001953 {
1954 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03001955 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001956
Derek Allard2067d1a2008-11-13 22:59:24 +00001957 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001958 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001959
Derek Allard2067d1a2008-11-13 22:59:24 +00001960 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001961 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001962 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001963 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001964 }
1965 }
1966 }
WanWizard7219c072011-12-28 14:09:05 +01001967
Derek Allard2067d1a2008-11-13 22:59:24 +00001968 // --------------------------------------------------------------------
1969
1970 /**
1971 * Compile the SELECT statement
1972 *
1973 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001974 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001975 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001976 * @return string
1977 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001978 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001979 {
1980 // Combine any cached components with the current statements
1981 $this->_merge_cache();
1982
Derek Allard2067d1a2008-11-13 22:59:24 +00001983 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001984 if ($select_override !== FALSE)
1985 {
1986 $sql = $select_override;
1987 }
1988 else
1989 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001990 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001991
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001992 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001993 {
Barry Mienydd671972010-10-04 16:33:58 +02001994 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001995 }
1996 else
Barry Mienydd671972010-10-04 16:33:58 +02001997 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001998 // Cycle through the "select" portion of the query and prep each column name.
1999 // The reason we protect identifiers here rather then in the select() function
2000 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002001 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002002 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002003 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002004 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002005 }
Barry Mienydd671972010-10-04 16:33:58 +02002006
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002007 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002008 }
2009 }
2010
Derek Allard2067d1a2008-11-13 22:59:24 +00002011 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002012 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002013 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002014 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002015 }
2016
Derek Allard2067d1a2008-11-13 22:59:24 +00002017 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002018 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002019 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002020 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002021 }
2022
Andrey Andreevd40459d2012-07-18 16:46:39 +03002023 // WHERE
2024 $sql .= $this->_compile_wh('qb_where');
Derek Allard2067d1a2008-11-13 22:59:24 +00002025
Andrey Andreevb0478652012-07-18 15:34:46 +03002026 // GROUP BY
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002027 $sql .= $this->_compile_group_by();
Andrey Andreevb0478652012-07-18 15:34:46 +03002028
2029 // HAVING
Andrey Andreevd40459d2012-07-18 16:46:39 +03002030 $sql .= $this->_compile_wh('qb_having');
Andrey Andreevb0478652012-07-18 15:34:46 +03002031
2032 // ORDER BY
2033 if (count($this->qb_orderby) > 0)
2034 {
2035 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
2036 }
2037
Andrey Andreevd40459d2012-07-18 16:46:39 +03002038 // LIMIT
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002039 if ($this->qb_limit)
Derek Allard2067d1a2008-11-13 22:59:24 +00002040 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002041 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002042 }
2043
2044 return $sql;
2045 }
2046
2047 // --------------------------------------------------------------------
2048
2049 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002050 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002051 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002052 * Escapes identifiers in WHERE and HAVING statements at execution time.
2053 *
Andrey Andreevb0478652012-07-18 15:34:46 +03002054 * Required so that aliases are tracked properly, regardless of wether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002055 * where(), or_where(), having(), or_having are called prior to from(),
2056 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002057 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002058 * @param string 'qb_where' or 'qb_having'
2059 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002060 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002061 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002062 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002063 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002064 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002065 $sql = ($qb_key === 'qb_having') ? "\nHAVING " : "\nWHERE ";
Andrey Andreev6e704752012-07-18 00:46:33 +03002066
Andrey Andreevd40459d2012-07-18 16:46:39 +03002067 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002068 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002069 if ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002070 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002071 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002072 continue;
2073 }
2074
Andrey Andreevd40459d2012-07-18 16:46:39 +03002075 $op = preg_quote($this->_get_operator($this->{$qb_key}[$i]['condition']));
2076 if ( ! preg_match('/^(\s*(?:AND|OR)\s+)?(\(?)(.*)('.$op.')(.*(?<!\)))?(\)?)$/i', $this->{$qb_key}[$i]['condition'], $matches))
Andrey Andreev6e704752012-07-18 00:46:33 +03002077 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002078 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002079 continue;
2080 }
2081
2082 // $matches = array(
2083 // 0 => 'OR (test <= foo)', /* the whole thing */
2084 // 1 => 'OR ', /* optional */
2085 // 2 => '(', /* optional */
2086 // 3 => 'test', /* the field name */
2087 // 4 => ' <= ', /* $op */
2088 // 5 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2089 // 6 => ')' /* optional */
2090 // );
2091 empty($matches[5]) OR $matches[5] = ' '.$this->protect_identifiers(trim($matches[5]));
Andrey Andreevd40459d2012-07-18 16:46:39 +03002092 $this->{$qb_key}[$i] = $matches[1].$matches[2].$this->protect_identifiers(trim($matches[3]))
Andrey Andreev6e704752012-07-18 00:46:33 +03002093 .' '.trim($matches[4]).$matches[5].$matches[6];
2094 }
2095
Andrey Andreevd40459d2012-07-18 16:46:39 +03002096 return implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002097 }
2098
Andrey Andreevb0478652012-07-18 15:34:46 +03002099 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002100 }
2101
2102 // --------------------------------------------------------------------
2103
2104 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002105 * Compile GROUP BY
2106 *
2107 * Escapes identifiers in GROUP BY statements at execution time.
2108 *
2109 * Required so that aliases are tracked properly, regardless of wether
2110 * group_by() is called prior to from(), join() and dbprefix is added
2111 * only if needed.
2112 *
2113 * @return string SQL statement
2114 */
2115 protected function _compile_group_by()
2116 {
2117 if (count($this->qb_groupby) > 0)
2118 {
2119 $sql = "\nGROUP BY ";
2120
2121 $sql .= implode(', ', $this->qb_groupby);
2122 }
2123
2124 return '';
2125 }
2126
2127 // --------------------------------------------------------------------
2128
2129 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002130 * Object to Array
2131 *
2132 * Takes an object as input and converts the class variables to array key/vals
2133 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002134 * @param object
2135 * @return array
2136 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002137 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002138 {
2139 if ( ! is_object($object))
2140 {
2141 return $object;
2142 }
Barry Mienydd671972010-10-04 16:33:58 +02002143
Derek Allard2067d1a2008-11-13 22:59:24 +00002144 $array = array();
2145 foreach (get_object_vars($object) as $key => $val)
2146 {
2147 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002148 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002149 {
2150 $array[$key] = $val;
2151 }
2152 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002153
2154 return $array;
2155 }
Barry Mienydd671972010-10-04 16:33:58 +02002156
Derek Jonesd10e8962010-03-02 17:10:36 -06002157 // --------------------------------------------------------------------
2158
2159 /**
2160 * Object to Array
2161 *
2162 * Takes an object as input and converts the class variables to array key/vals
2163 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002164 * @param object
2165 * @return array
2166 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002167 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002168 {
2169 if ( ! is_object($object))
2170 {
2171 return $object;
2172 }
Barry Mienydd671972010-10-04 16:33:58 +02002173
Derek Jonesd10e8962010-03-02 17:10:36 -06002174 $array = array();
2175 $out = get_object_vars($object);
2176 $fields = array_keys($out);
2177
2178 foreach ($fields as $val)
2179 {
2180 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002181 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002182 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002183 $i = 0;
2184 foreach ($out[$val] as $data)
2185 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002186 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002187 }
2188 }
2189 }
2190
Derek Allard2067d1a2008-11-13 22:59:24 +00002191 return $array;
2192 }
Barry Mienydd671972010-10-04 16:33:58 +02002193
Derek Allard2067d1a2008-11-13 22:59:24 +00002194 // --------------------------------------------------------------------
2195
2196 /**
2197 * Start Cache
2198 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002199 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002200 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002201 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002202 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002203 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002204 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002205 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002206 }
2207
2208 // --------------------------------------------------------------------
2209
2210 /**
2211 * Stop Cache
2212 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002213 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002214 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002215 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002216 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002217 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002218 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002219 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002220 }
2221
2222 // --------------------------------------------------------------------
2223
2224 /**
2225 * Flush Cache
2226 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002227 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002228 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002229 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002230 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002231 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002232 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002233 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002234 'qb_cache_select' => array(),
2235 'qb_cache_from' => array(),
2236 'qb_cache_join' => array(),
2237 'qb_cache_where' => array(),
2238 'qb_cache_like' => array(),
2239 'qb_cache_groupby' => array(),
2240 'qb_cache_having' => array(),
2241 'qb_cache_orderby' => array(),
2242 'qb_cache_set' => array(),
2243 'qb_cache_exists' => array(),
2244 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002245 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002246 }
2247
2248 // --------------------------------------------------------------------
2249
2250 /**
2251 * Merge Cache
2252 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002253 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002254 * locally called ones.
2255 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002256 * @return void
2257 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002258 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002259 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002260 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002261 {
2262 return;
2263 }
2264
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002265 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002266 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002267 $qb_variable = 'qb_'.$val;
2268 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002269
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002270 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002271 {
2272 continue;
2273 }
2274
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002275 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002276 }
2277
2278 // If we are "protecting identifiers" we need to examine the "from"
2279 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002280 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002281 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002282 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002283 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002284
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002285 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002286 }
WanWizard7219c072011-12-28 14:09:05 +01002287
Kyle Farris0c147b32011-08-26 02:29:31 -04002288 // --------------------------------------------------------------------
2289
2290 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002291 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002292 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002293 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002294 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002295 * @return void
2296 */
2297 public function reset_query()
2298 {
2299 $this->_reset_select();
2300 $this->_reset_write();
2301 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002302
2303 // --------------------------------------------------------------------
2304
2305 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002306 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002307 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002308 * @param array An array of fields to reset
2309 * @return void
2310 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002311 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002312 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002313 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002314 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002315 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002316 {
2317 $this->$item = $default_value;
2318 }
2319 }
2320 }
2321
2322 // --------------------------------------------------------------------
2323
2324 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002325 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002326 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002327 * @return void
2328 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002329 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002330 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002331 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002332 'qb_select' => array(),
2333 'qb_from' => array(),
2334 'qb_join' => array(),
2335 'qb_where' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002336 'qb_groupby' => array(),
2337 'qb_having' => array(),
2338 'qb_orderby' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002339 'qb_aliased_tables' => array(),
2340 'qb_no_escape' => array(),
2341 'qb_distinct' => FALSE,
2342 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002343 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002344 )
2345 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002346 }
Barry Mienydd671972010-10-04 16:33:58 +02002347
Derek Allard2067d1a2008-11-13 22:59:24 +00002348 // --------------------------------------------------------------------
2349
2350 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002351 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002352 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002353 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002354 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002355 * @return void
2356 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002357 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002358 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002359 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002360 'qb_set' => array(),
2361 'qb_from' => array(),
2362 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002363 'qb_orderby' => array(),
2364 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002365 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002366 )
2367 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002368 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002369
Derek Allard2067d1a2008-11-13 22:59:24 +00002370}
2371
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002372/* End of file DB_query_builder.php */
Andrey Andreev58803fb2012-06-24 00:45:37 +03002373/* Location: ./system/database/DB_query_builder.php */