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