blob: a3ba77360e5257cc1ebec613ad761fc235d71548 [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
647 if (is_null($v) && ! $this->_has_operator($k))
648 {
649 // value appears not to have been set, assign the test to IS NULL
650 $k .= ' IS NULL';
651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 if ( ! is_null($v))
654 {
655 if ($escape === TRUE)
656 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300657 $v = ' '.(is_int($v) ? $v : $this->escape($v));
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 }
WanWizard7219c072011-12-28 14:09:05 +0100659
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 if ( ! $this->_has_operator($k))
661 {
Greg Akere156c6e2011-04-20 16:03:04 -0500662 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 }
664 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000665
Andrey Andreevd40459d2012-07-18 16:46:39 +0300666 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000667 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300669 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
670 $this->qb_cache_exists[] = substr($qb_key, 3);
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 }
Barry Mienydd671972010-10-04 16:33:58 +0200672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 }
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 return $this;
676 }
677
678 // --------------------------------------------------------------------
679
680 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200681 * WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200683 * Generates a WHERE field IN('item', 'item') SQL query,
684 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200686 * @param string $key The field to search
687 * @param array $values The values searched on
688 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 * @return object
690 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300691 public function where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300693 return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 }
Barry Mienydd671972010-10-04 16:33:58 +0200695
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 // --------------------------------------------------------------------
697
698 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200699 * OR WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200701 * Generates a WHERE field IN('item', 'item') SQL query,
702 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200704 * @param string $key The field to search
705 * @param array $values The values searched on
706 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 * @return object
708 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300709 public function or_where_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300711 return $this->_where_in($key, $values, FALSE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 }
713
714 // --------------------------------------------------------------------
715
716 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200717 * WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200719 * Generates a WHERE field NOT IN('item', 'item') SQL query,
720 * joined with 'AND' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200722 * @param string $key The field to search
723 * @param array $values The values searched on
724 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 * @return object
726 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300727 public function where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300729 return $this->_where_in($key, $values, TRUE, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 }
Barry Mienydd671972010-10-04 16:33:58 +0200731
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 // --------------------------------------------------------------------
733
734 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200735 * OR WHERE NOT IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200737 * Generates a WHERE field NOT IN('item', 'item') SQL query,
738 * joined with 'OR' if appropriate.
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200740 * @param string $key The field to search
741 * @param array $values The values searched on
742 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 * @return object
744 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300745 public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 {
Andrey Andreev498c1e02012-06-16 03:34:10 +0300747 return $this->_where_in($key, $values, TRUE, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 }
749
750 // --------------------------------------------------------------------
751
752 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200753 * Internal WHERE IN
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200755 * @used-by where_in()
756 * @used-by or_where_in()
757 * @used-by where_not_in()
758 * @used-by or_where_not_in()
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200760 * @param string $key The field to search
761 * @param array $values The values searched on
762 * @param bool $not If the statement would be IN or NOT IN
763 * @param string $type
764 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 * @return object
766 */
Andrey Andreev498c1e02012-06-16 03:34:10 +0300767 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 {
769 if ($key === NULL OR $values === NULL)
770 {
Rafael Queiroz6600b692012-06-08 14:34:20 -0300771 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 }
Barry Mienydd671972010-10-04 16:33:58 +0200773
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 if ( ! is_array($values))
775 {
776 $values = array($values);
777 }
Barry Mienydd671972010-10-04 16:33:58 +0200778
Andrey Andreev498c1e02012-06-16 03:34:10 +0300779 is_bool($escape) OR $escape = $this->_protect_identifiers;
780
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 $not = ($not) ? ' NOT' : '';
782
Andrey Andreev94611df2012-07-19 12:29:54 +0300783 $where_in = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 foreach ($values as $value)
785 {
Andrey Andreevcc02db92012-10-12 14:30:10 +0300786 $where_in[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 }
788
WanWizardbc69f362012-06-22 00:10:11 +0200789 $prefix = (count($this->qb_where) === 0) ? $this->_group_get_type('') : $this->_group_get_type($type);
Andrey Andreev6e704752012-07-18 00:46:33 +0300790 $where_in = array(
Andrey Andreev94611df2012-07-19 12:29:54 +0300791 'condition' => $prefix.$key.$not.' IN('.implode(', ', $where_in).')',
Andrey Andreev6e704752012-07-18 00:46:33 +0300792 'escape' => $escape
793 );
Barry Mienydd671972010-10-04 16:33:58 +0200794
Andrey Andreev6e704752012-07-18 00:46:33 +0300795 $this->qb_where[] = $where_in;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000796 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000798 $this->qb_cache_where[] = $where_in;
799 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 }
801
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 return $this;
803 }
Barry Mienydd671972010-10-04 16:33:58 +0200804
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 // --------------------------------------------------------------------
806
807 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200808 * LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200810 * Generates a %LIKE% portion of the query.
811 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200813 * @param mixed $field
814 * @param string $match
815 * @param string $side
816 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 * @return object
818 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300819 public function like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300821 return $this->_like($field, $match, 'AND ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 }
823
824 // --------------------------------------------------------------------
825
826 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200827 * NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000828 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200829 * Generates a NOT LIKE portion of the query.
830 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200832 * @param mixed $field
833 * @param string $match
834 * @param string $side
835 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 * @return object
837 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300838 public function not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300840 return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 }
Barry Mienydd671972010-10-04 16:33:58 +0200842
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 // --------------------------------------------------------------------
844
845 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200846 * OR LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200848 * Generates a %LIKE% portion of the query.
849 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200851 * @param mixed $field
852 * @param string $match
853 * @param string $side
854 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 * @return object
856 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300857 public function or_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300859 return $this->_like($field, $match, 'OR ', $side, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 }
861
862 // --------------------------------------------------------------------
863
864 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200865 * OR NOT LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200867 * Generates a NOT LIKE portion of the query.
868 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +0000869 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200870 * @param mixed $field
871 * @param string $match
872 * @param string $side
873 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 * @return object
875 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300876 public function or_not_like($field, $match = '', $side = 'both', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300878 return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 }
Barry Mienydd671972010-10-04 16:33:58 +0200880
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 // --------------------------------------------------------------------
882
883 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +0200884 * Internal LIKE
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200886 * @used-by like()
887 * @used-by or_like()
888 * @used-by not_like()
889 * @used-by or_not_like()
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200891 * @param mixed $field
892 * @param string $match
893 * @param string $type
894 * @param string $side
895 * @param string $not
896 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 * @return object
898 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300899 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 {
901 if ( ! is_array($field))
902 {
903 $field = array($field => $match);
904 }
Barry Mienydd671972010-10-04 16:33:58 +0200905
Andrey Andreevb0478652012-07-18 15:34:46 +0300906 is_bool($escape) OR $escape = $this->_protect_identifiers;
907 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
908 ? $this->_group_get_type('') : $this->_group_get_type($type);
909
Derek Allard2067d1a2008-11-13 22:59:24 +0000910 foreach ($field as $k => $v)
911 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000912 $v = $this->escape_like_str($v);
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300913
Andrey Andreev24276a32012-01-08 02:44:38 +0200914 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400915 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300916 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}'";
Kyle Farris81ef70f2011-08-31 11:59:12 -0400917 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200918 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300920 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200922 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300924 $like_statement = "{$prefix} {$k} {$not} LIKE '{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 }
926 else
927 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300928 $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600930
Derek Jonese4ed5832009-02-20 21:44:59 +0000931 // some platforms require an escape sequence definition for LIKE wildcards
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100932 if ($this->_like_escape_str !== '')
Derek Jonese4ed5832009-02-20 21:44:59 +0000933 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300934 $like_statement .= sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000935 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600936
Andrey Andreevb0478652012-07-18 15:34:46 +0300937 $this->qb_where[] = array('condition' => $like_statement, 'escape' => $escape);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000938 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 {
Andrey Andreevededc4a2012-07-18 01:16:15 +0300940 $this->qb_cache_where[] = $like_statement;
941 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200944
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 return $this;
946 }
Barry Mienydd671972010-10-04 16:33:58 +0200947
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 // --------------------------------------------------------------------
949
950 /**
WanWizard7219c072011-12-28 14:09:05 +0100951 * Starts a query group.
952 *
Andrey Andreevae85eb42012-11-02 01:42:31 +0200953 * @param string $not (Internal use only)
954 * @param string $type (Internal use only)
WanWizard7219c072011-12-28 14:09:05 +0100955 * @return object
956 */
957 public function group_start($not = '', $type = 'AND ')
958 {
959 $type = $this->_group_get_type($type);
WanWizard7219c072011-12-28 14:09:05 +0100960
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000961 $this->qb_where_group_started = TRUE;
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +0100962 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
Andrey Andreev6e704752012-07-18 00:46:33 +0300963 $where = array(
964 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (',
965 'escape' => FALSE
966 );
WanWizard7219c072011-12-28 14:09:05 +0100967
Andrey Andreev6e704752012-07-18 00:46:33 +0300968 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000969 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100970 {
Andrey Andreev6e704752012-07-18 00:46:33 +0300971 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +0100972 }
973
974 return $this;
975 }
976
977 // --------------------------------------------------------------------
978
979 /**
980 * Starts a query group, but ORs the group
981 *
982 * @return object
983 */
984 public function or_group_start()
985 {
986 return $this->group_start('', 'OR ');
987 }
988
989 // --------------------------------------------------------------------
990
991 /**
992 * Starts a query group, but NOTs the group
993 *
994 * @return object
995 */
996 public function not_group_start()
997 {
998 return $this->group_start('NOT ', 'AND ');
999 }
1000
1001 // --------------------------------------------------------------------
1002
1003 /**
1004 * Starts a query group, but OR NOTs the group
1005 *
1006 * @return object
1007 */
1008 public function or_not_group_start()
1009 {
1010 return $this->group_start('NOT ', 'OR ');
1011 }
1012
1013 // --------------------------------------------------------------------
1014
1015 /**
1016 * Ends a query group
1017 *
1018 * @return object
1019 */
1020 public function group_end()
1021 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001022 $this->qb_where_group_started = FALSE;
Andrey Andreev6e704752012-07-18 00:46:33 +03001023 $where = array(
1024 'condition' => str_repeat(' ', $this->qb_where_group_count--).')',
1025 'escape' => FALSE
1026 );
WanWizard7219c072011-12-28 14:09:05 +01001027
Andrey Andreev6e704752012-07-18 00:46:33 +03001028 $this->qb_where[] = $where;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001029 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +01001030 {
Andrey Andreev6e704752012-07-18 00:46:33 +03001031 $this->qb_cache_where[] = $where;
WanWizard7219c072011-12-28 14:09:05 +01001032 }
1033
WanWizard7219c072011-12-28 14:09:05 +01001034 return $this;
1035 }
1036
1037 // --------------------------------------------------------------------
1038
1039 /**
1040 * Group_get_type
1041 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001042 * @used-by group_start()
1043 * @used-by _like()
1044 * @used-by _wh()
1045 * @used-by _where_in()
WanWizard7219c072011-12-28 14:09:05 +01001046 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001047 * @param string $type
WanWizard7219c072011-12-28 14:09:05 +01001048 * @return string
1049 */
1050 protected function _group_get_type($type)
1051 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001052 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +01001053 {
1054 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001055 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +01001056 }
1057
1058 return $type;
1059 }
1060
1061 // --------------------------------------------------------------------
1062
1063 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001064 * GROUP BY
1065 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001066 * @param string $by
1067 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 * @return object
1069 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001070 public function group_by($by, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001072 is_bool($escape) OR $escape = $this->_protect_identifiers;
1073
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 if (is_string($by))
1075 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001076 $by = ($escape === TRUE)
1077 ? explode(',', $by)
1078 : array($by);
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 }
Barry Mienydd671972010-10-04 16:33:58 +02001080
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 foreach ($by as $val)
1082 {
1083 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +02001084
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001085 if ($val !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 {
Andrey Andreev96feb582012-07-19 13:12:34 +03001087 $val = array('field' => $val, 'escape' => $escape);
Barry Mienydd671972010-10-04 16:33:58 +02001088
Andrey Andreev96feb582012-07-19 13:12:34 +03001089 $this->qb_groupby[] = $val;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001090 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001091 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001092 $this->qb_cache_groupby[] = $val;
1093 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001094 }
1095 }
1096 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001097
Derek Allard2067d1a2008-11-13 22:59:24 +00001098 return $this;
1099 }
1100
1101 // --------------------------------------------------------------------
1102
1103 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001104 * HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001106 * Separates multiple calls with 'AND'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001108 * @param string $key
1109 * @param string $value
1110 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 * @return object
1112 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001113 public function having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001115 return $this->_wh('qb_having', $key, $value, 'AND ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 }
Barry Mienydd671972010-10-04 16:33:58 +02001117
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 // --------------------------------------------------------------------
1119
1120 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001121 * OR HAVING
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001123 * Separates multiple calls with 'OR'.
Derek Allard2067d1a2008-11-13 22:59:24 +00001124 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001125 * @param string $key
1126 * @param string $value
1127 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 * @return object
1129 */
Andrey Andreev0bcf5902012-10-12 13:03:29 +03001130 public function or_having($key, $value = NULL, $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03001132 return $this->_wh('qb_having', $key, $value, 'OR ', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 }
Barry Mienydd671972010-10-04 16:33:58 +02001134
Derek Allard2067d1a2008-11-13 22:59:24 +00001135 // --------------------------------------------------------------------
1136
1137 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001138 * ORDER BY
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001140 * @param string $orderby
1141 * @param string $direction ASC or DESC
1142 * @param bool $escape
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 * @return object
1144 */
Andrey Andreevd24160c2012-06-16 03:21:20 +03001145 public function order_by($orderby, $direction = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001147 $direction = trim($direction);
1148
1149 if (strtolower($direction) === 'random' OR $orderby === $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001151 // Random ordered results don't need a field name
1152 $orderby = $this->_random_keyword;
1153 $direction = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 }
Andrey Andreev2d486232012-07-19 14:46:51 +03001155 elseif (empty($orderby))
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001157 return $this;
1158 }
1159 elseif ($direction !== '')
1160 {
1161 $direction = in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE) ? ' '.$direction : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 }
Barry Mienydd671972010-10-04 16:33:58 +02001163
Andrey Andreevd24160c2012-06-16 03:21:20 +03001164 is_bool($escape) OR $escape = $this->_protect_identifiers;
Barry Mienydd671972010-10-04 16:33:58 +02001165
Andrey Andreev2d486232012-07-19 14:46:51 +03001166 if ($escape === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 {
Andrey Andreev93dd2f22012-10-24 10:09:18 +03001168 $qb_orderby[] = array('field' => $orderby, 'direction' => $direction, 'escape' => FALSE);
Andrey Andreev2d486232012-07-19 14:46:51 +03001169 }
1170 else
1171 {
1172 $qb_orderby = array();
1173 foreach (explode(',', $orderby) as $field)
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001175 $qb_orderby[] = ($direction === '' && preg_match('/\s+(ASC|DESC)$/i', rtrim($field), $match, PREG_OFFSET_CAPTURE))
1176 ? array('field' => ltrim(substr($field, 0, $match[0][1])), 'direction' => ' '.$match[1][0], 'escape' => TRUE)
1177 : array('field' => trim($field), 'direction' => $direction, 'escape' => TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 }
Barry Mienydd671972010-10-04 16:33:58 +02001180
Andrey Andreev2d486232012-07-19 14:46:51 +03001181 $this->qb_orderby = array_merge($this->qb_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001182 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 {
Andrey Andreev2d486232012-07-19 14:46:51 +03001184 $this->qb_cache_orderby = array_merge($this->qb_cache_orderby, $qb_orderby);
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001185 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 }
1187
1188 return $this;
1189 }
Barry Mienydd671972010-10-04 16:33:58 +02001190
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 // --------------------------------------------------------------------
1192
1193 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001194 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001196 * @param int $value LIMIT value
1197 * @param int $offset OFFSET value
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 * @return object
1199 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001200 public function limit($value, $offset = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001201 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001202 is_null($value) OR $this->qb_limit = (int) $value;
1203 empty($offset) OR $this->qb_offset = (int) $offset;
Barry Mienydd671972010-10-04 16:33:58 +02001204
Derek Allard2067d1a2008-11-13 22:59:24 +00001205 return $this;
1206 }
Barry Mienydd671972010-10-04 16:33:58 +02001207
Derek Allard2067d1a2008-11-13 22:59:24 +00001208 // --------------------------------------------------------------------
1209
1210 /**
1211 * Sets the OFFSET value
1212 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001213 * @param int $offset OFFSET value
Derek Allard2067d1a2008-11-13 22:59:24 +00001214 * @return object
1215 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001216 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001217 {
Andrey Andreev777153d2012-06-18 13:30:45 +03001218 empty($offset) OR $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 return $this;
1220 }
Barry Mienydd671972010-10-04 16:33:58 +02001221
Derek Allard2067d1a2008-11-13 22:59:24 +00001222 // --------------------------------------------------------------------
1223
1224 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001225 * LIMIT string
Andrey Andreev2c35b642012-06-24 03:05:26 +03001226 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001227 * Generates a platform-specific LIMIT clause.
Andrey Andreev2c35b642012-06-24 03:05:26 +03001228 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001229 * @param string $sql SQL Query
Andrey Andreev2c35b642012-06-24 03:05:26 +03001230 * @return string
1231 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001232 protected function _limit($sql)
Andrey Andreev2c35b642012-06-24 03:05:26 +03001233 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03001234 return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').$this->qb_limit;
Andrey Andreev2c35b642012-06-24 03:05:26 +03001235 }
1236
1237 // --------------------------------------------------------------------
1238
1239 /**
Andrey Andreevfe642da2012-06-16 03:47:33 +03001240 * The "set" function.
1241 *
1242 * Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001244 * @param mixed
1245 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001246 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 * @return object
1248 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001249 public function set($key, $value = '', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001250 {
1251 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001252
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 if ( ! is_array($key))
1254 {
1255 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001256 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001257
Andrey Andreevfe642da2012-06-16 03:47:33 +03001258 is_bool($escape) OR $escape = $this->_protect_identifiers;
1259
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 foreach ($key as $k => $v)
1261 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001262 $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape)
1263 ? $this->escape($v) : $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 }
Barry Mienydd671972010-10-04 16:33:58 +02001265
Derek Allard2067d1a2008-11-13 22:59:24 +00001266 return $this;
1267 }
WanWizard7219c072011-12-28 14:09:05 +01001268
Kyle Farris0c147b32011-08-26 02:29:31 -04001269 // --------------------------------------------------------------------
1270
1271 /**
1272 * Get SELECT query string
1273 *
1274 * Compiles a SELECT query string and returns the sql.
1275 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001276 * @param string the table name to select from (optional)
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001277 * @param bool TRUE: resets QB values; FALSE: leave QB vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001278 * @return string
1279 */
WanWizard7219c072011-12-28 14:09:05 +01001280 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001281 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001282 if ($table !== '')
kylefarris0a3176b2011-08-26 02:37:52 -04001283 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001284 $this->_track_aliases($table);
1285 $this->from($table);
1286 }
WanWizard7219c072011-12-28 14:09:05 +01001287
Andrey Andreev650b4c02012-06-11 12:07:15 +03001288 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001289
Kyle Farris0c147b32011-08-26 02:29:31 -04001290 if ($reset === TRUE)
1291 {
1292 $this->_reset_select();
1293 }
WanWizard7219c072011-12-28 14:09:05 +01001294
Kyle Farris0c147b32011-08-26 02:29:31 -04001295 return $select;
1296 }
WanWizard7219c072011-12-28 14:09:05 +01001297
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 // --------------------------------------------------------------------
1299
1300 /**
1301 * Get
1302 *
1303 * Compiles the select statement based on the other functions called
1304 * and runs the query
1305 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001306 * @param string the table
1307 * @param string the limit clause
1308 * @param string the offset clause
1309 * @return object
1310 */
Andrey Andreev650b4c02012-06-11 12:07:15 +03001311 public function get($table = '', $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001313 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 {
1315 $this->_track_aliases($table);
1316 $this->from($table);
1317 }
Barry Mienydd671972010-10-04 16:33:58 +02001318
Andrey Andreev650b4c02012-06-11 12:07:15 +03001319 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001320 {
1321 $this->limit($limit, $offset);
1322 }
Barry Mienydd671972010-10-04 16:33:58 +02001323
Andrey Andreev24276a32012-01-08 02:44:38 +02001324 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001325 $this->_reset_select();
1326 return $result;
1327 }
1328
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001329 // --------------------------------------------------------------------
1330
Derek Allard2067d1a2008-11-13 22:59:24 +00001331 /**
1332 * "Count All Results" query
1333 *
Barry Mienydd671972010-10-04 16:33:58 +02001334 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001335 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001336 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001337 * @param string
1338 * @return string
1339 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001340 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001342 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001343 {
1344 $this->_track_aliases($table);
1345 $this->from($table);
1346 }
Barry Mienydd671972010-10-04 16:33:58 +02001347
Andrey Andreevb05f5062012-10-26 12:01:02 +03001348 $result = ($this->qb_distinct === TRUE)
1349 ? $this->query($this->_count_string.$this->protect_identifiers('numrows')."\nFROM (\n".$this->_compile_select()."\n) CI_count_all_results")
1350 : $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001351 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001352
Purwandi1d160e72012-01-09 16:33:28 +07001353 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001355 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 }
1357
Purwandi1d160e72012-01-09 16:33:28 +07001358 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001359 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001360 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001361
Derek Allard2067d1a2008-11-13 22:59:24 +00001362 // --------------------------------------------------------------------
1363
1364 /**
1365 * Get_Where
1366 *
1367 * Allows the where clause, limit and offset to be added directly
1368 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001369 * @param string $table
1370 * @param string $where
1371 * @param int $limit
1372 * @param int $offset
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 * @return object
1374 */
Andrey Andreeveb22d542012-06-26 23:16:35 +03001375 public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001377 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001378 {
1379 $this->from($table);
1380 }
1381
1382 if ( ! is_null($where))
1383 {
1384 $this->where($where);
1385 }
Barry Mienydd671972010-10-04 16:33:58 +02001386
Andrey Andreev650b4c02012-06-11 12:07:15 +03001387 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001388 {
1389 $this->limit($limit, $offset);
1390 }
Barry Mienydd671972010-10-04 16:33:58 +02001391
Andrey Andreev24276a32012-01-08 02:44:38 +02001392 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001393 $this->_reset_select();
1394 return $result;
1395 }
1396
1397 // --------------------------------------------------------------------
1398
1399 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001400 * Insert_Batch
1401 *
1402 * Compiles batch insert strings and runs the queries
1403 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001404 * @param string $table Table to insert into
1405 * @param array $set An associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001406 * @param bool $escape Whether to escape values and identifiers
Andrey Andreevae85eb42012-11-02 01:42:31 +02001407 * @return int Number of rows inserted or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001408 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001409 public function insert_batch($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001410 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001411 if ( ! is_null($set))
1412 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001413 $this->set_insert_batch($set, '', $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001414 }
Barry Mienydd671972010-10-04 16:33:58 +02001415
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001416 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001417 {
Andrey Andreev9f808b02012-10-24 17:38:48 +03001418 // No valid data array. Folds in cases where keys and values did not match up
1419 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001420 }
1421
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001422 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001423 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001424 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001425 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001426 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001427 }
Barry Mienydd671972010-10-04 16:33:58 +02001428
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001429 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001430 }
1431
1432 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001433 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001434 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001435 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001436 $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 +03001437 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001438 }
Barry Mienydd671972010-10-04 16:33:58 +02001439
Derek Jonesd10e8962010-03-02 17:10:36 -06001440 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001441 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001442 }
1443
1444 // --------------------------------------------------------------------
1445
1446 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001447 * Insert_batch statement
1448 *
1449 * Generates a platform-specific insert string from the supplied data.
1450 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001451 * @param string the table name
1452 * @param array the insert keys
1453 * @param array the insert values
1454 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001455 */
1456 protected function _insert_batch($table, $keys, $values)
1457 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001458 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001459 }
1460
1461 // --------------------------------------------------------------------
1462
1463 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001464 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001465 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001466 * @param mixed
1467 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001468 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001469 * @return object
1470 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001471 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001472 {
1473 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001474
Derek Jonesd10e8962010-03-02 17:10:36 -06001475 if ( ! is_array($key))
1476 {
1477 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001478 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001479
Andrey Andreevfe642da2012-06-16 03:47:33 +03001480 is_bool($escape) OR $escape = $this->_protect_identifiers;
1481
Iban Eguia3c0a4522012-04-15 13:30:44 +02001482 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001483 sort($keys);
1484
1485 foreach ($key as $row)
1486 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001487 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001488 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1489 {
1490 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001491 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001492 return;
1493 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001494
Barry Mienydd671972010-10-04 16:33:58 +02001495 ksort($row); // puts $row in the same order as our keys
1496
Andrey Andreev650b4c02012-06-11 12:07:15 +03001497 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001498 {
1499 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001500 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001501 {
Barry Mienydd671972010-10-04 16:33:58 +02001502 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001503 }
1504
Andrey Andreev650b4c02012-06-11 12:07:15 +03001505 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001506 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001507
1508 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001509 }
1510
1511 foreach ($keys as $k)
1512 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001513 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001514 }
Barry Mienydd671972010-10-04 16:33:58 +02001515
Derek Jonesd10e8962010-03-02 17:10:36 -06001516 return $this;
1517 }
WanWizard7219c072011-12-28 14:09:05 +01001518
Kyle Farris0c147b32011-08-26 02:29:31 -04001519 // --------------------------------------------------------------------
1520
1521 /**
1522 * Get INSERT query string
1523 *
1524 * Compiles an insert query and returns the sql
1525 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001526 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001527 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001528 * @return string
1529 */
1530 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001531 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001532 if ($this->_validate_insert($table) === FALSE)
1533 {
1534 return FALSE;
1535 }
WanWizard7219c072011-12-28 14:09:05 +01001536
Kyle Farris76116012011-08-31 11:17:48 -04001537 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001538 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001539 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001540 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001541 array_keys($this->qb_set),
1542 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001543 );
WanWizard7219c072011-12-28 14:09:05 +01001544
Kyle Farris0c147b32011-08-26 02:29:31 -04001545 if ($reset === TRUE)
1546 {
1547 $this->_reset_write();
1548 }
WanWizard7219c072011-12-28 14:09:05 +01001549
Kyle Farris0c147b32011-08-26 02:29:31 -04001550 return $sql;
1551 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001552
Derek Allard2067d1a2008-11-13 22:59:24 +00001553 // --------------------------------------------------------------------
1554
1555 /**
1556 * Insert
1557 *
1558 * Compiles an insert string and runs the query
1559 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001560 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001561 * @param array an associative array of insert values
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001562 * @param bool $escape Whether to escape values and identifiers
Derek Allard2067d1a2008-11-13 22:59:24 +00001563 * @return object
1564 */
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001565 public function insert($table = '', $set = NULL, $escape = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001566 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001567 if ( ! is_null($set))
1568 {
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001569 $this->set($set, '', $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001570 }
WanWizard7219c072011-12-28 14:09:05 +01001571
Kyle Farris0c147b32011-08-26 02:29:31 -04001572 if ($this->_validate_insert($table) === FALSE)
1573 {
1574 return FALSE;
1575 }
WanWizard7219c072011-12-28 14:09:05 +01001576
Kyle Farris76116012011-08-31 11:17:48 -04001577 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001578 $this->protect_identifiers(
Andrey Andreevbdd9c112012-11-06 12:07:16 +02001579 $this->qb_from[0], TRUE, $escape, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001580 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001581 array_keys($this->qb_set),
1582 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001583 );
Barry Mienydd671972010-10-04 16:33:58 +02001584
Kyle Farris0c147b32011-08-26 02:29:31 -04001585 $this->_reset_write();
1586 return $this->query($sql);
1587 }
WanWizard7219c072011-12-28 14:09:05 +01001588
Kyle Farris0c147b32011-08-26 02:29:31 -04001589 // --------------------------------------------------------------------
1590
1591 /**
1592 * Validate Insert
1593 *
1594 * This method is used by both insert() and get_compiled_insert() to
1595 * validate that the there data is actually being set and that table
1596 * has been chosen to be inserted into.
1597 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001598 * @param string the table to insert data into
1599 * @return string
1600 */
WanWizard7219c072011-12-28 14:09:05 +01001601 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001602 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001603 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001604 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001605 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001606 }
1607
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001608 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001609 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001610 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001611 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001612 elseif ( ! isset($this->qb_from[0]))
1613 {
1614 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1615 }
WanWizard7219c072011-12-28 14:09:05 +01001616
Kyle Farris0c147b32011-08-26 02:29:31 -04001617 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001618 }
Barry Mienydd671972010-10-04 16:33:58 +02001619
Phil Sturgeon9789f322011-07-15 15:14:05 -06001620 // --------------------------------------------------------------------
1621
1622 /**
1623 * Replace
1624 *
1625 * Compiles an replace into string and runs the query
1626 *
1627 * @param string the table to replace data into
1628 * @param array an associative array of insert values
1629 * @return object
1630 */
1631 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001632 {
1633 if ( ! is_null($set))
1634 {
1635 $this->set($set);
1636 }
Barry Mienydd671972010-10-04 16:33:58 +02001637
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001638 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001639 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001640 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001641 }
1642
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001643 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001644 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001645 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001646 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001647 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001648 }
Barry Mienydd671972010-10-04 16:33:58 +02001649
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001650 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001651 }
1652
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001653 $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 +00001654
Derek Jonesd10e8962010-03-02 17:10:36 -06001655 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001656 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001657 }
WanWizard7219c072011-12-28 14:09:05 +01001658
Kyle Farris0c147b32011-08-26 02:29:31 -04001659 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001660
Kyle Farris0c147b32011-08-26 02:29:31 -04001661 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001662 * Replace statement
1663 *
1664 * Generates a platform-specific replace string from the supplied data
1665 *
1666 * @param string the table name
1667 * @param array the insert keys
1668 * @param array the insert values
1669 * @return string
1670 */
1671 protected function _replace($table, $keys, $values)
1672 {
1673 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1674 }
1675
1676 // --------------------------------------------------------------------
1677
1678 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001679 * FROM tables
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001680 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001681 * Groups tables in FROM clauses if needed, so there is no confusion
1682 * about operator precedence.
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001683 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001684 * Note: This is only used (and overriden) by MySQL and CUBRID.
1685 *
1686 * @return string
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001687 */
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001688 protected function _from_tables()
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001689 {
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001690 return implode(', ', $this->qb_from);
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001691 }
1692
1693 // --------------------------------------------------------------------
1694
1695 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001696 * Get UPDATE query string
1697 *
1698 * Compiles an update query and returns the sql
1699 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001700 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001701 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001702 * @return string
1703 */
1704 public function get_compiled_update($table = '', $reset = TRUE)
1705 {
1706 // Combine any cached components with the current statements
1707 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001708
Kyle Farris0c147b32011-08-26 02:29:31 -04001709 if ($this->_validate_update($table) === FALSE)
1710 {
1711 return FALSE;
1712 }
WanWizard7219c072011-12-28 14:09:05 +01001713
Andrey Andreevb0478652012-07-18 15:34:46 +03001714 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001715
Kyle Farris0c147b32011-08-26 02:29:31 -04001716 if ($reset === TRUE)
1717 {
1718 $this->_reset_write();
1719 }
WanWizard7219c072011-12-28 14:09:05 +01001720
Kyle Farris0c147b32011-08-26 02:29:31 -04001721 return $sql;
1722 }
WanWizard7219c072011-12-28 14:09:05 +01001723
Derek Allard2067d1a2008-11-13 22:59:24 +00001724 // --------------------------------------------------------------------
1725
1726 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001727 * UPDATE
Derek Allard2067d1a2008-11-13 22:59:24 +00001728 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001729 * Compiles an update string and runs the query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001730 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001731 * @param string $table
1732 * @param array $set An associative array of update values
1733 * @param mixed $where
1734 * @param int $limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001735 * @return object
1736 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001737 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001738 {
1739 // Combine any cached components with the current statements
1740 $this->_merge_cache();
1741
1742 if ( ! is_null($set))
1743 {
1744 $this->set($set);
1745 }
Barry Mienydd671972010-10-04 16:33:58 +02001746
Kyle Farris0c147b32011-08-26 02:29:31 -04001747 if ($this->_validate_update($table) === FALSE)
1748 {
1749 return FALSE;
1750 }
1751
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001752 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001753 {
1754 $this->where($where);
1755 }
1756
Andrey Andreev650b4c02012-06-11 12:07:15 +03001757 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001758 {
1759 $this->limit($limit);
1760 }
1761
Andrey Andreevb0478652012-07-18 15:34:46 +03001762 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
Kyle Farris0c147b32011-08-26 02:29:31 -04001763 $this->_reset_write();
1764 return $this->query($sql);
1765 }
WanWizard7219c072011-12-28 14:09:05 +01001766
Kyle Farris0c147b32011-08-26 02:29:31 -04001767 // --------------------------------------------------------------------
1768
1769 /**
1770 * Validate Update
1771 *
1772 * This method is used by both update() and get_compiled_update() to
1773 * validate that data is actually being set and that a table has been
1774 * chosen to be update.
1775 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001776 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001777 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001778 */
1779 protected function _validate_update($table = '')
1780 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001781 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001782 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001783 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001784 }
1785
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001786 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001787 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001788 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001790 elseif ( ! isset($this->qb_from[0]))
1791 {
1792 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1793 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001794
1795 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001796 }
WanWizard7219c072011-12-28 14:09:05 +01001797
Derek Jonesd10e8962010-03-02 17:10:36 -06001798 // --------------------------------------------------------------------
1799
1800 /**
1801 * Update_Batch
1802 *
1803 * Compiles an update string and runs the query
1804 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001805 * @param string the table to retrieve the results from
1806 * @param array an associative array of update values
1807 * @param string the where key
Andrey Andreev9f808b02012-10-24 17:38:48 +03001808 * @return int number of rows affected or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001809 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001810 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001811 {
1812 // Combine any cached components with the current statements
1813 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001814
Derek Jonesd10e8962010-03-02 17:10:36 -06001815 if (is_null($index))
1816 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001817 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001818 }
1819
1820 if ( ! is_null($set))
1821 {
1822 $this->set_update_batch($set, $index);
1823 }
1824
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001825 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001826 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001827 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001828 }
1829
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001830 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001831 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001832 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001833 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001834 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001835 }
Barry Mienydd671972010-10-04 16:33:58 +02001836
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001837 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001838 }
Barry Mienydd671972010-10-04 16:33:58 +02001839
Derek Jonesd10e8962010-03-02 17:10:36 -06001840 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001841 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001842 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001843 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001844 $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 +03001845 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001846 }
Barry Mienydd671972010-10-04 16:33:58 +02001847
Derek Jonesd10e8962010-03-02 17:10:36 -06001848 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001849 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001850 }
1851
1852 // --------------------------------------------------------------------
1853
1854 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001855 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001856 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001857 * @param array
1858 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001859 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001860 * @return object
1861 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001862 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001863 {
1864 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001865
Derek Jonesd10e8962010-03-02 17:10:36 -06001866 if ( ! is_array($key))
1867 {
1868 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001869 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001870
Andrey Andreevfe642da2012-06-16 03:47:33 +03001871 is_bool($escape) OR $escape = $this->_protect_identifiers;
1872
Derek Jonesd10e8962010-03-02 17:10:36 -06001873 foreach ($key as $k => $v)
1874 {
1875 $index_set = FALSE;
1876 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001877 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001878 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001879 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001880 {
1881 $index_set = TRUE;
1882 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001883
Andrey Andreevfe642da2012-06-16 03:47:33 +03001884 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001885 }
1886
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001887 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001888 {
1889 return $this->display_error('db_batch_missing_index');
1890 }
1891
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001892 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001893 }
Barry Mienydd671972010-10-04 16:33:58 +02001894
Derek Jonesd10e8962010-03-02 17:10:36 -06001895 return $this;
1896 }
1897
Derek Allard2067d1a2008-11-13 22:59:24 +00001898 // --------------------------------------------------------------------
1899
1900 /**
1901 * Empty Table
1902 *
1903 * Compiles a delete string and runs "DELETE FROM table"
1904 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001905 * @param string the table to empty
1906 * @return object
1907 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001908 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001909 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001910 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001911 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001912 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001913 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001914 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001915 }
1916
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001917 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001918 }
1919 else
1920 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001921 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001922 }
1923
1924 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001925 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001926 return $this->query($sql);
1927 }
1928
1929 // --------------------------------------------------------------------
1930
1931 /**
1932 * Truncate
1933 *
1934 * Compiles a truncate string and runs the query
1935 * If the database does not support the truncate() command
1936 * This function maps to "DELETE FROM table"
1937 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001938 * @param string the table to truncate
1939 * @return object
1940 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001941 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001942 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001943 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001944 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001945 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001946 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001947 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001948 }
1949
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001950 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001951 }
1952 else
1953 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001954 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001955 }
1956
1957 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001958 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001959 return $this->query($sql);
1960 }
WanWizard7219c072011-12-28 14:09:05 +01001961
Kyle Farris0c147b32011-08-26 02:29:31 -04001962 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001963
Kyle Farris0c147b32011-08-26 02:29:31 -04001964 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001965 * Truncate statement
1966 *
1967 * Generates a platform-specific truncate string from the supplied data
1968 *
1969 * If the database does not support the truncate() command,
1970 * then this method maps to 'DELETE FROM table'
1971 *
1972 * @param string the table name
1973 * @return string
1974 */
1975 protected function _truncate($table)
1976 {
1977 return 'TRUNCATE '.$table;
1978 }
1979
1980 // --------------------------------------------------------------------
1981
1982 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001983 * Get DELETE query string
1984 *
1985 * Compiles a delete query string and returns the sql
1986 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001987 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001988 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001989 * @return string
1990 */
1991 public function get_compiled_delete($table = '', $reset = TRUE)
1992 {
1993 $this->return_delete_sql = TRUE;
1994 $sql = $this->delete($table, '', NULL, $reset);
1995 $this->return_delete_sql = FALSE;
1996 return $sql;
1997 }
WanWizard7219c072011-12-28 14:09:05 +01001998
Derek Allard2067d1a2008-11-13 22:59:24 +00001999 // --------------------------------------------------------------------
2000
2001 /**
2002 * Delete
2003 *
2004 * Compiles a delete string and runs the query
2005 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002006 * @param mixed the table(s) to delete from. String or array
2007 * @param mixed the where clause
2008 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03002009 * @param bool
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002010 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00002011 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002012 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002013 {
2014 // Combine any cached components with the current statements
2015 $this->_merge_cache();
2016
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002017 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002018 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002019 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002020 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002021 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002022 }
2023
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002024 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002025 }
2026 elseif (is_array($table))
2027 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05002028 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002029 {
Andrey Andreev13f50542012-10-12 12:31:02 +03002030 $this->delete($single_table, $where, $limit, $reset_data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002031 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002032 return;
2033 }
2034 else
2035 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002036 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002037 }
2038
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002039 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002040 {
2041 $this->where($where);
2042 }
2043
Andrey Andreev650b4c02012-06-11 12:07:15 +03002044 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002045 {
2046 $this->limit($limit);
2047 }
2048
Andrey Andreev94611df2012-07-19 12:29:54 +03002049 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002050 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002051 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02002052 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002053
Andrey Andreevb0478652012-07-18 15:34:46 +03002054 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002055 if ($reset_data)
2056 {
2057 $this->_reset_write();
2058 }
WanWizard7219c072011-12-28 14:09:05 +01002059
Andrey Andreev24276a32012-01-08 02:44:38 +02002060 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00002061 }
WanWizard7219c072011-12-28 14:09:05 +01002062
Derek Allard2067d1a2008-11-13 22:59:24 +00002063 // --------------------------------------------------------------------
2064
2065 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03002066 * Delete statement
2067 *
2068 * Generates a platform-specific delete string from the supplied data
2069 *
2070 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03002071 * @return string
2072 */
Andrey Andreevb0478652012-07-18 15:34:46 +03002073 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03002074 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002075 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002076 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03002077 }
2078
2079 // --------------------------------------------------------------------
2080
2081 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002082 * DB Prefix
2083 *
2084 * Prepends a database prefix if one exists in configuration
2085 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002086 * @param string the table
2087 * @return string
2088 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002089 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002090 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002091 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002092 {
2093 $this->display_error('db_table_name_required');
2094 }
2095
2096 return $this->dbprefix.$table;
2097 }
2098
2099 // --------------------------------------------------------------------
2100
2101 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06002102 * Set DB Prefix
2103 *
2104 * Set's the DB Prefix to something new without needing to reconnect
2105 *
2106 * @param string the prefix
2107 * @return string
2108 */
2109 public function set_dbprefix($prefix = '')
2110 {
2111 return $this->dbprefix = $prefix;
2112 }
2113
2114 // --------------------------------------------------------------------
2115
2116 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002117 * Track Aliases
2118 *
2119 * Used to track SQL statements written with aliased tables.
2120 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002121 * @param string The table to inspect
2122 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002123 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002124 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002125 {
2126 if (is_array($table))
2127 {
2128 foreach ($table as $t)
2129 {
2130 $this->_track_aliases($t);
2131 }
2132 return;
2133 }
Barry Mienydd671972010-10-04 16:33:58 +02002134
Derek Jones37f4b9c2011-07-01 17:56:50 -05002135 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002136 // the string into discreet statements
2137 if (strpos($table, ',') !== FALSE)
2138 {
2139 return $this->_track_aliases(explode(',', $table));
2140 }
Barry Mienydd671972010-10-04 16:33:58 +02002141
Derek Allard2067d1a2008-11-13 22:59:24 +00002142 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002143 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002144 {
2145 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002146 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002147
Derek Allard2067d1a2008-11-13 22:59:24 +00002148 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002149 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002150
Derek Allard2067d1a2008-11-13 22:59:24 +00002151 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002152 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002153 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002154 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002155 }
2156 }
2157 }
WanWizard7219c072011-12-28 14:09:05 +01002158
Derek Allard2067d1a2008-11-13 22:59:24 +00002159 // --------------------------------------------------------------------
2160
2161 /**
2162 * Compile the SELECT statement
2163 *
2164 * Generates a query string based on which functions were used.
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002165 * Should not be called directly.
Derek Allard2067d1a2008-11-13 22:59:24 +00002166 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002167 * @param bool $select_override
Derek Allard2067d1a2008-11-13 22:59:24 +00002168 * @return string
2169 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002170 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002171 {
2172 // Combine any cached components with the current statements
2173 $this->_merge_cache();
2174
Derek Allard2067d1a2008-11-13 22:59:24 +00002175 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002176 if ($select_override !== FALSE)
2177 {
2178 $sql = $select_override;
2179 }
2180 else
2181 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002182 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002183
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002184 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002185 {
Barry Mienydd671972010-10-04 16:33:58 +02002186 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002187 }
2188 else
Barry Mienydd671972010-10-04 16:33:58 +02002189 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002190 // Cycle through the "select" portion of the query and prep each column name.
2191 // The reason we protect identifiers here rather then in the select() function
2192 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002193 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002194 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002195 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002196 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002197 }
Barry Mienydd671972010-10-04 16:33:58 +02002198
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002199 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002200 }
2201 }
2202
Derek Allard2067d1a2008-11-13 22:59:24 +00002203 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002204 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002205 {
Andrey Andreeve78f8152012-10-09 11:38:38 +03002206 $sql .= "\nFROM ".$this->_from_tables();
Derek Allard2067d1a2008-11-13 22:59:24 +00002207 }
2208
Derek Allard2067d1a2008-11-13 22:59:24 +00002209 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002210 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002211 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002212 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002213 }
2214
Andrey Andreev2d486232012-07-19 14:46:51 +03002215 $sql .= $this->_compile_wh('qb_where')
2216 .$this->_compile_group_by()
2217 .$this->_compile_wh('qb_having')
2218 .$this->_compile_order_by(); // ORDER BY
Andrey Andreevb0478652012-07-18 15:34:46 +03002219
Andrey Andreevd40459d2012-07-18 16:46:39 +03002220 // LIMIT
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002221 if ($this->qb_limit)
Derek Allard2067d1a2008-11-13 22:59:24 +00002222 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002223 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002224 }
2225
2226 return $sql;
2227 }
2228
2229 // --------------------------------------------------------------------
2230
2231 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002232 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002233 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002234 * Escapes identifiers in WHERE and HAVING statements at execution time.
2235 *
Andrey Andreevb0478652012-07-18 15:34:46 +03002236 * Required so that aliases are tracked properly, regardless of wether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002237 * where(), or_where(), having(), or_having are called prior to from(),
2238 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002239 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002240 * @param string $qb_key 'qb_where' or 'qb_having'
Andrey Andreevd40459d2012-07-18 16:46:39 +03002241 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002242 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002243 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002244 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002245 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002246 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002247 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002248 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002249 if ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002250 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002251 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002252 continue;
2253 }
2254
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002255 // Split multiple conditions
2256 $conditions = preg_split(
2257 '/(\s*AND\s+|\s*OR\s+)/i',
2258 $this->{$qb_key}[$i]['condition'],
2259 -1,
2260 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
2261 );
2262
2263 for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002264 {
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002265 if (($op = $this->_get_operator($conditions[$ci])) === FALSE
Andrey Andreeve4742582012-10-25 13:25:13 +03002266 OR ! preg_match('/^(\(?)(.*)('.preg_quote($op, '/').')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches))
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002267 {
2268 continue;
2269 }
2270
2271 // $matches = array(
2272 // 0 => '(test <= foo)', /* the whole thing */
2273 // 1 => '(', /* optional */
2274 // 2 => 'test', /* the field name */
2275 // 3 => ' <= ', /* $op */
2276 // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2277 // 5 => ')' /* optional */
2278 // );
Andrey Andreev082aa402012-10-22 19:41:55 +03002279
2280 if ( ! empty($matches[4]))
2281 {
2282 $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4]));
2283 $matches[4] = ' '.$matches[4];
2284 }
2285
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002286 $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2]))
2287 .' '.trim($matches[3]).$matches[4].$matches[5];
Andrey Andreev6e704752012-07-18 00:46:33 +03002288 }
2289
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002290 $this->{$qb_key}[$i] = implode('', $conditions);
Andrey Andreev6e704752012-07-18 00:46:33 +03002291 }
2292
Andrey Andreev9d3aa1b2012-10-12 12:14:09 +03002293 return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ")
2294 .implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002295 }
2296
Andrey Andreevb0478652012-07-18 15:34:46 +03002297 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002298 }
2299
2300 // --------------------------------------------------------------------
2301
2302 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002303 * Compile GROUP BY
2304 *
2305 * Escapes identifiers in GROUP BY statements at execution time.
2306 *
2307 * Required so that aliases are tracked properly, regardless of wether
2308 * group_by() is called prior to from(), join() and dbprefix is added
2309 * only if needed.
2310 *
2311 * @return string SQL statement
2312 */
2313 protected function _compile_group_by()
2314 {
2315 if (count($this->qb_groupby) > 0)
2316 {
Andrey Andreev96feb582012-07-19 13:12:34 +03002317 for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
2318 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002319 $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 +03002320 ? $this->qb_groupby[$i]['field']
Andrey Andreev13f50542012-10-12 12:31:02 +03002321 : $this->protect_identifiers($this->qb_groupby[$i]['field']);
Andrey Andreev96feb582012-07-19 13:12:34 +03002322 }
2323
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002324 return "\nGROUP BY ".implode(', ', $this->qb_groupby);
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002325 }
2326
2327 return '';
2328 }
2329
2330 // --------------------------------------------------------------------
2331
2332 /**
Andrey Andreev2d486232012-07-19 14:46:51 +03002333 * Compile ORDER BY
2334 *
2335 * Escapes identifiers in ORDER BY statements at execution time.
2336 *
2337 * Required so that aliases are tracked properly, regardless of wether
2338 * order_by() is called prior to from(), join() and dbprefix is added
2339 * only if needed.
2340 *
2341 * @return string SQL statement
2342 */
2343 protected function _compile_order_by()
2344 {
Andrey Andreeva53ea842012-10-23 12:44:09 +03002345 if (is_array($this->qb_orderby) && count($this->qb_orderby) > 0)
Andrey Andreev2d486232012-07-19 14:46:51 +03002346 {
Andrey Andreev2d486232012-07-19 14:46:51 +03002347 for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++)
2348 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002349 if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field']))
Andrey Andreev2d486232012-07-19 14:46:51 +03002350 {
Andrey Andreevfc043b32012-10-12 14:46:14 +03002351 $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']);
Andrey Andreev2d486232012-07-19 14:46:51 +03002352 }
2353
2354 $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction'];
2355 }
2356
Andrey Andreeva53ea842012-10-23 12:44:09 +03002357 return $this->qb_orderby = "\nORDER BY ".implode(', ', $this->qb_orderby);
2358 }
2359 elseif (is_string($this->qb_orderby))
2360 {
2361 return $this->qb_orderby;
Andrey Andreev2d486232012-07-19 14:46:51 +03002362 }
2363
2364 return '';
2365 }
2366
2367 // --------------------------------------------------------------------
2368
2369 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002370 * Object to Array
2371 *
2372 * Takes an object as input and converts the class variables to array key/vals
2373 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002374 * @param object
2375 * @return array
2376 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002377 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002378 {
2379 if ( ! is_object($object))
2380 {
2381 return $object;
2382 }
Barry Mienydd671972010-10-04 16:33:58 +02002383
Derek Allard2067d1a2008-11-13 22:59:24 +00002384 $array = array();
2385 foreach (get_object_vars($object) as $key => $val)
2386 {
2387 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002388 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002389 {
2390 $array[$key] = $val;
2391 }
2392 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002393
2394 return $array;
2395 }
Barry Mienydd671972010-10-04 16:33:58 +02002396
Derek Jonesd10e8962010-03-02 17:10:36 -06002397 // --------------------------------------------------------------------
2398
2399 /**
2400 * Object to Array
2401 *
2402 * Takes an object as input and converts the class variables to array key/vals
2403 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002404 * @param object
2405 * @return array
2406 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002407 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002408 {
2409 if ( ! is_object($object))
2410 {
2411 return $object;
2412 }
Barry Mienydd671972010-10-04 16:33:58 +02002413
Derek Jonesd10e8962010-03-02 17:10:36 -06002414 $array = array();
2415 $out = get_object_vars($object);
2416 $fields = array_keys($out);
2417
2418 foreach ($fields as $val)
2419 {
2420 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002421 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002422 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002423 $i = 0;
2424 foreach ($out[$val] as $data)
2425 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002426 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002427 }
2428 }
2429 }
2430
Derek Allard2067d1a2008-11-13 22:59:24 +00002431 return $array;
2432 }
Barry Mienydd671972010-10-04 16:33:58 +02002433
Derek Allard2067d1a2008-11-13 22:59:24 +00002434 // --------------------------------------------------------------------
2435
2436 /**
2437 * Start Cache
2438 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002439 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002440 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002441 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002442 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002443 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002444 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002445 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002446 }
2447
2448 // --------------------------------------------------------------------
2449
2450 /**
2451 * Stop Cache
2452 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002453 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002454 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002455 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002456 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002457 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002458 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002459 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002460 }
2461
2462 // --------------------------------------------------------------------
2463
2464 /**
2465 * Flush Cache
2466 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002467 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002468 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002469 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002470 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002471 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002472 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002473 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002474 'qb_cache_select' => array(),
2475 'qb_cache_from' => array(),
2476 'qb_cache_join' => array(),
2477 'qb_cache_where' => array(),
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002478 'qb_cache_groupby' => array(),
2479 'qb_cache_having' => array(),
2480 'qb_cache_orderby' => array(),
2481 'qb_cache_set' => array(),
2482 'qb_cache_exists' => array(),
2483 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002484 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002485 }
2486
2487 // --------------------------------------------------------------------
2488
2489 /**
2490 * Merge Cache
2491 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002492 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002493 * locally called ones.
2494 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002495 * @return void
2496 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002497 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002498 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002499 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002500 {
2501 return;
2502 }
2503
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002504 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002505 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002506 $qb_variable = 'qb_'.$val;
2507 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002508
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002509 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002510 {
2511 continue;
2512 }
2513
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002514 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002515 }
2516
2517 // If we are "protecting identifiers" we need to examine the "from"
2518 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002519 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002520 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002521 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002522 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002523
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002524 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002525 }
WanWizard7219c072011-12-28 14:09:05 +01002526
Kyle Farris0c147b32011-08-26 02:29:31 -04002527 // --------------------------------------------------------------------
2528
2529 /**
Andrey Andreev082aa402012-10-22 19:41:55 +03002530 * Is literal
2531 *
2532 * Determines if a string represents a literal value or a field name
2533 *
2534 * @param string
2535 * @return bool
2536 */
2537 protected function _is_literal($str)
2538 {
2539 $str = trim($str);
2540
2541 if (empty($str))
2542 {
2543 return TRUE;
2544 }
2545
2546 static $_str;
2547
2548 if (empty($_str))
2549 {
2550 $_str = ($this->_escape_char !== '"')
2551 ? array('"', "'") : array("'");
2552 }
2553
2554 return (ctype_digit($str) OR in_array($str[0], $_str, TRUE));
2555 }
2556
2557 // --------------------------------------------------------------------
2558
2559 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002560 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002561 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002562 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002563 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002564 * @return void
2565 */
2566 public function reset_query()
2567 {
2568 $this->_reset_select();
2569 $this->_reset_write();
2570 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002571
2572 // --------------------------------------------------------------------
2573
2574 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002575 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002576 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002577 * @param array An array of fields to reset
2578 * @return void
2579 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002580 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002581 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002582 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002583 {
Andrey Andreevae85eb42012-11-02 01:42:31 +02002584 $this->$item = $default_value;
Derek Allard2067d1a2008-11-13 22:59:24 +00002585 }
2586 }
2587
2588 // --------------------------------------------------------------------
2589
2590 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002591 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002592 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002593 * @return void
2594 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002595 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002596 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002597 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002598 'qb_select' => array(),
2599 'qb_from' => array(),
2600 'qb_join' => array(),
2601 'qb_where' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002602 'qb_groupby' => array(),
2603 'qb_having' => array(),
2604 'qb_orderby' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002605 'qb_aliased_tables' => array(),
2606 'qb_no_escape' => array(),
2607 'qb_distinct' => FALSE,
2608 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002609 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002610 )
2611 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002612 }
Barry Mienydd671972010-10-04 16:33:58 +02002613
Derek Allard2067d1a2008-11-13 22:59:24 +00002614 // --------------------------------------------------------------------
2615
2616 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002617 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002618 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002619 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002620 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002621 * @return void
2622 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002623 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002624 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002625 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002626 'qb_set' => array(),
2627 'qb_from' => array(),
2628 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002629 'qb_orderby' => array(),
2630 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002631 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002632 )
2633 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002634 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002635
Derek Allard2067d1a2008-11-13 22:59:24 +00002636}
2637
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002638/* End of file DB_query_builder.php */
Andrey Andreev58803fb2012-06-24 00:45:37 +03002639/* Location: ./system/database/DB_query_builder.php */