blob: c180a17e922183fc922a5688c6e1fcfeaf1332dd [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 Andreev125ef472016-01-11 12:33:00 +02009 * Copyright (c) 2014 - 2016, 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
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreev125ef472016-01-11 12:33:00 +020032 * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @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
Andrey Andreevbd202c92016-01-11 12:50:18 +020049 * @link https://codeigniter.com/user_guide/database/
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
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 Andreevf7631f12015-07-27 21:54:41 +0300660 if ($escape === TRUE)
661 {
662 $v = ' '.$this->escape($v);
663 }
WanWizard7219c072011-12-28 14:09:05 +0100664
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 if ( ! $this->_has_operator($k))
666 {
Greg Akere156c6e2011-04-20 16:03:04 -0500667 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 }
669 }
Andrey Andreev3a5efc22012-11-20 21:18:08 +0200670 elseif ( ! $this->_has_operator($k))
671 {
672 // value appears not to have been set, assign the test to IS NULL
673 $k .= ' IS NULL';
674 }
Andrey Andreev5078eb52014-12-02 01:11:54 +0200675 elseif (preg_match('/\s*(!?=|<>|IS(?:\s+NOT)?)\s*$/i', $k, $match, PREG_OFFSET_CAPTURE))
Andrey Andreev5bf4dcd2014-09-29 20:07:15 +0300676 {
677 $k = substr($k, 0, $match[0][1]).($match[1][0] === '=' ? ' IS NULL' : ' IS NOT NULL');
678 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000679
Andrey Andreevd40459d2012-07-18 16:46:39 +0300680 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000681 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300683 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
684 $this->qb_cache_exists[] = substr($qb_key, 3);
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 }
Barry Mienydd671972010-10-04 16:33:58 +0200686
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 }
Barry Mienydd671972010-10-04 16:33:58 +0200688
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 return $this;
690 }
691
692 // --------------------------------------------------------------------
693
694 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200695 * WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200697 * Generates a WHERE field IN('item', 'item') SQL query,
698 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200700 * @param string $key The field to search
701 * @param array $values The values searched on
702 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500703 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300705 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300707 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 }
Barry Mienydd671972010-10-04 16:33:58 +0200709
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 // --------------------------------------------------------------------
711
712 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200713 * OR WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200715 * Generates a WHERE field IN('item', 'item') SQL query,
716 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200718 * @param string $key The field to search
719 * @param array $values The values searched on
720 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500721 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300723 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300725 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 }
727
728 // --------------------------------------------------------------------
729
730 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200731 * WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200733 * Generates a WHERE field NOT IN('item', 'item') SQL query,
734 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200736 * @param string $key The field to search
737 * @param array $values The values searched on
738 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500739 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300741 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300743 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 }
Barry Mienydd671972010-10-04 16:33:58 +0200745
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 // --------------------------------------------------------------------
747
748 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200749 * OR WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200751 * Generates a WHERE field NOT IN('item', 'item') SQL query,
752 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200754 * @param string $key The field to search
755 * @param array $values The values searched on
756 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500757 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300759 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300761 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 }
763
764 // --------------------------------------------------------------------
765
766 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200767 * Internal WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200769 * @used-by where_in()
770 * @used-by or_where_in()
771 * @used-by where_not_in()
772 * @used-by or_where_not_in()
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200774 * @param string $key The field to search
775 * @param array $values The values searched on
776 * @param bool $not If the statement would be IN or NOT IN
777 * @param string $type
778 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500779 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300781 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 {
783 if ($key === NULL OR $values === NULL)
784 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300785 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 }
Barry Mienydd671972010-10-04 16:33:58 +0200787
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 if ( ! is_array($values))
789 {
790 $values = array($values);
791 }
Barry Mienydd671972010-10-04 16:33:58 +0200792
Andrey Andreev498c1e02012-06-16 03:34:10 +0300793 is_bool($escape) OR $escape = $this->_protect_identifiers;
794
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 $not = ($not) ? ' NOT' : '';
796
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300797 if ($escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 {
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300799 $where_in = array();
800 foreach ($values as $value)
801 {
802 $where_in[] = $this->escape($value);
803 }
804 }
805 else
806 {
807 $where_in = array_values($values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 }
809
Andrey Andreev0d2d84f2015-07-31 13:48:59 +0300810 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
811 ? $this->_group_get_type('')
812 : $this->_group_get_type($type);
813
Andrey Andreev6e704752012-07-18 00:46:33 +0300814 $where_in = array(
Andrey Andreev94611df2012-07-19 12:29:54 +0300815 'condition' => $prefix.$key.$not.' IN('.implode(', ', $where_in).')',
Andrey Andreev6e704752012-07-18 00:46:33 +0300816 'escape' => $escape
817 );
Barry Mienydd671972010-10-04 16:33:58 +0200818
Andrey Andreev6e704752012-07-18 00:46:33 +0300819 $this->qb_where[] = $where_in;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000820 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000822 $this->qb_cache_where[] = $where_in;
823 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 }
825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 return $this;
827 }
Barry Mienydd671972010-10-04 16:33:58 +0200828
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 // --------------------------------------------------------------------
830
831 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200832 * LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200834 * Generates a %LIKE% portion of the query.
835 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200837 * @param mixed $field
838 * @param string $match
839 * @param string $side
840 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500841 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300843 public function like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300845 return $this->_like($field, $match, 'AND ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 }
847
848 // --------------------------------------------------------------------
849
850 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200851 * NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200853 * Generates a NOT LIKE portion of the query.
854 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200856 * @param mixed $field
857 * @param string $match
858 * @param string $side
859 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500860 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300862 public function not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300864 return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 }
Barry Mienydd671972010-10-04 16:33:58 +0200866
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 // --------------------------------------------------------------------
868
869 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200870 * OR LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000871 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200872 * Generates a %LIKE% portion of the query.
873 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200875 * @param mixed $field
876 * @param string $match
877 * @param string $side
878 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500879 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300881 public function or_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300883 return $this->_like($field, $match, 'OR ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 }
885
886 // --------------------------------------------------------------------
887
888 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200889 * OR NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200891 * Generates a NOT LIKE portion of the query.
892 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200894 * @param mixed $field
895 * @param string $match
896 * @param string $side
897 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500898 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300900 public function or_not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300902 return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 }
Barry Mienydd671972010-10-04 16:33:58 +0200904
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 // --------------------------------------------------------------------
906
907 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200908 * Internal LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200910 * @used-by like()
911 * @used-by or_like()
912 * @used-by not_like()
913 * @used-by or_not_like()
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200915 * @param mixed $field
916 * @param string $match
917 * @param string $type
918 * @param string $side
919 * @param string $not
920 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500921 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300923 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 {
925 if ( ! is_array($field))
926 {
927 $field = array($field => $match);
928 }
Barry Mienydd671972010-10-04 16:33:58 +0200929
Andrey Andreevb0478652012-07-18 15:34:46 +0300930 is_bool($escape) OR $escape = $this->_protect_identifiers;
Andrey Andreev19311362015-04-07 00:02:14 +0300931 // lowercase $side in case somebody writes e.g. 'BEFORE' instead of 'before' (doh)
932 $side = strtolower($side);
Andrey Andreevb0478652012-07-18 15:34:46 +0300933
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 foreach ($field as $k => $v)
935 {
Andrey Andreev41738232012-11-30 00:13:17 +0200936 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
937 ? $this->_group_get_type('') : $this->_group_get_type($type);
938
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300939 if ($escape === TRUE)
940 {
941 $v = $this->escape_like_str($v);
942 }
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300943
Andrey Andreev24276a32012-01-08 02:44:38 +0200944 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400945 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300946 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400947 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200948 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300950 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200952 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300954 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 }
956 else
957 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300958 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600960
Derek Jonese4ed5832009-02-20 21:44:59 +0000961 // some platforms require an escape sequence definition for LIKE wildcards
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300962 if ($escape === TRUE && $this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000963 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300964 $like_statement .= sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000965 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600966
Andrey Andreevb0478652012-07-18 15:34:46 +0300967 $this->qb_where[] = array('condition' => $like_statement, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000968 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 {
Andrey Andreev55bbd722013-01-28 19:02:13 +0200970 $this->qb_cache_where[] = array('condition' => $like_statement, 'escape' => $escape);
Andrey Andreevededc4a2012-07-18 01:16:15 +0300971 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200974
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 return $this;
976 }
Barry Mienydd671972010-10-04 16:33:58 +0200977
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 // --------------------------------------------------------------------
979
980 /**
WanWizard7219c072011-12-28 14:09:05 +0100981 * Starts a query group.
982 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200983 * @param string $not (Internal use only)
984 * @param string $type (Internal use only)
Andrew Podner4296a652012-12-17 07:51:15 -0500985 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +0100986 */
987 public function group_start($not = '', $type = 'AND ')
988 {
989 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100990
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000991 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100992 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Andrey Andreev6e704752012-07-18 00:46:33 +0300993 $where = array(
994 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (',
995 'escape' => FALSE
996 );
WanWizard7219c072011-12-28 14:09:05 +0100997
Andrey Andreev6e704752012-07-18 00:46:33 +0300998 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000999 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +01001000 {
Andrey Andreev6e704752012-07-18 00:46:33 +03001001 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +01001002 }
1003
1004 return $this;
1005 }
1006
1007 // --------------------------------------------------------------------
1008
1009 /**
1010 * Starts a query group, but ORs the group
1011 *
Andrew Podner4296a652012-12-17 07:51:15 -05001012 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001013 */
1014 public function or_group_start()
1015 {
1016 return $this->group_start('', 'OR ');
1017 }
1018
1019 // --------------------------------------------------------------------
1020
1021 /**
1022 * Starts a query group, but NOTs the group
1023 *
Andrew Podner4296a652012-12-17 07:51:15 -05001024 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001025 */
1026 public function not_group_start()
1027 {
1028 return $this->group_start('NOT ', 'AND ');
1029 }
1030
1031 // --------------------------------------------------------------------
1032
1033 /**
1034 * Starts a query group, but OR NOTs the group
1035 *
Andrew Podner4296a652012-12-17 07:51:15 -05001036 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001037 */
1038 public function or_not_group_start()
1039 {
1040 return $this->group_start('NOT ', 'OR ');
1041 }
1042
1043 // --------------------------------------------------------------------
1044
1045 /**
1046 * Ends a query group
1047 *
Andrew Podner4296a652012-12-17 07:51:15 -05001048 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001049 */
1050 public function group_end()
1051 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001052 $this->qb_where_group_started = FALSE;
Andrey Andreev6e704752012-07-18 00:46:33 +03001053 $where = array(
1054 'condition' => str_repeat(' ', $this->qb_where_group_count--).')',
1055 'escape' => FALSE
1056 );
WanWizard7219c072011-12-28 14:09:05 +01001057
Andrey Andreev6e704752012-07-18 00:46:33 +03001058 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001059 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +01001060 {
Andrey Andreev6e704752012-07-18 00:46:33 +03001061 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +01001062 }
1063
WanWizard7219c072011-12-28 14:09:05 +01001064 return $this;
1065 }
1066
1067 // --------------------------------------------------------------------
1068
1069 /**
1070 * Group_get_type
1071 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001072 * @used-by group_start()
1073 * @used-by _like()
1074 * @used-by _wh()
1075 * @used-by _where_in()
WanWizard7219c072011-12-28 14:09:05 +01001076 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001077 * @param string $type
WanWizard7219c072011-12-28 14:09:05 +01001078 * @return string
1079 */
1080 protected function _group_get_type($type)
1081 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001082 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +01001083 {
1084 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001085 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +01001086 }
1087
1088 return $type;
1089 }
1090
1091 // --------------------------------------------------------------------
1092
1093 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 * GROUP BY
1095 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001096 * @param string $by
1097 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -05001098 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001100 public function group_by($by, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001102 is_bool($escape) OR $escape = $this->_protect_identifiers;
1103
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 if (is_string($by))
1105 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001106 $by = ($escape === TRUE)
1107 ? explode(',', $by)
1108 : array($by);
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 }
Barry Mienydd671972010-10-04 16:33:58 +02001110
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 foreach ($by as $val)
1112 {
1113 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +02001114
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001115 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001117 $val = array('field' => $val, 'escape' => $escape);
Barry Mienydd671972010-10-04 16:33:58 +02001118
Andrey Andreev96feb582012-07-19 13:12:34 +03001119 $this->qb_groupby[] = $val;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001120 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001122 $this->qb_cache_groupby[] = $val;
1123 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 }
1125 }
1126 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001127
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 return $this;
1129 }
1130
1131 // --------------------------------------------------------------------
1132
1133 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001134 * HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001136 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001138 * @param string $key
1139 * @param string $value
1140 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 * @return object
1142 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001143 public function having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001145 return $this->_wh('qb_having', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 }
Barry Mienydd671972010-10-04 16:33:58 +02001147
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 // --------------------------------------------------------------------
1149
1150 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001151 * OR HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001153 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001155 * @param string $key
1156 * @param string $value
1157 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 * @return object
1159 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001160 public function or_having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001162 return $this->_wh('qb_having', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 }
Barry Mienydd671972010-10-04 16:33:58 +02001164
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 // --------------------------------------------------------------------
1166
1167 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001168 * ORDER BY
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001170 * @param string $orderby
vlakoff9f8e2992013-09-08 14:05:04 +02001171 * @param string $direction ASC, DESC or RANDOM
Andrey Andreevae85eb42012-11-02 01:42:31 +02001172 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -05001173 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 */
Andrey Andreevd24160c2012-06-16 03:21:20 +03001175 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001177 $direction = strtoupper(trim($direction));
Andrey Andreev2d486232012-07-19 14:46:51 +03001178
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001179 if ($direction === 'RANDOM')
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001181 $direction = '';
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001182
1183 // Do we have a seed value?
1184 $orderby = ctype_digit((string) $orderby)
vlakoffe6c4d5b2013-09-08 13:54:57 +02001185 ? sprintf($this->_random_keyword[1], $orderby)
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001186 : $this->_random_keyword[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 }
Andrey Andreev2d486232012-07-19 14:46:51 +03001188 elseif (empty($orderby))
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001190 return $this;
1191 }
1192 elseif ($direction !== '')
1193 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001194 $direction = in_array($direction, array('ASC', 'DESC'), TRUE) ? ' '.$direction : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 }
Barry Mienydd671972010-10-04 16:33:58 +02001196
Andrey Andreevd24160c2012-06-16 03:21:20 +03001197 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +02001198
Andrey Andreev2d486232012-07-19 14:46:51 +03001199 if ($escape === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 {
Andrey Andreev93dd2f22012-10-24 10:09:18 +03001201 $qb_orderby[] = array('field' => $orderby, 'direction' => $direction, 'escape' => FALSE);
Andrey Andreev2d486232012-07-19 14:46:51 +03001202 }
1203 else
1204 {
1205 $qb_orderby = array();
1206 foreach (explode(',', $orderby) as $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001208 $qb_orderby[] = ($direction === '' && preg_match('/\s+(ASC|DESC)$/i', rtrim($field), $match, PREG_OFFSET_CAPTURE))
1209 ? array('field' => ltrim(substr($field, 0, $match[0][1])), 'direction' => ' '.$match[1][0], 'escape' => TRUE)
1210 : array('field' => trim($field), 'direction' => $direction, 'escape' => TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 }
Barry Mienydd671972010-10-04 16:33:58 +02001213
Andrey Andreev2d486232012-07-19 14:46:51 +03001214 $this->qb_orderby = array_merge($this->qb_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001215 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001217 $this->qb_cache_orderby = array_merge($this->qb_cache_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001218 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 }
1220
1221 return $this;
1222 }
Barry Mienydd671972010-10-04 16:33:58 +02001223
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 // --------------------------------------------------------------------
1225
1226 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001227 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +00001228 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001229 * @param int $value LIMIT value
1230 * @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 */
Andrey Andreev4a587f52014-12-11 16:27:15 +02001233 public function limit($value, $offset = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 {
vlakoff912f1bc2013-01-15 03:34:12 +01001235 is_null($value) OR $this->qb_limit = (int) $value;
Andrey Andreev777153d2012-06-18 13:30:45 +03001236 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001237
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 return $this;
1239 }
Barry Mienydd671972010-10-04 16:33:58 +02001240
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 // --------------------------------------------------------------------
1242
1243 /**
1244 * Sets the OFFSET value
1245 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001246 * @param int $offset OFFSET value
Andrew Podner4296a652012-12-17 07:51:15 -05001247 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001249 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001251 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 return $this;
1253 }
Barry Mienydd671972010-10-04 16:33:58 +02001254
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 // --------------------------------------------------------------------
1256
1257 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001258 * LIMIT string
Andrey Andreev2c35b642012-06-24 03:05:26 +03001259 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001260 * Generates a platform-specific LIMIT clause.
Andrey Andreev2c35b642012-06-24 03:05:26 +03001261 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001262 * @param string $sql SQL Query
Andrey Andreev2c35b642012-06-24 03:05:26 +03001263 * @return string
1264 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001265 protected function _limit($sql)
Andrey Andreev2c35b642012-06-24 03:05:26 +03001266 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001267 return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').$this->qb_limit;
Andrey Andreev2c35b642012-06-24 03:05:26 +03001268 }
1269
1270 // --------------------------------------------------------------------
1271
1272 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001273 * The "set" function.
1274 *
1275 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 * @param mixed
1278 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001279 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001280 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001282 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 {
1284 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001285
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 if ( ! is_array($key))
1287 {
1288 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001289 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001290
Andrey Andreevfe642da2012-06-16 03:47:33 +03001291 is_bool($escape) OR $escape = $this->_protect_identifiers;
1292
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 foreach ($key as $k => $v)
1294 {
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001295 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1296 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 }
Barry Mienydd671972010-10-04 16:33:58 +02001298
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 return $this;
1300 }
WanWizard7219c072011-12-28 14:09:05 +01001301
Kyle Farris0c147b32011-08-26 02:29:31 -04001302 // --------------------------------------------------------------------
1303
1304 /**
1305 * Get SELECT query string
1306 *
1307 * Compiles a SELECT query string and returns the sql.
1308 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001309 * @param string the table name to select from (optional)
Calvin Tam55bc5052015-07-24 02:27:24 -07001310 * @param bool TRUE: resets QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001311 * @return string
1312 */
WanWizard7219c072011-12-28 14:09:05 +01001313 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001314 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001315 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001316 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001317 $this->_track_aliases($table);
1318 $this->from($table);
1319 }
WanWizard7219c072011-12-28 14:09:05 +01001320
Andrey Andreev650b4c02012-06-11 12:07:15 +03001321 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001322
Kyle Farris0c147b32011-08-26 02:29:31 -04001323 if ($reset === TRUE)
1324 {
1325 $this->_reset_select();
1326 }
WanWizard7219c072011-12-28 14:09:05 +01001327
Kyle Farris0c147b32011-08-26 02:29:31 -04001328 return $select;
1329 }
WanWizard7219c072011-12-28 14:09:05 +01001330
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 // --------------------------------------------------------------------
1332
1333 /**
1334 * Get
1335 *
1336 * Compiles the select statement based on the other functions called
1337 * and runs the query
1338 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 * @param string the table
1340 * @param string the limit clause
1341 * @param string the offset clause
1342 * @return object
1343 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001344 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001346 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 {
1348 $this->_track_aliases($table);
1349 $this->from($table);
1350 }
Barry Mienydd671972010-10-04 16:33:58 +02001351
Andrey Andreev650b4c02012-06-11 12:07:15 +03001352 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 {
1354 $this->limit($limit, $offset);
1355 }
Barry Mienydd671972010-10-04 16:33:58 +02001356
Andrey Andreev24276a32012-01-08 02:44:38 +02001357 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001358 $this->_reset_select();
1359 return $result;
1360 }
1361
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001362 // --------------------------------------------------------------------
1363
Derek Allard2067d1a2008-11-13 22:59:24 +00001364 /**
1365 * "Count All Results" query
1366 *
Barry Mienydd671972010-10-04 16:33:58 +02001367 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001368 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001369 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001370 * @param string
yaoshanliang2f164052015-03-16 16:48:15 +08001371 * @param bool the reset clause
vlakoffc6ac7482013-11-17 00:50:06 +01001372 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 */
yaoshanliang19c28472015-03-15 10:42:18 +08001374 public function count_all_results($table = '', $reset = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001376 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001377 {
1378 $this->_track_aliases($table);
1379 $this->from($table);
1380 }
Barry Mienydd671972010-10-04 16:33:58 +02001381
Andrey Andreev075bdb42016-01-25 13:31:23 +02001382 // ORDER BY usage is often problematic here (most notably
1383 // on Microsoft SQL Server) and ultimately unnecessary
1384 // for selecting COUNT(*) ...
1385 if ( ! empty($this->qb_orderby))
1386 {
1387 $orderby = $this->qb_orderby;
1388 $this->qb_orderby = NULL;
1389 }
1390
1391 $result = ($this->qb_distinct === TRUE)
Andrey Andreevb05f5062012-10-26 12:01:02 +03001392 ? $this->query($this->_count_string.$this->protect_identifiers('numrows')."\nFROM (\n".$this->_compile_select()."\n) CI_count_all_results")
1393 : $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
yaoshanliang9971e7b2015-03-14 13:09:16 +08001394
yaoshanliang2f164052015-03-16 16:48:15 +08001395 if ($reset === TRUE)
yaoshanliang19c28472015-03-15 10:42:18 +08001396 {
yaoshanliang2f164052015-03-16 16:48:15 +08001397 $this->_reset_select();
yaoshanliang19c28472015-03-15 10:42:18 +08001398 }
Andrey Andreev075bdb42016-01-25 13:31:23 +02001399 // If we've previously reset the qb_orderby values, get them back
1400 elseif ( ! isset($this->qb_orderby))
1401 {
1402 $this->qb_orderby = $orderby;
1403 }
Barry Mienydd671972010-10-04 16:33:58 +02001404
Purwandi1d160e72012-01-09 16:33:28 +07001405 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001406 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001407 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001408 }
1409
Purwandi1d160e72012-01-09 16:33:28 +07001410 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001411 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001412 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001413
Derek Allard2067d1a2008-11-13 22:59:24 +00001414 // --------------------------------------------------------------------
1415
1416 /**
1417 * Get_Where
1418 *
1419 * Allows the where clause, limit and offset to be added directly
1420 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001421 * @param string $table
1422 * @param string $where
1423 * @param int $limit
1424 * @param int $offset
Derek Allard2067d1a2008-11-13 22:59:24 +00001425 * @return object
1426 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001427 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001428 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001429 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 {
1431 $this->from($table);
1432 }
1433
vlakoff1228fe22013-01-14 01:30:09 +01001434 if ($where !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001435 {
1436 $this->where($where);
1437 }
Barry Mienydd671972010-10-04 16:33:58 +02001438
Andrey Andreev650b4c02012-06-11 12:07:15 +03001439 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001440 {
1441 $this->limit($limit, $offset);
1442 }
Barry Mienydd671972010-10-04 16:33:58 +02001443
Andrey Andreev24276a32012-01-08 02:44:38 +02001444 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001445 $this->_reset_select();
1446 return $result;
1447 }
1448
1449 // --------------------------------------------------------------------
1450
1451 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001452 * Insert_Batch
1453 *
1454 * Compiles batch insert strings and runs the queries
1455 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001456 * @param string $table Table to insert into
1457 * @param array $set An associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001458 * @param bool $escape Whether to escape values and identifiers
Andrey Andreevae85eb42012-11-02 01:42:31 +02001459 * @return int Number of rows inserted or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001460 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001461 public function insert_batch($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001462 {
vlakoff1228fe22013-01-14 01:30:09 +01001463 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001464 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001465 $this->set_insert_batch($set, '', $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001466 }
Barry Mienydd671972010-10-04 16:33:58 +02001467
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001468 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001469 {
Andrey Andreev9f808b02012-10-24 17:38:48 +03001470 // No valid data array. Folds in cases where keys and values did not match up
1471 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001472 }
1473
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001474 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001475 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001476 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001477 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001478 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001479 }
Barry Mienydd671972010-10-04 16:33:58 +02001480
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001481 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001482 }
1483
1484 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001485 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001486 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001487 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001488 $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 +03001489 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001490 }
Barry Mienydd671972010-10-04 16:33:58 +02001491
Derek Jonesd10e8962010-03-02 17:10:36 -06001492 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001493 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001494 }
1495
1496 // --------------------------------------------------------------------
1497
1498 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +02001499 * Insert batch statement
Andrey Andreev97f36972012-04-05 12:44:36 +03001500 *
1501 * Generates a platform-specific insert string from the supplied data.
1502 *
Andrey Andreev083e3c82012-11-06 12:48:32 +02001503 * @param string $table Table name
1504 * @param array $keys INSERT keys
1505 * @param array $values INSERT values
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001506 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001507 */
1508 protected function _insert_batch($table, $keys, $values)
1509 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001510 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001511 }
1512
1513 // --------------------------------------------------------------------
1514
1515 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001516 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001517 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001518 * @param mixed
1519 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001520 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001521 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001522 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001523 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001524 {
1525 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001526
Derek Jonesd10e8962010-03-02 17:10:36 -06001527 if ( ! is_array($key))
1528 {
1529 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001530 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001531
Andrey Andreevfe642da2012-06-16 03:47:33 +03001532 is_bool($escape) OR $escape = $this->_protect_identifiers;
1533
Iban Eguia3c0a4522012-04-15 13:30:44 +02001534 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001535 sort($keys);
1536
1537 foreach ($key as $row)
1538 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001539 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001540 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1541 {
1542 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001543 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001544 return;
1545 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001546
Barry Mienydd671972010-10-04 16:33:58 +02001547 ksort($row); // puts $row in the same order as our keys
1548
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001549 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001550 {
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001551 $clean = array();
1552 foreach ($row as $value)
1553 {
1554 $clean[] = $this->escape($value);
1555 }
1556
1557 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001558 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001559
Andrey Andreev838a9d62012-12-03 14:37:47 +02001560 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001561 }
1562
1563 foreach ($keys as $k)
1564 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001565 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001566 }
Barry Mienydd671972010-10-04 16:33:58 +02001567
Derek Jonesd10e8962010-03-02 17:10:36 -06001568 return $this;
1569 }
WanWizard7219c072011-12-28 14:09:05 +01001570
Kyle Farris0c147b32011-08-26 02:29:31 -04001571 // --------------------------------------------------------------------
1572
1573 /**
1574 * Get INSERT query string
1575 *
1576 * Compiles an insert query and returns the sql
1577 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001578 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001579 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001580 * @return string
1581 */
1582 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001583 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001584 if ($this->_validate_insert($table) === FALSE)
1585 {
1586 return FALSE;
1587 }
WanWizard7219c072011-12-28 14:09:05 +01001588
Kyle Farris76116012011-08-31 11:17:48 -04001589 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001590 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001591 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001592 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001593 array_keys($this->qb_set),
1594 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001595 );
WanWizard7219c072011-12-28 14:09:05 +01001596
Kyle Farris0c147b32011-08-26 02:29:31 -04001597 if ($reset === TRUE)
1598 {
1599 $this->_reset_write();
1600 }
WanWizard7219c072011-12-28 14:09:05 +01001601
Kyle Farris0c147b32011-08-26 02:29:31 -04001602 return $sql;
1603 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001604
Derek Allard2067d1a2008-11-13 22:59:24 +00001605 // --------------------------------------------------------------------
1606
1607 /**
1608 * Insert
1609 *
1610 * Compiles an insert string and runs the query
1611 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001612 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001613 * @param array an associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001614 * @param bool $escape Whether to escape values and identifiers
Derek Allard2067d1a2008-11-13 22:59:24 +00001615 * @return object
1616 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001617 public function insert($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001618 {
vlakoff1228fe22013-01-14 01:30:09 +01001619 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001620 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001621 $this->set($set, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001622 }
WanWizard7219c072011-12-28 14:09:05 +01001623
Kyle Farris0c147b32011-08-26 02:29:31 -04001624 if ($this->_validate_insert($table) === FALSE)
1625 {
1626 return FALSE;
1627 }
WanWizard7219c072011-12-28 14:09:05 +01001628
Kyle Farris76116012011-08-31 11:17:48 -04001629 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001630 $this->protect_identifiers(
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001631 $this->qb_from[0], TRUE, $escape, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001632 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001633 array_keys($this->qb_set),
1634 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001635 );
Barry Mienydd671972010-10-04 16:33:58 +02001636
Kyle Farris0c147b32011-08-26 02:29:31 -04001637 $this->_reset_write();
1638 return $this->query($sql);
1639 }
WanWizard7219c072011-12-28 14:09:05 +01001640
Kyle Farris0c147b32011-08-26 02:29:31 -04001641 // --------------------------------------------------------------------
1642
1643 /**
1644 * Validate Insert
1645 *
1646 * This method is used by both insert() and get_compiled_insert() to
1647 * validate that the there data is actually being set and that table
1648 * has been chosen to be inserted into.
1649 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001650 * @param string the table to insert data into
1651 * @return string
1652 */
WanWizard7219c072011-12-28 14:09:05 +01001653 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001654 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001655 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001656 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001657 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001658 }
1659
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001660 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001661 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001662 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001663 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001664 elseif ( ! isset($this->qb_from[0]))
1665 {
1666 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1667 }
WanWizard7219c072011-12-28 14:09:05 +01001668
Kyle Farris0c147b32011-08-26 02:29:31 -04001669 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001670 }
Barry Mienydd671972010-10-04 16:33:58 +02001671
Phil Sturgeon9789f322011-07-15 15:14:05 -06001672 // --------------------------------------------------------------------
1673
1674 /**
1675 * Replace
1676 *
1677 * Compiles an replace into string and runs the query
1678 *
1679 * @param string the table to replace data into
1680 * @param array an associative array of insert values
1681 * @return object
1682 */
1683 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001684 {
vlakoff1228fe22013-01-14 01:30:09 +01001685 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001686 {
1687 $this->set($set);
1688 }
Barry Mienydd671972010-10-04 16:33:58 +02001689
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001690 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001691 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001692 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001693 }
1694
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001695 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001696 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001697 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001698 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001699 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001700 }
Barry Mienydd671972010-10-04 16:33:58 +02001701
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001702 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001703 }
1704
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001705 $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 +00001706
Derek Jonesd10e8962010-03-02 17:10:36 -06001707 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001708 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001709 }
WanWizard7219c072011-12-28 14:09:05 +01001710
Kyle Farris0c147b32011-08-26 02:29:31 -04001711 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001712
Kyle Farris0c147b32011-08-26 02:29:31 -04001713 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001714 * Replace statement
1715 *
1716 * Generates a platform-specific replace string from the supplied data
1717 *
1718 * @param string the table name
1719 * @param array the insert keys
1720 * @param array the insert values
1721 * @return string
1722 */
1723 protected function _replace($table, $keys, $values)
1724 {
1725 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1726 }
1727
1728 // --------------------------------------------------------------------
1729
1730 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001731 * FROM tables
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001732 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001733 * Groups tables in FROM clauses if needed, so there is no confusion
1734 * about operator precedence.
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001735 *
Claudio Galdiolo93e78132015-01-29 11:43:56 -05001736 * Note: This is only used (and overridden) by MySQL and CUBRID.
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001737 *
1738 * @return string
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001739 */
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001740 protected function _from_tables()
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001741 {
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001742 return implode(', ', $this->qb_from);
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001743 }
1744
1745 // --------------------------------------------------------------------
1746
1747 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001748 * Get UPDATE query string
1749 *
1750 * Compiles an update query and returns the sql
1751 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001752 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001753 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001754 * @return string
1755 */
1756 public function get_compiled_update($table = '', $reset = TRUE)
1757 {
1758 // Combine any cached components with the current statements
1759 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001760
Kyle Farris0c147b32011-08-26 02:29:31 -04001761 if ($this->_validate_update($table) === FALSE)
1762 {
1763 return FALSE;
1764 }
WanWizard7219c072011-12-28 14:09:05 +01001765
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001766 $sql = $this->_update($this->qb_from[0], $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001767
Kyle Farris0c147b32011-08-26 02:29:31 -04001768 if ($reset === TRUE)
1769 {
1770 $this->_reset_write();
1771 }
WanWizard7219c072011-12-28 14:09:05 +01001772
Kyle Farris0c147b32011-08-26 02:29:31 -04001773 return $sql;
1774 }
WanWizard7219c072011-12-28 14:09:05 +01001775
Derek Allard2067d1a2008-11-13 22:59:24 +00001776 // --------------------------------------------------------------------
1777
1778 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001779 * UPDATE
Derek Allard2067d1a2008-11-13 22:59:24 +00001780 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001781 * Compiles an update string and runs the query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001782 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001783 * @param string $table
1784 * @param array $set An associative array of update values
1785 * @param mixed $where
1786 * @param int $limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001787 * @return object
1788 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001789 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001790 {
1791 // Combine any cached components with the current statements
1792 $this->_merge_cache();
1793
vlakoff1228fe22013-01-14 01:30:09 +01001794 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001795 {
1796 $this->set($set);
1797 }
Barry Mienydd671972010-10-04 16:33:58 +02001798
Kyle Farris0c147b32011-08-26 02:29:31 -04001799 if ($this->_validate_update($table) === FALSE)
1800 {
1801 return FALSE;
1802 }
1803
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001804 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001805 {
1806 $this->where($where);
1807 }
1808
Andrey Andreev650b4c02012-06-11 12:07:15 +03001809 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001810 {
1811 $this->limit($limit);
1812 }
1813
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001814 $sql = $this->_update($this->qb_from[0], $this->qb_set);
Kyle Farris0c147b32011-08-26 02:29:31 -04001815 $this->_reset_write();
1816 return $this->query($sql);
1817 }
WanWizard7219c072011-12-28 14:09:05 +01001818
Kyle Farris0c147b32011-08-26 02:29:31 -04001819 // --------------------------------------------------------------------
1820
1821 /**
1822 * Validate Update
1823 *
1824 * This method is used by both update() and get_compiled_update() to
1825 * validate that data is actually being set and that a table has been
1826 * chosen to be update.
1827 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001828 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001829 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001830 */
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001831 protected function _validate_update($table)
Kyle Farris0c147b32011-08-26 02:29:31 -04001832 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001833 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001834 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001835 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001836 }
1837
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001838 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001839 {
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001840 $this->qb_from = array($this->protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +00001841 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001842 elseif ( ! isset($this->qb_from[0]))
1843 {
1844 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1845 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001846
1847 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001848 }
WanWizard7219c072011-12-28 14:09:05 +01001849
Derek Jonesd10e8962010-03-02 17:10:36 -06001850 // --------------------------------------------------------------------
1851
1852 /**
1853 * Update_Batch
1854 *
1855 * Compiles an update string and runs the query
1856 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001857 * @param string the table to retrieve the results from
1858 * @param array an associative array of update values
1859 * @param string the where key
Andrey Andreev9f808b02012-10-24 17:38:48 +03001860 * @return int number of rows affected or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001861 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001862 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001863 {
1864 // Combine any cached components with the current statements
1865 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001866
vlakoff1228fe22013-01-14 01:30:09 +01001867 if ($index === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001868 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001869 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001870 }
1871
vlakoff1228fe22013-01-14 01:30:09 +01001872 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001873 {
1874 $this->set_update_batch($set, $index);
1875 }
1876
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001877 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001878 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001879 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001880 }
1881
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001882 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001883 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001884 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001885 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001886 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001887 }
Barry Mienydd671972010-10-04 16:33:58 +02001888
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001889 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001890 }
Barry Mienydd671972010-10-04 16:33:58 +02001891
Derek Jonesd10e8962010-03-02 17:10:36 -06001892 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001893 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001894 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001895 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001896 $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 +03001897 $affected_rows += $this->affected_rows();
Andrey Andreev79f888b2013-08-06 13:59:23 +03001898 $this->qb_where = array();
Derek Jonesd10e8962010-03-02 17:10:36 -06001899 }
Barry Mienydd671972010-10-04 16:33:58 +02001900
Derek Jonesd10e8962010-03-02 17:10:36 -06001901 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001902 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001903 }
1904
1905 // --------------------------------------------------------------------
1906
1907 /**
Andrey Andreev219565d2013-03-12 20:00:08 +02001908 * Update_Batch statement
1909 *
1910 * Generates a platform-specific batch update string from the supplied data
1911 *
1912 * @param string $table Table name
1913 * @param array $values Update data
1914 * @param string $index WHERE key
1915 * @return string
1916 */
1917 protected function _update_batch($table, $values, $index)
1918 {
1919 $ids = array();
1920 foreach ($values as $key => $val)
1921 {
1922 $ids[] = $val[$index];
1923
1924 foreach (array_keys($val) as $field)
1925 {
1926 if ($field !== $index)
1927 {
1928 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
1929 }
1930 }
1931 }
1932
1933 $cases = '';
1934 foreach ($final as $k => $v)
1935 {
1936 $cases .= $k." = CASE \n"
1937 .implode("\n", $v)."\n"
1938 .'ELSE '.$k.' END, ';
1939 }
1940
1941 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
1942
1943 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
1944 }
1945
1946 // --------------------------------------------------------------------
1947
1948 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001949 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001950 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001951 * @param array
1952 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001953 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001954 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001955 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001956 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001957 {
1958 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001959
Derek Jonesd10e8962010-03-02 17:10:36 -06001960 if ( ! is_array($key))
1961 {
1962 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001963 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001964
Andrey Andreevfe642da2012-06-16 03:47:33 +03001965 is_bool($escape) OR $escape = $this->_protect_identifiers;
1966
Derek Jonesd10e8962010-03-02 17:10:36 -06001967 foreach ($key as $k => $v)
1968 {
1969 $index_set = FALSE;
1970 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001971 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001972 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001973 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001974 {
1975 $index_set = TRUE;
1976 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001977
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001978 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001979 }
1980
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001981 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001982 {
1983 return $this->display_error('db_batch_missing_index');
1984 }
1985
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001986 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001987 }
Barry Mienydd671972010-10-04 16:33:58 +02001988
Derek Jonesd10e8962010-03-02 17:10:36 -06001989 return $this;
1990 }
1991
Derek Allard2067d1a2008-11-13 22:59:24 +00001992 // --------------------------------------------------------------------
1993
1994 /**
1995 * Empty Table
1996 *
1997 * Compiles a delete string and runs "DELETE FROM table"
1998 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001999 * @param string the table to empty
2000 * @return object
2001 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002002 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002003 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002004 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002005 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002006 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002007 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002008 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002009 }
2010
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002011 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002012 }
2013 else
2014 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002015 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002016 }
2017
2018 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002019 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00002020 return $this->query($sql);
2021 }
2022
2023 // --------------------------------------------------------------------
2024
2025 /**
2026 * Truncate
2027 *
2028 * Compiles a truncate string and runs the query
2029 * If the database does not support the truncate() command
2030 * This function maps to "DELETE FROM table"
2031 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002032 * @param string the table to truncate
2033 * @return object
2034 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002035 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002036 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002037 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002038 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002039 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002040 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002041 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002042 }
2043
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002044 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002045 }
2046 else
2047 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002048 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002049 }
2050
2051 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002052 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 return $this->query($sql);
2054 }
WanWizard7219c072011-12-28 14:09:05 +01002055
Kyle Farris0c147b32011-08-26 02:29:31 -04002056 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02002057
Kyle Farris0c147b32011-08-26 02:29:31 -04002058 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03002059 * Truncate statement
2060 *
2061 * Generates a platform-specific truncate string from the supplied data
2062 *
2063 * If the database does not support the truncate() command,
2064 * then this method maps to 'DELETE FROM table'
2065 *
2066 * @param string the table name
2067 * @return string
2068 */
2069 protected function _truncate($table)
2070 {
2071 return 'TRUNCATE '.$table;
2072 }
2073
2074 // --------------------------------------------------------------------
2075
2076 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04002077 * Get DELETE query string
2078 *
2079 * Compiles a delete query string and returns the sql
2080 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002081 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002082 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04002083 * @return string
2084 */
2085 public function get_compiled_delete($table = '', $reset = TRUE)
2086 {
2087 $this->return_delete_sql = TRUE;
2088 $sql = $this->delete($table, '', NULL, $reset);
2089 $this->return_delete_sql = FALSE;
2090 return $sql;
2091 }
WanWizard7219c072011-12-28 14:09:05 +01002092
Derek Allard2067d1a2008-11-13 22:59:24 +00002093 // --------------------------------------------------------------------
2094
2095 /**
2096 * Delete
2097 *
2098 * Compiles a delete string and runs the query
2099 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002100 * @param mixed the table(s) to delete from. String or array
2101 * @param mixed the where clause
2102 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03002103 * @param bool
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002104 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00002105 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002106 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002107 {
2108 // Combine any cached components with the current statements
2109 $this->_merge_cache();
2110
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002111 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002112 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002113 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002114 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002115 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002116 }
2117
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002118 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002119 }
2120 elseif (is_array($table))
2121 {
Andrey Andreeva1170af2015-07-02 11:46:56 +03002122 empty($where) && $reset_data = FALSE;
2123
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05002124 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002125 {
Andrey Andreev13f50542012-10-12 12:31:02 +03002126 $this->delete($single_table, $where, $limit, $reset_data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002127 }
Andrey Andreeva1170af2015-07-02 11:46:56 +03002128
Derek Allard2067d1a2008-11-13 22:59:24 +00002129 return;
2130 }
2131 else
2132 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002133 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002134 }
2135
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002136 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002137 {
2138 $this->where($where);
2139 }
2140
Andrey Andreev650b4c02012-06-11 12:07:15 +03002141 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002142 {
2143 $this->limit($limit);
2144 }
2145
Andrey Andreev94611df2012-07-19 12:29:54 +03002146 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002147 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002148 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02002149 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002150
Andrey Andreevb0478652012-07-18 15:34:46 +03002151 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002152 if ($reset_data)
2153 {
2154 $this->_reset_write();
2155 }
WanWizard7219c072011-12-28 14:09:05 +01002156
Andrey Andreev24276a32012-01-08 02:44:38 +02002157 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00002158 }
WanWizard7219c072011-12-28 14:09:05 +01002159
Derek Allard2067d1a2008-11-13 22:59:24 +00002160 // --------------------------------------------------------------------
2161
2162 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03002163 * Delete statement
2164 *
2165 * Generates a platform-specific delete string from the supplied data
2166 *
2167 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03002168 * @return string
2169 */
Andrey Andreevb0478652012-07-18 15:34:46 +03002170 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03002171 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002172 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002173 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03002174 }
2175
2176 // --------------------------------------------------------------------
2177
2178 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002179 * DB Prefix
2180 *
2181 * Prepends a database prefix if one exists in configuration
2182 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002183 * @param string the table
2184 * @return string
2185 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002186 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002187 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002188 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002189 {
2190 $this->display_error('db_table_name_required');
2191 }
2192
2193 return $this->dbprefix.$table;
2194 }
2195
2196 // --------------------------------------------------------------------
2197
2198 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06002199 * Set DB Prefix
2200 *
2201 * Set's the DB Prefix to something new without needing to reconnect
2202 *
2203 * @param string the prefix
2204 * @return string
2205 */
2206 public function set_dbprefix($prefix = '')
2207 {
2208 return $this->dbprefix = $prefix;
2209 }
2210
2211 // --------------------------------------------------------------------
2212
2213 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002214 * Track Aliases
2215 *
2216 * Used to track SQL statements written with aliased tables.
2217 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002218 * @param string The table to inspect
2219 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002220 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002221 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002222 {
2223 if (is_array($table))
2224 {
2225 foreach ($table as $t)
2226 {
2227 $this->_track_aliases($t);
2228 }
2229 return;
2230 }
Barry Mienydd671972010-10-04 16:33:58 +02002231
Derek Jones37f4b9c2011-07-01 17:56:50 -05002232 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002233 // the string into discreet statements
2234 if (strpos($table, ',') !== FALSE)
2235 {
2236 return $this->_track_aliases(explode(',', $table));
2237 }
Barry Mienydd671972010-10-04 16:33:58 +02002238
Derek Allard2067d1a2008-11-13 22:59:24 +00002239 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002240 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002241 {
2242 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002243 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002244
Derek Allard2067d1a2008-11-13 22:59:24 +00002245 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002246 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002247
Derek Allard2067d1a2008-11-13 22:59:24 +00002248 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002249 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002250 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002251 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002252 }
2253 }
2254 }
WanWizard7219c072011-12-28 14:09:05 +01002255
Derek Allard2067d1a2008-11-13 22:59:24 +00002256 // --------------------------------------------------------------------
2257
2258 /**
2259 * Compile the SELECT statement
2260 *
2261 * Generates a query string based on which functions were used.
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002262 * Should not be called directly.
Derek Allard2067d1a2008-11-13 22:59:24 +00002263 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002264 * @param bool $select_override
Derek Allard2067d1a2008-11-13 22:59:24 +00002265 * @return string
2266 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002267 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002268 {
2269 // Combine any cached components with the current statements
2270 $this->_merge_cache();
2271
Derek Allard2067d1a2008-11-13 22:59:24 +00002272 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002273 if ($select_override !== FALSE)
2274 {
2275 $sql = $select_override;
2276 }
2277 else
2278 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002279 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002280
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002281 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002282 {
Barry Mienydd671972010-10-04 16:33:58 +02002283 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002284 }
2285 else
Barry Mienydd671972010-10-04 16:33:58 +02002286 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002287 // Cycle through the "select" portion of the query and prep each column name.
Andrey Andreev1924eb32015-04-08 17:19:24 +03002288 // The reason we protect identifiers here rather than in the select() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002289 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002290 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002291 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002292 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002293 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002294 }
Barry Mienydd671972010-10-04 16:33:58 +02002295
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002296 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002297 }
2298 }
2299
Derek Allard2067d1a2008-11-13 22:59:24 +00002300 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002301 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002302 {
Andrey Andreeve78f8152012-10-09 11:38:38 +03002303 $sql .= "\nFROM ".$this->_from_tables();
Derek Allard2067d1a2008-11-13 22:59:24 +00002304 }
2305
Derek Allard2067d1a2008-11-13 22:59:24 +00002306 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002307 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002308 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002309 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002310 }
2311
Andrey Andreev2d486232012-07-19 14:46:51 +03002312 $sql .= $this->_compile_wh('qb_where')
2313 .$this->_compile_group_by()
2314 .$this->_compile_wh('qb_having')
2315 .$this->_compile_order_by(); // ORDER BY
Andrey Andreevb0478652012-07-18 15:34:46 +03002316
Andrey Andreevd40459d2012-07-18 16:46:39 +03002317 // LIMIT
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002318 if ($this->qb_limit)
Derek Allard2067d1a2008-11-13 22:59:24 +00002319 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002320 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002321 }
2322
2323 return $sql;
2324 }
2325
2326 // --------------------------------------------------------------------
2327
2328 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002329 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002330 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002331 * Escapes identifiers in WHERE and HAVING statements at execution time.
2332 *
Andrey Andreevb0478652012-07-18 15:34:46 +03002333 * Required so that aliases are tracked properly, regardless of wether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002334 * where(), or_where(), having(), or_having are called prior to from(),
2335 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002336 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002337 * @param string $qb_key 'qb_where' or 'qb_having'
Andrey Andreevd40459d2012-07-18 16:46:39 +03002338 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002339 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002340 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002341 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002342 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002343 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002344 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002345 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002346 // Is this condition already compiled?
2347 if (is_string($this->{$qb_key}[$i]))
2348 {
2349 continue;
2350 }
2351 elseif ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002352 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002353 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002354 continue;
2355 }
2356
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002357 // Split multiple conditions
2358 $conditions = preg_split(
Andrey Andreev554b4522015-09-01 13:51:26 +03002359 '/((?:^|\s+)AND\s+|(?:^|\s+)OR\s+)/i',
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002360 $this->{$qb_key}[$i]['condition'],
2361 -1,
2362 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
2363 );
2364
2365 for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002366 {
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002367 if (($op = $this->_get_operator($conditions[$ci])) === FALSE
Andrey Andreeve4742582012-10-25 13:25:13 +03002368 OR ! preg_match('/^(\(?)(.*)('.preg_quote($op, '/').')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches))
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002369 {
2370 continue;
2371 }
2372
2373 // $matches = array(
2374 // 0 => '(test <= foo)', /* the whole thing */
2375 // 1 => '(', /* optional */
2376 // 2 => 'test', /* the field name */
2377 // 3 => ' <= ', /* $op */
2378 // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2379 // 5 => ')' /* optional */
2380 // );
Andrey Andreev082aa402012-10-22 19:41:55 +03002381
2382 if ( ! empty($matches[4]))
2383 {
2384 $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4]));
2385 $matches[4] = ' '.$matches[4];
2386 }
2387
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002388 $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2]))
2389 .' '.trim($matches[3]).$matches[4].$matches[5];
Andrey Andreev6e704752012-07-18 00:46:33 +03002390 }
2391
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002392 $this->{$qb_key}[$i] = implode('', $conditions);
Andrey Andreev6e704752012-07-18 00:46:33 +03002393 }
2394
Andrey Andreev9d3aa1b2012-10-12 12:14:09 +03002395 return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ")
2396 .implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002397 }
2398
Andrey Andreevb0478652012-07-18 15:34:46 +03002399 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002400 }
2401
2402 // --------------------------------------------------------------------
2403
2404 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002405 * Compile GROUP BY
2406 *
2407 * Escapes identifiers in GROUP BY statements at execution time.
2408 *
2409 * Required so that aliases are tracked properly, regardless of wether
2410 * group_by() is called prior to from(), join() and dbprefix is added
2411 * only if needed.
2412 *
2413 * @return string SQL statement
2414 */
2415 protected function _compile_group_by()
2416 {
2417 if (count($this->qb_groupby) > 0)
2418 {
Andrey Andreev96feb582012-07-19 13:12:34 +03002419 for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
2420 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002421 // Is it already compiled?
Andrey Andreev18eba242014-01-23 22:52:31 +02002422 if (is_string($this->qb_groupby[$i]))
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002423 {
2424 continue;
2425 }
2426
Andrey Andreev082aa402012-10-22 19:41:55 +03002427 $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 +03002428 ? $this->qb_groupby[$i]['field']
Andrey Andreev13f50542012-10-12 12:31:02 +03002429 : $this->protect_identifiers($this->qb_groupby[$i]['field']);
Andrey Andreev96feb582012-07-19 13:12:34 +03002430 }
2431
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002432 return "\nGROUP BY ".implode(', ', $this->qb_groupby);
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002433 }
2434
2435 return '';
2436 }
2437
2438 // --------------------------------------------------------------------
2439
2440 /**
Andrey Andreev2d486232012-07-19 14:46:51 +03002441 * Compile ORDER BY
2442 *
2443 * Escapes identifiers in ORDER BY statements at execution time.
2444 *
2445 * Required so that aliases are tracked properly, regardless of wether
2446 * order_by() is called prior to from(), join() and dbprefix is added
2447 * only if needed.
2448 *
2449 * @return string SQL statement
2450 */
2451 protected function _compile_order_by()
2452 {
Andrey Andreeva53ea842012-10-23 12:44:09 +03002453 if (is_array($this->qb_orderby) && count($this->qb_orderby) > 0)
Andrey Andreev2d486232012-07-19 14:46:51 +03002454 {
Andrey Andreev2d486232012-07-19 14:46:51 +03002455 for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++)
2456 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002457 if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field']))
Andrey Andreev2d486232012-07-19 14:46:51 +03002458 {
Andrey Andreevfc043b32012-10-12 14:46:14 +03002459 $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']);
Andrey Andreev2d486232012-07-19 14:46:51 +03002460 }
2461
2462 $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction'];
2463 }
2464
Andrey Andreeva53ea842012-10-23 12:44:09 +03002465 return $this->qb_orderby = "\nORDER BY ".implode(', ', $this->qb_orderby);
2466 }
2467 elseif (is_string($this->qb_orderby))
2468 {
2469 return $this->qb_orderby;
Andrey Andreev2d486232012-07-19 14:46:51 +03002470 }
2471
2472 return '';
2473 }
2474
2475 // --------------------------------------------------------------------
2476
2477 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002478 * Object to Array
2479 *
2480 * Takes an object as input and converts the class variables to array key/vals
2481 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002482 * @param object
2483 * @return array
2484 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002485 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002486 {
2487 if ( ! is_object($object))
2488 {
2489 return $object;
2490 }
Barry Mienydd671972010-10-04 16:33:58 +02002491
Derek Allard2067d1a2008-11-13 22:59:24 +00002492 $array = array();
2493 foreach (get_object_vars($object) as $key => $val)
2494 {
2495 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002496 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002497 {
2498 $array[$key] = $val;
2499 }
2500 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002501
2502 return $array;
2503 }
Barry Mienydd671972010-10-04 16:33:58 +02002504
Derek Jonesd10e8962010-03-02 17:10:36 -06002505 // --------------------------------------------------------------------
2506
2507 /**
2508 * Object to Array
2509 *
2510 * Takes an object as input and converts the class variables to array key/vals
2511 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002512 * @param object
2513 * @return array
2514 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002515 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002516 {
2517 if ( ! is_object($object))
2518 {
2519 return $object;
2520 }
Barry Mienydd671972010-10-04 16:33:58 +02002521
Derek Jonesd10e8962010-03-02 17:10:36 -06002522 $array = array();
2523 $out = get_object_vars($object);
2524 $fields = array_keys($out);
2525
2526 foreach ($fields as $val)
2527 {
2528 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002529 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002530 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002531 $i = 0;
2532 foreach ($out[$val] as $data)
2533 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002534 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002535 }
2536 }
2537 }
2538
Derek Allard2067d1a2008-11-13 22:59:24 +00002539 return $array;
2540 }
Barry Mienydd671972010-10-04 16:33:58 +02002541
Derek Allard2067d1a2008-11-13 22:59:24 +00002542 // --------------------------------------------------------------------
2543
2544 /**
2545 * Start Cache
2546 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002547 * Starts QB caching
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 start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002552 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002553 $this->qb_caching = TRUE;
Andrey Andreev4a587f52014-12-11 16:27:15 +02002554 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002555 }
2556
2557 // --------------------------------------------------------------------
2558
2559 /**
2560 * Stop Cache
2561 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002562 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002563 *
Andrey Andreev4a587f52014-12-11 16:27:15 +02002564 * @return CI_DB_query_builder
Barry Mienydd671972010-10-04 16:33:58 +02002565 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002566 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002567 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002568 $this->qb_caching = FALSE;
Andrey Andreev4a587f52014-12-11 16:27:15 +02002569 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002570 }
2571
2572 // --------------------------------------------------------------------
2573
2574 /**
2575 * Flush Cache
2576 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002577 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002578 *
Andrey Andreev4a587f52014-12-11 16:27:15 +02002579 * @return CI_DB_query_builder
Barry Mienydd671972010-10-04 16:33:58 +02002580 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002581 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002582 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002583 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002584 'qb_cache_select' => array(),
2585 'qb_cache_from' => array(),
2586 'qb_cache_join' => array(),
2587 'qb_cache_where' => array(),
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002588 'qb_cache_groupby' => array(),
2589 'qb_cache_having' => array(),
2590 'qb_cache_orderby' => array(),
2591 'qb_cache_set' => array(),
2592 'qb_cache_exists' => array(),
2593 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002594 ));
Andrey Andreev4a587f52014-12-11 16:27:15 +02002595
2596 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002597 }
2598
2599 // --------------------------------------------------------------------
2600
2601 /**
2602 * Merge Cache
2603 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002604 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002605 * locally called ones.
2606 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002607 * @return void
2608 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002609 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002610 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002611 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002612 {
2613 return;
2614 }
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002615 elseif (in_array('select', $this->qb_cache_exists, TRUE))
2616 {
2617 $qb_no_escape = $this->qb_cache_no_escape;
2618 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002619
GDmac17a05282013-11-11 13:18:09 +01002620 foreach (array_unique($this->qb_cache_exists) as $val) // select, from, etc.
Derek Allard2067d1a2008-11-13 22:59:24 +00002621 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002622 $qb_variable = 'qb_'.$val;
2623 $qb_cache_var = 'qb_cache_'.$val;
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002624 $qb_new = $this->$qb_cache_var;
GDmace1b86832013-11-08 16:52:54 +01002625
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002626 for ($i = 0, $c = count($this->$qb_variable); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00002627 {
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002628 if ( ! in_array($this->{$qb_variable}[$i], $qb_new, TRUE))
2629 {
2630 $qb_new[] = $this->{$qb_variable}[$i];
2631 if ($val === 'select')
2632 {
2633 $qb_no_escape[] = $this->qb_no_escape[$i];
2634 }
2635 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002636 }
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002637
GDmace1b86832013-11-08 16:52:54 +01002638 $this->$qb_variable = $qb_new;
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002639 if ($val === 'select')
2640 {
2641 $this->qb_no_escape = $qb_no_escape;
2642 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002643 }
2644
2645 // If we are "protecting identifiers" we need to examine the "from"
2646 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002647 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002648 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002649 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002650 }
2651 }
WanWizard7219c072011-12-28 14:09:05 +01002652
Kyle Farris0c147b32011-08-26 02:29:31 -04002653 // --------------------------------------------------------------------
2654
2655 /**
Andrey Andreev082aa402012-10-22 19:41:55 +03002656 * Is literal
2657 *
2658 * Determines if a string represents a literal value or a field name
2659 *
Andrey Andreev02e4cd72012-11-13 11:50:47 +02002660 * @param string $str
Andrey Andreev082aa402012-10-22 19:41:55 +03002661 * @return bool
2662 */
2663 protected function _is_literal($str)
2664 {
2665 $str = trim($str);
2666
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002667 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 +03002668 {
2669 return TRUE;
2670 }
2671
2672 static $_str;
2673
2674 if (empty($_str))
2675 {
2676 $_str = ($this->_escape_char !== '"')
2677 ? array('"', "'") : array("'");
2678 }
2679
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002680 return in_array($str[0], $_str, TRUE);
Andrey Andreev082aa402012-10-22 19:41:55 +03002681 }
2682
2683 // --------------------------------------------------------------------
2684
2685 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002686 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002687 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002688 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002689 *
Andrey Andreev435e0c22014-12-11 16:30:13 +02002690 * @return CI_DB_query_builder
Kyle Farris0c147b32011-08-26 02:29:31 -04002691 */
2692 public function reset_query()
2693 {
2694 $this->_reset_select();
2695 $this->_reset_write();
Andrey Andreev435e0c22014-12-11 16:30:13 +02002696 return $this;
Kyle Farris0c147b32011-08-26 02:29:31 -04002697 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002698
2699 // --------------------------------------------------------------------
2700
2701 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002702 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002703 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002704 * @param array An array of fields to reset
2705 * @return void
2706 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002707 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002708 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002709 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002710 {
Andrey Andreevae85eb42012-11-02 01:42:31 +02002711 $this->$item = $default_value;
Derek Allard2067d1a2008-11-13 22:59:24 +00002712 }
2713 }
2714
2715 // --------------------------------------------------------------------
2716
2717 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002718 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002719 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002720 * @return void
2721 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002722 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002723 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002724 $this->_reset_run(array(
Andrey Andreev4a587f52014-12-11 16:27:15 +02002725 'qb_select' => array(),
2726 'qb_from' => array(),
2727 'qb_join' => array(),
2728 'qb_where' => array(),
2729 'qb_groupby' => array(),
2730 'qb_having' => array(),
2731 'qb_orderby' => array(),
2732 'qb_aliased_tables' => array(),
2733 'qb_no_escape' => array(),
2734 'qb_distinct' => FALSE,
2735 'qb_limit' => FALSE,
2736 'qb_offset' => FALSE
2737 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002738 }
Barry Mienydd671972010-10-04 16:33:58 +02002739
Derek Allard2067d1a2008-11-13 22:59:24 +00002740 // --------------------------------------------------------------------
2741
2742 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002743 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002744 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002745 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002746 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002747 * @return void
2748 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002749 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002750 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002751 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002752 'qb_set' => array(),
2753 'qb_from' => array(),
Andrey Andreev3e014372013-02-21 15:59:34 +02002754 'qb_join' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002755 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002756 'qb_orderby' => array(),
2757 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002758 'qb_limit' => FALSE
Andrey Andreev4a587f52014-12-11 16:27:15 +02002759 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002760 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002761
Derek Allard2067d1a2008-11-13 22:59:24 +00002762}