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