blob: 41b30aec32668634e966d96cf444b3974d9c0d5f [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
1406 * @return int Number of rows inserted or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001407 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001408 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001409 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001410 if ( ! is_null($set))
1411 {
1412 $this->set_insert_batch($set);
1413 }
Barry Mienydd671972010-10-04 16:33:58 +02001414
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001415 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001416 {
Andrey Andreev9f808b02012-10-24 17:38:48 +03001417 // No valid data array. Folds in cases where keys and values did not match up
1418 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001419 }
1420
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001421 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001422 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001423 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001424 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001425 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001426 }
Barry Mienydd671972010-10-04 16:33:58 +02001427
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001428 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001429 }
1430
1431 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001432 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001433 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001434 {
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001435 $this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, 100)));
Andrey Andreev9f808b02012-10-24 17:38:48 +03001436 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001437 }
Barry Mienydd671972010-10-04 16:33:58 +02001438
Derek Jonesd10e8962010-03-02 17:10:36 -06001439 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001440 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001441 }
1442
1443 // --------------------------------------------------------------------
1444
1445 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001446 * Insert_batch statement
1447 *
1448 * Generates a platform-specific insert string from the supplied data.
1449 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001450 * @param string the table name
1451 * @param array the insert keys
1452 * @param array the insert values
1453 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001454 */
1455 protected function _insert_batch($table, $keys, $values)
1456 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001457 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001458 }
1459
1460 // --------------------------------------------------------------------
1461
1462 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001463 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001464 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001465 * @param mixed
1466 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001467 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001468 * @return object
1469 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001470 public function set_insert_batch($key, $value = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001471 {
1472 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001473
Derek Jonesd10e8962010-03-02 17:10:36 -06001474 if ( ! is_array($key))
1475 {
1476 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001477 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001478
Andrey Andreevfe642da2012-06-16 03:47:33 +03001479 is_bool($escape) OR $escape = $this->_protect_identifiers;
1480
Iban Eguia3c0a4522012-04-15 13:30:44 +02001481 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001482 sort($keys);
1483
1484 foreach ($key as $row)
1485 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001486 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001487 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1488 {
1489 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001490 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001491 return;
1492 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001493
Barry Mienydd671972010-10-04 16:33:58 +02001494 ksort($row); // puts $row in the same order as our keys
1495
Andrey Andreev650b4c02012-06-11 12:07:15 +03001496 if ($escape !== FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001497 {
1498 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001499 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001500 {
Barry Mienydd671972010-10-04 16:33:58 +02001501 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001502 }
1503
Andrey Andreev650b4c02012-06-11 12:07:15 +03001504 $row = $clean;
Barry Mienydd671972010-10-04 16:33:58 +02001505 }
Andrey Andreev650b4c02012-06-11 12:07:15 +03001506
1507 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001508 }
1509
1510 foreach ($keys as $k)
1511 {
Andrey Andreevfe642da2012-06-16 03:47:33 +03001512 $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape);
Derek Jonesd10e8962010-03-02 17:10:36 -06001513 }
Barry Mienydd671972010-10-04 16:33:58 +02001514
Derek Jonesd10e8962010-03-02 17:10:36 -06001515 return $this;
1516 }
WanWizard7219c072011-12-28 14:09:05 +01001517
Kyle Farris0c147b32011-08-26 02:29:31 -04001518 // --------------------------------------------------------------------
1519
1520 /**
1521 * Get INSERT query string
1522 *
1523 * Compiles an insert query and returns the sql
1524 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001525 * @param string the table to insert into
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001526 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001527 * @return string
1528 */
1529 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001530 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001531 if ($this->_validate_insert($table) === FALSE)
1532 {
1533 return FALSE;
1534 }
WanWizard7219c072011-12-28 14:09:05 +01001535
Kyle Farris76116012011-08-31 11:17:48 -04001536 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001537 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001538 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001539 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001540 array_keys($this->qb_set),
1541 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001542 );
WanWizard7219c072011-12-28 14:09:05 +01001543
Kyle Farris0c147b32011-08-26 02:29:31 -04001544 if ($reset === TRUE)
1545 {
1546 $this->_reset_write();
1547 }
WanWizard7219c072011-12-28 14:09:05 +01001548
Kyle Farris0c147b32011-08-26 02:29:31 -04001549 return $sql;
1550 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001551
Derek Allard2067d1a2008-11-13 22:59:24 +00001552 // --------------------------------------------------------------------
1553
1554 /**
1555 * Insert
1556 *
1557 * Compiles an insert string and runs the query
1558 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001559 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001560 * @param array an associative array of insert values
1561 * @return object
1562 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001563 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001564 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001565 if ( ! is_null($set))
1566 {
1567 $this->set($set);
1568 }
WanWizard7219c072011-12-28 14:09:05 +01001569
Kyle Farris0c147b32011-08-26 02:29:31 -04001570 if ($this->_validate_insert($table) === FALSE)
1571 {
1572 return FALSE;
1573 }
WanWizard7219c072011-12-28 14:09:05 +01001574
Kyle Farris76116012011-08-31 11:17:48 -04001575 $sql = $this->_insert(
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001576 $this->protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001577 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001578 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001579 array_keys($this->qb_set),
1580 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001581 );
Barry Mienydd671972010-10-04 16:33:58 +02001582
Kyle Farris0c147b32011-08-26 02:29:31 -04001583 $this->_reset_write();
1584 return $this->query($sql);
1585 }
WanWizard7219c072011-12-28 14:09:05 +01001586
Kyle Farris0c147b32011-08-26 02:29:31 -04001587 // --------------------------------------------------------------------
1588
1589 /**
1590 * Validate Insert
1591 *
1592 * This method is used by both insert() and get_compiled_insert() to
1593 * validate that the there data is actually being set and that table
1594 * has been chosen to be inserted into.
1595 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001596 * @param string the table to insert data into
1597 * @return string
1598 */
WanWizard7219c072011-12-28 14:09:05 +01001599 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001600 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001601 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001602 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001603 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001604 }
1605
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001606 if ($table !== '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001607 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001608 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001609 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001610 elseif ( ! isset($this->qb_from[0]))
1611 {
1612 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1613 }
WanWizard7219c072011-12-28 14:09:05 +01001614
Kyle Farris0c147b32011-08-26 02:29:31 -04001615 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001616 }
Barry Mienydd671972010-10-04 16:33:58 +02001617
Phil Sturgeon9789f322011-07-15 15:14:05 -06001618 // --------------------------------------------------------------------
1619
1620 /**
1621 * Replace
1622 *
1623 * Compiles an replace into string and runs the query
1624 *
1625 * @param string the table to replace data into
1626 * @param array an associative array of insert values
1627 * @return object
1628 */
1629 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001630 {
1631 if ( ! is_null($set))
1632 {
1633 $this->set($set);
1634 }
Barry Mienydd671972010-10-04 16:33:58 +02001635
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001636 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001637 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001638 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001639 }
1640
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001641 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001642 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001643 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001644 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001645 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001646 }
Barry Mienydd671972010-10-04 16:33:58 +02001647
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001648 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001649 }
1650
Jamie Rumbelow0c092992012-03-06 22:05:16 +00001651 $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 +00001652
Derek Jonesd10e8962010-03-02 17:10:36 -06001653 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001654 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001655 }
WanWizard7219c072011-12-28 14:09:05 +01001656
Kyle Farris0c147b32011-08-26 02:29:31 -04001657 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001658
Kyle Farris0c147b32011-08-26 02:29:31 -04001659 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001660 * Replace statement
1661 *
1662 * Generates a platform-specific replace string from the supplied data
1663 *
1664 * @param string the table name
1665 * @param array the insert keys
1666 * @param array the insert values
1667 * @return string
1668 */
1669 protected function _replace($table, $keys, $values)
1670 {
1671 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1672 }
1673
1674 // --------------------------------------------------------------------
1675
1676 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001677 * FROM tables
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001678 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001679 * Groups tables in FROM clauses if needed, so there is no confusion
1680 * about operator precedence.
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001681 *
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001682 * Note: This is only used (and overriden) by MySQL and CUBRID.
1683 *
1684 * @return string
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001685 */
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001686 protected function _from_tables()
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001687 {
Andrey Andreev7eaa14f2012-10-09 11:34:01 +03001688 return implode(', ', $this->qb_from);
Andrey Andreevc78e56a2012-06-08 02:12:07 +03001689 }
1690
1691 // --------------------------------------------------------------------
1692
1693 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001694 * Get UPDATE query string
1695 *
1696 * Compiles an update query and returns the sql
1697 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001698 * @param string the table to update
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001699 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001700 * @return string
1701 */
1702 public function get_compiled_update($table = '', $reset = TRUE)
1703 {
1704 // Combine any cached components with the current statements
1705 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001706
Kyle Farris0c147b32011-08-26 02:29:31 -04001707 if ($this->_validate_update($table) === FALSE)
1708 {
1709 return FALSE;
1710 }
WanWizard7219c072011-12-28 14:09:05 +01001711
Andrey Andreevb0478652012-07-18 15:34:46 +03001712 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
WanWizard7219c072011-12-28 14:09:05 +01001713
Kyle Farris0c147b32011-08-26 02:29:31 -04001714 if ($reset === TRUE)
1715 {
1716 $this->_reset_write();
1717 }
WanWizard7219c072011-12-28 14:09:05 +01001718
Kyle Farris0c147b32011-08-26 02:29:31 -04001719 return $sql;
1720 }
WanWizard7219c072011-12-28 14:09:05 +01001721
Derek Allard2067d1a2008-11-13 22:59:24 +00001722 // --------------------------------------------------------------------
1723
1724 /**
Andrey Andreevae85eb42012-11-02 01:42:31 +02001725 * UPDATE
Derek Allard2067d1a2008-11-13 22:59:24 +00001726 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001727 * Compiles an update string and runs the query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001728 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02001729 * @param string $table
1730 * @param array $set An associative array of update values
1731 * @param mixed $where
1732 * @param int $limit
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 * @return object
1734 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001735 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001736 {
1737 // Combine any cached components with the current statements
1738 $this->_merge_cache();
1739
1740 if ( ! is_null($set))
1741 {
1742 $this->set($set);
1743 }
Barry Mienydd671972010-10-04 16:33:58 +02001744
Kyle Farris0c147b32011-08-26 02:29:31 -04001745 if ($this->_validate_update($table) === FALSE)
1746 {
1747 return FALSE;
1748 }
1749
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001750 if ($where !== NULL)
Kyle Farris0c147b32011-08-26 02:29:31 -04001751 {
1752 $this->where($where);
1753 }
1754
Andrey Andreev650b4c02012-06-11 12:07:15 +03001755 if ( ! empty($limit))
Kyle Farris0c147b32011-08-26 02:29:31 -04001756 {
1757 $this->limit($limit);
1758 }
1759
Andrey Andreevb0478652012-07-18 15:34:46 +03001760 $sql = $this->_update($this->protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set);
Kyle Farris0c147b32011-08-26 02:29:31 -04001761 $this->_reset_write();
1762 return $this->query($sql);
1763 }
WanWizard7219c072011-12-28 14:09:05 +01001764
Kyle Farris0c147b32011-08-26 02:29:31 -04001765 // --------------------------------------------------------------------
1766
1767 /**
1768 * Validate Update
1769 *
1770 * This method is used by both update() and get_compiled_update() to
1771 * validate that data is actually being set and that a table has been
1772 * chosen to be update.
1773 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001774 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001775 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001776 */
1777 protected function _validate_update($table = '')
1778 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001779 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001780 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001781 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001782 }
1783
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001784 if ($table !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001785 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001786 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001787 }
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001788 elseif ( ! isset($this->qb_from[0]))
1789 {
1790 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
1791 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001792
1793 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001794 }
WanWizard7219c072011-12-28 14:09:05 +01001795
Derek Jonesd10e8962010-03-02 17:10:36 -06001796 // --------------------------------------------------------------------
1797
1798 /**
1799 * Update_Batch
1800 *
1801 * Compiles an update string and runs the query
1802 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001803 * @param string the table to retrieve the results from
1804 * @param array an associative array of update values
1805 * @param string the where key
Andrey Andreev9f808b02012-10-24 17:38:48 +03001806 * @return int number of rows affected or FALSE on failure
Derek Jonesd10e8962010-03-02 17:10:36 -06001807 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001808 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001809 {
1810 // Combine any cached components with the current statements
1811 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001812
Derek Jonesd10e8962010-03-02 17:10:36 -06001813 if (is_null($index))
1814 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001815 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001816 }
1817
1818 if ( ! is_null($set))
1819 {
1820 $this->set_update_batch($set, $index);
1821 }
1822
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001823 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001824 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001825 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001826 }
1827
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001828 if ($table === '')
Derek Jonesd10e8962010-03-02 17:10:36 -06001829 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001830 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001831 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001832 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001833 }
Barry Mienydd671972010-10-04 16:33:58 +02001834
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001835 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001836 }
Barry Mienydd671972010-10-04 16:33:58 +02001837
Derek Jonesd10e8962010-03-02 17:10:36 -06001838 // Batch this baby
Andrey Andreev9f808b02012-10-24 17:38:48 +03001839 $affected_rows = 0;
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001840 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001841 {
Andrey Andreevb0478652012-07-18 15:34:46 +03001842 $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 +03001843 $affected_rows += $this->affected_rows();
Derek Jonesd10e8962010-03-02 17:10:36 -06001844 }
Barry Mienydd671972010-10-04 16:33:58 +02001845
Derek Jonesd10e8962010-03-02 17:10:36 -06001846 $this->_reset_write();
Andrey Andreev9f808b02012-10-24 17:38:48 +03001847 return $affected_rows;
Derek Jonesd10e8962010-03-02 17:10:36 -06001848 }
1849
1850 // --------------------------------------------------------------------
1851
1852 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001853 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001854 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001855 * @param array
1856 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001857 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001858 * @return object
1859 */
Andrey Andreevfe642da2012-06-16 03:47:33 +03001860 public function set_update_batch($key, $index = '', $escape = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001861 {
1862 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001863
Derek Jonesd10e8962010-03-02 17:10:36 -06001864 if ( ! is_array($key))
1865 {
1866 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001867 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001868
Andrey Andreevfe642da2012-06-16 03:47:33 +03001869 is_bool($escape) OR $escape = $this->_protect_identifiers;
1870
Derek Jonesd10e8962010-03-02 17:10:36 -06001871 foreach ($key as $k => $v)
1872 {
1873 $index_set = FALSE;
1874 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001875 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001876 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001877 if ($k2 === $index)
Derek Jonesd10e8962010-03-02 17:10:36 -06001878 {
1879 $index_set = TRUE;
1880 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001881
Andrey Andreevfe642da2012-06-16 03:47:33 +03001882 $clean[$this->protect_identifiers($k2, FALSE, $escape)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001883 }
1884
Andrey Andreev7b5eb732012-05-24 20:52:41 +03001885 if ($index_set === FALSE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001886 {
1887 return $this->display_error('db_batch_missing_index');
1888 }
1889
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001890 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001891 }
Barry Mienydd671972010-10-04 16:33:58 +02001892
Derek Jonesd10e8962010-03-02 17:10:36 -06001893 return $this;
1894 }
1895
Derek Allard2067d1a2008-11-13 22:59:24 +00001896 // --------------------------------------------------------------------
1897
1898 /**
1899 * Empty Table
1900 *
1901 * Compiles a delete string and runs "DELETE FROM table"
1902 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001903 * @param string the table to empty
1904 * @return object
1905 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001906 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001907 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001908 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001909 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001910 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001911 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001912 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001913 }
1914
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001915 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001916 }
1917 else
1918 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001919 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001920 }
1921
1922 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001923 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001924 return $this->query($sql);
1925 }
1926
1927 // --------------------------------------------------------------------
1928
1929 /**
1930 * Truncate
1931 *
1932 * Compiles a truncate string and runs the query
1933 * If the database does not support the truncate() command
1934 * This function maps to "DELETE FROM table"
1935 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001936 * @param string the table to truncate
1937 * @return object
1938 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001939 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001940 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01001941 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001942 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001943 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001944 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001945 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001946 }
1947
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001948 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001949 }
1950 else
1951 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001952 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001953 }
1954
1955 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001956 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001957 return $this->query($sql);
1958 }
WanWizard7219c072011-12-28 14:09:05 +01001959
Kyle Farris0c147b32011-08-26 02:29:31 -04001960 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001961
Kyle Farris0c147b32011-08-26 02:29:31 -04001962 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001963 * Truncate statement
1964 *
1965 * Generates a platform-specific truncate string from the supplied data
1966 *
1967 * If the database does not support the truncate() command,
1968 * then this method maps to 'DELETE FROM table'
1969 *
1970 * @param string the table name
1971 * @return string
1972 */
1973 protected function _truncate($table)
1974 {
1975 return 'TRUNCATE '.$table;
1976 }
1977
1978 // --------------------------------------------------------------------
1979
1980 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001981 * Get DELETE query string
1982 *
1983 * Compiles a delete query string and returns the sql
1984 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001985 * @param string the table to delete from
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01001986 * @param bool TRUE: reset QB values; FALSE: leave QB values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001987 * @return string
1988 */
1989 public function get_compiled_delete($table = '', $reset = TRUE)
1990 {
1991 $this->return_delete_sql = TRUE;
1992 $sql = $this->delete($table, '', NULL, $reset);
1993 $this->return_delete_sql = FALSE;
1994 return $sql;
1995 }
WanWizard7219c072011-12-28 14:09:05 +01001996
Derek Allard2067d1a2008-11-13 22:59:24 +00001997 // --------------------------------------------------------------------
1998
1999 /**
2000 * Delete
2001 *
2002 * Compiles a delete string and runs the query
2003 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002004 * @param mixed the table(s) to delete from. String or array
2005 * @param mixed the where clause
2006 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03002007 * @param bool
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002008 * @return mixed
Derek Allard2067d1a2008-11-13 22:59:24 +00002009 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002010 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002011 {
2012 // Combine any cached components with the current statements
2013 $this->_merge_cache();
2014
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002015 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002016 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002017 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00002018 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002019 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002020 }
2021
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002022 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00002023 }
2024 elseif (is_array($table))
2025 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05002026 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002027 {
Andrey Andreev13f50542012-10-12 12:31:02 +03002028 $this->delete($single_table, $where, $limit, $reset_data);
Derek Allard2067d1a2008-11-13 22:59:24 +00002029 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002030 return;
2031 }
2032 else
2033 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002034 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00002035 }
2036
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002037 if ($where !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002038 {
2039 $this->where($where);
2040 }
2041
Andrey Andreev650b4c02012-06-11 12:07:15 +03002042 if ( ! empty($limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00002043 {
2044 $this->limit($limit);
2045 }
2046
Andrey Andreev94611df2012-07-19 12:29:54 +03002047 if (count($this->qb_where) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002048 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002049 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02002050 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002051
Andrey Andreevb0478652012-07-18 15:34:46 +03002052 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 if ($reset_data)
2054 {
2055 $this->_reset_write();
2056 }
WanWizard7219c072011-12-28 14:09:05 +01002057
Andrey Andreev24276a32012-01-08 02:44:38 +02002058 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00002059 }
WanWizard7219c072011-12-28 14:09:05 +01002060
Derek Allard2067d1a2008-11-13 22:59:24 +00002061 // --------------------------------------------------------------------
2062
2063 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03002064 * Delete statement
2065 *
2066 * Generates a platform-specific delete string from the supplied data
2067 *
2068 * @param string the table name
Andrey Andreevc01d3162012-04-09 12:55:11 +03002069 * @return string
2070 */
Andrey Andreevb0478652012-07-18 15:34:46 +03002071 protected function _delete($table)
Andrey Andreevc01d3162012-04-09 12:55:11 +03002072 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002073 return 'DELETE FROM '.$table.$this->_compile_wh('qb_where')
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002074 .($this->qb_limit ? ' LIMIT '.$this->qb_limit : '');
Andrey Andreevc01d3162012-04-09 12:55:11 +03002075 }
2076
2077 // --------------------------------------------------------------------
2078
2079 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002080 * DB Prefix
2081 *
2082 * Prepends a database prefix if one exists in configuration
2083 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002084 * @param string the table
2085 * @return string
2086 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002087 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002088 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002089 if ($table === '')
Derek Allard2067d1a2008-11-13 22:59:24 +00002090 {
2091 $this->display_error('db_table_name_required');
2092 }
2093
2094 return $this->dbprefix.$table;
2095 }
2096
2097 // --------------------------------------------------------------------
2098
2099 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06002100 * Set DB Prefix
2101 *
2102 * Set's the DB Prefix to something new without needing to reconnect
2103 *
2104 * @param string the prefix
2105 * @return string
2106 */
2107 public function set_dbprefix($prefix = '')
2108 {
2109 return $this->dbprefix = $prefix;
2110 }
2111
2112 // --------------------------------------------------------------------
2113
2114 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002115 * Track Aliases
2116 *
2117 * Used to track SQL statements written with aliased tables.
2118 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002119 * @param string The table to inspect
2120 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02002121 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002122 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00002123 {
2124 if (is_array($table))
2125 {
2126 foreach ($table as $t)
2127 {
2128 $this->_track_aliases($t);
2129 }
2130 return;
2131 }
Barry Mienydd671972010-10-04 16:33:58 +02002132
Derek Jones37f4b9c2011-07-01 17:56:50 -05002133 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00002134 // the string into discreet statements
2135 if (strpos($table, ',') !== FALSE)
2136 {
2137 return $this->_track_aliases(explode(',', $table));
2138 }
Barry Mienydd671972010-10-04 16:33:58 +02002139
Derek Allard2067d1a2008-11-13 22:59:24 +00002140 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02002141 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002142 {
2143 // if the alias is written with the AS keyword, remove it
Andrey Andreev5a257182012-06-10 06:18:14 +03002144 $table = preg_replace('/\s+AS\s+/i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02002145
Derek Allard2067d1a2008-11-13 22:59:24 +00002146 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02002147 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02002148
Derek Allard2067d1a2008-11-13 22:59:24 +00002149 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002150 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00002151 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002152 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00002153 }
2154 }
2155 }
WanWizard7219c072011-12-28 14:09:05 +01002156
Derek Allard2067d1a2008-11-13 22:59:24 +00002157 // --------------------------------------------------------------------
2158
2159 /**
2160 * Compile the SELECT statement
2161 *
2162 * Generates a query string based on which functions were used.
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03002163 * Should not be called directly.
Derek Allard2067d1a2008-11-13 22:59:24 +00002164 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002165 * @param bool $select_override
Derek Allard2067d1a2008-11-13 22:59:24 +00002166 * @return string
2167 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002168 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00002169 {
2170 // Combine any cached components with the current statements
2171 $this->_merge_cache();
2172
Derek Allard2067d1a2008-11-13 22:59:24 +00002173 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002174 if ($select_override !== FALSE)
2175 {
2176 $sql = $select_override;
2177 }
2178 else
2179 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002180 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002181
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002182 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002183 {
Barry Mienydd671972010-10-04 16:33:58 +02002184 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002185 }
2186 else
Barry Mienydd671972010-10-04 16:33:58 +02002187 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002188 // Cycle through the "select" portion of the query and prep each column name.
2189 // The reason we protect identifiers here rather then in the select() function
2190 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002191 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002192 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002193 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
Jamie Rumbelow0c092992012-03-06 22:05:16 +00002194 $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002195 }
Barry Mienydd671972010-10-04 16:33:58 +02002196
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002197 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00002198 }
2199 }
2200
Derek Allard2067d1a2008-11-13 22:59:24 +00002201 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002202 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002203 {
Andrey Andreeve78f8152012-10-09 11:38:38 +03002204 $sql .= "\nFROM ".$this->_from_tables();
Derek Allard2067d1a2008-11-13 22:59:24 +00002205 }
2206
Derek Allard2067d1a2008-11-13 22:59:24 +00002207 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002208 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002209 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002210 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002211 }
2212
Andrey Andreev2d486232012-07-19 14:46:51 +03002213 $sql .= $this->_compile_wh('qb_where')
2214 .$this->_compile_group_by()
2215 .$this->_compile_wh('qb_having')
2216 .$this->_compile_order_by(); // ORDER BY
Andrey Andreevb0478652012-07-18 15:34:46 +03002217
Andrey Andreevd40459d2012-07-18 16:46:39 +03002218 // LIMIT
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002219 if ($this->qb_limit)
Derek Allard2067d1a2008-11-13 22:59:24 +00002220 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002221 return $this->_limit($sql."\n");
Derek Allard2067d1a2008-11-13 22:59:24 +00002222 }
2223
2224 return $sql;
2225 }
2226
2227 // --------------------------------------------------------------------
2228
2229 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +03002230 * Compile WHERE, HAVING statements
Andrey Andreev6e704752012-07-18 00:46:33 +03002231 *
Andrey Andreevd40459d2012-07-18 16:46:39 +03002232 * Escapes identifiers in WHERE and HAVING statements at execution time.
2233 *
Andrey Andreevb0478652012-07-18 15:34:46 +03002234 * Required so that aliases are tracked properly, regardless of wether
Andrey Andreevd40459d2012-07-18 16:46:39 +03002235 * where(), or_where(), having(), or_having are called prior to from(),
2236 * join() and dbprefix is added only if needed.
Andrey Andreev6e704752012-07-18 00:46:33 +03002237 *
Andrey Andreevae85eb42012-11-02 01:42:31 +02002238 * @param string $qb_key 'qb_where' or 'qb_having'
Andrey Andreevd40459d2012-07-18 16:46:39 +03002239 * @return string SQL statement
Andrey Andreev6e704752012-07-18 00:46:33 +03002240 */
Andrey Andreevd40459d2012-07-18 16:46:39 +03002241 protected function _compile_wh($qb_key)
Andrey Andreev6e704752012-07-18 00:46:33 +03002242 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002243 if (count($this->$qb_key) > 0)
Andrey Andreev6e704752012-07-18 00:46:33 +03002244 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002245 for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002246 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002247 if ($this->{$qb_key}[$i]['escape'] === FALSE)
Andrey Andreev6e704752012-07-18 00:46:33 +03002248 {
Andrey Andreevd40459d2012-07-18 16:46:39 +03002249 $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'];
Andrey Andreev6e704752012-07-18 00:46:33 +03002250 continue;
2251 }
2252
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002253 // Split multiple conditions
2254 $conditions = preg_split(
2255 '/(\s*AND\s+|\s*OR\s+)/i',
2256 $this->{$qb_key}[$i]['condition'],
2257 -1,
2258 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
2259 );
2260
2261 for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++)
Andrey Andreev6e704752012-07-18 00:46:33 +03002262 {
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002263 if (($op = $this->_get_operator($conditions[$ci])) === FALSE
Andrey Andreeve4742582012-10-25 13:25:13 +03002264 OR ! preg_match('/^(\(?)(.*)('.preg_quote($op, '/').')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches))
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002265 {
2266 continue;
2267 }
2268
2269 // $matches = array(
2270 // 0 => '(test <= foo)', /* the whole thing */
2271 // 1 => '(', /* optional */
2272 // 2 => 'test', /* the field name */
2273 // 3 => ' <= ', /* $op */
2274 // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */
2275 // 5 => ')' /* optional */
2276 // );
Andrey Andreev082aa402012-10-22 19:41:55 +03002277
2278 if ( ! empty($matches[4]))
2279 {
2280 $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4]));
2281 $matches[4] = ' '.$matches[4];
2282 }
2283
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002284 $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2]))
2285 .' '.trim($matches[3]).$matches[4].$matches[5];
Andrey Andreev6e704752012-07-18 00:46:33 +03002286 }
2287
Andrey Andreevf2ec8b82012-10-12 14:01:13 +03002288 $this->{$qb_key}[$i] = implode('', $conditions);
Andrey Andreev6e704752012-07-18 00:46:33 +03002289 }
2290
Andrey Andreev9d3aa1b2012-10-12 12:14:09 +03002291 return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ")
2292 .implode("\n", $this->$qb_key);
Andrey Andreev6e704752012-07-18 00:46:33 +03002293 }
2294
Andrey Andreevb0478652012-07-18 15:34:46 +03002295 return '';
Andrey Andreev6e704752012-07-18 00:46:33 +03002296 }
2297
2298 // --------------------------------------------------------------------
2299
2300 /**
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002301 * Compile GROUP BY
2302 *
2303 * Escapes identifiers in GROUP BY statements at execution time.
2304 *
2305 * Required so that aliases are tracked properly, regardless of wether
2306 * group_by() is called prior to from(), join() and dbprefix is added
2307 * only if needed.
2308 *
2309 * @return string SQL statement
2310 */
2311 protected function _compile_group_by()
2312 {
2313 if (count($this->qb_groupby) > 0)
2314 {
Andrey Andreev96feb582012-07-19 13:12:34 +03002315 for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++)
2316 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002317 $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 +03002318 ? $this->qb_groupby[$i]['field']
Andrey Andreev13f50542012-10-12 12:31:02 +03002319 : $this->protect_identifiers($this->qb_groupby[$i]['field']);
Andrey Andreev96feb582012-07-19 13:12:34 +03002320 }
2321
Andrey Andreev0bcf5902012-10-12 13:03:29 +03002322 return "\nGROUP BY ".implode(', ', $this->qb_groupby);
Andrey Andreevc9b924c2012-07-19 13:06:02 +03002323 }
2324
2325 return '';
2326 }
2327
2328 // --------------------------------------------------------------------
2329
2330 /**
Andrey Andreev2d486232012-07-19 14:46:51 +03002331 * Compile ORDER BY
2332 *
2333 * Escapes identifiers in ORDER BY statements at execution time.
2334 *
2335 * Required so that aliases are tracked properly, regardless of wether
2336 * order_by() is called prior to from(), join() and dbprefix is added
2337 * only if needed.
2338 *
2339 * @return string SQL statement
2340 */
2341 protected function _compile_order_by()
2342 {
Andrey Andreeva53ea842012-10-23 12:44:09 +03002343 if (is_array($this->qb_orderby) && count($this->qb_orderby) > 0)
Andrey Andreev2d486232012-07-19 14:46:51 +03002344 {
Andrey Andreev2d486232012-07-19 14:46:51 +03002345 for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++)
2346 {
Andrey Andreev082aa402012-10-22 19:41:55 +03002347 if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field']))
Andrey Andreev2d486232012-07-19 14:46:51 +03002348 {
Andrey Andreevfc043b32012-10-12 14:46:14 +03002349 $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']);
Andrey Andreev2d486232012-07-19 14:46:51 +03002350 }
2351
2352 $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction'];
2353 }
2354
Andrey Andreeva53ea842012-10-23 12:44:09 +03002355 return $this->qb_orderby = "\nORDER BY ".implode(', ', $this->qb_orderby);
2356 }
2357 elseif (is_string($this->qb_orderby))
2358 {
2359 return $this->qb_orderby;
Andrey Andreev2d486232012-07-19 14:46:51 +03002360 }
2361
2362 return '';
2363 }
2364
2365 // --------------------------------------------------------------------
2366
2367 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00002368 * Object to Array
2369 *
2370 * Takes an object as input and converts the class variables to array key/vals
2371 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002372 * @param object
2373 * @return array
2374 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002375 protected function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002376 {
2377 if ( ! is_object($object))
2378 {
2379 return $object;
2380 }
Barry Mienydd671972010-10-04 16:33:58 +02002381
Derek Allard2067d1a2008-11-13 22:59:24 +00002382 $array = array();
2383 foreach (get_object_vars($object) as $key => $val)
2384 {
2385 // There are some built in keys we need to ignore for this conversion
Alex Bilbie48a2baf2012-06-02 11:09:54 +01002386 if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002387 {
2388 $array[$key] = $val;
2389 }
2390 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002391
2392 return $array;
2393 }
Barry Mienydd671972010-10-04 16:33:58 +02002394
Derek Jonesd10e8962010-03-02 17:10:36 -06002395 // --------------------------------------------------------------------
2396
2397 /**
2398 * Object to Array
2399 *
2400 * Takes an object as input and converts the class variables to array key/vals
2401 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002402 * @param object
2403 * @return array
2404 */
Andrey Andreev7b5eb732012-05-24 20:52:41 +03002405 protected function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002406 {
2407 if ( ! is_object($object))
2408 {
2409 return $object;
2410 }
Barry Mienydd671972010-10-04 16:33:58 +02002411
Derek Jonesd10e8962010-03-02 17:10:36 -06002412 $array = array();
2413 $out = get_object_vars($object);
2414 $fields = array_keys($out);
2415
2416 foreach ($fields as $val)
2417 {
2418 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002419 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002420 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002421 $i = 0;
2422 foreach ($out[$val] as $data)
2423 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002424 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002425 }
2426 }
2427 }
2428
Derek Allard2067d1a2008-11-13 22:59:24 +00002429 return $array;
2430 }
Barry Mienydd671972010-10-04 16:33:58 +02002431
Derek Allard2067d1a2008-11-13 22:59:24 +00002432 // --------------------------------------------------------------------
2433
2434 /**
2435 * Start Cache
2436 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002437 * Starts QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002438 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002439 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002440 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002441 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002442 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002443 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002444 }
2445
2446 // --------------------------------------------------------------------
2447
2448 /**
2449 * Stop Cache
2450 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002451 * Stops QB caching
Derek Allard2067d1a2008-11-13 22:59:24 +00002452 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002453 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002454 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002455 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002456 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002457 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002458 }
2459
2460 // --------------------------------------------------------------------
2461
2462 /**
2463 * Flush Cache
2464 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002465 * Empties the QB cache
Derek Allard2067d1a2008-11-13 22:59:24 +00002466 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002467 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002468 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002469 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002470 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002471 $this->_reset_run(array(
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002472 'qb_cache_select' => array(),
2473 'qb_cache_from' => array(),
2474 'qb_cache_join' => array(),
2475 'qb_cache_where' => array(),
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002476 'qb_cache_groupby' => array(),
2477 'qb_cache_having' => array(),
2478 'qb_cache_orderby' => array(),
2479 'qb_cache_set' => array(),
2480 'qb_cache_exists' => array(),
2481 'qb_cache_no_escape' => array()
Phil Sturgeon9789f322011-07-15 15:14:05 -06002482 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002483 }
2484
2485 // --------------------------------------------------------------------
2486
2487 /**
2488 * Merge Cache
2489 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002490 * When called, this function merges any cached QB arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002491 * locally called ones.
2492 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002493 * @return void
2494 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002495 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002496 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002497 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002498 {
2499 return;
2500 }
2501
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002502 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002503 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002504 $qb_variable = 'qb_'.$val;
2505 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002506
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002507 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002508 {
2509 continue;
2510 }
2511
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002512 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002513 }
2514
2515 // If we are "protecting identifiers" we need to examine the "from"
2516 // portion of the query to determine if there are any aliases
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002517 if ($this->_protect_identifiers === TRUE && count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002518 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002519 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002520 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002521
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002522 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002523 }
WanWizard7219c072011-12-28 14:09:05 +01002524
Kyle Farris0c147b32011-08-26 02:29:31 -04002525 // --------------------------------------------------------------------
2526
2527 /**
Andrey Andreev082aa402012-10-22 19:41:55 +03002528 * Is literal
2529 *
2530 * Determines if a string represents a literal value or a field name
2531 *
2532 * @param string
2533 * @return bool
2534 */
2535 protected function _is_literal($str)
2536 {
2537 $str = trim($str);
2538
2539 if (empty($str))
2540 {
2541 return TRUE;
2542 }
2543
2544 static $_str;
2545
2546 if (empty($_str))
2547 {
2548 $_str = ($this->_escape_char !== '"')
2549 ? array('"', "'") : array("'");
2550 }
2551
2552 return (ctype_digit($str) OR in_array($str[0], $_str, TRUE));
2553 }
2554
2555 // --------------------------------------------------------------------
2556
2557 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002558 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002559 *
Jamie Rumbelow17c1bed2012-03-06 21:30:38 +00002560 * Publicly-visible method to reset the QB values.
Kyle Farris0c147b32011-08-26 02:29:31 -04002561 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002562 * @return void
2563 */
2564 public function reset_query()
2565 {
2566 $this->_reset_select();
2567 $this->_reset_write();
2568 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002569
2570 // --------------------------------------------------------------------
2571
2572 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002573 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002574 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002575 * @param array An array of fields to reset
2576 * @return void
2577 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002578 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002579 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002580 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002581 {
Andrey Andreevae85eb42012-11-02 01:42:31 +02002582 $this->$item = $default_value;
Derek Allard2067d1a2008-11-13 22:59:24 +00002583 }
2584 }
2585
2586 // --------------------------------------------------------------------
2587
2588 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002589 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002590 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002591 * @return void
2592 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002593 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002594 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002595 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002596 'qb_select' => array(),
2597 'qb_from' => array(),
2598 'qb_join' => array(),
2599 'qb_where' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002600 'qb_groupby' => array(),
2601 'qb_having' => array(),
2602 'qb_orderby' => array(),
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002603 'qb_aliased_tables' => array(),
2604 'qb_no_escape' => array(),
2605 'qb_distinct' => FALSE,
2606 'qb_limit' => FALSE,
Andrey Andreev650b4c02012-06-11 12:07:15 +03002607 'qb_offset' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002608 )
2609 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002610 }
Barry Mienydd671972010-10-04 16:33:58 +02002611
Derek Allard2067d1a2008-11-13 22:59:24 +00002612 // --------------------------------------------------------------------
2613
2614 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002615 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002616 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002617 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002618 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002619 * @return void
2620 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002621 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002622 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002623 $this->_reset_run(array(
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002624 'qb_set' => array(),
2625 'qb_from' => array(),
2626 'qb_where' => array(),
Jamie Rumbelowd6ce1e92012-04-26 13:27:35 +01002627 'qb_orderby' => array(),
2628 'qb_keys' => array(),
Andrey Andreev650b4c02012-06-11 12:07:15 +03002629 'qb_limit' => FALSE
Timothy Warren215890b2012-03-20 09:38:16 -04002630 )
2631 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002632 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002633
Derek Allard2067d1a2008-11-13 22:59:24 +00002634}
2635
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002636/* End of file DB_query_builder.php */
Andrey Andreev58803fb2012-06-24 00:45:37 +03002637/* Location: ./system/database/DB_query_builder.php */