blob: 66ef913c90f5bff6152c0cb2a2e941511e6fa59a [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
WanWizard7219c072011-12-28 14:09:05 +01008 *
Andrey Andreev125ef472016-01-11 12:33:00 +02009 * Copyright (c) 2014 - 2016, British Columbia Institute of Technology
WanWizard7219c072011-12-28 14:09:05 +010010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreev125ef472016-01-11 12:33:00 +020032 * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
Jamie Rumbelow7efad202012-02-19 12:37:00 +000041 * Query Builder Class
Derek Allard2067d1a2008-11-13 22:59:24 +000042 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +000043 * This is the platform-independent base Query Builder implementation class.
Derek Allard2067d1a2008-11-13 22:59:24 +000044 *
45 * @package CodeIgniter
46 * @subpackage Drivers
47 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050048 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020049 * @link https://codeigniter.com/user_guide/database/
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +010051
52abstract class CI_DB_query_builder extends CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000053
Andrey Andreevae85eb42012-11-02 01:42:31 +020054 /**
55 * Return DELETE SQL flag
56 *
57 * @var bool
58 */
WanWizard7219c072011-12-28 14:09:05 +010059 protected $return_delete_sql = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +020060
61 /**
62 * Reset DELETE data flag
63 *
64 * @var bool
65 */
WanWizard7219c072011-12-28 14:09:05 +010066 protected $reset_delete_data = FALSE;
67
Andrey Andreevae85eb42012-11-02 01:42:31 +020068 /**
69 * QB SELECT data
70 *
71 * @var array
72 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +000073 protected $qb_select = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +020074
75 /**
76 * QB DISTINCT flag
77 *
78 * @var bool
79 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +000080 protected $qb_distinct = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +020081
82 /**
83 * QB FROM data
84 *
85 * @var array
86 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +000087 protected $qb_from = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +020088
89 /**
90 * QB JOIN data
91 *
92 * @var array
93 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +000094 protected $qb_join = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +020095
96 /**
97 * QB WHERE data
98 *
99 * @var array
100 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000101 protected $qb_where = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200102
103 /**
104 * QB GROUP BY data
105 *
106 * @var array
107 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000108 protected $qb_groupby = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200109
110 /**
111 * QB HAVING data
112 *
113 * @var array
114 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000115 protected $qb_having = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200116
117 /**
118 * QB keys
119 *
120 * @var array
121 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000122 protected $qb_keys = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200123
124 /**
125 * QB LIMIT data
126 *
127 * @var int
128 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000129 protected $qb_limit = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200130
131 /**
132 * QB OFFSET data
133 *
134 * @var int
135 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000136 protected $qb_offset = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200137
138 /**
139 * QB ORDER BY data
140 *
141 * @var array
142 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000143 protected $qb_orderby = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200144
145 /**
146 * QB data sets
147 *
148 * @var array
149 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000150 protected $qb_set = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200151
152 /**
153 * QB aliased tables list
154 *
155 * @var array
156 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000157 protected $qb_aliased_tables = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200158
159 /**
160 * QB WHERE group started flag
161 *
162 * @var bool
163 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000164 protected $qb_where_group_started = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200165
166 /**
167 * QB WHERE group count
168 *
169 * @var int
170 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000171 protected $qb_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +0200172
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000173 // Query Builder Caching variables
Andrey Andreevae85eb42012-11-02 01:42:31 +0200174
175 /**
176 * QB Caching flag
177 *
178 * @var bool
179 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000180 protected $qb_caching = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200181
182 /**
183 * QB Cache exists list
184 *
185 * @var array
186 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000187 protected $qb_cache_exists = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200188
189 /**
190 * QB Cache SELECT data
191 *
192 * @var array
193 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000194 protected $qb_cache_select = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200195
196 /**
197 * QB Cache FROM data
198 *
199 * @var array
200 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000201 protected $qb_cache_from = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200202
203 /**
204 * QB Cache JOIN data
205 *
206 * @var array
207 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000208 protected $qb_cache_join = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200209
210 /**
211 * QB Cache WHERE data
212 *
213 * @var array
214 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000215 protected $qb_cache_where = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200216
217 /**
218 * QB Cache GROUP BY data
219 *
220 * @var array
221 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000222 protected $qb_cache_groupby = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200223
224 /**
225 * QB Cache HAVING data
226 *
227 * @var array
228 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000229 protected $qb_cache_having = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200230
231 /**
232 * QB Cache ORDER BY data
233 *
234 * @var array
235 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000236 protected $qb_cache_orderby = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200237
238 /**
239 * QB Cache data sets
240 *
241 * @var array
242 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000243 protected $qb_cache_set = array();
WanWizard7219c072011-12-28 14:09:05 +0100244
Andrey Andreevae85eb42012-11-02 01:42:31 +0200245 /**
246 * QB No Escape data
247 *
248 * @var array
249 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000250 protected $qb_no_escape = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200251
252 /**
253 * QB Cache No Escape data
254 *
255 * @var array
256 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000257 protected $qb_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000258
Andrey Andreevae85eb42012-11-02 01:42:31 +0200259 // --------------------------------------------------------------------
260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 /**
262 * Select
263 *
264 * Generates the SELECT portion of the query
265 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300267 * @param mixed
Andrew Podner4296a652012-12-17 07:51:15 -0500268 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600270 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 if (is_string($select))
273 {
274 $select = explode(',', $select);
275 }
276
Vivek Dinesh33578d22014-02-06 09:46:07 +0530277 // If the escape value was not set, we will base it on the global setting
Andrey Andreev42870232012-06-12 01:30:20 +0300278 is_bool($escape) OR $escape = $this->_protect_identifiers;
279
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 foreach ($select as $val)
281 {
282 $val = trim($val);
283
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100284 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000286 $this->qb_select[] = $val;
287 $this->qb_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000288
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000289 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000291 $this->qb_cache_select[] = $val;
292 $this->qb_cache_exists[] = 'select';
293 $this->qb_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
295 }
296 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 return $this;
299 }
300
301 // --------------------------------------------------------------------
302
303 /**
304 * Select Max
305 *
306 * Generates a SELECT MAX(field) portion of a query
307 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 * @param string the field
309 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500310 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600312 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
314 return $this->_max_min_avg_sum($select, $alias, 'MAX');
315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // --------------------------------------------------------------------
318
319 /**
320 * Select Min
321 *
322 * Generates a SELECT MIN(field) portion of a query
323 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * @param string the field
325 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500326 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600328 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
330 return $this->_max_min_avg_sum($select, $alias, 'MIN');
331 }
332
333 // --------------------------------------------------------------------
334
335 /**
336 * Select Average
337 *
338 * Generates a SELECT AVG(field) portion of a query
339 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 * @param string the field
341 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500342 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600344 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
346 return $this->_max_min_avg_sum($select, $alias, 'AVG');
347 }
348
349 // --------------------------------------------------------------------
350
351 /**
352 * Select Sum
353 *
354 * Generates a SELECT SUM(field) portion of a query
355 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 * @param string the field
357 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500358 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600360 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
362 return $this->_max_min_avg_sum($select, $alias, 'SUM');
363 }
364
365 // --------------------------------------------------------------------
366
367 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200368 * SELECT [MAX|MIN|AVG|SUM]()
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200370 * @used-by select_max()
371 * @used-by select_min()
372 * @used-by select_avg()
373 * @used-by select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200374 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200375 * @param string $select Field name
376 * @param string $alias
377 * @param string $type
Andrew Podner4296a652012-12-17 07:51:15 -0500378 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600380 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100382 if ( ! is_string($select) OR $select === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
384 $this->display_error('db_invalid_query');
385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
390 {
391 show_error('Invalid function type: '.$type);
392 }
Barry Mienydd671972010-10-04 16:33:58 +0200393
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100394 if ($alias === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
396 $alias = $this->_create_alias_from_table(trim($select));
397 }
Barry Mienydd671972010-10-04 16:33:58 +0200398
Andrey Andreev5b55c152013-08-06 14:14:32 +0300399 $sql = $type.'('.$this->protect_identifiers(trim($select)).') AS '.$this->escape_identifiers(trim($alias));
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300400
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000401 $this->qb_select[] = $sql;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100402 $this->qb_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200403
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000404 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000406 $this->qb_cache_select[] = $sql;
407 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 }
Barry Mienydd671972010-10-04 16:33:58 +0200409
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 return $this;
411 }
412
413 // --------------------------------------------------------------------
414
415 /**
416 * Determines the alias name based on the table
417 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200418 * @param string $item
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 * @return string
420 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600421 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
423 if (strpos($item, '.') !== FALSE)
424 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200425 $item = explode('.', $item);
426 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 }
Barry Mienydd671972010-10-04 16:33:58 +0200428
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 return $item;
430 }
431
432 // --------------------------------------------------------------------
433
434 /**
435 * DISTINCT
436 *
437 * Sets a flag which tells the query string compiler to add DISTINCT
438 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200439 * @param bool $val
Andrew Podner4296a652012-12-17 07:51:15 -0500440 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600442 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300444 $this->qb_distinct = is_bool($val) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 return $this;
446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 // --------------------------------------------------------------------
449
450 /**
451 * From
452 *
453 * Generates the FROM portion of the query
454 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200455 * @param mixed $from can be a string or array
Andrew Podner4296a652012-12-17 07:51:15 -0500456 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600458 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300460 foreach ((array) $from as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
462 if (strpos($val, ',') !== FALSE)
463 {
464 foreach (explode(',', $val) as $v)
465 {
466 $v = trim($v);
467 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200468
George Petsagourakis193d4482012-04-28 11:16:18 +0300469 $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000470
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000471 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000473 $this->qb_cache_from[] = $v;
474 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200475 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 }
478 else
479 {
480 $val = trim($val);
481
Andrey Andreev24276a32012-01-08 02:44:38 +0200482 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000483 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200485
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000486 $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000487
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000488 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000490 $this->qb_cache_from[] = $val;
491 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 }
493 }
494 }
495
496 return $this;
497 }
498
499 // --------------------------------------------------------------------
500
501 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200502 * JOIN
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 *
504 * Generates the JOIN portion of the query
505 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 * @param string
507 * @param string the join condition
508 * @param string the type of join
Alex Bilbief512b732012-06-16 11:15:19 +0100509 * @param string whether not to try to escape identifiers
Andrew Podner4296a652012-12-17 07:51:15 -0500510 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300512 public function join($table, $cond, $type = '', $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200513 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100514 if ($type !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
516 $type = strtoupper(trim($type));
517
Andrey Andreev42870232012-06-12 01:30:20 +0300518 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 {
520 $type = '';
521 }
522 else
523 {
524 $type .= ' ';
525 }
526 }
527
Andrey Andreev24276a32012-01-08 02:44:38 +0200528 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000529 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 $this->_track_aliases($table);
531
Andrey Andreevfe642da2012-06-16 03:47:33 +0300532 is_bool($escape) OR $escape = $this->_protect_identifiers;
533
Andrey Andreev42870232012-06-12 01:30:20 +0300534 // Split multiple conditions
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300535 if ($escape === TRUE && preg_match_all('/\sAND\s|\sOR\s/i', $cond, $m, PREG_OFFSET_CAPTURE))
Andrey Andreev42870232012-06-12 01:30:20 +0300536 {
537 $newcond = '';
538 $m[0][] = array('', strlen($cond));
539
540 for ($i = 0, $c = count($m[0]), $s = 0;
541 $i < $c;
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300542 $s = $m[0][$i][1] + strlen($m[0][$i][0]), $i++)
Andrey Andreev42870232012-06-12 01:30:20 +0300543 {
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300544 $temp = substr($cond, $s, ($m[0][$i][1] - $s));
Andrey Andreev95bd7632016-02-05 14:15:45 +0200545 $newcond .= preg_match("/(\(*)?([\[\]\w\.'-]+)(\s*[^\"\[`'\w]+\s*)(.+)/i", $temp, $match)
Andrey Andreev805edda2016-02-05 12:44:19 +0200546 ? $match[1].$this->protect_identifiers($match[2]).$match[3].$this->protect_identifiers($match[4])
Andrey Andreev42870232012-06-12 01:30:20 +0300547 : $temp;
548
549 $newcond .= $m[0][$i][0];
550 }
551
Andrey Andreev3751f932012-06-17 18:07:48 +0300552 $cond = ' ON '.$newcond;
Andrey Andreev42870232012-06-12 01:30:20 +0300553 }
554 // Split apart the condition and protect the identifiers
Andrey Andreev95bd7632016-02-05 14:15:45 +0200555 elseif ($escape === TRUE && preg_match("/(\(*)?([\[\]\w\.'-]+)(\s*[^\"\[`'\w]+\s*)(.+)/i", $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 {
Andrey Andreev95bd7632016-02-05 14:15:45 +0200557 $cond = ' ON '.$match[1].$this->protect_identifiers($match[2]).$match[3].$this->protect_identifiers($match[4]);
Andrey Andreev3751f932012-06-17 18:07:48 +0300558 }
559 elseif ( ! $this->_has_operator($cond))
560 {
561 $cond = ' USING ('.($escape ? $this->escape_identifiers($cond) : $cond).')';
562 }
563 else
564 {
565 $cond = ' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 }
Barry Mienydd671972010-10-04 16:33:58 +0200567
Andrey Andreev42870232012-06-12 01:30:20 +0300568 // Do we want to escape the table name?
569 if ($escape === TRUE)
570 {
571 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
572 }
573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 // Assemble the JOIN statement
Andrey Andreev3751f932012-06-17 18:07:48 +0300575 $this->qb_join[] = $join = $type.'JOIN '.$table.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000576
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000577 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000579 $this->qb_cache_join[] = $join;
580 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 }
582
583 return $this;
584 }
585
586 // --------------------------------------------------------------------
587
588 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200589 * WHERE
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200591 * Generates the WHERE portion of the query.
592 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 * @param mixed
595 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300596 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500597 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300599 public function where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300601 return $this->_wh('qb_where', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 }
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 // --------------------------------------------------------------------
605
606 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200607 * OR WHERE
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200609 * Generates the WHERE portion of the query.
610 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 * @param mixed
613 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300614 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500615 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300617 public function or_where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300619 return $this->_wh('qb_where', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 }
621
622 // --------------------------------------------------------------------
623
624 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +0300625 * WHERE, HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200627 * @used-by where()
628 * @used-by or_where()
629 * @used-by having()
630 * @used-by or_having()
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200632 * @param string $qb_key 'qb_where' or 'qb_having'
633 * @param mixed $key
634 * @param mixed $value
635 * @param string $type
636 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500637 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 */
Andrey Andreevd40459d2012-07-18 16:46:39 +0300639 protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300641 $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where';
642
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 if ( ! is_array($key))
644 {
645 $key = array($key => $value);
646 }
Barry Mienydd671972010-10-04 16:33:58 +0200647
Rougin Royce Gutib191550a2014-08-24 16:19:08 +0800648 // If the escape value was not set will base it on the global setting
Andrey Andreeve10fb792012-06-15 12:07:04 +0300649 is_bool($escape) OR $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000650
651 foreach ($key as $k => $v)
652 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300653 $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0)
Andrey Andreev58803fb2012-06-24 00:45:37 +0300654 ? $this->_group_get_type('')
655 : $this->_group_get_type($type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000656
vlakoff1228fe22013-01-14 01:30:09 +0100657 if ($v !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 {
Andrey Andreevf7631f12015-07-27 21:54:41 +0300659 if ($escape === TRUE)
660 {
661 $v = ' '.$this->escape($v);
662 }
WanWizard7219c072011-12-28 14:09:05 +0100663
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 if ( ! $this->_has_operator($k))
665 {
Greg Akere156c6e2011-04-20 16:03:04 -0500666 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 }
668 }
Andrey Andreev3a5efc22012-11-20 21:18:08 +0200669 elseif ( ! $this->_has_operator($k))
670 {
671 // value appears not to have been set, assign the test to IS NULL
672 $k .= ' IS NULL';
673 }
Andrey Andreev5078eb52014-12-02 01:11:54 +0200674 elseif (preg_match('/\s*(!?=|<>|IS(?:\s+NOT)?)\s*$/i', $k, $match, PREG_OFFSET_CAPTURE))
Andrey Andreev5bf4dcd2014-09-29 20:07:15 +0300675 {
676 $k = substr($k, 0, $match[0][1]).($match[1][0] === '=' ? ' IS NULL' : ' IS NOT NULL');
677 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000678
Andrey Andreevd40459d2012-07-18 16:46:39 +0300679 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000680 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300682 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
683 $this->qb_cache_exists[] = substr($qb_key, 3);
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 }
Barry Mienydd671972010-10-04 16:33:58 +0200685
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 }
Barry Mienydd671972010-10-04 16:33:58 +0200687
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 return $this;
689 }
690
691 // --------------------------------------------------------------------
692
693 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200694 * WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200696 * Generates a WHERE field IN('item', 'item') SQL query,
697 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200699 * @param string $key The field to search
700 * @param array $values The values searched on
701 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500702 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300704 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300706 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 }
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 // --------------------------------------------------------------------
710
711 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200712 * OR WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200714 * Generates a WHERE field IN('item', 'item') SQL query,
715 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200717 * @param string $key The field to search
718 * @param array $values The values searched on
719 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500720 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300722 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300724 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 }
726
727 // --------------------------------------------------------------------
728
729 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200730 * WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200732 * Generates a WHERE field NOT IN('item', 'item') SQL query,
733 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200735 * @param string $key The field to search
736 * @param array $values The values searched on
737 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500738 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300740 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300742 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 }
Barry Mienydd671972010-10-04 16:33:58 +0200744
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 // --------------------------------------------------------------------
746
747 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200748 * OR WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200750 * Generates a WHERE field NOT IN('item', 'item') SQL query,
751 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200753 * @param string $key The field to search
754 * @param array $values The values searched on
755 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500756 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300758 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300760 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 }
762
763 // --------------------------------------------------------------------
764
765 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200766 * Internal WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200768 * @used-by where_in()
769 * @used-by or_where_in()
770 * @used-by where_not_in()
771 * @used-by or_where_not_in()
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200773 * @param string $key The field to search
774 * @param array $values The values searched on
775 * @param bool $not If the statement would be IN or NOT IN
776 * @param string $type
777 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500778 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300780 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 {
782 if ($key === NULL OR $values === NULL)
783 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300784 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 }
Barry Mienydd671972010-10-04 16:33:58 +0200786
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 if ( ! is_array($values))
788 {
789 $values = array($values);
790 }
Barry Mienydd671972010-10-04 16:33:58 +0200791
Andrey Andreev498c1e02012-06-16 03:34:10 +0300792 is_bool($escape) OR $escape = $this->_protect_identifiers;
793
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 $not = ($not) ? ' NOT' : '';
795
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300796 if ($escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 {
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300798 $where_in = array();
799 foreach ($values as $value)
800 {
801 $where_in[] = $this->escape($value);
802 }
803 }
804 else
805 {
806 $where_in = array_values($values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 }
808
Andrey Andreev0d2d84f2015-07-31 13:48:59 +0300809 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
810 ? $this->_group_get_type('')
811 : $this->_group_get_type($type);
812
Andrey Andreev6e704752012-07-18 00:46:33 +0300813 $where_in = array(
Andrey Andreev94611df2012-07-19 12:29:54 +0300814 'condition' => $prefix.$key.$not.' IN('.implode(', ', $where_in).')',
Andrey Andreev6e704752012-07-18 00:46:33 +0300815 'escape' => $escape
816 );
Barry Mienydd671972010-10-04 16:33:58 +0200817
Andrey Andreev6e704752012-07-18 00:46:33 +0300818 $this->qb_where[] = $where_in;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000819 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000821 $this->qb_cache_where[] = $where_in;
822 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 }
824
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 return $this;
826 }
Barry Mienydd671972010-10-04 16:33:58 +0200827
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 // --------------------------------------------------------------------
829
830 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200831 * LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200833 * Generates a %LIKE% portion of the query.
834 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200836 * @param mixed $field
837 * @param string $match
838 * @param string $side
839 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500840 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300842 public function like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300844 return $this->_like($field, $match, 'AND ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 }
846
847 // --------------------------------------------------------------------
848
849 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200850 * NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200852 * Generates a NOT LIKE portion of the query.
853 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200855 * @param mixed $field
856 * @param string $match
857 * @param string $side
858 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500859 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300861 public function not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300863 return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 }
Barry Mienydd671972010-10-04 16:33:58 +0200865
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 // --------------------------------------------------------------------
867
868 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200869 * OR LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200871 * Generates a %LIKE% portion of the query.
872 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200874 * @param mixed $field
875 * @param string $match
876 * @param string $side
877 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500878 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300880 public function or_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300882 return $this->_like($field, $match, 'OR ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 }
884
885 // --------------------------------------------------------------------
886
887 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200888 * OR NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200890 * Generates a NOT LIKE portion of the query.
891 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200893 * @param mixed $field
894 * @param string $match
895 * @param string $side
896 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500897 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300899 public function or_not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300901 return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 }
Barry Mienydd671972010-10-04 16:33:58 +0200903
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 // --------------------------------------------------------------------
905
906 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200907 * Internal LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200909 * @used-by like()
910 * @used-by or_like()
911 * @used-by not_like()
912 * @used-by or_not_like()
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200914 * @param mixed $field
915 * @param string $match
916 * @param string $type
917 * @param string $side
918 * @param string $not
919 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500920 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300922 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 {
924 if ( ! is_array($field))
925 {
926 $field = array($field => $match);
927 }
Barry Mienydd671972010-10-04 16:33:58 +0200928
Andrey Andreevb0478652012-07-18 15:34:46 +0300929 is_bool($escape) OR $escape = $this->_protect_identifiers;
Andrey Andreev19311362015-04-07 00:02:14 +0300930 // lowercase $side in case somebody writes e.g. 'BEFORE' instead of 'before' (doh)
931 $side = strtolower($side);
Andrey Andreevb0478652012-07-18 15:34:46 +0300932
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 foreach ($field as $k => $v)
934 {
Andrey Andreev41738232012-11-30 00:13:17 +0200935 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
936 ? $this->_group_get_type('') : $this->_group_get_type($type);
937
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300938 if ($escape === TRUE)
939 {
940 $v = $this->escape_like_str($v);
941 }
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300942
Andrey Andreev24276a32012-01-08 02:44:38 +0200943 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400944 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300945 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400946 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200947 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300949 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200951 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300953 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 }
955 else
956 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300957 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600959
Derek Jonese4ed5832009-02-20 21:44:59 +0000960 // some platforms require an escape sequence definition for LIKE wildcards
Andrey Andreevd738b6b2015-07-29 16:24:57 +0300961 if ($escape === TRUE && $this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000962 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300963 $like_statement .= sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000964 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600965
Andrey Andreevb0478652012-07-18 15:34:46 +0300966 $this->qb_where[] = array('condition' => $like_statement, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000967 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 {
Andrey Andreev55bbd722013-01-28 19:02:13 +0200969 $this->qb_cache_where[] = array('condition' => $like_statement, 'escape' => $escape);
Andrey Andreevededc4a2012-07-18 01:16:15 +0300970 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200973
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 return $this;
975 }
Barry Mienydd671972010-10-04 16:33:58 +0200976
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 // --------------------------------------------------------------------
978
979 /**
WanWizard7219c072011-12-28 14:09:05 +0100980 * Starts a query group.
981 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200982 * @param string $not (Internal use only)
983 * @param string $type (Internal use only)
Andrew Podner4296a652012-12-17 07:51:15 -0500984 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +0100985 */
986 public function group_start($not = '', $type = 'AND ')
987 {
988 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100989
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000990 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100991 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Andrey Andreev6e704752012-07-18 00:46:33 +0300992 $where = array(
993 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (',
994 'escape' => FALSE
995 );
WanWizard7219c072011-12-28 14:09:05 +0100996
Andrey Andreev6e704752012-07-18 00:46:33 +0300997 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000998 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100999 {
Andrey Andreev6e704752012-07-18 00:46:33 +03001000 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +01001001 }
1002
1003 return $this;
1004 }
1005
1006 // --------------------------------------------------------------------
1007
1008 /**
1009 * Starts a query group, but ORs the group
1010 *
Andrew Podner4296a652012-12-17 07:51:15 -05001011 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001012 */
1013 public function or_group_start()
1014 {
1015 return $this->group_start('', 'OR ');
1016 }
1017
1018 // --------------------------------------------------------------------
1019
1020 /**
1021 * Starts a query group, but NOTs the group
1022 *
Andrew Podner4296a652012-12-17 07:51:15 -05001023 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001024 */
1025 public function not_group_start()
1026 {
1027 return $this->group_start('NOT ', 'AND ');
1028 }
1029
1030 // --------------------------------------------------------------------
1031
1032 /**
1033 * Starts a query group, but OR NOTs the group
1034 *
Andrew Podner4296a652012-12-17 07:51:15 -05001035 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001036 */
1037 public function or_not_group_start()
1038 {
1039 return $this->group_start('NOT ', 'OR ');
1040 }
1041
1042 // --------------------------------------------------------------------
1043
1044 /**
1045 * Ends a query group
1046 *
Andrew Podner4296a652012-12-17 07:51:15 -05001047 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001048 */
1049 public function group_end()
1050 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001051 $this->qb_where_group_started = FALSE;
Andrey Andreev6e704752012-07-18 00:46:33 +03001052 $where = array(
1053 'condition' => str_repeat(' ', $this->qb_where_group_count--).')',
1054 'escape' => FALSE
1055 );
WanWizard7219c072011-12-28 14:09:05 +01001056
Andrey Andreev6e704752012-07-18 00:46:33 +03001057 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001058 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +01001059 {
Andrey Andreev6e704752012-07-18 00:46:33 +03001060 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +01001061 }
1062
WanWizard7219c072011-12-28 14:09:05 +01001063 return $this;
1064 }
1065
1066 // --------------------------------------------------------------------
1067
1068 /**
1069 * Group_get_type
1070 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001071 * @used-by group_start()
1072 * @used-by _like()
1073 * @used-by _wh()
1074 * @used-by _where_in()
WanWizard7219c072011-12-28 14:09:05 +01001075 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001076 * @param string $type
WanWizard7219c072011-12-28 14:09:05 +01001077 * @return string
1078 */
1079 protected function _group_get_type($type)
1080 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001081 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +01001082 {
1083 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001084 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +01001085 }
1086
1087 return $type;
1088 }
1089
1090 // --------------------------------------------------------------------
1091
1092 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 * GROUP BY
1094 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001095 * @param string $by
1096 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -05001097 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001099 public function group_by($by, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001101 is_bool($escape) OR $escape = $this->_protect_identifiers;
1102
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 if (is_string($by))
1104 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001105 $by = ($escape === TRUE)
1106 ? explode(',', $by)
1107 : array($by);
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 }
Barry Mienydd671972010-10-04 16:33:58 +02001109
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 foreach ($by as $val)
1111 {
1112 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +02001113
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001114 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001116 $val = array('field' => $val, 'escape' => $escape);
Barry Mienydd671972010-10-04 16:33:58 +02001117
Andrey Andreev96feb582012-07-19 13:12:34 +03001118 $this->qb_groupby[] = $val;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001119 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001121 $this->qb_cache_groupby[] = $val;
1122 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 }
1124 }
1125 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001126
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 return $this;
1128 }
1129
1130 // --------------------------------------------------------------------
1131
1132 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001133 * HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001135 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001137 * @param string $key
1138 * @param string $value
1139 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 * @return object
1141 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001142 public function having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001144 return $this->_wh('qb_having', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 }
Barry Mienydd671972010-10-04 16:33:58 +02001146
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 // --------------------------------------------------------------------
1148
1149 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001150 * OR HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001152 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001154 * @param string $key
1155 * @param string $value
1156 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 * @return object
1158 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001159 public function or_having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001161 return $this->_wh('qb_having', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 }
Barry Mienydd671972010-10-04 16:33:58 +02001163
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 // --------------------------------------------------------------------
1165
1166 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001167 * ORDER BY
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001169 * @param string $orderby
vlakoff9f8e2992013-09-08 14:05:04 +02001170 * @param string $direction ASC, DESC or RANDOM
Andrey Andreevae85eb42012-11-02 01:42:31 +02001171 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -05001172 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 */
Andrey Andreevd24160c2012-06-16 03:21:20 +03001174 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001175 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001176 $direction = strtoupper(trim($direction));
Andrey Andreev2d486232012-07-19 14:46:51 +03001177
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001178 if ($direction === 'RANDOM')
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001180 $direction = '';
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001181
1182 // Do we have a seed value?
1183 $orderby = ctype_digit((string) $orderby)
vlakoffe6c4d5b2013-09-08 13:54:57 +02001184 ? sprintf($this->_random_keyword[1], $orderby)
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001185 : $this->_random_keyword[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 }
Andrey Andreev2d486232012-07-19 14:46:51 +03001187 elseif (empty($orderby))
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001189 return $this;
1190 }
1191 elseif ($direction !== '')
1192 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001193 $direction = in_array($direction, array('ASC', 'DESC'), TRUE) ? ' '.$direction : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 }
Barry Mienydd671972010-10-04 16:33:58 +02001195
Andrey Andreevd24160c2012-06-16 03:21:20 +03001196 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +02001197
Andrey Andreev2d486232012-07-19 14:46:51 +03001198 if ($escape === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 {
Andrey Andreev93dd2f22012-10-24 10:09:18 +03001200 $qb_orderby[] = array('field' => $orderby, 'direction' => $direction, 'escape' => FALSE);
Andrey Andreev2d486232012-07-19 14:46:51 +03001201 }
1202 else
1203 {
1204 $qb_orderby = array();
1205 foreach (explode(',', $orderby) as $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001207 $qb_orderby[] = ($direction === '' && preg_match('/\s+(ASC|DESC)$/i', rtrim($field), $match, PREG_OFFSET_CAPTURE))
1208 ? array('field' => ltrim(substr($field, 0, $match[0][1])), 'direction' => ' '.$match[1][0], 'escape' => TRUE)
1209 : array('field' => trim($field), 'direction' => $direction, 'escape' => TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 }
Barry Mienydd671972010-10-04 16:33:58 +02001212
Andrey Andreev2d486232012-07-19 14:46:51 +03001213 $this->qb_orderby = array_merge($this->qb_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001214 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001216 $this->qb_cache_orderby = array_merge($this->qb_cache_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001217 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001218 }
1219
1220 return $this;
1221 }
Barry Mienydd671972010-10-04 16:33:58 +02001222
Derek Allard2067d1a2008-11-13 22:59:24 +00001223 // --------------------------------------------------------------------
1224
1225 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001226 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001228 * @param int $value LIMIT value
1229 * @param int $offset OFFSET value
Andrew Podner4296a652012-12-17 07:51:15 -05001230 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 */
Andrey Andreev4a587f52014-12-11 16:27:15 +02001232 public function limit($value, $offset = 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 {
vlakoff912f1bc2013-01-15 03:34:12 +01001234 is_null($value) OR $this->qb_limit = (int) $value;
Andrey Andreev777153d2012-06-18 13:30:45 +03001235 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001236
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 return $this;
1238 }
Barry Mienydd671972010-10-04 16:33:58 +02001239
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 // --------------------------------------------------------------------
1241
1242 /**
1243 * Sets the OFFSET value
1244 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001245 * @param int $offset OFFSET value
Andrew Podner4296a652012-12-17 07:51:15 -05001246 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001248 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001250 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 return $this;
1252 }
Barry Mienydd671972010-10-04 16:33:58 +02001253
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 // --------------------------------------------------------------------
1255
1256 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001257 * LIMIT string
Andrey Andreev2c35b642012-06-24 03:05:26 +03001258 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001259 * Generates a platform-specific LIMIT clause.
Andrey Andreev2c35b642012-06-24 03:05:26 +03001260 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001261 * @param string $sql SQL Query
Andrey Andreev2c35b642012-06-24 03:05:26 +03001262 * @return string
1263 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001264 protected function _limit($sql)
Andrey Andreev2c35b642012-06-24 03:05:26 +03001265 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001266 return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').$this->qb_limit;
Andrey Andreev2c35b642012-06-24 03:05:26 +03001267 }
1268
1269 // --------------------------------------------------------------------
1270
1271 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001272 * The "set" function.
1273 *
1274 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 * @param mixed
1277 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001278 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001279 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001281 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 {
1283 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001284
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 if ( ! is_array($key))
1286 {
1287 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001288 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001289
Andrey Andreevfe642da2012-06-16 03:47:33 +03001290 is_bool($escape) OR $escape = $this->_protect_identifiers;
1291
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 foreach ($key as $k => $v)
1293 {
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001294 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1295 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001296 }
Barry Mienydd671972010-10-04 16:33:58 +02001297
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 return $this;
1299 }
WanWizard7219c072011-12-28 14:09:05 +01001300
Kyle Farris0c147b32011-08-26 02:29:31 -04001301 // --------------------------------------------------------------------
1302
1303 /**
1304 * Get SELECT query string
1305 *
1306 * Compiles a SELECT query string and returns the sql.
1307 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001308 * @param string the table name to select from (optional)
Calvin Tam55bc5052015-07-24 02:27:24 -07001309 * @param bool TRUE: resets QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001310 * @return string
1311 */
WanWizard7219c072011-12-28 14:09:05 +01001312 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001313 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001314 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001315 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001316 $this->_track_aliases($table);
1317 $this->from($table);
1318 }
WanWizard7219c072011-12-28 14:09:05 +01001319
Andrey Andreev650b4c02012-06-11 12:07:15 +03001320 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001321
Kyle Farris0c147b32011-08-26 02:29:31 -04001322 if ($reset === TRUE)
1323 {
1324 $this->_reset_select();
1325 }
WanWizard7219c072011-12-28 14:09:05 +01001326
Kyle Farris0c147b32011-08-26 02:29:31 -04001327 return $select;
1328 }
WanWizard7219c072011-12-28 14:09:05 +01001329
Derek Allard2067d1a2008-11-13 22:59:24 +00001330 // --------------------------------------------------------------------
1331
1332 /**
1333 * Get
1334 *
1335 * Compiles the select statement based on the other functions called
1336 * and runs the query
1337 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001338 * @param string the table
1339 * @param string the limit clause
1340 * @param string the offset clause
1341 * @return object
1342 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001343 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001345 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 {
1347 $this->_track_aliases($table);
1348 $this->from($table);
1349 }
Barry Mienydd671972010-10-04 16:33:58 +02001350
Andrey Andreev650b4c02012-06-11 12:07:15 +03001351 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001352 {
1353 $this->limit($limit, $offset);
1354 }
Barry Mienydd671972010-10-04 16:33:58 +02001355
Andrey Andreev24276a32012-01-08 02:44:38 +02001356 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 $this->_reset_select();
1358 return $result;
1359 }
1360
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001361 // --------------------------------------------------------------------
1362
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 /**
1364 * "Count All Results" query
1365 *
Barry Mienydd671972010-10-04 16:33:58 +02001366 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001367 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001369 * @param string
yaoshanliang2f164052015-03-16 16:48:15 +08001370 * @param bool the reset clause
vlakoffc6ac7482013-11-17 00:50:06 +01001371 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +00001372 */
yaoshanliang19c28472015-03-15 10:42:18 +08001373 public function count_all_results($table = '', $reset = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001374 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001375 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 {
1377 $this->_track_aliases($table);
1378 $this->from($table);
1379 }
Barry Mienydd671972010-10-04 16:33:58 +02001380
Andrey Andreev075bdb42016-01-25 13:31:23 +02001381 // ORDER BY usage is often problematic here (most notably
1382 // on Microsoft SQL Server) and ultimately unnecessary
1383 // for selecting COUNT(*) ...
1384 if ( ! empty($this->qb_orderby))
1385 {
1386 $orderby = $this->qb_orderby;
1387 $this->qb_orderby = NULL;
1388 }
1389
1390 $result = ($this->qb_distinct === TRUE)
Andrey Andreevb05f5062012-10-26 12:01:02 +03001391 ? $this->query($this->_count_string.$this->protect_identifiers('numrows')."\nFROM (\n".$this->_compile_select()."\n) CI_count_all_results")
1392 : $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
yaoshanliang9971e7b2015-03-14 13:09:16 +08001393
yaoshanliang2f164052015-03-16 16:48:15 +08001394 if ($reset === TRUE)
yaoshanliang19c28472015-03-15 10:42:18 +08001395 {
yaoshanliang2f164052015-03-16 16:48:15 +08001396 $this->_reset_select();
yaoshanliang19c28472015-03-15 10:42:18 +08001397 }
Andrey Andreev075bdb42016-01-25 13:31:23 +02001398 // If we've previously reset the qb_orderby values, get them back
1399 elseif ( ! isset($this->qb_orderby))
1400 {
1401 $this->qb_orderby = $orderby;
1402 }
Barry Mienydd671972010-10-04 16:33:58 +02001403
Purwandi1d160e72012-01-09 16:33:28 +07001404 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001405 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001406 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001407 }
1408
Purwandi1d160e72012-01-09 16:33:28 +07001409 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001410 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001411 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001412
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 // --------------------------------------------------------------------
1414
1415 /**
1416 * Get_Where
1417 *
1418 * Allows the where clause, limit and offset to be added directly
1419 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001420 * @param string $table
1421 * @param string $where
1422 * @param int $limit
1423 * @param int $offset
Derek Allard2067d1a2008-11-13 22:59:24 +00001424 * @return object
1425 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001426 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001428 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001429 {
1430 $this->from($table);
1431 }
1432
vlakoff1228fe22013-01-14 01:30:09 +01001433 if ($where !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001434 {
1435 $this->where($where);
1436 }
Barry Mienydd671972010-10-04 16:33:58 +02001437
Andrey Andreev650b4c02012-06-11 12:07:15 +03001438 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001439 {
1440 $this->limit($limit, $offset);
1441 }
Barry Mienydd671972010-10-04 16:33:58 +02001442
Andrey Andreev24276a32012-01-08 02:44:38 +02001443 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001444 $this->_reset_select();
1445 return $result;
1446 }
1447
1448 // --------------------------------------------------------------------
1449
1450 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001451 * Insert_Batch
1452 *
1453 * Compiles batch insert strings and runs the queries
1454 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001455 * @param string $table Table to insert into
1456 * @param array $set An associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001457 * @param bool $escape Whether to escape values and identifiers
Andrey Andreevae85eb42012-11-02 01:42:31 +02001458 * @return int Number of rows inserted or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001459 */
Andrey Andreev105a48b2016-02-04 15:45:10 +02001460 public function insert_batch($table, $set = NULL, $escape = NULL, $batch_size = 100)
Barry Mienydd671972010-10-04 16:33:58 +02001461 {
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001462 if ($set === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001463 {
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001464 if (empty($this->qb_set))
1465 {
1466 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
1467 }
1468 }
1469 else
1470 {
1471 if (empty($set))
1472 {
1473 return ($this->db_debug) ? $this->display_error('insert_batch() called with no data') : FALSE;
1474 }
1475
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001476 $this->set_insert_batch($set, '', $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001477 }
Barry Mienydd671972010-10-04 16:33:58 +02001478
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001479 if (strlen($table) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001480 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001481 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001482 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001483 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001484 }
Barry Mienydd671972010-10-04 16:33:58 +02001485
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001486 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001487 }
1488
1489 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001490 $affected_rows = 0;
Andrey Andreev105a48b2016-02-04 15:45:10 +02001491 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += $batch_size)
Derek Jonesd10e8962010-03-02 17:10:36 -06001492 {
Andrey Andreev105a48b2016-02-04 15:45:10 +02001493 $this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, $escape, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, $batch_size)));
Andrey Andreev9f808b02012-10-24 17:38:48 +03001494 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001495 }
Barry Mienydd671972010-10-04 16:33:58 +02001496
Derek Jonesd10e8962010-03-02 17:10:36 -06001497 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001498 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001499 }
1500
1501 // --------------------------------------------------------------------
1502
1503 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +02001504 * Insert batch statement
Andrey Andreev97f36972012-04-05 12:44:36 +03001505 *
1506 * Generates a platform-specific insert string from the supplied data.
1507 *
Andrey Andreev083e3c82012-11-06 12:48:32 +02001508 * @param string $table Table name
1509 * @param array $keys INSERT keys
1510 * @param array $values INSERT values
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001511 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001512 */
1513 protected function _insert_batch($table, $keys, $values)
1514 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001515 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001516 }
1517
1518 // --------------------------------------------------------------------
1519
1520 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001521 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001522 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001523 * @param mixed
1524 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001525 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001526 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001527 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001528 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001529 {
1530 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001531
Derek Jonesd10e8962010-03-02 17:10:36 -06001532 if ( ! is_array($key))
1533 {
1534 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001535 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001536
Andrey Andreevfe642da2012-06-16 03:47:33 +03001537 is_bool($escape) OR $escape = $this->_protect_identifiers;
1538
Iban Eguia3c0a4522012-04-15 13:30:44 +02001539 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001540 sort($keys);
1541
1542 foreach ($key as $row)
1543 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001544 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001545 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1546 {
1547 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001548 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001549 return;
1550 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001551
Barry Mienydd671972010-10-04 16:33:58 +02001552 ksort($row); // puts $row in the same order as our keys
1553
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001554 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001555 {
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001556 $clean = array();
1557 foreach ($row as $value)
1558 {
1559 $clean[] = $this->escape($value);
1560 }
1561
1562 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001563 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001564
Andrey Andreev838a9d62012-12-03 14:37:47 +02001565 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001566 }
1567
1568 foreach ($keys as $k)
1569 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001570 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001571 }
Barry Mienydd671972010-10-04 16:33:58 +02001572
Derek Jonesd10e8962010-03-02 17:10:36 -06001573 return $this;
1574 }
WanWizard7219c072011-12-28 14:09:05 +01001575
Kyle Farris0c147b32011-08-26 02:29:31 -04001576 // --------------------------------------------------------------------
1577
1578 /**
1579 * Get INSERT query string
1580 *
1581 * Compiles an insert query and returns the sql
1582 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001583 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001584 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001585 * @return string
1586 */
1587 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001588 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001589 if ($this->_validate_insert($table) === FALSE)
1590 {
1591 return FALSE;
1592 }
WanWizard7219c072011-12-28 14:09:05 +01001593
Kyle Farris76116012011-08-31 11:17:48 -04001594 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001595 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001596 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001597 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001598 array_keys($this->qb_set),
1599 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001600 );
WanWizard7219c072011-12-28 14:09:05 +01001601
Kyle Farris0c147b32011-08-26 02:29:31 -04001602 if ($reset === TRUE)
1603 {
1604 $this->_reset_write();
1605 }
WanWizard7219c072011-12-28 14:09:05 +01001606
Kyle Farris0c147b32011-08-26 02:29:31 -04001607 return $sql;
1608 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001609
Derek Allard2067d1a2008-11-13 22:59:24 +00001610 // --------------------------------------------------------------------
1611
1612 /**
1613 * Insert
1614 *
1615 * Compiles an insert string and runs the query
1616 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001617 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001618 * @param array an associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001619 * @param bool $escape Whether to escape values and identifiers
Derek Allard2067d1a2008-11-13 22:59:24 +00001620 * @return object
1621 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001622 public function insert($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001623 {
vlakoff1228fe22013-01-14 01:30:09 +01001624 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001625 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001626 $this->set($set, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001627 }
WanWizard7219c072011-12-28 14:09:05 +01001628
Kyle Farris0c147b32011-08-26 02:29:31 -04001629 if ($this->_validate_insert($table) === FALSE)
1630 {
1631 return FALSE;
1632 }
WanWizard7219c072011-12-28 14:09:05 +01001633
Kyle Farris76116012011-08-31 11:17:48 -04001634 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001635 $this->protect_identifiers(
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001636 $this->qb_from[0], TRUE, $escape, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001637 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001638 array_keys($this->qb_set),
1639 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001640 );
Barry Mienydd671972010-10-04 16:33:58 +02001641
Kyle Farris0c147b32011-08-26 02:29:31 -04001642 $this->_reset_write();
1643 return $this->query($sql);
1644 }
WanWizard7219c072011-12-28 14:09:05 +01001645
Kyle Farris0c147b32011-08-26 02:29:31 -04001646 // --------------------------------------------------------------------
1647
1648 /**
1649 * Validate Insert
1650 *
1651 * This method is used by both insert() and get_compiled_insert() to
1652 * validate that the there data is actually being set and that table
1653 * has been chosen to be inserted into.
1654 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001655 * @param string the table to insert data into
1656 * @return string
1657 */
WanWizard7219c072011-12-28 14:09:05 +01001658 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001659 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001660 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001661 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001662 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001663 }
1664
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001665 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001666 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001667 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001668 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001669 elseif ( ! isset($this->qb_from[0]))
1670 {
1671 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1672 }
WanWizard7219c072011-12-28 14:09:05 +01001673
Kyle Farris0c147b32011-08-26 02:29:31 -04001674 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001675 }
Barry Mienydd671972010-10-04 16:33:58 +02001676
Phil Sturgeon9789f322011-07-15 15:14:05 -06001677 // --------------------------------------------------------------------
1678
1679 /**
1680 * Replace
1681 *
1682 * Compiles an replace into string and runs the query
1683 *
1684 * @param string the table to replace data into
1685 * @param array an associative array of insert values
1686 * @return object
1687 */
1688 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001689 {
vlakoff1228fe22013-01-14 01:30:09 +01001690 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001691 {
1692 $this->set($set);
1693 }
Barry Mienydd671972010-10-04 16:33:58 +02001694
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001695 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001696 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001697 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001698 }
1699
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001700 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001701 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001702 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001703 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001704 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001705 }
Barry Mienydd671972010-10-04 16:33:58 +02001706
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001707 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001708 }
1709
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001710 $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 +00001711
Derek Jonesd10e8962010-03-02 17:10:36 -06001712 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001713 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001714 }
WanWizard7219c072011-12-28 14:09:05 +01001715
Kyle Farris0c147b32011-08-26 02:29:31 -04001716 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001717
Kyle Farris0c147b32011-08-26 02:29:31 -04001718 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001719 * Replace statement
1720 *
1721 * Generates a platform-specific replace string from the supplied data
1722 *
1723 * @param string the table name
1724 * @param array the insert keys
1725 * @param array the insert values
1726 * @return string
1727 */
1728 protected function _replace($table, $keys, $values)
1729 {
1730 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1731 }
1732
1733 // --------------------------------------------------------------------
1734
1735 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001736 * FROM tables
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001737 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001738 * Groups tables in FROM clauses if needed, so there is no confusion
1739 * about operator precedence.
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001740 *
Claudio Galdiolo93e78132015-01-29 11:43:56 -05001741 * Note: This is only used (and overridden) by MySQL and CUBRID.
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001742 *
1743 * @return string
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001744 */
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001745 protected function _from_tables()
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001746 {
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001747 return implode(', ', $this->qb_from);
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001748 }
1749
1750 // --------------------------------------------------------------------
1751
1752 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001753 * Get UPDATE query string
1754 *
1755 * Compiles an update query and returns the sql
1756 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001757 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001758 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001759 * @return string
1760 */
1761 public function get_compiled_update($table = '', $reset = TRUE)
1762 {
1763 // Combine any cached components with the current statements
1764 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001765
Kyle Farris0c147b32011-08-26 02:29:31 -04001766 if ($this->_validate_update($table) === FALSE)
1767 {
1768 return FALSE;
1769 }
WanWizard7219c072011-12-28 14:09:05 +01001770
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001771 $sql = $this->_update($this->qb_from[0], $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001772
Kyle Farris0c147b32011-08-26 02:29:31 -04001773 if ($reset === TRUE)
1774 {
1775 $this->_reset_write();
1776 }
WanWizard7219c072011-12-28 14:09:05 +01001777
Kyle Farris0c147b32011-08-26 02:29:31 -04001778 return $sql;
1779 }
WanWizard7219c072011-12-28 14:09:05 +01001780
Derek Allard2067d1a2008-11-13 22:59:24 +00001781 // --------------------------------------------------------------------
1782
1783 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001784 * UPDATE
Derek Allard2067d1a2008-11-13 22:59:24 +00001785 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001786 * Compiles an update string and runs the query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001787 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001788 * @param string $table
1789 * @param array $set An associative array of update values
1790 * @param mixed $where
1791 * @param int $limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001792 * @return object
1793 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001794 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001795 {
1796 // Combine any cached components with the current statements
1797 $this->_merge_cache();
1798
vlakoff1228fe22013-01-14 01:30:09 +01001799 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001800 {
1801 $this->set($set);
1802 }
Barry Mienydd671972010-10-04 16:33:58 +02001803
Kyle Farris0c147b32011-08-26 02:29:31 -04001804 if ($this->_validate_update($table) === FALSE)
1805 {
1806 return FALSE;
1807 }
1808
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001809 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001810 {
1811 $this->where($where);
1812 }
1813
Andrey Andreev650b4c02012-06-11 12:07:15 +03001814 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001815 {
1816 $this->limit($limit);
1817 }
1818
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001819 $sql = $this->_update($this->qb_from[0], $this->qb_set);
Kyle Farris0c147b32011-08-26 02:29:31 -04001820 $this->_reset_write();
1821 return $this->query($sql);
1822 }
WanWizard7219c072011-12-28 14:09:05 +01001823
Kyle Farris0c147b32011-08-26 02:29:31 -04001824 // --------------------------------------------------------------------
1825
1826 /**
1827 * Validate Update
1828 *
1829 * This method is used by both update() and get_compiled_update() to
1830 * validate that data is actually being set and that a table has been
1831 * chosen to be update.
1832 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001833 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001834 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001835 */
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001836 protected function _validate_update($table)
Kyle Farris0c147b32011-08-26 02:29:31 -04001837 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001838 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001839 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001840 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001841 }
1842
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001843 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001844 {
Andrey Andreev4b9fec62015-07-20 17:26:31 +03001845 $this->qb_from = array($this->protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +00001846 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001847 elseif ( ! isset($this->qb_from[0]))
1848 {
1849 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1850 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001851
1852 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001853 }
WanWizard7219c072011-12-28 14:09:05 +01001854
Derek Jonesd10e8962010-03-02 17:10:36 -06001855 // --------------------------------------------------------------------
1856
1857 /**
1858 * Update_Batch
1859 *
1860 * Compiles an update string and runs the query
1861 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001862 * @param string the table to retrieve the results from
1863 * @param array an associative array of update values
1864 * @param string the where key
Andrey Andreev9f808b02012-10-24 17:38:48 +03001865 * @return int number of rows affected or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001866 */
Andrey Andreev105a48b2016-02-04 15:45:10 +02001867 public function update_batch($table, $set = NULL, $index = NULL, $batch_size = 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001868 {
1869 // Combine any cached components with the current statements
1870 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001871
vlakoff1228fe22013-01-14 01:30:09 +01001872 if ($index === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001873 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001874 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001875 }
1876
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001877 if ($set === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001878 {
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001879 if (empty($this->qb_set))
1880 {
1881 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
1882 }
1883 }
1884 else
1885 {
1886 if (empty($set))
1887 {
1888 return ($this->db_debug) ? $this->display_error('update_batch() called with no data') : FALSE;
1889 }
1890
Derek Jonesd10e8962010-03-02 17:10:36 -06001891 $this->set_update_batch($set, $index);
1892 }
1893
Andrey Andreev8ec82e22016-01-26 16:33:31 +02001894 if (strlen($table) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001895 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001896 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001897 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001898 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001899 }
Barry Mienydd671972010-10-04 16:33:58 +02001900
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001901 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001902 }
Barry Mienydd671972010-10-04 16:33:58 +02001903
Derek Jonesd10e8962010-03-02 17:10:36 -06001904 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001905 $affected_rows = 0;
Andrey Andreev105a48b2016-02-04 15:45:10 +02001906 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += $batch_size)
Derek Jonesd10e8962010-03-02 17:10:36 -06001907 {
Andrey Andreev105a48b2016-02-04 15:45:10 +02001908 $this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, $batch_size), $this->protect_identifiers($index)));
Andrey Andreev9f808b02012-10-24 17:38:48 +03001909 $affected_rows += $this->affected_rows();
Andrey Andreev79f888b2013-08-06 13:59:23 +03001910 $this->qb_where = array();
Derek Jonesd10e8962010-03-02 17:10:36 -06001911 }
Barry Mienydd671972010-10-04 16:33:58 +02001912
Derek Jonesd10e8962010-03-02 17:10:36 -06001913 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001914 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001915 }
1916
1917 // --------------------------------------------------------------------
1918
1919 /**
Andrey Andreev219565d2013-03-12 20:00:08 +02001920 * Update_Batch statement
1921 *
1922 * Generates a platform-specific batch update string from the supplied data
1923 *
1924 * @param string $table Table name
1925 * @param array $values Update data
1926 * @param string $index WHERE key
1927 * @return string
1928 */
1929 protected function _update_batch($table, $values, $index)
1930 {
1931 $ids = array();
1932 foreach ($values as $key => $val)
1933 {
1934 $ids[] = $val[$index];
1935
1936 foreach (array_keys($val) as $field)
1937 {
1938 if ($field !== $index)
1939 {
1940 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
1941 }
1942 }
1943 }
1944
1945 $cases = '';
1946 foreach ($final as $k => $v)
1947 {
1948 $cases .= $k." = CASE \n"
1949 .implode("\n", $v)."\n"
1950 .'ELSE '.$k.' END, ';
1951 }
1952
1953 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
1954
1955 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
1956 }
1957
1958 // --------------------------------------------------------------------
1959
1960 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001961 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001962 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001963 * @param array
1964 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001965 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001966 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001967 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001968 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001969 {
1970 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001971
Derek Jonesd10e8962010-03-02 17:10:36 -06001972 if ( ! is_array($key))
1973 {
1974 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001975 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001976
Andrey Andreevfe642da2012-06-16 03:47:33 +03001977 is_bool($escape) OR $escape = $this->_protect_identifiers;
1978
Derek Jonesd10e8962010-03-02 17:10:36 -06001979 foreach ($key as $k => $v)
1980 {
1981 $index_set = FALSE;
1982 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001983 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001984 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001985 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001986 {
1987 $index_set = TRUE;
1988 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001989
Andrey Andreev2dfbe992015-07-27 21:54:18 +03001990 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001991 }
1992
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001993 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001994 {
1995 return $this->display_error('db_batch_missing_index');
1996 }
1997
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001998 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001999 }
Barry Mienydd671972010-10-04 16:33:58 +02002000
Derek Jonesd10e8962010-03-02 17:10:36 -06002001 return $this;
2002 }
2003
Derek Allard2067d1a2008-11-13 22:59:24 +00002004 // --------------------------------------------------------------------
2005
2006 /**
2007 * Empty Table
2008 *
2009 * Compiles a delete string and runs "DELETE FROM table"
2010 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002011 * @param string the table to empty
2012 * @return object
2013 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002014 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002015 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002016 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002017 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002018 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002019 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002020 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002021 }
2022
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002023 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002024 }
2025 else
2026 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002027 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002028 }
2029
2030 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002031 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00002032 return $this->query($sql);
2033 }
2034
2035 // --------------------------------------------------------------------
2036
2037 /**
2038 * Truncate
2039 *
2040 * Compiles a truncate string and runs the query
2041 * If the database does not support the truncate() command
2042 * This function maps to "DELETE FROM table"
2043 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002044 * @param string the table to truncate
2045 * @return object
2046 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002047 public function truncate($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->_truncate($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 }
WanWizard7219c072011-12-28 14:09:05 +01002067
Kyle Farris0c147b32011-08-26 02:29:31 -04002068 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02002069
Kyle Farris0c147b32011-08-26 02:29:31 -04002070 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03002071 * Truncate statement
2072 *
2073 * Generates a platform-specific truncate string from the supplied data
2074 *
2075 * If the database does not support the truncate() command,
2076 * then this method maps to 'DELETE FROM table'
2077 *
2078 * @param string the table name
2079 * @return string
2080 */
2081 protected function _truncate($table)
2082 {
2083 return 'TRUNCATE '.$table;
2084 }
2085
2086 // --------------------------------------------------------------------
2087
2088 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04002089 * Get DELETE query string
2090 *
2091 * Compiles a delete query string and returns the sql
2092 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002093 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002094 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04002095 * @return string
2096 */
2097 public function get_compiled_delete($table = '', $reset = TRUE)
2098 {
2099 $this->return_delete_sql = TRUE;
2100 $sql = $this->delete($table, '', NULL, $reset);
2101 $this->return_delete_sql = FALSE;
2102 return $sql;
2103 }
WanWizard7219c072011-12-28 14:09:05 +01002104
Derek Allard2067d1a2008-11-13 22:59:24 +00002105 // --------------------------------------------------------------------
2106
2107 /**
2108 * Delete
2109 *
2110 * Compiles a delete string and runs the query
2111 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002112 * @param mixed the table(s) to delete from. String or array
2113 * @param mixed the where clause
2114 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03002115 * @param bool
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002116 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00002117 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002118 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002119 {
2120 // Combine any cached components with the current statements
2121 $this->_merge_cache();
2122
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002123 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002124 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002125 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002126 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002127 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002128 }
2129
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002130 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002131 }
2132 elseif (is_array($table))
2133 {
Andrey Andreeva1170af2015-07-02 11:46:56 +03002134 empty($where) && $reset_data = FALSE;
2135
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05002136 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002137 {
Andrey Andreev13f50542012-10-12 12:31:02 +03002138 $this->delete($single_table, $where, $limit, $reset_data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002139 }
Andrey Andreeva1170af2015-07-02 11:46:56 +03002140
Derek Allard2067d1a2008-11-13 22:59:24 +00002141 return;
2142 }
2143 else
2144 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002145 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002146 }
2147
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002148 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002149 {
2150 $this->where($where);
2151 }
2152
Andrey Andreev650b4c02012-06-11 12:07:15 +03002153 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002154 {
2155 $this->limit($limit);
2156 }
2157
Andrey Andreev94611df2012-07-19 12:29:54 +03002158 if (count($this->qb_where) === 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_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02002161 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002162
Andrey Andreevb0478652012-07-18 15:34:46 +03002163 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002164 if ($reset_data)
2165 {
2166 $this->_reset_write();
2167 }
WanWizard7219c072011-12-28 14:09:05 +01002168
Andrey Andreev24276a32012-01-08 02:44:38 +02002169 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00002170 }
WanWizard7219c072011-12-28 14:09:05 +01002171
Derek Allard2067d1a2008-11-13 22:59:24 +00002172 // --------------------------------------------------------------------
2173
2174 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03002175 * Delete statement
2176 *
2177 * Generates a platform-specific delete string from the supplied data
2178 *
2179 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03002180 * @return string
2181 */
Andrey Andreevb0478652012-07-18 15:34:46 +03002182 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03002183 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002184 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002185 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03002186 }
2187
2188 // --------------------------------------------------------------------
2189
2190 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002191 * DB Prefix
2192 *
2193 * Prepends a database prefix if one exists in configuration
2194 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002195 * @param string the table
2196 * @return string
2197 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002198 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002199 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002200 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002201 {
2202 $this->display_error('db_table_name_required');
2203 }
2204
2205 return $this->dbprefix.$table;
2206 }
2207
2208 // --------------------------------------------------------------------
2209
2210 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06002211 * Set DB Prefix
2212 *
2213 * Set's the DB Prefix to something new without needing to reconnect
2214 *
2215 * @param string the prefix
2216 * @return string
2217 */
2218 public function set_dbprefix($prefix = '')
2219 {
2220 return $this->dbprefix = $prefix;
2221 }
2222
2223 // --------------------------------------------------------------------
2224
2225 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002226 * Track Aliases
2227 *
2228 * Used to track SQL statements written with aliased tables.
2229 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002230 * @param string The table to inspect
2231 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002232 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002233 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002234 {
2235 if (is_array($table))
2236 {
2237 foreach ($table as $t)
2238 {
2239 $this->_track_aliases($t);
2240 }
2241 return;
2242 }
Barry Mienydd671972010-10-04 16:33:58 +02002243
Derek Jones37f4b9c2011-07-01 17:56:50 -05002244 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002245 // the string into discreet statements
2246 if (strpos($table, ',') !== FALSE)
2247 {
2248 return $this->_track_aliases(explode(',', $table));
2249 }
Barry Mienydd671972010-10-04 16:33:58 +02002250
Derek Allard2067d1a2008-11-13 22:59:24 +00002251 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002252 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002253 {
2254 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002255 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002256
Derek Allard2067d1a2008-11-13 22:59:24 +00002257 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002258 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002259
Derek Allard2067d1a2008-11-13 22:59:24 +00002260 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002261 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002262 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002263 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002264 }
2265 }
2266 }
WanWizard7219c072011-12-28 14:09:05 +01002267
Derek Allard2067d1a2008-11-13 22:59:24 +00002268 // --------------------------------------------------------------------
2269
2270 /**
2271 * Compile the SELECT statement
2272 *
2273 * Generates a query string based on which functions were used.
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002274 * Should not be called directly.
Derek Allard2067d1a2008-11-13 22:59:24 +00002275 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002276 * @param bool $select_override
Derek Allard2067d1a2008-11-13 22:59:24 +00002277 * @return string
2278 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002279 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002280 {
2281 // Combine any cached components with the current statements
2282 $this->_merge_cache();
2283
Derek Allard2067d1a2008-11-13 22:59:24 +00002284 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002285 if ($select_override !== FALSE)
2286 {
2287 $sql = $select_override;
2288 }
2289 else
2290 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002291 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002292
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002293 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002294 {
Barry Mienydd671972010-10-04 16:33:58 +02002295 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002296 }
2297 else
Barry Mienydd671972010-10-04 16:33:58 +02002298 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002299 // Cycle through the "select" portion of the query and prep each column name.
Andrey Andreev1924eb32015-04-08 17:19:24 +03002300 // The reason we protect identifiers here rather than in the select() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002301 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002302 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002303 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002304 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002305 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002306 }
Barry Mienydd671972010-10-04 16:33:58 +02002307
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002308 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002309 }
2310 }
2311
Derek Allard2067d1a2008-11-13 22:59:24 +00002312 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002313 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002314 {
Andrey Andreeve78f8152012-10-09 11:38:38 +03002315 $sql .= "\nFROM ".$this->_from_tables();
Derek Allard2067d1a2008-11-13 22:59:24 +00002316 }
2317
Derek Allard2067d1a2008-11-13 22:59:24 +00002318 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002319 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002320 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002321 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002322 }
2323
Andrey Andreev2d486232012-07-19 14:46:51 +03002324 $sql .= $this->_compile_wh('qb_where')
2325 .$this->_compile_group_by()
2326 .$this->_compile_wh('qb_having')
2327 .$this->_compile_order_by(); // ORDER BY
Andrey Andreevb0478652012-07-18 15:34:46 +03002328
Andrey Andreevd40459d2012-07-18 16:46:39 +03002329 // LIMIT
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002330 if ($this->qb_limit)
Derek Allard2067d1a2008-11-13 22:59:24 +00002331 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002332 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002333 }
2334
2335 return $sql;
2336 }
2337
2338 // --------------------------------------------------------------------
2339
2340 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002341 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002342 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002343 * Escapes identifiers in WHERE and HAVING statements at execution time.
2344 *
Andrey Andreevb0478652012-07-18 15:34:46 +03002345 * Required so that aliases are tracked properly, regardless of wether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002346 * where(), or_where(), having(), or_having are called prior to from(),
2347 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002348 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002349 * @param string $qb_key 'qb_where' or 'qb_having'
Andrey Andreevd40459d2012-07-18 16:46:39 +03002350 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002351 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002352 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002353 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002354 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002355 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002356 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002357 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002358 // Is this condition already compiled?
2359 if (is_string($this->{$qb_key}[$i]))
2360 {
2361 continue;
2362 }
2363 elseif ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002364 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002365 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002366 continue;
2367 }
2368
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002369 // Split multiple conditions
2370 $conditions = preg_split(
Andrey Andreev554b4522015-09-01 13:51:26 +03002371 '/((?:^|\s+)AND\s+|(?:^|\s+)OR\s+)/i',
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002372 $this->{$qb_key}[$i]['condition'],
2373 -1,
2374 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
2375 );
2376
2377 for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002378 {
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002379 if (($op = $this->_get_operator($conditions[$ci])) === FALSE
Andrey Andreeve4742582012-10-25 13:25:13 +03002380 OR ! preg_match('/^(\(?)(.*)('.preg_quote($op, '/').')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches))
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002381 {
2382 continue;
2383 }
2384
2385 // $matches = array(
2386 // 0 => '(test <= foo)', /* the whole thing */
2387 // 1 => '(', /* optional */
2388 // 2 => 'test', /* the field name */
2389 // 3 => ' <= ', /* $op */
2390 // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2391 // 5 => ')' /* optional */
2392 // );
Andrey Andreev082aa402012-10-22 19:41:55 +03002393
2394 if ( ! empty($matches[4]))
2395 {
2396 $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4]));
2397 $matches[4] = ' '.$matches[4];
2398 }
2399
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002400 $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2]))
2401 .' '.trim($matches[3]).$matches[4].$matches[5];
Andrey Andreev6e704752012-07-18 00:46:33 +03002402 }
2403
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002404 $this->{$qb_key}[$i] = implode('', $conditions);
Andrey Andreev6e704752012-07-18 00:46:33 +03002405 }
2406
Andrey Andreev9d3aa1b2012-10-12 12:14:09 +03002407 return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ")
2408 .implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002409 }
2410
Andrey Andreevb0478652012-07-18 15:34:46 +03002411 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002412 }
2413
2414 // --------------------------------------------------------------------
2415
2416 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002417 * Compile GROUP BY
2418 *
2419 * Escapes identifiers in GROUP BY statements at execution time.
2420 *
2421 * Required so that aliases are tracked properly, regardless of wether
2422 * group_by() is called prior to from(), join() and dbprefix is added
2423 * only if needed.
2424 *
2425 * @return string SQL statement
2426 */
2427 protected function _compile_group_by()
2428 {
2429 if (count($this->qb_groupby) > 0)
2430 {
Andrey Andreev96feb582012-07-19 13:12:34 +03002431 for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
2432 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002433 // Is it already compiled?
Andrey Andreev18eba242014-01-23 22:52:31 +02002434 if (is_string($this->qb_groupby[$i]))
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002435 {
2436 continue;
2437 }
2438
Andrey Andreev082aa402012-10-22 19:41:55 +03002439 $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 +03002440 ? $this->qb_groupby[$i]['field']
Andrey Andreev13f50542012-10-12 12:31:02 +03002441 : $this->protect_identifiers($this->qb_groupby[$i]['field']);
Andrey Andreev96feb582012-07-19 13:12:34 +03002442 }
2443
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002444 return "\nGROUP BY ".implode(', ', $this->qb_groupby);
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002445 }
2446
2447 return '';
2448 }
2449
2450 // --------------------------------------------------------------------
2451
2452 /**
Andrey Andreev2d486232012-07-19 14:46:51 +03002453 * Compile ORDER BY
2454 *
2455 * Escapes identifiers in ORDER BY statements at execution time.
2456 *
2457 * Required so that aliases are tracked properly, regardless of wether
2458 * order_by() is called prior to from(), join() and dbprefix is added
2459 * only if needed.
2460 *
2461 * @return string SQL statement
2462 */
2463 protected function _compile_order_by()
2464 {
Andrey Andreeva53ea842012-10-23 12:44:09 +03002465 if (is_array($this->qb_orderby) && count($this->qb_orderby) > 0)
Andrey Andreev2d486232012-07-19 14:46:51 +03002466 {
Andrey Andreev2d486232012-07-19 14:46:51 +03002467 for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++)
2468 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002469 if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field']))
Andrey Andreev2d486232012-07-19 14:46:51 +03002470 {
Andrey Andreevfc043b32012-10-12 14:46:14 +03002471 $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']);
Andrey Andreev2d486232012-07-19 14:46:51 +03002472 }
2473
2474 $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction'];
2475 }
2476
Andrey Andreeva53ea842012-10-23 12:44:09 +03002477 return $this->qb_orderby = "\nORDER BY ".implode(', ', $this->qb_orderby);
2478 }
2479 elseif (is_string($this->qb_orderby))
2480 {
2481 return $this->qb_orderby;
Andrey Andreev2d486232012-07-19 14:46:51 +03002482 }
2483
2484 return '';
2485 }
2486
2487 // --------------------------------------------------------------------
2488
2489 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002490 * Object to Array
2491 *
2492 * Takes an object as input and converts the class variables to array key/vals
2493 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002494 * @param object
2495 * @return array
2496 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002497 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002498 {
2499 if ( ! is_object($object))
2500 {
2501 return $object;
2502 }
Barry Mienydd671972010-10-04 16:33:58 +02002503
Derek Allard2067d1a2008-11-13 22:59:24 +00002504 $array = array();
2505 foreach (get_object_vars($object) as $key => $val)
2506 {
2507 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002508 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002509 {
2510 $array[$key] = $val;
2511 }
2512 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002513
2514 return $array;
2515 }
Barry Mienydd671972010-10-04 16:33:58 +02002516
Derek Jonesd10e8962010-03-02 17:10:36 -06002517 // --------------------------------------------------------------------
2518
2519 /**
2520 * Object to Array
2521 *
2522 * Takes an object as input and converts the class variables to array key/vals
2523 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002524 * @param object
2525 * @return array
2526 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002527 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002528 {
2529 if ( ! is_object($object))
2530 {
2531 return $object;
2532 }
Barry Mienydd671972010-10-04 16:33:58 +02002533
Derek Jonesd10e8962010-03-02 17:10:36 -06002534 $array = array();
2535 $out = get_object_vars($object);
2536 $fields = array_keys($out);
2537
2538 foreach ($fields as $val)
2539 {
2540 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002541 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002542 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002543 $i = 0;
2544 foreach ($out[$val] as $data)
2545 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002546 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002547 }
2548 }
2549 }
2550
Derek Allard2067d1a2008-11-13 22:59:24 +00002551 return $array;
2552 }
Barry Mienydd671972010-10-04 16:33:58 +02002553
Derek Allard2067d1a2008-11-13 22:59:24 +00002554 // --------------------------------------------------------------------
2555
2556 /**
2557 * Start Cache
2558 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002559 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002560 *
Andrey Andreev4a587f52014-12-11 16:27:15 +02002561 * @return CI_DB_query_builder
Barry Mienydd671972010-10-04 16:33:58 +02002562 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002563 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002564 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002565 $this->qb_caching = TRUE;
Andrey Andreev4a587f52014-12-11 16:27:15 +02002566 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002567 }
2568
2569 // --------------------------------------------------------------------
2570
2571 /**
2572 * Stop Cache
2573 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002574 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002575 *
Andrey Andreev4a587f52014-12-11 16:27:15 +02002576 * @return CI_DB_query_builder
Barry Mienydd671972010-10-04 16:33:58 +02002577 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002578 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002579 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002580 $this->qb_caching = FALSE;
Andrey Andreev4a587f52014-12-11 16:27:15 +02002581 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002582 }
2583
2584 // --------------------------------------------------------------------
2585
2586 /**
2587 * Flush Cache
2588 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002589 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002590 *
Andrey Andreev4a587f52014-12-11 16:27:15 +02002591 * @return CI_DB_query_builder
Barry Mienydd671972010-10-04 16:33:58 +02002592 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002593 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002594 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002595 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002596 'qb_cache_select' => array(),
2597 'qb_cache_from' => array(),
2598 'qb_cache_join' => array(),
2599 'qb_cache_where' => array(),
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002600 'qb_cache_groupby' => array(),
2601 'qb_cache_having' => array(),
2602 'qb_cache_orderby' => array(),
2603 'qb_cache_set' => array(),
2604 'qb_cache_exists' => array(),
2605 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002606 ));
Andrey Andreev4a587f52014-12-11 16:27:15 +02002607
2608 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00002609 }
2610
2611 // --------------------------------------------------------------------
2612
2613 /**
2614 * Merge Cache
2615 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002616 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002617 * locally called ones.
2618 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002619 * @return void
2620 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002621 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002622 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002623 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002624 {
2625 return;
2626 }
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002627 elseif (in_array('select', $this->qb_cache_exists, TRUE))
2628 {
2629 $qb_no_escape = $this->qb_cache_no_escape;
2630 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002631
GDmac17a05282013-11-11 13:18:09 +01002632 foreach (array_unique($this->qb_cache_exists) as $val) // select, from, etc.
Derek Allard2067d1a2008-11-13 22:59:24 +00002633 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002634 $qb_variable = 'qb_'.$val;
2635 $qb_cache_var = 'qb_cache_'.$val;
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002636 $qb_new = $this->$qb_cache_var;
GDmace1b86832013-11-08 16:52:54 +01002637
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002638 for ($i = 0, $c = count($this->$qb_variable); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00002639 {
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002640 if ( ! in_array($this->{$qb_variable}[$i], $qb_new, TRUE))
2641 {
2642 $qb_new[] = $this->{$qb_variable}[$i];
2643 if ($val === 'select')
2644 {
2645 $qb_no_escape[] = $this->qb_no_escape[$i];
2646 }
2647 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002648 }
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002649
GDmace1b86832013-11-08 16:52:54 +01002650 $this->$qb_variable = $qb_new;
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002651 if ($val === 'select')
2652 {
2653 $this->qb_no_escape = $qb_no_escape;
2654 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002655 }
2656
2657 // If we are "protecting identifiers" we need to examine the "from"
2658 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002659 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002660 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002661 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002662 }
2663 }
WanWizard7219c072011-12-28 14:09:05 +01002664
Kyle Farris0c147b32011-08-26 02:29:31 -04002665 // --------------------------------------------------------------------
2666
2667 /**
Andrey Andreev082aa402012-10-22 19:41:55 +03002668 * Is literal
2669 *
2670 * Determines if a string represents a literal value or a field name
2671 *
Andrey Andreev02e4cd72012-11-13 11:50:47 +02002672 * @param string $str
Andrey Andreev082aa402012-10-22 19:41:55 +03002673 * @return bool
2674 */
2675 protected function _is_literal($str)
2676 {
2677 $str = trim($str);
2678
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002679 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 +03002680 {
2681 return TRUE;
2682 }
2683
2684 static $_str;
2685
2686 if (empty($_str))
2687 {
2688 $_str = ($this->_escape_char !== '"')
2689 ? array('"', "'") : array("'");
2690 }
2691
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002692 return in_array($str[0], $_str, TRUE);
Andrey Andreev082aa402012-10-22 19:41:55 +03002693 }
2694
2695 // --------------------------------------------------------------------
2696
2697 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002698 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002699 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002700 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002701 *
Andrey Andreev435e0c22014-12-11 16:30:13 +02002702 * @return CI_DB_query_builder
Kyle Farris0c147b32011-08-26 02:29:31 -04002703 */
2704 public function reset_query()
2705 {
2706 $this->_reset_select();
2707 $this->_reset_write();
Andrey Andreev435e0c22014-12-11 16:30:13 +02002708 return $this;
Kyle Farris0c147b32011-08-26 02:29:31 -04002709 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002710
2711 // --------------------------------------------------------------------
2712
2713 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002714 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002715 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002716 * @param array An array of fields to reset
2717 * @return void
2718 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002719 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002720 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002721 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002722 {
Andrey Andreevae85eb42012-11-02 01:42:31 +02002723 $this->$item = $default_value;
Derek Allard2067d1a2008-11-13 22:59:24 +00002724 }
2725 }
2726
2727 // --------------------------------------------------------------------
2728
2729 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002730 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002731 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002732 * @return void
2733 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002734 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002735 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002736 $this->_reset_run(array(
Andrey Andreev4a587f52014-12-11 16:27:15 +02002737 'qb_select' => array(),
2738 'qb_from' => array(),
2739 'qb_join' => array(),
2740 'qb_where' => array(),
2741 'qb_groupby' => array(),
2742 'qb_having' => array(),
2743 'qb_orderby' => array(),
2744 'qb_aliased_tables' => array(),
2745 'qb_no_escape' => array(),
2746 'qb_distinct' => FALSE,
2747 'qb_limit' => FALSE,
2748 'qb_offset' => FALSE
2749 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002750 }
Barry Mienydd671972010-10-04 16:33:58 +02002751
Derek Allard2067d1a2008-11-13 22:59:24 +00002752 // --------------------------------------------------------------------
2753
2754 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002755 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002756 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002757 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002758 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002759 * @return void
2760 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002761 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002762 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002763 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002764 'qb_set' => array(),
2765 'qb_from' => array(),
Andrey Andreev3e014372013-02-21 15:59:34 +02002766 'qb_join' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002767 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002768 'qb_orderby' => array(),
2769 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002770 'qb_limit' => FALSE
Andrey Andreev4a587f52014-12-11 16:27:15 +02002771 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002772 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002773
Derek Allard2067d1a2008-11-13 22:59:24 +00002774}