Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 2 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 3 | * CodeIgniter
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Rick Ellis | 37b3ecf | 2008-09-12 23:34:18 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008, EllisLab, Inc.
|
Derek Allard | cdd2ab2 | 2008-01-23 00:05:38 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 12 | * @since Version 1.0
|
| 13 | * @filesource
|
| 14 | */
|
| 15 |
|
| 16 | // ------------------------------------------------------------------------
|
| 17 |
|
| 18 | /**
|
| 19 | * Active Record Class
|
| 20 | *
|
| 21 | * This is the platform-independent base Active Record implementation class.
|
| 22 | *
|
| 23 | * @package CodeIgniter
|
| 24 | * @subpackage Drivers
|
| 25 | * @category Database
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 26 | * @author ExpressionEngine Dev Team
|
Derek Allard | cdd2ab2 | 2008-01-23 00:05:38 +0000 | [diff] [blame] | 27 | * @link http://codeigniter.com/user_guide/database/
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 28 | */
|
| 29 | class CI_DB_active_record extends CI_DB_driver {
|
| 30 |
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 31 | var $ar_select = array();
|
| 32 | var $ar_distinct = FALSE;
|
| 33 | var $ar_from = array();
|
| 34 | var $ar_join = array();
|
| 35 | var $ar_where = array();
|
| 36 | var $ar_like = array();
|
| 37 | var $ar_groupby = array();
|
| 38 | var $ar_having = array();
|
| 39 | var $ar_limit = FALSE;
|
| 40 | var $ar_offset = FALSE;
|
| 41 | var $ar_order = FALSE;
|
| 42 | var $ar_orderby = array();
|
| 43 | var $ar_set = array();
|
| 44 | var $ar_wherein = array();
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 45 | var $ar_aliased_tables = array();
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 46 | var $ar_store_array = array();
|
| 47 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 48 | // Active Record Caching variables
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 49 | var $ar_caching = FALSE;
|
| 50 | var $ar_cache_select = array();
|
| 51 | var $ar_cache_from = array();
|
| 52 | var $ar_cache_join = array();
|
| 53 | var $ar_cache_where = array();
|
| 54 | var $ar_cache_like = array();
|
| 55 | var $ar_cache_groupby = array();
|
| 56 | var $ar_cache_having = array();
|
| 57 | var $ar_cache_limit = FALSE;
|
| 58 | var $ar_cache_offset = FALSE;
|
| 59 | var $ar_cache_order = FALSE;
|
| 60 | var $ar_cache_orderby = array();
|
| 61 | var $ar_cache_set = array();
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 62 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 63 |
|
Derek Allard | 3b11868 | 2008-01-22 23:44:32 +0000 | [diff] [blame] | 64 | // --------------------------------------------------------------------
|
| 65 |
|
| 66 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 67 | * Select
|
| 68 | *
|
| 69 | * Generates the SELECT portion of the query
|
| 70 | *
|
| 71 | * @access public
|
| 72 | * @param string
|
| 73 | * @return object
|
| 74 | */
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 75 | function select($select = '*', $escape = NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 76 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 77 | // Set the global value if this was sepecified
|
| 78 | if (is_bool($escape))
|
| 79 | {
|
| 80 | $this->_protect_identifiers = $escape;
|
| 81 | }
|
| 82 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 83 | if (is_string($select))
|
| 84 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 85 | $select = explode(',', $select);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 86 | }
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 87 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 88 | foreach ($select as $val)
|
| 89 | {
|
| 90 | $val = trim($val);
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 91 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 92 | if ($val != '')
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 93 | {
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 94 | $this->ar_select[] = $val;
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 95 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 96 | if ($this->ar_caching === TRUE)
|
| 97 | {
|
| 98 | $this->ar_cache_select[] = $val;
|
| 99 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 100 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 101 | }
|
| 102 | return $this;
|
| 103 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 104 |
|
| 105 | // --------------------------------------------------------------------
|
| 106 |
|
| 107 | /**
|
| 108 | * Select Max
|
| 109 | *
|
| 110 | * Generates a SELECT MAX(field) portion of a query
|
| 111 | *
|
| 112 | * @access public
|
| 113 | * @param string the field
|
| 114 | * @param string an alias
|
| 115 | * @return object
|
| 116 | */
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 117 | function select_max($select = '', $alias = '')
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 118 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 119 | return $this->_max_min_avg_sum($select, $alias, 'MAX');
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 120 | }
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 121 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 122 | // --------------------------------------------------------------------
|
| 123 |
|
| 124 | /**
|
| 125 | * Select Min
|
| 126 | *
|
| 127 | * Generates a SELECT MIN(field) portion of a query
|
| 128 | *
|
| 129 | * @access public
|
| 130 | * @param string the field
|
| 131 | * @param string an alias
|
| 132 | * @return object
|
| 133 | */
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 134 | function select_min($select = '', $alias = '')
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 135 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 136 | return $this->_max_min_avg_sum($select, $alias, 'MIN');
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 137 | }
|
| 138 |
|
| 139 | // --------------------------------------------------------------------
|
| 140 |
|
| 141 | /**
|
| 142 | * Select Average
|
| 143 | *
|
| 144 | * Generates a SELECT AVG(field) portion of a query
|
| 145 | *
|
| 146 | * @access public
|
| 147 | * @param string the field
|
| 148 | * @param string an alias
|
| 149 | * @return object
|
| 150 | */
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 151 | function select_avg($select = '', $alias = '')
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 152 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 153 | return $this->_max_min_avg_sum($select, $alias, 'AVG');
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 154 | }
|
| 155 |
|
| 156 | // --------------------------------------------------------------------
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 157 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 158 | /**
|
| 159 | * Select Sum
|
| 160 | *
|
| 161 | * Generates a SELECT SUM(field) portion of a query
|
| 162 | *
|
| 163 | * @access public
|
| 164 | * @param string the field
|
| 165 | * @param string an alias
|
| 166 | * @return object
|
| 167 | */
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 168 | function select_sum($select = '', $alias = '')
|
| 169 | {
|
| 170 | return $this->_max_min_avg_sum($select, $alias, 'SUM');
|
| 171 | }
|
| 172 |
|
| 173 | // --------------------------------------------------------------------
|
| 174 |
|
| 175 | /**
|
| 176 | * Processing Function for the four functions above:
|
| 177 | *
|
| 178 | * select_max()
|
| 179 | * select_min()
|
| 180 | * select_avg()
|
| 181 | * select_sum()
|
| 182 | *
|
| 183 | * @access public
|
| 184 | * @param string the field
|
| 185 | * @param string an alias
|
| 186 | * @return object
|
| 187 | */
|
| 188 | function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 189 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 190 | if ( ! is_string($select) OR $select == '')
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 191 | {
|
| 192 | $this->display_error('db_invalid_query');
|
| 193 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 194 |
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 195 | $type = strtoupper($type);
|
| 196 |
|
| 197 | if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
|
| 198 | {
|
| 199 | show_error('Invalid function type: '.$type);
|
| 200 | }
|
| 201 |
|
| 202 | if ($alias == '')
|
| 203 | {
|
| 204 | $alias = $this->_create_alias_from_table(trim($select));
|
| 205 | }
|
| 206 |
|
| 207 | $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$alias;
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 208 |
|
| 209 | $this->ar_select[] = $sql;
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 210 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 211 | if ($this->ar_caching === TRUE)
|
| 212 | {
|
| 213 | $this->ar_cache_select[] = $sql;
|
| 214 | }
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 215 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 216 | return $this;
|
| 217 | }
|
| 218 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 219 | // --------------------------------------------------------------------
|
| 220 |
|
| 221 | /**
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 222 | * Determines the alias name based on the table
|
| 223 | *
|
| 224 | * @access private
|
| 225 | * @param string
|
| 226 | * @return string
|
| 227 | */
|
| 228 | function _create_alias_from_table($item)
|
| 229 | {
|
| 230 | if (strpos($item, '.') !== FALSE)
|
| 231 | {
|
| 232 | return end(explode('.', $item));
|
| 233 | }
|
| 234 |
|
| 235 | return $item;
|
| 236 | }
|
| 237 |
|
| 238 | // --------------------------------------------------------------------
|
| 239 |
|
| 240 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 241 | * DISTINCT
|
| 242 | *
|
| 243 | * Sets a flag which tells the query string compiler to add DISTINCT
|
| 244 | *
|
| 245 | * @access public
|
| 246 | * @param bool
|
| 247 | * @return object
|
| 248 | */
|
| 249 | function distinct($val = TRUE)
|
| 250 | {
|
| 251 | $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
|
| 252 | return $this;
|
| 253 | }
|
| 254 |
|
| 255 | // --------------------------------------------------------------------
|
| 256 |
|
| 257 | /**
|
| 258 | * From
|
| 259 | *
|
| 260 | * Generates the FROM portion of the query
|
| 261 | *
|
| 262 | * @access public
|
| 263 | * @param mixed can be a string or array
|
| 264 | * @return object
|
| 265 | */
|
| 266 | function from($from)
|
| 267 | {
|
| 268 | foreach ((array)$from as $val)
|
| 269 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 270 | // Extract any aliases that might exist. We use this information
|
| 271 | // in the _protect_identifiers to know whether to add a table prefix
|
| 272 | $this->_track_aliases($val);
|
| 273 |
|
| 274 | $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
|
| 275 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 276 | if ($this->ar_caching === TRUE)
|
| 277 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 278 | $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 279 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 280 | }
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 281 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 282 | return $this;
|
| 283 | }
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 284 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 285 | // --------------------------------------------------------------------
|
| 286 |
|
| 287 | /**
|
| 288 | * Join
|
| 289 | *
|
| 290 | * Generates the JOIN portion of the query
|
| 291 | *
|
| 292 | * @access public
|
| 293 | * @param string
|
| 294 | * @param string the join condition
|
| 295 | * @param string the type of join
|
| 296 | * @return object
|
| 297 | */
|
| 298 | function join($table, $cond, $type = '')
|
| 299 | {
|
| 300 | if ($type != '')
|
| 301 | {
|
| 302 | $type = strtoupper(trim($type));
|
| 303 |
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 304 | if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 305 | {
|
| 306 | $type = '';
|
| 307 | }
|
| 308 | else
|
| 309 | {
|
| 310 | $type .= ' ';
|
| 311 | }
|
| 312 | }
|
| 313 |
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 314 | // Extract any aliases that might exist. We use this information
|
| 315 | // in the _protect_identifiers to know whether to add a table prefix
|
| 316 | $this->_track_aliases($table);
|
| 317 |
|
| 318 | // Strip apart the condition and protect the identifiers
|
| 319 | if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 320 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 321 | $match[1] = $this->_protect_identifiers($match[1]);
|
| 322 | $match[3] = $this->_protect_identifiers($match[3]);
|
| 323 |
|
| 324 | $cond = $match[1].$match[2].$match[3];
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 325 | }
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 326 |
|
| 327 | // Assemble the JOIN statement
|
| 328 | $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 329 |
|
| 330 | $this->ar_join[] = $join;
|
| 331 | if ($this->ar_caching === TRUE)
|
| 332 | {
|
| 333 | $this->ar_cache_join[] = $join;
|
| 334 | }
|
| 335 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 336 | return $this;
|
| 337 | }
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 338 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 339 | // --------------------------------------------------------------------
|
| 340 |
|
| 341 | /**
|
| 342 | * Where
|
| 343 | *
|
| 344 | * Generates the WHERE portion of the query. Separates
|
| 345 | * multiple calls with AND
|
| 346 | *
|
| 347 | * @access public
|
| 348 | * @param mixed
|
| 349 | * @param mixed
|
| 350 | * @return object
|
| 351 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 352 | function where($key, $value = NULL, $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 353 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 354 | return $this->_where($key, $value, 'AND ', $escape);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 355 | }
|
| 356 |
|
| 357 | // --------------------------------------------------------------------
|
| 358 |
|
| 359 | /**
|
| 360 | * OR Where
|
| 361 | *
|
| 362 | * Generates the WHERE portion of the query. Separates
|
| 363 | * multiple calls with OR
|
| 364 | *
|
| 365 | * @access public
|
| 366 | * @param mixed
|
| 367 | * @param mixed
|
| 368 | * @return object
|
| 369 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 370 | function or_where($key, $value = NULL, $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 371 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 372 | return $this->_where($key, $value, 'OR ', $escape);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 373 | }
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 374 |
|
| 375 | // --------------------------------------------------------------------
|
| 376 |
|
| 377 | /**
|
| 378 | * orwhere() is an alias of or_where()
|
| 379 | * this function is here for backwards compatibility, as
|
| 380 | * orwhere() has been deprecated
|
| 381 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 382 | function orwhere($key, $value = NULL, $escape = TRUE)
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 383 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 384 | return $this->or_where($key, $value, $escape);
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 385 | }
|
Derek Allard | 67b44ed | 2008-01-12 16:18:02 +0000 | [diff] [blame] | 386 |
|
| 387 | // --------------------------------------------------------------------
|
| 388 |
|
| 389 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 390 | * Where
|
| 391 | *
|
| 392 | * Called by where() or orwhere()
|
| 393 | *
|
| 394 | * @access private
|
| 395 | * @param mixed
|
| 396 | * @param mixed
|
| 397 | * @param string
|
| 398 | * @return object
|
| 399 | */
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 400 | function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 401 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 402 | if ( ! is_array($key))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 403 | {
|
| 404 | $key = array($key => $value);
|
| 405 | }
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 406 |
|
| 407 | // If the escape value was not set will will base it on the global setting
|
| 408 | if ( ! is_bool($escape))
|
| 409 | {
|
| 410 | $escape = $this->_protect_identifiers;
|
| 411 | }
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 412 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 413 | foreach ($key as $k => $v)
|
| 414 | {
|
| 415 | $prefix = (count($this->ar_where) == 0) ? '' : $type;
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 416 |
|
Derek Allard | d8364c4 | 2008-06-04 17:01:00 +0000 | [diff] [blame] | 417 | if (is_null($v) && ! $this->_has_operator($k))
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 418 | {
|
| 419 | // value appears not to have been set, assign the test to IS NULL
|
| 420 | $k .= ' IS NULL';
|
| 421 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 422 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 423 | if ( ! is_null($v))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 424 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 425 | if ($escape === TRUE)
|
| 426 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 427 | $k = $this->_protect_identifiers($k, FALSE, $escape);
|
Derek Allard | d8364c4 | 2008-06-04 17:01:00 +0000 | [diff] [blame] | 428 |
|
| 429 | $v = ' '.$this->escape($v);
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 430 | }
|
| 431 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 432 | if ( ! $this->_has_operator($k))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 433 | {
|
| 434 | $k .= ' =';
|
| 435 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 436 | }
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 437 | else
|
| 438 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 439 | $k = $this->_protect_identifiers($k, FALSE, $escape);
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 440 | }
|
| 441 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 442 | $this->ar_where[] = $prefix.$k.$v;
|
Derek Allard | d8364c4 | 2008-06-04 17:01:00 +0000 | [diff] [blame] | 443 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 444 | if ($this->ar_caching === TRUE)
|
| 445 | {
|
| 446 | $this->ar_cache_where[] = $prefix.$k.$v;
|
| 447 | }
|
| 448 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 449 | }
|
Derek Jones | 7e98a27 | 2008-06-04 17:05:44 +0000 | [diff] [blame] | 450 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 451 | return $this;
|
| 452 | }
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 453 |
|
| 454 | // --------------------------------------------------------------------
|
| 455 |
|
| 456 | /**
|
| 457 | * Where_in
|
| 458 | *
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 459 | * Generates a WHERE field IN ('item', 'item') SQL query joined with
|
| 460 | * AND if appropriate
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 461 | *
|
| 462 | * @access public
|
| 463 | * @param string The field to search
|
| 464 | * @param array The values searched on
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 465 | * @return object
|
| 466 | */
|
| 467 | function where_in($key = NULL, $values = NULL)
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 468 | {
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 469 | return $this->_where_in($key, $values);
|
| 470 | }
|
| 471 |
|
| 472 | // --------------------------------------------------------------------
|
| 473 |
|
| 474 | /**
|
| 475 | * Where_in_or
|
| 476 | *
|
| 477 | * Generates a WHERE field IN ('item', 'item') SQL query joined with
|
| 478 | * OR if appropriate
|
| 479 | *
|
| 480 | * @access public
|
| 481 | * @param string The field to search
|
| 482 | * @param array The values searched on
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 483 | * @return object
|
| 484 | */
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 485 | function or_where_in($key = NULL, $values = NULL)
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 486 | {
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 487 | return $this->_where_in($key, $values, FALSE, 'OR ');
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 488 | }
|
| 489 |
|
| 490 | // --------------------------------------------------------------------
|
| 491 |
|
| 492 | /**
|
| 493 | * Where_not_in
|
| 494 | *
|
| 495 | * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
|
| 496 | * with AND if appropriate
|
| 497 | *
|
| 498 | * @access public
|
| 499 | * @param string The field to search
|
| 500 | * @param array The values searched on
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 501 | * @return object
|
| 502 | */
|
| 503 | function where_not_in($key = NULL, $values = NULL)
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 504 | {
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 505 | return $this->_where_in($key, $values, TRUE);
|
| 506 | }
|
| 507 |
|
| 508 | // --------------------------------------------------------------------
|
| 509 |
|
| 510 | /**
|
| 511 | * Where_not_in_or
|
| 512 | *
|
| 513 | * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
|
| 514 | * with OR if appropriate
|
| 515 | *
|
| 516 | * @access public
|
| 517 | * @param string The field to search
|
| 518 | * @param array The values searched on
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 519 | * @return object
|
| 520 | */
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 521 | function or_where_not_in($key = NULL, $values = NULL)
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 522 | {
|
Derek Jones | d007243 | 2008-05-07 22:06:51 +0000 | [diff] [blame] | 523 | return $this->_where_in($key, $values, TRUE, 'OR ');
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 524 | }
|
| 525 |
|
| 526 | // --------------------------------------------------------------------
|
| 527 |
|
| 528 | /**
|
| 529 | * Where_in
|
| 530 | *
|
| 531 | * Called by where_in, where_in_or, where_not_in, where_not_in_or
|
| 532 | *
|
| 533 | * @access public
|
| 534 | * @param string The field to search
|
| 535 | * @param array The values searched on
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 536 | * @param boolean If the statement would be IN or NOT IN
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 537 | * @param string
|
| 538 | * @return object
|
| 539 | */
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 540 | function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 541 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 542 | if ($key === NULL OR $values === NULL)
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 543 | {
|
| 544 | return;
|
| 545 | }
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 546 |
|
| 547 | if ( ! is_array($values))
|
| 548 | {
|
| 549 | $values = array($values);
|
| 550 | }
|
| 551 |
|
| 552 | $not = ($not) ? ' NOT' : '';
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 553 |
|
| 554 | foreach ($values as $value)
|
| 555 | {
|
| 556 | $this->ar_wherein[] = $this->escape($value);
|
| 557 | }
|
| 558 |
|
| 559 | $prefix = (count($this->ar_where) == 0) ? '' : $type;
|
Derek Allard | 5fe155e | 2008-05-12 19:14:57 +0000 | [diff] [blame] | 560 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 561 | $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
|
| 562 |
|
| 563 | $this->ar_where[] = $where_in;
|
| 564 | if ($this->ar_caching === TRUE)
|
| 565 | {
|
| 566 | $this->ar_cache_where[] = $where_in;
|
| 567 | }
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 568 |
|
Derek Allard | 8f00021 | 2008-01-18 14:45:59 +0000 | [diff] [blame] | 569 | // reset the array for multiple calls
|
| 570 | $this->ar_wherein = array();
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 571 | return $this;
|
| 572 | }
|
| 573 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 574 | // --------------------------------------------------------------------
|
| 575 |
|
| 576 | /**
|
| 577 | * Like
|
| 578 | *
|
| 579 | * Generates a %LIKE% portion of the query. Separates
|
| 580 | * multiple calls with AND
|
| 581 | *
|
| 582 | * @access public
|
| 583 | * @param mixed
|
| 584 | * @param mixed
|
| 585 | * @return object
|
| 586 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 587 | function like($field, $match = '', $side = 'both')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 588 | {
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 589 | return $this->_like($field, $match, 'AND ', $side);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 590 | }
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 591 |
|
| 592 | // --------------------------------------------------------------------
|
| 593 |
|
| 594 | /**
|
| 595 | * Not Like
|
| 596 | *
|
| 597 | * Generates a NOT LIKE portion of the query. Separates
|
| 598 | * multiple calls with AND
|
| 599 | *
|
| 600 | * @access public
|
| 601 | * @param mixed
|
| 602 | * @param mixed
|
| 603 | * @return object
|
| 604 | */
|
| 605 | function not_like($field, $match = '', $side = 'both')
|
| 606 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 607 | return $this->_like($field, $match, 'AND ', $side, 'NOT');
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 608 | }
|
| 609 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 610 | // --------------------------------------------------------------------
|
| 611 |
|
| 612 | /**
|
| 613 | * OR Like
|
| 614 | *
|
| 615 | * Generates a %LIKE% portion of the query. Separates
|
| 616 | * multiple calls with OR
|
| 617 | *
|
| 618 | * @access public
|
| 619 | * @param mixed
|
| 620 | * @param mixed
|
| 621 | * @return object
|
| 622 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 623 | function or_like($field, $match = '', $side = 'both')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 624 | {
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 625 | return $this->_like($field, $match, 'OR ', $side);
|
| 626 | }
|
| 627 |
|
| 628 | // --------------------------------------------------------------------
|
| 629 |
|
| 630 | /**
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 631 | * OR Not Like
|
| 632 | *
|
| 633 | * Generates a NOT LIKE portion of the query. Separates
|
| 634 | * multiple calls with OR
|
| 635 | *
|
| 636 | * @access public
|
| 637 | * @param mixed
|
| 638 | * @param mixed
|
| 639 | * @return object
|
| 640 | */
|
| 641 | function or_not_like($field, $match = '', $side = 'both')
|
| 642 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 643 | return $this->_like($field, $match, 'OR ', $side, 'NOT');
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 644 | }
|
| 645 |
|
| 646 | // --------------------------------------------------------------------
|
| 647 |
|
| 648 | /**
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 649 | * orlike() is an alias of or_like()
|
| 650 | * this function is here for backwards compatibility, as
|
| 651 | * orlike() has been deprecated
|
| 652 | */
|
| 653 | function orlike($field, $match = '', $side = 'both')
|
| 654 | {
|
Derek Allard | 4a310b7 | 2008-01-30 21:32:47 +0000 | [diff] [blame] | 655 | return $this->or_like($field, $match, $side);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 656 | }
|
| 657 |
|
| 658 | // --------------------------------------------------------------------
|
| 659 |
|
| 660 | /**
|
| 661 | * Like
|
| 662 | *
|
| 663 | * Called by like() or orlike()
|
| 664 | *
|
| 665 | * @access private
|
| 666 | * @param mixed
|
| 667 | * @param mixed
|
| 668 | * @param string
|
| 669 | * @return object
|
| 670 | */
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 671 | function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 672 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 673 | if ( ! is_array($field))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 674 | {
|
| 675 | $field = array($field => $match);
|
| 676 | }
|
| 677 |
|
| 678 | foreach ($field as $k => $v)
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 679 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 680 | $k = $this->_protect_identifiers($k);
|
| 681 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 682 | $prefix = (count($this->ar_like) == 0) ? '' : $type;
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 683 |
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 684 | $v = $this->escape_str($v);
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 685 |
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 686 | if ($side == 'before')
|
| 687 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 688 | $like_statement = $prefix." $k $not LIKE '%{$v}'";
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 689 | }
|
| 690 | elseif ($side == 'after')
|
| 691 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 692 | $like_statement = $prefix." $k $not LIKE '{$v}%'";
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 693 | }
|
| 694 | else
|
| 695 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 696 | $like_statement = $prefix." $k $not LIKE '%{$v}%'";
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 697 | }
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 698 |
|
| 699 | $this->ar_like[] = $like_statement;
|
| 700 | if ($this->ar_caching === TRUE)
|
| 701 | {
|
| 702 | $this->ar_cache_like[] = $like_statement;
|
| 703 | }
|
| 704 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 705 | }
|
| 706 | return $this;
|
| 707 | }
|
| 708 |
|
| 709 | // --------------------------------------------------------------------
|
| 710 |
|
| 711 | /**
|
| 712 | * GROUP BY
|
| 713 | *
|
| 714 | * @access public
|
| 715 | * @param string
|
| 716 | * @return object
|
| 717 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 718 | function group_by($by)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 719 | {
|
| 720 | if (is_string($by))
|
| 721 | {
|
| 722 | $by = explode(',', $by);
|
| 723 | }
|
| 724 |
|
| 725 | foreach ($by as $val)
|
| 726 | {
|
| 727 | $val = trim($val);
|
| 728 |
|
| 729 | if ($val != '')
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 730 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 731 | $this->ar_groupby[] = $this->_protect_identifiers($val);
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 732 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 733 | if ($this->ar_caching === TRUE)
|
| 734 | {
|
| 735 | $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
|
| 736 | }
|
| 737 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 738 | }
|
| 739 | return $this;
|
| 740 | }
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 741 |
|
| 742 | // --------------------------------------------------------------------
|
| 743 |
|
| 744 | /**
|
| 745 | * groupby() is an alias of group_by()
|
| 746 | * this function is here for backwards compatibility, as
|
| 747 | * groupby() has been deprecated
|
| 748 | */
|
| 749 | function groupby($by)
|
| 750 | {
|
| 751 | return $this->group_by($by);
|
| 752 | }
|
| 753 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 754 | // --------------------------------------------------------------------
|
| 755 |
|
| 756 | /**
|
| 757 | * Sets the HAVING value
|
| 758 | *
|
| 759 | * Separates multiple calls with AND
|
| 760 | *
|
| 761 | * @access public
|
| 762 | * @param string
|
| 763 | * @param string
|
| 764 | * @return object
|
| 765 | */
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 766 | function having($key, $value = '', $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 767 | {
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 768 | return $this->_having($key, $value, 'AND ', $escape);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 769 | }
|
Derek Allard | 43e8cbf | 2008-03-01 23:14:43 +0000 | [diff] [blame] | 770 |
|
| 771 | // --------------------------------------------------------------------
|
| 772 |
|
| 773 | /**
|
| 774 | * orhaving() is an alias of or_having()
|
| 775 | * this function is here for backwards compatibility, as
|
| 776 | * orhaving() has been deprecated
|
| 777 | */
|
| 778 |
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 779 | function orhaving($key, $value = '', $escape = TRUE)
|
Derek Allard | 43e8cbf | 2008-03-01 23:14:43 +0000 | [diff] [blame] | 780 | {
|
Derek Allard | a459b46 | 2008-05-22 13:01:39 +0000 | [diff] [blame] | 781 | return $this->or_having($key, $value, $escape);
|
Derek Allard | 43e8cbf | 2008-03-01 23:14:43 +0000 | [diff] [blame] | 782 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 783 | // --------------------------------------------------------------------
|
| 784 |
|
| 785 | /**
|
| 786 | * Sets the OR HAVING value
|
| 787 | *
|
| 788 | * Separates multiple calls with OR
|
| 789 | *
|
| 790 | * @access public
|
| 791 | * @param string
|
| 792 | * @param string
|
| 793 | * @return object
|
| 794 | */
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 795 | function or_having($key, $value = '', $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 796 | {
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 797 | return $this->_having($key, $value, 'OR ', $escape);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 798 | }
|
| 799 |
|
| 800 | // --------------------------------------------------------------------
|
| 801 |
|
| 802 | /**
|
| 803 | * Sets the HAVING values
|
| 804 | *
|
Rick Ellis | ff73401 | 2008-09-30 20:38:12 +0000 | [diff] [blame] | 805 | * Called by having() or or_having()
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 806 | *
|
| 807 | * @access private
|
| 808 | * @param string
|
| 809 | * @param string
|
| 810 | * @return object
|
| 811 | */
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 812 | function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 813 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 814 | if ( ! is_array($key))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 815 | {
|
| 816 | $key = array($key => $value);
|
| 817 | }
|
| 818 |
|
| 819 | foreach ($key as $k => $v)
|
| 820 | {
|
| 821 | $prefix = (count($this->ar_having) == 0) ? '' : $type;
|
Derek Allard | e808aac | 2008-04-06 18:22:00 +0000 | [diff] [blame] | 822 |
|
| 823 | if ($escape === TRUE)
|
| 824 | {
|
| 825 | $k = $this->_protect_identifiers($k);
|
| 826 | }
|
| 827 |
|
Derek Allard | a459b46 | 2008-05-22 13:01:39 +0000 | [diff] [blame] | 828 | if ( ! $this->_has_operator($k))
|
| 829 | {
|
| 830 | $k .= ' = ';
|
| 831 | }
|
| 832 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 833 | if ($v != '')
|
| 834 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 835 | $v = ' '.$this->escape_str($v);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 836 | }
|
| 837 |
|
| 838 | $this->ar_having[] = $prefix.$k.$v;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 839 | if ($this->ar_caching === TRUE)
|
| 840 | {
|
| 841 | $this->ar_cache_having[] = $prefix.$k.$v;
|
| 842 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 843 | }
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 844 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 845 | return $this;
|
| 846 | }
|
| 847 |
|
| 848 | // --------------------------------------------------------------------
|
| 849 |
|
| 850 | /**
|
| 851 | * Sets the ORDER BY value
|
| 852 | *
|
| 853 | * @access public
|
| 854 | * @param string
|
| 855 | * @param string direction: asc or desc
|
| 856 | * @return object
|
| 857 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 858 | function order_by($orderby, $direction = '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 859 | {
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 860 | if (strtolower($direction) == 'random')
|
| 861 | {
|
| 862 | $orderby = ''; // Random results want or don't need a field name
|
| 863 | $direction = $this->_random_keyword;
|
| 864 | }
|
| 865 | elseif (trim($direction) != '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 866 | {
|
Derek Allard | 9278249 | 2007-08-10 11:26:01 +0000 | [diff] [blame] | 867 | $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 868 | }
|
| 869 |
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 870 | $orderby_statement = $this->_protect_identifiers($orderby).$direction;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 871 |
|
| 872 | $this->ar_orderby[] = $orderby_statement;
|
| 873 | if ($this->ar_caching === TRUE)
|
| 874 | {
|
| 875 | $this->ar_cache_orderby[] = $orderby_statement;
|
| 876 | }
|
| 877 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 878 | return $this;
|
| 879 | }
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 880 |
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 881 | // --------------------------------------------------------------------
|
| 882 |
|
| 883 | /**
|
| 884 | * orderby() is an alias of order_by()
|
| 885 | * this function is here for backwards compatibility, as
|
| 886 | * orderby() has been deprecated
|
| 887 | */
|
| 888 | function orderby($orderby, $direction = '')
|
| 889 | {
|
| 890 | return $this->order_by($orderby, $direction);
|
| 891 | }
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 892 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 893 | // --------------------------------------------------------------------
|
| 894 |
|
| 895 | /**
|
| 896 | * Sets the LIMIT value
|
| 897 | *
|
| 898 | * @access public
|
| 899 | * @param integer the limit value
|
| 900 | * @param integer the offset value
|
| 901 | * @return object
|
| 902 | */
|
| 903 | function limit($value, $offset = '')
|
| 904 | {
|
| 905 | $this->ar_limit = $value;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 906 | if ($this->ar_caching === TRUE)
|
| 907 | {
|
| 908 | $this->ar_cache_limit[] = $value;
|
| 909 | }
|
| 910 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 911 | if ($offset != '')
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 912 | {
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 913 | $this->ar_offset = $offset;
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 914 | if ($this->ar_caching === TRUE)
|
| 915 | {
|
| 916 | $this->ar_cache_offset[] = $offset;
|
| 917 | }
|
| 918 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 919 |
|
| 920 | return $this;
|
| 921 | }
|
| 922 |
|
| 923 | // --------------------------------------------------------------------
|
| 924 |
|
| 925 | /**
|
| 926 | * Sets the OFFSET value
|
| 927 | *
|
| 928 | * @access public
|
| 929 | * @param integer the offset value
|
| 930 | * @return object
|
| 931 | */
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 932 | function offset($offset)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 933 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 934 | $this->ar_offset = $offset;
|
| 935 | if ($this->ar_caching === TRUE)
|
| 936 | {
|
| 937 | $this->ar_cache_offset[] = $offset;
|
| 938 | }
|
| 939 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 940 | return $this;
|
| 941 | }
|
| 942 |
|
| 943 | // --------------------------------------------------------------------
|
| 944 |
|
| 945 | /**
|
| 946 | * The "set" function. Allows key/value pairs to be set for inserting or updating
|
| 947 | *
|
| 948 | * @access public
|
| 949 | * @param mixed
|
| 950 | * @param string
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 951 | * @param boolean
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 952 | * @return object
|
| 953 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 954 | function set($key, $value = '', $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 955 | {
|
| 956 | $key = $this->_object_to_array($key);
|
| 957 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 958 | if ( ! is_array($key))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 959 | {
|
| 960 | $key = array($key => $value);
|
| 961 | }
|
| 962 |
|
| 963 | foreach ($key as $k => $v)
|
| 964 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 965 | if ($escape === FALSE)
|
| 966 | {
|
| 967 | $this->ar_set[$this->_protect_identifiers($k)] = $v;
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 968 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 969 | if ($this->ar_caching === TRUE)
|
| 970 | {
|
| 971 | $this->ar_cache_offset[$this->_protect_identifiers($k)] = $v;
|
| 972 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 973 | }
|
| 974 | else
|
| 975 | {
|
| 976 | $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 977 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 978 | if ($this->ar_caching === TRUE)
|
| 979 | {
|
| 980 | $this->ar_cache_offset[$this->_protect_identifiers($k)] = $this->escape($v);
|
| 981 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 982 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 983 | }
|
| 984 |
|
| 985 | return $this;
|
| 986 | }
|
| 987 |
|
| 988 | // --------------------------------------------------------------------
|
| 989 |
|
| 990 | /**
|
| 991 | * Get
|
| 992 | *
|
| 993 | * Compiles the select statement based on the other functions called
|
| 994 | * and runs the query
|
| 995 | *
|
| 996 | * @access public
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 997 | * @param string the table
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 998 | * @param string the limit clause
|
| 999 | * @param string the offset clause
|
| 1000 | * @return object
|
| 1001 | */
|
| 1002 | function get($table = '', $limit = null, $offset = null)
|
| 1003 | {
|
| 1004 | if ($table != '')
|
| 1005 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1006 | $this->_track_aliases($table);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1007 | $this->from($table);
|
| 1008 | }
|
| 1009 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1010 | if ( ! is_null($limit))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1011 | {
|
| 1012 | $this->limit($limit, $offset);
|
| 1013 | }
|
| 1014 |
|
| 1015 | $sql = $this->_compile_select();
|
| 1016 |
|
| 1017 | $result = $this->query($sql);
|
| 1018 | $this->_reset_select();
|
| 1019 | return $result;
|
| 1020 | }
|
| 1021 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1022 | /**
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1023 | * "Count All Results" query
|
| 1024 | *
|
| 1025 | * Generates a platform-specific query string that counts all records
|
| 1026 | * returned by an Active Record query.
|
| 1027 | *
|
| 1028 | * @access public
|
| 1029 | * @param string
|
| 1030 | * @return string
|
| 1031 | */
|
| 1032 | function count_all_results($table = '')
|
| 1033 | {
|
| 1034 | if ($table != '')
|
| 1035 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1036 | $this->_track_aliases($table);
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1037 | $this->from($table);
|
| 1038 | }
|
| 1039 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1040 | $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1041 |
|
| 1042 | $query = $this->query($sql);
|
| 1043 | $this->_reset_select();
|
| 1044 |
|
| 1045 | if ($query->num_rows() == 0)
|
| 1046 | {
|
| 1047 | return '0';
|
| 1048 | }
|
| 1049 |
|
| 1050 | $row = $query->row();
|
| 1051 | return $row->numrows;
|
| 1052 | }
|
| 1053 |
|
| 1054 | // --------------------------------------------------------------------
|
| 1055 |
|
| 1056 | /**
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 1057 | * Get_Where
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1058 | *
|
| 1059 | * Allows the where clause, limit and offset to be added directly
|
| 1060 | *
|
| 1061 | * @access public
|
| 1062 | * @param string the where clause
|
| 1063 | * @param string the limit clause
|
| 1064 | * @param string the offset clause
|
| 1065 | * @return object
|
| 1066 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 1067 | function get_where($table = '', $where = null, $limit = null, $offset = null)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1068 | {
|
| 1069 | if ($table != '')
|
| 1070 | {
|
| 1071 | $this->from($table);
|
| 1072 | }
|
| 1073 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1074 | if ( ! is_null($where))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1075 | {
|
| 1076 | $this->where($where);
|
| 1077 | }
|
| 1078 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1079 | if ( ! is_null($limit))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1080 | {
|
| 1081 | $this->limit($limit, $offset);
|
| 1082 | }
|
| 1083 |
|
| 1084 | $sql = $this->_compile_select();
|
| 1085 |
|
| 1086 | $result = $this->query($sql);
|
| 1087 | $this->_reset_select();
|
| 1088 | return $result;
|
| 1089 | }
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 1090 |
|
| 1091 | // --------------------------------------------------------------------
|
| 1092 |
|
| 1093 | /**
|
| 1094 | * getwhere() is an alias of get_where()
|
| 1095 | * this function is here for backwards compatibility, as
|
| 1096 | * getwhere() has been deprecated
|
| 1097 | */
|
| 1098 | function getwhere($table = '', $where = null, $limit = null, $offset = null)
|
| 1099 | {
|
| 1100 | return $this->get_where($table, $where, $limit, $offset);
|
| 1101 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1102 |
|
| 1103 | // --------------------------------------------------------------------
|
| 1104 |
|
| 1105 | /**
|
| 1106 | * Insert
|
| 1107 | *
|
| 1108 | * Compiles an insert string and runs the query
|
| 1109 | *
|
| 1110 | * @access public
|
| 1111 | * @param string the table to retrieve the results from
|
| 1112 | * @param array an associative array of insert values
|
| 1113 | * @return object
|
| 1114 | */
|
| 1115 | function insert($table = '', $set = NULL)
|
| 1116 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1117 | if ( ! is_null($set))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1118 | {
|
| 1119 | $this->set($set);
|
| 1120 | }
|
| 1121 |
|
| 1122 | if (count($this->ar_set) == 0)
|
| 1123 | {
|
| 1124 | if ($this->db_debug)
|
| 1125 | {
|
| 1126 | return $this->display_error('db_must_use_set');
|
| 1127 | }
|
| 1128 | return FALSE;
|
| 1129 | }
|
| 1130 |
|
| 1131 | if ($table == '')
|
| 1132 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1133 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1134 | {
|
| 1135 | if ($this->db_debug)
|
| 1136 | {
|
| 1137 | return $this->display_error('db_must_set_table');
|
| 1138 | }
|
| 1139 | return FALSE;
|
| 1140 | }
|
| 1141 |
|
| 1142 | $table = $this->ar_from[0];
|
| 1143 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1144 |
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1145 | $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1146 |
|
| 1147 | $this->_reset_write();
|
| 1148 | return $this->query($sql);
|
| 1149 | }
|
| 1150 |
|
| 1151 | // --------------------------------------------------------------------
|
| 1152 |
|
| 1153 | /**
|
| 1154 | * Update
|
| 1155 | *
|
| 1156 | * Compiles an update string and runs the query
|
| 1157 | *
|
| 1158 | * @access public
|
| 1159 | * @param string the table to retrieve the results from
|
| 1160 | * @param array an associative array of update values
|
| 1161 | * @param mixed the where clause
|
| 1162 | * @return object
|
| 1163 | */
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1164 | function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1165 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1166 | if ( ! is_null($set))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1167 | {
|
| 1168 | $this->set($set);
|
| 1169 | }
|
| 1170 |
|
| 1171 | if (count($this->ar_set) == 0)
|
| 1172 | {
|
| 1173 | if ($this->db_debug)
|
| 1174 | {
|
| 1175 | return $this->display_error('db_must_use_set');
|
| 1176 | }
|
| 1177 | return FALSE;
|
| 1178 | }
|
| 1179 |
|
| 1180 | if ($table == '')
|
| 1181 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1182 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1183 | {
|
| 1184 | if ($this->db_debug)
|
| 1185 | {
|
| 1186 | return $this->display_error('db_must_set_table');
|
| 1187 | }
|
| 1188 | return FALSE;
|
| 1189 | }
|
| 1190 |
|
| 1191 | $table = $this->ar_from[0];
|
| 1192 | }
|
| 1193 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1194 | if ($where != NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1195 | {
|
| 1196 | $this->where($where);
|
| 1197 | }
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 1198 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1199 | if ($limit != NULL)
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 1200 | {
|
| 1201 | $this->limit($limit);
|
| 1202 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1203 |
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1204 | $sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1205 |
|
| 1206 | $this->_reset_write();
|
| 1207 | return $this->query($sql);
|
| 1208 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1209 |
|
| 1210 | // --------------------------------------------------------------------
|
| 1211 |
|
| 1212 | /**
|
| 1213 | * Empty Table
|
| 1214 | *
|
| 1215 | * Compiles a delete string and runs "DELETE FROM table"
|
| 1216 | *
|
| 1217 | * @access public
|
| 1218 | * @param string the table to empty
|
| 1219 | * @return object
|
| 1220 | */
|
| 1221 | function empty_table($table = '')
|
| 1222 | {
|
| 1223 | if ($table == '')
|
| 1224 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1225 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1226 | {
|
| 1227 | if ($this->db_debug)
|
| 1228 | {
|
| 1229 | return $this->display_error('db_must_set_table');
|
| 1230 | }
|
| 1231 | return FALSE;
|
| 1232 | }
|
| 1233 |
|
| 1234 | $table = $this->ar_from[0];
|
| 1235 | }
|
| 1236 | else
|
| 1237 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1238 | $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1239 | }
|
| 1240 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1241 | $sql = $this->_delete($table);
|
| 1242 |
|
| 1243 | $this->_reset_write();
|
| 1244 |
|
| 1245 | return $this->query($sql);
|
| 1246 | }
|
| 1247 |
|
| 1248 | // --------------------------------------------------------------------
|
| 1249 |
|
| 1250 | /**
|
| 1251 | * Truncate
|
| 1252 | *
|
| 1253 | * Compiles a truncate string and runs the query
|
| 1254 | * If the database does not support the truncate() command
|
| 1255 | * This function maps to "DELETE FROM table"
|
| 1256 | *
|
| 1257 | * @access public
|
| 1258 | * @param string the table to truncate
|
| 1259 | * @return object
|
| 1260 | */
|
| 1261 | function truncate($table = '')
|
| 1262 | {
|
| 1263 | if ($table == '')
|
| 1264 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1265 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1266 | {
|
| 1267 | if ($this->db_debug)
|
| 1268 | {
|
| 1269 | return $this->display_error('db_must_set_table');
|
| 1270 | }
|
| 1271 | return FALSE;
|
| 1272 | }
|
| 1273 |
|
| 1274 | $table = $this->ar_from[0];
|
| 1275 | }
|
| 1276 | else
|
| 1277 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1278 | $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1279 | }
|
| 1280 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1281 | $sql = $this->_truncate($table);
|
| 1282 |
|
| 1283 | $this->_reset_write();
|
| 1284 |
|
| 1285 | return $this->query($sql);
|
| 1286 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1287 |
|
| 1288 | // --------------------------------------------------------------------
|
| 1289 |
|
| 1290 | /**
|
| 1291 | * Delete
|
| 1292 | *
|
| 1293 | * Compiles a delete string and runs the query
|
| 1294 | *
|
| 1295 | * @access public
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1296 | * @param mixed the table(s) to delete from. String or array
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1297 | * @param mixed the where clause
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1298 | * @param mixed the limit clause
|
| 1299 | * @param boolean
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1300 | * @return object
|
| 1301 | */
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1302 | function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1303 | {
|
| 1304 | if ($table == '')
|
| 1305 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1306 | if ( ! isset($this->ar_from[0]))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1307 | {
|
| 1308 | if ($this->db_debug)
|
| 1309 | {
|
| 1310 | return $this->display_error('db_must_set_table');
|
| 1311 | }
|
| 1312 | return FALSE;
|
| 1313 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1314 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1315 | $table = $this->ar_from[0];
|
| 1316 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1317 | elseif (is_array($table))
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1318 | {
|
| 1319 | foreach($table as $single_table)
|
| 1320 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1321 | $this->delete($single_table, $where, $limit, FALSE);
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1322 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1323 |
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1324 | $this->_reset_write();
|
| 1325 | return;
|
| 1326 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1327 | else
|
| 1328 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1329 | $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1330 | }
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1331 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1332 | if ($where != '')
|
| 1333 | {
|
| 1334 | $this->where($where);
|
| 1335 | }
|
| 1336 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1337 | if ($limit != NULL)
|
| 1338 | {
|
| 1339 | $this->limit($limit);
|
| 1340 | }
|
| 1341 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1342 | if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1343 | {
|
| 1344 | if ($this->db_debug)
|
| 1345 | {
|
| 1346 | return $this->display_error('db_del_must_use_where');
|
| 1347 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1348 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1349 | return FALSE;
|
| 1350 | }
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1351 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1352 | $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1353 |
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1354 | if ($reset_data)
|
| 1355 | {
|
| 1356 | $this->_reset_write();
|
| 1357 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1358 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1359 | return $this->query($sql);
|
| 1360 | }
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 1361 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1362 | // --------------------------------------------------------------------
|
| 1363 |
|
| 1364 | /**
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1365 | * DB Prefix
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1366 | *
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1367 | * Prepends a database prefix if one exists in configuration
|
| 1368 | *
|
| 1369 | * @access public
|
| 1370 | * @param string the table
|
| 1371 | * @return string
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1372 | */
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1373 | function dbprefix($table = '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1374 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1375 | if ($table == '')
|
| 1376 | {
|
| 1377 | $this->display_error('db_table_name_required');
|
| 1378 | }
|
| 1379 |
|
| 1380 | return $this->dbprefix.$table;
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1381 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1382 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1383 | // --------------------------------------------------------------------
|
| 1384 |
|
| 1385 | /**
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1386 | * Track Aliases
|
| 1387 | *
|
| 1388 | * Used to track SQL statements written with aliased tables.
|
| 1389 | *
|
| 1390 | * @access private
|
| 1391 | * @param string The table to inspect
|
| 1392 | * @return string
|
| 1393 | */
|
| 1394 | function _track_aliases($table)
|
| 1395 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1396 | if (is_array($table))
|
| 1397 | {
|
| 1398 | foreach ($table as $t)
|
| 1399 | {
|
| 1400 | $this->_track_aliases($t);
|
| 1401 | }
|
| 1402 | return;
|
| 1403 | }
|
| 1404 |
|
| 1405 | // Does the string contain a comma? If so, we need to separate
|
| 1406 | // the string into discreet statements
|
| 1407 | if (strpos($table, ',') !== FALSE)
|
| 1408 | {
|
| 1409 | return $this->_track_aliases(explode(',', $table));
|
| 1410 | }
|
| 1411 |
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1412 | // if a table alias is used we can recognize it by a space
|
| 1413 | if (strpos($table, " ") !== FALSE)
|
| 1414 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1415 | // if the alias is written with the AS keyword, remove it
|
| 1416 | $table = preg_replace('/ AS /i', ' ', $table);
|
| 1417 |
|
| 1418 | // Grab the alias
|
| 1419 | $table = trim(strrchr($table, " "));
|
| 1420 |
|
| 1421 | // Store the alias, if it doesn't already exist
|
| 1422 | if ( ! in_array($table, $this->ar_aliased_tables))
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1423 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1424 | $this->ar_aliased_tables[] = $table;
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1425 | }
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1426 | }
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1427 | }
|
| 1428 |
|
| 1429 | // --------------------------------------------------------------------
|
| 1430 |
|
| 1431 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1432 | * Compile the SELECT statement
|
| 1433 | *
|
| 1434 | * Generates a query string based on which functions were used.
|
| 1435 | * Should not be called directly. The get() function calls it.
|
| 1436 | *
|
| 1437 | * @access private
|
| 1438 | * @return string
|
| 1439 | */
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1440 | function _compile_select($select_override = FALSE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1441 | {
|
Derek Allard | 7825526 | 2008-02-06 13:54:23 +0000 | [diff] [blame] | 1442 | $this->_merge_cache();
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1443 |
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1444 | // ----------------------------------------------------------------
|
| 1445 |
|
| 1446 | // Write the "select" portion of the query
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1447 |
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1448 | if ($select_override !== FALSE)
|
| 1449 | {
|
| 1450 | $sql = $select_override;
|
| 1451 | }
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1452 | else
|
| 1453 | {
|
| 1454 | $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
|
| 1455 |
|
| 1456 | if (count($this->ar_select) == 0)
|
| 1457 | {
|
| 1458 | $sql .= '*';
|
| 1459 | }
|
| 1460 | else
|
| 1461 | {
|
| 1462 | // Cycle through the "select" portion of the query and prep each column name.
|
| 1463 | // The reason we protect identifiers here rather then in the select() function
|
| 1464 | // is because until the user calls the from() function we don't know if there are aliases
|
| 1465 | foreach ($this->ar_select as $key => $val)
|
| 1466 | {
|
| 1467 | $this->ar_select[$key] = $this->_protect_identifiers($val);
|
| 1468 | }
|
| 1469 |
|
| 1470 | $sql .= implode(', ', $this->ar_select);
|
| 1471 | }
|
| 1472 | }
|
| 1473 |
|
| 1474 | // ----------------------------------------------------------------
|
| 1475 |
|
| 1476 | // Write the "FROM" portion of the query
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1477 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1478 | if (count($this->ar_from) > 0)
|
| 1479 | {
|
| 1480 | $sql .= "\nFROM ";
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1481 |
|
Derek Jones | c6ad023 | 2008-01-29 18:44:54 +0000 | [diff] [blame] | 1482 | $sql .= $this->_from_tables($this->ar_from);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1483 | }
|
| 1484 |
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1485 | // ----------------------------------------------------------------
|
| 1486 |
|
| 1487 | // Write the "JOIN" portion of the query
|
| 1488 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1489 | if (count($this->ar_join) > 0)
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1490 | {
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1491 | $sql .= "\n";
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1492 |
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1493 | $sql .= implode("\n", $this->ar_join);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1494 | }
|
| 1495 |
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1496 | // ----------------------------------------------------------------
|
| 1497 |
|
| 1498 | // Write the "WHERE" portion of the query
|
| 1499 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1500 | if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
|
| 1501 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1502 | $sql .= "\n";
|
| 1503 |
|
| 1504 | $sql .= "WHERE ";
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1505 | }
|
| 1506 |
|
| 1507 | $sql .= implode("\n", $this->ar_where);
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1508 |
|
| 1509 | // ----------------------------------------------------------------
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1510 |
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1511 | // Write the "LIKE" portion of the query
|
| 1512 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1513 | if (count($this->ar_like) > 0)
|
| 1514 | {
|
| 1515 | if (count($this->ar_where) > 0)
|
| 1516 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1517 | $sql .= "\nAND ";
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1518 | }
|
| 1519 |
|
| 1520 | $sql .= implode("\n", $this->ar_like);
|
| 1521 | }
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1522 |
|
| 1523 | // ----------------------------------------------------------------
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1524 |
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1525 | // Write the "GROUP BY" portion of the query
|
| 1526 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1527 | if (count($this->ar_groupby) > 0)
|
| 1528 | {
|
| 1529 | $sql .= "\nGROUP BY ";
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1530 |
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1531 | $sql .= implode(', ', $this->ar_groupby);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1532 | }
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1533 |
|
| 1534 | // ----------------------------------------------------------------
|
| 1535 |
|
| 1536 | // Write the "HAVING" portion of the query
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1537 |
|
| 1538 | if (count($this->ar_having) > 0)
|
| 1539 | {
|
| 1540 | $sql .= "\nHAVING ";
|
| 1541 | $sql .= implode("\n", $this->ar_having);
|
| 1542 | }
|
| 1543 |
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1544 | // ----------------------------------------------------------------
|
| 1545 |
|
| 1546 | // Write the "ORDER BY" portion of the query
|
| 1547 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1548 | if (count($this->ar_orderby) > 0)
|
| 1549 | {
|
| 1550 | $sql .= "\nORDER BY ";
|
| 1551 | $sql .= implode(', ', $this->ar_orderby);
|
| 1552 |
|
| 1553 | if ($this->ar_order !== FALSE)
|
| 1554 | {
|
| 1555 | $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
|
| 1556 | }
|
| 1557 | }
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1558 |
|
| 1559 | // ----------------------------------------------------------------
|
| 1560 |
|
| 1561 | // Write the "LIMIT" portion of the query
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1562 |
|
| 1563 | if (is_numeric($this->ar_limit))
|
| 1564 | {
|
| 1565 | $sql .= "\n";
|
| 1566 | $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
|
| 1567 | }
|
| 1568 |
|
| 1569 | return $sql;
|
| 1570 | }
|
| 1571 |
|
| 1572 | // --------------------------------------------------------------------
|
| 1573 |
|
| 1574 | /**
|
| 1575 | * Object to Array
|
| 1576 | *
|
| 1577 | * Takes an object as input and converts the class variables to array key/vals
|
| 1578 | *
|
| 1579 | * @access public
|
| 1580 | * @param object
|
| 1581 | * @return array
|
| 1582 | */
|
| 1583 | function _object_to_array($object)
|
| 1584 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1585 | if ( ! is_object($object))
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1586 | {
|
| 1587 | return $object;
|
| 1588 | }
|
| 1589 |
|
| 1590 | $array = array();
|
| 1591 | foreach (get_object_vars($object) as $key => $val)
|
| 1592 | {
|
Derek Allard | 848b776 | 2007-12-31 16:43:05 +0000 | [diff] [blame] | 1593 | // There are some built in keys we need to ignore for this conversion
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1594 | if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1595 | {
|
| 1596 | $array[$key] = $val;
|
| 1597 | }
|
| 1598 | }
|
| 1599 |
|
| 1600 | return $array;
|
| 1601 | }
|
| 1602 |
|
| 1603 | // --------------------------------------------------------------------
|
| 1604 |
|
| 1605 | /**
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1606 | * Start Cache
|
| 1607 | *
|
| 1608 | * Starts AR caching
|
| 1609 | *
|
| 1610 | * @access public
|
| 1611 | * @return void
|
| 1612 | */
|
| 1613 | function start_cache()
|
| 1614 | {
|
| 1615 | $this->ar_caching = TRUE;
|
| 1616 | }
|
| 1617 |
|
| 1618 | // --------------------------------------------------------------------
|
| 1619 |
|
| 1620 | /**
|
| 1621 | * Stop Cache
|
| 1622 | *
|
| 1623 | * Stops AR caching
|
| 1624 | *
|
| 1625 | * @access public
|
| 1626 | * @return void
|
| 1627 | */
|
| 1628 | function stop_cache()
|
| 1629 | {
|
| 1630 | $this->ar_caching = FALSE;
|
| 1631 | }
|
| 1632 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1633 | // --------------------------------------------------------------------
|
| 1634 |
|
| 1635 | /**
|
| 1636 | * Flush Cache
|
| 1637 | *
|
| 1638 | * Empties the AR cache
|
| 1639 | *
|
| 1640 | * @access public
|
| 1641 | * @return void
|
| 1642 | */
|
| 1643 | function flush_cache()
|
| 1644 | {
|
| 1645 | $ar_reset_items = array(
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1646 | 'ar_cache_select' => array(),
|
| 1647 | 'ar_cache_from' => array(),
|
| 1648 | 'ar_cache_join' => array(),
|
| 1649 | 'ar_cache_where' => array(),
|
| 1650 | 'ar_cache_like' => array(),
|
| 1651 | 'ar_cache_groupby' => array(),
|
| 1652 | 'ar_cache_having' =>array(),
|
| 1653 | 'ar_cache_orderby' => array(),
|
| 1654 | 'ar_cache_set' => array()
|
| 1655 | );
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1656 |
|
| 1657 | $this->_reset_run($ar_reset_items);
|
| 1658 | }
|
| 1659 |
|
| 1660 | // --------------------------------------------------------------------
|
| 1661 |
|
| 1662 | /**
|
| 1663 | * Merge Cache
|
| 1664 | *
|
| 1665 | * When called, this function merges any cached AR arrays with
|
| 1666 | * locally called ones.
|
| 1667 | *
|
| 1668 | * @access private
|
| 1669 | * @return void
|
| 1670 | */
|
| 1671 | function _merge_cache()
|
| 1672 | {
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1673 | if ($this->ar_caching == FALSE)
|
| 1674 | {
|
| 1675 | return;
|
| 1676 | }
|
| 1677 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1678 | $ar_items = array('select', 'from', 'join', 'where', 'like', 'groupby', 'having', 'orderby', 'set');
|
| 1679 |
|
| 1680 | foreach ($ar_items as $ar_item)
|
| 1681 | {
|
| 1682 | $ar_cache_item = 'ar_cache_'.$ar_item;
|
| 1683 | $ar_item = 'ar_'.$ar_item;
|
| 1684 | $this->$ar_item = array_unique(array_merge($this->$ar_item, $this->$ar_cache_item));
|
| 1685 | }
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1686 |
|
| 1687 | // If we are "protecting identifiers" we need to examine the "from"
|
| 1688 | // portion of the query to determine if there are any aliases
|
| 1689 | if ($this->_protect_identifiers === TRUE)
|
| 1690 | {
|
| 1691 | $this->_track_aliases($this->ar_from);
|
| 1692 | }
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1693 | }
|
| 1694 |
|
| 1695 | // --------------------------------------------------------------------
|
| 1696 |
|
| 1697 | /**
|
| 1698 | * Resets the active record values. Called by the get() function
|
| 1699 | *
|
| 1700 | * @access private
|
| 1701 | * @param array An array of fields to reset
|
| 1702 | * @return void
|
| 1703 | */
|
| 1704 | function _reset_run($ar_reset_items)
|
| 1705 | {
|
| 1706 | foreach ($ar_reset_items as $item => $default_value)
|
| 1707 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1708 | if ( ! in_array($item, $this->ar_store_array))
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1709 | {
|
| 1710 | $this->$item = $default_value;
|
| 1711 | }
|
| 1712 | }
|
| 1713 | }
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1714 |
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1715 | // --------------------------------------------------------------------
|
| 1716 |
|
| 1717 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1718 | * Resets the active record values. Called by the get() function
|
| 1719 | *
|
| 1720 | * @access private
|
| 1721 | * @return void
|
| 1722 | */
|
| 1723 | function _reset_select()
|
| 1724 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1725 | $ar_reset_items = array(
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1726 | 'ar_select' => array(),
|
| 1727 | 'ar_from' => array(),
|
| 1728 | 'ar_join' => array(),
|
| 1729 | 'ar_where' => array(),
|
| 1730 | 'ar_like' => array(),
|
| 1731 | 'ar_groupby' => array(),
|
| 1732 | 'ar_having' => array(),
|
| 1733 | 'ar_orderby' => array(),
|
| 1734 | 'ar_wherein' => array(),
|
| 1735 | 'ar_aliased_tables' => array(),
|
| 1736 | 'ar_distinct' => FALSE,
|
| 1737 | 'ar_limit' => FALSE,
|
| 1738 | 'ar_offset' => FALSE,
|
| 1739 | 'ar_order' => FALSE,
|
| 1740 | );
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1741 |
|
| 1742 | $this->_reset_run($ar_reset_items);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1743 | }
|
| 1744 |
|
| 1745 | // --------------------------------------------------------------------
|
| 1746 |
|
| 1747 | /**
|
| 1748 | * Resets the active record "write" values.
|
| 1749 | *
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1750 | * Called by the insert() update() and delete() functions
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1751 | *
|
| 1752 | * @access private
|
| 1753 | * @return void
|
| 1754 | */
|
| 1755 | function _reset_write()
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1756 | {
|
| 1757 | $ar_reset_items = array(
|
Rick Ellis | 5952359 | 2008-10-17 04:07:40 +0000 | [diff] [blame^] | 1758 | 'ar_set' => array(),
|
| 1759 | 'ar_from' => array(),
|
| 1760 | 'ar_where' => array(),
|
| 1761 | 'ar_like' => array(),
|
| 1762 | 'ar_orderby' => array(),
|
| 1763 | 'ar_limit' => FALSE,
|
| 1764 | 'ar_order' => FALSE
|
| 1765 | );
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 1766 |
|
| 1767 | $this->_reset_run($ar_reset_items);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1768 | }
|
| 1769 |
|
| 1770 | }
|
Derek Allard | a632589 | 2008-05-12 17:51:47 +0000 | [diff] [blame] | 1771 |
|
| 1772 | /* End of file DB_active_rec.php */
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 1773 | /* Location: ./system/database/DB_active_rec.php */ |