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