Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Greg Aker | 0711dc8 | 2011-01-05 10:49:40 -0600 | [diff] [blame^] | 9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 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 |
| 26 | * @author ExpressionEngine Dev Team |
| 27 | * @link http://codeigniter.com/user_guide/database/ |
| 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(); |
Robin Sowell | 43753fd | 2010-09-16 12:52:07 -0400 | [diff] [blame] | 39 | var $ar_keys = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | var $ar_limit = FALSE; |
| 41 | var $ar_offset = FALSE; |
| 42 | var $ar_order = FALSE; |
| 43 | var $ar_orderby = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 44 | var $ar_set = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | var $ar_wherein = array(); |
| 46 | var $ar_aliased_tables = array(); |
| 47 | var $ar_store_array = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 48 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 49 | // Active Record Caching variables |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 50 | var $ar_caching = FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | var $ar_cache_exists = array(); |
| 52 | var $ar_cache_select = array(); |
| 53 | var $ar_cache_from = array(); |
| 54 | var $ar_cache_join = array(); |
| 55 | var $ar_cache_where = array(); |
| 56 | var $ar_cache_like = array(); |
| 57 | var $ar_cache_groupby = array(); |
| 58 | var $ar_cache_having = array(); |
| 59 | var $ar_cache_orderby = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 60 | var $ar_cache_set = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | // -------------------------------------------------------------------- |
| 64 | |
| 65 | /** |
| 66 | * Select |
| 67 | * |
| 68 | * Generates the SELECT portion of the query |
| 69 | * |
| 70 | * @access public |
| 71 | * @param string |
| 72 | * @return object |
| 73 | */ |
| 74 | function select($select = '*', $escape = NULL) |
| 75 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 76 | // Set the global value if this was sepecified |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 77 | if (is_bool($escape)) |
| 78 | { |
| 79 | $this->_protect_identifiers = $escape; |
| 80 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 81 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | if (is_string($select)) |
| 83 | { |
| 84 | $select = explode(',', $select); |
| 85 | } |
| 86 | |
| 87 | foreach ($select as $val) |
| 88 | { |
| 89 | $val = trim($val); |
| 90 | |
| 91 | if ($val != '') |
| 92 | { |
| 93 | $this->ar_select[] = $val; |
| 94 | |
| 95 | if ($this->ar_caching === TRUE) |
| 96 | { |
| 97 | $this->ar_cache_select[] = $val; |
| 98 | $this->ar_cache_exists[] = 'select'; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | return $this; |
| 103 | } |
| 104 | |
| 105 | // -------------------------------------------------------------------- |
| 106 | |
| 107 | /** |
| 108 | * Select Max |
| 109 | * |
| 110 | * Generates a SELECT MAX(field) portion of a query |
| 111 | * |
| 112 | * @access public |
| 113 | * @param string the field |
| 114 | * @param string an alias |
| 115 | * @return object |
| 116 | */ |
| 117 | function select_max($select = '', $alias = '') |
| 118 | { |
| 119 | return $this->_max_min_avg_sum($select, $alias, 'MAX'); |
| 120 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 121 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 122 | // -------------------------------------------------------------------- |
| 123 | |
| 124 | /** |
| 125 | * Select Min |
| 126 | * |
| 127 | * Generates a SELECT MIN(field) portion of a query |
| 128 | * |
| 129 | * @access public |
| 130 | * @param string the field |
| 131 | * @param string an alias |
| 132 | * @return object |
| 133 | */ |
| 134 | function select_min($select = '', $alias = '') |
| 135 | { |
| 136 | return $this->_max_min_avg_sum($select, $alias, 'MIN'); |
| 137 | } |
| 138 | |
| 139 | // -------------------------------------------------------------------- |
| 140 | |
| 141 | /** |
| 142 | * Select Average |
| 143 | * |
| 144 | * Generates a SELECT AVG(field) portion of a query |
| 145 | * |
| 146 | * @access public |
| 147 | * @param string the field |
| 148 | * @param string an alias |
| 149 | * @return object |
| 150 | */ |
| 151 | function select_avg($select = '', $alias = '') |
| 152 | { |
| 153 | return $this->_max_min_avg_sum($select, $alias, 'AVG'); |
| 154 | } |
| 155 | |
| 156 | // -------------------------------------------------------------------- |
| 157 | |
| 158 | /** |
| 159 | * Select Sum |
| 160 | * |
| 161 | * Generates a SELECT SUM(field) portion of a query |
| 162 | * |
| 163 | * @access public |
| 164 | * @param string the field |
| 165 | * @param string an alias |
| 166 | * @return object |
| 167 | */ |
| 168 | function select_sum($select = '', $alias = '') |
| 169 | { |
| 170 | return $this->_max_min_avg_sum($select, $alias, 'SUM'); |
| 171 | } |
| 172 | |
| 173 | // -------------------------------------------------------------------- |
| 174 | |
| 175 | /** |
| 176 | * Processing Function for the four functions above: |
| 177 | * |
| 178 | * select_max() |
| 179 | * select_min() |
| 180 | * select_avg() |
| 181 | * select_sum() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 182 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | * @access public |
| 184 | * @param string the field |
| 185 | * @param string an alias |
| 186 | * @return object |
| 187 | */ |
| 188 | function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX') |
| 189 | { |
| 190 | if ( ! is_string($select) OR $select == '') |
| 191 | { |
| 192 | $this->display_error('db_invalid_query'); |
| 193 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 194 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | $type = strtoupper($type); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 196 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM'))) |
| 198 | { |
| 199 | show_error('Invalid function type: '.$type); |
| 200 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 201 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | if ($alias == '') |
| 203 | { |
| 204 | $alias = $this->_create_alias_from_table(trim($select)); |
| 205 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 206 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$alias; |
| 208 | |
| 209 | $this->ar_select[] = $sql; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 210 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | if ($this->ar_caching === TRUE) |
| 212 | { |
| 213 | $this->ar_cache_select[] = $sql; |
| 214 | $this->ar_cache_exists[] = 'select'; |
| 215 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 216 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 217 | return $this; |
| 218 | } |
| 219 | |
| 220 | // -------------------------------------------------------------------- |
| 221 | |
| 222 | /** |
| 223 | * Determines the alias name based on the table |
| 224 | * |
| 225 | * @access private |
| 226 | * @param string |
| 227 | * @return string |
| 228 | */ |
| 229 | function _create_alias_from_table($item) |
| 230 | { |
| 231 | if (strpos($item, '.') !== FALSE) |
| 232 | { |
| 233 | return end(explode('.', $item)); |
| 234 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 235 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | return $item; |
| 237 | } |
| 238 | |
| 239 | // -------------------------------------------------------------------- |
| 240 | |
| 241 | /** |
| 242 | * DISTINCT |
| 243 | * |
| 244 | * Sets a flag which tells the query string compiler to add DISTINCT |
| 245 | * |
| 246 | * @access public |
| 247 | * @param bool |
| 248 | * @return object |
| 249 | */ |
| 250 | function distinct($val = TRUE) |
| 251 | { |
| 252 | $this->ar_distinct = (is_bool($val)) ? $val : TRUE; |
| 253 | return $this; |
| 254 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 255 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | // -------------------------------------------------------------------- |
| 257 | |
| 258 | /** |
| 259 | * From |
| 260 | * |
| 261 | * Generates the FROM portion of the query |
| 262 | * |
| 263 | * @access public |
| 264 | * @param mixed can be a string or array |
| 265 | * @return object |
| 266 | */ |
| 267 | function from($from) |
| 268 | { |
| 269 | foreach ((array)$from as $val) |
| 270 | { |
| 271 | if (strpos($val, ',') !== FALSE) |
| 272 | { |
| 273 | foreach (explode(',', $val) as $v) |
| 274 | { |
| 275 | $v = trim($v); |
| 276 | $this->_track_aliases($v); |
| 277 | |
| 278 | $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 279 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | if ($this->ar_caching === TRUE) |
| 281 | { |
| 282 | $this->ar_cache_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE); |
| 283 | $this->ar_cache_exists[] = 'from'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 284 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | $val = trim($val); |
| 291 | |
| 292 | // Extract any aliases that might exist. We use this information |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 293 | // in the _protect_identifiers to know whether to add a table prefix |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | $this->_track_aliases($val); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 295 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 297 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | if ($this->ar_caching === TRUE) |
| 299 | { |
| 300 | $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE); |
| 301 | $this->ar_cache_exists[] = 'from'; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | return $this; |
| 307 | } |
| 308 | |
| 309 | // -------------------------------------------------------------------- |
| 310 | |
| 311 | /** |
| 312 | * Join |
| 313 | * |
| 314 | * Generates the JOIN portion of the query |
| 315 | * |
| 316 | * @access public |
| 317 | * @param string |
| 318 | * @param string the join condition |
| 319 | * @param string the type of join |
| 320 | * @return object |
| 321 | */ |
| 322 | function join($table, $cond, $type = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 323 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 324 | if ($type != '') |
| 325 | { |
| 326 | $type = strtoupper(trim($type)); |
| 327 | |
| 328 | if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'))) |
| 329 | { |
| 330 | $type = ''; |
| 331 | } |
| 332 | else |
| 333 | { |
| 334 | $type .= ' '; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // Extract any aliases that might exist. We use this information |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 339 | // in the _protect_identifiers to know whether to add a table prefix |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | $this->_track_aliases($table); |
| 341 | |
| 342 | // Strip apart the condition and protect the identifiers |
| 343 | if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match)) |
| 344 | { |
| 345 | $match[1] = $this->_protect_identifiers($match[1]); |
| 346 | $match[3] = $this->_protect_identifiers($match[3]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 347 | |
| 348 | $cond = $match[1].$match[2].$match[3]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 349 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 350 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 351 | // Assemble the JOIN statement |
| 352 | $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond; |
| 353 | |
| 354 | $this->ar_join[] = $join; |
| 355 | if ($this->ar_caching === TRUE) |
| 356 | { |
| 357 | $this->ar_cache_join[] = $join; |
| 358 | $this->ar_cache_exists[] = 'join'; |
| 359 | } |
| 360 | |
| 361 | return $this; |
| 362 | } |
| 363 | |
| 364 | // -------------------------------------------------------------------- |
| 365 | |
| 366 | /** |
| 367 | * Where |
| 368 | * |
| 369 | * Generates the WHERE portion of the query. Separates |
| 370 | * multiple calls with AND |
| 371 | * |
| 372 | * @access public |
| 373 | * @param mixed |
| 374 | * @param mixed |
| 375 | * @return object |
| 376 | */ |
| 377 | function where($key, $value = NULL, $escape = TRUE) |
| 378 | { |
| 379 | return $this->_where($key, $value, 'AND ', $escape); |
| 380 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 381 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | // -------------------------------------------------------------------- |
| 383 | |
| 384 | /** |
| 385 | * OR Where |
| 386 | * |
| 387 | * Generates the WHERE portion of the query. Separates |
| 388 | * multiple calls with OR |
| 389 | * |
| 390 | * @access public |
| 391 | * @param mixed |
| 392 | * @param mixed |
| 393 | * @return object |
| 394 | */ |
| 395 | function or_where($key, $value = NULL, $escape = TRUE) |
| 396 | { |
| 397 | return $this->_where($key, $value, 'OR ', $escape); |
| 398 | } |
| 399 | |
| 400 | // -------------------------------------------------------------------- |
| 401 | |
| 402 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | * Where |
| 404 | * |
| 405 | * Called by where() or orwhere() |
| 406 | * |
| 407 | * @access private |
| 408 | * @param mixed |
| 409 | * @param mixed |
| 410 | * @param string |
| 411 | * @return object |
| 412 | */ |
| 413 | function _where($key, $value = NULL, $type = 'AND ', $escape = NULL) |
| 414 | { |
| 415 | if ( ! is_array($key)) |
| 416 | { |
| 417 | $key = array($key => $value); |
| 418 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 419 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 420 | // If the escape value was not set will will base it on the global setting |
| 421 | if ( ! is_bool($escape)) |
| 422 | { |
| 423 | $escape = $this->_protect_identifiers; |
| 424 | } |
| 425 | |
| 426 | foreach ($key as $k => $v) |
| 427 | { |
| 428 | $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type; |
| 429 | |
| 430 | if (is_null($v) && ! $this->_has_operator($k)) |
| 431 | { |
| 432 | // value appears not to have been set, assign the test to IS NULL |
| 433 | $k .= ' IS NULL'; |
| 434 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 435 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | if ( ! is_null($v)) |
| 437 | { |
| 438 | if ($escape === TRUE) |
| 439 | { |
| 440 | $k = $this->_protect_identifiers($k, FALSE, $escape); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 441 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 442 | $v = ' '.$this->escape($v); |
| 443 | } |
| 444 | |
| 445 | if ( ! $this->_has_operator($k)) |
| 446 | { |
| 447 | $k .= ' ='; |
| 448 | } |
| 449 | } |
| 450 | else |
| 451 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 452 | $k = $this->_protect_identifiers($k, FALSE, $escape); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | $this->ar_where[] = $prefix.$k.$v; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 456 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 457 | if ($this->ar_caching === TRUE) |
| 458 | { |
| 459 | $this->ar_cache_where[] = $prefix.$k.$v; |
| 460 | $this->ar_cache_exists[] = 'where'; |
| 461 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 462 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 463 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 464 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | return $this; |
| 466 | } |
| 467 | |
| 468 | // -------------------------------------------------------------------- |
| 469 | |
| 470 | /** |
| 471 | * Where_in |
| 472 | * |
| 473 | * Generates a WHERE field IN ('item', 'item') SQL query joined with |
| 474 | * AND if appropriate |
| 475 | * |
| 476 | * @access public |
| 477 | * @param string The field to search |
| 478 | * @param array The values searched on |
| 479 | * @return object |
| 480 | */ |
| 481 | function where_in($key = NULL, $values = NULL) |
| 482 | { |
| 483 | return $this->_where_in($key, $values); |
| 484 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 485 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | * @return object |
| 498 | */ |
| 499 | function or_where_in($key = NULL, $values = NULL) |
| 500 | { |
| 501 | return $this->_where_in($key, $values, FALSE, 'OR '); |
| 502 | } |
| 503 | |
| 504 | // -------------------------------------------------------------------- |
| 505 | |
| 506 | /** |
| 507 | * Where_not_in |
| 508 | * |
| 509 | * Generates a WHERE field NOT IN ('item', 'item') SQL query joined |
| 510 | * with AND if appropriate |
| 511 | * |
| 512 | * @access public |
| 513 | * @param string The field to search |
| 514 | * @param array The values searched on |
| 515 | * @return object |
| 516 | */ |
| 517 | function where_not_in($key = NULL, $values = NULL) |
| 518 | { |
| 519 | return $this->_where_in($key, $values, TRUE); |
| 520 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 521 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 522 | // -------------------------------------------------------------------- |
| 523 | |
| 524 | /** |
| 525 | * Where_not_in_or |
| 526 | * |
| 527 | * Generates a WHERE field NOT IN ('item', 'item') SQL query joined |
| 528 | * with OR if appropriate |
| 529 | * |
| 530 | * @access public |
| 531 | * @param string The field to search |
| 532 | * @param array The values searched on |
| 533 | * @return object |
| 534 | */ |
| 535 | function or_where_not_in($key = NULL, $values = NULL) |
| 536 | { |
| 537 | return $this->_where_in($key, $values, TRUE, 'OR '); |
| 538 | } |
| 539 | |
| 540 | // -------------------------------------------------------------------- |
| 541 | |
| 542 | /** |
| 543 | * Where_in |
| 544 | * |
| 545 | * Called by where_in, where_in_or, where_not_in, where_not_in_or |
| 546 | * |
| 547 | * @access public |
| 548 | * @param string The field to search |
| 549 | * @param array The values searched on |
| 550 | * @param boolean If the statement would be IN or NOT IN |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 551 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 552 | * @return object |
| 553 | */ |
| 554 | function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ') |
| 555 | { |
| 556 | if ($key === NULL OR $values === NULL) |
| 557 | { |
| 558 | return; |
| 559 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 560 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 561 | if ( ! is_array($values)) |
| 562 | { |
| 563 | $values = array($values); |
| 564 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 565 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 566 | $not = ($not) ? ' NOT' : ''; |
| 567 | |
| 568 | foreach ($values as $value) |
| 569 | { |
| 570 | $this->ar_wherein[] = $this->escape($value); |
| 571 | } |
| 572 | |
| 573 | $prefix = (count($this->ar_where) == 0) ? '' : $type; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 574 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 575 | $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") "; |
| 576 | |
| 577 | $this->ar_where[] = $where_in; |
| 578 | if ($this->ar_caching === TRUE) |
| 579 | { |
| 580 | $this->ar_cache_where[] = $where_in; |
| 581 | $this->ar_cache_exists[] = 'where'; |
| 582 | } |
| 583 | |
| 584 | // reset the array for multiple calls |
| 585 | $this->ar_wherein = array(); |
| 586 | return $this; |
| 587 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 588 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +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 | */ |
| 602 | function like($field, $match = '', $side = 'both') |
| 603 | { |
| 604 | return $this->_like($field, $match, 'AND ', $side); |
| 605 | } |
| 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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 624 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +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 | */ |
| 638 | function or_like($field, $match = '', $side = 'both') |
| 639 | { |
| 640 | return $this->_like($field, $match, 'OR ', $side); |
| 641 | } |
| 642 | |
| 643 | // -------------------------------------------------------------------- |
| 644 | |
| 645 | /** |
| 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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 660 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 661 | // -------------------------------------------------------------------- |
| 662 | |
| 663 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 664 | * Like |
| 665 | * |
| 666 | * Called by like() or orlike() |
| 667 | * |
| 668 | * @access private |
| 669 | * @param mixed |
| 670 | * @param mixed |
| 671 | * @param string |
| 672 | * @return object |
| 673 | */ |
| 674 | function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '') |
| 675 | { |
| 676 | if ( ! is_array($field)) |
| 677 | { |
| 678 | $field = array($field => $match); |
| 679 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 680 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 681 | foreach ($field as $k => $v) |
| 682 | { |
| 683 | $k = $this->_protect_identifiers($k); |
| 684 | |
| 685 | $prefix = (count($this->ar_like) == 0) ? '' : $type; |
| 686 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 687 | $v = $this->escape_like_str($v); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 688 | |
| 689 | if ($side == 'before') |
| 690 | { |
| 691 | $like_statement = $prefix." $k $not LIKE '%{$v}'"; |
| 692 | } |
| 693 | elseif ($side == 'after') |
| 694 | { |
| 695 | $like_statement = $prefix." $k $not LIKE '{$v}%'"; |
| 696 | } |
| 697 | else |
| 698 | { |
| 699 | $like_statement = $prefix." $k $not LIKE '%{$v}%'"; |
| 700 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 701 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 702 | // some platforms require an escape sequence definition for LIKE wildcards |
| 703 | if ($this->_like_escape_str != '') |
| 704 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 705 | $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 706 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 707 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 708 | $this->ar_like[] = $like_statement; |
| 709 | if ($this->ar_caching === TRUE) |
| 710 | { |
| 711 | $this->ar_cache_like[] = $like_statement; |
| 712 | $this->ar_cache_exists[] = 'like'; |
| 713 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 714 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 715 | } |
| 716 | return $this; |
| 717 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 718 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 719 | // -------------------------------------------------------------------- |
| 720 | |
| 721 | /** |
| 722 | * GROUP BY |
| 723 | * |
| 724 | * @access public |
| 725 | * @param string |
| 726 | * @return object |
| 727 | */ |
| 728 | function group_by($by) |
| 729 | { |
| 730 | if (is_string($by)) |
| 731 | { |
| 732 | $by = explode(',', $by); |
| 733 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 734 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 735 | foreach ($by as $val) |
| 736 | { |
| 737 | $val = trim($val); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 738 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 739 | if ($val != '') |
| 740 | { |
| 741 | $this->ar_groupby[] = $this->_protect_identifiers($val); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 742 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 743 | if ($this->ar_caching === TRUE) |
| 744 | { |
| 745 | $this->ar_cache_groupby[] = $this->_protect_identifiers($val); |
| 746 | $this->ar_cache_exists[] = 'groupby'; |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | return $this; |
| 751 | } |
| 752 | |
| 753 | // -------------------------------------------------------------------- |
| 754 | |
| 755 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 756 | * Sets the HAVING value |
| 757 | * |
| 758 | * Separates multiple calls with AND |
| 759 | * |
| 760 | * @access public |
| 761 | * @param string |
| 762 | * @param string |
| 763 | * @return object |
| 764 | */ |
| 765 | function having($key, $value = '', $escape = TRUE) |
| 766 | { |
| 767 | return $this->_having($key, $value, 'AND ', $escape); |
| 768 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 769 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 770 | // -------------------------------------------------------------------- |
| 771 | |
| 772 | /** |
| 773 | * Sets the OR HAVING value |
| 774 | * |
| 775 | * Separates multiple calls with OR |
| 776 | * |
| 777 | * @access public |
| 778 | * @param string |
| 779 | * @param string |
| 780 | * @return object |
| 781 | */ |
| 782 | function or_having($key, $value = '', $escape = TRUE) |
| 783 | { |
| 784 | return $this->_having($key, $value, 'OR ', $escape); |
| 785 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 786 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 787 | // -------------------------------------------------------------------- |
| 788 | |
| 789 | /** |
| 790 | * Sets the HAVING values |
| 791 | * |
| 792 | * Called by having() or or_having() |
| 793 | * |
| 794 | * @access private |
| 795 | * @param string |
| 796 | * @param string |
| 797 | * @return object |
| 798 | */ |
| 799 | function _having($key, $value = '', $type = 'AND ', $escape = TRUE) |
| 800 | { |
| 801 | if ( ! is_array($key)) |
| 802 | { |
| 803 | $key = array($key => $value); |
| 804 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 805 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 806 | foreach ($key as $k => $v) |
| 807 | { |
| 808 | $prefix = (count($this->ar_having) == 0) ? '' : $type; |
| 809 | |
| 810 | if ($escape === TRUE) |
| 811 | { |
| 812 | $k = $this->_protect_identifiers($k); |
| 813 | } |
| 814 | |
| 815 | if ( ! $this->_has_operator($k)) |
| 816 | { |
| 817 | $k .= ' = '; |
| 818 | } |
| 819 | |
| 820 | if ($v != '') |
| 821 | { |
| 822 | $v = ' '.$this->escape_str($v); |
| 823 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 824 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 825 | $this->ar_having[] = $prefix.$k.$v; |
| 826 | if ($this->ar_caching === TRUE) |
| 827 | { |
| 828 | $this->ar_cache_having[] = $prefix.$k.$v; |
| 829 | $this->ar_cache_exists[] = 'having'; |
| 830 | } |
| 831 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 832 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 833 | return $this; |
| 834 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 835 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 836 | // -------------------------------------------------------------------- |
| 837 | |
| 838 | /** |
| 839 | * Sets the ORDER BY value |
| 840 | * |
| 841 | * @access public |
| 842 | * @param string |
| 843 | * @param string direction: asc or desc |
| 844 | * @return object |
| 845 | */ |
| 846 | function order_by($orderby, $direction = '') |
| 847 | { |
| 848 | if (strtolower($direction) == 'random') |
| 849 | { |
| 850 | $orderby = ''; // Random results want or don't need a field name |
| 851 | $direction = $this->_random_keyword; |
| 852 | } |
| 853 | elseif (trim($direction) != '') |
| 854 | { |
| 855 | $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC'; |
| 856 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 857 | |
| 858 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 859 | if (strpos($orderby, ',') !== FALSE) |
| 860 | { |
| 861 | $temp = array(); |
| 862 | foreach (explode(',', $orderby) as $part) |
| 863 | { |
| 864 | $part = trim($part); |
| 865 | if ( ! in_array($part, $this->ar_aliased_tables)) |
| 866 | { |
| 867 | $part = $this->_protect_identifiers(trim($part)); |
| 868 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 869 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 870 | $temp[] = $part; |
| 871 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 872 | |
| 873 | $orderby = implode(', ', $temp); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 874 | } |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 875 | else if ($direction != $this->_random_keyword) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 876 | { |
| 877 | $orderby = $this->_protect_identifiers($orderby); |
| 878 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 879 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 880 | $orderby_statement = $orderby.$direction; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 881 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 882 | $this->ar_orderby[] = $orderby_statement; |
| 883 | if ($this->ar_caching === TRUE) |
| 884 | { |
| 885 | $this->ar_cache_orderby[] = $orderby_statement; |
| 886 | $this->ar_cache_exists[] = 'orderby'; |
| 887 | } |
| 888 | |
| 889 | return $this; |
| 890 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 891 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 892 | // -------------------------------------------------------------------- |
| 893 | |
| 894 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 895 | * Sets the LIMIT value |
| 896 | * |
| 897 | * @access public |
| 898 | * @param integer the limit value |
| 899 | * @param integer the offset value |
| 900 | * @return object |
| 901 | */ |
| 902 | function limit($value, $offset = '') |
| 903 | { |
| 904 | $this->ar_limit = $value; |
| 905 | |
| 906 | if ($offset != '') |
| 907 | { |
| 908 | $this->ar_offset = $offset; |
| 909 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 910 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 911 | return $this; |
| 912 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 913 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 914 | // -------------------------------------------------------------------- |
| 915 | |
| 916 | /** |
| 917 | * Sets the OFFSET value |
| 918 | * |
| 919 | * @access public |
| 920 | * @param integer the offset value |
| 921 | * @return object |
| 922 | */ |
| 923 | function offset($offset) |
| 924 | { |
| 925 | $this->ar_offset = $offset; |
| 926 | return $this; |
| 927 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 928 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 929 | // -------------------------------------------------------------------- |
| 930 | |
| 931 | /** |
| 932 | * The "set" function. Allows key/value pairs to be set for inserting or updating |
| 933 | * |
| 934 | * @access public |
| 935 | * @param mixed |
| 936 | * @param string |
| 937 | * @param boolean |
| 938 | * @return object |
| 939 | */ |
| 940 | function set($key, $value = '', $escape = TRUE) |
| 941 | { |
| 942 | $key = $this->_object_to_array($key); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 943 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 944 | if ( ! is_array($key)) |
| 945 | { |
| 946 | $key = array($key => $value); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 947 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 948 | |
| 949 | foreach ($key as $k => $v) |
| 950 | { |
| 951 | if ($escape === FALSE) |
| 952 | { |
| 953 | $this->ar_set[$this->_protect_identifiers($k)] = $v; |
| 954 | } |
| 955 | else |
| 956 | { |
| 957 | $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v); |
| 958 | } |
| 959 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 960 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 961 | return $this; |
| 962 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 963 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 964 | // -------------------------------------------------------------------- |
| 965 | |
| 966 | /** |
| 967 | * Get |
| 968 | * |
| 969 | * Compiles the select statement based on the other functions called |
| 970 | * and runs the query |
| 971 | * |
| 972 | * @access public |
| 973 | * @param string the table |
| 974 | * @param string the limit clause |
| 975 | * @param string the offset clause |
| 976 | * @return object |
| 977 | */ |
| 978 | function get($table = '', $limit = null, $offset = null) |
| 979 | { |
| 980 | if ($table != '') |
| 981 | { |
| 982 | $this->_track_aliases($table); |
| 983 | $this->from($table); |
| 984 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 985 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 986 | if ( ! is_null($limit)) |
| 987 | { |
| 988 | $this->limit($limit, $offset); |
| 989 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 990 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 991 | $sql = $this->_compile_select(); |
| 992 | |
| 993 | $result = $this->query($sql); |
| 994 | $this->_reset_select(); |
| 995 | return $result; |
| 996 | } |
| 997 | |
| 998 | /** |
| 999 | * "Count All Results" query |
| 1000 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1001 | * Generates a platform-specific query string that counts all records |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1002 | * returned by an Active Record query. |
| 1003 | * |
| 1004 | * @access public |
| 1005 | * @param string |
| 1006 | * @return string |
| 1007 | */ |
| 1008 | function count_all_results($table = '') |
| 1009 | { |
| 1010 | if ($table != '') |
| 1011 | { |
| 1012 | $this->_track_aliases($table); |
| 1013 | $this->from($table); |
| 1014 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1015 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1016 | $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows')); |
| 1017 | |
| 1018 | $query = $this->query($sql); |
| 1019 | $this->_reset_select(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1020 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1021 | if ($query->num_rows() == 0) |
| 1022 | { |
| 1023 | return '0'; |
| 1024 | } |
| 1025 | |
| 1026 | $row = $query->row(); |
| 1027 | return $row->numrows; |
| 1028 | } |
| 1029 | |
| 1030 | // -------------------------------------------------------------------- |
| 1031 | |
| 1032 | /** |
| 1033 | * Get_Where |
| 1034 | * |
| 1035 | * Allows the where clause, limit and offset to be added directly |
| 1036 | * |
| 1037 | * @access public |
| 1038 | * @param string the where clause |
| 1039 | * @param string the limit clause |
| 1040 | * @param string the offset clause |
| 1041 | * @return object |
| 1042 | */ |
| 1043 | function get_where($table = '', $where = null, $limit = null, $offset = null) |
| 1044 | { |
| 1045 | if ($table != '') |
| 1046 | { |
| 1047 | $this->from($table); |
| 1048 | } |
| 1049 | |
| 1050 | if ( ! is_null($where)) |
| 1051 | { |
| 1052 | $this->where($where); |
| 1053 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1054 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1055 | if ( ! is_null($limit)) |
| 1056 | { |
| 1057 | $this->limit($limit, $offset); |
| 1058 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1059 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1060 | $sql = $this->_compile_select(); |
| 1061 | |
| 1062 | $result = $this->query($sql); |
| 1063 | $this->_reset_select(); |
| 1064 | return $result; |
| 1065 | } |
| 1066 | |
| 1067 | // -------------------------------------------------------------------- |
| 1068 | |
| 1069 | /** |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1070 | * Insert_Batch |
| 1071 | * |
| 1072 | * Compiles batch insert strings and runs the queries |
| 1073 | * |
| 1074 | * @access public |
| 1075 | * @param string the table to retrieve the results from |
| 1076 | * @param array an associative array of insert values |
| 1077 | * @return object |
| 1078 | */ |
| 1079 | function insert_batch($table = '', $set = NULL) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1080 | { |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1081 | if ( ! is_null($set)) |
| 1082 | { |
| 1083 | $this->set_insert_batch($set); |
| 1084 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1085 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1086 | if (count($this->ar_set) == 0) |
| 1087 | { |
| 1088 | if ($this->db_debug) |
| 1089 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1090 | //No valid data array. Folds in cases where keys and values did not match up |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1091 | return $this->display_error('db_must_use_set'); |
| 1092 | } |
| 1093 | return FALSE; |
| 1094 | } |
| 1095 | |
| 1096 | if ($table == '') |
| 1097 | { |
| 1098 | if ( ! isset($this->ar_from[0])) |
| 1099 | { |
| 1100 | if ($this->db_debug) |
| 1101 | { |
| 1102 | return $this->display_error('db_must_set_table'); |
| 1103 | } |
| 1104 | return FALSE; |
| 1105 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1106 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1107 | $table = $this->ar_from[0]; |
| 1108 | } |
| 1109 | |
| 1110 | // Batch this baby |
| 1111 | for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100) |
| 1112 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1113 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1114 | $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100)); |
| 1115 | |
| 1116 | //echo $sql; |
| 1117 | |
| 1118 | $this->query($sql); |
| 1119 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1120 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1121 | $this->_reset_write(); |
| 1122 | |
| 1123 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1124 | return TRUE; |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | // -------------------------------------------------------------------- |
| 1128 | |
| 1129 | /** |
| 1130 | * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts |
| 1131 | * |
| 1132 | * @access public |
| 1133 | * @param mixed |
| 1134 | * @param string |
| 1135 | * @param boolean |
| 1136 | * @return object |
| 1137 | */ |
| 1138 | |
| 1139 | function set_insert_batch($key, $value = '', $escape = TRUE) |
| 1140 | { |
| 1141 | $key = $this->_object_to_array_batch($key); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1142 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1143 | if ( ! is_array($key)) |
| 1144 | { |
| 1145 | $key = array($key => $value); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1146 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1147 | |
| 1148 | $keys = array_keys(current($key)); |
| 1149 | sort($keys); |
| 1150 | |
| 1151 | foreach ($key as $row) |
| 1152 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1153 | if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0) |
| 1154 | { |
| 1155 | // batch function above returns an error on an empty array |
| 1156 | $this->ar_set[] = array(); |
| 1157 | return; |
| 1158 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1159 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1160 | ksort($row); // puts $row in the same order as our keys |
| 1161 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1162 | if ($escape === FALSE) |
| 1163 | { |
| 1164 | $this->ar_set[] = '('.implode(',', $row).')'; |
| 1165 | } |
| 1166 | else |
| 1167 | { |
| 1168 | $clean = array(); |
| 1169 | |
| 1170 | foreach($row as $value) |
| 1171 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1172 | $clean[] = $this->escape($value); |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1173 | } |
| 1174 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1175 | $this->ar_set[] = '('.implode(',', $clean).')'; |
| 1176 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | foreach ($keys as $k) |
| 1180 | { |
| 1181 | $this->ar_keys[] = $this->_protect_identifiers($k); |
| 1182 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1183 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1184 | return $this; |
| 1185 | } |
| 1186 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1187 | // -------------------------------------------------------------------- |
| 1188 | |
| 1189 | /** |
| 1190 | * Insert |
| 1191 | * |
| 1192 | * Compiles an insert string and runs the query |
| 1193 | * |
| 1194 | * @access public |
| 1195 | * @param string the table to retrieve the results from |
| 1196 | * @param array an associative array of insert values |
| 1197 | * @return object |
| 1198 | */ |
| 1199 | function insert($table = '', $set = NULL) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1200 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1201 | if ( ! is_null($set)) |
| 1202 | { |
| 1203 | $this->set($set); |
| 1204 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1205 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1206 | if (count($this->ar_set) == 0) |
| 1207 | { |
| 1208 | if ($this->db_debug) |
| 1209 | { |
| 1210 | return $this->display_error('db_must_use_set'); |
| 1211 | } |
| 1212 | return FALSE; |
| 1213 | } |
| 1214 | |
| 1215 | if ($table == '') |
| 1216 | { |
| 1217 | if ( ! isset($this->ar_from[0])) |
| 1218 | { |
| 1219 | if ($this->db_debug) |
| 1220 | { |
| 1221 | return $this->display_error('db_must_set_table'); |
| 1222 | } |
| 1223 | return FALSE; |
| 1224 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1225 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1226 | $table = $this->ar_from[0]; |
| 1227 | } |
| 1228 | |
| 1229 | $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1230 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1231 | $this->_reset_write(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1232 | return $this->query($sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1233 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1234 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1235 | function replace($table = '', $set = NULL) |
| 1236 | { |
| 1237 | if ( ! is_null($set)) |
| 1238 | { |
| 1239 | $this->set($set); |
| 1240 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1241 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1242 | if (count($this->ar_set) == 0) |
| 1243 | { |
| 1244 | if ($this->db_debug) |
| 1245 | { |
| 1246 | return $this->display_error('db_must_use_set'); |
| 1247 | } |
| 1248 | return FALSE; |
| 1249 | } |
| 1250 | |
| 1251 | if ($table == '') |
| 1252 | { |
| 1253 | if ( ! isset($this->ar_from[0])) |
| 1254 | { |
| 1255 | if ($this->db_debug) |
| 1256 | { |
| 1257 | return $this->display_error('db_must_set_table'); |
| 1258 | } |
| 1259 | return FALSE; |
| 1260 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1261 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1262 | $table = $this->ar_from[0]; |
| 1263 | } |
| 1264 | |
| 1265 | $sql = $this->_replace($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1266 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1267 | $this->_reset_write(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1268 | return $this->query($sql); |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1269 | } |
| 1270 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1271 | // -------------------------------------------------------------------- |
| 1272 | |
| 1273 | /** |
| 1274 | * Update |
| 1275 | * |
| 1276 | * Compiles an update string and runs the query |
| 1277 | * |
| 1278 | * @access public |
| 1279 | * @param string the table to retrieve the results from |
| 1280 | * @param array an associative array of update values |
| 1281 | * @param mixed the where clause |
| 1282 | * @return object |
| 1283 | */ |
| 1284 | function update($table = '', $set = NULL, $where = NULL, $limit = NULL) |
| 1285 | { |
| 1286 | // Combine any cached components with the current statements |
| 1287 | $this->_merge_cache(); |
| 1288 | |
| 1289 | if ( ! is_null($set)) |
| 1290 | { |
| 1291 | $this->set($set); |
| 1292 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1293 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1294 | if (count($this->ar_set) == 0) |
| 1295 | { |
| 1296 | if ($this->db_debug) |
| 1297 | { |
| 1298 | return $this->display_error('db_must_use_set'); |
| 1299 | } |
| 1300 | return FALSE; |
| 1301 | } |
| 1302 | |
| 1303 | if ($table == '') |
| 1304 | { |
| 1305 | if ( ! isset($this->ar_from[0])) |
| 1306 | { |
| 1307 | if ($this->db_debug) |
| 1308 | { |
| 1309 | return $this->display_error('db_must_set_table'); |
| 1310 | } |
| 1311 | return FALSE; |
| 1312 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1313 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1314 | $table = $this->ar_from[0]; |
| 1315 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1316 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1317 | if ($where != NULL) |
| 1318 | { |
| 1319 | $this->where($where); |
| 1320 | } |
| 1321 | |
| 1322 | if ($limit != NULL) |
| 1323 | { |
| 1324 | $this->limit($limit); |
| 1325 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1326 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1327 | $sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1328 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1329 | $this->_reset_write(); |
| 1330 | return $this->query($sql); |
| 1331 | } |
| 1332 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1333 | |
| 1334 | // -------------------------------------------------------------------- |
| 1335 | |
| 1336 | /** |
| 1337 | * Update_Batch |
| 1338 | * |
| 1339 | * Compiles an update string and runs the query |
| 1340 | * |
| 1341 | * @access public |
| 1342 | * @param string the table to retrieve the results from |
| 1343 | * @param array an associative array of update values |
| 1344 | * @param string the where key |
| 1345 | * @return object |
| 1346 | */ |
| 1347 | function update_batch($table = '', $set = NULL, $index = NULL) |
| 1348 | { |
| 1349 | // Combine any cached components with the current statements |
| 1350 | $this->_merge_cache(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1351 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1352 | if (is_null($index)) |
| 1353 | { |
| 1354 | if ($this->db_debug) |
| 1355 | { |
| 1356 | return $this->display_error('db_myst_use_index'); |
| 1357 | } |
| 1358 | |
| 1359 | return FALSE; |
| 1360 | } |
| 1361 | |
| 1362 | if ( ! is_null($set)) |
| 1363 | { |
| 1364 | $this->set_update_batch($set, $index); |
| 1365 | } |
| 1366 | |
| 1367 | if (count($this->ar_set) == 0) |
| 1368 | { |
| 1369 | if ($this->db_debug) |
| 1370 | { |
| 1371 | return $this->display_error('db_must_use_set'); |
| 1372 | } |
| 1373 | |
| 1374 | return FALSE; |
| 1375 | } |
| 1376 | |
| 1377 | if ($table == '') |
| 1378 | { |
| 1379 | if ( ! isset($this->ar_from[0])) |
| 1380 | { |
| 1381 | if ($this->db_debug) |
| 1382 | { |
| 1383 | return $this->display_error('db_must_set_table'); |
| 1384 | } |
| 1385 | return FALSE; |
| 1386 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1387 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1388 | $table = $this->ar_from[0]; |
| 1389 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1390 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1391 | // Batch this baby |
| 1392 | for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100) |
| 1393 | { |
| 1394 | $sql = $this->_update_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->ar_set, $i, 100), $this->_protect_identifiers($index), $this->ar_where); |
| 1395 | |
| 1396 | $this->query($sql); |
| 1397 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1398 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1399 | $this->_reset_write(); |
| 1400 | } |
| 1401 | |
| 1402 | // -------------------------------------------------------------------- |
| 1403 | |
| 1404 | /** |
| 1405 | * The "set_update_batch" function. Allows key/value pairs to be set for batch updating |
| 1406 | * |
| 1407 | * @access public |
| 1408 | * @param array |
| 1409 | * @param string |
| 1410 | * @param boolean |
| 1411 | * @return object |
| 1412 | */ |
| 1413 | |
| 1414 | function set_update_batch($key, $index = '', $escape = TRUE) |
| 1415 | { |
| 1416 | $key = $this->_object_to_array_batch($key); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1417 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1418 | if ( ! is_array($key)) |
| 1419 | { |
| 1420 | // @todo error |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1421 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1422 | |
| 1423 | foreach ($key as $k => $v) |
| 1424 | { |
| 1425 | $index_set = FALSE; |
| 1426 | $clean = array(); |
| 1427 | |
| 1428 | foreach($v as $k2 => $v2) |
| 1429 | { |
| 1430 | if ($k2 == $index) |
| 1431 | { |
| 1432 | $index_set = TRUE; |
| 1433 | } |
| 1434 | else |
| 1435 | { |
| 1436 | $not[] = $k.'-'.$v; |
| 1437 | } |
| 1438 | |
| 1439 | if ($escape === FALSE) |
| 1440 | { |
| 1441 | $clean[$this->_protect_identifiers($k2)] = $v2; |
| 1442 | } |
| 1443 | else |
| 1444 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1445 | $clean[$this->_protect_identifiers($k2)] = $this->escape($v2); |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | if ($index_set == FALSE) |
| 1450 | { |
| 1451 | return $this->display_error('db_batch_missing_index'); |
| 1452 | } |
| 1453 | |
| 1454 | $this->ar_set[] = $clean; |
| 1455 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1456 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1457 | return $this; |
| 1458 | } |
| 1459 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1460 | // -------------------------------------------------------------------- |
| 1461 | |
| 1462 | /** |
| 1463 | * Empty Table |
| 1464 | * |
| 1465 | * Compiles a delete string and runs "DELETE FROM table" |
| 1466 | * |
| 1467 | * @access public |
| 1468 | * @param string the table to empty |
| 1469 | * @return object |
| 1470 | */ |
| 1471 | function empty_table($table = '') |
| 1472 | { |
| 1473 | if ($table == '') |
| 1474 | { |
| 1475 | if ( ! isset($this->ar_from[0])) |
| 1476 | { |
| 1477 | if ($this->db_debug) |
| 1478 | { |
| 1479 | return $this->display_error('db_must_set_table'); |
| 1480 | } |
| 1481 | return FALSE; |
| 1482 | } |
| 1483 | |
| 1484 | $table = $this->ar_from[0]; |
| 1485 | } |
| 1486 | else |
| 1487 | { |
| 1488 | $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); |
| 1489 | } |
| 1490 | |
| 1491 | $sql = $this->_delete($table); |
| 1492 | |
| 1493 | $this->_reset_write(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1494 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1495 | return $this->query($sql); |
| 1496 | } |
| 1497 | |
| 1498 | // -------------------------------------------------------------------- |
| 1499 | |
| 1500 | /** |
| 1501 | * Truncate |
| 1502 | * |
| 1503 | * Compiles a truncate string and runs the query |
| 1504 | * If the database does not support the truncate() command |
| 1505 | * This function maps to "DELETE FROM table" |
| 1506 | * |
| 1507 | * @access public |
| 1508 | * @param string the table to truncate |
| 1509 | * @return object |
| 1510 | */ |
| 1511 | function truncate($table = '') |
| 1512 | { |
| 1513 | if ($table == '') |
| 1514 | { |
| 1515 | if ( ! isset($this->ar_from[0])) |
| 1516 | { |
| 1517 | if ($this->db_debug) |
| 1518 | { |
| 1519 | return $this->display_error('db_must_set_table'); |
| 1520 | } |
| 1521 | return FALSE; |
| 1522 | } |
| 1523 | |
| 1524 | $table = $this->ar_from[0]; |
| 1525 | } |
| 1526 | else |
| 1527 | { |
| 1528 | $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); |
| 1529 | } |
| 1530 | |
| 1531 | $sql = $this->_truncate($table); |
| 1532 | |
| 1533 | $this->_reset_write(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1534 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1535 | return $this->query($sql); |
| 1536 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1537 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1538 | // -------------------------------------------------------------------- |
| 1539 | |
| 1540 | /** |
| 1541 | * Delete |
| 1542 | * |
| 1543 | * Compiles a delete string and runs the query |
| 1544 | * |
| 1545 | * @access public |
| 1546 | * @param mixed the table(s) to delete from. String or array |
| 1547 | * @param mixed the where clause |
| 1548 | * @param mixed the limit clause |
| 1549 | * @param boolean |
| 1550 | * @return object |
| 1551 | */ |
| 1552 | function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE) |
| 1553 | { |
| 1554 | // Combine any cached components with the current statements |
| 1555 | $this->_merge_cache(); |
| 1556 | |
| 1557 | if ($table == '') |
| 1558 | { |
| 1559 | if ( ! isset($this->ar_from[0])) |
| 1560 | { |
| 1561 | if ($this->db_debug) |
| 1562 | { |
| 1563 | return $this->display_error('db_must_set_table'); |
| 1564 | } |
| 1565 | return FALSE; |
| 1566 | } |
| 1567 | |
| 1568 | $table = $this->ar_from[0]; |
| 1569 | } |
| 1570 | elseif (is_array($table)) |
| 1571 | { |
| 1572 | foreach($table as $single_table) |
| 1573 | { |
| 1574 | $this->delete($single_table, $where, $limit, FALSE); |
| 1575 | } |
| 1576 | |
| 1577 | $this->_reset_write(); |
| 1578 | return; |
| 1579 | } |
| 1580 | else |
| 1581 | { |
| 1582 | $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); |
| 1583 | } |
| 1584 | |
| 1585 | if ($where != '') |
| 1586 | { |
| 1587 | $this->where($where); |
| 1588 | } |
| 1589 | |
| 1590 | if ($limit != NULL) |
| 1591 | { |
| 1592 | $this->limit($limit); |
| 1593 | } |
| 1594 | |
Derek Allard | 03d783b | 2009-05-03 19:45:45 +0000 | [diff] [blame] | 1595 | if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1596 | { |
| 1597 | if ($this->db_debug) |
| 1598 | { |
| 1599 | return $this->display_error('db_del_must_use_where'); |
| 1600 | } |
| 1601 | |
| 1602 | return FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1603 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1604 | |
| 1605 | $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit); |
| 1606 | |
| 1607 | if ($reset_data) |
| 1608 | { |
| 1609 | $this->_reset_write(); |
| 1610 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1611 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1612 | return $this->query($sql); |
| 1613 | } |
| 1614 | |
| 1615 | // -------------------------------------------------------------------- |
| 1616 | |
| 1617 | /** |
| 1618 | * DB Prefix |
| 1619 | * |
| 1620 | * Prepends a database prefix if one exists in configuration |
| 1621 | * |
| 1622 | * @access public |
| 1623 | * @param string the table |
| 1624 | * @return string |
| 1625 | */ |
| 1626 | function dbprefix($table = '') |
| 1627 | { |
| 1628 | if ($table == '') |
| 1629 | { |
| 1630 | $this->display_error('db_table_name_required'); |
| 1631 | } |
| 1632 | |
| 1633 | return $this->dbprefix.$table; |
| 1634 | } |
| 1635 | |
| 1636 | // -------------------------------------------------------------------- |
| 1637 | |
| 1638 | /** |
| 1639 | * Track Aliases |
| 1640 | * |
| 1641 | * Used to track SQL statements written with aliased tables. |
| 1642 | * |
| 1643 | * @access private |
| 1644 | * @param string The table to inspect |
| 1645 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1646 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1647 | function _track_aliases($table) |
| 1648 | { |
| 1649 | if (is_array($table)) |
| 1650 | { |
| 1651 | foreach ($table as $t) |
| 1652 | { |
| 1653 | $this->_track_aliases($t); |
| 1654 | } |
| 1655 | return; |
| 1656 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1657 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1658 | // Does the string contain a comma? If so, we need to separate |
| 1659 | // the string into discreet statements |
| 1660 | if (strpos($table, ',') !== FALSE) |
| 1661 | { |
| 1662 | return $this->_track_aliases(explode(',', $table)); |
| 1663 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1664 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1665 | // if a table alias is used we can recognize it by a space |
| 1666 | if (strpos($table, " ") !== FALSE) |
| 1667 | { |
| 1668 | // if the alias is written with the AS keyword, remove it |
| 1669 | $table = preg_replace('/ AS /i', ' ', $table); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1670 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1671 | // Grab the alias |
| 1672 | $table = trim(strrchr($table, " ")); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1673 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1674 | // Store the alias, if it doesn't already exist |
| 1675 | if ( ! in_array($table, $this->ar_aliased_tables)) |
| 1676 | { |
| 1677 | $this->ar_aliased_tables[] = $table; |
| 1678 | } |
| 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | // -------------------------------------------------------------------- |
| 1683 | |
| 1684 | /** |
| 1685 | * Compile the SELECT statement |
| 1686 | * |
| 1687 | * Generates a query string based on which functions were used. |
| 1688 | * Should not be called directly. The get() function calls it. |
| 1689 | * |
| 1690 | * @access private |
| 1691 | * @return string |
| 1692 | */ |
| 1693 | function _compile_select($select_override = FALSE) |
| 1694 | { |
| 1695 | // Combine any cached components with the current statements |
| 1696 | $this->_merge_cache(); |
| 1697 | |
| 1698 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1699 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1700 | // Write the "select" portion of the query |
| 1701 | |
| 1702 | if ($select_override !== FALSE) |
| 1703 | { |
| 1704 | $sql = $select_override; |
| 1705 | } |
| 1706 | else |
| 1707 | { |
| 1708 | $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1709 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1710 | if (count($this->ar_select) == 0) |
| 1711 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1712 | $sql .= '*'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1713 | } |
| 1714 | else |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1715 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1716 | // Cycle through the "select" portion of the query and prep each column name. |
| 1717 | // The reason we protect identifiers here rather then in the select() function |
| 1718 | // is because until the user calls the from() function we don't know if there are aliases |
| 1719 | foreach ($this->ar_select as $key => $val) |
| 1720 | { |
| 1721 | $this->ar_select[$key] = $this->_protect_identifiers($val); |
| 1722 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1723 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1724 | $sql .= implode(', ', $this->ar_select); |
| 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1729 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1730 | // Write the "FROM" portion of the query |
| 1731 | |
| 1732 | if (count($this->ar_from) > 0) |
| 1733 | { |
| 1734 | $sql .= "\nFROM "; |
| 1735 | |
| 1736 | $sql .= $this->_from_tables($this->ar_from); |
| 1737 | } |
| 1738 | |
| 1739 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1740 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1741 | // Write the "JOIN" portion of the query |
| 1742 | |
| 1743 | if (count($this->ar_join) > 0) |
| 1744 | { |
| 1745 | $sql .= "\n"; |
| 1746 | |
| 1747 | $sql .= implode("\n", $this->ar_join); |
| 1748 | } |
| 1749 | |
| 1750 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1751 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1752 | // Write the "WHERE" portion of the query |
| 1753 | |
| 1754 | if (count($this->ar_where) > 0 OR count($this->ar_like) > 0) |
| 1755 | { |
| 1756 | $sql .= "\n"; |
| 1757 | |
| 1758 | $sql .= "WHERE "; |
| 1759 | } |
| 1760 | |
| 1761 | $sql .= implode("\n", $this->ar_where); |
| 1762 | |
| 1763 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1764 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1765 | // Write the "LIKE" portion of the query |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1766 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1767 | if (count($this->ar_like) > 0) |
| 1768 | { |
| 1769 | if (count($this->ar_where) > 0) |
| 1770 | { |
| 1771 | $sql .= "\nAND "; |
| 1772 | } |
| 1773 | |
| 1774 | $sql .= implode("\n", $this->ar_like); |
| 1775 | } |
| 1776 | |
| 1777 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1778 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1779 | // Write the "GROUP BY" portion of the query |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1780 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1781 | if (count($this->ar_groupby) > 0) |
| 1782 | { |
| 1783 | $sql .= "\nGROUP BY "; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1784 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1785 | $sql .= implode(', ', $this->ar_groupby); |
| 1786 | } |
| 1787 | |
| 1788 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1789 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1790 | // Write the "HAVING" portion of the query |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1791 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1792 | if (count($this->ar_having) > 0) |
| 1793 | { |
| 1794 | $sql .= "\nHAVING "; |
| 1795 | $sql .= implode("\n", $this->ar_having); |
| 1796 | } |
| 1797 | |
| 1798 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1799 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1800 | // Write the "ORDER BY" portion of the query |
| 1801 | |
| 1802 | if (count($this->ar_orderby) > 0) |
| 1803 | { |
| 1804 | $sql .= "\nORDER BY "; |
| 1805 | $sql .= implode(', ', $this->ar_orderby); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1806 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1807 | if ($this->ar_order !== FALSE) |
| 1808 | { |
| 1809 | $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1810 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1814 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1815 | // Write the "LIMIT" portion of the query |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1816 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1817 | if (is_numeric($this->ar_limit)) |
| 1818 | { |
| 1819 | $sql .= "\n"; |
| 1820 | $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset); |
| 1821 | } |
| 1822 | |
| 1823 | return $sql; |
| 1824 | } |
| 1825 | |
| 1826 | // -------------------------------------------------------------------- |
| 1827 | |
| 1828 | /** |
| 1829 | * Object to Array |
| 1830 | * |
| 1831 | * Takes an object as input and converts the class variables to array key/vals |
| 1832 | * |
| 1833 | * @access public |
| 1834 | * @param object |
| 1835 | * @return array |
| 1836 | */ |
| 1837 | function _object_to_array($object) |
| 1838 | { |
| 1839 | if ( ! is_object($object)) |
| 1840 | { |
| 1841 | return $object; |
| 1842 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1843 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1844 | $array = array(); |
| 1845 | foreach (get_object_vars($object) as $key => $val) |
| 1846 | { |
| 1847 | // There are some built in keys we need to ignore for this conversion |
Derek Jones | cf57955 | 2010-03-11 09:13:34 -0600 | [diff] [blame] | 1848 | if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1849 | { |
| 1850 | $array[$key] = $val; |
| 1851 | } |
| 1852 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1853 | |
| 1854 | return $array; |
| 1855 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1856 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1857 | // -------------------------------------------------------------------- |
| 1858 | |
| 1859 | /** |
| 1860 | * Object to Array |
| 1861 | * |
| 1862 | * Takes an object as input and converts the class variables to array key/vals |
| 1863 | * |
| 1864 | * @access public |
| 1865 | * @param object |
| 1866 | * @return array |
| 1867 | */ |
| 1868 | function _object_to_array_batch($object) |
| 1869 | { |
| 1870 | if ( ! is_object($object)) |
| 1871 | { |
| 1872 | return $object; |
| 1873 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1874 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1875 | $array = array(); |
| 1876 | $out = get_object_vars($object); |
| 1877 | $fields = array_keys($out); |
| 1878 | |
| 1879 | foreach ($fields as $val) |
| 1880 | { |
| 1881 | // There are some built in keys we need to ignore for this conversion |
Derek Jones | cf57955 | 2010-03-11 09:13:34 -0600 | [diff] [blame] | 1882 | if ($val != '_parent_name') |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1883 | { |
| 1884 | |
| 1885 | $i = 0; |
| 1886 | foreach ($out[$val] as $data) |
| 1887 | { |
| 1888 | $array[$i][$val] = $data; |
| 1889 | $i++; |
| 1890 | } |
| 1891 | } |
| 1892 | } |
| 1893 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1894 | return $array; |
| 1895 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1896 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1897 | // -------------------------------------------------------------------- |
| 1898 | |
| 1899 | /** |
| 1900 | * Start Cache |
| 1901 | * |
| 1902 | * Starts AR caching |
| 1903 | * |
| 1904 | * @access public |
| 1905 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1906 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1907 | function start_cache() |
| 1908 | { |
| 1909 | $this->ar_caching = TRUE; |
| 1910 | } |
| 1911 | |
| 1912 | // -------------------------------------------------------------------- |
| 1913 | |
| 1914 | /** |
| 1915 | * Stop Cache |
| 1916 | * |
| 1917 | * Stops AR caching |
| 1918 | * |
| 1919 | * @access public |
| 1920 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1921 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1922 | function stop_cache() |
| 1923 | { |
| 1924 | $this->ar_caching = FALSE; |
| 1925 | } |
| 1926 | |
| 1927 | // -------------------------------------------------------------------- |
| 1928 | |
| 1929 | /** |
| 1930 | * Flush Cache |
| 1931 | * |
| 1932 | * Empties the AR cache |
| 1933 | * |
| 1934 | * @access public |
| 1935 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1936 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1937 | function flush_cache() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1938 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1939 | $this->_reset_run( |
| 1940 | array( |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1941 | 'ar_cache_select' => array(), |
| 1942 | 'ar_cache_from' => array(), |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1943 | 'ar_cache_join' => array(), |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1944 | 'ar_cache_where' => array(), |
| 1945 | 'ar_cache_like' => array(), |
| 1946 | 'ar_cache_groupby' => array(), |
| 1947 | 'ar_cache_having' => array(), |
| 1948 | 'ar_cache_orderby' => array(), |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1949 | 'ar_cache_set' => array(), |
| 1950 | 'ar_cache_exists' => array() |
| 1951 | ) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1952 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | // -------------------------------------------------------------------- |
| 1956 | |
| 1957 | /** |
| 1958 | * Merge Cache |
| 1959 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1960 | * When called, this function merges any cached AR arrays with |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1961 | * locally called ones. |
| 1962 | * |
| 1963 | * @access private |
| 1964 | * @return void |
| 1965 | */ |
| 1966 | function _merge_cache() |
| 1967 | { |
| 1968 | if (count($this->ar_cache_exists) == 0) |
| 1969 | { |
| 1970 | return; |
| 1971 | } |
| 1972 | |
| 1973 | foreach ($this->ar_cache_exists as $val) |
| 1974 | { |
| 1975 | $ar_variable = 'ar_'.$val; |
| 1976 | $ar_cache_var = 'ar_cache_'.$val; |
| 1977 | |
| 1978 | if (count($this->$ar_cache_var) == 0) |
| 1979 | { |
| 1980 | continue; |
| 1981 | } |
| 1982 | |
| 1983 | $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable)); |
| 1984 | } |
| 1985 | |
| 1986 | // If we are "protecting identifiers" we need to examine the "from" |
| 1987 | // portion of the query to determine if there are any aliases |
| 1988 | if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0) |
| 1989 | { |
| 1990 | $this->_track_aliases($this->ar_from); |
| 1991 | } |
| 1992 | } |
| 1993 | |
| 1994 | // -------------------------------------------------------------------- |
| 1995 | |
| 1996 | /** |
| 1997 | * Resets the active record values. Called by the get() function |
| 1998 | * |
| 1999 | * @access private |
| 2000 | * @param array An array of fields to reset |
| 2001 | * @return void |
| 2002 | */ |
| 2003 | function _reset_run($ar_reset_items) |
| 2004 | { |
| 2005 | foreach ($ar_reset_items as $item => $default_value) |
| 2006 | { |
| 2007 | if ( ! in_array($item, $this->ar_store_array)) |
| 2008 | { |
| 2009 | $this->$item = $default_value; |
| 2010 | } |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | // -------------------------------------------------------------------- |
| 2015 | |
| 2016 | /** |
| 2017 | * Resets the active record values. Called by the get() function |
| 2018 | * |
| 2019 | * @access private |
| 2020 | * @return void |
| 2021 | */ |
| 2022 | function _reset_select() |
| 2023 | { |
| 2024 | $ar_reset_items = array( |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2025 | 'ar_select' => array(), |
| 2026 | 'ar_from' => array(), |
| 2027 | 'ar_join' => array(), |
| 2028 | 'ar_where' => array(), |
| 2029 | 'ar_like' => array(), |
| 2030 | 'ar_groupby' => array(), |
| 2031 | 'ar_having' => array(), |
| 2032 | 'ar_orderby' => array(), |
| 2033 | 'ar_wherein' => array(), |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2034 | 'ar_aliased_tables' => array(), |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2035 | 'ar_distinct' => FALSE, |
| 2036 | 'ar_limit' => FALSE, |
| 2037 | 'ar_offset' => FALSE, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2038 | 'ar_order' => FALSE, |
| 2039 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2040 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2041 | $this->_reset_run($ar_reset_items); |
| 2042 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2043 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2044 | // -------------------------------------------------------------------- |
| 2045 | |
| 2046 | /** |
| 2047 | * Resets the active record "write" values. |
| 2048 | * |
Robin Sowell | 43753fd | 2010-09-16 12:52:07 -0400 | [diff] [blame] | 2049 | * Called by the insert() update() insert_batch() update_batch() and delete() functions |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2050 | * |
| 2051 | * @access private |
| 2052 | * @return void |
| 2053 | */ |
| 2054 | function _reset_write() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2055 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2056 | $ar_reset_items = array( |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2057 | 'ar_set' => array(), |
| 2058 | 'ar_from' => array(), |
| 2059 | 'ar_where' => array(), |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2060 | 'ar_like' => array(), |
Robin Sowell | 43753fd | 2010-09-16 12:52:07 -0400 | [diff] [blame] | 2061 | 'ar_orderby' => array(), |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2062 | 'ar_keys' => array(), |
| 2063 | 'ar_limit' => FALSE, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2064 | 'ar_order' => FALSE |
| 2065 | ); |
| 2066 | |
| 2067 | $this->_reset_run($ar_reset_items); |
| 2068 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2069 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | /* End of file DB_active_rec.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 2073 | /* Location: ./system/database/DB_active_rec.php */ |