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 | e808aac | 2008-04-06 18:22:00 +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 | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 855 |
|
| 856 | if ($v != '')
|
| 857 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 858 | $v = ' '.$this->escape_str($v);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 859 | }
|
| 860 |
|
| 861 | $this->ar_having[] = $prefix.$k.$v;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 862 | if ($this->ar_caching === TRUE)
|
| 863 | {
|
| 864 | $this->ar_cache_having[] = $prefix.$k.$v;
|
| 865 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 866 | }
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 867 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 868 | return $this;
|
| 869 | }
|
| 870 |
|
| 871 | // --------------------------------------------------------------------
|
| 872 |
|
| 873 | /**
|
| 874 | * Sets the ORDER BY value
|
| 875 | *
|
| 876 | * @access public
|
| 877 | * @param string
|
| 878 | * @param string direction: asc or desc
|
| 879 | * @return object
|
| 880 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 881 | function order_by($orderby, $direction = '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 882 | {
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 883 | if (strtolower($direction) == 'random')
|
| 884 | {
|
| 885 | $orderby = ''; // Random results want or don't need a field name
|
| 886 | $direction = $this->_random_keyword;
|
| 887 | }
|
| 888 | elseif (trim($direction) != '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 889 | {
|
Derek Allard | 9278249 | 2007-08-10 11:26:01 +0000 | [diff] [blame] | 890 | $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 891 | }
|
| 892 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 893 | $orderby_statement = $this->_protect_identifiers($orderby, TRUE).$direction;
|
| 894 |
|
| 895 | $this->ar_orderby[] = $orderby_statement;
|
| 896 | if ($this->ar_caching === TRUE)
|
| 897 | {
|
| 898 | $this->ar_cache_orderby[] = $orderby_statement;
|
| 899 | }
|
| 900 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 901 | return $this;
|
| 902 | }
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 903 |
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 904 | // --------------------------------------------------------------------
|
| 905 |
|
| 906 | /**
|
| 907 | * orderby() is an alias of order_by()
|
| 908 | * this function is here for backwards compatibility, as
|
| 909 | * orderby() has been deprecated
|
| 910 | */
|
| 911 | function orderby($orderby, $direction = '')
|
| 912 | {
|
| 913 | return $this->order_by($orderby, $direction);
|
| 914 | }
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 915 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 916 | // --------------------------------------------------------------------
|
| 917 |
|
| 918 | /**
|
| 919 | * Sets the LIMIT value
|
| 920 | *
|
| 921 | * @access public
|
| 922 | * @param integer the limit value
|
| 923 | * @param integer the offset value
|
| 924 | * @return object
|
| 925 | */
|
| 926 | function limit($value, $offset = '')
|
| 927 | {
|
| 928 | $this->ar_limit = $value;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 929 | if ($this->ar_caching === TRUE)
|
| 930 | {
|
| 931 | $this->ar_cache_limit[] = $value;
|
| 932 | }
|
| 933 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 934 | if ($offset != '')
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 935 | {
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 936 | $this->ar_offset = $offset;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 937 | if ($this->ar_caching === TRUE)
|
| 938 | {
|
| 939 | $this->ar_cache_offset[] = $offset;
|
| 940 | }
|
| 941 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 942 |
|
| 943 | return $this;
|
| 944 | }
|
| 945 |
|
| 946 | // --------------------------------------------------------------------
|
| 947 |
|
| 948 | /**
|
| 949 | * Sets the OFFSET value
|
| 950 | *
|
| 951 | * @access public
|
| 952 | * @param integer the offset value
|
| 953 | * @return object
|
| 954 | */
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 955 | function offset($offset)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 956 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 957 | $this->ar_offset = $offset;
|
| 958 | if ($this->ar_caching === TRUE)
|
| 959 | {
|
| 960 | $this->ar_cache_offset[] = $offset;
|
| 961 | }
|
| 962 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 963 | return $this;
|
| 964 | }
|
| 965 |
|
| 966 | // --------------------------------------------------------------------
|
| 967 |
|
| 968 | /**
|
| 969 | * The "set" function. Allows key/value pairs to be set for inserting or updating
|
| 970 | *
|
| 971 | * @access public
|
| 972 | * @param mixed
|
| 973 | * @param string
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 974 | * @param boolean
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 975 | * @return object
|
| 976 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 977 | function set($key, $value = '', $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 978 | {
|
| 979 | $key = $this->_object_to_array($key);
|
| 980 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 981 | if ( ! is_array($key))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 982 | {
|
| 983 | $key = array($key => $value);
|
| 984 | }
|
| 985 |
|
| 986 | foreach ($key as $k => $v)
|
| 987 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 988 | if ($escape === FALSE)
|
| 989 | {
|
| 990 | $this->ar_set[$this->_protect_identifiers($k)] = $v;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 991 | if ($this->ar_caching === TRUE)
|
| 992 | {
|
| 993 | $this->ar_cache_offset[$this->_protect_identifiers($k)] = $v;
|
| 994 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 995 | }
|
| 996 | else
|
| 997 | {
|
| 998 | $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 999 | if ($this->ar_caching === TRUE)
|
| 1000 | {
|
| 1001 | $this->ar_cache_offset[$this->_protect_identifiers($k)] = $this->escape($v);
|
| 1002 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1003 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1004 | }
|
| 1005 |
|
| 1006 | return $this;
|
| 1007 | }
|
| 1008 |
|
| 1009 | // --------------------------------------------------------------------
|
| 1010 |
|
| 1011 | /**
|
| 1012 | * Get
|
| 1013 | *
|
| 1014 | * Compiles the select statement based on the other functions called
|
| 1015 | * and runs the query
|
| 1016 | *
|
| 1017 | * @access public
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1018 | * @param string the table
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1019 | * @param string the limit clause
|
| 1020 | * @param string the offset clause
|
| 1021 | * @return object
|
| 1022 | */
|
| 1023 | function get($table = '', $limit = null, $offset = null)
|
| 1024 | {
|
| 1025 | if ($table != '')
|
| 1026 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1027 | $this->_track_aliases($table);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1028 | $this->from($table);
|
| 1029 | }
|
| 1030 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1031 | if ( ! is_null($limit))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1032 | {
|
| 1033 | $this->limit($limit, $offset);
|
| 1034 | }
|
| 1035 |
|
| 1036 | $sql = $this->_compile_select();
|
| 1037 |
|
| 1038 | $result = $this->query($sql);
|
| 1039 | $this->_reset_select();
|
| 1040 | return $result;
|
| 1041 | }
|
| 1042 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1043 | /**
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1044 | * "Count All Results" query
|
| 1045 | *
|
| 1046 | * Generates a platform-specific query string that counts all records
|
| 1047 | * returned by an Active Record query.
|
| 1048 | *
|
| 1049 | * @access public
|
| 1050 | * @param string
|
| 1051 | * @return string
|
| 1052 | */
|
| 1053 | function count_all_results($table = '')
|
| 1054 | {
|
| 1055 | if ($table != '')
|
| 1056 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1057 | $this->_track_aliases($table);
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1058 | $this->from($table);
|
| 1059 | }
|
| 1060 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1061 | $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1062 |
|
| 1063 | $query = $this->query($sql);
|
| 1064 | $this->_reset_select();
|
| 1065 |
|
| 1066 | if ($query->num_rows() == 0)
|
| 1067 | {
|
| 1068 | return '0';
|
| 1069 | }
|
| 1070 |
|
| 1071 | $row = $query->row();
|
| 1072 | return $row->numrows;
|
| 1073 | }
|
| 1074 |
|
| 1075 | // --------------------------------------------------------------------
|
| 1076 |
|
| 1077 | /**
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 1078 | * Get_Where
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1079 | *
|
| 1080 | * Allows the where clause, limit and offset to be added directly
|
| 1081 | *
|
| 1082 | * @access public
|
| 1083 | * @param string the where clause
|
| 1084 | * @param string the limit clause
|
| 1085 | * @param string the offset clause
|
| 1086 | * @return object
|
| 1087 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 1088 | function get_where($table = '', $where = null, $limit = null, $offset = null)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1089 | {
|
| 1090 | if ($table != '')
|
| 1091 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1092 | $this->_track_aliases($table);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1093 | $this->from($table);
|
| 1094 | }
|
| 1095 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1096 | if ( ! is_null($where))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1097 | {
|
| 1098 | $this->where($where);
|
| 1099 | }
|
| 1100 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1101 | if ( ! is_null($limit))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1102 | {
|
| 1103 | $this->limit($limit, $offset);
|
| 1104 | }
|
| 1105 |
|
| 1106 | $sql = $this->_compile_select();
|
| 1107 |
|
| 1108 | $result = $this->query($sql);
|
| 1109 | $this->_reset_select();
|
| 1110 | return $result;
|
| 1111 | }
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 1112 |
|
| 1113 | // --------------------------------------------------------------------
|
| 1114 |
|
| 1115 | /**
|
| 1116 | * getwhere() is an alias of get_where()
|
| 1117 | * this function is here for backwards compatibility, as
|
| 1118 | * getwhere() has been deprecated
|
| 1119 | */
|
| 1120 | function getwhere($table = '', $where = null, $limit = null, $offset = null)
|
| 1121 | {
|
| 1122 | return $this->get_where($table, $where, $limit, $offset);
|
| 1123 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1124 |
|
| 1125 | // --------------------------------------------------------------------
|
| 1126 |
|
| 1127 | /**
|
| 1128 | * Insert
|
| 1129 | *
|
| 1130 | * Compiles an insert string and runs the query
|
| 1131 | *
|
| 1132 | * @access public
|
| 1133 | * @param string the table to retrieve the results from
|
| 1134 | * @param array an associative array of insert values
|
| 1135 | * @return object
|
| 1136 | */
|
| 1137 | function insert($table = '', $set = NULL)
|
| 1138 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1139 | if ( ! is_null($set))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1140 | {
|
| 1141 | $this->set($set);
|
| 1142 | }
|
| 1143 |
|
| 1144 | if (count($this->ar_set) == 0)
|
| 1145 | {
|
| 1146 | if ($this->db_debug)
|
| 1147 | {
|
| 1148 | return $this->display_error('db_must_use_set');
|
| 1149 | }
|
| 1150 | return FALSE;
|
| 1151 | }
|
| 1152 |
|
| 1153 | if ($table == '')
|
| 1154 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1155 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1156 | {
|
| 1157 | if ($this->db_debug)
|
| 1158 | {
|
| 1159 | return $this->display_error('db_must_set_table');
|
| 1160 | }
|
| 1161 | return FALSE;
|
| 1162 | }
|
| 1163 |
|
| 1164 | $table = $this->ar_from[0];
|
| 1165 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1166 |
|
| 1167 | $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] | 1168 |
|
| 1169 | $this->_reset_write();
|
| 1170 | return $this->query($sql);
|
| 1171 | }
|
| 1172 |
|
| 1173 | // --------------------------------------------------------------------
|
| 1174 |
|
| 1175 | /**
|
| 1176 | * Update
|
| 1177 | *
|
| 1178 | * Compiles an update string and runs the query
|
| 1179 | *
|
| 1180 | * @access public
|
| 1181 | * @param string the table to retrieve the results from
|
| 1182 | * @param array an associative array of update values
|
| 1183 | * @param mixed the where clause
|
| 1184 | * @return object
|
| 1185 | */
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1186 | function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1187 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1188 | if ( ! is_null($set))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1189 | {
|
| 1190 | $this->set($set);
|
| 1191 | }
|
| 1192 |
|
| 1193 | if (count($this->ar_set) == 0)
|
| 1194 | {
|
| 1195 | if ($this->db_debug)
|
| 1196 | {
|
| 1197 | return $this->display_error('db_must_use_set');
|
| 1198 | }
|
| 1199 | return FALSE;
|
| 1200 | }
|
| 1201 |
|
| 1202 | if ($table == '')
|
| 1203 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1204 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1205 | {
|
| 1206 | if ($this->db_debug)
|
| 1207 | {
|
| 1208 | return $this->display_error('db_must_set_table');
|
| 1209 | }
|
| 1210 | return FALSE;
|
| 1211 | }
|
| 1212 |
|
| 1213 | $table = $this->ar_from[0];
|
| 1214 | }
|
| 1215 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1216 | if ($where != NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1217 | {
|
| 1218 | $this->where($where);
|
| 1219 | }
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 1220 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1221 | if ($limit != NULL)
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 1222 | {
|
| 1223 | $this->limit($limit);
|
| 1224 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1225 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1226 | $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] | 1227 |
|
| 1228 | $this->_reset_write();
|
| 1229 | return $this->query($sql);
|
| 1230 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1231 |
|
| 1232 | // --------------------------------------------------------------------
|
| 1233 |
|
| 1234 | /**
|
| 1235 | * Empty Table
|
| 1236 | *
|
| 1237 | * Compiles a delete string and runs "DELETE FROM table"
|
| 1238 | *
|
| 1239 | * @access public
|
| 1240 | * @param string the table to empty
|
| 1241 | * @return object
|
| 1242 | */
|
| 1243 | function empty_table($table = '')
|
| 1244 | {
|
| 1245 | if ($table == '')
|
| 1246 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1247 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1248 | {
|
| 1249 | if ($this->db_debug)
|
| 1250 | {
|
| 1251 | return $this->display_error('db_must_set_table');
|
| 1252 | }
|
| 1253 | return FALSE;
|
| 1254 | }
|
| 1255 |
|
| 1256 | $table = $this->ar_from[0];
|
| 1257 | }
|
| 1258 | else
|
| 1259 | {
|
| 1260 | $table = $this->_protect_identifiers($this->dbprefix.$table);
|
| 1261 | }
|
| 1262 |
|
| 1263 |
|
| 1264 | $sql = $this->_delete($table);
|
| 1265 |
|
| 1266 | $this->_reset_write();
|
| 1267 |
|
| 1268 | return $this->query($sql);
|
| 1269 | }
|
| 1270 |
|
| 1271 | // --------------------------------------------------------------------
|
| 1272 |
|
| 1273 | /**
|
| 1274 | * Truncate
|
| 1275 | *
|
| 1276 | * Compiles a truncate string and runs the query
|
| 1277 | * If the database does not support the truncate() command
|
| 1278 | * This function maps to "DELETE FROM table"
|
| 1279 | *
|
| 1280 | * @access public
|
| 1281 | * @param string the table to truncate
|
| 1282 | * @return object
|
| 1283 | */
|
| 1284 | function truncate($table = '')
|
| 1285 | {
|
| 1286 | if ($table == '')
|
| 1287 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1288 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1289 | {
|
| 1290 | if ($this->db_debug)
|
| 1291 | {
|
| 1292 | return $this->display_error('db_must_set_table');
|
| 1293 | }
|
| 1294 | return FALSE;
|
| 1295 | }
|
| 1296 |
|
| 1297 | $table = $this->ar_from[0];
|
| 1298 | }
|
| 1299 | else
|
| 1300 | {
|
| 1301 | $table = $this->_protect_identifiers($this->dbprefix.$table);
|
| 1302 | }
|
| 1303 |
|
| 1304 |
|
| 1305 | $sql = $this->_truncate($table);
|
| 1306 |
|
| 1307 | $this->_reset_write();
|
| 1308 |
|
| 1309 | return $this->query($sql);
|
| 1310 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1311 |
|
| 1312 | // --------------------------------------------------------------------
|
| 1313 |
|
| 1314 | /**
|
| 1315 | * Delete
|
| 1316 | *
|
| 1317 | * Compiles a delete string and runs the query
|
| 1318 | *
|
| 1319 | * @access public
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1320 | * @param mixed the table(s) to delete from. String or array
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1321 | * @param mixed the where clause
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1322 | * @param mixed the limit clause
|
| 1323 | * @param boolean
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1324 | * @return object
|
| 1325 | */
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1326 | function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1327 | {
|
| 1328 | if ($table == '')
|
| 1329 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1330 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1331 | {
|
| 1332 | if ($this->db_debug)
|
| 1333 | {
|
| 1334 | return $this->display_error('db_must_set_table');
|
| 1335 | }
|
| 1336 | return FALSE;
|
| 1337 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1338 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1339 | $table = $this->ar_from[0];
|
| 1340 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1341 | elseif (is_array($table))
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1342 | {
|
| 1343 | foreach($table as $single_table)
|
| 1344 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1345 | $this->delete($single_table, $where, $limit, FALSE);
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1346 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1347 |
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1348 | $this->_reset_write();
|
| 1349 | return;
|
| 1350 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1351 | else
|
| 1352 | {
|
| 1353 | $table = $this->_protect_identifiers($this->dbprefix.$table);
|
| 1354 | }
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1355 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1356 | if ($where != '')
|
| 1357 | {
|
| 1358 | $this->where($where);
|
| 1359 | }
|
| 1360 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1361 | if ($limit != NULL)
|
| 1362 | {
|
| 1363 | $this->limit($limit);
|
| 1364 | }
|
| 1365 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1366 | if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1367 | {
|
| 1368 | if ($this->db_debug)
|
| 1369 | {
|
| 1370 | return $this->display_error('db_del_must_use_where');
|
| 1371 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1372 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1373 | return FALSE;
|
| 1374 | }
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1375 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1376 | $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] | 1377 |
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1378 | if ($reset_data)
|
| 1379 | {
|
| 1380 | $this->_reset_write();
|
| 1381 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1382 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1383 | return $this->query($sql);
|
| 1384 | }
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 1385 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1386 | // --------------------------------------------------------------------
|
| 1387 |
|
| 1388 | /**
|
| 1389 | * Use Table - DEPRECATED
|
| 1390 | *
|
| 1391 | * @deprecated use $this->db->from instead
|
| 1392 | */
|
| 1393 | function use_table($table)
|
| 1394 | {
|
| 1395 | return $this->from($table);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1396 | }
|
| 1397 |
|
| 1398 | // --------------------------------------------------------------------
|
| 1399 |
|
| 1400 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1401 | * Tests whether the string has an SQL operator
|
| 1402 | *
|
| 1403 | * @access private
|
| 1404 | * @param string
|
| 1405 | * @return bool
|
| 1406 | */
|
| 1407 | function _has_operator($str)
|
| 1408 | {
|
| 1409 | $str = trim($str);
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1410 | if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1411 | {
|
| 1412 | return FALSE;
|
| 1413 | }
|
| 1414 |
|
| 1415 | return TRUE;
|
| 1416 | }
|
| 1417 |
|
| 1418 | // --------------------------------------------------------------------
|
| 1419 |
|
| 1420 | /**
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1421 | * Track Aliases
|
| 1422 | *
|
| 1423 | * Used to track SQL statements written with aliased tables.
|
| 1424 | *
|
| 1425 | * @access private
|
| 1426 | * @param string The table to inspect
|
| 1427 | * @return string
|
| 1428 | */
|
| 1429 | function _track_aliases($table)
|
| 1430 | {
|
| 1431 | // if a table alias is used we can recognize it by a space
|
| 1432 | if (strpos($table, " ") !== FALSE)
|
| 1433 | {
|
| 1434 | // if the alias is written with the AS keyowrd, get it out
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1435 | $table = preg_replace('/ AS /i', ' ', $table);
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1436 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1437 | $this->ar_aliased_tables[] = trim(strrchr($table, " "));
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1438 | }
|
| 1439 |
|
| 1440 | return $this->dbprefix.$table;
|
| 1441 | }
|
| 1442 |
|
| 1443 | // --------------------------------------------------------------------
|
| 1444 |
|
| 1445 | /**
|
| 1446 | * Filter Table Aliases
|
| 1447 | *
|
| 1448 | * Intelligently removes database prefixes from aliased tables
|
| 1449 | *
|
| 1450 | * @access private
|
| 1451 | * @param array An array of compiled SQL
|
| 1452 | * @return array Cleaned up statement with aliases accounted for
|
| 1453 | */
|
| 1454 | function _filter_table_aliases($statements)
|
| 1455 | {
|
Derek Allard | 8b2f19b | 2008-03-06 15:51:55 +0000 | [diff] [blame] | 1456 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1457 | foreach ($statements as $k => $v)
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1458 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1459 | foreach ($this->ar_aliased_tables as $table)
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1460 | {
|
Derek Allard | 8b2f19b | 2008-03-06 15:51:55 +0000 | [diff] [blame] | 1461 | $statements[$k] = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $statements[$k]); // makes `table.field`
|
| 1462 | $statements[$k] = str_replace($this->dbprefix.$table.'.', $table.'.', $statements[$k]);
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1463 | }
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1464 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1465 | return $statements;
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1466 | }
|
| 1467 |
|
| 1468 | // --------------------------------------------------------------------
|
| 1469 |
|
| 1470 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1471 | * Compile the SELECT statement
|
| 1472 | *
|
| 1473 | * Generates a query string based on which functions were used.
|
| 1474 | * Should not be called directly. The get() function calls it.
|
| 1475 | *
|
| 1476 | * @access private
|
| 1477 | * @return string
|
| 1478 | */
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1479 | function _compile_select($select_override = FALSE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1480 | {
|
Derek Allard | 7825526 | 2008-02-06 13:54:23 +0000 | [diff] [blame] | 1481 | $this->_merge_cache();
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1482 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1483 | $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1484 |
|
Derek Allard | 5162fc7 | 2008-04-15 20:05:37 +0000 | [diff] [blame] | 1485 | $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] | 1486 |
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1487 | if ($select_override !== FALSE)
|
| 1488 | {
|
| 1489 | $sql = $select_override;
|
| 1490 | }
|
| 1491 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1492 | if (count($this->ar_from) > 0)
|
| 1493 | {
|
| 1494 | $sql .= "\nFROM ";
|
Derek Jones | c6ad023 | 2008-01-29 18:44:54 +0000 | [diff] [blame] | 1495 | $sql .= $this->_from_tables($this->ar_from);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1496 | }
|
| 1497 |
|
| 1498 | if (count($this->ar_join) > 0)
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1499 | {
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1500 | $sql .= "\n";
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1501 |
|
| 1502 | // special consideration for table aliases
|
| 1503 | if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
|
| 1504 | {
|
| 1505 | $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
|
| 1506 | }
|
| 1507 | else
|
| 1508 | {
|
| 1509 | $sql .= implode("\n", $this->ar_join);
|
| 1510 | }
|
| 1511 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1512 | }
|
| 1513 |
|
| 1514 | if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
|
| 1515 | {
|
| 1516 | $sql .= "\nWHERE ";
|
| 1517 | }
|
| 1518 |
|
| 1519 | $sql .= implode("\n", $this->ar_where);
|
| 1520 |
|
| 1521 | if (count($this->ar_like) > 0)
|
| 1522 | {
|
| 1523 | if (count($this->ar_where) > 0)
|
| 1524 | {
|
| 1525 | $sql .= " AND ";
|
| 1526 | }
|
| 1527 |
|
| 1528 | $sql .= implode("\n", $this->ar_like);
|
| 1529 | }
|
| 1530 |
|
| 1531 | if (count($this->ar_groupby) > 0)
|
| 1532 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1533 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1534 | $sql .= "\nGROUP BY ";
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1535 |
|
| 1536 | // special consideration for table aliases
|
| 1537 | if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
|
| 1538 | {
|
| 1539 | $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
|
| 1540 | }
|
| 1541 | else
|
| 1542 | {
|
| 1543 | $sql .= implode(', ', $this->ar_groupby);
|
| 1544 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1545 | }
|
| 1546 |
|
| 1547 | if (count($this->ar_having) > 0)
|
| 1548 | {
|
| 1549 | $sql .= "\nHAVING ";
|
| 1550 | $sql .= implode("\n", $this->ar_having);
|
| 1551 | }
|
| 1552 |
|
| 1553 | if (count($this->ar_orderby) > 0)
|
| 1554 | {
|
| 1555 | $sql .= "\nORDER BY ";
|
| 1556 | $sql .= implode(', ', $this->ar_orderby);
|
| 1557 |
|
| 1558 | if ($this->ar_order !== FALSE)
|
| 1559 | {
|
| 1560 | $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
|
| 1561 | }
|
| 1562 | }
|
| 1563 |
|
| 1564 | if (is_numeric($this->ar_limit))
|
| 1565 | {
|
| 1566 | $sql .= "\n";
|
| 1567 | $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
|
| 1568 | }
|
| 1569 |
|
| 1570 | return $sql;
|
| 1571 | }
|
| 1572 |
|
| 1573 | // --------------------------------------------------------------------
|
| 1574 |
|
| 1575 | /**
|
| 1576 | * Object to Array
|
| 1577 | *
|
| 1578 | * Takes an object as input and converts the class variables to array key/vals
|
| 1579 | *
|
| 1580 | * @access public
|
| 1581 | * @param object
|
| 1582 | * @return array
|
| 1583 | */
|
| 1584 | function _object_to_array($object)
|
| 1585 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1586 | if ( ! is_object($object))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1587 | {
|
| 1588 | return $object;
|
| 1589 | }
|
| 1590 |
|
| 1591 | $array = array();
|
| 1592 | foreach (get_object_vars($object) as $key => $val)
|
| 1593 | {
|
Derek Allard | 848b776 | 2007-12-31 16:43:05 +0000 | [diff] [blame] | 1594 | // 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] | 1595 | 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] | 1596 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1597 | {
|
| 1598 | $array[$key] = $val;
|
| 1599 | }
|
| 1600 | }
|
| 1601 |
|
| 1602 | return $array;
|
| 1603 | }
|
| 1604 |
|
| 1605 | // --------------------------------------------------------------------
|
| 1606 |
|
| 1607 | /**
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1608 | * Start Cache
|
| 1609 | *
|
| 1610 | * Starts AR caching
|
| 1611 | *
|
| 1612 | * @access public
|
| 1613 | * @return void
|
| 1614 | */
|
| 1615 | function start_cache()
|
| 1616 | {
|
| 1617 | $this->ar_caching = TRUE;
|
| 1618 | }
|
| 1619 |
|
| 1620 | // --------------------------------------------------------------------
|
| 1621 |
|
| 1622 | /**
|
| 1623 | * Stop Cache
|
| 1624 | *
|
| 1625 | * Stops AR caching
|
| 1626 | *
|
| 1627 | * @access public
|
| 1628 | * @return void
|
| 1629 | */
|
| 1630 | function stop_cache()
|
| 1631 | {
|
| 1632 | $this->ar_caching = FALSE;
|
| 1633 | }
|
| 1634 |
|
| 1635 |
|
| 1636 | // --------------------------------------------------------------------
|
| 1637 |
|
| 1638 | /**
|
| 1639 | * Flush Cache
|
| 1640 | *
|
| 1641 | * Empties the AR cache
|
| 1642 | *
|
| 1643 | * @access public
|
| 1644 | * @return void
|
| 1645 | */
|
| 1646 | function flush_cache()
|
| 1647 | {
|
| 1648 | $ar_reset_items = array(
|
| 1649 | 'ar_cache_select' => array(),
|
| 1650 | 'ar_cache_from' => array(),
|
| 1651 | 'ar_cache_join' => array(),
|
| 1652 | 'ar_cache_where' => array(),
|
| 1653 | 'ar_cache_like' => array(),
|
| 1654 | 'ar_cache_groupby' => array(),
|
| 1655 | 'ar_cache_having' =>array(),
|
| 1656 | 'ar_cache_orderby' => array(),
|
| 1657 | 'ar_cache_set' => array()
|
| 1658 | );
|
| 1659 |
|
| 1660 | $this->_reset_run($ar_reset_items);
|
| 1661 | }
|
| 1662 |
|
| 1663 | // --------------------------------------------------------------------
|
| 1664 |
|
| 1665 | /**
|
| 1666 | * Merge Cache
|
| 1667 | *
|
| 1668 | * When called, this function merges any cached AR arrays with
|
| 1669 | * locally called ones.
|
| 1670 | *
|
| 1671 | * @access private
|
| 1672 | * @return void
|
| 1673 | */
|
| 1674 | function _merge_cache()
|
| 1675 | {
|
| 1676 | $ar_items = array('select', 'from', 'join', 'where', 'like', 'groupby', 'having', 'orderby', 'set');
|
| 1677 |
|
| 1678 | foreach ($ar_items as $ar_item)
|
| 1679 | {
|
| 1680 | $ar_cache_item = 'ar_cache_'.$ar_item;
|
| 1681 | $ar_item = 'ar_'.$ar_item;
|
| 1682 | $this->$ar_item = array_unique(array_merge($this->$ar_item, $this->$ar_cache_item));
|
| 1683 | }
|
| 1684 | }
|
| 1685 |
|
| 1686 | // --------------------------------------------------------------------
|
| 1687 |
|
| 1688 | /**
|
| 1689 | * Resets the active record values. Called by the get() function
|
| 1690 | *
|
| 1691 | * @access private
|
| 1692 | * @param array An array of fields to reset
|
| 1693 | * @return void
|
| 1694 | */
|
| 1695 | function _reset_run($ar_reset_items)
|
| 1696 | {
|
| 1697 | foreach ($ar_reset_items as $item => $default_value)
|
| 1698 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1699 | if ( ! in_array($item, $this->ar_store_array))
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1700 | {
|
| 1701 | $this->$item = $default_value;
|
| 1702 | }
|
| 1703 | }
|
| 1704 | }
|
| 1705 |
|
| 1706 | // --------------------------------------------------------------------
|
| 1707 |
|
| 1708 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1709 | * Resets the active record values. Called by the get() function
|
| 1710 | *
|
| 1711 | * @access private
|
| 1712 | * @return void
|
| 1713 | */
|
| 1714 | function _reset_select()
|
| 1715 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1716 | $ar_reset_items = array(
|
| 1717 | 'ar_select' => array(),
|
| 1718 | 'ar_from' => array(),
|
| 1719 | 'ar_join' => array(),
|
| 1720 | 'ar_where' => array(),
|
| 1721 | 'ar_like' => array(),
|
| 1722 | 'ar_groupby' => array(),
|
| 1723 | 'ar_having' => array(),
|
| 1724 | 'ar_orderby' => array(),
|
| 1725 | 'ar_wherein' => array(),
|
| 1726 | 'ar_aliased_tables' => array(),
|
| 1727 | 'ar_distinct' => FALSE,
|
| 1728 | 'ar_limit' => FALSE,
|
| 1729 | 'ar_offset' => FALSE,
|
| 1730 | 'ar_order' => FALSE,
|
| 1731 | );
|
| 1732 |
|
| 1733 | $this->_reset_run($ar_reset_items);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1734 | }
|
| 1735 |
|
| 1736 | // --------------------------------------------------------------------
|
| 1737 |
|
| 1738 | /**
|
| 1739 | * Resets the active record "write" values.
|
| 1740 | *
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1741 | * Called by the insert() update() and delete() functions
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1742 | *
|
| 1743 | * @access private
|
| 1744 | * @return void
|
| 1745 | */
|
| 1746 | function _reset_write()
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1747 | {
|
| 1748 | $ar_reset_items = array(
|
| 1749 | 'ar_set' => array(),
|
| 1750 | 'ar_from' => array(),
|
| 1751 | 'ar_where' => array(),
|
| 1752 | 'ar_like' => array(),
|
| 1753 | 'ar_orderby' => array(),
|
| 1754 | 'ar_limit' => FALSE,
|
| 1755 | 'ar_order' => FALSE
|
| 1756 | );
|
| 1757 |
|
| 1758 | $this->_reset_run($ar_reset_items);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1759 | }
|
| 1760 |
|
| 1761 | }
|
Derek Allard | a632589 | 2008-05-12 17:51:47 +0000 | [diff] [blame] | 1762 |
|
| 1763 | /* End of file DB_active_rec.php */
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 1764 | /* Location: ./system/database/DB_active_rec.php */ |