blob: 4b159b031a4f362d510185fd52b1a67512c7d837 [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
Dan Montgomery7e7338e2011-08-21 17:50:23 -0300199 $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
Derek Allard2067d1a2008-11-13 22:59:24 +0000200
201 $this->ar_select[] = $sql;
Greg Aker827f3de2011-05-06 11:29:57 -0500202 $this->ar_no_escape[] = NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 if ($this->ar_caching === TRUE)
205 {
206 $this->ar_cache_select[] = $sql;
207 $this->ar_cache_exists[] = 'select';
208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 return $this;
211 }
212
213 // --------------------------------------------------------------------
214
215 /**
216 * Determines the alias name based on the table
217 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 * @param string
219 * @return string
220 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600221 protected function _create_alias_from_table($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
223 if (strpos($item, '.') !== FALSE)
224 {
225 return end(explode('.', $item));
226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 return $item;
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * DISTINCT
235 *
236 * Sets a flag which tells the query string compiler to add DISTINCT
237 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 * @param bool
239 * @return object
240 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600241 public function distinct($val = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
243 $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
244 return $this;
245 }
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 // --------------------------------------------------------------------
248
249 /**
250 * From
251 *
252 * Generates the FROM portion of the query
253 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 * @param mixed can be a string or array
255 * @return object
256 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600257 public function from($from)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
259 foreach ((array)$from as $val)
260 {
261 if (strpos($val, ',') !== FALSE)
262 {
263 foreach (explode(',', $val) as $v)
264 {
265 $v = trim($v);
266 $this->_track_aliases($v);
267
268 $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 if ($this->ar_caching === TRUE)
271 {
272 $this->ar_cache_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
273 $this->ar_cache_exists[] = 'from';
Barry Mienydd671972010-10-04 16:33:58 +0200274 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 }
276
277 }
278 else
279 {
280 $val = trim($val);
281
Derek Jones37f4b9c2011-07-01 17:56:50 -0500282 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200283 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 $this->_track_aliases($val);
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 if ($this->ar_caching === TRUE)
289 {
290 $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
291 $this->ar_cache_exists[] = 'from';
292 }
293 }
294 }
295
296 return $this;
297 }
298
299 // --------------------------------------------------------------------
300
301 /**
302 * Join
303 *
304 * Generates the JOIN portion of the query
305 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 * @param string
307 * @param string the join condition
308 * @param string the type of join
309 * @return object
310 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600311 public function join($table, $cond, $type = '')
Barry Mienydd671972010-10-04 16:33:58 +0200312 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 if ($type != '')
314 {
315 $type = strtoupper(trim($type));
316
317 if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
318 {
319 $type = '';
320 }
321 else
322 {
323 $type .= ' ';
324 }
325 }
326
Derek Jones37f4b9c2011-07-01 17:56:50 -0500327 // Extract any aliases that might exist. We use this information
Barry Mienydd671972010-10-04 16:33:58 +0200328 // in the _protect_identifiers to know whether to add a table prefix
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 $this->_track_aliases($table);
330
331 // Strip apart the condition and protect the identifiers
332 if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
333 {
334 $match[1] = $this->_protect_identifiers($match[1]);
335 $match[3] = $this->_protect_identifiers($match[3]);
Barry Mienydd671972010-10-04 16:33:58 +0200336
337 $cond = $match[1].$match[2].$match[3];
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 }
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 // Assemble the JOIN statement
341 $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
342
343 $this->ar_join[] = $join;
344 if ($this->ar_caching === TRUE)
345 {
346 $this->ar_cache_join[] = $join;
347 $this->ar_cache_exists[] = 'join';
348 }
349
350 return $this;
351 }
352
353 // --------------------------------------------------------------------
354
355 /**
356 * Where
357 *
358 * Generates the WHERE portion of the query. Separates
359 * multiple calls with AND
360 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 * @param mixed
362 * @param mixed
363 * @return object
364 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600365 public function where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
367 return $this->_where($key, $value, 'AND ', $escape);
368 }
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 // --------------------------------------------------------------------
371
372 /**
373 * OR Where
374 *
375 * Generates the WHERE portion of the query. Separates
376 * multiple calls with OR
377 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 * @param mixed
379 * @param mixed
380 * @return object
381 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600382 public function or_where($key, $value = NULL, $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
384 return $this->_where($key, $value, 'OR ', $escape);
385 }
386
387 // --------------------------------------------------------------------
388
389 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 * Where
391 *
Phil Sturgeon9789f322011-07-15 15:14:05 -0600392 * Called by where() or or_where()
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 * @param mixed
395 * @param mixed
396 * @param string
397 * @return object
398 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600399 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
401 if ( ! is_array($key))
402 {
403 $key = array($key => $value);
404 }
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 // If the escape value was not set will will base it on the global setting
407 if ( ! is_bool($escape))
408 {
409 $escape = $this->_protect_identifiers;
410 }
411
412 foreach ($key as $k => $v)
413 {
414 $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type;
415
416 if (is_null($v) && ! $this->_has_operator($k))
417 {
418 // value appears not to have been set, assign the test to IS NULL
419 $k .= ' IS NULL';
420 }
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 if ( ! is_null($v))
423 {
424 if ($escape === TRUE)
425 {
426 $k = $this->_protect_identifiers($k, FALSE, $escape);
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 $v = ' '.$this->escape($v);
429 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 if ( ! $this->_has_operator($k))
432 {
Greg Akere156c6e2011-04-20 16:03:04 -0500433 $k .= ' = ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 }
435 }
436 else
437 {
Barry Mienydd671972010-10-04 16:33:58 +0200438 $k = $this->_protect_identifiers($k, FALSE, $escape);
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
440
441 $this->ar_where[] = $prefix.$k.$v;
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 if ($this->ar_caching === TRUE)
444 {
445 $this->ar_cache_where[] = $prefix.$k.$v;
446 $this->ar_cache_exists[] = 'where';
447 }
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 return $this;
452 }
453
454 // --------------------------------------------------------------------
455
456 /**
457 * Where_in
458 *
459 * Generates a WHERE field IN ('item', 'item') SQL query joined with
460 * AND if appropriate
461 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 * @param string The field to search
463 * @param array The values searched on
464 * @return object
465 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600466 public function where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 {
468 return $this->_where_in($key, $values);
469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 // --------------------------------------------------------------------
472
473 /**
474 * Where_in_or
475 *
476 * Generates a WHERE field IN ('item', 'item') SQL query joined with
477 * OR if appropriate
478 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 * @param string The field to search
480 * @param array The values searched on
481 * @return object
482 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600483 public function or_where_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
485 return $this->_where_in($key, $values, FALSE, 'OR ');
486 }
487
488 // --------------------------------------------------------------------
489
490 /**
491 * Where_not_in
492 *
493 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
494 * with AND if appropriate
495 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 * @param string The field to search
497 * @param array The values searched on
498 * @return object
499 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600500 public function where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 {
502 return $this->_where_in($key, $values, TRUE);
503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 // --------------------------------------------------------------------
506
507 /**
508 * Where_not_in_or
509 *
510 * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
511 * with OR if appropriate
512 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 * @param string The field to search
514 * @param array The values searched on
515 * @return object
516 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600517 public function or_where_not_in($key = NULL, $values = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
519 return $this->_where_in($key, $values, TRUE, 'OR ');
520 }
521
522 // --------------------------------------------------------------------
523
524 /**
525 * Where_in
526 *
527 * Called by where_in, where_in_or, where_not_in, where_not_in_or
528 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 * @param string The field to search
530 * @param array The values searched on
531 * @param boolean If the statement would be IN or NOT IN
Barry Mienydd671972010-10-04 16:33:58 +0200532 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 * @return object
534 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600535 protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 {
537 if ($key === NULL OR $values === NULL)
538 {
539 return;
540 }
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 if ( ! is_array($values))
543 {
544 $values = array($values);
545 }
Barry Mienydd671972010-10-04 16:33:58 +0200546
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 $not = ($not) ? ' NOT' : '';
548
549 foreach ($values as $value)
550 {
551 $this->ar_wherein[] = $this->escape($value);
552 }
553
554 $prefix = (count($this->ar_where) == 0) ? '' : $type;
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
557
558 $this->ar_where[] = $where_in;
559 if ($this->ar_caching === TRUE)
560 {
561 $this->ar_cache_where[] = $where_in;
562 $this->ar_cache_exists[] = 'where';
563 }
564
565 // reset the array for multiple calls
566 $this->ar_wherein = array();
567 return $this;
568 }
Barry Mienydd671972010-10-04 16:33:58 +0200569
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 // --------------------------------------------------------------------
571
572 /**
573 * Like
574 *
575 * Generates a %LIKE% portion of the query. Separates
576 * multiple calls with AND
577 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 * @param mixed
579 * @param mixed
580 * @return object
581 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600582 public function like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 {
584 return $this->_like($field, $match, 'AND ', $side);
585 }
586
587 // --------------------------------------------------------------------
588
589 /**
590 * Not Like
591 *
592 * Generates a NOT LIKE portion of the query. Separates
593 * multiple calls with AND
594 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 * @param mixed
596 * @param mixed
597 * @return object
598 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600599 public function not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 {
601 return $this->_like($field, $match, 'AND ', $side, 'NOT');
602 }
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 // --------------------------------------------------------------------
605
606 /**
607 * OR Like
608 *
609 * Generates a %LIKE% portion of the query. Separates
610 * multiple calls with OR
611 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 * @param mixed
613 * @param mixed
614 * @return object
615 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600616 public function or_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 {
618 return $this->_like($field, $match, 'OR ', $side);
619 }
620
621 // --------------------------------------------------------------------
622
623 /**
624 * OR Not Like
625 *
626 * Generates a NOT LIKE portion of the query. Separates
627 * multiple calls with OR
628 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 * @param mixed
630 * @param mixed
631 * @return object
632 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600633 public function or_not_like($field, $match = '', $side = 'both')
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
635 return $this->_like($field, $match, 'OR ', $side, 'NOT');
636 }
Barry Mienydd671972010-10-04 16:33:58 +0200637
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 // --------------------------------------------------------------------
639
640 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 * Like
642 *
643 * Called by like() or orlike()
644 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 * @param mixed
646 * @param mixed
647 * @param string
648 * @return object
649 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600650 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 {
652 if ( ! is_array($field))
653 {
654 $field = array($field => $match);
655 }
Barry Mienydd671972010-10-04 16:33:58 +0200656
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 foreach ($field as $k => $v)
658 {
659 $k = $this->_protect_identifiers($k);
660
661 $prefix = (count($this->ar_like) == 0) ? '' : $type;
662
Derek Jonese4ed5832009-02-20 21:44:59 +0000663 $v = $this->escape_like_str($v);
Nithinea4ad9b2011-08-21 01:23:47 -0300664
665 if ($side == 'none')
666 {
667 $like_statement = $prefix." $k $not LIKE '{$v}'";
668 }
669 elseif ($side == 'before')
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 {
671 $like_statement = $prefix." $k $not LIKE '%{$v}'";
672 }
673 elseif ($side == 'after')
674 {
675 $like_statement = $prefix." $k $not LIKE '{$v}%'";
676 }
677 else
678 {
679 $like_statement = $prefix." $k $not LIKE '%{$v}%'";
680 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600681
Derek Jonese4ed5832009-02-20 21:44:59 +0000682 // some platforms require an escape sequence definition for LIKE wildcards
683 if ($this->_like_escape_str != '')
684 {
Greg Aker0d424892010-01-26 02:14:44 +0000685 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Jonese4ed5832009-02-20 21:44:59 +0000686 }
Derek Jonesd10e8962010-03-02 17:10:36 -0600687
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 $this->ar_like[] = $like_statement;
689 if ($this->ar_caching === TRUE)
690 {
691 $this->ar_cache_like[] = $like_statement;
692 $this->ar_cache_exists[] = 'like';
693 }
Barry Mienydd671972010-10-04 16:33:58 +0200694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 }
696 return $this;
697 }
Barry Mienydd671972010-10-04 16:33:58 +0200698
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 // --------------------------------------------------------------------
700
701 /**
702 * GROUP BY
703 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 * @param string
705 * @return object
706 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600707 public function group_by($by)
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 {
709 if (is_string($by))
710 {
711 $by = explode(',', $by);
712 }
Barry Mienydd671972010-10-04 16:33:58 +0200713
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 foreach ($by as $val)
715 {
716 $val = trim($val);
Barry Mienydd671972010-10-04 16:33:58 +0200717
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 if ($val != '')
719 {
720 $this->ar_groupby[] = $this->_protect_identifiers($val);
Barry Mienydd671972010-10-04 16:33:58 +0200721
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 if ($this->ar_caching === TRUE)
723 {
724 $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
725 $this->ar_cache_exists[] = 'groupby';
726 }
727 }
728 }
729 return $this;
730 }
731
732 // --------------------------------------------------------------------
733
734 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 * Sets the HAVING value
736 *
737 * Separates multiple calls with AND
738 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 * @param string
740 * @param string
741 * @return object
742 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600743 public function having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 {
745 return $this->_having($key, $value, 'AND ', $escape);
746 }
Barry Mienydd671972010-10-04 16:33:58 +0200747
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 // --------------------------------------------------------------------
749
750 /**
751 * Sets the OR HAVING value
752 *
753 * Separates multiple calls with OR
754 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 * @param string
756 * @param string
757 * @return object
758 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600759 public function or_having($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 {
761 return $this->_having($key, $value, 'OR ', $escape);
762 }
Barry Mienydd671972010-10-04 16:33:58 +0200763
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 // --------------------------------------------------------------------
765
766 /**
767 * Sets the HAVING values
768 *
769 * Called by having() or or_having()
770 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 * @param string
772 * @param string
773 * @return object
774 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600775 protected function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 {
777 if ( ! is_array($key))
778 {
779 $key = array($key => $value);
780 }
Barry Mienydd671972010-10-04 16:33:58 +0200781
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 foreach ($key as $k => $v)
783 {
784 $prefix = (count($this->ar_having) == 0) ? '' : $type;
785
786 if ($escape === TRUE)
787 {
788 $k = $this->_protect_identifiers($k);
789 }
790
791 if ( ! $this->_has_operator($k))
792 {
793 $k .= ' = ';
794 }
795
796 if ($v != '')
797 {
Adam Jackette611d8c2011-07-23 11:45:05 -0400798 $v = ' '.$this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 }
Barry Mienydd671972010-10-04 16:33:58 +0200800
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 $this->ar_having[] = $prefix.$k.$v;
802 if ($this->ar_caching === TRUE)
803 {
804 $this->ar_cache_having[] = $prefix.$k.$v;
805 $this->ar_cache_exists[] = 'having';
806 }
807 }
Barry Mienydd671972010-10-04 16:33:58 +0200808
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 return $this;
810 }
Barry Mienydd671972010-10-04 16:33:58 +0200811
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 // --------------------------------------------------------------------
813
814 /**
815 * Sets the ORDER BY value
816 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 * @param string
818 * @param string direction: asc or desc
819 * @return object
820 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600821 public function order_by($orderby, $direction = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 {
823 if (strtolower($direction) == 'random')
824 {
825 $orderby = ''; // Random results want or don't need a field name
826 $direction = $this->_random_keyword;
827 }
828 elseif (trim($direction) != '')
829 {
830 $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
831 }
Barry Mienydd671972010-10-04 16:33:58 +0200832
833
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 if (strpos($orderby, ',') !== FALSE)
835 {
836 $temp = array();
837 foreach (explode(',', $orderby) as $part)
838 {
839 $part = trim($part);
840 if ( ! in_array($part, $this->ar_aliased_tables))
841 {
842 $part = $this->_protect_identifiers(trim($part));
843 }
Barry Mienydd671972010-10-04 16:33:58 +0200844
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 $temp[] = $part;
846 }
Barry Mienydd671972010-10-04 16:33:58 +0200847
848 $orderby = implode(', ', $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 }
Derek Allarde37ab382009-02-03 16:13:57 +0000850 else if ($direction != $this->_random_keyword)
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 {
852 $orderby = $this->_protect_identifiers($orderby);
853 }
Barry Mienydd671972010-10-04 16:33:58 +0200854
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 $orderby_statement = $orderby.$direction;
Barry Mienydd671972010-10-04 16:33:58 +0200856
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 $this->ar_orderby[] = $orderby_statement;
858 if ($this->ar_caching === TRUE)
859 {
860 $this->ar_cache_orderby[] = $orderby_statement;
861 $this->ar_cache_exists[] = 'orderby';
862 }
863
864 return $this;
865 }
Barry Mienydd671972010-10-04 16:33:58 +0200866
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 // --------------------------------------------------------------------
868
869 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 * Sets the LIMIT value
871 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 * @param integer the limit value
873 * @param integer the offset value
874 * @return object
875 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600876 public function limit($value, $offset = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 {
Phil Sturgeonb0eae5f2011-08-10 09:00:52 -0600878 $this->ar_limit = (int) $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000879
880 if ($offset != '')
881 {
Phil Sturgeonb0eae5f2011-08-10 09:00:52 -0600882 $this->ar_offset = (int) $offset;
Derek Allard2067d1a2008-11-13 22:59:24 +0000883 }
Barry Mienydd671972010-10-04 16:33:58 +0200884
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 return $this;
886 }
Barry Mienydd671972010-10-04 16:33:58 +0200887
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 // --------------------------------------------------------------------
889
890 /**
891 * Sets the OFFSET value
892 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 * @param integer the offset value
894 * @return object
895 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600896 public function offset($offset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 {
898 $this->ar_offset = $offset;
899 return $this;
900 }
Barry Mienydd671972010-10-04 16:33:58 +0200901
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 // --------------------------------------------------------------------
903
904 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500905 * The "set" function. Allows key/value pairs to be set for inserting or updating
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 * @param mixed
908 * @param string
909 * @param boolean
910 * @return object
911 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600912 public function set($key, $value = '', $escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
914 $key = $this->_object_to_array($key);
Barry Mienydd671972010-10-04 16:33:58 +0200915
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 if ( ! is_array($key))
917 {
918 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +0200919 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000920
921 foreach ($key as $k => $v)
922 {
923 if ($escape === FALSE)
924 {
925 $this->ar_set[$this->_protect_identifiers($k)] = $v;
926 }
927 else
928 {
Phil Sturgeond0ac1a22011-02-15 22:54:08 +0000929 $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 }
931 }
Barry Mienydd671972010-10-04 16:33:58 +0200932
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 return $this;
934 }
Barry Mienydd671972010-10-04 16:33:58 +0200935
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 // --------------------------------------------------------------------
937
938 /**
939 * Get
940 *
941 * Compiles the select statement based on the other functions called
942 * and runs the query
943 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000944 * @param string the table
945 * @param string the limit clause
946 * @param string the offset clause
947 * @return object
948 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600949 public function get($table = '', $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 {
951 if ($table != '')
952 {
953 $this->_track_aliases($table);
954 $this->from($table);
955 }
Barry Mienydd671972010-10-04 16:33:58 +0200956
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 if ( ! is_null($limit))
958 {
959 $this->limit($limit, $offset);
960 }
Barry Mienydd671972010-10-04 16:33:58 +0200961
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 $sql = $this->_compile_select();
963
964 $result = $this->query($sql);
965 $this->_reset_select();
966 return $result;
967 }
968
969 /**
970 * "Count All Results" query
971 *
Barry Mienydd671972010-10-04 16:33:58 +0200972 * Generates a platform-specific query string that counts all records
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 * returned by an Active Record query.
974 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 * @param string
976 * @return string
977 */
Phil Sturgeon9789f322011-07-15 15:14:05 -0600978 public function count_all_results($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 {
980 if ($table != '')
981 {
982 $this->_track_aliases($table);
983 $this->from($table);
984 }
Barry Mienydd671972010-10-04 16:33:58 +0200985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
987
988 $query = $this->query($sql);
989 $this->_reset_select();
Barry Mienydd671972010-10-04 16:33:58 +0200990
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 if ($query->num_rows() == 0)
992 {
Phil Sturgeonaf6f3442011-03-22 19:12:23 +0000993 return 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 }
995
996 $row = $query->row();
Phil Sturgeonaf6f3442011-03-22 19:12:23 +0000997 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 }
999
1000 // --------------------------------------------------------------------
1001
1002 /**
1003 * Get_Where
1004 *
1005 * Allows the where clause, limit and offset to be added directly
1006 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 * @param string the where clause
1008 * @param string the limit clause
1009 * @param string the offset clause
1010 * @return object
1011 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001012 public function get_where($table = '', $where = null, $limit = null, $offset = null)
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 {
1014 if ($table != '')
1015 {
1016 $this->from($table);
1017 }
1018
1019 if ( ! is_null($where))
1020 {
1021 $this->where($where);
1022 }
Barry Mienydd671972010-10-04 16:33:58 +02001023
Derek Allard2067d1a2008-11-13 22:59:24 +00001024 if ( ! is_null($limit))
1025 {
1026 $this->limit($limit, $offset);
1027 }
Barry Mienydd671972010-10-04 16:33:58 +02001028
Derek Allard2067d1a2008-11-13 22:59:24 +00001029 $sql = $this->_compile_select();
1030
1031 $result = $this->query($sql);
1032 $this->_reset_select();
1033 return $result;
1034 }
1035
1036 // --------------------------------------------------------------------
1037
1038 /**
Derek Jonesd10e8962010-03-02 17:10:36 -06001039 * Insert_Batch
1040 *
1041 * Compiles batch insert strings and runs the queries
1042 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001043 * @param string the table to retrieve the results from
1044 * @param array an associative array of insert values
1045 * @return object
1046 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001047 public function insert_batch($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001048 {
Derek Jonesd10e8962010-03-02 17:10:36 -06001049 if ( ! is_null($set))
1050 {
1051 $this->set_insert_batch($set);
1052 }
Barry Mienydd671972010-10-04 16:33:58 +02001053
Derek Jonesd10e8962010-03-02 17:10:36 -06001054 if (count($this->ar_set) == 0)
1055 {
1056 if ($this->db_debug)
1057 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001058 //No valid data array. Folds in cases where keys and values did not match up
Derek Jonesd10e8962010-03-02 17:10:36 -06001059 return $this->display_error('db_must_use_set');
1060 }
1061 return FALSE;
1062 }
1063
1064 if ($table == '')
1065 {
1066 if ( ! isset($this->ar_from[0]))
1067 {
1068 if ($this->db_debug)
1069 {
1070 return $this->display_error('db_must_set_table');
1071 }
1072 return FALSE;
1073 }
Barry Mienydd671972010-10-04 16:33:58 +02001074
Derek Jonesd10e8962010-03-02 17:10:36 -06001075 $table = $this->ar_from[0];
1076 }
1077
1078 // Batch this baby
1079 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1080 {
Barry Mienydd671972010-10-04 16:33:58 +02001081
Derek Jonesd10e8962010-03-02 17:10:36 -06001082 $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
1083
1084 //echo $sql;
1085
1086 $this->query($sql);
1087 }
Barry Mienydd671972010-10-04 16:33:58 +02001088
Derek Jonesd10e8962010-03-02 17:10:36 -06001089 $this->_reset_write();
1090
1091
Barry Mienydd671972010-10-04 16:33:58 +02001092 return TRUE;
Derek Jonesd10e8962010-03-02 17:10:36 -06001093 }
1094
1095 // --------------------------------------------------------------------
1096
1097 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001098 * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
Derek Jonesd10e8962010-03-02 17:10:36 -06001099 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001100 * @param mixed
1101 * @param string
1102 * @param boolean
1103 * @return object
1104 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001105 public function set_insert_batch($key, $value = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001106 {
1107 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001108
Derek Jonesd10e8962010-03-02 17:10:36 -06001109 if ( ! is_array($key))
1110 {
1111 $key = array($key => $value);
Barry Mienydd671972010-10-04 16:33:58 +02001112 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001113
1114 $keys = array_keys(current($key));
1115 sort($keys);
1116
1117 foreach ($key as $row)
1118 {
Barry Mienydd671972010-10-04 16:33:58 +02001119 if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
1120 {
1121 // batch function above returns an error on an empty array
1122 $this->ar_set[] = array();
1123 return;
1124 }
Phil Sturgeond0ac1a22011-02-15 22:54:08 +00001125
Barry Mienydd671972010-10-04 16:33:58 +02001126 ksort($row); // puts $row in the same order as our keys
1127
Derek Jonesd10e8962010-03-02 17:10:36 -06001128 if ($escape === FALSE)
1129 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001130 $this->ar_set[] = '('.implode(',', $row).')';
Derek Jonesd10e8962010-03-02 17:10:36 -06001131 }
1132 else
1133 {
1134 $clean = array();
1135
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001136 foreach ($row as $value)
Derek Jonesd10e8962010-03-02 17:10:36 -06001137 {
Barry Mienydd671972010-10-04 16:33:58 +02001138 $clean[] = $this->escape($value);
Derek Jonesd10e8962010-03-02 17:10:36 -06001139 }
1140
Derek Jones37f4b9c2011-07-01 17:56:50 -05001141 $this->ar_set[] = '('.implode(',', $clean).')';
Barry Mienydd671972010-10-04 16:33:58 +02001142 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001143 }
1144
1145 foreach ($keys as $k)
1146 {
1147 $this->ar_keys[] = $this->_protect_identifiers($k);
1148 }
Barry Mienydd671972010-10-04 16:33:58 +02001149
Derek Jonesd10e8962010-03-02 17:10:36 -06001150 return $this;
1151 }
1152
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 // --------------------------------------------------------------------
1154
1155 /**
1156 * Insert
1157 *
1158 * Compiles an insert string and runs the query
1159 *
Phil Sturgeon9789f322011-07-15 15:14:05 -06001160 * @param string the table to insert data into
Derek Allard2067d1a2008-11-13 22:59:24 +00001161 * @param array an associative array of insert values
1162 * @return object
1163 */
1164 function insert($table = '', $set = NULL)
Barry Mienydd671972010-10-04 16:33:58 +02001165 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001166 if ( ! is_null($set))
1167 {
1168 $this->set($set);
1169 }
Barry Mienydd671972010-10-04 16:33:58 +02001170
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 if (count($this->ar_set) == 0)
1172 {
1173 if ($this->db_debug)
1174 {
1175 return $this->display_error('db_must_use_set');
1176 }
1177 return FALSE;
1178 }
1179
1180 if ($table == '')
1181 {
1182 if ( ! isset($this->ar_from[0]))
1183 {
1184 if ($this->db_debug)
1185 {
1186 return $this->display_error('db_must_set_table');
1187 }
1188 return FALSE;
1189 }
Barry Mienydd671972010-10-04 16:33:58 +02001190
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 $table = $this->ar_from[0];
1192 }
1193
1194 $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 +02001195
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001197 return $this->query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +00001198 }
Barry Mienydd671972010-10-04 16:33:58 +02001199
Phil Sturgeon9789f322011-07-15 15:14:05 -06001200 // --------------------------------------------------------------------
1201
1202 /**
1203 * Replace
1204 *
1205 * Compiles an replace into string and runs the query
1206 *
1207 * @param string the table to replace data into
1208 * @param array an associative array of insert values
1209 * @return object
1210 */
1211 public function replace($table = '', $set = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001212 {
1213 if ( ! is_null($set))
1214 {
1215 $this->set($set);
1216 }
Barry Mienydd671972010-10-04 16:33:58 +02001217
Derek Jonesd10e8962010-03-02 17:10:36 -06001218 if (count($this->ar_set) == 0)
1219 {
1220 if ($this->db_debug)
1221 {
1222 return $this->display_error('db_must_use_set');
1223 }
1224 return FALSE;
1225 }
1226
1227 if ($table == '')
1228 {
1229 if ( ! isset($this->ar_from[0]))
1230 {
1231 if ($this->db_debug)
1232 {
1233 return $this->display_error('db_must_set_table');
1234 }
1235 return FALSE;
1236 }
Barry Mienydd671972010-10-04 16:33:58 +02001237
Derek Jonesd10e8962010-03-02 17:10:36 -06001238 $table = $this->ar_from[0];
1239 }
1240
1241 $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 +02001242
Derek Jonesd10e8962010-03-02 17:10:36 -06001243 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001244 return $this->query($sql);
Derek Jonesd10e8962010-03-02 17:10:36 -06001245 }
1246
Derek Allard2067d1a2008-11-13 22:59:24 +00001247 // --------------------------------------------------------------------
1248
1249 /**
1250 * Update
1251 *
1252 * Compiles an update string and runs the query
1253 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001254 * @param string the table to retrieve the results from
1255 * @param array an associative array of update values
1256 * @param mixed the where clause
1257 * @return object
1258 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001259 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +00001260 {
1261 // Combine any cached components with the current statements
1262 $this->_merge_cache();
1263
1264 if ( ! is_null($set))
1265 {
1266 $this->set($set);
1267 }
Barry Mienydd671972010-10-04 16:33:58 +02001268
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 if (count($this->ar_set) == 0)
1270 {
1271 if ($this->db_debug)
1272 {
1273 return $this->display_error('db_must_use_set');
1274 }
1275 return FALSE;
1276 }
1277
1278 if ($table == '')
1279 {
1280 if ( ! isset($this->ar_from[0]))
1281 {
1282 if ($this->db_debug)
1283 {
1284 return $this->display_error('db_must_set_table');
1285 }
1286 return FALSE;
1287 }
Barry Mienydd671972010-10-04 16:33:58 +02001288
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 $table = $this->ar_from[0];
1290 }
Barry Mienydd671972010-10-04 16:33:58 +02001291
Derek Allard2067d1a2008-11-13 22:59:24 +00001292 if ($where != NULL)
1293 {
1294 $this->where($where);
1295 }
1296
1297 if ($limit != NULL)
1298 {
1299 $this->limit($limit);
1300 }
Barry Mienydd671972010-10-04 16:33:58 +02001301
Derek Allard2067d1a2008-11-13 22:59:24 +00001302 $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 +02001303
Derek Allard2067d1a2008-11-13 22:59:24 +00001304 $this->_reset_write();
1305 return $this->query($sql);
1306 }
1307
Derek Jonesd10e8962010-03-02 17:10:36 -06001308
1309 // --------------------------------------------------------------------
1310
1311 /**
1312 * Update_Batch
1313 *
1314 * Compiles an update string and runs the query
1315 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001316 * @param string the table to retrieve the results from
1317 * @param array an associative array of update values
1318 * @param string the where key
1319 * @return object
1320 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001321 public function update_batch($table = '', $set = NULL, $index = NULL)
Derek Jonesd10e8962010-03-02 17:10:36 -06001322 {
1323 // Combine any cached components with the current statements
1324 $this->_merge_cache();
Barry Mienydd671972010-10-04 16:33:58 +02001325
Derek Jonesd10e8962010-03-02 17:10:36 -06001326 if (is_null($index))
1327 {
1328 if ($this->db_debug)
1329 {
Phil Sturgeonfcb8c9e2011-08-10 08:28:39 -06001330 return $this->display_error('db_must_use_index');
Derek Jonesd10e8962010-03-02 17:10:36 -06001331 }
1332
1333 return FALSE;
1334 }
1335
1336 if ( ! is_null($set))
1337 {
1338 $this->set_update_batch($set, $index);
1339 }
1340
1341 if (count($this->ar_set) == 0)
1342 {
1343 if ($this->db_debug)
1344 {
1345 return $this->display_error('db_must_use_set');
1346 }
1347
1348 return FALSE;
1349 }
1350
1351 if ($table == '')
1352 {
1353 if ( ! isset($this->ar_from[0]))
1354 {
1355 if ($this->db_debug)
1356 {
1357 return $this->display_error('db_must_set_table');
1358 }
1359 return FALSE;
1360 }
Barry Mienydd671972010-10-04 16:33:58 +02001361
Derek Jonesd10e8962010-03-02 17:10:36 -06001362 $table = $this->ar_from[0];
1363 }
Barry Mienydd671972010-10-04 16:33:58 +02001364
Derek Jonesd10e8962010-03-02 17:10:36 -06001365 // Batch this baby
1366 for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
1367 {
1368 $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);
1369
1370 $this->query($sql);
1371 }
Barry Mienydd671972010-10-04 16:33:58 +02001372
Derek Jonesd10e8962010-03-02 17:10:36 -06001373 $this->_reset_write();
1374 }
1375
1376 // --------------------------------------------------------------------
1377
1378 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001379 * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
Derek Jonesd10e8962010-03-02 17:10:36 -06001380 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001381 * @param array
1382 * @param string
1383 * @param boolean
1384 * @return object
1385 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001386 public function set_update_batch($key, $index = '', $escape = TRUE)
Derek Jonesd10e8962010-03-02 17:10:36 -06001387 {
1388 $key = $this->_object_to_array_batch($key);
Barry Mienydd671972010-10-04 16:33:58 +02001389
Derek Jonesd10e8962010-03-02 17:10:36 -06001390 if ( ! is_array($key))
1391 {
1392 // @todo error
Barry Mienydd671972010-10-04 16:33:58 +02001393 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001394
1395 foreach ($key as $k => $v)
1396 {
1397 $index_set = FALSE;
1398 $clean = array();
1399
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001400 foreach ($v as $k2 => $v2)
Derek Jonesd10e8962010-03-02 17:10:36 -06001401 {
1402 if ($k2 == $index)
1403 {
1404 $index_set = TRUE;
1405 }
1406 else
1407 {
1408 $not[] = $k.'-'.$v;
1409 }
1410
1411 if ($escape === FALSE)
1412 {
1413 $clean[$this->_protect_identifiers($k2)] = $v2;
1414 }
1415 else
1416 {
Barry Mienydd671972010-10-04 16:33:58 +02001417 $clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
Derek Jonesd10e8962010-03-02 17:10:36 -06001418 }
1419 }
1420
1421 if ($index_set == FALSE)
1422 {
1423 return $this->display_error('db_batch_missing_index');
1424 }
1425
1426 $this->ar_set[] = $clean;
1427 }
Barry Mienydd671972010-10-04 16:33:58 +02001428
Derek Jonesd10e8962010-03-02 17:10:36 -06001429 return $this;
1430 }
1431
Derek Allard2067d1a2008-11-13 22:59:24 +00001432 // --------------------------------------------------------------------
1433
1434 /**
1435 * Empty Table
1436 *
1437 * Compiles a delete string and runs "DELETE FROM table"
1438 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001439 * @param string the table to empty
1440 * @return object
1441 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001442 public function empty_table($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001443 {
1444 if ($table == '')
1445 {
1446 if ( ! isset($this->ar_from[0]))
1447 {
1448 if ($this->db_debug)
1449 {
1450 return $this->display_error('db_must_set_table');
1451 }
1452 return FALSE;
1453 }
1454
1455 $table = $this->ar_from[0];
1456 }
1457 else
1458 {
1459 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1460 }
1461
1462 $sql = $this->_delete($table);
1463
1464 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001465
Derek Allard2067d1a2008-11-13 22:59:24 +00001466 return $this->query($sql);
1467 }
1468
1469 // --------------------------------------------------------------------
1470
1471 /**
1472 * Truncate
1473 *
1474 * Compiles a truncate string and runs the query
1475 * If the database does not support the truncate() command
1476 * This function maps to "DELETE FROM table"
1477 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001478 * @param string the table to truncate
1479 * @return object
1480 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001481 public function truncate($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001482 {
1483 if ($table == '')
1484 {
1485 if ( ! isset($this->ar_from[0]))
1486 {
1487 if ($this->db_debug)
1488 {
1489 return $this->display_error('db_must_set_table');
1490 }
1491 return FALSE;
1492 }
1493
1494 $table = $this->ar_from[0];
1495 }
1496 else
1497 {
1498 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1499 }
1500
1501 $sql = $this->_truncate($table);
1502
1503 $this->_reset_write();
Barry Mienydd671972010-10-04 16:33:58 +02001504
Derek Allard2067d1a2008-11-13 22:59:24 +00001505 return $this->query($sql);
1506 }
Barry Mienydd671972010-10-04 16:33:58 +02001507
Derek Allard2067d1a2008-11-13 22:59:24 +00001508 // --------------------------------------------------------------------
1509
1510 /**
1511 * Delete
1512 *
1513 * Compiles a delete string and runs the query
1514 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001515 * @param mixed the table(s) to delete from. String or array
1516 * @param mixed the where clause
1517 * @param mixed the limit clause
1518 * @param boolean
1519 * @return object
1520 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001521 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001522 {
1523 // Combine any cached components with the current statements
1524 $this->_merge_cache();
1525
1526 if ($table == '')
1527 {
1528 if ( ! isset($this->ar_from[0]))
1529 {
1530 if ($this->db_debug)
1531 {
1532 return $this->display_error('db_must_set_table');
1533 }
1534 return FALSE;
1535 }
1536
1537 $table = $this->ar_from[0];
1538 }
1539 elseif (is_array($table))
1540 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001541 foreach ($table as $single_table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001542 {
1543 $this->delete($single_table, $where, $limit, FALSE);
1544 }
1545
1546 $this->_reset_write();
1547 return;
1548 }
1549 else
1550 {
1551 $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
1552 }
1553
1554 if ($where != '')
1555 {
1556 $this->where($where);
1557 }
1558
1559 if ($limit != NULL)
1560 {
1561 $this->limit($limit);
1562 }
1563
Derek Allard03d783b2009-05-03 19:45:45 +00001564 if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001565 {
1566 if ($this->db_debug)
1567 {
1568 return $this->display_error('db_del_must_use_where');
1569 }
1570
1571 return FALSE;
Barry Mienydd671972010-10-04 16:33:58 +02001572 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001573
1574 $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
1575
1576 if ($reset_data)
1577 {
1578 $this->_reset_write();
1579 }
Barry Mienydd671972010-10-04 16:33:58 +02001580
Derek Allard2067d1a2008-11-13 22:59:24 +00001581 return $this->query($sql);
1582 }
1583
1584 // --------------------------------------------------------------------
1585
1586 /**
1587 * DB Prefix
1588 *
1589 * Prepends a database prefix if one exists in configuration
1590 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001591 * @param string the table
1592 * @return string
1593 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001594 public function dbprefix($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001595 {
1596 if ($table == '')
1597 {
1598 $this->display_error('db_table_name_required');
1599 }
1600
1601 return $this->dbprefix.$table;
1602 }
1603
1604 // --------------------------------------------------------------------
1605
1606 /**
Phil Sturgeon8a022472011-07-15 15:25:15 -06001607 * Set DB Prefix
1608 *
1609 * Set's the DB Prefix to something new without needing to reconnect
1610 *
1611 * @param string the prefix
1612 * @return string
1613 */
1614 public function set_dbprefix($prefix = '')
1615 {
1616 return $this->dbprefix = $prefix;
1617 }
1618
1619 // --------------------------------------------------------------------
1620
1621 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001622 * Track Aliases
1623 *
1624 * Used to track SQL statements written with aliased tables.
1625 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001626 * @param string The table to inspect
1627 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001628 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001629 protected function _track_aliases($table)
Derek Allard2067d1a2008-11-13 22:59:24 +00001630 {
1631 if (is_array($table))
1632 {
1633 foreach ($table as $t)
1634 {
1635 $this->_track_aliases($t);
1636 }
1637 return;
1638 }
Barry Mienydd671972010-10-04 16:33:58 +02001639
Derek Jones37f4b9c2011-07-01 17:56:50 -05001640 // Does the string contain a comma? If so, we need to separate
Derek Allard2067d1a2008-11-13 22:59:24 +00001641 // the string into discreet statements
1642 if (strpos($table, ',') !== FALSE)
1643 {
1644 return $this->_track_aliases(explode(',', $table));
1645 }
Barry Mienydd671972010-10-04 16:33:58 +02001646
Derek Allard2067d1a2008-11-13 22:59:24 +00001647 // if a table alias is used we can recognize it by a space
1648 if (strpos($table, " ") !== FALSE)
1649 {
1650 // if the alias is written with the AS keyword, remove it
1651 $table = preg_replace('/ AS /i', ' ', $table);
Barry Mienydd671972010-10-04 16:33:58 +02001652
Derek Allard2067d1a2008-11-13 22:59:24 +00001653 // Grab the alias
1654 $table = trim(strrchr($table, " "));
Barry Mienydd671972010-10-04 16:33:58 +02001655
Derek Allard2067d1a2008-11-13 22:59:24 +00001656 // Store the alias, if it doesn't already exist
1657 if ( ! in_array($table, $this->ar_aliased_tables))
1658 {
1659 $this->ar_aliased_tables[] = $table;
1660 }
1661 }
1662 }
1663
1664 // --------------------------------------------------------------------
1665
1666 /**
1667 * Compile the SELECT statement
1668 *
1669 * Generates a query string based on which functions were used.
Derek Jones37f4b9c2011-07-01 17:56:50 -05001670 * Should not be called directly. The get() function calls it.
Derek Allard2067d1a2008-11-13 22:59:24 +00001671 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001672 * @return string
1673 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001674 protected function _compile_select($select_override = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001675 {
1676 // Combine any cached components with the current statements
1677 $this->_merge_cache();
1678
1679 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001680
Derek Allard2067d1a2008-11-13 22:59:24 +00001681 // Write the "select" portion of the query
1682
1683 if ($select_override !== FALSE)
1684 {
1685 $sql = $select_override;
1686 }
1687 else
1688 {
1689 $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
Barry Mienydd671972010-10-04 16:33:58 +02001690
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 if (count($this->ar_select) == 0)
1692 {
Barry Mienydd671972010-10-04 16:33:58 +02001693 $sql .= '*';
Derek Allard2067d1a2008-11-13 22:59:24 +00001694 }
1695 else
Barry Mienydd671972010-10-04 16:33:58 +02001696 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001697 // Cycle through the "select" portion of the query and prep each column name.
1698 // The reason we protect identifiers here rather then in the select() function
1699 // is because until the user calls the from() function we don't know if there are aliases
1700 foreach ($this->ar_select as $key => $val)
1701 {
Phil Sturgeon77cc0282011-08-09 16:03:49 -06001702 $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL;
1703 $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape);
Derek Allard2067d1a2008-11-13 22:59:24 +00001704 }
Barry Mienydd671972010-10-04 16:33:58 +02001705
Derek Allard2067d1a2008-11-13 22:59:24 +00001706 $sql .= implode(', ', $this->ar_select);
1707 }
1708 }
1709
1710 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001711
Derek Allard2067d1a2008-11-13 22:59:24 +00001712 // Write the "FROM" portion of the query
1713
1714 if (count($this->ar_from) > 0)
1715 {
1716 $sql .= "\nFROM ";
1717
1718 $sql .= $this->_from_tables($this->ar_from);
1719 }
1720
1721 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001722
Derek Allard2067d1a2008-11-13 22:59:24 +00001723 // Write the "JOIN" portion of the query
1724
1725 if (count($this->ar_join) > 0)
1726 {
1727 $sql .= "\n";
1728
1729 $sql .= implode("\n", $this->ar_join);
1730 }
1731
1732 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001733
Derek Allard2067d1a2008-11-13 22:59:24 +00001734 // Write the "WHERE" portion of the query
1735
1736 if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
1737 {
Greg Akere156c6e2011-04-20 16:03:04 -05001738 $sql .= "\nWHERE ";
Derek Allard2067d1a2008-11-13 22:59:24 +00001739 }
1740
1741 $sql .= implode("\n", $this->ar_where);
1742
1743 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001744
Derek Allard2067d1a2008-11-13 22:59:24 +00001745 // Write the "LIKE" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001746
Derek Allard2067d1a2008-11-13 22:59:24 +00001747 if (count($this->ar_like) > 0)
1748 {
1749 if (count($this->ar_where) > 0)
1750 {
1751 $sql .= "\nAND ";
1752 }
1753
1754 $sql .= implode("\n", $this->ar_like);
1755 }
1756
1757 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001758
Derek Allard2067d1a2008-11-13 22:59:24 +00001759 // Write the "GROUP BY" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001760
Derek Allard2067d1a2008-11-13 22:59:24 +00001761 if (count($this->ar_groupby) > 0)
1762 {
1763 $sql .= "\nGROUP BY ";
Barry Mienydd671972010-10-04 16:33:58 +02001764
Derek Allard2067d1a2008-11-13 22:59:24 +00001765 $sql .= implode(', ', $this->ar_groupby);
1766 }
1767
1768 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001769
Derek Allard2067d1a2008-11-13 22:59:24 +00001770 // Write the "HAVING" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001771
Derek Allard2067d1a2008-11-13 22:59:24 +00001772 if (count($this->ar_having) > 0)
1773 {
1774 $sql .= "\nHAVING ";
1775 $sql .= implode("\n", $this->ar_having);
1776 }
1777
1778 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001779
Derek Allard2067d1a2008-11-13 22:59:24 +00001780 // Write the "ORDER BY" portion of the query
1781
1782 if (count($this->ar_orderby) > 0)
1783 {
1784 $sql .= "\nORDER BY ";
1785 $sql .= implode(', ', $this->ar_orderby);
Barry Mienydd671972010-10-04 16:33:58 +02001786
Derek Allard2067d1a2008-11-13 22:59:24 +00001787 if ($this->ar_order !== FALSE)
1788 {
1789 $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
Barry Mienydd671972010-10-04 16:33:58 +02001790 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001791 }
1792
1793 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001794
Derek Allard2067d1a2008-11-13 22:59:24 +00001795 // Write the "LIMIT" portion of the query
Barry Mienydd671972010-10-04 16:33:58 +02001796
Derek Allard2067d1a2008-11-13 22:59:24 +00001797 if (is_numeric($this->ar_limit))
1798 {
1799 $sql .= "\n";
1800 $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
1801 }
1802
1803 return $sql;
1804 }
1805
1806 // --------------------------------------------------------------------
1807
1808 /**
1809 * Object to Array
1810 *
1811 * Takes an object as input and converts the class variables to array key/vals
1812 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001813 * @param object
1814 * @return array
1815 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001816 public function _object_to_array($object)
Derek Allard2067d1a2008-11-13 22:59:24 +00001817 {
1818 if ( ! is_object($object))
1819 {
1820 return $object;
1821 }
Barry Mienydd671972010-10-04 16:33:58 +02001822
Derek Allard2067d1a2008-11-13 22:59:24 +00001823 $array = array();
1824 foreach (get_object_vars($object) as $key => $val)
1825 {
1826 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06001827 if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
Derek Allard2067d1a2008-11-13 22:59:24 +00001828 {
1829 $array[$key] = $val;
1830 }
1831 }
Derek Jonesd10e8962010-03-02 17:10:36 -06001832
1833 return $array;
1834 }
Barry Mienydd671972010-10-04 16:33:58 +02001835
Derek Jonesd10e8962010-03-02 17:10:36 -06001836 // --------------------------------------------------------------------
1837
1838 /**
1839 * Object to Array
1840 *
1841 * Takes an object as input and converts the class variables to array key/vals
1842 *
Derek Jonesd10e8962010-03-02 17:10:36 -06001843 * @param object
1844 * @return array
1845 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001846 public function _object_to_array_batch($object)
Derek Jonesd10e8962010-03-02 17:10:36 -06001847 {
1848 if ( ! is_object($object))
1849 {
1850 return $object;
1851 }
Barry Mienydd671972010-10-04 16:33:58 +02001852
Derek Jonesd10e8962010-03-02 17:10:36 -06001853 $array = array();
1854 $out = get_object_vars($object);
1855 $fields = array_keys($out);
1856
1857 foreach ($fields as $val)
1858 {
1859 // There are some built in keys we need to ignore for this conversion
Derek Jonescf579552010-03-11 09:13:34 -06001860 if ($val != '_parent_name')
Derek Jonesd10e8962010-03-02 17:10:36 -06001861 {
1862
1863 $i = 0;
1864 foreach ($out[$val] as $data)
1865 {
1866 $array[$i][$val] = $data;
1867 $i++;
1868 }
1869 }
1870 }
1871
Derek Allard2067d1a2008-11-13 22:59:24 +00001872 return $array;
1873 }
Barry Mienydd671972010-10-04 16:33:58 +02001874
Derek Allard2067d1a2008-11-13 22:59:24 +00001875 // --------------------------------------------------------------------
1876
1877 /**
1878 * Start Cache
1879 *
1880 * Starts AR caching
1881 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001882 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001883 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001884 public function start_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00001885 {
1886 $this->ar_caching = TRUE;
1887 }
1888
1889 // --------------------------------------------------------------------
1890
1891 /**
1892 * Stop Cache
1893 *
1894 * Stops AR caching
1895 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001896 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001897 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001898 public function stop_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00001899 {
1900 $this->ar_caching = FALSE;
1901 }
1902
1903 // --------------------------------------------------------------------
1904
1905 /**
1906 * Flush Cache
1907 *
1908 * Empties the AR cache
1909 *
1910 * @access public
1911 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001912 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001913 public function flush_cache()
Barry Mienydd671972010-10-04 16:33:58 +02001914 {
Phil Sturgeon9789f322011-07-15 15:14:05 -06001915 $this->_reset_run(array(
1916 'ar_cache_select' => array(),
1917 'ar_cache_from' => array(),
1918 'ar_cache_join' => array(),
1919 'ar_cache_where' => array(),
1920 'ar_cache_like' => array(),
1921 'ar_cache_groupby' => array(),
1922 'ar_cache_having' => array(),
1923 'ar_cache_orderby' => array(),
1924 'ar_cache_set' => array(),
1925 'ar_cache_exists' => array(),
1926 'ar_cache_no_escape' => array()
1927 ));
Derek Allard2067d1a2008-11-13 22:59:24 +00001928 }
1929
1930 // --------------------------------------------------------------------
1931
1932 /**
1933 * Merge Cache
1934 *
Barry Mienydd671972010-10-04 16:33:58 +02001935 * When called, this function merges any cached AR arrays with
Derek Allard2067d1a2008-11-13 22:59:24 +00001936 * locally called ones.
1937 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001938 * @return void
1939 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001940 protected function _merge_cache()
Derek Allard2067d1a2008-11-13 22:59:24 +00001941 {
1942 if (count($this->ar_cache_exists) == 0)
1943 {
1944 return;
1945 }
1946
1947 foreach ($this->ar_cache_exists as $val)
1948 {
1949 $ar_variable = 'ar_'.$val;
1950 $ar_cache_var = 'ar_cache_'.$val;
1951
1952 if (count($this->$ar_cache_var) == 0)
1953 {
1954 continue;
1955 }
1956
1957 $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
1958 }
1959
1960 // If we are "protecting identifiers" we need to examine the "from"
1961 // portion of the query to determine if there are any aliases
1962 if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
1963 {
1964 $this->_track_aliases($this->ar_from);
1965 }
Greg Aker2e1837a2011-05-06 12:17:04 -05001966
1967 $this->ar_no_escape = $this->ar_cache_no_escape;
Derek Allard2067d1a2008-11-13 22:59:24 +00001968 }
1969
1970 // --------------------------------------------------------------------
1971
1972 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001973 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00001974 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001975 * @param array An array of fields to reset
1976 * @return void
1977 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001978 protected function _reset_run($ar_reset_items)
Derek Allard2067d1a2008-11-13 22:59:24 +00001979 {
1980 foreach ($ar_reset_items as $item => $default_value)
1981 {
1982 if ( ! in_array($item, $this->ar_store_array))
1983 {
1984 $this->$item = $default_value;
1985 }
1986 }
1987 }
1988
1989 // --------------------------------------------------------------------
1990
1991 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001992 * Resets the active record values. Called by the get() function
Derek Allard2067d1a2008-11-13 22:59:24 +00001993 *
Derek Allard2067d1a2008-11-13 22:59:24 +00001994 * @return void
1995 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06001996 protected function _reset_select()
Derek Allard2067d1a2008-11-13 22:59:24 +00001997 {
1998 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06001999 'ar_select' => array(),
2000 'ar_from' => array(),
2001 'ar_join' => array(),
2002 'ar_where' => array(),
2003 'ar_like' => array(),
2004 'ar_groupby' => array(),
2005 'ar_having' => array(),
2006 'ar_orderby' => array(),
2007 'ar_wherein' => array(),
2008 'ar_aliased_tables' => array(),
2009 'ar_no_escape' => array(),
2010 'ar_distinct' => FALSE,
2011 'ar_limit' => FALSE,
2012 'ar_offset' => FALSE,
2013 'ar_order' => FALSE,
2014 );
Barry Mienydd671972010-10-04 16:33:58 +02002015
Derek Allard2067d1a2008-11-13 22:59:24 +00002016 $this->_reset_run($ar_reset_items);
2017 }
Barry Mienydd671972010-10-04 16:33:58 +02002018
Derek Allard2067d1a2008-11-13 22:59:24 +00002019 // --------------------------------------------------------------------
2020
2021 /**
2022 * Resets the active record "write" values.
2023 *
Robin Sowell43753fd2010-09-16 12:52:07 -04002024 * Called by the insert() update() insert_batch() update_batch() and delete() functions
Derek Allard2067d1a2008-11-13 22:59:24 +00002025 *
Derek Allard2067d1a2008-11-13 22:59:24 +00002026 * @return void
2027 */
Phil Sturgeon9789f322011-07-15 15:14:05 -06002028 protected function _reset_write()
Barry Mienydd671972010-10-04 16:33:58 +02002029 {
Derek Allard2067d1a2008-11-13 22:59:24 +00002030 $ar_reset_items = array(
Phil Sturgeon9789f322011-07-15 15:14:05 -06002031 'ar_set' => array(),
2032 'ar_from' => array(),
2033 'ar_where' => array(),
2034 'ar_like' => array(),
2035 'ar_orderby' => array(),
2036 'ar_keys' => array(),
2037 'ar_limit' => FALSE,
2038 'ar_order' => FALSE
2039 );
Derek Allard2067d1a2008-11-13 22:59:24 +00002040
2041 $this->_reset_run($ar_reset_items);
2042 }
Derek Allard2067d1a2008-11-13 22:59:24 +00002043}
2044
2045/* End of file DB_active_rec.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00002046/* Location: ./system/database/DB_active_rec.php */