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