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