blob: e78b2a8934c50f33cabe894fb39c1bd3ca480823 [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
WanWizard7219c072011-12-28 14:09:05 +01008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
WanWizard7219c072011-12-28 14:09:05 +010010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Active Record Class
30 *
31 * This is the platform-independent base Active Record implementation class.
32 *
33 * @package CodeIgniter
34 * @subpackage Drivers
35 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/database/
38 */
Timothy Warren7eeda532012-03-19 16:01:55 -040039abstract class CI_DB_active_record extends CI_DB_driver {
Derek Allard2067d1a2008-11-13 22:59:24 +000040
WanWizard7219c072011-12-28 14:09:05 +010041 protected $return_delete_sql = FALSE;
42 protected $reset_delete_data = FALSE;
43
Andrey Andreev24276a32012-01-08 02:44:38 +020044 protected $ar_select = array();
45 protected $ar_distinct = FALSE;
46 protected $ar_from = array();
47 protected $ar_join = array();
48 protected $ar_where = array();
49 protected $ar_like = array();
50 protected $ar_groupby = array();
51 protected $ar_having = array();
52 protected $ar_keys = array();
53 protected $ar_limit = FALSE;
54 protected $ar_offset = FALSE;
55 protected $ar_order = FALSE;
56 protected $ar_orderby = array();
57 protected $ar_set = array();
58 protected $ar_wherein = array();
WanWizard7219c072011-12-28 14:09:05 +010059 protected $ar_aliased_tables = array();
Andrey Andreev24276a32012-01-08 02:44:38 +020060 protected $ar_store_array = array();
WanWizard7219c072011-12-28 14:09:05 +010061 protected $ar_where_group_started = FALSE;
62 protected $ar_where_group_count = 0;
Barry Mienydd671972010-10-04 16:33:58 +020063
Derek Allard2067d1a2008-11-13 22:59:24 +000064 // Active Record Caching variables
WanWizard7219c072011-12-28 14:09:05 +010065 protected $ar_caching = FALSE;
66 protected $ar_cache_exists = array();
67 protected $ar_cache_select = array();
68 protected $ar_cache_from = array();
69 protected $ar_cache_join = array();
70 protected $ar_cache_where = array();
71 protected $ar_cache_like = array();
72 protected $ar_cache_groupby = array();
73 protected $ar_cache_having = array();
74 protected $ar_cache_orderby = array();
75 protected $ar_cache_set = array();
76
77 protected $ar_no_escape = array();
Andrey Andreev24276a32012-01-08 02:44:38 +020078 protected $ar_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000079
80 /**
81 * Select
82 *
83 * Generates the SELECT portion of the query
84 *
Derek Allard2067d1a2008-11-13 22:59:24 +000085 * @param string
86 * @return object
87 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060088 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
Derek Allard2067d1a2008-11-13 22:59:24 +000090 if (is_string($select))
91 {
92 $select = explode(',', $select);
93 }
94
95 foreach ($select as $val)
96 {
97 $val = trim($val);
98
99 if ($val != '')
100 {
101 $this->ar_select[] = $val;
Greg Akere156c6e2011-04-20 16:03:04 -0500102 $this->ar_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000103
104 if ($this->ar_caching === TRUE)
105 {
106 $this->ar_cache_select[] = $val;
107 $this->ar_cache_exists[] = 'select';
Greg Aker2e1837a2011-05-06 12:17:04 -0500108 $this->ar_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 }
110 }
111 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 return $this;
114 }
115
116 // --------------------------------------------------------------------
117
118 /**
119 * Select Max
120 *
121 * Generates a SELECT MAX(field) portion of a query
122 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * @param string the field
124 * @param string an alias
125 * @return object
126 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600127 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
129 return $this->_max_min_avg_sum($select, $alias, 'MAX');
130 }
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 // --------------------------------------------------------------------
133
134 /**
135 * Select Min
136 *
137 * Generates a SELECT MIN(field) portion of a query
138 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 * @param string the field
140 * @param string an alias
141 * @return object
142 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600143 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
145 return $this->_max_min_avg_sum($select, $alias, 'MIN');
146 }
147
148 // --------------------------------------------------------------------
149
150 /**
151 * Select Average
152 *
153 * Generates a SELECT AVG(field) portion of a query
154 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 * @param string the field
156 * @param string an alias
157 * @return object
158 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600159 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
161 return $this->_max_min_avg_sum($select, $alias, 'AVG');
162 }
163
164 // --------------------------------------------------------------------
165
166 /**
167 * Select Sum
168 *
169 * Generates a SELECT SUM(field) portion of a query
170 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 * @param string the field
172 * @param string an alias
173 * @return object
174 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600175 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 {
177 return $this->_max_min_avg_sum($select, $alias, 'SUM');
178 }
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Processing Function for the four functions above:
184 *
185 * select_max()
186 * select_min()
187 * select_avg()
Andrey Andreev24276a32012-01-08 02:44:38 +0200188 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200189 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 * @param string the field
191 * @param string an alias
192 * @return object
193 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600194 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
196 if ( ! is_string($select) OR $select == '')
197 {
198 $this->display_error('db_invalid_query');
199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
204 {
205 show_error('Invalid function type: '.$type);
206 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 if ($alias == '')
209 {
210 $alias = $this->_create_alias_from_table(trim($select));
211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200213 $sql = $this->protect_identifiers($type.'('.trim($select).')').' AS '.$this->protect_identifiers(trim($alias));
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 $this->ar_select[] = $sql;
Greg Aker827f3de2011-05-06 11:29:57 -0500215 $this->ar_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 if ($this->ar_caching === TRUE)
218 {
219 $this->ar_cache_select[] = $sql;
220 $this->ar_cache_exists[] = 'select';
221 }
Barry Mienydd671972010-10-04 16:33:58 +0200222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 return $this;
224 }
225
226 // --------------------------------------------------------------------
227
228 /**
229 * Determines the alias name based on the table
230 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 * @param string
232 * @return string
233 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600234 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
236 if (strpos($item, '.') !== FALSE)
237 {
Andrey Andreevdb0c0622012-02-29 19:02:46 +0200238 $item = explode('.', $item);
239 return end($item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
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 {
257 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
258 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);
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200281 $v = $this->ar_from[] = $this->protect_identifiers($v, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 if ($this->ar_caching === TRUE)
284 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200285 $this->ar_cache_from[] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 $this->ar_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);
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200297 $this->ar_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 if ($this->ar_caching === TRUE)
300 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200301 $this->ar_cache_from[] = $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 $this->ar_cache_exists[] = 'from';
303 }
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
Hamza Bhattid3c1ccf2012-03-05 22:58:56 +0400343 if (preg_match('/([\[\w\.]+)([\W\s]+)(.+)/', $cond, $match))
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +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
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200349 $this->ar_join[] = $join = $type.'JOIN '.$this->protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
Derek Allard2067d1a2008-11-13 22:59:24 +0000350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 if ($this->ar_caching === TRUE)
352 {
353 $this->ar_cache_join[] = $join;
354 $this->ar_cache_exists[] = 'join';
355 }
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 {
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300423 $prefix = (count($this->ar_where) === 0 && count($this->ar_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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200435 $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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200446 $k = $this->protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 }
448
449 $this->ar_where[] = $prefix.$k.$v;
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 if ($this->ar_caching === TRUE)
451 {
452 $this->ar_cache_where[] = $prefix.$k.$v;
453 $this->ar_cache_exists[] = 'where';
454 }
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
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300538 * @param bool 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 {
560 $this->ar_wherein[] = $this->escape($value);
561 }
562
Andrey Andreev24276a32012-01-08 02:44:38 +0200563 $prefix = (count($this->ar_where) === 0) ? '' : $type;
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200564 $this->ar_where[] = $where_in = $prefix.$this->protect_identifiers($key).$not.' IN ('.implode(', ', $this->ar_wherein).') ';
Barry Mienydd671972010-10-04 16:33:58 +0200565
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 if ($this->ar_caching === TRUE)
567 {
568 $this->ar_cache_where[] = $where_in;
569 $this->ar_cache_exists[] = 'where';
570 }
571
572 // reset the array for multiple calls
573 $this->ar_wherein = array();
574 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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200668 $k = $this->protect_identifiers($k);
Andrey Andreev24276a32012-01-08 02:44:38 +0200669 $prefix = (count($this->ar_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
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 $this->ar_like[] = $like_statement;
696 if ($this->ar_caching === TRUE)
697 {
698 $this->ar_cache_like[] = $like_statement;
699 $this->ar_cache_exists[] = 'like';
700 }
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);
WanWizard7219c072011-12-28 14:09:05 +0100719 $this->ar_where_group_started = TRUE;
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300720 $prefix = (count($this->ar_where) === 0 && count($this->ar_cache_where) === 0) ? '' : $type;
Andrey Andreev24276a32012-01-08 02:44:38 +0200721 $this->ar_where[] = $value = $prefix.$not.str_repeat(' ', ++$this->ar_where_group_count).' (';
WanWizard7219c072011-12-28 14:09:05 +0100722
WanWizard7219c072011-12-28 14:09:05 +0100723 if ($this->ar_caching)
724 {
725 $this->ar_cache_where[] = $value;
726 }
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 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200776 $this->ar_where_group_started = FALSE;
777 $this->ar_where[] = $value = str_repeat(' ', $this->ar_where_group_count--) . ')';
WanWizard7219c072011-12-28 14:09:05 +0100778
WanWizard7219c072011-12-28 14:09:05 +0100779 if ($this->ar_caching)
780 {
781 $this->ar_cache_where[] = $value;
782 }
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 {
799 if ($this->ar_where_group_started)
800 {
801 $type = '';
802 $this->ar_where_group_started = FALSE;
803 }
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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200829 $this->ar_groupby[] = $val = $this->protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200830
Derek Allard2067d1a2008-11-13 22:59:24 +0000831 if ($this->ar_caching === TRUE)
832 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200833 $this->ar_cache_groupby[] = $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 $this->ar_cache_exists[] = 'groupby';
835 }
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 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200894 $prefix = (count($this->ar_having) === 0) ? '' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000895
896 if ($escape === TRUE)
897 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200898 $k = $this->protect_identifiers($k);
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 }
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 $this->ar_having[] = $prefix.$k.$v;
912 if ($this->ar_caching === TRUE)
913 {
914 $this->ar_cache_having[] = $prefix.$k.$v;
915 $this->ar_cache_exists[] = 'having';
916 }
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);
951 if ( ! in_array($part, $this->ar_aliased_tables))
952 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200953 $part = $this->protect_identifiers(trim($part));
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 }
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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200965 $orderby = $this->protect_identifiers($orderby);
pporlan2c685fb2011-12-22 12:15:25 +0100966 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 }
Barry Mienydd671972010-10-04 16:33:58 +0200968
Andrey Andreev24276a32012-01-08 02:44:38 +0200969 $this->ar_orderby[] = $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200970
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 if ($this->ar_caching === TRUE)
972 {
973 $this->ar_cache_orderby[] = $orderby_statement;
974 $this->ar_cache_exists[] = 'orderby';
975 }
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 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +0300985 * @param int the limit value
986 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 * @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 {
Kyle Farris76116012011-08-31 11:17:48 -0400991 $this->ar_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 {
Kyle Farris76116012011-08-31 11:17:48 -0400995 $this->ar_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 *
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001006 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 * @return object
1008 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001009 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 {
kenjis0e857632011-09-02 08:41:17 +09001011 $this->ar_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
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001022 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001023 * @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 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001038 $this->ar_set[$this->protect_identifiers($k)] = $v;
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 }
1040 else
1041 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001042 $this->ar_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 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001056 * @param string the table name to select from (optional)
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001057 * @param bool TRUE: resets AR values; FALSE: leave AR vaules alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001058 * @return string
1059 */
WanWizard7219c072011-12-28 14:09:05 +01001060 public function get_compiled_select($table = '', $reset = TRUE)
Kyle Farris0c147b32011-08-26 02:29:31 -04001061 {
1062 if ($table != '')
kylefarris0a3176b2011-08-26 02:37:52 -04001063 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001064 $this->_track_aliases($table);
1065 $this->from($table);
1066 }
WanWizard7219c072011-12-28 14:09:05 +01001067
Kyle Farris0c147b32011-08-26 02:29:31 -04001068 $select = $this->_compile_select();
WanWizard7219c072011-12-28 14:09:05 +01001069
Kyle Farris0c147b32011-08-26 02:29:31 -04001070 if ($reset === TRUE)
1071 {
1072 $this->_reset_select();
1073 }
WanWizard7219c072011-12-28 14:09:05 +01001074
Kyle Farris0c147b32011-08-26 02:29:31 -04001075 return $select;
1076 }
WanWizard7219c072011-12-28 14:09:05 +01001077
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 // --------------------------------------------------------------------
1079
1080 /**
1081 * Get
1082 *
1083 * Compiles the select statement based on the other functions called
1084 * and runs the query
1085 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001086 * @param string the table
1087 * @param string the limit clause
1088 * @param string the offset clause
1089 * @return object
1090 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001091 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001092 {
1093 if ($table != '')
1094 {
1095 $this->_track_aliases($table);
1096 $this->from($table);
1097 }
Barry Mienydd671972010-10-04 16:33:58 +02001098
Derek Allard2067d1a2008-11-13 22:59:24 +00001099 if ( ! is_null($limit))
1100 {
1101 $this->limit($limit, $offset);
1102 }
Barry Mienydd671972010-10-04 16:33:58 +02001103
Andrey Andreev24276a32012-01-08 02:44:38 +02001104 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 $this->_reset_select();
1106 return $result;
1107 }
1108
1109 /**
1110 * "Count All Results" query
1111 *
Barry Mienydd671972010-10-04 16:33:58 +02001112 * Generates a platform-specific query string that counts all records
Derek Allard2067d1a2008-11-13 22:59:24 +00001113 * returned by an Active Record query.
1114 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 * @param string
1116 * @return string
1117 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001118 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001119 {
1120 if ($table != '')
1121 {
1122 $this->_track_aliases($table);
1123 $this->from($table);
1124 }
Barry Mienydd671972010-10-04 16:33:58 +02001125
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001126 $result = $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows')));
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001128
Purwandi1d160e72012-01-09 16:33:28 +07001129 if ($result->num_rows() === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001131 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 }
1133
Purwandi1d160e72012-01-09 16:33:28 +07001134 $row = $result->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001135 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 // --------------------------------------------------------------------
1138
1139 /**
1140 * Get_Where
1141 *
1142 * Allows the where clause, limit and offset to be added directly
1143 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001144 * @param string the where clause
1145 * @param string the limit clause
1146 * @param string the offset clause
1147 * @return object
1148 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001149 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 {
1151 if ($table != '')
1152 {
1153 $this->from($table);
1154 }
1155
1156 if ( ! is_null($where))
1157 {
1158 $this->where($where);
1159 }
Barry Mienydd671972010-10-04 16:33:58 +02001160
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 if ( ! is_null($limit))
1162 {
1163 $this->limit($limit, $offset);
1164 }
Barry Mienydd671972010-10-04 16:33:58 +02001165
Andrey Andreev24276a32012-01-08 02:44:38 +02001166 $result = $this->query($this->_compile_select());
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 $this->_reset_select();
1168 return $result;
1169 }
1170
1171 // --------------------------------------------------------------------
1172
1173 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001174 * Insert_Batch
1175 *
1176 * Compiles batch insert strings and runs the queries
1177 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001178 * @param string the table to retrieve the results from
1179 * @param array an associative array of insert values
1180 * @return object
1181 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001182 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001183 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001184 if ( ! is_null($set))
1185 {
1186 $this->set_insert_batch($set);
1187 }
Barry Mienydd671972010-10-04 16:33:58 +02001188
Andrey Andreev24276a32012-01-08 02:44:38 +02001189 if (count($this->ar_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001190 {
1191 if ($this->db_debug)
1192 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001193 // No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001194 return $this->display_error('db_must_use_set');
1195 }
1196 return FALSE;
1197 }
1198
1199 if ($table == '')
1200 {
1201 if ( ! isset($this->ar_from[0]))
1202 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001203 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001204 }
Barry Mienydd671972010-10-04 16:33:58 +02001205
Derek Jonesd10e8962010-03-02 17:10:36 -06001206 $table = $this->ar_from[0];
1207 }
1208
1209 // Batch this baby
Andrey Andreev24276a32012-01-08 02:44:38 +02001210 for ($i = 0, $total = count($this->ar_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001211 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001212 $this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001213 }
Barry Mienydd671972010-10-04 16:33:58 +02001214
Derek Jonesd10e8962010-03-02 17:10:36 -06001215 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001216 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001217 }
1218
1219 // --------------------------------------------------------------------
1220
1221 /**
Andrey Andreev97f36972012-04-05 12:44:36 +03001222 * Insert_batch statement
1223 *
1224 * Generates a platform-specific insert string from the supplied data.
1225 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001226 * @param string the table name
1227 * @param array the insert keys
1228 * @param array the insert values
1229 * @return string
Andrey Andreev97f36972012-04-05 12:44:36 +03001230 */
1231 protected function _insert_batch($table, $keys, $values)
1232 {
Andrey Andreev65d537c2012-04-05 14:11:41 +03001233 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Andrey Andreev97f36972012-04-05 12:44:36 +03001234 }
1235
1236 // --------------------------------------------------------------------
1237
1238 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001239 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001240 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001241 * @param mixed
1242 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001243 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001244 * @return object
1245 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001246 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001247 {
1248 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001249
Derek Jonesd10e8962010-03-02 17:10:36 -06001250 if ( ! is_array($key))
1251 {
1252 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001253 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001254
Iban Eguia3c0a4522012-04-15 13:30:44 +02001255 $keys = array_keys($this->_object_to_array(current($key)));
Derek Jonesd10e8962010-03-02 17:10:36 -06001256 sort($keys);
1257
1258 foreach ($key as $row)
1259 {
Iban Eguia3c0a4522012-04-15 13:30:44 +02001260 $row = $this->_object_to_array($row);
Barry Mienydd671972010-10-04 16:33:58 +02001261 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1262 {
1263 // batch function above returns an error on an empty array
1264 $this->ar_set[] = array();
1265 return;
1266 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001267
Barry Mienydd671972010-10-04 16:33:58 +02001268 ksort($row); // puts $row in the same order as our keys
1269
Derek Jonesd10e8962010-03-02 17:10:36 -06001270 if ($escape === FALSE)
1271 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001272 $this->ar_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001273 }
1274 else
1275 {
1276 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001277 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001278 {
Barry Mienydd671972010-10-04 16:33:58 +02001279 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001280 }
1281
Derek Jones37f4b9c2011-07-01 17:56:50 -05001282 $this->ar_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001283 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001284 }
1285
1286 foreach ($keys as $k)
1287 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001288 $this->ar_keys[] = $this->protect_identifiers($k);
Derek Jonesd10e8962010-03-02 17:10:36 -06001289 }
Barry Mienydd671972010-10-04 16:33:58 +02001290
Derek Jonesd10e8962010-03-02 17:10:36 -06001291 return $this;
1292 }
WanWizard7219c072011-12-28 14:09:05 +01001293
Kyle Farris0c147b32011-08-26 02:29:31 -04001294 // --------------------------------------------------------------------
1295
1296 /**
1297 * Get INSERT query string
1298 *
1299 * Compiles an insert query and returns the sql
1300 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001301 * @param string the table to insert into
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001302 * @param bool TRUE: reset AR values; FALSE: leave AR values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001303 * @return string
1304 */
1305 public function get_compiled_insert($table = '', $reset = TRUE)
WanWizard7219c072011-12-28 14:09:05 +01001306 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001307 if ($this->_validate_insert($table) === FALSE)
1308 {
1309 return FALSE;
1310 }
WanWizard7219c072011-12-28 14:09:05 +01001311
Kyle Farris76116012011-08-31 11:17:48 -04001312 $sql = $this->_insert(
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001313 $this->protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE),
WanWizard7219c072011-12-28 14:09:05 +01001314 array_keys($this->ar_set),
Kyle Farris76116012011-08-31 11:17:48 -04001315 array_values($this->ar_set)
1316 );
WanWizard7219c072011-12-28 14:09:05 +01001317
Kyle Farris0c147b32011-08-26 02:29:31 -04001318 if ($reset === TRUE)
1319 {
1320 $this->_reset_write();
1321 }
WanWizard7219c072011-12-28 14:09:05 +01001322
Kyle Farris0c147b32011-08-26 02:29:31 -04001323 return $sql;
1324 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001325
Derek Allard2067d1a2008-11-13 22:59:24 +00001326 // --------------------------------------------------------------------
1327
1328 /**
1329 * Insert
1330 *
1331 * Compiles an insert string and runs the query
1332 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001333 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001334 * @param array an associative array of insert values
1335 * @return object
1336 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001337 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001338 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001339 if ( ! is_null($set))
1340 {
1341 $this->set($set);
1342 }
WanWizard7219c072011-12-28 14:09:05 +01001343
Kyle Farris0c147b32011-08-26 02:29:31 -04001344 if ($this->_validate_insert($table) === FALSE)
1345 {
1346 return FALSE;
1347 }
WanWizard7219c072011-12-28 14:09:05 +01001348
Kyle Farris76116012011-08-31 11:17:48 -04001349 $sql = $this->_insert(
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001350 $this->protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE),
WanWizard7219c072011-12-28 14:09:05 +01001351 array_keys($this->ar_set),
Kyle Farris76116012011-08-31 11:17:48 -04001352 array_values($this->ar_set)
1353 );
Barry Mienydd671972010-10-04 16:33:58 +02001354
Kyle Farris0c147b32011-08-26 02:29:31 -04001355 $this->_reset_write();
1356 return $this->query($sql);
1357 }
WanWizard7219c072011-12-28 14:09:05 +01001358
Kyle Farris0c147b32011-08-26 02:29:31 -04001359 // --------------------------------------------------------------------
1360
1361 /**
Andrey Andreev65d537c2012-04-05 14:11:41 +03001362 * Insert statement
1363 *
1364 * Generates a platform-specific insert string from the supplied data
1365 *
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001366 * @param string the table name
1367 * @param array the insert keys
1368 * @param array the insert values
1369 * @return string
Andrey Andreev65d537c2012-04-05 14:11:41 +03001370 */
1371 protected function _insert($table, $keys, $values)
1372 {
1373 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1374 }
1375
1376 // --------------------------------------------------------------------
1377
1378 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001379 * Validate Insert
1380 *
1381 * This method is used by both insert() and get_compiled_insert() to
1382 * validate that the there data is actually being set and that table
1383 * has been chosen to be inserted into.
1384 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001385 * @param string the table to insert data into
1386 * @return string
1387 */
WanWizard7219c072011-12-28 14:09:05 +01001388 protected function _validate_insert($table = '')
Kyle Farris0c147b32011-08-26 02:29:31 -04001389 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001390 if (count($this->ar_set) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001391 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001392 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001393 }
1394
1395 if ($table == '')
1396 {
1397 if ( ! isset($this->ar_from[0]))
1398 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001399 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001400 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001402 else
1403 {
1404 $this->ar_from[0] = $table;
1405 }
WanWizard7219c072011-12-28 14:09:05 +01001406
Kyle Farris0c147b32011-08-26 02:29:31 -04001407 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001408 }
Barry Mienydd671972010-10-04 16:33:58 +02001409
Phil Sturgeon9789f322011-07-15 15:14:05 -06001410 // --------------------------------------------------------------------
1411
1412 /**
1413 * Replace
1414 *
1415 * Compiles an replace into string and runs the query
1416 *
1417 * @param string the table to replace data into
1418 * @param array an associative array of insert values
1419 * @return object
1420 */
1421 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001422 {
1423 if ( ! is_null($set))
1424 {
1425 $this->set($set);
1426 }
Barry Mienydd671972010-10-04 16:33:58 +02001427
Andrey Andreev24276a32012-01-08 02:44:38 +02001428 if (count($this->ar_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001429 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001430 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001431 }
1432
1433 if ($table == '')
1434 {
1435 if ( ! isset($this->ar_from[0]))
1436 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001437 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001438 }
Barry Mienydd671972010-10-04 16:33:58 +02001439
Derek Jonesd10e8962010-03-02 17:10:36 -06001440 $table = $this->ar_from[0];
1441 }
1442
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001443 $sql = $this->_replace($this->protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
Derek Jonesd10e8962010-03-02 17:10:36 -06001444 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001445 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001446 }
WanWizard7219c072011-12-28 14:09:05 +01001447
Kyle Farris0c147b32011-08-26 02:29:31 -04001448 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001449
Kyle Farris0c147b32011-08-26 02:29:31 -04001450 /**
Andrey Andreeva3bca8f2012-04-05 15:17:25 +03001451 * Replace statement
1452 *
1453 * Generates a platform-specific replace string from the supplied data
1454 *
1455 * @param string the table name
1456 * @param array the insert keys
1457 * @param array the insert values
1458 * @return string
1459 */
1460 protected function _replace($table, $keys, $values)
1461 {
1462 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
1463 }
1464
1465 // --------------------------------------------------------------------
1466
1467 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001468 * Get UPDATE query string
1469 *
1470 * Compiles an update query and returns the sql
1471 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001472 * @param string the table to update
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001473 * @param bool TRUE: reset AR values; FALSE: leave AR values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001474 * @return string
1475 */
1476 public function get_compiled_update($table = '', $reset = TRUE)
1477 {
1478 // Combine any cached components with the current statements
1479 $this->_merge_cache();
WanWizard7219c072011-12-28 14:09:05 +01001480
Kyle Farris0c147b32011-08-26 02:29:31 -04001481 if ($this->_validate_update($table) === FALSE)
1482 {
1483 return FALSE;
1484 }
WanWizard7219c072011-12-28 14:09:05 +01001485
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001486 $sql = $this->_update($this->protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
WanWizard7219c072011-12-28 14:09:05 +01001487
Kyle Farris0c147b32011-08-26 02:29:31 -04001488 if ($reset === TRUE)
1489 {
1490 $this->_reset_write();
1491 }
WanWizard7219c072011-12-28 14:09:05 +01001492
Kyle Farris0c147b32011-08-26 02:29:31 -04001493 return $sql;
1494 }
WanWizard7219c072011-12-28 14:09:05 +01001495
Derek Allard2067d1a2008-11-13 22:59:24 +00001496 // --------------------------------------------------------------------
1497
1498 /**
1499 * Update
1500 *
1501 * Compiles an update string and runs the query
1502 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001503 * @param string the table to retrieve the results from
1504 * @param array an associative array of update values
1505 * @param mixed the where clause
1506 * @return object
1507 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001508 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001509 {
1510 // Combine any cached components with the current statements
1511 $this->_merge_cache();
1512
1513 if ( ! is_null($set))
1514 {
1515 $this->set($set);
1516 }
Barry Mienydd671972010-10-04 16:33:58 +02001517
Kyle Farris0c147b32011-08-26 02:29:31 -04001518 if ($this->_validate_update($table) === FALSE)
1519 {
1520 return FALSE;
1521 }
1522
1523 if ($where != NULL)
1524 {
1525 $this->where($where);
1526 }
1527
1528 if ($limit != NULL)
1529 {
1530 $this->limit($limit);
1531 }
1532
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001533 $sql = $this->_update($this->protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit, $this->ar_like);
Kyle Farris0c147b32011-08-26 02:29:31 -04001534 $this->_reset_write();
1535 return $this->query($sql);
1536 }
WanWizard7219c072011-12-28 14:09:05 +01001537
Kyle Farris0c147b32011-08-26 02:29:31 -04001538 // --------------------------------------------------------------------
1539
1540 /**
Andrey Andreev975034d2012-04-05 15:09:55 +03001541 * Update statement
1542 *
1543 * Generates a platform-specific update string from the supplied data
1544 *
1545 * @param string the table name
1546 * @param array the update data
1547 * @param array the where clause
1548 * @param array the orderby clause
1549 * @param array the limit clause
Andrey Andreev00541ae2012-04-09 11:43:10 +03001550 * @param array the like clause
Andrey Andreev975034d2012-04-05 15:09:55 +03001551 * @return string
1552 */
Andrey Andreev00541ae2012-04-09 11:43:10 +03001553 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Andrey Andreev975034d2012-04-05 15:09:55 +03001554 {
1555 foreach ($values as $key => $val)
1556 {
1557 $valstr[] = $key.' = '.$val;
1558 }
1559
Andrey Andreev00541ae2012-04-09 11:43:10 +03001560 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
1561
1562 if ( ! empty($like))
1563 {
1564 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
1565 }
1566
Andrey Andreev975034d2012-04-05 15:09:55 +03001567 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
Andrey Andreev00541ae2012-04-09 11:43:10 +03001568 .$where
Andrey Andreev975034d2012-04-05 15:09:55 +03001569 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
1570 .($limit ? ' LIMIT '.$limit : '');
1571 }
1572
1573 // --------------------------------------------------------------------
1574
1575 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001576 * Validate Update
1577 *
1578 * This method is used by both update() and get_compiled_update() to
1579 * validate that data is actually being set and that a table has been
1580 * chosen to be update.
1581 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001582 * @param string the table to update data on
Andrey Andreev24276a32012-01-08 02:44:38 +02001583 * @return bool
Kyle Farris0c147b32011-08-26 02:29:31 -04001584 */
1585 protected function _validate_update($table = '')
1586 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001587 if (count($this->ar_set) == 0)
1588 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001589 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001590 }
1591
1592 if ($table == '')
1593 {
1594 if ( ! isset($this->ar_from[0]))
1595 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001596 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001597 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001598 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001599 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001600 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001601 $this->ar_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001602 }
Andrey Andreev24276a32012-01-08 02:44:38 +02001603
1604 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001605 }
WanWizard7219c072011-12-28 14:09:05 +01001606
Derek Jonesd10e8962010-03-02 17:10:36 -06001607 // --------------------------------------------------------------------
1608
1609 /**
1610 * Update_Batch
1611 *
1612 * Compiles an update string and runs the query
1613 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001614 * @param string the table to retrieve the results from
1615 * @param array an associative array of update values
1616 * @param string the where key
Andrey Andreev24276a32012-01-08 02:44:38 +02001617 * @return bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001618 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001619 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001620 {
1621 // Combine any cached components with the current statements
1622 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001623
Derek Jonesd10e8962010-03-02 17:10:36 -06001624 if (is_null($index))
1625 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001626 return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001627 }
1628
1629 if ( ! is_null($set))
1630 {
1631 $this->set_update_batch($set, $index);
1632 }
1633
Andrey Andreev24276a32012-01-08 02:44:38 +02001634 if (count($this->ar_set) === 0)
Derek Jonesd10e8962010-03-02 17:10:36 -06001635 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001636 return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001637 }
1638
1639 if ($table == '')
1640 {
1641 if ( ! isset($this->ar_from[0]))
1642 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001643 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001644 }
Barry Mienydd671972010-10-04 16:33:58 +02001645
Derek Jonesd10e8962010-03-02 17:10:36 -06001646 $table = $this->ar_from[0];
1647 }
Barry Mienydd671972010-10-04 16:33:58 +02001648
Derek Jonesd10e8962010-03-02 17:10:36 -06001649 // Batch this baby
Andrey Andreev24276a32012-01-08 02:44:38 +02001650 for ($i = 0, $total = count($this->ar_set); $i < $total; $i += 100)
Derek Jonesd10e8962010-03-02 17:10:36 -06001651 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001652 $this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->ar_set, $i, 100), $this->protect_identifiers($index), $this->ar_where));
Derek Jonesd10e8962010-03-02 17:10:36 -06001653 }
Barry Mienydd671972010-10-04 16:33:58 +02001654
Derek Jonesd10e8962010-03-02 17:10:36 -06001655 $this->_reset_write();
Andrey Andreev24276a32012-01-08 02:44:38 +02001656 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001657 }
1658
1659 // --------------------------------------------------------------------
1660
1661 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001662 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001663 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001664 * @param array
1665 * @param string
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001666 * @param bool
Derek Jonesd10e8962010-03-02 17:10:36 -06001667 * @return object
1668 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001669 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001670 {
1671 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001672
Derek Jonesd10e8962010-03-02 17:10:36 -06001673 if ( ! is_array($key))
1674 {
1675 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001676 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001677
1678 foreach ($key as $k => $v)
1679 {
1680 $index_set = FALSE;
1681 $clean = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001682 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001683 {
1684 if ($k2 == $index)
1685 {
1686 $index_set = TRUE;
1687 }
1688 else
1689 {
1690 $not[] = $k.'-'.$v;
1691 }
1692
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001693 $clean[$this->protect_identifiers($k2)] = ($escape === FALSE) ? $v2 : $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001694 }
1695
1696 if ($index_set == FALSE)
1697 {
1698 return $this->display_error('db_batch_missing_index');
1699 }
1700
1701 $this->ar_set[] = $clean;
1702 }
Barry Mienydd671972010-10-04 16:33:58 +02001703
Derek Jonesd10e8962010-03-02 17:10:36 -06001704 return $this;
1705 }
1706
Derek Allard2067d1a2008-11-13 22:59:24 +00001707 // --------------------------------------------------------------------
1708
1709 /**
1710 * Empty Table
1711 *
1712 * Compiles a delete string and runs "DELETE FROM table"
1713 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001714 * @param string the table to empty
1715 * @return object
1716 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001717 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001718 {
1719 if ($table == '')
1720 {
1721 if ( ! isset($this->ar_from[0]))
1722 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001723 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001724 }
1725
1726 $table = $this->ar_from[0];
1727 }
1728 else
1729 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001730 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 }
1732
1733 $sql = $this->_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001734 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001735 return $this->query($sql);
1736 }
1737
1738 // --------------------------------------------------------------------
1739
1740 /**
1741 * Truncate
1742 *
1743 * Compiles a truncate string and runs the query
1744 * If the database does not support the truncate() command
1745 * This function maps to "DELETE FROM table"
1746 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001747 * @param string the table to truncate
1748 * @return object
1749 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001750 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001751 {
1752 if ($table == '')
1753 {
1754 if ( ! isset($this->ar_from[0]))
1755 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001756 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001757 }
1758
1759 $table = $this->ar_from[0];
1760 }
1761 else
1762 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001763 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001764 }
1765
1766 $sql = $this->_truncate($table);
Derek Allard2067d1a2008-11-13 22:59:24 +00001767 $this->_reset_write();
Derek Allard2067d1a2008-11-13 22:59:24 +00001768 return $this->query($sql);
1769 }
WanWizard7219c072011-12-28 14:09:05 +01001770
Kyle Farris0c147b32011-08-26 02:29:31 -04001771 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001772
Kyle Farris0c147b32011-08-26 02:29:31 -04001773 /**
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03001774 * Truncate statement
1775 *
1776 * Generates a platform-specific truncate string from the supplied data
1777 *
1778 * If the database does not support the truncate() command,
1779 * then this method maps to 'DELETE FROM table'
1780 *
1781 * @param string the table name
1782 * @return string
1783 */
1784 protected function _truncate($table)
1785 {
1786 return 'TRUNCATE '.$table;
1787 }
1788
1789 // --------------------------------------------------------------------
1790
1791 /**
Kyle Farris0c147b32011-08-26 02:29:31 -04001792 * Get DELETE query string
1793 *
1794 * Compiles a delete query string and returns the sql
1795 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001796 * @param string the table to delete from
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001797 * @param bool TRUE: reset AR values; FALSE: leave AR values alone
Kyle Farris0c147b32011-08-26 02:29:31 -04001798 * @return string
1799 */
1800 public function get_compiled_delete($table = '', $reset = TRUE)
1801 {
1802 $this->return_delete_sql = TRUE;
1803 $sql = $this->delete($table, '', NULL, $reset);
1804 $this->return_delete_sql = FALSE;
1805 return $sql;
1806 }
WanWizard7219c072011-12-28 14:09:05 +01001807
Derek Allard2067d1a2008-11-13 22:59:24 +00001808 // --------------------------------------------------------------------
1809
1810 /**
1811 * Delete
1812 *
1813 * Compiles a delete string and runs the query
1814 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001815 * @param mixed the table(s) to delete from. String or array
1816 * @param mixed the where clause
1817 * @param mixed the limit clause
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03001818 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +00001819 * @return object
1820 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001821 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001822 {
1823 // Combine any cached components with the current statements
1824 $this->_merge_cache();
1825
1826 if ($table == '')
1827 {
1828 if ( ! isset($this->ar_from[0]))
1829 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001830 return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001831 }
1832
1833 $table = $this->ar_from[0];
1834 }
1835 elseif (is_array($table))
1836 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001837 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001838 {
1839 $this->delete($single_table, $where, $limit, FALSE);
1840 }
1841
1842 $this->_reset_write();
1843 return;
1844 }
1845 else
1846 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +02001847 $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001848 }
1849
1850 if ($where != '')
1851 {
1852 $this->where($where);
1853 }
1854
1855 if ($limit != NULL)
1856 {
1857 $this->limit($limit);
1858 }
1859
Andrey Andreev24276a32012-01-08 02:44:38 +02001860 if (count($this->ar_where) === 0 && count($this->ar_wherein) === 0 && count($this->ar_like) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001861 {
Andrey Andreev24276a32012-01-08 02:44:38 +02001862 return ($this->db_debug) ? $this->display_error('db_del_must_use_where') : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001863 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001864
1865 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
Derek Allard2067d1a2008-11-13 22:59:24 +00001866 if ($reset_data)
1867 {
1868 $this->_reset_write();
1869 }
WanWizard7219c072011-12-28 14:09:05 +01001870
Andrey Andreev24276a32012-01-08 02:44:38 +02001871 return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001872 }
WanWizard7219c072011-12-28 14:09:05 +01001873
Derek Allard2067d1a2008-11-13 22:59:24 +00001874 // --------------------------------------------------------------------
1875
1876 /**
Andrey Andreevc01d3162012-04-09 12:55:11 +03001877 * Delete statement
1878 *
1879 * Generates a platform-specific delete string from the supplied data
1880 *
1881 * @param string the table name
1882 * @param array the where clause
1883 * @param array the like clause
1884 * @param string the limit clause
1885 * @return string
1886 */
1887 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
1888 {
1889 $conditions = array();
1890
1891 empty($where) OR $conditions[] = implode(' ', $where);
1892 empty($like) OR $conditions[] = implode(' ', $like);
1893
1894 return 'DELETE FROM '.$table
1895 .(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '')
1896 .($limit ? ' LIMIT '.$limit : '');
1897 }
1898
1899 // --------------------------------------------------------------------
1900
1901 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001902 * DB Prefix
1903 *
1904 * Prepends a database prefix if one exists in configuration
1905 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001906 * @param string the table
1907 * @return string
1908 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001909 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001910 {
1911 if ($table == '')
1912 {
1913 $this->display_error('db_table_name_required');
1914 }
1915
1916 return $this->dbprefix.$table;
1917 }
1918
1919 // --------------------------------------------------------------------
1920
1921 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001922 * Set DB Prefix
1923 *
1924 * Set's the DB Prefix to something new without needing to reconnect
1925 *
1926 * @param string the prefix
1927 * @return string
1928 */
1929 public function set_dbprefix($prefix = '')
1930 {
1931 return $this->dbprefix = $prefix;
1932 }
1933
1934 // --------------------------------------------------------------------
1935
1936 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001937 * Track Aliases
1938 *
1939 * Used to track SQL statements written with aliased tables.
1940 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001941 * @param string The table to inspect
1942 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001943 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001944 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001945 {
1946 if (is_array($table))
1947 {
1948 foreach ($table as $t)
1949 {
1950 $this->_track_aliases($t);
1951 }
1952 return;
1953 }
Barry Mienydd671972010-10-04 16:33:58 +02001954
Derek Jones37f4b9c2011-07-01 17:56:50 -05001955 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001956 // the string into discreet statements
1957 if (strpos($table, ',') !== FALSE)
1958 {
1959 return $this->_track_aliases(explode(',', $table));
1960 }
Barry Mienydd671972010-10-04 16:33:58 +02001961
Derek Allard2067d1a2008-11-13 22:59:24 +00001962 // if a table alias is used we can recognize it by a space
Andrey Andreev24276a32012-01-08 02:44:38 +02001963 if (strpos($table, ' ') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001964 {
1965 // if the alias is written with the AS keyword, remove it
1966 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001967
Derek Allard2067d1a2008-11-13 22:59:24 +00001968 // Grab the alias
Andrey Andreev24276a32012-01-08 02:44:38 +02001969 $table = trim(strrchr($table, ' '));
Barry Mienydd671972010-10-04 16:33:58 +02001970
Derek Allard2067d1a2008-11-13 22:59:24 +00001971 // Store the alias, if it doesn't already exist
1972 if ( ! in_array($table, $this->ar_aliased_tables))
1973 {
1974 $this->ar_aliased_tables[] = $table;
1975 }
1976 }
1977 }
WanWizard7219c072011-12-28 14:09:05 +01001978
Derek Allard2067d1a2008-11-13 22:59:24 +00001979 // --------------------------------------------------------------------
1980
1981 /**
1982 * Compile the SELECT statement
1983 *
1984 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001985 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001986 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001987 * @return string
1988 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001989 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001990 {
1991 // Combine any cached components with the current statements
1992 $this->_merge_cache();
1993
Derek Allard2067d1a2008-11-13 22:59:24 +00001994 // Write the "select" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00001995 if ($select_override !== FALSE)
1996 {
1997 $sql = $select_override;
1998 }
1999 else
2000 {
2001 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02002002
Andrey Andreev24276a32012-01-08 02:44:38 +02002003 if (count($this->ar_select) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002004 {
Barry Mienydd671972010-10-04 16:33:58 +02002005 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00002006 }
2007 else
Barry Mienydd671972010-10-04 16:33:58 +02002008 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002009 // Cycle through the "select" portion of the query and prep each column name.
2010 // The reason we protect identifiers here rather then in the select() function
2011 // is because until the user calls the from() function we don't know if there are aliases
2012 foreach ($this->ar_select as $key => $val)
2013 {
Phil Sturgeon77cc0282011-08-09 16:03:49 -06002014 $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL;
Andrey Andreev032e7ea2012-03-06 19:48:35 +02002015 $this->ar_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00002016 }
Barry Mienydd671972010-10-04 16:33:58 +02002017
Derek Allard2067d1a2008-11-13 22:59:24 +00002018 $sql .= implode(', ', $this->ar_select);
2019 }
2020 }
2021
Derek Allard2067d1a2008-11-13 22:59:24 +00002022 // Write the "FROM" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002023 if (count($this->ar_from) > 0)
2024 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002025 $sql .= "\nFROM ".$this->_from_tables($this->ar_from);
Derek Allard2067d1a2008-11-13 22:59:24 +00002026 }
2027
Derek Allard2067d1a2008-11-13 22:59:24 +00002028 // Write the "JOIN" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002029 if (count($this->ar_join) > 0)
2030 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002031 $sql .= "\n".implode("\n", $this->ar_join);
Derek Allard2067d1a2008-11-13 22:59:24 +00002032 }
2033
Derek Allard2067d1a2008-11-13 22:59:24 +00002034 // Write the "WHERE" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002035 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
2036 {
Greg Akere156c6e2011-04-20 16:03:04 -05002037 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00002038 }
2039
2040 $sql .= implode("\n", $this->ar_where);
2041
Derek Allard2067d1a2008-11-13 22:59:24 +00002042 // Write the "LIKE" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002043 if (count($this->ar_like) > 0)
2044 {
2045 if (count($this->ar_where) > 0)
2046 {
2047 $sql .= "\nAND ";
2048 }
2049
2050 $sql .= implode("\n", $this->ar_like);
2051 }
2052
Derek Allard2067d1a2008-11-13 22:59:24 +00002053 // Write the "GROUP BY" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002054 if (count($this->ar_groupby) > 0)
2055 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002056 $sql .= "\nGROUP BY ".implode(', ', $this->ar_groupby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002057 }
2058
Derek Allard2067d1a2008-11-13 22:59:24 +00002059 // Write the "HAVING" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002060 if (count($this->ar_having) > 0)
2061 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002062 $sql .= "\nHAVING ".implode("\n", $this->ar_having);
Derek Allard2067d1a2008-11-13 22:59:24 +00002063 }
2064
Derek Allard2067d1a2008-11-13 22:59:24 +00002065 // Write the "ORDER BY" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002066 if (count($this->ar_orderby) > 0)
2067 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002068 $sql .= "\nORDER BY ".implode(', ', $this->ar_orderby);
Derek Allard2067d1a2008-11-13 22:59:24 +00002069 if ($this->ar_order !== FALSE)
2070 {
2071 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02002072 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002073 }
2074
Derek Allard2067d1a2008-11-13 22:59:24 +00002075 // Write the "LIMIT" portion of the query
Derek Allard2067d1a2008-11-13 22:59:24 +00002076 if (is_numeric($this->ar_limit))
2077 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002078 return $this->_limit($sql."\n", $this->ar_limit, $this->ar_offset);
Derek Allard2067d1a2008-11-13 22:59:24 +00002079 }
2080
2081 return $sql;
2082 }
2083
2084 // --------------------------------------------------------------------
2085
2086 /**
2087 * Object to Array
2088 *
2089 * Takes an object as input and converts the class variables to array key/vals
2090 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002091 * @param object
2092 * @return array
2093 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002094 public function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002095 {
2096 if ( ! is_object($object))
2097 {
2098 return $object;
2099 }
Barry Mienydd671972010-10-04 16:33:58 +02002100
Derek Allard2067d1a2008-11-13 22:59:24 +00002101 $array = array();
2102 foreach (get_object_vars($object) as $key => $val)
2103 {
2104 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06002105 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002106 {
2107 $array[$key] = $val;
2108 }
2109 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002110
2111 return $array;
2112 }
Barry Mienydd671972010-10-04 16:33:58 +02002113
Derek Jonesd10e8962010-03-02 17:10:36 -06002114 // --------------------------------------------------------------------
2115
2116 /**
2117 * Object to Array
2118 *
2119 * Takes an object as input and converts the class variables to array key/vals
2120 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002121 * @param object
2122 * @return array
2123 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002124 public function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002125 {
2126 if ( ! is_object($object))
2127 {
2128 return $object;
2129 }
Barry Mienydd671972010-10-04 16:33:58 +02002130
Derek Jonesd10e8962010-03-02 17:10:36 -06002131 $array = array();
2132 $out = get_object_vars($object);
2133 $fields = array_keys($out);
2134
2135 foreach ($fields as $val)
2136 {
2137 // There are some built in keys we need to ignore for this conversion
Andrey Andreev24276a32012-01-08 02:44:38 +02002138 if ($val !== '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002139 {
Derek Jonesd10e8962010-03-02 17:10:36 -06002140 $i = 0;
2141 foreach ($out[$val] as $data)
2142 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002143 $array[$i++][$val] = $data;
Derek Jonesd10e8962010-03-02 17:10:36 -06002144 }
2145 }
2146 }
2147
Derek Allard2067d1a2008-11-13 22:59:24 +00002148 return $array;
2149 }
Barry Mienydd671972010-10-04 16:33:58 +02002150
Derek Allard2067d1a2008-11-13 22:59:24 +00002151 // --------------------------------------------------------------------
2152
2153 /**
2154 * Start Cache
2155 *
2156 * Starts AR caching
2157 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002158 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002159 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002160 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002161 {
2162 $this->ar_caching = TRUE;
2163 }
2164
2165 // --------------------------------------------------------------------
2166
2167 /**
2168 * Stop Cache
2169 *
2170 * Stops AR caching
2171 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002172 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002173 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002174 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002175 {
2176 $this->ar_caching = FALSE;
2177 }
2178
2179 // --------------------------------------------------------------------
2180
2181 /**
2182 * Flush Cache
2183 *
2184 * Empties the AR cache
2185 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002186 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002187 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002188 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002189 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002190 $this->_reset_run(array(
2191 'ar_cache_select' => array(),
2192 'ar_cache_from' => array(),
2193 'ar_cache_join' => array(),
2194 'ar_cache_where' => array(),
2195 'ar_cache_like' => array(),
2196 'ar_cache_groupby' => array(),
2197 'ar_cache_having' => array(),
2198 'ar_cache_orderby' => array(),
2199 'ar_cache_set' => array(),
2200 'ar_cache_exists' => array(),
2201 'ar_cache_no_escape' => array()
2202 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002203 }
2204
2205 // --------------------------------------------------------------------
2206
2207 /**
2208 * Merge Cache
2209 *
Barry Mienydd671972010-10-04 16:33:58 +02002210 * When called, this function merges any cached AR arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002211 * locally called ones.
2212 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002213 * @return void
2214 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002215 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002216 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002217 if (count($this->ar_cache_exists) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002218 {
2219 return;
2220 }
2221
2222 foreach ($this->ar_cache_exists as $val)
2223 {
2224 $ar_variable = 'ar_'.$val;
2225 $ar_cache_var = 'ar_cache_'.$val;
2226
Andrey Andreev24276a32012-01-08 02:44:38 +02002227 if (count($this->$ar_cache_var) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002228 {
2229 continue;
2230 }
2231
2232 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
2233 }
2234
2235 // If we are "protecting identifiers" we need to examine the "from"
2236 // portion of the query to determine if there are any aliases
Andrey Andreeva8bb4be2012-03-26 15:54:23 +03002237 if ($this->_protect_identifiers === TRUE && count($this->ar_cache_from) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00002238 {
2239 $this->_track_aliases($this->ar_from);
2240 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002241
2242 $this->ar_no_escape = $this->ar_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002243 }
WanWizard7219c072011-12-28 14:09:05 +01002244
Kyle Farris0c147b32011-08-26 02:29:31 -04002245 // --------------------------------------------------------------------
2246
2247 /**
2248 * Reset Active Record values.
WanWizard7219c072011-12-28 14:09:05 +01002249 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002250 * Publicly-visible method to reset the AR values.
2251 *
Kyle Farris0c147b32011-08-26 02:29:31 -04002252 * @return void
2253 */
2254 public function reset_query()
2255 {
2256 $this->_reset_select();
2257 $this->_reset_write();
2258 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002259
2260 // --------------------------------------------------------------------
2261
2262 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002263 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002264 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002265 * @param array An array of fields to reset
2266 * @return void
2267 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002268 protected function _reset_run($ar_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002269 {
2270 foreach ($ar_reset_items as $item => $default_value)
2271 {
2272 if ( ! in_array($item, $this->ar_store_array))
2273 {
2274 $this->$item = $default_value;
2275 }
2276 }
2277 }
2278
2279 // --------------------------------------------------------------------
2280
2281 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002282 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002283 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002284 * @return void
2285 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002286 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002287 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002288 $this->_reset_run(array(
2289 'ar_select' => array(),
2290 'ar_from' => array(),
2291 'ar_join' => array(),
2292 'ar_where' => array(),
2293 'ar_like' => array(),
2294 'ar_groupby' => array(),
2295 'ar_having' => array(),
2296 'ar_orderby' => array(),
2297 'ar_wherein' => array(),
2298 'ar_aliased_tables' => array(),
2299 'ar_no_escape' => array(),
2300 'ar_distinct' => FALSE,
2301 'ar_limit' => FALSE,
2302 'ar_offset' => FALSE,
2303 'ar_order' => FALSE
2304 )
2305 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002306 }
Barry Mienydd671972010-10-04 16:33:58 +02002307
Derek Allard2067d1a2008-11-13 22:59:24 +00002308 // --------------------------------------------------------------------
2309
2310 /**
2311 * Resets the active record "write" values.
2312 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002313 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002314 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002315 * @return void
2316 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002317 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002318 {
Andrey Andreev24276a32012-01-08 02:44:38 +02002319 $this->_reset_run(array(
Timothy Warren215890b2012-03-20 09:38:16 -04002320 'ar_set' => array(),
2321 'ar_from' => array(),
2322 'ar_where' => array(),
2323 'ar_like' => array(),
2324 'ar_orderby' => array(),
2325 'ar_keys' => array(),
2326 'ar_limit' => FALSE,
2327 'ar_order' => FALSE
2328 )
2329 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002330 }
Andrey Andreev24276a32012-01-08 02:44:38 +02002331
Derek Allard2067d1a2008-11-13 22:59:24 +00002332}
2333
2334/* End of file DB_active_rec.php */
Timothy Warren215890b2012-03-20 09:38:16 -04002335/* Location: ./system/database/DB_active_rec.php */