blob: 5a86ce50f4113f4c63c4c1d0e79c0fc2a8cb8306 [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 Andreevbe4bab92016-10-28 12:50:03 +0300682 elseif (preg_match('/\s*(!?=|<>|\sIS(?:\s+NOT)?\s)\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 Andreevacc64812016-07-29 11:42:28 +03001274 return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').(int) $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
Andrey Andreeve84a1f52016-05-26 10:09:37 +03001398 $result = ($this->qb_distinct === TRUE OR ! empty($this->qb_groupby))
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 Andreevd9a40632016-07-22 15:49:08 +03001501 if ($this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, $escape, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, $batch_size))))
1502 {
1503 $affected_rows += $this->affected_rows();
1504 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001505 }
Barry Mienydd671972010-10-04 16:33:58 +02001506
Derek Jonesd10e8962010-03-02 17:10:36 -06001507 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001508 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001509 }
1510
1511 // --------------------------------------------------------------------
1512
1513 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +02001514 * Insert batch statement
Andrey Andreev97f36972012-04-05 12:44:36 +03001515 *
1516 * Generates a platform-specific insert string from the supplied data.
1517 *
Andrey Andreev083e3c82012-11-06 12:48:32 +02001518 * @param string $table Table name
1519 * @param array $keys INSERT keys
1520 * @param array $values INSERT values
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001521 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001522 */
1523 protected function _insert_batch($table, $keys, $values)
1524 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001525 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001526 }
1527
1528 // --------------------------------------------------------------------
1529
1530 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001531 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001532 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001533 * @param mixed
1534 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001535 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001536 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001537 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001538 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001539 {
1540 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001541
Derek Jonesd10e8962010-03-02 17:10:36 -06001542 if ( ! is_array($key))
1543 {
1544 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001545 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001546
Andrey Andreevfe642da2012-06-16 03:47:33 +03001547 is_bool($escape) OR $escape = $this->_protect_identifiers;
1548
Iban Eguia3c0a4522012-04-15 13:30:44 +02001549 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001550 sort($keys);
1551
1552 foreach ($key as $row)
1553 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001554 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001555 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1556 {
1557 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001558 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001559 return;
1560 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001561
Barry Mienydd671972010-10-04 16:33:58 +02001562 ksort($row); // puts $row in the same order as our keys
1563
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001564 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001565 {
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001566 $clean = array();
1567 foreach ($row as $value)
1568 {
1569 $clean[] = $this->escape($value);
1570 }
1571
1572 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001573 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001574
Andrey Andreev838a9d62012-12-03 14:37:47 +02001575 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001576 }
1577
1578 foreach ($keys as $k)
1579 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001580 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001581 }
Barry Mienydd671972010-10-04 16:33:58 +02001582
Derek Jonesd10e8962010-03-02 17:10:36 -06001583 return $this;
1584 }
WanWizard7219c072011-12-28 14:09:05 +01001585
Kyle Farris0c147b32011-08-26 02:29:31 -04001586 // --------------------------------------------------------------------
1587
1588 /**
1589 * Get INSERT query string
1590 *
1591 * Compiles an insert query and returns the sql
1592 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001593 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001594 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001595 * @return string
1596 */
1597 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001598 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001599 if ($this->_validate_insert($table) === FALSE)
1600 {
1601 return FALSE;
1602 }
WanWizard7219c072011-12-28 14:09:05 +01001603
Kyle Farris76116012011-08-31 11:17:48 -04001604 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001605 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001606 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001607 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001608 array_keys($this->qb_set),
1609 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001610 );
WanWizard7219c072011-12-28 14:09:05 +01001611
Kyle Farris0c147b32011-08-26 02:29:31 -04001612 if ($reset === TRUE)
1613 {
1614 $this->_reset_write();
1615 }
WanWizard7219c072011-12-28 14:09:05 +01001616
Kyle Farris0c147b32011-08-26 02:29:31 -04001617 return $sql;
1618 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001619
Derek Allard2067d1a2008-11-13 22:59:24 +00001620 // --------------------------------------------------------------------
1621
1622 /**
1623 * Insert
1624 *
1625 * Compiles an insert string and runs the query
1626 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001627 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001628 * @param array an associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001629 * @param bool $escape Whether to escape values and identifiers
Andrey Andreevaec51262016-02-09 21:11:07 +02001630 * @return bool TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +00001631 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001632 public function insert($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001633 {
vlakoff1228fe22013-01-14 01:30:09 +01001634 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001635 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001636 $this->set($set, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001637 }
WanWizard7219c072011-12-28 14:09:05 +01001638
Kyle Farris0c147b32011-08-26 02:29:31 -04001639 if ($this->_validate_insert($table) === FALSE)
1640 {
1641 return FALSE;
1642 }
WanWizard7219c072011-12-28 14:09:05 +01001643
Kyle Farris76116012011-08-31 11:17:48 -04001644 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001645 $this->protect_identifiers(
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001646 $this->qb_from[0], TRUE, $escape, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001647 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001648 array_keys($this->qb_set),
1649 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001650 );
Barry Mienydd671972010-10-04 16:33:58 +02001651
Kyle Farris0c147b32011-08-26 02:29:31 -04001652 $this->_reset_write();
1653 return $this->query($sql);
1654 }
WanWizard7219c072011-12-28 14:09:05 +01001655
Kyle Farris0c147b32011-08-26 02:29:31 -04001656 // --------------------------------------------------------------------
1657
1658 /**
1659 * Validate Insert
1660 *
1661 * This method is used by both insert() and get_compiled_insert() to
1662 * validate that the there data is actually being set and that table
1663 * has been chosen to be inserted into.
1664 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001665 * @param string the table to insert data into
1666 * @return string
1667 */
WanWizard7219c072011-12-28 14:09:05 +01001668 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001669 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001670 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001671 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001672 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001673 }
1674
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001675 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001676 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001677 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001678 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001679 elseif ( ! isset($this->qb_from[0]))
1680 {
1681 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1682 }
WanWizard7219c072011-12-28 14:09:05 +01001683
Kyle Farris0c147b32011-08-26 02:29:31 -04001684 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001685 }
Barry Mienydd671972010-10-04 16:33:58 +02001686
Phil Sturgeon9789f322011-07-15 15:14:05 -06001687 // --------------------------------------------------------------------
1688
1689 /**
1690 * Replace
1691 *
1692 * Compiles an replace into string and runs the query
1693 *
1694 * @param string the table to replace data into
1695 * @param array an associative array of insert values
Andrey Andreevaec51262016-02-09 21:11:07 +02001696 * @return bool TRUE on success, FALSE on failure
Phil Sturgeon9789f322011-07-15 15:14:05 -06001697 */
1698 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001699 {
vlakoff1228fe22013-01-14 01:30:09 +01001700 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001701 {
1702 $this->set($set);
1703 }
Barry Mienydd671972010-10-04 16:33:58 +02001704
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001705 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001706 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001707 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001708 }
1709
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001710 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001711 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001712 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001713 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001714 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001715 }
Barry Mienydd671972010-10-04 16:33:58 +02001716
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001717 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001718 }
1719
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001720 $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 +00001721
Derek Jonesd10e8962010-03-02 17:10:36 -06001722 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001723 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001724 }
WanWizard7219c072011-12-28 14:09:05 +01001725
Kyle Farris0c147b32011-08-26 02:29:31 -04001726 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001727
Kyle Farris0c147b32011-08-26 02:29:31 -04001728 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001729 * Replace statement
1730 *
1731 * Generates a platform-specific replace string from the supplied data
1732 *
1733 * @param string the table name
1734 * @param array the insert keys
1735 * @param array the insert values
1736 * @return string
1737 */
1738 protected function _replace($table, $keys, $values)
1739 {
1740 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1741 }
1742
1743 // --------------------------------------------------------------------
1744
1745 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001746 * FROM tables
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001747 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001748 * Groups tables in FROM clauses if needed, so there is no confusion
1749 * about operator precedence.
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001750 *
Claudio Galdiolo93e78132015-01-29 11:43:56 -05001751 * Note: This is only used (and overridden) by MySQL and CUBRID.
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001752 *
1753 * @return string
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001754 */
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001755 protected function _from_tables()
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001756 {
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001757 return implode(', ', $this->qb_from);
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001758 }
1759
1760 // --------------------------------------------------------------------
1761
1762 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001763 * Get UPDATE query string
1764 *
1765 * Compiles an update query and returns the sql
1766 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001767 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001768 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001769 * @return string
1770 */
1771 public function get_compiled_update($table = '', $reset = TRUE)
1772 {
1773 // Combine any cached components with the current statements
1774 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001775
Kyle Farris0c147b32011-08-26 02:29:31 -04001776 if ($this->_validate_update($table) === FALSE)
1777 {
1778 return FALSE;
1779 }
WanWizard7219c072011-12-28 14:09:05 +01001780
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001781 $sql = $this->_update($this->qb_from[0], $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001782
Kyle Farris0c147b32011-08-26 02:29:31 -04001783 if ($reset === TRUE)
1784 {
1785 $this->_reset_write();
1786 }
WanWizard7219c072011-12-28 14:09:05 +01001787
Kyle Farris0c147b32011-08-26 02:29:31 -04001788 return $sql;
1789 }
WanWizard7219c072011-12-28 14:09:05 +01001790
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 // --------------------------------------------------------------------
1792
1793 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001794 * UPDATE
Derek Allard2067d1a2008-11-13 22:59:24 +00001795 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001796 * Compiles an update string and runs the query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001797 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001798 * @param string $table
1799 * @param array $set An associative array of update values
1800 * @param mixed $where
1801 * @param int $limit
Andrey Andreevaec51262016-02-09 21:11:07 +02001802 * @return bool TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +00001803 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001804 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001805 {
1806 // Combine any cached components with the current statements
1807 $this->_merge_cache();
1808
vlakoff1228fe22013-01-14 01:30:09 +01001809 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001810 {
1811 $this->set($set);
1812 }
Barry Mienydd671972010-10-04 16:33:58 +02001813
Kyle Farris0c147b32011-08-26 02:29:31 -04001814 if ($this->_validate_update($table) === FALSE)
1815 {
1816 return FALSE;
1817 }
1818
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001819 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001820 {
1821 $this->where($where);
1822 }
1823
Andrey Andreev650b4c02012-06-11 12:07:15 +03001824 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001825 {
1826 $this->limit($limit);
1827 }
1828
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001829 $sql = $this->_update($this->qb_from[0], $this->qb_set);
Kyle Farris0c147b32011-08-26 02:29:31 -04001830 $this->_reset_write();
1831 return $this->query($sql);
1832 }
WanWizard7219c072011-12-28 14:09:05 +01001833
Kyle Farris0c147b32011-08-26 02:29:31 -04001834 // --------------------------------------------------------------------
1835
1836 /**
1837 * Validate Update
1838 *
1839 * This method is used by both update() and get_compiled_update() to
1840 * validate that data is actually being set and that a table has been
1841 * chosen to be update.
1842 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001843 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001844 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001845 */
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001846 protected function _validate_update($table)
Kyle Farris0c147b32011-08-26 02:29:31 -04001847 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001848 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001849 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001850 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001851 }
1852
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001853 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001854 {
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001855 $this->qb_from = array($this->protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +00001856 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001857 elseif ( ! isset($this->qb_from[0]))
1858 {
1859 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1860 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001861
1862 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001863 }
WanWizard7219c072011-12-28 14:09:05 +01001864
Derek Jonesd10e8962010-03-02 17:10:36 -06001865 // --------------------------------------------------------------------
1866
1867 /**
1868 * Update_Batch
1869 *
1870 * Compiles an update string and runs the query
1871 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001872 * @param string the table to retrieve the results from
1873 * @param array an associative array of update values
1874 * @param string the where key
Andrey Andreev9f808b02012-10-24 17:38:48 +03001875 * @return int number of rows affected or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001876 */
Andrey Andreev105a48b2016-02-04 15:45:10 +02001877 public function update_batch($table, $set = NULL, $index = NULL, $batch_size = 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001878 {
1879 // Combine any cached components with the current statements
1880 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001881
vlakoff1228fe22013-01-14 01:30:09 +01001882 if ($index === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001883 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001884 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001885 }
1886
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001887 if ($set === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001888 {
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001889 if (empty($this->qb_set))
1890 {
1891 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
1892 }
1893 }
1894 else
1895 {
1896 if (empty($set))
1897 {
1898 return ($this->db_debug) ? $this->display_error('update_batch() called with no data') : FALSE;
1899 }
1900
Derek Jonesd10e8962010-03-02 17:10:36 -06001901 $this->set_update_batch($set, $index);
1902 }
1903
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001904 if (strlen($table) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001905 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001906 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001907 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001908 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001909 }
Barry Mienydd671972010-10-04 16:33:58 +02001910
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001911 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001912 }
Barry Mienydd671972010-10-04 16:33:58 +02001913
Derek Jonesd10e8962010-03-02 17:10:36 -06001914 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001915 $affected_rows = 0;
Andrey Andreev105a48b2016-02-04 15:45:10 +02001916 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += $batch_size)
Derek Jonesd10e8962010-03-02 17:10:36 -06001917 {
Andrey Andreev0c23e912016-10-27 16:55:19 +03001918 if ($this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, $batch_size), $index)))
Andrey Andreevd9a40632016-07-22 15:49:08 +03001919 {
1920 $affected_rows += $this->affected_rows();
1921 }
1922
Andrey Andreev79f888b2013-08-06 13:59:23 +03001923 $this->qb_where = array();
Derek Jonesd10e8962010-03-02 17:10:36 -06001924 }
Barry Mienydd671972010-10-04 16:33:58 +02001925
Derek Jonesd10e8962010-03-02 17:10:36 -06001926 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001927 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001928 }
1929
1930 // --------------------------------------------------------------------
1931
1932 /**
Andrey Andreev219565d2013-03-12 20:00:08 +02001933 * Update_Batch statement
1934 *
1935 * Generates a platform-specific batch update string from the supplied data
1936 *
1937 * @param string $table Table name
1938 * @param array $values Update data
1939 * @param string $index WHERE key
1940 * @return string
1941 */
1942 protected function _update_batch($table, $values, $index)
1943 {
Andrey Andreev0c23e912016-10-27 16:55:19 +03001944 $index_escaped = $this->protect_identifiers($index);
1945
Andrey Andreev219565d2013-03-12 20:00:08 +02001946 $ids = array();
1947 foreach ($values as $key => $val)
1948 {
1949 $ids[] = $val[$index];
1950
1951 foreach (array_keys($val) as $field)
1952 {
1953 if ($field !== $index)
1954 {
Andrey Andreev0c23e912016-10-27 16:55:19 +03001955 $final[$field][] = 'WHEN '.$index_escaped.' = '.$val[$index].' THEN '.$val[$field];
Andrey Andreev219565d2013-03-12 20:00:08 +02001956 }
1957 }
1958 }
1959
1960 $cases = '';
1961 foreach ($final as $k => $v)
1962 {
1963 $cases .= $k." = CASE \n"
1964 .implode("\n", $v)."\n"
1965 .'ELSE '.$k.' END, ';
1966 }
1967
Andrey Andreev0c23e912016-10-27 16:55:19 +03001968 $this->where($index_escaped.' IN('.implode(',', $ids).')', NULL, FALSE);
Andrey Andreev219565d2013-03-12 20:00:08 +02001969
1970 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
1971 }
1972
1973 // --------------------------------------------------------------------
1974
1975 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001976 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001977 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001978 * @param array
1979 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001980 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001981 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001982 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001983 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001984 {
1985 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001986
Derek Jonesd10e8962010-03-02 17:10:36 -06001987 if ( ! is_array($key))
1988 {
1989 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001990 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001991
Andrey Andreevfe642da2012-06-16 03:47:33 +03001992 is_bool($escape) OR $escape = $this->_protect_identifiers;
1993
Derek Jonesd10e8962010-03-02 17:10:36 -06001994 foreach ($key as $k => $v)
1995 {
1996 $index_set = FALSE;
1997 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001998 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001999 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002000 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06002001 {
2002 $index_set = TRUE;
2003 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002004
Andrey Andreev2dfbe992015-07-27 21:54:18 +03002005 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06002006 }
2007
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002008 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06002009 {
2010 return $this->display_error('db_batch_missing_index');
2011 }
2012
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002013 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06002014 }
Barry Mienydd671972010-10-04 16:33:58 +02002015
Derek Jonesd10e8962010-03-02 17:10:36 -06002016 return $this;
2017 }
2018
Derek Allard2067d1a2008-11-13 22:59:24 +00002019 // --------------------------------------------------------------------
2020
2021 /**
2022 * Empty Table
2023 *
2024 * Compiles a delete string and runs "DELETE FROM table"
2025 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002026 * @param string the table to empty
Andrey Andreevaec51262016-02-09 21:11:07 +02002027 * @return bool TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +00002028 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002029 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002030 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002031 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002032 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002033 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002034 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002035 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002036 }
2037
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002038 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002039 }
2040 else
2041 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002042 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002043 }
2044
2045 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002046 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00002047 return $this->query($sql);
2048 }
2049
2050 // --------------------------------------------------------------------
2051
2052 /**
2053 * Truncate
2054 *
2055 * Compiles a truncate string and runs the query
2056 * If the database does not support the truncate() command
2057 * This function maps to "DELETE FROM table"
2058 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002059 * @param string the table to truncate
Andrey Andreevaec51262016-02-09 21:11:07 +02002060 * @return bool TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +00002061 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002062 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002063 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002064 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002065 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002066 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002067 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002068 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002069 }
2070
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002071 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002072 }
2073 else
2074 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002075 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002076 }
2077
2078 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002079 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00002080 return $this->query($sql);
2081 }
WanWizard7219c072011-12-28 14:09:05 +01002082
Kyle Farris0c147b32011-08-26 02:29:31 -04002083 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02002084
Kyle Farris0c147b32011-08-26 02:29:31 -04002085 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03002086 * Truncate statement
2087 *
2088 * Generates a platform-specific truncate string from the supplied data
2089 *
2090 * If the database does not support the truncate() command,
2091 * then this method maps to 'DELETE FROM table'
2092 *
2093 * @param string the table name
2094 * @return string
2095 */
2096 protected function _truncate($table)
2097 {
2098 return 'TRUNCATE '.$table;
2099 }
2100
2101 // --------------------------------------------------------------------
2102
2103 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04002104 * Get DELETE query string
2105 *
2106 * Compiles a delete query string and returns the sql
2107 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002108 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002109 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04002110 * @return string
2111 */
2112 public function get_compiled_delete($table = '', $reset = TRUE)
2113 {
2114 $this->return_delete_sql = TRUE;
2115 $sql = $this->delete($table, '', NULL, $reset);
2116 $this->return_delete_sql = FALSE;
2117 return $sql;
2118 }
WanWizard7219c072011-12-28 14:09:05 +01002119
Derek Allard2067d1a2008-11-13 22:59:24 +00002120 // --------------------------------------------------------------------
2121
2122 /**
2123 * Delete
2124 *
2125 * Compiles a delete string and runs the query
2126 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002127 * @param mixed the table(s) to delete from. String or array
2128 * @param mixed the where clause
2129 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03002130 * @param bool
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002131 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00002132 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002133 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002134 {
2135 // Combine any cached components with the current statements
2136 $this->_merge_cache();
2137
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002138 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002139 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002140 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002141 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002142 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002143 }
2144
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002145 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002146 }
2147 elseif (is_array($table))
2148 {
Andrey Andreeva1170af2015-07-02 11:46:56 +03002149 empty($where) && $reset_data = FALSE;
2150
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05002151 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002152 {
Andrey Andreev13f50542012-10-12 12:31:02 +03002153 $this->delete($single_table, $where, $limit, $reset_data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002154 }
Andrey Andreeva1170af2015-07-02 11:46:56 +03002155
Derek Allard2067d1a2008-11-13 22:59:24 +00002156 return;
2157 }
2158 else
2159 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002160 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002161 }
2162
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002163 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002164 {
2165 $this->where($where);
2166 }
2167
Andrey Andreev650b4c02012-06-11 12:07:15 +03002168 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002169 {
2170 $this->limit($limit);
2171 }
2172
Andrey Andreev94611df2012-07-19 12:29:54 +03002173 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002174 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002175 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02002176 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002177
Andrey Andreevb0478652012-07-18 15:34:46 +03002178 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002179 if ($reset_data)
2180 {
2181 $this->_reset_write();
2182 }
WanWizard7219c072011-12-28 14:09:05 +01002183
Andrey Andreev24276a32012-01-08 02:44:38 +02002184 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00002185 }
WanWizard7219c072011-12-28 14:09:05 +01002186
Derek Allard2067d1a2008-11-13 22:59:24 +00002187 // --------------------------------------------------------------------
2188
2189 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03002190 * Delete statement
2191 *
2192 * Generates a platform-specific delete string from the supplied data
2193 *
2194 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03002195 * @return string
2196 */
Andrey Andreevb0478652012-07-18 15:34:46 +03002197 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03002198 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002199 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002200 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03002201 }
2202
2203 // --------------------------------------------------------------------
2204
2205 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002206 * DB Prefix
2207 *
2208 * Prepends a database prefix if one exists in configuration
2209 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002210 * @param string the table
2211 * @return string
2212 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002213 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002214 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002215 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002216 {
2217 $this->display_error('db_table_name_required');
2218 }
2219
2220 return $this->dbprefix.$table;
2221 }
2222
2223 // --------------------------------------------------------------------
2224
2225 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06002226 * Set DB Prefix
2227 *
2228 * Set's the DB Prefix to something new without needing to reconnect
2229 *
2230 * @param string the prefix
2231 * @return string
2232 */
2233 public function set_dbprefix($prefix = '')
2234 {
2235 return $this->dbprefix = $prefix;
2236 }
2237
2238 // --------------------------------------------------------------------
2239
2240 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002241 * Track Aliases
2242 *
2243 * Used to track SQL statements written with aliased tables.
2244 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002245 * @param string The table to inspect
2246 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002247 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002248 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002249 {
2250 if (is_array($table))
2251 {
2252 foreach ($table as $t)
2253 {
2254 $this->_track_aliases($t);
2255 }
2256 return;
2257 }
Barry Mienydd671972010-10-04 16:33:58 +02002258
Derek Jones37f4b9c2011-07-01 17:56:50 -05002259 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002260 // the string into discreet statements
2261 if (strpos($table, ',') !== FALSE)
2262 {
2263 return $this->_track_aliases(explode(',', $table));
2264 }
Barry Mienydd671972010-10-04 16:33:58 +02002265
Derek Allard2067d1a2008-11-13 22:59:24 +00002266 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002267 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002268 {
2269 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002270 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002271
Derek Allard2067d1a2008-11-13 22:59:24 +00002272 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002273 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002274
Derek Allard2067d1a2008-11-13 22:59:24 +00002275 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002276 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002277 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002278 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002279 }
2280 }
2281 }
WanWizard7219c072011-12-28 14:09:05 +01002282
Derek Allard2067d1a2008-11-13 22:59:24 +00002283 // --------------------------------------------------------------------
2284
2285 /**
2286 * Compile the SELECT statement
2287 *
2288 * Generates a query string based on which functions were used.
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002289 * Should not be called directly.
Derek Allard2067d1a2008-11-13 22:59:24 +00002290 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002291 * @param bool $select_override
Derek Allard2067d1a2008-11-13 22:59:24 +00002292 * @return string
2293 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002294 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002295 {
2296 // Combine any cached components with the current statements
2297 $this->_merge_cache();
2298
Derek Allard2067d1a2008-11-13 22:59:24 +00002299 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002300 if ($select_override !== FALSE)
2301 {
2302 $sql = $select_override;
2303 }
2304 else
2305 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002306 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002307
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002308 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002309 {
Barry Mienydd671972010-10-04 16:33:58 +02002310 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002311 }
2312 else
Barry Mienydd671972010-10-04 16:33:58 +02002313 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002314 // Cycle through the "select" portion of the query and prep each column name.
Andrey Andreev1924eb32015-04-08 17:19:24 +03002315 // The reason we protect identifiers here rather than in the select() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002316 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002317 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002318 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002319 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002320 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002321 }
Barry Mienydd671972010-10-04 16:33:58 +02002322
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002323 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002324 }
2325 }
2326
Derek Allard2067d1a2008-11-13 22:59:24 +00002327 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002328 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002329 {
Andrey Andreeve78f8152012-10-09 11:38:38 +03002330 $sql .= "\nFROM ".$this->_from_tables();
Derek Allard2067d1a2008-11-13 22:59:24 +00002331 }
2332
Derek Allard2067d1a2008-11-13 22:59:24 +00002333 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002334 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002335 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002336 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002337 }
2338
Andrey Andreev2d486232012-07-19 14:46:51 +03002339 $sql .= $this->_compile_wh('qb_where')
2340 .$this->_compile_group_by()
2341 .$this->_compile_wh('qb_having')
2342 .$this->_compile_order_by(); // ORDER BY
Andrey Andreevb0478652012-07-18 15:34:46 +03002343
Andrey Andreevd40459d2012-07-18 16:46:39 +03002344 // LIMIT
Andrey Andreevacc64812016-07-29 11:42:28 +03002345 if ($this->qb_limit OR $this->qb_offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00002346 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002347 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002348 }
2349
2350 return $sql;
2351 }
2352
2353 // --------------------------------------------------------------------
2354
2355 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002356 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002357 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002358 * Escapes identifiers in WHERE and HAVING statements at execution time.
2359 *
Andrey Andreev99c17e52016-02-29 16:53:45 +02002360 * Required so that aliases are tracked properly, regardless of whether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002361 * where(), or_where(), having(), or_having are called prior to from(),
2362 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002363 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002364 * @param string $qb_key 'qb_where' or 'qb_having'
Andrey Andreevd40459d2012-07-18 16:46:39 +03002365 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002366 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002367 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002368 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002369 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002370 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002371 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002372 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002373 // Is this condition already compiled?
2374 if (is_string($this->{$qb_key}[$i]))
2375 {
2376 continue;
2377 }
2378 elseif ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002379 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002380 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002381 continue;
2382 }
2383
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002384 // Split multiple conditions
2385 $conditions = preg_split(
Andrey Andreev554b4522015-09-01 13:51:26 +03002386 '/((?:^|\s+)AND\s+|(?:^|\s+)OR\s+)/i',
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002387 $this->{$qb_key}[$i]['condition'],
2388 -1,
2389 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
2390 );
2391
2392 for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002393 {
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002394 if (($op = $this->_get_operator($conditions[$ci])) === FALSE
Andrey Andreeve4742582012-10-25 13:25:13 +03002395 OR ! preg_match('/^(\(?)(.*)('.preg_quote($op, '/').')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches))
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002396 {
2397 continue;
2398 }
2399
2400 // $matches = array(
2401 // 0 => '(test <= foo)', /* the whole thing */
2402 // 1 => '(', /* optional */
2403 // 2 => 'test', /* the field name */
2404 // 3 => ' <= ', /* $op */
2405 // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2406 // 5 => ')' /* optional */
2407 // );
Andrey Andreev082aa402012-10-22 19:41:55 +03002408
2409 if ( ! empty($matches[4]))
2410 {
2411 $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4]));
2412 $matches[4] = ' '.$matches[4];
2413 }
2414
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002415 $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2]))
2416 .' '.trim($matches[3]).$matches[4].$matches[5];
Andrey Andreev6e704752012-07-18 00:46:33 +03002417 }
2418
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002419 $this->{$qb_key}[$i] = implode('', $conditions);
Andrey Andreev6e704752012-07-18 00:46:33 +03002420 }
2421
Andrey Andreev9d3aa1b2012-10-12 12:14:09 +03002422 return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ")
2423 .implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002424 }
2425
Andrey Andreevb0478652012-07-18 15:34:46 +03002426 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002427 }
2428
2429 // --------------------------------------------------------------------
2430
2431 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002432 * Compile GROUP BY
2433 *
2434 * Escapes identifiers in GROUP BY statements at execution time.
2435 *
2436 * Required so that aliases are tracked properly, regardless of wether
2437 * group_by() is called prior to from(), join() and dbprefix is added
2438 * only if needed.
2439 *
2440 * @return string SQL statement
2441 */
2442 protected function _compile_group_by()
2443 {
2444 if (count($this->qb_groupby) > 0)
2445 {
Andrey Andreev96feb582012-07-19 13:12:34 +03002446 for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
2447 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002448 // Is it already compiled?
Andrey Andreev18eba242014-01-23 22:52:31 +02002449 if (is_string($this->qb_groupby[$i]))
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002450 {
2451 continue;
2452 }
2453
Andrey Andreev082aa402012-10-22 19:41:55 +03002454 $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 +03002455 ? $this->qb_groupby[$i]['field']
Andrey Andreev13f50542012-10-12 12:31:02 +03002456 : $this->protect_identifiers($this->qb_groupby[$i]['field']);
Andrey Andreev96feb582012-07-19 13:12:34 +03002457 }
2458
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002459 return "\nGROUP BY ".implode(', ', $this->qb_groupby);
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002460 }
2461
2462 return '';
2463 }
2464
2465 // --------------------------------------------------------------------
2466
2467 /**
Andrey Andreev2d486232012-07-19 14:46:51 +03002468 * Compile ORDER BY
2469 *
2470 * Escapes identifiers in ORDER BY statements at execution time.
2471 *
2472 * Required so that aliases are tracked properly, regardless of wether
2473 * order_by() is called prior to from(), join() and dbprefix is added
2474 * only if needed.
2475 *
2476 * @return string SQL statement
2477 */
2478 protected function _compile_order_by()
2479 {
Andrey Andreeva53ea842012-10-23 12:44:09 +03002480 if (is_array($this->qb_orderby) && count($this->qb_orderby) > 0)
Andrey Andreev2d486232012-07-19 14:46:51 +03002481 {
Andrey Andreev2d486232012-07-19 14:46:51 +03002482 for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++)
2483 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002484 if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field']))
Andrey Andreev2d486232012-07-19 14:46:51 +03002485 {
Andrey Andreevfc043b32012-10-12 14:46:14 +03002486 $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']);
Andrey Andreev2d486232012-07-19 14:46:51 +03002487 }
2488
2489 $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction'];
2490 }
2491
Andrey Andreeva53ea842012-10-23 12:44:09 +03002492 return $this->qb_orderby = "\nORDER BY ".implode(', ', $this->qb_orderby);
2493 }
2494 elseif (is_string($this->qb_orderby))
2495 {
2496 return $this->qb_orderby;
Andrey Andreev2d486232012-07-19 14:46:51 +03002497 }
2498
2499 return '';
2500 }
2501
2502 // --------------------------------------------------------------------
2503
2504 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002505 * Object to Array
2506 *
2507 * Takes an object as input and converts the class variables to array key/vals
2508 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002509 * @param object
2510 * @return array
2511 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002512 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002513 {
2514 if ( ! is_object($object))
2515 {
2516 return $object;
2517 }
Barry Mienydd671972010-10-04 16:33:58 +02002518
Derek Allard2067d1a2008-11-13 22:59:24 +00002519 $array = array();
2520 foreach (get_object_vars($object) as $key => $val)
2521 {
2522 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002523 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002524 {
2525 $array[$key] = $val;
2526 }
2527 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002528
2529 return $array;
2530 }
Barry Mienydd671972010-10-04 16:33:58 +02002531
Derek Jonesd10e8962010-03-02 17:10:36 -06002532 // --------------------------------------------------------------------
2533
2534 /**
2535 * Object to Array
2536 *
2537 * Takes an object as input and converts the class variables to array key/vals
2538 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002539 * @param object
2540 * @return array
2541 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002542 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002543 {
2544 if ( ! is_object($object))
2545 {
2546 return $object;
2547 }
Barry Mienydd671972010-10-04 16:33:58 +02002548
Derek Jonesd10e8962010-03-02 17:10:36 -06002549 $array = array();
2550 $out = get_object_vars($object);
2551 $fields = array_keys($out);
2552
2553 foreach ($fields as $val)
2554 {
2555 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002556 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002557 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002558 $i = 0;
2559 foreach ($out[$val] as $data)
2560 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002561 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002562 }
2563 }
2564 }
2565
Derek Allard2067d1a2008-11-13 22:59:24 +00002566 return $array;
2567 }
Barry Mienydd671972010-10-04 16:33:58 +02002568
Derek Allard2067d1a2008-11-13 22:59:24 +00002569 // --------------------------------------------------------------------
2570
2571 /**
2572 * Start Cache
2573 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002574 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002575 *
Andrey Andreev4a587f52014-12-11 16:27:15 +02002576 * @return CI_DB_query_builder
Barry Mienydd671972010-10-04 16:33:58 +02002577 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002578 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002579 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002580 $this->qb_caching = TRUE;
Andrey Andreev4a587f52014-12-11 16:27:15 +02002581 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002582 }
2583
2584 // --------------------------------------------------------------------
2585
2586 /**
2587 * Stop Cache
2588 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002589 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002590 *
Andrey Andreev4a587f52014-12-11 16:27:15 +02002591 * @return CI_DB_query_builder
Barry Mienydd671972010-10-04 16:33:58 +02002592 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002593 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002594 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002595 $this->qb_caching = FALSE;
Andrey Andreev4a587f52014-12-11 16:27:15 +02002596 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002597 }
2598
2599 // --------------------------------------------------------------------
2600
2601 /**
2602 * Flush Cache
2603 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002604 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002605 *
Andrey Andreev4a587f52014-12-11 16:27:15 +02002606 * @return CI_DB_query_builder
Barry Mienydd671972010-10-04 16:33:58 +02002607 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002608 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002609 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002610 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002611 'qb_cache_select' => array(),
2612 'qb_cache_from' => array(),
2613 'qb_cache_join' => array(),
2614 'qb_cache_where' => array(),
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002615 'qb_cache_groupby' => array(),
2616 'qb_cache_having' => array(),
2617 'qb_cache_orderby' => array(),
2618 'qb_cache_set' => array(),
2619 'qb_cache_exists' => array(),
2620 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002621 ));
Andrey Andreev4a587f52014-12-11 16:27:15 +02002622
2623 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002624 }
2625
2626 // --------------------------------------------------------------------
2627
2628 /**
2629 * Merge Cache
2630 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002631 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002632 * locally called ones.
2633 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002634 * @return void
2635 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002636 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002637 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002638 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002639 {
2640 return;
2641 }
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002642 elseif (in_array('select', $this->qb_cache_exists, TRUE))
2643 {
2644 $qb_no_escape = $this->qb_cache_no_escape;
2645 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002646
GDmac17a05282013-11-11 13:18:09 +01002647 foreach (array_unique($this->qb_cache_exists) as $val) // select, from, etc.
Derek Allard2067d1a2008-11-13 22:59:24 +00002648 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002649 $qb_variable = 'qb_'.$val;
2650 $qb_cache_var = 'qb_cache_'.$val;
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002651 $qb_new = $this->$qb_cache_var;
GDmace1b86832013-11-08 16:52:54 +01002652
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002653 for ($i = 0, $c = count($this->$qb_variable); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00002654 {
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002655 if ( ! in_array($this->{$qb_variable}[$i], $qb_new, TRUE))
2656 {
2657 $qb_new[] = $this->{$qb_variable}[$i];
2658 if ($val === 'select')
2659 {
2660 $qb_no_escape[] = $this->qb_no_escape[$i];
2661 }
2662 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002663 }
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002664
GDmace1b86832013-11-08 16:52:54 +01002665 $this->$qb_variable = $qb_new;
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002666 if ($val === 'select')
2667 {
2668 $this->qb_no_escape = $qb_no_escape;
2669 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002670 }
2671
2672 // If we are "protecting identifiers" we need to examine the "from"
2673 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002674 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002675 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002676 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002677 }
2678 }
WanWizard7219c072011-12-28 14:09:05 +01002679
Kyle Farris0c147b32011-08-26 02:29:31 -04002680 // --------------------------------------------------------------------
2681
2682 /**
Andrey Andreev082aa402012-10-22 19:41:55 +03002683 * Is literal
2684 *
2685 * Determines if a string represents a literal value or a field name
2686 *
Andrey Andreev02e4cd72012-11-13 11:50:47 +02002687 * @param string $str
Andrey Andreev082aa402012-10-22 19:41:55 +03002688 * @return bool
2689 */
2690 protected function _is_literal($str)
2691 {
2692 $str = trim($str);
2693
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002694 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 +03002695 {
2696 return TRUE;
2697 }
2698
2699 static $_str;
2700
2701 if (empty($_str))
2702 {
2703 $_str = ($this->_escape_char !== '"')
2704 ? array('"', "'") : array("'");
2705 }
2706
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002707 return in_array($str[0], $_str, TRUE);
Andrey Andreev082aa402012-10-22 19:41:55 +03002708 }
2709
2710 // --------------------------------------------------------------------
2711
2712 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002713 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002714 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002715 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002716 *
Andrey Andreev435e0c22014-12-11 16:30:13 +02002717 * @return CI_DB_query_builder
Kyle Farris0c147b32011-08-26 02:29:31 -04002718 */
2719 public function reset_query()
2720 {
2721 $this->_reset_select();
2722 $this->_reset_write();
Andrey Andreev435e0c22014-12-11 16:30:13 +02002723 return $this;
Kyle Farris0c147b32011-08-26 02:29:31 -04002724 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002725
2726 // --------------------------------------------------------------------
2727
2728 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002729 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002730 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002731 * @param array An array of fields to reset
2732 * @return void
2733 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002734 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002735 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002736 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002737 {
Andrey Andreevae85eb42012-11-02 01:42:31 +02002738 $this->$item = $default_value;
Derek Allard2067d1a2008-11-13 22:59:24 +00002739 }
2740 }
2741
2742 // --------------------------------------------------------------------
2743
2744 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002745 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002746 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002747 * @return void
2748 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002749 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002750 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002751 $this->_reset_run(array(
Andrey Andreev4a587f52014-12-11 16:27:15 +02002752 'qb_select' => array(),
2753 'qb_from' => array(),
2754 'qb_join' => array(),
2755 'qb_where' => array(),
2756 'qb_groupby' => array(),
2757 'qb_having' => array(),
2758 'qb_orderby' => array(),
2759 'qb_aliased_tables' => array(),
2760 'qb_no_escape' => array(),
2761 'qb_distinct' => FALSE,
2762 'qb_limit' => FALSE,
2763 'qb_offset' => FALSE
2764 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002765 }
Barry Mienydd671972010-10-04 16:33:58 +02002766
Derek Allard2067d1a2008-11-13 22:59:24 +00002767 // --------------------------------------------------------------------
2768
2769 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002770 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002771 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002772 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002773 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002774 * @return void
2775 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002776 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002777 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002778 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002779 'qb_set' => array(),
2780 'qb_from' => array(),
Andrey Andreev3e014372013-02-21 15:59:34 +02002781 'qb_join' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002782 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002783 'qb_orderby' => array(),
2784 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002785 'qb_limit' => FALSE
Andrey Andreev4a587f52014-12-11 16:27:15 +02002786 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002787 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002788
Derek Allard2067d1a2008-11-13 22:59:24 +00002789}