blob: 1f77e41d7df99145be88dba1abbb0afd3cff6300 [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();
Greg Aker03abee32011-12-25 00:31:29 -060078 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
pporlan2c685fb2011-12-22 12:15:25 +0100834 * @param bool enable field name escaping
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 * @return object
836 */
pporlan2c685fb2011-12-22 12:15:25 +0100837 public function order_by($orderby, $direction = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 {
839 if (strtolower($direction) == 'random')
840 {
841 $orderby = ''; // Random results want or don't need a field name
842 $direction = $this->_random_keyword;
843 }
844 elseif (trim($direction) != '')
845 {
846 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
847 }
Barry Mienydd671972010-10-04 16:33:58 +0200848
849
pporlan2c685fb2011-12-22 12:15:25 +0100850 if ((strpos($orderby, ',') !== FALSE) && ($escape === TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 {
852 $temp = array();
853 foreach (explode(',', $orderby) as $part)
854 {
855 $part = trim($part);
856 if ( ! in_array($part, $this->ar_aliased_tables))
857 {
858 $part = $this->_protect_identifiers(trim($part));
859 }
Barry Mienydd671972010-10-04 16:33:58 +0200860
Derek Allard2067d1a2008-11-13 22:59:24 +0000861 $temp[] = $part;
862 }
Barry Mienydd671972010-10-04 16:33:58 +0200863
864 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 }
Derek Allarde37ab382009-02-03 16:13:57 +0000866 else if ($direction != $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 {
pporlan2c685fb2011-12-22 12:15:25 +0100868 if ($escape === TRUE)
869 {
870 $orderby = $this->_protect_identifiers($orderby);
871 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 }
Barry Mienydd671972010-10-04 16:33:58 +0200873
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200875
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 $this->ar_orderby[] = $orderby_statement;
877 if ($this->ar_caching === TRUE)
878 {
879 $this->ar_cache_orderby[] = $orderby_statement;
880 $this->ar_cache_exists[] = 'orderby';
881 }
882
883 return $this;
884 }
Barry Mienydd671972010-10-04 16:33:58 +0200885
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 // --------------------------------------------------------------------
887
888 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 * Sets the LIMIT value
890 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 * @param integer the limit value
892 * @param integer the offset value
893 * @return object
894 */
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200895 public function limit($value, $offset = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 {
Phil Sturgeonb0eae5f2011-08-10 09:00:52 -0600897 $this->ar_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000898
Phil Sturgeonbff3dfd2011-09-07 18:54:25 +0200899 if ( ! is_null($offset))
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 {
Phil Sturgeonb0eae5f2011-08-10 09:00:52 -0600901 $this->ar_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 }
Barry Mienydd671972010-10-04 16:33:58 +0200903
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 return $this;
905 }
Barry Mienydd671972010-10-04 16:33:58 +0200906
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 // --------------------------------------------------------------------
908
909 /**
910 * Sets the OFFSET value
911 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 * @param integer the offset value
913 * @return object
914 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600915 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 {
kenjis0e857632011-09-02 08:41:17 +0900917 $this->ar_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 return $this;
919 }
Barry Mienydd671972010-10-04 16:33:58 +0200920
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 // --------------------------------------------------------------------
922
923 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500924 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 * @param mixed
927 * @param string
928 * @param boolean
929 * @return object
930 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600931 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 {
933 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +0200934
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 if ( ! is_array($key))
936 {
937 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +0200938 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000939
940 foreach ($key as $k => $v)
941 {
942 if ($escape === FALSE)
943 {
944 $this->ar_set[$this->_protect_identifiers($k)] = $v;
945 }
946 else
947 {
Phil Sturgeond0ac1a22011-02-15 22:54:08 +0000948 $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 }
950 }
Barry Mienydd671972010-10-04 16:33:58 +0200951
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 return $this;
953 }
Kyle Farris0c147b32011-08-26 02:29:31 -0400954
Kyle Farris0c147b32011-08-26 02:29:31 -0400955 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200956
Kyle Farris0c147b32011-08-26 02:29:31 -0400957 /**
958 * Get SELECT query string
959 *
960 * Compiles a SELECT query string and returns the sql.
961 *
962 * @access public
963 * @param string the table name to select from (optional)
964 * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone
965 * @return string
966 */
967 public function get_compiled_select($table = '', $reset = TRUE)
968 {
969 if ($table != '')
kylefarris0a3176b2011-08-26 02:37:52 -0400970 {
Kyle Farris0c147b32011-08-26 02:29:31 -0400971 $this->_track_aliases($table);
972 $this->from($table);
973 }
974
975 $select = $this->_compile_select();
976
977 if ($reset === TRUE)
978 {
979 $this->_reset_select();
980 }
981
982 return $select;
983 }
984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 // --------------------------------------------------------------------
986
987 /**
988 * Get
989 *
990 * Compiles the select statement based on the other functions called
991 * and runs the query
992 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 * @param string the table
994 * @param string the limit clause
995 * @param string the offset clause
996 * @return object
997 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600998 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 {
1000 if ($table != '')
1001 {
1002 $this->_track_aliases($table);
1003 $this->from($table);
1004 }
Barry Mienydd671972010-10-04 16:33:58 +02001005
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 if ( ! is_null($limit))
1007 {
1008 $this->limit($limit, $offset);
1009 }
Barry Mienydd671972010-10-04 16:33:58 +02001010
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 $sql = $this->_compile_select();
1012
1013 $result = $this->query($sql);
1014 $this->_reset_select();
1015 return $result;
1016 }
1017
1018 /**
1019 * "Count All Results" query
1020 *
Barry Mienydd671972010-10-04 16:33:58 +02001021 * Generates a platform-specific query string that counts all records
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 * returned by an Active Record query.
1023 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 * @param string
1025 * @return string
1026 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001027 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 {
1029 if ($table != '')
1030 {
1031 $this->_track_aliases($table);
1032 $this->from($table);
1033 }
Barry Mienydd671972010-10-04 16:33:58 +02001034
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
1036
1037 $query = $this->query($sql);
1038 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +02001039
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 if ($query->num_rows() == 0)
1041 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001042 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +00001043 }
1044
1045 $row = $query->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +00001046 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 }
1048
1049 // --------------------------------------------------------------------
1050
1051 /**
1052 * Get_Where
1053 *
1054 * Allows the where clause, limit and offset to be added directly
1055 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 * @param string the where clause
1057 * @param string the limit clause
1058 * @param string the offset clause
1059 * @return object
1060 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001061 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 {
1063 if ($table != '')
1064 {
1065 $this->from($table);
1066 }
1067
1068 if ( ! is_null($where))
1069 {
1070 $this->where($where);
1071 }
Barry Mienydd671972010-10-04 16:33:58 +02001072
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 if ( ! is_null($limit))
1074 {
1075 $this->limit($limit, $offset);
1076 }
Barry Mienydd671972010-10-04 16:33:58 +02001077
Derek Allard2067d1a2008-11-13 22:59:24 +00001078 $sql = $this->_compile_select();
1079
1080 $result = $this->query($sql);
1081 $this->_reset_select();
1082 return $result;
1083 }
1084
1085 // --------------------------------------------------------------------
1086
1087 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001088 * Insert_Batch
1089 *
1090 * Compiles batch insert strings and runs the queries
1091 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001092 * @param string the table to retrieve the results from
1093 * @param array an associative array of insert values
1094 * @return object
1095 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001096 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001097 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001098 if ( ! is_null($set))
1099 {
1100 $this->set_insert_batch($set);
1101 }
Barry Mienydd671972010-10-04 16:33:58 +02001102
Derek Jonesd10e8962010-03-02 17:10:36 -06001103 if (count($this->ar_set) == 0)
1104 {
1105 if ($this->db_debug)
1106 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001107 //No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001108 return $this->display_error('db_must_use_set');
1109 }
1110 return FALSE;
1111 }
1112
1113 if ($table == '')
1114 {
1115 if ( ! isset($this->ar_from[0]))
1116 {
1117 if ($this->db_debug)
1118 {
1119 return $this->display_error('db_must_set_table');
1120 }
1121 return FALSE;
1122 }
Barry Mienydd671972010-10-04 16:33:58 +02001123
Derek Jonesd10e8962010-03-02 17:10:36 -06001124 $table = $this->ar_from[0];
1125 }
1126
1127 // Batch this baby
1128 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1129 {
Barry Mienydd671972010-10-04 16:33:58 +02001130
Derek Jonesd10e8962010-03-02 17:10:36 -06001131 $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
1132
1133 //echo $sql;
1134
1135 $this->query($sql);
1136 }
Barry Mienydd671972010-10-04 16:33:58 +02001137
Derek Jonesd10e8962010-03-02 17:10:36 -06001138 $this->_reset_write();
1139
1140
Barry Mienydd671972010-10-04 16:33:58 +02001141 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001142 }
1143
1144 // --------------------------------------------------------------------
1145
1146 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001147 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001148 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001149 * @param mixed
1150 * @param string
1151 * @param boolean
1152 * @return object
1153 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001154 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001155 {
1156 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001157
Derek Jonesd10e8962010-03-02 17:10:36 -06001158 if ( ! is_array($key))
1159 {
1160 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001161 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001162
1163 $keys = array_keys(current($key));
1164 sort($keys);
1165
1166 foreach ($key as $row)
1167 {
Barry Mienydd671972010-10-04 16:33:58 +02001168 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1169 {
1170 // batch function above returns an error on an empty array
1171 $this->ar_set[] = array();
1172 return;
1173 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001174
Barry Mienydd671972010-10-04 16:33:58 +02001175 ksort($row); // puts $row in the same order as our keys
1176
Derek Jonesd10e8962010-03-02 17:10:36 -06001177 if ($escape === FALSE)
1178 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001179 $this->ar_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001180 }
1181 else
1182 {
1183 $clean = array();
1184
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001185 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001186 {
Barry Mienydd671972010-10-04 16:33:58 +02001187 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001188 }
1189
Derek Jones37f4b9c2011-07-01 17:56:50 -05001190 $this->ar_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001191 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001192 }
1193
1194 foreach ($keys as $k)
1195 {
1196 $this->ar_keys[] = $this->_protect_identifiers($k);
1197 }
Barry Mienydd671972010-10-04 16:33:58 +02001198
Derek Jonesd10e8962010-03-02 17:10:36 -06001199 return $this;
1200 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001201
1202 // --------------------------------------------------------------------
1203
1204 /**
1205 * Get INSERT query string
1206 *
1207 * Compiles an insert query and returns the sql
1208 *
1209 * @access public
1210 * @param string the table to insert into
1211 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1212 * @return string
1213 */
1214 public function get_compiled_insert($table = '', $reset = TRUE)
1215 {
1216 if ($this->_validate_insert($table) === FALSE)
1217 {
1218 return FALSE;
1219 }
1220
Kyle Farris76116012011-08-31 11:17:48 -04001221 $sql = $this->_insert(
1222 $this->_protect_identifiers(
1223 $this->ar_from[0], TRUE, NULL, FALSE
1224 ),
1225 array_keys($this->ar_set),
1226 array_values($this->ar_set)
1227 );
Kyle Farris0c147b32011-08-26 02:29:31 -04001228
1229 if ($reset === TRUE)
1230 {
1231 $this->_reset_write();
1232 }
1233
1234 return $sql;
1235 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001236
Derek Allard2067d1a2008-11-13 22:59:24 +00001237 // --------------------------------------------------------------------
1238
1239 /**
1240 * Insert
1241 *
1242 * Compiles an insert string and runs the query
1243 *
Kyle Farris0c147b32011-08-26 02:29:31 -04001244 * @access public
Phil Sturgeon9789f322011-07-15 15:14:05 -06001245 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 * @param array an associative array of insert values
1247 * @return object
1248 */
Kyle Farris0c147b32011-08-26 02:29:31 -04001249 public function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001250 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 if ( ! is_null($set))
1252 {
1253 $this->set($set);
1254 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001255
1256 if ($this->_validate_insert($table) === FALSE)
1257 {
1258 return FALSE;
1259 }
1260
Kyle Farris76116012011-08-31 11:17:48 -04001261 $sql = $this->_insert(
1262 $this->_protect_identifiers(
1263 $this->ar_from[0], TRUE, NULL, FALSE
1264 ),
1265 array_keys($this->ar_set),
1266 array_values($this->ar_set)
1267 );
Barry Mienydd671972010-10-04 16:33:58 +02001268
Kyle Farris0c147b32011-08-26 02:29:31 -04001269 $this->_reset_write();
1270 return $this->query($sql);
1271 }
1272
Kyle Farris0c147b32011-08-26 02:29:31 -04001273 // --------------------------------------------------------------------
1274
1275 /**
1276 * Validate Insert
1277 *
1278 * This method is used by both insert() and get_compiled_insert() to
1279 * validate that the there data is actually being set and that table
1280 * has been chosen to be inserted into.
1281 *
1282 * @access public
1283 * @param string the table to insert data into
1284 * @return string
1285 */
1286 protected function _validate_insert($table = '')
1287 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001288 if (count($this->ar_set) == 0)
1289 {
1290 if ($this->db_debug)
1291 {
1292 return $this->display_error('db_must_use_set');
1293 }
1294 return FALSE;
1295 }
1296
1297 if ($table == '')
1298 {
1299 if ( ! isset($this->ar_from[0]))
1300 {
1301 if ($this->db_debug)
1302 {
1303 return $this->display_error('db_must_set_table');
1304 }
1305 return FALSE;
1306 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001307 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001308 else
1309 {
1310 $this->ar_from[0] = $table;
1311 }
1312
1313 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 }
Barry Mienydd671972010-10-04 16:33:58 +02001315
Phil Sturgeon9789f322011-07-15 15:14:05 -06001316 // --------------------------------------------------------------------
1317
1318 /**
1319 * Replace
1320 *
1321 * Compiles an replace into string and runs the query
1322 *
1323 * @param string the table to replace data into
1324 * @param array an associative array of insert values
1325 * @return object
1326 */
1327 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001328 {
1329 if ( ! is_null($set))
1330 {
1331 $this->set($set);
1332 }
Barry Mienydd671972010-10-04 16:33:58 +02001333
Derek Jonesd10e8962010-03-02 17:10:36 -06001334 if (count($this->ar_set) == 0)
1335 {
1336 if ($this->db_debug)
1337 {
1338 return $this->display_error('db_must_use_set');
1339 }
1340 return FALSE;
1341 }
1342
1343 if ($table == '')
1344 {
1345 if ( ! isset($this->ar_from[0]))
1346 {
1347 if ($this->db_debug)
1348 {
1349 return $this->display_error('db_must_set_table');
1350 }
1351 return FALSE;
1352 }
Barry Mienydd671972010-10-04 16:33:58 +02001353
Derek Jonesd10e8962010-03-02 17:10:36 -06001354 $table = $this->ar_from[0];
1355 }
1356
1357 $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 +02001358
Derek Jonesd10e8962010-03-02 17:10:36 -06001359 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001360 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001361 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001362
Kyle Farris0c147b32011-08-26 02:29:31 -04001363 // --------------------------------------------------------------------
Derek Jonesd10e8962010-03-02 17:10:36 -06001364
Kyle Farris0c147b32011-08-26 02:29:31 -04001365 /**
1366 * Get UPDATE query string
1367 *
1368 * Compiles an update query and returns the sql
1369 *
1370 * @access public
1371 * @param string the table to update
1372 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1373 * @return string
1374 */
1375 public function get_compiled_update($table = '', $reset = TRUE)
1376 {
1377 // Combine any cached components with the current statements
1378 $this->_merge_cache();
1379
1380 if ($this->_validate_update($table) === FALSE)
1381 {
1382 return FALSE;
1383 }
1384
1385 $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);
1386
1387 if ($reset === TRUE)
1388 {
1389 $this->_reset_write();
1390 }
1391
1392 return $sql;
1393 }
1394
Derek Allard2067d1a2008-11-13 22:59:24 +00001395 // --------------------------------------------------------------------
1396
1397 /**
1398 * Update
1399 *
1400 * Compiles an update string and runs the query
1401 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001402 * @param string the table to retrieve the results from
1403 * @param array an associative array of update values
1404 * @param mixed the where clause
1405 * @return object
1406 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001407 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001408 {
1409 // Combine any cached components with the current statements
1410 $this->_merge_cache();
1411
1412 if ( ! is_null($set))
1413 {
1414 $this->set($set);
1415 }
Barry Mienydd671972010-10-04 16:33:58 +02001416
Kyle Farris0c147b32011-08-26 02:29:31 -04001417 if ($this->_validate_update($table) === FALSE)
1418 {
1419 return FALSE;
1420 }
1421
1422 if ($where != NULL)
1423 {
1424 $this->where($where);
1425 }
1426
1427 if ($limit != NULL)
1428 {
1429 $this->limit($limit);
1430 }
1431
Mancy0d91fd22011-12-20 13:13:14 +03001432 $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 -04001433
1434 $this->_reset_write();
1435 return $this->query($sql);
1436 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001437
1438 // --------------------------------------------------------------------
1439
1440 /**
1441 * Validate Update
1442 *
1443 * This method is used by both update() and get_compiled_update() to
1444 * validate that data is actually being set and that a table has been
1445 * chosen to be update.
1446 *
1447 * @access public
1448 * @param string the table to update data on
1449 * @return string
1450 */
1451 protected function _validate_update($table = '')
1452 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001453 if (count($this->ar_set) == 0)
1454 {
1455 if ($this->db_debug)
1456 {
1457 return $this->display_error('db_must_use_set');
1458 }
1459 return FALSE;
1460 }
1461
1462 if ($table == '')
1463 {
1464 if ( ! isset($this->ar_from[0]))
1465 {
1466 if ($this->db_debug)
1467 {
1468 return $this->display_error('db_must_set_table');
1469 }
1470 return FALSE;
1471 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001472 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001473 else
Derek Allard2067d1a2008-11-13 22:59:24 +00001474 {
Kyle Farris0c147b32011-08-26 02:29:31 -04001475 $this->ar_from[0] = $table;
Derek Allard2067d1a2008-11-13 22:59:24 +00001476 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001477 }
Kyle Farris76116012011-08-31 11:17:48 -04001478
Derek Jonesd10e8962010-03-02 17:10:36 -06001479 // --------------------------------------------------------------------
1480
1481 /**
1482 * Update_Batch
1483 *
1484 * Compiles an update string and runs the query
1485 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001486 * @param string the table to retrieve the results from
1487 * @param array an associative array of update values
1488 * @param string the where key
1489 * @return object
1490 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001491 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001492 {
1493 // Combine any cached components with the current statements
1494 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001495
Derek Jonesd10e8962010-03-02 17:10:36 -06001496 if (is_null($index))
1497 {
1498 if ($this->db_debug)
1499 {
Phil Sturgeonfcb8c9e2011-08-10 08:28:39 -06001500 return $this->display_error('db_must_use_index');
Derek Jonesd10e8962010-03-02 17:10:36 -06001501 }
1502
1503 return FALSE;
1504 }
1505
1506 if ( ! is_null($set))
1507 {
1508 $this->set_update_batch($set, $index);
1509 }
1510
1511 if (count($this->ar_set) == 0)
1512 {
1513 if ($this->db_debug)
1514 {
1515 return $this->display_error('db_must_use_set');
1516 }
1517
1518 return FALSE;
1519 }
1520
1521 if ($table == '')
1522 {
1523 if ( ! isset($this->ar_from[0]))
1524 {
1525 if ($this->db_debug)
1526 {
1527 return $this->display_error('db_must_set_table');
1528 }
1529 return FALSE;
1530 }
Barry Mienydd671972010-10-04 16:33:58 +02001531
Derek Jonesd10e8962010-03-02 17:10:36 -06001532 $table = $this->ar_from[0];
1533 }
Barry Mienydd671972010-10-04 16:33:58 +02001534
Derek Jonesd10e8962010-03-02 17:10:36 -06001535 // Batch this baby
1536 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1537 {
1538 $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);
1539
1540 $this->query($sql);
1541 }
Barry Mienydd671972010-10-04 16:33:58 +02001542
Derek Jonesd10e8962010-03-02 17:10:36 -06001543 $this->_reset_write();
1544 }
1545
1546 // --------------------------------------------------------------------
1547
1548 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001549 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001550 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001551 * @param array
1552 * @param string
1553 * @param boolean
1554 * @return object
1555 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001556 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001557 {
1558 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001559
Derek Jonesd10e8962010-03-02 17:10:36 -06001560 if ( ! is_array($key))
1561 {
1562 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001563 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001564
1565 foreach ($key as $k => $v)
1566 {
1567 $index_set = FALSE;
1568 $clean = array();
1569
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001570 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001571 {
1572 if ($k2 == $index)
1573 {
1574 $index_set = TRUE;
1575 }
1576 else
1577 {
1578 $not[] = $k.'-'.$v;
1579 }
1580
1581 if ($escape === FALSE)
1582 {
1583 $clean[$this->_protect_identifiers($k2)] = $v2;
1584 }
1585 else
1586 {
Barry Mienydd671972010-10-04 16:33:58 +02001587 $clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001588 }
1589 }
1590
1591 if ($index_set == FALSE)
1592 {
1593 return $this->display_error('db_batch_missing_index');
1594 }
1595
1596 $this->ar_set[] = $clean;
1597 }
Barry Mienydd671972010-10-04 16:33:58 +02001598
Derek Jonesd10e8962010-03-02 17:10:36 -06001599 return $this;
1600 }
1601
Derek Allard2067d1a2008-11-13 22:59:24 +00001602 // --------------------------------------------------------------------
1603
1604 /**
1605 * Empty Table
1606 *
1607 * Compiles a delete string and runs "DELETE FROM table"
1608 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001609 * @param string the table to empty
1610 * @return object
1611 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001612 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001613 {
1614 if ($table == '')
1615 {
1616 if ( ! isset($this->ar_from[0]))
1617 {
1618 if ($this->db_debug)
1619 {
1620 return $this->display_error('db_must_set_table');
1621 }
1622 return FALSE;
1623 }
1624
1625 $table = $this->ar_from[0];
1626 }
1627 else
1628 {
1629 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1630 }
1631
1632 $sql = $this->_delete($table);
1633
1634 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001635
Derek Allard2067d1a2008-11-13 22:59:24 +00001636 return $this->query($sql);
1637 }
1638
1639 // --------------------------------------------------------------------
1640
1641 /**
1642 * Truncate
1643 *
1644 * Compiles a truncate string and runs the query
1645 * If the database does not support the truncate() command
1646 * This function maps to "DELETE FROM table"
1647 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001648 * @param string the table to truncate
1649 * @return object
1650 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001651 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001652 {
1653 if ($table == '')
1654 {
1655 if ( ! isset($this->ar_from[0]))
1656 {
1657 if ($this->db_debug)
1658 {
1659 return $this->display_error('db_must_set_table');
1660 }
1661 return FALSE;
1662 }
1663
1664 $table = $this->ar_from[0];
1665 }
1666 else
1667 {
1668 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1669 }
1670
1671 $sql = $this->_truncate($table);
1672
1673 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001674
Derek Allard2067d1a2008-11-13 22:59:24 +00001675 return $this->query($sql);
1676 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001677
1678 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001679
Kyle Farris0c147b32011-08-26 02:29:31 -04001680 /**
1681 * Get DELETE query string
1682 *
1683 * Compiles a delete query string and returns the sql
1684 *
1685 * @access public
1686 * @param string the table to delete from
1687 * @param boolean TRUE: reset AR values; FALSE: leave AR values alone
1688 * @return string
1689 */
1690 public function get_compiled_delete($table = '', $reset = TRUE)
1691 {
1692 $this->return_delete_sql = TRUE;
1693 $sql = $this->delete($table, '', NULL, $reset);
1694 $this->return_delete_sql = FALSE;
1695 return $sql;
1696 }
1697
Derek Allard2067d1a2008-11-13 22:59:24 +00001698 // --------------------------------------------------------------------
1699
1700 /**
1701 * Delete
1702 *
1703 * Compiles a delete string and runs the query
1704 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001705 * @param mixed the table(s) to delete from. String or array
1706 * @param mixed the where clause
1707 * @param mixed the limit clause
1708 * @param boolean
1709 * @return object
1710 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001711 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001712 {
1713 // Combine any cached components with the current statements
1714 $this->_merge_cache();
1715
1716 if ($table == '')
1717 {
1718 if ( ! isset($this->ar_from[0]))
1719 {
1720 if ($this->db_debug)
1721 {
1722 return $this->display_error('db_must_set_table');
1723 }
1724 return FALSE;
1725 }
1726
1727 $table = $this->ar_from[0];
1728 }
1729 elseif (is_array($table))
1730 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001731 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001732 {
1733 $this->delete($single_table, $where, $limit, FALSE);
1734 }
1735
1736 $this->_reset_write();
1737 return;
1738 }
1739 else
1740 {
1741 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1742 }
1743
1744 if ($where != '')
1745 {
1746 $this->where($where);
1747 }
1748
1749 if ($limit != NULL)
1750 {
1751 $this->limit($limit);
1752 }
1753
Derek Allard03d783b2009-05-03 19:45:45 +00001754 if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001755 {
1756 if ($this->db_debug)
1757 {
1758 return $this->display_error('db_del_must_use_where');
1759 }
1760
1761 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001762 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001763
1764 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
1765
1766 if ($reset_data)
1767 {
1768 $this->_reset_write();
1769 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001770
1771 if ($this->return_delete_sql === true)
1772 {
1773 return $sql;
1774 }
Barry Mienydd671972010-10-04 16:33:58 +02001775
Derek Allard2067d1a2008-11-13 22:59:24 +00001776 return $this->query($sql);
1777 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001778
Derek Allard2067d1a2008-11-13 22:59:24 +00001779 // --------------------------------------------------------------------
1780
1781 /**
1782 * DB Prefix
1783 *
1784 * Prepends a database prefix if one exists in configuration
1785 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001786 * @param string the table
1787 * @return string
1788 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001789 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001790 {
1791 if ($table == '')
1792 {
1793 $this->display_error('db_table_name_required');
1794 }
1795
1796 return $this->dbprefix.$table;
1797 }
1798
1799 // --------------------------------------------------------------------
1800
1801 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001802 * Set DB Prefix
1803 *
1804 * Set's the DB Prefix to something new without needing to reconnect
1805 *
1806 * @param string the prefix
1807 * @return string
1808 */
1809 public function set_dbprefix($prefix = '')
1810 {
1811 return $this->dbprefix = $prefix;
1812 }
1813
1814 // --------------------------------------------------------------------
1815
1816 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001817 * Track Aliases
1818 *
1819 * Used to track SQL statements written with aliased tables.
1820 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001821 * @param string The table to inspect
1822 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001823 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001824 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001825 {
1826 if (is_array($table))
1827 {
1828 foreach ($table as $t)
1829 {
1830 $this->_track_aliases($t);
1831 }
1832 return;
1833 }
Barry Mienydd671972010-10-04 16:33:58 +02001834
Derek Jones37f4b9c2011-07-01 17:56:50 -05001835 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001836 // the string into discreet statements
1837 if (strpos($table, ',') !== FALSE)
1838 {
1839 return $this->_track_aliases(explode(',', $table));
1840 }
Barry Mienydd671972010-10-04 16:33:58 +02001841
Derek Allard2067d1a2008-11-13 22:59:24 +00001842 // if a table alias is used we can recognize it by a space
1843 if (strpos($table, " ") !== FALSE)
1844 {
1845 // if the alias is written with the AS keyword, remove it
1846 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001847
Derek Allard2067d1a2008-11-13 22:59:24 +00001848 // Grab the alias
1849 $table = trim(strrchr($table, " "));
Barry Mienydd671972010-10-04 16:33:58 +02001850
Derek Allard2067d1a2008-11-13 22:59:24 +00001851 // Store the alias, if it doesn't already exist
1852 if ( ! in_array($table, $this->ar_aliased_tables))
1853 {
1854 $this->ar_aliased_tables[] = $table;
1855 }
1856 }
1857 }
Kyle Farris0c147b32011-08-26 02:29:31 -04001858
Derek Allard2067d1a2008-11-13 22:59:24 +00001859 // --------------------------------------------------------------------
1860
1861 /**
1862 * Compile the SELECT statement
1863 *
1864 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001865 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001866 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001867 * @return string
1868 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001869 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001870 {
1871 // Combine any cached components with the current statements
1872 $this->_merge_cache();
1873
1874 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001875
Derek Allard2067d1a2008-11-13 22:59:24 +00001876 // Write the "select" portion of the query
1877
1878 if ($select_override !== FALSE)
1879 {
1880 $sql = $select_override;
1881 }
1882 else
1883 {
1884 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001885
Derek Allard2067d1a2008-11-13 22:59:24 +00001886 if (count($this->ar_select) == 0)
1887 {
Barry Mienydd671972010-10-04 16:33:58 +02001888 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001889 }
1890 else
Barry Mienydd671972010-10-04 16:33:58 +02001891 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001892 // Cycle through the "select" portion of the query and prep each column name.
1893 // The reason we protect identifiers here rather then in the select() function
1894 // is because until the user calls the from() function we don't know if there are aliases
1895 foreach ($this->ar_select as $key => $val)
1896 {
Phil Sturgeon77cc0282011-08-09 16:03:49 -06001897 $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL;
1898 $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001899 }
Barry Mienydd671972010-10-04 16:33:58 +02001900
Derek Allard2067d1a2008-11-13 22:59:24 +00001901 $sql .= implode(', ', $this->ar_select);
1902 }
1903 }
1904
1905 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001906
Derek Allard2067d1a2008-11-13 22:59:24 +00001907 // Write the "FROM" portion of the query
1908
1909 if (count($this->ar_from) > 0)
1910 {
1911 $sql .= "\nFROM ";
1912
1913 $sql .= $this->_from_tables($this->ar_from);
1914 }
1915
1916 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001917
Derek Allard2067d1a2008-11-13 22:59:24 +00001918 // Write the "JOIN" portion of the query
1919
1920 if (count($this->ar_join) > 0)
1921 {
1922 $sql .= "\n";
1923
1924 $sql .= implode("\n", $this->ar_join);
1925 }
1926
1927 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001928
Derek Allard2067d1a2008-11-13 22:59:24 +00001929 // Write the "WHERE" portion of the query
1930
1931 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1932 {
Greg Akere156c6e2011-04-20 16:03:04 -05001933 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00001934 }
1935
1936 $sql .= implode("\n", $this->ar_where);
1937
1938 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001939
Derek Allard2067d1a2008-11-13 22:59:24 +00001940 // Write the "LIKE" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001941
Derek Allard2067d1a2008-11-13 22:59:24 +00001942 if (count($this->ar_like) > 0)
1943 {
1944 if (count($this->ar_where) > 0)
1945 {
1946 $sql .= "\nAND ";
1947 }
1948
1949 $sql .= implode("\n", $this->ar_like);
1950 }
1951
1952 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001953
Derek Allard2067d1a2008-11-13 22:59:24 +00001954 // Write the "GROUP BY" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001955
Derek Allard2067d1a2008-11-13 22:59:24 +00001956 if (count($this->ar_groupby) > 0)
1957 {
1958 $sql .= "\nGROUP BY ";
Barry Mienydd671972010-10-04 16:33:58 +02001959
Derek Allard2067d1a2008-11-13 22:59:24 +00001960 $sql .= implode(', ', $this->ar_groupby);
1961 }
1962
1963 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001964
Derek Allard2067d1a2008-11-13 22:59:24 +00001965 // Write the "HAVING" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001966
Derek Allard2067d1a2008-11-13 22:59:24 +00001967 if (count($this->ar_having) > 0)
1968 {
1969 $sql .= "\nHAVING ";
1970 $sql .= implode("\n", $this->ar_having);
1971 }
1972
1973 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001974
Derek Allard2067d1a2008-11-13 22:59:24 +00001975 // Write the "ORDER BY" portion of the query
1976
1977 if (count($this->ar_orderby) > 0)
1978 {
1979 $sql .= "\nORDER BY ";
1980 $sql .= implode(', ', $this->ar_orderby);
Barry Mienydd671972010-10-04 16:33:58 +02001981
Derek Allard2067d1a2008-11-13 22:59:24 +00001982 if ($this->ar_order !== FALSE)
1983 {
1984 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02001985 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001986 }
1987
1988 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001989
Derek Allard2067d1a2008-11-13 22:59:24 +00001990 // Write the "LIMIT" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001991
Derek Allard2067d1a2008-11-13 22:59:24 +00001992 if (is_numeric($this->ar_limit))
1993 {
1994 $sql .= "\n";
1995 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1996 }
1997
1998 return $sql;
1999 }
2000
2001 // --------------------------------------------------------------------
2002
2003 /**
2004 * Object to Array
2005 *
2006 * Takes an object as input and converts the class variables to array key/vals
2007 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002008 * @param object
2009 * @return array
2010 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002011 public function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00002012 {
2013 if ( ! is_object($object))
2014 {
2015 return $object;
2016 }
Barry Mienydd671972010-10-04 16:33:58 +02002017
Derek Allard2067d1a2008-11-13 22:59:24 +00002018 $array = array();
2019 foreach (get_object_vars($object) as $key => $val)
2020 {
2021 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06002022 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00002023 {
2024 $array[$key] = $val;
2025 }
2026 }
Derek Jonesd10e8962010-03-02 17:10:36 -06002027
2028 return $array;
2029 }
Barry Mienydd671972010-10-04 16:33:58 +02002030
Derek Jonesd10e8962010-03-02 17:10:36 -06002031 // --------------------------------------------------------------------
2032
2033 /**
2034 * Object to Array
2035 *
2036 * Takes an object as input and converts the class variables to array key/vals
2037 *
Derek Jonesd10e8962010-03-02 17:10:36 -06002038 * @param object
2039 * @return array
2040 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002041 public function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06002042 {
2043 if ( ! is_object($object))
2044 {
2045 return $object;
2046 }
Barry Mienydd671972010-10-04 16:33:58 +02002047
Derek Jonesd10e8962010-03-02 17:10:36 -06002048 $array = array();
2049 $out = get_object_vars($object);
2050 $fields = array_keys($out);
2051
2052 foreach ($fields as $val)
2053 {
2054 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06002055 if ($val != '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06002056 {
2057
2058 $i = 0;
2059 foreach ($out[$val] as $data)
2060 {
2061 $array[$i][$val] = $data;
2062 $i++;
2063 }
2064 }
2065 }
2066
Derek Allard2067d1a2008-11-13 22:59:24 +00002067 return $array;
2068 }
Barry Mienydd671972010-10-04 16:33:58 +02002069
Derek Allard2067d1a2008-11-13 22:59:24 +00002070 // --------------------------------------------------------------------
2071
2072 /**
2073 * Start Cache
2074 *
2075 * Starts AR caching
2076 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002077 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002078 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002079 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002080 {
2081 $this->ar_caching = TRUE;
2082 }
2083
2084 // --------------------------------------------------------------------
2085
2086 /**
2087 * Stop Cache
2088 *
2089 * Stops AR caching
2090 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002091 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002092 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002093 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002094 {
2095 $this->ar_caching = FALSE;
2096 }
2097
2098 // --------------------------------------------------------------------
2099
2100 /**
2101 * Flush Cache
2102 *
2103 * Empties the AR cache
2104 *
2105 * @access public
2106 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02002107 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002108 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02002109 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06002110 $this->_reset_run(array(
2111 'ar_cache_select' => array(),
2112 'ar_cache_from' => array(),
2113 'ar_cache_join' => array(),
2114 'ar_cache_where' => array(),
2115 'ar_cache_like' => array(),
2116 'ar_cache_groupby' => array(),
2117 'ar_cache_having' => array(),
2118 'ar_cache_orderby' => array(),
2119 'ar_cache_set' => array(),
2120 'ar_cache_exists' => array(),
2121 'ar_cache_no_escape' => array()
2122 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00002123 }
2124
2125 // --------------------------------------------------------------------
2126
2127 /**
2128 * Merge Cache
2129 *
Barry Mienydd671972010-10-04 16:33:58 +02002130 * When called, this function merges any cached AR arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00002131 * locally called ones.
2132 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002133 * @return void
2134 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002135 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00002136 {
2137 if (count($this->ar_cache_exists) == 0)
2138 {
2139 return;
2140 }
2141
2142 foreach ($this->ar_cache_exists as $val)
2143 {
2144 $ar_variable = 'ar_'.$val;
2145 $ar_cache_var = 'ar_cache_'.$val;
2146
2147 if (count($this->$ar_cache_var) == 0)
2148 {
2149 continue;
2150 }
2151
2152 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
2153 }
2154
2155 // If we are "protecting identifiers" we need to examine the "from"
2156 // portion of the query to determine if there are any aliases
2157 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
2158 {
2159 $this->_track_aliases($this->ar_from);
2160 }
Greg Aker2e1837a2011-05-06 12:17:04 -05002161
2162 $this->ar_no_escape = $this->ar_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00002163 }
Kyle Farris0c147b32011-08-26 02:29:31 -04002164
2165 // --------------------------------------------------------------------
2166
2167 /**
2168 * Reset Active Record values.
2169 *
2170 * Publicly-visible method to reset the AR values.
2171 *
2172 * @access public
2173 * @return void
2174 */
2175 public function reset_query()
2176 {
2177 $this->_reset_select();
2178 $this->_reset_write();
2179 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002180
2181 // --------------------------------------------------------------------
2182
2183 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002184 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002185 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002186 * @param array An array of fields to reset
2187 * @return void
2188 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002189 protected function _reset_run($ar_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00002190 {
2191 foreach ($ar_reset_items as $item => $default_value)
2192 {
2193 if ( ! in_array($item, $this->ar_store_array))
2194 {
2195 $this->$item = $default_value;
2196 }
2197 }
2198 }
2199
2200 // --------------------------------------------------------------------
2201
2202 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05002203 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00002204 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002205 * @return void
2206 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002207 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00002208 {
2209 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06002210 'ar_select' => array(),
2211 'ar_from' => array(),
2212 'ar_join' => array(),
2213 'ar_where' => array(),
2214 'ar_like' => array(),
2215 'ar_groupby' => array(),
2216 'ar_having' => array(),
2217 'ar_orderby' => array(),
2218 'ar_wherein' => array(),
2219 'ar_aliased_tables' => array(),
2220 'ar_no_escape' => array(),
2221 'ar_distinct' => FALSE,
2222 'ar_limit' => FALSE,
2223 'ar_offset' => FALSE,
2224 'ar_order' => FALSE,
2225 );
Barry Mienydd671972010-10-04 16:33:58 +02002226
Derek Allard2067d1a2008-11-13 22:59:24 +00002227 $this->_reset_run($ar_reset_items);
2228 }
Barry Mienydd671972010-10-04 16:33:58 +02002229
Derek Allard2067d1a2008-11-13 22:59:24 +00002230 // --------------------------------------------------------------------
2231
2232 /**
2233 * Resets the active record "write" values.
2234 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002235 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002236 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002237 * @return void
2238 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002239 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002240 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002241 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06002242 'ar_set' => array(),
2243 'ar_from' => array(),
2244 'ar_where' => array(),
2245 'ar_like' => array(),
2246 'ar_orderby' => array(),
2247 'ar_keys' => array(),
2248 'ar_limit' => FALSE,
2249 'ar_order' => FALSE
2250 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002251
2252 $this->_reset_run($ar_reset_items);
2253 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002254}
2255
2256/* End of file DB_active_rec.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00002257/* Location: ./system/database/DB_active_rec.php */