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