blob: ab59462b1f8c9c6e19316d6293b469c3c5e10503 [file] [log] [blame]
Andrey Andreev24276a32012-01-08 02:44:38 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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 */
27
28// ------------------------------------------------------------------------
29
30/**
Jamie Rumbelow7efad202012-02-19 12:37:00 +000031 * Query Builder Class
Derek Allard2067d1a2008-11-13 22:59:24 +000032 *
Jamie Rumbelow7efad202012-02-19 12:37:00 +000033 * This is the platform-independent base Query Builder implementation class.
Derek Allard2067d1a2008-11-13 22:59:24 +000034 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +000041class CI_DB_query_builder extends CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000042
WanWizard7219c072011-12-28 14:09:05 +010043 protected $return_delete_sql = FALSE;
44 protected $reset_delete_data = FALSE;
45
Jamie Rumbelow7efad202012-02-19 12:37:00 +000046 protected $qb_select = array();
47 protected $qb_distinct = FALSE;
48 protected $qb_from = array();
49 protected $qb_join = array();
50 protected $qb_where = array();
51 protected $qb_like = array();
52 protected $qb_groupby = array();
53 protected $qb_having = array();
54 protected $qb_keys = array();
55 protected $qb_limit = FALSE;
56 protected $qb_offset = FALSE;
57 protected $qb_order = FALSE;
58 protected $qb_orderby = array();
59 protected $qb_set = array();
60 protected $qb_wherein = array();
61 protected $qb_aliased_tables = array();
62 protected $qb_store_array = array();
63 protected $qb_where_group_started = FALSE;
64 protected $qb_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +020065
Jamie Rumbelow7efad202012-02-19 12:37:00 +000066 // Query Builder Caching variables
67 protected $qb_caching = FALSE;
68 protected $qb_cache_exists = array();
69 protected $qb_cache_select = array();
70 protected $qb_cache_from = array();
71 protected $qb_cache_join = array();
72 protected $qb_cache_where = array();
73 protected $qb_cache_like = array();
74 protected $qb_cache_groupby = array();
75 protected $qb_cache_having = array();
76 protected $qb_cache_orderby = array();
77 protected $qb_cache_set = array();
WanWizard7219c072011-12-28 14:09:05 +010078
Jamie Rumbelow7efad202012-02-19 12:37:00 +000079 protected $qb_no_escape = array();
80 protected $qb_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000081
82 /**
83 * Select
84 *
85 * Generates the SELECT portion of the query
86 *
Derek Allard2067d1a2008-11-13 22:59:24 +000087 * @param string
88 * @return object
89 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060090 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Derek Allard2067d1a2008-11-13 22:59:24 +000092 if (is_string($select))
93 {
94 $select = explode(',', $select);
95 }
96
97 foreach ($select as $val)
98 {
99 $val = trim($val);
100
101 if ($val != '')
102 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000103 $this->qb_select[] = $val;
104 $this->qb_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000105
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000106 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000108 $this->qb_cache_select[] = $val;
109 $this->qb_cache_exists[] = 'select';
110 $this->qb_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
112 }
113 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 return $this;
116 }
117
118 // --------------------------------------------------------------------
119
120 /**
121 * Select Max
122 *
123 * Generates a SELECT MAX(field) portion of a query
124 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 * @param string the field
126 * @param string an alias
127 * @return object
128 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600129 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
131 return $this->_max_min_avg_sum($select, $alias, 'MAX');
132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 // --------------------------------------------------------------------
135
136 /**
137 * Select Min
138 *
139 * Generates a SELECT MIN(field) portion of a query
140 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 * @param string the field
142 * @param string an alias
143 * @return object
144 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600145 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
147 return $this->_max_min_avg_sum($select, $alias, 'MIN');
148 }
149
150 // --------------------------------------------------------------------
151
152 /**
153 * Select Average
154 *
155 * Generates a SELECT AVG(field) portion of a query
156 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 * @param string the field
158 * @param string an alias
159 * @return object
160 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600161 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
163 return $this->_max_min_avg_sum($select, $alias, 'AVG');
164 }
165
166 // --------------------------------------------------------------------
167
168 /**
169 * Select Sum
170 *
171 * Generates a SELECT SUM(field) portion of a query
172 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 * @param string the field
174 * @param string an alias
175 * @return object
176 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600177 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
179 return $this->_max_min_avg_sum($select, $alias, 'SUM');
180 }
181
182 // --------------------------------------------------------------------
183
184 /**
185 * Processing Function for the four functions above:
186 *
187 * select_max()
188 * select_min()
189 * select_avg()
Andrey Andreev24276a32012-01-08 02:44:38 +0200190 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200191 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 * @param string the field
193 * @param string an alias
194 * @return object
195 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600196 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
198 if ( ! is_string($select) OR $select == '')
199 {
200 $this->display_error('db_invalid_query');
201 }
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
206 {
207 show_error('Invalid function type: '.$type);
208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 if ($alias == '')
211 {
212 $alias = $this->_create_alias_from_table(trim($select));
213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Juan José González8b4d83b2011-09-27 17:21:14 -0500215 $sql = $this->_protect_identifiers($type.'('.trim($select).')').' AS '.$this->_protect_identifiers(trim($alias));
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000216 $this->qb_select[] = $sql;
Barry Mienydd671972010-10-04 16:33:58 +0200217
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000218 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000220 $this->qb_cache_select[] = $sql;
221 $this->qb_cache_exists[] = 'select';
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 return $this;
225 }
226
227 // --------------------------------------------------------------------
228
229 /**
230 * Determines the alias name based on the table
231 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * @param string
233 * @return string
234 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600235 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
237 if (strpos($item, '.') !== FALSE)
238 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200239 $item = explode('.', $item);
240 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 return $item;
244 }
245
246 // --------------------------------------------------------------------
247
248 /**
249 * DISTINCT
250 *
251 * Sets a flag which tells the query string compiler to add DISTINCT
252 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 * @param bool
254 * @return object
255 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600256 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000258 $this->qb_distinct = (is_bool($val)) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 return $this;
260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 // --------------------------------------------------------------------
263
264 /**
265 * From
266 *
267 * Generates the FROM portion of the query
268 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 * @param mixed can be a string or array
270 * @return object
271 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600272 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
274 foreach ((array)$from as $val)
275 {
276 if (strpos($val, ',') !== FALSE)
277 {
278 foreach (explode(',', $val) as $v)
279 {
280 $v = trim($v);
281 $this->_track_aliases($v);
Barry Mienydd671972010-10-04 16:33:58 +0200282
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000283 $v = $this->qb_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000284
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000285 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000287 $this->qb_cache_from[] = $v;
288 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200289 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
292 else
293 {
294 $val = trim($val);
295
Andrey Andreev24276a32012-01-08 02:44:38 +0200296 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200297 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200299
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000300 $this->qb_from[] = $val = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000301
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000302 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000304 $this->qb_cache_from[] = $val;
305 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 }
307 }
308 }
309
310 return $this;
311 }
312
313 // --------------------------------------------------------------------
314
315 /**
316 * Join
317 *
318 * Generates the JOIN portion of the query
319 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 * @param string
321 * @param string the join condition
322 * @param string the type of join
323 * @return object
324 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600325 public function join($table, $cond, $type = '')
Barry Mienydd671972010-10-04 16:33:58 +0200326 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 if ($type != '')
328 {
329 $type = strtoupper(trim($type));
330
331 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
332 {
333 $type = '';
334 }
335 else
336 {
337 $type .= ' ';
338 }
339 }
340
Andrey Andreev24276a32012-01-08 02:44:38 +0200341 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200342 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 $this->_track_aliases($table);
344
345 // Strip apart the condition and protect the identifiers
Hamza Bhattid3c1ccf2012-03-05 22:58:56 +0400346 if (preg_match('/([\[\w\.]+)([\W\s]+)(.+)/', $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200348 $cond = $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 }
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // Assemble the JOIN statement
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000352 $this->qb_join[] = $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000353
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000354 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000356 $this->qb_cache_join[] = $join;
357 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 }
359
360 return $this;
361 }
362
363 // --------------------------------------------------------------------
364
365 /**
366 * Where
367 *
368 * Generates the WHERE portion of the query. Separates
369 * multiple calls with AND
370 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 * @param mixed
372 * @param mixed
373 * @return object
374 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600375 public function where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
377 return $this->_where($key, $value, 'AND ', $escape);
378 }
Barry Mienydd671972010-10-04 16:33:58 +0200379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 // --------------------------------------------------------------------
381
382 /**
383 * OR Where
384 *
385 * Generates the WHERE portion of the query. Separates
386 * multiple calls with OR
387 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 * @param mixed
389 * @param mixed
390 * @return object
391 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600392 public function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 {
394 return $this->_where($key, $value, 'OR ', $escape);
395 }
396
397 // --------------------------------------------------------------------
398
399 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 * Where
401 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600402 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 * @param mixed
405 * @param mixed
406 * @param string
407 * @return object
408 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600409 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
WanWizard7219c072011-12-28 14:09:05 +0100411 $type = $this->_group_get_type($type);
412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 if ( ! is_array($key))
414 {
415 $key = array($key => $value);
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // If the escape value was not set will will base it on the global setting
419 if ( ! is_bool($escape))
420 {
421 $escape = $this->_protect_identifiers;
422 }
423
424 foreach ($key as $k => $v)
425 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000426 $prefix = (count($this->qb_where) === 0 AND count($this->qb_cache_where) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000427
428 if (is_null($v) && ! $this->_has_operator($k))
429 {
430 // value appears not to have been set, assign the test to IS NULL
431 $k .= ' IS NULL';
432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 if ( ! is_null($v))
435 {
436 if ($escape === TRUE)
437 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200438 $k = $this->protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 $v = ' '.$this->escape($v);
440 }
WanWizard7219c072011-12-28 14:09:05 +0100441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 if ( ! $this->_has_operator($k))
443 {
Greg Akere156c6e2011-04-20 16:03:04 -0500444 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 }
446 }
447 else
448 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200449 $k = $this->protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 }
451
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000452 $this->qb_where[] = $prefix.$k.$v;
453 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000455 $this->qb_cache_where[] = $prefix.$k.$v;
456 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 return $this;
462 }
463
464 // --------------------------------------------------------------------
465
466 /**
467 * Where_in
468 *
469 * Generates a WHERE field IN ('item', 'item') SQL query joined with
470 * AND if appropriate
471 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 * @param string The field to search
473 * @param array The values searched on
474 * @return object
475 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600476 public function where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
478 return $this->_where_in($key, $values);
479 }
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 // --------------------------------------------------------------------
482
483 /**
484 * Where_in_or
485 *
486 * Generates a WHERE field IN ('item', 'item') SQL query joined with
487 * OR if appropriate
488 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 * @param string The field to search
490 * @param array The values searched on
491 * @return object
492 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600493 public function or_where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 {
495 return $this->_where_in($key, $values, FALSE, 'OR ');
496 }
497
498 // --------------------------------------------------------------------
499
500 /**
501 * Where_not_in
502 *
503 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
504 * with AND if appropriate
505 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 * @param string The field to search
507 * @param array The values searched on
508 * @return object
509 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600510 public function where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 {
512 return $this->_where_in($key, $values, TRUE);
513 }
Barry Mienydd671972010-10-04 16:33:58 +0200514
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 // --------------------------------------------------------------------
516
517 /**
518 * Where_not_in_or
519 *
520 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
521 * with OR if appropriate
522 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 * @param string The field to search
524 * @param array The values searched on
525 * @return object
526 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600527 public function or_where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 {
529 return $this->_where_in($key, $values, TRUE, 'OR ');
530 }
531
532 // --------------------------------------------------------------------
533
534 /**
535 * Where_in
536 *
537 * Called by where_in, where_in_or, where_not_in, where_not_in_or
538 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 * @param string The field to search
540 * @param array The values searched on
541 * @param boolean If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200542 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 * @return object
544 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600545 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 {
547 if ($key === NULL OR $values === NULL)
548 {
549 return;
550 }
Barry Mienydd671972010-10-04 16:33:58 +0200551
WanWizard7219c072011-12-28 14:09:05 +0100552 $type = $this->_group_get_type($type);
553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 if ( ! is_array($values))
555 {
556 $values = array($values);
557 }
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 $not = ($not) ? ' NOT' : '';
560
561 foreach ($values as $value)
562 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000563 $this->qb_wherein[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 }
565
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000566 $prefix = (count($this->qb_where) === 0) ? '' : $type;
567 $this->qb_where[] = $where_in = $prefix.$this->_protect_identifiers($key).$not.' IN ('.implode(', ', $this->qb_wherein).') ';
Barry Mienydd671972010-10-04 16:33:58 +0200568
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000569 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000571 $this->qb_cache_where[] = $where_in;
572 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 }
574
575 // reset the array for multiple calls
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000576 $this->qb_wherein = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 return $this;
578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 // --------------------------------------------------------------------
581
582 /**
583 * Like
584 *
585 * Generates a %LIKE% portion of the query. Separates
586 * multiple calls with AND
587 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 * @param mixed
589 * @param mixed
590 * @return object
591 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600592 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 {
594 return $this->_like($field, $match, 'AND ', $side);
595 }
596
597 // --------------------------------------------------------------------
598
599 /**
600 * Not Like
601 *
602 * Generates a NOT LIKE portion of the query. Separates
603 * multiple calls with AND
604 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 * @param mixed
606 * @param mixed
607 * @return object
608 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600609 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 {
611 return $this->_like($field, $match, 'AND ', $side, 'NOT');
612 }
Barry Mienydd671972010-10-04 16:33:58 +0200613
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 // --------------------------------------------------------------------
615
616 /**
617 * OR Like
618 *
619 * Generates a %LIKE% portion of the query. Separates
620 * multiple calls with OR
621 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 * @param mixed
623 * @param mixed
624 * @return object
625 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600626 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 {
628 return $this->_like($field, $match, 'OR ', $side);
629 }
630
631 // --------------------------------------------------------------------
632
633 /**
634 * OR Not Like
635 *
636 * Generates a NOT LIKE portion of the query. Separates
637 * multiple calls with OR
638 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 * @param mixed
640 * @param mixed
641 * @return object
642 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600643 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 {
645 return $this->_like($field, $match, 'OR ', $side, 'NOT');
646 }
Barry Mienydd671972010-10-04 16:33:58 +0200647
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 // --------------------------------------------------------------------
649
650 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 * Like
652 *
653 * Called by like() or orlike()
654 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 * @param mixed
656 * @param mixed
657 * @param string
658 * @return object
659 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600660 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 {
WanWizard7219c072011-12-28 14:09:05 +0100662 $type = $this->_group_get_type($type);
663
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 if ( ! is_array($field))
665 {
666 $field = array($field => $match);
667 }
Barry Mienydd671972010-10-04 16:33:58 +0200668
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 foreach ($field as $k => $v)
670 {
671 $k = $this->_protect_identifiers($k);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000672 $prefix = (count($this->qb_like) === 0) ? '' : $type;
Derek Jonese4ed5832009-02-20 21:44:59 +0000673 $v = $this->escape_like_str($v);
Kyle Farris0c147b32011-08-26 02:29:31 -0400674
Andrey Andreev24276a32012-01-08 02:44:38 +0200675 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400676 {
677 $like_statement = $prefix." $k $not LIKE '{$v}'";
678 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200679 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 {
681 $like_statement = $prefix." $k $not LIKE '%{$v}'";
682 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200683 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 {
685 $like_statement = $prefix." $k $not LIKE '{$v}%'";
686 }
687 else
688 {
689 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
690 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600691
Derek Jonese4ed5832009-02-20 21:44:59 +0000692 // some platforms require an escape sequence definition for LIKE wildcards
693 if ($this->_like_escape_str != '')
694 {
Greg Aker0d424892010-01-26 02:14:44 +0000695 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000696 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600697
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000698 $this->qb_like[] = $like_statement;
699 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000701 $this->qb_cache_like[] = $like_statement;
702 $this->qb_cache_exists[] = 'like';
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 }
Barry Mienydd671972010-10-04 16:33:58 +0200704
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 return $this;
708 }
Barry Mienydd671972010-10-04 16:33:58 +0200709
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 // --------------------------------------------------------------------
711
712 /**
WanWizard7219c072011-12-28 14:09:05 +0100713 * Starts a query group.
714 *
715 * @param string (Internal use only)
716 * @param string (Internal use only)
717 * @return object
718 */
719 public function group_start($not = '', $type = 'AND ')
720 {
721 $type = $this->_group_get_type($type);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000722 $this->qb_where_group_started = TRUE;
723 $prefix = (count($this->qb_where) === 0 AND count($this->qb_cache_where) === 0) ? '' : $type;
724 $this->qb_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (';
WanWizard7219c072011-12-28 14:09:05 +0100725
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000726 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100727 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000728 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100729 }
730
731 return $this;
732 }
733
734 // --------------------------------------------------------------------
735
736 /**
737 * Starts a query group, but ORs the group
738 *
739 * @return object
740 */
741 public function or_group_start()
742 {
743 return $this->group_start('', 'OR ');
744 }
745
746 // --------------------------------------------------------------------
747
748 /**
749 * Starts a query group, but NOTs the group
750 *
751 * @return object
752 */
753 public function not_group_start()
754 {
755 return $this->group_start('NOT ', 'AND ');
756 }
757
758 // --------------------------------------------------------------------
759
760 /**
761 * Starts a query group, but OR NOTs the group
762 *
763 * @return object
764 */
765 public function or_not_group_start()
766 {
767 return $this->group_start('NOT ', 'OR ');
768 }
769
770 // --------------------------------------------------------------------
771
772 /**
773 * Ends a query group
774 *
775 * @return object
776 */
777 public function group_end()
778 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000779 $this->qb_where_group_started = FALSE;
780 $this->qb_where[] = $value = str_repeat(' ', $this->qb_where_group_count--) . ')';
WanWizard7219c072011-12-28 14:09:05 +0100781
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000782 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100783 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000784 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100785 }
786
WanWizard7219c072011-12-28 14:09:05 +0100787 return $this;
788 }
789
790 // --------------------------------------------------------------------
791
792 /**
793 * Group_get_type
794 *
795 * Called by group_start(), _like(), _where() and _where_in()
796 *
797 * @param string
798 * @return string
799 */
800 protected function _group_get_type($type)
801 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000802 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100803 {
804 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000805 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100806 }
807
808 return $type;
809 }
810
811 // --------------------------------------------------------------------
812
813 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 * GROUP BY
815 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 * @param string
817 * @return object
818 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600819 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 {
821 if (is_string($by))
822 {
823 $by = explode(',', $by);
824 }
Barry Mienydd671972010-10-04 16:33:58 +0200825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 foreach ($by as $val)
827 {
828 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200829
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 if ($val != '')
831 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000832 $this->qb_groupby[] = $val = $this->_protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200833
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000834 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000836 $this->qb_cache_groupby[] = $val;
837 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 }
839 }
840 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200841
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 return $this;
843 }
844
845 // --------------------------------------------------------------------
846
847 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 * Sets the HAVING value
849 *
850 * Separates multiple calls with AND
851 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 * @param string
853 * @param string
854 * @return object
855 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600856 public function having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 {
858 return $this->_having($key, $value, 'AND ', $escape);
859 }
Barry Mienydd671972010-10-04 16:33:58 +0200860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 // --------------------------------------------------------------------
862
863 /**
864 * Sets the OR HAVING value
865 *
866 * Separates multiple calls with OR
867 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 * @param string
869 * @param string
870 * @return object
871 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600872 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 {
874 return $this->_having($key, $value, 'OR ', $escape);
875 }
Barry Mienydd671972010-10-04 16:33:58 +0200876
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 // --------------------------------------------------------------------
878
879 /**
880 * Sets the HAVING values
881 *
882 * Called by having() or or_having()
883 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 * @param string
885 * @param string
886 * @return object
887 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600888 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 {
890 if ( ! is_array($key))
891 {
892 $key = array($key => $value);
893 }
Barry Mienydd671972010-10-04 16:33:58 +0200894
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 foreach ($key as $k => $v)
896 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000897 $prefix = (count($this->qb_having) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000898
899 if ($escape === TRUE)
900 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200901 $k = $this->protect_identifiers($k);
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 }
903
904 if ( ! $this->_has_operator($k))
905 {
906 $k .= ' = ';
907 }
908
909 if ($v != '')
910 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400911 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 }
Barry Mienydd671972010-10-04 16:33:58 +0200913
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000914 $this->qb_having[] = $prefix.$k.$v;
915 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000917 $this->qb_cache_having[] = $prefix.$k.$v;
918 $this->qb_cache_exists[] = 'having';
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 }
920 }
Barry Mienydd671972010-10-04 16:33:58 +0200921
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 return $this;
923 }
Barry Mienydd671972010-10-04 16:33:58 +0200924
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 // --------------------------------------------------------------------
926
927 /**
928 * Sets the ORDER BY value
929 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 * @param string
931 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100932 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 * @return object
934 */
pporlan2c685fb2011-12-22 12:15:25 +0100935 public function order_by($orderby, $direction = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200937 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 {
939 $orderby = ''; // Random results want or don't need a field name
940 $direction = $this->_random_keyword;
941 }
942 elseif (trim($direction) != '')
943 {
944 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
945 }
Barry Mienydd671972010-10-04 16:33:58 +0200946
947
Andrey Andreev24276a32012-01-08 02:44:38 +0200948 if ((strpos($orderby, ',') !== FALSE) && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 {
950 $temp = array();
951 foreach (explode(',', $orderby) as $part)
952 {
953 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000954 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200956 $part = $this->protect_identifiers(trim($part));
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 }
Barry Mienydd671972010-10-04 16:33:58 +0200958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 $temp[] = $part;
960 }
Barry Mienydd671972010-10-04 16:33:58 +0200961
962 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200964 elseif ($direction != $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 {
pporlan2c685fb2011-12-22 12:15:25 +0100966 if ($escape === TRUE)
967 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200968 $orderby = $this->protect_identifiers($orderby);
pporlan2c685fb2011-12-22 12:15:25 +0100969 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 }
Barry Mienydd671972010-10-04 16:33:58 +0200971
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000972 $this->qb_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200973
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000974 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000976 $this->qb_cache_orderby[] = $orderby_statement;
977 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 }
979
980 return $this;
981 }
Barry Mienydd671972010-10-04 16:33:58 +0200982
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 // --------------------------------------------------------------------
984
985 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 * Sets the LIMIT value
987 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 * @param integer the limit value
989 * @param integer the offset value
990 * @return object
991 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200992 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000994 $this->qb_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000995
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200996 if ( ! is_null($offset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000998 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 }
Barry Mienydd671972010-10-04 16:33:58 +02001000
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 return $this;
1002 }
Barry Mienydd671972010-10-04 16:33:58 +02001003
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 // --------------------------------------------------------------------
1005
1006 /**
1007 * Sets the OFFSET value
1008 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 * @param integer the offset value
1010 * @return object
1011 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001012 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001014 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 return $this;
1016 }
Barry Mienydd671972010-10-04 16:33:58 +02001017
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 // --------------------------------------------------------------------
1019
1020 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001021 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 * @param mixed
1024 * @param string
1025 * @param boolean
1026 * @return object
1027 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001028 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 {
1030 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001031
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 if ( ! is_array($key))
1033 {
1034 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001035 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001036
1037 foreach ($key as $k => $v)
1038 {
1039 if ($escape === FALSE)
1040 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001041 $this->qb_set[$this->_protect_identifiers($k)] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 }
1043 else
1044 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001045 $this->qb_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 }
1047 }
Barry Mienydd671972010-10-04 16:33:58 +02001048
Derek Allard2067d1a2008-11-13 22:59:24 +00001049 return $this;
1050 }
WanWizard7219c072011-12-28 14:09:05 +01001051
Kyle Farris0c147b32011-08-26 02:29:31 -04001052 // --------------------------------------------------------------------
1053
1054 /**
1055 * Get SELECT query string
1056 *
1057 * Compiles a SELECT query string and returns the sql.
1058 *
1059 * @access public
1060 * @param string the table name to select from (optional)
1061 * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone
1062 * @return string
1063 */
WanWizard7219c072011-12-28 14:09:05 +01001064 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001065 {
1066 if ($table != '')
kylefarris0a3176b2011-08-26 02:37:52 -04001067 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001068 $this->_track_aliases($table);
1069 $this->from($table);
1070 }
WanWizard7219c072011-12-28 14:09:05 +01001071
Kyle Farris0c147b32011-08-26 02:29:31 -04001072 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001073
Kyle Farris0c147b32011-08-26 02:29:31 -04001074 if ($reset === TRUE)
1075 {
1076 $this->_reset_select();
1077 }
WanWizard7219c072011-12-28 14:09:05 +01001078
Kyle Farris0c147b32011-08-26 02:29:31 -04001079 return $select;
1080 }
WanWizard7219c072011-12-28 14:09:05 +01001081
Derek Allard2067d1a2008-11-13 22:59:24 +00001082 // --------------------------------------------------------------------
1083
1084 /**
1085 * Get
1086 *
1087 * Compiles the select statement based on the other functions called
1088 * and runs the query
1089 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001090 * @param string the table
1091 * @param string the limit clause
1092 * @param string the offset clause
1093 * @return object
1094 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001095 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 {
1097 if ($table != '')
1098 {
1099 $this->_track_aliases($table);
1100 $this->from($table);
1101 }
Barry Mienydd671972010-10-04 16:33:58 +02001102
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 if ( ! is_null($limit))
1104 {
1105 $this->limit($limit, $offset);
1106 }
Barry Mienydd671972010-10-04 16:33:58 +02001107
Andrey Andreev24276a32012-01-08 02:44:38 +02001108 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 $this->_reset_select();
1110 return $result;
1111 }
1112
1113 /**
1114 * "Count All Results" query
1115 *
Barry Mienydd671972010-10-04 16:33:58 +02001116 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001117 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001118 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 * @param string
1120 * @return string
1121 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001122 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 {
1124 if ($table != '')
1125 {
1126 $this->_track_aliases($table);
1127 $this->from($table);
1128 }
Barry Mienydd671972010-10-04 16:33:58 +02001129
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001130 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001132
Purwandi1d160e72012-01-09 16:33:28 +07001133 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001134 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001135 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 }
1137
Purwandi1d160e72012-01-09 16:33:28 +07001138 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001139 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001140 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 // --------------------------------------------------------------------
1142
1143 /**
1144 * Get_Where
1145 *
1146 * Allows the where clause, limit and offset to be added directly
1147 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 * @param string the where clause
1149 * @param string the limit clause
1150 * @param string the offset clause
1151 * @return object
1152 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001153 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 {
1155 if ($table != '')
1156 {
1157 $this->from($table);
1158 }
1159
1160 if ( ! is_null($where))
1161 {
1162 $this->where($where);
1163 }
Barry Mienydd671972010-10-04 16:33:58 +02001164
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 if ( ! is_null($limit))
1166 {
1167 $this->limit($limit, $offset);
1168 }
Barry Mienydd671972010-10-04 16:33:58 +02001169
Andrey Andreev24276a32012-01-08 02:44:38 +02001170 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 $this->_reset_select();
1172 return $result;
1173 }
1174
1175 // --------------------------------------------------------------------
1176
1177 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001178 * Insert_Batch
1179 *
1180 * Compiles batch insert strings and runs the queries
1181 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001182 * @param string the table to retrieve the results from
1183 * @param array an associative array of insert values
1184 * @return object
1185 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001186 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001187 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001188 if ( ! is_null($set))
1189 {
1190 $this->set_insert_batch($set);
1191 }
Barry Mienydd671972010-10-04 16:33:58 +02001192
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001193 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001194 {
1195 if ($this->db_debug)
1196 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001197 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001198 return $this->display_error('db_must_use_set');
1199 }
1200 return FALSE;
1201 }
1202
1203 if ($table == '')
1204 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001205 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001206 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001207 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001208 }
Barry Mienydd671972010-10-04 16:33:58 +02001209
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001210 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001211 }
1212
1213 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001214 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001215 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001216 $this->query($this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, 100)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001217 }
Barry Mienydd671972010-10-04 16:33:58 +02001218
Derek Jonesd10e8962010-03-02 17:10:36 -06001219 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001220 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001221 }
1222
1223 // --------------------------------------------------------------------
1224
1225 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001226 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001227 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001228 * @param mixed
1229 * @param string
1230 * @param boolean
1231 * @return object
1232 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001233 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001234 {
1235 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001236
Derek Jonesd10e8962010-03-02 17:10:36 -06001237 if ( ! is_array($key))
1238 {
1239 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001240 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001241
1242 $keys = array_keys(current($key));
1243 sort($keys);
1244
1245 foreach ($key as $row)
1246 {
Barry Mienydd671972010-10-04 16:33:58 +02001247 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1248 {
1249 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001250 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001251 return;
1252 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001253
Barry Mienydd671972010-10-04 16:33:58 +02001254 ksort($row); // puts $row in the same order as our keys
1255
Derek Jonesd10e8962010-03-02 17:10:36 -06001256 if ($escape === FALSE)
1257 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001258 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001259 }
1260 else
1261 {
1262 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001263 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001264 {
Barry Mienydd671972010-10-04 16:33:58 +02001265 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001266 }
1267
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001268 $this->qb_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001269 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001270 }
1271
1272 foreach ($keys as $k)
1273 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001274 $this->qb_keys[] = $this->_protect_identifiers($k);
Derek Jonesd10e8962010-03-02 17:10:36 -06001275 }
Barry Mienydd671972010-10-04 16:33:58 +02001276
Derek Jonesd10e8962010-03-02 17:10:36 -06001277 return $this;
1278 }
WanWizard7219c072011-12-28 14:09:05 +01001279
Kyle Farris0c147b32011-08-26 02:29:31 -04001280 // --------------------------------------------------------------------
1281
1282 /**
1283 * Get INSERT query string
1284 *
1285 * Compiles an insert query and returns the sql
1286 *
1287 * @access public
1288 * @param string the table to insert into
1289 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1290 * @return string
1291 */
1292 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001293 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001294 if ($this->_validate_insert($table) === FALSE)
1295 {
1296 return FALSE;
1297 }
WanWizard7219c072011-12-28 14:09:05 +01001298
Kyle Farris76116012011-08-31 11:17:48 -04001299 $sql = $this->_insert(
1300 $this->_protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001301 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001302 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001303 array_keys($this->qb_set),
1304 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001305 );
WanWizard7219c072011-12-28 14:09:05 +01001306
Kyle Farris0c147b32011-08-26 02:29:31 -04001307 if ($reset === TRUE)
1308 {
1309 $this->_reset_write();
1310 }
WanWizard7219c072011-12-28 14:09:05 +01001311
Kyle Farris0c147b32011-08-26 02:29:31 -04001312 return $sql;
1313 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001314
Derek Allard2067d1a2008-11-13 22:59:24 +00001315 // --------------------------------------------------------------------
1316
1317 /**
1318 * Insert
1319 *
1320 * Compiles an insert string and runs the query
1321 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001322 * @access public
Phil Sturgeon9789f322011-07-15 15:14:05 -06001323 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001324 * @param array an associative array of insert values
1325 * @return object
1326 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001327 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001328 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001329 if ( ! is_null($set))
1330 {
1331 $this->set($set);
1332 }
WanWizard7219c072011-12-28 14:09:05 +01001333
Kyle Farris0c147b32011-08-26 02:29:31 -04001334 if ($this->_validate_insert($table) === FALSE)
1335 {
1336 return FALSE;
1337 }
WanWizard7219c072011-12-28 14:09:05 +01001338
Kyle Farris76116012011-08-31 11:17:48 -04001339 $sql = $this->_insert(
1340 $this->_protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001341 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001342 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001343 array_keys($this->qb_set),
1344 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001345 );
Barry Mienydd671972010-10-04 16:33:58 +02001346
Kyle Farris0c147b32011-08-26 02:29:31 -04001347 $this->_reset_write();
1348 return $this->query($sql);
1349 }
WanWizard7219c072011-12-28 14:09:05 +01001350
Kyle Farris0c147b32011-08-26 02:29:31 -04001351 // --------------------------------------------------------------------
1352
1353 /**
1354 * Validate Insert
1355 *
1356 * This method is used by both insert() and get_compiled_insert() to
1357 * validate that the there data is actually being set and that table
1358 * has been chosen to be inserted into.
1359 *
1360 * @access public
1361 * @param string the table to insert data into
1362 * @return string
1363 */
WanWizard7219c072011-12-28 14:09:05 +01001364 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001365 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001366 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001368 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001369 }
1370
1371 if ($table == '')
1372 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001373 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001374 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001375 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001377 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001378 else
1379 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001380 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001381 }
WanWizard7219c072011-12-28 14:09:05 +01001382
Kyle Farris0c147b32011-08-26 02:29:31 -04001383 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001384 }
Barry Mienydd671972010-10-04 16:33:58 +02001385
Phil Sturgeon9789f322011-07-15 15:14:05 -06001386 // --------------------------------------------------------------------
1387
1388 /**
1389 * Replace
1390 *
1391 * Compiles an replace into string and runs the query
1392 *
1393 * @param string the table to replace data into
1394 * @param array an associative array of insert values
1395 * @return object
1396 */
1397 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001398 {
1399 if ( ! is_null($set))
1400 {
1401 $this->set($set);
1402 }
Barry Mienydd671972010-10-04 16:33:58 +02001403
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001404 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001405 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001406 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001407 }
1408
1409 if ($table == '')
1410 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001411 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001412 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001413 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001414 }
Barry Mienydd671972010-10-04 16:33:58 +02001415
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001416 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001417 }
1418
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001419 $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 +00001420
Derek Jonesd10e8962010-03-02 17:10:36 -06001421 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001422 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001423 }
WanWizard7219c072011-12-28 14:09:05 +01001424
Kyle Farris0c147b32011-08-26 02:29:31 -04001425 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001426
Kyle Farris0c147b32011-08-26 02:29:31 -04001427 /**
1428 * Get UPDATE query string
1429 *
1430 * Compiles an update query and returns the sql
1431 *
1432 * @access public
1433 * @param string the table to update
1434 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1435 * @return string
1436 */
1437 public function get_compiled_update($table = '', $reset = TRUE)
1438 {
1439 // Combine any cached components with the current statements
1440 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001441
Kyle Farris0c147b32011-08-26 02:29:31 -04001442 if ($this->_validate_update($table) === FALSE)
1443 {
1444 return FALSE;
1445 }
WanWizard7219c072011-12-28 14:09:05 +01001446
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001447 $sql = $this->_update($this->_protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set, $this->qb_where, $this->qb_orderby, $this->qb_limit);
WanWizard7219c072011-12-28 14:09:05 +01001448
Kyle Farris0c147b32011-08-26 02:29:31 -04001449 if ($reset === TRUE)
1450 {
1451 $this->_reset_write();
1452 }
WanWizard7219c072011-12-28 14:09:05 +01001453
Kyle Farris0c147b32011-08-26 02:29:31 -04001454 return $sql;
1455 }
WanWizard7219c072011-12-28 14:09:05 +01001456
Derek Allard2067d1a2008-11-13 22:59:24 +00001457 // --------------------------------------------------------------------
1458
1459 /**
1460 * Update
1461 *
1462 * Compiles an update string and runs the query
1463 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001464 * @param string the table to retrieve the results from
1465 * @param array an associative array of update values
1466 * @param mixed the where clause
1467 * @return object
1468 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001469 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001470 {
1471 // Combine any cached components with the current statements
1472 $this->_merge_cache();
1473
1474 if ( ! is_null($set))
1475 {
1476 $this->set($set);
1477 }
Barry Mienydd671972010-10-04 16:33:58 +02001478
Kyle Farris0c147b32011-08-26 02:29:31 -04001479 if ($this->_validate_update($table) === FALSE)
1480 {
1481 return FALSE;
1482 }
1483
1484 if ($where != NULL)
1485 {
1486 $this->where($where);
1487 }
1488
1489 if ($limit != NULL)
1490 {
1491 $this->limit($limit);
1492 }
1493
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001494 $sql = $this->_update($this->_protect_identifiers($this->qb_from[0], TRUE, NULL, FALSE), $this->qb_set, $this->qb_where, $this->qb_orderby, $this->qb_limit, $this->qb_like);
Jamie Rumbelow3b1355c2012-03-06 21:27:46 +00001495
Kyle Farris0c147b32011-08-26 02:29:31 -04001496 $this->_reset_write();
1497 return $this->query($sql);
1498 }
WanWizard7219c072011-12-28 14:09:05 +01001499
Kyle Farris0c147b32011-08-26 02:29:31 -04001500 // --------------------------------------------------------------------
1501
1502 /**
1503 * Validate Update
1504 *
1505 * This method is used by both update() and get_compiled_update() to
1506 * validate that data is actually being set and that a table has been
1507 * chosen to be update.
1508 *
1509 * @access public
1510 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001511 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001512 */
1513 protected function _validate_update($table = '')
1514 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001515 if (count($this->qb_set) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001516 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001517 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001518 }
1519
1520 if ($table == '')
1521 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001522 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001524 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001526 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001527 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001528 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001529 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001530 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001531
1532 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001533 }
WanWizard7219c072011-12-28 14:09:05 +01001534
Derek Jonesd10e8962010-03-02 17:10:36 -06001535 // --------------------------------------------------------------------
1536
1537 /**
1538 * Update_Batch
1539 *
1540 * Compiles an update string and runs the query
1541 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001542 * @param string the table to retrieve the results from
1543 * @param array an associative array of update values
1544 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001545 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001546 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001547 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001548 {
1549 // Combine any cached components with the current statements
1550 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001551
Derek Jonesd10e8962010-03-02 17:10:36 -06001552 if (is_null($index))
1553 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001554 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001555 }
1556
1557 if ( ! is_null($set))
1558 {
1559 $this->set_update_batch($set, $index);
1560 }
1561
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001562 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001563 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001564 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001565 }
1566
1567 if ($table == '')
1568 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001569 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001570 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001571 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001572 }
Barry Mienydd671972010-10-04 16:33:58 +02001573
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001574 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001575 }
Barry Mienydd671972010-10-04 16:33:58 +02001576
Derek Jonesd10e8962010-03-02 17:10:36 -06001577 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001578 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001579 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001580 $this->query($this->_update_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set, $i, 100), $this->_protect_identifiers($index), $this->qb_where));
Derek Jonesd10e8962010-03-02 17:10:36 -06001581 }
Barry Mienydd671972010-10-04 16:33:58 +02001582
Derek Jonesd10e8962010-03-02 17:10:36 -06001583 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001584 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001585 }
1586
1587 // --------------------------------------------------------------------
1588
1589 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001590 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001591 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001592 * @param array
1593 * @param string
1594 * @param boolean
1595 * @return object
1596 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001597 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001598 {
1599 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001600
Derek Jonesd10e8962010-03-02 17:10:36 -06001601 if ( ! is_array($key))
1602 {
1603 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001604 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001605
1606 foreach ($key as $k => $v)
1607 {
1608 $index_set = FALSE;
1609 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001610 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001611 {
1612 if ($k2 == $index)
1613 {
1614 $index_set = TRUE;
1615 }
1616 else
1617 {
1618 $not[] = $k.'-'.$v;
1619 }
1620
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001621 $clean[$this->protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001622 }
1623
1624 if ($index_set == FALSE)
1625 {
1626 return $this->display_error('db_batch_missing_index');
1627 }
1628
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001629 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001630 }
Barry Mienydd671972010-10-04 16:33:58 +02001631
Derek Jonesd10e8962010-03-02 17:10:36 -06001632 return $this;
1633 }
1634
Derek Allard2067d1a2008-11-13 22:59:24 +00001635 // --------------------------------------------------------------------
1636
1637 /**
1638 * Empty Table
1639 *
1640 * Compiles a delete string and runs "DELETE FROM table"
1641 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001642 * @param string the table to empty
1643 * @return object
1644 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001645 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001646 {
1647 if ($table == '')
1648 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001649 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001650 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001651 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001652 }
1653
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001654 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001655 }
1656 else
1657 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001658 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001659 }
1660
1661 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001662 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001663 return $this->query($sql);
1664 }
1665
1666 // --------------------------------------------------------------------
1667
1668 /**
1669 * Truncate
1670 *
1671 * Compiles a truncate string and runs the query
1672 * If the database does not support the truncate() command
1673 * This function maps to "DELETE FROM table"
1674 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001675 * @param string the table to truncate
1676 * @return object
1677 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001678 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001679 {
1680 if ($table == '')
1681 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001682 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001683 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001684 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001685 }
1686
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001687 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001688 }
1689 else
1690 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001691 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001692 }
1693
1694 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001695 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001696 return $this->query($sql);
1697 }
WanWizard7219c072011-12-28 14:09:05 +01001698
Kyle Farris0c147b32011-08-26 02:29:31 -04001699 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001700
Kyle Farris0c147b32011-08-26 02:29:31 -04001701 /**
1702 * Get DELETE query string
1703 *
1704 * Compiles a delete query string and returns the sql
1705 *
1706 * @access public
1707 * @param string the table to delete from
1708 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1709 * @return string
1710 */
1711 public function get_compiled_delete($table = '', $reset = TRUE)
1712 {
1713 $this->return_delete_sql = TRUE;
1714 $sql = $this->delete($table, '', NULL, $reset);
1715 $this->return_delete_sql = FALSE;
1716 return $sql;
1717 }
WanWizard7219c072011-12-28 14:09:05 +01001718
Derek Allard2067d1a2008-11-13 22:59:24 +00001719 // --------------------------------------------------------------------
1720
1721 /**
1722 * Delete
1723 *
1724 * Compiles a delete string and runs the query
1725 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001726 * @param mixed the table(s) to delete from. String or array
1727 * @param mixed the where clause
1728 * @param mixed the limit clause
1729 * @param boolean
1730 * @return object
1731 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001732 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001733 {
1734 // Combine any cached components with the current statements
1735 $this->_merge_cache();
1736
1737 if ($table == '')
1738 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001739 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001740 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001741 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001742 }
1743
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001744 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001745 }
1746 elseif (is_array($table))
1747 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001748 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 {
1750 $this->delete($single_table, $where, $limit, FALSE);
1751 }
1752
1753 $this->_reset_write();
1754 return;
1755 }
1756 else
1757 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001758 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001759 }
1760
1761 if ($where != '')
1762 {
1763 $this->where($where);
1764 }
1765
1766 if ($limit != NULL)
1767 {
1768 $this->limit($limit);
1769 }
1770
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001771 if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001773 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001774 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001775
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001776 $sql = $this->_delete($table, $this->qb_where, $this->qb_like, $this->qb_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001777 if ($reset_data)
1778 {
1779 $this->_reset_write();
1780 }
WanWizard7219c072011-12-28 14:09:05 +01001781
Andrey Andreev24276a32012-01-08 02:44:38 +02001782 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001783 }
WanWizard7219c072011-12-28 14:09:05 +01001784
Derek Allard2067d1a2008-11-13 22:59:24 +00001785 // --------------------------------------------------------------------
1786
1787 /**
1788 * DB Prefix
1789 *
1790 * Prepends a database prefix if one exists in configuration
1791 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001792 * @param string the table
1793 * @return string
1794 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001795 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001796 {
1797 if ($table == '')
1798 {
1799 $this->display_error('db_table_name_required');
1800 }
1801
1802 return $this->dbprefix.$table;
1803 }
1804
1805 // --------------------------------------------------------------------
1806
1807 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001808 * Set DB Prefix
1809 *
1810 * Set's the DB Prefix to something new without needing to reconnect
1811 *
1812 * @param string the prefix
1813 * @return string
1814 */
1815 public function set_dbprefix($prefix = '')
1816 {
1817 return $this->dbprefix = $prefix;
1818 }
1819
1820 // --------------------------------------------------------------------
1821
1822 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001823 * Track Aliases
1824 *
1825 * Used to track SQL statements written with aliased tables.
1826 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 * @param string The table to inspect
1828 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001829 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001830 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001831 {
1832 if (is_array($table))
1833 {
1834 foreach ($table as $t)
1835 {
1836 $this->_track_aliases($t);
1837 }
1838 return;
1839 }
Barry Mienydd671972010-10-04 16:33:58 +02001840
Derek Jones37f4b9c2011-07-01 17:56:50 -05001841 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001842 // the string into discreet statements
1843 if (strpos($table, ',') !== FALSE)
1844 {
1845 return $this->_track_aliases(explode(',', $table));
1846 }
Barry Mienydd671972010-10-04 16:33:58 +02001847
Derek Allard2067d1a2008-11-13 22:59:24 +00001848 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001849 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001850 {
1851 // if the alias is written with the AS keyword, remove it
1852 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001853
Derek Allard2067d1a2008-11-13 22:59:24 +00001854 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001855 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001856
Derek Allard2067d1a2008-11-13 22:59:24 +00001857 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001858 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001859 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001860 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001861 }
1862 }
1863 }
WanWizard7219c072011-12-28 14:09:05 +01001864
Derek Allard2067d1a2008-11-13 22:59:24 +00001865 // --------------------------------------------------------------------
1866
1867 /**
1868 * Compile the SELECT statement
1869 *
1870 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001871 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001872 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001873 * @return string
1874 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001875 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001876 {
1877 // Combine any cached components with the current statements
1878 $this->_merge_cache();
1879
Derek Allard2067d1a2008-11-13 22:59:24 +00001880 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001881 if ($select_override !== FALSE)
1882 {
1883 $sql = $select_override;
1884 }
1885 else
1886 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001887 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001888
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001889 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001890 {
Barry Mienydd671972010-10-04 16:33:58 +02001891 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001892 }
1893 else
Barry Mienydd671972010-10-04 16:33:58 +02001894 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001895 // Cycle through the "select" portion of the query and prep each column name.
1896 // The reason we protect identifiers here rather then in the select() function
1897 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001898 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001899 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001900 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
1901 $this->qb_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001902 }
Barry Mienydd671972010-10-04 16:33:58 +02001903
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001904 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00001905 }
1906 }
1907
Derek Allard2067d1a2008-11-13 22:59:24 +00001908 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001909 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001910 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001911 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00001912 }
1913
Derek Allard2067d1a2008-11-13 22:59:24 +00001914 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001915 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001916 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001917 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00001918 }
1919
Derek Allard2067d1a2008-11-13 22:59:24 +00001920 // Write the "WHERE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001921 if (count($this->qb_where) > 0 OR count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001922 {
Greg Akere156c6e2011-04-20 16:03:04 -05001923 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00001924 }
1925
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001926 $sql .= implode("\n", $this->qb_where);
Derek Allard2067d1a2008-11-13 22:59:24 +00001927
Derek Allard2067d1a2008-11-13 22:59:24 +00001928 // Write the "LIKE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001929 if (count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001930 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001931 if (count($this->qb_where) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001932 {
1933 $sql .= "\nAND ";
1934 }
1935
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001936 $sql .= implode("\n", $this->qb_like);
Derek Allard2067d1a2008-11-13 22:59:24 +00001937 }
1938
Derek Allard2067d1a2008-11-13 22:59:24 +00001939 // Write the "GROUP BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001940 if (count($this->qb_groupby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001941 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001942 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00001943 }
1944
Derek Allard2067d1a2008-11-13 22:59:24 +00001945 // Write the "HAVING" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001946 if (count($this->qb_having) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001947 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001948 $sql .= "\nHAVING ".implode("\n", $this->qb_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00001949 }
1950
Derek Allard2067d1a2008-11-13 22:59:24 +00001951 // Write the "ORDER BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001952 if (count($this->qb_orderby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001953 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001954 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
1955 if ($this->qb_order !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001956 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001957 $sql .= ($this->qb_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02001958 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001959 }
1960
Derek Allard2067d1a2008-11-13 22:59:24 +00001961 // Write the "LIMIT" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001962 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001963 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001964 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00001965 }
1966
1967 return $sql;
1968 }
1969
1970 // --------------------------------------------------------------------
1971
1972 /**
1973 * Object to Array
1974 *
1975 * Takes an object as input and converts the class variables to array key/vals
1976 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001977 * @param object
1978 * @return array
1979 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001980 public function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001981 {
1982 if ( ! is_object($object))
1983 {
1984 return $object;
1985 }
Barry Mienydd671972010-10-04 16:33:58 +02001986
Derek Allard2067d1a2008-11-13 22:59:24 +00001987 $array = array();
1988 foreach (get_object_vars($object) as $key => $val)
1989 {
1990 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06001991 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00001992 {
1993 $array[$key] = $val;
1994 }
1995 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001996
1997 return $array;
1998 }
Barry Mienydd671972010-10-04 16:33:58 +02001999
Derek Jonesd10e8962010-03-02 17:10:36 -06002000 // --------------------------------------------------------------------
2001
2002 /**
2003 * Object to Array
2004 *
2005 * Takes an object as input and converts the class variables to array key/vals
2006 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002007 * @param object
2008 * @return array
2009 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002010 public function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002011 {
2012 if ( ! is_object($object))
2013 {
2014 return $object;
2015 }
Barry Mienydd671972010-10-04 16:33:58 +02002016
Derek Jonesd10e8962010-03-02 17:10:36 -06002017 $array = array();
2018 $out = get_object_vars($object);
2019 $fields = array_keys($out);
2020
2021 foreach ($fields as $val)
2022 {
2023 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002024 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002025 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002026 $i = 0;
2027 foreach ($out[$val] as $data)
2028 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002029 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002030 }
2031 }
2032 }
2033
Derek Allard2067d1a2008-11-13 22:59:24 +00002034 return $array;
2035 }
Barry Mienydd671972010-10-04 16:33:58 +02002036
Derek Allard2067d1a2008-11-13 22:59:24 +00002037 // --------------------------------------------------------------------
2038
2039 /**
2040 * Start Cache
2041 *
2042 * Starts AR caching
2043 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002044 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002045 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002046 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002047 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002048 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002049 }
2050
2051 // --------------------------------------------------------------------
2052
2053 /**
2054 * Stop Cache
2055 *
2056 * Stops AR caching
2057 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002058 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002059 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002060 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002061 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002062 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002063 }
2064
2065 // --------------------------------------------------------------------
2066
2067 /**
2068 * Flush Cache
2069 *
2070 * Empties the AR cache
2071 *
2072 * @access public
2073 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002074 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002075 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002076 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002077 $this->_reset_run(array(
2078 'ar_cache_select' => array(),
2079 'ar_cache_from' => array(),
2080 'ar_cache_join' => array(),
2081 'ar_cache_where' => array(),
2082 'ar_cache_like' => array(),
2083 'ar_cache_groupby' => array(),
2084 'ar_cache_having' => array(),
2085 'ar_cache_orderby' => array(),
2086 'ar_cache_set' => array(),
2087 'ar_cache_exists' => array(),
2088 'ar_cache_no_escape' => array()
2089 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002090 }
2091
2092 // --------------------------------------------------------------------
2093
2094 /**
2095 * Merge Cache
2096 *
Barry Mienydd671972010-10-04 16:33:58 +02002097 * When called, this function merges any cached AR arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002098 * locally called ones.
2099 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002100 * @return void
2101 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002102 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002103 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002104 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002105 {
2106 return;
2107 }
2108
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002109 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002110 {
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002111 $qb_variable = 'qb_'.$val;
2112 $qb_cache_var = 'qb_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002113
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002114 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002115 {
2116 continue;
2117 }
2118
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002119 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002120 }
2121
2122 // If we are "protecting identifiers" we need to examine the "from"
2123 // portion of the query to determine if there are any aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002124 if ($this->_protect_identifiers === TRUE AND count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002125 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002126 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002127 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002128
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002129 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002130 }
WanWizard7219c072011-12-28 14:09:05 +01002131
Kyle Farris0c147b32011-08-26 02:29:31 -04002132 // --------------------------------------------------------------------
2133
2134 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002135 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002136 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002137 * Publicly-visible method to reset the AR values.
2138 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002139 * @return void
2140 */
2141 public function reset_query()
2142 {
2143 $this->_reset_select();
2144 $this->_reset_write();
2145 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002146
2147 // --------------------------------------------------------------------
2148
2149 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002150 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002151 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002152 * @param array An array of fields to reset
2153 * @return void
2154 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002155 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002156 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002157 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002158 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002159 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002160 {
2161 $this->$item = $default_value;
2162 }
2163 }
2164 }
2165
2166 // --------------------------------------------------------------------
2167
2168 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002169 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002170 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002171 * @return void
2172 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002173 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002174 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002175 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002176 'qb_select' => array(),
2177 'qb_from' => array(),
2178 'qb_join' => array(),
2179 'qb_where' => array(),
2180 'qb_like' => array(),
2181 'qb_groupby' => array(),
2182 'qb_having' => array(),
2183 'qb_orderby' => array(),
2184 'qb_wherein' => array(),
2185 'qb_aliased_tables' => array(),
2186 'qb_no_escape' => array(),
2187 'qb_distinct' => FALSE,
2188 'qb_limit' => FALSE,
2189 'qb_offset' => FALSE,
2190 'qb_order' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002191 )
2192 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002193 }
Barry Mienydd671972010-10-04 16:33:58 +02002194
Derek Allard2067d1a2008-11-13 22:59:24 +00002195 // --------------------------------------------------------------------
2196
2197 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002198 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002199 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002200 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002201 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002202 * @return void
2203 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002204 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002205 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002206 $this->_reset_run(array(
Jamie Rumbelowae123e02012-02-21 16:39:56 +00002207 'qb_set' => array(),
2208 'qb_from' => array(),
2209 'qb_where' => array(),
2210 'qb_like' => array(),
2211 'qb_orderby' => array(),
2212 'qb_keys' => array(),
2213 'qb_limit' => FALSE,
2214 'qb_order' => FALSE
Andrey Andreev24276a32012-01-08 02:44:38 +02002215 )
2216 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002217 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002218
Derek Allard2067d1a2008-11-13 22:59:24 +00002219}
2220
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002221/* End of file DB_query_builder.php */
2222/* Location: ./system/database/DB_query_builder.php */