blob: 2096ffd60bff18d116e267b2b717eecd52a823b9 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
WanWizard7219c072011-12-28 14:09:05 +01008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
WanWizard7219c072011-12-28 14:09:05 +010010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
Jamie Rumbelow7efad202012-02-19 12:37:00 +000030 * Query Builder Class
Derek Allard2067d1a2008-11-13 22:59:24 +000031 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +000032 * This is the platform-independent base Query Builder implementation class.
Derek Allard2067d1a2008-11-13 22:59:24 +000033 *
34 * @package CodeIgniter
35 * @subpackage Drivers
36 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/database/
39 */
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +010040
41abstract class CI_DB_query_builder extends CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000042
Andrey Andreevae85eb42012-11-02 01:42:31 +020043 /**
44 * Return DELETE SQL flag
45 *
46 * @var bool
47 */
WanWizard7219c072011-12-28 14:09:05 +010048 protected $return_delete_sql = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +020049
50 /**
51 * Reset DELETE data flag
52 *
53 * @var bool
54 */
WanWizard7219c072011-12-28 14:09:05 +010055 protected $reset_delete_data = FALSE;
56
Andrey Andreevae85eb42012-11-02 01:42:31 +020057 /**
58 * QB SELECT data
59 *
60 * @var array
61 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +000062 protected $qb_select = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +020063
64 /**
65 * QB DISTINCT flag
66 *
67 * @var bool
68 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +000069 protected $qb_distinct = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +020070
71 /**
72 * QB FROM data
73 *
74 * @var array
75 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +000076 protected $qb_from = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +020077
78 /**
79 * QB JOIN data
80 *
81 * @var array
82 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +000083 protected $qb_join = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +020084
85 /**
86 * QB WHERE data
87 *
88 * @var array
89 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +000090 protected $qb_where = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +020091
92 /**
93 * QB GROUP BY data
94 *
95 * @var array
96 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +000097 protected $qb_groupby = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +020098
99 /**
100 * QB HAVING data
101 *
102 * @var array
103 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000104 protected $qb_having = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200105
106 /**
107 * QB keys
108 *
109 * @var array
110 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000111 protected $qb_keys = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200112
113 /**
114 * QB LIMIT data
115 *
116 * @var int
117 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000118 protected $qb_limit = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200119
120 /**
121 * QB OFFSET data
122 *
123 * @var int
124 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000125 protected $qb_offset = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200126
127 /**
128 * QB ORDER BY data
129 *
130 * @var array
131 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000132 protected $qb_orderby = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200133
134 /**
135 * QB data sets
136 *
137 * @var array
138 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000139 protected $qb_set = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200140
141 /**
142 * QB aliased tables list
143 *
144 * @var array
145 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000146 protected $qb_aliased_tables = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200147
148 /**
149 * QB WHERE group started flag
150 *
151 * @var bool
152 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000153 protected $qb_where_group_started = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200154
155 /**
156 * QB WHERE group count
157 *
158 * @var int
159 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000160 protected $qb_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +0200161
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000162 // Query Builder Caching variables
Andrey Andreevae85eb42012-11-02 01:42:31 +0200163
164 /**
165 * QB Caching flag
166 *
167 * @var bool
168 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000169 protected $qb_caching = FALSE;
Andrey Andreevae85eb42012-11-02 01:42:31 +0200170
171 /**
172 * QB Cache exists list
173 *
174 * @var array
175 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000176 protected $qb_cache_exists = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200177
178 /**
179 * QB Cache SELECT data
180 *
181 * @var array
182 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000183 protected $qb_cache_select = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200184
185 /**
186 * QB Cache FROM data
187 *
188 * @var array
189 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000190 protected $qb_cache_from = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200191
192 /**
193 * QB Cache JOIN data
194 *
195 * @var array
196 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000197 protected $qb_cache_join = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200198
199 /**
200 * QB Cache WHERE data
201 *
202 * @var array
203 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000204 protected $qb_cache_where = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200205
206 /**
207 * QB Cache GROUP BY data
208 *
209 * @var array
210 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000211 protected $qb_cache_groupby = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200212
213 /**
214 * QB Cache HAVING data
215 *
216 * @var array
217 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000218 protected $qb_cache_having = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200219
220 /**
221 * QB Cache ORDER BY data
222 *
223 * @var array
224 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000225 protected $qb_cache_orderby = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200226
227 /**
228 * QB Cache data sets
229 *
230 * @var array
231 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000232 protected $qb_cache_set = array();
WanWizard7219c072011-12-28 14:09:05 +0100233
Andrey Andreevae85eb42012-11-02 01:42:31 +0200234 /**
235 * QB No Escape data
236 *
237 * @var array
238 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000239 protected $qb_no_escape = array();
Andrey Andreevae85eb42012-11-02 01:42:31 +0200240
241 /**
242 * QB Cache No Escape data
243 *
244 * @var array
245 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000246 protected $qb_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000247
Andrey Andreevae85eb42012-11-02 01:42:31 +0200248 // --------------------------------------------------------------------
249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 /**
251 * Select
252 *
253 * Generates the SELECT portion of the query
254 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 * @param string
Andrey Andreev42870232012-06-12 01:30:20 +0300256 * @param mixed
Andrew Podner4296a652012-12-17 07:51:15 -0500257 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600259 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 if (is_string($select))
262 {
263 $select = explode(',', $select);
264 }
265
Vivek Dinesh33578d22014-02-06 09:46:07 +0530266 // If the escape value was not set, we will base it on the global setting
Andrey Andreev42870232012-06-12 01:30:20 +0300267 is_bool($escape) OR $escape = $this->_protect_identifiers;
268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 foreach ($select as $val)
270 {
271 $val = trim($val);
272
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100273 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000275 $this->qb_select[] = $val;
276 $this->qb_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000277
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000278 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000280 $this->qb_cache_select[] = $val;
281 $this->qb_cache_exists[] = 'select';
282 $this->qb_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 }
284 }
285 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 return $this;
288 }
289
290 // --------------------------------------------------------------------
291
292 /**
293 * Select Max
294 *
295 * Generates a SELECT MAX(field) portion of a query
296 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 * @param string the field
298 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500299 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600301 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 {
303 return $this->_max_min_avg_sum($select, $alias, 'MAX');
304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 // --------------------------------------------------------------------
307
308 /**
309 * Select Min
310 *
311 * Generates a SELECT MIN(field) portion of a query
312 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 * @param string the field
314 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500315 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600317 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
319 return $this->_max_min_avg_sum($select, $alias, 'MIN');
320 }
321
322 // --------------------------------------------------------------------
323
324 /**
325 * Select Average
326 *
327 * Generates a SELECT AVG(field) portion of a query
328 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 * @param string the field
330 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500331 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600333 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
335 return $this->_max_min_avg_sum($select, $alias, 'AVG');
336 }
337
338 // --------------------------------------------------------------------
339
340 /**
341 * Select Sum
342 *
343 * Generates a SELECT SUM(field) portion of a query
344 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 * @param string the field
346 * @param string an alias
Andrew Podner4296a652012-12-17 07:51:15 -0500347 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600349 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
351 return $this->_max_min_avg_sum($select, $alias, 'SUM');
352 }
353
354 // --------------------------------------------------------------------
355
356 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200357 * SELECT [MAX|MIN|AVG|SUM]()
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200359 * @used-by select_max()
360 * @used-by select_min()
361 * @used-by select_avg()
362 * @used-by select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200363 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200364 * @param string $select Field name
365 * @param string $alias
366 * @param string $type
Andrew Podner4296a652012-12-17 07:51:15 -0500367 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600369 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100371 if ( ! is_string($select) OR $select === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 {
373 $this->display_error('db_invalid_query');
374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
379 {
380 show_error('Invalid function type: '.$type);
381 }
Barry Mienydd671972010-10-04 16:33:58 +0200382
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100383 if ($alias === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
385 $alias = $this->_create_alias_from_table(trim($select));
386 }
Barry Mienydd671972010-10-04 16:33:58 +0200387
Andrey Andreev5b55c152013-08-06 14:14:32 +0300388 $sql = $type.'('.$this->protect_identifiers(trim($select)).') AS '.$this->escape_identifiers(trim($alias));
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300389
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000390 $this->qb_select[] = $sql;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100391 $this->qb_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200392
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000393 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000395 $this->qb_cache_select[] = $sql;
396 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 }
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 return $this;
400 }
401
402 // --------------------------------------------------------------------
403
404 /**
405 * Determines the alias name based on the table
406 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200407 * @param string $item
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 * @return string
409 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600410 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
412 if (strpos($item, '.') !== FALSE)
413 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200414 $item = explode('.', $item);
415 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 return $item;
419 }
420
421 // --------------------------------------------------------------------
422
423 /**
424 * DISTINCT
425 *
426 * Sets a flag which tells the query string compiler to add DISTINCT
427 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200428 * @param bool $val
Andrew Podner4296a652012-12-17 07:51:15 -0500429 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600431 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300433 $this->qb_distinct = is_bool($val) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 return $this;
435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 // --------------------------------------------------------------------
438
439 /**
440 * From
441 *
442 * Generates the FROM portion of the query
443 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200444 * @param mixed $from can be a string or array
Andrew Podner4296a652012-12-17 07:51:15 -0500445 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600447 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
Andrey Andreev7b5eb732012-05-24 20:52:41 +0300449 foreach ((array) $from as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
451 if (strpos($val, ',') !== FALSE)
452 {
453 foreach (explode(',', $val) as $v)
454 {
455 $v = trim($v);
456 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200457
George Petsagourakis193d4482012-04-28 11:16:18 +0300458 $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000459
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000460 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000462 $this->qb_cache_from[] = $v;
463 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200464 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 }
467 else
468 {
469 $val = trim($val);
470
Andrey Andreev24276a32012-01-08 02:44:38 +0200471 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000472 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200474
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000475 $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000476
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000477 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000479 $this->qb_cache_from[] = $val;
480 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 }
482 }
483 }
484
485 return $this;
486 }
487
488 // --------------------------------------------------------------------
489
490 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200491 * JOIN
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 *
493 * Generates the JOIN portion of the query
494 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 * @param string
496 * @param string the join condition
497 * @param string the type of join
Alex Bilbief512b732012-06-16 11:15:19 +0100498 * @param string whether not to try to escape identifiers
Andrew Podner4296a652012-12-17 07:51:15 -0500499 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 */
Andrey Andreevfe642da2012-06-16 03:47:33 +0300501 public function join($table, $cond, $type = '', $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +0200502 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100503 if ($type !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 {
505 $type = strtoupper(trim($type));
506
Andrey Andreev42870232012-06-12 01:30:20 +0300507 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
509 $type = '';
510 }
511 else
512 {
513 $type .= ' ';
514 }
515 }
516
Andrey Andreev24276a32012-01-08 02:44:38 +0200517 // Extract any aliases that might exist. We use this information
Jamie Rumbelow0c092992012-03-06 22:05:16 +0000518 // in the protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 $this->_track_aliases($table);
520
Andrey Andreevfe642da2012-06-16 03:47:33 +0300521 is_bool($escape) OR $escape = $this->_protect_identifiers;
522
Andrey Andreev42870232012-06-12 01:30:20 +0300523 // Split multiple conditions
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300524 if ($escape === TRUE && preg_match_all('/\sAND\s|\sOR\s/i', $cond, $m, PREG_OFFSET_CAPTURE))
Andrey Andreev42870232012-06-12 01:30:20 +0300525 {
526 $newcond = '';
527 $m[0][] = array('', strlen($cond));
528
529 for ($i = 0, $c = count($m[0]), $s = 0;
530 $i < $c;
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300531 $s = $m[0][$i][1] + strlen($m[0][$i][0]), $i++)
Andrey Andreev42870232012-06-12 01:30:20 +0300532 {
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300533 $temp = substr($cond, $s, ($m[0][$i][1] - $s));
Andrey Andreev42870232012-06-12 01:30:20 +0300534
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300535 $newcond .= preg_match("/([\[\]\w\.'-]+)(\s*[^\"\[`'\w]+\s*)(.+)/i", $temp, $match)
Andrey Andreev42870232012-06-12 01:30:20 +0300536 ? $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3])
537 : $temp;
538
539 $newcond .= $m[0][$i][0];
540 }
541
Andrey Andreev3751f932012-06-17 18:07:48 +0300542 $cond = ' ON '.$newcond;
Andrey Andreev42870232012-06-12 01:30:20 +0300543 }
544 // Split apart the condition and protect the identifiers
Andrey Andreev49aa45b2012-07-06 16:22:21 +0300545 elseif ($escape === TRUE && preg_match("/([\[\]\w\.'-]+)(\s*[^\"\[`'\w]+\s*)(.+)/i", $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 {
Andrey Andreev3751f932012-06-17 18:07:48 +0300547 $cond = ' ON '.$this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
548 }
549 elseif ( ! $this->_has_operator($cond))
550 {
551 $cond = ' USING ('.($escape ? $this->escape_identifiers($cond) : $cond).')';
552 }
553 else
554 {
555 $cond = ' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 }
Barry Mienydd671972010-10-04 16:33:58 +0200557
Andrey Andreev42870232012-06-12 01:30:20 +0300558 // Do we want to escape the table name?
559 if ($escape === TRUE)
560 {
561 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
562 }
563
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 // Assemble the JOIN statement
Andrey Andreev3751f932012-06-17 18:07:48 +0300565 $this->qb_join[] = $join = $type.'JOIN '.$table.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000566
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000567 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000569 $this->qb_cache_join[] = $join;
570 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 }
572
573 return $this;
574 }
575
576 // --------------------------------------------------------------------
577
578 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200579 * WHERE
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200581 * Generates the WHERE portion of the query.
582 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 * @param mixed
585 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300586 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500587 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300589 public function where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300591 return $this->_wh('qb_where', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 }
Barry Mienydd671972010-10-04 16:33:58 +0200593
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 // --------------------------------------------------------------------
595
596 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200597 * OR WHERE
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200599 * Generates the WHERE portion of the query.
600 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 * @param mixed
603 * @param mixed
Andrey Andreev42870232012-06-12 01:30:20 +0300604 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -0500605 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300607 public function or_where($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300609 return $this->_wh('qb_where', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 }
611
612 // --------------------------------------------------------------------
613
614 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +0300615 * WHERE, HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200617 * @used-by where()
618 * @used-by or_where()
619 * @used-by having()
620 * @used-by or_having()
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200622 * @param string $qb_key 'qb_where' or 'qb_having'
623 * @param mixed $key
624 * @param mixed $value
625 * @param string $type
626 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500627 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 */
Andrey Andreevd40459d2012-07-18 16:46:39 +0300629 protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300631 $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where';
632
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 if ( ! is_array($key))
634 {
635 $key = array($key => $value);
636 }
Barry Mienydd671972010-10-04 16:33:58 +0200637
Rougin Royce Gutib191550a2014-08-24 16:19:08 +0800638 // If the escape value was not set will base it on the global setting
Andrey Andreeve10fb792012-06-15 12:07:04 +0300639 is_bool($escape) OR $escape = $this->_protect_identifiers;
Derek Allard2067d1a2008-11-13 22:59:24 +0000640
641 foreach ($key as $k => $v)
642 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300643 $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0)
Andrey Andreev58803fb2012-06-24 00:45:37 +0300644 ? $this->_group_get_type('')
645 : $this->_group_get_type($type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000646
vlakoff1228fe22013-01-14 01:30:09 +0100647 if ($v !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 {
649 if ($escape === TRUE)
650 {
Andrey Andreev3a5efc22012-11-20 21:18:08 +0200651 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 }
WanWizard7219c072011-12-28 14:09:05 +0100653
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 if ( ! $this->_has_operator($k))
655 {
Greg Akere156c6e2011-04-20 16:03:04 -0500656 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 }
658 }
Andrey Andreev3a5efc22012-11-20 21:18:08 +0200659 elseif ( ! $this->_has_operator($k))
660 {
661 // value appears not to have been set, assign the test to IS NULL
662 $k .= ' IS NULL';
663 }
Andrey Andreev5bf4dcd2014-09-29 20:07:15 +0300664 elseif (preg_match('/\s*(!?=|<>)\s*$/i', $k, $match, PREG_OFFSET_CAPTURE))
665 {
666 $k = substr($k, 0, $match[0][1]).($match[1][0] === '=' ? ' IS NULL' : ' IS NOT NULL');
667 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000668
Andrey Andreevd40459d2012-07-18 16:46:39 +0300669 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000670 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300672 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
673 $this->qb_cache_exists[] = substr($qb_key, 3);
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 }
Barry Mienydd671972010-10-04 16:33:58 +0200675
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 }
Barry Mienydd671972010-10-04 16:33:58 +0200677
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 return $this;
679 }
680
681 // --------------------------------------------------------------------
682
683 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200684 * WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200686 * Generates a WHERE field IN('item', 'item') SQL query,
687 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200689 * @param string $key The field to search
690 * @param array $values The values searched on
691 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500692 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300694 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300696 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 }
Barry Mienydd671972010-10-04 16:33:58 +0200698
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 // --------------------------------------------------------------------
700
701 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200702 * OR WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200704 * Generates a WHERE field IN('item', 'item') SQL query,
705 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200707 * @param string $key The field to search
708 * @param array $values The values searched on
709 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500710 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300712 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300714 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 }
716
717 // --------------------------------------------------------------------
718
719 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200720 * WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200722 * Generates a WHERE field NOT IN('item', 'item') SQL query,
723 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200725 * @param string $key The field to search
726 * @param array $values The values searched on
727 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500728 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300730 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300732 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 }
Barry Mienydd671972010-10-04 16:33:58 +0200734
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 // --------------------------------------------------------------------
736
737 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200738 * OR WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200740 * Generates a WHERE field NOT IN('item', 'item') SQL query,
741 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200743 * @param string $key The field to search
744 * @param array $values The values searched on
745 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500746 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300748 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300750 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 }
752
753 // --------------------------------------------------------------------
754
755 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200756 * Internal WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200758 * @used-by where_in()
759 * @used-by or_where_in()
760 * @used-by where_not_in()
761 * @used-by or_where_not_in()
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200763 * @param string $key The field to search
764 * @param array $values The values searched on
765 * @param bool $not If the statement would be IN or NOT IN
766 * @param string $type
767 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500768 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300770 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 {
772 if ($key === NULL OR $values === NULL)
773 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300774 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 }
Barry Mienydd671972010-10-04 16:33:58 +0200776
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 if ( ! is_array($values))
778 {
779 $values = array($values);
780 }
Barry Mienydd671972010-10-04 16:33:58 +0200781
Andrey Andreev498c1e02012-06-16 03:34:10 +0300782 is_bool($escape) OR $escape = $this->_protect_identifiers;
783
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 $not = ($not) ? ' NOT' : '';
785
Andrey Andreev94611df2012-07-19 12:29:54 +0300786 $where_in = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 foreach ($values as $value)
788 {
Andrey Andreevcc02db92012-10-12 14:30:10 +0300789 $where_in[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 }
791
WanWizardbc69f362012-06-22 00:10:11 +0200792 $prefix = (count($this->qb_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Andrey Andreev6e704752012-07-18 00:46:33 +0300793 $where_in = array(
Andrey Andreev94611df2012-07-19 12:29:54 +0300794 'condition' => $prefix.$key.$not.' IN('.implode(', ', $where_in).')',
Andrey Andreev6e704752012-07-18 00:46:33 +0300795 'escape' => $escape
796 );
Barry Mienydd671972010-10-04 16:33:58 +0200797
Andrey Andreev6e704752012-07-18 00:46:33 +0300798 $this->qb_where[] = $where_in;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000799 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000801 $this->qb_cache_where[] = $where_in;
802 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 }
804
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 return $this;
806 }
Barry Mienydd671972010-10-04 16:33:58 +0200807
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 // --------------------------------------------------------------------
809
810 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200811 * LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200813 * Generates a %LIKE% portion of the query.
814 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200816 * @param mixed $field
817 * @param string $match
818 * @param string $side
819 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500820 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300822 public function like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300824 return $this->_like($field, $match, 'AND ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 }
826
827 // --------------------------------------------------------------------
828
829 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200830 * NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200832 * Generates a NOT LIKE portion of the query.
833 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200835 * @param mixed $field
836 * @param string $match
837 * @param string $side
838 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500839 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300841 public function not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300843 return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 }
Barry Mienydd671972010-10-04 16:33:58 +0200845
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 // --------------------------------------------------------------------
847
848 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200849 * OR LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200851 * Generates a %LIKE% portion of the query.
852 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200854 * @param mixed $field
855 * @param string $match
856 * @param string $side
857 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500858 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300860 public function or_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300862 return $this->_like($field, $match, 'OR ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 }
864
865 // --------------------------------------------------------------------
866
867 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200868 * OR NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200870 * Generates a NOT LIKE portion of the query.
871 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200873 * @param mixed $field
874 * @param string $match
875 * @param string $side
876 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500877 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300879 public function or_not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300881 return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 }
Barry Mienydd671972010-10-04 16:33:58 +0200883
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 // --------------------------------------------------------------------
885
886 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200887 * Internal LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200889 * @used-by like()
890 * @used-by or_like()
891 * @used-by not_like()
892 * @used-by or_not_like()
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200894 * @param mixed $field
895 * @param string $match
896 * @param string $type
897 * @param string $side
898 * @param string $not
899 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500900 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300902 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 {
904 if ( ! is_array($field))
905 {
906 $field = array($field => $match);
907 }
Barry Mienydd671972010-10-04 16:33:58 +0200908
Andrey Andreevb0478652012-07-18 15:34:46 +0300909 is_bool($escape) OR $escape = $this->_protect_identifiers;
Andrey Andreevb0478652012-07-18 15:34:46 +0300910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 foreach ($field as $k => $v)
912 {
Andrey Andreev41738232012-11-30 00:13:17 +0200913 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
914 ? $this->_group_get_type('') : $this->_group_get_type($type);
915
Derek Jonese4ed5832009-02-20 21:44:59 +0000916 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300917
Andrey Andreev24276a32012-01-08 02:44:38 +0200918 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400919 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300920 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400921 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200922 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300924 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200926 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300928 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 }
930 else
931 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300932 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600934
Derek Jonese4ed5832009-02-20 21:44:59 +0000935 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100936 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000937 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300938 $like_statement .= sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000939 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600940
Andrey Andreevb0478652012-07-18 15:34:46 +0300941 $this->qb_where[] = array('condition' => $like_statement, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000942 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 {
Andrey Andreev55bbd722013-01-28 19:02:13 +0200944 $this->qb_cache_where[] = array('condition' => $like_statement, 'escape' => $escape);
Andrey Andreevededc4a2012-07-18 01:16:15 +0300945 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200948
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 return $this;
950 }
Barry Mienydd671972010-10-04 16:33:58 +0200951
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 // --------------------------------------------------------------------
953
954 /**
WanWizard7219c072011-12-28 14:09:05 +0100955 * Starts a query group.
956 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200957 * @param string $not (Internal use only)
958 * @param string $type (Internal use only)
Andrew Podner4296a652012-12-17 07:51:15 -0500959 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +0100960 */
961 public function group_start($not = '', $type = 'AND ')
962 {
963 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100964
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000965 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100966 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Andrey Andreev6e704752012-07-18 00:46:33 +0300967 $where = array(
968 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (',
969 'escape' => FALSE
970 );
WanWizard7219c072011-12-28 14:09:05 +0100971
Andrey Andreev6e704752012-07-18 00:46:33 +0300972 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000973 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100974 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300975 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +0100976 }
977
978 return $this;
979 }
980
981 // --------------------------------------------------------------------
982
983 /**
984 * Starts a query group, but ORs the group
985 *
Andrew Podner4296a652012-12-17 07:51:15 -0500986 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +0100987 */
988 public function or_group_start()
989 {
990 return $this->group_start('', 'OR ');
991 }
992
993 // --------------------------------------------------------------------
994
995 /**
996 * Starts a query group, but NOTs the group
997 *
Andrew Podner4296a652012-12-17 07:51:15 -0500998 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +0100999 */
1000 public function not_group_start()
1001 {
1002 return $this->group_start('NOT ', 'AND ');
1003 }
1004
1005 // --------------------------------------------------------------------
1006
1007 /**
1008 * Starts a query group, but OR NOTs the group
1009 *
Andrew Podner4296a652012-12-17 07:51:15 -05001010 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001011 */
1012 public function or_not_group_start()
1013 {
1014 return $this->group_start('NOT ', 'OR ');
1015 }
1016
1017 // --------------------------------------------------------------------
1018
1019 /**
1020 * Ends a query group
1021 *
Andrew Podner4296a652012-12-17 07:51:15 -05001022 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001023 */
1024 public function group_end()
1025 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001026 $this->qb_where_group_started = FALSE;
Andrey Andreev6e704752012-07-18 00:46:33 +03001027 $where = array(
1028 'condition' => str_repeat(' ', $this->qb_where_group_count--).')',
1029 'escape' => FALSE
1030 );
WanWizard7219c072011-12-28 14:09:05 +01001031
Andrey Andreev6e704752012-07-18 00:46:33 +03001032 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001033 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +01001034 {
Andrey Andreev6e704752012-07-18 00:46:33 +03001035 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +01001036 }
1037
WanWizard7219c072011-12-28 14:09:05 +01001038 return $this;
1039 }
1040
1041 // --------------------------------------------------------------------
1042
1043 /**
1044 * Group_get_type
1045 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001046 * @used-by group_start()
1047 * @used-by _like()
1048 * @used-by _wh()
1049 * @used-by _where_in()
WanWizard7219c072011-12-28 14:09:05 +01001050 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001051 * @param string $type
WanWizard7219c072011-12-28 14:09:05 +01001052 * @return string
1053 */
1054 protected function _group_get_type($type)
1055 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001056 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +01001057 {
1058 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001059 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +01001060 }
1061
1062 return $type;
1063 }
1064
1065 // --------------------------------------------------------------------
1066
1067 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 * GROUP BY
1069 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001070 * @param string $by
1071 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -05001072 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001074 public function group_by($by, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001076 is_bool($escape) OR $escape = $this->_protect_identifiers;
1077
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 if (is_string($by))
1079 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001080 $by = ($escape === TRUE)
1081 ? explode(',', $by)
1082 : array($by);
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 }
Barry Mienydd671972010-10-04 16:33:58 +02001084
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 foreach ($by as $val)
1086 {
1087 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +02001088
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001089 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001091 $val = array('field' => $val, 'escape' => $escape);
Barry Mienydd671972010-10-04 16:33:58 +02001092
Andrey Andreev96feb582012-07-19 13:12:34 +03001093 $this->qb_groupby[] = $val;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001094 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001096 $this->qb_cache_groupby[] = $val;
1097 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 }
1099 }
1100 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001101
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 return $this;
1103 }
1104
1105 // --------------------------------------------------------------------
1106
1107 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001108 * HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001110 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001112 * @param string $key
1113 * @param string $value
1114 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 * @return object
1116 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001117 public function having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001119 return $this->_wh('qb_having', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 }
Barry Mienydd671972010-10-04 16:33:58 +02001121
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 // --------------------------------------------------------------------
1123
1124 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001125 * OR HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001127 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001129 * @param string $key
1130 * @param string $value
1131 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 * @return object
1133 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001134 public function or_having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001136 return $this->_wh('qb_having', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 }
Barry Mienydd671972010-10-04 16:33:58 +02001138
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 // --------------------------------------------------------------------
1140
1141 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001142 * ORDER BY
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001144 * @param string $orderby
vlakoff9f8e2992013-09-08 14:05:04 +02001145 * @param string $direction ASC, DESC or RANDOM
Andrey Andreevae85eb42012-11-02 01:42:31 +02001146 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -05001147 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 */
Andrey Andreevd24160c2012-06-16 03:21:20 +03001149 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001151 $direction = strtoupper(trim($direction));
Andrey Andreev2d486232012-07-19 14:46:51 +03001152
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001153 if ($direction === 'RANDOM')
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001155 $direction = '';
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001156
1157 // Do we have a seed value?
1158 $orderby = ctype_digit((string) $orderby)
vlakoffe6c4d5b2013-09-08 13:54:57 +02001159 ? sprintf($this->_random_keyword[1], $orderby)
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001160 : $this->_random_keyword[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 }
Andrey Andreev2d486232012-07-19 14:46:51 +03001162 elseif (empty($orderby))
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001164 return $this;
1165 }
1166 elseif ($direction !== '')
1167 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001168 $direction = in_array($direction, array('ASC', 'DESC'), TRUE) ? ' '.$direction : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 }
Barry Mienydd671972010-10-04 16:33:58 +02001170
Andrey Andreevd24160c2012-06-16 03:21:20 +03001171 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +02001172
Andrey Andreev2d486232012-07-19 14:46:51 +03001173 if ($escape === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 {
Andrey Andreev93dd2f22012-10-24 10:09:18 +03001175 $qb_orderby[] = array('field' => $orderby, 'direction' => $direction, 'escape' => FALSE);
Andrey Andreev2d486232012-07-19 14:46:51 +03001176 }
1177 else
1178 {
1179 $qb_orderby = array();
1180 foreach (explode(',', $orderby) as $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001182 $qb_orderby[] = ($direction === '' && preg_match('/\s+(ASC|DESC)$/i', rtrim($field), $match, PREG_OFFSET_CAPTURE))
1183 ? array('field' => ltrim(substr($field, 0, $match[0][1])), 'direction' => ' '.$match[1][0], 'escape' => TRUE)
1184 : array('field' => trim($field), 'direction' => $direction, 'escape' => TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 }
Barry Mienydd671972010-10-04 16:33:58 +02001187
Andrey Andreev2d486232012-07-19 14:46:51 +03001188 $this->qb_orderby = array_merge($this->qb_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001189 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001191 $this->qb_cache_orderby = array_merge($this->qb_cache_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001192 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 }
1194
1195 return $this;
1196 }
Barry Mienydd671972010-10-04 16:33:58 +02001197
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 // --------------------------------------------------------------------
1199
1200 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001201 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001203 * @param int $value LIMIT value
1204 * @param int $offset OFFSET value
Andrew Podner4296a652012-12-17 07:51:15 -05001205 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001207 public function limit($value, $offset = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 {
vlakoff912f1bc2013-01-15 03:34:12 +01001209 is_null($value) OR $this->qb_limit = (int) $value;
Andrey Andreev777153d2012-06-18 13:30:45 +03001210 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001211
Derek Allard2067d1a2008-11-13 22:59:24 +00001212 return $this;
1213 }
Barry Mienydd671972010-10-04 16:33:58 +02001214
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 // --------------------------------------------------------------------
1216
1217 /**
1218 * Sets the OFFSET value
1219 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001220 * @param int $offset OFFSET value
Andrew Podner4296a652012-12-17 07:51:15 -05001221 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001223 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001225 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001226 return $this;
1227 }
Barry Mienydd671972010-10-04 16:33:58 +02001228
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 // --------------------------------------------------------------------
1230
1231 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001232 * LIMIT string
Andrey Andreev2c35b642012-06-24 03:05:26 +03001233 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001234 * Generates a platform-specific LIMIT clause.
Andrey Andreev2c35b642012-06-24 03:05:26 +03001235 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001236 * @param string $sql SQL Query
Andrey Andreev2c35b642012-06-24 03:05:26 +03001237 * @return string
1238 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001239 protected function _limit($sql)
Andrey Andreev2c35b642012-06-24 03:05:26 +03001240 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001241 return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').$this->qb_limit;
Andrey Andreev2c35b642012-06-24 03:05:26 +03001242 }
1243
1244 // --------------------------------------------------------------------
1245
1246 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001247 * The "set" function.
1248 *
1249 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 * @param mixed
1252 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001253 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001254 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001256 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001257 {
1258 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001259
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 if ( ! is_array($key))
1261 {
1262 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001263 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001264
Andrey Andreevfe642da2012-06-16 03:47:33 +03001265 is_bool($escape) OR $escape = $this->_protect_identifiers;
1266
Derek Allard2067d1a2008-11-13 22:59:24 +00001267 foreach ($key as $k => $v)
1268 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001269 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1270 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001271 }
Barry Mienydd671972010-10-04 16:33:58 +02001272
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 return $this;
1274 }
WanWizard7219c072011-12-28 14:09:05 +01001275
Kyle Farris0c147b32011-08-26 02:29:31 -04001276 // --------------------------------------------------------------------
1277
1278 /**
1279 * Get SELECT query string
1280 *
1281 * Compiles a SELECT query string and returns the sql.
1282 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001283 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001284 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001285 * @return string
1286 */
WanWizard7219c072011-12-28 14:09:05 +01001287 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001288 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001289 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001290 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001291 $this->_track_aliases($table);
1292 $this->from($table);
1293 }
WanWizard7219c072011-12-28 14:09:05 +01001294
Andrey Andreev650b4c02012-06-11 12:07:15 +03001295 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001296
Kyle Farris0c147b32011-08-26 02:29:31 -04001297 if ($reset === TRUE)
1298 {
1299 $this->_reset_select();
1300 }
WanWizard7219c072011-12-28 14:09:05 +01001301
Kyle Farris0c147b32011-08-26 02:29:31 -04001302 return $select;
1303 }
WanWizard7219c072011-12-28 14:09:05 +01001304
Derek Allard2067d1a2008-11-13 22:59:24 +00001305 // --------------------------------------------------------------------
1306
1307 /**
1308 * Get
1309 *
1310 * Compiles the select statement based on the other functions called
1311 * and runs the query
1312 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001313 * @param string the table
1314 * @param string the limit clause
1315 * @param string the offset clause
1316 * @return object
1317 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001318 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001319 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001320 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 {
1322 $this->_track_aliases($table);
1323 $this->from($table);
1324 }
Barry Mienydd671972010-10-04 16:33:58 +02001325
Andrey Andreev650b4c02012-06-11 12:07:15 +03001326 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 {
1328 $this->limit($limit, $offset);
1329 }
Barry Mienydd671972010-10-04 16:33:58 +02001330
Andrey Andreev24276a32012-01-08 02:44:38 +02001331 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001332 $this->_reset_select();
1333 return $result;
1334 }
1335
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001336 // --------------------------------------------------------------------
1337
Derek Allard2067d1a2008-11-13 22:59:24 +00001338 /**
1339 * "Count All Results" query
1340 *
Barry Mienydd671972010-10-04 16:33:58 +02001341 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001342 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001344 * @param string
vlakoffc6ac7482013-11-17 00:50:06 +01001345 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +00001346 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001347 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001349 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001350 {
1351 $this->_track_aliases($table);
1352 $this->from($table);
1353 }
Barry Mienydd671972010-10-04 16:33:58 +02001354
Andrey Andreevb05f5062012-10-26 12:01:02 +03001355 $result = ($this->qb_distinct === TRUE)
1356 ? $this->query($this->_count_string.$this->protect_identifiers('numrows')."\nFROM (\n".$this->_compile_select()."\n) CI_count_all_results")
1357 : $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001358 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001359
Purwandi1d160e72012-01-09 16:33:28 +07001360 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001362 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 }
1364
Purwandi1d160e72012-01-09 16:33:28 +07001365 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001366 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001368
Derek Allard2067d1a2008-11-13 22:59:24 +00001369 // --------------------------------------------------------------------
1370
1371 /**
1372 * Get_Where
1373 *
1374 * Allows the where clause, limit and offset to be added directly
1375 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001376 * @param string $table
1377 * @param string $where
1378 * @param int $limit
1379 * @param int $offset
Derek Allard2067d1a2008-11-13 22:59:24 +00001380 * @return object
1381 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001382 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001383 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001384 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001385 {
1386 $this->from($table);
1387 }
1388
vlakoff1228fe22013-01-14 01:30:09 +01001389 if ($where !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 {
1391 $this->where($where);
1392 }
Barry Mienydd671972010-10-04 16:33:58 +02001393
Andrey Andreev650b4c02012-06-11 12:07:15 +03001394 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001395 {
1396 $this->limit($limit, $offset);
1397 }
Barry Mienydd671972010-10-04 16:33:58 +02001398
Andrey Andreev24276a32012-01-08 02:44:38 +02001399 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001400 $this->_reset_select();
1401 return $result;
1402 }
1403
1404 // --------------------------------------------------------------------
1405
1406 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001407 * Insert_Batch
1408 *
1409 * Compiles batch insert strings and runs the queries
1410 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001411 * @param string $table Table to insert into
1412 * @param array $set An associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001413 * @param bool $escape Whether to escape values and identifiers
Andrey Andreevae85eb42012-11-02 01:42:31 +02001414 * @return int Number of rows inserted or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001415 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001416 public function insert_batch($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001417 {
vlakoff1228fe22013-01-14 01:30:09 +01001418 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001419 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001420 $this->set_insert_batch($set, '', $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001421 }
Barry Mienydd671972010-10-04 16:33:58 +02001422
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001423 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001424 {
Andrey Andreev9f808b02012-10-24 17:38:48 +03001425 // No valid data array. Folds in cases where keys and values did not match up
1426 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001427 }
1428
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001429 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001430 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001431 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001432 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001433 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001434 }
Barry Mienydd671972010-10-04 16:33:58 +02001435
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001436 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001437 }
1438
1439 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001440 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001441 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001442 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001443 $this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, $escape, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, 100)));
Andrey Andreev9f808b02012-10-24 17:38:48 +03001444 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001445 }
Barry Mienydd671972010-10-04 16:33:58 +02001446
Derek Jonesd10e8962010-03-02 17:10:36 -06001447 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001448 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001449 }
1450
1451 // --------------------------------------------------------------------
1452
1453 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +02001454 * Insert batch statement
Andrey Andreev97f36972012-04-05 12:44:36 +03001455 *
1456 * Generates a platform-specific insert string from the supplied data.
1457 *
Andrey Andreev083e3c82012-11-06 12:48:32 +02001458 * @param string $table Table name
1459 * @param array $keys INSERT keys
1460 * @param array $values INSERT values
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001461 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001462 */
1463 protected function _insert_batch($table, $keys, $values)
1464 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001465 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001466 }
1467
1468 // --------------------------------------------------------------------
1469
1470 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001471 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001472 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001473 * @param mixed
1474 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001475 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001476 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001477 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001478 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001479 {
1480 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001481
Derek Jonesd10e8962010-03-02 17:10:36 -06001482 if ( ! is_array($key))
1483 {
1484 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001485 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001486
Andrey Andreevfe642da2012-06-16 03:47:33 +03001487 is_bool($escape) OR $escape = $this->_protect_identifiers;
1488
Iban Eguia3c0a4522012-04-15 13:30:44 +02001489 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001490 sort($keys);
1491
1492 foreach ($key as $row)
1493 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001494 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001495 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1496 {
1497 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001498 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001499 return;
1500 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001501
Barry Mienydd671972010-10-04 16:33:58 +02001502 ksort($row); // puts $row in the same order as our keys
1503
Andrey Andreev650b4c02012-06-11 12:07:15 +03001504 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001505 {
1506 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001507 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001508 {
Barry Mienydd671972010-10-04 16:33:58 +02001509 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001510 }
1511
Andrey Andreev650b4c02012-06-11 12:07:15 +03001512 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001513 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001514
Andrey Andreev838a9d62012-12-03 14:37:47 +02001515 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001516 }
1517
1518 foreach ($keys as $k)
1519 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001520 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001521 }
Barry Mienydd671972010-10-04 16:33:58 +02001522
Derek Jonesd10e8962010-03-02 17:10:36 -06001523 return $this;
1524 }
WanWizard7219c072011-12-28 14:09:05 +01001525
Kyle Farris0c147b32011-08-26 02:29:31 -04001526 // --------------------------------------------------------------------
1527
1528 /**
1529 * Get INSERT query string
1530 *
1531 * Compiles an insert query and returns the sql
1532 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001533 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001534 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001535 * @return string
1536 */
1537 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001538 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001539 if ($this->_validate_insert($table) === FALSE)
1540 {
1541 return FALSE;
1542 }
WanWizard7219c072011-12-28 14:09:05 +01001543
Kyle Farris76116012011-08-31 11:17:48 -04001544 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001545 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001546 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001547 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001548 array_keys($this->qb_set),
1549 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001550 );
WanWizard7219c072011-12-28 14:09:05 +01001551
Kyle Farris0c147b32011-08-26 02:29:31 -04001552 if ($reset === TRUE)
1553 {
1554 $this->_reset_write();
1555 }
WanWizard7219c072011-12-28 14:09:05 +01001556
Kyle Farris0c147b32011-08-26 02:29:31 -04001557 return $sql;
1558 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001559
Derek Allard2067d1a2008-11-13 22:59:24 +00001560 // --------------------------------------------------------------------
1561
1562 /**
1563 * Insert
1564 *
1565 * Compiles an insert string and runs the query
1566 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001567 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001568 * @param array an associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001569 * @param bool $escape Whether to escape values and identifiers
Derek Allard2067d1a2008-11-13 22:59:24 +00001570 * @return object
1571 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001572 public function insert($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001573 {
vlakoff1228fe22013-01-14 01:30:09 +01001574 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001575 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001576 $this->set($set, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001577 }
WanWizard7219c072011-12-28 14:09:05 +01001578
Kyle Farris0c147b32011-08-26 02:29:31 -04001579 if ($this->_validate_insert($table) === FALSE)
1580 {
1581 return FALSE;
1582 }
WanWizard7219c072011-12-28 14:09:05 +01001583
Kyle Farris76116012011-08-31 11:17:48 -04001584 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001585 $this->protect_identifiers(
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001586 $this->qb_from[0], TRUE, $escape, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001587 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001588 array_keys($this->qb_set),
1589 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001590 );
Barry Mienydd671972010-10-04 16:33:58 +02001591
Kyle Farris0c147b32011-08-26 02:29:31 -04001592 $this->_reset_write();
1593 return $this->query($sql);
1594 }
WanWizard7219c072011-12-28 14:09:05 +01001595
Kyle Farris0c147b32011-08-26 02:29:31 -04001596 // --------------------------------------------------------------------
1597
1598 /**
1599 * Validate Insert
1600 *
1601 * This method is used by both insert() and get_compiled_insert() to
1602 * validate that the there data is actually being set and that table
1603 * has been chosen to be inserted into.
1604 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001605 * @param string the table to insert data into
1606 * @return string
1607 */
WanWizard7219c072011-12-28 14:09:05 +01001608 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001609 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001610 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001611 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001612 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001613 }
1614
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001615 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001616 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001617 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001618 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001619 elseif ( ! isset($this->qb_from[0]))
1620 {
1621 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1622 }
WanWizard7219c072011-12-28 14:09:05 +01001623
Kyle Farris0c147b32011-08-26 02:29:31 -04001624 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001625 }
Barry Mienydd671972010-10-04 16:33:58 +02001626
Phil Sturgeon9789f322011-07-15 15:14:05 -06001627 // --------------------------------------------------------------------
1628
1629 /**
1630 * Replace
1631 *
1632 * Compiles an replace into string and runs the query
1633 *
1634 * @param string the table to replace data into
1635 * @param array an associative array of insert values
1636 * @return object
1637 */
1638 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001639 {
vlakoff1228fe22013-01-14 01:30:09 +01001640 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001641 {
1642 $this->set($set);
1643 }
Barry Mienydd671972010-10-04 16:33:58 +02001644
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001645 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001646 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001647 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001648 }
1649
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001650 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001651 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001652 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001653 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001654 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001655 }
Barry Mienydd671972010-10-04 16:33:58 +02001656
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001657 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001658 }
1659
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001660 $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 +00001661
Derek Jonesd10e8962010-03-02 17:10:36 -06001662 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001663 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001664 }
WanWizard7219c072011-12-28 14:09:05 +01001665
Kyle Farris0c147b32011-08-26 02:29:31 -04001666 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001667
Kyle Farris0c147b32011-08-26 02:29:31 -04001668 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001669 * Replace statement
1670 *
1671 * Generates a platform-specific replace string from the supplied data
1672 *
1673 * @param string the table name
1674 * @param array the insert keys
1675 * @param array the insert values
1676 * @return string
1677 */
1678 protected function _replace($table, $keys, $values)
1679 {
1680 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1681 }
1682
1683 // --------------------------------------------------------------------
1684
1685 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001686 * FROM tables
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001687 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001688 * Groups tables in FROM clauses if needed, so there is no confusion
1689 * about operator precedence.
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001690 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001691 * Note: This is only used (and overriden) by MySQL and CUBRID.
1692 *
1693 * @return string
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001694 */
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001695 protected function _from_tables()
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001696 {
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001697 return implode(', ', $this->qb_from);
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001698 }
1699
1700 // --------------------------------------------------------------------
1701
1702 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001703 * Get UPDATE query string
1704 *
1705 * Compiles an update query and returns the sql
1706 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001707 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001708 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001709 * @return string
1710 */
1711 public function get_compiled_update($table = '', $reset = TRUE)
1712 {
1713 // Combine any cached components with the current statements
1714 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001715
Kyle Farris0c147b32011-08-26 02:29:31 -04001716 if ($this->_validate_update($table) === FALSE)
1717 {
1718 return FALSE;
1719 }
WanWizard7219c072011-12-28 14:09:05 +01001720
Andrey Andreevb0478652012-07-18 15:34:46 +03001721 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001722
Kyle Farris0c147b32011-08-26 02:29:31 -04001723 if ($reset === TRUE)
1724 {
1725 $this->_reset_write();
1726 }
WanWizard7219c072011-12-28 14:09:05 +01001727
Kyle Farris0c147b32011-08-26 02:29:31 -04001728 return $sql;
1729 }
WanWizard7219c072011-12-28 14:09:05 +01001730
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 // --------------------------------------------------------------------
1732
1733 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001734 * UPDATE
Derek Allard2067d1a2008-11-13 22:59:24 +00001735 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001736 * Compiles an update string and runs the query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001737 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001738 * @param string $table
1739 * @param array $set An associative array of update values
1740 * @param mixed $where
1741 * @param int $limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 * @return object
1743 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001744 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001745 {
1746 // Combine any cached components with the current statements
1747 $this->_merge_cache();
1748
vlakoff1228fe22013-01-14 01:30:09 +01001749 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001750 {
1751 $this->set($set);
1752 }
Barry Mienydd671972010-10-04 16:33:58 +02001753
Kyle Farris0c147b32011-08-26 02:29:31 -04001754 if ($this->_validate_update($table) === FALSE)
1755 {
1756 return FALSE;
1757 }
1758
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001759 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001760 {
1761 $this->where($where);
1762 }
1763
Andrey Andreev650b4c02012-06-11 12:07:15 +03001764 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001765 {
1766 $this->limit($limit);
1767 }
1768
Andrey Andreevb0478652012-07-18 15:34:46 +03001769 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
Kyle Farris0c147b32011-08-26 02:29:31 -04001770 $this->_reset_write();
1771 return $this->query($sql);
1772 }
WanWizard7219c072011-12-28 14:09:05 +01001773
Kyle Farris0c147b32011-08-26 02:29:31 -04001774 // --------------------------------------------------------------------
1775
1776 /**
1777 * Validate Update
1778 *
1779 * This method is used by both update() and get_compiled_update() to
1780 * validate that data is actually being set and that a table has been
1781 * chosen to be update.
1782 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001783 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001784 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001785 */
1786 protected function _validate_update($table = '')
1787 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001788 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001790 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 }
1792
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001793 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001794 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001795 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001796 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001797 elseif ( ! isset($this->qb_from[0]))
1798 {
1799 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1800 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001801
1802 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001803 }
WanWizard7219c072011-12-28 14:09:05 +01001804
Derek Jonesd10e8962010-03-02 17:10:36 -06001805 // --------------------------------------------------------------------
1806
1807 /**
1808 * Update_Batch
1809 *
1810 * Compiles an update string and runs the query
1811 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001812 * @param string the table to retrieve the results from
1813 * @param array an associative array of update values
1814 * @param string the where key
Andrey Andreev9f808b02012-10-24 17:38:48 +03001815 * @return int number of rows affected or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001816 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001817 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001818 {
1819 // Combine any cached components with the current statements
1820 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001821
vlakoff1228fe22013-01-14 01:30:09 +01001822 if ($index === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001823 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001824 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001825 }
1826
vlakoff1228fe22013-01-14 01:30:09 +01001827 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001828 {
1829 $this->set_update_batch($set, $index);
1830 }
1831
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001832 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001833 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001834 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001835 }
1836
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001837 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001838 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001839 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001840 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001841 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001842 }
Barry Mienydd671972010-10-04 16:33:58 +02001843
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001844 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001845 }
Barry Mienydd671972010-10-04 16:33:58 +02001846
Derek Jonesd10e8962010-03-02 17:10:36 -06001847 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001848 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001849 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001850 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001851 $this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, 100), $this->protect_identifiers($index)));
Andrey Andreev9f808b02012-10-24 17:38:48 +03001852 $affected_rows += $this->affected_rows();
Andrey Andreev79f888b2013-08-06 13:59:23 +03001853 $this->qb_where = array();
Derek Jonesd10e8962010-03-02 17:10:36 -06001854 }
Barry Mienydd671972010-10-04 16:33:58 +02001855
Derek Jonesd10e8962010-03-02 17:10:36 -06001856 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001857 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001858 }
1859
1860 // --------------------------------------------------------------------
1861
1862 /**
Andrey Andreev219565d2013-03-12 20:00:08 +02001863 * Update_Batch statement
1864 *
1865 * Generates a platform-specific batch update string from the supplied data
1866 *
1867 * @param string $table Table name
1868 * @param array $values Update data
1869 * @param string $index WHERE key
1870 * @return string
1871 */
1872 protected function _update_batch($table, $values, $index)
1873 {
1874 $ids = array();
1875 foreach ($values as $key => $val)
1876 {
1877 $ids[] = $val[$index];
1878
1879 foreach (array_keys($val) as $field)
1880 {
1881 if ($field !== $index)
1882 {
1883 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
1884 }
1885 }
1886 }
1887
1888 $cases = '';
1889 foreach ($final as $k => $v)
1890 {
1891 $cases .= $k." = CASE \n"
1892 .implode("\n", $v)."\n"
1893 .'ELSE '.$k.' END, ';
1894 }
1895
1896 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
1897
1898 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
1899 }
1900
1901 // --------------------------------------------------------------------
1902
1903 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001904 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001905 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001906 * @param array
1907 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001908 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001909 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001910 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001911 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001912 {
1913 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001914
Derek Jonesd10e8962010-03-02 17:10:36 -06001915 if ( ! is_array($key))
1916 {
1917 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001918 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001919
Andrey Andreevfe642da2012-06-16 03:47:33 +03001920 is_bool($escape) OR $escape = $this->_protect_identifiers;
1921
Derek Jonesd10e8962010-03-02 17:10:36 -06001922 foreach ($key as $k => $v)
1923 {
1924 $index_set = FALSE;
1925 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001926 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001927 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001928 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001929 {
1930 $index_set = TRUE;
1931 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001932
Andrey Andreevfe642da2012-06-16 03:47:33 +03001933 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001934 }
1935
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001936 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001937 {
1938 return $this->display_error('db_batch_missing_index');
1939 }
1940
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001941 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001942 }
Barry Mienydd671972010-10-04 16:33:58 +02001943
Derek Jonesd10e8962010-03-02 17:10:36 -06001944 return $this;
1945 }
1946
Derek Allard2067d1a2008-11-13 22:59:24 +00001947 // --------------------------------------------------------------------
1948
1949 /**
1950 * Empty Table
1951 *
1952 * Compiles a delete string and runs "DELETE FROM table"
1953 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001954 * @param string the table to empty
1955 * @return object
1956 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001957 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001958 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001959 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001960 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001961 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001962 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001963 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001964 }
1965
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001966 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001967 }
1968 else
1969 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001970 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001971 }
1972
1973 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001974 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001975 return $this->query($sql);
1976 }
1977
1978 // --------------------------------------------------------------------
1979
1980 /**
1981 * Truncate
1982 *
1983 * Compiles a truncate string and runs the query
1984 * If the database does not support the truncate() command
1985 * This function maps to "DELETE FROM table"
1986 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001987 * @param string the table to truncate
1988 * @return object
1989 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001990 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001991 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001992 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001993 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001994 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001995 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001996 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001997 }
1998
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001999 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002000 }
2001 else
2002 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002003 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002004 }
2005
2006 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002007 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00002008 return $this->query($sql);
2009 }
WanWizard7219c072011-12-28 14:09:05 +01002010
Kyle Farris0c147b32011-08-26 02:29:31 -04002011 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02002012
Kyle Farris0c147b32011-08-26 02:29:31 -04002013 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03002014 * Truncate statement
2015 *
2016 * Generates a platform-specific truncate string from the supplied data
2017 *
2018 * If the database does not support the truncate() command,
2019 * then this method maps to 'DELETE FROM table'
2020 *
2021 * @param string the table name
2022 * @return string
2023 */
2024 protected function _truncate($table)
2025 {
2026 return 'TRUNCATE '.$table;
2027 }
2028
2029 // --------------------------------------------------------------------
2030
2031 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04002032 * Get DELETE query string
2033 *
2034 * Compiles a delete query string and returns the sql
2035 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002036 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002037 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04002038 * @return string
2039 */
2040 public function get_compiled_delete($table = '', $reset = TRUE)
2041 {
2042 $this->return_delete_sql = TRUE;
2043 $sql = $this->delete($table, '', NULL, $reset);
2044 $this->return_delete_sql = FALSE;
2045 return $sql;
2046 }
WanWizard7219c072011-12-28 14:09:05 +01002047
Derek Allard2067d1a2008-11-13 22:59:24 +00002048 // --------------------------------------------------------------------
2049
2050 /**
2051 * Delete
2052 *
2053 * Compiles a delete string and runs the query
2054 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002055 * @param mixed the table(s) to delete from. String or array
2056 * @param mixed the where clause
2057 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03002058 * @param bool
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002059 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00002060 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002061 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002062 {
2063 // Combine any cached components with the current statements
2064 $this->_merge_cache();
2065
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002066 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002067 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002068 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002069 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002070 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002071 }
2072
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002073 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002074 }
2075 elseif (is_array($table))
2076 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05002077 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002078 {
Andrey Andreev13f50542012-10-12 12:31:02 +03002079 $this->delete($single_table, $where, $limit, $reset_data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002080 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002081 return;
2082 }
2083 else
2084 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002085 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002086 }
2087
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002088 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002089 {
2090 $this->where($where);
2091 }
2092
Andrey Andreev650b4c02012-06-11 12:07:15 +03002093 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002094 {
2095 $this->limit($limit);
2096 }
2097
Andrey Andreev94611df2012-07-19 12:29:54 +03002098 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002099 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002100 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02002101 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002102
Andrey Andreevb0478652012-07-18 15:34:46 +03002103 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002104 if ($reset_data)
2105 {
2106 $this->_reset_write();
2107 }
WanWizard7219c072011-12-28 14:09:05 +01002108
Andrey Andreev24276a32012-01-08 02:44:38 +02002109 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00002110 }
WanWizard7219c072011-12-28 14:09:05 +01002111
Derek Allard2067d1a2008-11-13 22:59:24 +00002112 // --------------------------------------------------------------------
2113
2114 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03002115 * Delete statement
2116 *
2117 * Generates a platform-specific delete string from the supplied data
2118 *
2119 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03002120 * @return string
2121 */
Andrey Andreevb0478652012-07-18 15:34:46 +03002122 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03002123 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002124 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002125 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03002126 }
2127
2128 // --------------------------------------------------------------------
2129
2130 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002131 * DB Prefix
2132 *
2133 * Prepends a database prefix if one exists in configuration
2134 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002135 * @param string the table
2136 * @return string
2137 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002138 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002139 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002140 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002141 {
2142 $this->display_error('db_table_name_required');
2143 }
2144
2145 return $this->dbprefix.$table;
2146 }
2147
2148 // --------------------------------------------------------------------
2149
2150 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06002151 * Set DB Prefix
2152 *
2153 * Set's the DB Prefix to something new without needing to reconnect
2154 *
2155 * @param string the prefix
2156 * @return string
2157 */
2158 public function set_dbprefix($prefix = '')
2159 {
2160 return $this->dbprefix = $prefix;
2161 }
2162
2163 // --------------------------------------------------------------------
2164
2165 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002166 * Track Aliases
2167 *
2168 * Used to track SQL statements written with aliased tables.
2169 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002170 * @param string The table to inspect
2171 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002172 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002173 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002174 {
2175 if (is_array($table))
2176 {
2177 foreach ($table as $t)
2178 {
2179 $this->_track_aliases($t);
2180 }
2181 return;
2182 }
Barry Mienydd671972010-10-04 16:33:58 +02002183
Derek Jones37f4b9c2011-07-01 17:56:50 -05002184 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002185 // the string into discreet statements
2186 if (strpos($table, ',') !== FALSE)
2187 {
2188 return $this->_track_aliases(explode(',', $table));
2189 }
Barry Mienydd671972010-10-04 16:33:58 +02002190
Derek Allard2067d1a2008-11-13 22:59:24 +00002191 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002192 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002193 {
2194 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002195 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002196
Derek Allard2067d1a2008-11-13 22:59:24 +00002197 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002198 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002199
Derek Allard2067d1a2008-11-13 22:59:24 +00002200 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002201 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002202 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002203 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002204 }
2205 }
2206 }
WanWizard7219c072011-12-28 14:09:05 +01002207
Derek Allard2067d1a2008-11-13 22:59:24 +00002208 // --------------------------------------------------------------------
2209
2210 /**
2211 * Compile the SELECT statement
2212 *
2213 * Generates a query string based on which functions were used.
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002214 * Should not be called directly.
Derek Allard2067d1a2008-11-13 22:59:24 +00002215 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002216 * @param bool $select_override
Derek Allard2067d1a2008-11-13 22:59:24 +00002217 * @return string
2218 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002219 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002220 {
2221 // Combine any cached components with the current statements
2222 $this->_merge_cache();
2223
Derek Allard2067d1a2008-11-13 22:59:24 +00002224 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002225 if ($select_override !== FALSE)
2226 {
2227 $sql = $select_override;
2228 }
2229 else
2230 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002231 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002232
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002233 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002234 {
Barry Mienydd671972010-10-04 16:33:58 +02002235 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002236 }
2237 else
Barry Mienydd671972010-10-04 16:33:58 +02002238 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002239 // Cycle through the "select" portion of the query and prep each column name.
2240 // The reason we protect identifiers here rather then in the select() function
2241 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002242 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002243 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002244 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002245 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002246 }
Barry Mienydd671972010-10-04 16:33:58 +02002247
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002248 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002249 }
2250 }
2251
Derek Allard2067d1a2008-11-13 22:59:24 +00002252 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002253 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002254 {
Andrey Andreeve78f8152012-10-09 11:38:38 +03002255 $sql .= "\nFROM ".$this->_from_tables();
Derek Allard2067d1a2008-11-13 22:59:24 +00002256 }
2257
Derek Allard2067d1a2008-11-13 22:59:24 +00002258 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002259 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002260 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002261 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002262 }
2263
Andrey Andreev2d486232012-07-19 14:46:51 +03002264 $sql .= $this->_compile_wh('qb_where')
2265 .$this->_compile_group_by()
2266 .$this->_compile_wh('qb_having')
2267 .$this->_compile_order_by(); // ORDER BY
Andrey Andreevb0478652012-07-18 15:34:46 +03002268
Andrey Andreevd40459d2012-07-18 16:46:39 +03002269 // LIMIT
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002270 if ($this->qb_limit)
Derek Allard2067d1a2008-11-13 22:59:24 +00002271 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002272 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002273 }
2274
2275 return $sql;
2276 }
2277
2278 // --------------------------------------------------------------------
2279
2280 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002281 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002282 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002283 * Escapes identifiers in WHERE and HAVING statements at execution time.
2284 *
Andrey Andreevb0478652012-07-18 15:34:46 +03002285 * Required so that aliases are tracked properly, regardless of wether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002286 * where(), or_where(), having(), or_having are called prior to from(),
2287 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002288 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002289 * @param string $qb_key 'qb_where' or 'qb_having'
Andrey Andreevd40459d2012-07-18 16:46:39 +03002290 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002291 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002292 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002293 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002294 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002295 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002296 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002297 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002298 // Is this condition already compiled?
2299 if (is_string($this->{$qb_key}[$i]))
2300 {
2301 continue;
2302 }
2303 elseif ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002304 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002305 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002306 continue;
2307 }
2308
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002309 // Split multiple conditions
2310 $conditions = preg_split(
2311 '/(\s*AND\s+|\s*OR\s+)/i',
2312 $this->{$qb_key}[$i]['condition'],
2313 -1,
2314 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
2315 );
2316
2317 for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002318 {
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002319 if (($op = $this->_get_operator($conditions[$ci])) === FALSE
Andrey Andreeve4742582012-10-25 13:25:13 +03002320 OR ! preg_match('/^(\(?)(.*)('.preg_quote($op, '/').')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches))
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002321 {
2322 continue;
2323 }
2324
2325 // $matches = array(
2326 // 0 => '(test <= foo)', /* the whole thing */
2327 // 1 => '(', /* optional */
2328 // 2 => 'test', /* the field name */
2329 // 3 => ' <= ', /* $op */
2330 // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2331 // 5 => ')' /* optional */
2332 // );
Andrey Andreev082aa402012-10-22 19:41:55 +03002333
2334 if ( ! empty($matches[4]))
2335 {
2336 $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4]));
2337 $matches[4] = ' '.$matches[4];
2338 }
2339
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002340 $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2]))
2341 .' '.trim($matches[3]).$matches[4].$matches[5];
Andrey Andreev6e704752012-07-18 00:46:33 +03002342 }
2343
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002344 $this->{$qb_key}[$i] = implode('', $conditions);
Andrey Andreev6e704752012-07-18 00:46:33 +03002345 }
2346
Andrey Andreev9d3aa1b2012-10-12 12:14:09 +03002347 return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ")
2348 .implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002349 }
2350
Andrey Andreevb0478652012-07-18 15:34:46 +03002351 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002352 }
2353
2354 // --------------------------------------------------------------------
2355
2356 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002357 * Compile GROUP BY
2358 *
2359 * Escapes identifiers in GROUP BY statements at execution time.
2360 *
2361 * Required so that aliases are tracked properly, regardless of wether
2362 * group_by() is called prior to from(), join() and dbprefix is added
2363 * only if needed.
2364 *
2365 * @return string SQL statement
2366 */
2367 protected function _compile_group_by()
2368 {
2369 if (count($this->qb_groupby) > 0)
2370 {
Andrey Andreev96feb582012-07-19 13:12:34 +03002371 for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
2372 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002373 // Is it already compiled?
Andrey Andreev18eba242014-01-23 22:52:31 +02002374 if (is_string($this->qb_groupby[$i]))
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002375 {
2376 continue;
2377 }
2378
Andrey Andreev082aa402012-10-22 19:41:55 +03002379 $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 +03002380 ? $this->qb_groupby[$i]['field']
Andrey Andreev13f50542012-10-12 12:31:02 +03002381 : $this->protect_identifiers($this->qb_groupby[$i]['field']);
Andrey Andreev96feb582012-07-19 13:12:34 +03002382 }
2383
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002384 return "\nGROUP BY ".implode(', ', $this->qb_groupby);
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002385 }
2386
2387 return '';
2388 }
2389
2390 // --------------------------------------------------------------------
2391
2392 /**
Andrey Andreev2d486232012-07-19 14:46:51 +03002393 * Compile ORDER BY
2394 *
2395 * Escapes identifiers in ORDER BY statements at execution time.
2396 *
2397 * Required so that aliases are tracked properly, regardless of wether
2398 * order_by() is called prior to from(), join() and dbprefix is added
2399 * only if needed.
2400 *
2401 * @return string SQL statement
2402 */
2403 protected function _compile_order_by()
2404 {
Andrey Andreeva53ea842012-10-23 12:44:09 +03002405 if (is_array($this->qb_orderby) && count($this->qb_orderby) > 0)
Andrey Andreev2d486232012-07-19 14:46:51 +03002406 {
Andrey Andreev2d486232012-07-19 14:46:51 +03002407 for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++)
2408 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002409 if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field']))
Andrey Andreev2d486232012-07-19 14:46:51 +03002410 {
Andrey Andreevfc043b32012-10-12 14:46:14 +03002411 $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']);
Andrey Andreev2d486232012-07-19 14:46:51 +03002412 }
2413
2414 $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction'];
2415 }
2416
Andrey Andreeva53ea842012-10-23 12:44:09 +03002417 return $this->qb_orderby = "\nORDER BY ".implode(', ', $this->qb_orderby);
2418 }
2419 elseif (is_string($this->qb_orderby))
2420 {
2421 return $this->qb_orderby;
Andrey Andreev2d486232012-07-19 14:46:51 +03002422 }
2423
2424 return '';
2425 }
2426
2427 // --------------------------------------------------------------------
2428
2429 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002430 * Object to Array
2431 *
2432 * Takes an object as input and converts the class variables to array key/vals
2433 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002434 * @param object
2435 * @return array
2436 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002437 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002438 {
2439 if ( ! is_object($object))
2440 {
2441 return $object;
2442 }
Barry Mienydd671972010-10-04 16:33:58 +02002443
Derek Allard2067d1a2008-11-13 22:59:24 +00002444 $array = array();
2445 foreach (get_object_vars($object) as $key => $val)
2446 {
2447 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002448 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002449 {
2450 $array[$key] = $val;
2451 }
2452 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002453
2454 return $array;
2455 }
Barry Mienydd671972010-10-04 16:33:58 +02002456
Derek Jonesd10e8962010-03-02 17:10:36 -06002457 // --------------------------------------------------------------------
2458
2459 /**
2460 * Object to Array
2461 *
2462 * Takes an object as input and converts the class variables to array key/vals
2463 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002464 * @param object
2465 * @return array
2466 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002467 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002468 {
2469 if ( ! is_object($object))
2470 {
2471 return $object;
2472 }
Barry Mienydd671972010-10-04 16:33:58 +02002473
Derek Jonesd10e8962010-03-02 17:10:36 -06002474 $array = array();
2475 $out = get_object_vars($object);
2476 $fields = array_keys($out);
2477
2478 foreach ($fields as $val)
2479 {
2480 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002481 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002482 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002483 $i = 0;
2484 foreach ($out[$val] as $data)
2485 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002486 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002487 }
2488 }
2489 }
2490
Derek Allard2067d1a2008-11-13 22:59:24 +00002491 return $array;
2492 }
Barry Mienydd671972010-10-04 16:33:58 +02002493
Derek Allard2067d1a2008-11-13 22:59:24 +00002494 // --------------------------------------------------------------------
2495
2496 /**
2497 * Start Cache
2498 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002499 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002500 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002501 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002502 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002503 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002504 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002505 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002506 }
2507
2508 // --------------------------------------------------------------------
2509
2510 /**
2511 * Stop Cache
2512 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002513 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002514 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002515 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002516 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002517 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002518 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002519 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002520 }
2521
2522 // --------------------------------------------------------------------
2523
2524 /**
2525 * Flush Cache
2526 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002527 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002528 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002529 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002530 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002531 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002532 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002533 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002534 'qb_cache_select' => array(),
2535 'qb_cache_from' => array(),
2536 'qb_cache_join' => array(),
2537 'qb_cache_where' => array(),
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002538 'qb_cache_groupby' => array(),
2539 'qb_cache_having' => array(),
2540 'qb_cache_orderby' => array(),
2541 'qb_cache_set' => array(),
2542 'qb_cache_exists' => array(),
2543 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002544 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002545 }
2546
2547 // --------------------------------------------------------------------
2548
2549 /**
2550 * Merge Cache
2551 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002552 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002553 * locally called ones.
2554 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002555 * @return void
2556 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002557 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002558 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002559 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002560 {
2561 return;
2562 }
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002563 elseif (in_array('select', $this->qb_cache_exists, TRUE))
2564 {
2565 $qb_no_escape = $this->qb_cache_no_escape;
2566 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002567
GDmac17a05282013-11-11 13:18:09 +01002568 foreach (array_unique($this->qb_cache_exists) as $val) // select, from, etc.
Derek Allard2067d1a2008-11-13 22:59:24 +00002569 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002570 $qb_variable = 'qb_'.$val;
2571 $qb_cache_var = 'qb_cache_'.$val;
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002572 $qb_new = $this->$qb_cache_var;
GDmace1b86832013-11-08 16:52:54 +01002573
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002574 for ($i = 0, $c = count($this->$qb_variable); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00002575 {
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002576 if ( ! in_array($this->{$qb_variable}[$i], $qb_new, TRUE))
2577 {
2578 $qb_new[] = $this->{$qb_variable}[$i];
2579 if ($val === 'select')
2580 {
2581 $qb_no_escape[] = $this->qb_no_escape[$i];
2582 }
2583 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002584 }
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002585
GDmace1b86832013-11-08 16:52:54 +01002586 $this->$qb_variable = $qb_new;
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002587 if ($val === 'select')
2588 {
2589 $this->qb_no_escape = $qb_no_escape;
2590 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002591 }
2592
2593 // If we are "protecting identifiers" we need to examine the "from"
2594 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002595 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002596 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002597 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002598 }
2599 }
WanWizard7219c072011-12-28 14:09:05 +01002600
Kyle Farris0c147b32011-08-26 02:29:31 -04002601 // --------------------------------------------------------------------
2602
2603 /**
Andrey Andreev082aa402012-10-22 19:41:55 +03002604 * Is literal
2605 *
2606 * Determines if a string represents a literal value or a field name
2607 *
Andrey Andreev02e4cd72012-11-13 11:50:47 +02002608 * @param string $str
Andrey Andreev082aa402012-10-22 19:41:55 +03002609 * @return bool
2610 */
2611 protected function _is_literal($str)
2612 {
2613 $str = trim($str);
2614
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002615 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 +03002616 {
2617 return TRUE;
2618 }
2619
2620 static $_str;
2621
2622 if (empty($_str))
2623 {
2624 $_str = ($this->_escape_char !== '"')
2625 ? array('"', "'") : array("'");
2626 }
2627
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002628 return in_array($str[0], $_str, TRUE);
Andrey Andreev082aa402012-10-22 19:41:55 +03002629 }
2630
2631 // --------------------------------------------------------------------
2632
2633 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002634 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002635 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002636 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002637 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002638 * @return void
2639 */
2640 public function reset_query()
2641 {
2642 $this->_reset_select();
2643 $this->_reset_write();
2644 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002645
2646 // --------------------------------------------------------------------
2647
2648 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002649 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002650 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002651 * @param array An array of fields to reset
2652 * @return void
2653 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002654 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002655 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002656 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002657 {
Andrey Andreevae85eb42012-11-02 01:42:31 +02002658 $this->$item = $default_value;
Derek Allard2067d1a2008-11-13 22:59:24 +00002659 }
2660 }
2661
2662 // --------------------------------------------------------------------
2663
2664 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002665 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002666 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002667 * @return void
2668 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002669 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002670 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002671 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002672 'qb_select' => array(),
2673 'qb_from' => array(),
2674 'qb_join' => array(),
2675 'qb_where' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002676 'qb_groupby' => array(),
2677 'qb_having' => array(),
2678 'qb_orderby' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002679 'qb_aliased_tables' => array(),
2680 'qb_no_escape' => array(),
2681 'qb_distinct' => FALSE,
2682 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002683 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002684 )
2685 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002686 }
Barry Mienydd671972010-10-04 16:33:58 +02002687
Derek Allard2067d1a2008-11-13 22:59:24 +00002688 // --------------------------------------------------------------------
2689
2690 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002691 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002692 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002693 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002694 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002695 * @return void
2696 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002697 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002698 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002699 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002700 'qb_set' => array(),
2701 'qb_from' => array(),
Andrey Andreev3e014372013-02-21 15:59:34 +02002702 'qb_join' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002703 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002704 'qb_orderby' => array(),
2705 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002706 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002707 )
2708 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002709 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002710
Derek Allard2067d1a2008-11-13 22:59:24 +00002711}
2712
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002713/* End of file DB_query_builder.php */
Rougin Gutiba7d32502014-08-27 10:52:49 +08002714/* Location: ./system/database/DB_query_builder.php */