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