blob: 75cad95de201482920c1fe11a05fd797b790bb71 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
Jamie Rumbelow7efad202012-02-19 12:37:00 +000030 * Query Builder Class
Derek Allard2067d1a2008-11-13 22:59:24 +000031 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +000032 * This is the platform-independent base Query Builder implementation class.
Derek Allard2067d1a2008-11-13 22:59:24 +000033 *
34 * @package CodeIgniter
35 * @subpackage Drivers
36 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/database/
39 */
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +010040
41abstract class CI_DB_query_builder extends CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000042
WanWizard7219c072011-12-28 14:09:05 +010043 protected $return_delete_sql = FALSE;
44 protected $reset_delete_data = FALSE;
45
Jamie Rumbelow7efad202012-02-19 12:37:00 +000046 protected $qb_select = array();
47 protected $qb_distinct = FALSE;
48 protected $qb_from = array();
49 protected $qb_join = array();
50 protected $qb_where = array();
Jamie Rumbelow7efad202012-02-19 12:37:00 +000051 protected $qb_groupby = array();
52 protected $qb_having = array();
53 protected $qb_keys = array();
54 protected $qb_limit = FALSE;
55 protected $qb_offset = FALSE;
Jamie Rumbelow7efad202012-02-19 12:37:00 +000056 protected $qb_orderby = array();
57 protected $qb_set = array();
Jamie Rumbelow7efad202012-02-19 12:37:00 +000058 protected $qb_aliased_tables = array();
59 protected $qb_store_array = array();
60 protected $qb_where_group_started = FALSE;
61 protected $qb_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +020062
Jamie Rumbelow7efad202012-02-19 12:37:00 +000063 // Query Builder Caching variables
64 protected $qb_caching = FALSE;
65 protected $qb_cache_exists = array();
66 protected $qb_cache_select = array();
67 protected $qb_cache_from = array();
68 protected $qb_cache_join = array();
69 protected $qb_cache_where = array();
70 protected $qb_cache_like = array();
71 protected $qb_cache_groupby = array();
72 protected $qb_cache_having = array();
73 protected $qb_cache_orderby = array();
74 protected $qb_cache_set = array();
WanWizard7219c072011-12-28 14:09:05 +010075
Jamie Rumbelow7efad202012-02-19 12:37:00 +000076 protected $qb_no_escape = array();
77 protected $qb_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000078
79 /**
80 * Select
81 *
82 * Generates the SELECT portion of the query
83 *
Derek Allard2067d1a2008-11-13 22:59:24 +000084 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +030085 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +000086 * @return object
87 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060088 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
Derek Allard2067d1a2008-11-13 22:59:24 +000090 if (is_string($select))
91 {
92 $select = explode(',', $select);
93 }
94
Andrey Andreev42870232012-06-12 01:30:20 +030095 // If the escape value was not set will will base it on the global setting
96 is_bool($escape) OR $escape = $this->_protect_identifiers;
97
Derek Allard2067d1a2008-11-13 22:59:24 +000098 foreach ($select as $val)
99 {
100 $val = trim($val);
101
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100102 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000104 $this->qb_select[] = $val;
105 $this->qb_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000106
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000107 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000109 $this->qb_cache_select[] = $val;
110 $this->qb_cache_exists[] = 'select';
111 $this->qb_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 }
113 }
114 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 return $this;
117 }
118
119 // --------------------------------------------------------------------
120
121 /**
122 * Select Max
123 *
124 * Generates a SELECT MAX(field) portion of a query
125 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 * @param string the field
127 * @param string an alias
128 * @return object
129 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600130 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
132 return $this->_max_min_avg_sum($select, $alias, 'MAX');
133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 // --------------------------------------------------------------------
136
137 /**
138 * Select Min
139 *
140 * Generates a SELECT MIN(field) portion of a query
141 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 * @param string the field
143 * @param string an alias
144 * @return object
145 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600146 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
148 return $this->_max_min_avg_sum($select, $alias, 'MIN');
149 }
150
151 // --------------------------------------------------------------------
152
153 /**
154 * Select Average
155 *
156 * Generates a SELECT AVG(field) portion of a query
157 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 * @param string the field
159 * @param string an alias
160 * @return object
161 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600162 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
164 return $this->_max_min_avg_sum($select, $alias, 'AVG');
165 }
166
167 // --------------------------------------------------------------------
168
169 /**
170 * Select Sum
171 *
172 * Generates a SELECT SUM(field) portion of a query
173 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 * @param string the field
175 * @param string an alias
176 * @return object
177 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600178 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
180 return $this->_max_min_avg_sum($select, $alias, 'SUM');
181 }
182
183 // --------------------------------------------------------------------
184
185 /**
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300186 * Processing Function for the following functions:
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 *
188 * select_max()
189 * select_min()
190 * select_avg()
Andrey Andreev24276a32012-01-08 02:44:38 +0200191 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200192 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300193 *
194 * @param string $select = '' field name
195 * @param string $alias = ''
196 * @param string $type = 'MAX'
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 * @return object
198 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600199 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100201 if ( ! is_string($select) OR $select === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
203 $this->display_error('db_invalid_query');
204 }
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
209 {
210 show_error('Invalid function type: '.$type);
211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100213 if ($alias === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
215 $alias = $this->_create_alias_from_table(trim($select));
216 }
Barry Mienydd671972010-10-04 16:33:58 +0200217
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300218 $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->escape_identifiers(trim($alias));
219
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000220 $this->qb_select[] = $sql;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100221 $this->qb_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200222
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000223 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000225 $this->qb_cache_select[] = $sql;
226 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 }
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 return $this;
230 }
231
232 // --------------------------------------------------------------------
233
234 /**
235 * Determines the alias name based on the table
236 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 * @param string
238 * @return string
239 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600240 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
242 if (strpos($item, '.') !== FALSE)
243 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200244 $item = explode('.', $item);
245 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 return $item;
249 }
250
251 // --------------------------------------------------------------------
252
253 /**
254 * DISTINCT
255 *
256 * Sets a flag which tells the query string compiler to add DISTINCT
257 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 * @param bool
259 * @return object
260 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600261 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300263 $this->qb_distinct = is_bool($val) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 return $this;
265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 // --------------------------------------------------------------------
268
269 /**
270 * From
271 *
272 * Generates the FROM portion of the query
273 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 * @param mixed can be a string or array
275 * @return object
276 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600277 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300279 foreach ((array) $from as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
281 if (strpos($val, ',') !== FALSE)
282 {
283 foreach (explode(',', $val) as $v)
284 {
285 $v = trim($v);
286 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200287
George Petsagourakis193d4482012-04-28 11:16:18 +0300288 $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000289
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000290 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000292 $this->qb_cache_from[] = $v;
293 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200294 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
297 else
298 {
299 $val = trim($val);
300
Andrey Andreev24276a32012-01-08 02:44:38 +0200301 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000302 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200304
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000305 $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000306
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000307 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000309 $this->qb_cache_from[] = $val;
310 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
312 }
313 }
314
315 return $this;
316 }
317
318 // --------------------------------------------------------------------
319
320 /**
321 * Join
322 *
323 * Generates the JOIN portion of the query
324 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 * @param string
326 * @param string the join condition
327 * @param string the type of join
Alex Bilbief512b732012-06-16 11:15:19 +0100328 * @param string whether not to try to escape identifiers
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 * @return object
330 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300331 public function join($table, $cond, $type = '', $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200332 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100333 if ($type !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
335 $type = strtoupper(trim($type));
336
Andrey Andreev42870232012-06-12 01:30:20 +0300337 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
339 $type = '';
340 }
341 else
342 {
343 $type .= ' ';
344 }
345 }
346
Andrey Andreev24276a32012-01-08 02:44:38 +0200347 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000348 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 $this->_track_aliases($table);
350
Andrey Andreevfe642da2012-06-16 03:47:33 +0300351 is_bool($escape) OR $escape = $this->_protect_identifiers;
352
Andrey Andreev42870232012-06-12 01:30:20 +0300353 // Split multiple conditions
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300354 if ($escape === TRUE && preg_match_all('/\sAND\s|\sOR\s/i', $cond, $m, PREG_OFFSET_CAPTURE))
Andrey Andreev42870232012-06-12 01:30:20 +0300355 {
356 $newcond = '';
357 $m[0][] = array('', strlen($cond));
358
359 for ($i = 0, $c = count($m[0]), $s = 0;
360 $i < $c;
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300361 $s = $m[0][$i][1] + strlen($m[0][$i][0]), $i++)
Andrey Andreev42870232012-06-12 01:30:20 +0300362 {
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300363 $temp = substr($cond, $s, ($m[0][$i][1] - $s));
Andrey Andreev42870232012-06-12 01:30:20 +0300364
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300365 $newcond .= preg_match("/([\[\]\w\.'-]+)(\s*[^\"\[`'\w]+\s*)(.+)/i", $temp, $match)
Andrey Andreev42870232012-06-12 01:30:20 +0300366 ? $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3])
367 : $temp;
368
369 $newcond .= $m[0][$i][0];
370 }
371
Andrey Andreev3751f932012-06-17 18:07:48 +0300372 $cond = ' ON '.$newcond;
Andrey Andreev42870232012-06-12 01:30:20 +0300373 }
374 // Split apart the condition and protect the identifiers
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300375 elseif ($escape === TRUE && preg_match("/([\[\]\w\.'-]+)(\s*[^\"\[`'\w]+\s*)(.+)/i", $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
Andrey Andreev3751f932012-06-17 18:07:48 +0300377 $cond = ' ON '.$this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
378 }
379 elseif ( ! $this->_has_operator($cond))
380 {
381 $cond = ' USING ('.($escape ? $this->escape_identifiers($cond) : $cond).')';
382 }
383 else
384 {
385 $cond = ' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
Barry Mienydd671972010-10-04 16:33:58 +0200387
Andrey Andreev42870232012-06-12 01:30:20 +0300388 // Do we want to escape the table name?
389 if ($escape === TRUE)
390 {
391 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
392 }
393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 // Assemble the JOIN statement
Andrey Andreev3751f932012-06-17 18:07:48 +0300395 $this->qb_join[] = $join = $type.'JOIN '.$table.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000396
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000397 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000399 $this->qb_cache_join[] = $join;
400 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 }
402
403 return $this;
404 }
405
406 // --------------------------------------------------------------------
407
408 /**
409 * Where
410 *
411 * Generates the WHERE portion of the query. Separates
412 * multiple calls with AND
413 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 * @param mixed
415 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300416 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 * @return object
418 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300419 public function where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300421 return $this->_wh('qb_where', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 // --------------------------------------------------------------------
425
426 /**
427 * OR Where
428 *
429 * Generates the WHERE portion of the query. Separates
430 * multiple calls with OR
431 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 * @param mixed
433 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300434 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 * @return object
436 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300437 public function or_where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300439 return $this->_wh('qb_where', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 }
441
442 // --------------------------------------------------------------------
443
444 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +0300445 * WHERE, HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300447 * Called by where(), or_where(), having(), or_having()
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300449 * @param string 'qb_where' or 'qb_having'
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 * @param mixed
451 * @param mixed
452 * @param string
Andrey Andreevb0478652012-07-18 15:34:46 +0300453 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 * @return object
455 */
Andrey Andreevd40459d2012-07-18 16:46:39 +0300456 protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300458 $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where';
459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 if ( ! is_array($key))
461 {
462 $key = array($key => $value);
463 }
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 // If the escape value was not set will will base it on the global setting
Andrey Andreeve10fb792012-06-15 12:07:04 +0300466 is_bool($escape) OR $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000467
468 foreach ($key as $k => $v)
469 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300470 $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0)
Andrey Andreev58803fb2012-06-24 00:45:37 +0300471 ? $this->_group_get_type('')
472 : $this->_group_get_type($type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000473
474 if (is_null($v) && ! $this->_has_operator($k))
475 {
476 // value appears not to have been set, assign the test to IS NULL
477 $k .= ' IS NULL';
478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 if ( ! is_null($v))
481 {
482 if ($escape === TRUE)
483 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300484 $v = ' '.(is_int($v) ? $v : $this->escape($v));
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 }
WanWizard7219c072011-12-28 14:09:05 +0100486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 if ( ! $this->_has_operator($k))
488 {
Greg Akere156c6e2011-04-20 16:03:04 -0500489 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 }
491 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000492
Andrey Andreevd40459d2012-07-18 16:46:39 +0300493 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000494 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300496 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
497 $this->qb_cache_exists[] = substr($qb_key, 3);
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 }
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
Barry Mienydd671972010-10-04 16:33:58 +0200501
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 return $this;
503 }
504
505 // --------------------------------------------------------------------
506
507 /**
508 * Where_in
509 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300510 * Generates a WHERE field IN('item', 'item') SQL query joined with
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 * AND if appropriate
512 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300513 * @param string $key = NULL The field to search
514 * @param array $values = NULL The values searched on
515 * @param bool $escape = NULL
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 * @return object
517 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300518 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300520 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 }
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 // --------------------------------------------------------------------
524
525 /**
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300526 * Or_where_in
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300528 * Generates a WHERE field IN('item', 'item') SQL query joined with
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 * OR if appropriate
530 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300531 * @param string $key = NULL The field to search
532 * @param array $values = NULL The values searched on
533 * @param bool $escape = NULL
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 * @return object
535 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300536 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300538 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 }
540
541 // --------------------------------------------------------------------
542
543 /**
544 * Where_not_in
545 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300546 * Generates a WHERE field NOT IN('item', 'item') SQL query joined
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 * with AND if appropriate
548 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300549 * @param string $key = NULL The field to search
550 * @param array $values = NULL The values searched on
551 * @param bool $escape = NULL
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 * @return object
553 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300554 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300556 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 }
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 // --------------------------------------------------------------------
560
561 /**
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300562 * Or_where_not_in
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300564 * Generates a WHERE field NOT IN('item', 'item') SQL query joined
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 * with OR if appropriate
566 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300567 * @param string $key = NULL The field to search
568 * @param array $values = NULL The values searched on
569 * @param bool $escape = NULL
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 * @return object
571 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300572 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300574 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 }
576
577 // --------------------------------------------------------------------
578
579 /**
580 * Where_in
581 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300582 * Called by where_in(), or_where_in(), where_not_in(), or_where_not_in()
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300584 * @param string $key = NULL The field to search
585 * @param array $values = NULL The values searched on
586 * @param bool $not = FALSE If the statement would be IN or NOT IN
587 * @param string $type = 'AND '
588 * @param bool $escape = NULL
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 * @return object
590 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300591 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
593 if ($key === NULL OR $values === NULL)
594 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300595 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 }
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 if ( ! is_array($values))
599 {
600 $values = array($values);
601 }
Barry Mienydd671972010-10-04 16:33:58 +0200602
Andrey Andreev498c1e02012-06-16 03:34:10 +0300603 is_bool($escape) OR $escape = $this->_protect_identifiers;
604
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 $not = ($not) ? ' NOT' : '';
606
Andrey Andreev94611df2012-07-19 12:29:54 +0300607 $where_in = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 foreach ($values as $value)
609 {
Andrey Andreevcc02db92012-10-12 14:30:10 +0300610 $where_in[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 }
612
WanWizardbc69f362012-06-22 00:10:11 +0200613 $prefix = (count($this->qb_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Andrey Andreev6e704752012-07-18 00:46:33 +0300614 $where_in = array(
Andrey Andreev94611df2012-07-19 12:29:54 +0300615 'condition' => $prefix.$key.$not.' IN('.implode(', ', $where_in).')',
Andrey Andreev6e704752012-07-18 00:46:33 +0300616 'escape' => $escape
617 );
Barry Mienydd671972010-10-04 16:33:58 +0200618
Andrey Andreev6e704752012-07-18 00:46:33 +0300619 $this->qb_where[] = $where_in;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000620 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000622 $this->qb_cache_where[] = $where_in;
623 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 }
625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 return $this;
627 }
Barry Mienydd671972010-10-04 16:33:58 +0200628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 // --------------------------------------------------------------------
630
631 /**
632 * Like
633 *
634 * Generates a %LIKE% portion of the query. Separates
635 * multiple calls with AND
636 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300638 * @param string
639 * @param string
640 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 * @return object
642 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300643 public function like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300645 return $this->_like($field, $match, 'AND ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 }
647
648 // --------------------------------------------------------------------
649
650 /**
651 * Not Like
652 *
653 * Generates a NOT LIKE portion of the query. Separates
654 * multiple calls with AND
655 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300657 * @param string
658 * @param string
659 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 * @return object
661 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300662 public function not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300664 return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 }
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 // --------------------------------------------------------------------
668
669 /**
670 * OR Like
671 *
672 * Generates a %LIKE% portion of the query. Separates
673 * multiple calls with OR
674 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300676 * @param string
677 * @param string
678 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 * @return object
680 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300681 public function or_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300683 return $this->_like($field, $match, 'OR ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 }
685
686 // --------------------------------------------------------------------
687
688 /**
689 * OR Not Like
690 *
691 * Generates a NOT LIKE portion of the query. Separates
692 * multiple calls with OR
693 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 * @param mixed
Andrey Andreevb0478652012-07-18 15:34:46 +0300695 * @param string
696 * @param string
697 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 * @return object
699 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300700 public function or_not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300702 return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 }
Barry Mienydd671972010-10-04 16:33:58 +0200704
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 // --------------------------------------------------------------------
706
707 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 * Like
709 *
Andrey Andreevb0478652012-07-18 15:34:46 +0300710 * Called by like(), or_like(), not_like, or_not_like()
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 * @param mixed
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 * @param string
Andrey Andreevb0478652012-07-18 15:34:46 +0300714 * @param string
715 * @param string
716 * @param string
717 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 * @return object
719 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300720 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 {
722 if ( ! is_array($field))
723 {
724 $field = array($field => $match);
725 }
Barry Mienydd671972010-10-04 16:33:58 +0200726
Andrey Andreevb0478652012-07-18 15:34:46 +0300727 is_bool($escape) OR $escape = $this->_protect_identifiers;
728 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
729 ? $this->_group_get_type('') : $this->_group_get_type($type);
730
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 foreach ($field as $k => $v)
732 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000733 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300734
Andrey Andreev24276a32012-01-08 02:44:38 +0200735 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400736 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300737 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400738 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200739 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300741 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200743 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300745 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 }
747 else
748 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300749 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600751
Derek Jonese4ed5832009-02-20 21:44:59 +0000752 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100753 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000754 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300755 $like_statement .= sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000756 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600757
Andrey Andreevb0478652012-07-18 15:34:46 +0300758 $this->qb_where[] = array('condition' => $like_statement, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000759 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 {
Andrey Andreevededc4a2012-07-18 01:16:15 +0300761 $this->qb_cache_where[] = $like_statement;
762 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200765
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 return $this;
767 }
Barry Mienydd671972010-10-04 16:33:58 +0200768
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 // --------------------------------------------------------------------
770
771 /**
WanWizard7219c072011-12-28 14:09:05 +0100772 * Starts a query group.
773 *
774 * @param string (Internal use only)
775 * @param string (Internal use only)
776 * @return object
777 */
778 public function group_start($not = '', $type = 'AND ')
779 {
780 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100781
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000782 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100783 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Andrey Andreev6e704752012-07-18 00:46:33 +0300784 $where = array(
785 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (',
786 'escape' => FALSE
787 );
WanWizard7219c072011-12-28 14:09:05 +0100788
Andrey Andreev6e704752012-07-18 00:46:33 +0300789 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000790 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100791 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300792 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +0100793 }
794
795 return $this;
796 }
797
798 // --------------------------------------------------------------------
799
800 /**
801 * Starts a query group, but ORs the group
802 *
803 * @return object
804 */
805 public function or_group_start()
806 {
807 return $this->group_start('', 'OR ');
808 }
809
810 // --------------------------------------------------------------------
811
812 /**
813 * Starts a query group, but NOTs the group
814 *
815 * @return object
816 */
817 public function not_group_start()
818 {
819 return $this->group_start('NOT ', 'AND ');
820 }
821
822 // --------------------------------------------------------------------
823
824 /**
825 * Starts a query group, but OR NOTs the group
826 *
827 * @return object
828 */
829 public function or_not_group_start()
830 {
831 return $this->group_start('NOT ', 'OR ');
832 }
833
834 // --------------------------------------------------------------------
835
836 /**
837 * Ends a query group
838 *
839 * @return object
840 */
841 public function group_end()
842 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000843 $this->qb_where_group_started = FALSE;
Andrey Andreev6e704752012-07-18 00:46:33 +0300844 $where = array(
845 'condition' => str_repeat(' ', $this->qb_where_group_count--).')',
846 'escape' => FALSE
847 );
WanWizard7219c072011-12-28 14:09:05 +0100848
Andrey Andreev6e704752012-07-18 00:46:33 +0300849 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000850 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100851 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300852 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +0100853 }
854
WanWizard7219c072011-12-28 14:09:05 +0100855 return $this;
856 }
857
858 // --------------------------------------------------------------------
859
860 /**
861 * Group_get_type
862 *
863 * Called by group_start(), _like(), _where() and _where_in()
864 *
865 * @param string
866 * @return string
867 */
868 protected function _group_get_type($type)
869 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000870 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100871 {
872 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000873 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100874 }
875
876 return $type;
877 }
878
879 // --------------------------------------------------------------------
880
881 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 * GROUP BY
883 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 * @param string
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300885 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 * @return object
887 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300888 public function group_by($by, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 {
Andrey Andreev96feb582012-07-19 13:12:34 +0300890 is_bool($escape) OR $escape = $this->_protect_identifiers;
891
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 if (is_string($by))
893 {
Andrey Andreev96feb582012-07-19 13:12:34 +0300894 $by = ($escape === TRUE)
895 ? explode(',', $by)
896 : array($by);
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 }
Barry Mienydd671972010-10-04 16:33:58 +0200898
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 foreach ($by as $val)
900 {
901 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200902
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100903 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 {
Andrey Andreev96feb582012-07-19 13:12:34 +0300905 $val = array('field' => $val, 'escape' => $escape);
Barry Mienydd671972010-10-04 16:33:58 +0200906
Andrey Andreev96feb582012-07-19 13:12:34 +0300907 $this->qb_groupby[] = $val;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000908 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000910 $this->qb_cache_groupby[] = $val;
911 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 }
913 }
914 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200915
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 return $this;
917 }
918
919 // --------------------------------------------------------------------
920
921 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 * Sets the HAVING value
923 *
924 * Separates multiple calls with AND
925 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 * @param string
927 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300928 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 * @return object
930 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +0300931 public function having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300933 return $this->_wh('qb_having', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 }
Barry Mienydd671972010-10-04 16:33:58 +0200935
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 // --------------------------------------------------------------------
937
938 /**
939 * Sets the OR HAVING value
940 *
941 * Separates multiple calls with OR
942 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 * @param string
944 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300945 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 * @return object
947 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +0300948 public function or_having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300950 return $this->_wh('qb_having', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 }
Barry Mienydd671972010-10-04 16:33:58 +0200952
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 // --------------------------------------------------------------------
954
955 /**
956 * Sets the ORDER BY value
957 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 * @param string
Andrey Andreev2d486232012-07-19 14:46:51 +0300959 * @param string direction: ASC or DESC
pporlan2c685fb2011-12-22 12:15:25 +0100960 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 * @return object
962 */
Andrey Andreevd24160c2012-06-16 03:21:20 +0300963 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300965 $direction = trim($direction);
966
967 if (strtolower($direction) === 'random' OR $orderby === $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300969 // Random ordered results don't need a field name
970 $orderby = $this->_random_keyword;
971 $direction = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 }
Andrey Andreev2d486232012-07-19 14:46:51 +0300973 elseif (empty($orderby))
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300975 return $this;
976 }
977 elseif ($direction !== '')
978 {
979 $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 }
Barry Mienydd671972010-10-04 16:33:58 +0200981
Andrey Andreevd24160c2012-06-16 03:21:20 +0300982 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +0200983
Andrey Andreev2d486232012-07-19 14:46:51 +0300984 if ($escape === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 {
Andrey Andreev93dd2f22012-10-24 10:09:18 +0300986 $qb_orderby[] = array('field' => $orderby, 'direction' => $direction, 'escape' => FALSE);
Andrey Andreev2d486232012-07-19 14:46:51 +0300987 }
988 else
989 {
990 $qb_orderby = array();
991 foreach (explode(',', $orderby) as $field)
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 {
Andrey Andreev2d486232012-07-19 14:46:51 +0300993 $qb_orderby[] = ($direction === '' && preg_match('/\s+(ASC|DESC)$/i', rtrim($field), $match, PREG_OFFSET_CAPTURE))
994 ? array('field' => ltrim(substr($field, 0, $match[0][1])), 'direction' => ' '.$match[1][0], 'escape' => TRUE)
995 : array('field' => trim($field), 'direction' => $direction, 'escape' => TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 }
Barry Mienydd671972010-10-04 16:33:58 +0200998
Andrey Andreev2d486232012-07-19 14:46:51 +0300999 $this->qb_orderby = array_merge($this->qb_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001000 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001002 $this->qb_cache_orderby = array_merge($this->qb_cache_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001003 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 }
1005
1006 return $this;
1007 }
Barry Mienydd671972010-10-04 16:33:58 +02001008
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 // --------------------------------------------------------------------
1010
1011 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 * Sets the LIMIT value
1013 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001014 * @param int the limit value
1015 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 * @return object
1017 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001018 public function limit($value, $offset = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001020 is_null($value) OR $this->qb_limit = (int) $value;
1021 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001022
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 return $this;
1024 }
Barry Mienydd671972010-10-04 16:33:58 +02001025
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 // --------------------------------------------------------------------
1027
1028 /**
1029 * Sets the OFFSET value
1030 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001031 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 * @return object
1033 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001034 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001036 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001037 return $this;
1038 }
Barry Mienydd671972010-10-04 16:33:58 +02001039
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 // --------------------------------------------------------------------
1041
1042 /**
Andrey Andreev2c35b642012-06-24 03:05:26 +03001043 * Limit string
1044 *
1045 * Generates a platform-specific LIMIT clause
1046 *
1047 * @param string the sql query string
Andrey Andreev2c35b642012-06-24 03:05:26 +03001048 * @return string
1049 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001050 protected function _limit($sql)
Andrey Andreev2c35b642012-06-24 03:05:26 +03001051 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001052 return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').$this->qb_limit;
Andrey Andreev2c35b642012-06-24 03:05:26 +03001053 }
1054
1055 // --------------------------------------------------------------------
1056
1057 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001058 * The "set" function.
1059 *
1060 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 * @param mixed
1063 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001064 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 * @return object
1066 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001067 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 {
1069 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001070
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 if ( ! is_array($key))
1072 {
1073 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001074 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001075
Andrey Andreevfe642da2012-06-16 03:47:33 +03001076 is_bool($escape) OR $escape = $this->_protect_identifiers;
1077
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 foreach ($key as $k => $v)
1079 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001080 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1081 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 }
Barry Mienydd671972010-10-04 16:33:58 +02001083
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 return $this;
1085 }
WanWizard7219c072011-12-28 14:09:05 +01001086
Kyle Farris0c147b32011-08-26 02:29:31 -04001087 // --------------------------------------------------------------------
1088
1089 /**
1090 * Get SELECT query string
1091 *
1092 * Compiles a SELECT query string and returns the sql.
1093 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001094 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001095 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001096 * @return string
1097 */
WanWizard7219c072011-12-28 14:09:05 +01001098 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001099 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001100 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001101 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001102 $this->_track_aliases($table);
1103 $this->from($table);
1104 }
WanWizard7219c072011-12-28 14:09:05 +01001105
Andrey Andreev650b4c02012-06-11 12:07:15 +03001106 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001107
Kyle Farris0c147b32011-08-26 02:29:31 -04001108 if ($reset === TRUE)
1109 {
1110 $this->_reset_select();
1111 }
WanWizard7219c072011-12-28 14:09:05 +01001112
Kyle Farris0c147b32011-08-26 02:29:31 -04001113 return $select;
1114 }
WanWizard7219c072011-12-28 14:09:05 +01001115
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 // --------------------------------------------------------------------
1117
1118 /**
1119 * Get
1120 *
1121 * Compiles the select statement based on the other functions called
1122 * and runs the query
1123 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 * @param string the table
1125 * @param string the limit clause
1126 * @param string the offset clause
1127 * @return object
1128 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001129 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001131 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 {
1133 $this->_track_aliases($table);
1134 $this->from($table);
1135 }
Barry Mienydd671972010-10-04 16:33:58 +02001136
Andrey Andreev650b4c02012-06-11 12:07:15 +03001137 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 {
1139 $this->limit($limit, $offset);
1140 }
Barry Mienydd671972010-10-04 16:33:58 +02001141
Andrey Andreev24276a32012-01-08 02:44:38 +02001142 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 $this->_reset_select();
1144 return $result;
1145 }
1146
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001147 // --------------------------------------------------------------------
1148
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 /**
1150 * "Count All Results" query
1151 *
Barry Mienydd671972010-10-04 16:33:58 +02001152 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001153 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 * @param string
1156 * @return string
1157 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001158 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001160 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 {
1162 $this->_track_aliases($table);
1163 $this->from($table);
1164 }
Barry Mienydd671972010-10-04 16:33:58 +02001165
Andrey Andreevb05f5062012-10-26 12:01:02 +03001166 $result = ($this->qb_distinct === TRUE)
1167 ? $this->query($this->_count_string.$this->protect_identifiers('numrows')."\nFROM (\n".$this->_compile_select()."\n) CI_count_all_results")
1168 : $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001170
Purwandi1d160e72012-01-09 16:33:28 +07001171 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001173 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 }
1175
Purwandi1d160e72012-01-09 16:33:28 +07001176 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001177 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001179
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 // --------------------------------------------------------------------
1181
1182 /**
1183 * Get_Where
1184 *
1185 * Allows the where clause, limit and offset to be added directly
1186 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001187 * @param string $table = ''
1188 * @param string $where = NULL
1189 * @param int $limit = NULL
1190 * @param int $offset = NULL
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 * @return object
1192 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001193 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001195 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 {
1197 $this->from($table);
1198 }
1199
1200 if ( ! is_null($where))
1201 {
1202 $this->where($where);
1203 }
Barry Mienydd671972010-10-04 16:33:58 +02001204
Andrey Andreev650b4c02012-06-11 12:07:15 +03001205 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 {
1207 $this->limit($limit, $offset);
1208 }
Barry Mienydd671972010-10-04 16:33:58 +02001209
Andrey Andreev24276a32012-01-08 02:44:38 +02001210 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 $this->_reset_select();
1212 return $result;
1213 }
1214
1215 // --------------------------------------------------------------------
1216
1217 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001218 * Insert_Batch
1219 *
1220 * Compiles batch insert strings and runs the queries
1221 *
Andrey Andreev9f808b02012-10-24 17:38:48 +03001222 * @param string $table = '' table to insert into
1223 * @param array $set an associative array of insert values
1224 * @return int number of rows inserted or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001225 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001226 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001227 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001228 if ( ! is_null($set))
1229 {
1230 $this->set_insert_batch($set);
1231 }
Barry Mienydd671972010-10-04 16:33:58 +02001232
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001233 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001234 {
Andrey Andreev9f808b02012-10-24 17:38:48 +03001235 // No valid data array. Folds in cases where keys and values did not match up
1236 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001237 }
1238
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001239 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001240 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001241 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001242 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001243 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001244 }
Barry Mienydd671972010-10-04 16:33:58 +02001245
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001246 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001247 }
1248
1249 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001250 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001251 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001252 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001253 $this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, 100)));
Andrey Andreev9f808b02012-10-24 17:38:48 +03001254 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001255 }
Barry Mienydd671972010-10-04 16:33:58 +02001256
Derek Jonesd10e8962010-03-02 17:10:36 -06001257 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001258 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001259 }
1260
1261 // --------------------------------------------------------------------
1262
1263 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001264 * Insert_batch statement
1265 *
1266 * Generates a platform-specific insert string from the supplied data.
1267 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001268 * @param string the table name
1269 * @param array the insert keys
1270 * @param array the insert values
1271 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001272 */
1273 protected function _insert_batch($table, $keys, $values)
1274 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001275 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001276 }
1277
1278 // --------------------------------------------------------------------
1279
1280 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001281 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001282 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001283 * @param mixed
1284 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001285 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001286 * @return object
1287 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001288 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001289 {
1290 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001291
Derek Jonesd10e8962010-03-02 17:10:36 -06001292 if ( ! is_array($key))
1293 {
1294 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001295 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001296
Andrey Andreevfe642da2012-06-16 03:47:33 +03001297 is_bool($escape) OR $escape = $this->_protect_identifiers;
1298
Iban Eguia3c0a4522012-04-15 13:30:44 +02001299 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001300 sort($keys);
1301
1302 foreach ($key as $row)
1303 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001304 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001305 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1306 {
1307 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001308 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001309 return;
1310 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001311
Barry Mienydd671972010-10-04 16:33:58 +02001312 ksort($row); // puts $row in the same order as our keys
1313
Andrey Andreev650b4c02012-06-11 12:07:15 +03001314 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001315 {
1316 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001317 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001318 {
Barry Mienydd671972010-10-04 16:33:58 +02001319 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001320 }
1321
Andrey Andreev650b4c02012-06-11 12:07:15 +03001322 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001323 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001324
1325 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001326 }
1327
1328 foreach ($keys as $k)
1329 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001330 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001331 }
Barry Mienydd671972010-10-04 16:33:58 +02001332
Derek Jonesd10e8962010-03-02 17:10:36 -06001333 return $this;
1334 }
WanWizard7219c072011-12-28 14:09:05 +01001335
Kyle Farris0c147b32011-08-26 02:29:31 -04001336 // --------------------------------------------------------------------
1337
1338 /**
1339 * Get INSERT query string
1340 *
1341 * Compiles an insert query and returns the sql
1342 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001343 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001344 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001345 * @return string
1346 */
1347 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001348 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001349 if ($this->_validate_insert($table) === FALSE)
1350 {
1351 return FALSE;
1352 }
WanWizard7219c072011-12-28 14:09:05 +01001353
Kyle Farris76116012011-08-31 11:17:48 -04001354 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001355 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001356 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001357 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001358 array_keys($this->qb_set),
1359 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001360 );
WanWizard7219c072011-12-28 14:09:05 +01001361
Kyle Farris0c147b32011-08-26 02:29:31 -04001362 if ($reset === TRUE)
1363 {
1364 $this->_reset_write();
1365 }
WanWizard7219c072011-12-28 14:09:05 +01001366
Kyle Farris0c147b32011-08-26 02:29:31 -04001367 return $sql;
1368 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001369
Derek Allard2067d1a2008-11-13 22:59:24 +00001370 // --------------------------------------------------------------------
1371
1372 /**
1373 * Insert
1374 *
1375 * Compiles an insert string and runs the query
1376 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001377 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 * @param array an associative array of insert values
1379 * @return object
1380 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001381 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001382 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001383 if ( ! is_null($set))
1384 {
1385 $this->set($set);
1386 }
WanWizard7219c072011-12-28 14:09:05 +01001387
Kyle Farris0c147b32011-08-26 02:29:31 -04001388 if ($this->_validate_insert($table) === FALSE)
1389 {
1390 return FALSE;
1391 }
WanWizard7219c072011-12-28 14:09:05 +01001392
Kyle Farris76116012011-08-31 11:17:48 -04001393 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001394 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001395 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001396 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001397 array_keys($this->qb_set),
1398 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001399 );
Barry Mienydd671972010-10-04 16:33:58 +02001400
Kyle Farris0c147b32011-08-26 02:29:31 -04001401 $this->_reset_write();
1402 return $this->query($sql);
1403 }
WanWizard7219c072011-12-28 14:09:05 +01001404
Kyle Farris0c147b32011-08-26 02:29:31 -04001405 // --------------------------------------------------------------------
1406
1407 /**
1408 * Validate Insert
1409 *
1410 * This method is used by both insert() and get_compiled_insert() to
1411 * validate that the there data is actually being set and that table
1412 * has been chosen to be inserted into.
1413 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001414 * @param string the table to insert data into
1415 * @return string
1416 */
WanWizard7219c072011-12-28 14:09:05 +01001417 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001418 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001419 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001420 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001421 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001422 }
1423
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001424 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001425 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001426 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001427 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001428 elseif ( ! isset($this->qb_from[0]))
1429 {
1430 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1431 }
WanWizard7219c072011-12-28 14:09:05 +01001432
Kyle Farris0c147b32011-08-26 02:29:31 -04001433 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001434 }
Barry Mienydd671972010-10-04 16:33:58 +02001435
Phil Sturgeon9789f322011-07-15 15:14:05 -06001436 // --------------------------------------------------------------------
1437
1438 /**
1439 * Replace
1440 *
1441 * Compiles an replace into string and runs the query
1442 *
1443 * @param string the table to replace data into
1444 * @param array an associative array of insert values
1445 * @return object
1446 */
1447 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001448 {
1449 if ( ! is_null($set))
1450 {
1451 $this->set($set);
1452 }
Barry Mienydd671972010-10-04 16:33:58 +02001453
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001454 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001455 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001456 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001457 }
1458
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001459 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001460 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001461 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001462 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001463 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001464 }
Barry Mienydd671972010-10-04 16:33:58 +02001465
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001466 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001467 }
1468
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001469 $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 +00001470
Derek Jonesd10e8962010-03-02 17:10:36 -06001471 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001472 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001473 }
WanWizard7219c072011-12-28 14:09:05 +01001474
Kyle Farris0c147b32011-08-26 02:29:31 -04001475 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001476
Kyle Farris0c147b32011-08-26 02:29:31 -04001477 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001478 * Replace statement
1479 *
1480 * Generates a platform-specific replace string from the supplied data
1481 *
1482 * @param string the table name
1483 * @param array the insert keys
1484 * @param array the insert values
1485 * @return string
1486 */
1487 protected function _replace($table, $keys, $values)
1488 {
1489 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1490 }
1491
1492 // --------------------------------------------------------------------
1493
1494 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001495 * FROM tables
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001496 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001497 * Groups tables in FROM clauses if needed, so there is no confusion
1498 * about operator precedence.
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001499 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001500 * Note: This is only used (and overriden) by MySQL and CUBRID.
1501 *
1502 * @return string
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001503 */
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001504 protected function _from_tables()
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001505 {
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001506 return implode(', ', $this->qb_from);
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001507 }
1508
1509 // --------------------------------------------------------------------
1510
1511 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001512 * Get UPDATE query string
1513 *
1514 * Compiles an update query and returns the sql
1515 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001516 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001517 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001518 * @return string
1519 */
1520 public function get_compiled_update($table = '', $reset = TRUE)
1521 {
1522 // Combine any cached components with the current statements
1523 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001524
Kyle Farris0c147b32011-08-26 02:29:31 -04001525 if ($this->_validate_update($table) === FALSE)
1526 {
1527 return FALSE;
1528 }
WanWizard7219c072011-12-28 14:09:05 +01001529
Andrey Andreevb0478652012-07-18 15:34:46 +03001530 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001531
Kyle Farris0c147b32011-08-26 02:29:31 -04001532 if ($reset === TRUE)
1533 {
1534 $this->_reset_write();
1535 }
WanWizard7219c072011-12-28 14:09:05 +01001536
Kyle Farris0c147b32011-08-26 02:29:31 -04001537 return $sql;
1538 }
WanWizard7219c072011-12-28 14:09:05 +01001539
Derek Allard2067d1a2008-11-13 22:59:24 +00001540 // --------------------------------------------------------------------
1541
1542 /**
1543 * Update
1544 *
1545 * Compiles an update string and runs the query
1546 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001547 * @param string $table = ''
1548 * @param array $set = NULL an associative array of update values
1549 * @param mixed $where = NULL
1550 * @param int $limit = NULL
Derek Allard2067d1a2008-11-13 22:59:24 +00001551 * @return object
1552 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001553 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001554 {
1555 // Combine any cached components with the current statements
1556 $this->_merge_cache();
1557
1558 if ( ! is_null($set))
1559 {
1560 $this->set($set);
1561 }
Barry Mienydd671972010-10-04 16:33:58 +02001562
Kyle Farris0c147b32011-08-26 02:29:31 -04001563 if ($this->_validate_update($table) === FALSE)
1564 {
1565 return FALSE;
1566 }
1567
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001568 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001569 {
1570 $this->where($where);
1571 }
1572
Andrey Andreev650b4c02012-06-11 12:07:15 +03001573 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001574 {
1575 $this->limit($limit);
1576 }
1577
Andrey Andreevb0478652012-07-18 15:34:46 +03001578 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
Kyle Farris0c147b32011-08-26 02:29:31 -04001579 $this->_reset_write();
1580 return $this->query($sql);
1581 }
WanWizard7219c072011-12-28 14:09:05 +01001582
Kyle Farris0c147b32011-08-26 02:29:31 -04001583 // --------------------------------------------------------------------
1584
1585 /**
1586 * Validate Update
1587 *
1588 * This method is used by both update() and get_compiled_update() to
1589 * validate that data is actually being set and that a table has been
1590 * chosen to be update.
1591 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001592 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001593 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001594 */
1595 protected function _validate_update($table = '')
1596 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001597 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001598 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001599 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001600 }
1601
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001602 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001603 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001604 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001605 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001606 elseif ( ! isset($this->qb_from[0]))
1607 {
1608 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1609 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001610
1611 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001612 }
WanWizard7219c072011-12-28 14:09:05 +01001613
Derek Jonesd10e8962010-03-02 17:10:36 -06001614 // --------------------------------------------------------------------
1615
1616 /**
1617 * Update_Batch
1618 *
1619 * Compiles an update string and runs the query
1620 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001621 * @param string the table to retrieve the results from
1622 * @param array an associative array of update values
1623 * @param string the where key
Andrey Andreev9f808b02012-10-24 17:38:48 +03001624 * @return int number of rows affected or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001625 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001626 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001627 {
1628 // Combine any cached components with the current statements
1629 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001630
Derek Jonesd10e8962010-03-02 17:10:36 -06001631 if (is_null($index))
1632 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001633 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001634 }
1635
1636 if ( ! is_null($set))
1637 {
1638 $this->set_update_batch($set, $index);
1639 }
1640
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001641 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001642 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001643 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001644 }
1645
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001646 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001647 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001648 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001649 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001650 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001651 }
Barry Mienydd671972010-10-04 16:33:58 +02001652
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001653 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001654 }
Barry Mienydd671972010-10-04 16:33:58 +02001655
Derek Jonesd10e8962010-03-02 17:10:36 -06001656 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001657 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001658 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001659 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001660 $this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, 100), $this->protect_identifiers($index)));
Andrey Andreev9f808b02012-10-24 17:38:48 +03001661 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001662 }
Barry Mienydd671972010-10-04 16:33:58 +02001663
Derek Jonesd10e8962010-03-02 17:10:36 -06001664 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001665 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001666 }
1667
1668 // --------------------------------------------------------------------
1669
1670 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001671 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001672 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001673 * @param array
1674 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001675 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001676 * @return object
1677 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001678 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001679 {
1680 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001681
Derek Jonesd10e8962010-03-02 17:10:36 -06001682 if ( ! is_array($key))
1683 {
1684 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001685 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001686
Andrey Andreevfe642da2012-06-16 03:47:33 +03001687 is_bool($escape) OR $escape = $this->_protect_identifiers;
1688
Derek Jonesd10e8962010-03-02 17:10:36 -06001689 foreach ($key as $k => $v)
1690 {
1691 $index_set = FALSE;
1692 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001693 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001694 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001695 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001696 {
1697 $index_set = TRUE;
1698 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001699
Andrey Andreevfe642da2012-06-16 03:47:33 +03001700 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001701 }
1702
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001703 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001704 {
1705 return $this->display_error('db_batch_missing_index');
1706 }
1707
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001708 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001709 }
Barry Mienydd671972010-10-04 16:33:58 +02001710
Derek Jonesd10e8962010-03-02 17:10:36 -06001711 return $this;
1712 }
1713
Derek Allard2067d1a2008-11-13 22:59:24 +00001714 // --------------------------------------------------------------------
1715
1716 /**
1717 * Empty Table
1718 *
1719 * Compiles a delete string and runs "DELETE FROM table"
1720 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001721 * @param string the table to empty
1722 * @return object
1723 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001724 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001725 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001726 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001727 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001728 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001729 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001730 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 }
1732
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001733 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001734 }
1735 else
1736 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001737 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001738 }
1739
1740 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001741 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 return $this->query($sql);
1743 }
1744
1745 // --------------------------------------------------------------------
1746
1747 /**
1748 * Truncate
1749 *
1750 * Compiles a truncate string and runs the query
1751 * If the database does not support the truncate() command
1752 * This function maps to "DELETE FROM table"
1753 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001754 * @param string the table to truncate
1755 * @return object
1756 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001757 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001758 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001759 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001760 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001761 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001762 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001763 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001764 }
1765
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001766 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001767 }
1768 else
1769 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001770 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001771 }
1772
1773 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001774 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001775 return $this->query($sql);
1776 }
WanWizard7219c072011-12-28 14:09:05 +01001777
Kyle Farris0c147b32011-08-26 02:29:31 -04001778 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001779
Kyle Farris0c147b32011-08-26 02:29:31 -04001780 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001781 * Truncate statement
1782 *
1783 * Generates a platform-specific truncate string from the supplied data
1784 *
1785 * If the database does not support the truncate() command,
1786 * then this method maps to 'DELETE FROM table'
1787 *
1788 * @param string the table name
1789 * @return string
1790 */
1791 protected function _truncate($table)
1792 {
1793 return 'TRUNCATE '.$table;
1794 }
1795
1796 // --------------------------------------------------------------------
1797
1798 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001799 * Get DELETE query string
1800 *
1801 * Compiles a delete query string and returns the sql
1802 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001803 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001804 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001805 * @return string
1806 */
1807 public function get_compiled_delete($table = '', $reset = TRUE)
1808 {
1809 $this->return_delete_sql = TRUE;
1810 $sql = $this->delete($table, '', NULL, $reset);
1811 $this->return_delete_sql = FALSE;
1812 return $sql;
1813 }
WanWizard7219c072011-12-28 14:09:05 +01001814
Derek Allard2067d1a2008-11-13 22:59:24 +00001815 // --------------------------------------------------------------------
1816
1817 /**
1818 * Delete
1819 *
1820 * Compiles a delete string and runs the query
1821 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001822 * @param mixed the table(s) to delete from. String or array
1823 * @param mixed the where clause
1824 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001825 * @param bool
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001826 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001828 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001829 {
1830 // Combine any cached components with the current statements
1831 $this->_merge_cache();
1832
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001833 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001834 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001835 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001836 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001837 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001838 }
1839
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001840 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001841 }
1842 elseif (is_array($table))
1843 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001844 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001845 {
Andrey Andreev13f50542012-10-12 12:31:02 +03001846 $this->delete($single_table, $where, $limit, $reset_data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001847 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001848 return;
1849 }
1850 else
1851 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001852 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001853 }
1854
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001855 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001856 {
1857 $this->where($where);
1858 }
1859
Andrey Andreev650b4c02012-06-11 12:07:15 +03001860 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001861 {
1862 $this->limit($limit);
1863 }
1864
Andrey Andreev94611df2012-07-19 12:29:54 +03001865 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001866 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001867 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001868 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001869
Andrey Andreevb0478652012-07-18 15:34:46 +03001870 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001871 if ($reset_data)
1872 {
1873 $this->_reset_write();
1874 }
WanWizard7219c072011-12-28 14:09:05 +01001875
Andrey Andreev24276a32012-01-08 02:44:38 +02001876 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001877 }
WanWizard7219c072011-12-28 14:09:05 +01001878
Derek Allard2067d1a2008-11-13 22:59:24 +00001879 // --------------------------------------------------------------------
1880
1881 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001882 * Delete statement
1883 *
1884 * Generates a platform-specific delete string from the supplied data
1885 *
1886 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03001887 * @return string
1888 */
Andrey Andreevb0478652012-07-18 15:34:46 +03001889 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03001890 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001891 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001892 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03001893 }
1894
1895 // --------------------------------------------------------------------
1896
1897 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001898 * DB Prefix
1899 *
1900 * Prepends a database prefix if one exists in configuration
1901 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001902 * @param string the table
1903 * @return string
1904 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001905 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001906 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001907 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001908 {
1909 $this->display_error('db_table_name_required');
1910 }
1911
1912 return $this->dbprefix.$table;
1913 }
1914
1915 // --------------------------------------------------------------------
1916
1917 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001918 * Set DB Prefix
1919 *
1920 * Set's the DB Prefix to something new without needing to reconnect
1921 *
1922 * @param string the prefix
1923 * @return string
1924 */
1925 public function set_dbprefix($prefix = '')
1926 {
1927 return $this->dbprefix = $prefix;
1928 }
1929
1930 // --------------------------------------------------------------------
1931
1932 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001933 * Track Aliases
1934 *
1935 * Used to track SQL statements written with aliased tables.
1936 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001937 * @param string The table to inspect
1938 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001939 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001940 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001941 {
1942 if (is_array($table))
1943 {
1944 foreach ($table as $t)
1945 {
1946 $this->_track_aliases($t);
1947 }
1948 return;
1949 }
Barry Mienydd671972010-10-04 16:33:58 +02001950
Derek Jones37f4b9c2011-07-01 17:56:50 -05001951 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001952 // the string into discreet statements
1953 if (strpos($table, ',') !== FALSE)
1954 {
1955 return $this->_track_aliases(explode(',', $table));
1956 }
Barry Mienydd671972010-10-04 16:33:58 +02001957
Derek Allard2067d1a2008-11-13 22:59:24 +00001958 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001959 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001960 {
1961 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03001962 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001963
Derek Allard2067d1a2008-11-13 22:59:24 +00001964 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001965 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001966
Derek Allard2067d1a2008-11-13 22:59:24 +00001967 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001968 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001969 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001970 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001971 }
1972 }
1973 }
WanWizard7219c072011-12-28 14:09:05 +01001974
Derek Allard2067d1a2008-11-13 22:59:24 +00001975 // --------------------------------------------------------------------
1976
1977 /**
1978 * Compile the SELECT statement
1979 *
1980 * Generates a query string based on which functions were used.
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001981 * Should not be called directly.
Derek Allard2067d1a2008-11-13 22:59:24 +00001982 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001983 * @param bool $select_override = FALSE
Derek Allard2067d1a2008-11-13 22:59:24 +00001984 * @return string
1985 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001986 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001987 {
1988 // Combine any cached components with the current statements
1989 $this->_merge_cache();
1990
Derek Allard2067d1a2008-11-13 22:59:24 +00001991 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001992 if ($select_override !== FALSE)
1993 {
1994 $sql = $select_override;
1995 }
1996 else
1997 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001998 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001999
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002000 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002001 {
Barry Mienydd671972010-10-04 16:33:58 +02002002 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002003 }
2004 else
Barry Mienydd671972010-10-04 16:33:58 +02002005 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002006 // Cycle through the "select" portion of the query and prep each column name.
2007 // The reason we protect identifiers here rather then in the select() function
2008 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002009 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002010 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002011 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002012 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002013 }
Barry Mienydd671972010-10-04 16:33:58 +02002014
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002015 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002016 }
2017 }
2018
Derek Allard2067d1a2008-11-13 22:59:24 +00002019 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002020 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002021 {
Andrey Andreeve78f8152012-10-09 11:38:38 +03002022 $sql .= "\nFROM ".$this->_from_tables();
Derek Allard2067d1a2008-11-13 22:59:24 +00002023 }
2024
Derek Allard2067d1a2008-11-13 22:59:24 +00002025 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002026 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002027 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002028 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002029 }
2030
Andrey Andreev2d486232012-07-19 14:46:51 +03002031 $sql .= $this->_compile_wh('qb_where')
2032 .$this->_compile_group_by()
2033 .$this->_compile_wh('qb_having')
2034 .$this->_compile_order_by(); // ORDER BY
Andrey Andreevb0478652012-07-18 15:34:46 +03002035
Andrey Andreevd40459d2012-07-18 16:46:39 +03002036 // LIMIT
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002037 if ($this->qb_limit)
Derek Allard2067d1a2008-11-13 22:59:24 +00002038 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002039 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002040 }
2041
2042 return $sql;
2043 }
2044
2045 // --------------------------------------------------------------------
2046
2047 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002048 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002049 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002050 * Escapes identifiers in WHERE and HAVING statements at execution time.
2051 *
Andrey Andreevb0478652012-07-18 15:34:46 +03002052 * Required so that aliases are tracked properly, regardless of wether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002053 * where(), or_where(), having(), or_having are called prior to from(),
2054 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002055 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002056 * @param string 'qb_where' or 'qb_having'
2057 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002058 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002059 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002060 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002061 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002062 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002063 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002064 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002065 if ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002066 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002067 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002068 continue;
2069 }
2070
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002071 // Split multiple conditions
2072 $conditions = preg_split(
2073 '/(\s*AND\s+|\s*OR\s+)/i',
2074 $this->{$qb_key}[$i]['condition'],
2075 -1,
2076 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
2077 );
2078
2079 for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002080 {
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002081 if (($op = $this->_get_operator($conditions[$ci])) === FALSE
Andrey Andreeve4742582012-10-25 13:25:13 +03002082 OR ! preg_match('/^(\(?)(.*)('.preg_quote($op, '/').')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches))
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002083 {
2084 continue;
2085 }
2086
2087 // $matches = array(
2088 // 0 => '(test <= foo)', /* the whole thing */
2089 // 1 => '(', /* optional */
2090 // 2 => 'test', /* the field name */
2091 // 3 => ' <= ', /* $op */
2092 // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2093 // 5 => ')' /* optional */
2094 // );
Andrey Andreev082aa402012-10-22 19:41:55 +03002095
2096 if ( ! empty($matches[4]))
2097 {
2098 $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4]));
2099 $matches[4] = ' '.$matches[4];
2100 }
2101
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002102 $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2]))
2103 .' '.trim($matches[3]).$matches[4].$matches[5];
Andrey Andreev6e704752012-07-18 00:46:33 +03002104 }
2105
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002106 $this->{$qb_key}[$i] = implode('', $conditions);
Andrey Andreev6e704752012-07-18 00:46:33 +03002107 }
2108
Andrey Andreev9d3aa1b2012-10-12 12:14:09 +03002109 return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ")
2110 .implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002111 }
2112
Andrey Andreevb0478652012-07-18 15:34:46 +03002113 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002114 }
2115
2116 // --------------------------------------------------------------------
2117
2118 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002119 * Compile GROUP BY
2120 *
2121 * Escapes identifiers in GROUP BY statements at execution time.
2122 *
2123 * Required so that aliases are tracked properly, regardless of wether
2124 * group_by() is called prior to from(), join() and dbprefix is added
2125 * only if needed.
2126 *
2127 * @return string SQL statement
2128 */
2129 protected function _compile_group_by()
2130 {
2131 if (count($this->qb_groupby) > 0)
2132 {
Andrey Andreev96feb582012-07-19 13:12:34 +03002133 for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
2134 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002135 $this->qb_groupby[$i] = ($this->qb_groupby[$i]['escape'] === FALSE OR $this->_is_literal($this->qb_groupby[$i]['field']))
Andrey Andreev96feb582012-07-19 13:12:34 +03002136 ? $this->qb_groupby[$i]['field']
Andrey Andreev13f50542012-10-12 12:31:02 +03002137 : $this->protect_identifiers($this->qb_groupby[$i]['field']);
Andrey Andreev96feb582012-07-19 13:12:34 +03002138 }
2139
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002140 return "\nGROUP BY ".implode(', ', $this->qb_groupby);
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002141 }
2142
2143 return '';
2144 }
2145
2146 // --------------------------------------------------------------------
2147
2148 /**
Andrey Andreev2d486232012-07-19 14:46:51 +03002149 * Compile ORDER BY
2150 *
2151 * Escapes identifiers in ORDER BY statements at execution time.
2152 *
2153 * Required so that aliases are tracked properly, regardless of wether
2154 * order_by() is called prior to from(), join() and dbprefix is added
2155 * only if needed.
2156 *
2157 * @return string SQL statement
2158 */
2159 protected function _compile_order_by()
2160 {
Andrey Andreeva53ea842012-10-23 12:44:09 +03002161 if (is_array($this->qb_orderby) && count($this->qb_orderby) > 0)
Andrey Andreev2d486232012-07-19 14:46:51 +03002162 {
Andrey Andreev2d486232012-07-19 14:46:51 +03002163 for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++)
2164 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002165 if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field']))
Andrey Andreev2d486232012-07-19 14:46:51 +03002166 {
Andrey Andreevfc043b32012-10-12 14:46:14 +03002167 $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']);
Andrey Andreev2d486232012-07-19 14:46:51 +03002168 }
2169
2170 $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction'];
2171 }
2172
Andrey Andreeva53ea842012-10-23 12:44:09 +03002173 return $this->qb_orderby = "\nORDER BY ".implode(', ', $this->qb_orderby);
2174 }
2175 elseif (is_string($this->qb_orderby))
2176 {
2177 return $this->qb_orderby;
Andrey Andreev2d486232012-07-19 14:46:51 +03002178 }
2179
2180 return '';
2181 }
2182
2183 // --------------------------------------------------------------------
2184
2185 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002186 * Object to Array
2187 *
2188 * Takes an object as input and converts the class variables to array key/vals
2189 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002190 * @param object
2191 * @return array
2192 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002193 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002194 {
2195 if ( ! is_object($object))
2196 {
2197 return $object;
2198 }
Barry Mienydd671972010-10-04 16:33:58 +02002199
Derek Allard2067d1a2008-11-13 22:59:24 +00002200 $array = array();
2201 foreach (get_object_vars($object) as $key => $val)
2202 {
2203 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002204 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002205 {
2206 $array[$key] = $val;
2207 }
2208 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002209
2210 return $array;
2211 }
Barry Mienydd671972010-10-04 16:33:58 +02002212
Derek Jonesd10e8962010-03-02 17:10:36 -06002213 // --------------------------------------------------------------------
2214
2215 /**
2216 * Object to Array
2217 *
2218 * Takes an object as input and converts the class variables to array key/vals
2219 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002220 * @param object
2221 * @return array
2222 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002223 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002224 {
2225 if ( ! is_object($object))
2226 {
2227 return $object;
2228 }
Barry Mienydd671972010-10-04 16:33:58 +02002229
Derek Jonesd10e8962010-03-02 17:10:36 -06002230 $array = array();
2231 $out = get_object_vars($object);
2232 $fields = array_keys($out);
2233
2234 foreach ($fields as $val)
2235 {
2236 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002237 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002238 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002239 $i = 0;
2240 foreach ($out[$val] as $data)
2241 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002242 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002243 }
2244 }
2245 }
2246
Derek Allard2067d1a2008-11-13 22:59:24 +00002247 return $array;
2248 }
Barry Mienydd671972010-10-04 16:33:58 +02002249
Derek Allard2067d1a2008-11-13 22:59:24 +00002250 // --------------------------------------------------------------------
2251
2252 /**
2253 * Start Cache
2254 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002255 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002256 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002257 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002258 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002259 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002260 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002261 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002262 }
2263
2264 // --------------------------------------------------------------------
2265
2266 /**
2267 * Stop Cache
2268 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002269 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002270 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002271 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002272 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002273 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002274 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002275 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002276 }
2277
2278 // --------------------------------------------------------------------
2279
2280 /**
2281 * Flush Cache
2282 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002283 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002284 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002285 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002286 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002287 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002288 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002289 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002290 'qb_cache_select' => array(),
2291 'qb_cache_from' => array(),
2292 'qb_cache_join' => array(),
2293 'qb_cache_where' => array(),
2294 'qb_cache_like' => array(),
2295 'qb_cache_groupby' => array(),
2296 'qb_cache_having' => array(),
2297 'qb_cache_orderby' => array(),
2298 'qb_cache_set' => array(),
2299 'qb_cache_exists' => array(),
2300 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002301 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002302 }
2303
2304 // --------------------------------------------------------------------
2305
2306 /**
2307 * Merge Cache
2308 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002309 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002310 * locally called ones.
2311 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002312 * @return void
2313 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002314 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002315 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002316 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002317 {
2318 return;
2319 }
2320
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002321 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002322 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002323 $qb_variable = 'qb_'.$val;
2324 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002325
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002326 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002327 {
2328 continue;
2329 }
2330
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002331 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002332 }
2333
2334 // If we are "protecting identifiers" we need to examine the "from"
2335 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002336 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002337 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002338 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002339 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002340
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002341 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002342 }
WanWizard7219c072011-12-28 14:09:05 +01002343
Kyle Farris0c147b32011-08-26 02:29:31 -04002344 // --------------------------------------------------------------------
2345
2346 /**
Andrey Andreev082aa402012-10-22 19:41:55 +03002347 * Is literal
2348 *
2349 * Determines if a string represents a literal value or a field name
2350 *
2351 * @param string
2352 * @return bool
2353 */
2354 protected function _is_literal($str)
2355 {
2356 $str = trim($str);
2357
2358 if (empty($str))
2359 {
2360 return TRUE;
2361 }
2362
2363 static $_str;
2364
2365 if (empty($_str))
2366 {
2367 $_str = ($this->_escape_char !== '"')
2368 ? array('"', "'") : array("'");
2369 }
2370
2371 return (ctype_digit($str) OR in_array($str[0], $_str, TRUE));
2372 }
2373
2374 // --------------------------------------------------------------------
2375
2376 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002377 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002378 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002379 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002380 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002381 * @return void
2382 */
2383 public function reset_query()
2384 {
2385 $this->_reset_select();
2386 $this->_reset_write();
2387 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002388
2389 // --------------------------------------------------------------------
2390
2391 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002392 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002393 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002394 * @param array An array of fields to reset
2395 * @return void
2396 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002397 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002398 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002399 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002400 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002401 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002402 {
2403 $this->$item = $default_value;
2404 }
2405 }
2406 }
2407
2408 // --------------------------------------------------------------------
2409
2410 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002411 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002412 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002413 * @return void
2414 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002415 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002416 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002417 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002418 'qb_select' => array(),
2419 'qb_from' => array(),
2420 'qb_join' => array(),
2421 'qb_where' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002422 'qb_groupby' => array(),
2423 'qb_having' => array(),
2424 'qb_orderby' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002425 'qb_aliased_tables' => array(),
2426 'qb_no_escape' => array(),
2427 'qb_distinct' => FALSE,
2428 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002429 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002430 )
2431 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002432 }
Barry Mienydd671972010-10-04 16:33:58 +02002433
Derek Allard2067d1a2008-11-13 22:59:24 +00002434 // --------------------------------------------------------------------
2435
2436 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002437 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002438 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002439 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002440 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002441 * @return void
2442 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002443 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002444 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002445 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002446 'qb_set' => array(),
2447 'qb_from' => array(),
2448 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002449 'qb_orderby' => array(),
2450 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002451 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002452 )
2453 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002454 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002455
Derek Allard2067d1a2008-11-13 22:59:24 +00002456}
2457
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002458/* End of file DB_query_builder.php */
Andrey Andreev58803fb2012-06-24 00:45:37 +03002459/* Location: ./system/database/DB_query_builder.php */