blob: 4b3aa4d6c5fc5eaddcd948696d388d5aa416ca13 [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 {
Andrey Andreev2c6cdd72014-09-17 11:13:46 +0300666 $operator = $this->_get_operator($k);
Andrey Andreevf186e1f2014-09-18 12:05:59 +0300667 if (stripos($operator, 'NULL') === FALSE && strncasecmp(ltrim($operator), 'IN', 2) !== 0)
Ivan Tcholakov7e6aba12014-08-21 20:04:52 +0300668 {
Andrey Andreev2c6cdd72014-09-17 11:13:46 +0300669 $op = strrpos($k, $operator);
670 if (strlen($k) === ($op + strlen($operator)))
671 {
672 $operator = strtr($operator, array('<>' => 'IS NOT', '!=' => 'IS NOT'));
673 $k = substr($k, 0, $op).rtrim($operator).' NULL';
674 }
Ivan Tcholakov7e6aba12014-08-21 20:04:52 +0300675 }
676 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000677
Andrey Andreevd40459d2012-07-18 16:46:39 +0300678 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000679 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300681 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
682 $this->qb_cache_exists[] = substr($qb_key, 3);
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 }
Barry Mienydd671972010-10-04 16:33:58 +0200684
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 }
Barry Mienydd671972010-10-04 16:33:58 +0200686
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 return $this;
688 }
689
690 // --------------------------------------------------------------------
691
692 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200693 * WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200695 * Generates a WHERE field IN('item', 'item') SQL query,
696 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200698 * @param string $key The field to search
699 * @param array $values The values searched on
700 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500701 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300703 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300705 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 }
Barry Mienydd671972010-10-04 16:33:58 +0200707
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 // --------------------------------------------------------------------
709
710 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200711 * OR WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200713 * Generates a WHERE field IN('item', 'item') SQL query,
714 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200716 * @param string $key The field to search
717 * @param array $values The values searched on
718 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500719 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300721 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300723 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 }
725
726 // --------------------------------------------------------------------
727
728 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200729 * WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200731 * Generates a WHERE field NOT IN('item', 'item') SQL query,
732 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200734 * @param string $key The field to search
735 * @param array $values The values searched on
736 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500737 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300739 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300741 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 }
Barry Mienydd671972010-10-04 16:33:58 +0200743
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 // --------------------------------------------------------------------
745
746 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200747 * OR WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200749 * Generates a WHERE field NOT IN('item', 'item') SQL query,
750 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200752 * @param string $key The field to search
753 * @param array $values The values searched on
754 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500755 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300757 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300759 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 }
761
762 // --------------------------------------------------------------------
763
764 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200765 * Internal WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200767 * @used-by where_in()
768 * @used-by or_where_in()
769 * @used-by where_not_in()
770 * @used-by or_where_not_in()
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200772 * @param string $key The field to search
773 * @param array $values The values searched on
774 * @param bool $not If the statement would be IN or NOT IN
775 * @param string $type
776 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500777 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300779 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 {
781 if ($key === NULL OR $values === NULL)
782 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300783 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 }
Barry Mienydd671972010-10-04 16:33:58 +0200785
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 if ( ! is_array($values))
787 {
788 $values = array($values);
789 }
Barry Mienydd671972010-10-04 16:33:58 +0200790
Andrey Andreev498c1e02012-06-16 03:34:10 +0300791 is_bool($escape) OR $escape = $this->_protect_identifiers;
792
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 $not = ($not) ? ' NOT' : '';
794
Andrey Andreev94611df2012-07-19 12:29:54 +0300795 $where_in = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 foreach ($values as $value)
797 {
Andrey Andreevcc02db92012-10-12 14:30:10 +0300798 $where_in[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 }
800
WanWizardbc69f362012-06-22 00:10:11 +0200801 $prefix = (count($this->qb_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Andrey Andreev6e704752012-07-18 00:46:33 +0300802 $where_in = array(
Andrey Andreev94611df2012-07-19 12:29:54 +0300803 'condition' => $prefix.$key.$not.' IN('.implode(', ', $where_in).')',
Andrey Andreev6e704752012-07-18 00:46:33 +0300804 'escape' => $escape
805 );
Barry Mienydd671972010-10-04 16:33:58 +0200806
Andrey Andreev6e704752012-07-18 00:46:33 +0300807 $this->qb_where[] = $where_in;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000808 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000810 $this->qb_cache_where[] = $where_in;
811 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 }
813
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 return $this;
815 }
Barry Mienydd671972010-10-04 16:33:58 +0200816
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 // --------------------------------------------------------------------
818
819 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200820 * LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200822 * Generates a %LIKE% portion of the query.
823 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200825 * @param mixed $field
826 * @param string $match
827 * @param string $side
828 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500829 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300831 public function like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300833 return $this->_like($field, $match, 'AND ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 }
835
836 // --------------------------------------------------------------------
837
838 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200839 * NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200841 * Generates a NOT LIKE portion of the query.
842 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200844 * @param mixed $field
845 * @param string $match
846 * @param string $side
847 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500848 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300850 public function not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300852 return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 }
Barry Mienydd671972010-10-04 16:33:58 +0200854
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 // --------------------------------------------------------------------
856
857 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200858 * OR LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200860 * Generates a %LIKE% portion of the query.
861 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200863 * @param mixed $field
864 * @param string $match
865 * @param string $side
866 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500867 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300869 public function or_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300871 return $this->_like($field, $match, 'OR ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 }
873
874 // --------------------------------------------------------------------
875
876 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200877 * OR NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200879 * Generates a NOT LIKE portion of the query.
880 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200882 * @param mixed $field
883 * @param string $match
884 * @param string $side
885 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500886 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300888 public function or_not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300890 return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 }
Barry Mienydd671972010-10-04 16:33:58 +0200892
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 // --------------------------------------------------------------------
894
895 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200896 * Internal LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200898 * @used-by like()
899 * @used-by or_like()
900 * @used-by not_like()
901 * @used-by or_not_like()
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200903 * @param mixed $field
904 * @param string $match
905 * @param string $type
906 * @param string $side
907 * @param string $not
908 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -0500909 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300911 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 {
913 if ( ! is_array($field))
914 {
915 $field = array($field => $match);
916 }
Barry Mienydd671972010-10-04 16:33:58 +0200917
Andrey Andreevb0478652012-07-18 15:34:46 +0300918 is_bool($escape) OR $escape = $this->_protect_identifiers;
Andrey Andreevb0478652012-07-18 15:34:46 +0300919
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 foreach ($field as $k => $v)
921 {
Andrey Andreev41738232012-11-30 00:13:17 +0200922 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
923 ? $this->_group_get_type('') : $this->_group_get_type($type);
924
Derek Jonese4ed5832009-02-20 21:44:59 +0000925 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300926
Andrey Andreev24276a32012-01-08 02:44:38 +0200927 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400928 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300929 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400930 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200931 elseif ($side === 'before')
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 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200935 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300937 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 }
939 else
940 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300941 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600943
Derek Jonese4ed5832009-02-20 21:44:59 +0000944 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100945 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000946 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300947 $like_statement .= sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000948 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600949
Andrey Andreevb0478652012-07-18 15:34:46 +0300950 $this->qb_where[] = array('condition' => $like_statement, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000951 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 {
Andrey Andreev55bbd722013-01-28 19:02:13 +0200953 $this->qb_cache_where[] = array('condition' => $like_statement, 'escape' => $escape);
Andrey Andreevededc4a2012-07-18 01:16:15 +0300954 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200957
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 return $this;
959 }
Barry Mienydd671972010-10-04 16:33:58 +0200960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 // --------------------------------------------------------------------
962
963 /**
WanWizard7219c072011-12-28 14:09:05 +0100964 * Starts a query group.
965 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200966 * @param string $not (Internal use only)
967 * @param string $type (Internal use only)
Andrew Podner4296a652012-12-17 07:51:15 -0500968 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +0100969 */
970 public function group_start($not = '', $type = 'AND ')
971 {
972 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100973
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000974 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100975 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Andrey Andreev6e704752012-07-18 00:46:33 +0300976 $where = array(
977 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (',
978 'escape' => FALSE
979 );
WanWizard7219c072011-12-28 14:09:05 +0100980
Andrey Andreev6e704752012-07-18 00:46:33 +0300981 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000982 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100983 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300984 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +0100985 }
986
987 return $this;
988 }
989
990 // --------------------------------------------------------------------
991
992 /**
993 * Starts a query group, but ORs the group
994 *
Andrew Podner4296a652012-12-17 07:51:15 -0500995 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +0100996 */
997 public function or_group_start()
998 {
999 return $this->group_start('', 'OR ');
1000 }
1001
1002 // --------------------------------------------------------------------
1003
1004 /**
1005 * Starts a query group, but NOTs the group
1006 *
Andrew Podner4296a652012-12-17 07:51:15 -05001007 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001008 */
1009 public function not_group_start()
1010 {
1011 return $this->group_start('NOT ', 'AND ');
1012 }
1013
1014 // --------------------------------------------------------------------
1015
1016 /**
1017 * Starts a query group, but OR NOTs the group
1018 *
Andrew Podner4296a652012-12-17 07:51:15 -05001019 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001020 */
1021 public function or_not_group_start()
1022 {
1023 return $this->group_start('NOT ', 'OR ');
1024 }
1025
1026 // --------------------------------------------------------------------
1027
1028 /**
1029 * Ends a query group
1030 *
Andrew Podner4296a652012-12-17 07:51:15 -05001031 * @return CI_DB_query_builder
WanWizard7219c072011-12-28 14:09:05 +01001032 */
1033 public function group_end()
1034 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001035 $this->qb_where_group_started = FALSE;
Andrey Andreev6e704752012-07-18 00:46:33 +03001036 $where = array(
1037 'condition' => str_repeat(' ', $this->qb_where_group_count--).')',
1038 'escape' => FALSE
1039 );
WanWizard7219c072011-12-28 14:09:05 +01001040
Andrey Andreev6e704752012-07-18 00:46:33 +03001041 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001042 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +01001043 {
Andrey Andreev6e704752012-07-18 00:46:33 +03001044 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +01001045 }
1046
WanWizard7219c072011-12-28 14:09:05 +01001047 return $this;
1048 }
1049
1050 // --------------------------------------------------------------------
1051
1052 /**
1053 * Group_get_type
1054 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001055 * @used-by group_start()
1056 * @used-by _like()
1057 * @used-by _wh()
1058 * @used-by _where_in()
WanWizard7219c072011-12-28 14:09:05 +01001059 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001060 * @param string $type
WanWizard7219c072011-12-28 14:09:05 +01001061 * @return string
1062 */
1063 protected function _group_get_type($type)
1064 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001065 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +01001066 {
1067 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001068 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +01001069 }
1070
1071 return $type;
1072 }
1073
1074 // --------------------------------------------------------------------
1075
1076 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 * GROUP BY
1078 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001079 * @param string $by
1080 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -05001081 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001083 public function group_by($by, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001084 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001085 is_bool($escape) OR $escape = $this->_protect_identifiers;
1086
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 if (is_string($by))
1088 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001089 $by = ($escape === TRUE)
1090 ? explode(',', $by)
1091 : array($by);
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 }
Barry Mienydd671972010-10-04 16:33:58 +02001093
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 foreach ($by as $val)
1095 {
1096 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +02001097
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001098 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001100 $val = array('field' => $val, 'escape' => $escape);
Barry Mienydd671972010-10-04 16:33:58 +02001101
Andrey Andreev96feb582012-07-19 13:12:34 +03001102 $this->qb_groupby[] = $val;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001103 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001105 $this->qb_cache_groupby[] = $val;
1106 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 }
1108 }
1109 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001110
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 return $this;
1112 }
1113
1114 // --------------------------------------------------------------------
1115
1116 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001117 * HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001119 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001121 * @param string $key
1122 * @param string $value
1123 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 * @return object
1125 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001126 public function having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001128 return $this->_wh('qb_having', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 }
Barry Mienydd671972010-10-04 16:33:58 +02001130
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 // --------------------------------------------------------------------
1132
1133 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001134 * OR HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001136 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001138 * @param string $key
1139 * @param string $value
1140 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 * @return object
1142 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001143 public function or_having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001145 return $this->_wh('qb_having', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 }
Barry Mienydd671972010-10-04 16:33:58 +02001147
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 // --------------------------------------------------------------------
1149
1150 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001151 * ORDER BY
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001153 * @param string $orderby
vlakoff9f8e2992013-09-08 14:05:04 +02001154 * @param string $direction ASC, DESC or RANDOM
Andrey Andreevae85eb42012-11-02 01:42:31 +02001155 * @param bool $escape
Andrew Podner4296a652012-12-17 07:51:15 -05001156 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 */
Andrey Andreevd24160c2012-06-16 03:21:20 +03001158 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001160 $direction = strtoupper(trim($direction));
Andrey Andreev2d486232012-07-19 14:46:51 +03001161
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001162 if ($direction === 'RANDOM')
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001164 $direction = '';
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001165
1166 // Do we have a seed value?
1167 $orderby = ctype_digit((string) $orderby)
vlakoffe6c4d5b2013-09-08 13:54:57 +02001168 ? sprintf($this->_random_keyword[1], $orderby)
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001169 : $this->_random_keyword[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 }
Andrey Andreev2d486232012-07-19 14:46:51 +03001171 elseif (empty($orderby))
Derek Allard2067d1a2008-11-13 22:59:24 +00001172 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001173 return $this;
1174 }
1175 elseif ($direction !== '')
1176 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001177 $direction = in_array($direction, array('ASC', 'DESC'), TRUE) ? ' '.$direction : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 }
Barry Mienydd671972010-10-04 16:33:58 +02001179
Andrey Andreevd24160c2012-06-16 03:21:20 +03001180 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +02001181
Andrey Andreev2d486232012-07-19 14:46:51 +03001182 if ($escape === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 {
Andrey Andreev93dd2f22012-10-24 10:09:18 +03001184 $qb_orderby[] = array('field' => $orderby, 'direction' => $direction, 'escape' => FALSE);
Andrey Andreev2d486232012-07-19 14:46:51 +03001185 }
1186 else
1187 {
1188 $qb_orderby = array();
1189 foreach (explode(',', $orderby) as $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001190 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001191 $qb_orderby[] = ($direction === '' && preg_match('/\s+(ASC|DESC)$/i', rtrim($field), $match, PREG_OFFSET_CAPTURE))
1192 ? array('field' => ltrim(substr($field, 0, $match[0][1])), 'direction' => ' '.$match[1][0], 'escape' => TRUE)
1193 : array('field' => trim($field), 'direction' => $direction, 'escape' => TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 }
Barry Mienydd671972010-10-04 16:33:58 +02001196
Andrey Andreev2d486232012-07-19 14:46:51 +03001197 $this->qb_orderby = array_merge($this->qb_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001198 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001200 $this->qb_cache_orderby = array_merge($this->qb_cache_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001201 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 }
1203
1204 return $this;
1205 }
Barry Mienydd671972010-10-04 16:33:58 +02001206
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 // --------------------------------------------------------------------
1208
1209 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001210 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +00001211 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001212 * @param int $value LIMIT value
1213 * @param int $offset OFFSET value
Andrew Podner4296a652012-12-17 07:51:15 -05001214 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001215 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001216 public function limit($value, $offset = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 {
vlakoff912f1bc2013-01-15 03:34:12 +01001218 is_null($value) OR $this->qb_limit = (int) $value;
Andrey Andreev777153d2012-06-18 13:30:45 +03001219 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001220
Derek Allard2067d1a2008-11-13 22:59:24 +00001221 return $this;
1222 }
Barry Mienydd671972010-10-04 16:33:58 +02001223
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 // --------------------------------------------------------------------
1225
1226 /**
1227 * Sets the OFFSET value
1228 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001229 * @param int $offset OFFSET value
Andrew Podner4296a652012-12-17 07:51:15 -05001230 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001231 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001232 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001234 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001235 return $this;
1236 }
Barry Mienydd671972010-10-04 16:33:58 +02001237
Derek Allard2067d1a2008-11-13 22:59:24 +00001238 // --------------------------------------------------------------------
1239
1240 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001241 * LIMIT string
Andrey Andreev2c35b642012-06-24 03:05:26 +03001242 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001243 * Generates a platform-specific LIMIT clause.
Andrey Andreev2c35b642012-06-24 03:05:26 +03001244 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001245 * @param string $sql SQL Query
Andrey Andreev2c35b642012-06-24 03:05:26 +03001246 * @return string
1247 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001248 protected function _limit($sql)
Andrey Andreev2c35b642012-06-24 03:05:26 +03001249 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001250 return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').$this->qb_limit;
Andrey Andreev2c35b642012-06-24 03:05:26 +03001251 }
1252
1253 // --------------------------------------------------------------------
1254
1255 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001256 * The "set" function.
1257 *
1258 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 * @param mixed
1261 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001262 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001263 * @return CI_DB_query_builder
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001265 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001266 {
1267 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001268
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 if ( ! is_array($key))
1270 {
1271 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001272 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001273
Andrey Andreevfe642da2012-06-16 03:47:33 +03001274 is_bool($escape) OR $escape = $this->_protect_identifiers;
1275
Derek Allard2067d1a2008-11-13 22:59:24 +00001276 foreach ($key as $k => $v)
1277 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001278 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1279 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001280 }
Barry Mienydd671972010-10-04 16:33:58 +02001281
Derek Allard2067d1a2008-11-13 22:59:24 +00001282 return $this;
1283 }
WanWizard7219c072011-12-28 14:09:05 +01001284
Kyle Farris0c147b32011-08-26 02:29:31 -04001285 // --------------------------------------------------------------------
1286
1287 /**
1288 * Get SELECT query string
1289 *
1290 * Compiles a SELECT query string and returns the sql.
1291 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001292 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001293 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001294 * @return string
1295 */
WanWizard7219c072011-12-28 14:09:05 +01001296 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001297 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001298 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001299 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001300 $this->_track_aliases($table);
1301 $this->from($table);
1302 }
WanWizard7219c072011-12-28 14:09:05 +01001303
Andrey Andreev650b4c02012-06-11 12:07:15 +03001304 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001305
Kyle Farris0c147b32011-08-26 02:29:31 -04001306 if ($reset === TRUE)
1307 {
1308 $this->_reset_select();
1309 }
WanWizard7219c072011-12-28 14:09:05 +01001310
Kyle Farris0c147b32011-08-26 02:29:31 -04001311 return $select;
1312 }
WanWizard7219c072011-12-28 14:09:05 +01001313
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 // --------------------------------------------------------------------
1315
1316 /**
1317 * Get
1318 *
1319 * Compiles the select statement based on the other functions called
1320 * and runs the query
1321 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001322 * @param string the table
1323 * @param string the limit clause
1324 * @param string the offset clause
1325 * @return object
1326 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001327 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001328 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001329 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001330 {
1331 $this->_track_aliases($table);
1332 $this->from($table);
1333 }
Barry Mienydd671972010-10-04 16:33:58 +02001334
Andrey Andreev650b4c02012-06-11 12:07:15 +03001335 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001336 {
1337 $this->limit($limit, $offset);
1338 }
Barry Mienydd671972010-10-04 16:33:58 +02001339
Andrey Andreev24276a32012-01-08 02:44:38 +02001340 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 $this->_reset_select();
1342 return $result;
1343 }
1344
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001345 // --------------------------------------------------------------------
1346
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 /**
1348 * "Count All Results" query
1349 *
Barry Mienydd671972010-10-04 16:33:58 +02001350 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001351 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001352 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 * @param string
vlakoffc6ac7482013-11-17 00:50:06 +01001354 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +00001355 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001356 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001357 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001358 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 {
1360 $this->_track_aliases($table);
1361 $this->from($table);
1362 }
Barry Mienydd671972010-10-04 16:33:58 +02001363
Andrey Andreevb05f5062012-10-26 12:01:02 +03001364 $result = ($this->qb_distinct === TRUE)
1365 ? $this->query($this->_count_string.$this->protect_identifiers('numrows')."\nFROM (\n".$this->_compile_select()."\n) CI_count_all_results")
1366 : $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001368
Purwandi1d160e72012-01-09 16:33:28 +07001369 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001370 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001371 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001372 }
1373
Purwandi1d160e72012-01-09 16:33:28 +07001374 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001375 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001377
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 // --------------------------------------------------------------------
1379
1380 /**
1381 * Get_Where
1382 *
1383 * Allows the where clause, limit and offset to be added directly
1384 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001385 * @param string $table
1386 * @param string $where
1387 * @param int $limit
1388 * @param int $offset
Derek Allard2067d1a2008-11-13 22:59:24 +00001389 * @return object
1390 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001391 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001392 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001393 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 {
1395 $this->from($table);
1396 }
1397
vlakoff1228fe22013-01-14 01:30:09 +01001398 if ($where !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001399 {
1400 $this->where($where);
1401 }
Barry Mienydd671972010-10-04 16:33:58 +02001402
Andrey Andreev650b4c02012-06-11 12:07:15 +03001403 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001404 {
1405 $this->limit($limit, $offset);
1406 }
Barry Mienydd671972010-10-04 16:33:58 +02001407
Andrey Andreev24276a32012-01-08 02:44:38 +02001408 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001409 $this->_reset_select();
1410 return $result;
1411 }
1412
1413 // --------------------------------------------------------------------
1414
1415 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001416 * Insert_Batch
1417 *
1418 * Compiles batch insert strings and runs the queries
1419 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001420 * @param string $table Table to insert into
1421 * @param array $set An associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001422 * @param bool $escape Whether to escape values and identifiers
Andrey Andreevae85eb42012-11-02 01:42:31 +02001423 * @return int Number of rows inserted or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001424 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001425 public function insert_batch($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001426 {
vlakoff1228fe22013-01-14 01:30:09 +01001427 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001428 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001429 $this->set_insert_batch($set, '', $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001430 }
Barry Mienydd671972010-10-04 16:33:58 +02001431
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001432 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001433 {
Andrey Andreev9f808b02012-10-24 17:38:48 +03001434 // No valid data array. Folds in cases where keys and values did not match up
1435 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001436 }
1437
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001438 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001439 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001440 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001441 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001442 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001443 }
Barry Mienydd671972010-10-04 16:33:58 +02001444
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001445 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001446 }
1447
1448 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001449 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001450 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001451 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001452 $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 +03001453 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001454 }
Barry Mienydd671972010-10-04 16:33:58 +02001455
Derek Jonesd10e8962010-03-02 17:10:36 -06001456 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001457 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001458 }
1459
1460 // --------------------------------------------------------------------
1461
1462 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +02001463 * Insert batch statement
Andrey Andreev97f36972012-04-05 12:44:36 +03001464 *
1465 * Generates a platform-specific insert string from the supplied data.
1466 *
Andrey Andreev083e3c82012-11-06 12:48:32 +02001467 * @param string $table Table name
1468 * @param array $keys INSERT keys
1469 * @param array $values INSERT values
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001470 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001471 */
1472 protected function _insert_batch($table, $keys, $values)
1473 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001474 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001475 }
1476
1477 // --------------------------------------------------------------------
1478
1479 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001480 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001481 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001482 * @param mixed
1483 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001484 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001485 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001486 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001487 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001488 {
1489 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001490
Derek Jonesd10e8962010-03-02 17:10:36 -06001491 if ( ! is_array($key))
1492 {
1493 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001494 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001495
Andrey Andreevfe642da2012-06-16 03:47:33 +03001496 is_bool($escape) OR $escape = $this->_protect_identifiers;
1497
Iban Eguia3c0a4522012-04-15 13:30:44 +02001498 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001499 sort($keys);
1500
1501 foreach ($key as $row)
1502 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001503 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001504 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1505 {
1506 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001507 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001508 return;
1509 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001510
Barry Mienydd671972010-10-04 16:33:58 +02001511 ksort($row); // puts $row in the same order as our keys
1512
Andrey Andreev650b4c02012-06-11 12:07:15 +03001513 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001514 {
1515 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001516 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001517 {
Barry Mienydd671972010-10-04 16:33:58 +02001518 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001519 }
1520
Andrey Andreev650b4c02012-06-11 12:07:15 +03001521 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001522 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001523
Andrey Andreev838a9d62012-12-03 14:37:47 +02001524 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001525 }
1526
1527 foreach ($keys as $k)
1528 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001529 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001530 }
Barry Mienydd671972010-10-04 16:33:58 +02001531
Derek Jonesd10e8962010-03-02 17:10:36 -06001532 return $this;
1533 }
WanWizard7219c072011-12-28 14:09:05 +01001534
Kyle Farris0c147b32011-08-26 02:29:31 -04001535 // --------------------------------------------------------------------
1536
1537 /**
1538 * Get INSERT query string
1539 *
1540 * Compiles an insert query and returns the sql
1541 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001542 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001543 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001544 * @return string
1545 */
1546 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001547 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001548 if ($this->_validate_insert($table) === FALSE)
1549 {
1550 return FALSE;
1551 }
WanWizard7219c072011-12-28 14:09:05 +01001552
Kyle Farris76116012011-08-31 11:17:48 -04001553 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001554 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001555 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001556 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001557 array_keys($this->qb_set),
1558 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001559 );
WanWizard7219c072011-12-28 14:09:05 +01001560
Kyle Farris0c147b32011-08-26 02:29:31 -04001561 if ($reset === TRUE)
1562 {
1563 $this->_reset_write();
1564 }
WanWizard7219c072011-12-28 14:09:05 +01001565
Kyle Farris0c147b32011-08-26 02:29:31 -04001566 return $sql;
1567 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001568
Derek Allard2067d1a2008-11-13 22:59:24 +00001569 // --------------------------------------------------------------------
1570
1571 /**
1572 * Insert
1573 *
1574 * Compiles an insert string and runs the query
1575 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001576 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001577 * @param array an associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001578 * @param bool $escape Whether to escape values and identifiers
Derek Allard2067d1a2008-11-13 22:59:24 +00001579 * @return object
1580 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001581 public function insert($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001582 {
vlakoff1228fe22013-01-14 01:30:09 +01001583 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001584 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001585 $this->set($set, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001586 }
WanWizard7219c072011-12-28 14:09:05 +01001587
Kyle Farris0c147b32011-08-26 02:29:31 -04001588 if ($this->_validate_insert($table) === FALSE)
1589 {
1590 return FALSE;
1591 }
WanWizard7219c072011-12-28 14:09:05 +01001592
Kyle Farris76116012011-08-31 11:17:48 -04001593 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001594 $this->protect_identifiers(
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001595 $this->qb_from[0], TRUE, $escape, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001596 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001597 array_keys($this->qb_set),
1598 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001599 );
Barry Mienydd671972010-10-04 16:33:58 +02001600
Kyle Farris0c147b32011-08-26 02:29:31 -04001601 $this->_reset_write();
1602 return $this->query($sql);
1603 }
WanWizard7219c072011-12-28 14:09:05 +01001604
Kyle Farris0c147b32011-08-26 02:29:31 -04001605 // --------------------------------------------------------------------
1606
1607 /**
1608 * Validate Insert
1609 *
1610 * This method is used by both insert() and get_compiled_insert() to
1611 * validate that the there data is actually being set and that table
1612 * has been chosen to be inserted into.
1613 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001614 * @param string the table to insert data into
1615 * @return string
1616 */
WanWizard7219c072011-12-28 14:09:05 +01001617 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001618 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001619 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001620 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001621 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001622 }
1623
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001624 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001625 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001626 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001627 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001628 elseif ( ! isset($this->qb_from[0]))
1629 {
1630 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1631 }
WanWizard7219c072011-12-28 14:09:05 +01001632
Kyle Farris0c147b32011-08-26 02:29:31 -04001633 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001634 }
Barry Mienydd671972010-10-04 16:33:58 +02001635
Phil Sturgeon9789f322011-07-15 15:14:05 -06001636 // --------------------------------------------------------------------
1637
1638 /**
1639 * Replace
1640 *
1641 * Compiles an replace into string and runs the query
1642 *
1643 * @param string the table to replace data into
1644 * @param array an associative array of insert values
1645 * @return object
1646 */
1647 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001648 {
vlakoff1228fe22013-01-14 01:30:09 +01001649 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001650 {
1651 $this->set($set);
1652 }
Barry Mienydd671972010-10-04 16:33:58 +02001653
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001654 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001655 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001656 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001657 }
1658
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001659 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001660 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001661 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001662 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001663 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001664 }
Barry Mienydd671972010-10-04 16:33:58 +02001665
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001666 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001667 }
1668
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001669 $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 +00001670
Derek Jonesd10e8962010-03-02 17:10:36 -06001671 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001672 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001673 }
WanWizard7219c072011-12-28 14:09:05 +01001674
Kyle Farris0c147b32011-08-26 02:29:31 -04001675 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001676
Kyle Farris0c147b32011-08-26 02:29:31 -04001677 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001678 * Replace statement
1679 *
1680 * Generates a platform-specific replace string from the supplied data
1681 *
1682 * @param string the table name
1683 * @param array the insert keys
1684 * @param array the insert values
1685 * @return string
1686 */
1687 protected function _replace($table, $keys, $values)
1688 {
1689 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1690 }
1691
1692 // --------------------------------------------------------------------
1693
1694 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001695 * FROM tables
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001696 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001697 * Groups tables in FROM clauses if needed, so there is no confusion
1698 * about operator precedence.
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001699 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001700 * Note: This is only used (and overriden) by MySQL and CUBRID.
1701 *
1702 * @return string
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001703 */
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001704 protected function _from_tables()
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001705 {
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001706 return implode(', ', $this->qb_from);
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001707 }
1708
1709 // --------------------------------------------------------------------
1710
1711 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001712 * Get UPDATE query string
1713 *
1714 * Compiles an update query and returns the sql
1715 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001716 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001717 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001718 * @return string
1719 */
1720 public function get_compiled_update($table = '', $reset = TRUE)
1721 {
1722 // Combine any cached components with the current statements
1723 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001724
Kyle Farris0c147b32011-08-26 02:29:31 -04001725 if ($this->_validate_update($table) === FALSE)
1726 {
1727 return FALSE;
1728 }
WanWizard7219c072011-12-28 14:09:05 +01001729
Andrey Andreevb0478652012-07-18 15:34:46 +03001730 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001731
Kyle Farris0c147b32011-08-26 02:29:31 -04001732 if ($reset === TRUE)
1733 {
1734 $this->_reset_write();
1735 }
WanWizard7219c072011-12-28 14:09:05 +01001736
Kyle Farris0c147b32011-08-26 02:29:31 -04001737 return $sql;
1738 }
WanWizard7219c072011-12-28 14:09:05 +01001739
Derek Allard2067d1a2008-11-13 22:59:24 +00001740 // --------------------------------------------------------------------
1741
1742 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001743 * UPDATE
Derek Allard2067d1a2008-11-13 22:59:24 +00001744 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001745 * Compiles an update string and runs the query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001746 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001747 * @param string $table
1748 * @param array $set An associative array of update values
1749 * @param mixed $where
1750 * @param int $limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001751 * @return object
1752 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001753 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001754 {
1755 // Combine any cached components with the current statements
1756 $this->_merge_cache();
1757
vlakoff1228fe22013-01-14 01:30:09 +01001758 if ($set !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001759 {
1760 $this->set($set);
1761 }
Barry Mienydd671972010-10-04 16:33:58 +02001762
Kyle Farris0c147b32011-08-26 02:29:31 -04001763 if ($this->_validate_update($table) === FALSE)
1764 {
1765 return FALSE;
1766 }
1767
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001768 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001769 {
1770 $this->where($where);
1771 }
1772
Andrey Andreev650b4c02012-06-11 12:07:15 +03001773 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001774 {
1775 $this->limit($limit);
1776 }
1777
Andrey Andreevb0478652012-07-18 15:34:46 +03001778 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
Kyle Farris0c147b32011-08-26 02:29:31 -04001779 $this->_reset_write();
1780 return $this->query($sql);
1781 }
WanWizard7219c072011-12-28 14:09:05 +01001782
Kyle Farris0c147b32011-08-26 02:29:31 -04001783 // --------------------------------------------------------------------
1784
1785 /**
1786 * Validate Update
1787 *
1788 * This method is used by both update() and get_compiled_update() to
1789 * validate that data is actually being set and that a table has been
1790 * chosen to be update.
1791 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001792 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001793 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001794 */
1795 protected function _validate_update($table = '')
1796 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001797 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001798 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001799 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001800 }
1801
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001802 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001803 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001804 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001805 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001806 elseif ( ! isset($this->qb_from[0]))
1807 {
1808 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1809 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001810
1811 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001812 }
WanWizard7219c072011-12-28 14:09:05 +01001813
Derek Jonesd10e8962010-03-02 17:10:36 -06001814 // --------------------------------------------------------------------
1815
1816 /**
1817 * Update_Batch
1818 *
1819 * Compiles an update string and runs the query
1820 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001821 * @param string the table to retrieve the results from
1822 * @param array an associative array of update values
1823 * @param string the where key
Andrey Andreev9f808b02012-10-24 17:38:48 +03001824 * @return int number of rows affected or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001825 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001826 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001827 {
1828 // Combine any cached components with the current statements
1829 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001830
vlakoff1228fe22013-01-14 01:30:09 +01001831 if ($index === NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001832 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001833 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001834 }
1835
vlakoff1228fe22013-01-14 01:30:09 +01001836 if ($set !== NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001837 {
1838 $this->set_update_batch($set, $index);
1839 }
1840
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001841 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001842 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001843 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001844 }
1845
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001846 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001847 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001848 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001849 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001850 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001851 }
Barry Mienydd671972010-10-04 16:33:58 +02001852
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001853 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001854 }
Barry Mienydd671972010-10-04 16:33:58 +02001855
Derek Jonesd10e8962010-03-02 17:10:36 -06001856 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001857 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001858 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001859 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001860 $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 +03001861 $affected_rows += $this->affected_rows();
Andrey Andreev79f888b2013-08-06 13:59:23 +03001862 $this->qb_where = array();
Derek Jonesd10e8962010-03-02 17:10:36 -06001863 }
Barry Mienydd671972010-10-04 16:33:58 +02001864
Derek Jonesd10e8962010-03-02 17:10:36 -06001865 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001866 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001867 }
1868
1869 // --------------------------------------------------------------------
1870
1871 /**
Andrey Andreev219565d2013-03-12 20:00:08 +02001872 * Update_Batch statement
1873 *
1874 * Generates a platform-specific batch update string from the supplied data
1875 *
1876 * @param string $table Table name
1877 * @param array $values Update data
1878 * @param string $index WHERE key
1879 * @return string
1880 */
1881 protected function _update_batch($table, $values, $index)
1882 {
1883 $ids = array();
1884 foreach ($values as $key => $val)
1885 {
1886 $ids[] = $val[$index];
1887
1888 foreach (array_keys($val) as $field)
1889 {
1890 if ($field !== $index)
1891 {
1892 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
1893 }
1894 }
1895 }
1896
1897 $cases = '';
1898 foreach ($final as $k => $v)
1899 {
1900 $cases .= $k." = CASE \n"
1901 .implode("\n", $v)."\n"
1902 .'ELSE '.$k.' END, ';
1903 }
1904
1905 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
1906
1907 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
1908 }
1909
1910 // --------------------------------------------------------------------
1911
1912 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001913 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001914 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001915 * @param array
1916 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001917 * @param bool
Andrew Podner4296a652012-12-17 07:51:15 -05001918 * @return CI_DB_query_builder
Derek Jonesd10e8962010-03-02 17:10:36 -06001919 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001920 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001921 {
1922 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001923
Derek Jonesd10e8962010-03-02 17:10:36 -06001924 if ( ! is_array($key))
1925 {
1926 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001927 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001928
Andrey Andreevfe642da2012-06-16 03:47:33 +03001929 is_bool($escape) OR $escape = $this->_protect_identifiers;
1930
Derek Jonesd10e8962010-03-02 17:10:36 -06001931 foreach ($key as $k => $v)
1932 {
1933 $index_set = FALSE;
1934 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001935 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001936 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001937 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001938 {
1939 $index_set = TRUE;
1940 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001941
Andrey Andreevfe642da2012-06-16 03:47:33 +03001942 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001943 }
1944
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001945 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001946 {
1947 return $this->display_error('db_batch_missing_index');
1948 }
1949
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001950 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001951 }
Barry Mienydd671972010-10-04 16:33:58 +02001952
Derek Jonesd10e8962010-03-02 17:10:36 -06001953 return $this;
1954 }
1955
Derek Allard2067d1a2008-11-13 22:59:24 +00001956 // --------------------------------------------------------------------
1957
1958 /**
1959 * Empty Table
1960 *
1961 * Compiles a delete string and runs "DELETE FROM table"
1962 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001963 * @param string the table to empty
1964 * @return object
1965 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001966 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001967 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001968 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001969 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001970 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001971 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001972 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001973 }
1974
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001975 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001976 }
1977 else
1978 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001979 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001980 }
1981
1982 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001983 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001984 return $this->query($sql);
1985 }
1986
1987 // --------------------------------------------------------------------
1988
1989 /**
1990 * Truncate
1991 *
1992 * Compiles a truncate string and runs the query
1993 * If the database does not support the truncate() command
1994 * This function maps to "DELETE FROM table"
1995 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001996 * @param string the table to truncate
1997 * @return object
1998 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001999 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002000 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002001 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002002 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002003 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002004 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002005 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002006 }
2007
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002008 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002009 }
2010 else
2011 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002012 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002013 }
2014
2015 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002016 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00002017 return $this->query($sql);
2018 }
WanWizard7219c072011-12-28 14:09:05 +01002019
Kyle Farris0c147b32011-08-26 02:29:31 -04002020 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02002021
Kyle Farris0c147b32011-08-26 02:29:31 -04002022 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03002023 * Truncate statement
2024 *
2025 * Generates a platform-specific truncate string from the supplied data
2026 *
2027 * If the database does not support the truncate() command,
2028 * then this method maps to 'DELETE FROM table'
2029 *
2030 * @param string the table name
2031 * @return string
2032 */
2033 protected function _truncate($table)
2034 {
2035 return 'TRUNCATE '.$table;
2036 }
2037
2038 // --------------------------------------------------------------------
2039
2040 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04002041 * Get DELETE query string
2042 *
2043 * Compiles a delete query string and returns the sql
2044 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002045 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002046 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04002047 * @return string
2048 */
2049 public function get_compiled_delete($table = '', $reset = TRUE)
2050 {
2051 $this->return_delete_sql = TRUE;
2052 $sql = $this->delete($table, '', NULL, $reset);
2053 $this->return_delete_sql = FALSE;
2054 return $sql;
2055 }
WanWizard7219c072011-12-28 14:09:05 +01002056
Derek Allard2067d1a2008-11-13 22:59:24 +00002057 // --------------------------------------------------------------------
2058
2059 /**
2060 * Delete
2061 *
2062 * Compiles a delete string and runs the query
2063 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002064 * @param mixed the table(s) to delete from. String or array
2065 * @param mixed the where clause
2066 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03002067 * @param bool
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002068 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00002069 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002070 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002071 {
2072 // Combine any cached components with the current statements
2073 $this->_merge_cache();
2074
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002075 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002076 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002077 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002078 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002079 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002080 }
2081
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002082 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002083 }
2084 elseif (is_array($table))
2085 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05002086 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002087 {
Andrey Andreev13f50542012-10-12 12:31:02 +03002088 $this->delete($single_table, $where, $limit, $reset_data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002089 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002090 return;
2091 }
2092 else
2093 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002094 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002095 }
2096
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002097 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002098 {
2099 $this->where($where);
2100 }
2101
Andrey Andreev650b4c02012-06-11 12:07:15 +03002102 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002103 {
2104 $this->limit($limit);
2105 }
2106
Andrey Andreev94611df2012-07-19 12:29:54 +03002107 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002108 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002109 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02002110 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002111
Andrey Andreevb0478652012-07-18 15:34:46 +03002112 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002113 if ($reset_data)
2114 {
2115 $this->_reset_write();
2116 }
WanWizard7219c072011-12-28 14:09:05 +01002117
Andrey Andreev24276a32012-01-08 02:44:38 +02002118 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00002119 }
WanWizard7219c072011-12-28 14:09:05 +01002120
Derek Allard2067d1a2008-11-13 22:59:24 +00002121 // --------------------------------------------------------------------
2122
2123 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03002124 * Delete statement
2125 *
2126 * Generates a platform-specific delete string from the supplied data
2127 *
2128 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03002129 * @return string
2130 */
Andrey Andreevb0478652012-07-18 15:34:46 +03002131 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03002132 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002133 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002134 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03002135 }
2136
2137 // --------------------------------------------------------------------
2138
2139 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002140 * DB Prefix
2141 *
2142 * Prepends a database prefix if one exists in configuration
2143 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002144 * @param string the table
2145 * @return string
2146 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002147 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002148 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002149 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002150 {
2151 $this->display_error('db_table_name_required');
2152 }
2153
2154 return $this->dbprefix.$table;
2155 }
2156
2157 // --------------------------------------------------------------------
2158
2159 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06002160 * Set DB Prefix
2161 *
2162 * Set's the DB Prefix to something new without needing to reconnect
2163 *
2164 * @param string the prefix
2165 * @return string
2166 */
2167 public function set_dbprefix($prefix = '')
2168 {
2169 return $this->dbprefix = $prefix;
2170 }
2171
2172 // --------------------------------------------------------------------
2173
2174 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002175 * Track Aliases
2176 *
2177 * Used to track SQL statements written with aliased tables.
2178 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002179 * @param string The table to inspect
2180 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002181 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002182 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002183 {
2184 if (is_array($table))
2185 {
2186 foreach ($table as $t)
2187 {
2188 $this->_track_aliases($t);
2189 }
2190 return;
2191 }
Barry Mienydd671972010-10-04 16:33:58 +02002192
Derek Jones37f4b9c2011-07-01 17:56:50 -05002193 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002194 // the string into discreet statements
2195 if (strpos($table, ',') !== FALSE)
2196 {
2197 return $this->_track_aliases(explode(',', $table));
2198 }
Barry Mienydd671972010-10-04 16:33:58 +02002199
Derek Allard2067d1a2008-11-13 22:59:24 +00002200 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002201 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002202 {
2203 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002204 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002205
Derek Allard2067d1a2008-11-13 22:59:24 +00002206 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002207 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002208
Derek Allard2067d1a2008-11-13 22:59:24 +00002209 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002210 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002211 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002212 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002213 }
2214 }
2215 }
WanWizard7219c072011-12-28 14:09:05 +01002216
Derek Allard2067d1a2008-11-13 22:59:24 +00002217 // --------------------------------------------------------------------
2218
2219 /**
2220 * Compile the SELECT statement
2221 *
2222 * Generates a query string based on which functions were used.
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002223 * Should not be called directly.
Derek Allard2067d1a2008-11-13 22:59:24 +00002224 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002225 * @param bool $select_override
Derek Allard2067d1a2008-11-13 22:59:24 +00002226 * @return string
2227 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002228 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002229 {
2230 // Combine any cached components with the current statements
2231 $this->_merge_cache();
2232
Derek Allard2067d1a2008-11-13 22:59:24 +00002233 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002234 if ($select_override !== FALSE)
2235 {
2236 $sql = $select_override;
2237 }
2238 else
2239 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002240 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002241
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002242 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002243 {
Barry Mienydd671972010-10-04 16:33:58 +02002244 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002245 }
2246 else
Barry Mienydd671972010-10-04 16:33:58 +02002247 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002248 // Cycle through the "select" portion of the query and prep each column name.
2249 // The reason we protect identifiers here rather then in the select() function
2250 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002251 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002252 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002253 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002254 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002255 }
Barry Mienydd671972010-10-04 16:33:58 +02002256
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002257 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002258 }
2259 }
2260
Derek Allard2067d1a2008-11-13 22:59:24 +00002261 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002262 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002263 {
Andrey Andreeve78f8152012-10-09 11:38:38 +03002264 $sql .= "\nFROM ".$this->_from_tables();
Derek Allard2067d1a2008-11-13 22:59:24 +00002265 }
2266
Derek Allard2067d1a2008-11-13 22:59:24 +00002267 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002268 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002269 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002270 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002271 }
2272
Andrey Andreev2d486232012-07-19 14:46:51 +03002273 $sql .= $this->_compile_wh('qb_where')
2274 .$this->_compile_group_by()
2275 .$this->_compile_wh('qb_having')
2276 .$this->_compile_order_by(); // ORDER BY
Andrey Andreevb0478652012-07-18 15:34:46 +03002277
Andrey Andreevd40459d2012-07-18 16:46:39 +03002278 // LIMIT
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002279 if ($this->qb_limit)
Derek Allard2067d1a2008-11-13 22:59:24 +00002280 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002281 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002282 }
2283
2284 return $sql;
2285 }
2286
2287 // --------------------------------------------------------------------
2288
2289 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002290 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002291 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002292 * Escapes identifiers in WHERE and HAVING statements at execution time.
2293 *
Andrey Andreevb0478652012-07-18 15:34:46 +03002294 * Required so that aliases are tracked properly, regardless of wether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002295 * where(), or_where(), having(), or_having are called prior to from(),
2296 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002297 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002298 * @param string $qb_key 'qb_where' or 'qb_having'
Andrey Andreevd40459d2012-07-18 16:46:39 +03002299 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002300 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002301 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002302 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002303 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002304 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002305 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002306 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002307 // Is this condition already compiled?
2308 if (is_string($this->{$qb_key}[$i]))
2309 {
2310 continue;
2311 }
2312 elseif ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002313 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002314 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002315 continue;
2316 }
2317
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002318 // Split multiple conditions
2319 $conditions = preg_split(
2320 '/(\s*AND\s+|\s*OR\s+)/i',
2321 $this->{$qb_key}[$i]['condition'],
2322 -1,
2323 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
2324 );
2325
2326 for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002327 {
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002328 if (($op = $this->_get_operator($conditions[$ci])) === FALSE
Andrey Andreeve4742582012-10-25 13:25:13 +03002329 OR ! preg_match('/^(\(?)(.*)('.preg_quote($op, '/').')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches))
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002330 {
2331 continue;
2332 }
2333
2334 // $matches = array(
2335 // 0 => '(test <= foo)', /* the whole thing */
2336 // 1 => '(', /* optional */
2337 // 2 => 'test', /* the field name */
2338 // 3 => ' <= ', /* $op */
2339 // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2340 // 5 => ')' /* optional */
2341 // );
Andrey Andreev082aa402012-10-22 19:41:55 +03002342
2343 if ( ! empty($matches[4]))
2344 {
2345 $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4]));
2346 $matches[4] = ' '.$matches[4];
2347 }
2348
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002349 $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2]))
2350 .' '.trim($matches[3]).$matches[4].$matches[5];
Andrey Andreev6e704752012-07-18 00:46:33 +03002351 }
2352
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002353 $this->{$qb_key}[$i] = implode('', $conditions);
Andrey Andreev6e704752012-07-18 00:46:33 +03002354 }
2355
Andrey Andreev9d3aa1b2012-10-12 12:14:09 +03002356 return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ")
2357 .implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002358 }
2359
Andrey Andreevb0478652012-07-18 15:34:46 +03002360 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002361 }
2362
2363 // --------------------------------------------------------------------
2364
2365 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002366 * Compile GROUP BY
2367 *
2368 * Escapes identifiers in GROUP BY statements at execution time.
2369 *
2370 * Required so that aliases are tracked properly, regardless of wether
2371 * group_by() is called prior to from(), join() and dbprefix is added
2372 * only if needed.
2373 *
2374 * @return string SQL statement
2375 */
2376 protected function _compile_group_by()
2377 {
2378 if (count($this->qb_groupby) > 0)
2379 {
Andrey Andreev96feb582012-07-19 13:12:34 +03002380 for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
2381 {
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002382 // Is it already compiled?
Andrey Andreev18eba242014-01-23 22:52:31 +02002383 if (is_string($this->qb_groupby[$i]))
Andrey Andreev5e3d48c2013-10-29 14:36:18 +02002384 {
2385 continue;
2386 }
2387
Andrey Andreev082aa402012-10-22 19:41:55 +03002388 $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 +03002389 ? $this->qb_groupby[$i]['field']
Andrey Andreev13f50542012-10-12 12:31:02 +03002390 : $this->protect_identifiers($this->qb_groupby[$i]['field']);
Andrey Andreev96feb582012-07-19 13:12:34 +03002391 }
2392
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002393 return "\nGROUP BY ".implode(', ', $this->qb_groupby);
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002394 }
2395
2396 return '';
2397 }
2398
2399 // --------------------------------------------------------------------
2400
2401 /**
Andrey Andreev2d486232012-07-19 14:46:51 +03002402 * Compile ORDER BY
2403 *
2404 * Escapes identifiers in ORDER BY statements at execution time.
2405 *
2406 * Required so that aliases are tracked properly, regardless of wether
2407 * order_by() is called prior to from(), join() and dbprefix is added
2408 * only if needed.
2409 *
2410 * @return string SQL statement
2411 */
2412 protected function _compile_order_by()
2413 {
Andrey Andreeva53ea842012-10-23 12:44:09 +03002414 if (is_array($this->qb_orderby) && count($this->qb_orderby) > 0)
Andrey Andreev2d486232012-07-19 14:46:51 +03002415 {
Andrey Andreev2d486232012-07-19 14:46:51 +03002416 for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++)
2417 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002418 if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field']))
Andrey Andreev2d486232012-07-19 14:46:51 +03002419 {
Andrey Andreevfc043b32012-10-12 14:46:14 +03002420 $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']);
Andrey Andreev2d486232012-07-19 14:46:51 +03002421 }
2422
2423 $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction'];
2424 }
2425
Andrey Andreeva53ea842012-10-23 12:44:09 +03002426 return $this->qb_orderby = "\nORDER BY ".implode(', ', $this->qb_orderby);
2427 }
2428 elseif (is_string($this->qb_orderby))
2429 {
2430 return $this->qb_orderby;
Andrey Andreev2d486232012-07-19 14:46:51 +03002431 }
2432
2433 return '';
2434 }
2435
2436 // --------------------------------------------------------------------
2437
2438 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002439 * Object to Array
2440 *
2441 * Takes an object as input and converts the class variables to array key/vals
2442 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002443 * @param object
2444 * @return array
2445 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002446 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002447 {
2448 if ( ! is_object($object))
2449 {
2450 return $object;
2451 }
Barry Mienydd671972010-10-04 16:33:58 +02002452
Derek Allard2067d1a2008-11-13 22:59:24 +00002453 $array = array();
2454 foreach (get_object_vars($object) as $key => $val)
2455 {
2456 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002457 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002458 {
2459 $array[$key] = $val;
2460 }
2461 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002462
2463 return $array;
2464 }
Barry Mienydd671972010-10-04 16:33:58 +02002465
Derek Jonesd10e8962010-03-02 17:10:36 -06002466 // --------------------------------------------------------------------
2467
2468 /**
2469 * Object to Array
2470 *
2471 * Takes an object as input and converts the class variables to array key/vals
2472 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002473 * @param object
2474 * @return array
2475 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002476 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002477 {
2478 if ( ! is_object($object))
2479 {
2480 return $object;
2481 }
Barry Mienydd671972010-10-04 16:33:58 +02002482
Derek Jonesd10e8962010-03-02 17:10:36 -06002483 $array = array();
2484 $out = get_object_vars($object);
2485 $fields = array_keys($out);
2486
2487 foreach ($fields as $val)
2488 {
2489 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002490 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002491 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002492 $i = 0;
2493 foreach ($out[$val] as $data)
2494 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002495 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002496 }
2497 }
2498 }
2499
Derek Allard2067d1a2008-11-13 22:59:24 +00002500 return $array;
2501 }
Barry Mienydd671972010-10-04 16:33:58 +02002502
Derek Allard2067d1a2008-11-13 22:59:24 +00002503 // --------------------------------------------------------------------
2504
2505 /**
2506 * Start Cache
2507 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002508 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002509 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002510 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002511 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002512 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002513 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002514 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002515 }
2516
2517 // --------------------------------------------------------------------
2518
2519 /**
2520 * Stop Cache
2521 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002522 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002523 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002524 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002525 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002526 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002527 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002528 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002529 }
2530
2531 // --------------------------------------------------------------------
2532
2533 /**
2534 * Flush Cache
2535 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002536 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002537 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002538 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002539 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002540 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002541 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002542 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002543 'qb_cache_select' => array(),
2544 'qb_cache_from' => array(),
2545 'qb_cache_join' => array(),
2546 'qb_cache_where' => array(),
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002547 'qb_cache_groupby' => array(),
2548 'qb_cache_having' => array(),
2549 'qb_cache_orderby' => array(),
2550 'qb_cache_set' => array(),
2551 'qb_cache_exists' => array(),
2552 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002553 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002554 }
2555
2556 // --------------------------------------------------------------------
2557
2558 /**
2559 * Merge Cache
2560 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002561 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002562 * locally called ones.
2563 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002564 * @return void
2565 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002566 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002567 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002568 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002569 {
2570 return;
2571 }
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002572 elseif (in_array('select', $this->qb_cache_exists, TRUE))
2573 {
2574 $qb_no_escape = $this->qb_cache_no_escape;
2575 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002576
GDmac17a05282013-11-11 13:18:09 +01002577 foreach (array_unique($this->qb_cache_exists) as $val) // select, from, etc.
Derek Allard2067d1a2008-11-13 22:59:24 +00002578 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002579 $qb_variable = 'qb_'.$val;
2580 $qb_cache_var = 'qb_cache_'.$val;
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002581 $qb_new = $this->$qb_cache_var;
GDmace1b86832013-11-08 16:52:54 +01002582
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002583 for ($i = 0, $c = count($this->$qb_variable); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00002584 {
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002585 if ( ! in_array($this->{$qb_variable}[$i], $qb_new, TRUE))
2586 {
2587 $qb_new[] = $this->{$qb_variable}[$i];
2588 if ($val === 'select')
2589 {
2590 $qb_no_escape[] = $this->qb_no_escape[$i];
2591 }
2592 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002593 }
Andrey Andreev35e3b0b2013-11-12 16:07:08 +02002594
GDmace1b86832013-11-08 16:52:54 +01002595 $this->$qb_variable = $qb_new;
Andrey Andreev1720a6a2014-01-06 13:50:05 +02002596 if ($val === 'select')
2597 {
2598 $this->qb_no_escape = $qb_no_escape;
2599 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002600 }
2601
2602 // If we are "protecting identifiers" we need to examine the "from"
2603 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002604 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002605 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002606 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002607 }
2608 }
WanWizard7219c072011-12-28 14:09:05 +01002609
Kyle Farris0c147b32011-08-26 02:29:31 -04002610 // --------------------------------------------------------------------
2611
2612 /**
Andrey Andreev082aa402012-10-22 19:41:55 +03002613 * Is literal
2614 *
2615 * Determines if a string represents a literal value or a field name
2616 *
Andrey Andreev02e4cd72012-11-13 11:50:47 +02002617 * @param string $str
Andrey Andreev082aa402012-10-22 19:41:55 +03002618 * @return bool
2619 */
2620 protected function _is_literal($str)
2621 {
2622 $str = trim($str);
2623
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002624 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 +03002625 {
2626 return TRUE;
2627 }
2628
2629 static $_str;
2630
2631 if (empty($_str))
2632 {
2633 $_str = ($this->_escape_char !== '"')
2634 ? array('"', "'") : array("'");
2635 }
2636
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002637 return in_array($str[0], $_str, TRUE);
Andrey Andreev082aa402012-10-22 19:41:55 +03002638 }
2639
2640 // --------------------------------------------------------------------
2641
2642 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002643 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002644 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002645 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002646 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002647 * @return void
2648 */
2649 public function reset_query()
2650 {
2651 $this->_reset_select();
2652 $this->_reset_write();
2653 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002654
2655 // --------------------------------------------------------------------
2656
2657 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002658 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002659 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002660 * @param array An array of fields to reset
2661 * @return void
2662 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002663 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002664 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002665 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002666 {
Andrey Andreevae85eb42012-11-02 01:42:31 +02002667 $this->$item = $default_value;
Derek Allard2067d1a2008-11-13 22:59:24 +00002668 }
2669 }
2670
2671 // --------------------------------------------------------------------
2672
2673 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002674 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002675 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002676 * @return void
2677 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002678 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002679 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002680 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002681 'qb_select' => array(),
2682 'qb_from' => array(),
2683 'qb_join' => array(),
2684 'qb_where' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002685 'qb_groupby' => array(),
2686 'qb_having' => array(),
2687 'qb_orderby' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002688 'qb_aliased_tables' => array(),
2689 'qb_no_escape' => array(),
2690 'qb_distinct' => FALSE,
2691 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002692 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002693 )
2694 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002695 }
Barry Mienydd671972010-10-04 16:33:58 +02002696
Derek Allard2067d1a2008-11-13 22:59:24 +00002697 // --------------------------------------------------------------------
2698
2699 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002700 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002701 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002702 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002703 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002704 * @return void
2705 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002706 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002707 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002708 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002709 'qb_set' => array(),
2710 'qb_from' => array(),
Andrey Andreev3e014372013-02-21 15:59:34 +02002711 'qb_join' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002712 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002713 'qb_orderby' => array(),
2714 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002715 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002716 )
2717 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002718 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002719
Derek Allard2067d1a2008-11-13 22:59:24 +00002720}
2721
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002722/* End of file DB_query_builder.php */
Rougin Gutiba7d32502014-08-27 10:52:49 +08002723/* Location: ./system/database/DB_query_builder.php */