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