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