blob: 7ddf20d07fe039bbd50e8d3b3d44c4ab5bcd681d [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 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Active Record Class
20 *
21 * This is the platform-independent base Active Record implementation class.
22 *
23 * @package CodeIgniter
24 * @subpackage Drivers
25 * @category Database
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/database/
28 */
29class CI_DB_active_record extends CI_DB_driver {
30
31 var $ar_select = array();
32 var $ar_distinct = FALSE;
33 var $ar_from = array();
34 var $ar_join = array();
35 var $ar_where = array();
36 var $ar_like = array();
37 var $ar_groupby = array();
38 var $ar_having = array();
Robin Sowell43753fd2010-09-16 12:52:07 -040039 var $ar_keys = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000040 var $ar_limit = FALSE;
41 var $ar_offset = FALSE;
42 var $ar_order = FALSE;
43 var $ar_orderby = array();
Barry Mienydd671972010-10-04 16:33:58 +020044 var $ar_set = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000045 var $ar_wherein = array();
46 var $ar_aliased_tables = array();
47 var $ar_store_array = array();
Barry Mienydd671972010-10-04 16:33:58 +020048
Derek Allard2067d1a2008-11-13 22:59:24 +000049 // Active Record Caching variables
Barry Mienydd671972010-10-04 16:33:58 +020050 var $ar_caching = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000051 var $ar_cache_exists = array();
52 var $ar_cache_select = array();
53 var $ar_cache_from = array();
54 var $ar_cache_join = array();
55 var $ar_cache_where = array();
56 var $ar_cache_like = array();
57 var $ar_cache_groupby = array();
58 var $ar_cache_having = array();
59 var $ar_cache_orderby = array();
Barry Mienydd671972010-10-04 16:33:58 +020060 var $ar_cache_set = array();
Derek Jones37f4b9c2011-07-01 17:56:50 -050061
Greg Akere156c6e2011-04-20 16:03:04 -050062 var $ar_no_escape = array();
Greg Aker2e1837a2011-05-06 12:17:04 -050063 var $ar_cache_no_escape = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000064
65 // --------------------------------------------------------------------
66
67 /**
68 * Select
69 *
70 * Generates the SELECT portion of the query
71 *
Derek Allard2067d1a2008-11-13 22:59:24 +000072 * @param string
73 * @return object
74 */
Phil Sturgeon9789f322011-07-15 15:14:05 -060075 public function select($select = '*', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000076 {
Derek Allard2067d1a2008-11-13 22:59:24 +000077 if (is_string($select))
78 {
79 $select = explode(',', $select);
80 }
81
82 foreach ($select as $val)
83 {
84 $val = trim($val);
85
86 if ($val != '')
87 {
88 $this->ar_select[] = $val;
Greg Akere156c6e2011-04-20 16:03:04 -050089 $this->ar_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +000090
91 if ($this->ar_caching === TRUE)
92 {
93 $this->ar_cache_select[] = $val;
94 $this->ar_cache_exists[] = 'select';
Greg Aker2e1837a2011-05-06 12:17:04 -050095 $this->ar_cache_no_escape[] = $escape;
Derek Allard2067d1a2008-11-13 22:59:24 +000096 }
97 }
98 }
99 return $this;
100 }
101
102 // --------------------------------------------------------------------
103
104 /**
105 * Select Max
106 *
107 * Generates a SELECT MAX(field) portion of a query
108 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 * @param string the field
110 * @param string an alias
111 * @return object
112 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600113 public function select_max($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 {
115 return $this->_max_min_avg_sum($select, $alias, 'MAX');
116 }
Barry Mienydd671972010-10-04 16:33:58 +0200117
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 // --------------------------------------------------------------------
119
120 /**
121 * Select Min
122 *
123 * Generates a SELECT MIN(field) portion of a query
124 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 * @param string the field
126 * @param string an alias
127 * @return object
128 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600129 public function select_min($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
131 return $this->_max_min_avg_sum($select, $alias, 'MIN');
132 }
133
134 // --------------------------------------------------------------------
135
136 /**
137 * Select Average
138 *
139 * Generates a SELECT AVG(field) portion of a query
140 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 * @param string the field
142 * @param string an alias
143 * @return object
144 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600145 public function select_avg($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
147 return $this->_max_min_avg_sum($select, $alias, 'AVG');
148 }
149
150 // --------------------------------------------------------------------
151
152 /**
153 * Select Sum
154 *
155 * Generates a SELECT SUM(field) portion of a query
156 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 * @param string the field
158 * @param string an alias
159 * @return object
160 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600161 public function select_sum($select = '', $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
163 return $this->_max_min_avg_sum($select, $alias, 'SUM');
164 }
165
166 // --------------------------------------------------------------------
167
168 /**
169 * Processing Function for the four functions above:
170 *
171 * select_max()
172 * select_min()
173 * select_avg()
Derek Jones37f4b9c2011-07-01 17:56:50 -0500174 * select_sum()
Barry Mienydd671972010-10-04 16:33:58 +0200175 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 * @param string the field
177 * @param string an alias
178 * @return object
179 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600180 protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
182 if ( ! is_string($select) OR $select == '')
183 {
184 $this->display_error('db_invalid_query');
185 }
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 $type = strtoupper($type);
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
190 {
191 show_error('Invalid function type: '.$type);
192 }
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 if ($alias == '')
195 {
196 $alias = $this->_create_alias_from_table(trim($select));
197 }
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$alias;
200
201 $this->ar_select[] = $sql;
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 if ($this->ar_caching === TRUE)
204 {
205 $this->ar_cache_select[] = $sql;
206 $this->ar_cache_exists[] = 'select';
207 }
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 return $this;
210 }
211
212 // --------------------------------------------------------------------
213
214 /**
215 * Determines the alias name based on the table
216 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 * @param string
218 * @return string
219 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600220 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
222 if (strpos($item, '.') !== FALSE)
223 {
224 return end(explode('.', $item));
225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 return $item;
228 }
229
230 // --------------------------------------------------------------------
231
232 /**
233 * DISTINCT
234 *
235 * Sets a flag which tells the query string compiler to add DISTINCT
236 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 * @param bool
238 * @return object
239 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600240 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
242 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
243 return $this;
244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 // --------------------------------------------------------------------
247
248 /**
249 * From
250 *
251 * Generates the FROM portion of the query
252 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 * @param mixed can be a string or array
254 * @return object
255 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600256 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
258 foreach ((array)$from as $val)
259 {
260 if (strpos($val, ',') !== FALSE)
261 {
262 foreach (explode(',', $val) as $v)
263 {
264 $v = trim($v);
265 $this->_track_aliases($v);
266
267 $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 if ($this->ar_caching === TRUE)
270 {
271 $this->ar_cache_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
272 $this->ar_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200273 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 }
275
276 }
277 else
278 {
279 $val = trim($val);
280
Derek Jones37f4b9c2011-07-01 17:56:50 -0500281 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200282 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 if ($this->ar_caching === TRUE)
288 {
289 $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
290 $this->ar_cache_exists[] = 'from';
291 }
292 }
293 }
294
295 return $this;
296 }
297
298 // --------------------------------------------------------------------
299
300 /**
301 * Join
302 *
303 * Generates the JOIN portion of the query
304 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 * @param string
306 * @param string the join condition
307 * @param string the type of join
308 * @return object
309 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600310 public function join($table, $cond, $type = '')
Barry Mienydd671972010-10-04 16:33:58 +0200311 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 if ($type != '')
313 {
314 $type = strtoupper(trim($type));
315
316 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
317 {
318 $type = '';
319 }
320 else
321 {
322 $type .= ' ';
323 }
324 }
325
Derek Jones37f4b9c2011-07-01 17:56:50 -0500326 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200327 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 $this->_track_aliases($table);
329
330 // Strip apart the condition and protect the identifiers
331 if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
332 {
333 $match[1] = $this->_protect_identifiers($match[1]);
334 $match[3] = $this->_protect_identifiers($match[3]);
Barry Mienydd671972010-10-04 16:33:58 +0200335
336 $cond = $match[1].$match[2].$match[3];
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 // Assemble the JOIN statement
340 $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
341
342 $this->ar_join[] = $join;
343 if ($this->ar_caching === TRUE)
344 {
345 $this->ar_cache_join[] = $join;
346 $this->ar_cache_exists[] = 'join';
347 }
348
349 return $this;
350 }
351
352 // --------------------------------------------------------------------
353
354 /**
355 * Where
356 *
357 * Generates the WHERE portion of the query. Separates
358 * multiple calls with AND
359 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 * @param mixed
361 * @param mixed
362 * @return object
363 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600364 public function where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
366 return $this->_where($key, $value, 'AND ', $escape);
367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 // --------------------------------------------------------------------
370
371 /**
372 * OR Where
373 *
374 * Generates the WHERE portion of the query. Separates
375 * multiple calls with OR
376 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 * @param mixed
378 * @param mixed
379 * @return object
380 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600381 public function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
383 return $this->_where($key, $value, 'OR ', $escape);
384 }
385
386 // --------------------------------------------------------------------
387
388 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 * Where
390 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600391 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 * @param mixed
394 * @param mixed
395 * @param string
396 * @return object
397 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600398 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
400 if ( ! is_array($key))
401 {
402 $key = array($key => $value);
403 }
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 // If the escape value was not set will will base it on the global setting
406 if ( ! is_bool($escape))
407 {
408 $escape = $this->_protect_identifiers;
409 }
410
411 foreach ($key as $k => $v)
412 {
413 $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type;
414
415 if (is_null($v) && ! $this->_has_operator($k))
416 {
417 // value appears not to have been set, assign the test to IS NULL
418 $k .= ' IS NULL';
419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 if ( ! is_null($v))
422 {
423 if ($escape === TRUE)
424 {
425 $k = $this->_protect_identifiers($k, FALSE, $escape);
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 $v = ' '.$this->escape($v);
428 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 if ( ! $this->_has_operator($k))
431 {
Greg Akere156c6e2011-04-20 16:03:04 -0500432 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 }
434 }
435 else
436 {
Barry Mienydd671972010-10-04 16:33:58 +0200437 $k = $this->_protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 }
439
440 $this->ar_where[] = $prefix.$k.$v;
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 if ($this->ar_caching === TRUE)
443 {
444 $this->ar_cache_where[] = $prefix.$k.$v;
445 $this->ar_cache_exists[] = 'where';
446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 return $this;
451 }
452
453 // --------------------------------------------------------------------
454
455 /**
456 * Where_in
457 *
458 * Generates a WHERE field IN ('item', 'item') SQL query joined with
459 * AND if appropriate
460 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 * @param string The field to search
462 * @param array The values searched on
463 * @return object
464 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600465 public function where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 {
467 return $this->_where_in($key, $values);
468 }
Barry Mienydd671972010-10-04 16:33:58 +0200469
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 // --------------------------------------------------------------------
471
472 /**
473 * Where_in_or
474 *
475 * Generates a WHERE field IN ('item', 'item') SQL query joined with
476 * OR if appropriate
477 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 * @param string The field to search
479 * @param array The values searched on
480 * @return object
481 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600482 public function or_where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
484 return $this->_where_in($key, $values, FALSE, 'OR ');
485 }
486
487 // --------------------------------------------------------------------
488
489 /**
490 * Where_not_in
491 *
492 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
493 * with AND if appropriate
494 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 * @param string The field to search
496 * @param array The values searched on
497 * @return object
498 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600499 public function where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
501 return $this->_where_in($key, $values, TRUE);
502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 // --------------------------------------------------------------------
505
506 /**
507 * Where_not_in_or
508 *
509 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
510 * with OR if appropriate
511 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 * @param string The field to search
513 * @param array The values searched on
514 * @return object
515 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600516 public function or_where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
518 return $this->_where_in($key, $values, TRUE, 'OR ');
519 }
520
521 // --------------------------------------------------------------------
522
523 /**
524 * Where_in
525 *
526 * Called by where_in, where_in_or, where_not_in, where_not_in_or
527 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 * @param string The field to search
529 * @param array The values searched on
530 * @param boolean If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200531 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 * @return object
533 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600534 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 {
536 if ($key === NULL OR $values === NULL)
537 {
538 return;
539 }
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 if ( ! is_array($values))
542 {
543 $values = array($values);
544 }
Barry Mienydd671972010-10-04 16:33:58 +0200545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 $not = ($not) ? ' NOT' : '';
547
548 foreach ($values as $value)
549 {
550 $this->ar_wherein[] = $this->escape($value);
551 }
552
553 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
556
557 $this->ar_where[] = $where_in;
558 if ($this->ar_caching === TRUE)
559 {
560 $this->ar_cache_where[] = $where_in;
561 $this->ar_cache_exists[] = 'where';
562 }
563
564 // reset the array for multiple calls
565 $this->ar_wherein = array();
566 return $this;
567 }
Barry Mienydd671972010-10-04 16:33:58 +0200568
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 // --------------------------------------------------------------------
570
571 /**
572 * Like
573 *
574 * Generates a %LIKE% portion of the query. Separates
575 * multiple calls with AND
576 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 * @param mixed
578 * @param mixed
579 * @return object
580 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600581 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 {
583 return $this->_like($field, $match, 'AND ', $side);
584 }
585
586 // --------------------------------------------------------------------
587
588 /**
589 * Not Like
590 *
591 * Generates a NOT LIKE portion of the query. Separates
592 * multiple calls with AND
593 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 * @param mixed
595 * @param mixed
596 * @return object
597 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600598 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 {
600 return $this->_like($field, $match, 'AND ', $side, 'NOT');
601 }
Barry Mienydd671972010-10-04 16:33:58 +0200602
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 // --------------------------------------------------------------------
604
605 /**
606 * OR Like
607 *
608 * Generates a %LIKE% portion of the query. Separates
609 * multiple calls with OR
610 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 * @param mixed
612 * @param mixed
613 * @return object
614 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600615 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 {
617 return $this->_like($field, $match, 'OR ', $side);
618 }
619
620 // --------------------------------------------------------------------
621
622 /**
623 * OR Not Like
624 *
625 * Generates a NOT LIKE portion of the query. Separates
626 * multiple calls with OR
627 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 * @param mixed
629 * @param mixed
630 * @return object
631 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600632 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 {
634 return $this->_like($field, $match, 'OR ', $side, 'NOT');
635 }
Barry Mienydd671972010-10-04 16:33:58 +0200636
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 // --------------------------------------------------------------------
638
639 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 * Like
641 *
642 * Called by like() or orlike()
643 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 * @param mixed
645 * @param mixed
646 * @param string
647 * @return object
648 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600649 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 {
651 if ( ! is_array($field))
652 {
653 $field = array($field => $match);
654 }
Barry Mienydd671972010-10-04 16:33:58 +0200655
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 foreach ($field as $k => $v)
657 {
658 $k = $this->_protect_identifiers($k);
659
660 $prefix = (count($this->ar_like) == 0) ? '' : $type;
661
Derek Jonese4ed5832009-02-20 21:44:59 +0000662 $v = $this->escape_like_str($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000663
664 if ($side == 'before')
665 {
666 $like_statement = $prefix." $k $not LIKE '%{$v}'";
667 }
668 elseif ($side == 'after')
669 {
670 $like_statement = $prefix." $k $not LIKE '{$v}%'";
671 }
672 else
673 {
674 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
675 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600676
Derek Jonese4ed5832009-02-20 21:44:59 +0000677 // some platforms require an escape sequence definition for LIKE wildcards
678 if ($this->_like_escape_str != '')
679 {
Greg Aker0d424892010-01-26 02:14:44 +0000680 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000681 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600682
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 $this->ar_like[] = $like_statement;
684 if ($this->ar_caching === TRUE)
685 {
686 $this->ar_cache_like[] = $like_statement;
687 $this->ar_cache_exists[] = 'like';
688 }
Barry Mienydd671972010-10-04 16:33:58 +0200689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 }
691 return $this;
692 }
Barry Mienydd671972010-10-04 16:33:58 +0200693
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 // --------------------------------------------------------------------
695
696 /**
697 * GROUP BY
698 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 * @param string
700 * @return object
701 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600702 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 {
704 if (is_string($by))
705 {
706 $by = explode(',', $by);
707 }
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 foreach ($by as $val)
710 {
711 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200712
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 if ($val != '')
714 {
715 $this->ar_groupby[] = $this->_protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200716
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 if ($this->ar_caching === TRUE)
718 {
719 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
720 $this->ar_cache_exists[] = 'groupby';
721 }
722 }
723 }
724 return $this;
725 }
726
727 // --------------------------------------------------------------------
728
729 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 * Sets the HAVING value
731 *
732 * Separates multiple calls with AND
733 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 * @param string
735 * @param string
736 * @return object
737 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600738 public function having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 {
740 return $this->_having($key, $value, 'AND ', $escape);
741 }
Barry Mienydd671972010-10-04 16:33:58 +0200742
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 // --------------------------------------------------------------------
744
745 /**
746 * Sets the OR HAVING value
747 *
748 * Separates multiple calls with OR
749 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 * @param string
751 * @param string
752 * @return object
753 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600754 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 {
756 return $this->_having($key, $value, 'OR ', $escape);
757 }
Barry Mienydd671972010-10-04 16:33:58 +0200758
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 // --------------------------------------------------------------------
760
761 /**
762 * Sets the HAVING values
763 *
764 * Called by having() or or_having()
765 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 * @param string
767 * @param string
768 * @return object
769 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600770 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 {
772 if ( ! is_array($key))
773 {
774 $key = array($key => $value);
775 }
Barry Mienydd671972010-10-04 16:33:58 +0200776
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 foreach ($key as $k => $v)
778 {
779 $prefix = (count($this->ar_having) == 0) ? '' : $type;
780
781 if ($escape === TRUE)
782 {
783 $k = $this->_protect_identifiers($k);
784 }
785
786 if ( ! $this->_has_operator($k))
787 {
788 $k .= ' = ';
789 }
790
791 if ($v != '')
792 {
793 $v = ' '.$this->escape_str($v);
794 }
Barry Mienydd671972010-10-04 16:33:58 +0200795
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 $this->ar_having[] = $prefix.$k.$v;
797 if ($this->ar_caching === TRUE)
798 {
799 $this->ar_cache_having[] = $prefix.$k.$v;
800 $this->ar_cache_exists[] = 'having';
801 }
802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 return $this;
805 }
Barry Mienydd671972010-10-04 16:33:58 +0200806
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 // --------------------------------------------------------------------
808
809 /**
810 * Sets the ORDER BY value
811 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 * @param string
813 * @param string direction: asc or desc
814 * @return object
815 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600816 public function order_by($orderby, $direction = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 {
818 if (strtolower($direction) == 'random')
819 {
820 $orderby = ''; // Random results want or don't need a field name
821 $direction = $this->_random_keyword;
822 }
823 elseif (trim($direction) != '')
824 {
825 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
826 }
Barry Mienydd671972010-10-04 16:33:58 +0200827
828
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 if (strpos($orderby, ',') !== FALSE)
830 {
831 $temp = array();
832 foreach (explode(',', $orderby) as $part)
833 {
834 $part = trim($part);
835 if ( ! in_array($part, $this->ar_aliased_tables))
836 {
837 $part = $this->_protect_identifiers(trim($part));
838 }
Barry Mienydd671972010-10-04 16:33:58 +0200839
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 $temp[] = $part;
841 }
Barry Mienydd671972010-10-04 16:33:58 +0200842
843 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 }
Derek Allarde37ab382009-02-03 16:13:57 +0000845 else if ($direction != $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 {
847 $orderby = $this->_protect_identifiers($orderby);
848 }
Barry Mienydd671972010-10-04 16:33:58 +0200849
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200851
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 $this->ar_orderby[] = $orderby_statement;
853 if ($this->ar_caching === TRUE)
854 {
855 $this->ar_cache_orderby[] = $orderby_statement;
856 $this->ar_cache_exists[] = 'orderby';
857 }
858
859 return $this;
860 }
Barry Mienydd671972010-10-04 16:33:58 +0200861
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 // --------------------------------------------------------------------
863
864 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 * Sets the LIMIT value
866 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 * @param integer the limit value
868 * @param integer the offset value
869 * @return object
870 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600871 public function limit($value, $offset = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 {
873 $this->ar_limit = $value;
874
875 if ($offset != '')
876 {
877 $this->ar_offset = $offset;
878 }
Barry Mienydd671972010-10-04 16:33:58 +0200879
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 return $this;
881 }
Barry Mienydd671972010-10-04 16:33:58 +0200882
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 // --------------------------------------------------------------------
884
885 /**
886 * Sets the OFFSET value
887 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 * @param integer the offset value
889 * @return object
890 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600891 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 {
893 $this->ar_offset = $offset;
894 return $this;
895 }
Barry Mienydd671972010-10-04 16:33:58 +0200896
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 // --------------------------------------------------------------------
898
899 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500900 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 * @param mixed
903 * @param string
904 * @param boolean
905 * @return object
906 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600907 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 {
909 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 if ( ! is_array($key))
912 {
913 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +0200914 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000915
916 foreach ($key as $k => $v)
917 {
918 if ($escape === FALSE)
919 {
920 $this->ar_set[$this->_protect_identifiers($k)] = $v;
921 }
922 else
923 {
Phil Sturgeond0ac1a22011-02-15 22:54:08 +0000924 $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 }
926 }
Barry Mienydd671972010-10-04 16:33:58 +0200927
Derek Allard2067d1a2008-11-13 22:59:24 +0000928 return $this;
929 }
Barry Mienydd671972010-10-04 16:33:58 +0200930
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 // --------------------------------------------------------------------
932
933 /**
934 * Get
935 *
936 * Compiles the select statement based on the other functions called
937 * and runs the query
938 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 * @param string the table
940 * @param string the limit clause
941 * @param string the offset clause
942 * @return object
943 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600944 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 {
946 if ($table != '')
947 {
948 $this->_track_aliases($table);
949 $this->from($table);
950 }
Barry Mienydd671972010-10-04 16:33:58 +0200951
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 if ( ! is_null($limit))
953 {
954 $this->limit($limit, $offset);
955 }
Barry Mienydd671972010-10-04 16:33:58 +0200956
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 $sql = $this->_compile_select();
958
959 $result = $this->query($sql);
960 $this->_reset_select();
961 return $result;
962 }
963
964 /**
965 * "Count All Results" query
966 *
Barry Mienydd671972010-10-04 16:33:58 +0200967 * Generates a platform-specific query string that counts all records
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 * returned by an Active Record query.
969 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 * @param string
971 * @return string
972 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600973 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 {
975 if ($table != '')
976 {
977 $this->_track_aliases($table);
978 $this->from($table);
979 }
Barry Mienydd671972010-10-04 16:33:58 +0200980
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
982
983 $query = $this->query($sql);
984 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +0200985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 if ($query->num_rows() == 0)
987 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +0000988 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 }
990
991 $row = $query->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +0000992 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 }
994
995 // --------------------------------------------------------------------
996
997 /**
998 * Get_Where
999 *
1000 * Allows the where clause, limit and offset to be added directly
1001 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 * @param string the where clause
1003 * @param string the limit clause
1004 * @param string the offset clause
1005 * @return object
1006 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001007 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 {
1009 if ($table != '')
1010 {
1011 $this->from($table);
1012 }
1013
1014 if ( ! is_null($where))
1015 {
1016 $this->where($where);
1017 }
Barry Mienydd671972010-10-04 16:33:58 +02001018
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 if ( ! is_null($limit))
1020 {
1021 $this->limit($limit, $offset);
1022 }
Barry Mienydd671972010-10-04 16:33:58 +02001023
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 $sql = $this->_compile_select();
1025
1026 $result = $this->query($sql);
1027 $this->_reset_select();
1028 return $result;
1029 }
1030
1031 // --------------------------------------------------------------------
1032
1033 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001034 * Insert_Batch
1035 *
1036 * Compiles batch insert strings and runs the queries
1037 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001038 * @param string the table to retrieve the results from
1039 * @param array an associative array of insert values
1040 * @return object
1041 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001042 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001043 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001044 if ( ! is_null($set))
1045 {
1046 $this->set_insert_batch($set);
1047 }
Barry Mienydd671972010-10-04 16:33:58 +02001048
Derek Jonesd10e8962010-03-02 17:10:36 -06001049 if (count($this->ar_set) == 0)
1050 {
1051 if ($this->db_debug)
1052 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001053 //No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001054 return $this->display_error('db_must_use_set');
1055 }
1056 return FALSE;
1057 }
1058
1059 if ($table == '')
1060 {
1061 if ( ! isset($this->ar_from[0]))
1062 {
1063 if ($this->db_debug)
1064 {
1065 return $this->display_error('db_must_set_table');
1066 }
1067 return FALSE;
1068 }
Barry Mienydd671972010-10-04 16:33:58 +02001069
Derek Jonesd10e8962010-03-02 17:10:36 -06001070 $table = $this->ar_from[0];
1071 }
1072
1073 // Batch this baby
1074 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1075 {
Barry Mienydd671972010-10-04 16:33:58 +02001076
Derek Jonesd10e8962010-03-02 17:10:36 -06001077 $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
1078
1079 //echo $sql;
1080
1081 $this->query($sql);
1082 }
Barry Mienydd671972010-10-04 16:33:58 +02001083
Derek Jonesd10e8962010-03-02 17:10:36 -06001084 $this->_reset_write();
1085
1086
Barry Mienydd671972010-10-04 16:33:58 +02001087 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001088 }
1089
1090 // --------------------------------------------------------------------
1091
1092 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001093 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001094 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001095 * @param mixed
1096 * @param string
1097 * @param boolean
1098 * @return object
1099 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001100 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001101 {
1102 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001103
Derek Jonesd10e8962010-03-02 17:10:36 -06001104 if ( ! is_array($key))
1105 {
1106 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001107 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001108
1109 $keys = array_keys(current($key));
1110 sort($keys);
1111
1112 foreach ($key as $row)
1113 {
Barry Mienydd671972010-10-04 16:33:58 +02001114 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1115 {
1116 // batch function above returns an error on an empty array
1117 $this->ar_set[] = array();
1118 return;
1119 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001120
Barry Mienydd671972010-10-04 16:33:58 +02001121 ksort($row); // puts $row in the same order as our keys
1122
Derek Jonesd10e8962010-03-02 17:10:36 -06001123 if ($escape === FALSE)
1124 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001125 $this->ar_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001126 }
1127 else
1128 {
1129 $clean = array();
1130
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001131 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001132 {
Barry Mienydd671972010-10-04 16:33:58 +02001133 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001134 }
1135
Derek Jones37f4b9c2011-07-01 17:56:50 -05001136 $this->ar_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001137 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001138 }
1139
1140 foreach ($keys as $k)
1141 {
1142 $this->ar_keys[] = $this->_protect_identifiers($k);
1143 }
Barry Mienydd671972010-10-04 16:33:58 +02001144
Derek Jonesd10e8962010-03-02 17:10:36 -06001145 return $this;
1146 }
1147
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 // --------------------------------------------------------------------
1149
1150 /**
1151 * Insert
1152 *
1153 * Compiles an insert string and runs the query
1154 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001155 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001156 * @param array an associative array of insert values
1157 * @return object
1158 */
1159 function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001160 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 if ( ! is_null($set))
1162 {
1163 $this->set($set);
1164 }
Barry Mienydd671972010-10-04 16:33:58 +02001165
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 if (count($this->ar_set) == 0)
1167 {
1168 if ($this->db_debug)
1169 {
1170 return $this->display_error('db_must_use_set');
1171 }
1172 return FALSE;
1173 }
1174
1175 if ($table == '')
1176 {
1177 if ( ! isset($this->ar_from[0]))
1178 {
1179 if ($this->db_debug)
1180 {
1181 return $this->display_error('db_must_set_table');
1182 }
1183 return FALSE;
1184 }
Barry Mienydd671972010-10-04 16:33:58 +02001185
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 $table = $this->ar_from[0];
1187 }
1188
1189 $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
Barry Mienydd671972010-10-04 16:33:58 +02001190
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001192 return $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 }
Barry Mienydd671972010-10-04 16:33:58 +02001194
Phil Sturgeon9789f322011-07-15 15:14:05 -06001195 // --------------------------------------------------------------------
1196
1197 /**
1198 * Replace
1199 *
1200 * Compiles an replace into string and runs the query
1201 *
1202 * @param string the table to replace data into
1203 * @param array an associative array of insert values
1204 * @return object
1205 */
1206 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001207 {
1208 if ( ! is_null($set))
1209 {
1210 $this->set($set);
1211 }
Barry Mienydd671972010-10-04 16:33:58 +02001212
Derek Jonesd10e8962010-03-02 17:10:36 -06001213 if (count($this->ar_set) == 0)
1214 {
1215 if ($this->db_debug)
1216 {
1217 return $this->display_error('db_must_use_set');
1218 }
1219 return FALSE;
1220 }
1221
1222 if ($table == '')
1223 {
1224 if ( ! isset($this->ar_from[0]))
1225 {
1226 if ($this->db_debug)
1227 {
1228 return $this->display_error('db_must_set_table');
1229 }
1230 return FALSE;
1231 }
Barry Mienydd671972010-10-04 16:33:58 +02001232
Derek Jonesd10e8962010-03-02 17:10:36 -06001233 $table = $this->ar_from[0];
1234 }
1235
1236 $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 +02001237
Derek Jonesd10e8962010-03-02 17:10:36 -06001238 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001239 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001240 }
1241
Derek Allard2067d1a2008-11-13 22:59:24 +00001242 // --------------------------------------------------------------------
1243
1244 /**
1245 * Update
1246 *
1247 * Compiles an update string and runs the query
1248 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 * @param string the table to retrieve the results from
1250 * @param array an associative array of update values
1251 * @param mixed the where clause
1252 * @return object
1253 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001254 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 {
1256 // Combine any cached components with the current statements
1257 $this->_merge_cache();
1258
1259 if ( ! is_null($set))
1260 {
1261 $this->set($set);
1262 }
Barry Mienydd671972010-10-04 16:33:58 +02001263
Derek Allard2067d1a2008-11-13 22:59:24 +00001264 if (count($this->ar_set) == 0)
1265 {
1266 if ($this->db_debug)
1267 {
1268 return $this->display_error('db_must_use_set');
1269 }
1270 return FALSE;
1271 }
1272
1273 if ($table == '')
1274 {
1275 if ( ! isset($this->ar_from[0]))
1276 {
1277 if ($this->db_debug)
1278 {
1279 return $this->display_error('db_must_set_table');
1280 }
1281 return FALSE;
1282 }
Barry Mienydd671972010-10-04 16:33:58 +02001283
Derek Allard2067d1a2008-11-13 22:59:24 +00001284 $table = $this->ar_from[0];
1285 }
Barry Mienydd671972010-10-04 16:33:58 +02001286
Derek Allard2067d1a2008-11-13 22:59:24 +00001287 if ($where != NULL)
1288 {
1289 $this->where($where);
1290 }
1291
1292 if ($limit != NULL)
1293 {
1294 $this->limit($limit);
1295 }
Barry Mienydd671972010-10-04 16:33:58 +02001296
Derek Allard2067d1a2008-11-13 22:59:24 +00001297 $sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
Barry Mienydd671972010-10-04 16:33:58 +02001298
Derek Allard2067d1a2008-11-13 22:59:24 +00001299 $this->_reset_write();
1300 return $this->query($sql);
1301 }
1302
Derek Jonesd10e8962010-03-02 17:10:36 -06001303
1304 // --------------------------------------------------------------------
1305
1306 /**
1307 * Update_Batch
1308 *
1309 * Compiles an update string and runs the query
1310 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001311 * @param string the table to retrieve the results from
1312 * @param array an associative array of update values
1313 * @param string the where key
1314 * @return object
1315 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001316 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001317 {
1318 // Combine any cached components with the current statements
1319 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001320
Derek Jonesd10e8962010-03-02 17:10:36 -06001321 if (is_null($index))
1322 {
1323 if ($this->db_debug)
1324 {
1325 return $this->display_error('db_myst_use_index');
1326 }
1327
1328 return FALSE;
1329 }
1330
1331 if ( ! is_null($set))
1332 {
1333 $this->set_update_batch($set, $index);
1334 }
1335
1336 if (count($this->ar_set) == 0)
1337 {
1338 if ($this->db_debug)
1339 {
1340 return $this->display_error('db_must_use_set');
1341 }
1342
1343 return FALSE;
1344 }
1345
1346 if ($table == '')
1347 {
1348 if ( ! isset($this->ar_from[0]))
1349 {
1350 if ($this->db_debug)
1351 {
1352 return $this->display_error('db_must_set_table');
1353 }
1354 return FALSE;
1355 }
Barry Mienydd671972010-10-04 16:33:58 +02001356
Derek Jonesd10e8962010-03-02 17:10:36 -06001357 $table = $this->ar_from[0];
1358 }
Barry Mienydd671972010-10-04 16:33:58 +02001359
Derek Jonesd10e8962010-03-02 17:10:36 -06001360 // Batch this baby
1361 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1362 {
1363 $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);
1364
1365 $this->query($sql);
1366 }
Barry Mienydd671972010-10-04 16:33:58 +02001367
Derek Jonesd10e8962010-03-02 17:10:36 -06001368 $this->_reset_write();
1369 }
1370
1371 // --------------------------------------------------------------------
1372
1373 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001374 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001375 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001376 * @param array
1377 * @param string
1378 * @param boolean
1379 * @return object
1380 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001381 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001382 {
1383 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001384
Derek Jonesd10e8962010-03-02 17:10:36 -06001385 if ( ! is_array($key))
1386 {
1387 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001388 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001389
1390 foreach ($key as $k => $v)
1391 {
1392 $index_set = FALSE;
1393 $clean = array();
1394
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001395 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001396 {
1397 if ($k2 == $index)
1398 {
1399 $index_set = TRUE;
1400 }
1401 else
1402 {
1403 $not[] = $k.'-'.$v;
1404 }
1405
1406 if ($escape === FALSE)
1407 {
1408 $clean[$this->_protect_identifiers($k2)] = $v2;
1409 }
1410 else
1411 {
Barry Mienydd671972010-10-04 16:33:58 +02001412 $clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001413 }
1414 }
1415
1416 if ($index_set == FALSE)
1417 {
1418 return $this->display_error('db_batch_missing_index');
1419 }
1420
1421 $this->ar_set[] = $clean;
1422 }
Barry Mienydd671972010-10-04 16:33:58 +02001423
Derek Jonesd10e8962010-03-02 17:10:36 -06001424 return $this;
1425 }
1426
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 // --------------------------------------------------------------------
1428
1429 /**
1430 * Empty Table
1431 *
1432 * Compiles a delete string and runs "DELETE FROM table"
1433 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001434 * @param string the table to empty
1435 * @return object
1436 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001437 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001438 {
1439 if ($table == '')
1440 {
1441 if ( ! isset($this->ar_from[0]))
1442 {
1443 if ($this->db_debug)
1444 {
1445 return $this->display_error('db_must_set_table');
1446 }
1447 return FALSE;
1448 }
1449
1450 $table = $this->ar_from[0];
1451 }
1452 else
1453 {
1454 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1455 }
1456
1457 $sql = $this->_delete($table);
1458
1459 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001460
Derek Allard2067d1a2008-11-13 22:59:24 +00001461 return $this->query($sql);
1462 }
1463
1464 // --------------------------------------------------------------------
1465
1466 /**
1467 * Truncate
1468 *
1469 * Compiles a truncate string and runs the query
1470 * If the database does not support the truncate() command
1471 * This function maps to "DELETE FROM table"
1472 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001473 * @param string the table to truncate
1474 * @return object
1475 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001476 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001477 {
1478 if ($table == '')
1479 {
1480 if ( ! isset($this->ar_from[0]))
1481 {
1482 if ($this->db_debug)
1483 {
1484 return $this->display_error('db_must_set_table');
1485 }
1486 return FALSE;
1487 }
1488
1489 $table = $this->ar_from[0];
1490 }
1491 else
1492 {
1493 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1494 }
1495
1496 $sql = $this->_truncate($table);
1497
1498 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001499
Derek Allard2067d1a2008-11-13 22:59:24 +00001500 return $this->query($sql);
1501 }
Barry Mienydd671972010-10-04 16:33:58 +02001502
Derek Allard2067d1a2008-11-13 22:59:24 +00001503 // --------------------------------------------------------------------
1504
1505 /**
1506 * Delete
1507 *
1508 * Compiles a delete string and runs the query
1509 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001510 * @param mixed the table(s) to delete from. String or array
1511 * @param mixed the where clause
1512 * @param mixed the limit clause
1513 * @param boolean
1514 * @return object
1515 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001516 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001517 {
1518 // Combine any cached components with the current statements
1519 $this->_merge_cache();
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 }
1531
1532 $table = $this->ar_from[0];
1533 }
1534 elseif (is_array($table))
1535 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001536 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 {
1538 $this->delete($single_table, $where, $limit, FALSE);
1539 }
1540
1541 $this->_reset_write();
1542 return;
1543 }
1544 else
1545 {
1546 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1547 }
1548
1549 if ($where != '')
1550 {
1551 $this->where($where);
1552 }
1553
1554 if ($limit != NULL)
1555 {
1556 $this->limit($limit);
1557 }
1558
Derek Allard03d783b2009-05-03 19:45:45 +00001559 if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001560 {
1561 if ($this->db_debug)
1562 {
1563 return $this->display_error('db_del_must_use_where');
1564 }
1565
1566 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001567 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001568
1569 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
1570
1571 if ($reset_data)
1572 {
1573 $this->_reset_write();
1574 }
Barry Mienydd671972010-10-04 16:33:58 +02001575
Derek Allard2067d1a2008-11-13 22:59:24 +00001576 return $this->query($sql);
1577 }
1578
1579 // --------------------------------------------------------------------
1580
1581 /**
1582 * DB Prefix
1583 *
1584 * Prepends a database prefix if one exists in configuration
1585 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001586 * @param string the table
1587 * @return string
1588 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001589 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001590 {
1591 if ($table == '')
1592 {
1593 $this->display_error('db_table_name_required');
1594 }
1595
1596 return $this->dbprefix.$table;
1597 }
1598
1599 // --------------------------------------------------------------------
1600
1601 /**
1602 * Track Aliases
1603 *
1604 * Used to track SQL statements written with aliased tables.
1605 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001606 * @param string The table to inspect
1607 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001608 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001609 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001610 {
1611 if (is_array($table))
1612 {
1613 foreach ($table as $t)
1614 {
1615 $this->_track_aliases($t);
1616 }
1617 return;
1618 }
Barry Mienydd671972010-10-04 16:33:58 +02001619
Derek Jones37f4b9c2011-07-01 17:56:50 -05001620 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001621 // the string into discreet statements
1622 if (strpos($table, ',') !== FALSE)
1623 {
1624 return $this->_track_aliases(explode(',', $table));
1625 }
Barry Mienydd671972010-10-04 16:33:58 +02001626
Derek Allard2067d1a2008-11-13 22:59:24 +00001627 // if a table alias is used we can recognize it by a space
1628 if (strpos($table, " ") !== FALSE)
1629 {
1630 // if the alias is written with the AS keyword, remove it
1631 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001632
Derek Allard2067d1a2008-11-13 22:59:24 +00001633 // Grab the alias
1634 $table = trim(strrchr($table, " "));
Barry Mienydd671972010-10-04 16:33:58 +02001635
Derek Allard2067d1a2008-11-13 22:59:24 +00001636 // Store the alias, if it doesn't already exist
1637 if ( ! in_array($table, $this->ar_aliased_tables))
1638 {
1639 $this->ar_aliased_tables[] = $table;
1640 }
1641 }
1642 }
1643
1644 // --------------------------------------------------------------------
1645
1646 /**
1647 * Compile the SELECT statement
1648 *
1649 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001650 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001651 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001652 * @return string
1653 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001654 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001655 {
1656 // Combine any cached components with the current statements
1657 $this->_merge_cache();
1658
1659 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001660
Derek Allard2067d1a2008-11-13 22:59:24 +00001661 // Write the "select" portion of the query
1662
1663 if ($select_override !== FALSE)
1664 {
1665 $sql = $select_override;
1666 }
1667 else
1668 {
1669 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001670
Derek Allard2067d1a2008-11-13 22:59:24 +00001671 if (count($this->ar_select) == 0)
1672 {
Barry Mienydd671972010-10-04 16:33:58 +02001673 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001674 }
1675 else
Barry Mienydd671972010-10-04 16:33:58 +02001676 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001677 // Cycle through the "select" portion of the query and prep each column name.
1678 // The reason we protect identifiers here rather then in the select() function
1679 // is because until the user calls the from() function we don't know if there are aliases
1680 foreach ($this->ar_select as $key => $val)
1681 {
Greg Akere156c6e2011-04-20 16:03:04 -05001682 $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $this->ar_no_escape[$key]);
Derek Allard2067d1a2008-11-13 22:59:24 +00001683 }
Barry Mienydd671972010-10-04 16:33:58 +02001684
Derek Allard2067d1a2008-11-13 22:59:24 +00001685 $sql .= implode(', ', $this->ar_select);
1686 }
1687 }
1688
1689 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001690
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 // Write the "FROM" portion of the query
1692
1693 if (count($this->ar_from) > 0)
1694 {
1695 $sql .= "\nFROM ";
1696
1697 $sql .= $this->_from_tables($this->ar_from);
1698 }
1699
1700 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001701
Derek Allard2067d1a2008-11-13 22:59:24 +00001702 // Write the "JOIN" portion of the query
1703
1704 if (count($this->ar_join) > 0)
1705 {
1706 $sql .= "\n";
1707
1708 $sql .= implode("\n", $this->ar_join);
1709 }
1710
1711 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001712
Derek Allard2067d1a2008-11-13 22:59:24 +00001713 // Write the "WHERE" portion of the query
1714
1715 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1716 {
Greg Akere156c6e2011-04-20 16:03:04 -05001717 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00001718 }
1719
1720 $sql .= implode("\n", $this->ar_where);
1721
1722 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001723
Derek Allard2067d1a2008-11-13 22:59:24 +00001724 // Write the "LIKE" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001725
Derek Allard2067d1a2008-11-13 22:59:24 +00001726 if (count($this->ar_like) > 0)
1727 {
1728 if (count($this->ar_where) > 0)
1729 {
1730 $sql .= "\nAND ";
1731 }
1732
1733 $sql .= implode("\n", $this->ar_like);
1734 }
1735
1736 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001737
Derek Allard2067d1a2008-11-13 22:59:24 +00001738 // Write the "GROUP BY" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001739
Derek Allard2067d1a2008-11-13 22:59:24 +00001740 if (count($this->ar_groupby) > 0)
1741 {
1742 $sql .= "\nGROUP BY ";
Barry Mienydd671972010-10-04 16:33:58 +02001743
Derek Allard2067d1a2008-11-13 22:59:24 +00001744 $sql .= implode(', ', $this->ar_groupby);
1745 }
1746
1747 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001748
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 // Write the "HAVING" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001750
Derek Allard2067d1a2008-11-13 22:59:24 +00001751 if (count($this->ar_having) > 0)
1752 {
1753 $sql .= "\nHAVING ";
1754 $sql .= implode("\n", $this->ar_having);
1755 }
1756
1757 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001758
Derek Allard2067d1a2008-11-13 22:59:24 +00001759 // Write the "ORDER BY" portion of the query
1760
1761 if (count($this->ar_orderby) > 0)
1762 {
1763 $sql .= "\nORDER BY ";
1764 $sql .= implode(', ', $this->ar_orderby);
Barry Mienydd671972010-10-04 16:33:58 +02001765
Derek Allard2067d1a2008-11-13 22:59:24 +00001766 if ($this->ar_order !== FALSE)
1767 {
1768 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02001769 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001770 }
1771
1772 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001773
Derek Allard2067d1a2008-11-13 22:59:24 +00001774 // Write the "LIMIT" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001775
Derek Allard2067d1a2008-11-13 22:59:24 +00001776 if (is_numeric($this->ar_limit))
1777 {
1778 $sql .= "\n";
1779 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1780 }
1781
1782 return $sql;
1783 }
1784
1785 // --------------------------------------------------------------------
1786
1787 /**
1788 * Object to Array
1789 *
1790 * Takes an object as input and converts the class variables to array key/vals
1791 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001792 * @param object
1793 * @return array
1794 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001795 public function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001796 {
1797 if ( ! is_object($object))
1798 {
1799 return $object;
1800 }
Barry Mienydd671972010-10-04 16:33:58 +02001801
Derek Allard2067d1a2008-11-13 22:59:24 +00001802 $array = array();
1803 foreach (get_object_vars($object) as $key => $val)
1804 {
1805 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06001806 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00001807 {
1808 $array[$key] = $val;
1809 }
1810 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001811
1812 return $array;
1813 }
Barry Mienydd671972010-10-04 16:33:58 +02001814
Derek Jonesd10e8962010-03-02 17:10:36 -06001815 // --------------------------------------------------------------------
1816
1817 /**
1818 * Object to Array
1819 *
1820 * Takes an object as input and converts the class variables to array key/vals
1821 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001822 * @param object
1823 * @return array
1824 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001825 public function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06001826 {
1827 if ( ! is_object($object))
1828 {
1829 return $object;
1830 }
Barry Mienydd671972010-10-04 16:33:58 +02001831
Derek Jonesd10e8962010-03-02 17:10:36 -06001832 $array = array();
1833 $out = get_object_vars($object);
1834 $fields = array_keys($out);
1835
1836 foreach ($fields as $val)
1837 {
1838 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06001839 if ($val != '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06001840 {
1841
1842 $i = 0;
1843 foreach ($out[$val] as $data)
1844 {
1845 $array[$i][$val] = $data;
1846 $i++;
1847 }
1848 }
1849 }
1850
Derek Allard2067d1a2008-11-13 22:59:24 +00001851 return $array;
1852 }
Barry Mienydd671972010-10-04 16:33:58 +02001853
Derek Allard2067d1a2008-11-13 22:59:24 +00001854 // --------------------------------------------------------------------
1855
1856 /**
1857 * Start Cache
1858 *
1859 * Starts AR caching
1860 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001861 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001862 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001863 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00001864 {
1865 $this->ar_caching = TRUE;
1866 }
1867
1868 // --------------------------------------------------------------------
1869
1870 /**
1871 * Stop Cache
1872 *
1873 * Stops AR caching
1874 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001875 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001876 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001877 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00001878 {
1879 $this->ar_caching = FALSE;
1880 }
1881
1882 // --------------------------------------------------------------------
1883
1884 /**
1885 * Flush Cache
1886 *
1887 * Empties the AR cache
1888 *
1889 * @access public
1890 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001891 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001892 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02001893 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06001894 $this->_reset_run(array(
1895 'ar_cache_select' => array(),
1896 'ar_cache_from' => array(),
1897 'ar_cache_join' => array(),
1898 'ar_cache_where' => array(),
1899 'ar_cache_like' => array(),
1900 'ar_cache_groupby' => array(),
1901 'ar_cache_having' => array(),
1902 'ar_cache_orderby' => array(),
1903 'ar_cache_set' => array(),
1904 'ar_cache_exists' => array(),
1905 'ar_cache_no_escape' => array()
1906 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00001907 }
1908
1909 // --------------------------------------------------------------------
1910
1911 /**
1912 * Merge Cache
1913 *
Barry Mienydd671972010-10-04 16:33:58 +02001914 * When called, this function merges any cached AR arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00001915 * locally called ones.
1916 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001917 * @return void
1918 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001919 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00001920 {
1921 if (count($this->ar_cache_exists) == 0)
1922 {
1923 return;
1924 }
1925
1926 foreach ($this->ar_cache_exists as $val)
1927 {
1928 $ar_variable = 'ar_'.$val;
1929 $ar_cache_var = 'ar_cache_'.$val;
1930
1931 if (count($this->$ar_cache_var) == 0)
1932 {
1933 continue;
1934 }
1935
1936 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
1937 }
1938
1939 // If we are "protecting identifiers" we need to examine the "from"
1940 // portion of the query to determine if there are any aliases
1941 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
1942 {
1943 $this->_track_aliases($this->ar_from);
1944 }
Greg Aker2e1837a2011-05-06 12:17:04 -05001945
1946 $this->ar_no_escape = $this->ar_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00001947 }
1948
1949 // --------------------------------------------------------------------
1950
1951 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001952 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00001953 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001954 * @param array An array of fields to reset
1955 * @return void
1956 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001957 protected function _reset_run($ar_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00001958 {
1959 foreach ($ar_reset_items as $item => $default_value)
1960 {
1961 if ( ! in_array($item, $this->ar_store_array))
1962 {
1963 $this->$item = $default_value;
1964 }
1965 }
1966 }
1967
1968 // --------------------------------------------------------------------
1969
1970 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001971 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00001972 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001973 * @return void
1974 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001975 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00001976 {
1977 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06001978 'ar_select' => array(),
1979 'ar_from' => array(),
1980 'ar_join' => array(),
1981 'ar_where' => array(),
1982 'ar_like' => array(),
1983 'ar_groupby' => array(),
1984 'ar_having' => array(),
1985 'ar_orderby' => array(),
1986 'ar_wherein' => array(),
1987 'ar_aliased_tables' => array(),
1988 'ar_no_escape' => array(),
1989 'ar_distinct' => FALSE,
1990 'ar_limit' => FALSE,
1991 'ar_offset' => FALSE,
1992 'ar_order' => FALSE,
1993 );
Barry Mienydd671972010-10-04 16:33:58 +02001994
Derek Allard2067d1a2008-11-13 22:59:24 +00001995 $this->_reset_run($ar_reset_items);
1996 }
Barry Mienydd671972010-10-04 16:33:58 +02001997
Derek Allard2067d1a2008-11-13 22:59:24 +00001998 // --------------------------------------------------------------------
1999
2000 /**
2001 * Resets the active record "write" values.
2002 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002003 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002004 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002005 * @return void
2006 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002007 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002008 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002009 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06002010 'ar_set' => array(),
2011 'ar_from' => array(),
2012 'ar_where' => array(),
2013 'ar_like' => array(),
2014 'ar_orderby' => array(),
2015 'ar_keys' => array(),
2016 'ar_limit' => FALSE,
2017 'ar_order' => FALSE
2018 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002019
2020 $this->_reset_run($ar_reset_items);
2021 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002022}
2023
2024/* End of file DB_active_rec.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00002025/* Location: ./system/database/DB_active_rec.php */