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