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