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