blob: c862d937de2c1905f94f94d0809adb9f25c00cd8 [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 Andreevc6011942016-02-11 23:49:37 +0200534 if ( ! $this->_has_operator($cond))
Andrey Andreev3751f932012-06-17 18:07:48 +0300535 {
536 $cond = ' USING ('.($escape ? $this->escape_identifiers($cond) : $cond).')';
537 }
Andrey Andreevc6011942016-02-11 23:49:37 +0200538 elseif ($escape === FALSE)
Andrey Andreev3751f932012-06-17 18:07:48 +0300539 {
540 $cond = ' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 }
Andrey Andreevc6011942016-02-11 23:49:37 +0200542 else
543 {
544 // Split multiple conditions
545 if (preg_match_all('/\sAND\s|\sOR\s/i', $cond, $joints, PREG_OFFSET_CAPTURE))
546 {
547 $conditions = array();
548 $joints = $joints[0];
549 array_unshift($joints, array('', 0));
550
551 for ($i = count($joints) - 1, $pos = strlen($cond); $i >= 0; $i--)
552 {
553 $joints[$i][1] += strlen($joints[$i][0]); // offset
554 $conditions[$i] = substr($cond, $joints[$i][1], $pos - $joints[$i][1]);
555 $pos = $joints[$i][1] - strlen($joints[$i][0]);
556 $joints[$i] = $joints[$i][0];
557 }
558 }
559 else
560 {
561 $conditions = array($cond);
562 $joints = array('');
563 }
564
565 $cond = ' ON ';
566 for ($i = 0, $c = count($conditions); $i < $c; $i++)
567 {
568 $operator = $this->_get_operator($conditions[$i]);
569 $cond .= $joints[$i];
570 $cond .= preg_match("/(\(*)?([\[\]\w\.'-]+)".preg_quote($operator)."(.*)/i", $conditions[$i], $match)
571 ? $match[1].$this->protect_identifiers($match[2]).$operator.$this->protect_identifiers($match[3])
572 : $conditions[$i];
573 }
574 }
Barry Mienydd671972010-10-04 16:33:58 +0200575
Andrey Andreev42870232012-06-12 01:30:20 +0300576 // Do we want to escape the table name?
577 if ($escape === TRUE)
578 {
579 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
580 }
581
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 // Assemble the JOIN statement
Andrey Andreev3751f932012-06-17 18:07:48 +0300583 $this->qb_join[] = $join = $type.'JOIN '.$table.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000584
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000585 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000587 $this->qb_cache_join[] = $join;
588 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 }
590
591 return $this;
592 }
593
594 // --------------------------------------------------------------------
595
596 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200597 * WHERE
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200599 * Generates the WHERE portion of the query.
600 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 * @param mixed
603 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300604 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500605 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300607 public function where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300609 return $this->_wh('qb_where', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 }
Barry Mienydd671972010-10-04 16:33:58 +0200611
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 // --------------------------------------------------------------------
613
614 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200615 * OR WHERE
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200617 * Generates the WHERE portion of the query.
618 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 * @param mixed
621 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300622 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500623 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300625 public function or_where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300627 return $this->_wh('qb_where', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 }
629
630 // --------------------------------------------------------------------
631
632 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +0300633 * WHERE, HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200635 * @used-by where()
636 * @used-by or_where()
637 * @used-by having()
638 * @used-by or_having()
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200640 * @param string $qb_key 'qb_where' or 'qb_having'
641 * @param mixed $key
642 * @param mixed $value
643 * @param string $type
644 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500645 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 */
Andrey Andreevd40459d2012-07-18 16:46:39 +0300647 protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300649 $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where';
650
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 if ( ! is_array($key))
652 {
653 $key = array($key => $value);
654 }
Barry Mienydd671972010-10-04 16:33:58 +0200655
Rougin Royce Gutib191550a2014-08-24 16:19:08 +0800656 // If the escape value was not set will base it on the global setting
Andrey Andreeve10fb792012-06-15 12:07:04 +0300657 is_bool($escape) OR $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000658
659 foreach ($key as $k => $v)
660 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300661 $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0)
Andrey Andreev58803fb2012-06-24 00:45:37 +0300662 ? $this->_group_get_type('')
663 : $this->_group_get_type($type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000664
vlakoff1228fe22013-01-14 01:30:09 +0100665 if ($v !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 {
Andrey Andreevf7631f12015-07-27 21:54:41 +0300667 if ($escape === TRUE)
668 {
669 $v = ' '.$this->escape($v);
670 }
WanWizard7219c072011-12-28 14:09:05 +0100671
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 if ( ! $this->_has_operator($k))
673 {
Greg Akere156c6e2011-04-20 16:03:04 -0500674 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 }
676 }
Andrey Andreev3a5efc22012-11-20 21:18:08 +0200677 elseif ( ! $this->_has_operator($k))
678 {
679 // value appears not to have been set, assign the test to IS NULL
680 $k .= ' IS NULL';
681 }
Andrey Andreev5078eb52014-12-02 01:11:54 +0200682 elseif (preg_match('/\s*(!?=|<>|IS(?:\s+NOT)?)\s*$/i', $k, $match, PREG_OFFSET_CAPTURE))
Andrey Andreev5bf4dcd2014-09-29 20:07:15 +0300683 {
684 $k = substr($k, 0, $match[0][1]).($match[1][0] === '=' ? ' IS NULL' : ' IS NOT NULL');
685 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000686
Andrey Andreevd40459d2012-07-18 16:46:39 +0300687 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000688 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300690 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
691 $this->qb_cache_exists[] = substr($qb_key, 3);
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 }
Barry Mienydd671972010-10-04 16:33:58 +0200693
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 }
Barry Mienydd671972010-10-04 16:33:58 +0200695
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 return $this;
697 }
698
699 // --------------------------------------------------------------------
700
701 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200702 * WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200704 * Generates a WHERE field IN('item', 'item') SQL query,
705 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200707 * @param string $key The field to search
708 * @param array $values The values searched on
709 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500710 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300712 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300714 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 }
Barry Mienydd671972010-10-04 16:33:58 +0200716
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 // --------------------------------------------------------------------
718
719 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200720 * OR WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200722 * Generates a WHERE field IN('item', 'item') SQL query,
723 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200725 * @param string $key The field to search
726 * @param array $values The values searched on
727 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500728 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300730 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300732 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 }
734
735 // --------------------------------------------------------------------
736
737 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200738 * WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200740 * Generates a WHERE field NOT IN('item', 'item') SQL query,
741 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200743 * @param string $key The field to search
744 * @param array $values The values searched on
745 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500746 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300748 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300750 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 }
Barry Mienydd671972010-10-04 16:33:58 +0200752
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 // --------------------------------------------------------------------
754
755 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200756 * OR WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200758 * Generates a WHERE field NOT IN('item', 'item') SQL query,
759 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200761 * @param string $key The field to search
762 * @param array $values The values searched on
763 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500764 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300766 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300768 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 }
770
771 // --------------------------------------------------------------------
772
773 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200774 * Internal WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200776 * @used-by where_in()
777 * @used-by or_where_in()
778 * @used-by where_not_in()
779 * @used-by or_where_not_in()
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200781 * @param string $key The field to search
782 * @param array $values The values searched on
783 * @param bool $not If the statement would be IN or NOT IN
784 * @param string $type
785 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500786 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300788 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 {
790 if ($key === NULL OR $values === NULL)
791 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300792 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 }
Barry Mienydd671972010-10-04 16:33:58 +0200794
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 if ( ! is_array($values))
796 {
797 $values = array($values);
798 }
Barry Mienydd671972010-10-04 16:33:58 +0200799
Andrey Andreev498c1e02012-06-16 03:34:10 +0300800 is_bool($escape) OR $escape = $this->_protect_identifiers;
801
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 $not = ($not) ? ' NOT' : '';
803
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300804 if ($escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 {
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300806 $where_in = array();
807 foreach ($values as $value)
808 {
809 $where_in[] = $this->escape($value);
810 }
811 }
812 else
813 {
814 $where_in = array_values($values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 }
816
Andrey Andreev0d2d84f2015-07-31 13:48:59 +0300817 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
818 ? $this->_group_get_type('')
819 : $this->_group_get_type($type);
820
Andrey Andreev6e704752012-07-18 00:46:33 +0300821 $where_in = array(
Andrey Andreev94611df2012-07-19 12:29:54 +0300822 'condition' => $prefix.$key.$not.' IN('.implode(', ', $where_in).')',
Andrey Andreev6e704752012-07-18 00:46:33 +0300823 'escape' => $escape
824 );
Barry Mienydd671972010-10-04 16:33:58 +0200825
Andrey Andreev6e704752012-07-18 00:46:33 +0300826 $this->qb_where[] = $where_in;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000827 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000829 $this->qb_cache_where[] = $where_in;
830 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 }
832
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 return $this;
834 }
Barry Mienydd671972010-10-04 16:33:58 +0200835
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 // --------------------------------------------------------------------
837
838 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200839 * LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200841 * Generates a %LIKE% portion of the query.
842 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200844 * @param mixed $field
845 * @param string $match
846 * @param string $side
847 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500848 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300850 public function like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300852 return $this->_like($field, $match, 'AND ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 }
854
855 // --------------------------------------------------------------------
856
857 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200858 * NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200860 * Generates a NOT LIKE portion of the query.
861 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200863 * @param mixed $field
864 * @param string $match
865 * @param string $side
866 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500867 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300869 public function not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300871 return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 }
Barry Mienydd671972010-10-04 16:33:58 +0200873
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 // --------------------------------------------------------------------
875
876 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200877 * OR LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200879 * Generates a %LIKE% portion of the query.
880 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200882 * @param mixed $field
883 * @param string $match
884 * @param string $side
885 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500886 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300888 public function or_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300890 return $this->_like($field, $match, 'OR ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 }
892
893 // --------------------------------------------------------------------
894
895 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200896 * OR NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200898 * Generates a NOT LIKE portion of the query.
899 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200901 * @param mixed $field
902 * @param string $match
903 * @param string $side
904 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500905 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300907 public function or_not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300909 return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 }
Barry Mienydd671972010-10-04 16:33:58 +0200911
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 // --------------------------------------------------------------------
913
914 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200915 * Internal LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200917 * @used-by like()
918 * @used-by or_like()
919 * @used-by not_like()
920 * @used-by or_not_like()
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200922 * @param mixed $field
923 * @param string $match
924 * @param string $type
925 * @param string $side
926 * @param string $not
927 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500928 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300930 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 {
932 if ( ! is_array($field))
933 {
934 $field = array($field => $match);
935 }
Barry Mienydd671972010-10-04 16:33:58 +0200936
Andrey Andreevb0478652012-07-18 15:34:46 +0300937 is_bool($escape) OR $escape = $this->_protect_identifiers;
Andrey Andreev19311362015-04-07 00:02:14 +0300938 // lowercase $side in case somebody writes e.g. 'BEFORE' instead of 'before' (doh)
939 $side = strtolower($side);
Andrey Andreevb0478652012-07-18 15:34:46 +0300940
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 foreach ($field as $k => $v)
942 {
Andrey Andreev41738232012-11-30 00:13:17 +0200943 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
944 ? $this->_group_get_type('') : $this->_group_get_type($type);
945
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300946 if ($escape === TRUE)
947 {
948 $v = $this->escape_like_str($v);
949 }
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300950
Andrey Andreev24276a32012-01-08 02:44:38 +0200951 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400952 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300953 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400954 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200955 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300957 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200959 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300961 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 }
963 else
964 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300965 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600967
Derek Jonese4ed5832009-02-20 21:44:59 +0000968 // some platforms require an escape sequence definition for LIKE wildcards
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300969 if ($escape === TRUE && $this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000970 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300971 $like_statement .= sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000972 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600973
Andrey Andreevb0478652012-07-18 15:34:46 +0300974 $this->qb_where[] = array('condition' => $like_statement, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000975 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 {
Andrey Andreev55bbd722013-01-28 19:02:13 +0200977 $this->qb_cache_where[] = array('condition' => $like_statement, 'escape' => $escape);
Andrey Andreevededc4a2012-07-18 01:16:15 +0300978 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200981
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 return $this;
983 }
Barry Mienydd671972010-10-04 16:33:58 +0200984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 // --------------------------------------------------------------------
986
987 /**
WanWizard7219c072011-12-28 14:09:05 +0100988 * Starts a query group.
989 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200990 * @param string $not (Internal use only)
991 * @param string $type (Internal use only)
Andrew Podner4296a652012-12-17 07:51:15 -0500992 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +0100993 */
994 public function group_start($not = '', $type = 'AND ')
995 {
996 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100997
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000998 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100999 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Andrey Andreev6e704752012-07-18 00:46:33 +03001000 $where = array(
1001 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (',
1002 'escape' => FALSE
1003 );
WanWizard7219c072011-12-28 14:09:05 +01001004
Andrey Andreev6e704752012-07-18 00:46:33 +03001005 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001006 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +01001007 {
Andrey Andreev6e704752012-07-18 00:46:33 +03001008 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +01001009 }
1010
1011 return $this;
1012 }
1013
1014 // --------------------------------------------------------------------
1015
1016 /**
1017 * Starts a query group, but ORs the group
1018 *
Andrew Podner4296a652012-12-17 07:51:15 -05001019 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001020 */
1021 public function or_group_start()
1022 {
1023 return $this->group_start('', 'OR ');
1024 }
1025
1026 // --------------------------------------------------------------------
1027
1028 /**
1029 * Starts a query group, but NOTs the group
1030 *
Andrew Podner4296a652012-12-17 07:51:15 -05001031 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001032 */
1033 public function not_group_start()
1034 {
1035 return $this->group_start('NOT ', 'AND ');
1036 }
1037
1038 // --------------------------------------------------------------------
1039
1040 /**
1041 * Starts a query group, but OR NOTs the group
1042 *
Andrew Podner4296a652012-12-17 07:51:15 -05001043 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001044 */
1045 public function or_not_group_start()
1046 {
1047 return $this->group_start('NOT ', 'OR ');
1048 }
1049
1050 // --------------------------------------------------------------------
1051
1052 /**
1053 * Ends a query group
1054 *
Andrew Podner4296a652012-12-17 07:51:15 -05001055 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001056 */
1057 public function group_end()
1058 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001059 $this->qb_where_group_started = FALSE;
Andrey Andreev6e704752012-07-18 00:46:33 +03001060 $where = array(
1061 'condition' => str_repeat(' ', $this->qb_where_group_count--).')',
1062 'escape' => FALSE
1063 );
WanWizard7219c072011-12-28 14:09:05 +01001064
Andrey Andreev6e704752012-07-18 00:46:33 +03001065 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001066 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +01001067 {
Andrey Andreev6e704752012-07-18 00:46:33 +03001068 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +01001069 }
1070
WanWizard7219c072011-12-28 14:09:05 +01001071 return $this;
1072 }
1073
1074 // --------------------------------------------------------------------
1075
1076 /**
1077 * Group_get_type
1078 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001079 * @used-by group_start()
1080 * @used-by _like()
1081 * @used-by _wh()
1082 * @used-by _where_in()
WanWizard7219c072011-12-28 14:09:05 +01001083 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001084 * @param string $type
WanWizard7219c072011-12-28 14:09:05 +01001085 * @return string
1086 */
1087 protected function _group_get_type($type)
1088 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001089 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +01001090 {
1091 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001092 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +01001093 }
1094
1095 return $type;
1096 }
1097
1098 // --------------------------------------------------------------------
1099
1100 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001101 * GROUP BY
1102 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001103 * @param string $by
1104 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -05001105 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001107 public function group_by($by, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001109 is_bool($escape) OR $escape = $this->_protect_identifiers;
1110
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 if (is_string($by))
1112 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001113 $by = ($escape === TRUE)
1114 ? explode(',', $by)
1115 : array($by);
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 }
Barry Mienydd671972010-10-04 16:33:58 +02001117
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 foreach ($by as $val)
1119 {
1120 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +02001121
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001122 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001124 $val = array('field' => $val, 'escape' => $escape);
Barry Mienydd671972010-10-04 16:33:58 +02001125
Andrey Andreev96feb582012-07-19 13:12:34 +03001126 $this->qb_groupby[] = $val;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001127 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001129 $this->qb_cache_groupby[] = $val;
1130 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 }
1132 }
1133 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001134
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 return $this;
1136 }
1137
1138 // --------------------------------------------------------------------
1139
1140 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001141 * HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001143 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001145 * @param string $key
1146 * @param string $value
1147 * @param bool $escape
Andrey Andreevaec51262016-02-09 21:11:07 +02001148 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001150 public function having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001152 return $this->_wh('qb_having', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 }
Barry Mienydd671972010-10-04 16:33:58 +02001154
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 // --------------------------------------------------------------------
1156
1157 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001158 * OR HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001160 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001162 * @param string $key
1163 * @param string $value
1164 * @param bool $escape
Andrey Andreevaec51262016-02-09 21:11:07 +02001165 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001167 public function or_having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001169 return $this->_wh('qb_having', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 }
Barry Mienydd671972010-10-04 16:33:58 +02001171
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 // --------------------------------------------------------------------
1173
1174 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001175 * ORDER BY
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001177 * @param string $orderby
vlakoff9f8e2992013-09-08 14:05:04 +02001178 * @param string $direction ASC, DESC or RANDOM
Andrey Andreevae85eb42012-11-02 01:42:31 +02001179 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -05001180 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 */
Andrey Andreevd24160c2012-06-16 03:21:20 +03001182 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001184 $direction = strtoupper(trim($direction));
Andrey Andreev2d486232012-07-19 14:46:51 +03001185
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001186 if ($direction === 'RANDOM')
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001188 $direction = '';
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001189
1190 // Do we have a seed value?
1191 $orderby = ctype_digit((string) $orderby)
vlakoffe6c4d5b2013-09-08 13:54:57 +02001192 ? sprintf($this->_random_keyword[1], $orderby)
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001193 : $this->_random_keyword[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 }
Andrey Andreev2d486232012-07-19 14:46:51 +03001195 elseif (empty($orderby))
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001197 return $this;
1198 }
1199 elseif ($direction !== '')
1200 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001201 $direction = in_array($direction, array('ASC', 'DESC'), TRUE) ? ' '.$direction : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 }
Barry Mienydd671972010-10-04 16:33:58 +02001203
Andrey Andreevd24160c2012-06-16 03:21:20 +03001204 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +02001205
Andrey Andreev2d486232012-07-19 14:46:51 +03001206 if ($escape === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 {
Andrey Andreev93dd2f22012-10-24 10:09:18 +03001208 $qb_orderby[] = array('field' => $orderby, 'direction' => $direction, 'escape' => FALSE);
Andrey Andreev2d486232012-07-19 14:46:51 +03001209 }
1210 else
1211 {
1212 $qb_orderby = array();
1213 foreach (explode(',', $orderby) as $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001215 $qb_orderby[] = ($direction === '' && preg_match('/\s+(ASC|DESC)$/i', rtrim($field), $match, PREG_OFFSET_CAPTURE))
1216 ? array('field' => ltrim(substr($field, 0, $match[0][1])), 'direction' => ' '.$match[1][0], 'escape' => TRUE)
1217 : array('field' => trim($field), 'direction' => $direction, 'escape' => TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001218 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 }
Barry Mienydd671972010-10-04 16:33:58 +02001220
Andrey Andreev2d486232012-07-19 14:46:51 +03001221 $this->qb_orderby = array_merge($this->qb_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001222 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001224 $this->qb_cache_orderby = array_merge($this->qb_cache_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001225 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 }
1227
1228 return $this;
1229 }
Barry Mienydd671972010-10-04 16:33:58 +02001230
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 // --------------------------------------------------------------------
1232
1233 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001234 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +00001235 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001236 * @param int $value LIMIT value
1237 * @param int $offset OFFSET value
Andrew Podner4296a652012-12-17 07:51:15 -05001238 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001239 */
Andrey Andreev4a587f52014-12-11 16:27:15 +02001240 public function limit($value, $offset = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001241 {
vlakoff912f1bc2013-01-15 03:34:12 +01001242 is_null($value) OR $this->qb_limit = (int) $value;
Andrey Andreev777153d2012-06-18 13:30:45 +03001243 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001244
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 return $this;
1246 }
Barry Mienydd671972010-10-04 16:33:58 +02001247
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 // --------------------------------------------------------------------
1249
1250 /**
1251 * Sets the OFFSET value
1252 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001253 * @param int $offset OFFSET value
Andrew Podner4296a652012-12-17 07:51:15 -05001254 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001256 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001258 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 return $this;
1260 }
Barry Mienydd671972010-10-04 16:33:58 +02001261
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 // --------------------------------------------------------------------
1263
1264 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001265 * LIMIT string
Andrey Andreev2c35b642012-06-24 03:05:26 +03001266 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001267 * Generates a platform-specific LIMIT clause.
Andrey Andreev2c35b642012-06-24 03:05:26 +03001268 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001269 * @param string $sql SQL Query
Andrey Andreev2c35b642012-06-24 03:05:26 +03001270 * @return string
1271 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001272 protected function _limit($sql)
Andrey Andreev2c35b642012-06-24 03:05:26 +03001273 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001274 return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').$this->qb_limit;
Andrey Andreev2c35b642012-06-24 03:05:26 +03001275 }
1276
1277 // --------------------------------------------------------------------
1278
1279 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001280 * The "set" function.
1281 *
1282 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001283 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 * @param mixed
1285 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001286 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001287 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001289 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 {
1291 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001292
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 if ( ! is_array($key))
1294 {
1295 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001296 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001297
Andrey Andreevfe642da2012-06-16 03:47:33 +03001298 is_bool($escape) OR $escape = $this->_protect_identifiers;
1299
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 foreach ($key as $k => $v)
1301 {
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001302 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1303 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 }
Barry Mienydd671972010-10-04 16:33:58 +02001305
Derek Allard2067d1a2008-11-13 22:59:24 +00001306 return $this;
1307 }
WanWizard7219c072011-12-28 14:09:05 +01001308
Kyle Farris0c147b32011-08-26 02:29:31 -04001309 // --------------------------------------------------------------------
1310
1311 /**
1312 * Get SELECT query string
1313 *
1314 * Compiles a SELECT query string and returns the sql.
1315 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001316 * @param string the table name to select from (optional)
Calvin Tam55bc5052015-07-24 02:27:24 -07001317 * @param bool TRUE: resets QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001318 * @return string
1319 */
WanWizard7219c072011-12-28 14:09:05 +01001320 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001321 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001322 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001323 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001324 $this->_track_aliases($table);
1325 $this->from($table);
1326 }
WanWizard7219c072011-12-28 14:09:05 +01001327
Andrey Andreev650b4c02012-06-11 12:07:15 +03001328 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001329
Kyle Farris0c147b32011-08-26 02:29:31 -04001330 if ($reset === TRUE)
1331 {
1332 $this->_reset_select();
1333 }
WanWizard7219c072011-12-28 14:09:05 +01001334
Kyle Farris0c147b32011-08-26 02:29:31 -04001335 return $select;
1336 }
WanWizard7219c072011-12-28 14:09:05 +01001337
Derek Allard2067d1a2008-11-13 22:59:24 +00001338 // --------------------------------------------------------------------
1339
1340 /**
1341 * Get
1342 *
1343 * Compiles the select statement based on the other functions called
1344 * and runs the query
1345 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 * @param string the table
1347 * @param string the limit clause
1348 * @param string the offset clause
Andrey Andreevaec51262016-02-09 21:11:07 +02001349 * @return CI_DB_result
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001351 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001352 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001353 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 {
1355 $this->_track_aliases($table);
1356 $this->from($table);
1357 }
Barry Mienydd671972010-10-04 16:33:58 +02001358
Andrey Andreev650b4c02012-06-11 12:07:15 +03001359 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001360 {
1361 $this->limit($limit, $offset);
1362 }
Barry Mienydd671972010-10-04 16:33:58 +02001363
Andrey Andreev24276a32012-01-08 02:44:38 +02001364 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 $this->_reset_select();
1366 return $result;
1367 }
1368
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001369 // --------------------------------------------------------------------
1370
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 /**
1372 * "Count All Results" query
1373 *
Barry Mienydd671972010-10-04 16:33:58 +02001374 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001375 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001377 * @param string
yaoshanliang2f164052015-03-16 16:48:15 +08001378 * @param bool the reset clause
vlakoffc6ac7482013-11-17 00:50:06 +01001379 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +00001380 */
yaoshanliang19c28472015-03-15 10:42:18 +08001381 public function count_all_results($table = '', $reset = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001382 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001383 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 {
1385 $this->_track_aliases($table);
1386 $this->from($table);
1387 }
Barry Mienydd671972010-10-04 16:33:58 +02001388
Andrey Andreev075bdb42016-01-25 13:31:23 +02001389 // ORDER BY usage is often problematic here (most notably
1390 // on Microsoft SQL Server) and ultimately unnecessary
1391 // for selecting COUNT(*) ...
1392 if ( ! empty($this->qb_orderby))
1393 {
1394 $orderby = $this->qb_orderby;
1395 $this->qb_orderby = NULL;
1396 }
1397
1398 $result = ($this->qb_distinct === TRUE)
Andrey Andreevb05f5062012-10-26 12:01:02 +03001399 ? $this->query($this->_count_string.$this->protect_identifiers('numrows')."\nFROM (\n".$this->_compile_select()."\n) CI_count_all_results")
1400 : $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
yaoshanliang9971e7b2015-03-14 13:09:16 +08001401
yaoshanliang2f164052015-03-16 16:48:15 +08001402 if ($reset === TRUE)
yaoshanliang19c28472015-03-15 10:42:18 +08001403 {
yaoshanliang2f164052015-03-16 16:48:15 +08001404 $this->_reset_select();
yaoshanliang19c28472015-03-15 10:42:18 +08001405 }
Andrey Andreev075bdb42016-01-25 13:31:23 +02001406 // If we've previously reset the qb_orderby values, get them back
1407 elseif ( ! isset($this->qb_orderby))
1408 {
1409 $this->qb_orderby = $orderby;
1410 }
Barry Mienydd671972010-10-04 16:33:58 +02001411
Purwandi1d160e72012-01-09 16:33:28 +07001412 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001414 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001415 }
1416
Purwandi1d160e72012-01-09 16:33:28 +07001417 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001418 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001419 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001420
Derek Allard2067d1a2008-11-13 22:59:24 +00001421 // --------------------------------------------------------------------
1422
1423 /**
1424 * Get_Where
1425 *
1426 * Allows the where clause, limit and offset to be added directly
1427 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001428 * @param string $table
1429 * @param string $where
1430 * @param int $limit
1431 * @param int $offset
Andrey Andreevaec51262016-02-09 21:11:07 +02001432 * @return CI_DB_result
Derek Allard2067d1a2008-11-13 22:59:24 +00001433 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001434 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001435 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001436 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001437 {
1438 $this->from($table);
1439 }
1440
vlakoff1228fe22013-01-14 01:30:09 +01001441 if ($where !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001442 {
1443 $this->where($where);
1444 }
Barry Mienydd671972010-10-04 16:33:58 +02001445
Andrey Andreev650b4c02012-06-11 12:07:15 +03001446 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001447 {
1448 $this->limit($limit, $offset);
1449 }
Barry Mienydd671972010-10-04 16:33:58 +02001450
Andrey Andreev24276a32012-01-08 02:44:38 +02001451 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001452 $this->_reset_select();
1453 return $result;
1454 }
1455
1456 // --------------------------------------------------------------------
1457
1458 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001459 * Insert_Batch
1460 *
1461 * Compiles batch insert strings and runs the queries
1462 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001463 * @param string $table Table to insert into
1464 * @param array $set An associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001465 * @param bool $escape Whether to escape values and identifiers
Andrey Andreevae85eb42012-11-02 01:42:31 +02001466 * @return int Number of rows inserted or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001467 */
Andrey Andreev105a48b2016-02-04 15:45:10 +02001468 public function insert_batch($table, $set = NULL, $escape = NULL, $batch_size = 100)
Barry Mienydd671972010-10-04 16:33:58 +02001469 {
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001470 if ($set === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001471 {
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001472 if (empty($this->qb_set))
1473 {
1474 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
1475 }
1476 }
1477 else
1478 {
1479 if (empty($set))
1480 {
1481 return ($this->db_debug) ? $this->display_error('insert_batch() called with no data') : FALSE;
1482 }
1483
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001484 $this->set_insert_batch($set, '', $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001485 }
Barry Mienydd671972010-10-04 16:33:58 +02001486
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001487 if (strlen($table) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001488 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001489 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001490 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001491 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001492 }
Barry Mienydd671972010-10-04 16:33:58 +02001493
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001494 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001495 }
1496
1497 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001498 $affected_rows = 0;
Andrey Andreev105a48b2016-02-04 15:45:10 +02001499 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += $batch_size)
Derek Jonesd10e8962010-03-02 17:10:36 -06001500 {
Andrey Andreev105a48b2016-02-04 15:45:10 +02001501 $this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, $escape, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, $batch_size)));
Andrey Andreev9f808b02012-10-24 17:38:48 +03001502 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001503 }
Barry Mienydd671972010-10-04 16:33:58 +02001504
Derek Jonesd10e8962010-03-02 17:10:36 -06001505 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001506 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001507 }
1508
1509 // --------------------------------------------------------------------
1510
1511 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +02001512 * Insert batch statement
Andrey Andreev97f36972012-04-05 12:44:36 +03001513 *
1514 * Generates a platform-specific insert string from the supplied data.
1515 *
Andrey Andreev083e3c82012-11-06 12:48:32 +02001516 * @param string $table Table name
1517 * @param array $keys INSERT keys
1518 * @param array $values INSERT values
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001519 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001520 */
1521 protected function _insert_batch($table, $keys, $values)
1522 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001523 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001524 }
1525
1526 // --------------------------------------------------------------------
1527
1528 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001529 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001530 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001531 * @param mixed
1532 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001533 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001534 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001535 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001536 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001537 {
1538 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001539
Derek Jonesd10e8962010-03-02 17:10:36 -06001540 if ( ! is_array($key))
1541 {
1542 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001543 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001544
Andrey Andreevfe642da2012-06-16 03:47:33 +03001545 is_bool($escape) OR $escape = $this->_protect_identifiers;
1546
Iban Eguia3c0a4522012-04-15 13:30:44 +02001547 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001548 sort($keys);
1549
1550 foreach ($key as $row)
1551 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001552 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001553 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1554 {
1555 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001556 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001557 return;
1558 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001559
Barry Mienydd671972010-10-04 16:33:58 +02001560 ksort($row); // puts $row in the same order as our keys
1561
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001562 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001563 {
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001564 $clean = array();
1565 foreach ($row as $value)
1566 {
1567 $clean[] = $this->escape($value);
1568 }
1569
1570 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001571 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001572
Andrey Andreev838a9d62012-12-03 14:37:47 +02001573 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001574 }
1575
1576 foreach ($keys as $k)
1577 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001578 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001579 }
Barry Mienydd671972010-10-04 16:33:58 +02001580
Derek Jonesd10e8962010-03-02 17:10:36 -06001581 return $this;
1582 }
WanWizard7219c072011-12-28 14:09:05 +01001583
Kyle Farris0c147b32011-08-26 02:29:31 -04001584 // --------------------------------------------------------------------
1585
1586 /**
1587 * Get INSERT query string
1588 *
1589 * Compiles an insert query and returns the sql
1590 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001591 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001592 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001593 * @return string
1594 */
1595 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001596 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001597 if ($this->_validate_insert($table) === FALSE)
1598 {
1599 return FALSE;
1600 }
WanWizard7219c072011-12-28 14:09:05 +01001601
Kyle Farris76116012011-08-31 11:17:48 -04001602 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001603 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001604 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001605 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001606 array_keys($this->qb_set),
1607 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001608 );
WanWizard7219c072011-12-28 14:09:05 +01001609
Kyle Farris0c147b32011-08-26 02:29:31 -04001610 if ($reset === TRUE)
1611 {
1612 $this->_reset_write();
1613 }
WanWizard7219c072011-12-28 14:09:05 +01001614
Kyle Farris0c147b32011-08-26 02:29:31 -04001615 return $sql;
1616 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001617
Derek Allard2067d1a2008-11-13 22:59:24 +00001618 // --------------------------------------------------------------------
1619
1620 /**
1621 * Insert
1622 *
1623 * Compiles an insert string and runs the query
1624 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001625 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001626 * @param array an associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001627 * @param bool $escape Whether to escape values and identifiers
Andrey Andreevaec51262016-02-09 21:11:07 +02001628 * @return bool TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +00001629 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001630 public function insert($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001631 {
vlakoff1228fe22013-01-14 01:30:09 +01001632 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001633 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001634 $this->set($set, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001635 }
WanWizard7219c072011-12-28 14:09:05 +01001636
Kyle Farris0c147b32011-08-26 02:29:31 -04001637 if ($this->_validate_insert($table) === FALSE)
1638 {
1639 return FALSE;
1640 }
WanWizard7219c072011-12-28 14:09:05 +01001641
Kyle Farris76116012011-08-31 11:17:48 -04001642 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001643 $this->protect_identifiers(
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001644 $this->qb_from[0], TRUE, $escape, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001645 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001646 array_keys($this->qb_set),
1647 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001648 );
Barry Mienydd671972010-10-04 16:33:58 +02001649
Kyle Farris0c147b32011-08-26 02:29:31 -04001650 $this->_reset_write();
1651 return $this->query($sql);
1652 }
WanWizard7219c072011-12-28 14:09:05 +01001653
Kyle Farris0c147b32011-08-26 02:29:31 -04001654 // --------------------------------------------------------------------
1655
1656 /**
1657 * Validate Insert
1658 *
1659 * This method is used by both insert() and get_compiled_insert() to
1660 * validate that the there data is actually being set and that table
1661 * has been chosen to be inserted into.
1662 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001663 * @param string the table to insert data into
1664 * @return string
1665 */
WanWizard7219c072011-12-28 14:09:05 +01001666 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001667 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001668 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001669 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001670 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001671 }
1672
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001673 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001674 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001675 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001676 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001677 elseif ( ! isset($this->qb_from[0]))
1678 {
1679 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1680 }
WanWizard7219c072011-12-28 14:09:05 +01001681
Kyle Farris0c147b32011-08-26 02:29:31 -04001682 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001683 }
Barry Mienydd671972010-10-04 16:33:58 +02001684
Phil Sturgeon9789f322011-07-15 15:14:05 -06001685 // --------------------------------------------------------------------
1686
1687 /**
1688 * Replace
1689 *
1690 * Compiles an replace into string and runs the query
1691 *
1692 * @param string the table to replace data into
1693 * @param array an associative array of insert values
Andrey Andreevaec51262016-02-09 21:11:07 +02001694 * @return bool TRUE on success, FALSE on failure
Phil Sturgeon9789f322011-07-15 15:14:05 -06001695 */
1696 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001697 {
vlakoff1228fe22013-01-14 01:30:09 +01001698 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001699 {
1700 $this->set($set);
1701 }
Barry Mienydd671972010-10-04 16:33:58 +02001702
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001703 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001704 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001705 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001706 }
1707
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001708 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001709 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001710 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001711 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001712 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001713 }
Barry Mienydd671972010-10-04 16:33:58 +02001714
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001715 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001716 }
1717
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001718 $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 +00001719
Derek Jonesd10e8962010-03-02 17:10:36 -06001720 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001721 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001722 }
WanWizard7219c072011-12-28 14:09:05 +01001723
Kyle Farris0c147b32011-08-26 02:29:31 -04001724 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001725
Kyle Farris0c147b32011-08-26 02:29:31 -04001726 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001727 * Replace statement
1728 *
1729 * Generates a platform-specific replace string from the supplied data
1730 *
1731 * @param string the table name
1732 * @param array the insert keys
1733 * @param array the insert values
1734 * @return string
1735 */
1736 protected function _replace($table, $keys, $values)
1737 {
1738 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1739 }
1740
1741 // --------------------------------------------------------------------
1742
1743 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001744 * FROM tables
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001745 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001746 * Groups tables in FROM clauses if needed, so there is no confusion
1747 * about operator precedence.
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001748 *
Claudio Galdiolo93e78132015-01-29 11:43:56 -05001749 * Note: This is only used (and overridden) by MySQL and CUBRID.
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001750 *
1751 * @return string
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001752 */
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001753 protected function _from_tables()
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001754 {
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001755 return implode(', ', $this->qb_from);
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001756 }
1757
1758 // --------------------------------------------------------------------
1759
1760 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001761 * Get UPDATE query string
1762 *
1763 * Compiles an update query and returns the sql
1764 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001765 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001766 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001767 * @return string
1768 */
1769 public function get_compiled_update($table = '', $reset = TRUE)
1770 {
1771 // Combine any cached components with the current statements
1772 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001773
Kyle Farris0c147b32011-08-26 02:29:31 -04001774 if ($this->_validate_update($table) === FALSE)
1775 {
1776 return FALSE;
1777 }
WanWizard7219c072011-12-28 14:09:05 +01001778
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001779 $sql = $this->_update($this->qb_from[0], $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001780
Kyle Farris0c147b32011-08-26 02:29:31 -04001781 if ($reset === TRUE)
1782 {
1783 $this->_reset_write();
1784 }
WanWizard7219c072011-12-28 14:09:05 +01001785
Kyle Farris0c147b32011-08-26 02:29:31 -04001786 return $sql;
1787 }
WanWizard7219c072011-12-28 14:09:05 +01001788
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 // --------------------------------------------------------------------
1790
1791 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001792 * UPDATE
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001794 * Compiles an update string and runs the query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001795 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001796 * @param string $table
1797 * @param array $set An associative array of update values
1798 * @param mixed $where
1799 * @param int $limit
Andrey Andreevaec51262016-02-09 21:11:07 +02001800 * @return bool TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +00001801 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001802 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001803 {
1804 // Combine any cached components with the current statements
1805 $this->_merge_cache();
1806
vlakoff1228fe22013-01-14 01:30:09 +01001807 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001808 {
1809 $this->set($set);
1810 }
Barry Mienydd671972010-10-04 16:33:58 +02001811
Kyle Farris0c147b32011-08-26 02:29:31 -04001812 if ($this->_validate_update($table) === FALSE)
1813 {
1814 return FALSE;
1815 }
1816
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001817 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001818 {
1819 $this->where($where);
1820 }
1821
Andrey Andreev650b4c02012-06-11 12:07:15 +03001822 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001823 {
1824 $this->limit($limit);
1825 }
1826
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001827 $sql = $this->_update($this->qb_from[0], $this->qb_set);
Kyle Farris0c147b32011-08-26 02:29:31 -04001828 $this->_reset_write();
1829 return $this->query($sql);
1830 }
WanWizard7219c072011-12-28 14:09:05 +01001831
Kyle Farris0c147b32011-08-26 02:29:31 -04001832 // --------------------------------------------------------------------
1833
1834 /**
1835 * Validate Update
1836 *
1837 * This method is used by both update() and get_compiled_update() to
1838 * validate that data is actually being set and that a table has been
1839 * chosen to be update.
1840 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001841 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001842 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001843 */
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001844 protected function _validate_update($table)
Kyle Farris0c147b32011-08-26 02:29:31 -04001845 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001846 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001847 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001848 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001849 }
1850
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001851 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001852 {
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001853 $this->qb_from = array($this->protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +00001854 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001855 elseif ( ! isset($this->qb_from[0]))
1856 {
1857 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1858 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001859
1860 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001861 }
WanWizard7219c072011-12-28 14:09:05 +01001862
Derek Jonesd10e8962010-03-02 17:10:36 -06001863 // --------------------------------------------------------------------
1864
1865 /**
1866 * Update_Batch
1867 *
1868 * Compiles an update string and runs the query
1869 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001870 * @param string the table to retrieve the results from
1871 * @param array an associative array of update values
1872 * @param string the where key
Andrey Andreev9f808b02012-10-24 17:38:48 +03001873 * @return int number of rows affected or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001874 */
Andrey Andreev105a48b2016-02-04 15:45:10 +02001875 public function update_batch($table, $set = NULL, $index = NULL, $batch_size = 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001876 {
1877 // Combine any cached components with the current statements
1878 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001879
vlakoff1228fe22013-01-14 01:30:09 +01001880 if ($index === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001881 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001882 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001883 }
1884
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001885 if ($set === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001886 {
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001887 if (empty($this->qb_set))
1888 {
1889 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
1890 }
1891 }
1892 else
1893 {
1894 if (empty($set))
1895 {
1896 return ($this->db_debug) ? $this->display_error('update_batch() called with no data') : FALSE;
1897 }
1898
Derek Jonesd10e8962010-03-02 17:10:36 -06001899 $this->set_update_batch($set, $index);
1900 }
1901
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001902 if (strlen($table) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001903 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001904 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001905 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001906 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001907 }
Barry Mienydd671972010-10-04 16:33:58 +02001908
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001909 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001910 }
Barry Mienydd671972010-10-04 16:33:58 +02001911
Derek Jonesd10e8962010-03-02 17:10:36 -06001912 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001913 $affected_rows = 0;
Andrey Andreev105a48b2016-02-04 15:45:10 +02001914 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += $batch_size)
Derek Jonesd10e8962010-03-02 17:10:36 -06001915 {
Andrey Andreev105a48b2016-02-04 15:45:10 +02001916 $this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, $batch_size), $this->protect_identifiers($index)));
Andrey Andreev9f808b02012-10-24 17:38:48 +03001917 $affected_rows += $this->affected_rows();
Andrey Andreev79f888b2013-08-06 13:59:23 +03001918 $this->qb_where = array();
Derek Jonesd10e8962010-03-02 17:10:36 -06001919 }
Barry Mienydd671972010-10-04 16:33:58 +02001920
Derek Jonesd10e8962010-03-02 17:10:36 -06001921 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001922 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001923 }
1924
1925 // --------------------------------------------------------------------
1926
1927 /**
Andrey Andreev219565d2013-03-12 20:00:08 +02001928 * Update_Batch statement
1929 *
1930 * Generates a platform-specific batch update string from the supplied data
1931 *
1932 * @param string $table Table name
1933 * @param array $values Update data
1934 * @param string $index WHERE key
1935 * @return string
1936 */
1937 protected function _update_batch($table, $values, $index)
1938 {
1939 $ids = array();
1940 foreach ($values as $key => $val)
1941 {
1942 $ids[] = $val[$index];
1943
1944 foreach (array_keys($val) as $field)
1945 {
1946 if ($field !== $index)
1947 {
1948 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
1949 }
1950 }
1951 }
1952
1953 $cases = '';
1954 foreach ($final as $k => $v)
1955 {
1956 $cases .= $k." = CASE \n"
1957 .implode("\n", $v)."\n"
1958 .'ELSE '.$k.' END, ';
1959 }
1960
1961 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
1962
1963 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
1964 }
1965
1966 // --------------------------------------------------------------------
1967
1968 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001969 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001970 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001971 * @param array
1972 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001973 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001974 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001975 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001976 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001977 {
1978 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001979
Derek Jonesd10e8962010-03-02 17:10:36 -06001980 if ( ! is_array($key))
1981 {
1982 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001983 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001984
Andrey Andreevfe642da2012-06-16 03:47:33 +03001985 is_bool($escape) OR $escape = $this->_protect_identifiers;
1986
Derek Jonesd10e8962010-03-02 17:10:36 -06001987 foreach ($key as $k => $v)
1988 {
1989 $index_set = FALSE;
1990 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001991 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001992 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001993 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001994 {
1995 $index_set = TRUE;
1996 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001997
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001998 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001999 }
2000
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002001 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06002002 {
2003 return $this->display_error('db_batch_missing_index');
2004 }
2005
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002006 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06002007 }
Barry Mienydd671972010-10-04 16:33:58 +02002008
Derek Jonesd10e8962010-03-02 17:10:36 -06002009 return $this;
2010 }
2011
Derek Allard2067d1a2008-11-13 22:59:24 +00002012 // --------------------------------------------------------------------
2013
2014 /**
2015 * Empty Table
2016 *
2017 * Compiles a delete string and runs "DELETE FROM table"
2018 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002019 * @param string the table to empty
Andrey Andreevaec51262016-02-09 21:11:07 +02002020 * @return bool TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +00002021 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002022 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002023 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002024 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002025 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002026 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002027 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002028 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002029 }
2030
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002031 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002032 }
2033 else
2034 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002035 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002036 }
2037
2038 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002039 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00002040 return $this->query($sql);
2041 }
2042
2043 // --------------------------------------------------------------------
2044
2045 /**
2046 * Truncate
2047 *
2048 * Compiles a truncate string and runs the query
2049 * If the database does not support the truncate() command
2050 * This function maps to "DELETE FROM table"
2051 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002052 * @param string the table to truncate
Andrey Andreevaec51262016-02-09 21:11:07 +02002053 * @return bool TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +00002054 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002055 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002056 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002057 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002058 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002059 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002060 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002061 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002062 }
2063
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002064 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002065 }
2066 else
2067 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002068 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002069 }
2070
2071 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002072 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00002073 return $this->query($sql);
2074 }
WanWizard7219c072011-12-28 14:09:05 +01002075
Kyle Farris0c147b32011-08-26 02:29:31 -04002076 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02002077
Kyle Farris0c147b32011-08-26 02:29:31 -04002078 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03002079 * Truncate statement
2080 *
2081 * Generates a platform-specific truncate string from the supplied data
2082 *
2083 * If the database does not support the truncate() command,
2084 * then this method maps to 'DELETE FROM table'
2085 *
2086 * @param string the table name
2087 * @return string
2088 */
2089 protected function _truncate($table)
2090 {
2091 return 'TRUNCATE '.$table;
2092 }
2093
2094 // --------------------------------------------------------------------
2095
2096 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04002097 * Get DELETE query string
2098 *
2099 * Compiles a delete query string and returns the sql
2100 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002101 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002102 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04002103 * @return string
2104 */
2105 public function get_compiled_delete($table = '', $reset = TRUE)
2106 {
2107 $this->return_delete_sql = TRUE;
2108 $sql = $this->delete($table, '', NULL, $reset);
2109 $this->return_delete_sql = FALSE;
2110 return $sql;
2111 }
WanWizard7219c072011-12-28 14:09:05 +01002112
Derek Allard2067d1a2008-11-13 22:59:24 +00002113 // --------------------------------------------------------------------
2114
2115 /**
2116 * Delete
2117 *
2118 * Compiles a delete string and runs the query
2119 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002120 * @param mixed the table(s) to delete from. String or array
2121 * @param mixed the where clause
2122 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03002123 * @param bool
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002124 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00002125 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002126 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002127 {
2128 // Combine any cached components with the current statements
2129 $this->_merge_cache();
2130
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002131 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002132 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002133 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002134 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002135 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002136 }
2137
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002138 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002139 }
2140 elseif (is_array($table))
2141 {
Andrey Andreeva1170af2015-07-02 11:46:56 +03002142 empty($where) && $reset_data = FALSE;
2143
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05002144 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002145 {
Andrey Andreev13f50542012-10-12 12:31:02 +03002146 $this->delete($single_table, $where, $limit, $reset_data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002147 }
Andrey Andreeva1170af2015-07-02 11:46:56 +03002148
Derek Allard2067d1a2008-11-13 22:59:24 +00002149 return;
2150 }
2151 else
2152 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002153 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002154 }
2155
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002156 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002157 {
2158 $this->where($where);
2159 }
2160
Andrey Andreev650b4c02012-06-11 12:07:15 +03002161 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002162 {
2163 $this->limit($limit);
2164 }
2165
Andrey Andreev94611df2012-07-19 12:29:54 +03002166 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002167 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002168 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02002169 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002170
Andrey Andreevb0478652012-07-18 15:34:46 +03002171 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002172 if ($reset_data)
2173 {
2174 $this->_reset_write();
2175 }
WanWizard7219c072011-12-28 14:09:05 +01002176
Andrey Andreev24276a32012-01-08 02:44:38 +02002177 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00002178 }
WanWizard7219c072011-12-28 14:09:05 +01002179
Derek Allard2067d1a2008-11-13 22:59:24 +00002180 // --------------------------------------------------------------------
2181
2182 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03002183 * Delete statement
2184 *
2185 * Generates a platform-specific delete string from the supplied data
2186 *
2187 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03002188 * @return string
2189 */
Andrey Andreevb0478652012-07-18 15:34:46 +03002190 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03002191 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002192 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002193 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03002194 }
2195
2196 // --------------------------------------------------------------------
2197
2198 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002199 * DB Prefix
2200 *
2201 * Prepends a database prefix if one exists in configuration
2202 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002203 * @param string the table
2204 * @return string
2205 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002206 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002207 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002208 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002209 {
2210 $this->display_error('db_table_name_required');
2211 }
2212
2213 return $this->dbprefix.$table;
2214 }
2215
2216 // --------------------------------------------------------------------
2217
2218 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06002219 * Set DB Prefix
2220 *
2221 * Set's the DB Prefix to something new without needing to reconnect
2222 *
2223 * @param string the prefix
2224 * @return string
2225 */
2226 public function set_dbprefix($prefix = '')
2227 {
2228 return $this->dbprefix = $prefix;
2229 }
2230
2231 // --------------------------------------------------------------------
2232
2233 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002234 * Track Aliases
2235 *
2236 * Used to track SQL statements written with aliased tables.
2237 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002238 * @param string The table to inspect
2239 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002240 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002241 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002242 {
2243 if (is_array($table))
2244 {
2245 foreach ($table as $t)
2246 {
2247 $this->_track_aliases($t);
2248 }
2249 return;
2250 }
Barry Mienydd671972010-10-04 16:33:58 +02002251
Derek Jones37f4b9c2011-07-01 17:56:50 -05002252 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002253 // the string into discreet statements
2254 if (strpos($table, ',') !== FALSE)
2255 {
2256 return $this->_track_aliases(explode(',', $table));
2257 }
Barry Mienydd671972010-10-04 16:33:58 +02002258
Derek Allard2067d1a2008-11-13 22:59:24 +00002259 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002260 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002261 {
2262 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002263 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002264
Derek Allard2067d1a2008-11-13 22:59:24 +00002265 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002266 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002267
Derek Allard2067d1a2008-11-13 22:59:24 +00002268 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002269 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002270 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002271 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002272 }
2273 }
2274 }
WanWizard7219c072011-12-28 14:09:05 +01002275
Derek Allard2067d1a2008-11-13 22:59:24 +00002276 // --------------------------------------------------------------------
2277
2278 /**
2279 * Compile the SELECT statement
2280 *
2281 * Generates a query string based on which functions were used.
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002282 * Should not be called directly.
Derek Allard2067d1a2008-11-13 22:59:24 +00002283 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002284 * @param bool $select_override
Derek Allard2067d1a2008-11-13 22:59:24 +00002285 * @return string
2286 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002287 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002288 {
2289 // Combine any cached components with the current statements
2290 $this->_merge_cache();
2291
Derek Allard2067d1a2008-11-13 22:59:24 +00002292 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002293 if ($select_override !== FALSE)
2294 {
2295 $sql = $select_override;
2296 }
2297 else
2298 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002299 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002300
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002301 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002302 {
Barry Mienydd671972010-10-04 16:33:58 +02002303 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002304 }
2305 else
Barry Mienydd671972010-10-04 16:33:58 +02002306 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002307 // Cycle through the "select" portion of the query and prep each column name.
Andrey Andreev1924eb32015-04-08 17:19:24 +03002308 // The reason we protect identifiers here rather than in the select() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002309 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002310 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002311 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002312 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002313 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002314 }
Barry Mienydd671972010-10-04 16:33:58 +02002315
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002316 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002317 }
2318 }
2319
Derek Allard2067d1a2008-11-13 22:59:24 +00002320 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002321 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002322 {
Andrey Andreeve78f8152012-10-09 11:38:38 +03002323 $sql .= "\nFROM ".$this->_from_tables();
Derek Allard2067d1a2008-11-13 22:59:24 +00002324 }
2325
Derek Allard2067d1a2008-11-13 22:59:24 +00002326 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002327 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002328 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002329 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002330 }
2331
Andrey Andreev2d486232012-07-19 14:46:51 +03002332 $sql .= $this->_compile_wh('qb_where')
2333 .$this->_compile_group_by()
2334 .$this->_compile_wh('qb_having')
2335 .$this->_compile_order_by(); // ORDER BY
Andrey Andreevb0478652012-07-18 15:34:46 +03002336
Andrey Andreevd40459d2012-07-18 16:46:39 +03002337 // LIMIT
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002338 if ($this->qb_limit)
Derek Allard2067d1a2008-11-13 22:59:24 +00002339 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002340 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002341 }
2342
2343 return $sql;
2344 }
2345
2346 // --------------------------------------------------------------------
2347
2348 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002349 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002350 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002351 * Escapes identifiers in WHERE and HAVING statements at execution time.
2352 *
Andrey Andreev99c17e52016-02-29 16:53:45 +02002353 * Required so that aliases are tracked properly, regardless of whether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002354 * where(), or_where(), having(), or_having are called prior to from(),
2355 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002356 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002357 * @param string $qb_key 'qb_where' or 'qb_having'
Andrey Andreevd40459d2012-07-18 16:46:39 +03002358 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002359 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002360 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002361 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002362 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002363 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002364 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002365 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002366 // Is this condition already compiled?
2367 if (is_string($this->{$qb_key}[$i]))
2368 {
2369 continue;
2370 }
2371 elseif ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002372 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002373 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002374 continue;
2375 }
2376
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002377 // Split multiple conditions
2378 $conditions = preg_split(
Andrey Andreev554b4522015-09-01 13:51:26 +03002379 '/((?:^|\s+)AND\s+|(?:^|\s+)OR\s+)/i',
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002380 $this->{$qb_key}[$i]['condition'],
2381 -1,
2382 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
2383 );
2384
2385 for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002386 {
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002387 if (($op = $this->_get_operator($conditions[$ci])) === FALSE
Andrey Andreeve4742582012-10-25 13:25:13 +03002388 OR ! preg_match('/^(\(?)(.*)('.preg_quote($op, '/').')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches))
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002389 {
2390 continue;
2391 }
2392
2393 // $matches = array(
2394 // 0 => '(test <= foo)', /* the whole thing */
2395 // 1 => '(', /* optional */
2396 // 2 => 'test', /* the field name */
2397 // 3 => ' <= ', /* $op */
2398 // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2399 // 5 => ')' /* optional */
2400 // );
Andrey Andreev082aa402012-10-22 19:41:55 +03002401
2402 if ( ! empty($matches[4]))
2403 {
2404 $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4]));
2405 $matches[4] = ' '.$matches[4];
2406 }
2407
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002408 $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2]))
2409 .' '.trim($matches[3]).$matches[4].$matches[5];
Andrey Andreev6e704752012-07-18 00:46:33 +03002410 }
2411
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002412 $this->{$qb_key}[$i] = implode('', $conditions);
Andrey Andreev6e704752012-07-18 00:46:33 +03002413 }
2414
Andrey Andreev9d3aa1b2012-10-12 12:14:09 +03002415 return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ")
2416 .implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002417 }
2418
Andrey Andreevb0478652012-07-18 15:34:46 +03002419 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002420 }
2421
2422 // --------------------------------------------------------------------
2423
2424 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002425 * Compile GROUP BY
2426 *
2427 * Escapes identifiers in GROUP BY statements at execution time.
2428 *
2429 * Required so that aliases are tracked properly, regardless of wether
2430 * group_by() is called prior to from(), join() and dbprefix is added
2431 * only if needed.
2432 *
2433 * @return string SQL statement
2434 */
2435 protected function _compile_group_by()
2436 {
2437 if (count($this->qb_groupby) > 0)
2438 {
Andrey Andreev96feb582012-07-19 13:12:34 +03002439 for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
2440 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002441 // Is it already compiled?
Andrey Andreev18eba242014-01-23 22:52:31 +02002442 if (is_string($this->qb_groupby[$i]))
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002443 {
2444 continue;
2445 }
2446
Andrey Andreev082aa402012-10-22 19:41:55 +03002447 $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 +03002448 ? $this->qb_groupby[$i]['field']
Andrey Andreev13f50542012-10-12 12:31:02 +03002449 : $this->protect_identifiers($this->qb_groupby[$i]['field']);
Andrey Andreev96feb582012-07-19 13:12:34 +03002450 }
2451
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002452 return "\nGROUP BY ".implode(', ', $this->qb_groupby);
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002453 }
2454
2455 return '';
2456 }
2457
2458 // --------------------------------------------------------------------
2459
2460 /**
Andrey Andreev2d486232012-07-19 14:46:51 +03002461 * Compile ORDER BY
2462 *
2463 * Escapes identifiers in ORDER BY statements at execution time.
2464 *
2465 * Required so that aliases are tracked properly, regardless of wether
2466 * order_by() is called prior to from(), join() and dbprefix is added
2467 * only if needed.
2468 *
2469 * @return string SQL statement
2470 */
2471 protected function _compile_order_by()
2472 {
Andrey Andreeva53ea842012-10-23 12:44:09 +03002473 if (is_array($this->qb_orderby) && count($this->qb_orderby) > 0)
Andrey Andreev2d486232012-07-19 14:46:51 +03002474 {
Andrey Andreev2d486232012-07-19 14:46:51 +03002475 for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++)
2476 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002477 if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field']))
Andrey Andreev2d486232012-07-19 14:46:51 +03002478 {
Andrey Andreevfc043b32012-10-12 14:46:14 +03002479 $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']);
Andrey Andreev2d486232012-07-19 14:46:51 +03002480 }
2481
2482 $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction'];
2483 }
2484
Andrey Andreeva53ea842012-10-23 12:44:09 +03002485 return $this->qb_orderby = "\nORDER BY ".implode(', ', $this->qb_orderby);
2486 }
2487 elseif (is_string($this->qb_orderby))
2488 {
2489 return $this->qb_orderby;
Andrey Andreev2d486232012-07-19 14:46:51 +03002490 }
2491
2492 return '';
2493 }
2494
2495 // --------------------------------------------------------------------
2496
2497 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002498 * Object to Array
2499 *
2500 * Takes an object as input and converts the class variables to array key/vals
2501 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002502 * @param object
2503 * @return array
2504 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002505 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002506 {
2507 if ( ! is_object($object))
2508 {
2509 return $object;
2510 }
Barry Mienydd671972010-10-04 16:33:58 +02002511
Derek Allard2067d1a2008-11-13 22:59:24 +00002512 $array = array();
2513 foreach (get_object_vars($object) as $key => $val)
2514 {
2515 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002516 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002517 {
2518 $array[$key] = $val;
2519 }
2520 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002521
2522 return $array;
2523 }
Barry Mienydd671972010-10-04 16:33:58 +02002524
Derek Jonesd10e8962010-03-02 17:10:36 -06002525 // --------------------------------------------------------------------
2526
2527 /**
2528 * Object to Array
2529 *
2530 * Takes an object as input and converts the class variables to array key/vals
2531 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002532 * @param object
2533 * @return array
2534 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002535 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002536 {
2537 if ( ! is_object($object))
2538 {
2539 return $object;
2540 }
Barry Mienydd671972010-10-04 16:33:58 +02002541
Derek Jonesd10e8962010-03-02 17:10:36 -06002542 $array = array();
2543 $out = get_object_vars($object);
2544 $fields = array_keys($out);
2545
2546 foreach ($fields as $val)
2547 {
2548 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002549 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002550 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002551 $i = 0;
2552 foreach ($out[$val] as $data)
2553 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002554 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002555 }
2556 }
2557 }
2558
Derek Allard2067d1a2008-11-13 22:59:24 +00002559 return $array;
2560 }
Barry Mienydd671972010-10-04 16:33:58 +02002561
Derek Allard2067d1a2008-11-13 22:59:24 +00002562 // --------------------------------------------------------------------
2563
2564 /**
2565 * Start Cache
2566 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002567 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002568 *
Andrey Andreev4a587f52014-12-11 16:27:15 +02002569 * @return CI_DB_query_builder
Barry Mienydd671972010-10-04 16:33:58 +02002570 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002571 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002572 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002573 $this->qb_caching = TRUE;
Andrey Andreev4a587f52014-12-11 16:27:15 +02002574 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002575 }
2576
2577 // --------------------------------------------------------------------
2578
2579 /**
2580 * Stop Cache
2581 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002582 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002583 *
Andrey Andreev4a587f52014-12-11 16:27:15 +02002584 * @return CI_DB_query_builder
Barry Mienydd671972010-10-04 16:33:58 +02002585 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002586 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002587 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002588 $this->qb_caching = FALSE;
Andrey Andreev4a587f52014-12-11 16:27:15 +02002589 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002590 }
2591
2592 // --------------------------------------------------------------------
2593
2594 /**
2595 * Flush Cache
2596 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002597 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002598 *
Andrey Andreev4a587f52014-12-11 16:27:15 +02002599 * @return CI_DB_query_builder
Barry Mienydd671972010-10-04 16:33:58 +02002600 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002601 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002602 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002603 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002604 'qb_cache_select' => array(),
2605 'qb_cache_from' => array(),
2606 'qb_cache_join' => array(),
2607 'qb_cache_where' => array(),
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002608 'qb_cache_groupby' => array(),
2609 'qb_cache_having' => array(),
2610 'qb_cache_orderby' => array(),
2611 'qb_cache_set' => array(),
2612 'qb_cache_exists' => array(),
2613 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002614 ));
Andrey Andreev4a587f52014-12-11 16:27:15 +02002615
2616 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002617 }
2618
2619 // --------------------------------------------------------------------
2620
2621 /**
2622 * Merge Cache
2623 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002624 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002625 * locally called ones.
2626 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002627 * @return void
2628 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002629 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002630 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002631 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002632 {
2633 return;
2634 }
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002635 elseif (in_array('select', $this->qb_cache_exists, TRUE))
2636 {
2637 $qb_no_escape = $this->qb_cache_no_escape;
2638 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002639
GDmac17a05282013-11-11 13:18:09 +01002640 foreach (array_unique($this->qb_cache_exists) as $val) // select, from, etc.
Derek Allard2067d1a2008-11-13 22:59:24 +00002641 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002642 $qb_variable = 'qb_'.$val;
2643 $qb_cache_var = 'qb_cache_'.$val;
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002644 $qb_new = $this->$qb_cache_var;
GDmace1b86832013-11-08 16:52:54 +01002645
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002646 for ($i = 0, $c = count($this->$qb_variable); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00002647 {
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002648 if ( ! in_array($this->{$qb_variable}[$i], $qb_new, TRUE))
2649 {
2650 $qb_new[] = $this->{$qb_variable}[$i];
2651 if ($val === 'select')
2652 {
2653 $qb_no_escape[] = $this->qb_no_escape[$i];
2654 }
2655 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002656 }
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002657
GDmace1b86832013-11-08 16:52:54 +01002658 $this->$qb_variable = $qb_new;
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002659 if ($val === 'select')
2660 {
2661 $this->qb_no_escape = $qb_no_escape;
2662 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002663 }
2664
2665 // If we are "protecting identifiers" we need to examine the "from"
2666 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002667 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002668 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002669 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002670 }
2671 }
WanWizard7219c072011-12-28 14:09:05 +01002672
Kyle Farris0c147b32011-08-26 02:29:31 -04002673 // --------------------------------------------------------------------
2674
2675 /**
Andrey Andreev082aa402012-10-22 19:41:55 +03002676 * Is literal
2677 *
2678 * Determines if a string represents a literal value or a field name
2679 *
Andrey Andreev02e4cd72012-11-13 11:50:47 +02002680 * @param string $str
Andrey Andreev082aa402012-10-22 19:41:55 +03002681 * @return bool
2682 */
2683 protected function _is_literal($str)
2684 {
2685 $str = trim($str);
2686
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002687 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 +03002688 {
2689 return TRUE;
2690 }
2691
2692 static $_str;
2693
2694 if (empty($_str))
2695 {
2696 $_str = ($this->_escape_char !== '"')
2697 ? array('"', "'") : array("'");
2698 }
2699
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002700 return in_array($str[0], $_str, TRUE);
Andrey Andreev082aa402012-10-22 19:41:55 +03002701 }
2702
2703 // --------------------------------------------------------------------
2704
2705 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002706 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002707 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002708 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002709 *
Andrey Andreev435e0c22014-12-11 16:30:13 +02002710 * @return CI_DB_query_builder
Kyle Farris0c147b32011-08-26 02:29:31 -04002711 */
2712 public function reset_query()
2713 {
2714 $this->_reset_select();
2715 $this->_reset_write();
Andrey Andreev435e0c22014-12-11 16:30:13 +02002716 return $this;
Kyle Farris0c147b32011-08-26 02:29:31 -04002717 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002718
2719 // --------------------------------------------------------------------
2720
2721 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002722 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002723 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002724 * @param array An array of fields to reset
2725 * @return void
2726 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002727 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002728 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002729 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002730 {
Andrey Andreevae85eb42012-11-02 01:42:31 +02002731 $this->$item = $default_value;
Derek Allard2067d1a2008-11-13 22:59:24 +00002732 }
2733 }
2734
2735 // --------------------------------------------------------------------
2736
2737 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002738 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002739 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002740 * @return void
2741 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002742 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002743 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002744 $this->_reset_run(array(
Andrey Andreev4a587f52014-12-11 16:27:15 +02002745 'qb_select' => array(),
2746 'qb_from' => array(),
2747 'qb_join' => array(),
2748 'qb_where' => array(),
2749 'qb_groupby' => array(),
2750 'qb_having' => array(),
2751 'qb_orderby' => array(),
2752 'qb_aliased_tables' => array(),
2753 'qb_no_escape' => array(),
2754 'qb_distinct' => FALSE,
2755 'qb_limit' => FALSE,
2756 'qb_offset' => FALSE
2757 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002758 }
Barry Mienydd671972010-10-04 16:33:58 +02002759
Derek Allard2067d1a2008-11-13 22:59:24 +00002760 // --------------------------------------------------------------------
2761
2762 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002763 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002764 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002765 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002766 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002767 * @return void
2768 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002769 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002770 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002771 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002772 'qb_set' => array(),
2773 'qb_from' => array(),
Andrey Andreev3e014372013-02-21 15:59:34 +02002774 'qb_join' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002775 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002776 'qb_orderby' => array(),
2777 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002778 'qb_limit' => FALSE
Andrey Andreev4a587f52014-12-11 16:27:15 +02002779 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002780 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002781
Derek Allard2067d1a2008-11-13 22:59:24 +00002782}