blob: fc2d5901e5cf8a46bf70b12474223af4fe437fc6 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
WanWizard7219c072011-12-28 14:09:05 +01008 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
WanWizard7219c072011-12-28 14:09:05 +010010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
Jamie Rumbelow7efad202012-02-19 12:37:00 +000041 * Query Builder Class
Derek Allard2067d1a2008-11-13 22:59:24 +000042 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +000043 * This is the platform-independent base Query Builder implementation class.
Derek Allard2067d1a2008-11-13 22:59:24 +000044 *
45 * @package CodeIgniter
46 * @subpackage Drivers
47 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050048 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000049 * @link http://codeigniter.com/user_guide/database/
50 */
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +010051
52abstract class CI_DB_query_builder extends CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000053
Andrey Andreevae85eb42012-11-02 01:42:31 +020054 /**
55 * Return DELETE SQL flag
56 *
57 * @var bool
58 */
WanWizard7219c072011-12-28 14:09:05 +010059 protected $return_delete_sql = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +020060
61 /**
62 * Reset DELETE data flag
63 *
64 * @var bool
65 */
WanWizard7219c072011-12-28 14:09:05 +010066 protected $reset_delete_data = FALSE;
67
Andrey Andreevae85eb42012-11-02 01:42:31 +020068 /**
69 * QB SELECT data
70 *
71 * @var array
72 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +000073 protected $qb_select = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +020074
75 /**
76 * QB DISTINCT flag
77 *
78 * @var bool
79 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +000080 protected $qb_distinct = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +020081
82 /**
83 * QB FROM data
84 *
85 * @var array
86 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +000087 protected $qb_from = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +020088
89 /**
90 * QB JOIN data
91 *
92 * @var array
93 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +000094 protected $qb_join = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +020095
96 /**
97 * QB WHERE data
98 *
99 * @var array
100 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000101 protected $qb_where = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200102
103 /**
104 * QB GROUP BY data
105 *
106 * @var array
107 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000108 protected $qb_groupby = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200109
110 /**
111 * QB HAVING data
112 *
113 * @var array
114 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000115 protected $qb_having = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200116
117 /**
118 * QB keys
119 *
120 * @var array
121 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000122 protected $qb_keys = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200123
124 /**
125 * QB LIMIT data
126 *
127 * @var int
128 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000129 protected $qb_limit = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200130
131 /**
132 * QB OFFSET data
133 *
134 * @var int
135 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000136 protected $qb_offset = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200137
138 /**
139 * QB ORDER BY data
140 *
141 * @var array
142 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000143 protected $qb_orderby = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200144
145 /**
146 * QB data sets
147 *
148 * @var array
149 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000150 protected $qb_set = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200151
152 /**
153 * QB aliased tables list
154 *
155 * @var array
156 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000157 protected $qb_aliased_tables = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200158
159 /**
160 * QB WHERE group started flag
161 *
162 * @var bool
163 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000164 protected $qb_where_group_started = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200165
166 /**
167 * QB WHERE group count
168 *
169 * @var int
170 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000171 protected $qb_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +0200172
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000173 // Query Builder Caching variables
Andrey Andreevae85eb42012-11-02 01:42:31 +0200174
175 /**
176 * QB Caching flag
177 *
178 * @var bool
179 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000180 protected $qb_caching = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200181
182 /**
183 * QB Cache exists list
184 *
185 * @var array
186 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000187 protected $qb_cache_exists = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200188
189 /**
190 * QB Cache SELECT data
191 *
192 * @var array
193 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000194 protected $qb_cache_select = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200195
196 /**
197 * QB Cache FROM data
198 *
199 * @var array
200 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000201 protected $qb_cache_from = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200202
203 /**
204 * QB Cache JOIN data
205 *
206 * @var array
207 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000208 protected $qb_cache_join = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200209
210 /**
211 * QB Cache WHERE data
212 *
213 * @var array
214 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000215 protected $qb_cache_where = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200216
217 /**
218 * QB Cache GROUP BY data
219 *
220 * @var array
221 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000222 protected $qb_cache_groupby = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200223
224 /**
225 * QB Cache HAVING data
226 *
227 * @var array
228 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000229 protected $qb_cache_having = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200230
231 /**
232 * QB Cache ORDER BY data
233 *
234 * @var array
235 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000236 protected $qb_cache_orderby = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200237
238 /**
239 * QB Cache data sets
240 *
241 * @var array
242 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000243 protected $qb_cache_set = array();
WanWizard7219c072011-12-28 14:09:05 +0100244
Andrey Andreevae85eb42012-11-02 01:42:31 +0200245 /**
246 * QB No Escape data
247 *
248 * @var array
249 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000250 protected $qb_no_escape = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200251
252 /**
253 * QB Cache No Escape data
254 *
255 * @var array
256 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000257 protected $qb_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000258
Andrey Andreevae85eb42012-11-02 01:42:31 +0200259 // --------------------------------------------------------------------
260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 /**
262 * Select
263 *
264 * Generates the SELECT portion of the query
265 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300267 * @param mixed
Andrew Podner4296a652012-12-17 07:51:15 -0500268 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600270 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 if (is_string($select))
273 {
274 $select = explode(',', $select);
275 }
276
Vivek Dinesh33578d22014-02-06 09:46:07 +0530277 // If the escape value was not set, we will base it on the global setting
Andrey Andreev42870232012-06-12 01:30:20 +0300278 is_bool($escape) OR $escape = $this->_protect_identifiers;
279
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 foreach ($select as $val)
281 {
282 $val = trim($val);
283
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100284 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000286 $this->qb_select[] = $val;
287 $this->qb_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000288
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000289 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000291 $this->qb_cache_select[] = $val;
292 $this->qb_cache_exists[] = 'select';
293 $this->qb_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
295 }
296 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 return $this;
299 }
300
301 // --------------------------------------------------------------------
302
303 /**
304 * Select Max
305 *
306 * Generates a SELECT MAX(field) portion of a query
307 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 * @param string the field
309 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500310 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600312 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
314 return $this->_max_min_avg_sum($select, $alias, 'MAX');
315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // --------------------------------------------------------------------
318
319 /**
320 * Select Min
321 *
322 * Generates a SELECT MIN(field) portion of a query
323 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * @param string the field
325 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500326 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600328 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
330 return $this->_max_min_avg_sum($select, $alias, 'MIN');
331 }
332
333 // --------------------------------------------------------------------
334
335 /**
336 * Select Average
337 *
338 * Generates a SELECT AVG(field) portion of a query
339 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 * @param string the field
341 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500342 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600344 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
346 return $this->_max_min_avg_sum($select, $alias, 'AVG');
347 }
348
349 // --------------------------------------------------------------------
350
351 /**
352 * Select Sum
353 *
354 * Generates a SELECT SUM(field) portion of a query
355 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 * @param string the field
357 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500358 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600360 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
362 return $this->_max_min_avg_sum($select, $alias, 'SUM');
363 }
364
365 // --------------------------------------------------------------------
366
367 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200368 * SELECT [MAX|MIN|AVG|SUM]()
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200370 * @used-by select_max()
371 * @used-by select_min()
372 * @used-by select_avg()
373 * @used-by select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200374 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200375 * @param string $select Field name
376 * @param string $alias
377 * @param string $type
Andrew Podner4296a652012-12-17 07:51:15 -0500378 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600380 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100382 if ( ! is_string($select) OR $select === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
384 $this->display_error('db_invalid_query');
385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
390 {
391 show_error('Invalid function type: '.$type);
392 }
Barry Mienydd671972010-10-04 16:33:58 +0200393
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100394 if ($alias === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
396 $alias = $this->_create_alias_from_table(trim($select));
397 }
Barry Mienydd671972010-10-04 16:33:58 +0200398
Andrey Andreev5b55c152013-08-06 14:14:32 +0300399 $sql = $type.'('.$this->protect_identifiers(trim($select)).') AS '.$this->escape_identifiers(trim($alias));
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300400
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000401 $this->qb_select[] = $sql;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100402 $this->qb_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200403
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000404 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000406 $this->qb_cache_select[] = $sql;
407 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 }
Barry Mienydd671972010-10-04 16:33:58 +0200409
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 return $this;
411 }
412
413 // --------------------------------------------------------------------
414
415 /**
416 * Determines the alias name based on the table
417 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200418 * @param string $item
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 * @return string
420 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600421 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
423 if (strpos($item, '.') !== FALSE)
424 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200425 $item = explode('.', $item);
426 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 }
Barry Mienydd671972010-10-04 16:33:58 +0200428
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 return $item;
430 }
431
432 // --------------------------------------------------------------------
433
434 /**
435 * DISTINCT
436 *
437 * Sets a flag which tells the query string compiler to add DISTINCT
438 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200439 * @param bool $val
Andrew Podner4296a652012-12-17 07:51:15 -0500440 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600442 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300444 $this->qb_distinct = is_bool($val) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 return $this;
446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 // --------------------------------------------------------------------
449
450 /**
451 * From
452 *
453 * Generates the FROM portion of the query
454 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200455 * @param mixed $from can be a string or array
Andrew Podner4296a652012-12-17 07:51:15 -0500456 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600458 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300460 foreach ((array) $from as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
462 if (strpos($val, ',') !== FALSE)
463 {
464 foreach (explode(',', $val) as $v)
465 {
466 $v = trim($v);
467 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200468
George Petsagourakis193d4482012-04-28 11:16:18 +0300469 $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000470
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000471 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000473 $this->qb_cache_from[] = $v;
474 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200475 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 }
478 else
479 {
480 $val = trim($val);
481
Andrey Andreev24276a32012-01-08 02:44:38 +0200482 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000483 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200485
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000486 $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000487
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000488 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000490 $this->qb_cache_from[] = $val;
491 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 }
493 }
494 }
495
496 return $this;
497 }
498
499 // --------------------------------------------------------------------
500
501 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200502 * JOIN
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 *
504 * Generates the JOIN portion of the query
505 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 * @param string
507 * @param string the join condition
508 * @param string the type of join
Alex Bilbief512b732012-06-16 11:15:19 +0100509 * @param string whether not to try to escape identifiers
Andrew Podner4296a652012-12-17 07:51:15 -0500510 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300512 public function join($table, $cond, $type = '', $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200513 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100514 if ($type !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
516 $type = strtoupper(trim($type));
517
Andrey Andreev42870232012-06-12 01:30:20 +0300518 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 {
520 $type = '';
521 }
522 else
523 {
524 $type .= ' ';
525 }
526 }
527
Andrey Andreev24276a32012-01-08 02:44:38 +0200528 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000529 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 $this->_track_aliases($table);
531
Andrey Andreevfe642da2012-06-16 03:47:33 +0300532 is_bool($escape) OR $escape = $this->_protect_identifiers;
533
Andrey Andreev42870232012-06-12 01:30:20 +0300534 // Split multiple conditions
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300535 if ($escape === TRUE && preg_match_all('/\sAND\s|\sOR\s/i', $cond, $m, PREG_OFFSET_CAPTURE))
Andrey Andreev42870232012-06-12 01:30:20 +0300536 {
537 $newcond = '';
538 $m[0][] = array('', strlen($cond));
539
540 for ($i = 0, $c = count($m[0]), $s = 0;
541 $i < $c;
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300542 $s = $m[0][$i][1] + strlen($m[0][$i][0]), $i++)
Andrey Andreev42870232012-06-12 01:30:20 +0300543 {
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300544 $temp = substr($cond, $s, ($m[0][$i][1] - $s));
Andrey Andreev42870232012-06-12 01:30:20 +0300545
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300546 $newcond .= preg_match("/([\[\]\w\.'-]+)(\s*[^\"\[`'\w]+\s*)(.+)/i", $temp, $match)
Andrey Andreev42870232012-06-12 01:30:20 +0300547 ? $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3])
548 : $temp;
549
550 $newcond .= $m[0][$i][0];
551 }
552
Andrey Andreev3751f932012-06-17 18:07:48 +0300553 $cond = ' ON '.$newcond;
Andrey Andreev42870232012-06-12 01:30:20 +0300554 }
555 // Split apart the condition and protect the identifiers
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300556 elseif ($escape === TRUE && preg_match("/([\[\]\w\.'-]+)(\s*[^\"\[`'\w]+\s*)(.+)/i", $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 {
Andrey Andreev3751f932012-06-17 18:07:48 +0300558 $cond = ' ON '.$this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
559 }
560 elseif ( ! $this->_has_operator($cond))
561 {
562 $cond = ' USING ('.($escape ? $this->escape_identifiers($cond) : $cond).')';
563 }
564 else
565 {
566 $cond = ' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 }
Barry Mienydd671972010-10-04 16:33:58 +0200568
Andrey Andreev42870232012-06-12 01:30:20 +0300569 // Do we want to escape the table name?
570 if ($escape === TRUE)
571 {
572 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
573 }
574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 // Assemble the JOIN statement
Andrey Andreev3751f932012-06-17 18:07:48 +0300576 $this->qb_join[] = $join = $type.'JOIN '.$table.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000577
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000578 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000580 $this->qb_cache_join[] = $join;
581 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 }
583
584 return $this;
585 }
586
587 // --------------------------------------------------------------------
588
589 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200590 * WHERE
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200592 * Generates the WHERE portion of the query.
593 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 * @param mixed
596 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300597 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500598 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300600 public function where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300602 return $this->_wh('qb_where', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 }
Barry Mienydd671972010-10-04 16:33:58 +0200604
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 // --------------------------------------------------------------------
606
607 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200608 * OR WHERE
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200610 * Generates the WHERE portion of the query.
611 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 * @param mixed
614 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300615 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500616 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300618 public function or_where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300620 return $this->_wh('qb_where', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 }
622
623 // --------------------------------------------------------------------
624
625 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +0300626 * WHERE, HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200628 * @used-by where()
629 * @used-by or_where()
630 * @used-by having()
631 * @used-by or_having()
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200633 * @param string $qb_key 'qb_where' or 'qb_having'
634 * @param mixed $key
635 * @param mixed $value
636 * @param string $type
637 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500638 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 */
Andrey Andreevd40459d2012-07-18 16:46:39 +0300640 protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300642 $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where';
643
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 if ( ! is_array($key))
645 {
646 $key = array($key => $value);
647 }
Barry Mienydd671972010-10-04 16:33:58 +0200648
Rougin Royce Gutib191550a2014-08-24 16:19:08 +0800649 // If the escape value was not set will base it on the global setting
Andrey Andreeve10fb792012-06-15 12:07:04 +0300650 is_bool($escape) OR $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000651
652 foreach ($key as $k => $v)
653 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300654 $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0)
Andrey Andreev58803fb2012-06-24 00:45:37 +0300655 ? $this->_group_get_type('')
656 : $this->_group_get_type($type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000657
vlakoff1228fe22013-01-14 01:30:09 +0100658 if ($v !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 {
Andrey Andreev43afc712015-07-20 12:32:02 +0300660 $v = ' '.$this->escape($v);
WanWizard7219c072011-12-28 14:09:05 +0100661
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 if ( ! $this->_has_operator($k))
663 {
Greg Akere156c6e2011-04-20 16:03:04 -0500664 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 }
666 }
Andrey Andreev3a5efc22012-11-20 21:18:08 +0200667 elseif ( ! $this->_has_operator($k))
668 {
669 // value appears not to have been set, assign the test to IS NULL
670 $k .= ' IS NULL';
671 }
Andrey Andreev5078eb52014-12-02 01:11:54 +0200672 elseif (preg_match('/\s*(!?=|<>|IS(?:\s+NOT)?)\s*$/i', $k, $match, PREG_OFFSET_CAPTURE))
Andrey Andreev5bf4dcd2014-09-29 20:07:15 +0300673 {
674 $k = substr($k, 0, $match[0][1]).($match[1][0] === '=' ? ' IS NULL' : ' IS NOT NULL');
675 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000676
Andrey Andreevd40459d2012-07-18 16:46:39 +0300677 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000678 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300680 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
681 $this->qb_cache_exists[] = substr($qb_key, 3);
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 }
Barry Mienydd671972010-10-04 16:33:58 +0200683
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 }
Barry Mienydd671972010-10-04 16:33:58 +0200685
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 return $this;
687 }
688
689 // --------------------------------------------------------------------
690
691 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200692 * WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200694 * Generates a WHERE field IN('item', 'item') SQL query,
695 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200697 * @param string $key The field to search
698 * @param array $values The values searched on
699 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500700 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300702 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300704 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 }
Barry Mienydd671972010-10-04 16:33:58 +0200706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 // --------------------------------------------------------------------
708
709 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200710 * OR WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200712 * Generates a WHERE field IN('item', 'item') SQL query,
713 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200715 * @param string $key The field to search
716 * @param array $values The values searched on
717 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500718 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300720 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300722 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 }
724
725 // --------------------------------------------------------------------
726
727 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200728 * WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200730 * Generates a WHERE field NOT IN('item', 'item') SQL query,
731 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200733 * @param string $key The field to search
734 * @param array $values The values searched on
735 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500736 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300738 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300740 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 }
Barry Mienydd671972010-10-04 16:33:58 +0200742
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 // --------------------------------------------------------------------
744
745 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200746 * OR WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200748 * Generates a WHERE field NOT IN('item', 'item') SQL query,
749 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200751 * @param string $key The field to search
752 * @param array $values The values searched on
753 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500754 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300756 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300758 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 }
760
761 // --------------------------------------------------------------------
762
763 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200764 * Internal WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200766 * @used-by where_in()
767 * @used-by or_where_in()
768 * @used-by where_not_in()
769 * @used-by or_where_not_in()
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200771 * @param string $key The field to search
772 * @param array $values The values searched on
773 * @param bool $not If the statement would be IN or NOT IN
774 * @param string $type
775 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500776 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300778 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 {
780 if ($key === NULL OR $values === NULL)
781 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300782 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 }
Barry Mienydd671972010-10-04 16:33:58 +0200784
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 if ( ! is_array($values))
786 {
787 $values = array($values);
788 }
Barry Mienydd671972010-10-04 16:33:58 +0200789
Andrey Andreev498c1e02012-06-16 03:34:10 +0300790 is_bool($escape) OR $escape = $this->_protect_identifiers;
791
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 $not = ($not) ? ' NOT' : '';
793
Andrey Andreev94611df2012-07-19 12:29:54 +0300794 $where_in = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 foreach ($values as $value)
796 {
Andrey Andreevcc02db92012-10-12 14:30:10 +0300797 $where_in[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 }
799
WanWizardbc69f362012-06-22 00:10:11 +0200800 $prefix = (count($this->qb_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Andrey Andreev6e704752012-07-18 00:46:33 +0300801 $where_in = array(
Andrey Andreev94611df2012-07-19 12:29:54 +0300802 'condition' => $prefix.$key.$not.' IN('.implode(', ', $where_in).')',
Andrey Andreev6e704752012-07-18 00:46:33 +0300803 'escape' => $escape
804 );
Barry Mienydd671972010-10-04 16:33:58 +0200805
Andrey Andreev6e704752012-07-18 00:46:33 +0300806 $this->qb_where[] = $where_in;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000807 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000809 $this->qb_cache_where[] = $where_in;
810 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 }
812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 return $this;
814 }
Barry Mienydd671972010-10-04 16:33:58 +0200815
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 // --------------------------------------------------------------------
817
818 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200819 * LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200821 * Generates a %LIKE% portion of the query.
822 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200824 * @param mixed $field
825 * @param string $match
826 * @param string $side
827 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500828 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300830 public function like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300832 return $this->_like($field, $match, 'AND ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 }
834
835 // --------------------------------------------------------------------
836
837 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200838 * NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200840 * Generates a NOT LIKE portion of the query.
841 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200843 * @param mixed $field
844 * @param string $match
845 * @param string $side
846 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500847 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300849 public function not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300851 return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 }
Barry Mienydd671972010-10-04 16:33:58 +0200853
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 // --------------------------------------------------------------------
855
856 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200857 * OR LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200859 * Generates a %LIKE% portion of the query.
860 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200862 * @param mixed $field
863 * @param string $match
864 * @param string $side
865 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500866 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300868 public function or_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300870 return $this->_like($field, $match, 'OR ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 }
872
873 // --------------------------------------------------------------------
874
875 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200876 * OR NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200878 * Generates a NOT LIKE portion of the query.
879 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200881 * @param mixed $field
882 * @param string $match
883 * @param string $side
884 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500885 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300887 public function or_not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300889 return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 }
Barry Mienydd671972010-10-04 16:33:58 +0200891
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 // --------------------------------------------------------------------
893
894 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200895 * Internal LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200897 * @used-by like()
898 * @used-by or_like()
899 * @used-by not_like()
900 * @used-by or_not_like()
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200902 * @param mixed $field
903 * @param string $match
904 * @param string $type
905 * @param string $side
906 * @param string $not
907 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500908 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300910 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 {
912 if ( ! is_array($field))
913 {
914 $field = array($field => $match);
915 }
Barry Mienydd671972010-10-04 16:33:58 +0200916
Andrey Andreevb0478652012-07-18 15:34:46 +0300917 is_bool($escape) OR $escape = $this->_protect_identifiers;
Andrey Andreev19311362015-04-07 00:02:14 +0300918 // lowercase $side in case somebody writes e.g. 'BEFORE' instead of 'before' (doh)
919 $side = strtolower($side);
Andrey Andreevb0478652012-07-18 15:34:46 +0300920
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 foreach ($field as $k => $v)
922 {
Andrey Andreev41738232012-11-30 00:13:17 +0200923 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
924 ? $this->_group_get_type('') : $this->_group_get_type($type);
925
Derek Jonese4ed5832009-02-20 21:44:59 +0000926 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300927
Andrey Andreev24276a32012-01-08 02:44:38 +0200928 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400929 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300930 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400931 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200932 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300934 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200936 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300938 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 }
940 else
941 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300942 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600944
Derek Jonese4ed5832009-02-20 21:44:59 +0000945 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100946 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000947 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300948 $like_statement .= sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000949 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600950
Andrey Andreevb0478652012-07-18 15:34:46 +0300951 $this->qb_where[] = array('condition' => $like_statement, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000952 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 {
Andrey Andreev55bbd722013-01-28 19:02:13 +0200954 $this->qb_cache_where[] = array('condition' => $like_statement, 'escape' => $escape);
Andrey Andreevededc4a2012-07-18 01:16:15 +0300955 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 return $this;
960 }
Barry Mienydd671972010-10-04 16:33:58 +0200961
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 // --------------------------------------------------------------------
963
964 /**
WanWizard7219c072011-12-28 14:09:05 +0100965 * Starts a query group.
966 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200967 * @param string $not (Internal use only)
968 * @param string $type (Internal use only)
Andrew Podner4296a652012-12-17 07:51:15 -0500969 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +0100970 */
971 public function group_start($not = '', $type = 'AND ')
972 {
973 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100974
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000975 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100976 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Andrey Andreev6e704752012-07-18 00:46:33 +0300977 $where = array(
978 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (',
979 'escape' => FALSE
980 );
WanWizard7219c072011-12-28 14:09:05 +0100981
Andrey Andreev6e704752012-07-18 00:46:33 +0300982 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000983 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100984 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300985 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +0100986 }
987
988 return $this;
989 }
990
991 // --------------------------------------------------------------------
992
993 /**
994 * Starts a query group, but ORs the group
995 *
Andrew Podner4296a652012-12-17 07:51:15 -0500996 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +0100997 */
998 public function or_group_start()
999 {
1000 return $this->group_start('', 'OR ');
1001 }
1002
1003 // --------------------------------------------------------------------
1004
1005 /**
1006 * Starts a query group, but NOTs the group
1007 *
Andrew Podner4296a652012-12-17 07:51:15 -05001008 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001009 */
1010 public function not_group_start()
1011 {
1012 return $this->group_start('NOT ', 'AND ');
1013 }
1014
1015 // --------------------------------------------------------------------
1016
1017 /**
1018 * Starts a query group, but OR NOTs the group
1019 *
Andrew Podner4296a652012-12-17 07:51:15 -05001020 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001021 */
1022 public function or_not_group_start()
1023 {
1024 return $this->group_start('NOT ', 'OR ');
1025 }
1026
1027 // --------------------------------------------------------------------
1028
1029 /**
1030 * Ends a query group
1031 *
Andrew Podner4296a652012-12-17 07:51:15 -05001032 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001033 */
1034 public function group_end()
1035 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001036 $this->qb_where_group_started = FALSE;
Andrey Andreev6e704752012-07-18 00:46:33 +03001037 $where = array(
1038 'condition' => str_repeat(' ', $this->qb_where_group_count--).')',
1039 'escape' => FALSE
1040 );
WanWizard7219c072011-12-28 14:09:05 +01001041
Andrey Andreev6e704752012-07-18 00:46:33 +03001042 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001043 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +01001044 {
Andrey Andreev6e704752012-07-18 00:46:33 +03001045 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +01001046 }
1047
WanWizard7219c072011-12-28 14:09:05 +01001048 return $this;
1049 }
1050
1051 // --------------------------------------------------------------------
1052
1053 /**
1054 * Group_get_type
1055 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001056 * @used-by group_start()
1057 * @used-by _like()
1058 * @used-by _wh()
1059 * @used-by _where_in()
WanWizard7219c072011-12-28 14:09:05 +01001060 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001061 * @param string $type
WanWizard7219c072011-12-28 14:09:05 +01001062 * @return string
1063 */
1064 protected function _group_get_type($type)
1065 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001066 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +01001067 {
1068 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001069 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +01001070 }
1071
1072 return $type;
1073 }
1074
1075 // --------------------------------------------------------------------
1076
1077 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 * GROUP BY
1079 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001080 * @param string $by
1081 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -05001082 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001084 public function group_by($by, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001086 is_bool($escape) OR $escape = $this->_protect_identifiers;
1087
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 if (is_string($by))
1089 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001090 $by = ($escape === TRUE)
1091 ? explode(',', $by)
1092 : array($by);
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 }
Barry Mienydd671972010-10-04 16:33:58 +02001094
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 foreach ($by as $val)
1096 {
1097 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +02001098
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001099 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001101 $val = array('field' => $val, 'escape' => $escape);
Barry Mienydd671972010-10-04 16:33:58 +02001102
Andrey Andreev96feb582012-07-19 13:12:34 +03001103 $this->qb_groupby[] = $val;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001104 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001106 $this->qb_cache_groupby[] = $val;
1107 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 }
1109 }
1110 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001111
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 return $this;
1113 }
1114
1115 // --------------------------------------------------------------------
1116
1117 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001118 * HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001120 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001122 * @param string $key
1123 * @param string $value
1124 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 * @return object
1126 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001127 public function having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001129 return $this->_wh('qb_having', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 }
Barry Mienydd671972010-10-04 16:33:58 +02001131
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 // --------------------------------------------------------------------
1133
1134 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001135 * OR HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001137 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001139 * @param string $key
1140 * @param string $value
1141 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 * @return object
1143 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001144 public function or_having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001146 return $this->_wh('qb_having', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 }
Barry Mienydd671972010-10-04 16:33:58 +02001148
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 // --------------------------------------------------------------------
1150
1151 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001152 * ORDER BY
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001154 * @param string $orderby
vlakoff9f8e2992013-09-08 14:05:04 +02001155 * @param string $direction ASC, DESC or RANDOM
Andrey Andreevae85eb42012-11-02 01:42:31 +02001156 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -05001157 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 */
Andrey Andreevd24160c2012-06-16 03:21:20 +03001159 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001161 $direction = strtoupper(trim($direction));
Andrey Andreev2d486232012-07-19 14:46:51 +03001162
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001163 if ($direction === 'RANDOM')
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001165 $direction = '';
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001166
1167 // Do we have a seed value?
1168 $orderby = ctype_digit((string) $orderby)
vlakoffe6c4d5b2013-09-08 13:54:57 +02001169 ? sprintf($this->_random_keyword[1], $orderby)
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001170 : $this->_random_keyword[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 }
Andrey Andreev2d486232012-07-19 14:46:51 +03001172 elseif (empty($orderby))
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001174 return $this;
1175 }
1176 elseif ($direction !== '')
1177 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001178 $direction = in_array($direction, array('ASC', 'DESC'), TRUE) ? ' '.$direction : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 }
Barry Mienydd671972010-10-04 16:33:58 +02001180
Andrey Andreevd24160c2012-06-16 03:21:20 +03001181 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +02001182
Andrey Andreev2d486232012-07-19 14:46:51 +03001183 if ($escape === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001184 {
Andrey Andreev93dd2f22012-10-24 10:09:18 +03001185 $qb_orderby[] = array('field' => $orderby, 'direction' => $direction, 'escape' => FALSE);
Andrey Andreev2d486232012-07-19 14:46:51 +03001186 }
1187 else
1188 {
1189 $qb_orderby = array();
1190 foreach (explode(',', $orderby) as $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001192 $qb_orderby[] = ($direction === '' && preg_match('/\s+(ASC|DESC)$/i', rtrim($field), $match, PREG_OFFSET_CAPTURE))
1193 ? array('field' => ltrim(substr($field, 0, $match[0][1])), 'direction' => ' '.$match[1][0], 'escape' => TRUE)
1194 : array('field' => trim($field), 'direction' => $direction, 'escape' => TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 }
Barry Mienydd671972010-10-04 16:33:58 +02001197
Andrey Andreev2d486232012-07-19 14:46:51 +03001198 $this->qb_orderby = array_merge($this->qb_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001199 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001201 $this->qb_cache_orderby = array_merge($this->qb_cache_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001202 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 }
1204
1205 return $this;
1206 }
Barry Mienydd671972010-10-04 16:33:58 +02001207
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 // --------------------------------------------------------------------
1209
1210 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001211 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001213 * @param int $value LIMIT value
1214 * @param int $offset OFFSET value
Andrew Podner4296a652012-12-17 07:51:15 -05001215 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 */
Andrey Andreev4a587f52014-12-11 16:27:15 +02001217 public function limit($value, $offset = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001218 {
vlakoff912f1bc2013-01-15 03:34:12 +01001219 is_null($value) OR $this->qb_limit = (int) $value;
Andrey Andreev777153d2012-06-18 13:30:45 +03001220 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001221
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 return $this;
1223 }
Barry Mienydd671972010-10-04 16:33:58 +02001224
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 // --------------------------------------------------------------------
1226
1227 /**
1228 * Sets the OFFSET value
1229 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001230 * @param int $offset OFFSET value
Andrew Podner4296a652012-12-17 07:51:15 -05001231 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001232 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001233 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001235 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001236 return $this;
1237 }
Barry Mienydd671972010-10-04 16:33:58 +02001238
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 // --------------------------------------------------------------------
1240
1241 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001242 * LIMIT string
Andrey Andreev2c35b642012-06-24 03:05:26 +03001243 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001244 * Generates a platform-specific LIMIT clause.
Andrey Andreev2c35b642012-06-24 03:05:26 +03001245 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001246 * @param string $sql SQL Query
Andrey Andreev2c35b642012-06-24 03:05:26 +03001247 * @return string
1248 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001249 protected function _limit($sql)
Andrey Andreev2c35b642012-06-24 03:05:26 +03001250 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001251 return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').$this->qb_limit;
Andrey Andreev2c35b642012-06-24 03:05:26 +03001252 }
1253
1254 // --------------------------------------------------------------------
1255
1256 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001257 * The "set" function.
1258 *
1259 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001261 * @param mixed
1262 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001263 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001264 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001266 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 {
1268 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001269
Derek Allard2067d1a2008-11-13 22:59:24 +00001270 if ( ! is_array($key))
1271 {
1272 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001273 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001274
Andrey Andreevfe642da2012-06-16 03:47:33 +03001275 is_bool($escape) OR $escape = $this->_protect_identifiers;
1276
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 foreach ($key as $k => $v)
1278 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001279 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1280 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 }
Barry Mienydd671972010-10-04 16:33:58 +02001282
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 return $this;
1284 }
WanWizard7219c072011-12-28 14:09:05 +01001285
Kyle Farris0c147b32011-08-26 02:29:31 -04001286 // --------------------------------------------------------------------
1287
1288 /**
1289 * Get SELECT query string
1290 *
1291 * Compiles a SELECT query string and returns the sql.
1292 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001293 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001294 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001295 * @return string
1296 */
WanWizard7219c072011-12-28 14:09:05 +01001297 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001298 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001299 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001300 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001301 $this->_track_aliases($table);
1302 $this->from($table);
1303 }
WanWizard7219c072011-12-28 14:09:05 +01001304
Andrey Andreev650b4c02012-06-11 12:07:15 +03001305 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001306
Kyle Farris0c147b32011-08-26 02:29:31 -04001307 if ($reset === TRUE)
1308 {
1309 $this->_reset_select();
1310 }
WanWizard7219c072011-12-28 14:09:05 +01001311
Kyle Farris0c147b32011-08-26 02:29:31 -04001312 return $select;
1313 }
WanWizard7219c072011-12-28 14:09:05 +01001314
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 // --------------------------------------------------------------------
1316
1317 /**
1318 * Get
1319 *
1320 * Compiles the select statement based on the other functions called
1321 * and runs the query
1322 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 * @param string the table
1324 * @param string the limit clause
1325 * @param string the offset clause
1326 * @return object
1327 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001328 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001330 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 {
1332 $this->_track_aliases($table);
1333 $this->from($table);
1334 }
Barry Mienydd671972010-10-04 16:33:58 +02001335
Andrey Andreev650b4c02012-06-11 12:07:15 +03001336 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 {
1338 $this->limit($limit, $offset);
1339 }
Barry Mienydd671972010-10-04 16:33:58 +02001340
Andrey Andreev24276a32012-01-08 02:44:38 +02001341 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001342 $this->_reset_select();
1343 return $result;
1344 }
1345
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001346 // --------------------------------------------------------------------
1347
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 /**
1349 * "Count All Results" query
1350 *
Barry Mienydd671972010-10-04 16:33:58 +02001351 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001352 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 * @param string
yaoshanliang2f164052015-03-16 16:48:15 +08001355 * @param bool the reset clause
vlakoffc6ac7482013-11-17 00:50:06 +01001356 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 */
yaoshanliang19c28472015-03-15 10:42:18 +08001358 public function count_all_results($table = '', $reset = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001360 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 {
1362 $this->_track_aliases($table);
1363 $this->from($table);
1364 }
Barry Mienydd671972010-10-04 16:33:58 +02001365
Andrey Andreevb05f5062012-10-26 12:01:02 +03001366 $result = ($this->qb_distinct === TRUE)
1367 ? $this->query($this->_count_string.$this->protect_identifiers('numrows')."\nFROM (\n".$this->_compile_select()."\n) CI_count_all_results")
1368 : $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
yaoshanliang9971e7b2015-03-14 13:09:16 +08001369
yaoshanliang2f164052015-03-16 16:48:15 +08001370 if ($reset === TRUE)
yaoshanliang19c28472015-03-15 10:42:18 +08001371 {
yaoshanliang2f164052015-03-16 16:48:15 +08001372 $this->_reset_select();
yaoshanliang19c28472015-03-15 10:42:18 +08001373 }
Barry Mienydd671972010-10-04 16:33:58 +02001374
Purwandi1d160e72012-01-09 16:33:28 +07001375 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001377 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 }
1379
Purwandi1d160e72012-01-09 16:33:28 +07001380 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001381 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001382 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001383
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 // --------------------------------------------------------------------
1385
1386 /**
1387 * Get_Where
1388 *
1389 * Allows the where clause, limit and offset to be added directly
1390 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001391 * @param string $table
1392 * @param string $where
1393 * @param int $limit
1394 * @param int $offset
Derek Allard2067d1a2008-11-13 22:59:24 +00001395 * @return object
1396 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001397 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001398 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001399 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001400 {
1401 $this->from($table);
1402 }
1403
vlakoff1228fe22013-01-14 01:30:09 +01001404 if ($where !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001405 {
1406 $this->where($where);
1407 }
Barry Mienydd671972010-10-04 16:33:58 +02001408
Andrey Andreev650b4c02012-06-11 12:07:15 +03001409 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001410 {
1411 $this->limit($limit, $offset);
1412 }
Barry Mienydd671972010-10-04 16:33:58 +02001413
Andrey Andreev24276a32012-01-08 02:44:38 +02001414 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001415 $this->_reset_select();
1416 return $result;
1417 }
1418
1419 // --------------------------------------------------------------------
1420
1421 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001422 * Insert_Batch
1423 *
1424 * Compiles batch insert strings and runs the queries
1425 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001426 * @param string $table Table to insert into
1427 * @param array $set An associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001428 * @param bool $escape Whether to escape values and identifiers
Andrey Andreevae85eb42012-11-02 01:42:31 +02001429 * @return int Number of rows inserted or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001430 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001431 public function insert_batch($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001432 {
vlakoff1228fe22013-01-14 01:30:09 +01001433 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001434 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001435 $this->set_insert_batch($set, '', $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001436 }
Barry Mienydd671972010-10-04 16:33:58 +02001437
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001438 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001439 {
Andrey Andreev9f808b02012-10-24 17:38:48 +03001440 // No valid data array. Folds in cases where keys and values did not match up
1441 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001442 }
1443
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001444 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001445 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001446 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001447 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001448 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001449 }
Barry Mienydd671972010-10-04 16:33:58 +02001450
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001451 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001452 }
1453
1454 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001455 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001456 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001457 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001458 $this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, $escape, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, 100)));
Andrey Andreev9f808b02012-10-24 17:38:48 +03001459 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001460 }
Barry Mienydd671972010-10-04 16:33:58 +02001461
Derek Jonesd10e8962010-03-02 17:10:36 -06001462 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001463 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001464 }
1465
1466 // --------------------------------------------------------------------
1467
1468 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +02001469 * Insert batch statement
Andrey Andreev97f36972012-04-05 12:44:36 +03001470 *
1471 * Generates a platform-specific insert string from the supplied data.
1472 *
Andrey Andreev083e3c82012-11-06 12:48:32 +02001473 * @param string $table Table name
1474 * @param array $keys INSERT keys
1475 * @param array $values INSERT values
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001476 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001477 */
1478 protected function _insert_batch($table, $keys, $values)
1479 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001480 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001481 }
1482
1483 // --------------------------------------------------------------------
1484
1485 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001486 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001487 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001488 * @param mixed
1489 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001490 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001491 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001492 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001493 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001494 {
1495 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001496
Derek Jonesd10e8962010-03-02 17:10:36 -06001497 if ( ! is_array($key))
1498 {
1499 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001500 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001501
Andrey Andreevfe642da2012-06-16 03:47:33 +03001502 is_bool($escape) OR $escape = $this->_protect_identifiers;
1503
Iban Eguia3c0a4522012-04-15 13:30:44 +02001504 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001505 sort($keys);
1506
1507 foreach ($key as $row)
1508 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001509 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001510 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1511 {
1512 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001513 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001514 return;
1515 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001516
Barry Mienydd671972010-10-04 16:33:58 +02001517 ksort($row); // puts $row in the same order as our keys
1518
Andrey Andreev650b4c02012-06-11 12:07:15 +03001519 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001520 {
1521 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001522 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001523 {
Barry Mienydd671972010-10-04 16:33:58 +02001524 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001525 }
1526
Andrey Andreev650b4c02012-06-11 12:07:15 +03001527 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001528 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001529
Andrey Andreev838a9d62012-12-03 14:37:47 +02001530 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001531 }
1532
1533 foreach ($keys as $k)
1534 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001535 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001536 }
Barry Mienydd671972010-10-04 16:33:58 +02001537
Derek Jonesd10e8962010-03-02 17:10:36 -06001538 return $this;
1539 }
WanWizard7219c072011-12-28 14:09:05 +01001540
Kyle Farris0c147b32011-08-26 02:29:31 -04001541 // --------------------------------------------------------------------
1542
1543 /**
1544 * Get INSERT query string
1545 *
1546 * Compiles an insert query and returns the sql
1547 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001548 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001549 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001550 * @return string
1551 */
1552 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001553 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001554 if ($this->_validate_insert($table) === FALSE)
1555 {
1556 return FALSE;
1557 }
WanWizard7219c072011-12-28 14:09:05 +01001558
Kyle Farris76116012011-08-31 11:17:48 -04001559 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001560 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001561 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001562 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001563 array_keys($this->qb_set),
1564 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001565 );
WanWizard7219c072011-12-28 14:09:05 +01001566
Kyle Farris0c147b32011-08-26 02:29:31 -04001567 if ($reset === TRUE)
1568 {
1569 $this->_reset_write();
1570 }
WanWizard7219c072011-12-28 14:09:05 +01001571
Kyle Farris0c147b32011-08-26 02:29:31 -04001572 return $sql;
1573 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001574
Derek Allard2067d1a2008-11-13 22:59:24 +00001575 // --------------------------------------------------------------------
1576
1577 /**
1578 * Insert
1579 *
1580 * Compiles an insert string and runs the query
1581 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001582 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001583 * @param array an associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001584 * @param bool $escape Whether to escape values and identifiers
Derek Allard2067d1a2008-11-13 22:59:24 +00001585 * @return object
1586 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001587 public function insert($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001588 {
vlakoff1228fe22013-01-14 01:30:09 +01001589 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001590 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001591 $this->set($set, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001592 }
WanWizard7219c072011-12-28 14:09:05 +01001593
Kyle Farris0c147b32011-08-26 02:29:31 -04001594 if ($this->_validate_insert($table) === FALSE)
1595 {
1596 return FALSE;
1597 }
WanWizard7219c072011-12-28 14:09:05 +01001598
Kyle Farris76116012011-08-31 11:17:48 -04001599 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001600 $this->protect_identifiers(
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001601 $this->qb_from[0], TRUE, $escape, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001602 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001603 array_keys($this->qb_set),
1604 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001605 );
Barry Mienydd671972010-10-04 16:33:58 +02001606
Kyle Farris0c147b32011-08-26 02:29:31 -04001607 $this->_reset_write();
1608 return $this->query($sql);
1609 }
WanWizard7219c072011-12-28 14:09:05 +01001610
Kyle Farris0c147b32011-08-26 02:29:31 -04001611 // --------------------------------------------------------------------
1612
1613 /**
1614 * Validate Insert
1615 *
1616 * This method is used by both insert() and get_compiled_insert() to
1617 * validate that the there data is actually being set and that table
1618 * has been chosen to be inserted into.
1619 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001620 * @param string the table to insert data into
1621 * @return string
1622 */
WanWizard7219c072011-12-28 14:09:05 +01001623 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001624 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001625 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001626 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001627 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001628 }
1629
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001630 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001631 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001632 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001633 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001634 elseif ( ! isset($this->qb_from[0]))
1635 {
1636 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1637 }
WanWizard7219c072011-12-28 14:09:05 +01001638
Kyle Farris0c147b32011-08-26 02:29:31 -04001639 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001640 }
Barry Mienydd671972010-10-04 16:33:58 +02001641
Phil Sturgeon9789f322011-07-15 15:14:05 -06001642 // --------------------------------------------------------------------
1643
1644 /**
1645 * Replace
1646 *
1647 * Compiles an replace into string and runs the query
1648 *
1649 * @param string the table to replace data into
1650 * @param array an associative array of insert values
1651 * @return object
1652 */
1653 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001654 {
vlakoff1228fe22013-01-14 01:30:09 +01001655 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001656 {
1657 $this->set($set);
1658 }
Barry Mienydd671972010-10-04 16:33:58 +02001659
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001660 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001661 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001662 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001663 }
1664
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001665 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001666 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001667 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001668 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001669 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001670 }
Barry Mienydd671972010-10-04 16:33:58 +02001671
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001672 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001673 }
1674
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001675 $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 +00001676
Derek Jonesd10e8962010-03-02 17:10:36 -06001677 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001678 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001679 }
WanWizard7219c072011-12-28 14:09:05 +01001680
Kyle Farris0c147b32011-08-26 02:29:31 -04001681 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001682
Kyle Farris0c147b32011-08-26 02:29:31 -04001683 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001684 * Replace statement
1685 *
1686 * Generates a platform-specific replace string from the supplied data
1687 *
1688 * @param string the table name
1689 * @param array the insert keys
1690 * @param array the insert values
1691 * @return string
1692 */
1693 protected function _replace($table, $keys, $values)
1694 {
1695 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1696 }
1697
1698 // --------------------------------------------------------------------
1699
1700 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001701 * FROM tables
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001702 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001703 * Groups tables in FROM clauses if needed, so there is no confusion
1704 * about operator precedence.
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001705 *
Claudio Galdiolo93e78132015-01-29 11:43:56 -05001706 * Note: This is only used (and overridden) by MySQL and CUBRID.
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001707 *
1708 * @return string
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001709 */
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001710 protected function _from_tables()
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001711 {
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001712 return implode(', ', $this->qb_from);
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001713 }
1714
1715 // --------------------------------------------------------------------
1716
1717 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001718 * Get UPDATE query string
1719 *
1720 * Compiles an update query and returns the sql
1721 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001722 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001723 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001724 * @return string
1725 */
1726 public function get_compiled_update($table = '', $reset = TRUE)
1727 {
1728 // Combine any cached components with the current statements
1729 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001730
Kyle Farris0c147b32011-08-26 02:29:31 -04001731 if ($this->_validate_update($table) === FALSE)
1732 {
1733 return FALSE;
1734 }
WanWizard7219c072011-12-28 14:09:05 +01001735
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001736 $sql = $this->_update($this->qb_from[0], $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001737
Kyle Farris0c147b32011-08-26 02:29:31 -04001738 if ($reset === TRUE)
1739 {
1740 $this->_reset_write();
1741 }
WanWizard7219c072011-12-28 14:09:05 +01001742
Kyle Farris0c147b32011-08-26 02:29:31 -04001743 return $sql;
1744 }
WanWizard7219c072011-12-28 14:09:05 +01001745
Derek Allard2067d1a2008-11-13 22:59:24 +00001746 // --------------------------------------------------------------------
1747
1748 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001749 * UPDATE
Derek Allard2067d1a2008-11-13 22:59:24 +00001750 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001751 * Compiles an update string and runs the query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001752 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001753 * @param string $table
1754 * @param array $set An associative array of update values
1755 * @param mixed $where
1756 * @param int $limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001757 * @return object
1758 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001759 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001760 {
1761 // Combine any cached components with the current statements
1762 $this->_merge_cache();
1763
vlakoff1228fe22013-01-14 01:30:09 +01001764 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001765 {
1766 $this->set($set);
1767 }
Barry Mienydd671972010-10-04 16:33:58 +02001768
Kyle Farris0c147b32011-08-26 02:29:31 -04001769 if ($this->_validate_update($table) === FALSE)
1770 {
1771 return FALSE;
1772 }
1773
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001774 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001775 {
1776 $this->where($where);
1777 }
1778
Andrey Andreev650b4c02012-06-11 12:07:15 +03001779 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001780 {
1781 $this->limit($limit);
1782 }
1783
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001784 $sql = $this->_update($this->qb_from[0], $this->qb_set);
Kyle Farris0c147b32011-08-26 02:29:31 -04001785 $this->_reset_write();
1786 return $this->query($sql);
1787 }
WanWizard7219c072011-12-28 14:09:05 +01001788
Kyle Farris0c147b32011-08-26 02:29:31 -04001789 // --------------------------------------------------------------------
1790
1791 /**
1792 * Validate Update
1793 *
1794 * This method is used by both update() and get_compiled_update() to
1795 * validate that data is actually being set and that a table has been
1796 * chosen to be update.
1797 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001798 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001799 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001800 */
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001801 protected function _validate_update($table)
Kyle Farris0c147b32011-08-26 02:29:31 -04001802 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001803 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001804 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001805 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001806 }
1807
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001808 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001809 {
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001810 $this->qb_from = array($this->protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +00001811 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001812 elseif ( ! isset($this->qb_from[0]))
1813 {
1814 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1815 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001816
1817 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001818 }
WanWizard7219c072011-12-28 14:09:05 +01001819
Derek Jonesd10e8962010-03-02 17:10:36 -06001820 // --------------------------------------------------------------------
1821
1822 /**
1823 * Update_Batch
1824 *
1825 * Compiles an update string and runs the query
1826 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001827 * @param string the table to retrieve the results from
1828 * @param array an associative array of update values
1829 * @param string the where key
Andrey Andreev9f808b02012-10-24 17:38:48 +03001830 * @return int number of rows affected or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001831 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001832 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001833 {
1834 // Combine any cached components with the current statements
1835 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001836
vlakoff1228fe22013-01-14 01:30:09 +01001837 if ($index === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001838 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001839 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001840 }
1841
vlakoff1228fe22013-01-14 01:30:09 +01001842 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001843 {
1844 $this->set_update_batch($set, $index);
1845 }
1846
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001847 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001848 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001849 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001850 }
1851
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001852 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001853 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001854 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001855 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001856 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001857 }
Barry Mienydd671972010-10-04 16:33:58 +02001858
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001859 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001860 }
Barry Mienydd671972010-10-04 16:33:58 +02001861
Derek Jonesd10e8962010-03-02 17:10:36 -06001862 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001863 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001864 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001865 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001866 $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 +03001867 $affected_rows += $this->affected_rows();
Andrey Andreev79f888b2013-08-06 13:59:23 +03001868 $this->qb_where = array();
Derek Jonesd10e8962010-03-02 17:10:36 -06001869 }
Barry Mienydd671972010-10-04 16:33:58 +02001870
Derek Jonesd10e8962010-03-02 17:10:36 -06001871 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001872 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001873 }
1874
1875 // --------------------------------------------------------------------
1876
1877 /**
Andrey Andreev219565d2013-03-12 20:00:08 +02001878 * Update_Batch statement
1879 *
1880 * Generates a platform-specific batch update string from the supplied data
1881 *
1882 * @param string $table Table name
1883 * @param array $values Update data
1884 * @param string $index WHERE key
1885 * @return string
1886 */
1887 protected function _update_batch($table, $values, $index)
1888 {
1889 $ids = array();
1890 foreach ($values as $key => $val)
1891 {
1892 $ids[] = $val[$index];
1893
1894 foreach (array_keys($val) as $field)
1895 {
1896 if ($field !== $index)
1897 {
1898 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
1899 }
1900 }
1901 }
1902
1903 $cases = '';
1904 foreach ($final as $k => $v)
1905 {
1906 $cases .= $k." = CASE \n"
1907 .implode("\n", $v)."\n"
1908 .'ELSE '.$k.' END, ';
1909 }
1910
1911 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
1912
1913 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
1914 }
1915
1916 // --------------------------------------------------------------------
1917
1918 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001919 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001920 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001921 * @param array
1922 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001923 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001924 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001925 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001926 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001927 {
1928 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001929
Derek Jonesd10e8962010-03-02 17:10:36 -06001930 if ( ! is_array($key))
1931 {
1932 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001933 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001934
Andrey Andreevfe642da2012-06-16 03:47:33 +03001935 is_bool($escape) OR $escape = $this->_protect_identifiers;
1936
Derek Jonesd10e8962010-03-02 17:10:36 -06001937 foreach ($key as $k => $v)
1938 {
1939 $index_set = FALSE;
1940 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001941 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001942 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001943 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001944 {
1945 $index_set = TRUE;
1946 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001947
Andrey Andreevfe642da2012-06-16 03:47:33 +03001948 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001949 }
1950
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001951 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001952 {
1953 return $this->display_error('db_batch_missing_index');
1954 }
1955
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001956 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001957 }
Barry Mienydd671972010-10-04 16:33:58 +02001958
Derek Jonesd10e8962010-03-02 17:10:36 -06001959 return $this;
1960 }
1961
Derek Allard2067d1a2008-11-13 22:59:24 +00001962 // --------------------------------------------------------------------
1963
1964 /**
1965 * Empty Table
1966 *
1967 * Compiles a delete string and runs "DELETE FROM table"
1968 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001969 * @param string the table to empty
1970 * @return object
1971 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001972 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001973 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001974 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001975 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001976 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001977 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001978 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001979 }
1980
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001981 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001982 }
1983 else
1984 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001985 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001986 }
1987
1988 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001989 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001990 return $this->query($sql);
1991 }
1992
1993 // --------------------------------------------------------------------
1994
1995 /**
1996 * Truncate
1997 *
1998 * Compiles a truncate string and runs the query
1999 * If the database does not support the truncate() command
2000 * This function maps to "DELETE FROM table"
2001 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002002 * @param string the table to truncate
2003 * @return object
2004 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002005 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002006 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002007 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002008 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002009 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002010 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002011 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002012 }
2013
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002014 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002015 }
2016 else
2017 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002018 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002019 }
2020
2021 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002022 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00002023 return $this->query($sql);
2024 }
WanWizard7219c072011-12-28 14:09:05 +01002025
Kyle Farris0c147b32011-08-26 02:29:31 -04002026 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02002027
Kyle Farris0c147b32011-08-26 02:29:31 -04002028 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03002029 * Truncate statement
2030 *
2031 * Generates a platform-specific truncate string from the supplied data
2032 *
2033 * If the database does not support the truncate() command,
2034 * then this method maps to 'DELETE FROM table'
2035 *
2036 * @param string the table name
2037 * @return string
2038 */
2039 protected function _truncate($table)
2040 {
2041 return 'TRUNCATE '.$table;
2042 }
2043
2044 // --------------------------------------------------------------------
2045
2046 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04002047 * Get DELETE query string
2048 *
2049 * Compiles a delete query string and returns the sql
2050 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002051 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002052 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04002053 * @return string
2054 */
2055 public function get_compiled_delete($table = '', $reset = TRUE)
2056 {
2057 $this->return_delete_sql = TRUE;
2058 $sql = $this->delete($table, '', NULL, $reset);
2059 $this->return_delete_sql = FALSE;
2060 return $sql;
2061 }
WanWizard7219c072011-12-28 14:09:05 +01002062
Derek Allard2067d1a2008-11-13 22:59:24 +00002063 // --------------------------------------------------------------------
2064
2065 /**
2066 * Delete
2067 *
2068 * Compiles a delete string and runs the query
2069 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002070 * @param mixed the table(s) to delete from. String or array
2071 * @param mixed the where clause
2072 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03002073 * @param bool
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002074 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00002075 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002076 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002077 {
2078 // Combine any cached components with the current statements
2079 $this->_merge_cache();
2080
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002081 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002082 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002083 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002084 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002085 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002086 }
2087
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002088 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002089 }
2090 elseif (is_array($table))
2091 {
Andrey Andreeva1170af2015-07-02 11:46:56 +03002092 empty($where) && $reset_data = FALSE;
2093
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05002094 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002095 {
Andrey Andreev13f50542012-10-12 12:31:02 +03002096 $this->delete($single_table, $where, $limit, $reset_data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002097 }
Andrey Andreeva1170af2015-07-02 11:46:56 +03002098
Derek Allard2067d1a2008-11-13 22:59:24 +00002099 return;
2100 }
2101 else
2102 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002103 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002104 }
2105
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002106 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002107 {
2108 $this->where($where);
2109 }
2110
Andrey Andreev650b4c02012-06-11 12:07:15 +03002111 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002112 {
2113 $this->limit($limit);
2114 }
2115
Andrey Andreev94611df2012-07-19 12:29:54 +03002116 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002117 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002118 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02002119 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002120
Andrey Andreevb0478652012-07-18 15:34:46 +03002121 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002122 if ($reset_data)
2123 {
2124 $this->_reset_write();
2125 }
WanWizard7219c072011-12-28 14:09:05 +01002126
Andrey Andreev24276a32012-01-08 02:44:38 +02002127 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00002128 }
WanWizard7219c072011-12-28 14:09:05 +01002129
Derek Allard2067d1a2008-11-13 22:59:24 +00002130 // --------------------------------------------------------------------
2131
2132 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03002133 * Delete statement
2134 *
2135 * Generates a platform-specific delete string from the supplied data
2136 *
2137 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03002138 * @return string
2139 */
Andrey Andreevb0478652012-07-18 15:34:46 +03002140 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03002141 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002142 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002143 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03002144 }
2145
2146 // --------------------------------------------------------------------
2147
2148 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002149 * DB Prefix
2150 *
2151 * Prepends a database prefix if one exists in configuration
2152 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002153 * @param string the table
2154 * @return string
2155 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002156 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002157 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002158 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002159 {
2160 $this->display_error('db_table_name_required');
2161 }
2162
2163 return $this->dbprefix.$table;
2164 }
2165
2166 // --------------------------------------------------------------------
2167
2168 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06002169 * Set DB Prefix
2170 *
2171 * Set's the DB Prefix to something new without needing to reconnect
2172 *
2173 * @param string the prefix
2174 * @return string
2175 */
2176 public function set_dbprefix($prefix = '')
2177 {
2178 return $this->dbprefix = $prefix;
2179 }
2180
2181 // --------------------------------------------------------------------
2182
2183 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002184 * Track Aliases
2185 *
2186 * Used to track SQL statements written with aliased tables.
2187 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002188 * @param string The table to inspect
2189 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002190 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002191 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002192 {
2193 if (is_array($table))
2194 {
2195 foreach ($table as $t)
2196 {
2197 $this->_track_aliases($t);
2198 }
2199 return;
2200 }
Barry Mienydd671972010-10-04 16:33:58 +02002201
Derek Jones37f4b9c2011-07-01 17:56:50 -05002202 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002203 // the string into discreet statements
2204 if (strpos($table, ',') !== FALSE)
2205 {
2206 return $this->_track_aliases(explode(',', $table));
2207 }
Barry Mienydd671972010-10-04 16:33:58 +02002208
Derek Allard2067d1a2008-11-13 22:59:24 +00002209 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002210 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002211 {
2212 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002213 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002214
Derek Allard2067d1a2008-11-13 22:59:24 +00002215 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002216 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002217
Derek Allard2067d1a2008-11-13 22:59:24 +00002218 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002219 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002220 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002221 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002222 }
2223 }
2224 }
WanWizard7219c072011-12-28 14:09:05 +01002225
Derek Allard2067d1a2008-11-13 22:59:24 +00002226 // --------------------------------------------------------------------
2227
2228 /**
2229 * Compile the SELECT statement
2230 *
2231 * Generates a query string based on which functions were used.
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002232 * Should not be called directly.
Derek Allard2067d1a2008-11-13 22:59:24 +00002233 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002234 * @param bool $select_override
Derek Allard2067d1a2008-11-13 22:59:24 +00002235 * @return string
2236 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002237 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002238 {
2239 // Combine any cached components with the current statements
2240 $this->_merge_cache();
2241
Derek Allard2067d1a2008-11-13 22:59:24 +00002242 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002243 if ($select_override !== FALSE)
2244 {
2245 $sql = $select_override;
2246 }
2247 else
2248 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002249 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002250
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002251 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002252 {
Barry Mienydd671972010-10-04 16:33:58 +02002253 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002254 }
2255 else
Barry Mienydd671972010-10-04 16:33:58 +02002256 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002257 // Cycle through the "select" portion of the query and prep each column name.
Andrey Andreev1924eb32015-04-08 17:19:24 +03002258 // The reason we protect identifiers here rather than in the select() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002259 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002260 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002261 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002262 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002263 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002264 }
Barry Mienydd671972010-10-04 16:33:58 +02002265
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002266 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002267 }
2268 }
2269
Derek Allard2067d1a2008-11-13 22:59:24 +00002270 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002271 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002272 {
Andrey Andreeve78f8152012-10-09 11:38:38 +03002273 $sql .= "\nFROM ".$this->_from_tables();
Derek Allard2067d1a2008-11-13 22:59:24 +00002274 }
2275
Derek Allard2067d1a2008-11-13 22:59:24 +00002276 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002277 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002278 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002279 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002280 }
2281
Andrey Andreev2d486232012-07-19 14:46:51 +03002282 $sql .= $this->_compile_wh('qb_where')
2283 .$this->_compile_group_by()
2284 .$this->_compile_wh('qb_having')
2285 .$this->_compile_order_by(); // ORDER BY
Andrey Andreevb0478652012-07-18 15:34:46 +03002286
Andrey Andreevd40459d2012-07-18 16:46:39 +03002287 // LIMIT
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002288 if ($this->qb_limit)
Derek Allard2067d1a2008-11-13 22:59:24 +00002289 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002290 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002291 }
2292
2293 return $sql;
2294 }
2295
2296 // --------------------------------------------------------------------
2297
2298 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002299 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002300 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002301 * Escapes identifiers in WHERE and HAVING statements at execution time.
2302 *
Andrey Andreevb0478652012-07-18 15:34:46 +03002303 * Required so that aliases are tracked properly, regardless of wether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002304 * where(), or_where(), having(), or_having are called prior to from(),
2305 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002306 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002307 * @param string $qb_key 'qb_where' or 'qb_having'
Andrey Andreevd40459d2012-07-18 16:46:39 +03002308 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002309 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002310 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002311 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002312 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002313 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002314 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002315 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002316 // Is this condition already compiled?
2317 if (is_string($this->{$qb_key}[$i]))
2318 {
2319 continue;
2320 }
2321 elseif ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002322 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002323 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002324 continue;
2325 }
2326
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002327 // Split multiple conditions
2328 $conditions = preg_split(
Andrey Andreev7ed93be2015-06-29 11:43:32 +03002329 '/((^|\s+)AND\s+|(^|\s+)OR\s+)/i',
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002330 $this->{$qb_key}[$i]['condition'],
2331 -1,
2332 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
2333 );
2334
2335 for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002336 {
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002337 if (($op = $this->_get_operator($conditions[$ci])) === FALSE
Andrey Andreeve4742582012-10-25 13:25:13 +03002338 OR ! preg_match('/^(\(?)(.*)('.preg_quote($op, '/').')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches))
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002339 {
2340 continue;
2341 }
2342
2343 // $matches = array(
2344 // 0 => '(test <= foo)', /* the whole thing */
2345 // 1 => '(', /* optional */
2346 // 2 => 'test', /* the field name */
2347 // 3 => ' <= ', /* $op */
2348 // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2349 // 5 => ')' /* optional */
2350 // );
Andrey Andreev082aa402012-10-22 19:41:55 +03002351
2352 if ( ! empty($matches[4]))
2353 {
2354 $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4]));
2355 $matches[4] = ' '.$matches[4];
2356 }
2357
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002358 $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2]))
2359 .' '.trim($matches[3]).$matches[4].$matches[5];
Andrey Andreev6e704752012-07-18 00:46:33 +03002360 }
2361
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002362 $this->{$qb_key}[$i] = implode('', $conditions);
Andrey Andreev6e704752012-07-18 00:46:33 +03002363 }
2364
Andrey Andreev9d3aa1b2012-10-12 12:14:09 +03002365 return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ")
2366 .implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002367 }
2368
Andrey Andreevb0478652012-07-18 15:34:46 +03002369 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002370 }
2371
2372 // --------------------------------------------------------------------
2373
2374 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002375 * Compile GROUP BY
2376 *
2377 * Escapes identifiers in GROUP BY statements at execution time.
2378 *
2379 * Required so that aliases are tracked properly, regardless of wether
2380 * group_by() is called prior to from(), join() and dbprefix is added
2381 * only if needed.
2382 *
2383 * @return string SQL statement
2384 */
2385 protected function _compile_group_by()
2386 {
2387 if (count($this->qb_groupby) > 0)
2388 {
Andrey Andreev96feb582012-07-19 13:12:34 +03002389 for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
2390 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002391 // Is it already compiled?
Andrey Andreev18eba242014-01-23 22:52:31 +02002392 if (is_string($this->qb_groupby[$i]))
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002393 {
2394 continue;
2395 }
2396
Andrey Andreev082aa402012-10-22 19:41:55 +03002397 $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 +03002398 ? $this->qb_groupby[$i]['field']
Andrey Andreev13f50542012-10-12 12:31:02 +03002399 : $this->protect_identifiers($this->qb_groupby[$i]['field']);
Andrey Andreev96feb582012-07-19 13:12:34 +03002400 }
2401
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002402 return "\nGROUP BY ".implode(', ', $this->qb_groupby);
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002403 }
2404
2405 return '';
2406 }
2407
2408 // --------------------------------------------------------------------
2409
2410 /**
Andrey Andreev2d486232012-07-19 14:46:51 +03002411 * Compile ORDER BY
2412 *
2413 * Escapes identifiers in ORDER BY statements at execution time.
2414 *
2415 * Required so that aliases are tracked properly, regardless of wether
2416 * order_by() is called prior to from(), join() and dbprefix is added
2417 * only if needed.
2418 *
2419 * @return string SQL statement
2420 */
2421 protected function _compile_order_by()
2422 {
Andrey Andreeva53ea842012-10-23 12:44:09 +03002423 if (is_array($this->qb_orderby) && count($this->qb_orderby) > 0)
Andrey Andreev2d486232012-07-19 14:46:51 +03002424 {
Andrey Andreev2d486232012-07-19 14:46:51 +03002425 for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++)
2426 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002427 if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field']))
Andrey Andreev2d486232012-07-19 14:46:51 +03002428 {
Andrey Andreevfc043b32012-10-12 14:46:14 +03002429 $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']);
Andrey Andreev2d486232012-07-19 14:46:51 +03002430 }
2431
2432 $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction'];
2433 }
2434
Andrey Andreeva53ea842012-10-23 12:44:09 +03002435 return $this->qb_orderby = "\nORDER BY ".implode(', ', $this->qb_orderby);
2436 }
2437 elseif (is_string($this->qb_orderby))
2438 {
2439 return $this->qb_orderby;
Andrey Andreev2d486232012-07-19 14:46:51 +03002440 }
2441
2442 return '';
2443 }
2444
2445 // --------------------------------------------------------------------
2446
2447 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002448 * Object to Array
2449 *
2450 * Takes an object as input and converts the class variables to array key/vals
2451 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002452 * @param object
2453 * @return array
2454 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002455 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002456 {
2457 if ( ! is_object($object))
2458 {
2459 return $object;
2460 }
Barry Mienydd671972010-10-04 16:33:58 +02002461
Derek Allard2067d1a2008-11-13 22:59:24 +00002462 $array = array();
2463 foreach (get_object_vars($object) as $key => $val)
2464 {
2465 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002466 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002467 {
2468 $array[$key] = $val;
2469 }
2470 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002471
2472 return $array;
2473 }
Barry Mienydd671972010-10-04 16:33:58 +02002474
Derek Jonesd10e8962010-03-02 17:10:36 -06002475 // --------------------------------------------------------------------
2476
2477 /**
2478 * Object to Array
2479 *
2480 * Takes an object as input and converts the class variables to array key/vals
2481 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002482 * @param object
2483 * @return array
2484 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002485 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002486 {
2487 if ( ! is_object($object))
2488 {
2489 return $object;
2490 }
Barry Mienydd671972010-10-04 16:33:58 +02002491
Derek Jonesd10e8962010-03-02 17:10:36 -06002492 $array = array();
2493 $out = get_object_vars($object);
2494 $fields = array_keys($out);
2495
2496 foreach ($fields as $val)
2497 {
2498 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002499 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002500 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002501 $i = 0;
2502 foreach ($out[$val] as $data)
2503 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002504 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002505 }
2506 }
2507 }
2508
Derek Allard2067d1a2008-11-13 22:59:24 +00002509 return $array;
2510 }
Barry Mienydd671972010-10-04 16:33:58 +02002511
Derek Allard2067d1a2008-11-13 22:59:24 +00002512 // --------------------------------------------------------------------
2513
2514 /**
2515 * Start Cache
2516 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002517 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002518 *
Andrey Andreev4a587f52014-12-11 16:27:15 +02002519 * @return CI_DB_query_builder
Barry Mienydd671972010-10-04 16:33:58 +02002520 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002521 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002522 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002523 $this->qb_caching = TRUE;
Andrey Andreev4a587f52014-12-11 16:27:15 +02002524 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002525 }
2526
2527 // --------------------------------------------------------------------
2528
2529 /**
2530 * Stop Cache
2531 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002532 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002533 *
Andrey Andreev4a587f52014-12-11 16:27:15 +02002534 * @return CI_DB_query_builder
Barry Mienydd671972010-10-04 16:33:58 +02002535 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002536 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002537 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002538 $this->qb_caching = FALSE;
Andrey Andreev4a587f52014-12-11 16:27:15 +02002539 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002540 }
2541
2542 // --------------------------------------------------------------------
2543
2544 /**
2545 * Flush Cache
2546 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002547 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002548 *
Andrey Andreev4a587f52014-12-11 16:27:15 +02002549 * @return CI_DB_query_builder
Barry Mienydd671972010-10-04 16:33:58 +02002550 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002551 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002552 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002553 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002554 'qb_cache_select' => array(),
2555 'qb_cache_from' => array(),
2556 'qb_cache_join' => array(),
2557 'qb_cache_where' => array(),
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002558 'qb_cache_groupby' => array(),
2559 'qb_cache_having' => array(),
2560 'qb_cache_orderby' => array(),
2561 'qb_cache_set' => array(),
2562 'qb_cache_exists' => array(),
2563 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002564 ));
Andrey Andreev4a587f52014-12-11 16:27:15 +02002565
2566 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002567 }
2568
2569 // --------------------------------------------------------------------
2570
2571 /**
2572 * Merge Cache
2573 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002574 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002575 * locally called ones.
2576 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002577 * @return void
2578 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002579 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002580 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002581 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002582 {
2583 return;
2584 }
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002585 elseif (in_array('select', $this->qb_cache_exists, TRUE))
2586 {
2587 $qb_no_escape = $this->qb_cache_no_escape;
2588 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002589
GDmac17a05282013-11-11 13:18:09 +01002590 foreach (array_unique($this->qb_cache_exists) as $val) // select, from, etc.
Derek Allard2067d1a2008-11-13 22:59:24 +00002591 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002592 $qb_variable = 'qb_'.$val;
2593 $qb_cache_var = 'qb_cache_'.$val;
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002594 $qb_new = $this->$qb_cache_var;
GDmace1b86832013-11-08 16:52:54 +01002595
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002596 for ($i = 0, $c = count($this->$qb_variable); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00002597 {
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002598 if ( ! in_array($this->{$qb_variable}[$i], $qb_new, TRUE))
2599 {
2600 $qb_new[] = $this->{$qb_variable}[$i];
2601 if ($val === 'select')
2602 {
2603 $qb_no_escape[] = $this->qb_no_escape[$i];
2604 }
2605 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002606 }
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002607
GDmace1b86832013-11-08 16:52:54 +01002608 $this->$qb_variable = $qb_new;
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002609 if ($val === 'select')
2610 {
2611 $this->qb_no_escape = $qb_no_escape;
2612 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002613 }
2614
2615 // If we are "protecting identifiers" we need to examine the "from"
2616 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002617 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002618 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002619 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002620 }
2621 }
WanWizard7219c072011-12-28 14:09:05 +01002622
Kyle Farris0c147b32011-08-26 02:29:31 -04002623 // --------------------------------------------------------------------
2624
2625 /**
Andrey Andreev082aa402012-10-22 19:41:55 +03002626 * Is literal
2627 *
2628 * Determines if a string represents a literal value or a field name
2629 *
Andrey Andreev02e4cd72012-11-13 11:50:47 +02002630 * @param string $str
Andrey Andreev082aa402012-10-22 19:41:55 +03002631 * @return bool
2632 */
2633 protected function _is_literal($str)
2634 {
2635 $str = trim($str);
2636
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002637 if (empty($str) OR ctype_digit($str) OR (string) (float) $str === $str OR in_array(strtoupper($str), array('TRUE', 'FALSE'), TRUE))
Andrey Andreev082aa402012-10-22 19:41:55 +03002638 {
2639 return TRUE;
2640 }
2641
2642 static $_str;
2643
2644 if (empty($_str))
2645 {
2646 $_str = ($this->_escape_char !== '"')
2647 ? array('"', "'") : array("'");
2648 }
2649
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002650 return in_array($str[0], $_str, TRUE);
Andrey Andreev082aa402012-10-22 19:41:55 +03002651 }
2652
2653 // --------------------------------------------------------------------
2654
2655 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002656 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002657 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002658 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002659 *
Andrey Andreev435e0c22014-12-11 16:30:13 +02002660 * @return CI_DB_query_builder
Kyle Farris0c147b32011-08-26 02:29:31 -04002661 */
2662 public function reset_query()
2663 {
2664 $this->_reset_select();
2665 $this->_reset_write();
Andrey Andreev435e0c22014-12-11 16:30:13 +02002666 return $this;
Kyle Farris0c147b32011-08-26 02:29:31 -04002667 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002668
2669 // --------------------------------------------------------------------
2670
2671 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002672 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002673 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002674 * @param array An array of fields to reset
2675 * @return void
2676 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002677 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002678 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002679 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002680 {
Andrey Andreevae85eb42012-11-02 01:42:31 +02002681 $this->$item = $default_value;
Derek Allard2067d1a2008-11-13 22:59:24 +00002682 }
2683 }
2684
2685 // --------------------------------------------------------------------
2686
2687 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002688 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002689 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002690 * @return void
2691 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002692 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002693 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002694 $this->_reset_run(array(
Andrey Andreev4a587f52014-12-11 16:27:15 +02002695 'qb_select' => array(),
2696 'qb_from' => array(),
2697 'qb_join' => array(),
2698 'qb_where' => array(),
2699 'qb_groupby' => array(),
2700 'qb_having' => array(),
2701 'qb_orderby' => array(),
2702 'qb_aliased_tables' => array(),
2703 'qb_no_escape' => array(),
2704 'qb_distinct' => FALSE,
2705 'qb_limit' => FALSE,
2706 'qb_offset' => FALSE
2707 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002708 }
Barry Mienydd671972010-10-04 16:33:58 +02002709
Derek Allard2067d1a2008-11-13 22:59:24 +00002710 // --------------------------------------------------------------------
2711
2712 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002713 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002714 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002715 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002716 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002717 * @return void
2718 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002719 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002720 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002721 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002722 'qb_set' => array(),
2723 'qb_from' => array(),
Andrey Andreev3e014372013-02-21 15:59:34 +02002724 'qb_join' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002725 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002726 'qb_orderby' => array(),
2727 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002728 'qb_limit' => FALSE
Andrey Andreev4a587f52014-12-11 16:27:15 +02002729 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002730 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002731
Derek Allard2067d1a2008-11-13 22:59:24 +00002732}