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