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