Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 41 | * Query Builder Class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 42 | * |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 43 | * This is the platform-independent base Query Builder implementation class. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | * |
| 45 | * @package CodeIgniter |
| 46 | * @subpackage Drivers |
| 47 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 48 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 49 | * @link https://codeigniter.com/user_guide/database/ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | */ |
Jamie Rumbelow | d6ce1e9 | 2012-04-26 13:27:35 +0100 | [diff] [blame] | 51 | |
| 52 | abstract class CI_DB_query_builder extends CI_DB_driver { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 53 | |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 54 | /** |
| 55 | * Return DELETE SQL flag |
| 56 | * |
| 57 | * @var bool |
| 58 | */ |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 59 | protected $return_delete_sql = FALSE; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Reset DELETE data flag |
| 63 | * |
| 64 | * @var bool |
| 65 | */ |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 66 | protected $reset_delete_data = FALSE; |
| 67 | |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 68 | /** |
| 69 | * QB SELECT data |
| 70 | * |
| 71 | * @var array |
| 72 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 73 | protected $qb_select = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * QB DISTINCT flag |
| 77 | * |
| 78 | * @var bool |
| 79 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 80 | protected $qb_distinct = FALSE; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * QB FROM data |
| 84 | * |
| 85 | * @var array |
| 86 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 87 | protected $qb_from = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * QB JOIN data |
| 91 | * |
| 92 | * @var array |
| 93 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 94 | protected $qb_join = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * QB WHERE data |
| 98 | * |
| 99 | * @var array |
| 100 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 101 | protected $qb_where = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * QB GROUP BY data |
| 105 | * |
| 106 | * @var array |
| 107 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 108 | protected $qb_groupby = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * QB HAVING data |
| 112 | * |
| 113 | * @var array |
| 114 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 115 | protected $qb_having = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * QB keys |
| 119 | * |
| 120 | * @var array |
| 121 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 122 | protected $qb_keys = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * QB LIMIT data |
| 126 | * |
| 127 | * @var int |
| 128 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 129 | protected $qb_limit = FALSE; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 130 | |
| 131 | /** |
| 132 | * QB OFFSET data |
| 133 | * |
| 134 | * @var int |
| 135 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 136 | protected $qb_offset = FALSE; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * QB ORDER BY data |
| 140 | * |
| 141 | * @var array |
| 142 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 143 | protected $qb_orderby = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * QB data sets |
| 147 | * |
| 148 | * @var array |
| 149 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 150 | protected $qb_set = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 151 | |
| 152 | /** |
Andrey Andreev | 8338bbb | 2016-12-12 14:17:52 +0200 | [diff] [blame] | 153 | * QB data set for update_batch() |
| 154 | * |
| 155 | * @var array |
| 156 | */ |
| 157 | protected $qb_set_ub = array(); |
| 158 | |
| 159 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 160 | * QB aliased tables list |
| 161 | * |
| 162 | * @var array |
| 163 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 164 | protected $qb_aliased_tables = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * QB WHERE group started flag |
| 168 | * |
| 169 | * @var bool |
| 170 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 171 | protected $qb_where_group_started = FALSE; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * QB WHERE group count |
| 175 | * |
| 176 | * @var int |
| 177 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 178 | protected $qb_where_group_count = 0; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 180 | // Query Builder Caching variables |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 181 | |
| 182 | /** |
| 183 | * QB Caching flag |
| 184 | * |
| 185 | * @var bool |
| 186 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 187 | protected $qb_caching = FALSE; |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * QB Cache exists list |
| 191 | * |
| 192 | * @var array |
| 193 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 194 | protected $qb_cache_exists = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 195 | |
| 196 | /** |
| 197 | * QB Cache SELECT data |
| 198 | * |
| 199 | * @var array |
| 200 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 201 | protected $qb_cache_select = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 202 | |
| 203 | /** |
| 204 | * QB Cache FROM data |
| 205 | * |
| 206 | * @var array |
| 207 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 208 | protected $qb_cache_from = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 209 | |
| 210 | /** |
| 211 | * QB Cache JOIN data |
| 212 | * |
| 213 | * @var array |
| 214 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 215 | protected $qb_cache_join = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 216 | |
| 217 | /** |
Andrey Andreev | 437ffe0 | 2017-01-17 12:44:19 +0200 | [diff] [blame] | 218 | * QB Cache aliased tables list |
| 219 | * |
| 220 | * @var array |
| 221 | */ |
| 222 | protected $qb_cache_aliased_tables = array(); |
| 223 | |
| 224 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 225 | * QB Cache WHERE data |
| 226 | * |
| 227 | * @var array |
| 228 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 229 | protected $qb_cache_where = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 230 | |
| 231 | /** |
| 232 | * QB Cache GROUP BY data |
| 233 | * |
| 234 | * @var array |
| 235 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 236 | protected $qb_cache_groupby = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 237 | |
| 238 | /** |
| 239 | * QB Cache HAVING data |
| 240 | * |
| 241 | * @var array |
| 242 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 243 | protected $qb_cache_having = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 244 | |
| 245 | /** |
| 246 | * QB Cache ORDER BY data |
| 247 | * |
| 248 | * @var array |
| 249 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 250 | protected $qb_cache_orderby = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 251 | |
| 252 | /** |
| 253 | * QB Cache data sets |
| 254 | * |
| 255 | * @var array |
| 256 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 257 | protected $qb_cache_set = array(); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 258 | |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 259 | /** |
| 260 | * QB No Escape data |
| 261 | * |
| 262 | * @var array |
| 263 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 264 | protected $qb_no_escape = array(); |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 265 | |
| 266 | /** |
| 267 | * QB Cache No Escape data |
| 268 | * |
| 269 | * @var array |
| 270 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 271 | protected $qb_cache_no_escape = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 273 | // -------------------------------------------------------------------- |
| 274 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | /** |
| 276 | * Select |
| 277 | * |
| 278 | * Generates the SELECT portion of the query |
| 279 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | * @param string |
Andrey Andreev | 4287023 | 2012-06-12 01:30:20 +0300 | [diff] [blame] | 281 | * @param mixed |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 282 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 283 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 284 | public function select($select = '*', $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 285 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 286 | if (is_string($select)) |
| 287 | { |
| 288 | $select = explode(',', $select); |
| 289 | } |
| 290 | |
Vivek Dinesh | 33578d2 | 2014-02-06 09:46:07 +0530 | [diff] [blame] | 291 | // If the escape value was not set, we will base it on the global setting |
Andrey Andreev | 4287023 | 2012-06-12 01:30:20 +0300 | [diff] [blame] | 292 | is_bool($escape) OR $escape = $this->_protect_identifiers; |
| 293 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | foreach ($select as $val) |
| 295 | { |
| 296 | $val = trim($val); |
| 297 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 298 | if ($val !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 299 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 300 | $this->qb_select[] = $val; |
| 301 | $this->qb_no_escape[] = $escape; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 303 | if ($this->qb_caching === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 305 | $this->qb_cache_select[] = $val; |
| 306 | $this->qb_cache_exists[] = 'select'; |
| 307 | $this->qb_cache_no_escape[] = $escape; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | } |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 311 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | return $this; |
| 313 | } |
| 314 | |
| 315 | // -------------------------------------------------------------------- |
| 316 | |
| 317 | /** |
| 318 | * Select Max |
| 319 | * |
| 320 | * Generates a SELECT MAX(field) portion of a query |
| 321 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | * @param string the field |
| 323 | * @param string an alias |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 324 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 325 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 326 | public function select_max($select = '', $alias = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | { |
| 328 | return $this->_max_min_avg_sum($select, $alias, 'MAX'); |
| 329 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 330 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 331 | // -------------------------------------------------------------------- |
| 332 | |
| 333 | /** |
| 334 | * Select Min |
| 335 | * |
| 336 | * Generates a SELECT MIN(field) portion of a query |
| 337 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 338 | * @param string the field |
| 339 | * @param string an alias |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 340 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 342 | public function select_min($select = '', $alias = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 343 | { |
| 344 | return $this->_max_min_avg_sum($select, $alias, 'MIN'); |
| 345 | } |
| 346 | |
| 347 | // -------------------------------------------------------------------- |
| 348 | |
| 349 | /** |
| 350 | * Select Average |
| 351 | * |
| 352 | * Generates a SELECT AVG(field) portion of a query |
| 353 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | * @param string the field |
| 355 | * @param string an alias |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 356 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 358 | public function select_avg($select = '', $alias = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | { |
| 360 | return $this->_max_min_avg_sum($select, $alias, 'AVG'); |
| 361 | } |
| 362 | |
| 363 | // -------------------------------------------------------------------- |
| 364 | |
| 365 | /** |
| 366 | * Select Sum |
| 367 | * |
| 368 | * Generates a SELECT SUM(field) portion of a query |
| 369 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 370 | * @param string the field |
| 371 | * @param string an alias |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 372 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 373 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 374 | public function select_sum($select = '', $alias = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | { |
| 376 | return $this->_max_min_avg_sum($select, $alias, 'SUM'); |
| 377 | } |
| 378 | |
| 379 | // -------------------------------------------------------------------- |
| 380 | |
| 381 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 382 | * SELECT [MAX|MIN|AVG|SUM]() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 383 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 384 | * @used-by select_max() |
| 385 | * @used-by select_min() |
| 386 | * @used-by select_avg() |
| 387 | * @used-by select_sum() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 388 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 389 | * @param string $select Field name |
| 390 | * @param string $alias |
| 391 | * @param string $type |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 392 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 393 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 394 | protected function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 396 | if ( ! is_string($select) OR $select === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 397 | { |
| 398 | $this->display_error('db_invalid_query'); |
| 399 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 400 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | $type = strtoupper($type); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 402 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM'))) |
| 404 | { |
| 405 | show_error('Invalid function type: '.$type); |
| 406 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 407 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 408 | if ($alias === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 409 | { |
| 410 | $alias = $this->_create_alias_from_table(trim($select)); |
| 411 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 412 | |
Andrey Andreev | 5b55c15 | 2013-08-06 14:14:32 +0300 | [diff] [blame] | 413 | $sql = $type.'('.$this->protect_identifiers(trim($select)).') AS '.$this->escape_identifiers(trim($alias)); |
Andrey Andreev | 7b5eb73 | 2012-05-24 20:52:41 +0300 | [diff] [blame] | 414 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 415 | $this->qb_select[] = $sql; |
Jamie Rumbelow | d6ce1e9 | 2012-04-26 13:27:35 +0100 | [diff] [blame] | 416 | $this->qb_no_escape[] = NULL; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 417 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 418 | if ($this->qb_caching === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 420 | $this->qb_cache_select[] = $sql; |
| 421 | $this->qb_cache_exists[] = 'select'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 422 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 423 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 424 | return $this; |
| 425 | } |
| 426 | |
| 427 | // -------------------------------------------------------------------- |
| 428 | |
| 429 | /** |
| 430 | * Determines the alias name based on the table |
| 431 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 432 | * @param string $item |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 433 | * @return string |
| 434 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 435 | protected function _create_alias_from_table($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | { |
| 437 | if (strpos($item, '.') !== FALSE) |
| 438 | { |
Andrey Andreev | db0c062 | 2012-02-29 19:02:46 +0200 | [diff] [blame] | 439 | $item = explode('.', $item); |
| 440 | return end($item); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 442 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 443 | return $item; |
| 444 | } |
| 445 | |
| 446 | // -------------------------------------------------------------------- |
| 447 | |
| 448 | /** |
| 449 | * DISTINCT |
| 450 | * |
| 451 | * Sets a flag which tells the query string compiler to add DISTINCT |
| 452 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 453 | * @param bool $val |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 454 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 455 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 456 | public function distinct($val = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 457 | { |
Andrey Andreev | 7b5eb73 | 2012-05-24 20:52:41 +0300 | [diff] [blame] | 458 | $this->qb_distinct = is_bool($val) ? $val : TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 459 | return $this; |
| 460 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 461 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | // -------------------------------------------------------------------- |
| 463 | |
| 464 | /** |
| 465 | * From |
| 466 | * |
| 467 | * Generates the FROM portion of the query |
| 468 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 469 | * @param mixed $from can be a string or array |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 470 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 471 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 472 | public function from($from) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 473 | { |
Andrey Andreev | 7b5eb73 | 2012-05-24 20:52:41 +0300 | [diff] [blame] | 474 | foreach ((array) $from as $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 475 | { |
| 476 | if (strpos($val, ',') !== FALSE) |
| 477 | { |
| 478 | foreach (explode(',', $val) as $v) |
| 479 | { |
| 480 | $v = trim($v); |
| 481 | $this->_track_aliases($v); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 482 | |
George Petsagourakis | 193d448 | 2012-04-28 11:16:18 +0300 | [diff] [blame] | 483 | $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, NULL, FALSE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 485 | if ($this->qb_caching === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 486 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 487 | $this->qb_cache_from[] = $v; |
| 488 | $this->qb_cache_exists[] = 'from'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 489 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 490 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | } |
| 492 | else |
| 493 | { |
| 494 | $val = trim($val); |
| 495 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 496 | // Extract any aliases that might exist. We use this information |
Jamie Rumbelow | 0c09299 | 2012-03-06 22:05:16 +0000 | [diff] [blame] | 497 | // in the protect_identifiers to know whether to add a table prefix |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 498 | $this->_track_aliases($val); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 499 | |
Jamie Rumbelow | 0c09299 | 2012-03-06 22:05:16 +0000 | [diff] [blame] | 500 | $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, NULL, FALSE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 502 | if ($this->qb_caching === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 503 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 504 | $this->qb_cache_from[] = $val; |
| 505 | $this->qb_cache_exists[] = 'from'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | return $this; |
| 511 | } |
| 512 | |
| 513 | // -------------------------------------------------------------------- |
| 514 | |
| 515 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 516 | * JOIN |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 517 | * |
| 518 | * Generates the JOIN portion of the query |
| 519 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 520 | * @param string |
| 521 | * @param string the join condition |
| 522 | * @param string the type of join |
Alex Bilbie | f512b73 | 2012-06-16 11:15:19 +0100 | [diff] [blame] | 523 | * @param string whether not to try to escape identifiers |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 524 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 525 | */ |
Andrey Andreev | fe642da | 2012-06-16 03:47:33 +0300 | [diff] [blame] | 526 | public function join($table, $cond, $type = '', $escape = NULL) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 527 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 528 | if ($type !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 529 | { |
| 530 | $type = strtoupper(trim($type)); |
| 531 | |
Andrey Andreev | 4287023 | 2012-06-12 01:30:20 +0300 | [diff] [blame] | 532 | if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 533 | { |
| 534 | $type = ''; |
| 535 | } |
| 536 | else |
| 537 | { |
| 538 | $type .= ' '; |
| 539 | } |
| 540 | } |
| 541 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 542 | // Extract any aliases that might exist. We use this information |
Jamie Rumbelow | 0c09299 | 2012-03-06 22:05:16 +0000 | [diff] [blame] | 543 | // in the protect_identifiers to know whether to add a table prefix |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 544 | $this->_track_aliases($table); |
| 545 | |
Andrey Andreev | fe642da | 2012-06-16 03:47:33 +0300 | [diff] [blame] | 546 | is_bool($escape) OR $escape = $this->_protect_identifiers; |
| 547 | |
Andrey Andreev | c601194 | 2016-02-11 23:49:37 +0200 | [diff] [blame] | 548 | if ( ! $this->_has_operator($cond)) |
Andrey Andreev | 3751f93 | 2012-06-17 18:07:48 +0300 | [diff] [blame] | 549 | { |
| 550 | $cond = ' USING ('.($escape ? $this->escape_identifiers($cond) : $cond).')'; |
| 551 | } |
Andrey Andreev | c601194 | 2016-02-11 23:49:37 +0200 | [diff] [blame] | 552 | elseif ($escape === FALSE) |
Andrey Andreev | 3751f93 | 2012-06-17 18:07:48 +0300 | [diff] [blame] | 553 | { |
| 554 | $cond = ' ON '.$cond; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 555 | } |
Andrey Andreev | c601194 | 2016-02-11 23:49:37 +0200 | [diff] [blame] | 556 | else |
| 557 | { |
| 558 | // Split multiple conditions |
| 559 | if (preg_match_all('/\sAND\s|\sOR\s/i', $cond, $joints, PREG_OFFSET_CAPTURE)) |
| 560 | { |
| 561 | $conditions = array(); |
| 562 | $joints = $joints[0]; |
| 563 | array_unshift($joints, array('', 0)); |
| 564 | |
| 565 | for ($i = count($joints) - 1, $pos = strlen($cond); $i >= 0; $i--) |
| 566 | { |
| 567 | $joints[$i][1] += strlen($joints[$i][0]); // offset |
| 568 | $conditions[$i] = substr($cond, $joints[$i][1], $pos - $joints[$i][1]); |
| 569 | $pos = $joints[$i][1] - strlen($joints[$i][0]); |
| 570 | $joints[$i] = $joints[$i][0]; |
| 571 | } |
| 572 | } |
| 573 | else |
| 574 | { |
| 575 | $conditions = array($cond); |
| 576 | $joints = array(''); |
| 577 | } |
| 578 | |
| 579 | $cond = ' ON '; |
| 580 | for ($i = 0, $c = count($conditions); $i < $c; $i++) |
| 581 | { |
| 582 | $operator = $this->_get_operator($conditions[$i]); |
| 583 | $cond .= $joints[$i]; |
| 584 | $cond .= preg_match("/(\(*)?([\[\]\w\.'-]+)".preg_quote($operator)."(.*)/i", $conditions[$i], $match) |
| 585 | ? $match[1].$this->protect_identifiers($match[2]).$operator.$this->protect_identifiers($match[3]) |
| 586 | : $conditions[$i]; |
| 587 | } |
| 588 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 589 | |
Andrey Andreev | 4287023 | 2012-06-12 01:30:20 +0300 | [diff] [blame] | 590 | // Do we want to escape the table name? |
| 591 | if ($escape === TRUE) |
| 592 | { |
| 593 | $table = $this->protect_identifiers($table, TRUE, NULL, FALSE); |
| 594 | } |
| 595 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 596 | // Assemble the JOIN statement |
Andrey Andreev | 3751f93 | 2012-06-17 18:07:48 +0300 | [diff] [blame] | 597 | $this->qb_join[] = $join = $type.'JOIN '.$table.$cond; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 598 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 599 | if ($this->qb_caching === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 600 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 601 | $this->qb_cache_join[] = $join; |
| 602 | $this->qb_cache_exists[] = 'join'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | return $this; |
| 606 | } |
| 607 | |
| 608 | // -------------------------------------------------------------------- |
| 609 | |
| 610 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 611 | * WHERE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 612 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 613 | * Generates the WHERE portion of the query. |
| 614 | * Separates multiple calls with 'AND'. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 615 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 616 | * @param mixed |
| 617 | * @param mixed |
Andrey Andreev | 4287023 | 2012-06-12 01:30:20 +0300 | [diff] [blame] | 618 | * @param bool |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 619 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 620 | */ |
Andrey Andreev | 498c1e0 | 2012-06-16 03:34:10 +0300 | [diff] [blame] | 621 | public function where($key, $value = NULL, $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 622 | { |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 623 | return $this->_wh('qb_where', $key, $value, 'AND ', $escape); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 624 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 625 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 626 | // -------------------------------------------------------------------- |
| 627 | |
| 628 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 629 | * OR WHERE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 630 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 631 | * Generates the WHERE portion of the query. |
| 632 | * Separates multiple calls with 'OR'. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 633 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 634 | * @param mixed |
| 635 | * @param mixed |
Andrey Andreev | 4287023 | 2012-06-12 01:30:20 +0300 | [diff] [blame] | 636 | * @param bool |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 637 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 638 | */ |
Andrey Andreev | 498c1e0 | 2012-06-16 03:34:10 +0300 | [diff] [blame] | 639 | public function or_where($key, $value = NULL, $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 640 | { |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 641 | return $this->_wh('qb_where', $key, $value, 'OR ', $escape); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | // -------------------------------------------------------------------- |
| 645 | |
| 646 | /** |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 647 | * WHERE, HAVING |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 648 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 649 | * @used-by where() |
| 650 | * @used-by or_where() |
| 651 | * @used-by having() |
| 652 | * @used-by or_having() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 653 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 654 | * @param string $qb_key 'qb_where' or 'qb_having' |
| 655 | * @param mixed $key |
| 656 | * @param mixed $value |
| 657 | * @param string $type |
| 658 | * @param bool $escape |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 659 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 660 | */ |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 661 | protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 662 | { |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 663 | $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where'; |
| 664 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 665 | if ( ! is_array($key)) |
| 666 | { |
| 667 | $key = array($key => $value); |
| 668 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 669 | |
Rougin Royce Gutib | 191550a | 2014-08-24 16:19:08 +0800 | [diff] [blame] | 670 | // If the escape value was not set will base it on the global setting |
Andrey Andreev | e10fb79 | 2012-06-15 12:07:04 +0300 | [diff] [blame] | 671 | is_bool($escape) OR $escape = $this->_protect_identifiers; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 672 | |
| 673 | foreach ($key as $k => $v) |
| 674 | { |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 675 | $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0) |
Andrey Andreev | 58803fb | 2012-06-24 00:45:37 +0300 | [diff] [blame] | 676 | ? $this->_group_get_type('') |
| 677 | : $this->_group_get_type($type); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 678 | |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 679 | if ($v !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 680 | { |
Andrey Andreev | f7631f1 | 2015-07-27 21:54:41 +0300 | [diff] [blame] | 681 | if ($escape === TRUE) |
| 682 | { |
Andrey Andreev | 7dd6f14 | 2018-01-30 15:08:21 +0200 | [diff] [blame] | 683 | $v = $this->escape($v); |
Andrey Andreev | f7631f1 | 2015-07-27 21:54:41 +0300 | [diff] [blame] | 684 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 685 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 686 | if ( ! $this->_has_operator($k)) |
| 687 | { |
Greg Aker | e156c6e | 2011-04-20 16:03:04 -0500 | [diff] [blame] | 688 | $k .= ' = '; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 689 | } |
| 690 | } |
Andrey Andreev | 3a5efc2 | 2012-11-20 21:18:08 +0200 | [diff] [blame] | 691 | elseif ( ! $this->_has_operator($k)) |
| 692 | { |
| 693 | // value appears not to have been set, assign the test to IS NULL |
| 694 | $k .= ' IS NULL'; |
| 695 | } |
Andrey Andreev | be4bab9 | 2016-10-28 12:50:03 +0300 | [diff] [blame] | 696 | elseif (preg_match('/\s*(!?=|<>|\sIS(?:\s+NOT)?\s)\s*$/i', $k, $match, PREG_OFFSET_CAPTURE)) |
Andrey Andreev | 5bf4dcd | 2014-09-29 20:07:15 +0300 | [diff] [blame] | 697 | { |
| 698 | $k = substr($k, 0, $match[0][1]).($match[1][0] === '=' ? ' IS NULL' : ' IS NOT NULL'); |
| 699 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 700 | |
Andrey Andreev | 7dd6f14 | 2018-01-30 15:08:21 +0200 | [diff] [blame] | 701 | ${$qb_key} = array('condition' => $prefix.$k, 'value' => $v, 'escape' => $escape); |
| 702 | $this->{$qb_key}[] = ${$qb_key}; |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 703 | if ($this->qb_caching === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 704 | { |
Andrey Andreev | 7dd6f14 | 2018-01-30 15:08:21 +0200 | [diff] [blame] | 705 | $this->{$qb_cache_key}[] = ${$qb_key}; |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 706 | $this->qb_cache_exists[] = substr($qb_key, 3); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 710 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 711 | return $this; |
| 712 | } |
| 713 | |
| 714 | // -------------------------------------------------------------------- |
| 715 | |
| 716 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 717 | * WHERE IN |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 718 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 719 | * Generates a WHERE field IN('item', 'item') SQL query, |
| 720 | * joined with 'AND' if appropriate. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 721 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 722 | * @param string $key The field to search |
| 723 | * @param array $values The values searched on |
| 724 | * @param bool $escape |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 725 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 726 | */ |
Andrey Andreev | 498c1e0 | 2012-06-16 03:34:10 +0300 | [diff] [blame] | 727 | public function where_in($key = NULL, $values = NULL, $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 728 | { |
Andrey Andreev | 498c1e0 | 2012-06-16 03:34:10 +0300 | [diff] [blame] | 729 | return $this->_where_in($key, $values, FALSE, 'AND ', $escape); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 730 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 731 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 732 | // -------------------------------------------------------------------- |
| 733 | |
| 734 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 735 | * OR WHERE IN |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 736 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 737 | * Generates a WHERE field IN('item', 'item') SQL query, |
| 738 | * joined with 'OR' if appropriate. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 739 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 740 | * @param string $key The field to search |
| 741 | * @param array $values The values searched on |
| 742 | * @param bool $escape |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 743 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 744 | */ |
Andrey Andreev | 498c1e0 | 2012-06-16 03:34:10 +0300 | [diff] [blame] | 745 | public function or_where_in($key = NULL, $values = NULL, $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 746 | { |
Andrey Andreev | 498c1e0 | 2012-06-16 03:34:10 +0300 | [diff] [blame] | 747 | return $this->_where_in($key, $values, FALSE, 'OR ', $escape); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | // -------------------------------------------------------------------- |
| 751 | |
| 752 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 753 | * WHERE NOT IN |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 754 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 755 | * Generates a WHERE field NOT IN('item', 'item') SQL query, |
| 756 | * joined with 'AND' if appropriate. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 757 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 758 | * @param string $key The field to search |
| 759 | * @param array $values The values searched on |
| 760 | * @param bool $escape |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 761 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 762 | */ |
Andrey Andreev | 498c1e0 | 2012-06-16 03:34:10 +0300 | [diff] [blame] | 763 | public function where_not_in($key = NULL, $values = NULL, $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 764 | { |
Andrey Andreev | 498c1e0 | 2012-06-16 03:34:10 +0300 | [diff] [blame] | 765 | return $this->_where_in($key, $values, TRUE, 'AND ', $escape); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 766 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 767 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 768 | // -------------------------------------------------------------------- |
| 769 | |
| 770 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 771 | * OR WHERE NOT IN |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 772 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 773 | * Generates a WHERE field NOT IN('item', 'item') SQL query, |
| 774 | * joined with 'OR' if appropriate. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 775 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 776 | * @param string $key The field to search |
| 777 | * @param array $values The values searched on |
| 778 | * @param bool $escape |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 779 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 780 | */ |
Andrey Andreev | 498c1e0 | 2012-06-16 03:34:10 +0300 | [diff] [blame] | 781 | public function or_where_not_in($key = NULL, $values = NULL, $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 782 | { |
Andrey Andreev | 498c1e0 | 2012-06-16 03:34:10 +0300 | [diff] [blame] | 783 | return $this->_where_in($key, $values, TRUE, 'OR ', $escape); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | // -------------------------------------------------------------------- |
| 787 | |
| 788 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 789 | * Internal WHERE IN |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 790 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 791 | * @used-by where_in() |
| 792 | * @used-by or_where_in() |
| 793 | * @used-by where_not_in() |
| 794 | * @used-by or_where_not_in() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 795 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 796 | * @param string $key The field to search |
| 797 | * @param array $values The values searched on |
| 798 | * @param bool $not If the statement would be IN or NOT IN |
| 799 | * @param string $type |
| 800 | * @param bool $escape |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 801 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 802 | */ |
Andrey Andreev | 498c1e0 | 2012-06-16 03:34:10 +0300 | [diff] [blame] | 803 | protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 804 | { |
| 805 | if ($key === NULL OR $values === NULL) |
| 806 | { |
Rafael Queiroz | 6600b69 | 2012-06-08 14:34:20 -0300 | [diff] [blame] | 807 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 808 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 809 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 810 | if ( ! is_array($values)) |
| 811 | { |
| 812 | $values = array($values); |
| 813 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 814 | |
Andrey Andreev | 498c1e0 | 2012-06-16 03:34:10 +0300 | [diff] [blame] | 815 | is_bool($escape) OR $escape = $this->_protect_identifiers; |
| 816 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 817 | $not = ($not) ? ' NOT' : ''; |
| 818 | |
Andrey Andreev | d738b6b | 2015-07-29 16:24:57 +0300 | [diff] [blame] | 819 | if ($escape === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 820 | { |
Andrey Andreev | d738b6b | 2015-07-29 16:24:57 +0300 | [diff] [blame] | 821 | $where_in = array(); |
| 822 | foreach ($values as $value) |
| 823 | { |
| 824 | $where_in[] = $this->escape($value); |
| 825 | } |
| 826 | } |
| 827 | else |
| 828 | { |
| 829 | $where_in = array_values($values); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Andrey Andreev | 0d2d84f | 2015-07-31 13:48:59 +0300 | [diff] [blame] | 832 | $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) |
| 833 | ? $this->_group_get_type('') |
| 834 | : $this->_group_get_type($type); |
| 835 | |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 836 | $where_in = array( |
Andrey Andreev | 94611df | 2012-07-19 12:29:54 +0300 | [diff] [blame] | 837 | 'condition' => $prefix.$key.$not.' IN('.implode(', ', $where_in).')', |
Andrey Andreev | 7dd6f14 | 2018-01-30 15:08:21 +0200 | [diff] [blame] | 838 | 'value' => NULL, |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 839 | 'escape' => $escape |
| 840 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 841 | |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 842 | $this->qb_where[] = $where_in; |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 843 | if ($this->qb_caching === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 844 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 845 | $this->qb_cache_where[] = $where_in; |
| 846 | $this->qb_cache_exists[] = 'where'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 849 | return $this; |
| 850 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 851 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 852 | // -------------------------------------------------------------------- |
| 853 | |
| 854 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 855 | * LIKE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 856 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 857 | * Generates a %LIKE% portion of the query. |
| 858 | * Separates multiple calls with 'AND'. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 859 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 860 | * @param mixed $field |
| 861 | * @param string $match |
| 862 | * @param string $side |
| 863 | * @param bool $escape |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 864 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 865 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 866 | public function like($field, $match = '', $side = 'both', $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 867 | { |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 868 | return $this->_like($field, $match, 'AND ', $side, '', $escape); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | // -------------------------------------------------------------------- |
| 872 | |
| 873 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 874 | * NOT LIKE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 875 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 876 | * Generates a NOT LIKE portion of the query. |
| 877 | * Separates multiple calls with 'AND'. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 878 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 879 | * @param mixed $field |
| 880 | * @param string $match |
| 881 | * @param string $side |
| 882 | * @param bool $escape |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 883 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 884 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 885 | public function not_like($field, $match = '', $side = 'both', $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 886 | { |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 887 | return $this->_like($field, $match, 'AND ', $side, 'NOT', $escape); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 888 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 889 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 890 | // -------------------------------------------------------------------- |
| 891 | |
| 892 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 893 | * OR LIKE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 894 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 895 | * Generates a %LIKE% portion of the query. |
| 896 | * Separates multiple calls with 'OR'. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 897 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 898 | * @param mixed $field |
| 899 | * @param string $match |
| 900 | * @param string $side |
| 901 | * @param bool $escape |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 902 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 903 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 904 | public function or_like($field, $match = '', $side = 'both', $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 905 | { |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 906 | return $this->_like($field, $match, 'OR ', $side, '', $escape); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | // -------------------------------------------------------------------- |
| 910 | |
| 911 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 912 | * OR NOT LIKE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 913 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 914 | * Generates a NOT LIKE portion of the query. |
| 915 | * Separates multiple calls with 'OR'. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 916 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 917 | * @param mixed $field |
| 918 | * @param string $match |
| 919 | * @param string $side |
| 920 | * @param bool $escape |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 921 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 922 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 923 | public function or_not_like($field, $match = '', $side = 'both', $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 924 | { |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 925 | return $this->_like($field, $match, 'OR ', $side, 'NOT', $escape); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 926 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 927 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 928 | // -------------------------------------------------------------------- |
| 929 | |
| 930 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 931 | * Internal LIKE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 932 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 933 | * @used-by like() |
| 934 | * @used-by or_like() |
| 935 | * @used-by not_like() |
| 936 | * @used-by or_not_like() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 937 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 938 | * @param mixed $field |
| 939 | * @param string $match |
| 940 | * @param string $type |
| 941 | * @param string $side |
| 942 | * @param string $not |
| 943 | * @param bool $escape |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 944 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 945 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 946 | protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '', $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 947 | { |
| 948 | if ( ! is_array($field)) |
| 949 | { |
| 950 | $field = array($field => $match); |
| 951 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 952 | |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 953 | is_bool($escape) OR $escape = $this->_protect_identifiers; |
Andrey Andreev | 1931136 | 2015-04-07 00:02:14 +0300 | [diff] [blame] | 954 | // lowercase $side in case somebody writes e.g. 'BEFORE' instead of 'before' (doh) |
| 955 | $side = strtolower($side); |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 956 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 957 | foreach ($field as $k => $v) |
| 958 | { |
Andrey Andreev | 4173823 | 2012-11-30 00:13:17 +0200 | [diff] [blame] | 959 | $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) |
| 960 | ? $this->_group_get_type('') : $this->_group_get_type($type); |
| 961 | |
Andrey Andreev | d738b6b | 2015-07-29 16:24:57 +0300 | [diff] [blame] | 962 | if ($escape === TRUE) |
| 963 | { |
| 964 | $v = $this->escape_like_str($v); |
| 965 | } |
Andrey Andreev | fc11dcc | 2012-06-04 16:39:19 +0300 | [diff] [blame] | 966 | |
Andrey Andreev | 7dd6f14 | 2018-01-30 15:08:21 +0200 | [diff] [blame] | 967 | switch ($side) |
Kyle Farris | 81ef70f | 2011-08-31 11:59:12 -0400 | [diff] [blame] | 968 | { |
Andrey Andreev | 7dd6f14 | 2018-01-30 15:08:21 +0200 | [diff] [blame] | 969 | case 'none': |
| 970 | $v = "'{$v}'"; |
| 971 | break; |
| 972 | case 'before': |
Andrey Andreev | 9341189 | 2018-04-14 16:45:56 +0300 | [diff] [blame^] | 973 | $v = "'%{$v}'"; |
Andrey Andreev | 7dd6f14 | 2018-01-30 15:08:21 +0200 | [diff] [blame] | 974 | break; |
| 975 | case 'after': |
| 976 | $v = "'{$v}%'"; |
| 977 | break; |
| 978 | case 'both': |
| 979 | default: |
| 980 | $v = "'%{$v}%'"; |
| 981 | break; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 982 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 983 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 984 | // some platforms require an escape sequence definition for LIKE wildcards |
Andrey Andreev | d738b6b | 2015-07-29 16:24:57 +0300 | [diff] [blame] | 985 | if ($escape === TRUE && $this->_like_escape_str !== '') |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 986 | { |
Andrey Andreev | 7dd6f14 | 2018-01-30 15:08:21 +0200 | [diff] [blame] | 987 | $v .= sprintf($this->_like_escape_str, $this->_like_escape_chr); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 988 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 989 | |
Andrey Andreev | 68b0e08 | 2018-04-13 13:08:52 +0300 | [diff] [blame] | 990 | $qb_where = array('condition' => "{$prefix} {$k} {$not} LIKE {$v}", 'value' => NULL, 'escape' => $escape); |
Andrey Andreev | 7dd6f14 | 2018-01-30 15:08:21 +0200 | [diff] [blame] | 991 | $this->qb_where[] = $qb_where; |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 992 | if ($this->qb_caching === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 993 | { |
Andrey Andreev | 7dd6f14 | 2018-01-30 15:08:21 +0200 | [diff] [blame] | 994 | $this->qb_cache_where[] = $qb_where; |
Andrey Andreev | ededc4a | 2012-07-18 01:16:15 +0300 | [diff] [blame] | 995 | $this->qb_cache_exists[] = 'where'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 996 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 997 | } |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 998 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 999 | return $this; |
| 1000 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1001 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1002 | // -------------------------------------------------------------------- |
| 1003 | |
| 1004 | /** |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1005 | * Starts a query group. |
| 1006 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1007 | * @param string $not (Internal use only) |
| 1008 | * @param string $type (Internal use only) |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 1009 | * @return CI_DB_query_builder |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1010 | */ |
| 1011 | public function group_start($not = '', $type = 'AND ') |
| 1012 | { |
| 1013 | $type = $this->_group_get_type($type); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1014 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1015 | $this->qb_where_group_started = TRUE; |
Jamie Rumbelow | d6ce1e9 | 2012-04-26 13:27:35 +0100 | [diff] [blame] | 1016 | $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type; |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 1017 | $where = array( |
| 1018 | 'condition' => $prefix.$not.str_repeat(' ', ++$this->qb_where_group_count).' (', |
Andrey Andreev | 7dd6f14 | 2018-01-30 15:08:21 +0200 | [diff] [blame] | 1019 | 'value' => NULL, |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 1020 | 'escape' => FALSE |
| 1021 | ); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1022 | |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 1023 | $this->qb_where[] = $where; |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1024 | if ($this->qb_caching) |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1025 | { |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 1026 | $this->qb_cache_where[] = $where; |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | return $this; |
| 1030 | } |
| 1031 | |
| 1032 | // -------------------------------------------------------------------- |
| 1033 | |
| 1034 | /** |
| 1035 | * Starts a query group, but ORs the group |
| 1036 | * |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 1037 | * @return CI_DB_query_builder |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1038 | */ |
| 1039 | public function or_group_start() |
| 1040 | { |
| 1041 | return $this->group_start('', 'OR '); |
| 1042 | } |
| 1043 | |
| 1044 | // -------------------------------------------------------------------- |
| 1045 | |
| 1046 | /** |
| 1047 | * Starts a query group, but NOTs the group |
| 1048 | * |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 1049 | * @return CI_DB_query_builder |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1050 | */ |
| 1051 | public function not_group_start() |
| 1052 | { |
| 1053 | return $this->group_start('NOT ', 'AND '); |
| 1054 | } |
| 1055 | |
| 1056 | // -------------------------------------------------------------------- |
| 1057 | |
| 1058 | /** |
| 1059 | * Starts a query group, but OR NOTs the group |
| 1060 | * |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 1061 | * @return CI_DB_query_builder |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1062 | */ |
| 1063 | public function or_not_group_start() |
| 1064 | { |
| 1065 | return $this->group_start('NOT ', 'OR '); |
| 1066 | } |
| 1067 | |
| 1068 | // -------------------------------------------------------------------- |
| 1069 | |
| 1070 | /** |
| 1071 | * Ends a query group |
| 1072 | * |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 1073 | * @return CI_DB_query_builder |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1074 | */ |
| 1075 | public function group_end() |
| 1076 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1077 | $this->qb_where_group_started = FALSE; |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 1078 | $where = array( |
| 1079 | 'condition' => str_repeat(' ', $this->qb_where_group_count--).')', |
Andrey Andreev | 7dd6f14 | 2018-01-30 15:08:21 +0200 | [diff] [blame] | 1080 | 'value' => NULL, |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 1081 | 'escape' => FALSE |
| 1082 | ); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1083 | |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 1084 | $this->qb_where[] = $where; |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1085 | if ($this->qb_caching) |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1086 | { |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 1087 | $this->qb_cache_where[] = $where; |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1088 | } |
| 1089 | |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1090 | return $this; |
| 1091 | } |
| 1092 | |
| 1093 | // -------------------------------------------------------------------- |
| 1094 | |
| 1095 | /** |
| 1096 | * Group_get_type |
| 1097 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1098 | * @used-by group_start() |
| 1099 | * @used-by _like() |
| 1100 | * @used-by _wh() |
| 1101 | * @used-by _where_in() |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1102 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1103 | * @param string $type |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1104 | * @return string |
| 1105 | */ |
| 1106 | protected function _group_get_type($type) |
| 1107 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1108 | if ($this->qb_where_group_started) |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1109 | { |
| 1110 | $type = ''; |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1111 | $this->qb_where_group_started = FALSE; |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | return $type; |
| 1115 | } |
| 1116 | |
| 1117 | // -------------------------------------------------------------------- |
| 1118 | |
| 1119 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1120 | * GROUP BY |
| 1121 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1122 | * @param string $by |
| 1123 | * @param bool $escape |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 1124 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1125 | */ |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 1126 | public function group_by($by, $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1127 | { |
Andrey Andreev | 96feb58 | 2012-07-19 13:12:34 +0300 | [diff] [blame] | 1128 | is_bool($escape) OR $escape = $this->_protect_identifiers; |
| 1129 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1130 | if (is_string($by)) |
| 1131 | { |
Andrey Andreev | 96feb58 | 2012-07-19 13:12:34 +0300 | [diff] [blame] | 1132 | $by = ($escape === TRUE) |
| 1133 | ? explode(',', $by) |
| 1134 | : array($by); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1135 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1136 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1137 | foreach ($by as $val) |
| 1138 | { |
| 1139 | $val = trim($val); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1140 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1141 | if ($val !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1142 | { |
Andrey Andreev | 96feb58 | 2012-07-19 13:12:34 +0300 | [diff] [blame] | 1143 | $val = array('field' => $val, 'escape' => $escape); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1144 | |
Andrey Andreev | 96feb58 | 2012-07-19 13:12:34 +0300 | [diff] [blame] | 1145 | $this->qb_groupby[] = $val; |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1146 | if ($this->qb_caching === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1147 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1148 | $this->qb_cache_groupby[] = $val; |
| 1149 | $this->qb_cache_exists[] = 'groupby'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | } |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 1153 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1154 | return $this; |
| 1155 | } |
| 1156 | |
| 1157 | // -------------------------------------------------------------------- |
| 1158 | |
| 1159 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1160 | * HAVING |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1161 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1162 | * Separates multiple calls with 'AND'. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1163 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1164 | * @param string $key |
| 1165 | * @param string $value |
| 1166 | * @param bool $escape |
Andrey Andreev | aec5126 | 2016-02-09 21:11:07 +0200 | [diff] [blame] | 1167 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1168 | */ |
Andrey Andreev | 0bcf590 | 2012-10-12 13:03:29 +0300 | [diff] [blame] | 1169 | public function having($key, $value = NULL, $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1170 | { |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 1171 | return $this->_wh('qb_having', $key, $value, 'AND ', $escape); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1172 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1173 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1174 | // -------------------------------------------------------------------- |
| 1175 | |
| 1176 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1177 | * OR HAVING |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1178 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1179 | * Separates multiple calls with 'OR'. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1180 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1181 | * @param string $key |
| 1182 | * @param string $value |
| 1183 | * @param bool $escape |
Andrey Andreev | aec5126 | 2016-02-09 21:11:07 +0200 | [diff] [blame] | 1184 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1185 | */ |
Andrey Andreev | 0bcf590 | 2012-10-12 13:03:29 +0300 | [diff] [blame] | 1186 | public function or_having($key, $value = NULL, $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1187 | { |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 1188 | return $this->_wh('qb_having', $key, $value, 'OR ', $escape); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1189 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1190 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1191 | // -------------------------------------------------------------------- |
| 1192 | |
| 1193 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1194 | * ORDER BY |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1195 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1196 | * @param string $orderby |
vlakoff | 9f8e299 | 2013-09-08 14:05:04 +0200 | [diff] [blame] | 1197 | * @param string $direction ASC, DESC or RANDOM |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1198 | * @param bool $escape |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 1199 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1200 | */ |
Andrey Andreev | d24160c | 2012-06-16 03:21:20 +0300 | [diff] [blame] | 1201 | public function order_by($orderby, $direction = '', $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1202 | { |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 1203 | $direction = strtoupper(trim($direction)); |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 1204 | |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 1205 | if ($direction === 'RANDOM') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1206 | { |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 1207 | $direction = ''; |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 1208 | |
| 1209 | // Do we have a seed value? |
| 1210 | $orderby = ctype_digit((string) $orderby) |
vlakoff | e6c4d5b | 2013-09-08 13:54:57 +0200 | [diff] [blame] | 1211 | ? sprintf($this->_random_keyword[1], $orderby) |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 1212 | : $this->_random_keyword[0]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1213 | } |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 1214 | elseif (empty($orderby)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1215 | { |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 1216 | return $this; |
| 1217 | } |
| 1218 | elseif ($direction !== '') |
| 1219 | { |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 1220 | $direction = in_array($direction, array('ASC', 'DESC'), TRUE) ? ' '.$direction : ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1221 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1222 | |
Andrey Andreev | d24160c | 2012-06-16 03:21:20 +0300 | [diff] [blame] | 1223 | is_bool($escape) OR $escape = $this->_protect_identifiers; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1224 | |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 1225 | if ($escape === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1226 | { |
Andrey Andreev | 93dd2f2 | 2012-10-24 10:09:18 +0300 | [diff] [blame] | 1227 | $qb_orderby[] = array('field' => $orderby, 'direction' => $direction, 'escape' => FALSE); |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 1228 | } |
| 1229 | else |
| 1230 | { |
| 1231 | $qb_orderby = array(); |
| 1232 | foreach (explode(',', $orderby) as $field) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1233 | { |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 1234 | $qb_orderby[] = ($direction === '' && preg_match('/\s+(ASC|DESC)$/i', rtrim($field), $match, PREG_OFFSET_CAPTURE)) |
| 1235 | ? array('field' => ltrim(substr($field, 0, $match[0][1])), 'direction' => ' '.$match[1][0], 'escape' => TRUE) |
| 1236 | : array('field' => trim($field), 'direction' => $direction, 'escape' => TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1237 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1238 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1239 | |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 1240 | $this->qb_orderby = array_merge($this->qb_orderby, $qb_orderby); |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1241 | if ($this->qb_caching === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1242 | { |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 1243 | $this->qb_cache_orderby = array_merge($this->qb_cache_orderby, $qb_orderby); |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1244 | $this->qb_cache_exists[] = 'orderby'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | return $this; |
| 1248 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1249 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1250 | // -------------------------------------------------------------------- |
| 1251 | |
| 1252 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1253 | * LIMIT |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1254 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1255 | * @param int $value LIMIT value |
| 1256 | * @param int $offset OFFSET value |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 1257 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1258 | */ |
Andrey Andreev | 4a587f5 | 2014-12-11 16:27:15 +0200 | [diff] [blame] | 1259 | public function limit($value, $offset = 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1260 | { |
vlakoff | 912f1bc | 2013-01-15 03:34:12 +0100 | [diff] [blame] | 1261 | is_null($value) OR $this->qb_limit = (int) $value; |
Andrey Andreev | 777153d | 2012-06-18 13:30:45 +0300 | [diff] [blame] | 1262 | empty($offset) OR $this->qb_offset = (int) $offset; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1263 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1264 | return $this; |
| 1265 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1266 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1267 | // -------------------------------------------------------------------- |
| 1268 | |
| 1269 | /** |
| 1270 | * Sets the OFFSET value |
| 1271 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1272 | * @param int $offset OFFSET value |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 1273 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1274 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1275 | public function offset($offset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1276 | { |
Andrey Andreev | 777153d | 2012-06-18 13:30:45 +0300 | [diff] [blame] | 1277 | empty($offset) OR $this->qb_offset = (int) $offset; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1278 | return $this; |
| 1279 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1280 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1281 | // -------------------------------------------------------------------- |
| 1282 | |
| 1283 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1284 | * LIMIT string |
Andrey Andreev | 2c35b64 | 2012-06-24 03:05:26 +0300 | [diff] [blame] | 1285 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1286 | * Generates a platform-specific LIMIT clause. |
Andrey Andreev | 2c35b64 | 2012-06-24 03:05:26 +0300 | [diff] [blame] | 1287 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1288 | * @param string $sql SQL Query |
Andrey Andreev | 2c35b64 | 2012-06-24 03:05:26 +0300 | [diff] [blame] | 1289 | * @return string |
| 1290 | */ |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 1291 | protected function _limit($sql) |
Andrey Andreev | 2c35b64 | 2012-06-24 03:05:26 +0300 | [diff] [blame] | 1292 | { |
Andrey Andreev | acc6481 | 2016-07-29 11:42:28 +0300 | [diff] [blame] | 1293 | return $sql.' LIMIT '.($this->qb_offset ? $this->qb_offset.', ' : '').(int) $this->qb_limit; |
Andrey Andreev | 2c35b64 | 2012-06-24 03:05:26 +0300 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | // -------------------------------------------------------------------- |
| 1297 | |
| 1298 | /** |
Andrey Andreev | fe642da | 2012-06-16 03:47:33 +0300 | [diff] [blame] | 1299 | * The "set" function. |
| 1300 | * |
| 1301 | * Allows key/value pairs to be set for inserting or updating |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1302 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1303 | * @param mixed |
| 1304 | * @param string |
Andrey Andreev | a8bb4be | 2012-03-26 15:54:23 +0300 | [diff] [blame] | 1305 | * @param bool |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 1306 | * @return CI_DB_query_builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1307 | */ |
Andrey Andreev | fe642da | 2012-06-16 03:47:33 +0300 | [diff] [blame] | 1308 | public function set($key, $value = '', $escape = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1309 | { |
| 1310 | $key = $this->_object_to_array($key); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1311 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1312 | if ( ! is_array($key)) |
| 1313 | { |
| 1314 | $key = array($key => $value); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1315 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1316 | |
Andrey Andreev | fe642da | 2012-06-16 03:47:33 +0300 | [diff] [blame] | 1317 | is_bool($escape) OR $escape = $this->_protect_identifiers; |
| 1318 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1319 | foreach ($key as $k => $v) |
| 1320 | { |
Andrey Andreev | 2dfbe99 | 2015-07-27 21:54:18 +0300 | [diff] [blame] | 1321 | $this->qb_set[$this->protect_identifiers($k, FALSE, $escape)] = ($escape) |
| 1322 | ? $this->escape($v) : $v; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1323 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1324 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1325 | return $this; |
| 1326 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1327 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1328 | // -------------------------------------------------------------------- |
| 1329 | |
| 1330 | /** |
| 1331 | * Get SELECT query string |
| 1332 | * |
| 1333 | * Compiles a SELECT query string and returns the sql. |
| 1334 | * |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1335 | * @param string the table name to select from (optional) |
Calvin Tam | 55bc505 | 2015-07-24 02:27:24 -0700 | [diff] [blame] | 1336 | * @param bool TRUE: resets QB values; FALSE: leave QB values alone |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1337 | * @return string |
| 1338 | */ |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1339 | public function get_compiled_select($table = '', $reset = TRUE) |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1340 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1341 | if ($table !== '') |
kylefarris | 0a3176b | 2011-08-26 02:37:52 -0400 | [diff] [blame] | 1342 | { |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1343 | $this->_track_aliases($table); |
| 1344 | $this->from($table); |
| 1345 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1346 | |
Andrey Andreev | 650b4c0 | 2012-06-11 12:07:15 +0300 | [diff] [blame] | 1347 | $select = $this->_compile_select(); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1348 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1349 | if ($reset === TRUE) |
| 1350 | { |
| 1351 | $this->_reset_select(); |
| 1352 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1353 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1354 | return $select; |
| 1355 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1356 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1357 | // -------------------------------------------------------------------- |
| 1358 | |
| 1359 | /** |
| 1360 | * Get |
| 1361 | * |
| 1362 | * Compiles the select statement based on the other functions called |
| 1363 | * and runs the query |
| 1364 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1365 | * @param string the table |
| 1366 | * @param string the limit clause |
| 1367 | * @param string the offset clause |
Andrey Andreev | aec5126 | 2016-02-09 21:11:07 +0200 | [diff] [blame] | 1368 | * @return CI_DB_result |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1369 | */ |
Andrey Andreev | 650b4c0 | 2012-06-11 12:07:15 +0300 | [diff] [blame] | 1370 | public function get($table = '', $limit = NULL, $offset = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1371 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1372 | if ($table !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1373 | { |
| 1374 | $this->_track_aliases($table); |
| 1375 | $this->from($table); |
| 1376 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1377 | |
Andrey Andreev | 650b4c0 | 2012-06-11 12:07:15 +0300 | [diff] [blame] | 1378 | if ( ! empty($limit)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1379 | { |
| 1380 | $this->limit($limit, $offset); |
| 1381 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1382 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 1383 | $result = $this->query($this->_compile_select()); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1384 | $this->_reset_select(); |
| 1385 | return $result; |
| 1386 | } |
| 1387 | |
Andrey Andreev | 7b5eb73 | 2012-05-24 20:52:41 +0300 | [diff] [blame] | 1388 | // -------------------------------------------------------------------- |
| 1389 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1390 | /** |
| 1391 | * "Count All Results" query |
| 1392 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1393 | * Generates a platform-specific query string that counts all records |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1394 | * returned by an Query Builder query. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1395 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1396 | * @param string |
yaoshanliang | 2f16405 | 2015-03-16 16:48:15 +0800 | [diff] [blame] | 1397 | * @param bool the reset clause |
vlakoff | c6ac748 | 2013-11-17 00:50:06 +0100 | [diff] [blame] | 1398 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1399 | */ |
yaoshanliang | 19c2847 | 2015-03-15 10:42:18 +0800 | [diff] [blame] | 1400 | public function count_all_results($table = '', $reset = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1401 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1402 | if ($table !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1403 | { |
| 1404 | $this->_track_aliases($table); |
| 1405 | $this->from($table); |
| 1406 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1407 | |
Andrey Andreev | 075bdb4 | 2016-01-25 13:31:23 +0200 | [diff] [blame] | 1408 | // ORDER BY usage is often problematic here (most notably |
| 1409 | // on Microsoft SQL Server) and ultimately unnecessary |
| 1410 | // for selecting COUNT(*) ... |
Andrey Andreev | 71a78c2 | 2017-07-10 14:51:08 +0300 | [diff] [blame] | 1411 | $qb_orderby = $this->qb_orderby; |
Andrey Andreev | 59bae57 | 2017-07-03 14:13:08 +0300 | [diff] [blame] | 1412 | $qb_cache_orderby = $this->qb_cache_orderby; |
pgee70 | cf3924e | 2017-10-03 10:09:04 +1100 | [diff] [blame] | 1413 | $this->qb_orderby = $this->qb_cache_orderby = array(); |
Andrey Andreev | 075bdb4 | 2016-01-25 13:31:23 +0200 | [diff] [blame] | 1414 | |
Andrey Andreev | 2c1b3d9 | 2017-06-15 14:22:49 +0300 | [diff] [blame] | 1415 | $result = ($this->qb_distinct === TRUE OR ! empty($this->qb_groupby) OR ! empty($this->qb_cache_groupby) OR $this->qb_limit OR $this->qb_offset) |
Andrey Andreev | b05f506 | 2012-10-26 12:01:02 +0300 | [diff] [blame] | 1416 | ? $this->query($this->_count_string.$this->protect_identifiers('numrows')."\nFROM (\n".$this->_compile_select()."\n) CI_count_all_results") |
| 1417 | : $this->query($this->_compile_select($this->_count_string.$this->protect_identifiers('numrows'))); |
yaoshanliang | 9971e7b | 2015-03-14 13:09:16 +0800 | [diff] [blame] | 1418 | |
yaoshanliang | 2f16405 | 2015-03-16 16:48:15 +0800 | [diff] [blame] | 1419 | if ($reset === TRUE) |
yaoshanliang | 19c2847 | 2015-03-15 10:42:18 +0800 | [diff] [blame] | 1420 | { |
yaoshanliang | 2f16405 | 2015-03-16 16:48:15 +0800 | [diff] [blame] | 1421 | $this->_reset_select(); |
yaoshanliang | 19c2847 | 2015-03-15 10:42:18 +0800 | [diff] [blame] | 1422 | } |
Andrey Andreev | 59bae57 | 2017-07-03 14:13:08 +0300 | [diff] [blame] | 1423 | else |
Andrey Andreev | 075bdb4 | 2016-01-25 13:31:23 +0200 | [diff] [blame] | 1424 | { |
Andrey Andreev | 59bae57 | 2017-07-03 14:13:08 +0300 | [diff] [blame] | 1425 | $this->qb_orderby = $qb_orderby; |
| 1426 | $this->qb_cache_orderby = $qb_cache_orderby; |
Andrey Andreev | 075bdb4 | 2016-01-25 13:31:23 +0200 | [diff] [blame] | 1427 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1428 | |
Purwandi | 1d160e7 | 2012-01-09 16:33:28 +0700 | [diff] [blame] | 1429 | if ($result->num_rows() === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1430 | { |
Phil Sturgeon | af6f344 | 2011-03-22 19:12:23 +0000 | [diff] [blame] | 1431 | return 0; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Purwandi | 1d160e7 | 2012-01-09 16:33:28 +0700 | [diff] [blame] | 1434 | $row = $result->row(); |
Phil Sturgeon | af6f344 | 2011-03-22 19:12:23 +0000 | [diff] [blame] | 1435 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1436 | } |
Andrey Andreev | 7b5eb73 | 2012-05-24 20:52:41 +0300 | [diff] [blame] | 1437 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1438 | // -------------------------------------------------------------------- |
| 1439 | |
| 1440 | /** |
Andrey Andreev | 7dd6f14 | 2018-01-30 15:08:21 +0200 | [diff] [blame] | 1441 | * get_where() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1442 | * |
| 1443 | * Allows the where clause, limit and offset to be added directly |
| 1444 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1445 | * @param string $table |
| 1446 | * @param string $where |
| 1447 | * @param int $limit |
| 1448 | * @param int $offset |
Andrey Andreev | aec5126 | 2016-02-09 21:11:07 +0200 | [diff] [blame] | 1449 | * @return CI_DB_result |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1450 | */ |
Andrey Andreev | eb22d54 | 2012-06-26 23:16:35 +0300 | [diff] [blame] | 1451 | public function get_where($table = '', $where = NULL, $limit = NULL, $offset = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1452 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1453 | if ($table !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1454 | { |
| 1455 | $this->from($table); |
| 1456 | } |
| 1457 | |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 1458 | if ($where !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1459 | { |
| 1460 | $this->where($where); |
| 1461 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1462 | |
Andrey Andreev | 650b4c0 | 2012-06-11 12:07:15 +0300 | [diff] [blame] | 1463 | if ( ! empty($limit)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1464 | { |
| 1465 | $this->limit($limit, $offset); |
| 1466 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1467 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 1468 | $result = $this->query($this->_compile_select()); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1469 | $this->_reset_select(); |
| 1470 | return $result; |
| 1471 | } |
| 1472 | |
| 1473 | // -------------------------------------------------------------------- |
| 1474 | |
| 1475 | /** |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1476 | * Insert_Batch |
| 1477 | * |
| 1478 | * Compiles batch insert strings and runs the queries |
| 1479 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1480 | * @param string $table Table to insert into |
| 1481 | * @param array $set An associative array of insert values |
Andrey Andreev | bdd9c11 | 2012-11-06 12:07:16 +0200 | [diff] [blame] | 1482 | * @param bool $escape Whether to escape values and identifiers |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1483 | * @return int Number of rows inserted or FALSE on failure |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1484 | */ |
Andrey Andreev | 105a48b | 2016-02-04 15:45:10 +0200 | [diff] [blame] | 1485 | public function insert_batch($table, $set = NULL, $escape = NULL, $batch_size = 100) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1486 | { |
Andrey Andreev | 8ec82e2 | 2016-01-26 16:33:31 +0200 | [diff] [blame] | 1487 | if ($set === NULL) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1488 | { |
Andrey Andreev | 8ec82e2 | 2016-01-26 16:33:31 +0200 | [diff] [blame] | 1489 | if (empty($this->qb_set)) |
| 1490 | { |
| 1491 | return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; |
| 1492 | } |
| 1493 | } |
| 1494 | else |
| 1495 | { |
| 1496 | if (empty($set)) |
| 1497 | { |
| 1498 | return ($this->db_debug) ? $this->display_error('insert_batch() called with no data') : FALSE; |
| 1499 | } |
| 1500 | |
Andrey Andreev | bdd9c11 | 2012-11-06 12:07:16 +0200 | [diff] [blame] | 1501 | $this->set_insert_batch($set, '', $escape); |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1502 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1503 | |
Andrey Andreev | 8ec82e2 | 2016-01-26 16:33:31 +0200 | [diff] [blame] | 1504 | if (strlen($table) === 0) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1505 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1506 | if ( ! isset($this->qb_from[0])) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1507 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 1508 | return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1509 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1510 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1511 | $table = $this->qb_from[0]; |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | // Batch this baby |
Andrey Andreev | 9f808b0 | 2012-10-24 17:38:48 +0300 | [diff] [blame] | 1515 | $affected_rows = 0; |
Andrey Andreev | 105a48b | 2016-02-04 15:45:10 +0200 | [diff] [blame] | 1516 | for ($i = 0, $total = count($this->qb_set); $i < $total; $i += $batch_size) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1517 | { |
Andrey Andreev | d9a4063 | 2016-07-22 15:49:08 +0300 | [diff] [blame] | 1518 | if ($this->query($this->_insert_batch($this->protect_identifiers($table, TRUE, $escape, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, $batch_size)))) |
| 1519 | { |
| 1520 | $affected_rows += $this->affected_rows(); |
| 1521 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1522 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1523 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1524 | $this->_reset_write(); |
Andrey Andreev | 9f808b0 | 2012-10-24 17:38:48 +0300 | [diff] [blame] | 1525 | return $affected_rows; |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | // -------------------------------------------------------------------- |
| 1529 | |
| 1530 | /** |
Andrey Andreev | 083e3c8 | 2012-11-06 12:48:32 +0200 | [diff] [blame] | 1531 | * Insert batch statement |
Andrey Andreev | 97f3697 | 2012-04-05 12:44:36 +0300 | [diff] [blame] | 1532 | * |
| 1533 | * Generates a platform-specific insert string from the supplied data. |
| 1534 | * |
Andrey Andreev | 083e3c8 | 2012-11-06 12:48:32 +0200 | [diff] [blame] | 1535 | * @param string $table Table name |
| 1536 | * @param array $keys INSERT keys |
| 1537 | * @param array $values INSERT values |
Andrey Andreev | a3bca8f | 2012-04-05 15:17:25 +0300 | [diff] [blame] | 1538 | * @return string |
Andrey Andreev | 97f3697 | 2012-04-05 12:44:36 +0300 | [diff] [blame] | 1539 | */ |
| 1540 | protected function _insert_batch($table, $keys, $values) |
| 1541 | { |
Andrey Andreev | 65d537c | 2012-04-05 14:11:41 +0300 | [diff] [blame] | 1542 | return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values); |
Andrey Andreev | 97f3697 | 2012-04-05 12:44:36 +0300 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | // -------------------------------------------------------------------- |
| 1546 | |
| 1547 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1548 | * 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] | 1549 | * |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1550 | * @param mixed |
| 1551 | * @param string |
Andrey Andreev | a8bb4be | 2012-03-26 15:54:23 +0300 | [diff] [blame] | 1552 | * @param bool |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 1553 | * @return CI_DB_query_builder |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1554 | */ |
Andrey Andreev | fe642da | 2012-06-16 03:47:33 +0300 | [diff] [blame] | 1555 | public function set_insert_batch($key, $value = '', $escape = NULL) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1556 | { |
| 1557 | $key = $this->_object_to_array_batch($key); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1558 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1559 | if ( ! is_array($key)) |
| 1560 | { |
| 1561 | $key = array($key => $value); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1562 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1563 | |
Andrey Andreev | fe642da | 2012-06-16 03:47:33 +0300 | [diff] [blame] | 1564 | is_bool($escape) OR $escape = $this->_protect_identifiers; |
| 1565 | |
Andrey Andreev | 51c84f3 | 2017-01-03 17:42:26 +0200 | [diff] [blame] | 1566 | $keys = array_keys($this->_object_to_array(reset($key))); |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1567 | sort($keys); |
| 1568 | |
| 1569 | foreach ($key as $row) |
| 1570 | { |
Iban Eguia | 3c0a452 | 2012-04-15 13:30:44 +0200 | [diff] [blame] | 1571 | $row = $this->_object_to_array($row); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1572 | if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0) |
| 1573 | { |
| 1574 | // batch function above returns an error on an empty array |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1575 | $this->qb_set[] = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1576 | return; |
| 1577 | } |
Phil Sturgeon | d0ac1a2 | 2011-02-15 22:54:08 +0000 | [diff] [blame] | 1578 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1579 | ksort($row); // puts $row in the same order as our keys |
| 1580 | |
Andrey Andreev | 2dfbe99 | 2015-07-27 21:54:18 +0300 | [diff] [blame] | 1581 | if ($escape !== FALSE) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1582 | { |
Andrey Andreev | 2dfbe99 | 2015-07-27 21:54:18 +0300 | [diff] [blame] | 1583 | $clean = array(); |
| 1584 | foreach ($row as $value) |
| 1585 | { |
| 1586 | $clean[] = $this->escape($value); |
| 1587 | } |
| 1588 | |
| 1589 | $row = $clean; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1590 | } |
Andrey Andreev | 650b4c0 | 2012-06-11 12:07:15 +0300 | [diff] [blame] | 1591 | |
Andrey Andreev | 838a9d6 | 2012-12-03 14:37:47 +0200 | [diff] [blame] | 1592 | $this->qb_set[] = '('.implode(',', $row).')'; |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | foreach ($keys as $k) |
| 1596 | { |
Andrey Andreev | fe642da | 2012-06-16 03:47:33 +0300 | [diff] [blame] | 1597 | $this->qb_keys[] = $this->protect_identifiers($k, FALSE, $escape); |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1598 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1599 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1600 | return $this; |
| 1601 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1602 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1603 | // -------------------------------------------------------------------- |
| 1604 | |
| 1605 | /** |
| 1606 | * Get INSERT query string |
| 1607 | * |
| 1608 | * Compiles an insert query and returns the sql |
| 1609 | * |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1610 | * @param string the table to insert into |
Jamie Rumbelow | d6ce1e9 | 2012-04-26 13:27:35 +0100 | [diff] [blame] | 1611 | * @param bool TRUE: reset QB values; FALSE: leave QB values alone |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1612 | * @return string |
| 1613 | */ |
| 1614 | public function get_compiled_insert($table = '', $reset = TRUE) |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1615 | { |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1616 | if ($this->_validate_insert($table) === FALSE) |
| 1617 | { |
| 1618 | return FALSE; |
| 1619 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1620 | |
Kyle Farris | 7611601 | 2011-08-31 11:17:48 -0400 | [diff] [blame] | 1621 | $sql = $this->_insert( |
Jamie Rumbelow | 0c09299 | 2012-03-06 22:05:16 +0000 | [diff] [blame] | 1622 | $this->protect_identifiers( |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1623 | $this->qb_from[0], TRUE, NULL, FALSE |
Kyle Farris | 7611601 | 2011-08-31 11:17:48 -0400 | [diff] [blame] | 1624 | ), |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1625 | array_keys($this->qb_set), |
| 1626 | array_values($this->qb_set) |
Kyle Farris | 7611601 | 2011-08-31 11:17:48 -0400 | [diff] [blame] | 1627 | ); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1628 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1629 | if ($reset === TRUE) |
| 1630 | { |
| 1631 | $this->_reset_write(); |
| 1632 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1633 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1634 | return $sql; |
| 1635 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1636 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1637 | // -------------------------------------------------------------------- |
| 1638 | |
| 1639 | /** |
| 1640 | * Insert |
| 1641 | * |
| 1642 | * Compiles an insert string and runs the query |
| 1643 | * |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1644 | * @param string the table to insert data into |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1645 | * @param array an associative array of insert values |
Andrey Andreev | bdd9c11 | 2012-11-06 12:07:16 +0200 | [diff] [blame] | 1646 | * @param bool $escape Whether to escape values and identifiers |
Andrey Andreev | aec5126 | 2016-02-09 21:11:07 +0200 | [diff] [blame] | 1647 | * @return bool TRUE on success, FALSE on failure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1648 | */ |
Andrey Andreev | bdd9c11 | 2012-11-06 12:07:16 +0200 | [diff] [blame] | 1649 | public function insert($table = '', $set = NULL, $escape = NULL) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1650 | { |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 1651 | if ($set !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1652 | { |
Andrey Andreev | bdd9c11 | 2012-11-06 12:07:16 +0200 | [diff] [blame] | 1653 | $this->set($set, '', $escape); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1654 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1655 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1656 | if ($this->_validate_insert($table) === FALSE) |
| 1657 | { |
| 1658 | return FALSE; |
| 1659 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1660 | |
Kyle Farris | 7611601 | 2011-08-31 11:17:48 -0400 | [diff] [blame] | 1661 | $sql = $this->_insert( |
Jamie Rumbelow | 0c09299 | 2012-03-06 22:05:16 +0000 | [diff] [blame] | 1662 | $this->protect_identifiers( |
Andrey Andreev | bdd9c11 | 2012-11-06 12:07:16 +0200 | [diff] [blame] | 1663 | $this->qb_from[0], TRUE, $escape, FALSE |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1664 | ), |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1665 | array_keys($this->qb_set), |
| 1666 | array_values($this->qb_set) |
Kyle Farris | 7611601 | 2011-08-31 11:17:48 -0400 | [diff] [blame] | 1667 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1668 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1669 | $this->_reset_write(); |
| 1670 | return $this->query($sql); |
| 1671 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1672 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1673 | // -------------------------------------------------------------------- |
| 1674 | |
| 1675 | /** |
| 1676 | * Validate Insert |
| 1677 | * |
| 1678 | * This method is used by both insert() and get_compiled_insert() to |
| 1679 | * validate that the there data is actually being set and that table |
| 1680 | * has been chosen to be inserted into. |
| 1681 | * |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1682 | * @param string the table to insert data into |
| 1683 | * @return string |
| 1684 | */ |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1685 | protected function _validate_insert($table = '') |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1686 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1687 | if (count($this->qb_set) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1688 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 1689 | return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1692 | if ($table !== '') |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1693 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1694 | $this->qb_from[0] = $table; |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1695 | } |
Andrey Andreev | 7b5eb73 | 2012-05-24 20:52:41 +0300 | [diff] [blame] | 1696 | elseif ( ! isset($this->qb_from[0])) |
| 1697 | { |
| 1698 | return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; |
| 1699 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1700 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1701 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1702 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1703 | |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1704 | // -------------------------------------------------------------------- |
| 1705 | |
| 1706 | /** |
| 1707 | * Replace |
| 1708 | * |
| 1709 | * Compiles an replace into string and runs the query |
| 1710 | * |
| 1711 | * @param string the table to replace data into |
| 1712 | * @param array an associative array of insert values |
Andrey Andreev | aec5126 | 2016-02-09 21:11:07 +0200 | [diff] [blame] | 1713 | * @return bool TRUE on success, FALSE on failure |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1714 | */ |
| 1715 | public function replace($table = '', $set = NULL) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1716 | { |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 1717 | if ($set !== NULL) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1718 | { |
| 1719 | $this->set($set); |
| 1720 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1721 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1722 | if (count($this->qb_set) === 0) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1723 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 1724 | return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1725 | } |
| 1726 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1727 | if ($table === '') |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1728 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1729 | if ( ! isset($this->qb_from[0])) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1730 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 1731 | return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1732 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1733 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1734 | $table = $this->qb_from[0]; |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1735 | } |
| 1736 | |
Jamie Rumbelow | 0c09299 | 2012-03-06 22:05:16 +0000 | [diff] [blame] | 1737 | $sql = $this->_replace($this->protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->qb_set), array_values($this->qb_set)); |
Jamie Rumbelow | 3b1355c | 2012-03-06 21:27:46 +0000 | [diff] [blame] | 1738 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1739 | $this->_reset_write(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1740 | return $this->query($sql); |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1741 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1742 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1743 | // -------------------------------------------------------------------- |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1744 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1745 | /** |
Andrey Andreev | a3bca8f | 2012-04-05 15:17:25 +0300 | [diff] [blame] | 1746 | * Replace statement |
| 1747 | * |
| 1748 | * Generates a platform-specific replace string from the supplied data |
| 1749 | * |
| 1750 | * @param string the table name |
| 1751 | * @param array the insert keys |
| 1752 | * @param array the insert values |
| 1753 | * @return string |
| 1754 | */ |
| 1755 | protected function _replace($table, $keys, $values) |
| 1756 | { |
| 1757 | return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; |
| 1758 | } |
| 1759 | |
| 1760 | // -------------------------------------------------------------------- |
| 1761 | |
| 1762 | /** |
Andrey Andreev | 7eaa14f | 2012-10-09 11:34:01 +0300 | [diff] [blame] | 1763 | * FROM tables |
Andrey Andreev | c78e56a | 2012-06-08 02:12:07 +0300 | [diff] [blame] | 1764 | * |
Andrey Andreev | 7eaa14f | 2012-10-09 11:34:01 +0300 | [diff] [blame] | 1765 | * Groups tables in FROM clauses if needed, so there is no confusion |
| 1766 | * about operator precedence. |
Andrey Andreev | c78e56a | 2012-06-08 02:12:07 +0300 | [diff] [blame] | 1767 | * |
Claudio Galdiolo | 93e7813 | 2015-01-29 11:43:56 -0500 | [diff] [blame] | 1768 | * Note: This is only used (and overridden) by MySQL and CUBRID. |
Andrey Andreev | 7eaa14f | 2012-10-09 11:34:01 +0300 | [diff] [blame] | 1769 | * |
| 1770 | * @return string |
Andrey Andreev | c78e56a | 2012-06-08 02:12:07 +0300 | [diff] [blame] | 1771 | */ |
Andrey Andreev | 7eaa14f | 2012-10-09 11:34:01 +0300 | [diff] [blame] | 1772 | protected function _from_tables() |
Andrey Andreev | c78e56a | 2012-06-08 02:12:07 +0300 | [diff] [blame] | 1773 | { |
Andrey Andreev | 7eaa14f | 2012-10-09 11:34:01 +0300 | [diff] [blame] | 1774 | return implode(', ', $this->qb_from); |
Andrey Andreev | c78e56a | 2012-06-08 02:12:07 +0300 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | // -------------------------------------------------------------------- |
| 1778 | |
| 1779 | /** |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1780 | * Get UPDATE query string |
| 1781 | * |
| 1782 | * Compiles an update query and returns the sql |
| 1783 | * |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1784 | * @param string the table to update |
Jamie Rumbelow | d6ce1e9 | 2012-04-26 13:27:35 +0100 | [diff] [blame] | 1785 | * @param bool TRUE: reset QB values; FALSE: leave QB values alone |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1786 | * @return string |
| 1787 | */ |
| 1788 | public function get_compiled_update($table = '', $reset = TRUE) |
| 1789 | { |
| 1790 | // Combine any cached components with the current statements |
| 1791 | $this->_merge_cache(); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1792 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1793 | if ($this->_validate_update($table) === FALSE) |
| 1794 | { |
| 1795 | return FALSE; |
| 1796 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1797 | |
Andrey Andreev | 4b9fec6 | 2015-07-20 17:26:31 +0300 | [diff] [blame] | 1798 | $sql = $this->_update($this->qb_from[0], $this->qb_set); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1799 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1800 | if ($reset === TRUE) |
| 1801 | { |
| 1802 | $this->_reset_write(); |
| 1803 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1804 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1805 | return $sql; |
| 1806 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1807 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1808 | // -------------------------------------------------------------------- |
| 1809 | |
| 1810 | /** |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1811 | * UPDATE |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1812 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1813 | * Compiles an update string and runs the query. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1814 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 1815 | * @param string $table |
| 1816 | * @param array $set An associative array of update values |
| 1817 | * @param mixed $where |
| 1818 | * @param int $limit |
Andrey Andreev | aec5126 | 2016-02-09 21:11:07 +0200 | [diff] [blame] | 1819 | * @return bool TRUE on success, FALSE on failure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1820 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 1821 | public function update($table = '', $set = NULL, $where = NULL, $limit = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1822 | { |
| 1823 | // Combine any cached components with the current statements |
| 1824 | $this->_merge_cache(); |
| 1825 | |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 1826 | if ($set !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1827 | { |
| 1828 | $this->set($set); |
| 1829 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1830 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1831 | if ($this->_validate_update($table) === FALSE) |
| 1832 | { |
| 1833 | return FALSE; |
| 1834 | } |
| 1835 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1836 | if ($where !== NULL) |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1837 | { |
| 1838 | $this->where($where); |
| 1839 | } |
| 1840 | |
Andrey Andreev | 650b4c0 | 2012-06-11 12:07:15 +0300 | [diff] [blame] | 1841 | if ( ! empty($limit)) |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1842 | { |
| 1843 | $this->limit($limit); |
| 1844 | } |
| 1845 | |
Andrey Andreev | 4b9fec6 | 2015-07-20 17:26:31 +0300 | [diff] [blame] | 1846 | $sql = $this->_update($this->qb_from[0], $this->qb_set); |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1847 | $this->_reset_write(); |
| 1848 | return $this->query($sql); |
| 1849 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1850 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1851 | // -------------------------------------------------------------------- |
| 1852 | |
| 1853 | /** |
| 1854 | * Validate Update |
| 1855 | * |
| 1856 | * This method is used by both update() and get_compiled_update() to |
| 1857 | * validate that data is actually being set and that a table has been |
| 1858 | * chosen to be update. |
| 1859 | * |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1860 | * @param string the table to update data on |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 1861 | * @return bool |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1862 | */ |
Andrey Andreev | 4b9fec6 | 2015-07-20 17:26:31 +0300 | [diff] [blame] | 1863 | protected function _validate_update($table) |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 1864 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1865 | if (count($this->qb_set) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1866 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 1867 | return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 1870 | if ($table !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1871 | { |
Andrey Andreev | 4b9fec6 | 2015-07-20 17:26:31 +0300 | [diff] [blame] | 1872 | $this->qb_from = array($this->protect_identifiers($table, TRUE, NULL, FALSE)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1873 | } |
Andrey Andreev | 7b5eb73 | 2012-05-24 20:52:41 +0300 | [diff] [blame] | 1874 | elseif ( ! isset($this->qb_from[0])) |
| 1875 | { |
| 1876 | return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; |
| 1877 | } |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 1878 | |
| 1879 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1880 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1881 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1882 | // -------------------------------------------------------------------- |
| 1883 | |
| 1884 | /** |
| 1885 | * Update_Batch |
| 1886 | * |
| 1887 | * Compiles an update string and runs the query |
| 1888 | * |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1889 | * @param string the table to retrieve the results from |
| 1890 | * @param array an associative array of update values |
| 1891 | * @param string the where key |
Andrey Andreev | 9f808b0 | 2012-10-24 17:38:48 +0300 | [diff] [blame] | 1892 | * @return int number of rows affected or FALSE on failure |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1893 | */ |
Andrey Andreev | 105a48b | 2016-02-04 15:45:10 +0200 | [diff] [blame] | 1894 | public function update_batch($table, $set = NULL, $index = NULL, $batch_size = 100) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1895 | { |
| 1896 | // Combine any cached components with the current statements |
| 1897 | $this->_merge_cache(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1898 | |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 1899 | if ($index === NULL) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1900 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 1901 | return ($this->db_debug) ? $this->display_error('db_must_use_index') : FALSE; |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1902 | } |
| 1903 | |
Andrey Andreev | 8ec82e2 | 2016-01-26 16:33:31 +0200 | [diff] [blame] | 1904 | if ($set === NULL) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1905 | { |
Andrey Andreev | 8338bbb | 2016-12-12 14:17:52 +0200 | [diff] [blame] | 1906 | if (empty($this->qb_set_ub)) |
Andrey Andreev | 8ec82e2 | 2016-01-26 16:33:31 +0200 | [diff] [blame] | 1907 | { |
| 1908 | return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; |
| 1909 | } |
| 1910 | } |
| 1911 | else |
| 1912 | { |
| 1913 | if (empty($set)) |
| 1914 | { |
| 1915 | return ($this->db_debug) ? $this->display_error('update_batch() called with no data') : FALSE; |
| 1916 | } |
| 1917 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1918 | $this->set_update_batch($set, $index); |
| 1919 | } |
| 1920 | |
Andrey Andreev | 8ec82e2 | 2016-01-26 16:33:31 +0200 | [diff] [blame] | 1921 | if (strlen($table) === 0) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1922 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1923 | if ( ! isset($this->qb_from[0])) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1924 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 1925 | return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1926 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1927 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1928 | $table = $this->qb_from[0]; |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1929 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1930 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1931 | // Batch this baby |
Andrey Andreev | 9f808b0 | 2012-10-24 17:38:48 +0300 | [diff] [blame] | 1932 | $affected_rows = 0; |
Andrey Andreev | 8338bbb | 2016-12-12 14:17:52 +0200 | [diff] [blame] | 1933 | for ($i = 0, $total = count($this->qb_set_ub); $i < $total; $i += $batch_size) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1934 | { |
Andrey Andreev | 8338bbb | 2016-12-12 14:17:52 +0200 | [diff] [blame] | 1935 | if ($this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set_ub, $i, $batch_size), $index))) |
Andrey Andreev | d9a4063 | 2016-07-22 15:49:08 +0300 | [diff] [blame] | 1936 | { |
| 1937 | $affected_rows += $this->affected_rows(); |
| 1938 | } |
| 1939 | |
Andrey Andreev | 79f888b | 2013-08-06 13:59:23 +0300 | [diff] [blame] | 1940 | $this->qb_where = array(); |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1941 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1942 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1943 | $this->_reset_write(); |
Andrey Andreev | 9f808b0 | 2012-10-24 17:38:48 +0300 | [diff] [blame] | 1944 | return $affected_rows; |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | // -------------------------------------------------------------------- |
| 1948 | |
| 1949 | /** |
Andrey Andreev | 219565d | 2013-03-12 20:00:08 +0200 | [diff] [blame] | 1950 | * Update_Batch statement |
| 1951 | * |
| 1952 | * Generates a platform-specific batch update string from the supplied data |
| 1953 | * |
| 1954 | * @param string $table Table name |
| 1955 | * @param array $values Update data |
| 1956 | * @param string $index WHERE key |
| 1957 | * @return string |
| 1958 | */ |
| 1959 | protected function _update_batch($table, $values, $index) |
| 1960 | { |
| 1961 | $ids = array(); |
| 1962 | foreach ($values as $key => $val) |
| 1963 | { |
Andrey Andreev | 8338bbb | 2016-12-12 14:17:52 +0200 | [diff] [blame] | 1964 | $ids[] = $val[$index]['value']; |
Andrey Andreev | 219565d | 2013-03-12 20:00:08 +0200 | [diff] [blame] | 1965 | |
| 1966 | foreach (array_keys($val) as $field) |
| 1967 | { |
| 1968 | if ($field !== $index) |
| 1969 | { |
Andrey Andreev | 8338bbb | 2016-12-12 14:17:52 +0200 | [diff] [blame] | 1970 | $final[$val[$field]['field']][] = 'WHEN '.$val[$index]['field'].' = '.$val[$index]['value'].' THEN '.$val[$field]['value']; |
Andrey Andreev | 219565d | 2013-03-12 20:00:08 +0200 | [diff] [blame] | 1971 | } |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | $cases = ''; |
| 1976 | foreach ($final as $k => $v) |
| 1977 | { |
| 1978 | $cases .= $k." = CASE \n" |
| 1979 | .implode("\n", $v)."\n" |
| 1980 | .'ELSE '.$k.' END, '; |
| 1981 | } |
| 1982 | |
Andrey Andreev | 8338bbb | 2016-12-12 14:17:52 +0200 | [diff] [blame] | 1983 | $this->where($val[$index]['field'].' IN('.implode(',', $ids).')', NULL, FALSE); |
Andrey Andreev | 219565d | 2013-03-12 20:00:08 +0200 | [diff] [blame] | 1984 | |
| 1985 | return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where'); |
| 1986 | } |
| 1987 | |
| 1988 | // -------------------------------------------------------------------- |
| 1989 | |
| 1990 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1991 | * 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] | 1992 | * |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1993 | * @param array |
| 1994 | * @param string |
Andrey Andreev | a8bb4be | 2012-03-26 15:54:23 +0300 | [diff] [blame] | 1995 | * @param bool |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 1996 | * @return CI_DB_query_builder |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1997 | */ |
Andrey Andreev | fe642da | 2012-06-16 03:47:33 +0300 | [diff] [blame] | 1998 | public function set_update_batch($key, $index = '', $escape = NULL) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 1999 | { |
| 2000 | $key = $this->_object_to_array_batch($key); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2001 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2002 | if ( ! is_array($key)) |
| 2003 | { |
| 2004 | // @todo error |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2005 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2006 | |
Andrey Andreev | fe642da | 2012-06-16 03:47:33 +0300 | [diff] [blame] | 2007 | is_bool($escape) OR $escape = $this->_protect_identifiers; |
| 2008 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2009 | foreach ($key as $k => $v) |
| 2010 | { |
| 2011 | $index_set = FALSE; |
| 2012 | $clean = array(); |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 2013 | foreach ($v as $k2 => $v2) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2014 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 2015 | if ($k2 === $index) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2016 | { |
| 2017 | $index_set = TRUE; |
| 2018 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2019 | |
Andrey Andreev | 8338bbb | 2016-12-12 14:17:52 +0200 | [diff] [blame] | 2020 | $clean[$k2] = array( |
| 2021 | 'field' => $this->protect_identifiers($k2, FALSE, $escape), |
| 2022 | 'value' => ($escape === FALSE ? $v2 : $this->escape($v2)) |
| 2023 | ); |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2024 | } |
| 2025 | |
Andrey Andreev | 7b5eb73 | 2012-05-24 20:52:41 +0300 | [diff] [blame] | 2026 | if ($index_set === FALSE) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2027 | { |
| 2028 | return $this->display_error('db_batch_missing_index'); |
| 2029 | } |
| 2030 | |
Andrey Andreev | 8338bbb | 2016-12-12 14:17:52 +0200 | [diff] [blame] | 2031 | $this->qb_set_ub[] = $clean; |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2032 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2033 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2034 | return $this; |
| 2035 | } |
| 2036 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2037 | // -------------------------------------------------------------------- |
| 2038 | |
| 2039 | /** |
| 2040 | * Empty Table |
| 2041 | * |
| 2042 | * Compiles a delete string and runs "DELETE FROM table" |
| 2043 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2044 | * @param string the table to empty |
Andrey Andreev | aec5126 | 2016-02-09 21:11:07 +0200 | [diff] [blame] | 2045 | * @return bool TRUE on success, FALSE on failure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2046 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2047 | public function empty_table($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2048 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 2049 | if ($table === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2050 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2051 | if ( ! isset($this->qb_from[0])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2052 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 2053 | return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2056 | $table = $this->qb_from[0]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2057 | } |
| 2058 | else |
| 2059 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 2060 | $table = $this->protect_identifiers($table, TRUE, NULL, FALSE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | $sql = $this->_delete($table); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2064 | $this->_reset_write(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2065 | return $this->query($sql); |
| 2066 | } |
| 2067 | |
| 2068 | // -------------------------------------------------------------------- |
| 2069 | |
| 2070 | /** |
| 2071 | * Truncate |
| 2072 | * |
| 2073 | * Compiles a truncate string and runs the query |
| 2074 | * If the database does not support the truncate() command |
| 2075 | * This function maps to "DELETE FROM table" |
| 2076 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2077 | * @param string the table to truncate |
Andrey Andreev | aec5126 | 2016-02-09 21:11:07 +0200 | [diff] [blame] | 2078 | * @return bool TRUE on success, FALSE on failure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2079 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2080 | public function truncate($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2081 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 2082 | if ($table === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2083 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2084 | if ( ! isset($this->qb_from[0])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2085 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 2086 | return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2087 | } |
| 2088 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2089 | $table = $this->qb_from[0]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2090 | } |
| 2091 | else |
| 2092 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 2093 | $table = $this->protect_identifiers($table, TRUE, NULL, FALSE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | $sql = $this->_truncate($table); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2097 | $this->_reset_write(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2098 | return $this->query($sql); |
| 2099 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 2100 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 2101 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2102 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 2103 | /** |
Andrey Andreev | a6fe36e | 2012-04-05 16:00:32 +0300 | [diff] [blame] | 2104 | * Truncate statement |
| 2105 | * |
| 2106 | * Generates a platform-specific truncate string from the supplied data |
| 2107 | * |
| 2108 | * If the database does not support the truncate() command, |
| 2109 | * then this method maps to 'DELETE FROM table' |
| 2110 | * |
| 2111 | * @param string the table name |
| 2112 | * @return string |
| 2113 | */ |
| 2114 | protected function _truncate($table) |
| 2115 | { |
| 2116 | return 'TRUNCATE '.$table; |
| 2117 | } |
| 2118 | |
| 2119 | // -------------------------------------------------------------------- |
| 2120 | |
| 2121 | /** |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 2122 | * Get DELETE query string |
| 2123 | * |
| 2124 | * Compiles a delete query string and returns the sql |
| 2125 | * |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 2126 | * @param string the table to delete from |
Jamie Rumbelow | d6ce1e9 | 2012-04-26 13:27:35 +0100 | [diff] [blame] | 2127 | * @param bool TRUE: reset QB values; FALSE: leave QB values alone |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 2128 | * @return string |
| 2129 | */ |
| 2130 | public function get_compiled_delete($table = '', $reset = TRUE) |
| 2131 | { |
| 2132 | $this->return_delete_sql = TRUE; |
| 2133 | $sql = $this->delete($table, '', NULL, $reset); |
| 2134 | $this->return_delete_sql = FALSE; |
| 2135 | return $sql; |
| 2136 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 2137 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2138 | // -------------------------------------------------------------------- |
| 2139 | |
| 2140 | /** |
| 2141 | * Delete |
| 2142 | * |
| 2143 | * Compiles a delete string and runs the query |
| 2144 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2145 | * @param mixed the table(s) to delete from. String or array |
| 2146 | * @param mixed the where clause |
| 2147 | * @param mixed the limit clause |
Andrey Andreev | a8bb4be | 2012-03-26 15:54:23 +0300 | [diff] [blame] | 2148 | * @param bool |
Andrey Andreev | 0bcf590 | 2012-10-12 13:03:29 +0300 | [diff] [blame] | 2149 | * @return mixed |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2150 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2151 | public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2152 | { |
| 2153 | // Combine any cached components with the current statements |
| 2154 | $this->_merge_cache(); |
| 2155 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 2156 | if ($table === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2157 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2158 | if ( ! isset($this->qb_from[0])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2159 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 2160 | return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2163 | $table = $this->qb_from[0]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2164 | } |
| 2165 | elseif (is_array($table)) |
| 2166 | { |
Andrey Andreev | a1170af | 2015-07-02 11:46:56 +0300 | [diff] [blame] | 2167 | empty($where) && $reset_data = FALSE; |
| 2168 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 2169 | foreach ($table as $single_table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2170 | { |
Andrey Andreev | 13f5054 | 2012-10-12 12:31:02 +0300 | [diff] [blame] | 2171 | $this->delete($single_table, $where, $limit, $reset_data); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2172 | } |
Andrey Andreev | a1170af | 2015-07-02 11:46:56 +0300 | [diff] [blame] | 2173 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2174 | return; |
| 2175 | } |
| 2176 | else |
| 2177 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 2178 | $table = $this->protect_identifiers($table, TRUE, NULL, FALSE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 2181 | if ($where !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2182 | { |
| 2183 | $this->where($where); |
| 2184 | } |
| 2185 | |
Andrey Andreev | 650b4c0 | 2012-06-11 12:07:15 +0300 | [diff] [blame] | 2186 | if ( ! empty($limit)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2187 | { |
| 2188 | $this->limit($limit); |
| 2189 | } |
| 2190 | |
Andrey Andreev | 94611df | 2012-07-19 12:29:54 +0300 | [diff] [blame] | 2191 | if (count($this->qb_where) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2192 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 2193 | 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] | 2194 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2195 | |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 2196 | $sql = $this->_delete($table); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2197 | if ($reset_data) |
| 2198 | { |
| 2199 | $this->_reset_write(); |
| 2200 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 2201 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 2202 | return ($this->return_delete_sql === TRUE) ? $sql : $this->query($sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2203 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 2204 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2205 | // -------------------------------------------------------------------- |
| 2206 | |
| 2207 | /** |
Andrey Andreev | c01d316 | 2012-04-09 12:55:11 +0300 | [diff] [blame] | 2208 | * Delete statement |
| 2209 | * |
| 2210 | * Generates a platform-specific delete string from the supplied data |
| 2211 | * |
| 2212 | * @param string the table name |
Andrey Andreev | c01d316 | 2012-04-09 12:55:11 +0300 | [diff] [blame] | 2213 | * @return string |
| 2214 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 2215 | protected function _delete($table) |
Andrey Andreev | c01d316 | 2012-04-09 12:55:11 +0300 | [diff] [blame] | 2216 | { |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 2217 | return 'DELETE FROM '.$table.$this->_compile_wh('qb_where') |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 2218 | .($this->qb_limit ? ' LIMIT '.$this->qb_limit : ''); |
Andrey Andreev | c01d316 | 2012-04-09 12:55:11 +0300 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | // -------------------------------------------------------------------- |
| 2222 | |
| 2223 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2224 | * DB Prefix |
| 2225 | * |
| 2226 | * Prepends a database prefix if one exists in configuration |
| 2227 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2228 | * @param string the table |
| 2229 | * @return string |
| 2230 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2231 | public function dbprefix($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2232 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 2233 | if ($table === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2234 | { |
| 2235 | $this->display_error('db_table_name_required'); |
| 2236 | } |
| 2237 | |
| 2238 | return $this->dbprefix.$table; |
| 2239 | } |
| 2240 | |
| 2241 | // -------------------------------------------------------------------- |
| 2242 | |
| 2243 | /** |
Phil Sturgeon | 8a02247 | 2011-07-15 15:25:15 -0600 | [diff] [blame] | 2244 | * Set DB Prefix |
| 2245 | * |
| 2246 | * Set's the DB Prefix to something new without needing to reconnect |
| 2247 | * |
| 2248 | * @param string the prefix |
| 2249 | * @return string |
| 2250 | */ |
| 2251 | public function set_dbprefix($prefix = '') |
| 2252 | { |
| 2253 | return $this->dbprefix = $prefix; |
| 2254 | } |
| 2255 | |
| 2256 | // -------------------------------------------------------------------- |
| 2257 | |
| 2258 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2259 | * Track Aliases |
| 2260 | * |
| 2261 | * Used to track SQL statements written with aliased tables. |
| 2262 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2263 | * @param string The table to inspect |
| 2264 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2265 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2266 | protected function _track_aliases($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2267 | { |
| 2268 | if (is_array($table)) |
| 2269 | { |
| 2270 | foreach ($table as $t) |
| 2271 | { |
| 2272 | $this->_track_aliases($t); |
| 2273 | } |
| 2274 | return; |
| 2275 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2276 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 2277 | // Does the string contain a comma? If so, we need to separate |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2278 | // the string into discreet statements |
| 2279 | if (strpos($table, ',') !== FALSE) |
| 2280 | { |
| 2281 | return $this->_track_aliases(explode(',', $table)); |
| 2282 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2283 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2284 | // 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] | 2285 | if (strpos($table, ' ') !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2286 | { |
| 2287 | // if the alias is written with the AS keyword, remove it |
Andrey Andreev | 5a25718 | 2012-06-10 06:18:14 +0300 | [diff] [blame] | 2288 | $table = preg_replace('/\s+AS\s+/i', ' ', $table); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2289 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2290 | // Grab the alias |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 2291 | $table = trim(strrchr($table, ' ')); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2292 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2293 | // Store the alias, if it doesn't already exist |
Andrey Andreev | 437ffe0 | 2017-01-17 12:44:19 +0200 | [diff] [blame] | 2294 | if ( ! in_array($table, $this->qb_aliased_tables, TRUE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2295 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2296 | $this->qb_aliased_tables[] = $table; |
Andrey Andreev | 437ffe0 | 2017-01-17 12:44:19 +0200 | [diff] [blame] | 2297 | if ($this->qb_caching === TRUE && ! in_array($table, $this->qb_cache_aliased_tables, TRUE)) |
| 2298 | { |
| 2299 | $this->qb_cache_aliased_tables[] = $table; |
| 2300 | $this->qb_cache_exists[] = 'aliased_tables'; |
| 2301 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2302 | } |
| 2303 | } |
| 2304 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 2305 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2306 | // -------------------------------------------------------------------- |
| 2307 | |
| 2308 | /** |
| 2309 | * Compile the SELECT statement |
| 2310 | * |
| 2311 | * Generates a query string based on which functions were used. |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 2312 | * Should not be called directly. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2313 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 2314 | * @param bool $select_override |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2315 | * @return string |
| 2316 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2317 | protected function _compile_select($select_override = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2318 | { |
| 2319 | // Combine any cached components with the current statements |
| 2320 | $this->_merge_cache(); |
| 2321 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2322 | // Write the "select" portion of the query |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2323 | if ($select_override !== FALSE) |
| 2324 | { |
| 2325 | $sql = $select_override; |
| 2326 | } |
| 2327 | else |
| 2328 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2329 | $sql = ( ! $this->qb_distinct) ? 'SELECT ' : 'SELECT DISTINCT '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2330 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2331 | if (count($this->qb_select) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2332 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2333 | $sql .= '*'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2334 | } |
| 2335 | else |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2336 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2337 | // Cycle through the "select" portion of the query and prep each column name. |
Andrey Andreev | 1924eb3 | 2015-04-08 17:19:24 +0300 | [diff] [blame] | 2338 | // The reason we protect identifiers here rather than in the select() function |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2339 | // is because until the user calls the from() function we don't know if there are aliases |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2340 | foreach ($this->qb_select as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2341 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2342 | $no_escape = isset($this->qb_no_escape[$key]) ? $this->qb_no_escape[$key] : NULL; |
Jamie Rumbelow | 0c09299 | 2012-03-06 22:05:16 +0000 | [diff] [blame] | 2343 | $this->qb_select[$key] = $this->protect_identifiers($val, FALSE, $no_escape); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2344 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2345 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2346 | $sql .= implode(', ', $this->qb_select); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2347 | } |
| 2348 | } |
| 2349 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2350 | // Write the "FROM" portion of the query |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2351 | if (count($this->qb_from) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2352 | { |
Andrey Andreev | e78f815 | 2012-10-09 11:38:38 +0300 | [diff] [blame] | 2353 | $sql .= "\nFROM ".$this->_from_tables(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2354 | } |
| 2355 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2356 | // Write the "JOIN" portion of the query |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2357 | if (count($this->qb_join) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2358 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2359 | $sql .= "\n".implode("\n", $this->qb_join); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 2362 | $sql .= $this->_compile_wh('qb_where') |
| 2363 | .$this->_compile_group_by() |
| 2364 | .$this->_compile_wh('qb_having') |
| 2365 | .$this->_compile_order_by(); // ORDER BY |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 2366 | |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 2367 | // LIMIT |
Andrey Andreev | acc6481 | 2016-07-29 11:42:28 +0300 | [diff] [blame] | 2368 | if ($this->qb_limit OR $this->qb_offset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2369 | { |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 2370 | return $this->_limit($sql."\n"); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | return $sql; |
| 2374 | } |
| 2375 | |
| 2376 | // -------------------------------------------------------------------- |
| 2377 | |
| 2378 | /** |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 2379 | * Compile WHERE, HAVING statements |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 2380 | * |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 2381 | * Escapes identifiers in WHERE and HAVING statements at execution time. |
| 2382 | * |
Andrey Andreev | 99c17e5 | 2016-02-29 16:53:45 +0200 | [diff] [blame] | 2383 | * Required so that aliases are tracked properly, regardless of whether |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 2384 | * where(), or_where(), having(), or_having are called prior to from(), |
| 2385 | * join() and dbprefix is added only if needed. |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 2386 | * |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 2387 | * @param string $qb_key 'qb_where' or 'qb_having' |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 2388 | * @return string SQL statement |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 2389 | */ |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 2390 | protected function _compile_wh($qb_key) |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 2391 | { |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 2392 | if (count($this->$qb_key) > 0) |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 2393 | { |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 2394 | for ($i = 0, $c = count($this->$qb_key); $i < $c; $i++) |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 2395 | { |
Andrey Andreev | 5e3d48c | 2013-10-29 14:36:18 +0200 | [diff] [blame] | 2396 | // Is this condition already compiled? |
| 2397 | if (is_string($this->{$qb_key}[$i])) |
| 2398 | { |
| 2399 | continue; |
| 2400 | } |
| 2401 | elseif ($this->{$qb_key}[$i]['escape'] === FALSE) |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 2402 | { |
Andrey Andreev | 7dd6f14 | 2018-01-30 15:08:21 +0200 | [diff] [blame] | 2403 | $this->{$qb_key}[$i] = $this->{$qb_key}[$i]['condition'].(isset($this->{$qb_key}[$i]['value']) ? ' '.$this->{$qb_key}[$i]['value'] : ''); |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 2404 | continue; |
| 2405 | } |
| 2406 | |
Andrey Andreev | f2ec8b8 | 2012-10-12 14:01:13 +0300 | [diff] [blame] | 2407 | // Split multiple conditions |
| 2408 | $conditions = preg_split( |
Andrey Andreev | 554b452 | 2015-09-01 13:51:26 +0300 | [diff] [blame] | 2409 | '/((?:^|\s+)AND\s+|(?:^|\s+)OR\s+)/i', |
Andrey Andreev | f2ec8b8 | 2012-10-12 14:01:13 +0300 | [diff] [blame] | 2410 | $this->{$qb_key}[$i]['condition'], |
| 2411 | -1, |
| 2412 | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY |
| 2413 | ); |
| 2414 | |
| 2415 | for ($ci = 0, $cc = count($conditions); $ci < $cc; $ci++) |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 2416 | { |
Andrey Andreev | f2ec8b8 | 2012-10-12 14:01:13 +0300 | [diff] [blame] | 2417 | if (($op = $this->_get_operator($conditions[$ci])) === FALSE |
Andrey Andreev | e474258 | 2012-10-25 13:25:13 +0300 | [diff] [blame] | 2418 | OR ! preg_match('/^(\(?)(.*)('.preg_quote($op, '/').')\s*(.*(?<!\)))?(\)?)$/i', $conditions[$ci], $matches)) |
Andrey Andreev | f2ec8b8 | 2012-10-12 14:01:13 +0300 | [diff] [blame] | 2419 | { |
| 2420 | continue; |
| 2421 | } |
| 2422 | |
| 2423 | // $matches = array( |
| 2424 | // 0 => '(test <= foo)', /* the whole thing */ |
| 2425 | // 1 => '(', /* optional */ |
| 2426 | // 2 => 'test', /* the field name */ |
| 2427 | // 3 => ' <= ', /* $op */ |
| 2428 | // 4 => 'foo', /* optional, if $op is e.g. 'IS NULL' */ |
| 2429 | // 5 => ')' /* optional */ |
| 2430 | // ); |
Andrey Andreev | 082aa40 | 2012-10-22 19:41:55 +0300 | [diff] [blame] | 2431 | |
| 2432 | if ( ! empty($matches[4])) |
| 2433 | { |
| 2434 | $this->_is_literal($matches[4]) OR $matches[4] = $this->protect_identifiers(trim($matches[4])); |
| 2435 | $matches[4] = ' '.$matches[4]; |
| 2436 | } |
| 2437 | |
Andrey Andreev | f2ec8b8 | 2012-10-12 14:01:13 +0300 | [diff] [blame] | 2438 | $conditions[$ci] = $matches[1].$this->protect_identifiers(trim($matches[2])) |
| 2439 | .' '.trim($matches[3]).$matches[4].$matches[5]; |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 2440 | } |
| 2441 | |
Andrey Andreev | 7dd6f14 | 2018-01-30 15:08:21 +0200 | [diff] [blame] | 2442 | $this->{$qb_key}[$i] = implode('', $conditions).(isset($this->{$qb_key}[$i]['value']) ? ' '.$this->{$qb_key}[$i]['value'] : ''); |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 2443 | } |
| 2444 | |
Andrey Andreev | 9d3aa1b | 2012-10-12 12:14:09 +0300 | [diff] [blame] | 2445 | return ($qb_key === 'qb_having' ? "\nHAVING " : "\nWHERE ") |
| 2446 | .implode("\n", $this->$qb_key); |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 2447 | } |
| 2448 | |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 2449 | return ''; |
Andrey Andreev | 6e70475 | 2012-07-18 00:46:33 +0300 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | // -------------------------------------------------------------------- |
| 2453 | |
| 2454 | /** |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 2455 | * Compile GROUP BY |
| 2456 | * |
| 2457 | * Escapes identifiers in GROUP BY statements at execution time. |
| 2458 | * |
Andrey Andreev | 71d8f72 | 2017-01-17 12:01:00 +0200 | [diff] [blame] | 2459 | * Required so that aliases are tracked properly, regardless of whether |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 2460 | * group_by() is called prior to from(), join() and dbprefix is added |
| 2461 | * only if needed. |
| 2462 | * |
| 2463 | * @return string SQL statement |
| 2464 | */ |
| 2465 | protected function _compile_group_by() |
| 2466 | { |
| 2467 | if (count($this->qb_groupby) > 0) |
| 2468 | { |
Andrey Andreev | 96feb58 | 2012-07-19 13:12:34 +0300 | [diff] [blame] | 2469 | for ($i = 0, $c = count($this->qb_groupby); $i < $c; $i++) |
| 2470 | { |
Andrey Andreev | 5e3d48c | 2013-10-29 14:36:18 +0200 | [diff] [blame] | 2471 | // Is it already compiled? |
Andrey Andreev | 18eba24 | 2014-01-23 22:52:31 +0200 | [diff] [blame] | 2472 | if (is_string($this->qb_groupby[$i])) |
Andrey Andreev | 5e3d48c | 2013-10-29 14:36:18 +0200 | [diff] [blame] | 2473 | { |
| 2474 | continue; |
| 2475 | } |
| 2476 | |
Andrey Andreev | 082aa40 | 2012-10-22 19:41:55 +0300 | [diff] [blame] | 2477 | $this->qb_groupby[$i] = ($this->qb_groupby[$i]['escape'] === FALSE OR $this->_is_literal($this->qb_groupby[$i]['field'])) |
Andrey Andreev | 96feb58 | 2012-07-19 13:12:34 +0300 | [diff] [blame] | 2478 | ? $this->qb_groupby[$i]['field'] |
Andrey Andreev | 13f5054 | 2012-10-12 12:31:02 +0300 | [diff] [blame] | 2479 | : $this->protect_identifiers($this->qb_groupby[$i]['field']); |
Andrey Andreev | 96feb58 | 2012-07-19 13:12:34 +0300 | [diff] [blame] | 2480 | } |
| 2481 | |
Andrey Andreev | 0bcf590 | 2012-10-12 13:03:29 +0300 | [diff] [blame] | 2482 | return "\nGROUP BY ".implode(', ', $this->qb_groupby); |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 2483 | } |
| 2484 | |
| 2485 | return ''; |
| 2486 | } |
| 2487 | |
| 2488 | // -------------------------------------------------------------------- |
| 2489 | |
| 2490 | /** |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 2491 | * Compile ORDER BY |
| 2492 | * |
| 2493 | * Escapes identifiers in ORDER BY statements at execution time. |
| 2494 | * |
Andrey Andreev | 71d8f72 | 2017-01-17 12:01:00 +0200 | [diff] [blame] | 2495 | * Required so that aliases are tracked properly, regardless of whether |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 2496 | * order_by() is called prior to from(), join() and dbprefix is added |
| 2497 | * only if needed. |
| 2498 | * |
| 2499 | * @return string SQL statement |
| 2500 | */ |
| 2501 | protected function _compile_order_by() |
| 2502 | { |
Andrey Andreev | 33cc3e1 | 2017-01-16 16:01:58 +0200 | [diff] [blame] | 2503 | if (empty($this->qb_orderby)) |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 2504 | { |
Andrey Andreev | 33cc3e1 | 2017-01-16 16:01:58 +0200 | [diff] [blame] | 2505 | return ''; |
| 2506 | } |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 2507 | |
Andrey Andreev | 33cc3e1 | 2017-01-16 16:01:58 +0200 | [diff] [blame] | 2508 | for ($i = 0, $c = count($this->qb_orderby); $i < $c; $i++) |
| 2509 | { |
| 2510 | if (is_string($this->qb_orderby[$i])) |
| 2511 | { |
| 2512 | continue; |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 2513 | } |
| 2514 | |
Andrey Andreev | 33cc3e1 | 2017-01-16 16:01:58 +0200 | [diff] [blame] | 2515 | if ($this->qb_orderby[$i]['escape'] !== FALSE && ! $this->_is_literal($this->qb_orderby[$i]['field'])) |
| 2516 | { |
| 2517 | $this->qb_orderby[$i]['field'] = $this->protect_identifiers($this->qb_orderby[$i]['field']); |
| 2518 | } |
| 2519 | |
| 2520 | $this->qb_orderby[$i] = $this->qb_orderby[$i]['field'].$this->qb_orderby[$i]['direction']; |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 2521 | } |
| 2522 | |
Andrey Andreev | 33cc3e1 | 2017-01-16 16:01:58 +0200 | [diff] [blame] | 2523 | return "\nORDER BY ".implode(', ', $this->qb_orderby); |
Andrey Andreev | 2d48623 | 2012-07-19 14:46:51 +0300 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | // -------------------------------------------------------------------- |
| 2527 | |
| 2528 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2529 | * Object to Array |
| 2530 | * |
| 2531 | * Takes an object as input and converts the class variables to array key/vals |
| 2532 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2533 | * @param object |
| 2534 | * @return array |
| 2535 | */ |
Andrey Andreev | 7b5eb73 | 2012-05-24 20:52:41 +0300 | [diff] [blame] | 2536 | protected function _object_to_array($object) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2537 | { |
| 2538 | if ( ! is_object($object)) |
| 2539 | { |
| 2540 | return $object; |
| 2541 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2542 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2543 | $array = array(); |
| 2544 | foreach (get_object_vars($object) as $key => $val) |
| 2545 | { |
| 2546 | // There are some built in keys we need to ignore for this conversion |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 2547 | if ( ! is_object($val) && ! is_array($val) && $key !== '_parent_name') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2548 | { |
| 2549 | $array[$key] = $val; |
| 2550 | } |
| 2551 | } |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2552 | |
| 2553 | return $array; |
| 2554 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2555 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2556 | // -------------------------------------------------------------------- |
| 2557 | |
| 2558 | /** |
| 2559 | * Object to Array |
| 2560 | * |
| 2561 | * Takes an object as input and converts the class variables to array key/vals |
| 2562 | * |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2563 | * @param object |
| 2564 | * @return array |
| 2565 | */ |
Andrey Andreev | 7b5eb73 | 2012-05-24 20:52:41 +0300 | [diff] [blame] | 2566 | protected function _object_to_array_batch($object) |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2567 | { |
| 2568 | if ( ! is_object($object)) |
| 2569 | { |
| 2570 | return $object; |
| 2571 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2572 | |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2573 | $array = array(); |
| 2574 | $out = get_object_vars($object); |
| 2575 | $fields = array_keys($out); |
| 2576 | |
| 2577 | foreach ($fields as $val) |
| 2578 | { |
| 2579 | // 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] | 2580 | if ($val !== '_parent_name') |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2581 | { |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2582 | $i = 0; |
| 2583 | foreach ($out[$val] as $data) |
| 2584 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 2585 | $array[$i++][$val] = $data; |
Derek Jones | d10e896 | 2010-03-02 17:10:36 -0600 | [diff] [blame] | 2586 | } |
| 2587 | } |
| 2588 | } |
| 2589 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2590 | return $array; |
| 2591 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2592 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2593 | // -------------------------------------------------------------------- |
| 2594 | |
| 2595 | /** |
| 2596 | * Start Cache |
| 2597 | * |
Jamie Rumbelow | 17c1bed | 2012-03-06 21:30:38 +0000 | [diff] [blame] | 2598 | * Starts QB caching |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2599 | * |
Andrey Andreev | 4a587f5 | 2014-12-11 16:27:15 +0200 | [diff] [blame] | 2600 | * @return CI_DB_query_builder |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2601 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2602 | public function start_cache() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2603 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2604 | $this->qb_caching = TRUE; |
Andrey Andreev | 4a587f5 | 2014-12-11 16:27:15 +0200 | [diff] [blame] | 2605 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | // -------------------------------------------------------------------- |
| 2609 | |
| 2610 | /** |
| 2611 | * Stop Cache |
| 2612 | * |
Jamie Rumbelow | 17c1bed | 2012-03-06 21:30:38 +0000 | [diff] [blame] | 2613 | * Stops QB caching |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2614 | * |
Andrey Andreev | 4a587f5 | 2014-12-11 16:27:15 +0200 | [diff] [blame] | 2615 | * @return CI_DB_query_builder |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2616 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2617 | public function stop_cache() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2618 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2619 | $this->qb_caching = FALSE; |
Andrey Andreev | 4a587f5 | 2014-12-11 16:27:15 +0200 | [diff] [blame] | 2620 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | // -------------------------------------------------------------------- |
| 2624 | |
| 2625 | /** |
| 2626 | * Flush Cache |
| 2627 | * |
Jamie Rumbelow | 17c1bed | 2012-03-06 21:30:38 +0000 | [diff] [blame] | 2628 | * Empties the QB cache |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2629 | * |
Andrey Andreev | 4a587f5 | 2014-12-11 16:27:15 +0200 | [diff] [blame] | 2630 | * @return CI_DB_query_builder |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2631 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2632 | public function flush_cache() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2633 | { |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2634 | $this->_reset_run(array( |
Jamie Rumbelow | 17c1bed | 2012-03-06 21:30:38 +0000 | [diff] [blame] | 2635 | 'qb_cache_select' => array(), |
| 2636 | 'qb_cache_from' => array(), |
| 2637 | 'qb_cache_join' => array(), |
| 2638 | 'qb_cache_where' => array(), |
Jamie Rumbelow | 17c1bed | 2012-03-06 21:30:38 +0000 | [diff] [blame] | 2639 | 'qb_cache_groupby' => array(), |
| 2640 | 'qb_cache_having' => array(), |
| 2641 | 'qb_cache_orderby' => array(), |
| 2642 | 'qb_cache_set' => array(), |
| 2643 | 'qb_cache_exists' => array(), |
Andrey Andreev | 437ffe0 | 2017-01-17 12:44:19 +0200 | [diff] [blame] | 2644 | 'qb_cache_no_escape' => array(), |
| 2645 | 'qb_cache_aliased_tables' => array() |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2646 | )); |
Andrey Andreev | 4a587f5 | 2014-12-11 16:27:15 +0200 | [diff] [blame] | 2647 | |
| 2648 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
| 2651 | // -------------------------------------------------------------------- |
| 2652 | |
| 2653 | /** |
| 2654 | * Merge Cache |
| 2655 | * |
Jamie Rumbelow | 17c1bed | 2012-03-06 21:30:38 +0000 | [diff] [blame] | 2656 | * When called, this function merges any cached QB arrays with |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2657 | * locally called ones. |
| 2658 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2659 | * @return void |
| 2660 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2661 | protected function _merge_cache() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2662 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2663 | if (count($this->qb_cache_exists) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2664 | { |
| 2665 | return; |
| 2666 | } |
Andrey Andreev | 1720a6a | 2014-01-06 13:50:05 +0200 | [diff] [blame] | 2667 | elseif (in_array('select', $this->qb_cache_exists, TRUE)) |
| 2668 | { |
| 2669 | $qb_no_escape = $this->qb_cache_no_escape; |
| 2670 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2671 | |
GDmac | 17a0528 | 2013-11-11 13:18:09 +0100 | [diff] [blame] | 2672 | foreach (array_unique($this->qb_cache_exists) as $val) // select, from, etc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2673 | { |
Jamie Rumbelow | ae123e0 | 2012-02-21 16:39:56 +0000 | [diff] [blame] | 2674 | $qb_variable = 'qb_'.$val; |
| 2675 | $qb_cache_var = 'qb_cache_'.$val; |
Andrey Andreev | 35e3b0b | 2013-11-12 16:07:08 +0200 | [diff] [blame] | 2676 | $qb_new = $this->$qb_cache_var; |
GDmac | e1b8683 | 2013-11-08 16:52:54 +0100 | [diff] [blame] | 2677 | |
Andrey Andreev | 1720a6a | 2014-01-06 13:50:05 +0200 | [diff] [blame] | 2678 | for ($i = 0, $c = count($this->$qb_variable); $i < $c; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2679 | { |
Andrey Andreev | 1720a6a | 2014-01-06 13:50:05 +0200 | [diff] [blame] | 2680 | if ( ! in_array($this->{$qb_variable}[$i], $qb_new, TRUE)) |
| 2681 | { |
| 2682 | $qb_new[] = $this->{$qb_variable}[$i]; |
| 2683 | if ($val === 'select') |
| 2684 | { |
| 2685 | $qb_no_escape[] = $this->qb_no_escape[$i]; |
| 2686 | } |
| 2687 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2688 | } |
Andrey Andreev | 35e3b0b | 2013-11-12 16:07:08 +0200 | [diff] [blame] | 2689 | |
GDmac | e1b8683 | 2013-11-08 16:52:54 +0100 | [diff] [blame] | 2690 | $this->$qb_variable = $qb_new; |
Andrey Andreev | 1720a6a | 2014-01-06 13:50:05 +0200 | [diff] [blame] | 2691 | if ($val === 'select') |
| 2692 | { |
| 2693 | $this->qb_no_escape = $qb_no_escape; |
| 2694 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2695 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2696 | } |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 2697 | |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 2698 | // -------------------------------------------------------------------- |
| 2699 | |
| 2700 | /** |
Andrey Andreev | 082aa40 | 2012-10-22 19:41:55 +0300 | [diff] [blame] | 2701 | * Is literal |
| 2702 | * |
| 2703 | * Determines if a string represents a literal value or a field name |
| 2704 | * |
Andrey Andreev | 02e4cd7 | 2012-11-13 11:50:47 +0200 | [diff] [blame] | 2705 | * @param string $str |
Andrey Andreev | 082aa40 | 2012-10-22 19:41:55 +0300 | [diff] [blame] | 2706 | * @return bool |
| 2707 | */ |
| 2708 | protected function _is_literal($str) |
| 2709 | { |
| 2710 | $str = trim($str); |
| 2711 | |
Andrey Andreev | 3a5efc2 | 2012-11-20 21:18:08 +0200 | [diff] [blame] | 2712 | if (empty($str) OR ctype_digit($str) OR (string) (float) $str === $str OR in_array(strtoupper($str), array('TRUE', 'FALSE'), TRUE)) |
Andrey Andreev | 082aa40 | 2012-10-22 19:41:55 +0300 | [diff] [blame] | 2713 | { |
| 2714 | return TRUE; |
| 2715 | } |
| 2716 | |
| 2717 | static $_str; |
| 2718 | |
| 2719 | if (empty($_str)) |
| 2720 | { |
| 2721 | $_str = ($this->_escape_char !== '"') |
| 2722 | ? array('"', "'") : array("'"); |
| 2723 | } |
| 2724 | |
Andrey Andreev | 3a5efc2 | 2012-11-20 21:18:08 +0200 | [diff] [blame] | 2725 | return in_array($str[0], $_str, TRUE); |
Andrey Andreev | 082aa40 | 2012-10-22 19:41:55 +0300 | [diff] [blame] | 2726 | } |
| 2727 | |
| 2728 | // -------------------------------------------------------------------- |
| 2729 | |
| 2730 | /** |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2731 | * Reset Query Builder values. |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 2732 | * |
Jamie Rumbelow | 17c1bed | 2012-03-06 21:30:38 +0000 | [diff] [blame] | 2733 | * Publicly-visible method to reset the QB values. |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 2734 | * |
Andrey Andreev | 435e0c2 | 2014-12-11 16:30:13 +0200 | [diff] [blame] | 2735 | * @return CI_DB_query_builder |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 2736 | */ |
| 2737 | public function reset_query() |
| 2738 | { |
| 2739 | $this->_reset_select(); |
| 2740 | $this->_reset_write(); |
Andrey Andreev | 435e0c2 | 2014-12-11 16:30:13 +0200 | [diff] [blame] | 2741 | return $this; |
Kyle Farris | 0c147b3 | 2011-08-26 02:29:31 -0400 | [diff] [blame] | 2742 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2743 | |
| 2744 | // -------------------------------------------------------------------- |
| 2745 | |
| 2746 | /** |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2747 | * Resets the query builder values. Called by the get() function |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2748 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2749 | * @param array An array of fields to reset |
| 2750 | * @return void |
| 2751 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2752 | protected function _reset_run($qb_reset_items) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2753 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2754 | foreach ($qb_reset_items as $item => $default_value) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2755 | { |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 2756 | $this->$item = $default_value; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2757 | } |
| 2758 | } |
| 2759 | |
| 2760 | // -------------------------------------------------------------------- |
| 2761 | |
| 2762 | /** |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2763 | * Resets the query builder values. Called by the get() function |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2764 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2765 | * @return void |
| 2766 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2767 | protected function _reset_select() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2768 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 2769 | $this->_reset_run(array( |
Andrey Andreev | 4a587f5 | 2014-12-11 16:27:15 +0200 | [diff] [blame] | 2770 | 'qb_select' => array(), |
| 2771 | 'qb_from' => array(), |
| 2772 | 'qb_join' => array(), |
| 2773 | 'qb_where' => array(), |
| 2774 | 'qb_groupby' => array(), |
| 2775 | 'qb_having' => array(), |
| 2776 | 'qb_orderby' => array(), |
| 2777 | 'qb_aliased_tables' => array(), |
| 2778 | 'qb_no_escape' => array(), |
| 2779 | 'qb_distinct' => FALSE, |
| 2780 | 'qb_limit' => FALSE, |
| 2781 | 'qb_offset' => FALSE |
| 2782 | )); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2783 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2784 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2785 | // -------------------------------------------------------------------- |
| 2786 | |
| 2787 | /** |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2788 | * Resets the query builder "write" values. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2789 | * |
Robin Sowell | 43753fd | 2010-09-16 12:52:07 -0400 | [diff] [blame] | 2790 | * Called by the insert() update() insert_batch() update_batch() and delete() functions |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2791 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2792 | * @return void |
| 2793 | */ |
Phil Sturgeon | 9789f32 | 2011-07-15 15:14:05 -0600 | [diff] [blame] | 2794 | protected function _reset_write() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 2795 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 2796 | $this->_reset_run(array( |
Jamie Rumbelow | d6ce1e9 | 2012-04-26 13:27:35 +0100 | [diff] [blame] | 2797 | 'qb_set' => array(), |
Andrey Andreev | 8338bbb | 2016-12-12 14:17:52 +0200 | [diff] [blame] | 2798 | 'qb_set_ub' => array(), |
Jamie Rumbelow | d6ce1e9 | 2012-04-26 13:27:35 +0100 | [diff] [blame] | 2799 | 'qb_from' => array(), |
Andrey Andreev | 3e01437 | 2013-02-21 15:59:34 +0200 | [diff] [blame] | 2800 | 'qb_join' => array(), |
Jamie Rumbelow | d6ce1e9 | 2012-04-26 13:27:35 +0100 | [diff] [blame] | 2801 | 'qb_where' => array(), |
Jamie Rumbelow | d6ce1e9 | 2012-04-26 13:27:35 +0100 | [diff] [blame] | 2802 | 'qb_orderby' => array(), |
| 2803 | 'qb_keys' => array(), |
Andrey Andreev | 650b4c0 | 2012-06-11 12:07:15 +0300 | [diff] [blame] | 2804 | 'qb_limit' => FALSE |
Andrey Andreev | 4a587f5 | 2014-12-11 16:27:15 +0200 | [diff] [blame] | 2805 | )); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2806 | } |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 2807 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2808 | } |