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