blob: c3836ae147ce19dfb40f8de9e2315f6738ab50b7 [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 }
Ivan Tcholakov7e6aba12014-08-21 20:04:52 +0300664 else
665 {
666 $operator = trim($this->_get_operator($k));
667
Ivan Tcholakova3cc8082014-08-22 12:00:05 +0300668 if ($operator === '<>' OR $operator === '!=')
Ivan Tcholakov7e6aba12014-08-21 20:04:52 +0300669 {
670 $k = str_replace($operator, ' IS NOT NULL', $k);
671 }
672 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000673
Andrey Andreevd40459d2012-07-18 16:46:39 +0300674 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000675 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300677 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
678 $this->qb_cache_exists[] = substr($qb_key, 3);
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 }
Barry Mienydd671972010-10-04 16:33:58 +0200680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 }
Barry Mienydd671972010-10-04 16:33:58 +0200682
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 return $this;
684 }
685
686 // --------------------------------------------------------------------
687
688 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200689 * WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200691 * Generates a WHERE field IN('item', 'item') SQL query,
692 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200694 * @param string $key The field to search
695 * @param array $values The values searched on
696 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500697 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300699 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300701 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 }
Barry Mienydd671972010-10-04 16:33:58 +0200703
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 // --------------------------------------------------------------------
705
706 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200707 * OR WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200709 * Generates a WHERE field IN('item', 'item') SQL query,
710 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200712 * @param string $key The field to search
713 * @param array $values The values searched on
714 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500715 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300717 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300719 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 }
721
722 // --------------------------------------------------------------------
723
724 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200725 * WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200727 * Generates a WHERE field NOT IN('item', 'item') SQL query,
728 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200730 * @param string $key The field to search
731 * @param array $values The values searched on
732 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500733 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300735 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300737 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 }
Barry Mienydd671972010-10-04 16:33:58 +0200739
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 // --------------------------------------------------------------------
741
742 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200743 * OR WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200745 * Generates a WHERE field NOT IN('item', 'item') SQL query,
746 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200748 * @param string $key The field to search
749 * @param array $values The values searched on
750 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500751 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300753 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300755 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 }
757
758 // --------------------------------------------------------------------
759
760 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200761 * Internal WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200763 * @used-by where_in()
764 * @used-by or_where_in()
765 * @used-by where_not_in()
766 * @used-by or_where_not_in()
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200768 * @param string $key The field to search
769 * @param array $values The values searched on
770 * @param bool $not If the statement would be IN or NOT IN
771 * @param string $type
772 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500773 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300775 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 {
777 if ($key === NULL OR $values === NULL)
778 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300779 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 }
Barry Mienydd671972010-10-04 16:33:58 +0200781
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 if ( ! is_array($values))
783 {
784 $values = array($values);
785 }
Barry Mienydd671972010-10-04 16:33:58 +0200786
Andrey Andreev498c1e02012-06-16 03:34:10 +0300787 is_bool($escape) OR $escape = $this->_protect_identifiers;
788
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 $not = ($not) ? ' NOT' : '';
790
Andrey Andreev94611df2012-07-19 12:29:54 +0300791 $where_in = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 foreach ($values as $value)
793 {
Andrey Andreevcc02db92012-10-12 14:30:10 +0300794 $where_in[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 }
796
WanWizardbc69f362012-06-22 00:10:11 +0200797 $prefix = (count($this->qb_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Andrey Andreev6e704752012-07-18 00:46:33 +0300798 $where_in = array(
Andrey Andreev94611df2012-07-19 12:29:54 +0300799 'condition' => $prefix.$key.$not.' IN('.implode(', ', $where_in).')',
Andrey Andreev6e704752012-07-18 00:46:33 +0300800 'escape' => $escape
801 );
Barry Mienydd671972010-10-04 16:33:58 +0200802
Andrey Andreev6e704752012-07-18 00:46:33 +0300803 $this->qb_where[] = $where_in;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000804 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000806 $this->qb_cache_where[] = $where_in;
807 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 }
809
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 return $this;
811 }
Barry Mienydd671972010-10-04 16:33:58 +0200812
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 // --------------------------------------------------------------------
814
815 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200816 * LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200818 * Generates a %LIKE% portion of the query.
819 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200821 * @param mixed $field
822 * @param string $match
823 * @param string $side
824 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500825 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300827 public function like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300829 return $this->_like($field, $match, 'AND ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 }
831
832 // --------------------------------------------------------------------
833
834 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200835 * NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200837 * Generates a NOT LIKE portion of the query.
838 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200840 * @param mixed $field
841 * @param string $match
842 * @param string $side
843 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500844 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300846 public function not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300848 return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 }
Barry Mienydd671972010-10-04 16:33:58 +0200850
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 // --------------------------------------------------------------------
852
853 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200854 * OR LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200856 * Generates a %LIKE% portion of the query.
857 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200859 * @param mixed $field
860 * @param string $match
861 * @param string $side
862 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500863 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300865 public function or_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300867 return $this->_like($field, $match, 'OR ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 }
869
870 // --------------------------------------------------------------------
871
872 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200873 * OR NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200875 * Generates a NOT LIKE portion of the query.
876 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200878 * @param mixed $field
879 * @param string $match
880 * @param string $side
881 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500882 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300884 public function or_not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300886 return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 }
Barry Mienydd671972010-10-04 16:33:58 +0200888
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 // --------------------------------------------------------------------
890
891 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200892 * Internal LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200894 * @used-by like()
895 * @used-by or_like()
896 * @used-by not_like()
897 * @used-by or_not_like()
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200899 * @param mixed $field
900 * @param string $match
901 * @param string $type
902 * @param string $side
903 * @param string $not
904 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500905 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300907 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 {
909 if ( ! is_array($field))
910 {
911 $field = array($field => $match);
912 }
Barry Mienydd671972010-10-04 16:33:58 +0200913
Andrey Andreevb0478652012-07-18 15:34:46 +0300914 is_bool($escape) OR $escape = $this->_protect_identifiers;
Andrey Andreevb0478652012-07-18 15:34:46 +0300915
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 foreach ($field as $k => $v)
917 {
Andrey Andreev41738232012-11-30 00:13:17 +0200918 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
919 ? $this->_group_get_type('') : $this->_group_get_type($type);
920
Derek Jonese4ed5832009-02-20 21:44:59 +0000921 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300922
Andrey Andreev24276a32012-01-08 02:44:38 +0200923 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400924 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300925 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400926 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200927 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300929 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200931 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300933 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 }
935 else
936 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300937 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600939
Derek Jonese4ed5832009-02-20 21:44:59 +0000940 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100941 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000942 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300943 $like_statement .= sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000944 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600945
Andrey Andreevb0478652012-07-18 15:34:46 +0300946 $this->qb_where[] = array('condition' => $like_statement, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000947 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 {
Andrey Andreev55bbd722013-01-28 19:02:13 +0200949 $this->qb_cache_where[] = array('condition' => $like_statement, 'escape' => $escape);
Andrey Andreevededc4a2012-07-18 01:16:15 +0300950 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200953
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 return $this;
955 }
Barry Mienydd671972010-10-04 16:33:58 +0200956
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 // --------------------------------------------------------------------
958
959 /**
WanWizard7219c072011-12-28 14:09:05 +0100960 * Starts a query group.
961 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200962 * @param string $not (Internal use only)
963 * @param string $type (Internal use only)
Andrew Podner4296a652012-12-17 07:51:15 -0500964 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +0100965 */
966 public function group_start($not = '', $type = 'AND ')
967 {
968 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100969
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000970 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100971 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Andrey Andreev6e704752012-07-18 00:46:33 +0300972 $where = array(
973 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (',
974 'escape' => FALSE
975 );
WanWizard7219c072011-12-28 14:09:05 +0100976
Andrey Andreev6e704752012-07-18 00:46:33 +0300977 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000978 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100979 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300980 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +0100981 }
982
983 return $this;
984 }
985
986 // --------------------------------------------------------------------
987
988 /**
989 * Starts a query group, but ORs the group
990 *
Andrew Podner4296a652012-12-17 07:51:15 -0500991 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +0100992 */
993 public function or_group_start()
994 {
995 return $this->group_start('', 'OR ');
996 }
997
998 // --------------------------------------------------------------------
999
1000 /**
1001 * Starts a query group, but NOTs the group
1002 *
Andrew Podner4296a652012-12-17 07:51:15 -05001003 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001004 */
1005 public function not_group_start()
1006 {
1007 return $this->group_start('NOT ', 'AND ');
1008 }
1009
1010 // --------------------------------------------------------------------
1011
1012 /**
1013 * Starts a query group, but OR NOTs the group
1014 *
Andrew Podner4296a652012-12-17 07:51:15 -05001015 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001016 */
1017 public function or_not_group_start()
1018 {
1019 return $this->group_start('NOT ', 'OR ');
1020 }
1021
1022 // --------------------------------------------------------------------
1023
1024 /**
1025 * Ends a query group
1026 *
Andrew Podner4296a652012-12-17 07:51:15 -05001027 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001028 */
1029 public function group_end()
1030 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001031 $this->qb_where_group_started = FALSE;
Andrey Andreev6e704752012-07-18 00:46:33 +03001032 $where = array(
1033 'condition' => str_repeat(' ', $this->qb_where_group_count--).')',
1034 'escape' => FALSE
1035 );
WanWizard7219c072011-12-28 14:09:05 +01001036
Andrey Andreev6e704752012-07-18 00:46:33 +03001037 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001038 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +01001039 {
Andrey Andreev6e704752012-07-18 00:46:33 +03001040 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +01001041 }
1042
WanWizard7219c072011-12-28 14:09:05 +01001043 return $this;
1044 }
1045
1046 // --------------------------------------------------------------------
1047
1048 /**
1049 * Group_get_type
1050 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001051 * @used-by group_start()
1052 * @used-by _like()
1053 * @used-by _wh()
1054 * @used-by _where_in()
WanWizard7219c072011-12-28 14:09:05 +01001055 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001056 * @param string $type
WanWizard7219c072011-12-28 14:09:05 +01001057 * @return string
1058 */
1059 protected function _group_get_type($type)
1060 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001061 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +01001062 {
1063 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001064 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +01001065 }
1066
1067 return $type;
1068 }
1069
1070 // --------------------------------------------------------------------
1071
1072 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 * GROUP BY
1074 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001075 * @param string $by
1076 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -05001077 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001079 public function group_by($by, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001081 is_bool($escape) OR $escape = $this->_protect_identifiers;
1082
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 if (is_string($by))
1084 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001085 $by = ($escape === TRUE)
1086 ? explode(',', $by)
1087 : array($by);
Derek Allard2067d1a2008-11-13 22:59:24 +00001088 }
Barry Mienydd671972010-10-04 16:33:58 +02001089
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 foreach ($by as $val)
1091 {
1092 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +02001093
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001094 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001095 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001096 $val = array('field' => $val, 'escape' => $escape);
Barry Mienydd671972010-10-04 16:33:58 +02001097
Andrey Andreev96feb582012-07-19 13:12:34 +03001098 $this->qb_groupby[] = $val;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001099 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001101 $this->qb_cache_groupby[] = $val;
1102 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 }
1104 }
1105 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001106
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 return $this;
1108 }
1109
1110 // --------------------------------------------------------------------
1111
1112 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001113 * HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001115 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001117 * @param string $key
1118 * @param string $value
1119 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 * @return object
1121 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001122 public function having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001124 return $this->_wh('qb_having', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001125 }
Barry Mienydd671972010-10-04 16:33:58 +02001126
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 // --------------------------------------------------------------------
1128
1129 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001130 * OR HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001132 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001134 * @param string $key
1135 * @param string $value
1136 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 * @return object
1138 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001139 public function or_having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001141 return $this->_wh('qb_having', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 }
Barry Mienydd671972010-10-04 16:33:58 +02001143
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 // --------------------------------------------------------------------
1145
1146 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001147 * ORDER BY
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001149 * @param string $orderby
vlakoff9f8e2992013-09-08 14:05:04 +02001150 * @param string $direction ASC, DESC or RANDOM
Andrey Andreevae85eb42012-11-02 01:42:31 +02001151 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -05001152 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 */
Andrey Andreevd24160c2012-06-16 03:21:20 +03001154 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001156 $direction = strtoupper(trim($direction));
Andrey Andreev2d486232012-07-19 14:46:51 +03001157
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001158 if ($direction === 'RANDOM')
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001160 $direction = '';
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001161
1162 // Do we have a seed value?
1163 $orderby = ctype_digit((string) $orderby)
vlakoffe6c4d5b2013-09-08 13:54:57 +02001164 ? sprintf($this->_random_keyword[1], $orderby)
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001165 : $this->_random_keyword[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 }
Andrey Andreev2d486232012-07-19 14:46:51 +03001167 elseif (empty($orderby))
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001169 return $this;
1170 }
1171 elseif ($direction !== '')
1172 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001173 $direction = in_array($direction, array('ASC', 'DESC'), TRUE) ? ' '.$direction : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 }
Barry Mienydd671972010-10-04 16:33:58 +02001175
Andrey Andreevd24160c2012-06-16 03:21:20 +03001176 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +02001177
Andrey Andreev2d486232012-07-19 14:46:51 +03001178 if ($escape === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 {
Andrey Andreev93dd2f22012-10-24 10:09:18 +03001180 $qb_orderby[] = array('field' => $orderby, 'direction' => $direction, 'escape' => FALSE);
Andrey Andreev2d486232012-07-19 14:46:51 +03001181 }
1182 else
1183 {
1184 $qb_orderby = array();
1185 foreach (explode(',', $orderby) as $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001187 $qb_orderby[] = ($direction === '' && preg_match('/\s+(ASC|DESC)$/i', rtrim($field), $match, PREG_OFFSET_CAPTURE))
1188 ? array('field' => ltrim(substr($field, 0, $match[0][1])), 'direction' => ' '.$match[1][0], 'escape' => TRUE)
1189 : array('field' => trim($field), 'direction' => $direction, 'escape' => TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 }
Barry Mienydd671972010-10-04 16:33:58 +02001192
Andrey Andreev2d486232012-07-19 14:46:51 +03001193 $this->qb_orderby = array_merge($this->qb_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001194 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001196 $this->qb_cache_orderby = array_merge($this->qb_cache_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001197 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 }
1199
1200 return $this;
1201 }
Barry Mienydd671972010-10-04 16:33:58 +02001202
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 // --------------------------------------------------------------------
1204
1205 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001206 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001208 * @param int $value LIMIT value
1209 * @param int $offset OFFSET value
Andrew Podner4296a652012-12-17 07:51:15 -05001210 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001212 public function limit($value, $offset = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001213 {
vlakoff912f1bc2013-01-15 03:34:12 +01001214 is_null($value) OR $this->qb_limit = (int) $value;
Andrey Andreev777153d2012-06-18 13:30:45 +03001215 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001216
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 return $this;
1218 }
Barry Mienydd671972010-10-04 16:33:58 +02001219
Derek Allard2067d1a2008-11-13 22:59:24 +00001220 // --------------------------------------------------------------------
1221
1222 /**
1223 * Sets the OFFSET value
1224 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001225 * @param int $offset OFFSET value
Andrew Podner4296a652012-12-17 07:51:15 -05001226 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001228 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001229 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001230 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 return $this;
1232 }
Barry Mienydd671972010-10-04 16:33:58 +02001233
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 // --------------------------------------------------------------------
1235
1236 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001237 * LIMIT string
Andrey Andreev2c35b642012-06-24 03:05:26 +03001238 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001239 * Generates a platform-specific LIMIT clause.
Andrey Andreev2c35b642012-06-24 03:05:26 +03001240 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001241 * @param string $sql SQL Query
Andrey Andreev2c35b642012-06-24 03:05:26 +03001242 * @return string
1243 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001244 protected function _limit($sql)
Andrey Andreev2c35b642012-06-24 03:05:26 +03001245 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001246 return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').$this->qb_limit;
Andrey Andreev2c35b642012-06-24 03:05:26 +03001247 }
1248
1249 // --------------------------------------------------------------------
1250
1251 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001252 * The "set" function.
1253 *
1254 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001256 * @param mixed
1257 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001258 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001259 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001261 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 {
1263 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001264
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 if ( ! is_array($key))
1266 {
1267 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001268 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001269
Andrey Andreevfe642da2012-06-16 03:47:33 +03001270 is_bool($escape) OR $escape = $this->_protect_identifiers;
1271
Derek Allard2067d1a2008-11-13 22:59:24 +00001272 foreach ($key as $k => $v)
1273 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001274 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1275 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 }
Barry Mienydd671972010-10-04 16:33:58 +02001277
Derek Allard2067d1a2008-11-13 22:59:24 +00001278 return $this;
1279 }
WanWizard7219c072011-12-28 14:09:05 +01001280
Kyle Farris0c147b32011-08-26 02:29:31 -04001281 // --------------------------------------------------------------------
1282
1283 /**
1284 * Get SELECT query string
1285 *
1286 * Compiles a SELECT query string and returns the sql.
1287 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001288 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001289 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001290 * @return string
1291 */
WanWizard7219c072011-12-28 14:09:05 +01001292 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001293 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001294 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001295 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001296 $this->_track_aliases($table);
1297 $this->from($table);
1298 }
WanWizard7219c072011-12-28 14:09:05 +01001299
Andrey Andreev650b4c02012-06-11 12:07:15 +03001300 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001301
Kyle Farris0c147b32011-08-26 02:29:31 -04001302 if ($reset === TRUE)
1303 {
1304 $this->_reset_select();
1305 }
WanWizard7219c072011-12-28 14:09:05 +01001306
Kyle Farris0c147b32011-08-26 02:29:31 -04001307 return $select;
1308 }
WanWizard7219c072011-12-28 14:09:05 +01001309
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 // --------------------------------------------------------------------
1311
1312 /**
1313 * Get
1314 *
1315 * Compiles the select statement based on the other functions called
1316 * and runs the query
1317 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001318 * @param string the table
1319 * @param string the limit clause
1320 * @param string the offset clause
1321 * @return object
1322 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001323 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001325 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 {
1327 $this->_track_aliases($table);
1328 $this->from($table);
1329 }
Barry Mienydd671972010-10-04 16:33:58 +02001330
Andrey Andreev650b4c02012-06-11 12:07:15 +03001331 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001332 {
1333 $this->limit($limit, $offset);
1334 }
Barry Mienydd671972010-10-04 16:33:58 +02001335
Andrey Andreev24276a32012-01-08 02:44:38 +02001336 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 $this->_reset_select();
1338 return $result;
1339 }
1340
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001341 // --------------------------------------------------------------------
1342
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 /**
1344 * "Count All Results" query
1345 *
Barry Mienydd671972010-10-04 16:33:58 +02001346 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001347 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 * @param string
vlakoffc6ac7482013-11-17 00:50:06 +01001350 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +00001351 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001352 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001354 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001355 {
1356 $this->_track_aliases($table);
1357 $this->from($table);
1358 }
Barry Mienydd671972010-10-04 16:33:58 +02001359
Andrey Andreevb05f5062012-10-26 12:01:02 +03001360 $result = ($this->qb_distinct === TRUE)
1361 ? $this->query($this->_count_string.$this->protect_identifiers('numrows')."\nFROM (\n".$this->_compile_select()."\n) CI_count_all_results")
1362 : $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001364
Purwandi1d160e72012-01-09 16:33:28 +07001365 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001366 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001367 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 }
1369
Purwandi1d160e72012-01-09 16:33:28 +07001370 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001371 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001372 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001373
Derek Allard2067d1a2008-11-13 22:59:24 +00001374 // --------------------------------------------------------------------
1375
1376 /**
1377 * Get_Where
1378 *
1379 * Allows the where clause, limit and offset to be added directly
1380 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001381 * @param string $table
1382 * @param string $where
1383 * @param int $limit
1384 * @param int $offset
Derek Allard2067d1a2008-11-13 22:59:24 +00001385 * @return object
1386 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001387 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001388 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001389 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 {
1391 $this->from($table);
1392 }
1393
vlakoff1228fe22013-01-14 01:30:09 +01001394 if ($where !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001395 {
1396 $this->where($where);
1397 }
Barry Mienydd671972010-10-04 16:33:58 +02001398
Andrey Andreev650b4c02012-06-11 12:07:15 +03001399 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001400 {
1401 $this->limit($limit, $offset);
1402 }
Barry Mienydd671972010-10-04 16:33:58 +02001403
Andrey Andreev24276a32012-01-08 02:44:38 +02001404 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001405 $this->_reset_select();
1406 return $result;
1407 }
1408
1409 // --------------------------------------------------------------------
1410
1411 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001412 * Insert_Batch
1413 *
1414 * Compiles batch insert strings and runs the queries
1415 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001416 * @param string $table Table to insert into
1417 * @param array $set An associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001418 * @param bool $escape Whether to escape values and identifiers
Andrey Andreevae85eb42012-11-02 01:42:31 +02001419 * @return int Number of rows inserted or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001420 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001421 public function insert_batch($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001422 {
vlakoff1228fe22013-01-14 01:30:09 +01001423 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001424 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001425 $this->set_insert_batch($set, '', $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001426 }
Barry Mienydd671972010-10-04 16:33:58 +02001427
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001428 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001429 {
Andrey Andreev9f808b02012-10-24 17:38:48 +03001430 // No valid data array. Folds in cases where keys and values did not match up
1431 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001432 }
1433
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001434 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001435 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001436 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001437 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001438 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001439 }
Barry Mienydd671972010-10-04 16:33:58 +02001440
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001441 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001442 }
1443
1444 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001445 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001446 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001447 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001448 $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 +03001449 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001450 }
Barry Mienydd671972010-10-04 16:33:58 +02001451
Derek Jonesd10e8962010-03-02 17:10:36 -06001452 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001453 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001454 }
1455
1456 // --------------------------------------------------------------------
1457
1458 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +02001459 * Insert batch statement
Andrey Andreev97f36972012-04-05 12:44:36 +03001460 *
1461 * Generates a platform-specific insert string from the supplied data.
1462 *
Andrey Andreev083e3c82012-11-06 12:48:32 +02001463 * @param string $table Table name
1464 * @param array $keys INSERT keys
1465 * @param array $values INSERT values
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001466 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001467 */
1468 protected function _insert_batch($table, $keys, $values)
1469 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001470 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001471 }
1472
1473 // --------------------------------------------------------------------
1474
1475 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001476 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001477 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001478 * @param mixed
1479 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001480 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001481 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001482 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001483 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001484 {
1485 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001486
Derek Jonesd10e8962010-03-02 17:10:36 -06001487 if ( ! is_array($key))
1488 {
1489 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001490 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001491
Andrey Andreevfe642da2012-06-16 03:47:33 +03001492 is_bool($escape) OR $escape = $this->_protect_identifiers;
1493
Iban Eguia3c0a4522012-04-15 13:30:44 +02001494 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001495 sort($keys);
1496
1497 foreach ($key as $row)
1498 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001499 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001500 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1501 {
1502 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001503 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001504 return;
1505 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001506
Barry Mienydd671972010-10-04 16:33:58 +02001507 ksort($row); // puts $row in the same order as our keys
1508
Andrey Andreev650b4c02012-06-11 12:07:15 +03001509 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001510 {
1511 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001512 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001513 {
Barry Mienydd671972010-10-04 16:33:58 +02001514 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001515 }
1516
Andrey Andreev650b4c02012-06-11 12:07:15 +03001517 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001518 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001519
Andrey Andreev838a9d62012-12-03 14:37:47 +02001520 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001521 }
1522
1523 foreach ($keys as $k)
1524 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001525 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001526 }
Barry Mienydd671972010-10-04 16:33:58 +02001527
Derek Jonesd10e8962010-03-02 17:10:36 -06001528 return $this;
1529 }
WanWizard7219c072011-12-28 14:09:05 +01001530
Kyle Farris0c147b32011-08-26 02:29:31 -04001531 // --------------------------------------------------------------------
1532
1533 /**
1534 * Get INSERT query string
1535 *
1536 * Compiles an insert query and returns the sql
1537 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001538 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001539 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001540 * @return string
1541 */
1542 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001543 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001544 if ($this->_validate_insert($table) === FALSE)
1545 {
1546 return FALSE;
1547 }
WanWizard7219c072011-12-28 14:09:05 +01001548
Kyle Farris76116012011-08-31 11:17:48 -04001549 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001550 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001551 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001552 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001553 array_keys($this->qb_set),
1554 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001555 );
WanWizard7219c072011-12-28 14:09:05 +01001556
Kyle Farris0c147b32011-08-26 02:29:31 -04001557 if ($reset === TRUE)
1558 {
1559 $this->_reset_write();
1560 }
WanWizard7219c072011-12-28 14:09:05 +01001561
Kyle Farris0c147b32011-08-26 02:29:31 -04001562 return $sql;
1563 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001564
Derek Allard2067d1a2008-11-13 22:59:24 +00001565 // --------------------------------------------------------------------
1566
1567 /**
1568 * Insert
1569 *
1570 * Compiles an insert string and runs the query
1571 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001572 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001573 * @param array an associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001574 * @param bool $escape Whether to escape values and identifiers
Derek Allard2067d1a2008-11-13 22:59:24 +00001575 * @return object
1576 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001577 public function insert($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001578 {
vlakoff1228fe22013-01-14 01:30:09 +01001579 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001580 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001581 $this->set($set, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001582 }
WanWizard7219c072011-12-28 14:09:05 +01001583
Kyle Farris0c147b32011-08-26 02:29:31 -04001584 if ($this->_validate_insert($table) === FALSE)
1585 {
1586 return FALSE;
1587 }
WanWizard7219c072011-12-28 14:09:05 +01001588
Kyle Farris76116012011-08-31 11:17:48 -04001589 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001590 $this->protect_identifiers(
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001591 $this->qb_from[0], TRUE, $escape, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001592 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001593 array_keys($this->qb_set),
1594 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001595 );
Barry Mienydd671972010-10-04 16:33:58 +02001596
Kyle Farris0c147b32011-08-26 02:29:31 -04001597 $this->_reset_write();
1598 return $this->query($sql);
1599 }
WanWizard7219c072011-12-28 14:09:05 +01001600
Kyle Farris0c147b32011-08-26 02:29:31 -04001601 // --------------------------------------------------------------------
1602
1603 /**
1604 * Validate Insert
1605 *
1606 * This method is used by both insert() and get_compiled_insert() to
1607 * validate that the there data is actually being set and that table
1608 * has been chosen to be inserted into.
1609 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001610 * @param string the table to insert data into
1611 * @return string
1612 */
WanWizard7219c072011-12-28 14:09:05 +01001613 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001614 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001615 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001616 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001617 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001618 }
1619
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001620 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001621 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001622 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001623 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001624 elseif ( ! isset($this->qb_from[0]))
1625 {
1626 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1627 }
WanWizard7219c072011-12-28 14:09:05 +01001628
Kyle Farris0c147b32011-08-26 02:29:31 -04001629 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001630 }
Barry Mienydd671972010-10-04 16:33:58 +02001631
Phil Sturgeon9789f322011-07-15 15:14:05 -06001632 // --------------------------------------------------------------------
1633
1634 /**
1635 * Replace
1636 *
1637 * Compiles an replace into string and runs the query
1638 *
1639 * @param string the table to replace data into
1640 * @param array an associative array of insert values
1641 * @return object
1642 */
1643 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001644 {
vlakoff1228fe22013-01-14 01:30:09 +01001645 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001646 {
1647 $this->set($set);
1648 }
Barry Mienydd671972010-10-04 16:33:58 +02001649
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001650 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001651 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001652 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001653 }
1654
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001655 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001656 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001657 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001658 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001659 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001660 }
Barry Mienydd671972010-10-04 16:33:58 +02001661
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001662 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001663 }
1664
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001665 $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 +00001666
Derek Jonesd10e8962010-03-02 17:10:36 -06001667 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001668 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001669 }
WanWizard7219c072011-12-28 14:09:05 +01001670
Kyle Farris0c147b32011-08-26 02:29:31 -04001671 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001672
Kyle Farris0c147b32011-08-26 02:29:31 -04001673 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001674 * Replace statement
1675 *
1676 * Generates a platform-specific replace string from the supplied data
1677 *
1678 * @param string the table name
1679 * @param array the insert keys
1680 * @param array the insert values
1681 * @return string
1682 */
1683 protected function _replace($table, $keys, $values)
1684 {
1685 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1686 }
1687
1688 // --------------------------------------------------------------------
1689
1690 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001691 * FROM tables
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001692 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001693 * Groups tables in FROM clauses if needed, so there is no confusion
1694 * about operator precedence.
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001695 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001696 * Note: This is only used (and overriden) by MySQL and CUBRID.
1697 *
1698 * @return string
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001699 */
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001700 protected function _from_tables()
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001701 {
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001702 return implode(', ', $this->qb_from);
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001703 }
1704
1705 // --------------------------------------------------------------------
1706
1707 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001708 * Get UPDATE query string
1709 *
1710 * Compiles an update query and returns the sql
1711 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001712 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001713 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001714 * @return string
1715 */
1716 public function get_compiled_update($table = '', $reset = TRUE)
1717 {
1718 // Combine any cached components with the current statements
1719 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001720
Kyle Farris0c147b32011-08-26 02:29:31 -04001721 if ($this->_validate_update($table) === FALSE)
1722 {
1723 return FALSE;
1724 }
WanWizard7219c072011-12-28 14:09:05 +01001725
Andrey Andreevb0478652012-07-18 15:34:46 +03001726 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001727
Kyle Farris0c147b32011-08-26 02:29:31 -04001728 if ($reset === TRUE)
1729 {
1730 $this->_reset_write();
1731 }
WanWizard7219c072011-12-28 14:09:05 +01001732
Kyle Farris0c147b32011-08-26 02:29:31 -04001733 return $sql;
1734 }
WanWizard7219c072011-12-28 14:09:05 +01001735
Derek Allard2067d1a2008-11-13 22:59:24 +00001736 // --------------------------------------------------------------------
1737
1738 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001739 * UPDATE
Derek Allard2067d1a2008-11-13 22:59:24 +00001740 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001741 * Compiles an update string and runs the query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001743 * @param string $table
1744 * @param array $set An associative array of update values
1745 * @param mixed $where
1746 * @param int $limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001747 * @return object
1748 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001749 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001750 {
1751 // Combine any cached components with the current statements
1752 $this->_merge_cache();
1753
vlakoff1228fe22013-01-14 01:30:09 +01001754 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001755 {
1756 $this->set($set);
1757 }
Barry Mienydd671972010-10-04 16:33:58 +02001758
Kyle Farris0c147b32011-08-26 02:29:31 -04001759 if ($this->_validate_update($table) === FALSE)
1760 {
1761 return FALSE;
1762 }
1763
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001764 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001765 {
1766 $this->where($where);
1767 }
1768
Andrey Andreev650b4c02012-06-11 12:07:15 +03001769 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001770 {
1771 $this->limit($limit);
1772 }
1773
Andrey Andreevb0478652012-07-18 15:34:46 +03001774 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
Kyle Farris0c147b32011-08-26 02:29:31 -04001775 $this->_reset_write();
1776 return $this->query($sql);
1777 }
WanWizard7219c072011-12-28 14:09:05 +01001778
Kyle Farris0c147b32011-08-26 02:29:31 -04001779 // --------------------------------------------------------------------
1780
1781 /**
1782 * Validate Update
1783 *
1784 * This method is used by both update() and get_compiled_update() to
1785 * validate that data is actually being set and that a table has been
1786 * chosen to be update.
1787 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001788 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001789 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001790 */
1791 protected function _validate_update($table = '')
1792 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001793 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001794 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001795 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001796 }
1797
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001798 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001799 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001800 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001801 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001802 elseif ( ! isset($this->qb_from[0]))
1803 {
1804 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1805 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001806
1807 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001808 }
WanWizard7219c072011-12-28 14:09:05 +01001809
Derek Jonesd10e8962010-03-02 17:10:36 -06001810 // --------------------------------------------------------------------
1811
1812 /**
1813 * Update_Batch
1814 *
1815 * Compiles an update string and runs the query
1816 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001817 * @param string the table to retrieve the results from
1818 * @param array an associative array of update values
1819 * @param string the where key
Andrey Andreev9f808b02012-10-24 17:38:48 +03001820 * @return int number of rows affected or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001821 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001822 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001823 {
1824 // Combine any cached components with the current statements
1825 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001826
vlakoff1228fe22013-01-14 01:30:09 +01001827 if ($index === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001828 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001829 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001830 }
1831
vlakoff1228fe22013-01-14 01:30:09 +01001832 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001833 {
1834 $this->set_update_batch($set, $index);
1835 }
1836
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001837 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001838 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001839 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001840 }
1841
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001842 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001843 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001844 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001845 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001846 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001847 }
Barry Mienydd671972010-10-04 16:33:58 +02001848
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001849 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001850 }
Barry Mienydd671972010-10-04 16:33:58 +02001851
Derek Jonesd10e8962010-03-02 17:10:36 -06001852 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001853 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001854 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001855 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001856 $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 +03001857 $affected_rows += $this->affected_rows();
Andrey Andreev79f888b2013-08-06 13:59:23 +03001858 $this->qb_where = array();
Derek Jonesd10e8962010-03-02 17:10:36 -06001859 }
Barry Mienydd671972010-10-04 16:33:58 +02001860
Derek Jonesd10e8962010-03-02 17:10:36 -06001861 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001862 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001863 }
1864
1865 // --------------------------------------------------------------------
1866
1867 /**
Andrey Andreev219565d2013-03-12 20:00:08 +02001868 * Update_Batch statement
1869 *
1870 * Generates a platform-specific batch update string from the supplied data
1871 *
1872 * @param string $table Table name
1873 * @param array $values Update data
1874 * @param string $index WHERE key
1875 * @return string
1876 */
1877 protected function _update_batch($table, $values, $index)
1878 {
1879 $ids = array();
1880 foreach ($values as $key => $val)
1881 {
1882 $ids[] = $val[$index];
1883
1884 foreach (array_keys($val) as $field)
1885 {
1886 if ($field !== $index)
1887 {
1888 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
1889 }
1890 }
1891 }
1892
1893 $cases = '';
1894 foreach ($final as $k => $v)
1895 {
1896 $cases .= $k." = CASE \n"
1897 .implode("\n", $v)."\n"
1898 .'ELSE '.$k.' END, ';
1899 }
1900
1901 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
1902
1903 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
1904 }
1905
1906 // --------------------------------------------------------------------
1907
1908 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001909 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001910 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001911 * @param array
1912 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001913 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001914 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001915 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001916 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001917 {
1918 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001919
Derek Jonesd10e8962010-03-02 17:10:36 -06001920 if ( ! is_array($key))
1921 {
1922 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001923 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001924
Andrey Andreevfe642da2012-06-16 03:47:33 +03001925 is_bool($escape) OR $escape = $this->_protect_identifiers;
1926
Derek Jonesd10e8962010-03-02 17:10:36 -06001927 foreach ($key as $k => $v)
1928 {
1929 $index_set = FALSE;
1930 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001931 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001932 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001933 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001934 {
1935 $index_set = TRUE;
1936 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001937
Andrey Andreevfe642da2012-06-16 03:47:33 +03001938 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001939 }
1940
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001941 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001942 {
1943 return $this->display_error('db_batch_missing_index');
1944 }
1945
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001946 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001947 }
Barry Mienydd671972010-10-04 16:33:58 +02001948
Derek Jonesd10e8962010-03-02 17:10:36 -06001949 return $this;
1950 }
1951
Derek Allard2067d1a2008-11-13 22:59:24 +00001952 // --------------------------------------------------------------------
1953
1954 /**
1955 * Empty Table
1956 *
1957 * Compiles a delete string and runs "DELETE FROM table"
1958 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001959 * @param string the table to empty
1960 * @return object
1961 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001962 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001963 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001964 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001965 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001966 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001967 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001968 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001969 }
1970
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001971 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001972 }
1973 else
1974 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001975 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001976 }
1977
1978 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001979 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001980 return $this->query($sql);
1981 }
1982
1983 // --------------------------------------------------------------------
1984
1985 /**
1986 * Truncate
1987 *
1988 * Compiles a truncate string and runs the query
1989 * If the database does not support the truncate() command
1990 * This function maps to "DELETE FROM table"
1991 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001992 * @param string the table to truncate
1993 * @return object
1994 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001995 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001996 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001997 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001998 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001999 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002000 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002001 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002002 }
2003
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002004 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002005 }
2006 else
2007 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002008 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002009 }
2010
2011 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002012 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00002013 return $this->query($sql);
2014 }
WanWizard7219c072011-12-28 14:09:05 +01002015
Kyle Farris0c147b32011-08-26 02:29:31 -04002016 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02002017
Kyle Farris0c147b32011-08-26 02:29:31 -04002018 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03002019 * Truncate statement
2020 *
2021 * Generates a platform-specific truncate string from the supplied data
2022 *
2023 * If the database does not support the truncate() command,
2024 * then this method maps to 'DELETE FROM table'
2025 *
2026 * @param string the table name
2027 * @return string
2028 */
2029 protected function _truncate($table)
2030 {
2031 return 'TRUNCATE '.$table;
2032 }
2033
2034 // --------------------------------------------------------------------
2035
2036 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04002037 * Get DELETE query string
2038 *
2039 * Compiles a delete query string and returns the sql
2040 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002041 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002042 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04002043 * @return string
2044 */
2045 public function get_compiled_delete($table = '', $reset = TRUE)
2046 {
2047 $this->return_delete_sql = TRUE;
2048 $sql = $this->delete($table, '', NULL, $reset);
2049 $this->return_delete_sql = FALSE;
2050 return $sql;
2051 }
WanWizard7219c072011-12-28 14:09:05 +01002052
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 // --------------------------------------------------------------------
2054
2055 /**
2056 * Delete
2057 *
2058 * Compiles a delete string and runs the query
2059 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002060 * @param mixed the table(s) to delete from. String or array
2061 * @param mixed the where clause
2062 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03002063 * @param bool
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002064 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00002065 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002066 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002067 {
2068 // Combine any cached components with the current statements
2069 $this->_merge_cache();
2070
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002071 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002072 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002073 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002074 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002075 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002076 }
2077
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002078 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002079 }
2080 elseif (is_array($table))
2081 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05002082 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002083 {
Andrey Andreev13f50542012-10-12 12:31:02 +03002084 $this->delete($single_table, $where, $limit, $reset_data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002085 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002086 return;
2087 }
2088 else
2089 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002090 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002091 }
2092
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002093 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002094 {
2095 $this->where($where);
2096 }
2097
Andrey Andreev650b4c02012-06-11 12:07:15 +03002098 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002099 {
2100 $this->limit($limit);
2101 }
2102
Andrey Andreev94611df2012-07-19 12:29:54 +03002103 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002104 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002105 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02002106 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002107
Andrey Andreevb0478652012-07-18 15:34:46 +03002108 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002109 if ($reset_data)
2110 {
2111 $this->_reset_write();
2112 }
WanWizard7219c072011-12-28 14:09:05 +01002113
Andrey Andreev24276a32012-01-08 02:44:38 +02002114 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00002115 }
WanWizard7219c072011-12-28 14:09:05 +01002116
Derek Allard2067d1a2008-11-13 22:59:24 +00002117 // --------------------------------------------------------------------
2118
2119 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03002120 * Delete statement
2121 *
2122 * Generates a platform-specific delete string from the supplied data
2123 *
2124 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03002125 * @return string
2126 */
Andrey Andreevb0478652012-07-18 15:34:46 +03002127 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03002128 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002129 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002130 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03002131 }
2132
2133 // --------------------------------------------------------------------
2134
2135 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002136 * DB Prefix
2137 *
2138 * Prepends a database prefix if one exists in configuration
2139 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002140 * @param string the table
2141 * @return string
2142 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002143 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002144 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002145 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002146 {
2147 $this->display_error('db_table_name_required');
2148 }
2149
2150 return $this->dbprefix.$table;
2151 }
2152
2153 // --------------------------------------------------------------------
2154
2155 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06002156 * Set DB Prefix
2157 *
2158 * Set's the DB Prefix to something new without needing to reconnect
2159 *
2160 * @param string the prefix
2161 * @return string
2162 */
2163 public function set_dbprefix($prefix = '')
2164 {
2165 return $this->dbprefix = $prefix;
2166 }
2167
2168 // --------------------------------------------------------------------
2169
2170 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002171 * Track Aliases
2172 *
2173 * Used to track SQL statements written with aliased tables.
2174 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002175 * @param string The table to inspect
2176 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002177 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002178 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002179 {
2180 if (is_array($table))
2181 {
2182 foreach ($table as $t)
2183 {
2184 $this->_track_aliases($t);
2185 }
2186 return;
2187 }
Barry Mienydd671972010-10-04 16:33:58 +02002188
Derek Jones37f4b9c2011-07-01 17:56:50 -05002189 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002190 // the string into discreet statements
2191 if (strpos($table, ',') !== FALSE)
2192 {
2193 return $this->_track_aliases(explode(',', $table));
2194 }
Barry Mienydd671972010-10-04 16:33:58 +02002195
Derek Allard2067d1a2008-11-13 22:59:24 +00002196 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002197 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002198 {
2199 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002200 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002201
Derek Allard2067d1a2008-11-13 22:59:24 +00002202 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002203 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002204
Derek Allard2067d1a2008-11-13 22:59:24 +00002205 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002206 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002207 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002208 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002209 }
2210 }
2211 }
WanWizard7219c072011-12-28 14:09:05 +01002212
Derek Allard2067d1a2008-11-13 22:59:24 +00002213 // --------------------------------------------------------------------
2214
2215 /**
2216 * Compile the SELECT statement
2217 *
2218 * Generates a query string based on which functions were used.
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002219 * Should not be called directly.
Derek Allard2067d1a2008-11-13 22:59:24 +00002220 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002221 * @param bool $select_override
Derek Allard2067d1a2008-11-13 22:59:24 +00002222 * @return string
2223 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002224 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002225 {
2226 // Combine any cached components with the current statements
2227 $this->_merge_cache();
2228
Derek Allard2067d1a2008-11-13 22:59:24 +00002229 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002230 if ($select_override !== FALSE)
2231 {
2232 $sql = $select_override;
2233 }
2234 else
2235 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002236 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002237
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002238 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002239 {
Barry Mienydd671972010-10-04 16:33:58 +02002240 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002241 }
2242 else
Barry Mienydd671972010-10-04 16:33:58 +02002243 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002244 // Cycle through the "select" portion of the query and prep each column name.
2245 // The reason we protect identifiers here rather then in the select() function
2246 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002247 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002248 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002249 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002250 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002251 }
Barry Mienydd671972010-10-04 16:33:58 +02002252
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002253 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002254 }
2255 }
2256
Derek Allard2067d1a2008-11-13 22:59:24 +00002257 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002258 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002259 {
Andrey Andreeve78f8152012-10-09 11:38:38 +03002260 $sql .= "\nFROM ".$this->_from_tables();
Derek Allard2067d1a2008-11-13 22:59:24 +00002261 }
2262
Derek Allard2067d1a2008-11-13 22:59:24 +00002263 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002264 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002265 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002266 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002267 }
2268
Andrey Andreev2d486232012-07-19 14:46:51 +03002269 $sql .= $this->_compile_wh('qb_where')
2270 .$this->_compile_group_by()
2271 .$this->_compile_wh('qb_having')
2272 .$this->_compile_order_by(); // ORDER BY
Andrey Andreevb0478652012-07-18 15:34:46 +03002273
Andrey Andreevd40459d2012-07-18 16:46:39 +03002274 // LIMIT
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002275 if ($this->qb_limit)
Derek Allard2067d1a2008-11-13 22:59:24 +00002276 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002277 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002278 }
2279
2280 return $sql;
2281 }
2282
2283 // --------------------------------------------------------------------
2284
2285 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002286 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002287 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002288 * Escapes identifiers in WHERE and HAVING statements at execution time.
2289 *
Andrey Andreevb0478652012-07-18 15:34:46 +03002290 * Required so that aliases are tracked properly, regardless of wether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002291 * where(), or_where(), having(), or_having are called prior to from(),
2292 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002293 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002294 * @param string $qb_key 'qb_where' or 'qb_having'
Andrey Andreevd40459d2012-07-18 16:46:39 +03002295 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002296 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002297 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002298 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002299 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002300 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002301 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002302 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002303 // Is this condition already compiled?
2304 if (is_string($this->{$qb_key}[$i]))
2305 {
2306 continue;
2307 }
2308 elseif ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002309 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002310 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002311 continue;
2312 }
2313
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002314 // Split multiple conditions
2315 $conditions = preg_split(
2316 '/(\s*AND\s+|\s*OR\s+)/i',
2317 $this->{$qb_key}[$i]['condition'],
2318 -1,
2319 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
2320 );
2321
2322 for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002323 {
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002324 if (($op = $this->_get_operator($conditions[$ci])) === FALSE
Andrey Andreeve4742582012-10-25 13:25:13 +03002325 OR ! preg_match('/^(\(?)(.*)('.preg_quote($op, '/').')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches))
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002326 {
2327 continue;
2328 }
2329
2330 // $matches = array(
2331 // 0 => '(test <= foo)', /* the whole thing */
2332 // 1 => '(', /* optional */
2333 // 2 => 'test', /* the field name */
2334 // 3 => ' <= ', /* $op */
2335 // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2336 // 5 => ')' /* optional */
2337 // );
Andrey Andreev082aa402012-10-22 19:41:55 +03002338
2339 if ( ! empty($matches[4]))
2340 {
2341 $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4]));
2342 $matches[4] = ' '.$matches[4];
2343 }
2344
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002345 $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2]))
2346 .' '.trim($matches[3]).$matches[4].$matches[5];
Andrey Andreev6e704752012-07-18 00:46:33 +03002347 }
2348
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002349 $this->{$qb_key}[$i] = implode('', $conditions);
Andrey Andreev6e704752012-07-18 00:46:33 +03002350 }
2351
Andrey Andreev9d3aa1b2012-10-12 12:14:09 +03002352 return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ")
2353 .implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002354 }
2355
Andrey Andreevb0478652012-07-18 15:34:46 +03002356 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002357 }
2358
2359 // --------------------------------------------------------------------
2360
2361 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002362 * Compile GROUP BY
2363 *
2364 * Escapes identifiers in GROUP BY statements at execution time.
2365 *
2366 * Required so that aliases are tracked properly, regardless of wether
2367 * group_by() is called prior to from(), join() and dbprefix is added
2368 * only if needed.
2369 *
2370 * @return string SQL statement
2371 */
2372 protected function _compile_group_by()
2373 {
2374 if (count($this->qb_groupby) > 0)
2375 {
Andrey Andreev96feb582012-07-19 13:12:34 +03002376 for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
2377 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002378 // Is it already compiled?
Andrey Andreev18eba242014-01-23 22:52:31 +02002379 if (is_string($this->qb_groupby[$i]))
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002380 {
2381 continue;
2382 }
2383
Andrey Andreev082aa402012-10-22 19:41:55 +03002384 $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 +03002385 ? $this->qb_groupby[$i]['field']
Andrey Andreev13f50542012-10-12 12:31:02 +03002386 : $this->protect_identifiers($this->qb_groupby[$i]['field']);
Andrey Andreev96feb582012-07-19 13:12:34 +03002387 }
2388
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002389 return "\nGROUP BY ".implode(', ', $this->qb_groupby);
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002390 }
2391
2392 return '';
2393 }
2394
2395 // --------------------------------------------------------------------
2396
2397 /**
Andrey Andreev2d486232012-07-19 14:46:51 +03002398 * Compile ORDER BY
2399 *
2400 * Escapes identifiers in ORDER BY statements at execution time.
2401 *
2402 * Required so that aliases are tracked properly, regardless of wether
2403 * order_by() is called prior to from(), join() and dbprefix is added
2404 * only if needed.
2405 *
2406 * @return string SQL statement
2407 */
2408 protected function _compile_order_by()
2409 {
Andrey Andreeva53ea842012-10-23 12:44:09 +03002410 if (is_array($this->qb_orderby) && count($this->qb_orderby) > 0)
Andrey Andreev2d486232012-07-19 14:46:51 +03002411 {
Andrey Andreev2d486232012-07-19 14:46:51 +03002412 for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++)
2413 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002414 if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field']))
Andrey Andreev2d486232012-07-19 14:46:51 +03002415 {
Andrey Andreevfc043b32012-10-12 14:46:14 +03002416 $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']);
Andrey Andreev2d486232012-07-19 14:46:51 +03002417 }
2418
2419 $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction'];
2420 }
2421
Andrey Andreeva53ea842012-10-23 12:44:09 +03002422 return $this->qb_orderby = "\nORDER BY ".implode(', ', $this->qb_orderby);
2423 }
2424 elseif (is_string($this->qb_orderby))
2425 {
2426 return $this->qb_orderby;
Andrey Andreev2d486232012-07-19 14:46:51 +03002427 }
2428
2429 return '';
2430 }
2431
2432 // --------------------------------------------------------------------
2433
2434 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002435 * Object to Array
2436 *
2437 * Takes an object as input and converts the class variables to array key/vals
2438 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002439 * @param object
2440 * @return array
2441 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002442 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002443 {
2444 if ( ! is_object($object))
2445 {
2446 return $object;
2447 }
Barry Mienydd671972010-10-04 16:33:58 +02002448
Derek Allard2067d1a2008-11-13 22:59:24 +00002449 $array = array();
2450 foreach (get_object_vars($object) as $key => $val)
2451 {
2452 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002453 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002454 {
2455 $array[$key] = $val;
2456 }
2457 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002458
2459 return $array;
2460 }
Barry Mienydd671972010-10-04 16:33:58 +02002461
Derek Jonesd10e8962010-03-02 17:10:36 -06002462 // --------------------------------------------------------------------
2463
2464 /**
2465 * Object to Array
2466 *
2467 * Takes an object as input and converts the class variables to array key/vals
2468 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002469 * @param object
2470 * @return array
2471 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002472 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002473 {
2474 if ( ! is_object($object))
2475 {
2476 return $object;
2477 }
Barry Mienydd671972010-10-04 16:33:58 +02002478
Derek Jonesd10e8962010-03-02 17:10:36 -06002479 $array = array();
2480 $out = get_object_vars($object);
2481 $fields = array_keys($out);
2482
2483 foreach ($fields as $val)
2484 {
2485 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002486 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002487 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002488 $i = 0;
2489 foreach ($out[$val] as $data)
2490 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002491 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002492 }
2493 }
2494 }
2495
Derek Allard2067d1a2008-11-13 22:59:24 +00002496 return $array;
2497 }
Barry Mienydd671972010-10-04 16:33:58 +02002498
Derek Allard2067d1a2008-11-13 22:59:24 +00002499 // --------------------------------------------------------------------
2500
2501 /**
2502 * Start Cache
2503 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002504 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002505 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002506 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002507 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002508 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002509 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002510 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002511 }
2512
2513 // --------------------------------------------------------------------
2514
2515 /**
2516 * Stop Cache
2517 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002518 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002519 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002520 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002521 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002522 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002523 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002524 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002525 }
2526
2527 // --------------------------------------------------------------------
2528
2529 /**
2530 * Flush Cache
2531 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002532 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002533 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002534 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002535 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002536 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002537 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002538 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002539 'qb_cache_select' => array(),
2540 'qb_cache_from' => array(),
2541 'qb_cache_join' => array(),
2542 'qb_cache_where' => array(),
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002543 'qb_cache_groupby' => array(),
2544 'qb_cache_having' => array(),
2545 'qb_cache_orderby' => array(),
2546 'qb_cache_set' => array(),
2547 'qb_cache_exists' => array(),
2548 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002549 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002550 }
2551
2552 // --------------------------------------------------------------------
2553
2554 /**
2555 * Merge Cache
2556 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002557 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002558 * locally called ones.
2559 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002560 * @return void
2561 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002562 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002563 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002564 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002565 {
2566 return;
2567 }
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002568 elseif (in_array('select', $this->qb_cache_exists, TRUE))
2569 {
2570 $qb_no_escape = $this->qb_cache_no_escape;
2571 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002572
GDmac17a05282013-11-11 13:18:09 +01002573 foreach (array_unique($this->qb_cache_exists) as $val) // select, from, etc.
Derek Allard2067d1a2008-11-13 22:59:24 +00002574 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002575 $qb_variable = 'qb_'.$val;
2576 $qb_cache_var = 'qb_cache_'.$val;
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002577 $qb_new = $this->$qb_cache_var;
GDmace1b86832013-11-08 16:52:54 +01002578
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002579 for ($i = 0, $c = count($this->$qb_variable); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00002580 {
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002581 if ( ! in_array($this->{$qb_variable}[$i], $qb_new, TRUE))
2582 {
2583 $qb_new[] = $this->{$qb_variable}[$i];
2584 if ($val === 'select')
2585 {
2586 $qb_no_escape[] = $this->qb_no_escape[$i];
2587 }
2588 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002589 }
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002590
GDmace1b86832013-11-08 16:52:54 +01002591 $this->$qb_variable = $qb_new;
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002592 if ($val === 'select')
2593 {
2594 $this->qb_no_escape = $qb_no_escape;
2595 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002596 }
2597
2598 // If we are "protecting identifiers" we need to examine the "from"
2599 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002600 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002601 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002602 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002603 }
2604 }
WanWizard7219c072011-12-28 14:09:05 +01002605
Kyle Farris0c147b32011-08-26 02:29:31 -04002606 // --------------------------------------------------------------------
2607
2608 /**
Andrey Andreev082aa402012-10-22 19:41:55 +03002609 * Is literal
2610 *
2611 * Determines if a string represents a literal value or a field name
2612 *
Andrey Andreev02e4cd72012-11-13 11:50:47 +02002613 * @param string $str
Andrey Andreev082aa402012-10-22 19:41:55 +03002614 * @return bool
2615 */
2616 protected function _is_literal($str)
2617 {
2618 $str = trim($str);
2619
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002620 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 +03002621 {
2622 return TRUE;
2623 }
2624
2625 static $_str;
2626
2627 if (empty($_str))
2628 {
2629 $_str = ($this->_escape_char !== '"')
2630 ? array('"', "'") : array("'");
2631 }
2632
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002633 return in_array($str[0], $_str, TRUE);
Andrey Andreev082aa402012-10-22 19:41:55 +03002634 }
2635
2636 // --------------------------------------------------------------------
2637
2638 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002639 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002640 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002641 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002642 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002643 * @return void
2644 */
2645 public function reset_query()
2646 {
2647 $this->_reset_select();
2648 $this->_reset_write();
2649 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002650
2651 // --------------------------------------------------------------------
2652
2653 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002654 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002655 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002656 * @param array An array of fields to reset
2657 * @return void
2658 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002659 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002660 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002661 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002662 {
Andrey Andreevae85eb42012-11-02 01:42:31 +02002663 $this->$item = $default_value;
Derek Allard2067d1a2008-11-13 22:59:24 +00002664 }
2665 }
2666
2667 // --------------------------------------------------------------------
2668
2669 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002670 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002671 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002672 * @return void
2673 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002674 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002675 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002676 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002677 'qb_select' => array(),
2678 'qb_from' => array(),
2679 'qb_join' => array(),
2680 'qb_where' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002681 'qb_groupby' => array(),
2682 'qb_having' => array(),
2683 'qb_orderby' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002684 'qb_aliased_tables' => array(),
2685 'qb_no_escape' => array(),
2686 'qb_distinct' => FALSE,
2687 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002688 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002689 )
2690 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002691 }
Barry Mienydd671972010-10-04 16:33:58 +02002692
Derek Allard2067d1a2008-11-13 22:59:24 +00002693 // --------------------------------------------------------------------
2694
2695 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002696 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002697 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002698 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002699 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002700 * @return void
2701 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002702 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002703 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002704 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002705 'qb_set' => array(),
2706 'qb_from' => array(),
Andrey Andreev3e014372013-02-21 15:59:34 +02002707 'qb_join' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002708 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002709 'qb_orderby' => array(),
2710 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002711 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002712 )
2713 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002714 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002715
Derek Allard2067d1a2008-11-13 22:59:24 +00002716}
2717
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002718/* End of file DB_query_builder.php */
Rougin Royce Gutib191550a2014-08-24 16:19:08 +08002719/* Location: ./system/database/DB_query_builder.php */