blob: 6b0e0327482fc3ef3efa7169b5f1704b86504158 [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 *
Master Yodada60e9b2016-12-31 08:46:18 -08009 * Copyright (c) 2014 - 2017, 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/)
Master Yodada60e9b2016-12-31 08:46:18 -080032 * @copyright Copyright (c) 2014 - 2017, 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 /**
Andrey Andreev8338bbb2016-12-12 14:17:52 +0200153 * QB data set for update_batch()
154 *
155 * @var array
156 */
157 protected $qb_set_ub = array();
158
159 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200160 * QB aliased tables list
161 *
162 * @var array
163 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000164 protected $qb_aliased_tables = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200165
166 /**
167 * QB WHERE group started flag
168 *
169 * @var bool
170 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000171 protected $qb_where_group_started = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200172
173 /**
174 * QB WHERE group count
175 *
176 * @var int
177 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000178 protected $qb_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +0200179
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000180 // Query Builder Caching variables
Andrey Andreevae85eb42012-11-02 01:42:31 +0200181
182 /**
183 * QB Caching flag
184 *
185 * @var bool
186 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000187 protected $qb_caching = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200188
189 /**
190 * QB Cache exists list
191 *
192 * @var array
193 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000194 protected $qb_cache_exists = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200195
196 /**
197 * QB Cache SELECT data
198 *
199 * @var array
200 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000201 protected $qb_cache_select = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200202
203 /**
204 * QB Cache FROM data
205 *
206 * @var array
207 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000208 protected $qb_cache_from = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200209
210 /**
211 * QB Cache JOIN data
212 *
213 * @var array
214 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000215 protected $qb_cache_join = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200216
217 /**
218 * QB Cache WHERE data
219 *
220 * @var array
221 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000222 protected $qb_cache_where = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200223
224 /**
225 * QB Cache GROUP BY data
226 *
227 * @var array
228 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000229 protected $qb_cache_groupby = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200230
231 /**
232 * QB Cache HAVING data
233 *
234 * @var array
235 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000236 protected $qb_cache_having = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200237
238 /**
239 * QB Cache ORDER BY data
240 *
241 * @var array
242 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000243 protected $qb_cache_orderby = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200244
245 /**
246 * QB Cache data sets
247 *
248 * @var array
249 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000250 protected $qb_cache_set = array();
WanWizard7219c072011-12-28 14:09:05 +0100251
Andrey Andreevae85eb42012-11-02 01:42:31 +0200252 /**
253 * QB No Escape data
254 *
255 * @var array
256 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000257 protected $qb_no_escape = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200258
259 /**
260 * QB Cache No Escape data
261 *
262 * @var array
263 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000264 protected $qb_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000265
Andrey Andreevae85eb42012-11-02 01:42:31 +0200266 // --------------------------------------------------------------------
267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 /**
269 * Select
270 *
271 * Generates the SELECT portion of the query
272 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300274 * @param mixed
Andrew Podner4296a652012-12-17 07:51:15 -0500275 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600277 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 if (is_string($select))
280 {
281 $select = explode(',', $select);
282 }
283
Vivek Dinesh33578d22014-02-06 09:46:07 +0530284 // If the escape value was not set, we will base it on the global setting
Andrey Andreev42870232012-06-12 01:30:20 +0300285 is_bool($escape) OR $escape = $this->_protect_identifiers;
286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 foreach ($select as $val)
288 {
289 $val = trim($val);
290
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100291 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000293 $this->qb_select[] = $val;
294 $this->qb_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000295
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000296 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000298 $this->qb_cache_select[] = $val;
299 $this->qb_cache_exists[] = 'select';
300 $this->qb_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
302 }
303 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 return $this;
306 }
307
308 // --------------------------------------------------------------------
309
310 /**
311 * Select Max
312 *
313 * Generates a SELECT MAX(field) portion of a query
314 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 * @param string the field
316 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500317 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600319 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 return $this->_max_min_avg_sum($select, $alias, 'MAX');
322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 // --------------------------------------------------------------------
325
326 /**
327 * Select Min
328 *
329 * Generates a SELECT MIN(field) portion of a query
330 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 * @param string the field
332 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500333 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600335 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
337 return $this->_max_min_avg_sum($select, $alias, 'MIN');
338 }
339
340 // --------------------------------------------------------------------
341
342 /**
343 * Select Average
344 *
345 * Generates a SELECT AVG(field) portion of a query
346 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 * @param string the field
348 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500349 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600351 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
353 return $this->_max_min_avg_sum($select, $alias, 'AVG');
354 }
355
356 // --------------------------------------------------------------------
357
358 /**
359 * Select Sum
360 *
361 * Generates a SELECT SUM(field) portion of a query
362 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 * @param string the field
364 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500365 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600367 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
369 return $this->_max_min_avg_sum($select, $alias, 'SUM');
370 }
371
372 // --------------------------------------------------------------------
373
374 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200375 * SELECT [MAX|MIN|AVG|SUM]()
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200377 * @used-by select_max()
378 * @used-by select_min()
379 * @used-by select_avg()
380 * @used-by select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200381 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200382 * @param string $select Field name
383 * @param string $alias
384 * @param string $type
Andrew Podner4296a652012-12-17 07:51:15 -0500385 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600387 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100389 if ( ! is_string($select) OR $select === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
391 $this->display_error('db_invalid_query');
392 }
Barry Mienydd671972010-10-04 16:33:58 +0200393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
397 {
398 show_error('Invalid function type: '.$type);
399 }
Barry Mienydd671972010-10-04 16:33:58 +0200400
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100401 if ($alias === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
403 $alias = $this->_create_alias_from_table(trim($select));
404 }
Barry Mienydd671972010-10-04 16:33:58 +0200405
Andrey Andreev5b55c152013-08-06 14:14:32 +0300406 $sql = $type.'('.$this->protect_identifiers(trim($select)).') AS '.$this->escape_identifiers(trim($alias));
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300407
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000408 $this->qb_select[] = $sql;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100409 $this->qb_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200410
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000411 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000413 $this->qb_cache_select[] = $sql;
414 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
Barry Mienydd671972010-10-04 16:33:58 +0200416
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 return $this;
418 }
419
420 // --------------------------------------------------------------------
421
422 /**
423 * Determines the alias name based on the table
424 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200425 * @param string $item
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 * @return string
427 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600428 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
430 if (strpos($item, '.') !== FALSE)
431 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200432 $item = explode('.', $item);
433 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 return $item;
437 }
438
439 // --------------------------------------------------------------------
440
441 /**
442 * DISTINCT
443 *
444 * Sets a flag which tells the query string compiler to add DISTINCT
445 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200446 * @param bool $val
Andrew Podner4296a652012-12-17 07:51:15 -0500447 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600449 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300451 $this->qb_distinct = is_bool($val) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 return $this;
453 }
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 // --------------------------------------------------------------------
456
457 /**
458 * From
459 *
460 * Generates the FROM portion of the query
461 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200462 * @param mixed $from can be a string or array
Andrew Podner4296a652012-12-17 07:51:15 -0500463 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600465 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300467 foreach ((array) $from as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
469 if (strpos($val, ',') !== FALSE)
470 {
471 foreach (explode(',', $val) as $v)
472 {
473 $v = trim($v);
474 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200475
George Petsagourakis193d4482012-04-28 11:16:18 +0300476 $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000477
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000478 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000480 $this->qb_cache_from[] = $v;
481 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200482 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
485 else
486 {
487 $val = trim($val);
488
Andrey Andreev24276a32012-01-08 02:44:38 +0200489 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000490 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200492
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000493 $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000494
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000495 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000497 $this->qb_cache_from[] = $val;
498 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 }
500 }
501 }
502
503 return $this;
504 }
505
506 // --------------------------------------------------------------------
507
508 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200509 * JOIN
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 *
511 * Generates the JOIN portion of the query
512 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 * @param string
514 * @param string the join condition
515 * @param string the type of join
Alex Bilbief512b732012-06-16 11:15:19 +0100516 * @param string whether not to try to escape identifiers
Andrew Podner4296a652012-12-17 07:51:15 -0500517 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300519 public function join($table, $cond, $type = '', $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200520 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100521 if ($type !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 {
523 $type = strtoupper(trim($type));
524
Andrey Andreev42870232012-06-12 01:30:20 +0300525 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 {
527 $type = '';
528 }
529 else
530 {
531 $type .= ' ';
532 }
533 }
534
Andrey Andreev24276a32012-01-08 02:44:38 +0200535 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000536 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 $this->_track_aliases($table);
538
Andrey Andreevfe642da2012-06-16 03:47:33 +0300539 is_bool($escape) OR $escape = $this->_protect_identifiers;
540
Andrey Andreevc6011942016-02-11 23:49:37 +0200541 if ( ! $this->_has_operator($cond))
Andrey Andreev3751f932012-06-17 18:07:48 +0300542 {
543 $cond = ' USING ('.($escape ? $this->escape_identifiers($cond) : $cond).')';
544 }
Andrey Andreevc6011942016-02-11 23:49:37 +0200545 elseif ($escape === FALSE)
Andrey Andreev3751f932012-06-17 18:07:48 +0300546 {
547 $cond = ' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 }
Andrey Andreevc6011942016-02-11 23:49:37 +0200549 else
550 {
551 // Split multiple conditions
552 if (preg_match_all('/\sAND\s|\sOR\s/i', $cond, $joints, PREG_OFFSET_CAPTURE))
553 {
554 $conditions = array();
555 $joints = $joints[0];
556 array_unshift($joints, array('', 0));
557
558 for ($i = count($joints) - 1, $pos = strlen($cond); $i >= 0; $i--)
559 {
560 $joints[$i][1] += strlen($joints[$i][0]); // offset
561 $conditions[$i] = substr($cond, $joints[$i][1], $pos - $joints[$i][1]);
562 $pos = $joints[$i][1] - strlen($joints[$i][0]);
563 $joints[$i] = $joints[$i][0];
564 }
565 }
566 else
567 {
568 $conditions = array($cond);
569 $joints = array('');
570 }
571
572 $cond = ' ON ';
573 for ($i = 0, $c = count($conditions); $i < $c; $i++)
574 {
575 $operator = $this->_get_operator($conditions[$i]);
576 $cond .= $joints[$i];
577 $cond .= preg_match("/(\(*)?([\[\]\w\.'-]+)".preg_quote($operator)."(.*)/i", $conditions[$i], $match)
578 ? $match[1].$this->protect_identifiers($match[2]).$operator.$this->protect_identifiers($match[3])
579 : $conditions[$i];
580 }
581 }
Barry Mienydd671972010-10-04 16:33:58 +0200582
Andrey Andreev42870232012-06-12 01:30:20 +0300583 // Do we want to escape the table name?
584 if ($escape === TRUE)
585 {
586 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
587 }
588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 // Assemble the JOIN statement
Andrey Andreev3751f932012-06-17 18:07:48 +0300590 $this->qb_join[] = $join = $type.'JOIN '.$table.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000591
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000592 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000594 $this->qb_cache_join[] = $join;
595 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 }
597
598 return $this;
599 }
600
601 // --------------------------------------------------------------------
602
603 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200604 * WHERE
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200606 * Generates the WHERE portion of the query.
607 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 * @param mixed
610 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300611 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500612 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300614 public function where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300616 return $this->_wh('qb_where', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 }
Barry Mienydd671972010-10-04 16:33:58 +0200618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 // --------------------------------------------------------------------
620
621 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200622 * OR WHERE
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200624 * Generates the WHERE portion of the query.
625 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 * @param mixed
628 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300629 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500630 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300632 public function or_where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300634 return $this->_wh('qb_where', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 }
636
637 // --------------------------------------------------------------------
638
639 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +0300640 * WHERE, HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200642 * @used-by where()
643 * @used-by or_where()
644 * @used-by having()
645 * @used-by or_having()
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200647 * @param string $qb_key 'qb_where' or 'qb_having'
648 * @param mixed $key
649 * @param mixed $value
650 * @param string $type
651 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500652 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 */
Andrey Andreevd40459d2012-07-18 16:46:39 +0300654 protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300656 $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where';
657
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 if ( ! is_array($key))
659 {
660 $key = array($key => $value);
661 }
Barry Mienydd671972010-10-04 16:33:58 +0200662
Rougin Royce Gutib191550a2014-08-24 16:19:08 +0800663 // If the escape value was not set will base it on the global setting
Andrey Andreeve10fb792012-06-15 12:07:04 +0300664 is_bool($escape) OR $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000665
666 foreach ($key as $k => $v)
667 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300668 $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0)
Andrey Andreev58803fb2012-06-24 00:45:37 +0300669 ? $this->_group_get_type('')
670 : $this->_group_get_type($type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000671
vlakoff1228fe22013-01-14 01:30:09 +0100672 if ($v !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 {
Andrey Andreevf7631f12015-07-27 21:54:41 +0300674 if ($escape === TRUE)
675 {
676 $v = ' '.$this->escape($v);
677 }
WanWizard7219c072011-12-28 14:09:05 +0100678
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 if ( ! $this->_has_operator($k))
680 {
Greg Akere156c6e2011-04-20 16:03:04 -0500681 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 }
683 }
Andrey Andreev3a5efc22012-11-20 21:18:08 +0200684 elseif ( ! $this->_has_operator($k))
685 {
686 // value appears not to have been set, assign the test to IS NULL
687 $k .= ' IS NULL';
688 }
Andrey Andreevbe4bab92016-10-28 12:50:03 +0300689 elseif (preg_match('/\s*(!?=|<>|\sIS(?:\s+NOT)?\s)\s*$/i', $k, $match, PREG_OFFSET_CAPTURE))
Andrey Andreev5bf4dcd2014-09-29 20:07:15 +0300690 {
691 $k = substr($k, 0, $match[0][1]).($match[1][0] === '=' ? ' IS NULL' : ' IS NOT NULL');
692 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000693
Andrey Andreevd40459d2012-07-18 16:46:39 +0300694 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000695 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300697 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
698 $this->qb_cache_exists[] = substr($qb_key, 3);
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 }
Barry Mienydd671972010-10-04 16:33:58 +0200700
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 }
Barry Mienydd671972010-10-04 16:33:58 +0200702
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 return $this;
704 }
705
706 // --------------------------------------------------------------------
707
708 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200709 * WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200711 * Generates a WHERE field IN('item', 'item') SQL query,
712 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200714 * @param string $key The field to search
715 * @param array $values The values searched on
716 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500717 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300719 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300721 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 }
Barry Mienydd671972010-10-04 16:33:58 +0200723
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 // --------------------------------------------------------------------
725
726 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200727 * OR WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200729 * Generates a WHERE field IN('item', 'item') SQL query,
730 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200732 * @param string $key The field to search
733 * @param array $values The values searched on
734 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500735 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300737 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300739 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 }
741
742 // --------------------------------------------------------------------
743
744 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200745 * WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200747 * Generates a WHERE field NOT IN('item', 'item') SQL query,
748 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200750 * @param string $key The field to search
751 * @param array $values The values searched on
752 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500753 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300755 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300757 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 }
Barry Mienydd671972010-10-04 16:33:58 +0200759
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 // --------------------------------------------------------------------
761
762 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200763 * OR WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200765 * Generates a WHERE field NOT IN('item', 'item') SQL query,
766 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200768 * @param string $key The field to search
769 * @param array $values The values searched on
770 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500771 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300773 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300775 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 }
777
778 // --------------------------------------------------------------------
779
780 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200781 * Internal WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200783 * @used-by where_in()
784 * @used-by or_where_in()
785 * @used-by where_not_in()
786 * @used-by or_where_not_in()
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200788 * @param string $key The field to search
789 * @param array $values The values searched on
790 * @param bool $not If the statement would be IN or NOT IN
791 * @param string $type
792 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500793 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300795 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 {
797 if ($key === NULL OR $values === NULL)
798 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300799 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 }
Barry Mienydd671972010-10-04 16:33:58 +0200801
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 if ( ! is_array($values))
803 {
804 $values = array($values);
805 }
Barry Mienydd671972010-10-04 16:33:58 +0200806
Andrey Andreev498c1e02012-06-16 03:34:10 +0300807 is_bool($escape) OR $escape = $this->_protect_identifiers;
808
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 $not = ($not) ? ' NOT' : '';
810
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300811 if ($escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 {
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300813 $where_in = array();
814 foreach ($values as $value)
815 {
816 $where_in[] = $this->escape($value);
817 }
818 }
819 else
820 {
821 $where_in = array_values($values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 }
823
Andrey Andreev0d2d84f2015-07-31 13:48:59 +0300824 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
825 ? $this->_group_get_type('')
826 : $this->_group_get_type($type);
827
Andrey Andreev6e704752012-07-18 00:46:33 +0300828 $where_in = array(
Andrey Andreev94611df2012-07-19 12:29:54 +0300829 'condition' => $prefix.$key.$not.' IN('.implode(', ', $where_in).')',
Andrey Andreev6e704752012-07-18 00:46:33 +0300830 'escape' => $escape
831 );
Barry Mienydd671972010-10-04 16:33:58 +0200832
Andrey Andreev6e704752012-07-18 00:46:33 +0300833 $this->qb_where[] = $where_in;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000834 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000836 $this->qb_cache_where[] = $where_in;
837 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 }
839
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 return $this;
841 }
Barry Mienydd671972010-10-04 16:33:58 +0200842
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 // --------------------------------------------------------------------
844
845 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200846 * LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200848 * Generates a %LIKE% portion of the query.
849 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200851 * @param mixed $field
852 * @param string $match
853 * @param string $side
854 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500855 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300857 public function like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300859 return $this->_like($field, $match, 'AND ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 }
861
862 // --------------------------------------------------------------------
863
864 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200865 * NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200867 * Generates a NOT LIKE portion of the query.
868 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200870 * @param mixed $field
871 * @param string $match
872 * @param string $side
873 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500874 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300876 public function not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300878 return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 }
Barry Mienydd671972010-10-04 16:33:58 +0200880
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 // --------------------------------------------------------------------
882
883 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200884 * OR LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200886 * Generates a %LIKE% portion of the query.
887 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200889 * @param mixed $field
890 * @param string $match
891 * @param string $side
892 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500893 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300895 public function or_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300897 return $this->_like($field, $match, 'OR ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 }
899
900 // --------------------------------------------------------------------
901
902 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200903 * OR NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200905 * Generates a NOT LIKE portion of the query.
906 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200908 * @param mixed $field
909 * @param string $match
910 * @param string $side
911 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500912 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300914 public function or_not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300916 return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 }
Barry Mienydd671972010-10-04 16:33:58 +0200918
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 // --------------------------------------------------------------------
920
921 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200922 * Internal LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200924 * @used-by like()
925 * @used-by or_like()
926 * @used-by not_like()
927 * @used-by or_not_like()
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200929 * @param mixed $field
930 * @param string $match
931 * @param string $type
932 * @param string $side
933 * @param string $not
934 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500935 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300937 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 {
939 if ( ! is_array($field))
940 {
941 $field = array($field => $match);
942 }
Barry Mienydd671972010-10-04 16:33:58 +0200943
Andrey Andreevb0478652012-07-18 15:34:46 +0300944 is_bool($escape) OR $escape = $this->_protect_identifiers;
Andrey Andreev19311362015-04-07 00:02:14 +0300945 // lowercase $side in case somebody writes e.g. 'BEFORE' instead of 'before' (doh)
946 $side = strtolower($side);
Andrey Andreevb0478652012-07-18 15:34:46 +0300947
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 foreach ($field as $k => $v)
949 {
Andrey Andreev41738232012-11-30 00:13:17 +0200950 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
951 ? $this->_group_get_type('') : $this->_group_get_type($type);
952
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300953 if ($escape === TRUE)
954 {
955 $v = $this->escape_like_str($v);
956 }
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300957
Andrey Andreev24276a32012-01-08 02:44:38 +0200958 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400959 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300960 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400961 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200962 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300964 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200966 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300968 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 }
970 else
971 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300972 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600974
Derek Jonese4ed5832009-02-20 21:44:59 +0000975 // some platforms require an escape sequence definition for LIKE wildcards
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300976 if ($escape === TRUE && $this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000977 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300978 $like_statement .= sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000979 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600980
Andrey Andreevb0478652012-07-18 15:34:46 +0300981 $this->qb_where[] = array('condition' => $like_statement, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000982 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 {
Andrey Andreev55bbd722013-01-28 19:02:13 +0200984 $this->qb_cache_where[] = array('condition' => $like_statement, 'escape' => $escape);
Andrey Andreevededc4a2012-07-18 01:16:15 +0300985 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200988
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 return $this;
990 }
Barry Mienydd671972010-10-04 16:33:58 +0200991
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 // --------------------------------------------------------------------
993
994 /**
WanWizard7219c072011-12-28 14:09:05 +0100995 * Starts a query group.
996 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200997 * @param string $not (Internal use only)
998 * @param string $type (Internal use only)
Andrew Podner4296a652012-12-17 07:51:15 -0500999 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001000 */
1001 public function group_start($not = '', $type = 'AND ')
1002 {
1003 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +01001004
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001005 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001006 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Andrey Andreev6e704752012-07-18 00:46:33 +03001007 $where = array(
1008 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (',
1009 'escape' => FALSE
1010 );
WanWizard7219c072011-12-28 14:09:05 +01001011
Andrey Andreev6e704752012-07-18 00:46:33 +03001012 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001013 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +01001014 {
Andrey Andreev6e704752012-07-18 00:46:33 +03001015 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +01001016 }
1017
1018 return $this;
1019 }
1020
1021 // --------------------------------------------------------------------
1022
1023 /**
1024 * Starts a query group, but ORs the group
1025 *
Andrew Podner4296a652012-12-17 07:51:15 -05001026 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001027 */
1028 public function or_group_start()
1029 {
1030 return $this->group_start('', 'OR ');
1031 }
1032
1033 // --------------------------------------------------------------------
1034
1035 /**
1036 * Starts a query group, but NOTs the group
1037 *
Andrew Podner4296a652012-12-17 07:51:15 -05001038 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001039 */
1040 public function not_group_start()
1041 {
1042 return $this->group_start('NOT ', 'AND ');
1043 }
1044
1045 // --------------------------------------------------------------------
1046
1047 /**
1048 * Starts a query group, but OR NOTs the group
1049 *
Andrew Podner4296a652012-12-17 07:51:15 -05001050 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001051 */
1052 public function or_not_group_start()
1053 {
1054 return $this->group_start('NOT ', 'OR ');
1055 }
1056
1057 // --------------------------------------------------------------------
1058
1059 /**
1060 * Ends a query group
1061 *
Andrew Podner4296a652012-12-17 07:51:15 -05001062 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001063 */
1064 public function group_end()
1065 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001066 $this->qb_where_group_started = FALSE;
Andrey Andreev6e704752012-07-18 00:46:33 +03001067 $where = array(
1068 'condition' => str_repeat(' ', $this->qb_where_group_count--).')',
1069 'escape' => FALSE
1070 );
WanWizard7219c072011-12-28 14:09:05 +01001071
Andrey Andreev6e704752012-07-18 00:46:33 +03001072 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001073 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +01001074 {
Andrey Andreev6e704752012-07-18 00:46:33 +03001075 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +01001076 }
1077
WanWizard7219c072011-12-28 14:09:05 +01001078 return $this;
1079 }
1080
1081 // --------------------------------------------------------------------
1082
1083 /**
1084 * Group_get_type
1085 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001086 * @used-by group_start()
1087 * @used-by _like()
1088 * @used-by _wh()
1089 * @used-by _where_in()
WanWizard7219c072011-12-28 14:09:05 +01001090 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001091 * @param string $type
WanWizard7219c072011-12-28 14:09:05 +01001092 * @return string
1093 */
1094 protected function _group_get_type($type)
1095 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001096 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +01001097 {
1098 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001099 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +01001100 }
1101
1102 return $type;
1103 }
1104
1105 // --------------------------------------------------------------------
1106
1107 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 * GROUP BY
1109 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001110 * @param string $by
1111 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -05001112 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001114 public function group_by($by, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001116 is_bool($escape) OR $escape = $this->_protect_identifiers;
1117
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 if (is_string($by))
1119 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001120 $by = ($escape === TRUE)
1121 ? explode(',', $by)
1122 : array($by);
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 }
Barry Mienydd671972010-10-04 16:33:58 +02001124
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 foreach ($by as $val)
1126 {
1127 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +02001128
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001129 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001131 $val = array('field' => $val, 'escape' => $escape);
Barry Mienydd671972010-10-04 16:33:58 +02001132
Andrey Andreev96feb582012-07-19 13:12:34 +03001133 $this->qb_groupby[] = $val;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001134 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001136 $this->qb_cache_groupby[] = $val;
1137 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 }
1139 }
1140 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001141
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 return $this;
1143 }
1144
1145 // --------------------------------------------------------------------
1146
1147 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001148 * HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001150 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001152 * @param string $key
1153 * @param string $value
1154 * @param bool $escape
Andrey Andreevaec51262016-02-09 21:11:07 +02001155 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001157 public function having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001159 return $this->_wh('qb_having', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 }
Barry Mienydd671972010-10-04 16:33:58 +02001161
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 // --------------------------------------------------------------------
1163
1164 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001165 * OR HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001167 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001169 * @param string $key
1170 * @param string $value
1171 * @param bool $escape
Andrey Andreevaec51262016-02-09 21:11:07 +02001172 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001174 public function or_having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001176 return $this->_wh('qb_having', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 }
Barry Mienydd671972010-10-04 16:33:58 +02001178
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 // --------------------------------------------------------------------
1180
1181 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001182 * ORDER BY
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001184 * @param string $orderby
vlakoff9f8e2992013-09-08 14:05:04 +02001185 * @param string $direction ASC, DESC or RANDOM
Andrey Andreevae85eb42012-11-02 01:42:31 +02001186 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -05001187 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 */
Andrey Andreevd24160c2012-06-16 03:21:20 +03001189 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001191 $direction = strtoupper(trim($direction));
Andrey Andreev2d486232012-07-19 14:46:51 +03001192
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001193 if ($direction === 'RANDOM')
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001195 $direction = '';
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001196
1197 // Do we have a seed value?
1198 $orderby = ctype_digit((string) $orderby)
vlakoffe6c4d5b2013-09-08 13:54:57 +02001199 ? sprintf($this->_random_keyword[1], $orderby)
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001200 : $this->_random_keyword[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 }
Andrey Andreev2d486232012-07-19 14:46:51 +03001202 elseif (empty($orderby))
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001204 return $this;
1205 }
1206 elseif ($direction !== '')
1207 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001208 $direction = in_array($direction, array('ASC', 'DESC'), TRUE) ? ' '.$direction : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001209 }
Barry Mienydd671972010-10-04 16:33:58 +02001210
Andrey Andreevd24160c2012-06-16 03:21:20 +03001211 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +02001212
Andrey Andreev2d486232012-07-19 14:46:51 +03001213 if ($escape === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 {
Andrey Andreev93dd2f22012-10-24 10:09:18 +03001215 $qb_orderby[] = array('field' => $orderby, 'direction' => $direction, 'escape' => FALSE);
Andrey Andreev2d486232012-07-19 14:46:51 +03001216 }
1217 else
1218 {
1219 $qb_orderby = array();
1220 foreach (explode(',', $orderby) as $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001222 $qb_orderby[] = ($direction === '' && preg_match('/\s+(ASC|DESC)$/i', rtrim($field), $match, PREG_OFFSET_CAPTURE))
1223 ? array('field' => ltrim(substr($field, 0, $match[0][1])), 'direction' => ' '.$match[1][0], 'escape' => TRUE)
1224 : array('field' => trim($field), 'direction' => $direction, 'escape' => TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001225 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 }
Barry Mienydd671972010-10-04 16:33:58 +02001227
Andrey Andreev2d486232012-07-19 14:46:51 +03001228 $this->qb_orderby = array_merge($this->qb_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001229 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001231 $this->qb_cache_orderby = array_merge($this->qb_cache_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001232 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 }
1234
1235 return $this;
1236 }
Barry Mienydd671972010-10-04 16:33:58 +02001237
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 // --------------------------------------------------------------------
1239
1240 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001241 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001243 * @param int $value LIMIT value
1244 * @param int $offset OFFSET value
Andrew Podner4296a652012-12-17 07:51:15 -05001245 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 */
Andrey Andreev4a587f52014-12-11 16:27:15 +02001247 public function limit($value, $offset = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001248 {
vlakoff912f1bc2013-01-15 03:34:12 +01001249 is_null($value) OR $this->qb_limit = (int) $value;
Andrey Andreev777153d2012-06-18 13:30:45 +03001250 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001251
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 return $this;
1253 }
Barry Mienydd671972010-10-04 16:33:58 +02001254
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 // --------------------------------------------------------------------
1256
1257 /**
1258 * Sets the OFFSET value
1259 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001260 * @param int $offset OFFSET value
Andrew Podner4296a652012-12-17 07:51:15 -05001261 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001263 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001265 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001266 return $this;
1267 }
Barry Mienydd671972010-10-04 16:33:58 +02001268
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 // --------------------------------------------------------------------
1270
1271 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001272 * LIMIT string
Andrey Andreev2c35b642012-06-24 03:05:26 +03001273 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001274 * Generates a platform-specific LIMIT clause.
Andrey Andreev2c35b642012-06-24 03:05:26 +03001275 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001276 * @param string $sql SQL Query
Andrey Andreev2c35b642012-06-24 03:05:26 +03001277 * @return string
1278 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001279 protected function _limit($sql)
Andrey Andreev2c35b642012-06-24 03:05:26 +03001280 {
Andrey Andreevacc64812016-07-29 11:42:28 +03001281 return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').(int) $this->qb_limit;
Andrey Andreev2c35b642012-06-24 03:05:26 +03001282 }
1283
1284 // --------------------------------------------------------------------
1285
1286 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001287 * The "set" function.
1288 *
1289 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001290 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001291 * @param mixed
1292 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001293 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001294 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001296 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 {
1298 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001299
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 if ( ! is_array($key))
1301 {
1302 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001303 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001304
Andrey Andreevfe642da2012-06-16 03:47:33 +03001305 is_bool($escape) OR $escape = $this->_protect_identifiers;
1306
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 foreach ($key as $k => $v)
1308 {
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001309 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1310 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001311 }
Barry Mienydd671972010-10-04 16:33:58 +02001312
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 return $this;
1314 }
WanWizard7219c072011-12-28 14:09:05 +01001315
Kyle Farris0c147b32011-08-26 02:29:31 -04001316 // --------------------------------------------------------------------
1317
1318 /**
1319 * Get SELECT query string
1320 *
1321 * Compiles a SELECT query string and returns the sql.
1322 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001323 * @param string the table name to select from (optional)
Calvin Tam55bc5052015-07-24 02:27:24 -07001324 * @param bool TRUE: resets QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001325 * @return string
1326 */
WanWizard7219c072011-12-28 14:09:05 +01001327 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001328 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001329 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001330 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001331 $this->_track_aliases($table);
1332 $this->from($table);
1333 }
WanWizard7219c072011-12-28 14:09:05 +01001334
Andrey Andreev650b4c02012-06-11 12:07:15 +03001335 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001336
Kyle Farris0c147b32011-08-26 02:29:31 -04001337 if ($reset === TRUE)
1338 {
1339 $this->_reset_select();
1340 }
WanWizard7219c072011-12-28 14:09:05 +01001341
Kyle Farris0c147b32011-08-26 02:29:31 -04001342 return $select;
1343 }
WanWizard7219c072011-12-28 14:09:05 +01001344
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 // --------------------------------------------------------------------
1346
1347 /**
1348 * Get
1349 *
1350 * Compiles the select statement based on the other functions called
1351 * and runs the query
1352 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 * @param string the table
1354 * @param string the limit clause
1355 * @param string the offset clause
Andrey Andreevaec51262016-02-09 21:11:07 +02001356 * @return CI_DB_result
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001358 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001360 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 {
1362 $this->_track_aliases($table);
1363 $this->from($table);
1364 }
Barry Mienydd671972010-10-04 16:33:58 +02001365
Andrey Andreev650b4c02012-06-11 12:07:15 +03001366 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 {
1368 $this->limit($limit, $offset);
1369 }
Barry Mienydd671972010-10-04 16:33:58 +02001370
Andrey Andreev24276a32012-01-08 02:44:38 +02001371 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001372 $this->_reset_select();
1373 return $result;
1374 }
1375
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001376 // --------------------------------------------------------------------
1377
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 /**
1379 * "Count All Results" query
1380 *
Barry Mienydd671972010-10-04 16:33:58 +02001381 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001382 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001383 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 * @param string
yaoshanliang2f164052015-03-16 16:48:15 +08001385 * @param bool the reset clause
vlakoffc6ac7482013-11-17 00:50:06 +01001386 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +00001387 */
yaoshanliang19c28472015-03-15 10:42:18 +08001388 public function count_all_results($table = '', $reset = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001389 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001390 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001391 {
1392 $this->_track_aliases($table);
1393 $this->from($table);
1394 }
Barry Mienydd671972010-10-04 16:33:58 +02001395
Andrey Andreev075bdb42016-01-25 13:31:23 +02001396 // ORDER BY usage is often problematic here (most notably
1397 // on Microsoft SQL Server) and ultimately unnecessary
1398 // for selecting COUNT(*) ...
1399 if ( ! empty($this->qb_orderby))
1400 {
1401 $orderby = $this->qb_orderby;
1402 $this->qb_orderby = NULL;
1403 }
1404
Andrey Andreev2cae5582017-01-03 13:18:27 +02001405 $result = ($this->qb_distinct === TRUE OR ! empty($this->qb_groupby) OR ! empty($this->qb_cache_groupby))
Andrey Andreevb05f5062012-10-26 12:01:02 +03001406 ? $this->query($this->_count_string.$this->protect_identifiers('numrows')."\nFROM (\n".$this->_compile_select()."\n) CI_count_all_results")
1407 : $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
yaoshanliang9971e7b2015-03-14 13:09:16 +08001408
yaoshanliang2f164052015-03-16 16:48:15 +08001409 if ($reset === TRUE)
yaoshanliang19c28472015-03-15 10:42:18 +08001410 {
yaoshanliang2f164052015-03-16 16:48:15 +08001411 $this->_reset_select();
yaoshanliang19c28472015-03-15 10:42:18 +08001412 }
Andrey Andreev075bdb42016-01-25 13:31:23 +02001413 // If we've previously reset the qb_orderby values, get them back
1414 elseif ( ! isset($this->qb_orderby))
1415 {
1416 $this->qb_orderby = $orderby;
1417 }
Barry Mienydd671972010-10-04 16:33:58 +02001418
Purwandi1d160e72012-01-09 16:33:28 +07001419 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001420 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001421 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001422 }
1423
Purwandi1d160e72012-01-09 16:33:28 +07001424 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001425 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001426 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001427
Derek Allard2067d1a2008-11-13 22:59:24 +00001428 // --------------------------------------------------------------------
1429
1430 /**
1431 * Get_Where
1432 *
1433 * Allows the where clause, limit and offset to be added directly
1434 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001435 * @param string $table
1436 * @param string $where
1437 * @param int $limit
1438 * @param int $offset
Andrey Andreevaec51262016-02-09 21:11:07 +02001439 * @return CI_DB_result
Derek Allard2067d1a2008-11-13 22:59:24 +00001440 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001441 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001442 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001443 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001444 {
1445 $this->from($table);
1446 }
1447
vlakoff1228fe22013-01-14 01:30:09 +01001448 if ($where !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001449 {
1450 $this->where($where);
1451 }
Barry Mienydd671972010-10-04 16:33:58 +02001452
Andrey Andreev650b4c02012-06-11 12:07:15 +03001453 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001454 {
1455 $this->limit($limit, $offset);
1456 }
Barry Mienydd671972010-10-04 16:33:58 +02001457
Andrey Andreev24276a32012-01-08 02:44:38 +02001458 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 $this->_reset_select();
1460 return $result;
1461 }
1462
1463 // --------------------------------------------------------------------
1464
1465 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001466 * Insert_Batch
1467 *
1468 * Compiles batch insert strings and runs the queries
1469 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001470 * @param string $table Table to insert into
1471 * @param array $set An associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001472 * @param bool $escape Whether to escape values and identifiers
Andrey Andreevae85eb42012-11-02 01:42:31 +02001473 * @return int Number of rows inserted or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001474 */
Andrey Andreev105a48b2016-02-04 15:45:10 +02001475 public function insert_batch($table, $set = NULL, $escape = NULL, $batch_size = 100)
Barry Mienydd671972010-10-04 16:33:58 +02001476 {
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001477 if ($set === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001478 {
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001479 if (empty($this->qb_set))
1480 {
1481 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
1482 }
1483 }
1484 else
1485 {
1486 if (empty($set))
1487 {
1488 return ($this->db_debug) ? $this->display_error('insert_batch() called with no data') : FALSE;
1489 }
1490
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001491 $this->set_insert_batch($set, '', $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001492 }
Barry Mienydd671972010-10-04 16:33:58 +02001493
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001494 if (strlen($table) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001495 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001496 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001497 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001498 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001499 }
Barry Mienydd671972010-10-04 16:33:58 +02001500
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001501 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001502 }
1503
1504 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001505 $affected_rows = 0;
Andrey Andreev105a48b2016-02-04 15:45:10 +02001506 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += $batch_size)
Derek Jonesd10e8962010-03-02 17:10:36 -06001507 {
Andrey Andreevd9a40632016-07-22 15:49:08 +03001508 if ($this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, $escape, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, $batch_size))))
1509 {
1510 $affected_rows += $this->affected_rows();
1511 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001512 }
Barry Mienydd671972010-10-04 16:33:58 +02001513
Derek Jonesd10e8962010-03-02 17:10:36 -06001514 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001515 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001516 }
1517
1518 // --------------------------------------------------------------------
1519
1520 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +02001521 * Insert batch statement
Andrey Andreev97f36972012-04-05 12:44:36 +03001522 *
1523 * Generates a platform-specific insert string from the supplied data.
1524 *
Andrey Andreev083e3c82012-11-06 12:48:32 +02001525 * @param string $table Table name
1526 * @param array $keys INSERT keys
1527 * @param array $values INSERT values
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001528 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001529 */
1530 protected function _insert_batch($table, $keys, $values)
1531 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001532 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001533 }
1534
1535 // --------------------------------------------------------------------
1536
1537 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001538 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001539 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001540 * @param mixed
1541 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001542 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001543 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001544 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001545 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001546 {
1547 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001548
Derek Jonesd10e8962010-03-02 17:10:36 -06001549 if ( ! is_array($key))
1550 {
1551 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001552 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001553
Andrey Andreevfe642da2012-06-16 03:47:33 +03001554 is_bool($escape) OR $escape = $this->_protect_identifiers;
1555
Andrey Andreev51c84f32017-01-03 17:42:26 +02001556 $keys = array_keys($this->_object_to_array(reset($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001557 sort($keys);
1558
1559 foreach ($key as $row)
1560 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001561 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001562 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1563 {
1564 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001565 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001566 return;
1567 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001568
Barry Mienydd671972010-10-04 16:33:58 +02001569 ksort($row); // puts $row in the same order as our keys
1570
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001571 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001572 {
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001573 $clean = array();
1574 foreach ($row as $value)
1575 {
1576 $clean[] = $this->escape($value);
1577 }
1578
1579 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001580 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001581
Andrey Andreev838a9d62012-12-03 14:37:47 +02001582 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001583 }
1584
1585 foreach ($keys as $k)
1586 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001587 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001588 }
Barry Mienydd671972010-10-04 16:33:58 +02001589
Derek Jonesd10e8962010-03-02 17:10:36 -06001590 return $this;
1591 }
WanWizard7219c072011-12-28 14:09:05 +01001592
Kyle Farris0c147b32011-08-26 02:29:31 -04001593 // --------------------------------------------------------------------
1594
1595 /**
1596 * Get INSERT query string
1597 *
1598 * Compiles an insert query and returns the sql
1599 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001600 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001601 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001602 * @return string
1603 */
1604 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001605 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001606 if ($this->_validate_insert($table) === FALSE)
1607 {
1608 return FALSE;
1609 }
WanWizard7219c072011-12-28 14:09:05 +01001610
Kyle Farris76116012011-08-31 11:17:48 -04001611 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001612 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001613 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001614 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001615 array_keys($this->qb_set),
1616 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001617 );
WanWizard7219c072011-12-28 14:09:05 +01001618
Kyle Farris0c147b32011-08-26 02:29:31 -04001619 if ($reset === TRUE)
1620 {
1621 $this->_reset_write();
1622 }
WanWizard7219c072011-12-28 14:09:05 +01001623
Kyle Farris0c147b32011-08-26 02:29:31 -04001624 return $sql;
1625 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001626
Derek Allard2067d1a2008-11-13 22:59:24 +00001627 // --------------------------------------------------------------------
1628
1629 /**
1630 * Insert
1631 *
1632 * Compiles an insert string and runs the query
1633 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001634 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001635 * @param array an associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001636 * @param bool $escape Whether to escape values and identifiers
Andrey Andreevaec51262016-02-09 21:11:07 +02001637 * @return bool TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +00001638 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001639 public function insert($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001640 {
vlakoff1228fe22013-01-14 01:30:09 +01001641 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001642 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001643 $this->set($set, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001644 }
WanWizard7219c072011-12-28 14:09:05 +01001645
Kyle Farris0c147b32011-08-26 02:29:31 -04001646 if ($this->_validate_insert($table) === FALSE)
1647 {
1648 return FALSE;
1649 }
WanWizard7219c072011-12-28 14:09:05 +01001650
Kyle Farris76116012011-08-31 11:17:48 -04001651 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001652 $this->protect_identifiers(
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001653 $this->qb_from[0], TRUE, $escape, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001654 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001655 array_keys($this->qb_set),
1656 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001657 );
Barry Mienydd671972010-10-04 16:33:58 +02001658
Kyle Farris0c147b32011-08-26 02:29:31 -04001659 $this->_reset_write();
1660 return $this->query($sql);
1661 }
WanWizard7219c072011-12-28 14:09:05 +01001662
Kyle Farris0c147b32011-08-26 02:29:31 -04001663 // --------------------------------------------------------------------
1664
1665 /**
1666 * Validate Insert
1667 *
1668 * This method is used by both insert() and get_compiled_insert() to
1669 * validate that the there data is actually being set and that table
1670 * has been chosen to be inserted into.
1671 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001672 * @param string the table to insert data into
1673 * @return string
1674 */
WanWizard7219c072011-12-28 14:09:05 +01001675 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001676 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001677 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001678 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001679 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001680 }
1681
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001682 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001683 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001684 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001685 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001686 elseif ( ! isset($this->qb_from[0]))
1687 {
1688 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1689 }
WanWizard7219c072011-12-28 14:09:05 +01001690
Kyle Farris0c147b32011-08-26 02:29:31 -04001691 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001692 }
Barry Mienydd671972010-10-04 16:33:58 +02001693
Phil Sturgeon9789f322011-07-15 15:14:05 -06001694 // --------------------------------------------------------------------
1695
1696 /**
1697 * Replace
1698 *
1699 * Compiles an replace into string and runs the query
1700 *
1701 * @param string the table to replace data into
1702 * @param array an associative array of insert values
Andrey Andreevaec51262016-02-09 21:11:07 +02001703 * @return bool TRUE on success, FALSE on failure
Phil Sturgeon9789f322011-07-15 15:14:05 -06001704 */
1705 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001706 {
vlakoff1228fe22013-01-14 01:30:09 +01001707 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001708 {
1709 $this->set($set);
1710 }
Barry Mienydd671972010-10-04 16:33:58 +02001711
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001712 if (count($this->qb_set) === 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_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001715 }
1716
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001717 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001718 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001719 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001720 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001721 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001722 }
Barry Mienydd671972010-10-04 16:33:58 +02001723
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001724 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001725 }
1726
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001727 $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 +00001728
Derek Jonesd10e8962010-03-02 17:10:36 -06001729 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001730 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001731 }
WanWizard7219c072011-12-28 14:09:05 +01001732
Kyle Farris0c147b32011-08-26 02:29:31 -04001733 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001734
Kyle Farris0c147b32011-08-26 02:29:31 -04001735 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001736 * Replace statement
1737 *
1738 * Generates a platform-specific replace string from the supplied data
1739 *
1740 * @param string the table name
1741 * @param array the insert keys
1742 * @param array the insert values
1743 * @return string
1744 */
1745 protected function _replace($table, $keys, $values)
1746 {
1747 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1748 }
1749
1750 // --------------------------------------------------------------------
1751
1752 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001753 * FROM tables
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001754 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001755 * Groups tables in FROM clauses if needed, so there is no confusion
1756 * about operator precedence.
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001757 *
Claudio Galdiolo93e78132015-01-29 11:43:56 -05001758 * Note: This is only used (and overridden) by MySQL and CUBRID.
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001759 *
1760 * @return string
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001761 */
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001762 protected function _from_tables()
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001763 {
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001764 return implode(', ', $this->qb_from);
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001765 }
1766
1767 // --------------------------------------------------------------------
1768
1769 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001770 * Get UPDATE query string
1771 *
1772 * Compiles an update query and returns the sql
1773 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001774 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001775 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001776 * @return string
1777 */
1778 public function get_compiled_update($table = '', $reset = TRUE)
1779 {
1780 // Combine any cached components with the current statements
1781 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001782
Kyle Farris0c147b32011-08-26 02:29:31 -04001783 if ($this->_validate_update($table) === FALSE)
1784 {
1785 return FALSE;
1786 }
WanWizard7219c072011-12-28 14:09:05 +01001787
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001788 $sql = $this->_update($this->qb_from[0], $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001789
Kyle Farris0c147b32011-08-26 02:29:31 -04001790 if ($reset === TRUE)
1791 {
1792 $this->_reset_write();
1793 }
WanWizard7219c072011-12-28 14:09:05 +01001794
Kyle Farris0c147b32011-08-26 02:29:31 -04001795 return $sql;
1796 }
WanWizard7219c072011-12-28 14:09:05 +01001797
Derek Allard2067d1a2008-11-13 22:59:24 +00001798 // --------------------------------------------------------------------
1799
1800 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001801 * UPDATE
Derek Allard2067d1a2008-11-13 22:59:24 +00001802 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001803 * Compiles an update string and runs the query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001804 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001805 * @param string $table
1806 * @param array $set An associative array of update values
1807 * @param mixed $where
1808 * @param int $limit
Andrey Andreevaec51262016-02-09 21:11:07 +02001809 * @return bool TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +00001810 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001811 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001812 {
1813 // Combine any cached components with the current statements
1814 $this->_merge_cache();
1815
vlakoff1228fe22013-01-14 01:30:09 +01001816 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001817 {
1818 $this->set($set);
1819 }
Barry Mienydd671972010-10-04 16:33:58 +02001820
Kyle Farris0c147b32011-08-26 02:29:31 -04001821 if ($this->_validate_update($table) === FALSE)
1822 {
1823 return FALSE;
1824 }
1825
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001826 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001827 {
1828 $this->where($where);
1829 }
1830
Andrey Andreev650b4c02012-06-11 12:07:15 +03001831 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001832 {
1833 $this->limit($limit);
1834 }
1835
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001836 $sql = $this->_update($this->qb_from[0], $this->qb_set);
Kyle Farris0c147b32011-08-26 02:29:31 -04001837 $this->_reset_write();
1838 return $this->query($sql);
1839 }
WanWizard7219c072011-12-28 14:09:05 +01001840
Kyle Farris0c147b32011-08-26 02:29:31 -04001841 // --------------------------------------------------------------------
1842
1843 /**
1844 * Validate Update
1845 *
1846 * This method is used by both update() and get_compiled_update() to
1847 * validate that data is actually being set and that a table has been
1848 * chosen to be update.
1849 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001850 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001851 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001852 */
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001853 protected function _validate_update($table)
Kyle Farris0c147b32011-08-26 02:29:31 -04001854 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001855 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001856 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001857 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001858 }
1859
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001860 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001861 {
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001862 $this->qb_from = array($this->protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +00001863 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001864 elseif ( ! isset($this->qb_from[0]))
1865 {
1866 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1867 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001868
1869 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001870 }
WanWizard7219c072011-12-28 14:09:05 +01001871
Derek Jonesd10e8962010-03-02 17:10:36 -06001872 // --------------------------------------------------------------------
1873
1874 /**
1875 * Update_Batch
1876 *
1877 * Compiles an update string and runs the query
1878 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001879 * @param string the table to retrieve the results from
1880 * @param array an associative array of update values
1881 * @param string the where key
Andrey Andreev9f808b02012-10-24 17:38:48 +03001882 * @return int number of rows affected or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001883 */
Andrey Andreev105a48b2016-02-04 15:45:10 +02001884 public function update_batch($table, $set = NULL, $index = NULL, $batch_size = 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001885 {
1886 // Combine any cached components with the current statements
1887 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001888
vlakoff1228fe22013-01-14 01:30:09 +01001889 if ($index === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001890 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001891 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001892 }
1893
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001894 if ($set === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001895 {
Andrey Andreev8338bbb2016-12-12 14:17:52 +02001896 if (empty($this->qb_set_ub))
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001897 {
1898 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
1899 }
1900 }
1901 else
1902 {
1903 if (empty($set))
1904 {
1905 return ($this->db_debug) ? $this->display_error('update_batch() called with no data') : FALSE;
1906 }
1907
Derek Jonesd10e8962010-03-02 17:10:36 -06001908 $this->set_update_batch($set, $index);
1909 }
1910
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001911 if (strlen($table) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001912 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001913 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001914 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001915 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001916 }
Barry Mienydd671972010-10-04 16:33:58 +02001917
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001918 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001919 }
Barry Mienydd671972010-10-04 16:33:58 +02001920
Derek Jonesd10e8962010-03-02 17:10:36 -06001921 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001922 $affected_rows = 0;
Andrey Andreev8338bbb2016-12-12 14:17:52 +02001923 for ($i = 0, $total = count($this->qb_set_ub); $i < $total; $i += $batch_size)
Derek Jonesd10e8962010-03-02 17:10:36 -06001924 {
Andrey Andreev8338bbb2016-12-12 14:17:52 +02001925 if ($this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set_ub, $i, $batch_size), $index)))
Andrey Andreevd9a40632016-07-22 15:49:08 +03001926 {
1927 $affected_rows += $this->affected_rows();
1928 }
1929
Andrey Andreev79f888b2013-08-06 13:59:23 +03001930 $this->qb_where = array();
Derek Jonesd10e8962010-03-02 17:10:36 -06001931 }
Barry Mienydd671972010-10-04 16:33:58 +02001932
Derek Jonesd10e8962010-03-02 17:10:36 -06001933 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001934 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001935 }
1936
1937 // --------------------------------------------------------------------
1938
1939 /**
Andrey Andreev219565d2013-03-12 20:00:08 +02001940 * Update_Batch statement
1941 *
1942 * Generates a platform-specific batch update string from the supplied data
1943 *
1944 * @param string $table Table name
1945 * @param array $values Update data
1946 * @param string $index WHERE key
1947 * @return string
1948 */
1949 protected function _update_batch($table, $values, $index)
1950 {
1951 $ids = array();
1952 foreach ($values as $key => $val)
1953 {
Andrey Andreev8338bbb2016-12-12 14:17:52 +02001954 $ids[] = $val[$index]['value'];
Andrey Andreev219565d2013-03-12 20:00:08 +02001955
1956 foreach (array_keys($val) as $field)
1957 {
1958 if ($field !== $index)
1959 {
Andrey Andreev8338bbb2016-12-12 14:17:52 +02001960 $final[$val[$field]['field']][] = 'WHEN '.$val[$index]['field'].' = '.$val[$index]['value'].' THEN '.$val[$field]['value'];
Andrey Andreev219565d2013-03-12 20:00:08 +02001961 }
1962 }
1963 }
1964
1965 $cases = '';
1966 foreach ($final as $k => $v)
1967 {
1968 $cases .= $k." = CASE \n"
1969 .implode("\n", $v)."\n"
1970 .'ELSE '.$k.' END, ';
1971 }
1972
Andrey Andreev8338bbb2016-12-12 14:17:52 +02001973 $this->where($val[$index]['field'].' IN('.implode(',', $ids).')', NULL, FALSE);
Andrey Andreev219565d2013-03-12 20:00:08 +02001974
1975 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
1976 }
1977
1978 // --------------------------------------------------------------------
1979
1980 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001981 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001982 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001983 * @param array
1984 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001985 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001986 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001987 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001988 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001989 {
1990 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001991
Derek Jonesd10e8962010-03-02 17:10:36 -06001992 if ( ! is_array($key))
1993 {
1994 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001995 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001996
Andrey Andreevfe642da2012-06-16 03:47:33 +03001997 is_bool($escape) OR $escape = $this->_protect_identifiers;
1998
Derek Jonesd10e8962010-03-02 17:10:36 -06001999 foreach ($key as $k => $v)
2000 {
2001 $index_set = FALSE;
2002 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05002003 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06002004 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002005 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06002006 {
2007 $index_set = TRUE;
2008 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002009
Andrey Andreev8338bbb2016-12-12 14:17:52 +02002010 $clean[$k2] = array(
2011 'field' => $this->protect_identifiers($k2, FALSE, $escape),
2012 'value' => ($escape === FALSE ? $v2 : $this->escape($v2))
2013 );
Derek Jonesd10e8962010-03-02 17:10:36 -06002014 }
2015
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002016 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06002017 {
2018 return $this->display_error('db_batch_missing_index');
2019 }
2020
Andrey Andreev8338bbb2016-12-12 14:17:52 +02002021 $this->qb_set_ub[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06002022 }
Barry Mienydd671972010-10-04 16:33:58 +02002023
Derek Jonesd10e8962010-03-02 17:10:36 -06002024 return $this;
2025 }
2026
Derek Allard2067d1a2008-11-13 22:59:24 +00002027 // --------------------------------------------------------------------
2028
2029 /**
2030 * Empty Table
2031 *
2032 * Compiles a delete string and runs "DELETE FROM table"
2033 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002034 * @param string the table to empty
Andrey Andreevaec51262016-02-09 21:11:07 +02002035 * @return bool TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +00002036 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002037 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002038 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002039 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002040 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002041 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002042 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002043 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002044 }
2045
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002046 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002047 }
2048 else
2049 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002050 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002051 }
2052
2053 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002054 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00002055 return $this->query($sql);
2056 }
2057
2058 // --------------------------------------------------------------------
2059
2060 /**
2061 * Truncate
2062 *
2063 * Compiles a truncate string and runs the query
2064 * If the database does not support the truncate() command
2065 * This function maps to "DELETE FROM table"
2066 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002067 * @param string the table to truncate
Andrey Andreevaec51262016-02-09 21:11:07 +02002068 * @return bool TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +00002069 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002070 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002071 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002072 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002073 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002074 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002075 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002076 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002077 }
2078
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002079 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002080 }
2081 else
2082 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002083 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002084 }
2085
2086 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002087 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00002088 return $this->query($sql);
2089 }
WanWizard7219c072011-12-28 14:09:05 +01002090
Kyle Farris0c147b32011-08-26 02:29:31 -04002091 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02002092
Kyle Farris0c147b32011-08-26 02:29:31 -04002093 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03002094 * Truncate statement
2095 *
2096 * Generates a platform-specific truncate string from the supplied data
2097 *
2098 * If the database does not support the truncate() command,
2099 * then this method maps to 'DELETE FROM table'
2100 *
2101 * @param string the table name
2102 * @return string
2103 */
2104 protected function _truncate($table)
2105 {
2106 return 'TRUNCATE '.$table;
2107 }
2108
2109 // --------------------------------------------------------------------
2110
2111 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04002112 * Get DELETE query string
2113 *
2114 * Compiles a delete query string and returns the sql
2115 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002116 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002117 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04002118 * @return string
2119 */
2120 public function get_compiled_delete($table = '', $reset = TRUE)
2121 {
2122 $this->return_delete_sql = TRUE;
2123 $sql = $this->delete($table, '', NULL, $reset);
2124 $this->return_delete_sql = FALSE;
2125 return $sql;
2126 }
WanWizard7219c072011-12-28 14:09:05 +01002127
Derek Allard2067d1a2008-11-13 22:59:24 +00002128 // --------------------------------------------------------------------
2129
2130 /**
2131 * Delete
2132 *
2133 * Compiles a delete string and runs the query
2134 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002135 * @param mixed the table(s) to delete from. String or array
2136 * @param mixed the where clause
2137 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03002138 * @param bool
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002139 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00002140 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002141 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002142 {
2143 // Combine any cached components with the current statements
2144 $this->_merge_cache();
2145
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002146 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002147 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002148 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002149 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002150 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002151 }
2152
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002153 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002154 }
2155 elseif (is_array($table))
2156 {
Andrey Andreeva1170af2015-07-02 11:46:56 +03002157 empty($where) && $reset_data = FALSE;
2158
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05002159 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002160 {
Andrey Andreev13f50542012-10-12 12:31:02 +03002161 $this->delete($single_table, $where, $limit, $reset_data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002162 }
Andrey Andreeva1170af2015-07-02 11:46:56 +03002163
Derek Allard2067d1a2008-11-13 22:59:24 +00002164 return;
2165 }
2166 else
2167 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002168 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002169 }
2170
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002171 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002172 {
2173 $this->where($where);
2174 }
2175
Andrey Andreev650b4c02012-06-11 12:07:15 +03002176 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002177 {
2178 $this->limit($limit);
2179 }
2180
Andrey Andreev94611df2012-07-19 12:29:54 +03002181 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002182 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002183 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02002184 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002185
Andrey Andreevb0478652012-07-18 15:34:46 +03002186 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002187 if ($reset_data)
2188 {
2189 $this->_reset_write();
2190 }
WanWizard7219c072011-12-28 14:09:05 +01002191
Andrey Andreev24276a32012-01-08 02:44:38 +02002192 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00002193 }
WanWizard7219c072011-12-28 14:09:05 +01002194
Derek Allard2067d1a2008-11-13 22:59:24 +00002195 // --------------------------------------------------------------------
2196
2197 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03002198 * Delete statement
2199 *
2200 * Generates a platform-specific delete string from the supplied data
2201 *
2202 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03002203 * @return string
2204 */
Andrey Andreevb0478652012-07-18 15:34:46 +03002205 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03002206 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002207 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002208 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03002209 }
2210
2211 // --------------------------------------------------------------------
2212
2213 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002214 * DB Prefix
2215 *
2216 * Prepends a database prefix if one exists in configuration
2217 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002218 * @param string the table
2219 * @return string
2220 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002221 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002222 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002223 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002224 {
2225 $this->display_error('db_table_name_required');
2226 }
2227
2228 return $this->dbprefix.$table;
2229 }
2230
2231 // --------------------------------------------------------------------
2232
2233 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06002234 * Set DB Prefix
2235 *
2236 * Set's the DB Prefix to something new without needing to reconnect
2237 *
2238 * @param string the prefix
2239 * @return string
2240 */
2241 public function set_dbprefix($prefix = '')
2242 {
2243 return $this->dbprefix = $prefix;
2244 }
2245
2246 // --------------------------------------------------------------------
2247
2248 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002249 * Track Aliases
2250 *
2251 * Used to track SQL statements written with aliased tables.
2252 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002253 * @param string The table to inspect
2254 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002255 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002256 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002257 {
2258 if (is_array($table))
2259 {
2260 foreach ($table as $t)
2261 {
2262 $this->_track_aliases($t);
2263 }
2264 return;
2265 }
Barry Mienydd671972010-10-04 16:33:58 +02002266
Derek Jones37f4b9c2011-07-01 17:56:50 -05002267 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002268 // the string into discreet statements
2269 if (strpos($table, ',') !== FALSE)
2270 {
2271 return $this->_track_aliases(explode(',', $table));
2272 }
Barry Mienydd671972010-10-04 16:33:58 +02002273
Derek Allard2067d1a2008-11-13 22:59:24 +00002274 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002275 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002276 {
2277 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002278 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002279
Derek Allard2067d1a2008-11-13 22:59:24 +00002280 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002281 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002282
Derek Allard2067d1a2008-11-13 22:59:24 +00002283 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002284 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002285 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002286 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002287 }
2288 }
2289 }
WanWizard7219c072011-12-28 14:09:05 +01002290
Derek Allard2067d1a2008-11-13 22:59:24 +00002291 // --------------------------------------------------------------------
2292
2293 /**
2294 * Compile the SELECT statement
2295 *
2296 * Generates a query string based on which functions were used.
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002297 * Should not be called directly.
Derek Allard2067d1a2008-11-13 22:59:24 +00002298 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002299 * @param bool $select_override
Derek Allard2067d1a2008-11-13 22:59:24 +00002300 * @return string
2301 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002302 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002303 {
2304 // Combine any cached components with the current statements
2305 $this->_merge_cache();
2306
Derek Allard2067d1a2008-11-13 22:59:24 +00002307 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002308 if ($select_override !== FALSE)
2309 {
2310 $sql = $select_override;
2311 }
2312 else
2313 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002314 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002315
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002316 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002317 {
Barry Mienydd671972010-10-04 16:33:58 +02002318 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002319 }
2320 else
Barry Mienydd671972010-10-04 16:33:58 +02002321 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002322 // Cycle through the "select" portion of the query and prep each column name.
Andrey Andreev1924eb32015-04-08 17:19:24 +03002323 // The reason we protect identifiers here rather than in the select() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002324 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002325 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002326 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002327 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002328 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002329 }
Barry Mienydd671972010-10-04 16:33:58 +02002330
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002331 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002332 }
2333 }
2334
Derek Allard2067d1a2008-11-13 22:59:24 +00002335 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002336 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002337 {
Andrey Andreeve78f8152012-10-09 11:38:38 +03002338 $sql .= "\nFROM ".$this->_from_tables();
Derek Allard2067d1a2008-11-13 22:59:24 +00002339 }
2340
Derek Allard2067d1a2008-11-13 22:59:24 +00002341 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002342 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002343 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002344 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002345 }
2346
Andrey Andreev2d486232012-07-19 14:46:51 +03002347 $sql .= $this->_compile_wh('qb_where')
2348 .$this->_compile_group_by()
2349 .$this->_compile_wh('qb_having')
2350 .$this->_compile_order_by(); // ORDER BY
Andrey Andreevb0478652012-07-18 15:34:46 +03002351
Andrey Andreevd40459d2012-07-18 16:46:39 +03002352 // LIMIT
Andrey Andreevacc64812016-07-29 11:42:28 +03002353 if ($this->qb_limit OR $this->qb_offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00002354 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002355 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002356 }
2357
2358 return $sql;
2359 }
2360
2361 // --------------------------------------------------------------------
2362
2363 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002364 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002365 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002366 * Escapes identifiers in WHERE and HAVING statements at execution time.
2367 *
Andrey Andreev99c17e52016-02-29 16:53:45 +02002368 * Required so that aliases are tracked properly, regardless of whether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002369 * where(), or_where(), having(), or_having are called prior to from(),
2370 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002371 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002372 * @param string $qb_key 'qb_where' or 'qb_having'
Andrey Andreevd40459d2012-07-18 16:46:39 +03002373 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002374 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002375 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002376 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002377 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002378 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002379 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002380 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002381 // Is this condition already compiled?
2382 if (is_string($this->{$qb_key}[$i]))
2383 {
2384 continue;
2385 }
2386 elseif ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002387 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002388 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002389 continue;
2390 }
2391
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002392 // Split multiple conditions
2393 $conditions = preg_split(
Andrey Andreev554b4522015-09-01 13:51:26 +03002394 '/((?:^|\s+)AND\s+|(?:^|\s+)OR\s+)/i',
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002395 $this->{$qb_key}[$i]['condition'],
2396 -1,
2397 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
2398 );
2399
2400 for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002401 {
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002402 if (($op = $this->_get_operator($conditions[$ci])) === FALSE
Andrey Andreeve4742582012-10-25 13:25:13 +03002403 OR ! preg_match('/^(\(?)(.*)('.preg_quote($op, '/').')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches))
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002404 {
2405 continue;
2406 }
2407
2408 // $matches = array(
2409 // 0 => '(test <= foo)', /* the whole thing */
2410 // 1 => '(', /* optional */
2411 // 2 => 'test', /* the field name */
2412 // 3 => ' <= ', /* $op */
2413 // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2414 // 5 => ')' /* optional */
2415 // );
Andrey Andreev082aa402012-10-22 19:41:55 +03002416
2417 if ( ! empty($matches[4]))
2418 {
2419 $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4]));
2420 $matches[4] = ' '.$matches[4];
2421 }
2422
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002423 $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2]))
2424 .' '.trim($matches[3]).$matches[4].$matches[5];
Andrey Andreev6e704752012-07-18 00:46:33 +03002425 }
2426
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002427 $this->{$qb_key}[$i] = implode('', $conditions);
Andrey Andreev6e704752012-07-18 00:46:33 +03002428 }
2429
Andrey Andreev9d3aa1b2012-10-12 12:14:09 +03002430 return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ")
2431 .implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002432 }
2433
Andrey Andreevb0478652012-07-18 15:34:46 +03002434 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002435 }
2436
2437 // --------------------------------------------------------------------
2438
2439 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002440 * Compile GROUP BY
2441 *
2442 * Escapes identifiers in GROUP BY statements at execution time.
2443 *
Andrey Andreev71d8f722017-01-17 12:01:00 +02002444 * Required so that aliases are tracked properly, regardless of whether
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002445 * group_by() is called prior to from(), join() and dbprefix is added
2446 * only if needed.
2447 *
2448 * @return string SQL statement
2449 */
2450 protected function _compile_group_by()
2451 {
2452 if (count($this->qb_groupby) > 0)
2453 {
Andrey Andreev96feb582012-07-19 13:12:34 +03002454 for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
2455 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002456 // Is it already compiled?
Andrey Andreev18eba242014-01-23 22:52:31 +02002457 if (is_string($this->qb_groupby[$i]))
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002458 {
2459 continue;
2460 }
2461
Andrey Andreev082aa402012-10-22 19:41:55 +03002462 $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 +03002463 ? $this->qb_groupby[$i]['field']
Andrey Andreev13f50542012-10-12 12:31:02 +03002464 : $this->protect_identifiers($this->qb_groupby[$i]['field']);
Andrey Andreev96feb582012-07-19 13:12:34 +03002465 }
2466
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002467 return "\nGROUP BY ".implode(', ', $this->qb_groupby);
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002468 }
2469
2470 return '';
2471 }
2472
2473 // --------------------------------------------------------------------
2474
2475 /**
Andrey Andreev2d486232012-07-19 14:46:51 +03002476 * Compile ORDER BY
2477 *
2478 * Escapes identifiers in ORDER BY statements at execution time.
2479 *
Andrey Andreev71d8f722017-01-17 12:01:00 +02002480 * Required so that aliases are tracked properly, regardless of whether
Andrey Andreev2d486232012-07-19 14:46:51 +03002481 * order_by() is called prior to from(), join() and dbprefix is added
2482 * only if needed.
2483 *
2484 * @return string SQL statement
2485 */
2486 protected function _compile_order_by()
2487 {
Andrey Andreev33cc3e12017-01-16 16:01:58 +02002488 if (empty($this->qb_orderby))
Andrey Andreev2d486232012-07-19 14:46:51 +03002489 {
Andrey Andreev33cc3e12017-01-16 16:01:58 +02002490 return '';
2491 }
Andrey Andreev2d486232012-07-19 14:46:51 +03002492
Andrey Andreev33cc3e12017-01-16 16:01:58 +02002493 for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++)
2494 {
2495 if (is_string($this->qb_orderby[$i]))
2496 {
2497 continue;
Andrey Andreev2d486232012-07-19 14:46:51 +03002498 }
2499
Andrey Andreev33cc3e12017-01-16 16:01:58 +02002500 if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field']))
2501 {
2502 $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']);
2503 }
2504
2505 $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction'];
Andrey Andreev2d486232012-07-19 14:46:51 +03002506 }
2507
Andrey Andreev33cc3e12017-01-16 16:01:58 +02002508 return "\nORDER BY ".implode(', ', $this->qb_orderby);
Andrey Andreev2d486232012-07-19 14:46:51 +03002509 }
2510
2511 // --------------------------------------------------------------------
2512
2513 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002514 * Object to Array
2515 *
2516 * Takes an object as input and converts the class variables to array key/vals
2517 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002518 * @param object
2519 * @return array
2520 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002521 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002522 {
2523 if ( ! is_object($object))
2524 {
2525 return $object;
2526 }
Barry Mienydd671972010-10-04 16:33:58 +02002527
Derek Allard2067d1a2008-11-13 22:59:24 +00002528 $array = array();
2529 foreach (get_object_vars($object) as $key => $val)
2530 {
2531 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002532 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002533 {
2534 $array[$key] = $val;
2535 }
2536 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002537
2538 return $array;
2539 }
Barry Mienydd671972010-10-04 16:33:58 +02002540
Derek Jonesd10e8962010-03-02 17:10:36 -06002541 // --------------------------------------------------------------------
2542
2543 /**
2544 * Object to Array
2545 *
2546 * Takes an object as input and converts the class variables to array key/vals
2547 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002548 * @param object
2549 * @return array
2550 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002551 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002552 {
2553 if ( ! is_object($object))
2554 {
2555 return $object;
2556 }
Barry Mienydd671972010-10-04 16:33:58 +02002557
Derek Jonesd10e8962010-03-02 17:10:36 -06002558 $array = array();
2559 $out = get_object_vars($object);
2560 $fields = array_keys($out);
2561
2562 foreach ($fields as $val)
2563 {
2564 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002565 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002566 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002567 $i = 0;
2568 foreach ($out[$val] as $data)
2569 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002570 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002571 }
2572 }
2573 }
2574
Derek Allard2067d1a2008-11-13 22:59:24 +00002575 return $array;
2576 }
Barry Mienydd671972010-10-04 16:33:58 +02002577
Derek Allard2067d1a2008-11-13 22:59:24 +00002578 // --------------------------------------------------------------------
2579
2580 /**
2581 * Start Cache
2582 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002583 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002584 *
Andrey Andreev4a587f52014-12-11 16:27:15 +02002585 * @return CI_DB_query_builder
Barry Mienydd671972010-10-04 16:33:58 +02002586 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002587 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002588 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002589 $this->qb_caching = TRUE;
Andrey Andreev4a587f52014-12-11 16:27:15 +02002590 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002591 }
2592
2593 // --------------------------------------------------------------------
2594
2595 /**
2596 * Stop Cache
2597 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002598 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002599 *
Andrey Andreev4a587f52014-12-11 16:27:15 +02002600 * @return CI_DB_query_builder
Barry Mienydd671972010-10-04 16:33:58 +02002601 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002602 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002603 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002604 $this->qb_caching = FALSE;
Andrey Andreev4a587f52014-12-11 16:27:15 +02002605 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002606 }
2607
2608 // --------------------------------------------------------------------
2609
2610 /**
2611 * Flush Cache
2612 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002613 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002614 *
Andrey Andreev4a587f52014-12-11 16:27:15 +02002615 * @return CI_DB_query_builder
Barry Mienydd671972010-10-04 16:33:58 +02002616 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002617 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002618 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002619 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002620 'qb_cache_select' => array(),
2621 'qb_cache_from' => array(),
2622 'qb_cache_join' => array(),
2623 'qb_cache_where' => array(),
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002624 'qb_cache_groupby' => array(),
2625 'qb_cache_having' => array(),
2626 'qb_cache_orderby' => array(),
2627 'qb_cache_set' => array(),
2628 'qb_cache_exists' => array(),
2629 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002630 ));
Andrey Andreev4a587f52014-12-11 16:27:15 +02002631
2632 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002633 }
2634
2635 // --------------------------------------------------------------------
2636
2637 /**
2638 * Merge Cache
2639 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002640 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002641 * locally called ones.
2642 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002643 * @return void
2644 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002645 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002646 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002647 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002648 {
2649 return;
2650 }
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002651 elseif (in_array('select', $this->qb_cache_exists, TRUE))
2652 {
2653 $qb_no_escape = $this->qb_cache_no_escape;
2654 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002655
GDmac17a05282013-11-11 13:18:09 +01002656 foreach (array_unique($this->qb_cache_exists) as $val) // select, from, etc.
Derek Allard2067d1a2008-11-13 22:59:24 +00002657 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002658 $qb_variable = 'qb_'.$val;
2659 $qb_cache_var = 'qb_cache_'.$val;
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002660 $qb_new = $this->$qb_cache_var;
GDmace1b86832013-11-08 16:52:54 +01002661
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002662 for ($i = 0, $c = count($this->$qb_variable); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00002663 {
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002664 if ( ! in_array($this->{$qb_variable}[$i], $qb_new, TRUE))
2665 {
2666 $qb_new[] = $this->{$qb_variable}[$i];
2667 if ($val === 'select')
2668 {
2669 $qb_no_escape[] = $this->qb_no_escape[$i];
2670 }
2671 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002672 }
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002673
GDmace1b86832013-11-08 16:52:54 +01002674 $this->$qb_variable = $qb_new;
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002675 if ($val === 'select')
2676 {
2677 $this->qb_no_escape = $qb_no_escape;
2678 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002679 }
2680
2681 // If we are "protecting identifiers" we need to examine the "from"
2682 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002683 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002684 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002685 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002686 }
2687 }
WanWizard7219c072011-12-28 14:09:05 +01002688
Kyle Farris0c147b32011-08-26 02:29:31 -04002689 // --------------------------------------------------------------------
2690
2691 /**
Andrey Andreev082aa402012-10-22 19:41:55 +03002692 * Is literal
2693 *
2694 * Determines if a string represents a literal value or a field name
2695 *
Andrey Andreev02e4cd72012-11-13 11:50:47 +02002696 * @param string $str
Andrey Andreev082aa402012-10-22 19:41:55 +03002697 * @return bool
2698 */
2699 protected function _is_literal($str)
2700 {
2701 $str = trim($str);
2702
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002703 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 +03002704 {
2705 return TRUE;
2706 }
2707
2708 static $_str;
2709
2710 if (empty($_str))
2711 {
2712 $_str = ($this->_escape_char !== '"')
2713 ? array('"', "'") : array("'");
2714 }
2715
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002716 return in_array($str[0], $_str, TRUE);
Andrey Andreev082aa402012-10-22 19:41:55 +03002717 }
2718
2719 // --------------------------------------------------------------------
2720
2721 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002722 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002723 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002724 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002725 *
Andrey Andreev435e0c22014-12-11 16:30:13 +02002726 * @return CI_DB_query_builder
Kyle Farris0c147b32011-08-26 02:29:31 -04002727 */
2728 public function reset_query()
2729 {
2730 $this->_reset_select();
2731 $this->_reset_write();
Andrey Andreev435e0c22014-12-11 16:30:13 +02002732 return $this;
Kyle Farris0c147b32011-08-26 02:29:31 -04002733 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002734
2735 // --------------------------------------------------------------------
2736
2737 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002738 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002739 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002740 * @param array An array of fields to reset
2741 * @return void
2742 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002743 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002744 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002745 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002746 {
Andrey Andreevae85eb42012-11-02 01:42:31 +02002747 $this->$item = $default_value;
Derek Allard2067d1a2008-11-13 22:59:24 +00002748 }
2749 }
2750
2751 // --------------------------------------------------------------------
2752
2753 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002754 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002755 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002756 * @return void
2757 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002758 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002759 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002760 $this->_reset_run(array(
Andrey Andreev4a587f52014-12-11 16:27:15 +02002761 'qb_select' => array(),
2762 'qb_from' => array(),
2763 'qb_join' => array(),
2764 'qb_where' => array(),
2765 'qb_groupby' => array(),
2766 'qb_having' => array(),
2767 'qb_orderby' => array(),
2768 'qb_aliased_tables' => array(),
2769 'qb_no_escape' => array(),
2770 'qb_distinct' => FALSE,
2771 'qb_limit' => FALSE,
2772 'qb_offset' => FALSE
2773 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002774 }
Barry Mienydd671972010-10-04 16:33:58 +02002775
Derek Allard2067d1a2008-11-13 22:59:24 +00002776 // --------------------------------------------------------------------
2777
2778 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002779 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002780 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002781 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002782 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002783 * @return void
2784 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002785 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002786 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002787 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002788 'qb_set' => array(),
Andrey Andreev8338bbb2016-12-12 14:17:52 +02002789 'qb_set_ub' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002790 'qb_from' => array(),
Andrey Andreev3e014372013-02-21 15:59:34 +02002791 'qb_join' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002792 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002793 'qb_orderby' => array(),
2794 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002795 'qb_limit' => FALSE
Andrey Andreev4a587f52014-12-11 16:27:15 +02002796 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002797 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002798
Derek Allard2067d1a2008-11-13 22:59:24 +00002799}