blob: 5c4284ff2594f4025c1b72601b75b4f035d9e8eb [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Active Record Class
32 *
33 * This is the platform-independent base Active Record implementation class.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_active_record extends CI_DB_driver {
42
Kyle Farris76116012011-08-31 11:17:48 -040043 protected $return_delete_sql = FALSE;
44 protected $reset_delete_data = FALSE;
Kyle Farris0c147b32011-08-26 02:29:31 -040045
Kyle Farris76116012011-08-31 11:17:48 -040046 protected $ar_select = array();
47 protected $ar_distinct = FALSE;
48 protected $ar_from = array();
49 protected $ar_join = array();
50 protected $ar_where = array();
51 protected $ar_like = array();
52 protected $ar_groupby = array();
53 protected $ar_having = array();
54 protected $ar_keys = array();
55 protected $ar_limit = FALSE;
56 protected $ar_offset = FALSE;
57 protected $ar_order = FALSE;
58 protected $ar_orderby = array();
59 protected $ar_set = array();
60 protected $ar_wherein = array();
61 protected $ar_aliased_tables = array();
62 protected $ar_store_array = array();
Barry Mienydd671972010-10-04 16:33:58 +020063
Derek Allard2067d1a2008-11-13 22:59:24 +000064 // Active Record Caching variables
Kyle Farris76116012011-08-31 11:17:48 -040065 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();
Derek Jones37f4b9c2011-07-01 17:56:50 -050076
Kyle Farris76116012011-08-31 11:17:48 -040077 protected $ar_no_escape = array();
78 protected $ar_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000079
80 // --------------------------------------------------------------------
81
82 /**
83 * Select
84 *
85 * Generates the SELECT portion of the query
86 *
Derek Allard2067d1a2008-11-13 22:59:24 +000087 * @param string
88 * @return object
89 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060090 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Derek Allard2067d1a2008-11-13 22:59:24 +000092 if (is_string($select))
93 {
94 $select = explode(',', $select);
95 }
96
97 foreach ($select as $val)
98 {
99 $val = trim($val);
100
101 if ($val != '')
102 {
103 $this->ar_select[] = $val;
Greg Akere156c6e2011-04-20 16:03:04 -0500104 $this->ar_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000105
106 if ($this->ar_caching === TRUE)
107 {
108 $this->ar_cache_select[] = $val;
109 $this->ar_cache_exists[] = 'select';
Greg Aker2e1837a2011-05-06 12:17:04 -0500110 $this->ar_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
112 }
113 }
114 return $this;
115 }
116
117 // --------------------------------------------------------------------
118
119 /**
120 * Select Max
121 *
122 * Generates a SELECT MAX(field) portion of a query
123 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 * @param string the field
125 * @param string an alias
126 * @return object
127 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600128 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
130 return $this->_max_min_avg_sum($select, $alias, 'MAX');
131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 // --------------------------------------------------------------------
134
135 /**
136 * Select Min
137 *
138 * Generates a SELECT MIN(field) portion of a query
139 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 * @param string the field
141 * @param string an alias
142 * @return object
143 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600144 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
146 return $this->_max_min_avg_sum($select, $alias, 'MIN');
147 }
148
149 // --------------------------------------------------------------------
150
151 /**
152 * Select Average
153 *
154 * Generates a SELECT AVG(field) portion of a query
155 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 * @param string the field
157 * @param string an alias
158 * @return object
159 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600160 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
162 return $this->_max_min_avg_sum($select, $alias, 'AVG');
163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Select Sum
169 *
170 * Generates a SELECT SUM(field) portion of a query
171 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 * @param string the field
173 * @param string an alias
174 * @return object
175 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600176 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
178 return $this->_max_min_avg_sum($select, $alias, 'SUM');
179 }
180
181 // --------------------------------------------------------------------
182
183 /**
184 * Processing Function for the four functions above:
185 *
186 * select_max()
187 * select_min()
188 * select_avg()
Derek Jones37f4b9c2011-07-01 17:56:50 -0500189 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200190 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 * @param string the field
192 * @param string an alias
193 * @return object
194 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600195 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
197 if ( ! is_string($select) OR $select == '')
198 {
199 $this->display_error('db_invalid_query');
200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
205 {
206 show_error('Invalid function type: '.$type);
207 }
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 if ($alias == '')
210 {
211 $alias = $this->_create_alias_from_table(trim($select));
212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Juan José González8b4d83b2011-09-27 17:21:14 -0500214 $sql = $this->_protect_identifiers($type.'('.trim($select).')').' AS '.$this->_protect_identifiers(trim($alias));
Derek Allard2067d1a2008-11-13 22:59:24 +0000215
216 $this->ar_select[] = $sql;
Greg Aker827f3de2011-05-06 11:29:57 -0500217 $this->ar_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 if ($this->ar_caching === TRUE)
220 {
221 $this->ar_cache_select[] = $sql;
222 $this->ar_cache_exists[] = 'select';
223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 return $this;
226 }
227
228 // --------------------------------------------------------------------
229
230 /**
231 * Determines the alias name based on the table
232 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 * @param string
234 * @return string
235 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600236 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
238 if (strpos($item, '.') !== FALSE)
239 {
240 return end(explode('.', $item));
241 }
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 return $item;
244 }
245
246 // --------------------------------------------------------------------
247
248 /**
249 * DISTINCT
250 *
251 * Sets a flag which tells the query string compiler to add DISTINCT
252 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 * @param bool
254 * @return object
255 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600256 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
258 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
259 return $this;
260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 // --------------------------------------------------------------------
263
264 /**
265 * From
266 *
267 * Generates the FROM portion of the query
268 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 * @param mixed can be a string or array
270 * @return object
271 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600272 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
274 foreach ((array)$from as $val)
275 {
276 if (strpos($val, ',') !== FALSE)
277 {
278 foreach (explode(',', $val) as $v)
279 {
280 $v = trim($v);
281 $this->_track_aliases($v);
282
283 $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 if ($this->ar_caching === TRUE)
286 {
287 $this->ar_cache_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
288 $this->ar_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200289 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
291
292 }
293 else
294 {
295 $val = trim($val);
296
Derek Jones37f4b9c2011-07-01 17:56:50 -0500297 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200298 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 if ($this->ar_caching === TRUE)
304 {
305 $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
306 $this->ar_cache_exists[] = 'from';
307 }
308 }
309 }
310
311 return $this;
312 }
313
314 // --------------------------------------------------------------------
315
316 /**
317 * Join
318 *
319 * Generates the JOIN portion of the query
320 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 * @param string
322 * @param string the join condition
323 * @param string the type of join
324 * @return object
325 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600326 public function join($table, $cond, $type = '')
Barry Mienydd671972010-10-04 16:33:58 +0200327 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 if ($type != '')
329 {
330 $type = strtoupper(trim($type));
331
332 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
333 {
334 $type = '';
335 }
336 else
337 {
338 $type .= ' ';
339 }
340 }
341
Derek Jones37f4b9c2011-07-01 17:56:50 -0500342 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200343 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 $this->_track_aliases($table);
345
346 // Strip apart the condition and protect the identifiers
347 if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
348 {
349 $match[1] = $this->_protect_identifiers($match[1]);
350 $match[3] = $this->_protect_identifiers($match[3]);
Barry Mienydd671972010-10-04 16:33:58 +0200351
352 $cond = $match[1].$match[2].$match[3];
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 // Assemble the JOIN statement
356 $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
357
358 $this->ar_join[] = $join;
359 if ($this->ar_caching === TRUE)
360 {
361 $this->ar_cache_join[] = $join;
362 $this->ar_cache_exists[] = 'join';
363 }
364
365 return $this;
366 }
367
368 // --------------------------------------------------------------------
369
370 /**
371 * Where
372 *
373 * Generates the WHERE portion of the query. Separates
374 * multiple calls with AND
375 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 * @param mixed
377 * @param mixed
378 * @return object
379 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600380 public function where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
382 return $this->_where($key, $value, 'AND ', $escape);
383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 // --------------------------------------------------------------------
386
387 /**
388 * OR Where
389 *
390 * Generates the WHERE portion of the query. Separates
391 * multiple calls with OR
392 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 * @param mixed
394 * @param mixed
395 * @return object
396 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600397 public function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
399 return $this->_where($key, $value, 'OR ', $escape);
400 }
401
402 // --------------------------------------------------------------------
403
404 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * Where
406 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600407 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 * @param mixed
410 * @param mixed
411 * @param string
412 * @return object
413 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600414 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
416 if ( ! is_array($key))
417 {
418 $key = array($key => $value);
419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 // If the escape value was not set will will base it on the global setting
422 if ( ! is_bool($escape))
423 {
424 $escape = $this->_protect_identifiers;
425 }
426
427 foreach ($key as $k => $v)
428 {
429 $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type;
430
431 if (is_null($v) && ! $this->_has_operator($k))
432 {
433 // value appears not to have been set, assign the test to IS NULL
434 $k .= ' IS NULL';
435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 if ( ! is_null($v))
438 {
439 if ($escape === TRUE)
440 {
441 $k = $this->_protect_identifiers($k, FALSE, $escape);
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 $v = ' '.$this->escape($v);
444 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 if ( ! $this->_has_operator($k))
447 {
Greg Akere156c6e2011-04-20 16:03:04 -0500448 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
450 }
451 else
452 {
Barry Mienydd671972010-10-04 16:33:58 +0200453 $k = $this->_protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
455
456 $this->ar_where[] = $prefix.$k.$v;
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 if ($this->ar_caching === TRUE)
459 {
460 $this->ar_cache_where[] = $prefix.$k.$v;
461 $this->ar_cache_exists[] = 'where';
462 }
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 return $this;
467 }
468
469 // --------------------------------------------------------------------
470
471 /**
472 * Where_in
473 *
474 * Generates a WHERE field IN ('item', 'item') SQL query joined with
475 * AND if appropriate
476 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 * @param string The field to search
478 * @param array The values searched on
479 * @return object
480 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600481 public function where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
483 return $this->_where_in($key, $values);
484 }
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 // --------------------------------------------------------------------
487
488 /**
489 * Where_in_or
490 *
491 * Generates a WHERE field IN ('item', 'item') SQL query joined with
492 * OR if appropriate
493 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 * @param string The field to search
495 * @param array The values searched on
496 * @return object
497 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600498 public function or_where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 {
500 return $this->_where_in($key, $values, FALSE, 'OR ');
501 }
502
503 // --------------------------------------------------------------------
504
505 /**
506 * Where_not_in
507 *
508 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
509 * with AND if appropriate
510 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 * @param string The field to search
512 * @param array The values searched on
513 * @return object
514 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600515 public function where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 {
517 return $this->_where_in($key, $values, TRUE);
518 }
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 // --------------------------------------------------------------------
521
522 /**
523 * Where_not_in_or
524 *
525 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
526 * with OR if appropriate
527 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 * @param string The field to search
529 * @param array The values searched on
530 * @return object
531 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600532 public function or_where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
534 return $this->_where_in($key, $values, TRUE, 'OR ');
535 }
536
537 // --------------------------------------------------------------------
538
539 /**
540 * Where_in
541 *
542 * Called by where_in, where_in_or, where_not_in, where_not_in_or
543 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 * @param string The field to search
545 * @param array The values searched on
546 * @param boolean If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200547 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 * @return object
549 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600550 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 {
552 if ($key === NULL OR $values === NULL)
553 {
554 return;
555 }
Barry Mienydd671972010-10-04 16:33:58 +0200556
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 if ( ! is_array($values))
558 {
559 $values = array($values);
560 }
Barry Mienydd671972010-10-04 16:33:58 +0200561
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 $not = ($not) ? ' NOT' : '';
563
564 foreach ($values as $value)
565 {
566 $this->ar_wherein[] = $this->escape($value);
567 }
568
569 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Barry Mienydd671972010-10-04 16:33:58 +0200570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
572
573 $this->ar_where[] = $where_in;
574 if ($this->ar_caching === TRUE)
575 {
576 $this->ar_cache_where[] = $where_in;
577 $this->ar_cache_exists[] = 'where';
578 }
579
580 // reset the array for multiple calls
581 $this->ar_wherein = array();
582 return $this;
583 }
Barry Mienydd671972010-10-04 16:33:58 +0200584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 // --------------------------------------------------------------------
586
587 /**
588 * Like
589 *
590 * Generates a %LIKE% portion of the query. Separates
591 * multiple calls with AND
592 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 * @param mixed
594 * @param mixed
595 * @return object
596 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600597 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
599 return $this->_like($field, $match, 'AND ', $side);
600 }
601
602 // --------------------------------------------------------------------
603
604 /**
605 * Not Like
606 *
607 * Generates a NOT LIKE portion of the query. Separates
608 * multiple calls with AND
609 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 * @param mixed
611 * @param mixed
612 * @return object
613 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600614 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 {
616 return $this->_like($field, $match, 'AND ', $side, 'NOT');
617 }
Barry Mienydd671972010-10-04 16:33:58 +0200618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 // --------------------------------------------------------------------
620
621 /**
622 * OR Like
623 *
624 * Generates a %LIKE% portion of the query. Separates
625 * multiple calls with OR
626 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 * @param mixed
628 * @param mixed
629 * @return object
630 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600631 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 {
633 return $this->_like($field, $match, 'OR ', $side);
634 }
635
636 // --------------------------------------------------------------------
637
638 /**
639 * OR Not Like
640 *
641 * Generates a NOT LIKE portion of the query. Separates
642 * multiple calls with OR
643 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 * @param mixed
645 * @param mixed
646 * @return object
647 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600648 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 {
650 return $this->_like($field, $match, 'OR ', $side, 'NOT');
651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 // --------------------------------------------------------------------
654
655 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 * Like
657 *
658 * Called by like() or orlike()
659 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 * @param mixed
661 * @param mixed
662 * @param string
663 * @return object
664 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600665 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 {
667 if ( ! is_array($field))
668 {
669 $field = array($field => $match);
670 }
Barry Mienydd671972010-10-04 16:33:58 +0200671
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 foreach ($field as $k => $v)
673 {
674 $k = $this->_protect_identifiers($k);
675
676 $prefix = (count($this->ar_like) == 0) ? '' : $type;
677
Derek Jonese4ed5832009-02-20 21:44:59 +0000678 $v = $this->escape_like_str($v);
Kyle Farris0c147b32011-08-26 02:29:31 -0400679
Nithinea4ad9b2011-08-21 01:23:47 -0300680 if ($side == 'none')
681 {
682 $like_statement = $prefix." $k $not LIKE '{$v}'";
683 }
684 elseif ($side == 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 {
686 $like_statement = $prefix." $k $not LIKE '%{$v}'";
687 }
688 elseif ($side == 'after')
689 {
690 $like_statement = $prefix." $k $not LIKE '{$v}%'";
691 }
692 else
693 {
694 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
695 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600696
Derek Jonese4ed5832009-02-20 21:44:59 +0000697 // some platforms require an escape sequence definition for LIKE wildcards
698 if ($this->_like_escape_str != '')
699 {
Greg Aker0d424892010-01-26 02:14:44 +0000700 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000701 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600702
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 $this->ar_like[] = $like_statement;
704 if ($this->ar_caching === TRUE)
705 {
706 $this->ar_cache_like[] = $like_statement;
707 $this->ar_cache_exists[] = 'like';
708 }
Barry Mienydd671972010-10-04 16:33:58 +0200709
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 }
711 return $this;
712 }
Barry Mienydd671972010-10-04 16:33:58 +0200713
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 // --------------------------------------------------------------------
715
716 /**
717 * GROUP BY
718 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 * @param string
720 * @return object
721 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600722 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000723 {
724 if (is_string($by))
725 {
726 $by = explode(',', $by);
727 }
Barry Mienydd671972010-10-04 16:33:58 +0200728
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 foreach ($by as $val)
730 {
731 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200732
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 if ($val != '')
734 {
735 $this->ar_groupby[] = $this->_protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200736
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 if ($this->ar_caching === TRUE)
738 {
739 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
740 $this->ar_cache_exists[] = 'groupby';
741 }
742 }
743 }
744 return $this;
745 }
746
747 // --------------------------------------------------------------------
748
749 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 * Sets the HAVING value
751 *
752 * Separates multiple calls with AND
753 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 * @param string
755 * @param string
756 * @return object
757 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600758 public function having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 {
760 return $this->_having($key, $value, 'AND ', $escape);
761 }
Barry Mienydd671972010-10-04 16:33:58 +0200762
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 // --------------------------------------------------------------------
764
765 /**
766 * Sets the OR HAVING value
767 *
768 * Separates multiple calls with OR
769 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000770 * @param string
771 * @param string
772 * @return object
773 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600774 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 {
776 return $this->_having($key, $value, 'OR ', $escape);
777 }
Barry Mienydd671972010-10-04 16:33:58 +0200778
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 // --------------------------------------------------------------------
780
781 /**
782 * Sets the HAVING values
783 *
784 * Called by having() or or_having()
785 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 * @param string
787 * @param string
788 * @return object
789 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600790 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 {
792 if ( ! is_array($key))
793 {
794 $key = array($key => $value);
795 }
Barry Mienydd671972010-10-04 16:33:58 +0200796
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 foreach ($key as $k => $v)
798 {
799 $prefix = (count($this->ar_having) == 0) ? '' : $type;
800
801 if ($escape === TRUE)
802 {
803 $k = $this->_protect_identifiers($k);
804 }
805
806 if ( ! $this->_has_operator($k))
807 {
808 $k .= ' = ';
809 }
810
811 if ($v != '')
812 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400813 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 }
Barry Mienydd671972010-10-04 16:33:58 +0200815
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 $this->ar_having[] = $prefix.$k.$v;
817 if ($this->ar_caching === TRUE)
818 {
819 $this->ar_cache_having[] = $prefix.$k.$v;
820 $this->ar_cache_exists[] = 'having';
821 }
822 }
Barry Mienydd671972010-10-04 16:33:58 +0200823
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 return $this;
825 }
Barry Mienydd671972010-10-04 16:33:58 +0200826
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 // --------------------------------------------------------------------
828
829 /**
830 * Sets the ORDER BY value
831 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 * @param string
833 * @param string direction: asc or desc
834 * @return object
835 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600836 public function order_by($orderby, $direction = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 {
838 if (strtolower($direction) == 'random')
839 {
840 $orderby = ''; // Random results want or don't need a field name
841 $direction = $this->_random_keyword;
842 }
843 elseif (trim($direction) != '')
844 {
845 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
846 }
Barry Mienydd671972010-10-04 16:33:58 +0200847
848
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 if (strpos($orderby, ',') !== FALSE)
850 {
851 $temp = array();
852 foreach (explode(',', $orderby) as $part)
853 {
854 $part = trim($part);
855 if ( ! in_array($part, $this->ar_aliased_tables))
856 {
857 $part = $this->_protect_identifiers(trim($part));
858 }
Barry Mienydd671972010-10-04 16:33:58 +0200859
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 $temp[] = $part;
861 }
Barry Mienydd671972010-10-04 16:33:58 +0200862
863 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 }
Derek Allarde37ab382009-02-03 16:13:57 +0000865 else if ($direction != $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 {
867 $orderby = $this->_protect_identifiers($orderby);
868 }
Barry Mienydd671972010-10-04 16:33:58 +0200869
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200871
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 $this->ar_orderby[] = $orderby_statement;
873 if ($this->ar_caching === TRUE)
874 {
875 $this->ar_cache_orderby[] = $orderby_statement;
876 $this->ar_cache_exists[] = 'orderby';
877 }
878
879 return $this;
880 }
Barry Mienydd671972010-10-04 16:33:58 +0200881
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 // --------------------------------------------------------------------
883
884 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 * Sets the LIMIT value
886 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 * @param integer the limit value
888 * @param integer the offset value
889 * @return object
890 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200891 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 {
Phil Sturgeonb0eae5f2011-08-10 09:00:52 -0600893 $this->ar_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000894
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200895 if ( ! is_null($offset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 {
Phil Sturgeonb0eae5f2011-08-10 09:00:52 -0600897 $this->ar_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 }
Barry Mienydd671972010-10-04 16:33:58 +0200899
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 return $this;
901 }
Barry Mienydd671972010-10-04 16:33:58 +0200902
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 // --------------------------------------------------------------------
904
905 /**
906 * Sets the OFFSET value
907 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 * @param integer the offset value
909 * @return object
910 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600911 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 {
kenjis0e857632011-09-02 08:41:17 +0900913 $this->ar_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 return $this;
915 }
Barry Mienydd671972010-10-04 16:33:58 +0200916
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 // --------------------------------------------------------------------
918
919 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500920 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 * @param mixed
923 * @param string
924 * @param boolean
925 * @return object
926 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600927 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 {
929 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +0200930
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 if ( ! is_array($key))
932 {
933 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +0200934 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000935
936 foreach ($key as $k => $v)
937 {
938 if ($escape === FALSE)
939 {
940 $this->ar_set[$this->_protect_identifiers($k)] = $v;
941 }
942 else
943 {
Phil Sturgeond0ac1a22011-02-15 22:54:08 +0000944 $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 }
946 }
Barry Mienydd671972010-10-04 16:33:58 +0200947
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 return $this;
949 }
Kyle Farris0c147b32011-08-26 02:29:31 -0400950
Kyle Farris0c147b32011-08-26 02:29:31 -0400951 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200952
Kyle Farris0c147b32011-08-26 02:29:31 -0400953 /**
954 * Get SELECT query string
955 *
956 * Compiles a SELECT query string and returns the sql.
957 *
958 * @access public
959 * @param string the table name to select from (optional)
960 * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone
961 * @return string
962 */
963 public function get_compiled_select($table = '', $reset = TRUE)
964 {
965 if ($table != '')
kylefarris0a3176b2011-08-26 02:37:52 -0400966 {
Kyle Farris0c147b32011-08-26 02:29:31 -0400967 $this->_track_aliases($table);
968 $this->from($table);
969 }
970
971 $select = $this->_compile_select();
972
973 if ($reset === TRUE)
974 {
975 $this->_reset_select();
976 }
977
978 return $select;
979 }
980
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 // --------------------------------------------------------------------
982
983 /**
984 * Get
985 *
986 * Compiles the select statement based on the other functions called
987 * and runs the query
988 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 * @param string the table
990 * @param string the limit clause
991 * @param string the offset clause
992 * @return object
993 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600994 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 {
996 if ($table != '')
997 {
998 $this->_track_aliases($table);
999 $this->from($table);
1000 }
Barry Mienydd671972010-10-04 16:33:58 +02001001
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 if ( ! is_null($limit))
1003 {
1004 $this->limit($limit, $offset);
1005 }
Barry Mienydd671972010-10-04 16:33:58 +02001006
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 $sql = $this->_compile_select();
1008
1009 $result = $this->query($sql);
1010 $this->_reset_select();
1011 return $result;
1012 }
1013
1014 /**
1015 * "Count All Results" query
1016 *
Barry Mienydd671972010-10-04 16:33:58 +02001017 * Generates a platform-specific query string that counts all records
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 * returned by an Active Record query.
1019 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001020 * @param string
1021 * @return string
1022 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001023 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 {
1025 if ($table != '')
1026 {
1027 $this->_track_aliases($table);
1028 $this->from($table);
1029 }
Barry Mienydd671972010-10-04 16:33:58 +02001030
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
1032
1033 $query = $this->query($sql);
1034 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001035
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 if ($query->num_rows() == 0)
1037 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001038 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 }
1040
1041 $row = $query->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001042 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 }
1044
1045 // --------------------------------------------------------------------
1046
1047 /**
1048 * Get_Where
1049 *
1050 * Allows the where clause, limit and offset to be added directly
1051 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001052 * @param string the where clause
1053 * @param string the limit clause
1054 * @param string the offset clause
1055 * @return object
1056 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001057 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001058 {
1059 if ($table != '')
1060 {
1061 $this->from($table);
1062 }
1063
1064 if ( ! is_null($where))
1065 {
1066 $this->where($where);
1067 }
Barry Mienydd671972010-10-04 16:33:58 +02001068
Derek Allard2067d1a2008-11-13 22:59:24 +00001069 if ( ! is_null($limit))
1070 {
1071 $this->limit($limit, $offset);
1072 }
Barry Mienydd671972010-10-04 16:33:58 +02001073
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 $sql = $this->_compile_select();
1075
1076 $result = $this->query($sql);
1077 $this->_reset_select();
1078 return $result;
1079 }
1080
1081 // --------------------------------------------------------------------
1082
1083 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001084 * Insert_Batch
1085 *
1086 * Compiles batch insert strings and runs the queries
1087 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001088 * @param string the table to retrieve the results from
1089 * @param array an associative array of insert values
1090 * @return object
1091 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001092 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001093 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001094 if ( ! is_null($set))
1095 {
1096 $this->set_insert_batch($set);
1097 }
Barry Mienydd671972010-10-04 16:33:58 +02001098
Derek Jonesd10e8962010-03-02 17:10:36 -06001099 if (count($this->ar_set) == 0)
1100 {
1101 if ($this->db_debug)
1102 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001103 //No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001104 return $this->display_error('db_must_use_set');
1105 }
1106 return FALSE;
1107 }
1108
1109 if ($table == '')
1110 {
1111 if ( ! isset($this->ar_from[0]))
1112 {
1113 if ($this->db_debug)
1114 {
1115 return $this->display_error('db_must_set_table');
1116 }
1117 return FALSE;
1118 }
Barry Mienydd671972010-10-04 16:33:58 +02001119
Derek Jonesd10e8962010-03-02 17:10:36 -06001120 $table = $this->ar_from[0];
1121 }
1122
1123 // Batch this baby
1124 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1125 {
Barry Mienydd671972010-10-04 16:33:58 +02001126
Derek Jonesd10e8962010-03-02 17:10:36 -06001127 $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
1128
1129 //echo $sql;
1130
1131 $this->query($sql);
1132 }
Barry Mienydd671972010-10-04 16:33:58 +02001133
Derek Jonesd10e8962010-03-02 17:10:36 -06001134 $this->_reset_write();
1135
1136
Barry Mienydd671972010-10-04 16:33:58 +02001137 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001138 }
1139
1140 // --------------------------------------------------------------------
1141
1142 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001143 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001144 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001145 * @param mixed
1146 * @param string
1147 * @param boolean
1148 * @return object
1149 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001150 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001151 {
1152 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001153
Derek Jonesd10e8962010-03-02 17:10:36 -06001154 if ( ! is_array($key))
1155 {
1156 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001157 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001158
1159 $keys = array_keys(current($key));
1160 sort($keys);
1161
1162 foreach ($key as $row)
1163 {
Barry Mienydd671972010-10-04 16:33:58 +02001164 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1165 {
1166 // batch function above returns an error on an empty array
1167 $this->ar_set[] = array();
1168 return;
1169 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001170
Barry Mienydd671972010-10-04 16:33:58 +02001171 ksort($row); // puts $row in the same order as our keys
1172
Derek Jonesd10e8962010-03-02 17:10:36 -06001173 if ($escape === FALSE)
1174 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001175 $this->ar_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001176 }
1177 else
1178 {
1179 $clean = array();
1180
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001181 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001182 {
Barry Mienydd671972010-10-04 16:33:58 +02001183 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001184 }
1185
Derek Jones37f4b9c2011-07-01 17:56:50 -05001186 $this->ar_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001187 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001188 }
1189
1190 foreach ($keys as $k)
1191 {
1192 $this->ar_keys[] = $this->_protect_identifiers($k);
1193 }
Barry Mienydd671972010-10-04 16:33:58 +02001194
Derek Jonesd10e8962010-03-02 17:10:36 -06001195 return $this;
1196 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001197
1198 // --------------------------------------------------------------------
1199
1200 /**
1201 * Get INSERT query string
1202 *
1203 * Compiles an insert query and returns the sql
1204 *
1205 * @access public
1206 * @param string the table to insert into
1207 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1208 * @return string
1209 */
1210 public function get_compiled_insert($table = '', $reset = TRUE)
1211 {
1212 if ($this->_validate_insert($table) === FALSE)
1213 {
1214 return FALSE;
1215 }
1216
Kyle Farris76116012011-08-31 11:17:48 -04001217 $sql = $this->_insert(
1218 $this->_protect_identifiers(
1219 $this->ar_from[0], TRUE, NULL, FALSE
1220 ),
1221 array_keys($this->ar_set),
1222 array_values($this->ar_set)
1223 );
Kyle Farris0c147b32011-08-26 02:29:31 -04001224
1225 if ($reset === TRUE)
1226 {
1227 $this->_reset_write();
1228 }
1229
1230 return $sql;
1231 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001232
Derek Allard2067d1a2008-11-13 22:59:24 +00001233 // --------------------------------------------------------------------
1234
1235 /**
1236 * Insert
1237 *
1238 * Compiles an insert string and runs the query
1239 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001240 * @access public
Phil Sturgeon9789f322011-07-15 15:14:05 -06001241 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 * @param array an associative array of insert values
1243 * @return object
1244 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001245 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001246 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 if ( ! is_null($set))
1248 {
1249 $this->set($set);
1250 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001251
1252 if ($this->_validate_insert($table) === FALSE)
1253 {
1254 return FALSE;
1255 }
1256
Kyle Farris76116012011-08-31 11:17:48 -04001257 $sql = $this->_insert(
1258 $this->_protect_identifiers(
1259 $this->ar_from[0], TRUE, NULL, FALSE
1260 ),
1261 array_keys($this->ar_set),
1262 array_values($this->ar_set)
1263 );
Barry Mienydd671972010-10-04 16:33:58 +02001264
Kyle Farris0c147b32011-08-26 02:29:31 -04001265 $this->_reset_write();
1266 return $this->query($sql);
1267 }
1268
Kyle Farris0c147b32011-08-26 02:29:31 -04001269 // --------------------------------------------------------------------
1270
1271 /**
1272 * Validate Insert
1273 *
1274 * This method is used by both insert() and get_compiled_insert() to
1275 * validate that the there data is actually being set and that table
1276 * has been chosen to be inserted into.
1277 *
1278 * @access public
1279 * @param string the table to insert data into
1280 * @return string
1281 */
1282 protected function _validate_insert($table = '')
1283 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 if (count($this->ar_set) == 0)
1285 {
1286 if ($this->db_debug)
1287 {
1288 return $this->display_error('db_must_use_set');
1289 }
1290 return FALSE;
1291 }
1292
1293 if ($table == '')
1294 {
1295 if ( ! isset($this->ar_from[0]))
1296 {
1297 if ($this->db_debug)
1298 {
1299 return $this->display_error('db_must_set_table');
1300 }
1301 return FALSE;
1302 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001303 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001304 else
1305 {
1306 $this->ar_from[0] = $table;
1307 }
1308
1309 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 }
Barry Mienydd671972010-10-04 16:33:58 +02001311
Phil Sturgeon9789f322011-07-15 15:14:05 -06001312 // --------------------------------------------------------------------
1313
1314 /**
1315 * Replace
1316 *
1317 * Compiles an replace into string and runs the query
1318 *
1319 * @param string the table to replace data into
1320 * @param array an associative array of insert values
1321 * @return object
1322 */
1323 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001324 {
1325 if ( ! is_null($set))
1326 {
1327 $this->set($set);
1328 }
Barry Mienydd671972010-10-04 16:33:58 +02001329
Derek Jonesd10e8962010-03-02 17:10:36 -06001330 if (count($this->ar_set) == 0)
1331 {
1332 if ($this->db_debug)
1333 {
1334 return $this->display_error('db_must_use_set');
1335 }
1336 return FALSE;
1337 }
1338
1339 if ($table == '')
1340 {
1341 if ( ! isset($this->ar_from[0]))
1342 {
1343 if ($this->db_debug)
1344 {
1345 return $this->display_error('db_must_set_table');
1346 }
1347 return FALSE;
1348 }
Barry Mienydd671972010-10-04 16:33:58 +02001349
Derek Jonesd10e8962010-03-02 17:10:36 -06001350 $table = $this->ar_from[0];
1351 }
1352
1353 $sql = $this->_replace($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
Barry Mienydd671972010-10-04 16:33:58 +02001354
Derek Jonesd10e8962010-03-02 17:10:36 -06001355 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001356 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001357 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001358
Kyle Farris0c147b32011-08-26 02:29:31 -04001359 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001360
Kyle Farris0c147b32011-08-26 02:29:31 -04001361 /**
1362 * Get UPDATE query string
1363 *
1364 * Compiles an update query and returns the sql
1365 *
1366 * @access public
1367 * @param string the table to update
1368 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1369 * @return string
1370 */
1371 public function get_compiled_update($table = '', $reset = TRUE)
1372 {
1373 // Combine any cached components with the current statements
1374 $this->_merge_cache();
1375
1376 if ($this->_validate_update($table) === FALSE)
1377 {
1378 return FALSE;
1379 }
1380
1381 $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);
1382
1383 if ($reset === TRUE)
1384 {
1385 $this->_reset_write();
1386 }
1387
1388 return $sql;
1389 }
1390
Derek Allard2067d1a2008-11-13 22:59:24 +00001391 // --------------------------------------------------------------------
1392
1393 /**
1394 * Update
1395 *
1396 * Compiles an update string and runs the query
1397 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001398 * @param string the table to retrieve the results from
1399 * @param array an associative array of update values
1400 * @param mixed the where clause
1401 * @return object
1402 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001403 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001404 {
1405 // Combine any cached components with the current statements
1406 $this->_merge_cache();
1407
1408 if ( ! is_null($set))
1409 {
1410 $this->set($set);
1411 }
Barry Mienydd671972010-10-04 16:33:58 +02001412
Kyle Farris0c147b32011-08-26 02:29:31 -04001413 if ($this->_validate_update($table) === FALSE)
1414 {
1415 return FALSE;
1416 }
1417
1418 if ($where != NULL)
1419 {
1420 $this->where($where);
1421 }
1422
1423 if ($limit != NULL)
1424 {
1425 $this->limit($limit);
1426 }
1427
1428 $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);
1429
1430 $this->_reset_write();
1431 return $this->query($sql);
1432 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001433
1434 // --------------------------------------------------------------------
1435
1436 /**
1437 * Validate Update
1438 *
1439 * This method is used by both update() and get_compiled_update() to
1440 * validate that data is actually being set and that a table has been
1441 * chosen to be update.
1442 *
1443 * @access public
1444 * @param string the table to update data on
1445 * @return string
1446 */
1447 protected function _validate_update($table = '')
1448 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001449 if (count($this->ar_set) == 0)
1450 {
1451 if ($this->db_debug)
1452 {
1453 return $this->display_error('db_must_use_set');
1454 }
1455 return FALSE;
1456 }
1457
1458 if ($table == '')
1459 {
1460 if ( ! isset($this->ar_from[0]))
1461 {
1462 if ($this->db_debug)
1463 {
1464 return $this->display_error('db_must_set_table');
1465 }
1466 return FALSE;
1467 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001468 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001469 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001470 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001471 $this->ar_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001472 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001473 }
Kyle Farris76116012011-08-31 11:17:48 -04001474
Derek Jonesd10e8962010-03-02 17:10:36 -06001475 // --------------------------------------------------------------------
1476
1477 /**
1478 * Update_Batch
1479 *
1480 * Compiles an update string and runs the query
1481 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001482 * @param string the table to retrieve the results from
1483 * @param array an associative array of update values
1484 * @param string the where key
1485 * @return object
1486 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001487 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001488 {
1489 // Combine any cached components with the current statements
1490 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001491
Derek Jonesd10e8962010-03-02 17:10:36 -06001492 if (is_null($index))
1493 {
1494 if ($this->db_debug)
1495 {
Phil Sturgeonfcb8c9e2011-08-10 08:28:39 -06001496 return $this->display_error('db_must_use_index');
Derek Jonesd10e8962010-03-02 17:10:36 -06001497 }
1498
1499 return FALSE;
1500 }
1501
1502 if ( ! is_null($set))
1503 {
1504 $this->set_update_batch($set, $index);
1505 }
1506
1507 if (count($this->ar_set) == 0)
1508 {
1509 if ($this->db_debug)
1510 {
1511 return $this->display_error('db_must_use_set');
1512 }
1513
1514 return FALSE;
1515 }
1516
1517 if ($table == '')
1518 {
1519 if ( ! isset($this->ar_from[0]))
1520 {
1521 if ($this->db_debug)
1522 {
1523 return $this->display_error('db_must_set_table');
1524 }
1525 return FALSE;
1526 }
Barry Mienydd671972010-10-04 16:33:58 +02001527
Derek Jonesd10e8962010-03-02 17:10:36 -06001528 $table = $this->ar_from[0];
1529 }
Barry Mienydd671972010-10-04 16:33:58 +02001530
Derek Jonesd10e8962010-03-02 17:10:36 -06001531 // Batch this baby
1532 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1533 {
1534 $sql = $this->_update_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->ar_set, $i, 100), $this->_protect_identifiers($index), $this->ar_where);
1535
1536 $this->query($sql);
1537 }
Barry Mienydd671972010-10-04 16:33:58 +02001538
Derek Jonesd10e8962010-03-02 17:10:36 -06001539 $this->_reset_write();
1540 }
1541
1542 // --------------------------------------------------------------------
1543
1544 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001545 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001546 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001547 * @param array
1548 * @param string
1549 * @param boolean
1550 * @return object
1551 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001552 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001553 {
1554 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001555
Derek Jonesd10e8962010-03-02 17:10:36 -06001556 if ( ! is_array($key))
1557 {
1558 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001559 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001560
1561 foreach ($key as $k => $v)
1562 {
1563 $index_set = FALSE;
1564 $clean = array();
1565
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001566 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001567 {
1568 if ($k2 == $index)
1569 {
1570 $index_set = TRUE;
1571 }
1572 else
1573 {
1574 $not[] = $k.'-'.$v;
1575 }
1576
1577 if ($escape === FALSE)
1578 {
1579 $clean[$this->_protect_identifiers($k2)] = $v2;
1580 }
1581 else
1582 {
Barry Mienydd671972010-10-04 16:33:58 +02001583 $clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001584 }
1585 }
1586
1587 if ($index_set == FALSE)
1588 {
1589 return $this->display_error('db_batch_missing_index');
1590 }
1591
1592 $this->ar_set[] = $clean;
1593 }
Barry Mienydd671972010-10-04 16:33:58 +02001594
Derek Jonesd10e8962010-03-02 17:10:36 -06001595 return $this;
1596 }
1597
Derek Allard2067d1a2008-11-13 22:59:24 +00001598 // --------------------------------------------------------------------
1599
1600 /**
1601 * Empty Table
1602 *
1603 * Compiles a delete string and runs "DELETE FROM table"
1604 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001605 * @param string the table to empty
1606 * @return object
1607 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001608 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001609 {
1610 if ($table == '')
1611 {
1612 if ( ! isset($this->ar_from[0]))
1613 {
1614 if ($this->db_debug)
1615 {
1616 return $this->display_error('db_must_set_table');
1617 }
1618 return FALSE;
1619 }
1620
1621 $table = $this->ar_from[0];
1622 }
1623 else
1624 {
1625 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1626 }
1627
1628 $sql = $this->_delete($table);
1629
1630 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001631
Derek Allard2067d1a2008-11-13 22:59:24 +00001632 return $this->query($sql);
1633 }
1634
1635 // --------------------------------------------------------------------
1636
1637 /**
1638 * Truncate
1639 *
1640 * Compiles a truncate string and runs the query
1641 * If the database does not support the truncate() command
1642 * This function maps to "DELETE FROM table"
1643 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001644 * @param string the table to truncate
1645 * @return object
1646 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001647 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001648 {
1649 if ($table == '')
1650 {
1651 if ( ! isset($this->ar_from[0]))
1652 {
1653 if ($this->db_debug)
1654 {
1655 return $this->display_error('db_must_set_table');
1656 }
1657 return FALSE;
1658 }
1659
1660 $table = $this->ar_from[0];
1661 }
1662 else
1663 {
1664 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1665 }
1666
1667 $sql = $this->_truncate($table);
1668
1669 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001670
Derek Allard2067d1a2008-11-13 22:59:24 +00001671 return $this->query($sql);
1672 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001673
1674 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001675
Kyle Farris0c147b32011-08-26 02:29:31 -04001676 /**
1677 * Get DELETE query string
1678 *
1679 * Compiles a delete query string and returns the sql
1680 *
1681 * @access public
1682 * @param string the table to delete from
1683 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1684 * @return string
1685 */
1686 public function get_compiled_delete($table = '', $reset = TRUE)
1687 {
1688 $this->return_delete_sql = TRUE;
1689 $sql = $this->delete($table, '', NULL, $reset);
1690 $this->return_delete_sql = FALSE;
1691 return $sql;
1692 }
1693
Derek Allard2067d1a2008-11-13 22:59:24 +00001694 // --------------------------------------------------------------------
1695
1696 /**
1697 * Delete
1698 *
1699 * Compiles a delete string and runs the query
1700 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001701 * @param mixed the table(s) to delete from. String or array
1702 * @param mixed the where clause
1703 * @param mixed the limit clause
1704 * @param boolean
1705 * @return object
1706 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001707 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001708 {
1709 // Combine any cached components with the current statements
1710 $this->_merge_cache();
1711
1712 if ($table == '')
1713 {
1714 if ( ! isset($this->ar_from[0]))
1715 {
1716 if ($this->db_debug)
1717 {
1718 return $this->display_error('db_must_set_table');
1719 }
1720 return FALSE;
1721 }
1722
1723 $table = $this->ar_from[0];
1724 }
1725 elseif (is_array($table))
1726 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001727 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001728 {
1729 $this->delete($single_table, $where, $limit, FALSE);
1730 }
1731
1732 $this->_reset_write();
1733 return;
1734 }
1735 else
1736 {
1737 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1738 }
1739
1740 if ($where != '')
1741 {
1742 $this->where($where);
1743 }
1744
1745 if ($limit != NULL)
1746 {
1747 $this->limit($limit);
1748 }
1749
Derek Allard03d783b2009-05-03 19:45:45 +00001750 if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001751 {
1752 if ($this->db_debug)
1753 {
1754 return $this->display_error('db_del_must_use_where');
1755 }
1756
1757 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001758 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001759
1760 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
1761
1762 if ($reset_data)
1763 {
1764 $this->_reset_write();
1765 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001766
1767 if ($this->return_delete_sql === true)
1768 {
1769 return $sql;
1770 }
Barry Mienydd671972010-10-04 16:33:58 +02001771
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 return $this->query($sql);
1773 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001774
Derek Allard2067d1a2008-11-13 22:59:24 +00001775 // --------------------------------------------------------------------
1776
1777 /**
1778 * DB Prefix
1779 *
1780 * Prepends a database prefix if one exists in configuration
1781 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001782 * @param string the table
1783 * @return string
1784 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001785 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001786 {
1787 if ($table == '')
1788 {
1789 $this->display_error('db_table_name_required');
1790 }
1791
1792 return $this->dbprefix.$table;
1793 }
1794
1795 // --------------------------------------------------------------------
1796
1797 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001798 * Set DB Prefix
1799 *
1800 * Set's the DB Prefix to something new without needing to reconnect
1801 *
1802 * @param string the prefix
1803 * @return string
1804 */
1805 public function set_dbprefix($prefix = '')
1806 {
1807 return $this->dbprefix = $prefix;
1808 }
1809
1810 // --------------------------------------------------------------------
1811
1812 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001813 * Track Aliases
1814 *
1815 * Used to track SQL statements written with aliased tables.
1816 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001817 * @param string The table to inspect
1818 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001819 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001820 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001821 {
1822 if (is_array($table))
1823 {
1824 foreach ($table as $t)
1825 {
1826 $this->_track_aliases($t);
1827 }
1828 return;
1829 }
Barry Mienydd671972010-10-04 16:33:58 +02001830
Derek Jones37f4b9c2011-07-01 17:56:50 -05001831 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001832 // the string into discreet statements
1833 if (strpos($table, ',') !== FALSE)
1834 {
1835 return $this->_track_aliases(explode(',', $table));
1836 }
Barry Mienydd671972010-10-04 16:33:58 +02001837
Derek Allard2067d1a2008-11-13 22:59:24 +00001838 // if a table alias is used we can recognize it by a space
1839 if (strpos($table, " ") !== FALSE)
1840 {
1841 // if the alias is written with the AS keyword, remove it
1842 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001843
Derek Allard2067d1a2008-11-13 22:59:24 +00001844 // Grab the alias
1845 $table = trim(strrchr($table, " "));
Barry Mienydd671972010-10-04 16:33:58 +02001846
Derek Allard2067d1a2008-11-13 22:59:24 +00001847 // Store the alias, if it doesn't already exist
1848 if ( ! in_array($table, $this->ar_aliased_tables))
1849 {
1850 $this->ar_aliased_tables[] = $table;
1851 }
1852 }
1853 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001854
Derek Allard2067d1a2008-11-13 22:59:24 +00001855 // --------------------------------------------------------------------
1856
1857 /**
1858 * Compile the SELECT statement
1859 *
1860 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001861 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001862 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001863 * @return string
1864 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001865 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001866 {
1867 // Combine any cached components with the current statements
1868 $this->_merge_cache();
1869
1870 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001871
Derek Allard2067d1a2008-11-13 22:59:24 +00001872 // Write the "select" portion of the query
1873
1874 if ($select_override !== FALSE)
1875 {
1876 $sql = $select_override;
1877 }
1878 else
1879 {
1880 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001881
Derek Allard2067d1a2008-11-13 22:59:24 +00001882 if (count($this->ar_select) == 0)
1883 {
Barry Mienydd671972010-10-04 16:33:58 +02001884 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001885 }
1886 else
Barry Mienydd671972010-10-04 16:33:58 +02001887 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001888 // Cycle through the "select" portion of the query and prep each column name.
1889 // The reason we protect identifiers here rather then in the select() function
1890 // is because until the user calls the from() function we don't know if there are aliases
1891 foreach ($this->ar_select as $key => $val)
1892 {
Phil Sturgeon77cc0282011-08-09 16:03:49 -06001893 $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL;
1894 $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001895 }
Barry Mienydd671972010-10-04 16:33:58 +02001896
Derek Allard2067d1a2008-11-13 22:59:24 +00001897 $sql .= implode(', ', $this->ar_select);
1898 }
1899 }
1900
1901 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001902
Derek Allard2067d1a2008-11-13 22:59:24 +00001903 // Write the "FROM" portion of the query
1904
1905 if (count($this->ar_from) > 0)
1906 {
1907 $sql .= "\nFROM ";
1908
1909 $sql .= $this->_from_tables($this->ar_from);
1910 }
1911
1912 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001913
Derek Allard2067d1a2008-11-13 22:59:24 +00001914 // Write the "JOIN" portion of the query
1915
1916 if (count($this->ar_join) > 0)
1917 {
1918 $sql .= "\n";
1919
1920 $sql .= implode("\n", $this->ar_join);
1921 }
1922
1923 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001924
Derek Allard2067d1a2008-11-13 22:59:24 +00001925 // Write the "WHERE" portion of the query
1926
1927 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1928 {
Greg Akere156c6e2011-04-20 16:03:04 -05001929 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00001930 }
1931
1932 $sql .= implode("\n", $this->ar_where);
1933
1934 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001935
Derek Allard2067d1a2008-11-13 22:59:24 +00001936 // Write the "LIKE" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001937
Derek Allard2067d1a2008-11-13 22:59:24 +00001938 if (count($this->ar_like) > 0)
1939 {
1940 if (count($this->ar_where) > 0)
1941 {
1942 $sql .= "\nAND ";
1943 }
1944
1945 $sql .= implode("\n", $this->ar_like);
1946 }
1947
1948 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001949
Derek Allard2067d1a2008-11-13 22:59:24 +00001950 // Write the "GROUP BY" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001951
Derek Allard2067d1a2008-11-13 22:59:24 +00001952 if (count($this->ar_groupby) > 0)
1953 {
1954 $sql .= "\nGROUP BY ";
Barry Mienydd671972010-10-04 16:33:58 +02001955
Derek Allard2067d1a2008-11-13 22:59:24 +00001956 $sql .= implode(', ', $this->ar_groupby);
1957 }
1958
1959 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001960
Derek Allard2067d1a2008-11-13 22:59:24 +00001961 // Write the "HAVING" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001962
Derek Allard2067d1a2008-11-13 22:59:24 +00001963 if (count($this->ar_having) > 0)
1964 {
1965 $sql .= "\nHAVING ";
1966 $sql .= implode("\n", $this->ar_having);
1967 }
1968
1969 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001970
Derek Allard2067d1a2008-11-13 22:59:24 +00001971 // Write the "ORDER BY" portion of the query
1972
1973 if (count($this->ar_orderby) > 0)
1974 {
1975 $sql .= "\nORDER BY ";
1976 $sql .= implode(', ', $this->ar_orderby);
Barry Mienydd671972010-10-04 16:33:58 +02001977
Derek Allard2067d1a2008-11-13 22:59:24 +00001978 if ($this->ar_order !== FALSE)
1979 {
1980 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02001981 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001982 }
1983
1984 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001985
Derek Allard2067d1a2008-11-13 22:59:24 +00001986 // Write the "LIMIT" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001987
Derek Allard2067d1a2008-11-13 22:59:24 +00001988 if (is_numeric($this->ar_limit))
1989 {
1990 $sql .= "\n";
1991 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1992 }
1993
1994 return $sql;
1995 }
1996
1997 // --------------------------------------------------------------------
1998
1999 /**
2000 * Object to Array
2001 *
2002 * Takes an object as input and converts the class variables to array key/vals
2003 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002004 * @param object
2005 * @return array
2006 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002007 public function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002008 {
2009 if ( ! is_object($object))
2010 {
2011 return $object;
2012 }
Barry Mienydd671972010-10-04 16:33:58 +02002013
Derek Allard2067d1a2008-11-13 22:59:24 +00002014 $array = array();
2015 foreach (get_object_vars($object) as $key => $val)
2016 {
2017 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06002018 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002019 {
2020 $array[$key] = $val;
2021 }
2022 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002023
2024 return $array;
2025 }
Barry Mienydd671972010-10-04 16:33:58 +02002026
Derek Jonesd10e8962010-03-02 17:10:36 -06002027 // --------------------------------------------------------------------
2028
2029 /**
2030 * Object to Array
2031 *
2032 * Takes an object as input and converts the class variables to array key/vals
2033 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002034 * @param object
2035 * @return array
2036 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002037 public function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002038 {
2039 if ( ! is_object($object))
2040 {
2041 return $object;
2042 }
Barry Mienydd671972010-10-04 16:33:58 +02002043
Derek Jonesd10e8962010-03-02 17:10:36 -06002044 $array = array();
2045 $out = get_object_vars($object);
2046 $fields = array_keys($out);
2047
2048 foreach ($fields as $val)
2049 {
2050 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06002051 if ($val != '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002052 {
2053
2054 $i = 0;
2055 foreach ($out[$val] as $data)
2056 {
2057 $array[$i][$val] = $data;
2058 $i++;
2059 }
2060 }
2061 }
2062
Derek Allard2067d1a2008-11-13 22:59:24 +00002063 return $array;
2064 }
Barry Mienydd671972010-10-04 16:33:58 +02002065
Derek Allard2067d1a2008-11-13 22:59:24 +00002066 // --------------------------------------------------------------------
2067
2068 /**
2069 * Start Cache
2070 *
2071 * Starts AR caching
2072 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002073 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002074 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002075 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002076 {
2077 $this->ar_caching = TRUE;
2078 }
2079
2080 // --------------------------------------------------------------------
2081
2082 /**
2083 * Stop Cache
2084 *
2085 * Stops AR caching
2086 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002087 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002088 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002089 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002090 {
2091 $this->ar_caching = FALSE;
2092 }
2093
2094 // --------------------------------------------------------------------
2095
2096 /**
2097 * Flush Cache
2098 *
2099 * Empties the AR cache
2100 *
2101 * @access public
2102 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002103 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002104 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002105 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002106 $this->_reset_run(array(
2107 'ar_cache_select' => array(),
2108 'ar_cache_from' => array(),
2109 'ar_cache_join' => array(),
2110 'ar_cache_where' => array(),
2111 'ar_cache_like' => array(),
2112 'ar_cache_groupby' => array(),
2113 'ar_cache_having' => array(),
2114 'ar_cache_orderby' => array(),
2115 'ar_cache_set' => array(),
2116 'ar_cache_exists' => array(),
2117 'ar_cache_no_escape' => array()
2118 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002119 }
2120
2121 // --------------------------------------------------------------------
2122
2123 /**
2124 * Merge Cache
2125 *
Barry Mienydd671972010-10-04 16:33:58 +02002126 * When called, this function merges any cached AR arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002127 * locally called ones.
2128 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002129 * @return void
2130 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002131 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002132 {
2133 if (count($this->ar_cache_exists) == 0)
2134 {
2135 return;
2136 }
2137
2138 foreach ($this->ar_cache_exists as $val)
2139 {
2140 $ar_variable = 'ar_'.$val;
2141 $ar_cache_var = 'ar_cache_'.$val;
2142
2143 if (count($this->$ar_cache_var) == 0)
2144 {
2145 continue;
2146 }
2147
2148 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
2149 }
2150
2151 // If we are "protecting identifiers" we need to examine the "from"
2152 // portion of the query to determine if there are any aliases
2153 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
2154 {
2155 $this->_track_aliases($this->ar_from);
2156 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002157
2158 $this->ar_no_escape = $this->ar_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002159 }
Kyle Farris0c147b32011-08-26 02:29:31 -04002160
2161 // --------------------------------------------------------------------
2162
2163 /**
2164 * Reset Active Record values.
2165 *
2166 * Publicly-visible method to reset the AR values.
2167 *
2168 * @access public
2169 * @return void
2170 */
2171 public function reset_query()
2172 {
2173 $this->_reset_select();
2174 $this->_reset_write();
2175 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002176
2177 // --------------------------------------------------------------------
2178
2179 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002180 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002181 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002182 * @param array An array of fields to reset
2183 * @return void
2184 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002185 protected function _reset_run($ar_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002186 {
2187 foreach ($ar_reset_items as $item => $default_value)
2188 {
2189 if ( ! in_array($item, $this->ar_store_array))
2190 {
2191 $this->$item = $default_value;
2192 }
2193 }
2194 }
2195
2196 // --------------------------------------------------------------------
2197
2198 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002199 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002200 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002201 * @return void
2202 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002203 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002204 {
2205 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06002206 'ar_select' => array(),
2207 'ar_from' => array(),
2208 'ar_join' => array(),
2209 'ar_where' => array(),
2210 'ar_like' => array(),
2211 'ar_groupby' => array(),
2212 'ar_having' => array(),
2213 'ar_orderby' => array(),
2214 'ar_wherein' => array(),
2215 'ar_aliased_tables' => array(),
2216 'ar_no_escape' => array(),
2217 'ar_distinct' => FALSE,
2218 'ar_limit' => FALSE,
2219 'ar_offset' => FALSE,
2220 'ar_order' => FALSE,
2221 );
Barry Mienydd671972010-10-04 16:33:58 +02002222
Derek Allard2067d1a2008-11-13 22:59:24 +00002223 $this->_reset_run($ar_reset_items);
2224 }
Barry Mienydd671972010-10-04 16:33:58 +02002225
Derek Allard2067d1a2008-11-13 22:59:24 +00002226 // --------------------------------------------------------------------
2227
2228 /**
2229 * Resets the active record "write" values.
2230 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002231 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002232 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002233 * @return void
2234 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002235 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002236 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002237 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06002238 'ar_set' => array(),
2239 'ar_from' => array(),
2240 'ar_where' => array(),
2241 'ar_like' => array(),
2242 'ar_orderby' => array(),
2243 'ar_keys' => array(),
2244 'ar_limit' => FALSE,
2245 'ar_order' => FALSE
2246 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002247
2248 $this->_reset_run($ar_reset_items);
2249 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002250}
2251
2252/* End of file DB_active_rec.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00002253/* Location: ./system/database/DB_active_rec.php */