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