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