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