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