Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
| 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
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Allard | 6838f00 | 2007-10-04 19:29:59 +0000 | [diff] [blame] | 10 | * @license http://www.codeigniter.com/user_guide/license.html
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 11 | * @link http://www.codeigniter.com
|
| 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 | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 27 | * @link http://www.codeigniter.com/user_guide/database/
|
| 28 | */
|
| 29 | class CI_DB_active_record extends CI_DB_driver {
|
| 30 |
|
| 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();
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 44 | var $ar_wherein = array();
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 45 | var $ar_aliased_tables = array();
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 46 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 47 | /**
|
| 48 | * Select
|
| 49 | *
|
| 50 | * Generates the SELECT portion of the query
|
| 51 | *
|
| 52 | * @access public
|
| 53 | * @param string
|
| 54 | * @return object
|
| 55 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 56 | function select($select = '*', $protect_identifiers = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 57 | {
|
| 58 | if (is_string($select))
|
| 59 | {
|
| 60 | $select = explode(',', $select);
|
| 61 | }
|
| 62 |
|
| 63 | foreach ($select as $val)
|
| 64 | {
|
| 65 | $val = trim($val);
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 66 |
|
| 67 | if ($val != '*' && $protect_identifiers !== FALSE)
|
| 68 | {
|
| 69 | $val = $this->_protect_identifiers($val);
|
| 70 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 71 |
|
| 72 | if ($val != '')
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 73 | {
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 74 | $this->ar_select[] = $val;
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 75 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 76 | }
|
| 77 | return $this;
|
| 78 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 79 |
|
| 80 | // --------------------------------------------------------------------
|
| 81 |
|
| 82 | /**
|
| 83 | * Select Max
|
| 84 | *
|
| 85 | * Generates a SELECT MAX(field) portion of a query
|
| 86 | *
|
| 87 | * @access public
|
| 88 | * @param string the field
|
| 89 | * @param string an alias
|
| 90 | * @return object
|
| 91 | */
|
| 92 | function select_max($select = '', $alias='')
|
| 93 | {
|
| 94 | if (!is_string($select) || $select == '')
|
| 95 | {
|
| 96 | $this->display_error('db_invalid_query');
|
| 97 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 98 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 99 | $alias = ($alias != '') ? $alias : $select;
|
| 100 |
|
| 101 | $sql = 'MAX('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
|
| 102 |
|
| 103 | $this->ar_select[] = $sql;
|
| 104 |
|
| 105 | return $this;
|
| 106 |
|
| 107 | return $this;
|
| 108 | }
|
| 109 |
|
| 110 | // --------------------------------------------------------------------
|
| 111 |
|
| 112 | /**
|
| 113 | * Select Min
|
| 114 | *
|
| 115 | * Generates a SELECT MIN(field) portion of a query
|
| 116 | *
|
| 117 | * @access public
|
| 118 | * @param string the field
|
| 119 | * @param string an alias
|
| 120 | * @return object
|
| 121 | */
|
| 122 | function select_min($select = '', $alias='')
|
| 123 | {
|
| 124 | if (!is_string($select) || $select == '')
|
| 125 | {
|
| 126 | $this->display_error('db_invalid_query');
|
| 127 | }
|
| 128 |
|
| 129 | $alias = ($alias != '') ? $alias : $select;
|
| 130 |
|
| 131 | $sql = 'MIN('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
|
| 132 |
|
| 133 | $this->ar_select[] = $sql;
|
| 134 |
|
| 135 | return $this;
|
| 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 | */
|
| 150 | function select_avg($select = '', $alias='')
|
| 151 | {
|
| 152 | if (!is_string($select) || $select == '')
|
| 153 | {
|
| 154 | $this->display_error('db_invalid_query');
|
| 155 | }
|
| 156 |
|
| 157 | $alias = ($alias != '') ? $alias : $select;
|
| 158 |
|
| 159 | $sql = 'AVG('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
|
| 160 |
|
| 161 | $this->ar_select[] = $sql;
|
| 162 |
|
| 163 | return $this;
|
| 164 | }
|
| 165 |
|
| 166 | // --------------------------------------------------------------------
|
| 167 |
|
| 168 | /**
|
| 169 | * Select Sum
|
| 170 | *
|
| 171 | * Generates a SELECT SUM(field) portion of a query
|
| 172 | *
|
| 173 | * @access public
|
| 174 | * @param string the field
|
| 175 | * @param string an alias
|
| 176 | * @return object
|
| 177 | */
|
| 178 | function select_sum($select = '', $alias='')
|
| 179 | {
|
| 180 | if (!is_string($select) || $select == '')
|
| 181 | {
|
| 182 | $this->display_error('db_invalid_query');
|
| 183 | }
|
| 184 |
|
| 185 | $alias = ($alias != '') ? $alias : $select;
|
| 186 |
|
| 187 | $sql = 'SUM('.$this->_protect_identifiers(trim($select)).') AS '.$this->_protect_identifiers(trim($alias));
|
| 188 |
|
| 189 | $this->ar_select[] = $sql;
|
| 190 |
|
| 191 | return $this;
|
| 192 | }
|
| 193 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 194 | // --------------------------------------------------------------------
|
| 195 |
|
| 196 | /**
|
| 197 | * DISTINCT
|
| 198 | *
|
| 199 | * Sets a flag which tells the query string compiler to add DISTINCT
|
| 200 | *
|
| 201 | * @access public
|
| 202 | * @param bool
|
| 203 | * @return object
|
| 204 | */
|
| 205 | function distinct($val = TRUE)
|
| 206 | {
|
| 207 | $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
|
| 208 | return $this;
|
| 209 | }
|
| 210 |
|
| 211 | // --------------------------------------------------------------------
|
| 212 |
|
| 213 | /**
|
| 214 | * From
|
| 215 | *
|
| 216 | * Generates the FROM portion of the query
|
| 217 | *
|
| 218 | * @access public
|
| 219 | * @param mixed can be a string or array
|
| 220 | * @return object
|
| 221 | */
|
| 222 | function from($from)
|
| 223 | {
|
| 224 | foreach ((array)$from as $val)
|
| 225 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 226 | $this->ar_from[] = $this->_protect_identifiers($this->_track_aliases($val));
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 227 | }
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 228 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 229 | return $this;
|
| 230 | }
|
| 231 |
|
| 232 | // --------------------------------------------------------------------
|
| 233 |
|
| 234 | /**
|
| 235 | * Join
|
| 236 | *
|
| 237 | * Generates the JOIN portion of the query
|
| 238 | *
|
| 239 | * @access public
|
| 240 | * @param string
|
| 241 | * @param string the join condition
|
| 242 | * @param string the type of join
|
| 243 | * @return object
|
| 244 | */
|
| 245 | function join($table, $cond, $type = '')
|
| 246 | {
|
| 247 | if ($type != '')
|
| 248 | {
|
| 249 | $type = strtoupper(trim($type));
|
| 250 |
|
| 251 | if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
|
| 252 | {
|
| 253 | $type = '';
|
| 254 | }
|
| 255 | else
|
| 256 | {
|
| 257 | $type .= ' ';
|
| 258 | }
|
| 259 | }
|
| 260 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 261 | // If a DB prefix is used we might need to add it to the column names
|
| 262 | if ($this->dbprefix)
|
| 263 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 264 | $this->_track_aliases($table);
|
| 265 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 266 | // First we remove any existing prefixes in the condition to avoid duplicates
|
| 267 | $cond = preg_replace('|('.$this->dbprefix.')([\w\.]+)([\W\s]+)|', "$2$3", $cond);
|
| 268 |
|
| 269 | // Next we add the prefixes to the condition
|
| 270 | $cond = preg_replace('|([\w\.]+)([\W\s]+)(.+)|', $this->dbprefix . "$1$2" . $this->dbprefix . "$3", $cond);
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 271 | }
|
| 272 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 273 | $this->ar_join[] = $type.'JOIN '.$this->_protect_identifiers($this->dbprefix.$table, TRUE).' ON '.$cond;
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 274 | return $this;
|
| 275 | }
|
| 276 |
|
| 277 | // --------------------------------------------------------------------
|
| 278 |
|
| 279 | /**
|
| 280 | * Where
|
| 281 | *
|
| 282 | * Generates the WHERE portion of the query. Separates
|
| 283 | * multiple calls with AND
|
| 284 | *
|
| 285 | * @access public
|
| 286 | * @param mixed
|
| 287 | * @param mixed
|
| 288 | * @return object
|
| 289 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 290 | function where($key, $value = NULL, $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 291 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 292 | return $this->_where($key, $value, 'AND ', $escape);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 293 | }
|
| 294 |
|
| 295 | // --------------------------------------------------------------------
|
| 296 |
|
| 297 | /**
|
| 298 | * OR Where
|
| 299 | *
|
| 300 | * Generates the WHERE portion of the query. Separates
|
| 301 | * multiple calls with OR
|
| 302 | *
|
| 303 | * @access public
|
| 304 | * @param mixed
|
| 305 | * @param mixed
|
| 306 | * @return object
|
| 307 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 308 | function or_where($key, $value = NULL, $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 309 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 310 | return $this->_where($key, $value, 'OR ', $escape);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 311 | }
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 312 |
|
| 313 | // --------------------------------------------------------------------
|
| 314 |
|
| 315 | /**
|
| 316 | * orwhere() is an alias of or_where()
|
| 317 | * this function is here for backwards compatibility, as
|
| 318 | * orwhere() has been deprecated
|
| 319 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 320 | function orwhere($key, $value = NULL, $escape = TRUE)
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 321 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 322 | return $this->or_where($key, $value, $escape);
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 323 | }
|
Derek Allard | 67b44ed | 2008-01-12 16:18:02 +0000 | [diff] [blame] | 324 |
|
| 325 | // --------------------------------------------------------------------
|
| 326 |
|
| 327 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 328 | * Where
|
| 329 | *
|
| 330 | * Called by where() or orwhere()
|
| 331 | *
|
| 332 | * @access private
|
| 333 | * @param mixed
|
| 334 | * @param mixed
|
| 335 | * @param string
|
| 336 | * @return object
|
| 337 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 338 | function _where($key, $value = NULL, $type = 'AND ', $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 339 | {
|
| 340 | if ( ! is_array($key))
|
| 341 | {
|
| 342 | $key = array($key => $value);
|
| 343 | }
|
| 344 |
|
| 345 | foreach ($key as $k => $v)
|
| 346 | {
|
| 347 | $prefix = (count($this->ar_where) == 0) ? '' : $type;
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 348 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 349 | if ( ! $this->_has_operator($k) && is_null($key[$k]))
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 350 | {
|
| 351 | // value appears not to have been set, assign the test to IS NULL
|
| 352 | $k .= ' IS NULL';
|
| 353 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 354 |
|
| 355 | if ( ! is_null($v))
|
| 356 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 357 |
|
| 358 | if ($escape === TRUE)
|
| 359 | {
|
| 360 | // exception for "field<=" keys
|
| 361 | if ($this->_has_operator($k))
|
| 362 | {
|
| 363 | $k = preg_replace("/([A-Za-z_0-9]+)/", $this->_protect_identifiers('$1'), $k);
|
| 364 | }
|
| 365 | else
|
| 366 | {
|
| 367 | $k = $this->_protect_identifiers($k);
|
| 368 | }
|
| 369 | }
|
| 370 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 371 | if ( ! $this->_has_operator($k))
|
| 372 | {
|
| 373 | $k .= ' =';
|
| 374 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 375 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 376 | $v = ' '.$this->escape($v);
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 377 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 378 | }
|
| 379 |
|
| 380 | $this->ar_where[] = $prefix.$k.$v;
|
| 381 | }
|
| 382 | return $this;
|
| 383 | }
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 384 |
|
| 385 | // --------------------------------------------------------------------
|
| 386 |
|
| 387 | /**
|
| 388 | * Where_in
|
| 389 | *
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 390 | * Generates a WHERE field IN ('item', 'item') SQL query joined with
|
| 391 | * AND if appropriate
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 392 | *
|
| 393 | * @access public
|
| 394 | * @param string The field to search
|
| 395 | * @param array The values searched on
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 396 |
|
| 397 | * @return object
|
| 398 | */
|
| 399 | function where_in($key = NULL, $values = NULL)
|
| 400 | {
|
| 401 | return $this->_where_in($key, $values);
|
| 402 | }
|
| 403 |
|
| 404 | // --------------------------------------------------------------------
|
| 405 |
|
| 406 | /**
|
| 407 | * Where_in_or
|
| 408 | *
|
| 409 | * Generates a WHERE field IN ('item', 'item') SQL query joined with
|
| 410 | * OR if appropriate
|
| 411 | *
|
| 412 | * @access public
|
| 413 | * @param string The field to search
|
| 414 | * @param array The values searched on
|
| 415 |
|
| 416 | * @return object
|
| 417 | */
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 418 | function or_where_in($key = NULL, $values = NULL)
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 419 | {
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 420 | return $this->_where_in($key, $values, FALSE, 'OR ');
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 421 | }
|
| 422 |
|
| 423 | // --------------------------------------------------------------------
|
| 424 |
|
| 425 | /**
|
| 426 | * Where_not_in
|
| 427 | *
|
| 428 | * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
|
| 429 | * with AND if appropriate
|
| 430 | *
|
| 431 | * @access public
|
| 432 | * @param string The field to search
|
| 433 | * @param array The values searched on
|
| 434 |
|
| 435 | * @return object
|
| 436 | */
|
| 437 | function where_not_in($key = NULL, $values = NULL)
|
| 438 | {
|
| 439 | return $this->_where_in($key, $values, TRUE);
|
| 440 | }
|
| 441 |
|
| 442 | // --------------------------------------------------------------------
|
| 443 |
|
| 444 | /**
|
| 445 | * Where_not_in_or
|
| 446 | *
|
| 447 | * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
|
| 448 | * with OR if appropriate
|
| 449 | *
|
| 450 | * @access public
|
| 451 | * @param string The field to search
|
| 452 | * @param array The values searched on
|
| 453 |
|
| 454 | * @return object
|
| 455 | */
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 456 | function or_where_not_in($key = NULL, $values = NULL)
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 457 | {
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 458 | return $this->_where_in($key, $values, FALSE, 'OR ');
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 459 | }
|
| 460 |
|
| 461 | // --------------------------------------------------------------------
|
| 462 |
|
| 463 | /**
|
| 464 | * Where_in
|
| 465 | *
|
| 466 | * Called by where_in, where_in_or, where_not_in, where_not_in_or
|
| 467 | *
|
| 468 | * @access public
|
| 469 | * @param string The field to search
|
| 470 | * @param array The values searched on
|
| 471 | * @param boolean If the statement whould be IN or NOT IN
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 472 | * @param string
|
| 473 | * @return object
|
| 474 | */
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 475 | function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 476 | {
|
| 477 | if ($key === NULL || !is_array($values))
|
| 478 | {
|
| 479 | return;
|
| 480 | }
|
| 481 |
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 482 | $not = ($not) ? ' NOT ' : '';
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 483 |
|
| 484 | foreach ($values as $value)
|
| 485 | {
|
| 486 | $this->ar_wherein[] = $this->escape($value);
|
| 487 | }
|
| 488 |
|
| 489 | $prefix = (count($this->ar_where) == 0) ? '' : $type;
|
| 490 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 491 | $this->ar_where[] = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 492 |
|
Derek Allard | 8f00021 | 2008-01-18 14:45:59 +0000 | [diff] [blame] | 493 | // reset the array for multiple calls
|
| 494 | $this->ar_wherein = array();
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 495 | return $this;
|
| 496 | }
|
| 497 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 498 | // --------------------------------------------------------------------
|
| 499 |
|
| 500 | /**
|
| 501 | * Like
|
| 502 | *
|
| 503 | * Generates a %LIKE% portion of the query. Separates
|
| 504 | * multiple calls with AND
|
| 505 | *
|
| 506 | * @access public
|
| 507 | * @param mixed
|
| 508 | * @param mixed
|
| 509 | * @return object
|
| 510 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 511 | function like($field, $match = '', $side = 'both')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 512 | {
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 513 | return $this->_like($field, $match, 'AND ', $side);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 514 | }
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 515 |
|
| 516 | // --------------------------------------------------------------------
|
| 517 |
|
| 518 | /**
|
| 519 | * Not Like
|
| 520 | *
|
| 521 | * Generates a NOT LIKE portion of the query. Separates
|
| 522 | * multiple calls with AND
|
| 523 | *
|
| 524 | * @access public
|
| 525 | * @param mixed
|
| 526 | * @param mixed
|
| 527 | * @return object
|
| 528 | */
|
| 529 | function not_like($field, $match = '', $side = 'both')
|
| 530 | {
|
| 531 | return $this->_like($field, $match, 'AND ', $side, ' NOT');
|
| 532 | }
|
| 533 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 534 | // --------------------------------------------------------------------
|
| 535 |
|
| 536 | /**
|
| 537 | * OR Like
|
| 538 | *
|
| 539 | * Generates a %LIKE% portion of the query. Separates
|
| 540 | * multiple calls with OR
|
| 541 | *
|
| 542 | * @access public
|
| 543 | * @param mixed
|
| 544 | * @param mixed
|
| 545 | * @return object
|
| 546 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 547 | function or_like($field, $match = '', $side = 'both')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 548 | {
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 549 | return $this->_like($field, $match, 'OR ', $side);
|
| 550 | }
|
| 551 |
|
| 552 | // --------------------------------------------------------------------
|
| 553 |
|
| 554 | /**
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 555 | * OR Not Like
|
| 556 | *
|
| 557 | * Generates a NOT LIKE portion of the query. Separates
|
| 558 | * multiple calls with OR
|
| 559 | *
|
| 560 | * @access public
|
| 561 | * @param mixed
|
| 562 | * @param mixed
|
| 563 | * @return object
|
| 564 | */
|
| 565 | function or_not_like($field, $match = '', $side = 'both')
|
| 566 | {
|
| 567 | return $this->_like($field, $match, 'OR ', $side, 'NOT ');
|
| 568 | }
|
| 569 |
|
| 570 | // --------------------------------------------------------------------
|
| 571 |
|
| 572 | /**
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 573 | * orlike() is an alias of or_like()
|
| 574 | * this function is here for backwards compatibility, as
|
| 575 | * orlike() has been deprecated
|
| 576 | */
|
| 577 | function orlike($field, $match = '', $side = 'both')
|
| 578 | {
|
| 579 | return $this->orlike($field, $match, $side);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 580 | }
|
| 581 |
|
| 582 | // --------------------------------------------------------------------
|
| 583 |
|
| 584 | /**
|
| 585 | * Like
|
| 586 | *
|
| 587 | * Called by like() or orlike()
|
| 588 | *
|
| 589 | * @access private
|
| 590 | * @param mixed
|
| 591 | * @param mixed
|
| 592 | * @param string
|
| 593 | * @return object
|
| 594 | */
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 595 | function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 596 | {
|
| 597 | if ( ! is_array($field))
|
| 598 | {
|
| 599 | $field = array($field => $match);
|
| 600 | }
|
| 601 |
|
| 602 | foreach ($field as $k => $v)
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 603 | {
|
| 604 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 605 | $k = $this->_protect_identifiers($k);
|
| 606 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 607 | $prefix = (count($this->ar_like) == 0) ? '' : $type;
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 608 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 609 | $v = $this->escape_str($v);
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 610 |
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 611 | if ($side == 'before')
|
| 612 | {
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 613 | $this->ar_like[] = $prefix." $k $not LIKE '%{$v}'";
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 614 | }
|
| 615 | elseif ($side == 'after')
|
| 616 | {
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 617 | $this->ar_like[] = $prefix." $k $not LIKE '{$v}%'";
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 618 | }
|
| 619 | else
|
| 620 | {
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 621 | $this->ar_like[] = $prefix." $k $not LIKE '%{$v}%'";
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 622 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 623 | }
|
| 624 | return $this;
|
| 625 | }
|
| 626 |
|
| 627 | // --------------------------------------------------------------------
|
| 628 |
|
| 629 | /**
|
| 630 | * GROUP BY
|
| 631 | *
|
| 632 | * @access public
|
| 633 | * @param string
|
| 634 | * @return object
|
| 635 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 636 | function group_by($by)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 637 | {
|
| 638 | if (is_string($by))
|
| 639 | {
|
| 640 | $by = explode(',', $by);
|
| 641 | }
|
| 642 |
|
| 643 | foreach ($by as $val)
|
| 644 | {
|
| 645 | $val = trim($val);
|
| 646 |
|
| 647 | if ($val != '')
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 648 | $this->ar_groupby[] = $this->_protect_identifiers($val);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 649 | }
|
| 650 | return $this;
|
| 651 | }
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 652 |
|
| 653 | // --------------------------------------------------------------------
|
| 654 |
|
| 655 | /**
|
| 656 | * groupby() is an alias of group_by()
|
| 657 | * this function is here for backwards compatibility, as
|
| 658 | * groupby() has been deprecated
|
| 659 | */
|
| 660 | function groupby($by)
|
| 661 | {
|
| 662 | return $this->group_by($by);
|
| 663 | }
|
| 664 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 665 | // --------------------------------------------------------------------
|
| 666 |
|
| 667 | /**
|
| 668 | * Sets the HAVING value
|
| 669 | *
|
| 670 | * Separates multiple calls with AND
|
| 671 | *
|
| 672 | * @access public
|
| 673 | * @param string
|
| 674 | * @param string
|
| 675 | * @return object
|
| 676 | */
|
| 677 | function having($key, $value = '')
|
| 678 | {
|
| 679 | return $this->_having($key, $value, 'AND ');
|
| 680 | }
|
| 681 |
|
| 682 | // --------------------------------------------------------------------
|
| 683 |
|
| 684 | /**
|
| 685 | * Sets the OR HAVING value
|
| 686 | *
|
| 687 | * Separates multiple calls with OR
|
| 688 | *
|
| 689 | * @access public
|
| 690 | * @param string
|
| 691 | * @param string
|
| 692 | * @return object
|
| 693 | */
|
| 694 | function orhaving($key, $value = '')
|
| 695 | {
|
| 696 | return $this->_having($key, $value, 'OR ');
|
| 697 | }
|
| 698 |
|
| 699 | // --------------------------------------------------------------------
|
| 700 |
|
| 701 | /**
|
| 702 | * Sets the HAVING values
|
| 703 | *
|
| 704 | * Called by having() or orhaving()
|
| 705 | *
|
| 706 | * @access private
|
| 707 | * @param string
|
| 708 | * @param string
|
| 709 | * @return object
|
| 710 | */
|
| 711 | function _having($key, $value = '', $type = 'AND ')
|
| 712 | {
|
| 713 | if ( ! is_array($key))
|
| 714 | {
|
| 715 | $key = array($key => $value);
|
| 716 | }
|
| 717 |
|
| 718 | foreach ($key as $k => $v)
|
| 719 | {
|
| 720 | $prefix = (count($this->ar_having) == 0) ? '' : $type;
|
| 721 |
|
| 722 | if ($v != '')
|
| 723 | {
|
| 724 | $v = ' '.$this->escape($v);
|
| 725 | }
|
| 726 |
|
| 727 | $this->ar_having[] = $prefix.$k.$v;
|
| 728 | }
|
| 729 | return $this;
|
| 730 | }
|
| 731 |
|
| 732 | // --------------------------------------------------------------------
|
| 733 |
|
| 734 | /**
|
| 735 | * Sets the ORDER BY value
|
| 736 | *
|
| 737 | * @access public
|
| 738 | * @param string
|
| 739 | * @param string direction: asc or desc
|
| 740 | * @return object
|
| 741 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 742 | function order_by($orderby, $direction = '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 743 | {
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 744 | if (strtolower($direction) == 'random')
|
| 745 | {
|
| 746 | $orderby = ''; // Random results want or don't need a field name
|
| 747 | $direction = $this->_random_keyword;
|
| 748 | }
|
| 749 | elseif (trim($direction) != '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 750 | {
|
Derek Allard | 9278249 | 2007-08-10 11:26:01 +0000 | [diff] [blame] | 751 | $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 752 | }
|
| 753 |
|
Derek Allard | 05b3a5d | 2008-01-17 20:25:46 +0000 | [diff] [blame] | 754 | $this->ar_orderby[] = $this->_protect_identifiers($orderby, TRUE).$direction;
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 755 | return $this;
|
| 756 | }
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 757 |
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 758 | // --------------------------------------------------------------------
|
| 759 |
|
| 760 | /**
|
| 761 | * orderby() is an alias of order_by()
|
| 762 | * this function is here for backwards compatibility, as
|
| 763 | * orderby() has been deprecated
|
| 764 | */
|
| 765 | function orderby($orderby, $direction = '')
|
| 766 | {
|
| 767 | return $this->order_by($orderby, $direction);
|
| 768 | }
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 769 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 770 | // --------------------------------------------------------------------
|
| 771 |
|
| 772 | /**
|
| 773 | * Sets the LIMIT value
|
| 774 | *
|
| 775 | * @access public
|
| 776 | * @param integer the limit value
|
| 777 | * @param integer the offset value
|
| 778 | * @return object
|
| 779 | */
|
| 780 | function limit($value, $offset = '')
|
| 781 | {
|
| 782 | $this->ar_limit = $value;
|
| 783 |
|
| 784 | if ($offset != '')
|
| 785 | $this->ar_offset = $offset;
|
| 786 |
|
| 787 | return $this;
|
| 788 | }
|
| 789 |
|
| 790 | // --------------------------------------------------------------------
|
| 791 |
|
| 792 | /**
|
| 793 | * Sets the OFFSET value
|
| 794 | *
|
| 795 | * @access public
|
| 796 | * @param integer the offset value
|
| 797 | * @return object
|
| 798 | */
|
| 799 | function offset($value)
|
| 800 | {
|
| 801 | $this->ar_offset = $value;
|
| 802 | return $this;
|
| 803 | }
|
| 804 |
|
| 805 | // --------------------------------------------------------------------
|
| 806 |
|
| 807 | /**
|
| 808 | * The "set" function. Allows key/value pairs to be set for inserting or updating
|
| 809 | *
|
| 810 | * @access public
|
| 811 | * @param mixed
|
| 812 | * @param string
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 813 | * @param boolean
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 814 | * @return object
|
| 815 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 816 | function set($key, $value = '', $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 817 | {
|
| 818 | $key = $this->_object_to_array($key);
|
| 819 |
|
| 820 | if ( ! is_array($key))
|
| 821 | {
|
| 822 | $key = array($key => $value);
|
| 823 | }
|
| 824 |
|
| 825 | foreach ($key as $k => $v)
|
| 826 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 827 | if ($escape === FALSE)
|
| 828 | {
|
| 829 | $this->ar_set[$this->_protect_identifiers($k)] = $v;
|
| 830 | }
|
| 831 | else
|
| 832 | {
|
| 833 | $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
|
| 834 | }
|
| 835 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 836 | }
|
| 837 |
|
| 838 | return $this;
|
| 839 | }
|
| 840 |
|
| 841 | // --------------------------------------------------------------------
|
| 842 |
|
| 843 | /**
|
| 844 | * Get
|
| 845 | *
|
| 846 | * Compiles the select statement based on the other functions called
|
| 847 | * and runs the query
|
| 848 | *
|
| 849 | * @access public
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 850 | * @param string the table
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 851 | * @param string the limit clause
|
| 852 | * @param string the offset clause
|
| 853 | * @return object
|
| 854 | */
|
| 855 | function get($table = '', $limit = null, $offset = null)
|
| 856 | {
|
| 857 | if ($table != '')
|
| 858 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 859 | $this->_track_aliases($table);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 860 | $this->from($table);
|
| 861 | }
|
| 862 |
|
| 863 | if ( ! is_null($limit))
|
| 864 | {
|
| 865 | $this->limit($limit, $offset);
|
| 866 | }
|
| 867 |
|
| 868 | $sql = $this->_compile_select();
|
| 869 |
|
| 870 | $result = $this->query($sql);
|
| 871 | $this->_reset_select();
|
| 872 | return $result;
|
| 873 | }
|
| 874 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 875 | /**
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 876 | * "Count All Results" query
|
| 877 | *
|
| 878 | * Generates a platform-specific query string that counts all records
|
| 879 | * returned by an Active Record query.
|
| 880 | *
|
| 881 | * @access public
|
| 882 | * @param string
|
| 883 | * @return string
|
| 884 | */
|
| 885 | function count_all_results($table = '')
|
| 886 | {
|
| 887 | if ($table != '')
|
| 888 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 889 | $this->_track_aliases($table);
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 890 | $this->from($table);
|
| 891 | }
|
| 892 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 893 | $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 894 |
|
| 895 | $query = $this->query($sql);
|
| 896 | $this->_reset_select();
|
| 897 |
|
| 898 | if ($query->num_rows() == 0)
|
| 899 | {
|
| 900 | return '0';
|
| 901 | }
|
| 902 |
|
| 903 | $row = $query->row();
|
| 904 | return $row->numrows;
|
| 905 | }
|
| 906 |
|
| 907 | // --------------------------------------------------------------------
|
| 908 |
|
| 909 | /**
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 910 | * Get_Where
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 911 | *
|
| 912 | * Allows the where clause, limit and offset to be added directly
|
| 913 | *
|
| 914 | * @access public
|
| 915 | * @param string the where clause
|
| 916 | * @param string the limit clause
|
| 917 | * @param string the offset clause
|
| 918 | * @return object
|
| 919 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 920 | function get_where($table = '', $where = null, $limit = null, $offset = null)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 921 | {
|
| 922 | if ($table != '')
|
| 923 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 924 | $this->_track_aliases($table);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 925 | $this->from($table);
|
| 926 | }
|
| 927 |
|
| 928 | if ( ! is_null($where))
|
| 929 | {
|
| 930 | $this->where($where);
|
| 931 | }
|
| 932 |
|
| 933 | if ( ! is_null($limit))
|
| 934 | {
|
| 935 | $this->limit($limit, $offset);
|
| 936 | }
|
| 937 |
|
| 938 | $sql = $this->_compile_select();
|
| 939 |
|
| 940 | $result = $this->query($sql);
|
| 941 | $this->_reset_select();
|
| 942 | return $result;
|
| 943 | }
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 944 |
|
| 945 | // --------------------------------------------------------------------
|
| 946 |
|
| 947 | /**
|
| 948 | * getwhere() is an alias of get_where()
|
| 949 | * this function is here for backwards compatibility, as
|
| 950 | * getwhere() has been deprecated
|
| 951 | */
|
| 952 | function getwhere($table = '', $where = null, $limit = null, $offset = null)
|
| 953 | {
|
| 954 | return $this->get_where($table, $where, $limit, $offset);
|
| 955 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 956 |
|
| 957 | // --------------------------------------------------------------------
|
| 958 |
|
| 959 | /**
|
| 960 | * Insert
|
| 961 | *
|
| 962 | * Compiles an insert string and runs the query
|
| 963 | *
|
| 964 | * @access public
|
| 965 | * @param string the table to retrieve the results from
|
| 966 | * @param array an associative array of insert values
|
| 967 | * @return object
|
| 968 | */
|
| 969 | function insert($table = '', $set = NULL)
|
| 970 | {
|
| 971 | if ( ! is_null($set))
|
| 972 | {
|
| 973 | $this->set($set);
|
| 974 | }
|
| 975 |
|
| 976 | if (count($this->ar_set) == 0)
|
| 977 | {
|
| 978 | if ($this->db_debug)
|
| 979 | {
|
| 980 | return $this->display_error('db_must_use_set');
|
| 981 | }
|
| 982 | return FALSE;
|
| 983 | }
|
| 984 |
|
| 985 | if ($table == '')
|
| 986 | {
|
| 987 | if ( ! isset($this->ar_from[0]))
|
| 988 | {
|
| 989 | if ($this->db_debug)
|
| 990 | {
|
| 991 | return $this->display_error('db_must_set_table');
|
| 992 | }
|
| 993 | return FALSE;
|
| 994 | }
|
| 995 |
|
| 996 | $table = $this->ar_from[0];
|
| 997 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 998 |
|
| 999 | $sql = $this->_insert($this->_protect_identifiers($this->dbprefix.$table), array_keys($this->ar_set), array_values($this->ar_set));
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1000 |
|
| 1001 | $this->_reset_write();
|
| 1002 | return $this->query($sql);
|
| 1003 | }
|
| 1004 |
|
| 1005 | // --------------------------------------------------------------------
|
| 1006 |
|
| 1007 | /**
|
| 1008 | * Update
|
| 1009 | *
|
| 1010 | * Compiles an update string and runs the query
|
| 1011 | *
|
| 1012 | * @access public
|
| 1013 | * @param string the table to retrieve the results from
|
| 1014 | * @param array an associative array of update values
|
| 1015 | * @param mixed the where clause
|
| 1016 | * @return object
|
| 1017 | */
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1018 | function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1019 | {
|
| 1020 | if ( ! is_null($set))
|
| 1021 | {
|
| 1022 | $this->set($set);
|
| 1023 | }
|
| 1024 |
|
| 1025 | if (count($this->ar_set) == 0)
|
| 1026 | {
|
| 1027 | if ($this->db_debug)
|
| 1028 | {
|
| 1029 | return $this->display_error('db_must_use_set');
|
| 1030 | }
|
| 1031 | return FALSE;
|
| 1032 | }
|
| 1033 |
|
| 1034 | if ($table == '')
|
| 1035 | {
|
| 1036 | if ( ! isset($this->ar_from[0]))
|
| 1037 | {
|
| 1038 | if ($this->db_debug)
|
| 1039 | {
|
| 1040 | return $this->display_error('db_must_set_table');
|
| 1041 | }
|
| 1042 | return FALSE;
|
| 1043 | }
|
| 1044 |
|
| 1045 | $table = $this->ar_from[0];
|
| 1046 | }
|
| 1047 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1048 | if ($where != NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1049 | {
|
| 1050 | $this->where($where);
|
| 1051 | }
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 1052 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1053 | if ($limit != NULL)
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 1054 | {
|
| 1055 | $this->limit($limit);
|
| 1056 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1057 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1058 | $sql = $this->_update($this->_protect_identifiers($this->dbprefix.$table), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1059 |
|
| 1060 | $this->_reset_write();
|
| 1061 | return $this->query($sql);
|
| 1062 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1063 |
|
| 1064 | // --------------------------------------------------------------------
|
| 1065 |
|
| 1066 | /**
|
| 1067 | * Empty Table
|
| 1068 | *
|
| 1069 | * Compiles a delete string and runs "DELETE FROM table"
|
| 1070 | *
|
| 1071 | * @access public
|
| 1072 | * @param string the table to empty
|
| 1073 | * @return object
|
| 1074 | */
|
| 1075 | function empty_table($table = '')
|
| 1076 | {
|
| 1077 | if ($table == '')
|
| 1078 | {
|
| 1079 | if ( ! isset($this->ar_from[0]))
|
| 1080 | {
|
| 1081 | if ($this->db_debug)
|
| 1082 | {
|
| 1083 | return $this->display_error('db_must_set_table');
|
| 1084 | }
|
| 1085 | return FALSE;
|
| 1086 | }
|
| 1087 |
|
| 1088 | $table = $this->ar_from[0];
|
| 1089 | }
|
| 1090 | else
|
| 1091 | {
|
| 1092 | $table = $this->_protect_identifiers($this->dbprefix.$table);
|
| 1093 | }
|
| 1094 |
|
| 1095 |
|
| 1096 | $sql = $this->_delete($table);
|
| 1097 |
|
| 1098 | $this->_reset_write();
|
| 1099 |
|
| 1100 | return $this->query($sql);
|
| 1101 | }
|
| 1102 |
|
| 1103 | // --------------------------------------------------------------------
|
| 1104 |
|
| 1105 | /**
|
| 1106 | * Truncate
|
| 1107 | *
|
| 1108 | * Compiles a truncate string and runs the query
|
| 1109 | * If the database does not support the truncate() command
|
| 1110 | * This function maps to "DELETE FROM table"
|
| 1111 | *
|
| 1112 | * @access public
|
| 1113 | * @param string the table to truncate
|
| 1114 | * @return object
|
| 1115 | */
|
| 1116 | function truncate($table = '')
|
| 1117 | {
|
| 1118 | if ($table == '')
|
| 1119 | {
|
| 1120 | if ( ! isset($this->ar_from[0]))
|
| 1121 | {
|
| 1122 | if ($this->db_debug)
|
| 1123 | {
|
| 1124 | return $this->display_error('db_must_set_table');
|
| 1125 | }
|
| 1126 | return FALSE;
|
| 1127 | }
|
| 1128 |
|
| 1129 | $table = $this->ar_from[0];
|
| 1130 | }
|
| 1131 | else
|
| 1132 | {
|
| 1133 | $table = $this->_protect_identifiers($this->dbprefix.$table);
|
| 1134 | }
|
| 1135 |
|
| 1136 |
|
| 1137 | $sql = $this->_truncate($table);
|
| 1138 |
|
| 1139 | $this->_reset_write();
|
| 1140 |
|
| 1141 | return $this->query($sql);
|
| 1142 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1143 |
|
| 1144 | // --------------------------------------------------------------------
|
| 1145 |
|
| 1146 | /**
|
| 1147 | * Delete
|
| 1148 | *
|
| 1149 | * Compiles a delete string and runs the query
|
| 1150 | *
|
| 1151 | * @access public
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1152 | * @param mixed the table(s) to delete from. String or array
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1153 | * @param mixed the where clause
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1154 | * @param mixed the limit clause
|
| 1155 | * @param boolean
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1156 | * @return object
|
| 1157 | */
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1158 | function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1159 | {
|
| 1160 | if ($table == '')
|
| 1161 | {
|
| 1162 | if ( ! isset($this->ar_from[0]))
|
| 1163 | {
|
| 1164 | if ($this->db_debug)
|
| 1165 | {
|
| 1166 | return $this->display_error('db_must_set_table');
|
| 1167 | }
|
| 1168 | return FALSE;
|
| 1169 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1170 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1171 | $table = $this->ar_from[0];
|
| 1172 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1173 | elseif (is_array($table))
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1174 | {
|
| 1175 | foreach($table as $single_table)
|
| 1176 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1177 | $this->delete($single_table, $where, $limit, FALSE);
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1178 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1179 |
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1180 | $this->_reset_write();
|
| 1181 | return;
|
| 1182 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1183 | else
|
| 1184 | {
|
| 1185 | $table = $this->_protect_identifiers($this->dbprefix.$table);
|
| 1186 | }
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1187 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1188 | if ($where != '')
|
| 1189 | {
|
| 1190 | $this->where($where);
|
| 1191 | }
|
| 1192 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1193 | if ($limit != NULL)
|
| 1194 | {
|
| 1195 | $this->limit($limit);
|
| 1196 | }
|
| 1197 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1198 | if (count($this->ar_where) == 0 && count($this->ar_like) == 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_del_must_use_where');
|
| 1203 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1204 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1205 | return FALSE;
|
| 1206 | }
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1207 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1208 | $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] | 1209 |
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1210 | if ($reset_data)
|
| 1211 | {
|
| 1212 | $this->_reset_write();
|
| 1213 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1214 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1215 | return $this->query($sql);
|
| 1216 | }
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 1217 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1218 | // --------------------------------------------------------------------
|
| 1219 |
|
| 1220 | /**
|
| 1221 | * Use Table - DEPRECATED
|
| 1222 | *
|
| 1223 | * @deprecated use $this->db->from instead
|
| 1224 | */
|
| 1225 | function use_table($table)
|
| 1226 | {
|
| 1227 | return $this->from($table);
|
| 1228 | return $this;
|
| 1229 | }
|
| 1230 |
|
| 1231 | // --------------------------------------------------------------------
|
| 1232 |
|
| 1233 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1234 | * Tests whether the string has an SQL operator
|
| 1235 | *
|
| 1236 | * @access private
|
| 1237 | * @param string
|
| 1238 | * @return bool
|
| 1239 | */
|
| 1240 | function _has_operator($str)
|
| 1241 | {
|
| 1242 | $str = trim($str);
|
| 1243 | if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
|
| 1244 | {
|
| 1245 | return FALSE;
|
| 1246 | }
|
| 1247 |
|
| 1248 | return TRUE;
|
| 1249 | }
|
| 1250 |
|
| 1251 | // --------------------------------------------------------------------
|
| 1252 |
|
| 1253 | /**
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1254 | * Track Aliases
|
| 1255 | *
|
| 1256 | * Used to track SQL statements written with aliased tables.
|
| 1257 | *
|
| 1258 | * @access private
|
| 1259 | * @param string The table to inspect
|
| 1260 | * @return string
|
| 1261 | */
|
| 1262 | function _track_aliases($table)
|
| 1263 | {
|
| 1264 | // if a table alias is used we can recognize it by a space
|
| 1265 | if (strpos($table, " ") !== FALSE)
|
| 1266 | {
|
| 1267 | // if the alias is written with the AS keyowrd, get it out
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1268 | $table = preg_replace('/ AS /i', ' ', $table);
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1269 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1270 | $this->ar_aliased_tables[] = trim(strrchr($table, " "));
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1271 | }
|
| 1272 |
|
| 1273 | return $this->dbprefix.$table;
|
| 1274 | }
|
| 1275 |
|
| 1276 | // --------------------------------------------------------------------
|
| 1277 |
|
| 1278 | /**
|
| 1279 | * Filter Table Aliases
|
| 1280 | *
|
| 1281 | * Intelligently removes database prefixes from aliased tables
|
| 1282 | *
|
| 1283 | * @access private
|
| 1284 | * @param array An array of compiled SQL
|
| 1285 | * @return array Cleaned up statement with aliases accounted for
|
| 1286 | */
|
| 1287 | function _filter_table_aliases($statements)
|
| 1288 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1289 | $statements_without_aliases = array();
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1290 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1291 | foreach ($statements as $k => $v)
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1292 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1293 | foreach ($this->ar_aliased_tables as $table)
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1294 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1295 | $statement = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $v); // makes `table.field`
|
| 1296 | $statement = str_replace(array($this->dbprefix.$table, '.'), array($table, $this->_protect_identifiers('.')), $statement);
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1297 | }
|
| 1298 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1299 | $statements[$k] = $statement;
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1300 | }
|
| 1301 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1302 | return $statements;
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1303 | }
|
| 1304 |
|
| 1305 | // --------------------------------------------------------------------
|
| 1306 |
|
| 1307 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1308 | * Compile the SELECT statement
|
| 1309 | *
|
| 1310 | * Generates a query string based on which functions were used.
|
| 1311 | * Should not be called directly. The get() function calls it.
|
| 1312 | *
|
| 1313 | * @access private
|
| 1314 | * @return string
|
| 1315 | */
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1316 | function _compile_select($select_override = FALSE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1317 | {
|
| 1318 | $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
|
| 1319 |
|
| 1320 | $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->ar_select);
|
| 1321 |
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1322 | if ($select_override !== FALSE)
|
| 1323 | {
|
| 1324 | $sql = $select_override;
|
| 1325 | }
|
| 1326 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1327 | if (count($this->ar_from) > 0)
|
| 1328 | {
|
| 1329 | $sql .= "\nFROM ";
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 1330 | $sql .= '(' . implode(', ', $this->ar_from) . ')';
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1331 | }
|
| 1332 |
|
| 1333 | if (count($this->ar_join) > 0)
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1334 | {
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1335 | $sql .= "\n";
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1336 |
|
| 1337 | // special consideration for table aliases
|
| 1338 | if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
|
| 1339 | {
|
| 1340 | $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
|
| 1341 | }
|
| 1342 | else
|
| 1343 | {
|
| 1344 | $sql .= implode("\n", $this->ar_join);
|
| 1345 | }
|
| 1346 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1347 | }
|
| 1348 |
|
| 1349 | if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
|
| 1350 | {
|
| 1351 | $sql .= "\nWHERE ";
|
| 1352 | }
|
| 1353 |
|
| 1354 | $sql .= implode("\n", $this->ar_where);
|
| 1355 |
|
| 1356 | if (count($this->ar_like) > 0)
|
| 1357 | {
|
| 1358 | if (count($this->ar_where) > 0)
|
| 1359 | {
|
| 1360 | $sql .= " AND ";
|
| 1361 | }
|
| 1362 |
|
| 1363 | $sql .= implode("\n", $this->ar_like);
|
| 1364 | }
|
| 1365 |
|
| 1366 | if (count($this->ar_groupby) > 0)
|
| 1367 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1368 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1369 | $sql .= "\nGROUP BY ";
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1370 |
|
| 1371 | // special consideration for table aliases
|
| 1372 | if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
|
| 1373 | {
|
| 1374 | $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
|
| 1375 | }
|
| 1376 | else
|
| 1377 | {
|
| 1378 | $sql .= implode(', ', $this->ar_groupby);
|
| 1379 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1380 | }
|
| 1381 |
|
| 1382 | if (count($this->ar_having) > 0)
|
| 1383 | {
|
| 1384 | $sql .= "\nHAVING ";
|
| 1385 | $sql .= implode("\n", $this->ar_having);
|
| 1386 | }
|
| 1387 |
|
| 1388 | if (count($this->ar_orderby) > 0)
|
| 1389 | {
|
| 1390 | $sql .= "\nORDER BY ";
|
| 1391 | $sql .= implode(', ', $this->ar_orderby);
|
| 1392 |
|
| 1393 | if ($this->ar_order !== FALSE)
|
| 1394 | {
|
| 1395 | $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
|
| 1396 | }
|
| 1397 | }
|
| 1398 |
|
| 1399 | if (is_numeric($this->ar_limit))
|
| 1400 | {
|
| 1401 | $sql .= "\n";
|
| 1402 | $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
|
| 1403 | }
|
| 1404 |
|
| 1405 | return $sql;
|
| 1406 | }
|
| 1407 |
|
| 1408 | // --------------------------------------------------------------------
|
| 1409 |
|
| 1410 | /**
|
| 1411 | * Object to Array
|
| 1412 | *
|
| 1413 | * Takes an object as input and converts the class variables to array key/vals
|
| 1414 | *
|
| 1415 | * @access public
|
| 1416 | * @param object
|
| 1417 | * @return array
|
| 1418 | */
|
| 1419 | function _object_to_array($object)
|
| 1420 | {
|
| 1421 | if ( ! is_object($object))
|
| 1422 | {
|
| 1423 | return $object;
|
| 1424 | }
|
| 1425 |
|
| 1426 | $array = array();
|
| 1427 | foreach (get_object_vars($object) as $key => $val)
|
| 1428 | {
|
Derek Allard | 848b776 | 2007-12-31 16:43:05 +0000 | [diff] [blame] | 1429 | // There are some built in keys we need to ignore for this conversion
|
| 1430 | if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
|
| 1431 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1432 | {
|
| 1433 | $array[$key] = $val;
|
| 1434 | }
|
| 1435 | }
|
| 1436 |
|
| 1437 | return $array;
|
| 1438 | }
|
| 1439 |
|
| 1440 | // --------------------------------------------------------------------
|
| 1441 |
|
| 1442 | /**
|
| 1443 | * Resets the active record values. Called by the get() function
|
| 1444 | *
|
| 1445 | * @access private
|
| 1446 | * @return void
|
| 1447 | */
|
| 1448 | function _reset_select()
|
| 1449 | {
|
| 1450 | $this->ar_select = array();
|
| 1451 | $this->ar_distinct = FALSE;
|
| 1452 | $this->ar_from = array();
|
| 1453 | $this->ar_join = array();
|
| 1454 | $this->ar_where = array();
|
| 1455 | $this->ar_like = array();
|
| 1456 | $this->ar_groupby = array();
|
| 1457 | $this->ar_having = array();
|
| 1458 | $this->ar_limit = FALSE;
|
| 1459 | $this->ar_offset = FALSE;
|
| 1460 | $this->ar_order = FALSE;
|
| 1461 | $this->ar_orderby = array();
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 1462 | $this->ar_wherein = array();
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1463 | $this->ar_aliased_tables = array();
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1464 | }
|
| 1465 |
|
| 1466 | // --------------------------------------------------------------------
|
| 1467 |
|
| 1468 | /**
|
| 1469 | * Resets the active record "write" values.
|
| 1470 | *
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1471 | * Called by the insert() update() and delete() functions
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1472 | *
|
| 1473 | * @access private
|
| 1474 | * @return void
|
| 1475 | */
|
| 1476 | function _reset_write()
|
| 1477 | {
|
| 1478 | $this->ar_set = array();
|
| 1479 | $this->ar_from = array();
|
| 1480 | $this->ar_where = array();
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1481 | $this->ar_like = array();
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 1482 | $this->ar_limit = FALSE;
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1483 | $this->ar_order = FALSE;
|
| 1484 | $this->ar_orderby = array();
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1485 | }
|
| 1486 |
|
| 1487 | }
|
| 1488 |
|
admin | ac94f38 | 2006-09-24 20:28:12 +0000 | [diff] [blame] | 1489 | ?> |