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 |
pporlan | 2c685fb | 2011-12-22 12:15:25 +0100 | [diff] [blame] | 833 | * @param bool enable field name escaping |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 834 | * @return object |
| 835 | */ |
pporlan | 2c685fb | 2011-12-22 12:15:25 +0100 | [diff] [blame] | 836 | public function order_by($orderby, $direction = '', $escape = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 837 | { |
| 838 | if (strtolower($direction) == 'random') |
| 839 | { |
| 840 | $orderby = ''; // Random results want or don't need a field name |
| 841 | $direction = $this->_random_keyword; |
| 842 | } |
| 843 | elseif (trim($direction) != '') |
| 844 | { |
| 845 | $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC'; |
| 846 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 847 | |
| 848 | |
pporlan | 2c685fb | 2011-12-22 12:15:25 +0100 | [diff] [blame] | 849 | if ((strpos($orderby, ',') !== FALSE) && ($escape === TRUE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 850 | { |
| 851 | $temp = array(); |
| 852 | foreach (explode(',', $orderby) as $part) |
| 853 | { |
| 854 | $part = trim($part); |
| 855 | if ( ! in_array($part, $this->ar_aliased_tables)) |
| 856 | { |
| 857 | $part = $this->_protect_identifiers(trim($part)); |
| 858 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 859 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 860 | $temp[] = $part; |
| 861 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 862 | |
| 863 | $orderby = implode(', ', $temp); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 864 | } |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 865 | else if ($direction != $this->_random_keyword) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 866 | { |
pporlan | 2c685fb | 2011-12-22 12:15:25 +0100 | [diff] [blame] | 867 | if ($escape === TRUE) |
| 868 | { |
| 869 | $orderby = $this->_protect_identifiers($orderby); |
| 870 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 871 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 872 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 873 | $orderby_statement = $orderby.$direction; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 874 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 875 | $this->ar_orderby[] = $orderby_statement; |
| 876 | if ($this->ar_caching === TRUE) |
| 877 | { |
| 878 | $this->ar_cache_orderby[] = $orderby_statement; |
| 879 | $this->ar_cache_exists[] = 'orderby'; |
| 880 | } |
| 881 | |
| 882 | return $this; |
| 883 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 884 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 885 | // -------------------------------------------------------------------- |
| 886 | |
| 887 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 888 | * Sets the LIMIT value |
| 889 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 890 | * @param integer the limit value |
| 891 | * @param integer the offset value |
| 892 | * @return object |
| 893 | */ |
Phil Sturgeon | bff3dfd | 2011-09-07 18:54:25 +0200 | [diff] [blame] | 894 | public function limit($value, $offset = NULL) |
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_limit = (int) $value; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 897 | |
Phil Sturgeon | bff3dfd | 2011-09-07 18:54:25 +0200 | [diff] [blame] | 898 | if ( ! is_null($offset)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 899 | { |
Kyle Farris | 7611601 | 2011-08-31 11:17:48 -0400 | [diff] [blame] | 900 | $this->ar_offset = (int) $offset; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 901 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 902 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 903 | return $this; |
| 904 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 905 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 906 | // -------------------------------------------------------------------- |
| 907 | |
| 908 | /** |
| 909 | * Sets the OFFSET value |
| 910 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 911 | * @param integer the offset value |
| 912 | * @return object |
| 913 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 914 | public function offset($offset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 915 | { |
kenjis | 0e85763 | 2011-09-02 08:41:17 +0900 | [diff] [blame] | 916 | $this->ar_offset = (int) $offset; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 917 | return $this; |
| 918 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 919 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 920 | // -------------------------------------------------------------------- |
| 921 | |
| 922 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 923 | * 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] | 924 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 925 | * @param mixed |
| 926 | * @param string |
| 927 | * @param boolean |
| 928 | * @return object |
| 929 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 930 | public function set($key, $value = '', $escape = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 931 | { |
| 932 | $key = $this->_object_to_array($key); |
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 | if ( ! is_array($key)) |
| 935 | { |
| 936 | $key = array($key => $value); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 937 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 938 | |
| 939 | foreach ($key as $k => $v) |
| 940 | { |
| 941 | if ($escape === FALSE) |
| 942 | { |
| 943 | $this->ar_set[$this->_protect_identifiers($k)] = $v; |
| 944 | } |
| 945 | else |
| 946 | { |
Phil Sturgeon | d0ac1a2 | 2011-02-15 22:54:08 +0000 | [diff] [blame] | 947 | $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 948 | } |
| 949 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 950 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 951 | return $this; |
| 952 | } |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 953 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 954 | // -------------------------------------------------------------------- |
| 955 | |
| 956 | /** |
| 957 | * Get SELECT query string |
| 958 | * |
| 959 | * Compiles a SELECT query string and returns the sql. |
| 960 | * |
| 961 | * @access public |
| 962 | * @param string the table name to select from (optional) |
| 963 | * @param boolean TRUE: resets AR values; FALSE: leave AR vaules alone |
| 964 | * @return string |
| 965 | */ |
| 966 | public function get_compiled_select($table = '', $reset = TRUE) |
| 967 | { |
| 968 | if ($table != '') |
kylefarris | 0a3176b | 2011-08-26 02:37:52 -0400 | [diff] [blame] | 969 | { |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 970 | $this->_track_aliases($table); |
| 971 | $this->from($table); |
| 972 | } |
| 973 | |
| 974 | $select = $this->_compile_select(); |
| 975 | |
| 976 | if ($reset === TRUE) |
| 977 | { |
| 978 | $this->_reset_select(); |
| 979 | } |
| 980 | |
| 981 | return $select; |
| 982 | } |
| 983 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 984 | // -------------------------------------------------------------------- |
| 985 | |
| 986 | /** |
| 987 | * Get |
| 988 | * |
| 989 | * Compiles the select statement based on the other functions called |
| 990 | * and runs the query |
| 991 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 992 | * @param string the table |
| 993 | * @param string the limit clause |
| 994 | * @param string the offset clause |
| 995 | * @return object |
| 996 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 997 | public function get($table = '', $limit = null, $offset = null) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 998 | { |
| 999 | if ($table != '') |
| 1000 | { |
| 1001 | $this->_track_aliases($table); |
| 1002 | $this->from($table); |
| 1003 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1004 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1005 | if ( ! is_null($limit)) |
| 1006 | { |
| 1007 | $this->limit($limit, $offset); |
| 1008 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1009 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1010 | $sql = $this->_compile_select(); |
| 1011 | |
| 1012 | $result = $this->query($sql); |
| 1013 | $this->_reset_select(); |
| 1014 | return $result; |
| 1015 | } |
| 1016 | |
| 1017 | /** |
| 1018 | * "Count All Results" query |
| 1019 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1020 | * Generates a platform-specific query string that counts all records |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1021 | * returned by an Active Record query. |
| 1022 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1023 | * @param string |
| 1024 | * @return string |
| 1025 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1026 | public function count_all_results($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1027 | { |
| 1028 | if ($table != '') |
| 1029 | { |
| 1030 | $this->_track_aliases($table); |
| 1031 | $this->from($table); |
| 1032 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1033 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1034 | $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows')); |
| 1035 | |
| 1036 | $query = $this->query($sql); |
| 1037 | $this->_reset_select(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1038 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1039 | if ($query->num_rows() == 0) |
| 1040 | { |
Phil Sturgeon | af6f344 | 2011-03-22 19:12:23 +0000 | [diff] [blame] | 1041 | return 0; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | $row = $query->row(); |
Phil Sturgeon | af6f344 | 2011-03-22 19:12:23 +0000 | [diff] [blame] | 1045 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | // -------------------------------------------------------------------- |
| 1049 | |
| 1050 | /** |
| 1051 | * Get_Where |
| 1052 | * |
| 1053 | * Allows the where clause, limit and offset to be added directly |
| 1054 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1055 | * @param string the where clause |
| 1056 | * @param string the limit clause |
| 1057 | * @param string the offset clause |
| 1058 | * @return object |
| 1059 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1060 | public function get_where($table = '', $where = null, $limit = null, $offset = null) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1061 | { |
| 1062 | if ($table != '') |
| 1063 | { |
| 1064 | $this->from($table); |
| 1065 | } |
| 1066 | |
| 1067 | if ( ! is_null($where)) |
| 1068 | { |
| 1069 | $this->where($where); |
| 1070 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1071 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1072 | if ( ! is_null($limit)) |
| 1073 | { |
| 1074 | $this->limit($limit, $offset); |
| 1075 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1076 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1077 | $sql = $this->_compile_select(); |
| 1078 | |
| 1079 | $result = $this->query($sql); |
| 1080 | $this->_reset_select(); |
| 1081 | return $result; |
| 1082 | } |
| 1083 | |
| 1084 | // -------------------------------------------------------------------- |
| 1085 | |
| 1086 | /** |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1087 | * Insert_Batch |
| 1088 | * |
| 1089 | * Compiles batch insert strings and runs the queries |
| 1090 | * |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1091 | * @param string the table to retrieve the results from |
| 1092 | * @param array an associative array of insert values |
| 1093 | * @return object |
| 1094 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1095 | public function insert_batch($table = '', $set = NULL) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1096 | { |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1097 | if ( ! is_null($set)) |
| 1098 | { |
| 1099 | $this->set_insert_batch($set); |
| 1100 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1101 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1102 | if (count($this->ar_set) == 0) |
| 1103 | { |
| 1104 | if ($this->db_debug) |
| 1105 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1106 | //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] | 1107 | return $this->display_error('db_must_use_set'); |
| 1108 | } |
| 1109 | return FALSE; |
| 1110 | } |
| 1111 | |
| 1112 | if ($table == '') |
| 1113 | { |
| 1114 | if ( ! isset($this->ar_from[0])) |
| 1115 | { |
| 1116 | if ($this->db_debug) |
| 1117 | { |
| 1118 | return $this->display_error('db_must_set_table'); |
| 1119 | } |
| 1120 | return FALSE; |
| 1121 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1122 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1123 | $table = $this->ar_from[0]; |
| 1124 | } |
| 1125 | |
| 1126 | // Batch this baby |
| 1127 | for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100) |
| 1128 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1129 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1130 | $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100)); |
| 1131 | |
| 1132 | //echo $sql; |
| 1133 | |
| 1134 | $this->query($sql); |
| 1135 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1136 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1137 | $this->_reset_write(); |
| 1138 | |
| 1139 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1140 | return TRUE; |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | // -------------------------------------------------------------------- |
| 1144 | |
| 1145 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1146 | * 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] | 1147 | * |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1148 | * @param mixed |
| 1149 | * @param string |
| 1150 | * @param boolean |
| 1151 | * @return object |
| 1152 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1153 | public function set_insert_batch($key, $value = '', $escape = TRUE) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1154 | { |
| 1155 | $key = $this->_object_to_array_batch($key); |
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 | if ( ! is_array($key)) |
| 1158 | { |
| 1159 | $key = array($key => $value); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1160 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1161 | |
| 1162 | $keys = array_keys(current($key)); |
| 1163 | sort($keys); |
| 1164 | |
| 1165 | foreach ($key as $row) |
| 1166 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1167 | if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0) |
| 1168 | { |
| 1169 | // batch function above returns an error on an empty array |
| 1170 | $this->ar_set[] = array(); |
| 1171 | return; |
| 1172 | } |
Phil Sturgeon | d0ac1a2 | 2011-02-15 22:54:08 +0000 | [diff] [blame] | 1173 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1174 | ksort($row); // puts $row in the same order as our keys |
| 1175 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1176 | if ($escape === FALSE) |
| 1177 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1178 | $this->ar_set[] = '('.implode(',', $row).')'; |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1179 | } |
| 1180 | else |
| 1181 | { |
| 1182 | $clean = array(); |
| 1183 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 1184 | foreach ($row as $value) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1185 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1186 | $clean[] = $this->escape($value); |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1187 | } |
| 1188 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1189 | $this->ar_set[] = '('.implode(',', $clean).')'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1190 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | foreach ($keys as $k) |
| 1194 | { |
| 1195 | $this->ar_keys[] = $this->_protect_identifiers($k); |
| 1196 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1197 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1198 | return $this; |
| 1199 | } |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1200 | |
| 1201 | // -------------------------------------------------------------------- |
| 1202 | |
| 1203 | /** |
| 1204 | * Get INSERT query string |
| 1205 | * |
| 1206 | * Compiles an insert query and returns the sql |
| 1207 | * |
| 1208 | * @access public |
| 1209 | * @param string the table to insert into |
| 1210 | * @param boolean TRUE: reset AR values; FALSE: leave AR values alone |
| 1211 | * @return string |
| 1212 | */ |
| 1213 | public function get_compiled_insert($table = '', $reset = TRUE) |
| 1214 | { |
| 1215 | if ($this->_validate_insert($table) === FALSE) |
| 1216 | { |
| 1217 | return FALSE; |
| 1218 | } |
| 1219 | |
Kyle Farris | 7611601 | 2011-08-31 11:17:48 -0400 | [diff] [blame] | 1220 | $sql = $this->_insert( |
| 1221 | $this->_protect_identifiers( |
| 1222 | $this->ar_from[0], TRUE, NULL, FALSE |
| 1223 | ), |
| 1224 | array_keys($this->ar_set), |
| 1225 | array_values($this->ar_set) |
| 1226 | ); |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1227 | |
| 1228 | if ($reset === TRUE) |
| 1229 | { |
| 1230 | $this->_reset_write(); |
| 1231 | } |
| 1232 | |
| 1233 | return $sql; |
| 1234 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1235 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1236 | // -------------------------------------------------------------------- |
| 1237 | |
| 1238 | /** |
| 1239 | * Insert |
| 1240 | * |
| 1241 | * Compiles an insert string and runs the query |
| 1242 | * |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1243 | * @access public |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1244 | * @param string the table to insert data into |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1245 | * @param array an associative array of insert values |
| 1246 | * @return object |
| 1247 | */ |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1248 | public function insert($table = '', $set = NULL) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1249 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1250 | if ( ! is_null($set)) |
| 1251 | { |
| 1252 | $this->set($set); |
| 1253 | } |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1254 | |
| 1255 | if ($this->_validate_insert($table) === FALSE) |
| 1256 | { |
| 1257 | return FALSE; |
| 1258 | } |
| 1259 | |
Kyle Farris | 7611601 | 2011-08-31 11:17:48 -0400 | [diff] [blame] | 1260 | $sql = $this->_insert( |
| 1261 | $this->_protect_identifiers( |
| 1262 | $this->ar_from[0], TRUE, NULL, FALSE |
| 1263 | ), |
| 1264 | array_keys($this->ar_set), |
| 1265 | array_values($this->ar_set) |
| 1266 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1267 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1268 | $this->_reset_write(); |
| 1269 | return $this->query($sql); |
| 1270 | } |
| 1271 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1272 | // -------------------------------------------------------------------- |
| 1273 | |
| 1274 | /** |
| 1275 | * Validate Insert |
| 1276 | * |
| 1277 | * This method is used by both insert() and get_compiled_insert() to |
| 1278 | * validate that the there data is actually being set and that table |
| 1279 | * has been chosen to be inserted into. |
| 1280 | * |
| 1281 | * @access public |
| 1282 | * @param string the table to insert data into |
| 1283 | * @return string |
| 1284 | */ |
| 1285 | protected function _validate_insert($table = '') |
| 1286 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1287 | if (count($this->ar_set) == 0) |
| 1288 | { |
| 1289 | if ($this->db_debug) |
| 1290 | { |
| 1291 | return $this->display_error('db_must_use_set'); |
| 1292 | } |
| 1293 | return FALSE; |
| 1294 | } |
| 1295 | |
| 1296 | if ($table == '') |
| 1297 | { |
| 1298 | if ( ! isset($this->ar_from[0])) |
| 1299 | { |
| 1300 | if ($this->db_debug) |
| 1301 | { |
| 1302 | return $this->display_error('db_must_set_table'); |
| 1303 | } |
| 1304 | return FALSE; |
| 1305 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1306 | } |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1307 | else |
| 1308 | { |
| 1309 | $this->ar_from[0] = $table; |
| 1310 | } |
| 1311 | |
| 1312 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1313 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1314 | |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1315 | // -------------------------------------------------------------------- |
| 1316 | |
| 1317 | /** |
| 1318 | * Replace |
| 1319 | * |
| 1320 | * Compiles an replace into string and runs the query |
| 1321 | * |
| 1322 | * @param string the table to replace data into |
| 1323 | * @param array an associative array of insert values |
| 1324 | * @return object |
| 1325 | */ |
| 1326 | public function replace($table = '', $set = NULL) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1327 | { |
| 1328 | if ( ! is_null($set)) |
| 1329 | { |
| 1330 | $this->set($set); |
| 1331 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1332 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1333 | if (count($this->ar_set) == 0) |
| 1334 | { |
| 1335 | if ($this->db_debug) |
| 1336 | { |
| 1337 | return $this->display_error('db_must_use_set'); |
| 1338 | } |
| 1339 | return FALSE; |
| 1340 | } |
| 1341 | |
| 1342 | if ($table == '') |
| 1343 | { |
| 1344 | if ( ! isset($this->ar_from[0])) |
| 1345 | { |
| 1346 | if ($this->db_debug) |
| 1347 | { |
| 1348 | return $this->display_error('db_must_set_table'); |
| 1349 | } |
| 1350 | return FALSE; |
| 1351 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1352 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1353 | $table = $this->ar_from[0]; |
| 1354 | } |
| 1355 | |
| 1356 | $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] | 1357 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1358 | $this->_reset_write(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1359 | return $this->query($sql); |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1360 | } |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1361 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1362 | // -------------------------------------------------------------------- |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1363 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1364 | /** |
| 1365 | * Get UPDATE query string |
| 1366 | * |
| 1367 | * Compiles an update query and returns the sql |
| 1368 | * |
| 1369 | * @access public |
| 1370 | * @param string the table to update |
| 1371 | * @param boolean TRUE: reset AR values; FALSE: leave AR values alone |
| 1372 | * @return string |
| 1373 | */ |
| 1374 | public function get_compiled_update($table = '', $reset = TRUE) |
| 1375 | { |
| 1376 | // Combine any cached components with the current statements |
| 1377 | $this->_merge_cache(); |
| 1378 | |
| 1379 | if ($this->_validate_update($table) === FALSE) |
| 1380 | { |
| 1381 | return FALSE; |
| 1382 | } |
| 1383 | |
| 1384 | $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); |
| 1385 | |
| 1386 | if ($reset === TRUE) |
| 1387 | { |
| 1388 | $this->_reset_write(); |
| 1389 | } |
| 1390 | |
| 1391 | return $sql; |
| 1392 | } |
| 1393 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1394 | // -------------------------------------------------------------------- |
| 1395 | |
| 1396 | /** |
| 1397 | * Update |
| 1398 | * |
| 1399 | * Compiles an update string and runs the query |
| 1400 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1401 | * @param string the table to retrieve the results from |
| 1402 | * @param array an associative array of update values |
| 1403 | * @param mixed the where clause |
| 1404 | * @return object |
| 1405 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1406 | public function update($table = '', $set = NULL, $where = NULL, $limit = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1407 | { |
| 1408 | // Combine any cached components with the current statements |
| 1409 | $this->_merge_cache(); |
| 1410 | |
| 1411 | if ( ! is_null($set)) |
| 1412 | { |
| 1413 | $this->set($set); |
| 1414 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1415 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1416 | if ($this->_validate_update($table) === FALSE) |
| 1417 | { |
| 1418 | return FALSE; |
| 1419 | } |
| 1420 | |
| 1421 | if ($where != NULL) |
| 1422 | { |
| 1423 | $this->where($where); |
| 1424 | } |
| 1425 | |
| 1426 | if ($limit != NULL) |
| 1427 | { |
| 1428 | $this->limit($limit); |
| 1429 | } |
| 1430 | |
Mancy | 0d91fd2 | 2011-12-20 13:13:14 +0300 | [diff] [blame] | 1431 | $sql = $this->_update($this->_protect_identifiers($this->ar_from[0], TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit, $this->ar_like); |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1432 | |
| 1433 | $this->_reset_write(); |
| 1434 | return $this->query($sql); |
| 1435 | } |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1436 | |
| 1437 | // -------------------------------------------------------------------- |
| 1438 | |
| 1439 | /** |
| 1440 | * Validate Update |
| 1441 | * |
| 1442 | * This method is used by both update() and get_compiled_update() to |
| 1443 | * validate that data is actually being set and that a table has been |
| 1444 | * chosen to be update. |
| 1445 | * |
| 1446 | * @access public |
| 1447 | * @param string the table to update data on |
| 1448 | * @return string |
| 1449 | */ |
| 1450 | protected function _validate_update($table = '') |
| 1451 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1452 | if (count($this->ar_set) == 0) |
| 1453 | { |
| 1454 | if ($this->db_debug) |
| 1455 | { |
| 1456 | return $this->display_error('db_must_use_set'); |
| 1457 | } |
| 1458 | return FALSE; |
| 1459 | } |
| 1460 | |
| 1461 | if ($table == '') |
| 1462 | { |
| 1463 | if ( ! isset($this->ar_from[0])) |
| 1464 | { |
| 1465 | if ($this->db_debug) |
| 1466 | { |
| 1467 | return $this->display_error('db_must_set_table'); |
| 1468 | } |
| 1469 | return FALSE; |
| 1470 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1471 | } |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1472 | else |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1473 | { |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1474 | $this->ar_from[0] = $table; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1475 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1476 | } |
Kyle Farris | 7611601 | 2011-08-31 11:17:48 -0400 | [diff] [blame] | 1477 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1478 | // -------------------------------------------------------------------- |
| 1479 | |
| 1480 | /** |
| 1481 | * Update_Batch |
| 1482 | * |
| 1483 | * Compiles an update string and runs the query |
| 1484 | * |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1485 | * @param string the table to retrieve the results from |
| 1486 | * @param array an associative array of update values |
| 1487 | * @param string the where key |
| 1488 | * @return object |
| 1489 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1490 | public function update_batch($table = '', $set = NULL, $index = NULL) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1491 | { |
| 1492 | // Combine any cached components with the current statements |
| 1493 | $this->_merge_cache(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1494 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1495 | if (is_null($index)) |
| 1496 | { |
| 1497 | if ($this->db_debug) |
| 1498 | { |
Kyle Farris | 2de2fa0 | 2011-08-31 11:52:20 -0400 | [diff] [blame] | 1499 | return $this->display_error('db_must_use_index'); |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | return FALSE; |
| 1503 | } |
| 1504 | |
| 1505 | if ( ! is_null($set)) |
| 1506 | { |
| 1507 | $this->set_update_batch($set, $index); |
| 1508 | } |
| 1509 | |
| 1510 | if (count($this->ar_set) == 0) |
| 1511 | { |
| 1512 | if ($this->db_debug) |
| 1513 | { |
| 1514 | return $this->display_error('db_must_use_set'); |
| 1515 | } |
| 1516 | |
| 1517 | return FALSE; |
| 1518 | } |
| 1519 | |
| 1520 | if ($table == '') |
| 1521 | { |
| 1522 | if ( ! isset($this->ar_from[0])) |
| 1523 | { |
| 1524 | if ($this->db_debug) |
| 1525 | { |
| 1526 | return $this->display_error('db_must_set_table'); |
| 1527 | } |
| 1528 | return FALSE; |
| 1529 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1530 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1531 | $table = $this->ar_from[0]; |
| 1532 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1533 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1534 | // Batch this baby |
| 1535 | for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100) |
| 1536 | { |
| 1537 | $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); |
| 1538 | |
| 1539 | $this->query($sql); |
| 1540 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1541 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1542 | $this->_reset_write(); |
| 1543 | } |
| 1544 | |
| 1545 | // -------------------------------------------------------------------- |
| 1546 | |
| 1547 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1548 | * 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] | 1549 | * |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1550 | * @param array |
| 1551 | * @param string |
| 1552 | * @param boolean |
| 1553 | * @return object |
| 1554 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1555 | public function set_update_batch($key, $index = '', $escape = TRUE) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1556 | { |
| 1557 | $key = $this->_object_to_array_batch($key); |
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 | if ( ! is_array($key)) |
| 1560 | { |
| 1561 | // @todo error |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1562 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1563 | |
| 1564 | foreach ($key as $k => $v) |
| 1565 | { |
| 1566 | $index_set = FALSE; |
| 1567 | $clean = array(); |
| 1568 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 1569 | foreach ($v as $k2 => $v2) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1570 | { |
| 1571 | if ($k2 == $index) |
| 1572 | { |
| 1573 | $index_set = TRUE; |
| 1574 | } |
| 1575 | else |
| 1576 | { |
| 1577 | $not[] = $k.'-'.$v; |
| 1578 | } |
| 1579 | |
| 1580 | if ($escape === FALSE) |
| 1581 | { |
| 1582 | $clean[$this->_protect_identifiers($k2)] = $v2; |
| 1583 | } |
| 1584 | else |
| 1585 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1586 | $clean[$this->_protect_identifiers($k2)] = $this->escape($v2); |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | if ($index_set == FALSE) |
| 1591 | { |
| 1592 | return $this->display_error('db_batch_missing_index'); |
| 1593 | } |
| 1594 | |
| 1595 | $this->ar_set[] = $clean; |
| 1596 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1597 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1598 | return $this; |
| 1599 | } |
| 1600 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1601 | // -------------------------------------------------------------------- |
| 1602 | |
| 1603 | /** |
| 1604 | * Empty Table |
| 1605 | * |
| 1606 | * Compiles a delete string and runs "DELETE FROM table" |
| 1607 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1608 | * @param string the table to empty |
| 1609 | * @return object |
| 1610 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1611 | public function empty_table($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1612 | { |
| 1613 | if ($table == '') |
| 1614 | { |
| 1615 | if ( ! isset($this->ar_from[0])) |
| 1616 | { |
| 1617 | if ($this->db_debug) |
| 1618 | { |
| 1619 | return $this->display_error('db_must_set_table'); |
| 1620 | } |
| 1621 | return FALSE; |
| 1622 | } |
| 1623 | |
| 1624 | $table = $this->ar_from[0]; |
| 1625 | } |
| 1626 | else |
| 1627 | { |
| 1628 | $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); |
| 1629 | } |
| 1630 | |
| 1631 | $sql = $this->_delete($table); |
| 1632 | |
| 1633 | $this->_reset_write(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1634 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1635 | return $this->query($sql); |
| 1636 | } |
| 1637 | |
| 1638 | // -------------------------------------------------------------------- |
| 1639 | |
| 1640 | /** |
| 1641 | * Truncate |
| 1642 | * |
| 1643 | * Compiles a truncate string and runs the query |
| 1644 | * If the database does not support the truncate() command |
| 1645 | * This function maps to "DELETE FROM table" |
| 1646 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1647 | * @param string the table to truncate |
| 1648 | * @return object |
| 1649 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1650 | public function truncate($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1651 | { |
| 1652 | if ($table == '') |
| 1653 | { |
| 1654 | if ( ! isset($this->ar_from[0])) |
| 1655 | { |
| 1656 | if ($this->db_debug) |
| 1657 | { |
| 1658 | return $this->display_error('db_must_set_table'); |
| 1659 | } |
| 1660 | return FALSE; |
| 1661 | } |
| 1662 | |
| 1663 | $table = $this->ar_from[0]; |
| 1664 | } |
| 1665 | else |
| 1666 | { |
| 1667 | $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); |
| 1668 | } |
| 1669 | |
| 1670 | $sql = $this->_truncate($table); |
| 1671 | |
| 1672 | $this->_reset_write(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1673 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1674 | return $this->query($sql); |
| 1675 | } |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1676 | |
| 1677 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1678 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1679 | /** |
| 1680 | * Get DELETE query string |
| 1681 | * |
| 1682 | * Compiles a delete query string and returns the sql |
| 1683 | * |
| 1684 | * @access public |
| 1685 | * @param string the table to delete from |
| 1686 | * @param boolean TRUE: reset AR values; FALSE: leave AR values alone |
| 1687 | * @return string |
| 1688 | */ |
| 1689 | public function get_compiled_delete($table = '', $reset = TRUE) |
| 1690 | { |
| 1691 | $this->return_delete_sql = TRUE; |
| 1692 | $sql = $this->delete($table, '', NULL, $reset); |
| 1693 | $this->return_delete_sql = FALSE; |
| 1694 | return $sql; |
| 1695 | } |
| 1696 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1697 | // -------------------------------------------------------------------- |
| 1698 | |
| 1699 | /** |
| 1700 | * Delete |
| 1701 | * |
| 1702 | * Compiles a delete string and runs the query |
| 1703 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1704 | * @param mixed the table(s) to delete from. String or array |
| 1705 | * @param mixed the where clause |
| 1706 | * @param mixed the limit clause |
| 1707 | * @param boolean |
| 1708 | * @return object |
| 1709 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1710 | public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1711 | { |
| 1712 | // Combine any cached components with the current statements |
| 1713 | $this->_merge_cache(); |
| 1714 | |
| 1715 | if ($table == '') |
| 1716 | { |
| 1717 | if ( ! isset($this->ar_from[0])) |
| 1718 | { |
| 1719 | if ($this->db_debug) |
| 1720 | { |
| 1721 | return $this->display_error('db_must_set_table'); |
| 1722 | } |
| 1723 | return FALSE; |
| 1724 | } |
| 1725 | |
| 1726 | $table = $this->ar_from[0]; |
| 1727 | } |
| 1728 | elseif (is_array($table)) |
| 1729 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 1730 | foreach ($table as $single_table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1731 | { |
| 1732 | $this->delete($single_table, $where, $limit, FALSE); |
| 1733 | } |
| 1734 | |
| 1735 | $this->_reset_write(); |
| 1736 | return; |
| 1737 | } |
| 1738 | else |
| 1739 | { |
| 1740 | $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE); |
| 1741 | } |
| 1742 | |
| 1743 | if ($where != '') |
| 1744 | { |
| 1745 | $this->where($where); |
| 1746 | } |
| 1747 | |
| 1748 | if ($limit != NULL) |
| 1749 | { |
| 1750 | $this->limit($limit); |
| 1751 | } |
| 1752 | |
Derek Allard | 03d783b | 2009-05-03 19:45:45 +0000 | [diff] [blame] | 1753 | 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] | 1754 | { |
| 1755 | if ($this->db_debug) |
| 1756 | { |
| 1757 | return $this->display_error('db_del_must_use_where'); |
| 1758 | } |
| 1759 | |
| 1760 | return FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1761 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1762 | |
| 1763 | $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit); |
| 1764 | |
| 1765 | if ($reset_data) |
| 1766 | { |
| 1767 | $this->_reset_write(); |
| 1768 | } |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1769 | |
| 1770 | if ($this->return_delete_sql === true) |
| 1771 | { |
| 1772 | return $sql; |
| 1773 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1774 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1775 | return $this->query($sql); |
| 1776 | } |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1777 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1778 | // -------------------------------------------------------------------- |
| 1779 | |
| 1780 | /** |
| 1781 | * DB Prefix |
| 1782 | * |
| 1783 | * Prepends a database prefix if one exists in configuration |
| 1784 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1785 | * @param string the table |
| 1786 | * @return string |
| 1787 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1788 | public function dbprefix($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1789 | { |
| 1790 | if ($table == '') |
| 1791 | { |
| 1792 | $this->display_error('db_table_name_required'); |
| 1793 | } |
| 1794 | |
| 1795 | return $this->dbprefix.$table; |
| 1796 | } |
| 1797 | |
| 1798 | // -------------------------------------------------------------------- |
| 1799 | |
| 1800 | /** |
Phil Sturgeon | 8a02247 | 2011-07-15 15:25:15 -0600 | [diff] [blame] | 1801 | * Set DB Prefix |
| 1802 | * |
| 1803 | * Set's the DB Prefix to something new without needing to reconnect |
| 1804 | * |
| 1805 | * @param string the prefix |
| 1806 | * @return string |
| 1807 | */ |
| 1808 | public function set_dbprefix($prefix = '') |
| 1809 | { |
| 1810 | return $this->dbprefix = $prefix; |
| 1811 | } |
| 1812 | |
| 1813 | // -------------------------------------------------------------------- |
| 1814 | |
| 1815 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1816 | * Track Aliases |
| 1817 | * |
| 1818 | * Used to track SQL statements written with aliased tables. |
| 1819 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1820 | * @param string The table to inspect |
| 1821 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1822 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1823 | protected function _track_aliases($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1824 | { |
| 1825 | if (is_array($table)) |
| 1826 | { |
| 1827 | foreach ($table as $t) |
| 1828 | { |
| 1829 | $this->_track_aliases($t); |
| 1830 | } |
| 1831 | return; |
| 1832 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1833 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1834 | // Does the string contain a comma? If so, we need to separate |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1835 | // the string into discreet statements |
| 1836 | if (strpos($table, ',') !== FALSE) |
| 1837 | { |
| 1838 | return $this->_track_aliases(explode(',', $table)); |
| 1839 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1840 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1841 | // if a table alias is used we can recognize it by a space |
| 1842 | if (strpos($table, " ") !== FALSE) |
| 1843 | { |
| 1844 | // if the alias is written with the AS keyword, remove it |
| 1845 | $table = preg_replace('/ AS /i', ' ', $table); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1846 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1847 | // Grab the alias |
| 1848 | $table = trim(strrchr($table, " ")); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1849 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1850 | // Store the alias, if it doesn't already exist |
| 1851 | if ( ! in_array($table, $this->ar_aliased_tables)) |
| 1852 | { |
| 1853 | $this->ar_aliased_tables[] = $table; |
| 1854 | } |
| 1855 | } |
| 1856 | } |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1857 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1858 | // -------------------------------------------------------------------- |
| 1859 | |
| 1860 | /** |
| 1861 | * Compile the SELECT statement |
| 1862 | * |
| 1863 | * Generates a query string based on which functions were used. |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1864 | * Should not be called directly. The get() function calls it. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1865 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1866 | * @return string |
| 1867 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1868 | protected function _compile_select($select_override = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1869 | { |
| 1870 | // Combine any cached components with the current statements |
| 1871 | $this->_merge_cache(); |
| 1872 | |
| 1873 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1874 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1875 | // Write the "select" portion of the query |
| 1876 | |
| 1877 | if ($select_override !== FALSE) |
| 1878 | { |
| 1879 | $sql = $select_override; |
| 1880 | } |
| 1881 | else |
| 1882 | { |
| 1883 | $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1884 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1885 | if (count($this->ar_select) == 0) |
| 1886 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1887 | $sql .= '*'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1888 | } |
| 1889 | else |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1890 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1891 | // Cycle through the "select" portion of the query and prep each column name. |
| 1892 | // The reason we protect identifiers here rather then in the select() function |
| 1893 | // is because until the user calls the from() function we don't know if there are aliases |
| 1894 | foreach ($this->ar_select as $key => $val) |
| 1895 | { |
Phil Sturgeon | 77cc028 | 2011-08-09 16:03:49 -0600 | [diff] [blame] | 1896 | $no_escape = isset($this->ar_no_escape[$key]) ? $this->ar_no_escape[$key] : NULL; |
| 1897 | $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $no_escape); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1898 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1899 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1900 | $sql .= implode(', ', $this->ar_select); |
| 1901 | } |
| 1902 | } |
| 1903 | |
| 1904 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1905 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1906 | // Write the "FROM" portion of the query |
| 1907 | |
| 1908 | if (count($this->ar_from) > 0) |
| 1909 | { |
| 1910 | $sql .= "\nFROM "; |
| 1911 | |
| 1912 | $sql .= $this->_from_tables($this->ar_from); |
| 1913 | } |
| 1914 | |
| 1915 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1916 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1917 | // Write the "JOIN" portion of the query |
| 1918 | |
| 1919 | if (count($this->ar_join) > 0) |
| 1920 | { |
| 1921 | $sql .= "\n"; |
| 1922 | |
| 1923 | $sql .= implode("\n", $this->ar_join); |
| 1924 | } |
| 1925 | |
| 1926 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1927 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1928 | // Write the "WHERE" portion of the query |
| 1929 | |
| 1930 | if (count($this->ar_where) > 0 OR count($this->ar_like) > 0) |
| 1931 | { |
Greg Aker | e156c6e | 2011-04-20 16:03:04 -0500 | [diff] [blame] | 1932 | $sql .= "\nWHERE "; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | $sql .= implode("\n", $this->ar_where); |
| 1936 | |
| 1937 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1938 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1939 | // Write the "LIKE" portion of the query |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1940 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1941 | if (count($this->ar_like) > 0) |
| 1942 | { |
| 1943 | if (count($this->ar_where) > 0) |
| 1944 | { |
| 1945 | $sql .= "\nAND "; |
| 1946 | } |
| 1947 | |
| 1948 | $sql .= implode("\n", $this->ar_like); |
| 1949 | } |
| 1950 | |
| 1951 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1952 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1953 | // Write the "GROUP BY" portion of the query |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1954 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1955 | if (count($this->ar_groupby) > 0) |
| 1956 | { |
| 1957 | $sql .= "\nGROUP BY "; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1958 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1959 | $sql .= implode(', ', $this->ar_groupby); |
| 1960 | } |
| 1961 | |
| 1962 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1963 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1964 | // Write the "HAVING" portion of the query |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1965 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1966 | if (count($this->ar_having) > 0) |
| 1967 | { |
| 1968 | $sql .= "\nHAVING "; |
| 1969 | $sql .= implode("\n", $this->ar_having); |
| 1970 | } |
| 1971 | |
| 1972 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1973 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1974 | // Write the "ORDER BY" portion of the query |
| 1975 | |
| 1976 | if (count($this->ar_orderby) > 0) |
| 1977 | { |
| 1978 | $sql .= "\nORDER BY "; |
| 1979 | $sql .= implode(', ', $this->ar_orderby); |
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 | if ($this->ar_order !== FALSE) |
| 1982 | { |
| 1983 | $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC'; |
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 | } |
| 1986 | |
| 1987 | // ---------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1988 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1989 | // Write the "LIMIT" portion of the query |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1990 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1991 | if (is_numeric($this->ar_limit)) |
| 1992 | { |
| 1993 | $sql .= "\n"; |
| 1994 | $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset); |
| 1995 | } |
| 1996 | |
| 1997 | return $sql; |
| 1998 | } |
| 1999 | |
| 2000 | // -------------------------------------------------------------------- |
| 2001 | |
| 2002 | /** |
| 2003 | * Object to Array |
| 2004 | * |
| 2005 | * Takes an object as input and converts the class variables to array key/vals |
| 2006 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2007 | * @param object |
| 2008 | * @return array |
| 2009 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2010 | public function _object_to_array($object) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2011 | { |
| 2012 | if ( ! is_object($object)) |
| 2013 | { |
| 2014 | return $object; |
| 2015 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2016 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2017 | $array = array(); |
| 2018 | foreach (get_object_vars($object) as $key => $val) |
| 2019 | { |
| 2020 | // 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] | 2021 | if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2022 | { |
| 2023 | $array[$key] = $val; |
| 2024 | } |
| 2025 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2026 | |
| 2027 | return $array; |
| 2028 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2029 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2030 | // -------------------------------------------------------------------- |
| 2031 | |
| 2032 | /** |
| 2033 | * Object to Array |
| 2034 | * |
| 2035 | * Takes an object as input and converts the class variables to array key/vals |
| 2036 | * |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2037 | * @param object |
| 2038 | * @return array |
| 2039 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2040 | public function _object_to_array_batch($object) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2041 | { |
| 2042 | if ( ! is_object($object)) |
| 2043 | { |
| 2044 | return $object; |
| 2045 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2046 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2047 | $array = array(); |
| 2048 | $out = get_object_vars($object); |
| 2049 | $fields = array_keys($out); |
| 2050 | |
| 2051 | foreach ($fields as $val) |
| 2052 | { |
| 2053 | // 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] | 2054 | if ($val != '_parent_name') |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2055 | { |
| 2056 | |
| 2057 | $i = 0; |
| 2058 | foreach ($out[$val] as $data) |
| 2059 | { |
| 2060 | $array[$i][$val] = $data; |
| 2061 | $i++; |
| 2062 | } |
| 2063 | } |
| 2064 | } |
| 2065 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2066 | return $array; |
| 2067 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2068 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2069 | // -------------------------------------------------------------------- |
| 2070 | |
| 2071 | /** |
| 2072 | * Start Cache |
| 2073 | * |
| 2074 | * Starts AR caching |
| 2075 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2076 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2077 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2078 | public function start_cache() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2079 | { |
| 2080 | $this->ar_caching = TRUE; |
| 2081 | } |
| 2082 | |
| 2083 | // -------------------------------------------------------------------- |
| 2084 | |
| 2085 | /** |
| 2086 | * Stop Cache |
| 2087 | * |
| 2088 | * Stops AR caching |
| 2089 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2090 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2091 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2092 | public function stop_cache() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2093 | { |
| 2094 | $this->ar_caching = FALSE; |
| 2095 | } |
| 2096 | |
| 2097 | // -------------------------------------------------------------------- |
| 2098 | |
| 2099 | /** |
| 2100 | * Flush Cache |
| 2101 | * |
| 2102 | * Empties the AR cache |
| 2103 | * |
| 2104 | * @access public |
| 2105 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2106 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2107 | public function flush_cache() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2108 | { |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2109 | $this->_reset_run(array( |
| 2110 | 'ar_cache_select' => array(), |
| 2111 | 'ar_cache_from' => array(), |
| 2112 | 'ar_cache_join' => array(), |
| 2113 | 'ar_cache_where' => array(), |
| 2114 | 'ar_cache_like' => array(), |
| 2115 | 'ar_cache_groupby' => array(), |
| 2116 | 'ar_cache_having' => array(), |
| 2117 | 'ar_cache_orderby' => array(), |
| 2118 | 'ar_cache_set' => array(), |
| 2119 | 'ar_cache_exists' => array(), |
| 2120 | 'ar_cache_no_escape' => array() |
| 2121 | )); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | // -------------------------------------------------------------------- |
| 2125 | |
| 2126 | /** |
| 2127 | * Merge Cache |
| 2128 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2129 | * When called, this function merges any cached AR arrays with |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2130 | * locally called ones. |
| 2131 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2132 | * @return void |
| 2133 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2134 | protected function _merge_cache() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2135 | { |
| 2136 | if (count($this->ar_cache_exists) == 0) |
| 2137 | { |
| 2138 | return; |
| 2139 | } |
| 2140 | |
| 2141 | foreach ($this->ar_cache_exists as $val) |
| 2142 | { |
| 2143 | $ar_variable = 'ar_'.$val; |
| 2144 | $ar_cache_var = 'ar_cache_'.$val; |
| 2145 | |
| 2146 | if (count($this->$ar_cache_var) == 0) |
| 2147 | { |
| 2148 | continue; |
| 2149 | } |
| 2150 | |
| 2151 | $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable)); |
| 2152 | } |
| 2153 | |
| 2154 | // If we are "protecting identifiers" we need to examine the "from" |
| 2155 | // portion of the query to determine if there are any aliases |
| 2156 | if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0) |
| 2157 | { |
| 2158 | $this->_track_aliases($this->ar_from); |
| 2159 | } |
Greg Aker | 2e1837a | 2011-05-06 12:17:04 -0500 | [diff] [blame] | 2160 | |
| 2161 | $this->ar_no_escape = $this->ar_cache_no_escape; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2162 | } |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 2163 | |
| 2164 | // -------------------------------------------------------------------- |
| 2165 | |
| 2166 | /** |
| 2167 | * Reset Active Record values. |
| 2168 | * |
| 2169 | * Publicly-visible method to reset the AR values. |
| 2170 | * |
| 2171 | * @access public |
| 2172 | * @return void |
| 2173 | */ |
| 2174 | public function reset_query() |
| 2175 | { |
| 2176 | $this->_reset_select(); |
| 2177 | $this->_reset_write(); |
| 2178 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2179 | |
| 2180 | // -------------------------------------------------------------------- |
| 2181 | |
| 2182 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 2183 | * Resets the active record values. Called by the get() function |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2184 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2185 | * @param array An array of fields to reset |
| 2186 | * @return void |
| 2187 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2188 | protected function _reset_run($ar_reset_items) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2189 | { |
| 2190 | foreach ($ar_reset_items as $item => $default_value) |
| 2191 | { |
| 2192 | if ( ! in_array($item, $this->ar_store_array)) |
| 2193 | { |
| 2194 | $this->$item = $default_value; |
| 2195 | } |
| 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | // -------------------------------------------------------------------- |
| 2200 | |
| 2201 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 2202 | * Resets the active record values. Called by the get() function |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2203 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2204 | * @return void |
| 2205 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2206 | protected function _reset_select() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2207 | { |
| 2208 | $ar_reset_items = array( |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2209 | 'ar_select' => array(), |
| 2210 | 'ar_from' => array(), |
| 2211 | 'ar_join' => array(), |
| 2212 | 'ar_where' => array(), |
| 2213 | 'ar_like' => array(), |
| 2214 | 'ar_groupby' => array(), |
| 2215 | 'ar_having' => array(), |
| 2216 | 'ar_orderby' => array(), |
| 2217 | 'ar_wherein' => array(), |
| 2218 | 'ar_aliased_tables' => array(), |
| 2219 | 'ar_no_escape' => array(), |
| 2220 | 'ar_distinct' => FALSE, |
| 2221 | 'ar_limit' => FALSE, |
| 2222 | 'ar_offset' => FALSE, |
| 2223 | 'ar_order' => FALSE, |
| 2224 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2225 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2226 | $this->_reset_run($ar_reset_items); |
| 2227 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2228 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2229 | // -------------------------------------------------------------------- |
| 2230 | |
| 2231 | /** |
| 2232 | * Resets the active record "write" values. |
| 2233 | * |
Robin Sowell | 43753fd | 2010-09-16 12:52:07 -0400 | [diff] [blame] | 2234 | * Called by the insert() update() insert_batch() update_batch() and delete() functions |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2235 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2236 | * @return void |
| 2237 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2238 | protected function _reset_write() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2239 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2240 | $ar_reset_items = array( |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2241 | 'ar_set' => array(), |
| 2242 | 'ar_from' => array(), |
| 2243 | 'ar_where' => array(), |
| 2244 | 'ar_like' => array(), |
| 2245 | 'ar_orderby' => array(), |
| 2246 | 'ar_keys' => array(), |
| 2247 | 'ar_limit' => FALSE, |
| 2248 | 'ar_order' => FALSE |
| 2249 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2250 | |
| 2251 | $this->_reset_run($ar_reset_items); |
| 2252 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | /* End of file DB_active_rec.php */ |
Kyle Farris | 2de2fa0 | 2011-08-31 11:52:20 -0400 | [diff] [blame] | 2256 | /* Location: ./system/database/DB_active_rec.php */ |