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
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, 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 Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 430 | if ( ! $this->_has_operator($k) && is_null($key[$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 |
|
| 439 | if ($escape === TRUE)
|
| 440 | {
|
| 441 | // exception for "field<=" keys
|
| 442 | if ($this->_has_operator($k))
|
| 443 | {
|
| 444 | $k = preg_replace("/([A-Za-z_0-9]+)/", $this->_protect_identifiers('$1'), $k);
|
| 445 | }
|
| 446 | else
|
| 447 | {
|
| 448 | $k = $this->_protect_identifiers($k);
|
| 449 | }
|
| 450 | }
|
| 451 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 452 | if ( ! $this->_has_operator($k))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 453 | {
|
| 454 | $k .= ' =';
|
| 455 | }
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 456 |
|
Derek Allard | 16629b1 | 2008-04-06 19:58:20 +0000 | [diff] [blame] | 457 | if ($v !== '' AND $v !== NULL)
|
Derek Allard | 96863ce | 2008-04-06 19:20:17 +0000 | [diff] [blame] | 458 | {
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 459 | if ($escape === TRUE)
|
| 460 | {
|
| 461 | $v = ' '.$this->escape($v);
|
| 462 | }
|
Derek Allard | 96863ce | 2008-04-06 19:20:17 +0000 | [diff] [blame] | 463 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 464 | }
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 465 | else
|
| 466 | {
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 467 |
|
Derek Allard | 16629b1 | 2008-04-06 19:58:20 +0000 | [diff] [blame] | 468 | if ($escape === TRUE)
|
| 469 | {
|
| 470 | $k = $this->_protect_identifiers($k, TRUE);
|
| 471 | }
|
| 472 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 473 | }
|
| 474 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 475 | $this->ar_where[] = $prefix.$k.$v;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 476 | if ($this->ar_caching === TRUE)
|
| 477 | {
|
| 478 | $this->ar_cache_where[] = $prefix.$k.$v;
|
| 479 | }
|
| 480 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 481 | }
|
| 482 | return $this;
|
| 483 | }
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 484 |
|
| 485 | // --------------------------------------------------------------------
|
| 486 |
|
| 487 | /**
|
| 488 | * Where_in
|
| 489 | *
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 490 | * Generates a WHERE field IN ('item', 'item') SQL query joined with
|
| 491 | * AND if appropriate
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 492 | *
|
| 493 | * @access public
|
| 494 | * @param string The field to search
|
| 495 | * @param array The values searched on
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 496 | * @return object
|
| 497 | */
|
| 498 | function where_in($key = NULL, $values = NULL)
|
| 499 | {
|
| 500 | return $this->_where_in($key, $values);
|
| 501 | }
|
| 502 |
|
| 503 | // --------------------------------------------------------------------
|
| 504 |
|
| 505 | /**
|
| 506 | * Where_in_or
|
| 507 | *
|
| 508 | * Generates a WHERE field IN ('item', 'item') SQL query joined with
|
| 509 | * OR if appropriate
|
| 510 | *
|
| 511 | * @access public
|
| 512 | * @param string The field to search
|
| 513 | * @param array The values searched on
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 514 | * @return object
|
| 515 | */
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 516 | function or_where_in($key = NULL, $values = NULL)
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 517 | {
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 518 | return $this->_where_in($key, $values, FALSE, 'OR ');
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 519 | }
|
| 520 |
|
| 521 | // --------------------------------------------------------------------
|
| 522 |
|
| 523 | /**
|
| 524 | * Where_not_in
|
| 525 | *
|
| 526 | * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
|
| 527 | * with AND if appropriate
|
| 528 | *
|
| 529 | * @access public
|
| 530 | * @param string The field to search
|
| 531 | * @param array The values searched on
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 532 | * @return object
|
| 533 | */
|
| 534 | function where_not_in($key = NULL, $values = NULL)
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 535 | {
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 536 | return $this->_where_in($key, $values, TRUE);
|
| 537 | }
|
| 538 |
|
| 539 | // --------------------------------------------------------------------
|
| 540 |
|
| 541 | /**
|
| 542 | * Where_not_in_or
|
| 543 | *
|
| 544 | * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
|
| 545 | * with OR if appropriate
|
| 546 | *
|
| 547 | * @access public
|
| 548 | * @param string The field to search
|
| 549 | * @param array The values searched on
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 550 | * @return object
|
| 551 | */
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 552 | function or_where_not_in($key = NULL, $values = NULL)
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 553 | {
|
Derek Jones | d007243 | 2008-05-07 22:06:51 +0000 | [diff] [blame] | 554 | return $this->_where_in($key, $values, TRUE, 'OR ');
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 555 | }
|
| 556 |
|
| 557 | // --------------------------------------------------------------------
|
| 558 |
|
| 559 | /**
|
| 560 | * Where_in
|
| 561 | *
|
| 562 | * Called by where_in, where_in_or, where_not_in, where_not_in_or
|
| 563 | *
|
| 564 | * @access public
|
| 565 | * @param string The field to search
|
| 566 | * @param array The values searched on
|
| 567 | * @param boolean If the statement whould be IN or NOT IN
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 568 | * @param string
|
| 569 | * @return object
|
| 570 | */
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 571 | function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 572 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 573 | if ($key === NULL OR ! is_array($values))
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 574 | {
|
| 575 | return;
|
| 576 | }
|
| 577 |
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 578 | $not = ($not) ? ' NOT ' : '';
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 579 |
|
| 580 | foreach ($values as $value)
|
| 581 | {
|
| 582 | $this->ar_wherein[] = $this->escape($value);
|
| 583 | }
|
| 584 |
|
| 585 | $prefix = (count($this->ar_where) == 0) ? '' : $type;
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 586 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 587 | $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
|
| 588 |
|
| 589 | $this->ar_where[] = $where_in;
|
| 590 | if ($this->ar_caching === TRUE)
|
| 591 | {
|
| 592 | $this->ar_cache_where[] = $where_in;
|
| 593 | }
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 594 |
|
Derek Allard | 8f00021 | 2008-01-18 14:45:59 +0000 | [diff] [blame] | 595 | // reset the array for multiple calls
|
| 596 | $this->ar_wherein = array();
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 597 | return $this;
|
| 598 | }
|
| 599 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 600 | // --------------------------------------------------------------------
|
| 601 |
|
| 602 | /**
|
| 603 | * Like
|
| 604 | *
|
| 605 | * Generates a %LIKE% portion of the query. Separates
|
| 606 | * multiple calls with AND
|
| 607 | *
|
| 608 | * @access public
|
| 609 | * @param mixed
|
| 610 | * @param mixed
|
| 611 | * @return object
|
| 612 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 613 | function like($field, $match = '', $side = 'both')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 614 | {
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 615 | return $this->_like($field, $match, 'AND ', $side);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 616 | }
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 617 |
|
| 618 | // --------------------------------------------------------------------
|
| 619 |
|
| 620 | /**
|
| 621 | * Not Like
|
| 622 | *
|
| 623 | * Generates a NOT LIKE portion of the query. Separates
|
| 624 | * multiple calls with AND
|
| 625 | *
|
| 626 | * @access public
|
| 627 | * @param mixed
|
| 628 | * @param mixed
|
| 629 | * @return object
|
| 630 | */
|
| 631 | function not_like($field, $match = '', $side = 'both')
|
| 632 | {
|
| 633 | return $this->_like($field, $match, 'AND ', $side, ' NOT');
|
| 634 | }
|
| 635 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 636 | // --------------------------------------------------------------------
|
| 637 |
|
| 638 | /**
|
| 639 | * OR Like
|
| 640 | *
|
| 641 | * Generates a %LIKE% portion of the query. Separates
|
| 642 | * multiple calls with OR
|
| 643 | *
|
| 644 | * @access public
|
| 645 | * @param mixed
|
| 646 | * @param mixed
|
| 647 | * @return object
|
| 648 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 649 | function or_like($field, $match = '', $side = 'both')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 650 | {
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 651 | return $this->_like($field, $match, 'OR ', $side);
|
| 652 | }
|
| 653 |
|
| 654 | // --------------------------------------------------------------------
|
| 655 |
|
| 656 | /**
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 657 | * OR Not Like
|
| 658 | *
|
| 659 | * Generates a NOT LIKE portion of the query. Separates
|
| 660 | * multiple calls with OR
|
| 661 | *
|
| 662 | * @access public
|
| 663 | * @param mixed
|
| 664 | * @param mixed
|
| 665 | * @return object
|
| 666 | */
|
| 667 | function or_not_like($field, $match = '', $side = 'both')
|
| 668 | {
|
| 669 | return $this->_like($field, $match, 'OR ', $side, 'NOT ');
|
| 670 | }
|
| 671 |
|
| 672 | // --------------------------------------------------------------------
|
| 673 |
|
| 674 | /**
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 675 | * orlike() is an alias of or_like()
|
| 676 | * this function is here for backwards compatibility, as
|
| 677 | * orlike() has been deprecated
|
| 678 | */
|
| 679 | function orlike($field, $match = '', $side = 'both')
|
| 680 | {
|
Derek Allard | 4a310b7 | 2008-01-30 21:32:47 +0000 | [diff] [blame] | 681 | return $this->or_like($field, $match, $side);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 682 | }
|
| 683 |
|
| 684 | // --------------------------------------------------------------------
|
| 685 |
|
| 686 | /**
|
| 687 | * Like
|
| 688 | *
|
| 689 | * Called by like() or orlike()
|
| 690 | *
|
| 691 | * @access private
|
| 692 | * @param mixed
|
| 693 | * @param mixed
|
| 694 | * @param string
|
| 695 | * @return object
|
| 696 | */
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 697 | function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 698 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 699 | if ( ! is_array($field))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 700 | {
|
| 701 | $field = array($field => $match);
|
| 702 | }
|
| 703 |
|
| 704 | foreach ($field as $k => $v)
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 705 | {
|
| 706 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 707 | $k = $this->_protect_identifiers($k);
|
| 708 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 709 | $prefix = (count($this->ar_like) == 0) ? '' : $type;
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 710 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 711 | $v = $this->escape_str($v);
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 712 |
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 713 | if ($side == 'before')
|
| 714 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 715 | $like_statement = $prefix." $k $not LIKE '%{$v}'";
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 716 | }
|
| 717 | elseif ($side == 'after')
|
| 718 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 719 | $like_statement = $prefix." $k $not LIKE '{$v}%'";
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 720 | }
|
| 721 | else
|
| 722 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 723 | $like_statement = $prefix." $k $not LIKE '%{$v}%'";
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 724 | }
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 725 |
|
| 726 | $this->ar_like[] = $like_statement;
|
| 727 | if ($this->ar_caching === TRUE)
|
| 728 | {
|
| 729 | $this->ar_cache_like[] = $like_statement;
|
| 730 | }
|
| 731 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 732 | }
|
| 733 | return $this;
|
| 734 | }
|
| 735 |
|
| 736 | // --------------------------------------------------------------------
|
| 737 |
|
| 738 | /**
|
| 739 | * GROUP BY
|
| 740 | *
|
| 741 | * @access public
|
| 742 | * @param string
|
| 743 | * @return object
|
| 744 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 745 | function group_by($by)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 746 | {
|
| 747 | if (is_string($by))
|
| 748 | {
|
| 749 | $by = explode(',', $by);
|
| 750 | }
|
| 751 |
|
| 752 | foreach ($by as $val)
|
| 753 | {
|
| 754 | $val = trim($val);
|
| 755 |
|
| 756 | if ($val != '')
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 757 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 758 | $this->ar_groupby[] = $this->_protect_identifiers($val);
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 759 | if ($this->ar_caching === TRUE)
|
| 760 | {
|
| 761 | $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
|
| 762 | }
|
| 763 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 764 | }
|
| 765 | return $this;
|
| 766 | }
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 767 |
|
| 768 | // --------------------------------------------------------------------
|
| 769 |
|
| 770 | /**
|
| 771 | * groupby() is an alias of group_by()
|
| 772 | * this function is here for backwards compatibility, as
|
| 773 | * groupby() has been deprecated
|
| 774 | */
|
| 775 | function groupby($by)
|
| 776 | {
|
| 777 | return $this->group_by($by);
|
| 778 | }
|
| 779 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 780 | // --------------------------------------------------------------------
|
| 781 |
|
| 782 | /**
|
| 783 | * Sets the HAVING value
|
| 784 | *
|
| 785 | * Separates multiple calls with AND
|
| 786 | *
|
| 787 | * @access public
|
| 788 | * @param string
|
| 789 | * @param string
|
| 790 | * @return object
|
| 791 | */
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 792 | function having($key, $value = '', $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 793 | {
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 794 | return $this->_having($key, $value, 'AND ', $escape);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 795 | }
|
Derek Allard | 43e8cbf | 2008-03-01 23:14:43 +0000 | [diff] [blame] | 796 |
|
| 797 | // --------------------------------------------------------------------
|
| 798 |
|
| 799 | /**
|
| 800 | * orhaving() is an alias of or_having()
|
| 801 | * this function is here for backwards compatibility, as
|
| 802 | * orhaving() has been deprecated
|
| 803 | */
|
| 804 |
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 805 | function orhaving($key, $value = '', $escape = TRUE)
|
Derek Allard | 43e8cbf | 2008-03-01 23:14:43 +0000 | [diff] [blame] | 806 | {
|
Derek Allard | a459b46 | 2008-05-22 13:01:39 +0000 | [diff] [blame] | 807 | return $this->or_having($key, $value, $escape);
|
Derek Allard | 43e8cbf | 2008-03-01 23:14:43 +0000 | [diff] [blame] | 808 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 809 | // --------------------------------------------------------------------
|
| 810 |
|
| 811 | /**
|
| 812 | * Sets the OR HAVING value
|
| 813 | *
|
| 814 | * Separates multiple calls with OR
|
| 815 | *
|
| 816 | * @access public
|
| 817 | * @param string
|
| 818 | * @param string
|
| 819 | * @return object
|
| 820 | */
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 821 | function or_having($key, $value = '', $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 822 | {
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 823 | return $this->_having($key, $value, 'OR ', $escape);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 824 | }
|
| 825 |
|
| 826 | // --------------------------------------------------------------------
|
| 827 |
|
| 828 | /**
|
| 829 | * Sets the HAVING values
|
| 830 | *
|
| 831 | * Called by having() or orhaving()
|
| 832 | *
|
| 833 | * @access private
|
| 834 | * @param string
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 835 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 836 | * @param string
|
| 837 | * @return object
|
| 838 | */
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 839 | function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 840 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 841 | if ( ! is_array($key))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 842 | {
|
| 843 | $key = array($key => $value);
|
| 844 | }
|
| 845 |
|
| 846 | foreach ($key as $k => $v)
|
| 847 | {
|
| 848 | $prefix = (count($this->ar_having) == 0) ? '' : $type;
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 849 |
|
| 850 | if ($escape === TRUE)
|
| 851 | {
|
| 852 | $k = $this->_protect_identifiers($k);
|
| 853 | }
|
| 854 |
|
Derek Allard | a459b46 | 2008-05-22 13:01:39 +0000 | [diff] [blame] | 855 | if ( ! $this->_has_operator($k))
|
| 856 | {
|
| 857 | $k .= ' = ';
|
| 858 | }
|
| 859 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 860 | if ($v != '')
|
| 861 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 862 | $v = ' '.$this->escape_str($v);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 863 | }
|
| 864 |
|
| 865 | $this->ar_having[] = $prefix.$k.$v;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 866 | if ($this->ar_caching === TRUE)
|
| 867 | {
|
| 868 | $this->ar_cache_having[] = $prefix.$k.$v;
|
| 869 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 870 | }
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 871 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 872 | return $this;
|
| 873 | }
|
| 874 |
|
| 875 | // --------------------------------------------------------------------
|
| 876 |
|
| 877 | /**
|
| 878 | * Sets the ORDER BY value
|
| 879 | *
|
| 880 | * @access public
|
| 881 | * @param string
|
| 882 | * @param string direction: asc or desc
|
| 883 | * @return object
|
| 884 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 885 | function order_by($orderby, $direction = '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 886 | {
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 887 | if (strtolower($direction) == 'random')
|
| 888 | {
|
| 889 | $orderby = ''; // Random results want or don't need a field name
|
| 890 | $direction = $this->_random_keyword;
|
| 891 | }
|
| 892 | elseif (trim($direction) != '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 893 | {
|
Derek Allard | 9278249 | 2007-08-10 11:26:01 +0000 | [diff] [blame] | 894 | $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 895 | }
|
| 896 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 897 | $orderby_statement = $this->_protect_identifiers($orderby, TRUE).$direction;
|
| 898 |
|
| 899 | $this->ar_orderby[] = $orderby_statement;
|
| 900 | if ($this->ar_caching === TRUE)
|
| 901 | {
|
| 902 | $this->ar_cache_orderby[] = $orderby_statement;
|
| 903 | }
|
| 904 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 905 | return $this;
|
| 906 | }
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 907 |
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 908 | // --------------------------------------------------------------------
|
| 909 |
|
| 910 | /**
|
| 911 | * orderby() is an alias of order_by()
|
| 912 | * this function is here for backwards compatibility, as
|
| 913 | * orderby() has been deprecated
|
| 914 | */
|
| 915 | function orderby($orderby, $direction = '')
|
| 916 | {
|
| 917 | return $this->order_by($orderby, $direction);
|
| 918 | }
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 919 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 920 | // --------------------------------------------------------------------
|
| 921 |
|
| 922 | /**
|
| 923 | * Sets the LIMIT value
|
| 924 | *
|
| 925 | * @access public
|
| 926 | * @param integer the limit value
|
| 927 | * @param integer the offset value
|
| 928 | * @return object
|
| 929 | */
|
| 930 | function limit($value, $offset = '')
|
| 931 | {
|
| 932 | $this->ar_limit = $value;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 933 | if ($this->ar_caching === TRUE)
|
| 934 | {
|
| 935 | $this->ar_cache_limit[] = $value;
|
| 936 | }
|
| 937 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 938 | if ($offset != '')
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 939 | {
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 940 | $this->ar_offset = $offset;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 941 | if ($this->ar_caching === TRUE)
|
| 942 | {
|
| 943 | $this->ar_cache_offset[] = $offset;
|
| 944 | }
|
| 945 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 946 |
|
| 947 | return $this;
|
| 948 | }
|
| 949 |
|
| 950 | // --------------------------------------------------------------------
|
| 951 |
|
| 952 | /**
|
| 953 | * Sets the OFFSET value
|
| 954 | *
|
| 955 | * @access public
|
| 956 | * @param integer the offset value
|
| 957 | * @return object
|
| 958 | */
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 959 | function offset($offset)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 960 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 961 | $this->ar_offset = $offset;
|
| 962 | if ($this->ar_caching === TRUE)
|
| 963 | {
|
| 964 | $this->ar_cache_offset[] = $offset;
|
| 965 | }
|
| 966 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 967 | return $this;
|
| 968 | }
|
| 969 |
|
| 970 | // --------------------------------------------------------------------
|
| 971 |
|
| 972 | /**
|
| 973 | * The "set" function. Allows key/value pairs to be set for inserting or updating
|
| 974 | *
|
| 975 | * @access public
|
| 976 | * @param mixed
|
| 977 | * @param string
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 978 | * @param boolean
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 979 | * @return object
|
| 980 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 981 | function set($key, $value = '', $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 982 | {
|
| 983 | $key = $this->_object_to_array($key);
|
| 984 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 985 | if ( ! is_array($key))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 986 | {
|
| 987 | $key = array($key => $value);
|
| 988 | }
|
| 989 |
|
| 990 | foreach ($key as $k => $v)
|
| 991 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 992 | if ($escape === FALSE)
|
| 993 | {
|
| 994 | $this->ar_set[$this->_protect_identifiers($k)] = $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)] = $v;
|
| 998 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 999 | }
|
| 1000 | else
|
| 1001 | {
|
| 1002 | $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1003 | if ($this->ar_caching === TRUE)
|
| 1004 | {
|
| 1005 | $this->ar_cache_offset[$this->_protect_identifiers($k)] = $this->escape($v);
|
| 1006 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1007 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1008 | }
|
| 1009 |
|
| 1010 | return $this;
|
| 1011 | }
|
| 1012 |
|
| 1013 | // --------------------------------------------------------------------
|
| 1014 |
|
| 1015 | /**
|
| 1016 | * Get
|
| 1017 | *
|
| 1018 | * Compiles the select statement based on the other functions called
|
| 1019 | * and runs the query
|
| 1020 | *
|
| 1021 | * @access public
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1022 | * @param string the table
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1023 | * @param string the limit clause
|
| 1024 | * @param string the offset clause
|
| 1025 | * @return object
|
| 1026 | */
|
| 1027 | function get($table = '', $limit = null, $offset = null)
|
| 1028 | {
|
| 1029 | if ($table != '')
|
| 1030 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1031 | $this->_track_aliases($table);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1032 | $this->from($table);
|
| 1033 | }
|
| 1034 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1035 | if ( ! is_null($limit))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1036 | {
|
| 1037 | $this->limit($limit, $offset);
|
| 1038 | }
|
| 1039 |
|
| 1040 | $sql = $this->_compile_select();
|
| 1041 |
|
| 1042 | $result = $this->query($sql);
|
| 1043 | $this->_reset_select();
|
| 1044 | return $result;
|
| 1045 | }
|
| 1046 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1047 | /**
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1048 | * "Count All Results" query
|
| 1049 | *
|
| 1050 | * Generates a platform-specific query string that counts all records
|
| 1051 | * returned by an Active Record query.
|
| 1052 | *
|
| 1053 | * @access public
|
| 1054 | * @param string
|
| 1055 | * @return string
|
| 1056 | */
|
| 1057 | function count_all_results($table = '')
|
| 1058 | {
|
| 1059 | if ($table != '')
|
| 1060 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1061 | $this->_track_aliases($table);
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1062 | $this->from($table);
|
| 1063 | }
|
| 1064 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1065 | $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1066 |
|
| 1067 | $query = $this->query($sql);
|
| 1068 | $this->_reset_select();
|
| 1069 |
|
| 1070 | if ($query->num_rows() == 0)
|
| 1071 | {
|
| 1072 | return '0';
|
| 1073 | }
|
| 1074 |
|
| 1075 | $row = $query->row();
|
| 1076 | return $row->numrows;
|
| 1077 | }
|
| 1078 |
|
| 1079 | // --------------------------------------------------------------------
|
| 1080 |
|
| 1081 | /**
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 1082 | * Get_Where
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1083 | *
|
| 1084 | * Allows the where clause, limit and offset to be added directly
|
| 1085 | *
|
| 1086 | * @access public
|
| 1087 | * @param string the where clause
|
| 1088 | * @param string the limit clause
|
| 1089 | * @param string the offset clause
|
| 1090 | * @return object
|
| 1091 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 1092 | function get_where($table = '', $where = null, $limit = null, $offset = null)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1093 | {
|
| 1094 | if ($table != '')
|
| 1095 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1096 | $this->_track_aliases($table);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1097 | $this->from($table);
|
| 1098 | }
|
| 1099 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1100 | if ( ! is_null($where))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1101 | {
|
| 1102 | $this->where($where);
|
| 1103 | }
|
| 1104 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1105 | if ( ! is_null($limit))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1106 | {
|
| 1107 | $this->limit($limit, $offset);
|
| 1108 | }
|
| 1109 |
|
| 1110 | $sql = $this->_compile_select();
|
| 1111 |
|
| 1112 | $result = $this->query($sql);
|
| 1113 | $this->_reset_select();
|
| 1114 | return $result;
|
| 1115 | }
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 1116 |
|
| 1117 | // --------------------------------------------------------------------
|
| 1118 |
|
| 1119 | /**
|
| 1120 | * getwhere() is an alias of get_where()
|
| 1121 | * this function is here for backwards compatibility, as
|
| 1122 | * getwhere() has been deprecated
|
| 1123 | */
|
| 1124 | function getwhere($table = '', $where = null, $limit = null, $offset = null)
|
| 1125 | {
|
| 1126 | return $this->get_where($table, $where, $limit, $offset);
|
| 1127 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1128 |
|
| 1129 | // --------------------------------------------------------------------
|
| 1130 |
|
| 1131 | /**
|
| 1132 | * Insert
|
| 1133 | *
|
| 1134 | * Compiles an insert string and runs the query
|
| 1135 | *
|
| 1136 | * @access public
|
| 1137 | * @param string the table to retrieve the results from
|
| 1138 | * @param array an associative array of insert values
|
| 1139 | * @return object
|
| 1140 | */
|
| 1141 | function insert($table = '', $set = NULL)
|
| 1142 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1143 | if ( ! is_null($set))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1144 | {
|
| 1145 | $this->set($set);
|
| 1146 | }
|
| 1147 |
|
| 1148 | if (count($this->ar_set) == 0)
|
| 1149 | {
|
| 1150 | if ($this->db_debug)
|
| 1151 | {
|
| 1152 | return $this->display_error('db_must_use_set');
|
| 1153 | }
|
| 1154 | return FALSE;
|
| 1155 | }
|
| 1156 |
|
| 1157 | if ($table == '')
|
| 1158 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1159 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1160 | {
|
| 1161 | if ($this->db_debug)
|
| 1162 | {
|
| 1163 | return $this->display_error('db_must_set_table');
|
| 1164 | }
|
| 1165 | return FALSE;
|
| 1166 | }
|
| 1167 |
|
| 1168 | $table = $this->ar_from[0];
|
| 1169 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1170 |
|
| 1171 | $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] | 1172 |
|
| 1173 | $this->_reset_write();
|
| 1174 | return $this->query($sql);
|
| 1175 | }
|
| 1176 |
|
| 1177 | // --------------------------------------------------------------------
|
| 1178 |
|
| 1179 | /**
|
| 1180 | * Update
|
| 1181 | *
|
| 1182 | * Compiles an update string and runs the query
|
| 1183 | *
|
| 1184 | * @access public
|
| 1185 | * @param string the table to retrieve the results from
|
| 1186 | * @param array an associative array of update values
|
| 1187 | * @param mixed the where clause
|
| 1188 | * @return object
|
| 1189 | */
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1190 | function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1191 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1192 | if ( ! is_null($set))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1193 | {
|
| 1194 | $this->set($set);
|
| 1195 | }
|
| 1196 |
|
| 1197 | if (count($this->ar_set) == 0)
|
| 1198 | {
|
| 1199 | if ($this->db_debug)
|
| 1200 | {
|
| 1201 | return $this->display_error('db_must_use_set');
|
| 1202 | }
|
| 1203 | return FALSE;
|
| 1204 | }
|
| 1205 |
|
| 1206 | if ($table == '')
|
| 1207 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1208 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1209 | {
|
| 1210 | if ($this->db_debug)
|
| 1211 | {
|
| 1212 | return $this->display_error('db_must_set_table');
|
| 1213 | }
|
| 1214 | return FALSE;
|
| 1215 | }
|
| 1216 |
|
| 1217 | $table = $this->ar_from[0];
|
| 1218 | }
|
| 1219 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1220 | if ($where != NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1221 | {
|
| 1222 | $this->where($where);
|
| 1223 | }
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 1224 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1225 | if ($limit != NULL)
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 1226 | {
|
| 1227 | $this->limit($limit);
|
| 1228 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1229 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1230 | $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] | 1231 |
|
| 1232 | $this->_reset_write();
|
| 1233 | return $this->query($sql);
|
| 1234 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1235 |
|
| 1236 | // --------------------------------------------------------------------
|
| 1237 |
|
| 1238 | /**
|
| 1239 | * Empty Table
|
| 1240 | *
|
| 1241 | * Compiles a delete string and runs "DELETE FROM table"
|
| 1242 | *
|
| 1243 | * @access public
|
| 1244 | * @param string the table to empty
|
| 1245 | * @return object
|
| 1246 | */
|
| 1247 | function empty_table($table = '')
|
| 1248 | {
|
| 1249 | if ($table == '')
|
| 1250 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1251 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1252 | {
|
| 1253 | if ($this->db_debug)
|
| 1254 | {
|
| 1255 | return $this->display_error('db_must_set_table');
|
| 1256 | }
|
| 1257 | return FALSE;
|
| 1258 | }
|
| 1259 |
|
| 1260 | $table = $this->ar_from[0];
|
| 1261 | }
|
| 1262 | else
|
| 1263 | {
|
| 1264 | $table = $this->_protect_identifiers($this->dbprefix.$table);
|
| 1265 | }
|
| 1266 |
|
| 1267 |
|
| 1268 | $sql = $this->_delete($table);
|
| 1269 |
|
| 1270 | $this->_reset_write();
|
| 1271 |
|
| 1272 | return $this->query($sql);
|
| 1273 | }
|
| 1274 |
|
| 1275 | // --------------------------------------------------------------------
|
| 1276 |
|
| 1277 | /**
|
| 1278 | * Truncate
|
| 1279 | *
|
| 1280 | * Compiles a truncate string and runs the query
|
| 1281 | * If the database does not support the truncate() command
|
| 1282 | * This function maps to "DELETE FROM table"
|
| 1283 | *
|
| 1284 | * @access public
|
| 1285 | * @param string the table to truncate
|
| 1286 | * @return object
|
| 1287 | */
|
| 1288 | function truncate($table = '')
|
| 1289 | {
|
| 1290 | if ($table == '')
|
| 1291 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1292 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1293 | {
|
| 1294 | if ($this->db_debug)
|
| 1295 | {
|
| 1296 | return $this->display_error('db_must_set_table');
|
| 1297 | }
|
| 1298 | return FALSE;
|
| 1299 | }
|
| 1300 |
|
| 1301 | $table = $this->ar_from[0];
|
| 1302 | }
|
| 1303 | else
|
| 1304 | {
|
| 1305 | $table = $this->_protect_identifiers($this->dbprefix.$table);
|
| 1306 | }
|
| 1307 |
|
| 1308 |
|
| 1309 | $sql = $this->_truncate($table);
|
| 1310 |
|
| 1311 | $this->_reset_write();
|
| 1312 |
|
| 1313 | return $this->query($sql);
|
| 1314 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1315 |
|
| 1316 | // --------------------------------------------------------------------
|
| 1317 |
|
| 1318 | /**
|
| 1319 | * Delete
|
| 1320 | *
|
| 1321 | * Compiles a delete string and runs the query
|
| 1322 | *
|
| 1323 | * @access public
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1324 | * @param mixed the table(s) to delete from. String or array
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1325 | * @param mixed the where clause
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1326 | * @param mixed the limit clause
|
| 1327 | * @param boolean
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1328 | * @return object
|
| 1329 | */
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1330 | function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1331 | {
|
| 1332 | if ($table == '')
|
| 1333 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1334 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1335 | {
|
| 1336 | if ($this->db_debug)
|
| 1337 | {
|
| 1338 | return $this->display_error('db_must_set_table');
|
| 1339 | }
|
| 1340 | return FALSE;
|
| 1341 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1342 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1343 | $table = $this->ar_from[0];
|
| 1344 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1345 | elseif (is_array($table))
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1346 | {
|
| 1347 | foreach($table as $single_table)
|
| 1348 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1349 | $this->delete($single_table, $where, $limit, FALSE);
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1350 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1351 |
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1352 | $this->_reset_write();
|
| 1353 | return;
|
| 1354 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1355 | else
|
| 1356 | {
|
| 1357 | $table = $this->_protect_identifiers($this->dbprefix.$table);
|
| 1358 | }
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1359 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1360 | if ($where != '')
|
| 1361 | {
|
| 1362 | $this->where($where);
|
| 1363 | }
|
| 1364 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1365 | if ($limit != NULL)
|
| 1366 | {
|
| 1367 | $this->limit($limit);
|
| 1368 | }
|
| 1369 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1370 | if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1371 | {
|
| 1372 | if ($this->db_debug)
|
| 1373 | {
|
| 1374 | return $this->display_error('db_del_must_use_where');
|
| 1375 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1376 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1377 | return FALSE;
|
| 1378 | }
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1379 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1380 | $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] | 1381 |
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1382 | if ($reset_data)
|
| 1383 | {
|
| 1384 | $this->_reset_write();
|
| 1385 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1386 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1387 | return $this->query($sql);
|
| 1388 | }
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 1389 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1390 | // --------------------------------------------------------------------
|
| 1391 |
|
| 1392 | /**
|
| 1393 | * Use Table - DEPRECATED
|
| 1394 | *
|
| 1395 | * @deprecated use $this->db->from instead
|
| 1396 | */
|
| 1397 | function use_table($table)
|
| 1398 | {
|
| 1399 | return $this->from($table);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1400 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1401 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1402 | // --------------------------------------------------------------------
|
| 1403 |
|
| 1404 | /**
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1405 | * Track Aliases
|
| 1406 | *
|
| 1407 | * Used to track SQL statements written with aliased tables.
|
| 1408 | *
|
| 1409 | * @access private
|
| 1410 | * @param string The table to inspect
|
| 1411 | * @return string
|
| 1412 | */
|
| 1413 | function _track_aliases($table)
|
| 1414 | {
|
| 1415 | // if a table alias is used we can recognize it by a space
|
| 1416 | if (strpos($table, " ") !== FALSE)
|
| 1417 | {
|
| 1418 | // if the alias is written with the AS keyowrd, get it out
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1419 | $table = preg_replace('/ AS /i', ' ', $table);
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1420 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1421 | $this->ar_aliased_tables[] = trim(strrchr($table, " "));
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1422 | }
|
| 1423 |
|
| 1424 | return $this->dbprefix.$table;
|
| 1425 | }
|
| 1426 |
|
| 1427 | // --------------------------------------------------------------------
|
| 1428 |
|
| 1429 | /**
|
| 1430 | * Filter Table Aliases
|
| 1431 | *
|
| 1432 | * Intelligently removes database prefixes from aliased tables
|
| 1433 | *
|
| 1434 | * @access private
|
| 1435 | * @param array An array of compiled SQL
|
| 1436 | * @return array Cleaned up statement with aliases accounted for
|
| 1437 | */
|
| 1438 | function _filter_table_aliases($statements)
|
| 1439 | {
|
Derek Allard | 8b2f19b | 2008-03-06 15:51:55 +0000 | [diff] [blame] | 1440 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1441 | foreach ($statements as $k => $v)
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1442 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1443 | foreach ($this->ar_aliased_tables as $table)
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1444 | {
|
Derek Allard | 8b2f19b | 2008-03-06 15:51:55 +0000 | [diff] [blame] | 1445 | $statements[$k] = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $statements[$k]); // makes `table.field`
|
| 1446 | $statements[$k] = str_replace($this->dbprefix.$table.'.', $table.'.', $statements[$k]);
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1447 | }
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1448 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1449 | return $statements;
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1450 | }
|
| 1451 |
|
| 1452 | // --------------------------------------------------------------------
|
| 1453 |
|
| 1454 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1455 | * Compile the SELECT statement
|
| 1456 | *
|
| 1457 | * Generates a query string based on which functions were used.
|
| 1458 | * Should not be called directly. The get() function calls it.
|
| 1459 | *
|
| 1460 | * @access private
|
| 1461 | * @return string
|
| 1462 | */
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1463 | function _compile_select($select_override = FALSE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1464 | {
|
Derek Allard | 7825526 | 2008-02-06 13:54:23 +0000 | [diff] [blame] | 1465 | $this->_merge_cache();
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1466 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1467 | $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1468 |
|
Derek Allard | 5162fc7 | 2008-04-15 20:05:37 +0000 | [diff] [blame] | 1469 | $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] | 1470 |
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1471 | if ($select_override !== FALSE)
|
| 1472 | {
|
| 1473 | $sql = $select_override;
|
| 1474 | }
|
| 1475 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1476 | if (count($this->ar_from) > 0)
|
| 1477 | {
|
| 1478 | $sql .= "\nFROM ";
|
Derek Jones | c6ad023 | 2008-01-29 18:44:54 +0000 | [diff] [blame] | 1479 | $sql .= $this->_from_tables($this->ar_from);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1480 | }
|
| 1481 |
|
| 1482 | if (count($this->ar_join) > 0)
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1483 | {
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1484 | $sql .= "\n";
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1485 |
|
| 1486 | // special consideration for table aliases
|
| 1487 | if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
|
| 1488 | {
|
| 1489 | $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
|
| 1490 | }
|
| 1491 | else
|
| 1492 | {
|
| 1493 | $sql .= implode("\n", $this->ar_join);
|
| 1494 | }
|
| 1495 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1496 | }
|
| 1497 |
|
| 1498 | if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
|
| 1499 | {
|
| 1500 | $sql .= "\nWHERE ";
|
| 1501 | }
|
| 1502 |
|
| 1503 | $sql .= implode("\n", $this->ar_where);
|
| 1504 |
|
| 1505 | if (count($this->ar_like) > 0)
|
| 1506 | {
|
| 1507 | if (count($this->ar_where) > 0)
|
| 1508 | {
|
| 1509 | $sql .= " AND ";
|
| 1510 | }
|
| 1511 |
|
| 1512 | $sql .= implode("\n", $this->ar_like);
|
| 1513 | }
|
| 1514 |
|
| 1515 | if (count($this->ar_groupby) > 0)
|
| 1516 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1517 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1518 | $sql .= "\nGROUP BY ";
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1519 |
|
| 1520 | // special consideration for table aliases
|
| 1521 | if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
|
| 1522 | {
|
| 1523 | $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
|
| 1524 | }
|
| 1525 | else
|
| 1526 | {
|
| 1527 | $sql .= implode(', ', $this->ar_groupby);
|
| 1528 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1529 | }
|
| 1530 |
|
| 1531 | if (count($this->ar_having) > 0)
|
| 1532 | {
|
| 1533 | $sql .= "\nHAVING ";
|
| 1534 | $sql .= implode("\n", $this->ar_having);
|
| 1535 | }
|
| 1536 |
|
| 1537 | if (count($this->ar_orderby) > 0)
|
| 1538 | {
|
| 1539 | $sql .= "\nORDER BY ";
|
| 1540 | $sql .= implode(', ', $this->ar_orderby);
|
| 1541 |
|
| 1542 | if ($this->ar_order !== FALSE)
|
| 1543 | {
|
| 1544 | $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
|
| 1545 | }
|
| 1546 | }
|
| 1547 |
|
| 1548 | if (is_numeric($this->ar_limit))
|
| 1549 | {
|
| 1550 | $sql .= "\n";
|
| 1551 | $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
|
| 1552 | }
|
| 1553 |
|
| 1554 | return $sql;
|
| 1555 | }
|
| 1556 |
|
| 1557 | // --------------------------------------------------------------------
|
| 1558 |
|
| 1559 | /**
|
| 1560 | * Object to Array
|
| 1561 | *
|
| 1562 | * Takes an object as input and converts the class variables to array key/vals
|
| 1563 | *
|
| 1564 | * @access public
|
| 1565 | * @param object
|
| 1566 | * @return array
|
| 1567 | */
|
| 1568 | function _object_to_array($object)
|
| 1569 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1570 | if ( ! is_object($object))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1571 | {
|
| 1572 | return $object;
|
| 1573 | }
|
| 1574 |
|
| 1575 | $array = array();
|
| 1576 | foreach (get_object_vars($object) as $key => $val)
|
| 1577 | {
|
Derek Allard | 848b776 | 2007-12-31 16:43:05 +0000 | [diff] [blame] | 1578 | // 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] | 1579 | 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] | 1580 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1581 | {
|
| 1582 | $array[$key] = $val;
|
| 1583 | }
|
| 1584 | }
|
| 1585 |
|
| 1586 | return $array;
|
| 1587 | }
|
| 1588 |
|
| 1589 | // --------------------------------------------------------------------
|
| 1590 |
|
| 1591 | /**
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1592 | * Start Cache
|
| 1593 | *
|
| 1594 | * Starts AR caching
|
| 1595 | *
|
| 1596 | * @access public
|
| 1597 | * @return void
|
| 1598 | */
|
| 1599 | function start_cache()
|
| 1600 | {
|
| 1601 | $this->ar_caching = TRUE;
|
| 1602 | }
|
| 1603 |
|
| 1604 | // --------------------------------------------------------------------
|
| 1605 |
|
| 1606 | /**
|
| 1607 | * Stop Cache
|
| 1608 | *
|
| 1609 | * Stops AR caching
|
| 1610 | *
|
| 1611 | * @access public
|
| 1612 | * @return void
|
| 1613 | */
|
| 1614 | function stop_cache()
|
| 1615 | {
|
| 1616 | $this->ar_caching = FALSE;
|
| 1617 | }
|
| 1618 |
|
| 1619 |
|
| 1620 | // --------------------------------------------------------------------
|
| 1621 |
|
| 1622 | /**
|
| 1623 | * Flush Cache
|
| 1624 | *
|
| 1625 | * Empties the AR cache
|
| 1626 | *
|
| 1627 | * @access public
|
| 1628 | * @return void
|
| 1629 | */
|
| 1630 | function flush_cache()
|
| 1631 | {
|
| 1632 | $ar_reset_items = array(
|
| 1633 | 'ar_cache_select' => array(),
|
| 1634 | 'ar_cache_from' => array(),
|
| 1635 | 'ar_cache_join' => array(),
|
| 1636 | 'ar_cache_where' => array(),
|
| 1637 | 'ar_cache_like' => array(),
|
| 1638 | 'ar_cache_groupby' => array(),
|
| 1639 | 'ar_cache_having' =>array(),
|
| 1640 | 'ar_cache_orderby' => array(),
|
| 1641 | 'ar_cache_set' => array()
|
| 1642 | );
|
| 1643 |
|
| 1644 | $this->_reset_run($ar_reset_items);
|
| 1645 | }
|
| 1646 |
|
| 1647 | // --------------------------------------------------------------------
|
| 1648 |
|
| 1649 | /**
|
| 1650 | * Merge Cache
|
| 1651 | *
|
| 1652 | * When called, this function merges any cached AR arrays with
|
| 1653 | * locally called ones.
|
| 1654 | *
|
| 1655 | * @access private
|
| 1656 | * @return void
|
| 1657 | */
|
| 1658 | function _merge_cache()
|
| 1659 | {
|
| 1660 | $ar_items = array('select', 'from', 'join', 'where', 'like', 'groupby', 'having', 'orderby', 'set');
|
| 1661 |
|
| 1662 | foreach ($ar_items as $ar_item)
|
| 1663 | {
|
| 1664 | $ar_cache_item = 'ar_cache_'.$ar_item;
|
| 1665 | $ar_item = 'ar_'.$ar_item;
|
| 1666 | $this->$ar_item = array_unique(array_merge($this->$ar_item, $this->$ar_cache_item));
|
| 1667 | }
|
| 1668 | }
|
| 1669 |
|
| 1670 | // --------------------------------------------------------------------
|
| 1671 |
|
| 1672 | /**
|
| 1673 | * Resets the active record values. Called by the get() function
|
| 1674 | *
|
| 1675 | * @access private
|
| 1676 | * @param array An array of fields to reset
|
| 1677 | * @return void
|
| 1678 | */
|
| 1679 | function _reset_run($ar_reset_items)
|
| 1680 | {
|
| 1681 | foreach ($ar_reset_items as $item => $default_value)
|
| 1682 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1683 | if ( ! in_array($item, $this->ar_store_array))
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1684 | {
|
| 1685 | $this->$item = $default_value;
|
| 1686 | }
|
| 1687 | }
|
| 1688 | }
|
| 1689 |
|
| 1690 | // --------------------------------------------------------------------
|
| 1691 |
|
| 1692 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1693 | * Resets the active record values. Called by the get() function
|
| 1694 | *
|
| 1695 | * @access private
|
| 1696 | * @return void
|
| 1697 | */
|
| 1698 | function _reset_select()
|
| 1699 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1700 | $ar_reset_items = array(
|
| 1701 | 'ar_select' => array(),
|
| 1702 | 'ar_from' => array(),
|
| 1703 | 'ar_join' => array(),
|
| 1704 | 'ar_where' => array(),
|
| 1705 | 'ar_like' => array(),
|
| 1706 | 'ar_groupby' => array(),
|
| 1707 | 'ar_having' => array(),
|
| 1708 | 'ar_orderby' => array(),
|
| 1709 | 'ar_wherein' => array(),
|
| 1710 | 'ar_aliased_tables' => array(),
|
| 1711 | 'ar_distinct' => FALSE,
|
| 1712 | 'ar_limit' => FALSE,
|
| 1713 | 'ar_offset' => FALSE,
|
| 1714 | 'ar_order' => FALSE,
|
| 1715 | );
|
| 1716 |
|
| 1717 | $this->_reset_run($ar_reset_items);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1718 | }
|
| 1719 |
|
| 1720 | // --------------------------------------------------------------------
|
| 1721 |
|
| 1722 | /**
|
| 1723 | * Resets the active record "write" values.
|
| 1724 | *
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1725 | * Called by the insert() update() and delete() functions
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1726 | *
|
| 1727 | * @access private
|
| 1728 | * @return void
|
| 1729 | */
|
| 1730 | function _reset_write()
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1731 | {
|
| 1732 | $ar_reset_items = array(
|
| 1733 | 'ar_set' => array(),
|
| 1734 | 'ar_from' => array(),
|
| 1735 | 'ar_where' => array(),
|
| 1736 | 'ar_like' => array(),
|
| 1737 | 'ar_orderby' => array(),
|
| 1738 | 'ar_limit' => FALSE,
|
| 1739 | 'ar_order' => FALSE
|
| 1740 | );
|
| 1741 |
|
| 1742 | $this->_reset_run($ar_reset_items);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1743 | }
|
| 1744 |
|
| 1745 | }
|
Derek Allard | a632589 | 2008-05-12 17:51:47 +0000 | [diff] [blame] | 1746 |
|
| 1747 | /* End of file DB_active_rec.php */
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 1748 | /* Location: ./system/database/DB_active_rec.php */ |