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