blob: 6c247f9570a8ad63ccfc85af0bb673366a775a9e [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 {
Andrey Andreev96feb582012-07-19 13:12:34 +0300882 is_bool($escape) OR $escape = $this->_protect_identifiers;
883
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 if (is_string($by))
885 {
Andrey Andreev96feb582012-07-19 13:12:34 +0300886 $by = ($escape === TRUE)
887 ? explode(',', $by)
888 : array($by);
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 }
Barry Mienydd671972010-10-04 16:33:58 +0200890
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 foreach ($by as $val)
892 {
893 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200894
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100895 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 {
Andrey Andreev96feb582012-07-19 13:12:34 +0300897 $val = array('field' => $val, 'escape' => $escape);
Barry Mienydd671972010-10-04 16:33:58 +0200898
Andrey Andreev96feb582012-07-19 13:12:34 +0300899 $this->qb_groupby[] = $val;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000900 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000902 $this->qb_cache_groupby[] = $val;
903 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 }
905 }
906 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200907
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 return $this;
909 }
910
911 // --------------------------------------------------------------------
912
913 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 * Sets the HAVING value
915 *
916 * Separates multiple calls with AND
917 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 * @param string
919 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300920 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 * @return object
922 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300923 public function having($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300925 return $this->_wh('qb_having', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 }
Barry Mienydd671972010-10-04 16:33:58 +0200927
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 // --------------------------------------------------------------------
929
930 /**
931 * Sets the OR HAVING value
932 *
933 * Separates multiple calls with OR
934 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 * @param string
936 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300937 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 * @return object
939 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300940 public function or_having($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300942 return $this->_wh('qb_having', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 }
Barry Mienydd671972010-10-04 16:33:58 +0200944
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 // --------------------------------------------------------------------
946
947 /**
948 * Sets the ORDER BY value
949 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 * @param string
951 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100952 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 * @return object
954 */
Andrey Andreevd24160c2012-06-16 03:21:20 +0300955 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200957 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 {
959 $orderby = ''; // Random results want or don't need a field name
960 $direction = $this->_random_keyword;
961 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100962 elseif (trim($direction) !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
Andrey Andreev650b4c02012-06-11 12:07:15 +0300964 $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : ' ASC';
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
Barry Mienydd671972010-10-04 16:33:58 +0200966
Andrey Andreevd24160c2012-06-16 03:21:20 +0300967 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +0200968
Andrey Andreevd24160c2012-06-16 03:21:20 +0300969 if ($escape === TRUE && strpos($orderby, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 {
971 $temp = array();
972 foreach (explode(',', $orderby) as $part)
973 {
974 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000975 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 {
Andrey Andreev88cb2782012-06-11 20:40:50 +0300977 $part = preg_match('/^(.+)\s+(ASC|DESC)$/i', $part, $matches)
978 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
979 : $this->protect_identifiers($part);
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 }
Barry Mienydd671972010-10-04 16:33:58 +0200981
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 $temp[] = $part;
983 }
Barry Mienydd671972010-10-04 16:33:58 +0200984
985 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 }
Andrey Andreev650b4c02012-06-11 12:07:15 +0300987 elseif ($direction !== $this->_random_keyword && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 {
Andrey Andreev1d1d7ff2012-06-11 22:35:35 +0300989 $orderby = preg_match('/^(.+)\s+(ASC|DESC)$/i', $orderby, $matches)
Andrey Andreev88cb2782012-06-11 20:40:50 +0300990 ? $this->protect_identifiers(rtrim($matches[1])).' '.$matches[2]
991 : $this->protect_identifiers($orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 }
Barry Mienydd671972010-10-04 16:33:58 +0200993
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000994 $this->qb_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200995
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000996 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000998 $this->qb_cache_orderby[] = $orderby_statement;
999 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 }
1001
1002 return $this;
1003 }
Barry Mienydd671972010-10-04 16:33:58 +02001004
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 // --------------------------------------------------------------------
1006
1007 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 * Sets the LIMIT value
1009 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001010 * @param int the limit value
1011 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 * @return object
1013 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001014 public function limit($value, $offset = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001016 is_null($value) OR $this->qb_limit = (int) $value;
1017 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001018
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 return $this;
1020 }
Barry Mienydd671972010-10-04 16:33:58 +02001021
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 // --------------------------------------------------------------------
1023
1024 /**
1025 * Sets the OFFSET value
1026 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001027 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 * @return object
1029 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001030 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001032 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001033 return $this;
1034 }
Barry Mienydd671972010-10-04 16:33:58 +02001035
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 // --------------------------------------------------------------------
1037
1038 /**
Andrey Andreev2c35b642012-06-24 03:05:26 +03001039 * Limit string
1040 *
1041 * Generates a platform-specific LIMIT clause
1042 *
1043 * @param string the sql query string
Andrey Andreev2c35b642012-06-24 03:05:26 +03001044 * @return string
1045 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001046 protected function _limit($sql)
Andrey Andreev2c35b642012-06-24 03:05:26 +03001047 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001048 return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').$this->qb_limit;
Andrey Andreev2c35b642012-06-24 03:05:26 +03001049 }
1050
1051 // --------------------------------------------------------------------
1052
1053 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001054 * The "set" function.
1055 *
1056 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 * @param mixed
1059 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001060 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 * @return object
1062 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001063 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 {
1065 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001066
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 if ( ! is_array($key))
1068 {
1069 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001070 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001071
Andrey Andreevfe642da2012-06-16 03:47:33 +03001072 is_bool($escape) OR $escape = $this->_protect_identifiers;
1073
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 foreach ($key as $k => $v)
1075 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001076 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1077 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 }
Barry Mienydd671972010-10-04 16:33:58 +02001079
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 return $this;
1081 }
WanWizard7219c072011-12-28 14:09:05 +01001082
Kyle Farris0c147b32011-08-26 02:29:31 -04001083 // --------------------------------------------------------------------
1084
1085 /**
1086 * Get SELECT query string
1087 *
1088 * Compiles a SELECT query string and returns the sql.
1089 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001090 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001091 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001092 * @return string
1093 */
WanWizard7219c072011-12-28 14:09:05 +01001094 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001095 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001096 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001097 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001098 $this->_track_aliases($table);
1099 $this->from($table);
1100 }
WanWizard7219c072011-12-28 14:09:05 +01001101
Andrey Andreev650b4c02012-06-11 12:07:15 +03001102 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001103
Kyle Farris0c147b32011-08-26 02:29:31 -04001104 if ($reset === TRUE)
1105 {
1106 $this->_reset_select();
1107 }
WanWizard7219c072011-12-28 14:09:05 +01001108
Kyle Farris0c147b32011-08-26 02:29:31 -04001109 return $select;
1110 }
WanWizard7219c072011-12-28 14:09:05 +01001111
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 // --------------------------------------------------------------------
1113
1114 /**
1115 * Get
1116 *
1117 * Compiles the select statement based on the other functions called
1118 * and runs the query
1119 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 * @param string the table
1121 * @param string the limit clause
1122 * @param string the offset clause
1123 * @return object
1124 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001125 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001127 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 {
1129 $this->_track_aliases($table);
1130 $this->from($table);
1131 }
Barry Mienydd671972010-10-04 16:33:58 +02001132
Andrey Andreev650b4c02012-06-11 12:07:15 +03001133 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 {
1135 $this->limit($limit, $offset);
1136 }
Barry Mienydd671972010-10-04 16:33:58 +02001137
Andrey Andreev24276a32012-01-08 02:44:38 +02001138 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 $this->_reset_select();
1140 return $result;
1141 }
1142
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001143 // --------------------------------------------------------------------
1144
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 /**
1146 * "Count All Results" query
1147 *
Barry Mienydd671972010-10-04 16:33:58 +02001148 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001149 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 * @param string
1152 * @return string
1153 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001154 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001156 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 {
1158 $this->_track_aliases($table);
1159 $this->from($table);
1160 }
Barry Mienydd671972010-10-04 16:33:58 +02001161
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001162 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001164
Purwandi1d160e72012-01-09 16:33:28 +07001165 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001167 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 }
1169
Purwandi1d160e72012-01-09 16:33:28 +07001170 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001171 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001173
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 // --------------------------------------------------------------------
1175
1176 /**
1177 * Get_Where
1178 *
1179 * Allows the where clause, limit and offset to be added directly
1180 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 * @param string the where clause
1182 * @param string the limit clause
1183 * @param string the offset clause
1184 * @return object
1185 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001186 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001188 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 {
1190 $this->from($table);
1191 }
1192
1193 if ( ! is_null($where))
1194 {
1195 $this->where($where);
1196 }
Barry Mienydd671972010-10-04 16:33:58 +02001197
Andrey Andreev650b4c02012-06-11 12:07:15 +03001198 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 {
1200 $this->limit($limit, $offset);
1201 }
Barry Mienydd671972010-10-04 16:33:58 +02001202
Andrey Andreev24276a32012-01-08 02:44:38 +02001203 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001204 $this->_reset_select();
1205 return $result;
1206 }
1207
1208 // --------------------------------------------------------------------
1209
1210 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001211 * Insert_Batch
1212 *
1213 * Compiles batch insert strings and runs the queries
1214 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001215 * @param string the table to retrieve the results from
1216 * @param array an associative array of insert values
1217 * @return object
1218 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001219 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001220 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001221 if ( ! is_null($set))
1222 {
1223 $this->set_insert_batch($set);
1224 }
Barry Mienydd671972010-10-04 16:33:58 +02001225
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001226 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001227 {
1228 if ($this->db_debug)
1229 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001230 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001231 return $this->display_error('db_must_use_set');
1232 }
1233 return FALSE;
1234 }
1235
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001236 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001237 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001238 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001239 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001240 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001241 }
Barry Mienydd671972010-10-04 16:33:58 +02001242
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001243 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001244 }
1245
1246 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001247 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001248 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001249 $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 -06001250 }
Barry Mienydd671972010-10-04 16:33:58 +02001251
Derek Jonesd10e8962010-03-02 17:10:36 -06001252 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001253 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001254 }
1255
1256 // --------------------------------------------------------------------
1257
1258 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001259 * Insert_batch statement
1260 *
1261 * Generates a platform-specific insert string from the supplied data.
1262 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001263 * @param string the table name
1264 * @param array the insert keys
1265 * @param array the insert values
1266 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001267 */
1268 protected function _insert_batch($table, $keys, $values)
1269 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001270 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001271 }
1272
1273 // --------------------------------------------------------------------
1274
1275 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001276 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001277 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001278 * @param mixed
1279 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001280 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001281 * @return object
1282 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001283 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001284 {
1285 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001286
Derek Jonesd10e8962010-03-02 17:10:36 -06001287 if ( ! is_array($key))
1288 {
1289 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001290 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001291
Andrey Andreevfe642da2012-06-16 03:47:33 +03001292 is_bool($escape) OR $escape = $this->_protect_identifiers;
1293
Iban Eguia3c0a4522012-04-15 13:30:44 +02001294 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001295 sort($keys);
1296
1297 foreach ($key as $row)
1298 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001299 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001300 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1301 {
1302 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001303 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001304 return;
1305 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001306
Barry Mienydd671972010-10-04 16:33:58 +02001307 ksort($row); // puts $row in the same order as our keys
1308
Andrey Andreev650b4c02012-06-11 12:07:15 +03001309 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001310 {
1311 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001312 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001313 {
Barry Mienydd671972010-10-04 16:33:58 +02001314 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001315 }
1316
Andrey Andreev650b4c02012-06-11 12:07:15 +03001317 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001318 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001319
1320 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001321 }
1322
1323 foreach ($keys as $k)
1324 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001325 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001326 }
Barry Mienydd671972010-10-04 16:33:58 +02001327
Derek Jonesd10e8962010-03-02 17:10:36 -06001328 return $this;
1329 }
WanWizard7219c072011-12-28 14:09:05 +01001330
Kyle Farris0c147b32011-08-26 02:29:31 -04001331 // --------------------------------------------------------------------
1332
1333 /**
1334 * Get INSERT query string
1335 *
1336 * Compiles an insert query and returns the sql
1337 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001338 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001339 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001340 * @return string
1341 */
1342 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001343 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001344 if ($this->_validate_insert($table) === FALSE)
1345 {
1346 return FALSE;
1347 }
WanWizard7219c072011-12-28 14:09:05 +01001348
Kyle Farris76116012011-08-31 11:17:48 -04001349 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001350 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001351 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001352 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001353 array_keys($this->qb_set),
1354 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001355 );
WanWizard7219c072011-12-28 14:09:05 +01001356
Kyle Farris0c147b32011-08-26 02:29:31 -04001357 if ($reset === TRUE)
1358 {
1359 $this->_reset_write();
1360 }
WanWizard7219c072011-12-28 14:09:05 +01001361
Kyle Farris0c147b32011-08-26 02:29:31 -04001362 return $sql;
1363 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001364
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 // --------------------------------------------------------------------
1366
1367 /**
1368 * Insert
1369 *
1370 * Compiles an insert string and runs the query
1371 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001372 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 * @param array an associative array of insert values
1374 * @return object
1375 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001376 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001377 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 if ( ! is_null($set))
1379 {
1380 $this->set($set);
1381 }
WanWizard7219c072011-12-28 14:09:05 +01001382
Kyle Farris0c147b32011-08-26 02:29:31 -04001383 if ($this->_validate_insert($table) === FALSE)
1384 {
1385 return FALSE;
1386 }
WanWizard7219c072011-12-28 14:09:05 +01001387
Kyle Farris76116012011-08-31 11:17:48 -04001388 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001389 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001390 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001391 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001392 array_keys($this->qb_set),
1393 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001394 );
Barry Mienydd671972010-10-04 16:33:58 +02001395
Kyle Farris0c147b32011-08-26 02:29:31 -04001396 $this->_reset_write();
1397 return $this->query($sql);
1398 }
WanWizard7219c072011-12-28 14:09:05 +01001399
Kyle Farris0c147b32011-08-26 02:29:31 -04001400 // --------------------------------------------------------------------
1401
1402 /**
1403 * Validate Insert
1404 *
1405 * This method is used by both insert() and get_compiled_insert() to
1406 * validate that the there data is actually being set and that table
1407 * has been chosen to be inserted into.
1408 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001409 * @param string the table to insert data into
1410 * @return string
1411 */
WanWizard7219c072011-12-28 14:09:05 +01001412 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001413 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001414 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001415 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001416 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 }
1418
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001419 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001420 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001421 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001422 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001423 elseif ( ! isset($this->qb_from[0]))
1424 {
1425 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1426 }
WanWizard7219c072011-12-28 14:09:05 +01001427
Kyle Farris0c147b32011-08-26 02:29:31 -04001428 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001429 }
Barry Mienydd671972010-10-04 16:33:58 +02001430
Phil Sturgeon9789f322011-07-15 15:14:05 -06001431 // --------------------------------------------------------------------
1432
1433 /**
1434 * Replace
1435 *
1436 * Compiles an replace into string and runs the query
1437 *
1438 * @param string the table to replace data into
1439 * @param array an associative array of insert values
1440 * @return object
1441 */
1442 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001443 {
1444 if ( ! is_null($set))
1445 {
1446 $this->set($set);
1447 }
Barry Mienydd671972010-10-04 16:33:58 +02001448
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001449 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001450 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001451 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001452 }
1453
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001454 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001455 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001456 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001457 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001458 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001459 }
Barry Mienydd671972010-10-04 16:33:58 +02001460
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001461 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001462 }
1463
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001464 $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 +00001465
Derek Jonesd10e8962010-03-02 17:10:36 -06001466 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001467 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001468 }
WanWizard7219c072011-12-28 14:09:05 +01001469
Kyle Farris0c147b32011-08-26 02:29:31 -04001470 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001471
Kyle Farris0c147b32011-08-26 02:29:31 -04001472 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001473 * Replace statement
1474 *
1475 * Generates a platform-specific replace string from the supplied data
1476 *
1477 * @param string the table name
1478 * @param array the insert keys
1479 * @param array the insert values
1480 * @return string
1481 */
1482 protected function _replace($table, $keys, $values)
1483 {
1484 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1485 }
1486
1487 // --------------------------------------------------------------------
1488
1489 /**
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001490 * From Tables
1491 *
1492 * This public function implicitly groups FROM tables so there is no confusion
1493 * about operator precedence in harmony with SQL standards
1494 *
1495 * @param array
1496 * @return string
1497 */
1498 protected function _from_tables($tables)
1499 {
1500 is_array($tables) OR $tables = array($tables);
1501
1502 return (count($tables) === 1) ? $tables[0] : '('.implode(', ', $tables).')';
1503 }
1504
1505 // --------------------------------------------------------------------
1506
1507 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001508 * Get UPDATE query string
1509 *
1510 * Compiles an update query and returns the sql
1511 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001512 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001513 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001514 * @return string
1515 */
1516 public function get_compiled_update($table = '', $reset = TRUE)
1517 {
1518 // Combine any cached components with the current statements
1519 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001520
Kyle Farris0c147b32011-08-26 02:29:31 -04001521 if ($this->_validate_update($table) === FALSE)
1522 {
1523 return FALSE;
1524 }
WanWizard7219c072011-12-28 14:09:05 +01001525
Andrey Andreevb0478652012-07-18 15:34:46 +03001526 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001527
Kyle Farris0c147b32011-08-26 02:29:31 -04001528 if ($reset === TRUE)
1529 {
1530 $this->_reset_write();
1531 }
WanWizard7219c072011-12-28 14:09:05 +01001532
Kyle Farris0c147b32011-08-26 02:29:31 -04001533 return $sql;
1534 }
WanWizard7219c072011-12-28 14:09:05 +01001535
Derek Allard2067d1a2008-11-13 22:59:24 +00001536 // --------------------------------------------------------------------
1537
1538 /**
1539 * Update
1540 *
1541 * Compiles an update string and runs the query
1542 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001543 * @param string the table to retrieve the results from
1544 * @param array an associative array of update values
1545 * @param mixed the where clause
1546 * @return object
1547 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001548 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001549 {
1550 // Combine any cached components with the current statements
1551 $this->_merge_cache();
1552
1553 if ( ! is_null($set))
1554 {
1555 $this->set($set);
1556 }
Barry Mienydd671972010-10-04 16:33:58 +02001557
Kyle Farris0c147b32011-08-26 02:29:31 -04001558 if ($this->_validate_update($table) === FALSE)
1559 {
1560 return FALSE;
1561 }
1562
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001563 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001564 {
1565 $this->where($where);
1566 }
1567
Andrey Andreev650b4c02012-06-11 12:07:15 +03001568 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001569 {
1570 $this->limit($limit);
1571 }
1572
Andrey Andreevb0478652012-07-18 15:34:46 +03001573 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
Jamie Rumbelow3b1355c2012-03-06 21:27:46 +00001574
Kyle Farris0c147b32011-08-26 02:29:31 -04001575 $this->_reset_write();
1576 return $this->query($sql);
1577 }
WanWizard7219c072011-12-28 14:09:05 +01001578
Kyle Farris0c147b32011-08-26 02:29:31 -04001579 // --------------------------------------------------------------------
1580
1581 /**
1582 * Validate Update
1583 *
1584 * This method is used by both update() and get_compiled_update() to
1585 * validate that data is actually being set and that a table has been
1586 * chosen to be update.
1587 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001588 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001589 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001590 */
1591 protected function _validate_update($table = '')
1592 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001593 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001594 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001595 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001596 }
1597
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001598 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001599 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001600 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001601 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001602 elseif ( ! isset($this->qb_from[0]))
1603 {
1604 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1605 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001606
1607 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001608 }
WanWizard7219c072011-12-28 14:09:05 +01001609
Derek Jonesd10e8962010-03-02 17:10:36 -06001610 // --------------------------------------------------------------------
1611
1612 /**
1613 * Update_Batch
1614 *
1615 * Compiles an update string and runs the query
1616 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001617 * @param string the table to retrieve the results from
1618 * @param array an associative array of update values
1619 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001620 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001621 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001622 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001623 {
1624 // Combine any cached components with the current statements
1625 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001626
Derek Jonesd10e8962010-03-02 17:10:36 -06001627 if (is_null($index))
1628 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001629 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001630 }
1631
1632 if ( ! is_null($set))
1633 {
1634 $this->set_update_batch($set, $index);
1635 }
1636
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001637 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001638 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001639 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001640 }
1641
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001642 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001643 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001644 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001645 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001646 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001647 }
Barry Mienydd671972010-10-04 16:33:58 +02001648
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001649 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001650 }
Barry Mienydd671972010-10-04 16:33:58 +02001651
Derek Jonesd10e8962010-03-02 17:10:36 -06001652 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001653 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001654 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001655 $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 -06001656 }
Barry Mienydd671972010-10-04 16:33:58 +02001657
Derek Jonesd10e8962010-03-02 17:10:36 -06001658 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001659 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001660 }
1661
1662 // --------------------------------------------------------------------
1663
1664 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001665 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001666 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001667 * @param array
1668 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001669 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001670 * @return object
1671 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001672 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001673 {
1674 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001675
Derek Jonesd10e8962010-03-02 17:10:36 -06001676 if ( ! is_array($key))
1677 {
1678 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001679 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001680
Andrey Andreevfe642da2012-06-16 03:47:33 +03001681 is_bool($escape) OR $escape = $this->_protect_identifiers;
1682
Derek Jonesd10e8962010-03-02 17:10:36 -06001683 foreach ($key as $k => $v)
1684 {
1685 $index_set = FALSE;
1686 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001687 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001688 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001689 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001690 {
1691 $index_set = TRUE;
1692 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001693
Andrey Andreevfe642da2012-06-16 03:47:33 +03001694 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001695 }
1696
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001697 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001698 {
1699 return $this->display_error('db_batch_missing_index');
1700 }
1701
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001702 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001703 }
Barry Mienydd671972010-10-04 16:33:58 +02001704
Derek Jonesd10e8962010-03-02 17:10:36 -06001705 return $this;
1706 }
1707
Derek Allard2067d1a2008-11-13 22:59:24 +00001708 // --------------------------------------------------------------------
1709
1710 /**
1711 * Empty Table
1712 *
1713 * Compiles a delete string and runs "DELETE FROM table"
1714 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001715 * @param string the table to empty
1716 * @return object
1717 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001718 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001719 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001720 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001721 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001722 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001723 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001724 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001725 }
1726
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001727 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001728 }
1729 else
1730 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001731 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001732 }
1733
1734 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001735 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001736 return $this->query($sql);
1737 }
1738
1739 // --------------------------------------------------------------------
1740
1741 /**
1742 * Truncate
1743 *
1744 * Compiles a truncate string and runs the query
1745 * If the database does not support the truncate() command
1746 * This function maps to "DELETE FROM table"
1747 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001748 * @param string the table to truncate
1749 * @return object
1750 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001751 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001752 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001753 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001754 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001755 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001756 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001757 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001758 }
1759
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001760 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001761 }
1762 else
1763 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001764 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001765 }
1766
1767 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001768 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001769 return $this->query($sql);
1770 }
WanWizard7219c072011-12-28 14:09:05 +01001771
Kyle Farris0c147b32011-08-26 02:29:31 -04001772 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001773
Kyle Farris0c147b32011-08-26 02:29:31 -04001774 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001775 * Truncate statement
1776 *
1777 * Generates a platform-specific truncate string from the supplied data
1778 *
1779 * If the database does not support the truncate() command,
1780 * then this method maps to 'DELETE FROM table'
1781 *
1782 * @param string the table name
1783 * @return string
1784 */
1785 protected function _truncate($table)
1786 {
1787 return 'TRUNCATE '.$table;
1788 }
1789
1790 // --------------------------------------------------------------------
1791
1792 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001793 * Get DELETE query string
1794 *
1795 * Compiles a delete query string and returns the sql
1796 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001797 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001798 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001799 * @return string
1800 */
1801 public function get_compiled_delete($table = '', $reset = TRUE)
1802 {
1803 $this->return_delete_sql = TRUE;
1804 $sql = $this->delete($table, '', NULL, $reset);
1805 $this->return_delete_sql = FALSE;
1806 return $sql;
1807 }
WanWizard7219c072011-12-28 14:09:05 +01001808
Derek Allard2067d1a2008-11-13 22:59:24 +00001809 // --------------------------------------------------------------------
1810
1811 /**
1812 * Delete
1813 *
1814 * Compiles a delete string and runs the query
1815 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001816 * @param mixed the table(s) to delete from. String or array
1817 * @param mixed the where clause
1818 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001819 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 * @return object
1821 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001822 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001823 {
1824 // Combine any cached components with the current statements
1825 $this->_merge_cache();
1826
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001827 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001828 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001829 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001830 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001831 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001832 }
1833
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001834 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001835 }
1836 elseif (is_array($table))
1837 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001838 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001839 {
1840 $this->delete($single_table, $where, $limit, FALSE);
1841 }
1842
1843 $this->_reset_write();
1844 return;
1845 }
1846 else
1847 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001848 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001849 }
1850
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001851 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001852 {
1853 $this->where($where);
1854 }
1855
Andrey Andreev650b4c02012-06-11 12:07:15 +03001856 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001857 {
1858 $this->limit($limit);
1859 }
1860
Andrey Andreev94611df2012-07-19 12:29:54 +03001861 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001862 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001863 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001864 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001865
Andrey Andreevb0478652012-07-18 15:34:46 +03001866 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001867 if ($reset_data)
1868 {
1869 $this->_reset_write();
1870 }
WanWizard7219c072011-12-28 14:09:05 +01001871
Andrey Andreev24276a32012-01-08 02:44:38 +02001872 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001873 }
WanWizard7219c072011-12-28 14:09:05 +01001874
Derek Allard2067d1a2008-11-13 22:59:24 +00001875 // --------------------------------------------------------------------
1876
1877 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001878 * Delete statement
1879 *
1880 * Generates a platform-specific delete string from the supplied data
1881 *
1882 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03001883 * @return string
1884 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001885 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03001886 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001887 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001888 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001889 }
1890
1891 // --------------------------------------------------------------------
1892
1893 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001894 * DB Prefix
1895 *
1896 * Prepends a database prefix if one exists in configuration
1897 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001898 * @param string the table
1899 * @return string
1900 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001901 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001902 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001903 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001904 {
1905 $this->display_error('db_table_name_required');
1906 }
1907
1908 return $this->dbprefix.$table;
1909 }
1910
1911 // --------------------------------------------------------------------
1912
1913 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001914 * Set DB Prefix
1915 *
1916 * Set's the DB Prefix to something new without needing to reconnect
1917 *
1918 * @param string the prefix
1919 * @return string
1920 */
1921 public function set_dbprefix($prefix = '')
1922 {
1923 return $this->dbprefix = $prefix;
1924 }
1925
1926 // --------------------------------------------------------------------
1927
1928 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001929 * Track Aliases
1930 *
1931 * Used to track SQL statements written with aliased tables.
1932 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001933 * @param string The table to inspect
1934 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001935 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001936 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001937 {
1938 if (is_array($table))
1939 {
1940 foreach ($table as $t)
1941 {
1942 $this->_track_aliases($t);
1943 }
1944 return;
1945 }
Barry Mienydd671972010-10-04 16:33:58 +02001946
Derek Jones37f4b9c2011-07-01 17:56:50 -05001947 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001948 // the string into discreet statements
1949 if (strpos($table, ',') !== FALSE)
1950 {
1951 return $this->_track_aliases(explode(',', $table));
1952 }
Barry Mienydd671972010-10-04 16:33:58 +02001953
Derek Allard2067d1a2008-11-13 22:59:24 +00001954 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001955 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001956 {
1957 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03001958 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001959
Derek Allard2067d1a2008-11-13 22:59:24 +00001960 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001961 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001962
Derek Allard2067d1a2008-11-13 22:59:24 +00001963 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001964 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001965 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001966 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001967 }
1968 }
1969 }
WanWizard7219c072011-12-28 14:09:05 +01001970
Derek Allard2067d1a2008-11-13 22:59:24 +00001971 // --------------------------------------------------------------------
1972
1973 /**
1974 * Compile the SELECT statement
1975 *
1976 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001977 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001978 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001979 * @return string
1980 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001981 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001982 {
1983 // Combine any cached components with the current statements
1984 $this->_merge_cache();
1985
Derek Allard2067d1a2008-11-13 22:59:24 +00001986 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001987 if ($select_override !== FALSE)
1988 {
1989 $sql = $select_override;
1990 }
1991 else
1992 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001993 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001994
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001995 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001996 {
Barry Mienydd671972010-10-04 16:33:58 +02001997 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001998 }
1999 else
Barry Mienydd671972010-10-04 16:33:58 +02002000 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002001 // Cycle through the "select" portion of the query and prep each column name.
2002 // The reason we protect identifiers here rather then in the select() function
2003 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002004 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002005 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002006 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002007 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002008 }
Barry Mienydd671972010-10-04 16:33:58 +02002009
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002010 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002011 }
2012 }
2013
Derek Allard2067d1a2008-11-13 22:59:24 +00002014 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002015 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002016 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002017 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002018 }
2019
Derek Allard2067d1a2008-11-13 22:59:24 +00002020 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002021 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002022 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002023 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002024 }
2025
Andrey Andreevd40459d2012-07-18 16:46:39 +03002026 // WHERE
2027 $sql .= $this->_compile_wh('qb_where');
Derek Allard2067d1a2008-11-13 22:59:24 +00002028
Andrey Andreevb0478652012-07-18 15:34:46 +03002029 // GROUP BY
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002030 $sql .= $this->_compile_group_by();
Andrey Andreevb0478652012-07-18 15:34:46 +03002031
2032 // HAVING
Andrey Andreevd40459d2012-07-18 16:46:39 +03002033 $sql .= $this->_compile_wh('qb_having');
Andrey Andreevb0478652012-07-18 15:34:46 +03002034
2035 // ORDER BY
2036 if (count($this->qb_orderby) > 0)
2037 {
2038 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
2039 }
2040
Andrey Andreevd40459d2012-07-18 16:46:39 +03002041 // LIMIT
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002042 if ($this->qb_limit)
Derek Allard2067d1a2008-11-13 22:59:24 +00002043 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002044 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002045 }
2046
2047 return $sql;
2048 }
2049
2050 // --------------------------------------------------------------------
2051
2052 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002053 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002054 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002055 * Escapes identifiers in WHERE and HAVING statements at execution time.
2056 *
Andrey Andreevb0478652012-07-18 15:34:46 +03002057 * Required so that aliases are tracked properly, regardless of wether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002058 * where(), or_where(), having(), or_having are called prior to from(),
2059 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002060 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002061 * @param string 'qb_where' or 'qb_having'
2062 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002063 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002064 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002065 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002066 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002067 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002068 $sql = ($qb_key === 'qb_having') ? "\nHAVING " : "\nWHERE ";
Andrey Andreev6e704752012-07-18 00:46:33 +03002069
Andrey Andreevd40459d2012-07-18 16:46:39 +03002070 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002071 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002072 if ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002073 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002074 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002075 continue;
2076 }
2077
Andrey Andreevd40459d2012-07-18 16:46:39 +03002078 $op = preg_quote($this->_get_operator($this->{$qb_key}[$i]['condition']));
2079 if ( ! preg_match('/^(\s*(?:AND|OR)\s+)?(\(?)(.*)('.$op.')(.*(?<!\)))?(\)?)$/i', $this->{$qb_key}[$i]['condition'], $matches))
Andrey Andreev6e704752012-07-18 00:46:33 +03002080 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002081 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002082 continue;
2083 }
2084
2085 // $matches = array(
2086 // 0 => 'OR (test <= foo)', /* the whole thing */
2087 // 1 => 'OR ', /* optional */
2088 // 2 => '(', /* optional */
2089 // 3 => 'test', /* the field name */
2090 // 4 => ' <= ', /* $op */
2091 // 5 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2092 // 6 => ')' /* optional */
2093 // );
2094 empty($matches[5]) OR $matches[5] = ' '.$this->protect_identifiers(trim($matches[5]));
Andrey Andreevd40459d2012-07-18 16:46:39 +03002095 $this->{$qb_key}[$i] = $matches[1].$matches[2].$this->protect_identifiers(trim($matches[3]))
Andrey Andreev6e704752012-07-18 00:46:33 +03002096 .' '.trim($matches[4]).$matches[5].$matches[6];
2097 }
2098
Andrey Andreevd40459d2012-07-18 16:46:39 +03002099 return implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002100 }
2101
Andrey Andreevb0478652012-07-18 15:34:46 +03002102 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002103 }
2104
2105 // --------------------------------------------------------------------
2106
2107 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002108 * Compile GROUP BY
2109 *
2110 * Escapes identifiers in GROUP BY statements at execution time.
2111 *
2112 * Required so that aliases are tracked properly, regardless of wether
2113 * group_by() is called prior to from(), join() and dbprefix is added
2114 * only if needed.
2115 *
2116 * @return string SQL statement
2117 */
2118 protected function _compile_group_by()
2119 {
2120 if (count($this->qb_groupby) > 0)
2121 {
2122 $sql = "\nGROUP BY ";
2123
Andrey Andreev96feb582012-07-19 13:12:34 +03002124 for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
2125 {
2126 $this->qb_groupby[$i] = ($this->qb_groupby[$i]['escape'] === FALSE)
2127 ? $this->qb_groupby[$i]['field']
2128 : $this->protect_identifiers($qb_groupby[$i]['field']);
2129 }
2130
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002131 $sql .= implode(', ', $this->qb_groupby);
2132 }
2133
2134 return '';
2135 }
2136
2137 // --------------------------------------------------------------------
2138
2139 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002140 * Object to Array
2141 *
2142 * Takes an object as input and converts the class variables to array key/vals
2143 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002144 * @param object
2145 * @return array
2146 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002147 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002148 {
2149 if ( ! is_object($object))
2150 {
2151 return $object;
2152 }
Barry Mienydd671972010-10-04 16:33:58 +02002153
Derek Allard2067d1a2008-11-13 22:59:24 +00002154 $array = array();
2155 foreach (get_object_vars($object) as $key => $val)
2156 {
2157 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002158 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002159 {
2160 $array[$key] = $val;
2161 }
2162 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002163
2164 return $array;
2165 }
Barry Mienydd671972010-10-04 16:33:58 +02002166
Derek Jonesd10e8962010-03-02 17:10:36 -06002167 // --------------------------------------------------------------------
2168
2169 /**
2170 * Object to Array
2171 *
2172 * Takes an object as input and converts the class variables to array key/vals
2173 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002174 * @param object
2175 * @return array
2176 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002177 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002178 {
2179 if ( ! is_object($object))
2180 {
2181 return $object;
2182 }
Barry Mienydd671972010-10-04 16:33:58 +02002183
Derek Jonesd10e8962010-03-02 17:10:36 -06002184 $array = array();
2185 $out = get_object_vars($object);
2186 $fields = array_keys($out);
2187
2188 foreach ($fields as $val)
2189 {
2190 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002191 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002192 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002193 $i = 0;
2194 foreach ($out[$val] as $data)
2195 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002196 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002197 }
2198 }
2199 }
2200
Derek Allard2067d1a2008-11-13 22:59:24 +00002201 return $array;
2202 }
Barry Mienydd671972010-10-04 16:33:58 +02002203
Derek Allard2067d1a2008-11-13 22:59:24 +00002204 // --------------------------------------------------------------------
2205
2206 /**
2207 * Start Cache
2208 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002209 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002210 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002211 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002212 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002213 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002214 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002215 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002216 }
2217
2218 // --------------------------------------------------------------------
2219
2220 /**
2221 * Stop Cache
2222 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002223 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002224 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002225 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002226 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002227 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002228 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002229 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002230 }
2231
2232 // --------------------------------------------------------------------
2233
2234 /**
2235 * Flush Cache
2236 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002237 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002238 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002239 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002240 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002241 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002242 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002243 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002244 'qb_cache_select' => array(),
2245 'qb_cache_from' => array(),
2246 'qb_cache_join' => array(),
2247 'qb_cache_where' => array(),
2248 'qb_cache_like' => array(),
2249 'qb_cache_groupby' => array(),
2250 'qb_cache_having' => array(),
2251 'qb_cache_orderby' => array(),
2252 'qb_cache_set' => array(),
2253 'qb_cache_exists' => array(),
2254 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002255 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002256 }
2257
2258 // --------------------------------------------------------------------
2259
2260 /**
2261 * Merge Cache
2262 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002263 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002264 * locally called ones.
2265 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002266 * @return void
2267 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002268 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002269 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002270 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002271 {
2272 return;
2273 }
2274
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002275 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002276 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002277 $qb_variable = 'qb_'.$val;
2278 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002279
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002280 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002281 {
2282 continue;
2283 }
2284
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002285 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002286 }
2287
2288 // If we are "protecting identifiers" we need to examine the "from"
2289 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002290 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002291 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002292 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002293 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002294
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002295 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002296 }
WanWizard7219c072011-12-28 14:09:05 +01002297
Kyle Farris0c147b32011-08-26 02:29:31 -04002298 // --------------------------------------------------------------------
2299
2300 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002301 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002302 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002303 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002304 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002305 * @return void
2306 */
2307 public function reset_query()
2308 {
2309 $this->_reset_select();
2310 $this->_reset_write();
2311 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002312
2313 // --------------------------------------------------------------------
2314
2315 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002316 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002317 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002318 * @param array An array of fields to reset
2319 * @return void
2320 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002321 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002322 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002323 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002324 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002325 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002326 {
2327 $this->$item = $default_value;
2328 }
2329 }
2330 }
2331
2332 // --------------------------------------------------------------------
2333
2334 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002335 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002336 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002337 * @return void
2338 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002339 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002340 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002341 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002342 'qb_select' => array(),
2343 'qb_from' => array(),
2344 'qb_join' => array(),
2345 'qb_where' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002346 'qb_groupby' => array(),
2347 'qb_having' => array(),
2348 'qb_orderby' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002349 'qb_aliased_tables' => array(),
2350 'qb_no_escape' => array(),
2351 'qb_distinct' => FALSE,
2352 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002353 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002354 )
2355 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002356 }
Barry Mienydd671972010-10-04 16:33:58 +02002357
Derek Allard2067d1a2008-11-13 22:59:24 +00002358 // --------------------------------------------------------------------
2359
2360 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002361 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002362 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002363 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002364 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002365 * @return void
2366 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002367 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002368 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002369 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002370 'qb_set' => array(),
2371 'qb_from' => array(),
2372 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002373 'qb_orderby' => array(),
2374 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002375 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002376 )
2377 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002378 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002379
Derek Allard2067d1a2008-11-13 22:59:24 +00002380}
2381
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002382/* End of file DB_query_builder.php */
Andrey Andreev58803fb2012-06-24 00:45:37 +03002383/* Location: ./system/database/DB_query_builder.php */