Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 2 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 3 | * CodeIgniter
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Rick Ellis | 37b3ecf | 2008-09-12 23:34:18 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008, EllisLab, Inc.
|
Derek Allard | cdd2ab2 | 2008-01-23 00:05:38 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 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
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 26 | * @author ExpressionEngine Dev Team
|
Derek Allard | cdd2ab2 | 2008-01-23 00:05:38 +0000 | [diff] [blame] | 27 | * @link http://codeigniter.com/user_guide/database/
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 28 | */
|
| 29 | class 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();
|
| 39 | var $ar_limit = FALSE;
|
| 40 | var $ar_offset = FALSE;
|
| 41 | var $ar_order = FALSE;
|
| 42 | var $ar_orderby = array();
|
| 43 | var $ar_set = array();
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 44 | var $ar_wherein = array();
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 45 | var $ar_aliased_tables = array();
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 46 | var $ar_store_array = array();
|
| 47 |
|
| 48 | // Active Record Caching variables
|
| 49 | var $ar_caching = FALSE;
|
| 50 | var $ar_cache_select = array();
|
| 51 | var $ar_cache_from = array();
|
| 52 | var $ar_cache_join = array();
|
| 53 | var $ar_cache_where = array();
|
| 54 | var $ar_cache_like = array();
|
| 55 | var $ar_cache_groupby = array();
|
| 56 | var $ar_cache_having = array();
|
| 57 | var $ar_cache_limit = FALSE;
|
| 58 | var $ar_cache_offset = FALSE;
|
| 59 | var $ar_cache_order = FALSE;
|
| 60 | var $ar_cache_orderby = array();
|
| 61 | var $ar_cache_set = array();
|
| 62 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 63 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 64 | /**
|
Derek Allard | 3b11868 | 2008-01-22 23:44:32 +0000 | [diff] [blame] | 65 | * DB Prefix
|
| 66 | *
|
| 67 | * Prepends a database prefix if one exists in configuration
|
| 68 | *
|
| 69 | * @access public
|
| 70 | * @param string the table
|
| 71 | * @return string
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 72 | */
|
Derek Allard | 3b11868 | 2008-01-22 23:44:32 +0000 | [diff] [blame] | 73 | function dbprefix($table = '')
|
| 74 | {
|
| 75 | if ($table == '')
|
| 76 | {
|
| 77 | $this->display_error('db_table_name_required');
|
| 78 | }
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 79 |
|
Derek Allard | 3b11868 | 2008-01-22 23:44:32 +0000 | [diff] [blame] | 80 | return $this->dbprefix.$table;
|
| 81 | }
|
| 82 |
|
| 83 | // --------------------------------------------------------------------
|
| 84 |
|
| 85 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 86 | * Select
|
| 87 | *
|
| 88 | * Generates the SELECT portion of the query
|
| 89 | *
|
| 90 | * @access public
|
| 91 | * @param string
|
| 92 | * @return object
|
| 93 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 94 | function select($select = '*', $protect_identifiers = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 95 | {
|
| 96 | if (is_string($select))
|
| 97 | {
|
Derek Allard | a632589 | 2008-05-12 17:51:47 +0000 | [diff] [blame] | 98 | if ($protect_identifiers !== FALSE)
|
| 99 | {
|
| 100 | $select = explode(',', $select);
|
| 101 | }
|
| 102 | else
|
| 103 | {
|
| 104 | $select = array($select);
|
| 105 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 106 | }
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 107 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 108 | foreach ($select as $val)
|
| 109 | {
|
| 110 | $val = trim($val);
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 111 |
|
| 112 | if ($val != '*' && $protect_identifiers !== FALSE)
|
| 113 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 114 | if (strpos($val, '.') !== FALSE)
|
| 115 | {
|
| 116 | $val = $this->dbprefix.$val;
|
| 117 | }
|
| 118 | else
|
| 119 | {
|
| 120 | $val = $this->_protect_identifiers($val);
|
| 121 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 122 | }
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 123 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 124 | if ($val != '')
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 125 | {
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 126 | $this->ar_select[] = $val;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 127 | if ($this->ar_caching === TRUE)
|
| 128 | {
|
| 129 | $this->ar_cache_select[] = $val;
|
| 130 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 131 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 132 | }
|
| 133 | return $this;
|
| 134 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 135 |
|
| 136 | // --------------------------------------------------------------------
|
| 137 |
|
| 138 | /**
|
| 139 | * Select Max
|
| 140 | *
|
| 141 | * Generates a SELECT MAX(field) portion of a query
|
| 142 | *
|
| 143 | * @access public
|
| 144 | * @param string the field
|
| 145 | * @param string an alias
|
| 146 | * @return object
|
| 147 | */
|
| 148 | function select_max($select = '', $alias='')
|
| 149 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 150 | if ( ! is_string($select) OR $select == '')
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 151 | {
|
| 152 | $this->display_error('db_invalid_query');
|
| 153 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 154 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 155 | $alias = ($alias != '') ? $alias : $select;
|
| 156 |
|
| 157 | $sql = 'MAX('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
|
| 158 |
|
| 159 | $this->ar_select[] = $sql;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 160 | if ($this->ar_caching === TRUE)
|
| 161 | {
|
| 162 | $this->ar_cache_select[] = $sql;
|
| 163 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 164 |
|
| 165 | return $this;
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 166 | }
|
| 167 |
|
| 168 | // --------------------------------------------------------------------
|
| 169 |
|
| 170 | /**
|
| 171 | * Select Min
|
| 172 | *
|
| 173 | * Generates a SELECT MIN(field) portion of a query
|
| 174 | *
|
| 175 | * @access public
|
| 176 | * @param string the field
|
| 177 | * @param string an alias
|
| 178 | * @return object
|
| 179 | */
|
| 180 | function select_min($select = '', $alias='')
|
| 181 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 182 | if ( ! is_string($select) OR $select == '')
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 183 | {
|
| 184 | $this->display_error('db_invalid_query');
|
| 185 | }
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 186 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 187 | $alias = ($alias != '') ? $alias : $select;
|
| 188 |
|
| 189 | $sql = 'MIN('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
|
| 190 |
|
| 191 | $this->ar_select[] = $sql;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 192 | if ($this->ar_caching === TRUE)
|
| 193 | {
|
| 194 | $this->ar_cache_select[] = $sql;
|
| 195 | }
|
| 196 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 197 | return $this;
|
| 198 | }
|
| 199 |
|
| 200 | // --------------------------------------------------------------------
|
| 201 |
|
| 202 | /**
|
| 203 | * Select Average
|
| 204 | *
|
| 205 | * Generates a SELECT AVG(field) portion of a query
|
| 206 | *
|
| 207 | * @access public
|
| 208 | * @param string the field
|
| 209 | * @param string an alias
|
| 210 | * @return object
|
| 211 | */
|
| 212 | function select_avg($select = '', $alias='')
|
| 213 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 214 | if ( ! is_string($select) OR $select == '')
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 215 | {
|
| 216 | $this->display_error('db_invalid_query');
|
| 217 | }
|
| 218 |
|
| 219 | $alias = ($alias != '') ? $alias : $select;
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 220 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 221 | $sql = 'AVG('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
|
| 222 |
|
| 223 | $this->ar_select[] = $sql;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 224 | if ($this->ar_caching === TRUE)
|
| 225 | {
|
| 226 | $this->ar_cache_select[] = $sql;
|
| 227 | }
|
| 228 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 229 | return $this;
|
| 230 | }
|
| 231 |
|
| 232 | // --------------------------------------------------------------------
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 233 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 234 | /**
|
| 235 | * Select Sum
|
| 236 | *
|
| 237 | * Generates a SELECT SUM(field) portion of a query
|
| 238 | *
|
| 239 | * @access public
|
| 240 | * @param string the field
|
| 241 | * @param string an alias
|
| 242 | * @return object
|
| 243 | */
|
| 244 | function select_sum($select = '', $alias='')
|
| 245 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 246 | if ( ! is_string($select) OR $select == '')
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 247 | {
|
| 248 | $this->display_error('db_invalid_query');
|
| 249 | }
|
| 250 |
|
| 251 | $alias = ($alias != '') ? $alias : $select;
|
| 252 |
|
| 253 | $sql = 'SUM('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
|
| 254 |
|
| 255 | $this->ar_select[] = $sql;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 256 | if ($this->ar_caching === TRUE)
|
| 257 | {
|
| 258 | $this->ar_cache_select[] = $sql;
|
| 259 | }
|
| 260 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 261 | return $this;
|
| 262 | }
|
| 263 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 264 | // --------------------------------------------------------------------
|
| 265 |
|
| 266 | /**
|
| 267 | * DISTINCT
|
| 268 | *
|
| 269 | * Sets a flag which tells the query string compiler to add DISTINCT
|
| 270 | *
|
| 271 | * @access public
|
| 272 | * @param bool
|
| 273 | * @return object
|
| 274 | */
|
| 275 | function distinct($val = TRUE)
|
| 276 | {
|
| 277 | $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
|
| 278 | return $this;
|
| 279 | }
|
| 280 |
|
| 281 | // --------------------------------------------------------------------
|
| 282 |
|
| 283 | /**
|
| 284 | * From
|
| 285 | *
|
| 286 | * Generates the FROM portion of the query
|
| 287 | *
|
| 288 | * @access public
|
| 289 | * @param mixed can be a string or array
|
| 290 | * @return object
|
| 291 | */
|
| 292 | function from($from)
|
| 293 | {
|
| 294 | foreach ((array)$from as $val)
|
| 295 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 296 | $this->ar_from[] = $this->_protect_identifiers($this->_track_aliases($val));
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 297 | if ($this->ar_caching === TRUE)
|
| 298 | {
|
Derek Allard | 9a4d1da | 2008-02-25 14:18:38 +0000 | [diff] [blame] | 299 | $this->ar_cache_from[] = $this->_protect_identifiers($this->_track_aliases($val));
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 300 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 301 | }
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 302 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 303 | return $this;
|
| 304 | }
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 305 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 306 | // --------------------------------------------------------------------
|
| 307 |
|
| 308 | /**
|
| 309 | * Join
|
| 310 | *
|
| 311 | * Generates the JOIN portion of the query
|
| 312 | *
|
| 313 | * @access public
|
| 314 | * @param string
|
| 315 | * @param string the join condition
|
| 316 | * @param string the type of join
|
| 317 | * @return object
|
| 318 | */
|
| 319 | function join($table, $cond, $type = '')
|
| 320 | {
|
| 321 | if ($type != '')
|
| 322 | {
|
| 323 | $type = strtoupper(trim($type));
|
| 324 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 325 | if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 326 | {
|
| 327 | $type = '';
|
| 328 | }
|
| 329 | else
|
| 330 | {
|
| 331 | $type .= ' ';
|
| 332 | }
|
| 333 | }
|
| 334 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 335 | // If a DB prefix is used we might need to add it to the column names
|
| 336 | if ($this->dbprefix)
|
| 337 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 338 | $this->_track_aliases($table);
|
| 339 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 340 | // First we remove any existing prefixes in the condition to avoid duplicates
|
| 341 | $cond = preg_replace('|('.$this->dbprefix.')([\w\.]+)([\W\s]+)|', "$2$3", $cond);
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 342 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 343 | // Next we add the prefixes to the condition
|
| 344 | $cond = preg_replace('|([\w\.]+)([\W\s]+)(.+)|', $this->dbprefix . "$1$2" . $this->dbprefix . "$3", $cond);
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 345 | }
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 346 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 347 | $join = $type.'JOIN '.$this->_protect_identifiers($this->dbprefix.$table, TRUE).' ON '.$cond;
|
| 348 |
|
| 349 | $this->ar_join[] = $join;
|
| 350 | if ($this->ar_caching === TRUE)
|
| 351 | {
|
| 352 | $this->ar_cache_join[] = $join;
|
| 353 | }
|
| 354 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 355 | return $this;
|
| 356 | }
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 357 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 358 | // --------------------------------------------------------------------
|
| 359 |
|
| 360 | /**
|
| 361 | * Where
|
| 362 | *
|
| 363 | * Generates the WHERE portion of the query. Separates
|
| 364 | * multiple calls with AND
|
| 365 | *
|
| 366 | * @access public
|
| 367 | * @param mixed
|
| 368 | * @param mixed
|
| 369 | * @return object
|
| 370 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 371 | function where($key, $value = NULL, $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 372 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 373 | return $this->_where($key, $value, 'AND ', $escape);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 374 | }
|
| 375 |
|
| 376 | // --------------------------------------------------------------------
|
| 377 |
|
| 378 | /**
|
| 379 | * OR Where
|
| 380 | *
|
| 381 | * Generates the WHERE portion of the query. Separates
|
| 382 | * multiple calls with OR
|
| 383 | *
|
| 384 | * @access public
|
| 385 | * @param mixed
|
| 386 | * @param mixed
|
| 387 | * @return object
|
| 388 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 389 | function or_where($key, $value = NULL, $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 390 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 391 | return $this->_where($key, $value, 'OR ', $escape);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 392 | }
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 393 |
|
| 394 | // --------------------------------------------------------------------
|
| 395 |
|
| 396 | /**
|
| 397 | * orwhere() is an alias of or_where()
|
| 398 | * this function is here for backwards compatibility, as
|
| 399 | * orwhere() has been deprecated
|
| 400 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 401 | function orwhere($key, $value = NULL, $escape = TRUE)
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 402 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 403 | return $this->or_where($key, $value, $escape);
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 404 | }
|
Derek Allard | 67b44ed | 2008-01-12 16:18:02 +0000 | [diff] [blame] | 405 |
|
| 406 | // --------------------------------------------------------------------
|
| 407 |
|
| 408 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 409 | * Where
|
| 410 | *
|
| 411 | * Called by where() or orwhere()
|
| 412 | *
|
| 413 | * @access private
|
| 414 | * @param mixed
|
| 415 | * @param mixed
|
| 416 | * @param string
|
| 417 | * @return object
|
| 418 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 419 | function _where($key, $value = NULL, $type = 'AND ', $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 420 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 421 | if ( ! is_array($key))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 422 | {
|
| 423 | $key = array($key => $value);
|
| 424 | }
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 425 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 426 | foreach ($key as $k => $v)
|
| 427 | {
|
| 428 | $prefix = (count($this->ar_where) == 0) ? '' : $type;
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 429 |
|
Derek Allard | d8364c4 | 2008-06-04 17:01:00 +0000 | [diff] [blame] | 430 | if (is_null($v) && ! $this->_has_operator($k))
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 431 | {
|
| 432 | // value appears not to have been set, assign the test to IS NULL
|
| 433 | $k .= ' IS NULL';
|
| 434 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 435 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 436 | if ( ! is_null($v))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 437 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 438 | if ($escape === TRUE)
|
| 439 | {
|
| 440 | // exception for "field<=" keys
|
| 441 | if ($this->_has_operator($k))
|
| 442 | {
|
Rick Ellis | 9f02e3c | 2008-09-30 21:51:01 +0000 | [diff] [blame] | 443 | $k = preg_replace("/([A-Za-z_0-9]+)/", $this->_escape_column($this->_protect_identifiers('$1')), $k);
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 444 | }
|
| 445 | else
|
| 446 | {
|
| 447 | $k = $this->_protect_identifiers($k);
|
| 448 | }
|
Derek Allard | d8364c4 | 2008-06-04 17:01:00 +0000 | [diff] [blame] | 449 |
|
| 450 | $v = ' '.$this->escape($v);
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 451 | }
|
| 452 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 453 | if ( ! $this->_has_operator($k))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 454 | {
|
| 455 | $k .= ' =';
|
| 456 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 457 | }
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 458 | else
|
| 459 | {
|
Derek Allard | 16629b1 | 2008-04-06 19:58:20 +0000 | [diff] [blame] | 460 | if ($escape === TRUE)
|
| 461 | {
|
| 462 | $k = $this->_protect_identifiers($k, TRUE);
|
Derek Jones | c06f58e | 2008-06-04 17:04:48 +0000 | [diff] [blame] | 463 | }
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 464 | }
|
| 465 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 466 | $this->ar_where[] = $prefix.$k.$v;
|
Derek Allard | d8364c4 | 2008-06-04 17:01:00 +0000 | [diff] [blame] | 467 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 468 | if ($this->ar_caching === TRUE)
|
| 469 | {
|
| 470 | $this->ar_cache_where[] = $prefix.$k.$v;
|
| 471 | }
|
| 472 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 473 | }
|
Derek Jones | 7e98a27 | 2008-06-04 17:05:44 +0000 | [diff] [blame] | 474 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 475 | return $this;
|
| 476 | }
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 477 |
|
| 478 | // --------------------------------------------------------------------
|
| 479 |
|
| 480 | /**
|
| 481 | * Where_in
|
| 482 | *
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 483 | * Generates a WHERE field IN ('item', 'item') SQL query joined with
|
| 484 | * AND if appropriate
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 485 | *
|
| 486 | * @access public
|
| 487 | * @param string The field to search
|
| 488 | * @param array The values searched on
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 489 | * @return object
|
| 490 | */
|
| 491 | function where_in($key = NULL, $values = NULL)
|
| 492 | {
|
| 493 | return $this->_where_in($key, $values);
|
| 494 | }
|
| 495 |
|
| 496 | // --------------------------------------------------------------------
|
| 497 |
|
| 498 | /**
|
| 499 | * Where_in_or
|
| 500 | *
|
| 501 | * Generates a WHERE field IN ('item', 'item') SQL query joined with
|
| 502 | * OR if appropriate
|
| 503 | *
|
| 504 | * @access public
|
| 505 | * @param string The field to search
|
| 506 | * @param array The values searched on
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 507 | * @return object
|
| 508 | */
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 509 | function or_where_in($key = NULL, $values = NULL)
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 510 | {
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 511 | return $this->_where_in($key, $values, FALSE, 'OR ');
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 512 | }
|
| 513 |
|
| 514 | // --------------------------------------------------------------------
|
| 515 |
|
| 516 | /**
|
| 517 | * Where_not_in
|
| 518 | *
|
| 519 | * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
|
| 520 | * with AND if appropriate
|
| 521 | *
|
| 522 | * @access public
|
| 523 | * @param string The field to search
|
| 524 | * @param array The values searched on
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 525 | * @return object
|
| 526 | */
|
| 527 | function where_not_in($key = NULL, $values = NULL)
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 528 | {
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 529 | return $this->_where_in($key, $values, TRUE);
|
| 530 | }
|
| 531 |
|
| 532 | // --------------------------------------------------------------------
|
| 533 |
|
| 534 | /**
|
| 535 | * Where_not_in_or
|
| 536 | *
|
| 537 | * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
|
| 538 | * with OR if appropriate
|
| 539 | *
|
| 540 | * @access public
|
| 541 | * @param string The field to search
|
| 542 | * @param array The values searched on
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 543 | * @return object
|
| 544 | */
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 545 | function or_where_not_in($key = NULL, $values = NULL)
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 546 | {
|
Derek Jones | d007243 | 2008-05-07 22:06:51 +0000 | [diff] [blame] | 547 | return $this->_where_in($key, $values, TRUE, 'OR ');
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 548 | }
|
| 549 |
|
| 550 | // --------------------------------------------------------------------
|
| 551 |
|
| 552 | /**
|
| 553 | * Where_in
|
| 554 | *
|
| 555 | * Called by where_in, where_in_or, where_not_in, where_not_in_or
|
| 556 | *
|
| 557 | * @access public
|
| 558 | * @param string The field to search
|
| 559 | * @param array The values searched on
|
| 560 | * @param boolean If the statement whould be IN or NOT IN
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 561 | * @param string
|
| 562 | * @return object
|
| 563 | */
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 564 | function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 565 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 566 | if ($key === NULL OR ! is_array($values))
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 567 | {
|
| 568 | return;
|
| 569 | }
|
| 570 |
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 571 | $not = ($not) ? ' NOT ' : '';
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 572 |
|
| 573 | foreach ($values as $value)
|
| 574 | {
|
| 575 | $this->ar_wherein[] = $this->escape($value);
|
| 576 | }
|
| 577 |
|
| 578 | $prefix = (count($this->ar_where) == 0) ? '' : $type;
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 579 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 580 | $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
|
| 581 |
|
| 582 | $this->ar_where[] = $where_in;
|
| 583 | if ($this->ar_caching === TRUE)
|
| 584 | {
|
| 585 | $this->ar_cache_where[] = $where_in;
|
| 586 | }
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 587 |
|
Derek Allard | 8f00021 | 2008-01-18 14:45:59 +0000 | [diff] [blame] | 588 | // reset the array for multiple calls
|
| 589 | $this->ar_wherein = array();
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 590 | return $this;
|
| 591 | }
|
| 592 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 593 | // --------------------------------------------------------------------
|
| 594 |
|
| 595 | /**
|
| 596 | * Like
|
| 597 | *
|
| 598 | * Generates a %LIKE% portion of the query. Separates
|
| 599 | * multiple calls with AND
|
| 600 | *
|
| 601 | * @access public
|
| 602 | * @param mixed
|
| 603 | * @param mixed
|
| 604 | * @return object
|
| 605 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 606 | function like($field, $match = '', $side = 'both')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 607 | {
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 608 | return $this->_like($field, $match, 'AND ', $side);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 609 | }
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 610 |
|
| 611 | // --------------------------------------------------------------------
|
| 612 |
|
| 613 | /**
|
| 614 | * Not Like
|
| 615 | *
|
| 616 | * Generates a NOT LIKE portion of the query. Separates
|
| 617 | * multiple calls with AND
|
| 618 | *
|
| 619 | * @access public
|
| 620 | * @param mixed
|
| 621 | * @param mixed
|
| 622 | * @return object
|
| 623 | */
|
| 624 | function not_like($field, $match = '', $side = 'both')
|
| 625 | {
|
| 626 | return $this->_like($field, $match, 'AND ', $side, ' NOT');
|
| 627 | }
|
| 628 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 629 | // --------------------------------------------------------------------
|
| 630 |
|
| 631 | /**
|
| 632 | * OR Like
|
| 633 | *
|
| 634 | * Generates a %LIKE% portion of the query. Separates
|
| 635 | * multiple calls with OR
|
| 636 | *
|
| 637 | * @access public
|
| 638 | * @param mixed
|
| 639 | * @param mixed
|
| 640 | * @return object
|
| 641 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 642 | function or_like($field, $match = '', $side = 'both')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 643 | {
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 644 | return $this->_like($field, $match, 'OR ', $side);
|
| 645 | }
|
| 646 |
|
| 647 | // --------------------------------------------------------------------
|
| 648 |
|
| 649 | /**
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 650 | * OR Not Like
|
| 651 | *
|
| 652 | * Generates a NOT LIKE portion of the query. Separates
|
| 653 | * multiple calls with OR
|
| 654 | *
|
| 655 | * @access public
|
| 656 | * @param mixed
|
| 657 | * @param mixed
|
| 658 | * @return object
|
| 659 | */
|
| 660 | function or_not_like($field, $match = '', $side = 'both')
|
| 661 | {
|
| 662 | return $this->_like($field, $match, 'OR ', $side, 'NOT ');
|
| 663 | }
|
| 664 |
|
| 665 | // --------------------------------------------------------------------
|
| 666 |
|
| 667 | /**
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 668 | * orlike() is an alias of or_like()
|
| 669 | * this function is here for backwards compatibility, as
|
| 670 | * orlike() has been deprecated
|
| 671 | */
|
| 672 | function orlike($field, $match = '', $side = 'both')
|
| 673 | {
|
Derek Allard | 4a310b7 | 2008-01-30 21:32:47 +0000 | [diff] [blame] | 674 | return $this->or_like($field, $match, $side);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 675 | }
|
| 676 |
|
| 677 | // --------------------------------------------------------------------
|
| 678 |
|
| 679 | /**
|
| 680 | * Like
|
| 681 | *
|
| 682 | * Called by like() or orlike()
|
| 683 | *
|
| 684 | * @access private
|
| 685 | * @param mixed
|
| 686 | * @param mixed
|
| 687 | * @param string
|
| 688 | * @return object
|
| 689 | */
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 690 | function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 691 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 692 | if ( ! is_array($field))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 693 | {
|
| 694 | $field = array($field => $match);
|
| 695 | }
|
| 696 |
|
| 697 | foreach ($field as $k => $v)
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 698 | {
|
| 699 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 700 | $k = $this->_protect_identifiers($k);
|
| 701 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 702 | $prefix = (count($this->ar_like) == 0) ? '' : $type;
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 703 |
|
Rick Ellis | 852160a | 2008-09-30 21:55:34 +0000 | [diff] [blame] | 704 | $v = $this->escape($v);
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 705 |
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 706 | if ($side == 'before')
|
| 707 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 708 | $like_statement = $prefix." $k $not LIKE '%{$v}'";
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 709 | }
|
| 710 | elseif ($side == 'after')
|
| 711 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 712 | $like_statement = $prefix." $k $not LIKE '{$v}%'";
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 713 | }
|
| 714 | else
|
| 715 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 716 | $like_statement = $prefix." $k $not LIKE '%{$v}%'";
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 717 | }
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 718 |
|
| 719 | $this->ar_like[] = $like_statement;
|
| 720 | if ($this->ar_caching === TRUE)
|
| 721 | {
|
| 722 | $this->ar_cache_like[] = $like_statement;
|
| 723 | }
|
| 724 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 725 | }
|
| 726 | return $this;
|
| 727 | }
|
| 728 |
|
| 729 | // --------------------------------------------------------------------
|
| 730 |
|
| 731 | /**
|
| 732 | * GROUP BY
|
| 733 | *
|
| 734 | * @access public
|
| 735 | * @param string
|
| 736 | * @return object
|
| 737 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 738 | function group_by($by)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 739 | {
|
| 740 | if (is_string($by))
|
| 741 | {
|
| 742 | $by = explode(',', $by);
|
| 743 | }
|
| 744 |
|
| 745 | foreach ($by as $val)
|
| 746 | {
|
| 747 | $val = trim($val);
|
| 748 |
|
| 749 | if ($val != '')
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 750 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 751 | $this->ar_groupby[] = $this->_protect_identifiers($val);
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 752 | if ($this->ar_caching === TRUE)
|
| 753 | {
|
| 754 | $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
|
| 755 | }
|
| 756 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 757 | }
|
| 758 | return $this;
|
| 759 | }
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 760 |
|
| 761 | // --------------------------------------------------------------------
|
| 762 |
|
| 763 | /**
|
| 764 | * groupby() is an alias of group_by()
|
| 765 | * this function is here for backwards compatibility, as
|
| 766 | * groupby() has been deprecated
|
| 767 | */
|
| 768 | function groupby($by)
|
| 769 | {
|
| 770 | return $this->group_by($by);
|
| 771 | }
|
| 772 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 773 | // --------------------------------------------------------------------
|
| 774 |
|
| 775 | /**
|
| 776 | * Sets the HAVING value
|
| 777 | *
|
| 778 | * Separates multiple calls with AND
|
| 779 | *
|
| 780 | * @access public
|
| 781 | * @param string
|
| 782 | * @param string
|
| 783 | * @return object
|
| 784 | */
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 785 | function having($key, $value = '', $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 786 | {
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 787 | return $this->_having($key, $value, 'AND ', $escape);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 788 | }
|
Derek Allard | 43e8cbf | 2008-03-01 23:14:43 +0000 | [diff] [blame] | 789 |
|
| 790 | // --------------------------------------------------------------------
|
| 791 |
|
| 792 | /**
|
| 793 | * orhaving() is an alias of or_having()
|
| 794 | * this function is here for backwards compatibility, as
|
| 795 | * orhaving() has been deprecated
|
| 796 | */
|
| 797 |
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 798 | function orhaving($key, $value = '', $escape = TRUE)
|
Derek Allard | 43e8cbf | 2008-03-01 23:14:43 +0000 | [diff] [blame] | 799 | {
|
Derek Allard | a459b46 | 2008-05-22 13:01:39 +0000 | [diff] [blame] | 800 | return $this->or_having($key, $value, $escape);
|
Derek Allard | 43e8cbf | 2008-03-01 23:14:43 +0000 | [diff] [blame] | 801 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 802 | // --------------------------------------------------------------------
|
| 803 |
|
| 804 | /**
|
| 805 | * Sets the OR HAVING value
|
| 806 | *
|
| 807 | * Separates multiple calls with OR
|
| 808 | *
|
| 809 | * @access public
|
| 810 | * @param string
|
| 811 | * @param string
|
| 812 | * @return object
|
| 813 | */
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 814 | function or_having($key, $value = '', $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 815 | {
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 816 | return $this->_having($key, $value, 'OR ', $escape);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 817 | }
|
| 818 |
|
| 819 | // --------------------------------------------------------------------
|
| 820 |
|
| 821 | /**
|
| 822 | * Sets the HAVING values
|
| 823 | *
|
Rick Ellis | ff73401 | 2008-09-30 20:38:12 +0000 | [diff] [blame] | 824 | * Called by having() or or_having()
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 825 | *
|
| 826 | * @access private
|
| 827 | * @param string
|
| 828 | * @param string
|
| 829 | * @return object
|
| 830 | */
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 831 | function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 832 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 833 | if ( ! is_array($key))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 834 | {
|
| 835 | $key = array($key => $value);
|
| 836 | }
|
| 837 |
|
| 838 | foreach ($key as $k => $v)
|
| 839 | {
|
| 840 | $prefix = (count($this->ar_having) == 0) ? '' : $type;
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 841 |
|
| 842 | if ($escape === TRUE)
|
| 843 | {
|
| 844 | $k = $this->_protect_identifiers($k);
|
| 845 | }
|
| 846 |
|
Derek Allard | a459b46 | 2008-05-22 13:01:39 +0000 | [diff] [blame] | 847 | if ( ! $this->_has_operator($k))
|
| 848 | {
|
| 849 | $k .= ' = ';
|
| 850 | }
|
| 851 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 852 | if ($v != '')
|
| 853 | {
|
Rick Ellis | 852160a | 2008-09-30 21:55:34 +0000 | [diff] [blame] | 854 | $v = ' '.$this->escape($v);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 855 | }
|
| 856 |
|
| 857 | $this->ar_having[] = $prefix.$k.$v;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 858 | if ($this->ar_caching === TRUE)
|
| 859 | {
|
| 860 | $this->ar_cache_having[] = $prefix.$k.$v;
|
| 861 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 862 | }
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 863 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 864 | return $this;
|
| 865 | }
|
| 866 |
|
| 867 | // --------------------------------------------------------------------
|
| 868 |
|
| 869 | /**
|
| 870 | * Sets the ORDER BY value
|
| 871 | *
|
| 872 | * @access public
|
| 873 | * @param string
|
| 874 | * @param string direction: asc or desc
|
| 875 | * @return object
|
| 876 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 877 | function order_by($orderby, $direction = '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 878 | {
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 879 | if (strtolower($direction) == 'random')
|
| 880 | {
|
| 881 | $orderby = ''; // Random results want or don't need a field name
|
| 882 | $direction = $this->_random_keyword;
|
| 883 | }
|
| 884 | elseif (trim($direction) != '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 885 | {
|
Derek Allard | 9278249 | 2007-08-10 11:26:01 +0000 | [diff] [blame] | 886 | $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 887 | }
|
| 888 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 889 | $orderby_statement = $this->_protect_identifiers($orderby, TRUE).$direction;
|
| 890 |
|
| 891 | $this->ar_orderby[] = $orderby_statement;
|
| 892 | if ($this->ar_caching === TRUE)
|
| 893 | {
|
| 894 | $this->ar_cache_orderby[] = $orderby_statement;
|
| 895 | }
|
| 896 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 897 | return $this;
|
| 898 | }
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 899 |
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 900 | // --------------------------------------------------------------------
|
| 901 |
|
| 902 | /**
|
| 903 | * orderby() is an alias of order_by()
|
| 904 | * this function is here for backwards compatibility, as
|
| 905 | * orderby() has been deprecated
|
| 906 | */
|
| 907 | function orderby($orderby, $direction = '')
|
| 908 | {
|
| 909 | return $this->order_by($orderby, $direction);
|
| 910 | }
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 911 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 912 | // --------------------------------------------------------------------
|
| 913 |
|
| 914 | /**
|
| 915 | * Sets the LIMIT value
|
| 916 | *
|
| 917 | * @access public
|
| 918 | * @param integer the limit value
|
| 919 | * @param integer the offset value
|
| 920 | * @return object
|
| 921 | */
|
| 922 | function limit($value, $offset = '')
|
| 923 | {
|
| 924 | $this->ar_limit = $value;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 925 | if ($this->ar_caching === TRUE)
|
| 926 | {
|
| 927 | $this->ar_cache_limit[] = $value;
|
| 928 | }
|
| 929 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 930 | if ($offset != '')
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 931 | {
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 932 | $this->ar_offset = $offset;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 933 | if ($this->ar_caching === TRUE)
|
| 934 | {
|
| 935 | $this->ar_cache_offset[] = $offset;
|
| 936 | }
|
| 937 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 938 |
|
| 939 | return $this;
|
| 940 | }
|
| 941 |
|
| 942 | // --------------------------------------------------------------------
|
| 943 |
|
| 944 | /**
|
| 945 | * Sets the OFFSET value
|
| 946 | *
|
| 947 | * @access public
|
| 948 | * @param integer the offset value
|
| 949 | * @return object
|
| 950 | */
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 951 | function offset($offset)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 952 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 953 | $this->ar_offset = $offset;
|
| 954 | if ($this->ar_caching === TRUE)
|
| 955 | {
|
| 956 | $this->ar_cache_offset[] = $offset;
|
| 957 | }
|
| 958 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 959 | return $this;
|
| 960 | }
|
| 961 |
|
| 962 | // --------------------------------------------------------------------
|
| 963 |
|
| 964 | /**
|
| 965 | * The "set" function. Allows key/value pairs to be set for inserting or updating
|
| 966 | *
|
| 967 | * @access public
|
| 968 | * @param mixed
|
| 969 | * @param string
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 970 | * @param boolean
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 971 | * @return object
|
| 972 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 973 | function set($key, $value = '', $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 974 | {
|
| 975 | $key = $this->_object_to_array($key);
|
| 976 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 977 | if ( ! is_array($key))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 978 | {
|
| 979 | $key = array($key => $value);
|
| 980 | }
|
| 981 |
|
| 982 | foreach ($key as $k => $v)
|
| 983 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 984 | if ($escape === FALSE)
|
| 985 | {
|
| 986 | $this->ar_set[$this->_protect_identifiers($k)] = $v;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 987 | if ($this->ar_caching === TRUE)
|
| 988 | {
|
| 989 | $this->ar_cache_offset[$this->_protect_identifiers($k)] = $v;
|
| 990 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 991 | }
|
| 992 | else
|
| 993 | {
|
| 994 | $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 995 | if ($this->ar_caching === TRUE)
|
| 996 | {
|
| 997 | $this->ar_cache_offset[$this->_protect_identifiers($k)] = $this->escape($v);
|
| 998 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 999 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1000 | }
|
| 1001 |
|
| 1002 | return $this;
|
| 1003 | }
|
| 1004 |
|
| 1005 | // --------------------------------------------------------------------
|
| 1006 |
|
| 1007 | /**
|
| 1008 | * Get
|
| 1009 | *
|
| 1010 | * Compiles the select statement based on the other functions called
|
| 1011 | * and runs the query
|
| 1012 | *
|
| 1013 | * @access public
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1014 | * @param string the table
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1015 | * @param string the limit clause
|
| 1016 | * @param string the offset clause
|
| 1017 | * @return object
|
| 1018 | */
|
| 1019 | function get($table = '', $limit = null, $offset = null)
|
| 1020 | {
|
| 1021 | if ($table != '')
|
| 1022 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1023 | $this->_track_aliases($table);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1024 | $this->from($table);
|
| 1025 | }
|
| 1026 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1027 | if ( ! is_null($limit))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1028 | {
|
| 1029 | $this->limit($limit, $offset);
|
| 1030 | }
|
| 1031 |
|
| 1032 | $sql = $this->_compile_select();
|
| 1033 |
|
| 1034 | $result = $this->query($sql);
|
| 1035 | $this->_reset_select();
|
| 1036 | return $result;
|
| 1037 | }
|
| 1038 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1039 | /**
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1040 | * "Count All Results" query
|
| 1041 | *
|
| 1042 | * Generates a platform-specific query string that counts all records
|
| 1043 | * returned by an Active Record query.
|
| 1044 | *
|
| 1045 | * @access public
|
| 1046 | * @param string
|
| 1047 | * @return string
|
| 1048 | */
|
| 1049 | function count_all_results($table = '')
|
| 1050 | {
|
| 1051 | if ($table != '')
|
| 1052 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1053 | $this->_track_aliases($table);
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1054 | $this->from($table);
|
| 1055 | }
|
| 1056 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1057 | $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1058 |
|
| 1059 | $query = $this->query($sql);
|
| 1060 | $this->_reset_select();
|
| 1061 |
|
| 1062 | if ($query->num_rows() == 0)
|
| 1063 | {
|
| 1064 | return '0';
|
| 1065 | }
|
| 1066 |
|
| 1067 | $row = $query->row();
|
| 1068 | return $row->numrows;
|
| 1069 | }
|
| 1070 |
|
| 1071 | // --------------------------------------------------------------------
|
| 1072 |
|
| 1073 | /**
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 1074 | * Get_Where
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1075 | *
|
| 1076 | * Allows the where clause, limit and offset to be added directly
|
| 1077 | *
|
| 1078 | * @access public
|
| 1079 | * @param string the where clause
|
| 1080 | * @param string the limit clause
|
| 1081 | * @param string the offset clause
|
| 1082 | * @return object
|
| 1083 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 1084 | function get_where($table = '', $where = null, $limit = null, $offset = null)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1085 | {
|
| 1086 | if ($table != '')
|
| 1087 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1088 | $this->_track_aliases($table);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1089 | $this->from($table);
|
| 1090 | }
|
| 1091 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1092 | if ( ! is_null($where))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1093 | {
|
| 1094 | $this->where($where);
|
| 1095 | }
|
| 1096 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1097 | if ( ! is_null($limit))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1098 | {
|
| 1099 | $this->limit($limit, $offset);
|
| 1100 | }
|
| 1101 |
|
| 1102 | $sql = $this->_compile_select();
|
| 1103 |
|
| 1104 | $result = $this->query($sql);
|
| 1105 | $this->_reset_select();
|
| 1106 | return $result;
|
| 1107 | }
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 1108 |
|
| 1109 | // --------------------------------------------------------------------
|
| 1110 |
|
| 1111 | /**
|
| 1112 | * getwhere() is an alias of get_where()
|
| 1113 | * this function is here for backwards compatibility, as
|
| 1114 | * getwhere() has been deprecated
|
| 1115 | */
|
| 1116 | function getwhere($table = '', $where = null, $limit = null, $offset = null)
|
| 1117 | {
|
| 1118 | return $this->get_where($table, $where, $limit, $offset);
|
| 1119 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1120 |
|
| 1121 | // --------------------------------------------------------------------
|
| 1122 |
|
| 1123 | /**
|
| 1124 | * Insert
|
| 1125 | *
|
| 1126 | * Compiles an insert string and runs the query
|
| 1127 | *
|
| 1128 | * @access public
|
| 1129 | * @param string the table to retrieve the results from
|
| 1130 | * @param array an associative array of insert values
|
| 1131 | * @return object
|
| 1132 | */
|
| 1133 | function insert($table = '', $set = NULL)
|
| 1134 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1135 | if ( ! is_null($set))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1136 | {
|
| 1137 | $this->set($set);
|
| 1138 | }
|
| 1139 |
|
| 1140 | if (count($this->ar_set) == 0)
|
| 1141 | {
|
| 1142 | if ($this->db_debug)
|
| 1143 | {
|
| 1144 | return $this->display_error('db_must_use_set');
|
| 1145 | }
|
| 1146 | return FALSE;
|
| 1147 | }
|
| 1148 |
|
| 1149 | if ($table == '')
|
| 1150 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1151 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1152 | {
|
| 1153 | if ($this->db_debug)
|
| 1154 | {
|
| 1155 | return $this->display_error('db_must_set_table');
|
| 1156 | }
|
| 1157 | return FALSE;
|
| 1158 | }
|
| 1159 |
|
| 1160 | $table = $this->ar_from[0];
|
| 1161 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1162 |
|
| 1163 | $sql = $this->_insert($this->_protect_identifiers($this->dbprefix.$table), array_keys($this->ar_set), array_values($this->ar_set));
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1164 |
|
| 1165 | $this->_reset_write();
|
| 1166 | return $this->query($sql);
|
| 1167 | }
|
| 1168 |
|
| 1169 | // --------------------------------------------------------------------
|
| 1170 |
|
| 1171 | /**
|
| 1172 | * Update
|
| 1173 | *
|
| 1174 | * Compiles an update string and runs the query
|
| 1175 | *
|
| 1176 | * @access public
|
| 1177 | * @param string the table to retrieve the results from
|
| 1178 | * @param array an associative array of update values
|
| 1179 | * @param mixed the where clause
|
| 1180 | * @return object
|
| 1181 | */
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1182 | function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1183 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1184 | if ( ! is_null($set))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1185 | {
|
| 1186 | $this->set($set);
|
| 1187 | }
|
| 1188 |
|
| 1189 | if (count($this->ar_set) == 0)
|
| 1190 | {
|
| 1191 | if ($this->db_debug)
|
| 1192 | {
|
| 1193 | return $this->display_error('db_must_use_set');
|
| 1194 | }
|
| 1195 | return FALSE;
|
| 1196 | }
|
| 1197 |
|
| 1198 | if ($table == '')
|
| 1199 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1200 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1201 | {
|
| 1202 | if ($this->db_debug)
|
| 1203 | {
|
| 1204 | return $this->display_error('db_must_set_table');
|
| 1205 | }
|
| 1206 | return FALSE;
|
| 1207 | }
|
| 1208 |
|
| 1209 | $table = $this->ar_from[0];
|
| 1210 | }
|
| 1211 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1212 | if ($where != NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1213 | {
|
| 1214 | $this->where($where);
|
| 1215 | }
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 1216 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1217 | if ($limit != NULL)
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 1218 | {
|
| 1219 | $this->limit($limit);
|
| 1220 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1221 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1222 | $sql = $this->_update($this->_protect_identifiers($this->dbprefix.$table), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1223 |
|
| 1224 | $this->_reset_write();
|
| 1225 | return $this->query($sql);
|
| 1226 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1227 |
|
| 1228 | // --------------------------------------------------------------------
|
| 1229 |
|
| 1230 | /**
|
| 1231 | * Empty Table
|
| 1232 | *
|
| 1233 | * Compiles a delete string and runs "DELETE FROM table"
|
| 1234 | *
|
| 1235 | * @access public
|
| 1236 | * @param string the table to empty
|
| 1237 | * @return object
|
| 1238 | */
|
| 1239 | function empty_table($table = '')
|
| 1240 | {
|
| 1241 | if ($table == '')
|
| 1242 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1243 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1244 | {
|
| 1245 | if ($this->db_debug)
|
| 1246 | {
|
| 1247 | return $this->display_error('db_must_set_table');
|
| 1248 | }
|
| 1249 | return FALSE;
|
| 1250 | }
|
| 1251 |
|
| 1252 | $table = $this->ar_from[0];
|
| 1253 | }
|
| 1254 | else
|
| 1255 | {
|
| 1256 | $table = $this->_protect_identifiers($this->dbprefix.$table);
|
| 1257 | }
|
| 1258 |
|
| 1259 |
|
| 1260 | $sql = $this->_delete($table);
|
| 1261 |
|
| 1262 | $this->_reset_write();
|
| 1263 |
|
| 1264 | return $this->query($sql);
|
| 1265 | }
|
| 1266 |
|
| 1267 | // --------------------------------------------------------------------
|
| 1268 |
|
| 1269 | /**
|
| 1270 | * Truncate
|
| 1271 | *
|
| 1272 | * Compiles a truncate string and runs the query
|
| 1273 | * If the database does not support the truncate() command
|
| 1274 | * This function maps to "DELETE FROM table"
|
| 1275 | *
|
| 1276 | * @access public
|
| 1277 | * @param string the table to truncate
|
| 1278 | * @return object
|
| 1279 | */
|
| 1280 | function truncate($table = '')
|
| 1281 | {
|
| 1282 | if ($table == '')
|
| 1283 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1284 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1285 | {
|
| 1286 | if ($this->db_debug)
|
| 1287 | {
|
| 1288 | return $this->display_error('db_must_set_table');
|
| 1289 | }
|
| 1290 | return FALSE;
|
| 1291 | }
|
| 1292 |
|
| 1293 | $table = $this->ar_from[0];
|
| 1294 | }
|
| 1295 | else
|
| 1296 | {
|
| 1297 | $table = $this->_protect_identifiers($this->dbprefix.$table);
|
| 1298 | }
|
| 1299 |
|
| 1300 |
|
| 1301 | $sql = $this->_truncate($table);
|
| 1302 |
|
| 1303 | $this->_reset_write();
|
| 1304 |
|
| 1305 | return $this->query($sql);
|
| 1306 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1307 |
|
| 1308 | // --------------------------------------------------------------------
|
| 1309 |
|
| 1310 | /**
|
| 1311 | * Delete
|
| 1312 | *
|
| 1313 | * Compiles a delete string and runs the query
|
| 1314 | *
|
| 1315 | * @access public
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1316 | * @param mixed the table(s) to delete from. String or array
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1317 | * @param mixed the where clause
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1318 | * @param mixed the limit clause
|
| 1319 | * @param boolean
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1320 | * @return object
|
| 1321 | */
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1322 | function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1323 | {
|
| 1324 | if ($table == '')
|
| 1325 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1326 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1327 | {
|
| 1328 | if ($this->db_debug)
|
| 1329 | {
|
| 1330 | return $this->display_error('db_must_set_table');
|
| 1331 | }
|
| 1332 | return FALSE;
|
| 1333 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1334 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1335 | $table = $this->ar_from[0];
|
| 1336 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1337 | elseif (is_array($table))
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1338 | {
|
| 1339 | foreach($table as $single_table)
|
| 1340 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1341 | $this->delete($single_table, $where, $limit, FALSE);
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1342 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1343 |
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1344 | $this->_reset_write();
|
| 1345 | return;
|
| 1346 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1347 | else
|
| 1348 | {
|
| 1349 | $table = $this->_protect_identifiers($this->dbprefix.$table);
|
| 1350 | }
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1351 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1352 | if ($where != '')
|
| 1353 | {
|
| 1354 | $this->where($where);
|
| 1355 | }
|
| 1356 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1357 | if ($limit != NULL)
|
| 1358 | {
|
| 1359 | $this->limit($limit);
|
| 1360 | }
|
| 1361 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1362 | if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1363 | {
|
| 1364 | if ($this->db_debug)
|
| 1365 | {
|
| 1366 | return $this->display_error('db_del_must_use_where');
|
| 1367 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1368 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1369 | return FALSE;
|
| 1370 | }
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1371 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1372 | $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1373 |
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1374 | if ($reset_data)
|
| 1375 | {
|
| 1376 | $this->_reset_write();
|
| 1377 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1378 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1379 | return $this->query($sql);
|
| 1380 | }
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 1381 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1382 | // --------------------------------------------------------------------
|
| 1383 |
|
| 1384 | /**
|
| 1385 | * Use Table - DEPRECATED
|
| 1386 | *
|
| 1387 | * @deprecated use $this->db->from instead
|
| 1388 | */
|
| 1389 | function use_table($table)
|
| 1390 | {
|
| 1391 | return $this->from($table);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1392 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1393 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1394 | // --------------------------------------------------------------------
|
| 1395 |
|
| 1396 | /**
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1397 | * Track Aliases
|
| 1398 | *
|
| 1399 | * Used to track SQL statements written with aliased tables.
|
| 1400 | *
|
| 1401 | * @access private
|
| 1402 | * @param string The table to inspect
|
| 1403 | * @return string
|
| 1404 | */
|
| 1405 | function _track_aliases($table)
|
| 1406 | {
|
| 1407 | // if a table alias is used we can recognize it by a space
|
| 1408 | if (strpos($table, " ") !== FALSE)
|
| 1409 | {
|
| 1410 | // if the alias is written with the AS keyowrd, get it out
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1411 | $table = preg_replace('/ AS /i', ' ', $table);
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1412 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1413 | $this->ar_aliased_tables[] = trim(strrchr($table, " "));
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1414 | }
|
| 1415 |
|
| 1416 | return $this->dbprefix.$table;
|
| 1417 | }
|
| 1418 |
|
| 1419 | // --------------------------------------------------------------------
|
| 1420 |
|
| 1421 | /**
|
| 1422 | * Filter Table Aliases
|
| 1423 | *
|
| 1424 | * Intelligently removes database prefixes from aliased tables
|
| 1425 | *
|
| 1426 | * @access private
|
| 1427 | * @param array An array of compiled SQL
|
| 1428 | * @return array Cleaned up statement with aliases accounted for
|
| 1429 | */
|
| 1430 | function _filter_table_aliases($statements)
|
| 1431 | {
|
Derek Allard | 8b2f19b | 2008-03-06 15:51:55 +0000 | [diff] [blame] | 1432 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1433 | foreach ($statements as $k => $v)
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1434 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1435 | foreach ($this->ar_aliased_tables as $table)
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1436 | {
|
Derek Allard | 8b2f19b | 2008-03-06 15:51:55 +0000 | [diff] [blame] | 1437 | $statements[$k] = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $statements[$k]); // makes `table.field`
|
| 1438 | $statements[$k] = str_replace($this->dbprefix.$table.'.', $table.'.', $statements[$k]);
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1439 | }
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1440 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1441 | return $statements;
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1442 | }
|
| 1443 |
|
| 1444 | // --------------------------------------------------------------------
|
| 1445 |
|
| 1446 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1447 | * Compile the SELECT statement
|
| 1448 | *
|
| 1449 | * Generates a query string based on which functions were used.
|
| 1450 | * Should not be called directly. The get() function calls it.
|
| 1451 | *
|
| 1452 | * @access private
|
| 1453 | * @return string
|
| 1454 | */
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1455 | function _compile_select($select_override = FALSE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1456 | {
|
Derek Allard | 7825526 | 2008-02-06 13:54:23 +0000 | [diff] [blame] | 1457 | $this->_merge_cache();
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1458 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1459 | $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1460 |
|
Derek Allard | 5162fc7 | 2008-04-15 20:05:37 +0000 | [diff] [blame] | 1461 | $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->_filter_table_aliases($this->ar_select));
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1462 |
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1463 | if ($select_override !== FALSE)
|
| 1464 | {
|
| 1465 | $sql = $select_override;
|
| 1466 | }
|
| 1467 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1468 | if (count($this->ar_from) > 0)
|
| 1469 | {
|
| 1470 | $sql .= "\nFROM ";
|
Derek Jones | c6ad023 | 2008-01-29 18:44:54 +0000 | [diff] [blame] | 1471 | $sql .= $this->_from_tables($this->ar_from);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1472 | }
|
| 1473 |
|
| 1474 | if (count($this->ar_join) > 0)
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1475 | {
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1476 | $sql .= "\n";
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1477 |
|
| 1478 | // special consideration for table aliases
|
| 1479 | if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
|
| 1480 | {
|
| 1481 | $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
|
| 1482 | }
|
| 1483 | else
|
| 1484 | {
|
| 1485 | $sql .= implode("\n", $this->ar_join);
|
| 1486 | }
|
| 1487 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1488 | }
|
| 1489 |
|
| 1490 | if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
|
| 1491 | {
|
| 1492 | $sql .= "\nWHERE ";
|
| 1493 | }
|
| 1494 |
|
| 1495 | $sql .= implode("\n", $this->ar_where);
|
| 1496 |
|
| 1497 | if (count($this->ar_like) > 0)
|
| 1498 | {
|
| 1499 | if (count($this->ar_where) > 0)
|
| 1500 | {
|
| 1501 | $sql .= " AND ";
|
| 1502 | }
|
| 1503 |
|
| 1504 | $sql .= implode("\n", $this->ar_like);
|
| 1505 | }
|
| 1506 |
|
| 1507 | if (count($this->ar_groupby) > 0)
|
| 1508 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1509 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1510 | $sql .= "\nGROUP BY ";
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1511 |
|
| 1512 | // special consideration for table aliases
|
| 1513 | if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
|
| 1514 | {
|
| 1515 | $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
|
| 1516 | }
|
| 1517 | else
|
| 1518 | {
|
| 1519 | $sql .= implode(', ', $this->ar_groupby);
|
| 1520 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1521 | }
|
| 1522 |
|
| 1523 | if (count($this->ar_having) > 0)
|
| 1524 | {
|
| 1525 | $sql .= "\nHAVING ";
|
| 1526 | $sql .= implode("\n", $this->ar_having);
|
| 1527 | }
|
| 1528 |
|
| 1529 | if (count($this->ar_orderby) > 0)
|
| 1530 | {
|
| 1531 | $sql .= "\nORDER BY ";
|
| 1532 | $sql .= implode(', ', $this->ar_orderby);
|
| 1533 |
|
| 1534 | if ($this->ar_order !== FALSE)
|
| 1535 | {
|
| 1536 | $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
|
| 1537 | }
|
| 1538 | }
|
| 1539 |
|
| 1540 | if (is_numeric($this->ar_limit))
|
| 1541 | {
|
| 1542 | $sql .= "\n";
|
| 1543 | $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
|
| 1544 | }
|
| 1545 |
|
| 1546 | return $sql;
|
| 1547 | }
|
| 1548 |
|
| 1549 | // --------------------------------------------------------------------
|
| 1550 |
|
| 1551 | /**
|
| 1552 | * Object to Array
|
| 1553 | *
|
| 1554 | * Takes an object as input and converts the class variables to array key/vals
|
| 1555 | *
|
| 1556 | * @access public
|
| 1557 | * @param object
|
| 1558 | * @return array
|
| 1559 | */
|
| 1560 | function _object_to_array($object)
|
| 1561 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1562 | if ( ! is_object($object))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1563 | {
|
| 1564 | return $object;
|
| 1565 | }
|
| 1566 |
|
| 1567 | $array = array();
|
| 1568 | foreach (get_object_vars($object) as $key => $val)
|
| 1569 | {
|
Derek Allard | 848b776 | 2007-12-31 16:43:05 +0000 | [diff] [blame] | 1570 | // There are some built in keys we need to ignore for this conversion
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1571 | if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
|
Derek Allard | 848b776 | 2007-12-31 16:43:05 +0000 | [diff] [blame] | 1572 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1573 | {
|
| 1574 | $array[$key] = $val;
|
| 1575 | }
|
| 1576 | }
|
| 1577 |
|
| 1578 | return $array;
|
| 1579 | }
|
| 1580 |
|
| 1581 | // --------------------------------------------------------------------
|
| 1582 |
|
| 1583 | /**
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1584 | * Start Cache
|
| 1585 | *
|
| 1586 | * Starts AR caching
|
| 1587 | *
|
| 1588 | * @access public
|
| 1589 | * @return void
|
| 1590 | */
|
| 1591 | function start_cache()
|
| 1592 | {
|
| 1593 | $this->ar_caching = TRUE;
|
| 1594 | }
|
| 1595 |
|
| 1596 | // --------------------------------------------------------------------
|
| 1597 |
|
| 1598 | /**
|
| 1599 | * Stop Cache
|
| 1600 | *
|
| 1601 | * Stops AR caching
|
| 1602 | *
|
| 1603 | * @access public
|
| 1604 | * @return void
|
| 1605 | */
|
| 1606 | function stop_cache()
|
| 1607 | {
|
| 1608 | $this->ar_caching = FALSE;
|
| 1609 | }
|
| 1610 |
|
| 1611 |
|
| 1612 | // --------------------------------------------------------------------
|
| 1613 |
|
| 1614 | /**
|
| 1615 | * Flush Cache
|
| 1616 | *
|
| 1617 | * Empties the AR cache
|
| 1618 | *
|
| 1619 | * @access public
|
| 1620 | * @return void
|
| 1621 | */
|
| 1622 | function flush_cache()
|
| 1623 | {
|
| 1624 | $ar_reset_items = array(
|
| 1625 | 'ar_cache_select' => array(),
|
| 1626 | 'ar_cache_from' => array(),
|
| 1627 | 'ar_cache_join' => array(),
|
| 1628 | 'ar_cache_where' => array(),
|
| 1629 | 'ar_cache_like' => array(),
|
| 1630 | 'ar_cache_groupby' => array(),
|
| 1631 | 'ar_cache_having' =>array(),
|
| 1632 | 'ar_cache_orderby' => array(),
|
| 1633 | 'ar_cache_set' => array()
|
| 1634 | );
|
| 1635 |
|
| 1636 | $this->_reset_run($ar_reset_items);
|
| 1637 | }
|
| 1638 |
|
| 1639 | // --------------------------------------------------------------------
|
| 1640 |
|
| 1641 | /**
|
| 1642 | * Merge Cache
|
| 1643 | *
|
| 1644 | * When called, this function merges any cached AR arrays with
|
| 1645 | * locally called ones.
|
| 1646 | *
|
| 1647 | * @access private
|
| 1648 | * @return void
|
| 1649 | */
|
| 1650 | function _merge_cache()
|
| 1651 | {
|
| 1652 | $ar_items = array('select', 'from', 'join', 'where', 'like', 'groupby', 'having', 'orderby', 'set');
|
| 1653 |
|
| 1654 | foreach ($ar_items as $ar_item)
|
| 1655 | {
|
| 1656 | $ar_cache_item = 'ar_cache_'.$ar_item;
|
| 1657 | $ar_item = 'ar_'.$ar_item;
|
| 1658 | $this->$ar_item = array_unique(array_merge($this->$ar_item, $this->$ar_cache_item));
|
| 1659 | }
|
| 1660 | }
|
| 1661 |
|
| 1662 | // --------------------------------------------------------------------
|
| 1663 |
|
| 1664 | /**
|
| 1665 | * Resets the active record values. Called by the get() function
|
| 1666 | *
|
| 1667 | * @access private
|
| 1668 | * @param array An array of fields to reset
|
| 1669 | * @return void
|
| 1670 | */
|
| 1671 | function _reset_run($ar_reset_items)
|
| 1672 | {
|
| 1673 | foreach ($ar_reset_items as $item => $default_value)
|
| 1674 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1675 | if ( ! in_array($item, $this->ar_store_array))
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1676 | {
|
| 1677 | $this->$item = $default_value;
|
| 1678 | }
|
| 1679 | }
|
| 1680 | }
|
| 1681 |
|
| 1682 | // --------------------------------------------------------------------
|
| 1683 |
|
| 1684 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1685 | * Resets the active record values. Called by the get() function
|
| 1686 | *
|
| 1687 | * @access private
|
| 1688 | * @return void
|
| 1689 | */
|
| 1690 | function _reset_select()
|
| 1691 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1692 | $ar_reset_items = array(
|
| 1693 | 'ar_select' => array(),
|
| 1694 | 'ar_from' => array(),
|
| 1695 | 'ar_join' => array(),
|
| 1696 | 'ar_where' => array(),
|
| 1697 | 'ar_like' => array(),
|
| 1698 | 'ar_groupby' => array(),
|
| 1699 | 'ar_having' => array(),
|
| 1700 | 'ar_orderby' => array(),
|
| 1701 | 'ar_wherein' => array(),
|
| 1702 | 'ar_aliased_tables' => array(),
|
| 1703 | 'ar_distinct' => FALSE,
|
| 1704 | 'ar_limit' => FALSE,
|
| 1705 | 'ar_offset' => FALSE,
|
| 1706 | 'ar_order' => FALSE,
|
| 1707 | );
|
| 1708 |
|
| 1709 | $this->_reset_run($ar_reset_items);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1710 | }
|
| 1711 |
|
| 1712 | // --------------------------------------------------------------------
|
| 1713 |
|
| 1714 | /**
|
| 1715 | * Resets the active record "write" values.
|
| 1716 | *
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1717 | * Called by the insert() update() and delete() functions
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1718 | *
|
| 1719 | * @access private
|
| 1720 | * @return void
|
| 1721 | */
|
| 1722 | function _reset_write()
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1723 | {
|
| 1724 | $ar_reset_items = array(
|
| 1725 | 'ar_set' => array(),
|
| 1726 | 'ar_from' => array(),
|
| 1727 | 'ar_where' => array(),
|
| 1728 | 'ar_like' => array(),
|
| 1729 | 'ar_orderby' => array(),
|
| 1730 | 'ar_limit' => FALSE,
|
| 1731 | 'ar_order' => FALSE
|
| 1732 | );
|
| 1733 |
|
| 1734 | $this->_reset_run($ar_reset_items);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1735 | }
|
| 1736 |
|
| 1737 | }
|
Derek Allard | a632589 | 2008-05-12 17:51:47 +0000 | [diff] [blame] | 1738 |
|
| 1739 | /* End of file DB_active_rec.php */
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 1740 | /* Location: ./system/database/DB_active_rec.php */ |