blob: 5c0528a3fd2c54f7948727556049369b8ff8b0cf [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
WanWizard7219c072011-12-28 14:09:05 +01008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
WanWizard7219c072011-12-28 14:09:05 +010010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, 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 /**
Andrey Andreev437ffe02017-01-17 12:44:19 +0200218 * QB Cache aliased tables list
219 *
220 * @var array
221 */
222 protected $qb_cache_aliased_tables = array();
223
224 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200225 * QB Cache WHERE data
226 *
227 * @var array
228 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000229 protected $qb_cache_where = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200230
231 /**
232 * QB Cache GROUP BY data
233 *
234 * @var array
235 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000236 protected $qb_cache_groupby = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200237
238 /**
239 * QB Cache HAVING data
240 *
241 * @var array
242 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000243 protected $qb_cache_having = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200244
245 /**
246 * QB Cache ORDER BY data
247 *
248 * @var array
249 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000250 protected $qb_cache_orderby = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200251
252 /**
253 * QB Cache data sets
254 *
255 * @var array
256 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000257 protected $qb_cache_set = array();
WanWizard7219c072011-12-28 14:09:05 +0100258
Andrey Andreevae85eb42012-11-02 01:42:31 +0200259 /**
260 * QB No Escape data
261 *
262 * @var array
263 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000264 protected $qb_no_escape = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200265
266 /**
267 * QB Cache No Escape data
268 *
269 * @var array
270 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000271 protected $qb_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000272
Andrey Andreevae85eb42012-11-02 01:42:31 +0200273 // --------------------------------------------------------------------
274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 /**
276 * Select
277 *
278 * Generates the SELECT portion of the query
279 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300281 * @param mixed
Andrew Podner4296a652012-12-17 07:51:15 -0500282 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600284 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 if (is_string($select))
287 {
288 $select = explode(',', $select);
289 }
290
Vivek Dinesh33578d22014-02-06 09:46:07 +0530291 // If the escape value was not set, we will base it on the global setting
Andrey Andreev42870232012-06-12 01:30:20 +0300292 is_bool($escape) OR $escape = $this->_protect_identifiers;
293
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 foreach ($select as $val)
295 {
296 $val = trim($val);
297
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100298 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000300 $this->qb_select[] = $val;
301 $this->qb_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000302
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000303 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000305 $this->qb_cache_select[] = $val;
306 $this->qb_cache_exists[] = 'select';
307 $this->qb_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
309 }
310 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 return $this;
313 }
314
315 // --------------------------------------------------------------------
316
317 /**
318 * Select Max
319 *
320 * Generates a SELECT MAX(field) portion of a query
321 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 * @param string the field
323 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500324 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600326 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
328 return $this->_max_min_avg_sum($select, $alias, 'MAX');
329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 // --------------------------------------------------------------------
332
333 /**
334 * Select Min
335 *
336 * Generates a SELECT MIN(field) portion of a query
337 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 * @param string the field
339 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500340 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600342 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 {
344 return $this->_max_min_avg_sum($select, $alias, 'MIN');
345 }
346
347 // --------------------------------------------------------------------
348
349 /**
350 * Select Average
351 *
352 * Generates a SELECT AVG(field) portion of a query
353 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 * @param string the field
355 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500356 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600358 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
360 return $this->_max_min_avg_sum($select, $alias, 'AVG');
361 }
362
363 // --------------------------------------------------------------------
364
365 /**
366 * Select Sum
367 *
368 * Generates a SELECT SUM(field) portion of a query
369 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 * @param string the field
371 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500372 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600374 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
376 return $this->_max_min_avg_sum($select, $alias, 'SUM');
377 }
378
379 // --------------------------------------------------------------------
380
381 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200382 * SELECT [MAX|MIN|AVG|SUM]()
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200384 * @used-by select_max()
385 * @used-by select_min()
386 * @used-by select_avg()
387 * @used-by select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200388 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200389 * @param string $select Field name
390 * @param string $alias
391 * @param string $type
Andrew Podner4296a652012-12-17 07:51:15 -0500392 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600394 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100396 if ( ! is_string($select) OR $select === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
398 $this->display_error('db_invalid_query');
399 }
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
404 {
405 show_error('Invalid function type: '.$type);
406 }
Barry Mienydd671972010-10-04 16:33:58 +0200407
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100408 if ($alias === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
410 $alias = $this->_create_alias_from_table(trim($select));
411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Andrey Andreev5b55c152013-08-06 14:14:32 +0300413 $sql = $type.'('.$this->protect_identifiers(trim($select)).') AS '.$this->escape_identifiers(trim($alias));
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300414
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000415 $this->qb_select[] = $sql;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100416 $this->qb_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200417
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000418 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000420 $this->qb_cache_select[] = $sql;
421 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 return $this;
425 }
426
427 // --------------------------------------------------------------------
428
429 /**
430 * Determines the alias name based on the table
431 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200432 * @param string $item
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 * @return string
434 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600435 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 {
437 if (strpos($item, '.') !== FALSE)
438 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200439 $item = explode('.', $item);
440 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 }
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 return $item;
444 }
445
446 // --------------------------------------------------------------------
447
448 /**
449 * DISTINCT
450 *
451 * Sets a flag which tells the query string compiler to add DISTINCT
452 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200453 * @param bool $val
Andrew Podner4296a652012-12-17 07:51:15 -0500454 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600456 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300458 $this->qb_distinct = is_bool($val) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 return $this;
460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 // --------------------------------------------------------------------
463
464 /**
465 * From
466 *
467 * Generates the FROM portion of the query
468 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200469 * @param mixed $from can be a string or array
Andrew Podner4296a652012-12-17 07:51:15 -0500470 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600472 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300474 foreach ((array) $from as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
476 if (strpos($val, ',') !== FALSE)
477 {
478 foreach (explode(',', $val) as $v)
479 {
480 $v = trim($v);
481 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200482
George Petsagourakis193d4482012-04-28 11:16:18 +0300483 $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000484
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000485 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000487 $this->qb_cache_from[] = $v;
488 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200489 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 }
492 else
493 {
494 $val = trim($val);
495
Andrey Andreev24276a32012-01-08 02:44:38 +0200496 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000497 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200499
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000500 $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000501
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000502 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000504 $this->qb_cache_from[] = $val;
505 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 }
507 }
508 }
509
510 return $this;
511 }
512
513 // --------------------------------------------------------------------
514
515 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200516 * JOIN
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 *
518 * Generates the JOIN portion of the query
519 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 * @param string
521 * @param string the join condition
522 * @param string the type of join
Alex Bilbief512b732012-06-16 11:15:19 +0100523 * @param string whether not to try to escape identifiers
Andrew Podner4296a652012-12-17 07:51:15 -0500524 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300526 public function join($table, $cond, $type = '', $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200527 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100528 if ($type !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 {
530 $type = strtoupper(trim($type));
531
Andrey Andreev42870232012-06-12 01:30:20 +0300532 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
534 $type = '';
535 }
536 else
537 {
538 $type .= ' ';
539 }
540 }
541
Andrey Andreev24276a32012-01-08 02:44:38 +0200542 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000543 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 $this->_track_aliases($table);
545
Andrey Andreevfe642da2012-06-16 03:47:33 +0300546 is_bool($escape) OR $escape = $this->_protect_identifiers;
547
Andrey Andreevc6011942016-02-11 23:49:37 +0200548 if ( ! $this->_has_operator($cond))
Andrey Andreev3751f932012-06-17 18:07:48 +0300549 {
550 $cond = ' USING ('.($escape ? $this->escape_identifiers($cond) : $cond).')';
551 }
Andrey Andreevc6011942016-02-11 23:49:37 +0200552 elseif ($escape === FALSE)
Andrey Andreev3751f932012-06-17 18:07:48 +0300553 {
554 $cond = ' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 }
Andrey Andreevc6011942016-02-11 23:49:37 +0200556 else
557 {
558 // Split multiple conditions
559 if (preg_match_all('/\sAND\s|\sOR\s/i', $cond, $joints, PREG_OFFSET_CAPTURE))
560 {
561 $conditions = array();
562 $joints = $joints[0];
563 array_unshift($joints, array('', 0));
564
565 for ($i = count($joints) - 1, $pos = strlen($cond); $i >= 0; $i--)
566 {
567 $joints[$i][1] += strlen($joints[$i][0]); // offset
568 $conditions[$i] = substr($cond, $joints[$i][1], $pos - $joints[$i][1]);
569 $pos = $joints[$i][1] - strlen($joints[$i][0]);
570 $joints[$i] = $joints[$i][0];
571 }
572 }
573 else
574 {
575 $conditions = array($cond);
576 $joints = array('');
577 }
578
579 $cond = ' ON ';
580 for ($i = 0, $c = count($conditions); $i < $c; $i++)
581 {
582 $operator = $this->_get_operator($conditions[$i]);
583 $cond .= $joints[$i];
584 $cond .= preg_match("/(\(*)?([\[\]\w\.'-]+)".preg_quote($operator)."(.*)/i", $conditions[$i], $match)
585 ? $match[1].$this->protect_identifiers($match[2]).$operator.$this->protect_identifiers($match[3])
586 : $conditions[$i];
587 }
588 }
Barry Mienydd671972010-10-04 16:33:58 +0200589
Andrey Andreev42870232012-06-12 01:30:20 +0300590 // Do we want to escape the table name?
591 if ($escape === TRUE)
592 {
593 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
594 }
595
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 // Assemble the JOIN statement
Andrey Andreev3751f932012-06-17 18:07:48 +0300597 $this->qb_join[] = $join = $type.'JOIN '.$table.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000598
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000599 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000601 $this->qb_cache_join[] = $join;
602 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 }
604
605 return $this;
606 }
607
608 // --------------------------------------------------------------------
609
610 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200611 * WHERE
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200613 * Generates the WHERE portion of the query.
614 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 * @param mixed
617 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300618 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500619 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300621 public function where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300623 return $this->_wh('qb_where', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 }
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 // --------------------------------------------------------------------
627
628 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200629 * OR WHERE
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200631 * Generates the WHERE portion of the query.
632 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 * @param mixed
635 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300636 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500637 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300639 public function or_where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300641 return $this->_wh('qb_where', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 }
643
644 // --------------------------------------------------------------------
645
646 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +0300647 * WHERE, HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200649 * @used-by where()
650 * @used-by or_where()
651 * @used-by having()
652 * @used-by or_having()
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200654 * @param string $qb_key 'qb_where' or 'qb_having'
655 * @param mixed $key
656 * @param mixed $value
657 * @param string $type
658 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500659 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 */
Andrey Andreevd40459d2012-07-18 16:46:39 +0300661 protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300663 $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where';
664
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 if ( ! is_array($key))
666 {
667 $key = array($key => $value);
668 }
Barry Mienydd671972010-10-04 16:33:58 +0200669
Rougin Royce Gutib191550a2014-08-24 16:19:08 +0800670 // If the escape value was not set will base it on the global setting
Andrey Andreeve10fb792012-06-15 12:07:04 +0300671 is_bool($escape) OR $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000672
673 foreach ($key as $k => $v)
674 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300675 $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0)
Andrey Andreev58803fb2012-06-24 00:45:37 +0300676 ? $this->_group_get_type('')
677 : $this->_group_get_type($type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000678
vlakoff1228fe22013-01-14 01:30:09 +0100679 if ($v !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 {
Andrey Andreevf7631f12015-07-27 21:54:41 +0300681 if ($escape === TRUE)
682 {
Andrey Andreev7dd6f142018-01-30 15:08:21 +0200683 $v = $this->escape($v);
Andrey Andreevf7631f12015-07-27 21:54:41 +0300684 }
WanWizard7219c072011-12-28 14:09:05 +0100685
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 if ( ! $this->_has_operator($k))
687 {
Greg Akere156c6e2011-04-20 16:03:04 -0500688 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 }
690 }
Andrey Andreev3a5efc22012-11-20 21:18:08 +0200691 elseif ( ! $this->_has_operator($k))
692 {
693 // value appears not to have been set, assign the test to IS NULL
694 $k .= ' IS NULL';
695 }
Andrey Andreevbe4bab92016-10-28 12:50:03 +0300696 elseif (preg_match('/\s*(!?=|<>|\sIS(?:\s+NOT)?\s)\s*$/i', $k, $match, PREG_OFFSET_CAPTURE))
Andrey Andreev5bf4dcd2014-09-29 20:07:15 +0300697 {
698 $k = substr($k, 0, $match[0][1]).($match[1][0] === '=' ? ' IS NULL' : ' IS NOT NULL');
699 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000700
Andrey Andreev7dd6f142018-01-30 15:08:21 +0200701 ${$qb_key} = array('condition' => $prefix.$k, 'value' => $v, 'escape' => $escape);
702 $this->{$qb_key}[] = ${$qb_key};
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000703 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 {
Andrey Andreev7dd6f142018-01-30 15:08:21 +0200705 $this->{$qb_cache_key}[] = ${$qb_key};
Andrey Andreevd40459d2012-07-18 16:46:39 +0300706 $this->qb_cache_exists[] = substr($qb_key, 3);
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 }
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 }
Barry Mienydd671972010-10-04 16:33:58 +0200710
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 return $this;
712 }
713
714 // --------------------------------------------------------------------
715
716 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200717 * WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200719 * Generates a WHERE field IN('item', 'item') SQL query,
720 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200722 * @param string $key The field to search
723 * @param array $values The values searched on
724 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500725 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300727 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300729 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 }
Barry Mienydd671972010-10-04 16:33:58 +0200731
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 // --------------------------------------------------------------------
733
734 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200735 * OR WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200737 * Generates a WHERE field IN('item', 'item') SQL query,
738 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200740 * @param string $key The field to search
741 * @param array $values The values searched on
742 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500743 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300745 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300747 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 }
749
750 // --------------------------------------------------------------------
751
752 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200753 * WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200755 * Generates a WHERE field NOT IN('item', 'item') SQL query,
756 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200758 * @param string $key The field to search
759 * @param array $values The values searched on
760 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500761 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300763 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300765 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 }
Barry Mienydd671972010-10-04 16:33:58 +0200767
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 // --------------------------------------------------------------------
769
770 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200771 * OR WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200773 * Generates a WHERE field NOT IN('item', 'item') SQL query,
774 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200776 * @param string $key The field to search
777 * @param array $values The values searched on
778 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500779 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300781 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300783 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 }
785
786 // --------------------------------------------------------------------
787
788 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200789 * Internal WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200791 * @used-by where_in()
792 * @used-by or_where_in()
793 * @used-by where_not_in()
794 * @used-by or_where_not_in()
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200796 * @param string $key The field to search
797 * @param array $values The values searched on
798 * @param bool $not If the statement would be IN or NOT IN
799 * @param string $type
800 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500801 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300803 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 {
805 if ($key === NULL OR $values === NULL)
806 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300807 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 }
Barry Mienydd671972010-10-04 16:33:58 +0200809
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 if ( ! is_array($values))
811 {
812 $values = array($values);
813 }
Barry Mienydd671972010-10-04 16:33:58 +0200814
Andrey Andreev498c1e02012-06-16 03:34:10 +0300815 is_bool($escape) OR $escape = $this->_protect_identifiers;
816
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 $not = ($not) ? ' NOT' : '';
818
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300819 if ($escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 {
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300821 $where_in = array();
822 foreach ($values as $value)
823 {
824 $where_in[] = $this->escape($value);
825 }
826 }
827 else
828 {
829 $where_in = array_values($values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 }
831
Andrey Andreev0d2d84f2015-07-31 13:48:59 +0300832 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
833 ? $this->_group_get_type('')
834 : $this->_group_get_type($type);
835
Andrey Andreev6e704752012-07-18 00:46:33 +0300836 $where_in = array(
Andrey Andreev94611df2012-07-19 12:29:54 +0300837 'condition' => $prefix.$key.$not.' IN('.implode(', ', $where_in).')',
Andrey Andreev7dd6f142018-01-30 15:08:21 +0200838 'value' => NULL,
Andrey Andreev6e704752012-07-18 00:46:33 +0300839 'escape' => $escape
840 );
Barry Mienydd671972010-10-04 16:33:58 +0200841
Andrey Andreev6e704752012-07-18 00:46:33 +0300842 $this->qb_where[] = $where_in;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000843 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000845 $this->qb_cache_where[] = $where_in;
846 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 }
848
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 return $this;
850 }
Barry Mienydd671972010-10-04 16:33:58 +0200851
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 // --------------------------------------------------------------------
853
854 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200855 * LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200857 * Generates a %LIKE% portion of the query.
858 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200860 * @param mixed $field
861 * @param string $match
862 * @param string $side
863 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500864 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300866 public function like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300868 return $this->_like($field, $match, 'AND ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 }
870
871 // --------------------------------------------------------------------
872
873 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200874 * NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200876 * Generates a NOT LIKE portion of the query.
877 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200879 * @param mixed $field
880 * @param string $match
881 * @param string $side
882 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500883 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300885 public function not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300887 return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 }
Barry Mienydd671972010-10-04 16:33:58 +0200889
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 // --------------------------------------------------------------------
891
892 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200893 * OR LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200895 * Generates a %LIKE% portion of the query.
896 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200898 * @param mixed $field
899 * @param string $match
900 * @param string $side
901 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500902 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300904 public function or_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300906 return $this->_like($field, $match, 'OR ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 }
908
909 // --------------------------------------------------------------------
910
911 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200912 * OR NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200914 * Generates a NOT LIKE portion of the query.
915 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200917 * @param mixed $field
918 * @param string $match
919 * @param string $side
920 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500921 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300923 public function or_not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300925 return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 }
Barry Mienydd671972010-10-04 16:33:58 +0200927
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 // --------------------------------------------------------------------
929
930 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200931 * Internal LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200933 * @used-by like()
934 * @used-by or_like()
935 * @used-by not_like()
936 * @used-by or_not_like()
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200938 * @param mixed $field
939 * @param string $match
940 * @param string $type
941 * @param string $side
942 * @param string $not
943 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500944 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300946 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 {
948 if ( ! is_array($field))
949 {
950 $field = array($field => $match);
951 }
Barry Mienydd671972010-10-04 16:33:58 +0200952
Andrey Andreevb0478652012-07-18 15:34:46 +0300953 is_bool($escape) OR $escape = $this->_protect_identifiers;
Andrey Andreev19311362015-04-07 00:02:14 +0300954 // lowercase $side in case somebody writes e.g. 'BEFORE' instead of 'before' (doh)
955 $side = strtolower($side);
Andrey Andreevb0478652012-07-18 15:34:46 +0300956
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 foreach ($field as $k => $v)
958 {
Andrey Andreev41738232012-11-30 00:13:17 +0200959 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
960 ? $this->_group_get_type('') : $this->_group_get_type($type);
961
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300962 if ($escape === TRUE)
963 {
964 $v = $this->escape_like_str($v);
965 }
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300966
Andrey Andreev7dd6f142018-01-30 15:08:21 +0200967 switch ($side)
Kyle Farris81ef70f2011-08-31 11:59:12 -0400968 {
Andrey Andreev7dd6f142018-01-30 15:08:21 +0200969 case 'none':
970 $v = "'{$v}'";
971 break;
972 case 'before':
Andrey Andreev93411892018-04-14 16:45:56 +0300973 $v = "'%{$v}'";
Andrey Andreev7dd6f142018-01-30 15:08:21 +0200974 break;
975 case 'after':
976 $v = "'{$v}%'";
977 break;
978 case 'both':
979 default:
980 $v = "'%{$v}%'";
981 break;
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600983
Derek Jonese4ed5832009-02-20 21:44:59 +0000984 // some platforms require an escape sequence definition for LIKE wildcards
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300985 if ($escape === TRUE && $this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000986 {
Andrey Andreev7dd6f142018-01-30 15:08:21 +0200987 $v .= sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000988 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600989
Andrey Andreev68b0e082018-04-13 13:08:52 +0300990 $qb_where = array('condition' => "{$prefix} {$k} {$not} LIKE {$v}", 'value' => NULL, 'escape' => $escape);
Andrey Andreev7dd6f142018-01-30 15:08:21 +0200991 $this->qb_where[] = $qb_where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000992 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 {
Andrey Andreev7dd6f142018-01-30 15:08:21 +0200994 $this->qb_cache_where[] = $qb_where;
Andrey Andreevededc4a2012-07-18 01:16:15 +0300995 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200998
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 return $this;
1000 }
Barry Mienydd671972010-10-04 16:33:58 +02001001
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 // --------------------------------------------------------------------
1003
1004 /**
WanWizard7219c072011-12-28 14:09:05 +01001005 * Starts a query group.
1006 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001007 * @param string $not (Internal use only)
1008 * @param string $type (Internal use only)
Andrew Podner4296a652012-12-17 07:51:15 -05001009 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001010 */
1011 public function group_start($not = '', $type = 'AND ')
1012 {
1013 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +01001014
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001015 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001016 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Andrey Andreev6e704752012-07-18 00:46:33 +03001017 $where = array(
1018 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (',
Andrey Andreev7dd6f142018-01-30 15:08:21 +02001019 'value' => NULL,
Andrey Andreev6e704752012-07-18 00:46:33 +03001020 'escape' => FALSE
1021 );
WanWizard7219c072011-12-28 14:09:05 +01001022
Andrey Andreev6e704752012-07-18 00:46:33 +03001023 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001024 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +01001025 {
Andrey Andreev6e704752012-07-18 00:46:33 +03001026 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +01001027 }
1028
1029 return $this;
1030 }
1031
1032 // --------------------------------------------------------------------
1033
1034 /**
1035 * Starts a query group, but ORs the group
1036 *
Andrew Podner4296a652012-12-17 07:51:15 -05001037 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001038 */
1039 public function or_group_start()
1040 {
1041 return $this->group_start('', 'OR ');
1042 }
1043
1044 // --------------------------------------------------------------------
1045
1046 /**
1047 * Starts a query group, but NOTs the group
1048 *
Andrew Podner4296a652012-12-17 07:51:15 -05001049 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001050 */
1051 public function not_group_start()
1052 {
1053 return $this->group_start('NOT ', 'AND ');
1054 }
1055
1056 // --------------------------------------------------------------------
1057
1058 /**
1059 * Starts a query group, but OR NOTs the group
1060 *
Andrew Podner4296a652012-12-17 07:51:15 -05001061 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001062 */
1063 public function or_not_group_start()
1064 {
1065 return $this->group_start('NOT ', 'OR ');
1066 }
1067
1068 // --------------------------------------------------------------------
1069
1070 /**
1071 * Ends a query group
1072 *
Andrew Podner4296a652012-12-17 07:51:15 -05001073 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001074 */
1075 public function group_end()
1076 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001077 $this->qb_where_group_started = FALSE;
Andrey Andreev6e704752012-07-18 00:46:33 +03001078 $where = array(
1079 'condition' => str_repeat(' ', $this->qb_where_group_count--).')',
Andrey Andreev7dd6f142018-01-30 15:08:21 +02001080 'value' => NULL,
Andrey Andreev6e704752012-07-18 00:46:33 +03001081 'escape' => FALSE
1082 );
WanWizard7219c072011-12-28 14:09:05 +01001083
Andrey Andreev6e704752012-07-18 00:46:33 +03001084 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001085 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +01001086 {
Andrey Andreev6e704752012-07-18 00:46:33 +03001087 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +01001088 }
1089
WanWizard7219c072011-12-28 14:09:05 +01001090 return $this;
1091 }
1092
1093 // --------------------------------------------------------------------
1094
1095 /**
1096 * Group_get_type
1097 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001098 * @used-by group_start()
1099 * @used-by _like()
1100 * @used-by _wh()
1101 * @used-by _where_in()
WanWizard7219c072011-12-28 14:09:05 +01001102 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001103 * @param string $type
WanWizard7219c072011-12-28 14:09:05 +01001104 * @return string
1105 */
1106 protected function _group_get_type($type)
1107 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001108 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +01001109 {
1110 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001111 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +01001112 }
1113
1114 return $type;
1115 }
1116
1117 // --------------------------------------------------------------------
1118
1119 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 * GROUP BY
1121 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001122 * @param string $by
1123 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -05001124 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001126 public function group_by($by, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001128 is_bool($escape) OR $escape = $this->_protect_identifiers;
1129
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 if (is_string($by))
1131 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001132 $by = ($escape === TRUE)
1133 ? explode(',', $by)
1134 : array($by);
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 }
Barry Mienydd671972010-10-04 16:33:58 +02001136
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 foreach ($by as $val)
1138 {
1139 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +02001140
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001141 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001143 $val = array('field' => $val, 'escape' => $escape);
Barry Mienydd671972010-10-04 16:33:58 +02001144
Andrey Andreev96feb582012-07-19 13:12:34 +03001145 $this->qb_groupby[] = $val;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001146 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001148 $this->qb_cache_groupby[] = $val;
1149 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 }
1151 }
1152 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001153
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 return $this;
1155 }
1156
1157 // --------------------------------------------------------------------
1158
1159 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001160 * HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001162 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001164 * @param string $key
1165 * @param string $value
1166 * @param bool $escape
Andrey Andreevaec51262016-02-09 21:11:07 +02001167 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001169 public function having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001171 return $this->_wh('qb_having', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 }
Barry Mienydd671972010-10-04 16:33:58 +02001173
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 // --------------------------------------------------------------------
1175
1176 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001177 * OR HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001179 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001181 * @param string $key
1182 * @param string $value
1183 * @param bool $escape
Andrey Andreevaec51262016-02-09 21:11:07 +02001184 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001186 public function or_having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001187 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001188 return $this->_wh('qb_having', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 }
Barry Mienydd671972010-10-04 16:33:58 +02001190
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 // --------------------------------------------------------------------
1192
1193 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001194 * ORDER BY
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001196 * @param string $orderby
vlakoff9f8e2992013-09-08 14:05:04 +02001197 * @param string $direction ASC, DESC or RANDOM
Andrey Andreevae85eb42012-11-02 01:42:31 +02001198 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -05001199 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 */
Andrey Andreevd24160c2012-06-16 03:21:20 +03001201 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001203 $direction = strtoupper(trim($direction));
Andrey Andreev2d486232012-07-19 14:46:51 +03001204
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001205 if ($direction === 'RANDOM')
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001207 $direction = '';
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001208
1209 // Do we have a seed value?
1210 $orderby = ctype_digit((string) $orderby)
vlakoffe6c4d5b2013-09-08 13:54:57 +02001211 ? sprintf($this->_random_keyword[1], $orderby)
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001212 : $this->_random_keyword[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 }
Andrey Andreev2d486232012-07-19 14:46:51 +03001214 elseif (empty($orderby))
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001216 return $this;
1217 }
1218 elseif ($direction !== '')
1219 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001220 $direction = in_array($direction, array('ASC', 'DESC'), TRUE) ? ' '.$direction : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 }
Barry Mienydd671972010-10-04 16:33:58 +02001222
Andrey Andreevd24160c2012-06-16 03:21:20 +03001223 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +02001224
Andrey Andreev2d486232012-07-19 14:46:51 +03001225 if ($escape === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 {
Andrey Andreev93dd2f22012-10-24 10:09:18 +03001227 $qb_orderby[] = array('field' => $orderby, 'direction' => $direction, 'escape' => FALSE);
Andrey Andreev2d486232012-07-19 14:46:51 +03001228 }
1229 else
1230 {
1231 $qb_orderby = array();
1232 foreach (explode(',', $orderby) as $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001234 $qb_orderby[] = ($direction === '' && preg_match('/\s+(ASC|DESC)$/i', rtrim($field), $match, PREG_OFFSET_CAPTURE))
1235 ? array('field' => ltrim(substr($field, 0, $match[0][1])), 'direction' => ' '.$match[1][0], 'escape' => TRUE)
1236 : array('field' => trim($field), 'direction' => $direction, 'escape' => TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 }
Barry Mienydd671972010-10-04 16:33:58 +02001239
Andrey Andreev2d486232012-07-19 14:46:51 +03001240 $this->qb_orderby = array_merge($this->qb_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001241 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001243 $this->qb_cache_orderby = array_merge($this->qb_cache_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001244 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 }
1246
1247 return $this;
1248 }
Barry Mienydd671972010-10-04 16:33:58 +02001249
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 // --------------------------------------------------------------------
1251
1252 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001253 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001255 * @param int $value LIMIT value
1256 * @param int $offset OFFSET value
Andrew Podner4296a652012-12-17 07:51:15 -05001257 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001258 */
Andrey Andreev4a587f52014-12-11 16:27:15 +02001259 public function limit($value, $offset = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 {
vlakoff912f1bc2013-01-15 03:34:12 +01001261 is_null($value) OR $this->qb_limit = (int) $value;
Andrey Andreev777153d2012-06-18 13:30:45 +03001262 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001263
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 return $this;
1265 }
Barry Mienydd671972010-10-04 16:33:58 +02001266
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 // --------------------------------------------------------------------
1268
1269 /**
1270 * Sets the OFFSET value
1271 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001272 * @param int $offset OFFSET value
Andrew Podner4296a652012-12-17 07:51:15 -05001273 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001274 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001275 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001277 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 return $this;
1279 }
Barry Mienydd671972010-10-04 16:33:58 +02001280
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 // --------------------------------------------------------------------
1282
1283 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001284 * LIMIT string
Andrey Andreev2c35b642012-06-24 03:05:26 +03001285 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001286 * Generates a platform-specific LIMIT clause.
Andrey Andreev2c35b642012-06-24 03:05:26 +03001287 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001288 * @param string $sql SQL Query
Andrey Andreev2c35b642012-06-24 03:05:26 +03001289 * @return string
1290 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001291 protected function _limit($sql)
Andrey Andreev2c35b642012-06-24 03:05:26 +03001292 {
Andrey Andreevacc64812016-07-29 11:42:28 +03001293 return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').(int) $this->qb_limit;
Andrey Andreev2c35b642012-06-24 03:05:26 +03001294 }
1295
1296 // --------------------------------------------------------------------
1297
1298 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001299 * The "set" function.
1300 *
1301 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 * @param mixed
1304 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001305 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001306 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001308 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001309 {
1310 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001311
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 if ( ! is_array($key))
1313 {
1314 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001315 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001316
Andrey Andreevfe642da2012-06-16 03:47:33 +03001317 is_bool($escape) OR $escape = $this->_protect_identifiers;
1318
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 foreach ($key as $k => $v)
1320 {
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001321 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1322 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001323 }
Barry Mienydd671972010-10-04 16:33:58 +02001324
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 return $this;
1326 }
WanWizard7219c072011-12-28 14:09:05 +01001327
Kyle Farris0c147b32011-08-26 02:29:31 -04001328 // --------------------------------------------------------------------
1329
1330 /**
1331 * Get SELECT query string
1332 *
1333 * Compiles a SELECT query string and returns the sql.
1334 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001335 * @param string the table name to select from (optional)
Calvin Tam55bc5052015-07-24 02:27:24 -07001336 * @param bool TRUE: resets QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001337 * @return string
1338 */
WanWizard7219c072011-12-28 14:09:05 +01001339 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001340 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001341 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001342 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001343 $this->_track_aliases($table);
1344 $this->from($table);
1345 }
WanWizard7219c072011-12-28 14:09:05 +01001346
Andrey Andreev650b4c02012-06-11 12:07:15 +03001347 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001348
Kyle Farris0c147b32011-08-26 02:29:31 -04001349 if ($reset === TRUE)
1350 {
1351 $this->_reset_select();
1352 }
WanWizard7219c072011-12-28 14:09:05 +01001353
Kyle Farris0c147b32011-08-26 02:29:31 -04001354 return $select;
1355 }
WanWizard7219c072011-12-28 14:09:05 +01001356
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 // --------------------------------------------------------------------
1358
1359 /**
1360 * Get
1361 *
1362 * Compiles the select statement based on the other functions called
1363 * and runs the query
1364 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 * @param string the table
1366 * @param string the limit clause
1367 * @param string the offset clause
Andrey Andreevaec51262016-02-09 21:11:07 +02001368 * @return CI_DB_result
Derek Allard2067d1a2008-11-13 22:59:24 +00001369 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001370 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001372 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 {
1374 $this->_track_aliases($table);
1375 $this->from($table);
1376 }
Barry Mienydd671972010-10-04 16:33:58 +02001377
Andrey Andreev650b4c02012-06-11 12:07:15 +03001378 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001379 {
1380 $this->limit($limit, $offset);
1381 }
Barry Mienydd671972010-10-04 16:33:58 +02001382
Andrey Andreev24276a32012-01-08 02:44:38 +02001383 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 $this->_reset_select();
1385 return $result;
1386 }
1387
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001388 // --------------------------------------------------------------------
1389
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 /**
1391 * "Count All Results" query
1392 *
Barry Mienydd671972010-10-04 16:33:58 +02001393 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001394 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001395 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001396 * @param string
yaoshanliang2f164052015-03-16 16:48:15 +08001397 * @param bool the reset clause
vlakoffc6ac7482013-11-17 00:50:06 +01001398 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +00001399 */
yaoshanliang19c28472015-03-15 10:42:18 +08001400 public function count_all_results($table = '', $reset = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001402 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001403 {
1404 $this->_track_aliases($table);
1405 $this->from($table);
1406 }
Barry Mienydd671972010-10-04 16:33:58 +02001407
Andrey Andreev075bdb42016-01-25 13:31:23 +02001408 // ORDER BY usage is often problematic here (most notably
1409 // on Microsoft SQL Server) and ultimately unnecessary
1410 // for selecting COUNT(*) ...
Andrey Andreev71a78c22017-07-10 14:51:08 +03001411 $qb_orderby = $this->qb_orderby;
Andrey Andreev59bae572017-07-03 14:13:08 +03001412 $qb_cache_orderby = $this->qb_cache_orderby;
pgee70cf3924e2017-10-03 10:09:04 +11001413 $this->qb_orderby = $this->qb_cache_orderby = array();
Andrey Andreev075bdb42016-01-25 13:31:23 +02001414
Andrey Andreev2c1b3d92017-06-15 14:22:49 +03001415 $result = ($this->qb_distinct === TRUE OR ! empty($this->qb_groupby) OR ! empty($this->qb_cache_groupby) OR $this->qb_limit OR $this->qb_offset)
Andrey Andreevb05f5062012-10-26 12:01:02 +03001416 ? $this->query($this->_count_string.$this->protect_identifiers('numrows')."\nFROM (\n".$this->_compile_select()."\n) CI_count_all_results")
1417 : $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
yaoshanliang9971e7b2015-03-14 13:09:16 +08001418
yaoshanliang2f164052015-03-16 16:48:15 +08001419 if ($reset === TRUE)
yaoshanliang19c28472015-03-15 10:42:18 +08001420 {
yaoshanliang2f164052015-03-16 16:48:15 +08001421 $this->_reset_select();
yaoshanliang19c28472015-03-15 10:42:18 +08001422 }
Andrey Andreev59bae572017-07-03 14:13:08 +03001423 else
Andrey Andreev075bdb42016-01-25 13:31:23 +02001424 {
Andrey Andreev59bae572017-07-03 14:13:08 +03001425 $this->qb_orderby = $qb_orderby;
1426 $this->qb_cache_orderby = $qb_cache_orderby;
Andrey Andreev075bdb42016-01-25 13:31:23 +02001427 }
Barry Mienydd671972010-10-04 16:33:58 +02001428
Purwandi1d160e72012-01-09 16:33:28 +07001429 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001430 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001431 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001432 }
1433
Purwandi1d160e72012-01-09 16:33:28 +07001434 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001435 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001436 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001437
Derek Allard2067d1a2008-11-13 22:59:24 +00001438 // --------------------------------------------------------------------
1439
1440 /**
Andrey Andreev7dd6f142018-01-30 15:08:21 +02001441 * get_where()
Derek Allard2067d1a2008-11-13 22:59:24 +00001442 *
1443 * Allows the where clause, limit and offset to be added directly
1444 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001445 * @param string $table
1446 * @param string $where
1447 * @param int $limit
1448 * @param int $offset
Andrey Andreevaec51262016-02-09 21:11:07 +02001449 * @return CI_DB_result
Derek Allard2067d1a2008-11-13 22:59:24 +00001450 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001451 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001452 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001453 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001454 {
1455 $this->from($table);
1456 }
1457
vlakoff1228fe22013-01-14 01:30:09 +01001458 if ($where !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 {
1460 $this->where($where);
1461 }
Barry Mienydd671972010-10-04 16:33:58 +02001462
Andrey Andreev650b4c02012-06-11 12:07:15 +03001463 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001464 {
1465 $this->limit($limit, $offset);
1466 }
Barry Mienydd671972010-10-04 16:33:58 +02001467
Andrey Andreev24276a32012-01-08 02:44:38 +02001468 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001469 $this->_reset_select();
1470 return $result;
1471 }
1472
1473 // --------------------------------------------------------------------
1474
1475 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001476 * Insert_Batch
1477 *
1478 * Compiles batch insert strings and runs the queries
1479 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001480 * @param string $table Table to insert into
1481 * @param array $set An associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001482 * @param bool $escape Whether to escape values and identifiers
Andrey Andreevae85eb42012-11-02 01:42:31 +02001483 * @return int Number of rows inserted or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001484 */
Andrey Andreev105a48b2016-02-04 15:45:10 +02001485 public function insert_batch($table, $set = NULL, $escape = NULL, $batch_size = 100)
Barry Mienydd671972010-10-04 16:33:58 +02001486 {
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001487 if ($set === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001488 {
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001489 if (empty($this->qb_set))
1490 {
1491 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
1492 }
1493 }
1494 else
1495 {
1496 if (empty($set))
1497 {
1498 return ($this->db_debug) ? $this->display_error('insert_batch() called with no data') : FALSE;
1499 }
1500
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001501 $this->set_insert_batch($set, '', $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001502 }
Barry Mienydd671972010-10-04 16:33:58 +02001503
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001504 if (strlen($table) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001505 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001506 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001507 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001508 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001509 }
Barry Mienydd671972010-10-04 16:33:58 +02001510
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001511 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001512 }
1513
1514 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001515 $affected_rows = 0;
Andrey Andreev105a48b2016-02-04 15:45:10 +02001516 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += $batch_size)
Derek Jonesd10e8962010-03-02 17:10:36 -06001517 {
Andrey Andreevd9a40632016-07-22 15:49:08 +03001518 if ($this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, $escape, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, $batch_size))))
1519 {
1520 $affected_rows += $this->affected_rows();
1521 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001522 }
Barry Mienydd671972010-10-04 16:33:58 +02001523
Derek Jonesd10e8962010-03-02 17:10:36 -06001524 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001525 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001526 }
1527
1528 // --------------------------------------------------------------------
1529
1530 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +02001531 * Insert batch statement
Andrey Andreev97f36972012-04-05 12:44:36 +03001532 *
1533 * Generates a platform-specific insert string from the supplied data.
1534 *
Andrey Andreev083e3c82012-11-06 12:48:32 +02001535 * @param string $table Table name
1536 * @param array $keys INSERT keys
1537 * @param array $values INSERT values
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001538 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001539 */
1540 protected function _insert_batch($table, $keys, $values)
1541 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001542 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001543 }
1544
1545 // --------------------------------------------------------------------
1546
1547 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001548 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001549 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001550 * @param mixed
1551 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001552 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001553 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001554 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001555 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001556 {
1557 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001558
Derek Jonesd10e8962010-03-02 17:10:36 -06001559 if ( ! is_array($key))
1560 {
1561 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001562 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001563
Andrey Andreevfe642da2012-06-16 03:47:33 +03001564 is_bool($escape) OR $escape = $this->_protect_identifiers;
1565
Andrey Andreev51c84f32017-01-03 17:42:26 +02001566 $keys = array_keys($this->_object_to_array(reset($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001567 sort($keys);
1568
1569 foreach ($key as $row)
1570 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001571 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001572 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1573 {
1574 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001575 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001576 return;
1577 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001578
Barry Mienydd671972010-10-04 16:33:58 +02001579 ksort($row); // puts $row in the same order as our keys
1580
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001581 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001582 {
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001583 $clean = array();
1584 foreach ($row as $value)
1585 {
1586 $clean[] = $this->escape($value);
1587 }
1588
1589 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001590 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001591
Andrey Andreev838a9d62012-12-03 14:37:47 +02001592 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001593 }
1594
1595 foreach ($keys as $k)
1596 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001597 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001598 }
Barry Mienydd671972010-10-04 16:33:58 +02001599
Derek Jonesd10e8962010-03-02 17:10:36 -06001600 return $this;
1601 }
WanWizard7219c072011-12-28 14:09:05 +01001602
Kyle Farris0c147b32011-08-26 02:29:31 -04001603 // --------------------------------------------------------------------
1604
1605 /**
1606 * Get INSERT query string
1607 *
1608 * Compiles an insert query and returns the sql
1609 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001610 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001611 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001612 * @return string
1613 */
1614 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001615 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001616 if ($this->_validate_insert($table) === FALSE)
1617 {
1618 return FALSE;
1619 }
WanWizard7219c072011-12-28 14:09:05 +01001620
Kyle Farris76116012011-08-31 11:17:48 -04001621 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001622 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001623 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001624 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001625 array_keys($this->qb_set),
1626 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001627 );
WanWizard7219c072011-12-28 14:09:05 +01001628
Kyle Farris0c147b32011-08-26 02:29:31 -04001629 if ($reset === TRUE)
1630 {
1631 $this->_reset_write();
1632 }
WanWizard7219c072011-12-28 14:09:05 +01001633
Kyle Farris0c147b32011-08-26 02:29:31 -04001634 return $sql;
1635 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001636
Derek Allard2067d1a2008-11-13 22:59:24 +00001637 // --------------------------------------------------------------------
1638
1639 /**
1640 * Insert
1641 *
1642 * Compiles an insert string and runs the query
1643 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001644 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001645 * @param array an associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001646 * @param bool $escape Whether to escape values and identifiers
Andrey Andreevaec51262016-02-09 21:11:07 +02001647 * @return bool TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +00001648 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001649 public function insert($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001650 {
vlakoff1228fe22013-01-14 01:30:09 +01001651 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001652 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001653 $this->set($set, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001654 }
WanWizard7219c072011-12-28 14:09:05 +01001655
Kyle Farris0c147b32011-08-26 02:29:31 -04001656 if ($this->_validate_insert($table) === FALSE)
1657 {
1658 return FALSE;
1659 }
WanWizard7219c072011-12-28 14:09:05 +01001660
Kyle Farris76116012011-08-31 11:17:48 -04001661 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001662 $this->protect_identifiers(
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001663 $this->qb_from[0], TRUE, $escape, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001664 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001665 array_keys($this->qb_set),
1666 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001667 );
Barry Mienydd671972010-10-04 16:33:58 +02001668
Kyle Farris0c147b32011-08-26 02:29:31 -04001669 $this->_reset_write();
1670 return $this->query($sql);
1671 }
WanWizard7219c072011-12-28 14:09:05 +01001672
Kyle Farris0c147b32011-08-26 02:29:31 -04001673 // --------------------------------------------------------------------
1674
1675 /**
1676 * Validate Insert
1677 *
1678 * This method is used by both insert() and get_compiled_insert() to
1679 * validate that the there data is actually being set and that table
1680 * has been chosen to be inserted into.
1681 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001682 * @param string the table to insert data into
1683 * @return string
1684 */
WanWizard7219c072011-12-28 14:09:05 +01001685 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001686 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001687 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001688 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001689 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001690 }
1691
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001692 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001693 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001694 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001695 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001696 elseif ( ! isset($this->qb_from[0]))
1697 {
1698 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1699 }
WanWizard7219c072011-12-28 14:09:05 +01001700
Kyle Farris0c147b32011-08-26 02:29:31 -04001701 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001702 }
Barry Mienydd671972010-10-04 16:33:58 +02001703
Phil Sturgeon9789f322011-07-15 15:14:05 -06001704 // --------------------------------------------------------------------
1705
1706 /**
1707 * Replace
1708 *
1709 * Compiles an replace into string and runs the query
1710 *
1711 * @param string the table to replace data into
1712 * @param array an associative array of insert values
Andrey Andreevaec51262016-02-09 21:11:07 +02001713 * @return bool TRUE on success, FALSE on failure
Phil Sturgeon9789f322011-07-15 15:14:05 -06001714 */
1715 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001716 {
vlakoff1228fe22013-01-14 01:30:09 +01001717 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001718 {
1719 $this->set($set);
1720 }
Barry Mienydd671972010-10-04 16:33:58 +02001721
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001722 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001723 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001724 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001725 }
1726
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001727 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001728 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001729 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001730 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001731 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001732 }
Barry Mienydd671972010-10-04 16:33:58 +02001733
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001734 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001735 }
1736
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001737 $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 +00001738
Derek Jonesd10e8962010-03-02 17:10:36 -06001739 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001740 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001741 }
WanWizard7219c072011-12-28 14:09:05 +01001742
Kyle Farris0c147b32011-08-26 02:29:31 -04001743 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001744
Kyle Farris0c147b32011-08-26 02:29:31 -04001745 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001746 * Replace statement
1747 *
1748 * Generates a platform-specific replace string from the supplied data
1749 *
1750 * @param string the table name
1751 * @param array the insert keys
1752 * @param array the insert values
1753 * @return string
1754 */
1755 protected function _replace($table, $keys, $values)
1756 {
1757 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1758 }
1759
1760 // --------------------------------------------------------------------
1761
1762 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001763 * FROM tables
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001764 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001765 * Groups tables in FROM clauses if needed, so there is no confusion
1766 * about operator precedence.
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001767 *
Claudio Galdiolo93e78132015-01-29 11:43:56 -05001768 * Note: This is only used (and overridden) by MySQL and CUBRID.
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001769 *
1770 * @return string
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001771 */
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001772 protected function _from_tables()
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001773 {
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001774 return implode(', ', $this->qb_from);
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001775 }
1776
1777 // --------------------------------------------------------------------
1778
1779 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001780 * Get UPDATE query string
1781 *
1782 * Compiles an update query and returns the sql
1783 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001784 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001785 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001786 * @return string
1787 */
1788 public function get_compiled_update($table = '', $reset = TRUE)
1789 {
1790 // Combine any cached components with the current statements
1791 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001792
Kyle Farris0c147b32011-08-26 02:29:31 -04001793 if ($this->_validate_update($table) === FALSE)
1794 {
1795 return FALSE;
1796 }
WanWizard7219c072011-12-28 14:09:05 +01001797
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001798 $sql = $this->_update($this->qb_from[0], $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001799
Kyle Farris0c147b32011-08-26 02:29:31 -04001800 if ($reset === TRUE)
1801 {
1802 $this->_reset_write();
1803 }
WanWizard7219c072011-12-28 14:09:05 +01001804
Kyle Farris0c147b32011-08-26 02:29:31 -04001805 return $sql;
1806 }
WanWizard7219c072011-12-28 14:09:05 +01001807
Derek Allard2067d1a2008-11-13 22:59:24 +00001808 // --------------------------------------------------------------------
1809
1810 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001811 * UPDATE
Derek Allard2067d1a2008-11-13 22:59:24 +00001812 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001813 * Compiles an update string and runs the query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001815 * @param string $table
1816 * @param array $set An associative array of update values
1817 * @param mixed $where
1818 * @param int $limit
Andrey Andreevaec51262016-02-09 21:11:07 +02001819 * @return bool TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001821 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001822 {
1823 // Combine any cached components with the current statements
1824 $this->_merge_cache();
1825
vlakoff1228fe22013-01-14 01:30:09 +01001826 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 {
1828 $this->set($set);
1829 }
Barry Mienydd671972010-10-04 16:33:58 +02001830
Kyle Farris0c147b32011-08-26 02:29:31 -04001831 if ($this->_validate_update($table) === FALSE)
1832 {
1833 return FALSE;
1834 }
1835
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001836 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001837 {
1838 $this->where($where);
1839 }
1840
Andrey Andreev650b4c02012-06-11 12:07:15 +03001841 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001842 {
1843 $this->limit($limit);
1844 }
1845
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001846 $sql = $this->_update($this->qb_from[0], $this->qb_set);
Kyle Farris0c147b32011-08-26 02:29:31 -04001847 $this->_reset_write();
1848 return $this->query($sql);
1849 }
WanWizard7219c072011-12-28 14:09:05 +01001850
Kyle Farris0c147b32011-08-26 02:29:31 -04001851 // --------------------------------------------------------------------
1852
1853 /**
1854 * Validate Update
1855 *
1856 * This method is used by both update() and get_compiled_update() to
1857 * validate that data is actually being set and that a table has been
1858 * chosen to be update.
1859 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001860 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001861 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001862 */
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001863 protected function _validate_update($table)
Kyle Farris0c147b32011-08-26 02:29:31 -04001864 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001865 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001866 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001867 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001868 }
1869
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001870 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001871 {
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001872 $this->qb_from = array($this->protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +00001873 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001874 elseif ( ! isset($this->qb_from[0]))
1875 {
1876 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1877 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001878
1879 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001880 }
WanWizard7219c072011-12-28 14:09:05 +01001881
Derek Jonesd10e8962010-03-02 17:10:36 -06001882 // --------------------------------------------------------------------
1883
1884 /**
1885 * Update_Batch
1886 *
1887 * Compiles an update string and runs the query
1888 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001889 * @param string the table to retrieve the results from
1890 * @param array an associative array of update values
1891 * @param string the where key
Andrey Andreev9f808b02012-10-24 17:38:48 +03001892 * @return int number of rows affected or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001893 */
Andrey Andreev105a48b2016-02-04 15:45:10 +02001894 public function update_batch($table, $set = NULL, $index = NULL, $batch_size = 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001895 {
1896 // Combine any cached components with the current statements
1897 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001898
vlakoff1228fe22013-01-14 01:30:09 +01001899 if ($index === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001900 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001901 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001902 }
1903
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001904 if ($set === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001905 {
Andrey Andreev8338bbb2016-12-12 14:17:52 +02001906 if (empty($this->qb_set_ub))
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001907 {
1908 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
1909 }
1910 }
1911 else
1912 {
1913 if (empty($set))
1914 {
1915 return ($this->db_debug) ? $this->display_error('update_batch() called with no data') : FALSE;
1916 }
1917
Derek Jonesd10e8962010-03-02 17:10:36 -06001918 $this->set_update_batch($set, $index);
1919 }
1920
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001921 if (strlen($table) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001922 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001923 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001924 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001925 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001926 }
Barry Mienydd671972010-10-04 16:33:58 +02001927
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001928 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001929 }
Barry Mienydd671972010-10-04 16:33:58 +02001930
Derek Jonesd10e8962010-03-02 17:10:36 -06001931 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001932 $affected_rows = 0;
Andrey Andreev8338bbb2016-12-12 14:17:52 +02001933 for ($i = 0, $total = count($this->qb_set_ub); $i < $total; $i += $batch_size)
Derek Jonesd10e8962010-03-02 17:10:36 -06001934 {
Andrey Andreev8338bbb2016-12-12 14:17:52 +02001935 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 +03001936 {
1937 $affected_rows += $this->affected_rows();
1938 }
1939
Andrey Andreev79f888b2013-08-06 13:59:23 +03001940 $this->qb_where = array();
Derek Jonesd10e8962010-03-02 17:10:36 -06001941 }
Barry Mienydd671972010-10-04 16:33:58 +02001942
Derek Jonesd10e8962010-03-02 17:10:36 -06001943 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001944 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001945 }
1946
1947 // --------------------------------------------------------------------
1948
1949 /**
Andrey Andreev219565d2013-03-12 20:00:08 +02001950 * Update_Batch statement
1951 *
1952 * Generates a platform-specific batch update string from the supplied data
1953 *
1954 * @param string $table Table name
1955 * @param array $values Update data
1956 * @param string $index WHERE key
1957 * @return string
1958 */
1959 protected function _update_batch($table, $values, $index)
1960 {
1961 $ids = array();
1962 foreach ($values as $key => $val)
1963 {
Andrey Andreev8338bbb2016-12-12 14:17:52 +02001964 $ids[] = $val[$index]['value'];
Andrey Andreev219565d2013-03-12 20:00:08 +02001965
1966 foreach (array_keys($val) as $field)
1967 {
1968 if ($field !== $index)
1969 {
Andrey Andreev8338bbb2016-12-12 14:17:52 +02001970 $final[$val[$field]['field']][] = 'WHEN '.$val[$index]['field'].' = '.$val[$index]['value'].' THEN '.$val[$field]['value'];
Andrey Andreev219565d2013-03-12 20:00:08 +02001971 }
1972 }
1973 }
1974
1975 $cases = '';
1976 foreach ($final as $k => $v)
1977 {
1978 $cases .= $k." = CASE \n"
1979 .implode("\n", $v)."\n"
1980 .'ELSE '.$k.' END, ';
1981 }
1982
Andrey Andreev8338bbb2016-12-12 14:17:52 +02001983 $this->where($val[$index]['field'].' IN('.implode(',', $ids).')', NULL, FALSE);
Andrey Andreev219565d2013-03-12 20:00:08 +02001984
1985 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
1986 }
1987
1988 // --------------------------------------------------------------------
1989
1990 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001991 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001992 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001993 * @param array
1994 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001995 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001996 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001997 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001998 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001999 {
2000 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02002001
Derek Jonesd10e8962010-03-02 17:10:36 -06002002 if ( ! is_array($key))
2003 {
2004 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02002005 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002006
Andrey Andreevfe642da2012-06-16 03:47:33 +03002007 is_bool($escape) OR $escape = $this->_protect_identifiers;
2008
Derek Jonesd10e8962010-03-02 17:10:36 -06002009 foreach ($key as $k => $v)
2010 {
2011 $index_set = FALSE;
2012 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05002013 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06002014 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002015 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06002016 {
2017 $index_set = TRUE;
2018 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002019
Andrey Andreev8338bbb2016-12-12 14:17:52 +02002020 $clean[$k2] = array(
2021 'field' => $this->protect_identifiers($k2, FALSE, $escape),
2022 'value' => ($escape === FALSE ? $v2 : $this->escape($v2))
2023 );
Derek Jonesd10e8962010-03-02 17:10:36 -06002024 }
2025
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002026 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06002027 {
2028 return $this->display_error('db_batch_missing_index');
2029 }
2030
Andrey Andreev8338bbb2016-12-12 14:17:52 +02002031 $this->qb_set_ub[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06002032 }
Barry Mienydd671972010-10-04 16:33:58 +02002033
Derek Jonesd10e8962010-03-02 17:10:36 -06002034 return $this;
2035 }
2036
Derek Allard2067d1a2008-11-13 22:59:24 +00002037 // --------------------------------------------------------------------
2038
2039 /**
2040 * Empty Table
2041 *
2042 * Compiles a delete string and runs "DELETE FROM table"
2043 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002044 * @param string the table to empty
Andrey Andreevaec51262016-02-09 21:11:07 +02002045 * @return bool TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +00002046 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002047 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002048 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002049 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002050 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002051 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002052 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002053 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002054 }
2055
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002056 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002057 }
2058 else
2059 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002060 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002061 }
2062
2063 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002064 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00002065 return $this->query($sql);
2066 }
2067
2068 // --------------------------------------------------------------------
2069
2070 /**
2071 * Truncate
2072 *
2073 * Compiles a truncate string and runs the query
2074 * If the database does not support the truncate() command
2075 * This function maps to "DELETE FROM table"
2076 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002077 * @param string the table to truncate
Andrey Andreevaec51262016-02-09 21:11:07 +02002078 * @return bool TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +00002079 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002080 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002081 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002082 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002083 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002084 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002085 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002086 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002087 }
2088
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002089 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002090 }
2091 else
2092 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002093 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002094 }
2095
2096 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002097 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00002098 return $this->query($sql);
2099 }
WanWizard7219c072011-12-28 14:09:05 +01002100
Kyle Farris0c147b32011-08-26 02:29:31 -04002101 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02002102
Kyle Farris0c147b32011-08-26 02:29:31 -04002103 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03002104 * Truncate statement
2105 *
2106 * Generates a platform-specific truncate string from the supplied data
2107 *
2108 * If the database does not support the truncate() command,
2109 * then this method maps to 'DELETE FROM table'
2110 *
2111 * @param string the table name
2112 * @return string
2113 */
2114 protected function _truncate($table)
2115 {
2116 return 'TRUNCATE '.$table;
2117 }
2118
2119 // --------------------------------------------------------------------
2120
2121 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04002122 * Get DELETE query string
2123 *
2124 * Compiles a delete query string and returns the sql
2125 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002126 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002127 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04002128 * @return string
2129 */
2130 public function get_compiled_delete($table = '', $reset = TRUE)
2131 {
2132 $this->return_delete_sql = TRUE;
2133 $sql = $this->delete($table, '', NULL, $reset);
2134 $this->return_delete_sql = FALSE;
2135 return $sql;
2136 }
WanWizard7219c072011-12-28 14:09:05 +01002137
Derek Allard2067d1a2008-11-13 22:59:24 +00002138 // --------------------------------------------------------------------
2139
2140 /**
2141 * Delete
2142 *
2143 * Compiles a delete string and runs the query
2144 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002145 * @param mixed the table(s) to delete from. String or array
2146 * @param mixed the where clause
2147 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03002148 * @param bool
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002149 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00002150 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002151 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002152 {
2153 // Combine any cached components with the current statements
2154 $this->_merge_cache();
2155
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002156 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002157 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002158 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002159 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002160 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002161 }
2162
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002163 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002164 }
2165 elseif (is_array($table))
2166 {
Andrey Andreeva1170af2015-07-02 11:46:56 +03002167 empty($where) && $reset_data = FALSE;
2168
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05002169 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002170 {
Andrey Andreev13f50542012-10-12 12:31:02 +03002171 $this->delete($single_table, $where, $limit, $reset_data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002172 }
Andrey Andreeva1170af2015-07-02 11:46:56 +03002173
Derek Allard2067d1a2008-11-13 22:59:24 +00002174 return;
2175 }
2176 else
2177 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002178 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002179 }
2180
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002181 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002182 {
2183 $this->where($where);
2184 }
2185
Andrey Andreev650b4c02012-06-11 12:07:15 +03002186 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002187 {
2188 $this->limit($limit);
2189 }
2190
Andrey Andreev94611df2012-07-19 12:29:54 +03002191 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002192 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002193 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02002194 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002195
Andrey Andreevb0478652012-07-18 15:34:46 +03002196 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002197 if ($reset_data)
2198 {
2199 $this->_reset_write();
2200 }
WanWizard7219c072011-12-28 14:09:05 +01002201
Andrey Andreev24276a32012-01-08 02:44:38 +02002202 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00002203 }
WanWizard7219c072011-12-28 14:09:05 +01002204
Derek Allard2067d1a2008-11-13 22:59:24 +00002205 // --------------------------------------------------------------------
2206
2207 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03002208 * Delete statement
2209 *
2210 * Generates a platform-specific delete string from the supplied data
2211 *
2212 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03002213 * @return string
2214 */
Andrey Andreevb0478652012-07-18 15:34:46 +03002215 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03002216 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002217 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002218 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03002219 }
2220
2221 // --------------------------------------------------------------------
2222
2223 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002224 * DB Prefix
2225 *
2226 * Prepends a database prefix if one exists in configuration
2227 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002228 * @param string the table
2229 * @return string
2230 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002231 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002232 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002233 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002234 {
2235 $this->display_error('db_table_name_required');
2236 }
2237
2238 return $this->dbprefix.$table;
2239 }
2240
2241 // --------------------------------------------------------------------
2242
2243 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06002244 * Set DB Prefix
2245 *
2246 * Set's the DB Prefix to something new without needing to reconnect
2247 *
2248 * @param string the prefix
2249 * @return string
2250 */
2251 public function set_dbprefix($prefix = '')
2252 {
2253 return $this->dbprefix = $prefix;
2254 }
2255
2256 // --------------------------------------------------------------------
2257
2258 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002259 * Track Aliases
2260 *
2261 * Used to track SQL statements written with aliased tables.
2262 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002263 * @param string The table to inspect
2264 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002265 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002266 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002267 {
2268 if (is_array($table))
2269 {
2270 foreach ($table as $t)
2271 {
2272 $this->_track_aliases($t);
2273 }
2274 return;
2275 }
Barry Mienydd671972010-10-04 16:33:58 +02002276
Derek Jones37f4b9c2011-07-01 17:56:50 -05002277 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002278 // the string into discreet statements
2279 if (strpos($table, ',') !== FALSE)
2280 {
2281 return $this->_track_aliases(explode(',', $table));
2282 }
Barry Mienydd671972010-10-04 16:33:58 +02002283
Derek Allard2067d1a2008-11-13 22:59:24 +00002284 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002285 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002286 {
2287 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002288 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002289
Derek Allard2067d1a2008-11-13 22:59:24 +00002290 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002291 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002292
Derek Allard2067d1a2008-11-13 22:59:24 +00002293 // Store the alias, if it doesn't already exist
Andrey Andreev437ffe02017-01-17 12:44:19 +02002294 if ( ! in_array($table, $this->qb_aliased_tables, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +00002295 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002296 $this->qb_aliased_tables[] = $table;
Andrey Andreev437ffe02017-01-17 12:44:19 +02002297 if ($this->qb_caching === TRUE && ! in_array($table, $this->qb_cache_aliased_tables, TRUE))
2298 {
2299 $this->qb_cache_aliased_tables[] = $table;
2300 $this->qb_cache_exists[] = 'aliased_tables';
2301 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002302 }
2303 }
2304 }
WanWizard7219c072011-12-28 14:09:05 +01002305
Derek Allard2067d1a2008-11-13 22:59:24 +00002306 // --------------------------------------------------------------------
2307
2308 /**
2309 * Compile the SELECT statement
2310 *
2311 * Generates a query string based on which functions were used.
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002312 * Should not be called directly.
Derek Allard2067d1a2008-11-13 22:59:24 +00002313 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002314 * @param bool $select_override
Derek Allard2067d1a2008-11-13 22:59:24 +00002315 * @return string
2316 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002317 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002318 {
2319 // Combine any cached components with the current statements
2320 $this->_merge_cache();
2321
Derek Allard2067d1a2008-11-13 22:59:24 +00002322 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002323 if ($select_override !== FALSE)
2324 {
2325 $sql = $select_override;
2326 }
2327 else
2328 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002329 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002330
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002331 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002332 {
Barry Mienydd671972010-10-04 16:33:58 +02002333 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002334 }
2335 else
Barry Mienydd671972010-10-04 16:33:58 +02002336 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002337 // Cycle through the "select" portion of the query and prep each column name.
Andrey Andreev1924eb32015-04-08 17:19:24 +03002338 // The reason we protect identifiers here rather than in the select() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002339 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002340 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002341 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002342 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002343 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002344 }
Barry Mienydd671972010-10-04 16:33:58 +02002345
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002346 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002347 }
2348 }
2349
Derek Allard2067d1a2008-11-13 22:59:24 +00002350 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002351 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002352 {
Andrey Andreeve78f8152012-10-09 11:38:38 +03002353 $sql .= "\nFROM ".$this->_from_tables();
Derek Allard2067d1a2008-11-13 22:59:24 +00002354 }
2355
Derek Allard2067d1a2008-11-13 22:59:24 +00002356 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002357 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002358 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002359 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002360 }
2361
Andrey Andreev2d486232012-07-19 14:46:51 +03002362 $sql .= $this->_compile_wh('qb_where')
2363 .$this->_compile_group_by()
2364 .$this->_compile_wh('qb_having')
2365 .$this->_compile_order_by(); // ORDER BY
Andrey Andreevb0478652012-07-18 15:34:46 +03002366
Andrey Andreevd40459d2012-07-18 16:46:39 +03002367 // LIMIT
Andrey Andreevacc64812016-07-29 11:42:28 +03002368 if ($this->qb_limit OR $this->qb_offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00002369 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002370 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002371 }
2372
2373 return $sql;
2374 }
2375
2376 // --------------------------------------------------------------------
2377
2378 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002379 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002380 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002381 * Escapes identifiers in WHERE and HAVING statements at execution time.
2382 *
Andrey Andreev99c17e52016-02-29 16:53:45 +02002383 * Required so that aliases are tracked properly, regardless of whether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002384 * where(), or_where(), having(), or_having are called prior to from(),
2385 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002386 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002387 * @param string $qb_key 'qb_where' or 'qb_having'
Andrey Andreevd40459d2012-07-18 16:46:39 +03002388 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002389 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002390 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002391 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002392 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002393 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002394 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002395 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002396 // Is this condition already compiled?
2397 if (is_string($this->{$qb_key}[$i]))
2398 {
2399 continue;
2400 }
2401 elseif ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002402 {
Andrey Andreev7dd6f142018-01-30 15:08:21 +02002403 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'].(isset($this->{$qb_key}[$i]['value']) ? ' '.$this->{$qb_key}[$i]['value'] : '');
Andrey Andreev6e704752012-07-18 00:46:33 +03002404 continue;
2405 }
2406
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002407 // Split multiple conditions
2408 $conditions = preg_split(
Andrey Andreev554b4522015-09-01 13:51:26 +03002409 '/((?:^|\s+)AND\s+|(?:^|\s+)OR\s+)/i',
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002410 $this->{$qb_key}[$i]['condition'],
2411 -1,
2412 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
2413 );
2414
2415 for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002416 {
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002417 if (($op = $this->_get_operator($conditions[$ci])) === FALSE
Andrey Andreeve4742582012-10-25 13:25:13 +03002418 OR ! preg_match('/^(\(?)(.*)('.preg_quote($op, '/').')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches))
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002419 {
2420 continue;
2421 }
2422
2423 // $matches = array(
2424 // 0 => '(test <= foo)', /* the whole thing */
2425 // 1 => '(', /* optional */
2426 // 2 => 'test', /* the field name */
2427 // 3 => ' <= ', /* $op */
2428 // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2429 // 5 => ')' /* optional */
2430 // );
Andrey Andreev082aa402012-10-22 19:41:55 +03002431
2432 if ( ! empty($matches[4]))
2433 {
2434 $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4]));
2435 $matches[4] = ' '.$matches[4];
2436 }
2437
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002438 $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2]))
2439 .' '.trim($matches[3]).$matches[4].$matches[5];
Andrey Andreev6e704752012-07-18 00:46:33 +03002440 }
2441
Andrey Andreev7dd6f142018-01-30 15:08:21 +02002442 $this->{$qb_key}[$i] = implode('', $conditions).(isset($this->{$qb_key}[$i]['value']) ? ' '.$this->{$qb_key}[$i]['value'] : '');
Andrey Andreev6e704752012-07-18 00:46:33 +03002443 }
2444
Andrey Andreev9d3aa1b2012-10-12 12:14:09 +03002445 return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ")
2446 .implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002447 }
2448
Andrey Andreevb0478652012-07-18 15:34:46 +03002449 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002450 }
2451
2452 // --------------------------------------------------------------------
2453
2454 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002455 * Compile GROUP BY
2456 *
2457 * Escapes identifiers in GROUP BY statements at execution time.
2458 *
Andrey Andreev71d8f722017-01-17 12:01:00 +02002459 * Required so that aliases are tracked properly, regardless of whether
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002460 * group_by() is called prior to from(), join() and dbprefix is added
2461 * only if needed.
2462 *
2463 * @return string SQL statement
2464 */
2465 protected function _compile_group_by()
2466 {
2467 if (count($this->qb_groupby) > 0)
2468 {
Andrey Andreev96feb582012-07-19 13:12:34 +03002469 for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
2470 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002471 // Is it already compiled?
Andrey Andreev18eba242014-01-23 22:52:31 +02002472 if (is_string($this->qb_groupby[$i]))
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002473 {
2474 continue;
2475 }
2476
Andrey Andreev082aa402012-10-22 19:41:55 +03002477 $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 +03002478 ? $this->qb_groupby[$i]['field']
Andrey Andreev13f50542012-10-12 12:31:02 +03002479 : $this->protect_identifiers($this->qb_groupby[$i]['field']);
Andrey Andreev96feb582012-07-19 13:12:34 +03002480 }
2481
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002482 return "\nGROUP BY ".implode(', ', $this->qb_groupby);
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002483 }
2484
2485 return '';
2486 }
2487
2488 // --------------------------------------------------------------------
2489
2490 /**
Andrey Andreev2d486232012-07-19 14:46:51 +03002491 * Compile ORDER BY
2492 *
2493 * Escapes identifiers in ORDER BY statements at execution time.
2494 *
Andrey Andreev71d8f722017-01-17 12:01:00 +02002495 * Required so that aliases are tracked properly, regardless of whether
Andrey Andreev2d486232012-07-19 14:46:51 +03002496 * order_by() is called prior to from(), join() and dbprefix is added
2497 * only if needed.
2498 *
2499 * @return string SQL statement
2500 */
2501 protected function _compile_order_by()
2502 {
Andrey Andreev33cc3e12017-01-16 16:01:58 +02002503 if (empty($this->qb_orderby))
Andrey Andreev2d486232012-07-19 14:46:51 +03002504 {
Andrey Andreev33cc3e12017-01-16 16:01:58 +02002505 return '';
2506 }
Andrey Andreev2d486232012-07-19 14:46:51 +03002507
Andrey Andreev33cc3e12017-01-16 16:01:58 +02002508 for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++)
2509 {
2510 if (is_string($this->qb_orderby[$i]))
2511 {
2512 continue;
Andrey Andreev2d486232012-07-19 14:46:51 +03002513 }
2514
Andrey Andreev33cc3e12017-01-16 16:01:58 +02002515 if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field']))
2516 {
2517 $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']);
2518 }
2519
2520 $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction'];
Andrey Andreev2d486232012-07-19 14:46:51 +03002521 }
2522
Andrey Andreev33cc3e12017-01-16 16:01:58 +02002523 return "\nORDER BY ".implode(', ', $this->qb_orderby);
Andrey Andreev2d486232012-07-19 14:46:51 +03002524 }
2525
2526 // --------------------------------------------------------------------
2527
2528 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002529 * Object to Array
2530 *
2531 * Takes an object as input and converts the class variables to array key/vals
2532 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002533 * @param object
2534 * @return array
2535 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002536 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002537 {
2538 if ( ! is_object($object))
2539 {
2540 return $object;
2541 }
Barry Mienydd671972010-10-04 16:33:58 +02002542
Derek Allard2067d1a2008-11-13 22:59:24 +00002543 $array = array();
2544 foreach (get_object_vars($object) as $key => $val)
2545 {
2546 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002547 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002548 {
2549 $array[$key] = $val;
2550 }
2551 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002552
2553 return $array;
2554 }
Barry Mienydd671972010-10-04 16:33:58 +02002555
Derek Jonesd10e8962010-03-02 17:10:36 -06002556 // --------------------------------------------------------------------
2557
2558 /**
2559 * Object to Array
2560 *
2561 * Takes an object as input and converts the class variables to array key/vals
2562 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002563 * @param object
2564 * @return array
2565 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002566 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002567 {
2568 if ( ! is_object($object))
2569 {
2570 return $object;
2571 }
Barry Mienydd671972010-10-04 16:33:58 +02002572
Derek Jonesd10e8962010-03-02 17:10:36 -06002573 $array = array();
2574 $out = get_object_vars($object);
2575 $fields = array_keys($out);
2576
2577 foreach ($fields as $val)
2578 {
2579 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002580 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002581 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002582 $i = 0;
2583 foreach ($out[$val] as $data)
2584 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002585 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002586 }
2587 }
2588 }
2589
Derek Allard2067d1a2008-11-13 22:59:24 +00002590 return $array;
2591 }
Barry Mienydd671972010-10-04 16:33:58 +02002592
Derek Allard2067d1a2008-11-13 22:59:24 +00002593 // --------------------------------------------------------------------
2594
2595 /**
2596 * Start Cache
2597 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002598 * Starts 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 start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002603 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002604 $this->qb_caching = TRUE;
Andrey Andreev4a587f52014-12-11 16:27:15 +02002605 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002606 }
2607
2608 // --------------------------------------------------------------------
2609
2610 /**
2611 * Stop Cache
2612 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002613 * Stops QB caching
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 stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002618 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002619 $this->qb_caching = FALSE;
Andrey Andreev4a587f52014-12-11 16:27:15 +02002620 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002621 }
2622
2623 // --------------------------------------------------------------------
2624
2625 /**
2626 * Flush Cache
2627 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002628 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002629 *
Andrey Andreev4a587f52014-12-11 16:27:15 +02002630 * @return CI_DB_query_builder
Barry Mienydd671972010-10-04 16:33:58 +02002631 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002632 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002633 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002634 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002635 'qb_cache_select' => array(),
2636 'qb_cache_from' => array(),
2637 'qb_cache_join' => array(),
2638 'qb_cache_where' => array(),
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002639 'qb_cache_groupby' => array(),
2640 'qb_cache_having' => array(),
2641 'qb_cache_orderby' => array(),
2642 'qb_cache_set' => array(),
2643 'qb_cache_exists' => array(),
Andrey Andreev437ffe02017-01-17 12:44:19 +02002644 'qb_cache_no_escape' => array(),
2645 'qb_cache_aliased_tables' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002646 ));
Andrey Andreev4a587f52014-12-11 16:27:15 +02002647
2648 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002649 }
2650
2651 // --------------------------------------------------------------------
2652
2653 /**
2654 * Merge Cache
2655 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002656 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002657 * locally called ones.
2658 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002659 * @return void
2660 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002661 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002662 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002663 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002664 {
2665 return;
2666 }
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002667 elseif (in_array('select', $this->qb_cache_exists, TRUE))
2668 {
2669 $qb_no_escape = $this->qb_cache_no_escape;
2670 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002671
GDmac17a05282013-11-11 13:18:09 +01002672 foreach (array_unique($this->qb_cache_exists) as $val) // select, from, etc.
Derek Allard2067d1a2008-11-13 22:59:24 +00002673 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002674 $qb_variable = 'qb_'.$val;
2675 $qb_cache_var = 'qb_cache_'.$val;
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002676 $qb_new = $this->$qb_cache_var;
GDmace1b86832013-11-08 16:52:54 +01002677
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002678 for ($i = 0, $c = count($this->$qb_variable); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00002679 {
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002680 if ( ! in_array($this->{$qb_variable}[$i], $qb_new, TRUE))
2681 {
2682 $qb_new[] = $this->{$qb_variable}[$i];
2683 if ($val === 'select')
2684 {
2685 $qb_no_escape[] = $this->qb_no_escape[$i];
2686 }
2687 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002688 }
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002689
GDmace1b86832013-11-08 16:52:54 +01002690 $this->$qb_variable = $qb_new;
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002691 if ($val === 'select')
2692 {
2693 $this->qb_no_escape = $qb_no_escape;
2694 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002695 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002696 }
WanWizard7219c072011-12-28 14:09:05 +01002697
Kyle Farris0c147b32011-08-26 02:29:31 -04002698 // --------------------------------------------------------------------
2699
2700 /**
Andrey Andreev082aa402012-10-22 19:41:55 +03002701 * Is literal
2702 *
2703 * Determines if a string represents a literal value or a field name
2704 *
Andrey Andreev02e4cd72012-11-13 11:50:47 +02002705 * @param string $str
Andrey Andreev082aa402012-10-22 19:41:55 +03002706 * @return bool
2707 */
2708 protected function _is_literal($str)
2709 {
2710 $str = trim($str);
2711
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002712 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 +03002713 {
2714 return TRUE;
2715 }
2716
2717 static $_str;
2718
2719 if (empty($_str))
2720 {
2721 $_str = ($this->_escape_char !== '"')
2722 ? array('"', "'") : array("'");
2723 }
2724
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002725 return in_array($str[0], $_str, TRUE);
Andrey Andreev082aa402012-10-22 19:41:55 +03002726 }
2727
2728 // --------------------------------------------------------------------
2729
2730 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002731 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002732 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002733 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002734 *
Andrey Andreev435e0c22014-12-11 16:30:13 +02002735 * @return CI_DB_query_builder
Kyle Farris0c147b32011-08-26 02:29:31 -04002736 */
2737 public function reset_query()
2738 {
2739 $this->_reset_select();
2740 $this->_reset_write();
Andrey Andreev435e0c22014-12-11 16:30:13 +02002741 return $this;
Kyle Farris0c147b32011-08-26 02:29:31 -04002742 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002743
2744 // --------------------------------------------------------------------
2745
2746 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002747 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002748 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002749 * @param array An array of fields to reset
2750 * @return void
2751 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002752 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002753 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002754 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002755 {
Andrey Andreevae85eb42012-11-02 01:42:31 +02002756 $this->$item = $default_value;
Derek Allard2067d1a2008-11-13 22:59:24 +00002757 }
2758 }
2759
2760 // --------------------------------------------------------------------
2761
2762 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002763 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002764 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002765 * @return void
2766 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002767 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002768 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002769 $this->_reset_run(array(
Andrey Andreev4a587f52014-12-11 16:27:15 +02002770 'qb_select' => array(),
2771 'qb_from' => array(),
2772 'qb_join' => array(),
2773 'qb_where' => array(),
2774 'qb_groupby' => array(),
2775 'qb_having' => array(),
2776 'qb_orderby' => array(),
2777 'qb_aliased_tables' => array(),
2778 'qb_no_escape' => array(),
2779 'qb_distinct' => FALSE,
2780 'qb_limit' => FALSE,
2781 'qb_offset' => FALSE
2782 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002783 }
Barry Mienydd671972010-10-04 16:33:58 +02002784
Derek Allard2067d1a2008-11-13 22:59:24 +00002785 // --------------------------------------------------------------------
2786
2787 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002788 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002789 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002790 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002791 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002792 * @return void
2793 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002794 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002795 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002796 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002797 'qb_set' => array(),
Andrey Andreev8338bbb2016-12-12 14:17:52 +02002798 'qb_set_ub' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002799 'qb_from' => array(),
Andrey Andreev3e014372013-02-21 15:59:34 +02002800 'qb_join' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002801 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002802 'qb_orderby' => array(),
2803 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002804 'qb_limit' => FALSE
Andrey Andreev4a587f52014-12-11 16:27:15 +02002805 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002806 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002807
Derek Allard2067d1a2008-11-13 22:59:24 +00002808}