blob: 49592840b73e8b8ccf00cf2d1a0cae0a1690695f [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 Andreev0bcf5902012-10-12 13:03:29 +0300923 public function having($key, $value = NULL, $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 Andreev0bcf5902012-10-12 13:03:29 +0300940 public function or_having($key, $value = NULL, $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
Andrey Andreev2d486232012-07-19 14:46:51 +0300951 * @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 Andreev2d486232012-07-19 14:46:51 +0300957 $direction = trim($direction);
958
959 if (strtolower($direction) === 'random' OR $orderby === $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300961 // Random ordered results don't need a field name
962 $orderby = $this->_random_keyword;
963 $direction = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 }
Andrey Andreev2d486232012-07-19 14:46:51 +0300965 elseif (empty($orderby))
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300967 return $this;
968 }
969 elseif ($direction !== '')
970 {
971 $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 }
Barry Mienydd671972010-10-04 16:33:58 +0200973
Andrey Andreevd24160c2012-06-16 03:21:20 +0300974 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +0200975
Andrey Andreev2d486232012-07-19 14:46:51 +0300976 if ($escape === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300978 $qb_orderby[] = array(array('field' => $orderby, 'direction' => $direction, $escape => FALSE));
979 }
980 else
981 {
982 $qb_orderby = array();
983 foreach (explode(',', $orderby) as $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300985 $qb_orderby[] = ($direction === '' && preg_match('/\s+(ASC|DESC)$/i', rtrim($field), $match, PREG_OFFSET_CAPTURE))
986 ? array('field' => ltrim(substr($field, 0, $match[0][1])), 'direction' => ' '.$match[1][0], 'escape' => TRUE)
987 : array('field' => trim($field), 'direction' => $direction, 'escape' => TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 }
Barry Mienydd671972010-10-04 16:33:58 +0200990
Andrey Andreev2d486232012-07-19 14:46:51 +0300991 $this->qb_orderby = array_merge($this->qb_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000992 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300994 $this->qb_cache_orderby = array_merge($this->qb_cache_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000995 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 }
997
998 return $this;
999 }
Barry Mienydd671972010-10-04 16:33:58 +02001000
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 // --------------------------------------------------------------------
1002
1003 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 * Sets the LIMIT value
1005 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001006 * @param int the limit value
1007 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 * @return object
1009 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001010 public function limit($value, $offset = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001012 is_null($value) OR $this->qb_limit = (int) $value;
1013 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001014
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 return $this;
1016 }
Barry Mienydd671972010-10-04 16:33:58 +02001017
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 // --------------------------------------------------------------------
1019
1020 /**
1021 * Sets the OFFSET value
1022 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001023 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 * @return object
1025 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001026 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001028 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 return $this;
1030 }
Barry Mienydd671972010-10-04 16:33:58 +02001031
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 // --------------------------------------------------------------------
1033
1034 /**
Andrey Andreev2c35b642012-06-24 03:05:26 +03001035 * Limit string
1036 *
1037 * Generates a platform-specific LIMIT clause
1038 *
1039 * @param string the sql query string
Andrey Andreev2c35b642012-06-24 03:05:26 +03001040 * @return string
1041 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001042 protected function _limit($sql)
Andrey Andreev2c35b642012-06-24 03:05:26 +03001043 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001044 return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').$this->qb_limit;
Andrey Andreev2c35b642012-06-24 03:05:26 +03001045 }
1046
1047 // --------------------------------------------------------------------
1048
1049 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001050 * The "set" function.
1051 *
1052 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001054 * @param mixed
1055 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001056 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 * @return object
1058 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001059 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 {
1061 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001062
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 if ( ! is_array($key))
1064 {
1065 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001066 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001067
Andrey Andreevfe642da2012-06-16 03:47:33 +03001068 is_bool($escape) OR $escape = $this->_protect_identifiers;
1069
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 foreach ($key as $k => $v)
1071 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001072 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1073 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 }
Barry Mienydd671972010-10-04 16:33:58 +02001075
Derek Allard2067d1a2008-11-13 22:59:24 +00001076 return $this;
1077 }
WanWizard7219c072011-12-28 14:09:05 +01001078
Kyle Farris0c147b32011-08-26 02:29:31 -04001079 // --------------------------------------------------------------------
1080
1081 /**
1082 * Get SELECT query string
1083 *
1084 * Compiles a SELECT query string and returns the sql.
1085 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001086 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001087 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001088 * @return string
1089 */
WanWizard7219c072011-12-28 14:09:05 +01001090 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001091 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001092 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001093 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001094 $this->_track_aliases($table);
1095 $this->from($table);
1096 }
WanWizard7219c072011-12-28 14:09:05 +01001097
Andrey Andreev650b4c02012-06-11 12:07:15 +03001098 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001099
Kyle Farris0c147b32011-08-26 02:29:31 -04001100 if ($reset === TRUE)
1101 {
1102 $this->_reset_select();
1103 }
WanWizard7219c072011-12-28 14:09:05 +01001104
Kyle Farris0c147b32011-08-26 02:29:31 -04001105 return $select;
1106 }
WanWizard7219c072011-12-28 14:09:05 +01001107
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 // --------------------------------------------------------------------
1109
1110 /**
1111 * Get
1112 *
1113 * Compiles the select statement based on the other functions called
1114 * and runs the query
1115 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 * @param string the table
1117 * @param string the limit clause
1118 * @param string the offset clause
1119 * @return object
1120 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001121 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001123 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 {
1125 $this->_track_aliases($table);
1126 $this->from($table);
1127 }
Barry Mienydd671972010-10-04 16:33:58 +02001128
Andrey Andreev650b4c02012-06-11 12:07:15 +03001129 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 {
1131 $this->limit($limit, $offset);
1132 }
Barry Mienydd671972010-10-04 16:33:58 +02001133
Andrey Andreev24276a32012-01-08 02:44:38 +02001134 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 $this->_reset_select();
1136 return $result;
1137 }
1138
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001139 // --------------------------------------------------------------------
1140
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 /**
1142 * "Count All Results" query
1143 *
Barry Mienydd671972010-10-04 16:33:58 +02001144 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001145 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 * @param string
1148 * @return string
1149 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001150 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001152 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 {
1154 $this->_track_aliases($table);
1155 $this->from($table);
1156 }
Barry Mienydd671972010-10-04 16:33:58 +02001157
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001158 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001160
Purwandi1d160e72012-01-09 16:33:28 +07001161 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001163 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 }
1165
Purwandi1d160e72012-01-09 16:33:28 +07001166 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001167 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001169
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 // --------------------------------------------------------------------
1171
1172 /**
1173 * Get_Where
1174 *
1175 * Allows the where clause, limit and offset to be added directly
1176 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 * @param string the where clause
1178 * @param string the limit clause
1179 * @param string the offset clause
1180 * @return object
1181 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001182 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001184 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 {
1186 $this->from($table);
1187 }
1188
1189 if ( ! is_null($where))
1190 {
1191 $this->where($where);
1192 }
Barry Mienydd671972010-10-04 16:33:58 +02001193
Andrey Andreev650b4c02012-06-11 12:07:15 +03001194 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 {
1196 $this->limit($limit, $offset);
1197 }
Barry Mienydd671972010-10-04 16:33:58 +02001198
Andrey Andreev24276a32012-01-08 02:44:38 +02001199 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 $this->_reset_select();
1201 return $result;
1202 }
1203
1204 // --------------------------------------------------------------------
1205
1206 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001207 * Insert_Batch
1208 *
1209 * Compiles batch insert strings and runs the queries
1210 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001211 * @param string the table to retrieve the results from
1212 * @param array an associative array of insert values
1213 * @return object
1214 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001215 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001216 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001217 if ( ! is_null($set))
1218 {
1219 $this->set_insert_batch($set);
1220 }
Barry Mienydd671972010-10-04 16:33:58 +02001221
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001222 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001223 {
1224 if ($this->db_debug)
1225 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001226 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001227 return $this->display_error('db_must_use_set');
1228 }
1229 return FALSE;
1230 }
1231
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001232 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001233 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001234 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001235 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001236 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001237 }
Barry Mienydd671972010-10-04 16:33:58 +02001238
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001239 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001240 }
1241
1242 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001243 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001244 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001245 $this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, 100)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001246 }
Barry Mienydd671972010-10-04 16:33:58 +02001247
Derek Jonesd10e8962010-03-02 17:10:36 -06001248 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001249 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001250 }
1251
1252 // --------------------------------------------------------------------
1253
1254 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001255 * Insert_batch statement
1256 *
1257 * Generates a platform-specific insert string from the supplied data.
1258 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001259 * @param string the table name
1260 * @param array the insert keys
1261 * @param array the insert values
1262 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001263 */
1264 protected function _insert_batch($table, $keys, $values)
1265 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001266 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001267 }
1268
1269 // --------------------------------------------------------------------
1270
1271 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001272 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001273 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001274 * @param mixed
1275 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001276 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001277 * @return object
1278 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001279 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001280 {
1281 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001282
Derek Jonesd10e8962010-03-02 17:10:36 -06001283 if ( ! is_array($key))
1284 {
1285 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001286 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001287
Andrey Andreevfe642da2012-06-16 03:47:33 +03001288 is_bool($escape) OR $escape = $this->_protect_identifiers;
1289
Iban Eguia3c0a4522012-04-15 13:30:44 +02001290 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001291 sort($keys);
1292
1293 foreach ($key as $row)
1294 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001295 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001296 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1297 {
1298 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001299 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001300 return;
1301 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001302
Barry Mienydd671972010-10-04 16:33:58 +02001303 ksort($row); // puts $row in the same order as our keys
1304
Andrey Andreev650b4c02012-06-11 12:07:15 +03001305 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001306 {
1307 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001308 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001309 {
Barry Mienydd671972010-10-04 16:33:58 +02001310 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001311 }
1312
Andrey Andreev650b4c02012-06-11 12:07:15 +03001313 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001314 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001315
1316 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001317 }
1318
1319 foreach ($keys as $k)
1320 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001321 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001322 }
Barry Mienydd671972010-10-04 16:33:58 +02001323
Derek Jonesd10e8962010-03-02 17:10:36 -06001324 return $this;
1325 }
WanWizard7219c072011-12-28 14:09:05 +01001326
Kyle Farris0c147b32011-08-26 02:29:31 -04001327 // --------------------------------------------------------------------
1328
1329 /**
1330 * Get INSERT query string
1331 *
1332 * Compiles an insert query and returns the sql
1333 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001334 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001335 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001336 * @return string
1337 */
1338 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001339 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001340 if ($this->_validate_insert($table) === FALSE)
1341 {
1342 return FALSE;
1343 }
WanWizard7219c072011-12-28 14:09:05 +01001344
Kyle Farris76116012011-08-31 11:17:48 -04001345 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001346 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001347 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001348 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001349 array_keys($this->qb_set),
1350 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001351 );
WanWizard7219c072011-12-28 14:09:05 +01001352
Kyle Farris0c147b32011-08-26 02:29:31 -04001353 if ($reset === TRUE)
1354 {
1355 $this->_reset_write();
1356 }
WanWizard7219c072011-12-28 14:09:05 +01001357
Kyle Farris0c147b32011-08-26 02:29:31 -04001358 return $sql;
1359 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001360
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 // --------------------------------------------------------------------
1362
1363 /**
1364 * Insert
1365 *
1366 * Compiles an insert string and runs the query
1367 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001368 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001369 * @param array an associative array of insert values
1370 * @return object
1371 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001372 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001373 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001374 if ( ! is_null($set))
1375 {
1376 $this->set($set);
1377 }
WanWizard7219c072011-12-28 14:09:05 +01001378
Kyle Farris0c147b32011-08-26 02:29:31 -04001379 if ($this->_validate_insert($table) === FALSE)
1380 {
1381 return FALSE;
1382 }
WanWizard7219c072011-12-28 14:09:05 +01001383
Kyle Farris76116012011-08-31 11:17:48 -04001384 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001385 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001386 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001387 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001388 array_keys($this->qb_set),
1389 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001390 );
Barry Mienydd671972010-10-04 16:33:58 +02001391
Kyle Farris0c147b32011-08-26 02:29:31 -04001392 $this->_reset_write();
1393 return $this->query($sql);
1394 }
WanWizard7219c072011-12-28 14:09:05 +01001395
Kyle Farris0c147b32011-08-26 02:29:31 -04001396 // --------------------------------------------------------------------
1397
1398 /**
1399 * Validate Insert
1400 *
1401 * This method is used by both insert() and get_compiled_insert() to
1402 * validate that the there data is actually being set and that table
1403 * has been chosen to be inserted into.
1404 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001405 * @param string the table to insert data into
1406 * @return string
1407 */
WanWizard7219c072011-12-28 14:09:05 +01001408 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001409 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001410 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001411 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001412 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 }
1414
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001415 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001416 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001417 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001418 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001419 elseif ( ! isset($this->qb_from[0]))
1420 {
1421 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1422 }
WanWizard7219c072011-12-28 14:09:05 +01001423
Kyle Farris0c147b32011-08-26 02:29:31 -04001424 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001425 }
Barry Mienydd671972010-10-04 16:33:58 +02001426
Phil Sturgeon9789f322011-07-15 15:14:05 -06001427 // --------------------------------------------------------------------
1428
1429 /**
1430 * Replace
1431 *
1432 * Compiles an replace into string and runs the query
1433 *
1434 * @param string the table to replace data into
1435 * @param array an associative array of insert values
1436 * @return object
1437 */
1438 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001439 {
1440 if ( ! is_null($set))
1441 {
1442 $this->set($set);
1443 }
Barry Mienydd671972010-10-04 16:33:58 +02001444
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001445 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001446 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001447 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001448 }
1449
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001450 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001451 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001452 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001453 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001454 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001455 }
Barry Mienydd671972010-10-04 16:33:58 +02001456
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001457 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001458 }
1459
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001460 $sql = $this->_replace($this->protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->qb_set), array_values($this->qb_set));
Jamie Rumbelow3b1355c2012-03-06 21:27:46 +00001461
Derek Jonesd10e8962010-03-02 17:10:36 -06001462 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001463 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001464 }
WanWizard7219c072011-12-28 14:09:05 +01001465
Kyle Farris0c147b32011-08-26 02:29:31 -04001466 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001467
Kyle Farris0c147b32011-08-26 02:29:31 -04001468 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001469 * Replace statement
1470 *
1471 * Generates a platform-specific replace string from the supplied data
1472 *
1473 * @param string the table name
1474 * @param array the insert keys
1475 * @param array the insert values
1476 * @return string
1477 */
1478 protected function _replace($table, $keys, $values)
1479 {
1480 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1481 }
1482
1483 // --------------------------------------------------------------------
1484
1485 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001486 * FROM tables
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001487 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001488 * Groups tables in FROM clauses if needed, so there is no confusion
1489 * about operator precedence.
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001490 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001491 * Note: This is only used (and overriden) by MySQL and CUBRID.
1492 *
1493 * @return string
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001494 */
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001495 protected function _from_tables()
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001496 {
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001497 return implode(', ', $this->qb_from);
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001498 }
1499
1500 // --------------------------------------------------------------------
1501
1502 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001503 * Get UPDATE query string
1504 *
1505 * Compiles an update query and returns the sql
1506 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001507 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001508 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001509 * @return string
1510 */
1511 public function get_compiled_update($table = '', $reset = TRUE)
1512 {
1513 // Combine any cached components with the current statements
1514 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001515
Kyle Farris0c147b32011-08-26 02:29:31 -04001516 if ($this->_validate_update($table) === FALSE)
1517 {
1518 return FALSE;
1519 }
WanWizard7219c072011-12-28 14:09:05 +01001520
Andrey Andreevb0478652012-07-18 15:34:46 +03001521 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001522
Kyle Farris0c147b32011-08-26 02:29:31 -04001523 if ($reset === TRUE)
1524 {
1525 $this->_reset_write();
1526 }
WanWizard7219c072011-12-28 14:09:05 +01001527
Kyle Farris0c147b32011-08-26 02:29:31 -04001528 return $sql;
1529 }
WanWizard7219c072011-12-28 14:09:05 +01001530
Derek Allard2067d1a2008-11-13 22:59:24 +00001531 // --------------------------------------------------------------------
1532
1533 /**
1534 * Update
1535 *
1536 * Compiles an update string and runs the query
1537 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001538 * @param string the table to retrieve the results from
1539 * @param array an associative array of update values
1540 * @param mixed the where clause
1541 * @return object
1542 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001543 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001544 {
1545 // Combine any cached components with the current statements
1546 $this->_merge_cache();
1547
1548 if ( ! is_null($set))
1549 {
1550 $this->set($set);
1551 }
Barry Mienydd671972010-10-04 16:33:58 +02001552
Kyle Farris0c147b32011-08-26 02:29:31 -04001553 if ($this->_validate_update($table) === FALSE)
1554 {
1555 return FALSE;
1556 }
1557
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001558 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001559 {
1560 $this->where($where);
1561 }
1562
Andrey Andreev650b4c02012-06-11 12:07:15 +03001563 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001564 {
1565 $this->limit($limit);
1566 }
1567
Andrey Andreevb0478652012-07-18 15:34:46 +03001568 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
Jamie Rumbelow3b1355c2012-03-06 21:27:46 +00001569
Kyle Farris0c147b32011-08-26 02:29:31 -04001570 $this->_reset_write();
1571 return $this->query($sql);
1572 }
WanWizard7219c072011-12-28 14:09:05 +01001573
Kyle Farris0c147b32011-08-26 02:29:31 -04001574 // --------------------------------------------------------------------
1575
1576 /**
1577 * Validate Update
1578 *
1579 * This method is used by both update() and get_compiled_update() to
1580 * validate that data is actually being set and that a table has been
1581 * chosen to be update.
1582 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001583 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001584 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001585 */
1586 protected function _validate_update($table = '')
1587 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001588 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001589 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001590 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001591 }
1592
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001593 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001594 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001595 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001596 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001597 elseif ( ! isset($this->qb_from[0]))
1598 {
1599 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1600 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001601
1602 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001603 }
WanWizard7219c072011-12-28 14:09:05 +01001604
Derek Jonesd10e8962010-03-02 17:10:36 -06001605 // --------------------------------------------------------------------
1606
1607 /**
1608 * Update_Batch
1609 *
1610 * Compiles an update string and runs the query
1611 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001612 * @param string the table to retrieve the results from
1613 * @param array an associative array of update values
1614 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001615 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001616 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001617 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001618 {
1619 // Combine any cached components with the current statements
1620 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001621
Derek Jonesd10e8962010-03-02 17:10:36 -06001622 if (is_null($index))
1623 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001624 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001625 }
1626
1627 if ( ! is_null($set))
1628 {
1629 $this->set_update_batch($set, $index);
1630 }
1631
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001632 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001633 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001634 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001635 }
1636
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001637 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001638 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001639 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001640 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001641 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001642 }
Barry Mienydd671972010-10-04 16:33:58 +02001643
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001644 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001645 }
Barry Mienydd671972010-10-04 16:33:58 +02001646
Derek Jonesd10e8962010-03-02 17:10:36 -06001647 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001648 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001649 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001650 $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 -06001651 }
Barry Mienydd671972010-10-04 16:33:58 +02001652
Derek Jonesd10e8962010-03-02 17:10:36 -06001653 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001654 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001655 }
1656
1657 // --------------------------------------------------------------------
1658
1659 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001660 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001661 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001662 * @param array
1663 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001664 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001665 * @return object
1666 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001667 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001668 {
1669 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001670
Derek Jonesd10e8962010-03-02 17:10:36 -06001671 if ( ! is_array($key))
1672 {
1673 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001674 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001675
Andrey Andreevfe642da2012-06-16 03:47:33 +03001676 is_bool($escape) OR $escape = $this->_protect_identifiers;
1677
Derek Jonesd10e8962010-03-02 17:10:36 -06001678 foreach ($key as $k => $v)
1679 {
1680 $index_set = FALSE;
1681 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001682 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001683 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001684 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001685 {
1686 $index_set = TRUE;
1687 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001688
Andrey Andreevfe642da2012-06-16 03:47:33 +03001689 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001690 }
1691
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001692 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001693 {
1694 return $this->display_error('db_batch_missing_index');
1695 }
1696
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001697 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001698 }
Barry Mienydd671972010-10-04 16:33:58 +02001699
Derek Jonesd10e8962010-03-02 17:10:36 -06001700 return $this;
1701 }
1702
Derek Allard2067d1a2008-11-13 22:59:24 +00001703 // --------------------------------------------------------------------
1704
1705 /**
1706 * Empty Table
1707 *
1708 * Compiles a delete string and runs "DELETE FROM table"
1709 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001710 * @param string the table to empty
1711 * @return object
1712 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001713 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001714 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001715 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001716 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001717 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001718 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001719 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001720 }
1721
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001722 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001723 }
1724 else
1725 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001726 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001727 }
1728
1729 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001730 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 return $this->query($sql);
1732 }
1733
1734 // --------------------------------------------------------------------
1735
1736 /**
1737 * Truncate
1738 *
1739 * Compiles a truncate string and runs the query
1740 * If the database does not support the truncate() command
1741 * This function maps to "DELETE FROM table"
1742 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001743 * @param string the table to truncate
1744 * @return object
1745 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001746 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001747 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001748 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001750 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001751 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001752 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001753 }
1754
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001755 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001756 }
1757 else
1758 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001759 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001760 }
1761
1762 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001763 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001764 return $this->query($sql);
1765 }
WanWizard7219c072011-12-28 14:09:05 +01001766
Kyle Farris0c147b32011-08-26 02:29:31 -04001767 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001768
Kyle Farris0c147b32011-08-26 02:29:31 -04001769 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001770 * Truncate statement
1771 *
1772 * Generates a platform-specific truncate string from the supplied data
1773 *
1774 * If the database does not support the truncate() command,
1775 * then this method maps to 'DELETE FROM table'
1776 *
1777 * @param string the table name
1778 * @return string
1779 */
1780 protected function _truncate($table)
1781 {
1782 return 'TRUNCATE '.$table;
1783 }
1784
1785 // --------------------------------------------------------------------
1786
1787 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001788 * Get DELETE query string
1789 *
1790 * Compiles a delete query string and returns the sql
1791 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001792 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001793 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001794 * @return string
1795 */
1796 public function get_compiled_delete($table = '', $reset = TRUE)
1797 {
1798 $this->return_delete_sql = TRUE;
1799 $sql = $this->delete($table, '', NULL, $reset);
1800 $this->return_delete_sql = FALSE;
1801 return $sql;
1802 }
WanWizard7219c072011-12-28 14:09:05 +01001803
Derek Allard2067d1a2008-11-13 22:59:24 +00001804 // --------------------------------------------------------------------
1805
1806 /**
1807 * Delete
1808 *
1809 * Compiles a delete string and runs the query
1810 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001811 * @param mixed the table(s) to delete from. String or array
1812 * @param mixed the where clause
1813 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001814 * @param bool
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001815 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00001816 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001817 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001818 {
1819 // Combine any cached components with the current statements
1820 $this->_merge_cache();
1821
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001822 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001823 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001824 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001825 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001826 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 }
1828
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001829 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001830 }
1831 elseif (is_array($table))
1832 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001833 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001834 {
Andrey Andreev13f50542012-10-12 12:31:02 +03001835 $this->delete($single_table, $where, $limit, $reset_data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001836 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001837 return;
1838 }
1839 else
1840 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001841 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001842 }
1843
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001844 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001845 {
1846 $this->where($where);
1847 }
1848
Andrey Andreev650b4c02012-06-11 12:07:15 +03001849 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001850 {
1851 $this->limit($limit);
1852 }
1853
Andrey Andreev94611df2012-07-19 12:29:54 +03001854 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001855 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001856 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001857 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001858
Andrey Andreevb0478652012-07-18 15:34:46 +03001859 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001860 if ($reset_data)
1861 {
1862 $this->_reset_write();
1863 }
WanWizard7219c072011-12-28 14:09:05 +01001864
Andrey Andreev24276a32012-01-08 02:44:38 +02001865 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001866 }
WanWizard7219c072011-12-28 14:09:05 +01001867
Derek Allard2067d1a2008-11-13 22:59:24 +00001868 // --------------------------------------------------------------------
1869
1870 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001871 * Delete statement
1872 *
1873 * Generates a platform-specific delete string from the supplied data
1874 *
1875 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03001876 * @return string
1877 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001878 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03001879 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001880 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001881 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001882 }
1883
1884 // --------------------------------------------------------------------
1885
1886 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001887 * DB Prefix
1888 *
1889 * Prepends a database prefix if one exists in configuration
1890 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001891 * @param string the table
1892 * @return string
1893 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001894 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001895 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001896 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001897 {
1898 $this->display_error('db_table_name_required');
1899 }
1900
1901 return $this->dbprefix.$table;
1902 }
1903
1904 // --------------------------------------------------------------------
1905
1906 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001907 * Set DB Prefix
1908 *
1909 * Set's the DB Prefix to something new without needing to reconnect
1910 *
1911 * @param string the prefix
1912 * @return string
1913 */
1914 public function set_dbprefix($prefix = '')
1915 {
1916 return $this->dbprefix = $prefix;
1917 }
1918
1919 // --------------------------------------------------------------------
1920
1921 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001922 * Track Aliases
1923 *
1924 * Used to track SQL statements written with aliased tables.
1925 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001926 * @param string The table to inspect
1927 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001928 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001929 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001930 {
1931 if (is_array($table))
1932 {
1933 foreach ($table as $t)
1934 {
1935 $this->_track_aliases($t);
1936 }
1937 return;
1938 }
Barry Mienydd671972010-10-04 16:33:58 +02001939
Derek Jones37f4b9c2011-07-01 17:56:50 -05001940 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001941 // the string into discreet statements
1942 if (strpos($table, ',') !== FALSE)
1943 {
1944 return $this->_track_aliases(explode(',', $table));
1945 }
Barry Mienydd671972010-10-04 16:33:58 +02001946
Derek Allard2067d1a2008-11-13 22:59:24 +00001947 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001948 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001949 {
1950 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03001951 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001952
Derek Allard2067d1a2008-11-13 22:59:24 +00001953 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001954 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001955
Derek Allard2067d1a2008-11-13 22:59:24 +00001956 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001957 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001958 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001959 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001960 }
1961 }
1962 }
WanWizard7219c072011-12-28 14:09:05 +01001963
Derek Allard2067d1a2008-11-13 22:59:24 +00001964 // --------------------------------------------------------------------
1965
1966 /**
1967 * Compile the SELECT statement
1968 *
1969 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001970 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001971 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001972 * @return string
1973 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001974 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001975 {
1976 // Combine any cached components with the current statements
1977 $this->_merge_cache();
1978
Derek Allard2067d1a2008-11-13 22:59:24 +00001979 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001980 if ($select_override !== FALSE)
1981 {
1982 $sql = $select_override;
1983 }
1984 else
1985 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001986 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001987
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001988 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001989 {
Barry Mienydd671972010-10-04 16:33:58 +02001990 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001991 }
1992 else
Barry Mienydd671972010-10-04 16:33:58 +02001993 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001994 // Cycle through the "select" portion of the query and prep each column name.
1995 // The reason we protect identifiers here rather then in the select() function
1996 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001997 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001998 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001999 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002000 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002001 }
Barry Mienydd671972010-10-04 16:33:58 +02002002
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002003 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002004 }
2005 }
2006
Derek Allard2067d1a2008-11-13 22:59:24 +00002007 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002008 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002009 {
Andrey Andreeve78f8152012-10-09 11:38:38 +03002010 $sql .= "\nFROM ".$this->_from_tables();
Derek Allard2067d1a2008-11-13 22:59:24 +00002011 }
2012
Derek Allard2067d1a2008-11-13 22:59:24 +00002013 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002014 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002015 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002016 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002017 }
2018
Andrey Andreev2d486232012-07-19 14:46:51 +03002019 $sql .= $this->_compile_wh('qb_where')
2020 .$this->_compile_group_by()
2021 .$this->_compile_wh('qb_having')
2022 .$this->_compile_order_by(); // ORDER BY
Andrey Andreevb0478652012-07-18 15:34:46 +03002023
Andrey Andreevd40459d2012-07-18 16:46:39 +03002024 // LIMIT
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002025 if ($this->qb_limit)
Derek Allard2067d1a2008-11-13 22:59:24 +00002026 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002027 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002028 }
2029
2030 return $sql;
2031 }
2032
2033 // --------------------------------------------------------------------
2034
2035 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002036 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002037 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002038 * Escapes identifiers in WHERE and HAVING statements at execution time.
2039 *
Andrey Andreevb0478652012-07-18 15:34:46 +03002040 * Required so that aliases are tracked properly, regardless of wether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002041 * where(), or_where(), having(), or_having are called prior to from(),
2042 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002043 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002044 * @param string 'qb_where' or 'qb_having'
2045 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002046 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002047 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002048 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002049 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002050 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002051 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002052 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002053 if ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002054 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002055 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002056 continue;
2057 }
2058
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002059 // Split multiple conditions
2060 $conditions = preg_split(
2061 '/(\s*AND\s+|\s*OR\s+)/i',
2062 $this->{$qb_key}[$i]['condition'],
2063 -1,
2064 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
2065 );
2066
2067 for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002068 {
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002069 if (($op = $this->_get_operator($conditions[$ci])) === FALSE
2070 OR ! preg_match('/^(\(?)(.*)('.preg_quote($op).')(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches))
2071 {
2072 continue;
2073 }
2074
2075 // $matches = array(
2076 // 0 => '(test <= foo)', /* the whole thing */
2077 // 1 => '(', /* optional */
2078 // 2 => 'test', /* the field name */
2079 // 3 => ' <= ', /* $op */
2080 // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2081 // 5 => ')' /* optional */
2082 // );
2083 empty($matches[4]) OR $matches[4] = ' '.$this->protect_identifiers(trim($matches[4]));
2084 $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2]))
2085 .' '.trim($matches[3]).$matches[4].$matches[5];
Andrey Andreev6e704752012-07-18 00:46:33 +03002086 }
2087
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002088 $this->{$qb_key}[$i] = implode('', $conditions);
Andrey Andreev6e704752012-07-18 00:46:33 +03002089 }
2090
Andrey Andreev9d3aa1b2012-10-12 12:14:09 +03002091 return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ")
2092 .implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002093 }
2094
Andrey Andreevb0478652012-07-18 15:34:46 +03002095 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002096 }
2097
2098 // --------------------------------------------------------------------
2099
2100 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002101 * Compile GROUP BY
2102 *
2103 * Escapes identifiers in GROUP BY statements at execution time.
2104 *
2105 * Required so that aliases are tracked properly, regardless of wether
2106 * group_by() is called prior to from(), join() and dbprefix is added
2107 * only if needed.
2108 *
2109 * @return string SQL statement
2110 */
2111 protected function _compile_group_by()
2112 {
2113 if (count($this->qb_groupby) > 0)
2114 {
Andrey Andreev96feb582012-07-19 13:12:34 +03002115 for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
2116 {
2117 $this->qb_groupby[$i] = ($this->qb_groupby[$i]['escape'] === FALSE)
2118 ? $this->qb_groupby[$i]['field']
Andrey Andreev13f50542012-10-12 12:31:02 +03002119 : $this->protect_identifiers($this->qb_groupby[$i]['field']);
Andrey Andreev96feb582012-07-19 13:12:34 +03002120 }
2121
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002122 return "\nGROUP BY ".implode(', ', $this->qb_groupby);
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002123 }
2124
2125 return '';
2126 }
2127
2128 // --------------------------------------------------------------------
2129
2130 /**
Andrey Andreev2d486232012-07-19 14:46:51 +03002131 * Compile ORDER BY
2132 *
2133 * Escapes identifiers in ORDER BY statements at execution time.
2134 *
2135 * Required so that aliases are tracked properly, regardless of wether
2136 * order_by() is called prior to from(), join() and dbprefix is added
2137 * only if needed.
2138 *
2139 * @return string SQL statement
2140 */
2141 protected function _compile_order_by()
2142 {
2143 if (count($this->qb_orderby) > 0)
2144 {
2145 $sql = "\nORDER BY ";
2146
2147 for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++)
2148 {
2149 if ($this->qb_orderby[$i]['escape'] !== FALSE)
2150 {
2151 $this->qb_orderby[$i]['field'] = $this->protect_identifiers($field);
2152 }
2153
2154 $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction'];
2155 }
2156
2157 $sql .= implode(', ', $this->qb_orderby);
2158 }
2159
2160 return '';
2161 }
2162
2163 // --------------------------------------------------------------------
2164
2165 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002166 * Object to Array
2167 *
2168 * Takes an object as input and converts the class variables to array key/vals
2169 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002170 * @param object
2171 * @return array
2172 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002173 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002174 {
2175 if ( ! is_object($object))
2176 {
2177 return $object;
2178 }
Barry Mienydd671972010-10-04 16:33:58 +02002179
Derek Allard2067d1a2008-11-13 22:59:24 +00002180 $array = array();
2181 foreach (get_object_vars($object) as $key => $val)
2182 {
2183 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002184 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002185 {
2186 $array[$key] = $val;
2187 }
2188 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002189
2190 return $array;
2191 }
Barry Mienydd671972010-10-04 16:33:58 +02002192
Derek Jonesd10e8962010-03-02 17:10:36 -06002193 // --------------------------------------------------------------------
2194
2195 /**
2196 * Object to Array
2197 *
2198 * Takes an object as input and converts the class variables to array key/vals
2199 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002200 * @param object
2201 * @return array
2202 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002203 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002204 {
2205 if ( ! is_object($object))
2206 {
2207 return $object;
2208 }
Barry Mienydd671972010-10-04 16:33:58 +02002209
Derek Jonesd10e8962010-03-02 17:10:36 -06002210 $array = array();
2211 $out = get_object_vars($object);
2212 $fields = array_keys($out);
2213
2214 foreach ($fields as $val)
2215 {
2216 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002217 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002218 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002219 $i = 0;
2220 foreach ($out[$val] as $data)
2221 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002222 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002223 }
2224 }
2225 }
2226
Derek Allard2067d1a2008-11-13 22:59:24 +00002227 return $array;
2228 }
Barry Mienydd671972010-10-04 16:33:58 +02002229
Derek Allard2067d1a2008-11-13 22:59:24 +00002230 // --------------------------------------------------------------------
2231
2232 /**
2233 * Start Cache
2234 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002235 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002236 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002237 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002238 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002239 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002240 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002241 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002242 }
2243
2244 // --------------------------------------------------------------------
2245
2246 /**
2247 * Stop Cache
2248 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002249 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002250 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002251 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002252 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002253 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002254 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002255 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002256 }
2257
2258 // --------------------------------------------------------------------
2259
2260 /**
2261 * Flush Cache
2262 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002263 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002264 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002265 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002266 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002267 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002268 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002269 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002270 'qb_cache_select' => array(),
2271 'qb_cache_from' => array(),
2272 'qb_cache_join' => array(),
2273 'qb_cache_where' => array(),
2274 'qb_cache_like' => array(),
2275 'qb_cache_groupby' => array(),
2276 'qb_cache_having' => array(),
2277 'qb_cache_orderby' => array(),
2278 'qb_cache_set' => array(),
2279 'qb_cache_exists' => array(),
2280 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002281 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002282 }
2283
2284 // --------------------------------------------------------------------
2285
2286 /**
2287 * Merge Cache
2288 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002289 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002290 * locally called ones.
2291 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002292 * @return void
2293 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002294 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002295 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002296 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002297 {
2298 return;
2299 }
2300
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002301 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002302 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002303 $qb_variable = 'qb_'.$val;
2304 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002305
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002306 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002307 {
2308 continue;
2309 }
2310
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002311 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002312 }
2313
2314 // If we are "protecting identifiers" we need to examine the "from"
2315 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002316 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002317 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002318 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002319 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002320
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002321 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002322 }
WanWizard7219c072011-12-28 14:09:05 +01002323
Kyle Farris0c147b32011-08-26 02:29:31 -04002324 // --------------------------------------------------------------------
2325
2326 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002327 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002328 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002329 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002330 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002331 * @return void
2332 */
2333 public function reset_query()
2334 {
2335 $this->_reset_select();
2336 $this->_reset_write();
2337 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002338
2339 // --------------------------------------------------------------------
2340
2341 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002342 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002343 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002344 * @param array An array of fields to reset
2345 * @return void
2346 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002347 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002348 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002349 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002350 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002351 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002352 {
2353 $this->$item = $default_value;
2354 }
2355 }
2356 }
2357
2358 // --------------------------------------------------------------------
2359
2360 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002361 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002362 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002363 * @return void
2364 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002365 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002366 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002367 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002368 'qb_select' => array(),
2369 'qb_from' => array(),
2370 'qb_join' => array(),
2371 'qb_where' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002372 'qb_groupby' => array(),
2373 'qb_having' => array(),
2374 'qb_orderby' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002375 'qb_aliased_tables' => array(),
2376 'qb_no_escape' => array(),
2377 'qb_distinct' => FALSE,
2378 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002379 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002380 )
2381 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002382 }
Barry Mienydd671972010-10-04 16:33:58 +02002383
Derek Allard2067d1a2008-11-13 22:59:24 +00002384 // --------------------------------------------------------------------
2385
2386 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002387 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002388 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002389 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002390 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002391 * @return void
2392 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002393 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002394 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002395 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002396 'qb_set' => array(),
2397 'qb_from' => array(),
2398 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002399 'qb_orderby' => array(),
2400 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002401 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002402 )
2403 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002404 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002405
Derek Allard2067d1a2008-11-13 22:59:24 +00002406}
2407
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002408/* End of file DB_query_builder.php */
Andrey Andreev58803fb2012-06-24 00:45:37 +03002409/* Location: ./system/database/DB_query_builder.php */