blob: 9bd535b0e7979f711858f524711ffce1be0f80e5 [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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 * @return object
258 */
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
Andrey Andreev42870232012-06-12 01:30:20 +0300266 // If the escape value was not set will will base it on the global setting
267 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
299 * @return object
300 */
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
315 * @return object
316 */
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
331 * @return object
332 */
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
347 * @return object
348 */
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 * @return object
368 */
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 Andreev7b5eb732012-05-24 20:52:41 +0300388 $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->escape_identifiers(trim($alias));
389
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 * @return object
430 */
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 * @return object
446 */
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 * @return object
500 */
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 * @return object
588 */
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 * @return object
606 */
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 * @return object
628 */
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 // If the escape value was not set will 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
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 if ( ! is_null($v))
648 {
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 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000664
Andrey Andreevd40459d2012-07-18 16:46:39 +0300665 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000666 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300668 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
669 $this->qb_cache_exists[] = substr($qb_key, 3);
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 }
Barry Mienydd671972010-10-04 16:33:58 +0200671
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 }
Barry Mienydd671972010-10-04 16:33:58 +0200673
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 return $this;
675 }
676
677 // --------------------------------------------------------------------
678
679 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200680 * WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200682 * Generates a WHERE field IN('item', 'item') SQL query,
683 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200685 * @param string $key The field to search
686 * @param array $values The values searched on
687 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 * @return object
689 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300690 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300692 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 }
Barry Mienydd671972010-10-04 16:33:58 +0200694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 // --------------------------------------------------------------------
696
697 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200698 * OR WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200700 * Generates a WHERE field IN('item', 'item') SQL query,
701 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200703 * @param string $key The field to search
704 * @param array $values The values searched on
705 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 * @return object
707 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300708 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300710 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 }
712
713 // --------------------------------------------------------------------
714
715 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200716 * WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200718 * Generates a WHERE field NOT IN('item', 'item') SQL query,
719 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200721 * @param string $key The field to search
722 * @param array $values The values searched on
723 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 * @return object
725 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300726 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300728 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 }
Barry Mienydd671972010-10-04 16:33:58 +0200730
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 // --------------------------------------------------------------------
732
733 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200734 * OR WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200736 * Generates a WHERE field NOT IN('item', 'item') SQL query,
737 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200739 * @param string $key The field to search
740 * @param array $values The values searched on
741 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 * @return object
743 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300744 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300746 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 }
748
749 // --------------------------------------------------------------------
750
751 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200752 * Internal WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200754 * @used-by where_in()
755 * @used-by or_where_in()
756 * @used-by where_not_in()
757 * @used-by or_where_not_in()
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200759 * @param string $key The field to search
760 * @param array $values The values searched on
761 * @param bool $not If the statement would be IN or NOT IN
762 * @param string $type
763 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 * @return object
765 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300766 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 {
768 if ($key === NULL OR $values === NULL)
769 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300770 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 }
Barry Mienydd671972010-10-04 16:33:58 +0200772
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 if ( ! is_array($values))
774 {
775 $values = array($values);
776 }
Barry Mienydd671972010-10-04 16:33:58 +0200777
Andrey Andreev498c1e02012-06-16 03:34:10 +0300778 is_bool($escape) OR $escape = $this->_protect_identifiers;
779
Derek Allard2067d1a2008-11-13 22:59:24 +0000780 $not = ($not) ? ' NOT' : '';
781
Andrey Andreev94611df2012-07-19 12:29:54 +0300782 $where_in = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 foreach ($values as $value)
784 {
Andrey Andreevcc02db92012-10-12 14:30:10 +0300785 $where_in[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 }
787
WanWizardbc69f362012-06-22 00:10:11 +0200788 $prefix = (count($this->qb_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Andrey Andreev6e704752012-07-18 00:46:33 +0300789 $where_in = array(
Andrey Andreev94611df2012-07-19 12:29:54 +0300790 'condition' => $prefix.$key.$not.' IN('.implode(', ', $where_in).')',
Andrey Andreev6e704752012-07-18 00:46:33 +0300791 'escape' => $escape
792 );
Barry Mienydd671972010-10-04 16:33:58 +0200793
Andrey Andreev6e704752012-07-18 00:46:33 +0300794 $this->qb_where[] = $where_in;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000795 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000797 $this->qb_cache_where[] = $where_in;
798 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 }
800
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 return $this;
802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 // --------------------------------------------------------------------
805
806 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200807 * LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200809 * Generates a %LIKE% portion of the query.
810 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200812 * @param mixed $field
813 * @param string $match
814 * @param string $side
815 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 * @return object
817 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300818 public function like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300820 return $this->_like($field, $match, 'AND ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 }
822
823 // --------------------------------------------------------------------
824
825 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200826 * NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200828 * Generates a NOT LIKE portion of the query.
829 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200831 * @param mixed $field
832 * @param string $match
833 * @param string $side
834 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 * @return object
836 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300837 public function not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300839 return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 }
Barry Mienydd671972010-10-04 16:33:58 +0200841
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 // --------------------------------------------------------------------
843
844 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200845 * OR LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200847 * Generates a %LIKE% portion of the query.
848 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200850 * @param mixed $field
851 * @param string $match
852 * @param string $side
853 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 * @return object
855 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300856 public function or_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300858 return $this->_like($field, $match, 'OR ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 }
860
861 // --------------------------------------------------------------------
862
863 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200864 * OR NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200866 * Generates a NOT LIKE portion of the query.
867 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200869 * @param mixed $field
870 * @param string $match
871 * @param string $side
872 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 * @return object
874 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300875 public function or_not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300877 return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 }
Barry Mienydd671972010-10-04 16:33:58 +0200879
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 // --------------------------------------------------------------------
881
882 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200883 * Internal LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200885 * @used-by like()
886 * @used-by or_like()
887 * @used-by not_like()
888 * @used-by or_not_like()
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200890 * @param mixed $field
891 * @param string $match
892 * @param string $type
893 * @param string $side
894 * @param string $not
895 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 * @return object
897 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300898 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 {
900 if ( ! is_array($field))
901 {
902 $field = array($field => $match);
903 }
Barry Mienydd671972010-10-04 16:33:58 +0200904
Andrey Andreevb0478652012-07-18 15:34:46 +0300905 is_bool($escape) OR $escape = $this->_protect_identifiers;
906 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
907 ? $this->_group_get_type('') : $this->_group_get_type($type);
908
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 foreach ($field as $k => $v)
910 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000911 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300912
Andrey Andreev24276a32012-01-08 02:44:38 +0200913 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400914 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300915 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400916 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200917 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300919 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200921 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300923 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 }
925 else
926 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300927 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600929
Derek Jonese4ed5832009-02-20 21:44:59 +0000930 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100931 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000932 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300933 $like_statement .= sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000934 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600935
Andrey Andreevb0478652012-07-18 15:34:46 +0300936 $this->qb_where[] = array('condition' => $like_statement, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000937 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 {
Andrey Andreevededc4a2012-07-18 01:16:15 +0300939 $this->qb_cache_where[] = $like_statement;
940 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200943
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 return $this;
945 }
Barry Mienydd671972010-10-04 16:33:58 +0200946
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 // --------------------------------------------------------------------
948
949 /**
WanWizard7219c072011-12-28 14:09:05 +0100950 * Starts a query group.
951 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200952 * @param string $not (Internal use only)
953 * @param string $type (Internal use only)
WanWizard7219c072011-12-28 14:09:05 +0100954 * @return object
955 */
956 public function group_start($not = '', $type = 'AND ')
957 {
958 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100959
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000960 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100961 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Andrey Andreev6e704752012-07-18 00:46:33 +0300962 $where = array(
963 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (',
964 'escape' => FALSE
965 );
WanWizard7219c072011-12-28 14:09:05 +0100966
Andrey Andreev6e704752012-07-18 00:46:33 +0300967 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000968 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100969 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300970 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +0100971 }
972
973 return $this;
974 }
975
976 // --------------------------------------------------------------------
977
978 /**
979 * Starts a query group, but ORs the group
980 *
981 * @return object
982 */
983 public function or_group_start()
984 {
985 return $this->group_start('', 'OR ');
986 }
987
988 // --------------------------------------------------------------------
989
990 /**
991 * Starts a query group, but NOTs the group
992 *
993 * @return object
994 */
995 public function not_group_start()
996 {
997 return $this->group_start('NOT ', 'AND ');
998 }
999
1000 // --------------------------------------------------------------------
1001
1002 /**
1003 * Starts a query group, but OR NOTs the group
1004 *
1005 * @return object
1006 */
1007 public function or_not_group_start()
1008 {
1009 return $this->group_start('NOT ', 'OR ');
1010 }
1011
1012 // --------------------------------------------------------------------
1013
1014 /**
1015 * Ends a query group
1016 *
1017 * @return object
1018 */
1019 public function group_end()
1020 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001021 $this->qb_where_group_started = FALSE;
Andrey Andreev6e704752012-07-18 00:46:33 +03001022 $where = array(
1023 'condition' => str_repeat(' ', $this->qb_where_group_count--).')',
1024 'escape' => FALSE
1025 );
WanWizard7219c072011-12-28 14:09:05 +01001026
Andrey Andreev6e704752012-07-18 00:46:33 +03001027 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001028 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +01001029 {
Andrey Andreev6e704752012-07-18 00:46:33 +03001030 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +01001031 }
1032
WanWizard7219c072011-12-28 14:09:05 +01001033 return $this;
1034 }
1035
1036 // --------------------------------------------------------------------
1037
1038 /**
1039 * Group_get_type
1040 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001041 * @used-by group_start()
1042 * @used-by _like()
1043 * @used-by _wh()
1044 * @used-by _where_in()
WanWizard7219c072011-12-28 14:09:05 +01001045 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001046 * @param string $type
WanWizard7219c072011-12-28 14:09:05 +01001047 * @return string
1048 */
1049 protected function _group_get_type($type)
1050 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001051 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +01001052 {
1053 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001054 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +01001055 }
1056
1057 return $type;
1058 }
1059
1060 // --------------------------------------------------------------------
1061
1062 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 * GROUP BY
1064 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001065 * @param string $by
1066 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 * @return object
1068 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001069 public function group_by($by, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001071 is_bool($escape) OR $escape = $this->_protect_identifiers;
1072
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 if (is_string($by))
1074 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001075 $by = ($escape === TRUE)
1076 ? explode(',', $by)
1077 : array($by);
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 }
Barry Mienydd671972010-10-04 16:33:58 +02001079
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 foreach ($by as $val)
1081 {
1082 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +02001083
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001084 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001086 $val = array('field' => $val, 'escape' => $escape);
Barry Mienydd671972010-10-04 16:33:58 +02001087
Andrey Andreev96feb582012-07-19 13:12:34 +03001088 $this->qb_groupby[] = $val;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001089 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001091 $this->qb_cache_groupby[] = $val;
1092 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 }
1094 }
1095 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001096
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 return $this;
1098 }
1099
1100 // --------------------------------------------------------------------
1101
1102 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001103 * HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001104 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001105 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001107 * @param string $key
1108 * @param string $value
1109 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001110 * @return object
1111 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001112 public function having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001114 return $this->_wh('qb_having', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 }
Barry Mienydd671972010-10-04 16:33:58 +02001116
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 // --------------------------------------------------------------------
1118
1119 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001120 * OR HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001122 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001124 * @param string $key
1125 * @param string $value
1126 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 * @return object
1128 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001129 public function or_having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001131 return $this->_wh('qb_having', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 }
Barry Mienydd671972010-10-04 16:33:58 +02001133
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 // --------------------------------------------------------------------
1135
1136 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001137 * ORDER BY
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001139 * @param string $orderby
1140 * @param string $direction ASC or DESC
1141 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 * @return object
1143 */
Andrey Andreevd24160c2012-06-16 03:21:20 +03001144 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001146 $direction = strtoupper(trim($direction));
Andrey Andreev2d486232012-07-19 14:46:51 +03001147
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001148 if ($direction === 'RANDOM')
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001150 $direction = '';
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001151
1152 // Do we have a seed value?
1153 $orderby = ctype_digit((string) $orderby)
1154 ? $orderby = sprintf($this->_random_keyword[1], $orderby)
1155 : $this->_random_keyword[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 }
Andrey Andreev2d486232012-07-19 14:46:51 +03001157 elseif (empty($orderby))
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001159 return $this;
1160 }
1161 elseif ($direction !== '')
1162 {
Andrey Andreev98e46cf2012-11-13 03:01:42 +02001163 $direction = in_array($direction, array('ASC', 'DESC'), TRUE) ? ' '.$direction : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 }
Barry Mienydd671972010-10-04 16:33:58 +02001165
Andrey Andreevd24160c2012-06-16 03:21:20 +03001166 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +02001167
Andrey Andreev2d486232012-07-19 14:46:51 +03001168 if ($escape === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 {
Andrey Andreev93dd2f22012-10-24 10:09:18 +03001170 $qb_orderby[] = array('field' => $orderby, 'direction' => $direction, 'escape' => FALSE);
Andrey Andreev2d486232012-07-19 14:46:51 +03001171 }
1172 else
1173 {
1174 $qb_orderby = array();
1175 foreach (explode(',', $orderby) as $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001176 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001177 $qb_orderby[] = ($direction === '' && preg_match('/\s+(ASC|DESC)$/i', rtrim($field), $match, PREG_OFFSET_CAPTURE))
1178 ? array('field' => ltrim(substr($field, 0, $match[0][1])), 'direction' => ' '.$match[1][0], 'escape' => TRUE)
1179 : array('field' => trim($field), 'direction' => $direction, 'escape' => TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001181 }
Barry Mienydd671972010-10-04 16:33:58 +02001182
Andrey Andreev2d486232012-07-19 14:46:51 +03001183 $this->qb_orderby = array_merge($this->qb_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001184 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001185 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001186 $this->qb_cache_orderby = array_merge($this->qb_cache_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001187 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 }
1189
1190 return $this;
1191 }
Barry Mienydd671972010-10-04 16:33:58 +02001192
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 // --------------------------------------------------------------------
1194
1195 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001196 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001198 * @param int $value LIMIT value
1199 * @param int $offset OFFSET value
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 * @return object
1201 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001202 public function limit($value, $offset = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001203 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001204 is_null($value) OR $this->qb_limit = (int) $value;
1205 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001206
Derek Allard2067d1a2008-11-13 22:59:24 +00001207 return $this;
1208 }
Barry Mienydd671972010-10-04 16:33:58 +02001209
Derek Allard2067d1a2008-11-13 22:59:24 +00001210 // --------------------------------------------------------------------
1211
1212 /**
1213 * Sets the OFFSET value
1214 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001215 * @param int $offset OFFSET value
Derek Allard2067d1a2008-11-13 22:59:24 +00001216 * @return object
1217 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001218 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001220 empty($offset) OR $this->qb_offset = (int) $offset;
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 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001227 * LIMIT string
Andrey Andreev2c35b642012-06-24 03:05:26 +03001228 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001229 * Generates a platform-specific LIMIT clause.
Andrey Andreev2c35b642012-06-24 03:05:26 +03001230 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001231 * @param string $sql SQL Query
Andrey Andreev2c35b642012-06-24 03:05:26 +03001232 * @return string
1233 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001234 protected function _limit($sql)
Andrey Andreev2c35b642012-06-24 03:05:26 +03001235 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001236 return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').$this->qb_limit;
Andrey Andreev2c35b642012-06-24 03:05:26 +03001237 }
1238
1239 // --------------------------------------------------------------------
1240
1241 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001242 * The "set" function.
1243 *
1244 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001245 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 * @param mixed
1247 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001248 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 * @return object
1250 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001251 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001252 {
1253 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001254
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 if ( ! is_array($key))
1256 {
1257 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001258 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001259
Andrey Andreevfe642da2012-06-16 03:47:33 +03001260 is_bool($escape) OR $escape = $this->_protect_identifiers;
1261
Derek Allard2067d1a2008-11-13 22:59:24 +00001262 foreach ($key as $k => $v)
1263 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001264 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1265 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001266 }
Barry Mienydd671972010-10-04 16:33:58 +02001267
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 return $this;
1269 }
WanWizard7219c072011-12-28 14:09:05 +01001270
Kyle Farris0c147b32011-08-26 02:29:31 -04001271 // --------------------------------------------------------------------
1272
1273 /**
1274 * Get SELECT query string
1275 *
1276 * Compiles a SELECT query string and returns the sql.
1277 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001278 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001279 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001280 * @return string
1281 */
WanWizard7219c072011-12-28 14:09:05 +01001282 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001283 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001284 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001285 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001286 $this->_track_aliases($table);
1287 $this->from($table);
1288 }
WanWizard7219c072011-12-28 14:09:05 +01001289
Andrey Andreev650b4c02012-06-11 12:07:15 +03001290 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001291
Kyle Farris0c147b32011-08-26 02:29:31 -04001292 if ($reset === TRUE)
1293 {
1294 $this->_reset_select();
1295 }
WanWizard7219c072011-12-28 14:09:05 +01001296
Kyle Farris0c147b32011-08-26 02:29:31 -04001297 return $select;
1298 }
WanWizard7219c072011-12-28 14:09:05 +01001299
Derek Allard2067d1a2008-11-13 22:59:24 +00001300 // --------------------------------------------------------------------
1301
1302 /**
1303 * Get
1304 *
1305 * Compiles the select statement based on the other functions called
1306 * and runs the query
1307 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 * @param string the table
1309 * @param string the limit clause
1310 * @param string the offset clause
1311 * @return object
1312 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001313 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001315 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001316 {
1317 $this->_track_aliases($table);
1318 $this->from($table);
1319 }
Barry Mienydd671972010-10-04 16:33:58 +02001320
Andrey Andreev650b4c02012-06-11 12:07:15 +03001321 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001322 {
1323 $this->limit($limit, $offset);
1324 }
Barry Mienydd671972010-10-04 16:33:58 +02001325
Andrey Andreev24276a32012-01-08 02:44:38 +02001326 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001327 $this->_reset_select();
1328 return $result;
1329 }
1330
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001331 // --------------------------------------------------------------------
1332
Derek Allard2067d1a2008-11-13 22:59:24 +00001333 /**
1334 * "Count All Results" query
1335 *
Barry Mienydd671972010-10-04 16:33:58 +02001336 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001337 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001338 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 * @param string
1340 * @return string
1341 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001342 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001344 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001345 {
1346 $this->_track_aliases($table);
1347 $this->from($table);
1348 }
Barry Mienydd671972010-10-04 16:33:58 +02001349
Andrey Andreevb05f5062012-10-26 12:01:02 +03001350 $result = ($this->qb_distinct === TRUE)
1351 ? $this->query($this->_count_string.$this->protect_identifiers('numrows')."\nFROM (\n".$this->_compile_select()."\n) CI_count_all_results")
1352 : $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001354
Purwandi1d160e72012-01-09 16:33:28 +07001355 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001357 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001358 }
1359
Purwandi1d160e72012-01-09 16:33:28 +07001360 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001361 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001363
Derek Allard2067d1a2008-11-13 22:59:24 +00001364 // --------------------------------------------------------------------
1365
1366 /**
1367 * Get_Where
1368 *
1369 * Allows the where clause, limit and offset to be added directly
1370 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001371 * @param string $table
1372 * @param string $where
1373 * @param int $limit
1374 * @param int $offset
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 * @return object
1376 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001377 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001379 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001380 {
1381 $this->from($table);
1382 }
1383
1384 if ( ! is_null($where))
1385 {
1386 $this->where($where);
1387 }
Barry Mienydd671972010-10-04 16:33:58 +02001388
Andrey Andreev650b4c02012-06-11 12:07:15 +03001389 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 {
1391 $this->limit($limit, $offset);
1392 }
Barry Mienydd671972010-10-04 16:33:58 +02001393
Andrey Andreev24276a32012-01-08 02:44:38 +02001394 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001395 $this->_reset_select();
1396 return $result;
1397 }
1398
1399 // --------------------------------------------------------------------
1400
1401 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001402 * Insert_Batch
1403 *
1404 * Compiles batch insert strings and runs the queries
1405 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001406 * @param string $table Table to insert into
1407 * @param array $set An associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001408 * @param bool $escape Whether to escape values and identifiers
Andrey Andreevae85eb42012-11-02 01:42:31 +02001409 * @return int Number of rows inserted or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001410 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001411 public function insert_batch($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001412 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001413 if ( ! is_null($set))
1414 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001415 $this->set_insert_batch($set, '', $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001416 }
Barry Mienydd671972010-10-04 16:33:58 +02001417
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001418 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001419 {
Andrey Andreev9f808b02012-10-24 17:38:48 +03001420 // No valid data array. Folds in cases where keys and values did not match up
1421 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001422 }
1423
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001424 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001425 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001426 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001427 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001428 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001429 }
Barry Mienydd671972010-10-04 16:33:58 +02001430
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001431 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001432 }
1433
1434 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001435 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001436 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001437 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001438 $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 +03001439 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001440 }
Barry Mienydd671972010-10-04 16:33:58 +02001441
Derek Jonesd10e8962010-03-02 17:10:36 -06001442 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001443 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001444 }
1445
1446 // --------------------------------------------------------------------
1447
1448 /**
Andrey Andreev083e3c82012-11-06 12:48:32 +02001449 * Insert batch statement
Andrey Andreev97f36972012-04-05 12:44:36 +03001450 *
1451 * Generates a platform-specific insert string from the supplied data.
1452 *
Andrey Andreev083e3c82012-11-06 12:48:32 +02001453 * @param string $table Table name
1454 * @param array $keys INSERT keys
1455 * @param array $values INSERT values
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001456 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001457 */
1458 protected function _insert_batch($table, $keys, $values)
1459 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001460 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001461 }
1462
1463 // --------------------------------------------------------------------
1464
1465 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001466 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001467 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001468 * @param mixed
1469 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001470 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001471 * @return object
1472 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001473 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001474 {
1475 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001476
Derek Jonesd10e8962010-03-02 17:10:36 -06001477 if ( ! is_array($key))
1478 {
1479 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001480 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001481
Andrey Andreevfe642da2012-06-16 03:47:33 +03001482 is_bool($escape) OR $escape = $this->_protect_identifiers;
1483
Iban Eguia3c0a4522012-04-15 13:30:44 +02001484 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001485 sort($keys);
1486
1487 foreach ($key as $row)
1488 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001489 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001490 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1491 {
1492 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001493 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001494 return;
1495 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001496
Barry Mienydd671972010-10-04 16:33:58 +02001497 ksort($row); // puts $row in the same order as our keys
1498
Andrey Andreev650b4c02012-06-11 12:07:15 +03001499 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001500 {
1501 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001502 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001503 {
Barry Mienydd671972010-10-04 16:33:58 +02001504 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001505 }
1506
Andrey Andreev650b4c02012-06-11 12:07:15 +03001507 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001508 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001509
1510 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001511 }
1512
1513 foreach ($keys as $k)
1514 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001515 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001516 }
Barry Mienydd671972010-10-04 16:33:58 +02001517
Derek Jonesd10e8962010-03-02 17:10:36 -06001518 return $this;
1519 }
WanWizard7219c072011-12-28 14:09:05 +01001520
Kyle Farris0c147b32011-08-26 02:29:31 -04001521 // --------------------------------------------------------------------
1522
1523 /**
1524 * Get INSERT query string
1525 *
1526 * Compiles an insert query and returns the sql
1527 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001528 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001529 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001530 * @return string
1531 */
1532 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001533 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001534 if ($this->_validate_insert($table) === FALSE)
1535 {
1536 return FALSE;
1537 }
WanWizard7219c072011-12-28 14:09:05 +01001538
Kyle Farris76116012011-08-31 11:17:48 -04001539 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001540 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001541 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001542 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001543 array_keys($this->qb_set),
1544 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001545 );
WanWizard7219c072011-12-28 14:09:05 +01001546
Kyle Farris0c147b32011-08-26 02:29:31 -04001547 if ($reset === TRUE)
1548 {
1549 $this->_reset_write();
1550 }
WanWizard7219c072011-12-28 14:09:05 +01001551
Kyle Farris0c147b32011-08-26 02:29:31 -04001552 return $sql;
1553 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001554
Derek Allard2067d1a2008-11-13 22:59:24 +00001555 // --------------------------------------------------------------------
1556
1557 /**
1558 * Insert
1559 *
1560 * Compiles an insert string and runs the query
1561 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001562 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001563 * @param array an associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001564 * @param bool $escape Whether to escape values and identifiers
Derek Allard2067d1a2008-11-13 22:59:24 +00001565 * @return object
1566 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001567 public function insert($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001568 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001569 if ( ! is_null($set))
1570 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001571 $this->set($set, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001572 }
WanWizard7219c072011-12-28 14:09:05 +01001573
Kyle Farris0c147b32011-08-26 02:29:31 -04001574 if ($this->_validate_insert($table) === FALSE)
1575 {
1576 return FALSE;
1577 }
WanWizard7219c072011-12-28 14:09:05 +01001578
Kyle Farris76116012011-08-31 11:17:48 -04001579 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001580 $this->protect_identifiers(
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001581 $this->qb_from[0], TRUE, $escape, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001582 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001583 array_keys($this->qb_set),
1584 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001585 );
Barry Mienydd671972010-10-04 16:33:58 +02001586
Kyle Farris0c147b32011-08-26 02:29:31 -04001587 $this->_reset_write();
1588 return $this->query($sql);
1589 }
WanWizard7219c072011-12-28 14:09:05 +01001590
Kyle Farris0c147b32011-08-26 02:29:31 -04001591 // --------------------------------------------------------------------
1592
1593 /**
1594 * Validate Insert
1595 *
1596 * This method is used by both insert() and get_compiled_insert() to
1597 * validate that the there data is actually being set and that table
1598 * has been chosen to be inserted into.
1599 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001600 * @param string the table to insert data into
1601 * @return string
1602 */
WanWizard7219c072011-12-28 14:09:05 +01001603 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001604 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001605 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001606 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001607 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001608 }
1609
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001610 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001611 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001612 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001613 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001614 elseif ( ! isset($this->qb_from[0]))
1615 {
1616 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1617 }
WanWizard7219c072011-12-28 14:09:05 +01001618
Kyle Farris0c147b32011-08-26 02:29:31 -04001619 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001620 }
Barry Mienydd671972010-10-04 16:33:58 +02001621
Phil Sturgeon9789f322011-07-15 15:14:05 -06001622 // --------------------------------------------------------------------
1623
1624 /**
1625 * Replace
1626 *
1627 * Compiles an replace into string and runs the query
1628 *
1629 * @param string the table to replace data into
1630 * @param array an associative array of insert values
1631 * @return object
1632 */
1633 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001634 {
1635 if ( ! is_null($set))
1636 {
1637 $this->set($set);
1638 }
Barry Mienydd671972010-10-04 16:33:58 +02001639
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001640 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001641 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001642 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001643 }
1644
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001645 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001646 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001647 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001648 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001649 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001650 }
Barry Mienydd671972010-10-04 16:33:58 +02001651
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001652 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001653 }
1654
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001655 $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 +00001656
Derek Jonesd10e8962010-03-02 17:10:36 -06001657 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001658 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001659 }
WanWizard7219c072011-12-28 14:09:05 +01001660
Kyle Farris0c147b32011-08-26 02:29:31 -04001661 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001662
Kyle Farris0c147b32011-08-26 02:29:31 -04001663 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001664 * Replace statement
1665 *
1666 * Generates a platform-specific replace string from the supplied data
1667 *
1668 * @param string the table name
1669 * @param array the insert keys
1670 * @param array the insert values
1671 * @return string
1672 */
1673 protected function _replace($table, $keys, $values)
1674 {
1675 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1676 }
1677
1678 // --------------------------------------------------------------------
1679
1680 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001681 * FROM tables
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001682 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001683 * Groups tables in FROM clauses if needed, so there is no confusion
1684 * about operator precedence.
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001685 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001686 * Note: This is only used (and overriden) by MySQL and CUBRID.
1687 *
1688 * @return string
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001689 */
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001690 protected function _from_tables()
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001691 {
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001692 return implode(', ', $this->qb_from);
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001693 }
1694
1695 // --------------------------------------------------------------------
1696
1697 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001698 * Get UPDATE query string
1699 *
1700 * Compiles an update query and returns the sql
1701 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001702 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001703 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001704 * @return string
1705 */
1706 public function get_compiled_update($table = '', $reset = TRUE)
1707 {
1708 // Combine any cached components with the current statements
1709 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001710
Kyle Farris0c147b32011-08-26 02:29:31 -04001711 if ($this->_validate_update($table) === FALSE)
1712 {
1713 return FALSE;
1714 }
WanWizard7219c072011-12-28 14:09:05 +01001715
Andrey Andreevb0478652012-07-18 15:34:46 +03001716 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001717
Kyle Farris0c147b32011-08-26 02:29:31 -04001718 if ($reset === TRUE)
1719 {
1720 $this->_reset_write();
1721 }
WanWizard7219c072011-12-28 14:09:05 +01001722
Kyle Farris0c147b32011-08-26 02:29:31 -04001723 return $sql;
1724 }
WanWizard7219c072011-12-28 14:09:05 +01001725
Derek Allard2067d1a2008-11-13 22:59:24 +00001726 // --------------------------------------------------------------------
1727
1728 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001729 * UPDATE
Derek Allard2067d1a2008-11-13 22:59:24 +00001730 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001731 * Compiles an update string and runs the query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001732 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001733 * @param string $table
1734 * @param array $set An associative array of update values
1735 * @param mixed $where
1736 * @param int $limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001737 * @return object
1738 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001739 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001740 {
1741 // Combine any cached components with the current statements
1742 $this->_merge_cache();
1743
1744 if ( ! is_null($set))
1745 {
1746 $this->set($set);
1747 }
Barry Mienydd671972010-10-04 16:33:58 +02001748
Kyle Farris0c147b32011-08-26 02:29:31 -04001749 if ($this->_validate_update($table) === FALSE)
1750 {
1751 return FALSE;
1752 }
1753
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001754 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001755 {
1756 $this->where($where);
1757 }
1758
Andrey Andreev650b4c02012-06-11 12:07:15 +03001759 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001760 {
1761 $this->limit($limit);
1762 }
1763
Andrey Andreevb0478652012-07-18 15:34:46 +03001764 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
Kyle Farris0c147b32011-08-26 02:29:31 -04001765 $this->_reset_write();
1766 return $this->query($sql);
1767 }
WanWizard7219c072011-12-28 14:09:05 +01001768
Kyle Farris0c147b32011-08-26 02:29:31 -04001769 // --------------------------------------------------------------------
1770
1771 /**
1772 * Validate Update
1773 *
1774 * This method is used by both update() and get_compiled_update() to
1775 * validate that data is actually being set and that a table has been
1776 * chosen to be update.
1777 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001778 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001779 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001780 */
1781 protected function _validate_update($table = '')
1782 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001783 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001784 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001785 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001786 }
1787
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001788 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001790 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001792 elseif ( ! isset($this->qb_from[0]))
1793 {
1794 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1795 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001796
1797 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001798 }
WanWizard7219c072011-12-28 14:09:05 +01001799
Derek Jonesd10e8962010-03-02 17:10:36 -06001800 // --------------------------------------------------------------------
1801
1802 /**
1803 * Update_Batch
1804 *
1805 * Compiles an update string and runs the query
1806 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001807 * @param string the table to retrieve the results from
1808 * @param array an associative array of update values
1809 * @param string the where key
Andrey Andreev9f808b02012-10-24 17:38:48 +03001810 * @return int number of rows affected or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001811 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001812 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001813 {
1814 // Combine any cached components with the current statements
1815 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001816
Derek Jonesd10e8962010-03-02 17:10:36 -06001817 if (is_null($index))
1818 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001819 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001820 }
1821
1822 if ( ! is_null($set))
1823 {
1824 $this->set_update_batch($set, $index);
1825 }
1826
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001827 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001828 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001829 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001830 }
1831
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001832 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001833 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001834 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001835 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001836 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001837 }
Barry Mienydd671972010-10-04 16:33:58 +02001838
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001839 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001840 }
Barry Mienydd671972010-10-04 16:33:58 +02001841
Derek Jonesd10e8962010-03-02 17:10:36 -06001842 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001843 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001844 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001845 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001846 $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 +03001847 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001848 }
Barry Mienydd671972010-10-04 16:33:58 +02001849
Derek Jonesd10e8962010-03-02 17:10:36 -06001850 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001851 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001852 }
1853
1854 // --------------------------------------------------------------------
1855
1856 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001857 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001858 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001859 * @param array
1860 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001861 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001862 * @return object
1863 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001864 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001865 {
1866 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001867
Derek Jonesd10e8962010-03-02 17:10:36 -06001868 if ( ! is_array($key))
1869 {
1870 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001871 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001872
Andrey Andreevfe642da2012-06-16 03:47:33 +03001873 is_bool($escape) OR $escape = $this->_protect_identifiers;
1874
Derek Jonesd10e8962010-03-02 17:10:36 -06001875 foreach ($key as $k => $v)
1876 {
1877 $index_set = FALSE;
1878 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001879 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001880 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001881 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001882 {
1883 $index_set = TRUE;
1884 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001885
Andrey Andreevfe642da2012-06-16 03:47:33 +03001886 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001887 }
1888
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001889 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001890 {
1891 return $this->display_error('db_batch_missing_index');
1892 }
1893
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001894 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001895 }
Barry Mienydd671972010-10-04 16:33:58 +02001896
Derek Jonesd10e8962010-03-02 17:10:36 -06001897 return $this;
1898 }
1899
Derek Allard2067d1a2008-11-13 22:59:24 +00001900 // --------------------------------------------------------------------
1901
1902 /**
1903 * Empty Table
1904 *
1905 * Compiles a delete string and runs "DELETE FROM table"
1906 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001907 * @param string the table to empty
1908 * @return object
1909 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001910 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001911 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001912 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001913 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001914 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001915 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001916 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001917 }
1918
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001919 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001920 }
1921 else
1922 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001923 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001924 }
1925
1926 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001927 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001928 return $this->query($sql);
1929 }
1930
1931 // --------------------------------------------------------------------
1932
1933 /**
1934 * Truncate
1935 *
1936 * Compiles a truncate string and runs the query
1937 * If the database does not support the truncate() command
1938 * This function maps to "DELETE FROM table"
1939 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001940 * @param string the table to truncate
1941 * @return object
1942 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001943 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001944 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001945 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001946 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001947 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001948 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001949 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001950 }
1951
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001952 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001953 }
1954 else
1955 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001956 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001957 }
1958
1959 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001960 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001961 return $this->query($sql);
1962 }
WanWizard7219c072011-12-28 14:09:05 +01001963
Kyle Farris0c147b32011-08-26 02:29:31 -04001964 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001965
Kyle Farris0c147b32011-08-26 02:29:31 -04001966 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001967 * Truncate statement
1968 *
1969 * Generates a platform-specific truncate string from the supplied data
1970 *
1971 * If the database does not support the truncate() command,
1972 * then this method maps to 'DELETE FROM table'
1973 *
1974 * @param string the table name
1975 * @return string
1976 */
1977 protected function _truncate($table)
1978 {
1979 return 'TRUNCATE '.$table;
1980 }
1981
1982 // --------------------------------------------------------------------
1983
1984 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001985 * Get DELETE query string
1986 *
1987 * Compiles a delete query string and returns the sql
1988 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001989 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001990 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001991 * @return string
1992 */
1993 public function get_compiled_delete($table = '', $reset = TRUE)
1994 {
1995 $this->return_delete_sql = TRUE;
1996 $sql = $this->delete($table, '', NULL, $reset);
1997 $this->return_delete_sql = FALSE;
1998 return $sql;
1999 }
WanWizard7219c072011-12-28 14:09:05 +01002000
Derek Allard2067d1a2008-11-13 22:59:24 +00002001 // --------------------------------------------------------------------
2002
2003 /**
2004 * Delete
2005 *
2006 * Compiles a delete string and runs the query
2007 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002008 * @param mixed the table(s) to delete from. String or array
2009 * @param mixed the where clause
2010 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03002011 * @param bool
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002012 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00002013 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002014 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002015 {
2016 // Combine any cached components with the current statements
2017 $this->_merge_cache();
2018
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002019 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002020 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002021 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002022 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002023 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002024 }
2025
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002026 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002027 }
2028 elseif (is_array($table))
2029 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05002030 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002031 {
Andrey Andreev13f50542012-10-12 12:31:02 +03002032 $this->delete($single_table, $where, $limit, $reset_data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002033 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002034 return;
2035 }
2036 else
2037 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002038 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002039 }
2040
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002041 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002042 {
2043 $this->where($where);
2044 }
2045
Andrey Andreev650b4c02012-06-11 12:07:15 +03002046 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002047 {
2048 $this->limit($limit);
2049 }
2050
Andrey Andreev94611df2012-07-19 12:29:54 +03002051 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002052 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002053 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02002054 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002055
Andrey Andreevb0478652012-07-18 15:34:46 +03002056 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002057 if ($reset_data)
2058 {
2059 $this->_reset_write();
2060 }
WanWizard7219c072011-12-28 14:09:05 +01002061
Andrey Andreev24276a32012-01-08 02:44:38 +02002062 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00002063 }
WanWizard7219c072011-12-28 14:09:05 +01002064
Derek Allard2067d1a2008-11-13 22:59:24 +00002065 // --------------------------------------------------------------------
2066
2067 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03002068 * Delete statement
2069 *
2070 * Generates a platform-specific delete string from the supplied data
2071 *
2072 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03002073 * @return string
2074 */
Andrey Andreevb0478652012-07-18 15:34:46 +03002075 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03002076 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002077 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002078 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03002079 }
2080
2081 // --------------------------------------------------------------------
2082
2083 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002084 * DB Prefix
2085 *
2086 * Prepends a database prefix if one exists in configuration
2087 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002088 * @param string the table
2089 * @return string
2090 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002091 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002092 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002093 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002094 {
2095 $this->display_error('db_table_name_required');
2096 }
2097
2098 return $this->dbprefix.$table;
2099 }
2100
2101 // --------------------------------------------------------------------
2102
2103 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06002104 * Set DB Prefix
2105 *
2106 * Set's the DB Prefix to something new without needing to reconnect
2107 *
2108 * @param string the prefix
2109 * @return string
2110 */
2111 public function set_dbprefix($prefix = '')
2112 {
2113 return $this->dbprefix = $prefix;
2114 }
2115
2116 // --------------------------------------------------------------------
2117
2118 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002119 * Track Aliases
2120 *
2121 * Used to track SQL statements written with aliased tables.
2122 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002123 * @param string The table to inspect
2124 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002125 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002126 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002127 {
2128 if (is_array($table))
2129 {
2130 foreach ($table as $t)
2131 {
2132 $this->_track_aliases($t);
2133 }
2134 return;
2135 }
Barry Mienydd671972010-10-04 16:33:58 +02002136
Derek Jones37f4b9c2011-07-01 17:56:50 -05002137 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002138 // the string into discreet statements
2139 if (strpos($table, ',') !== FALSE)
2140 {
2141 return $this->_track_aliases(explode(',', $table));
2142 }
Barry Mienydd671972010-10-04 16:33:58 +02002143
Derek Allard2067d1a2008-11-13 22:59:24 +00002144 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002145 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002146 {
2147 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002148 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002149
Derek Allard2067d1a2008-11-13 22:59:24 +00002150 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002151 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002152
Derek Allard2067d1a2008-11-13 22:59:24 +00002153 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002154 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002155 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002156 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002157 }
2158 }
2159 }
WanWizard7219c072011-12-28 14:09:05 +01002160
Derek Allard2067d1a2008-11-13 22:59:24 +00002161 // --------------------------------------------------------------------
2162
2163 /**
2164 * Compile the SELECT statement
2165 *
2166 * Generates a query string based on which functions were used.
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002167 * Should not be called directly.
Derek Allard2067d1a2008-11-13 22:59:24 +00002168 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002169 * @param bool $select_override
Derek Allard2067d1a2008-11-13 22:59:24 +00002170 * @return string
2171 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002172 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002173 {
2174 // Combine any cached components with the current statements
2175 $this->_merge_cache();
2176
Derek Allard2067d1a2008-11-13 22:59:24 +00002177 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002178 if ($select_override !== FALSE)
2179 {
2180 $sql = $select_override;
2181 }
2182 else
2183 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002184 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002185
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002186 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002187 {
Barry Mienydd671972010-10-04 16:33:58 +02002188 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002189 }
2190 else
Barry Mienydd671972010-10-04 16:33:58 +02002191 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002192 // Cycle through the "select" portion of the query and prep each column name.
2193 // The reason we protect identifiers here rather then in the select() function
2194 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002195 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002196 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002197 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002198 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002199 }
Barry Mienydd671972010-10-04 16:33:58 +02002200
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002201 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002202 }
2203 }
2204
Derek Allard2067d1a2008-11-13 22:59:24 +00002205 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002206 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002207 {
Andrey Andreeve78f8152012-10-09 11:38:38 +03002208 $sql .= "\nFROM ".$this->_from_tables();
Derek Allard2067d1a2008-11-13 22:59:24 +00002209 }
2210
Derek Allard2067d1a2008-11-13 22:59:24 +00002211 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002212 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002213 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002214 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002215 }
2216
Andrey Andreev2d486232012-07-19 14:46:51 +03002217 $sql .= $this->_compile_wh('qb_where')
2218 .$this->_compile_group_by()
2219 .$this->_compile_wh('qb_having')
2220 .$this->_compile_order_by(); // ORDER BY
Andrey Andreevb0478652012-07-18 15:34:46 +03002221
Andrey Andreevd40459d2012-07-18 16:46:39 +03002222 // LIMIT
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002223 if ($this->qb_limit)
Derek Allard2067d1a2008-11-13 22:59:24 +00002224 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002225 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002226 }
2227
2228 return $sql;
2229 }
2230
2231 // --------------------------------------------------------------------
2232
2233 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002234 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002235 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002236 * Escapes identifiers in WHERE and HAVING statements at execution time.
2237 *
Andrey Andreevb0478652012-07-18 15:34:46 +03002238 * Required so that aliases are tracked properly, regardless of wether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002239 * where(), or_where(), having(), or_having are called prior to from(),
2240 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002241 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002242 * @param string $qb_key 'qb_where' or 'qb_having'
Andrey Andreevd40459d2012-07-18 16:46:39 +03002243 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002244 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002245 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002246 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002247 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002248 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002249 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002250 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002251 if ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002252 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002253 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002254 continue;
2255 }
2256
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002257 // Split multiple conditions
2258 $conditions = preg_split(
2259 '/(\s*AND\s+|\s*OR\s+)/i',
2260 $this->{$qb_key}[$i]['condition'],
2261 -1,
2262 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
2263 );
2264
2265 for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002266 {
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002267 if (($op = $this->_get_operator($conditions[$ci])) === FALSE
Andrey Andreeve4742582012-10-25 13:25:13 +03002268 OR ! preg_match('/^(\(?)(.*)('.preg_quote($op, '/').')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches))
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002269 {
2270 continue;
2271 }
2272
2273 // $matches = array(
2274 // 0 => '(test <= foo)', /* the whole thing */
2275 // 1 => '(', /* optional */
2276 // 2 => 'test', /* the field name */
2277 // 3 => ' <= ', /* $op */
2278 // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2279 // 5 => ')' /* optional */
2280 // );
Andrey Andreev082aa402012-10-22 19:41:55 +03002281
2282 if ( ! empty($matches[4]))
2283 {
2284 $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4]));
2285 $matches[4] = ' '.$matches[4];
2286 }
2287
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002288 $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2]))
2289 .' '.trim($matches[3]).$matches[4].$matches[5];
Andrey Andreev6e704752012-07-18 00:46:33 +03002290 }
2291
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002292 $this->{$qb_key}[$i] = implode('', $conditions);
Andrey Andreev6e704752012-07-18 00:46:33 +03002293 }
2294
Andrey Andreev9d3aa1b2012-10-12 12:14:09 +03002295 return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ")
2296 .implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002297 }
2298
Andrey Andreevb0478652012-07-18 15:34:46 +03002299 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002300 }
2301
2302 // --------------------------------------------------------------------
2303
2304 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002305 * Compile GROUP BY
2306 *
2307 * Escapes identifiers in GROUP BY statements at execution time.
2308 *
2309 * Required so that aliases are tracked properly, regardless of wether
2310 * group_by() is called prior to from(), join() and dbprefix is added
2311 * only if needed.
2312 *
2313 * @return string SQL statement
2314 */
2315 protected function _compile_group_by()
2316 {
2317 if (count($this->qb_groupby) > 0)
2318 {
Andrey Andreev96feb582012-07-19 13:12:34 +03002319 for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
2320 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002321 $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 +03002322 ? $this->qb_groupby[$i]['field']
Andrey Andreev13f50542012-10-12 12:31:02 +03002323 : $this->protect_identifiers($this->qb_groupby[$i]['field']);
Andrey Andreev96feb582012-07-19 13:12:34 +03002324 }
2325
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002326 return "\nGROUP BY ".implode(', ', $this->qb_groupby);
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002327 }
2328
2329 return '';
2330 }
2331
2332 // --------------------------------------------------------------------
2333
2334 /**
Andrey Andreev2d486232012-07-19 14:46:51 +03002335 * Compile ORDER BY
2336 *
2337 * Escapes identifiers in ORDER BY statements at execution time.
2338 *
2339 * Required so that aliases are tracked properly, regardless of wether
2340 * order_by() is called prior to from(), join() and dbprefix is added
2341 * only if needed.
2342 *
2343 * @return string SQL statement
2344 */
2345 protected function _compile_order_by()
2346 {
Andrey Andreeva53ea842012-10-23 12:44:09 +03002347 if (is_array($this->qb_orderby) && count($this->qb_orderby) > 0)
Andrey Andreev2d486232012-07-19 14:46:51 +03002348 {
Andrey Andreev2d486232012-07-19 14:46:51 +03002349 for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++)
2350 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002351 if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field']))
Andrey Andreev2d486232012-07-19 14:46:51 +03002352 {
Andrey Andreevfc043b32012-10-12 14:46:14 +03002353 $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']);
Andrey Andreev2d486232012-07-19 14:46:51 +03002354 }
2355
2356 $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction'];
2357 }
2358
Andrey Andreeva53ea842012-10-23 12:44:09 +03002359 return $this->qb_orderby = "\nORDER BY ".implode(', ', $this->qb_orderby);
2360 }
2361 elseif (is_string($this->qb_orderby))
2362 {
2363 return $this->qb_orderby;
Andrey Andreev2d486232012-07-19 14:46:51 +03002364 }
2365
2366 return '';
2367 }
2368
2369 // --------------------------------------------------------------------
2370
2371 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002372 * Object to Array
2373 *
2374 * Takes an object as input and converts the class variables to array key/vals
2375 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002376 * @param object
2377 * @return array
2378 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002379 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002380 {
2381 if ( ! is_object($object))
2382 {
2383 return $object;
2384 }
Barry Mienydd671972010-10-04 16:33:58 +02002385
Derek Allard2067d1a2008-11-13 22:59:24 +00002386 $array = array();
2387 foreach (get_object_vars($object) as $key => $val)
2388 {
2389 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002390 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002391 {
2392 $array[$key] = $val;
2393 }
2394 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002395
2396 return $array;
2397 }
Barry Mienydd671972010-10-04 16:33:58 +02002398
Derek Jonesd10e8962010-03-02 17:10:36 -06002399 // --------------------------------------------------------------------
2400
2401 /**
2402 * Object to Array
2403 *
2404 * Takes an object as input and converts the class variables to array key/vals
2405 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002406 * @param object
2407 * @return array
2408 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002409 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002410 {
2411 if ( ! is_object($object))
2412 {
2413 return $object;
2414 }
Barry Mienydd671972010-10-04 16:33:58 +02002415
Derek Jonesd10e8962010-03-02 17:10:36 -06002416 $array = array();
2417 $out = get_object_vars($object);
2418 $fields = array_keys($out);
2419
2420 foreach ($fields as $val)
2421 {
2422 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002423 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002424 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002425 $i = 0;
2426 foreach ($out[$val] as $data)
2427 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002428 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002429 }
2430 }
2431 }
2432
Derek Allard2067d1a2008-11-13 22:59:24 +00002433 return $array;
2434 }
Barry Mienydd671972010-10-04 16:33:58 +02002435
Derek Allard2067d1a2008-11-13 22:59:24 +00002436 // --------------------------------------------------------------------
2437
2438 /**
2439 * Start Cache
2440 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002441 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002442 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002443 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002444 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002445 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002446 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002447 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002448 }
2449
2450 // --------------------------------------------------------------------
2451
2452 /**
2453 * Stop Cache
2454 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002455 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002456 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002457 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002458 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002459 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002460 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002461 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002462 }
2463
2464 // --------------------------------------------------------------------
2465
2466 /**
2467 * Flush Cache
2468 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002469 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002470 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002471 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002472 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002473 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002474 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002475 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002476 'qb_cache_select' => array(),
2477 'qb_cache_from' => array(),
2478 'qb_cache_join' => array(),
2479 'qb_cache_where' => array(),
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002480 'qb_cache_groupby' => array(),
2481 'qb_cache_having' => array(),
2482 'qb_cache_orderby' => array(),
2483 'qb_cache_set' => array(),
2484 'qb_cache_exists' => array(),
2485 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002486 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002487 }
2488
2489 // --------------------------------------------------------------------
2490
2491 /**
2492 * Merge Cache
2493 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002494 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002495 * locally called ones.
2496 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002497 * @return void
2498 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002499 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002500 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002501 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002502 {
2503 return;
2504 }
2505
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002506 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002507 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002508 $qb_variable = 'qb_'.$val;
2509 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002510
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002511 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002512 {
2513 continue;
2514 }
Andrey Andreeveae17d12012-11-17 23:55:18 +02002515 $this->$qb_variable = array_merge($this->$qb_variable, array_diff($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002516 }
2517
2518 // If we are "protecting identifiers" we need to examine the "from"
2519 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002520 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002521 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002522 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002523 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002524
Andrey Andreeveae17d12012-11-17 23:55:18 +02002525 $this->qb_no_escape = array_merge($this->qb_no_escape, array_diff($this->qb_cache_no_escape, $this->qb_no_escape));
Derek Allard2067d1a2008-11-13 22:59:24 +00002526 }
WanWizard7219c072011-12-28 14:09:05 +01002527
Kyle Farris0c147b32011-08-26 02:29:31 -04002528 // --------------------------------------------------------------------
2529
2530 /**
Andrey Andreev082aa402012-10-22 19:41:55 +03002531 * Is literal
2532 *
2533 * Determines if a string represents a literal value or a field name
2534 *
Andrey Andreev02e4cd72012-11-13 11:50:47 +02002535 * @param string $str
Andrey Andreev082aa402012-10-22 19:41:55 +03002536 * @return bool
2537 */
2538 protected function _is_literal($str)
2539 {
2540 $str = trim($str);
2541
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002542 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 +03002543 {
2544 return TRUE;
2545 }
2546
2547 static $_str;
2548
2549 if (empty($_str))
2550 {
2551 $_str = ($this->_escape_char !== '"')
2552 ? array('"', "'") : array("'");
2553 }
2554
Andrey Andreev3a5efc22012-11-20 21:18:08 +02002555 return in_array($str[0], $_str, TRUE);
Andrey Andreev082aa402012-10-22 19:41:55 +03002556 }
2557
2558 // --------------------------------------------------------------------
2559
2560 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002561 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002562 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002563 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002564 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002565 * @return void
2566 */
2567 public function reset_query()
2568 {
2569 $this->_reset_select();
2570 $this->_reset_write();
2571 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002572
2573 // --------------------------------------------------------------------
2574
2575 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002576 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002577 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002578 * @param array An array of fields to reset
2579 * @return void
2580 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002581 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002582 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002583 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002584 {
Andrey Andreevae85eb42012-11-02 01:42:31 +02002585 $this->$item = $default_value;
Derek Allard2067d1a2008-11-13 22:59:24 +00002586 }
2587 }
2588
2589 // --------------------------------------------------------------------
2590
2591 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002592 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002593 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002594 * @return void
2595 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002596 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002597 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002598 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002599 'qb_select' => array(),
2600 'qb_from' => array(),
2601 'qb_join' => array(),
2602 'qb_where' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002603 'qb_groupby' => array(),
2604 'qb_having' => array(),
2605 'qb_orderby' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002606 'qb_aliased_tables' => array(),
2607 'qb_no_escape' => array(),
2608 'qb_distinct' => FALSE,
2609 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002610 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002611 )
2612 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002613 }
Barry Mienydd671972010-10-04 16:33:58 +02002614
Derek Allard2067d1a2008-11-13 22:59:24 +00002615 // --------------------------------------------------------------------
2616
2617 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002618 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002619 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002620 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002621 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002622 * @return void
2623 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002624 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002625 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002626 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002627 'qb_set' => array(),
2628 'qb_from' => array(),
2629 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002630 'qb_orderby' => array(),
2631 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002632 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002633 )
2634 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002635 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002636
Derek Allard2067d1a2008-11-13 22:59:24 +00002637}
2638
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002639/* End of file DB_query_builder.php */
Andrey Andreev58803fb2012-06-24 00:45:37 +03002640/* Location: ./system/database/DB_query_builder.php */