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