blob: b6ad0f3d83ce21d543b0640a1993dbe547fc6337 [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 {
239 return end(explode('.', $item));
240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 return $item;
243 }
244
245 // --------------------------------------------------------------------
246
247 /**
248 * DISTINCT
249 *
250 * Sets a flag which tells the query string compiler to add DISTINCT
251 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 * @param bool
253 * @return object
254 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600255 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000257 $this->qb_distinct = (is_bool($val)) ? $val : TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 return $this;
259 }
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 // --------------------------------------------------------------------
262
263 /**
264 * From
265 *
266 * Generates the FROM portion of the query
267 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 * @param mixed can be a string or array
269 * @return object
270 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600271 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
273 foreach ((array)$from as $val)
274 {
275 if (strpos($val, ',') !== FALSE)
276 {
277 foreach (explode(',', $val) as $v)
278 {
279 $v = trim($v);
280 $this->_track_aliases($v);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000281 $v = $this->qb_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200282
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000283 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000285 $this->qb_cache_from[] = $v;
286 $this->qb_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200287 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
290 else
291 {
292 $val = trim($val);
293
Andrey Andreev24276a32012-01-08 02:44:38 +0200294 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200295 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 $this->_track_aliases($val);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000297 $this->qb_from[] = $val = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200298
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000299 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000301 $this->qb_cache_from[] = $val;
302 $this->qb_cache_exists[] = 'from';
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
304 }
305 }
306
307 return $this;
308 }
309
310 // --------------------------------------------------------------------
311
312 /**
313 * Join
314 *
315 * Generates the JOIN portion of the query
316 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 * @param string
318 * @param string the join condition
319 * @param string the type of join
320 * @return object
321 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600322 public function join($table, $cond, $type = '')
Barry Mienydd671972010-10-04 16:33:58 +0200323 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 if ($type != '')
325 {
326 $type = strtoupper(trim($type));
327
328 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
329 {
330 $type = '';
331 }
332 else
333 {
334 $type .= ' ';
335 }
336 }
337
Andrey Andreev24276a32012-01-08 02:44:38 +0200338 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200339 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 $this->_track_aliases($table);
341
342 // Strip apart the condition and protect the identifiers
343 if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
344 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200345 $cond = $this->_protect_identifiers($match[1]).$match[2].$this->_protect_identifiers($match[3]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // Assemble the JOIN statement
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000349 $this->qb_join[] = $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000350
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000351 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000353 $this->qb_cache_join[] = $join;
354 $this->qb_cache_exists[] = 'join';
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
356
357 return $this;
358 }
359
360 // --------------------------------------------------------------------
361
362 /**
363 * Where
364 *
365 * Generates the WHERE portion of the query. Separates
366 * multiple calls with AND
367 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 * @param mixed
369 * @param mixed
370 * @return object
371 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600372 public function where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
374 return $this->_where($key, $value, 'AND ', $escape);
375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 // --------------------------------------------------------------------
378
379 /**
380 * OR Where
381 *
382 * Generates the WHERE portion of the query. Separates
383 * multiple calls with OR
384 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 * @param mixed
386 * @param mixed
387 * @return object
388 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600389 public function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
391 return $this->_where($key, $value, 'OR ', $escape);
392 }
393
394 // --------------------------------------------------------------------
395
396 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * Where
398 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600399 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 * @param mixed
402 * @param mixed
403 * @param string
404 * @return object
405 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600406 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 {
WanWizard7219c072011-12-28 14:09:05 +0100408 $type = $this->_group_get_type($type);
409
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 if ( ! is_array($key))
411 {
412 $key = array($key => $value);
413 }
Barry Mienydd671972010-10-04 16:33:58 +0200414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 // If the escape value was not set will will base it on the global setting
416 if ( ! is_bool($escape))
417 {
418 $escape = $this->_protect_identifiers;
419 }
420
421 foreach ($key as $k => $v)
422 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000423 $prefix = (count($this->qb_where) === 0 AND count($this->qb_cache_where) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000424
425 if (is_null($v) && ! $this->_has_operator($k))
426 {
427 // value appears not to have been set, assign the test to IS NULL
428 $k .= ' IS NULL';
429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 if ( ! is_null($v))
432 {
433 if ($escape === TRUE)
434 {
435 $k = $this->_protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 $v = ' '.$this->escape($v);
437 }
WanWizard7219c072011-12-28 14:09:05 +0100438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 if ( ! $this->_has_operator($k))
440 {
Greg Akere156c6e2011-04-20 16:03:04 -0500441 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 }
443 }
444 else
445 {
Barry Mienydd671972010-10-04 16:33:58 +0200446 $k = $this->_protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 }
448
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000449 $this->qb_where[] = $prefix.$k.$v;
450 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000452 $this->qb_cache_where[] = $prefix.$k.$v;
453 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 return $this;
459 }
460
461 // --------------------------------------------------------------------
462
463 /**
464 * Where_in
465 *
466 * Generates a WHERE field IN ('item', 'item') SQL query joined with
467 * AND if appropriate
468 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 * @param string The field to search
470 * @param array The values searched on
471 * @return object
472 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600473 public function where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
475 return $this->_where_in($key, $values);
476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 // --------------------------------------------------------------------
479
480 /**
481 * Where_in_or
482 *
483 * Generates a WHERE field IN ('item', 'item') SQL query joined with
484 * OR if appropriate
485 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 * @param string The field to search
487 * @param array The values searched on
488 * @return object
489 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600490 public function or_where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
492 return $this->_where_in($key, $values, FALSE, 'OR ');
493 }
494
495 // --------------------------------------------------------------------
496
497 /**
498 * Where_not_in
499 *
500 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
501 * with AND if appropriate
502 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 * @param string The field to search
504 * @param array The values searched on
505 * @return object
506 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600507 public function where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
509 return $this->_where_in($key, $values, TRUE);
510 }
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 // --------------------------------------------------------------------
513
514 /**
515 * Where_not_in_or
516 *
517 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
518 * with OR if appropriate
519 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 * @param string The field to search
521 * @param array The values searched on
522 * @return object
523 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600524 public function or_where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
526 return $this->_where_in($key, $values, TRUE, 'OR ');
527 }
528
529 // --------------------------------------------------------------------
530
531 /**
532 * Where_in
533 *
534 * Called by where_in, where_in_or, where_not_in, where_not_in_or
535 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 * @param string The field to search
537 * @param array The values searched on
538 * @param boolean If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200539 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 * @return object
541 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600542 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 {
544 if ($key === NULL OR $values === NULL)
545 {
546 return;
547 }
Barry Mienydd671972010-10-04 16:33:58 +0200548
WanWizard7219c072011-12-28 14:09:05 +0100549 $type = $this->_group_get_type($type);
550
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 if ( ! is_array($values))
552 {
553 $values = array($values);
554 }
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 $not = ($not) ? ' NOT' : '';
557
558 foreach ($values as $value)
559 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000560 $this->qb_wherein[] = $this->escape($value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 }
562
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000563 $prefix = (count($this->qb_where) === 0) ? '' : $type;
564 $this->qb_where[] = $where_in = $prefix.$this->_protect_identifiers($key).$not.' IN ('.implode(', ', $this->qb_wherein).') ';
Barry Mienydd671972010-10-04 16:33:58 +0200565
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000566 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000568 $this->qb_cache_where[] = $where_in;
569 $this->qb_cache_exists[] = 'where';
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 }
571
572 // reset the array for multiple calls
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000573 $this->qb_wherein = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 return $this;
575 }
Barry Mienydd671972010-10-04 16:33:58 +0200576
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 // --------------------------------------------------------------------
578
579 /**
580 * Like
581 *
582 * Generates a %LIKE% portion of the query. Separates
583 * multiple calls with AND
584 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 * @param mixed
586 * @param mixed
587 * @return object
588 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600589 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 {
591 return $this->_like($field, $match, 'AND ', $side);
592 }
593
594 // --------------------------------------------------------------------
595
596 /**
597 * Not Like
598 *
599 * Generates a NOT LIKE portion of the query. Separates
600 * multiple calls with AND
601 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 * @param mixed
603 * @param mixed
604 * @return object
605 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600606 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 {
608 return $this->_like($field, $match, 'AND ', $side, 'NOT');
609 }
Barry Mienydd671972010-10-04 16:33:58 +0200610
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 // --------------------------------------------------------------------
612
613 /**
614 * OR Like
615 *
616 * Generates a %LIKE% portion of the query. Separates
617 * multiple calls with OR
618 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 * @param mixed
620 * @param mixed
621 * @return object
622 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600623 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 {
625 return $this->_like($field, $match, 'OR ', $side);
626 }
627
628 // --------------------------------------------------------------------
629
630 /**
631 * OR Not Like
632 *
633 * Generates a NOT LIKE portion of the query. Separates
634 * multiple calls with OR
635 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 * @param mixed
637 * @param mixed
638 * @return object
639 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600640 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 {
642 return $this->_like($field, $match, 'OR ', $side, 'NOT');
643 }
Barry Mienydd671972010-10-04 16:33:58 +0200644
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 // --------------------------------------------------------------------
646
647 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 * Like
649 *
650 * Called by like() or orlike()
651 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 * @param mixed
653 * @param mixed
654 * @param string
655 * @return object
656 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600657 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 {
WanWizard7219c072011-12-28 14:09:05 +0100659 $type = $this->_group_get_type($type);
660
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 if ( ! is_array($field))
662 {
663 $field = array($field => $match);
664 }
Barry Mienydd671972010-10-04 16:33:58 +0200665
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 foreach ($field as $k => $v)
667 {
668 $k = $this->_protect_identifiers($k);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000669 $prefix = (count($this->qb_like) === 0) ? '' : $type;
Derek Jonese4ed5832009-02-20 21:44:59 +0000670 $v = $this->escape_like_str($v);
Kyle Farris0c147b32011-08-26 02:29:31 -0400671
Andrey Andreev24276a32012-01-08 02:44:38 +0200672 if ($side === 'none')
Kyle Farris81ef70f2011-08-31 11:59:12 -0400673 {
674 $like_statement = $prefix." $k $not LIKE '{$v}'";
675 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200676 elseif ($side === 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 {
678 $like_statement = $prefix." $k $not LIKE '%{$v}'";
679 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200680 elseif ($side === 'after')
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 {
682 $like_statement = $prefix." $k $not LIKE '{$v}%'";
683 }
684 else
685 {
686 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
687 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600688
Derek Jonese4ed5832009-02-20 21:44:59 +0000689 // some platforms require an escape sequence definition for LIKE wildcards
690 if ($this->_like_escape_str != '')
691 {
Greg Aker0d424892010-01-26 02:14:44 +0000692 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000693 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600694
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000695 $this->qb_like[] = $like_statement;
696 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000698 $this->qb_cache_like[] = $like_statement;
699 $this->qb_cache_exists[] = 'like';
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 }
Barry Mienydd671972010-10-04 16:33:58 +0200701
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200703
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 return $this;
705 }
Barry Mienydd671972010-10-04 16:33:58 +0200706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 // --------------------------------------------------------------------
708
709 /**
WanWizard7219c072011-12-28 14:09:05 +0100710 * Starts a query group.
711 *
712 * @param string (Internal use only)
713 * @param string (Internal use only)
714 * @return object
715 */
716 public function group_start($not = '', $type = 'AND ')
717 {
718 $type = $this->_group_get_type($type);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000719 $this->qb_where_group_started = TRUE;
720 $prefix = (count($this->qb_where) === 0 AND count($this->qb_cache_where) === 0) ? '' : $type;
721 $this->qb_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (';
WanWizard7219c072011-12-28 14:09:05 +0100722
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000723 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100724 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000725 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100726 }
727
728 return $this;
729 }
730
731 // --------------------------------------------------------------------
732
733 /**
734 * Starts a query group, but ORs the group
735 *
736 * @return object
737 */
738 public function or_group_start()
739 {
740 return $this->group_start('', 'OR ');
741 }
742
743 // --------------------------------------------------------------------
744
745 /**
746 * Starts a query group, but NOTs the group
747 *
748 * @return object
749 */
750 public function not_group_start()
751 {
752 return $this->group_start('NOT ', 'AND ');
753 }
754
755 // --------------------------------------------------------------------
756
757 /**
758 * Starts a query group, but OR NOTs the group
759 *
760 * @return object
761 */
762 public function or_not_group_start()
763 {
764 return $this->group_start('NOT ', 'OR ');
765 }
766
767 // --------------------------------------------------------------------
768
769 /**
770 * Ends a query group
771 *
772 * @return object
773 */
774 public function group_end()
775 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000776 $this->qb_where_group_started = FALSE;
777 $this->qb_where[] = $value = str_repeat(' ', $this->qb_where_group_count--) . ')';
WanWizard7219c072011-12-28 14:09:05 +0100778
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000779 if ($this->qb_caching)
WanWizard7219c072011-12-28 14:09:05 +0100780 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000781 $this->qb_cache_where[] = $value;
WanWizard7219c072011-12-28 14:09:05 +0100782 }
783
WanWizard7219c072011-12-28 14:09:05 +0100784 return $this;
785 }
786
787 // --------------------------------------------------------------------
788
789 /**
790 * Group_get_type
791 *
792 * Called by group_start(), _like(), _where() and _where_in()
793 *
794 * @param string
795 * @return string
796 */
797 protected function _group_get_type($type)
798 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000799 if ($this->qb_where_group_started)
WanWizard7219c072011-12-28 14:09:05 +0100800 {
801 $type = '';
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000802 $this->qb_where_group_started = FALSE;
WanWizard7219c072011-12-28 14:09:05 +0100803 }
804
805 return $type;
806 }
807
808 // --------------------------------------------------------------------
809
810 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 * GROUP BY
812 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 * @param string
814 * @return object
815 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600816 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 {
818 if (is_string($by))
819 {
820 $by = explode(',', $by);
821 }
Barry Mienydd671972010-10-04 16:33:58 +0200822
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 foreach ($by as $val)
824 {
825 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200826
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 if ($val != '')
828 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000829 $this->qb_groupby[] = $val = $this->_protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200830
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000831 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000833 $this->qb_cache_groupby[] = $val;
834 $this->qb_cache_exists[] = 'groupby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 }
836 }
837 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200838
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 return $this;
840 }
841
842 // --------------------------------------------------------------------
843
844 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 * Sets the HAVING value
846 *
847 * Separates multiple calls with AND
848 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 * @param string
850 * @param string
851 * @return object
852 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600853 public function having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 {
855 return $this->_having($key, $value, 'AND ', $escape);
856 }
Barry Mienydd671972010-10-04 16:33:58 +0200857
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 // --------------------------------------------------------------------
859
860 /**
861 * Sets the OR HAVING value
862 *
863 * Separates multiple calls with OR
864 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 * @param string
866 * @param string
867 * @return object
868 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600869 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 {
871 return $this->_having($key, $value, 'OR ', $escape);
872 }
Barry Mienydd671972010-10-04 16:33:58 +0200873
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 // --------------------------------------------------------------------
875
876 /**
877 * Sets the HAVING values
878 *
879 * Called by having() or or_having()
880 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000881 * @param string
882 * @param string
883 * @return object
884 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600885 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 {
887 if ( ! is_array($key))
888 {
889 $key = array($key => $value);
890 }
Barry Mienydd671972010-10-04 16:33:58 +0200891
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 foreach ($key as $k => $v)
893 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000894 $prefix = (count($this->qb_having) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000895
896 if ($escape === TRUE)
897 {
898 $k = $this->_protect_identifiers($k);
899 }
900
901 if ( ! $this->_has_operator($k))
902 {
903 $k .= ' = ';
904 }
905
906 if ($v != '')
907 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400908 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 }
Barry Mienydd671972010-10-04 16:33:58 +0200910
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000911 $this->qb_having[] = $prefix.$k.$v;
912 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000914 $this->qb_cache_having[] = $prefix.$k.$v;
915 $this->qb_cache_exists[] = 'having';
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 }
917 }
Barry Mienydd671972010-10-04 16:33:58 +0200918
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 return $this;
920 }
Barry Mienydd671972010-10-04 16:33:58 +0200921
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 // --------------------------------------------------------------------
923
924 /**
925 * Sets the ORDER BY value
926 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 * @param string
928 * @param string direction: asc or desc
pporlan2c685fb2011-12-22 12:15:25 +0100929 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 * @return object
931 */
pporlan2c685fb2011-12-22 12:15:25 +0100932 public function order_by($orderby, $direction = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200934 if (strtolower($direction) === 'random')
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 {
936 $orderby = ''; // Random results want or don't need a field name
937 $direction = $this->_random_keyword;
938 }
939 elseif (trim($direction) != '')
940 {
941 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
942 }
Barry Mienydd671972010-10-04 16:33:58 +0200943
944
Andrey Andreev24276a32012-01-08 02:44:38 +0200945 if ((strpos($orderby, ',') !== FALSE) && $escape === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 {
947 $temp = array();
948 foreach (explode(',', $orderby) as $part)
949 {
950 $part = trim($part);
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000951 if ( ! in_array($part, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 {
953 $part = $this->_protect_identifiers(trim($part));
954 }
Barry Mienydd671972010-10-04 16:33:58 +0200955
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 $temp[] = $part;
957 }
Barry Mienydd671972010-10-04 16:33:58 +0200958
959 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200961 elseif ($direction != $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 {
pporlan2c685fb2011-12-22 12:15:25 +0100963 if ($escape === TRUE)
964 {
965 $orderby = $this->_protect_identifiers($orderby);
966 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 }
Barry Mienydd671972010-10-04 16:33:58 +0200968
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000969 $this->qb_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200970
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000971 if ($this->qb_caching === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000973 $this->qb_cache_orderby[] = $orderby_statement;
974 $this->qb_cache_exists[] = 'orderby';
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 }
976
977 return $this;
978 }
Barry Mienydd671972010-10-04 16:33:58 +0200979
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 // --------------------------------------------------------------------
981
982 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000983 * Sets the LIMIT value
984 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 * @param integer the limit value
986 * @param integer the offset value
987 * @return object
988 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200989 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000991 $this->qb_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000992
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200993 if ( ! is_null($offset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000995 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 }
Barry Mienydd671972010-10-04 16:33:58 +0200997
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 return $this;
999 }
Barry Mienydd671972010-10-04 16:33:58 +02001000
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 // --------------------------------------------------------------------
1002
1003 /**
1004 * Sets the OFFSET value
1005 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 * @param integer the offset value
1007 * @return object
1008 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001009 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001011 $this->qb_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 return $this;
1013 }
Barry Mienydd671972010-10-04 16:33:58 +02001014
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 // --------------------------------------------------------------------
1016
1017 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001018 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 * @param mixed
1021 * @param string
1022 * @param boolean
1023 * @return object
1024 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001025 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 {
1027 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +02001028
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 if ( ! is_array($key))
1030 {
1031 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001032 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001033
1034 foreach ($key as $k => $v)
1035 {
1036 if ($escape === FALSE)
1037 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001038 $this->qb_set[$this->_protect_identifiers($k)] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 }
1040 else
1041 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001042 $this->qb_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 }
1044 }
Barry Mienydd671972010-10-04 16:33:58 +02001045
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 return $this;
1047 }
WanWizard7219c072011-12-28 14:09:05 +01001048
Kyle Farris0c147b32011-08-26 02:29:31 -04001049 // --------------------------------------------------------------------
1050
1051 /**
1052 * Get SELECT query string
1053 *
1054 * Compiles a SELECT query string and returns the sql.
1055 *
1056 * @access public
1057 * @param string the table name to select from (optional)
1058 * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone
1059 * @return string
1060 */
WanWizard7219c072011-12-28 14:09:05 +01001061 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001062 {
1063 if ($table != '')
kylefarris0a3176b2011-08-26 02:37:52 -04001064 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001065 $this->_track_aliases($table);
1066 $this->from($table);
1067 }
WanWizard7219c072011-12-28 14:09:05 +01001068
Kyle Farris0c147b32011-08-26 02:29:31 -04001069 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001070
Kyle Farris0c147b32011-08-26 02:29:31 -04001071 if ($reset === TRUE)
1072 {
1073 $this->_reset_select();
1074 }
WanWizard7219c072011-12-28 14:09:05 +01001075
Kyle Farris0c147b32011-08-26 02:29:31 -04001076 return $select;
1077 }
WanWizard7219c072011-12-28 14:09:05 +01001078
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 // --------------------------------------------------------------------
1080
1081 /**
1082 * Get
1083 *
1084 * Compiles the select statement based on the other functions called
1085 * and runs the query
1086 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 * @param string the table
1088 * @param string the limit clause
1089 * @param string the offset clause
1090 * @return object
1091 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001092 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001093 {
1094 if ($table != '')
1095 {
1096 $this->_track_aliases($table);
1097 $this->from($table);
1098 }
Barry Mienydd671972010-10-04 16:33:58 +02001099
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 if ( ! is_null($limit))
1101 {
1102 $this->limit($limit, $offset);
1103 }
Barry Mienydd671972010-10-04 16:33:58 +02001104
Andrey Andreev24276a32012-01-08 02:44:38 +02001105 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 $this->_reset_select();
1107 return $result;
1108 }
1109
1110 /**
1111 * "Count All Results" query
1112 *
Barry Mienydd671972010-10-04 16:33:58 +02001113 * Generates a platform-specific query string that counts all records
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001114 * returned by an Query Builder query.
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 * @param string
1117 * @return string
1118 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001119 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 {
1121 if ($table != '')
1122 {
1123 $this->_track_aliases($table);
1124 $this->from($table);
1125 }
Barry Mienydd671972010-10-04 16:33:58 +02001126
Andrey Andreev24276a32012-01-08 02:44:38 +02001127 $result = $this->query($this->_compile_select($this->_count_string.$this->_protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001129
Purwandi1d160e72012-01-09 16:33:28 +07001130 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001132 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 }
1134
Purwandi1d160e72012-01-09 16:33:28 +07001135 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001136 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001138 // --------------------------------------------------------------------
1139
1140 /**
1141 * Get_Where
1142 *
1143 * Allows the where clause, limit and offset to be added directly
1144 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 * @param string the where clause
1146 * @param string the limit clause
1147 * @param string the offset clause
1148 * @return object
1149 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001150 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 {
1152 if ($table != '')
1153 {
1154 $this->from($table);
1155 }
1156
1157 if ( ! is_null($where))
1158 {
1159 $this->where($where);
1160 }
Barry Mienydd671972010-10-04 16:33:58 +02001161
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 if ( ! is_null($limit))
1163 {
1164 $this->limit($limit, $offset);
1165 }
Barry Mienydd671972010-10-04 16:33:58 +02001166
Andrey Andreev24276a32012-01-08 02:44:38 +02001167 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 $this->_reset_select();
1169 return $result;
1170 }
1171
1172 // --------------------------------------------------------------------
1173
1174 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001175 * Insert_Batch
1176 *
1177 * Compiles batch insert strings and runs the queries
1178 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001179 * @param string the table to retrieve the results from
1180 * @param array an associative array of insert values
1181 * @return object
1182 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001183 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001184 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001185 if ( ! is_null($set))
1186 {
1187 $this->set_insert_batch($set);
1188 }
Barry Mienydd671972010-10-04 16:33:58 +02001189
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001190 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001191 {
1192 if ($this->db_debug)
1193 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001194 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001195 return $this->display_error('db_must_use_set');
1196 }
1197 return FALSE;
1198 }
1199
1200 if ($table == '')
1201 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001202 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001203 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001204 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001205 }
Barry Mienydd671972010-10-04 16:33:58 +02001206
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001207 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001208 }
1209
1210 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001211 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001212 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001213 $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 -06001214 }
Barry Mienydd671972010-10-04 16:33:58 +02001215
Derek Jonesd10e8962010-03-02 17:10:36 -06001216 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001217 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001218 }
1219
1220 // --------------------------------------------------------------------
1221
1222 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001223 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001224 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001225 * @param mixed
1226 * @param string
1227 * @param boolean
1228 * @return object
1229 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001230 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001231 {
1232 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001233
Derek Jonesd10e8962010-03-02 17:10:36 -06001234 if ( ! is_array($key))
1235 {
1236 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001237 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001238
1239 $keys = array_keys(current($key));
1240 sort($keys);
1241
1242 foreach ($key as $row)
1243 {
Barry Mienydd671972010-10-04 16:33:58 +02001244 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1245 {
1246 // batch function above returns an error on an empty array
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001247 $this->qb_set[] = array();
Barry Mienydd671972010-10-04 16:33:58 +02001248 return;
1249 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001250
Barry Mienydd671972010-10-04 16:33:58 +02001251 ksort($row); // puts $row in the same order as our keys
1252
Derek Jonesd10e8962010-03-02 17:10:36 -06001253 if ($escape === FALSE)
1254 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001255 $this->qb_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001256 }
1257 else
1258 {
1259 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001260 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001261 {
Barry Mienydd671972010-10-04 16:33:58 +02001262 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001263 }
1264
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001265 $this->qb_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001266 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001267 }
1268
1269 foreach ($keys as $k)
1270 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001271 $this->qb_keys[] = $this->_protect_identifiers($k);
Derek Jonesd10e8962010-03-02 17:10:36 -06001272 }
Barry Mienydd671972010-10-04 16:33:58 +02001273
Derek Jonesd10e8962010-03-02 17:10:36 -06001274 return $this;
1275 }
WanWizard7219c072011-12-28 14:09:05 +01001276
Kyle Farris0c147b32011-08-26 02:29:31 -04001277 // --------------------------------------------------------------------
1278
1279 /**
1280 * Get INSERT query string
1281 *
1282 * Compiles an insert query and returns the sql
1283 *
1284 * @access public
1285 * @param string the table to insert into
1286 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1287 * @return string
1288 */
1289 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001290 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001291 if ($this->_validate_insert($table) === FALSE)
1292 {
1293 return FALSE;
1294 }
WanWizard7219c072011-12-28 14:09:05 +01001295
Kyle Farris76116012011-08-31 11:17:48 -04001296 $sql = $this->_insert(
1297 $this->_protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001298 $this->qb_from[0], TRUE, NULL, FALSE
Kyle Farris76116012011-08-31 11:17:48 -04001299 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001300 array_keys($this->qb_set),
1301 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001302 );
WanWizard7219c072011-12-28 14:09:05 +01001303
Kyle Farris0c147b32011-08-26 02:29:31 -04001304 if ($reset === TRUE)
1305 {
1306 $this->_reset_write();
1307 }
WanWizard7219c072011-12-28 14:09:05 +01001308
Kyle Farris0c147b32011-08-26 02:29:31 -04001309 return $sql;
1310 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001311
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 // --------------------------------------------------------------------
1313
1314 /**
1315 * Insert
1316 *
1317 * Compiles an insert string and runs the query
1318 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001319 * @access public
Phil Sturgeon9789f322011-07-15 15:14:05 -06001320 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001321 * @param array an associative array of insert values
1322 * @return object
1323 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001324 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001325 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 if ( ! is_null($set))
1327 {
1328 $this->set($set);
1329 }
WanWizard7219c072011-12-28 14:09:05 +01001330
Kyle Farris0c147b32011-08-26 02:29:31 -04001331 if ($this->_validate_insert($table) === FALSE)
1332 {
1333 return FALSE;
1334 }
WanWizard7219c072011-12-28 14:09:05 +01001335
Kyle Farris76116012011-08-31 11:17:48 -04001336 $sql = $this->_insert(
1337 $this->_protect_identifiers(
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001338 $this->qb_from[0], TRUE, NULL, FALSE
WanWizard7219c072011-12-28 14:09:05 +01001339 ),
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001340 array_keys($this->qb_set),
1341 array_values($this->qb_set)
Kyle Farris76116012011-08-31 11:17:48 -04001342 );
Barry Mienydd671972010-10-04 16:33:58 +02001343
Kyle Farris0c147b32011-08-26 02:29:31 -04001344 $this->_reset_write();
1345 return $this->query($sql);
1346 }
WanWizard7219c072011-12-28 14:09:05 +01001347
Kyle Farris0c147b32011-08-26 02:29:31 -04001348 // --------------------------------------------------------------------
1349
1350 /**
1351 * Validate Insert
1352 *
1353 * This method is used by both insert() and get_compiled_insert() to
1354 * validate that the there data is actually being set and that table
1355 * has been chosen to be inserted into.
1356 *
1357 * @access public
1358 * @param string the table to insert data into
1359 * @return string
1360 */
WanWizard7219c072011-12-28 14:09:05 +01001361 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001362 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001363 if (count($this->qb_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001364 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001365 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001366 }
1367
1368 if ($table == '')
1369 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001370 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001372 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001374 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001375 else
1376 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001377 $this->qb_from[0] = $table;
Kyle Farris0c147b32011-08-26 02:29:31 -04001378 }
WanWizard7219c072011-12-28 14:09:05 +01001379
Kyle Farris0c147b32011-08-26 02:29:31 -04001380 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 }
Barry Mienydd671972010-10-04 16:33:58 +02001382
Phil Sturgeon9789f322011-07-15 15:14:05 -06001383 // --------------------------------------------------------------------
1384
1385 /**
1386 * Replace
1387 *
1388 * Compiles an replace into string and runs the query
1389 *
1390 * @param string the table to replace data into
1391 * @param array an associative array of insert values
1392 * @return object
1393 */
1394 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001395 {
1396 if ( ! is_null($set))
1397 {
1398 $this->set($set);
1399 }
Barry Mienydd671972010-10-04 16:33:58 +02001400
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001401 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001402 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001403 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001404 }
1405
1406 if ($table == '')
1407 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001408 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001409 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001410 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001411 }
Barry Mienydd671972010-10-04 16:33:58 +02001412
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001413 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001414 }
1415
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001416 $sql = $this->_replace($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->qb_set), array_values($this->qb_set));
Derek Jonesd10e8962010-03-02 17:10:36 -06001417 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001418 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001419 }
WanWizard7219c072011-12-28 14:09:05 +01001420
Kyle Farris0c147b32011-08-26 02:29:31 -04001421 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001422
Kyle Farris0c147b32011-08-26 02:29:31 -04001423 /**
1424 * Get UPDATE query string
1425 *
1426 * Compiles an update query and returns the sql
1427 *
1428 * @access public
1429 * @param string the table to update
1430 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1431 * @return string
1432 */
1433 public function get_compiled_update($table = '', $reset = TRUE)
1434 {
1435 // Combine any cached components with the current statements
1436 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001437
Kyle Farris0c147b32011-08-26 02:29:31 -04001438 if ($this->_validate_update($table) === FALSE)
1439 {
1440 return FALSE;
1441 }
WanWizard7219c072011-12-28 14:09:05 +01001442
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001443 $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 +01001444
Kyle Farris0c147b32011-08-26 02:29:31 -04001445 if ($reset === TRUE)
1446 {
1447 $this->_reset_write();
1448 }
WanWizard7219c072011-12-28 14:09:05 +01001449
Kyle Farris0c147b32011-08-26 02:29:31 -04001450 return $sql;
1451 }
WanWizard7219c072011-12-28 14:09:05 +01001452
Derek Allard2067d1a2008-11-13 22:59:24 +00001453 // --------------------------------------------------------------------
1454
1455 /**
1456 * Update
1457 *
1458 * Compiles an update string and runs the query
1459 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001460 * @param string the table to retrieve the results from
1461 * @param array an associative array of update values
1462 * @param mixed the where clause
1463 * @return object
1464 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001465 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001466 {
1467 // Combine any cached components with the current statements
1468 $this->_merge_cache();
1469
1470 if ( ! is_null($set))
1471 {
1472 $this->set($set);
1473 }
Barry Mienydd671972010-10-04 16:33:58 +02001474
Kyle Farris0c147b32011-08-26 02:29:31 -04001475 if ($this->_validate_update($table) === FALSE)
1476 {
1477 return FALSE;
1478 }
1479
1480 if ($where != NULL)
1481 {
1482 $this->where($where);
1483 }
1484
1485 if ($limit != NULL)
1486 {
1487 $this->limit($limit);
1488 }
1489
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001490 $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);
Kyle Farris0c147b32011-08-26 02:29:31 -04001491 $this->_reset_write();
1492 return $this->query($sql);
1493 }
WanWizard7219c072011-12-28 14:09:05 +01001494
Kyle Farris0c147b32011-08-26 02:29:31 -04001495 // --------------------------------------------------------------------
1496
1497 /**
1498 * Validate Update
1499 *
1500 * This method is used by both update() and get_compiled_update() to
1501 * validate that data is actually being set and that a table has been
1502 * chosen to be update.
1503 *
1504 * @access public
1505 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001506 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001507 */
1508 protected function _validate_update($table = '')
1509 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001510 if (count($this->qb_set) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001511 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001512 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 }
1514
1515 if ($table == '')
1516 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001517 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001518 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001519 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001520 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001521 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001522 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001523 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001524 $this->qb_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001526
1527 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001528 }
WanWizard7219c072011-12-28 14:09:05 +01001529
Derek Jonesd10e8962010-03-02 17:10:36 -06001530 // --------------------------------------------------------------------
1531
1532 /**
1533 * Update_Batch
1534 *
1535 * Compiles an update string and runs the query
1536 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001537 * @param string the table to retrieve the results from
1538 * @param array an associative array of update values
1539 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001540 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001541 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001542 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001543 {
1544 // Combine any cached components with the current statements
1545 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001546
Derek Jonesd10e8962010-03-02 17:10:36 -06001547 if (is_null($index))
1548 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001549 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001550 }
1551
1552 if ( ! is_null($set))
1553 {
1554 $this->set_update_batch($set, $index);
1555 }
1556
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001557 if (count($this->qb_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001558 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001559 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001560 }
1561
1562 if ($table == '')
1563 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001564 if ( ! isset($this->qb_from[0]))
Derek Jonesd10e8962010-03-02 17:10:36 -06001565 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001566 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001567 }
Barry Mienydd671972010-10-04 16:33:58 +02001568
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001569 $table = $this->qb_from[0];
Derek Jonesd10e8962010-03-02 17:10:36 -06001570 }
Barry Mienydd671972010-10-04 16:33:58 +02001571
Derek Jonesd10e8962010-03-02 17:10:36 -06001572 // Batch this baby
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001573 for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001574 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001575 $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 -06001576 }
Barry Mienydd671972010-10-04 16:33:58 +02001577
Derek Jonesd10e8962010-03-02 17:10:36 -06001578 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001579 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001580 }
1581
1582 // --------------------------------------------------------------------
1583
1584 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001585 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001586 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001587 * @param array
1588 * @param string
1589 * @param boolean
1590 * @return object
1591 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001592 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001593 {
1594 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001595
Derek Jonesd10e8962010-03-02 17:10:36 -06001596 if ( ! is_array($key))
1597 {
1598 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001599 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001600
1601 foreach ($key as $k => $v)
1602 {
1603 $index_set = FALSE;
1604 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001605 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001606 {
1607 if ($k2 == $index)
1608 {
1609 $index_set = TRUE;
1610 }
1611 else
1612 {
1613 $not[] = $k.'-'.$v;
1614 }
1615
Andrey Andreev24276a32012-01-08 02:44:38 +02001616 $clean[$this->_protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001617 }
1618
1619 if ($index_set == FALSE)
1620 {
1621 return $this->display_error('db_batch_missing_index');
1622 }
1623
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001624 $this->qb_set[] = $clean;
Derek Jonesd10e8962010-03-02 17:10:36 -06001625 }
Barry Mienydd671972010-10-04 16:33:58 +02001626
Derek Jonesd10e8962010-03-02 17:10:36 -06001627 return $this;
1628 }
1629
Derek Allard2067d1a2008-11-13 22:59:24 +00001630 // --------------------------------------------------------------------
1631
1632 /**
1633 * Empty Table
1634 *
1635 * Compiles a delete string and runs "DELETE FROM table"
1636 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001637 * @param string the table to empty
1638 * @return object
1639 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001640 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001641 {
1642 if ($table == '')
1643 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001644 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001645 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001646 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001647 }
1648
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001649 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001650 }
1651 else
1652 {
1653 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1654 }
1655
1656 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001657 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001658 return $this->query($sql);
1659 }
1660
1661 // --------------------------------------------------------------------
1662
1663 /**
1664 * Truncate
1665 *
1666 * Compiles a truncate string and runs the query
1667 * If the database does not support the truncate() command
1668 * This function maps to "DELETE FROM table"
1669 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001670 * @param string the table to truncate
1671 * @return object
1672 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001673 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001674 {
1675 if ($table == '')
1676 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001677 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001678 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001679 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001680 }
1681
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001682 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001683 }
1684 else
1685 {
1686 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1687 }
1688
1689 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001690 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 return $this->query($sql);
1692 }
WanWizard7219c072011-12-28 14:09:05 +01001693
Kyle Farris0c147b32011-08-26 02:29:31 -04001694 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001695
Kyle Farris0c147b32011-08-26 02:29:31 -04001696 /**
1697 * Get DELETE query string
1698 *
1699 * Compiles a delete query string and returns the sql
1700 *
1701 * @access public
1702 * @param string the table to delete from
1703 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1704 * @return string
1705 */
1706 public function get_compiled_delete($table = '', $reset = TRUE)
1707 {
1708 $this->return_delete_sql = TRUE;
1709 $sql = $this->delete($table, '', NULL, $reset);
1710 $this->return_delete_sql = FALSE;
1711 return $sql;
1712 }
WanWizard7219c072011-12-28 14:09:05 +01001713
Derek Allard2067d1a2008-11-13 22:59:24 +00001714 // --------------------------------------------------------------------
1715
1716 /**
1717 * Delete
1718 *
1719 * Compiles a delete string and runs the query
1720 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001721 * @param mixed the table(s) to delete from. String or array
1722 * @param mixed the where clause
1723 * @param mixed the limit clause
1724 * @param boolean
1725 * @return object
1726 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001727 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001728 {
1729 // Combine any cached components with the current statements
1730 $this->_merge_cache();
1731
1732 if ($table == '')
1733 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001734 if ( ! isset($this->qb_from[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001735 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001736 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001737 }
1738
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001739 $table = $this->qb_from[0];
Derek Allard2067d1a2008-11-13 22:59:24 +00001740 }
1741 elseif (is_array($table))
1742 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001743 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001744 {
1745 $this->delete($single_table, $where, $limit, FALSE);
1746 }
1747
1748 $this->_reset_write();
1749 return;
1750 }
1751 else
1752 {
1753 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1754 }
1755
1756 if ($where != '')
1757 {
1758 $this->where($where);
1759 }
1760
1761 if ($limit != NULL)
1762 {
1763 $this->limit($limit);
1764 }
1765
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001766 if (count($this->qb_where) === 0 && count($this->qb_wherein) === 0 && count($this->qb_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001767 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001768 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001769 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001770
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001771 $sql = $this->_delete($table, $this->qb_where, $this->qb_like, $this->qb_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 if ($reset_data)
1773 {
1774 $this->_reset_write();
1775 }
WanWizard7219c072011-12-28 14:09:05 +01001776
Andrey Andreev24276a32012-01-08 02:44:38 +02001777 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001778 }
WanWizard7219c072011-12-28 14:09:05 +01001779
Derek Allard2067d1a2008-11-13 22:59:24 +00001780 // --------------------------------------------------------------------
1781
1782 /**
1783 * DB Prefix
1784 *
1785 * Prepends a database prefix if one exists in configuration
1786 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001787 * @param string the table
1788 * @return string
1789 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001790 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 {
1792 if ($table == '')
1793 {
1794 $this->display_error('db_table_name_required');
1795 }
1796
1797 return $this->dbprefix.$table;
1798 }
1799
1800 // --------------------------------------------------------------------
1801
1802 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001803 * Set DB Prefix
1804 *
1805 * Set's the DB Prefix to something new without needing to reconnect
1806 *
1807 * @param string the prefix
1808 * @return string
1809 */
1810 public function set_dbprefix($prefix = '')
1811 {
1812 return $this->dbprefix = $prefix;
1813 }
1814
1815 // --------------------------------------------------------------------
1816
1817 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001818 * Track Aliases
1819 *
1820 * Used to track SQL statements written with aliased tables.
1821 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001822 * @param string The table to inspect
1823 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001824 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001825 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001826 {
1827 if (is_array($table))
1828 {
1829 foreach ($table as $t)
1830 {
1831 $this->_track_aliases($t);
1832 }
1833 return;
1834 }
Barry Mienydd671972010-10-04 16:33:58 +02001835
Derek Jones37f4b9c2011-07-01 17:56:50 -05001836 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001837 // the string into discreet statements
1838 if (strpos($table, ',') !== FALSE)
1839 {
1840 return $this->_track_aliases(explode(',', $table));
1841 }
Barry Mienydd671972010-10-04 16:33:58 +02001842
Derek Allard2067d1a2008-11-13 22:59:24 +00001843 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001844 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001845 {
1846 // if the alias is written with the AS keyword, remove it
1847 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001848
Derek Allard2067d1a2008-11-13 22:59:24 +00001849 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001850 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001851
Derek Allard2067d1a2008-11-13 22:59:24 +00001852 // Store the alias, if it doesn't already exist
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001853 if ( ! in_array($table, $this->qb_aliased_tables))
Derek Allard2067d1a2008-11-13 22:59:24 +00001854 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001855 $this->qb_aliased_tables[] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001856 }
1857 }
1858 }
WanWizard7219c072011-12-28 14:09:05 +01001859
Derek Allard2067d1a2008-11-13 22:59:24 +00001860 // --------------------------------------------------------------------
1861
1862 /**
1863 * Compile the SELECT statement
1864 *
1865 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001866 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001867 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001868 * @return string
1869 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001870 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001871 {
1872 // Combine any cached components with the current statements
1873 $this->_merge_cache();
1874
Derek Allard2067d1a2008-11-13 22:59:24 +00001875 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001876 if ($select_override !== FALSE)
1877 {
1878 $sql = $select_override;
1879 }
1880 else
1881 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001882 $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001883
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001884 if (count($this->qb_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001885 {
Barry Mienydd671972010-10-04 16:33:58 +02001886 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001887 }
1888 else
Barry Mienydd671972010-10-04 16:33:58 +02001889 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001890 // Cycle through the "select" portion of the query and prep each column name.
1891 // The reason we protect identifiers here rather then in the select() function
1892 // is because until the user calls the from() function we don't know if there are aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001893 foreach ($this->qb_select as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001894 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001895 $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL;
1896 $this->qb_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001897 }
Barry Mienydd671972010-10-04 16:33:58 +02001898
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001899 $sql .= implode(', ', $this->qb_select);
Derek Allard2067d1a2008-11-13 22:59:24 +00001900 }
1901 }
1902
Derek Allard2067d1a2008-11-13 22:59:24 +00001903 // Write the "FROM" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001904 if (count($this->qb_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001905 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001906 $sql .= "\nFROM ".$this->_from_tables($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00001907 }
1908
Derek Allard2067d1a2008-11-13 22:59:24 +00001909 // Write the "JOIN" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001910 if (count($this->qb_join) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001911 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001912 $sql .= "\n".implode("\n", $this->qb_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00001913 }
1914
Derek Allard2067d1a2008-11-13 22:59:24 +00001915 // Write the "WHERE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001916 if (count($this->qb_where) > 0 OR count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001917 {
Greg Akere156c6e2011-04-20 16:03:04 -05001918 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00001919 }
1920
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001921 $sql .= implode("\n", $this->qb_where);
Derek Allard2067d1a2008-11-13 22:59:24 +00001922
Derek Allard2067d1a2008-11-13 22:59:24 +00001923 // Write the "LIKE" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001924 if (count($this->qb_like) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001925 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001926 if (count($this->qb_where) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001927 {
1928 $sql .= "\nAND ";
1929 }
1930
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001931 $sql .= implode("\n", $this->qb_like);
Derek Allard2067d1a2008-11-13 22:59:24 +00001932 }
1933
Derek Allard2067d1a2008-11-13 22:59:24 +00001934 // Write the "GROUP BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001935 if (count($this->qb_groupby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001936 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001937 $sql .= "\nGROUP BY ".implode(', ', $this->qb_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00001938 }
1939
Derek Allard2067d1a2008-11-13 22:59:24 +00001940 // Write the "HAVING" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001941 if (count($this->qb_having) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001942 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001943 $sql .= "\nHAVING ".implode("\n", $this->qb_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00001944 }
1945
Derek Allard2067d1a2008-11-13 22:59:24 +00001946 // Write the "ORDER BY" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001947 if (count($this->qb_orderby) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001948 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001949 $sql .= "\nORDER BY ".implode(', ', $this->qb_orderby);
1950 if ($this->qb_order !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001951 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001952 $sql .= ($this->qb_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02001953 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001954 }
1955
Derek Allard2067d1a2008-11-13 22:59:24 +00001956 // Write the "LIMIT" portion of the query
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001957 if (is_numeric($this->qb_limit))
Derek Allard2067d1a2008-11-13 22:59:24 +00001958 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001959 return $this->_limit($sql."\n", $this->qb_limit, $this->qb_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00001960 }
1961
1962 return $sql;
1963 }
1964
1965 // --------------------------------------------------------------------
1966
1967 /**
1968 * Object to Array
1969 *
1970 * Takes an object as input and converts the class variables to array key/vals
1971 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001972 * @param object
1973 * @return array
1974 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001975 public function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001976 {
1977 if ( ! is_object($object))
1978 {
1979 return $object;
1980 }
Barry Mienydd671972010-10-04 16:33:58 +02001981
Derek Allard2067d1a2008-11-13 22:59:24 +00001982 $array = array();
1983 foreach (get_object_vars($object) as $key => $val)
1984 {
1985 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06001986 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00001987 {
1988 $array[$key] = $val;
1989 }
1990 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001991
1992 return $array;
1993 }
Barry Mienydd671972010-10-04 16:33:58 +02001994
Derek Jonesd10e8962010-03-02 17:10:36 -06001995 // --------------------------------------------------------------------
1996
1997 /**
1998 * Object to Array
1999 *
2000 * Takes an object as input and converts the class variables to array key/vals
2001 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002002 * @param object
2003 * @return array
2004 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002005 public function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002006 {
2007 if ( ! is_object($object))
2008 {
2009 return $object;
2010 }
Barry Mienydd671972010-10-04 16:33:58 +02002011
Derek Jonesd10e8962010-03-02 17:10:36 -06002012 $array = array();
2013 $out = get_object_vars($object);
2014 $fields = array_keys($out);
2015
2016 foreach ($fields as $val)
2017 {
2018 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002019 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002020 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002021 $i = 0;
2022 foreach ($out[$val] as $data)
2023 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002024 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002025 }
2026 }
2027 }
2028
Derek Allard2067d1a2008-11-13 22:59:24 +00002029 return $array;
2030 }
Barry Mienydd671972010-10-04 16:33:58 +02002031
Derek Allard2067d1a2008-11-13 22:59:24 +00002032 // --------------------------------------------------------------------
2033
2034 /**
2035 * Start Cache
2036 *
2037 * Starts AR caching
2038 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002039 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002040 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002041 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002042 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002043 $this->qb_caching = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002044 }
2045
2046 // --------------------------------------------------------------------
2047
2048 /**
2049 * Stop Cache
2050 *
2051 * Stops AR caching
2052 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002054 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002055 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002056 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002057 $this->qb_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00002058 }
2059
2060 // --------------------------------------------------------------------
2061
2062 /**
2063 * Flush Cache
2064 *
2065 * Empties the AR cache
2066 *
2067 * @access public
2068 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002069 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002070 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002071 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002072 $this->_reset_run(array(
2073 'ar_cache_select' => array(),
2074 'ar_cache_from' => array(),
2075 'ar_cache_join' => array(),
2076 'ar_cache_where' => array(),
2077 'ar_cache_like' => array(),
2078 'ar_cache_groupby' => array(),
2079 'ar_cache_having' => array(),
2080 'ar_cache_orderby' => array(),
2081 'ar_cache_set' => array(),
2082 'ar_cache_exists' => array(),
2083 'ar_cache_no_escape' => array()
2084 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002085 }
2086
2087 // --------------------------------------------------------------------
2088
2089 /**
2090 * Merge Cache
2091 *
Barry Mienydd671972010-10-04 16:33:58 +02002092 * When called, this function merges any cached AR arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002093 * locally called ones.
2094 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002095 * @return void
2096 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002097 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002098 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002099 if (count($this->qb_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002100 {
2101 return;
2102 }
2103
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002104 foreach ($this->qb_cache_exists as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00002105 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002106 $qb_variable = 'ar_'.$val;
2107 $qb_cache_var = 'ar_cache_'.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +00002108
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002109 if (count($this->$qb_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002110 {
2111 continue;
2112 }
2113
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002114 $this->$qb_variable = array_unique(array_merge($this->$qb_cache_var, $this->$qb_variable));
Derek Allard2067d1a2008-11-13 22:59:24 +00002115 }
2116
2117 // If we are "protecting identifiers" we need to examine the "from"
2118 // portion of the query to determine if there are any aliases
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002119 if ($this->_protect_identifiers === TRUE AND count($this->qb_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002120 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002121 $this->_track_aliases($this->qb_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002122 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002123
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002124 $this->qb_no_escape = $this->qb_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002125 }
WanWizard7219c072011-12-28 14:09:05 +01002126
Kyle Farris0c147b32011-08-26 02:29:31 -04002127 // --------------------------------------------------------------------
2128
2129 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002130 * Reset Query Builder values.
WanWizard7219c072011-12-28 14:09:05 +01002131 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002132 * Publicly-visible method to reset the AR values.
2133 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002134 * @return void
2135 */
2136 public function reset_query()
2137 {
2138 $this->_reset_select();
2139 $this->_reset_write();
2140 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002141
2142 // --------------------------------------------------------------------
2143
2144 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002145 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002146 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002147 * @param array An array of fields to reset
2148 * @return void
2149 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002150 protected function _reset_run($qb_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002151 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002152 foreach ($qb_reset_items as $item => $default_value)
Derek Allard2067d1a2008-11-13 22:59:24 +00002153 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002154 if ( ! in_array($item, $this->qb_store_array))
Derek Allard2067d1a2008-11-13 22:59:24 +00002155 {
2156 $this->$item = $default_value;
2157 }
2158 }
2159 }
2160
2161 // --------------------------------------------------------------------
2162
2163 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002164 * Resets the query builder values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002165 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002166 * @return void
2167 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002168 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002169 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002170 $this->_reset_run(array(
2171 'ar_select' => array(),
2172 'ar_from' => array(),
2173 'ar_join' => array(),
2174 'ar_where' => array(),
2175 'ar_like' => array(),
2176 'ar_groupby' => array(),
2177 'ar_having' => array(),
2178 'ar_orderby' => array(),
2179 'ar_wherein' => array(),
2180 'ar_aliased_tables' => array(),
2181 'ar_no_escape' => array(),
2182 'ar_distinct' => FALSE,
2183 'ar_limit' => FALSE,
2184 'ar_offset' => FALSE,
2185 'ar_order' => FALSE
2186 )
2187 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002188 }
Barry Mienydd671972010-10-04 16:33:58 +02002189
Derek Allard2067d1a2008-11-13 22:59:24 +00002190 // --------------------------------------------------------------------
2191
2192 /**
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002193 * Resets the query builder "write" values.
Derek Allard2067d1a2008-11-13 22:59:24 +00002194 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002195 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002196 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002197 * @return void
2198 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002199 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002200 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002201 $this->_reset_run(array(
2202 'ar_set' => array(),
2203 'ar_from' => array(),
2204 'ar_where' => array(),
2205 'ar_like' => array(),
2206 'ar_orderby' => array(),
2207 'ar_keys' => array(),
2208 'ar_limit' => FALSE,
2209 'ar_order' => FALSE
2210 )
2211 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002212 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002213
Derek Allard2067d1a2008-11-13 22:59:24 +00002214}
2215
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002216/* End of file DB_query_builder.php */
2217/* Location: ./system/database/DB_query_builder.php */