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
|
| 8 | * @author Rick Ellis
|
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
|
| 26 | * @author Rick Ellis
|
| 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 |
|
| 493 | return $this;
|
| 494 | }
|
| 495 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 496 | // --------------------------------------------------------------------
|
| 497 |
|
| 498 | /**
|
| 499 | * Like
|
| 500 | *
|
| 501 | * Generates a %LIKE% portion of the query. Separates
|
| 502 | * multiple calls with AND
|
| 503 | *
|
| 504 | * @access public
|
| 505 | * @param mixed
|
| 506 | * @param mixed
|
| 507 | * @return object
|
| 508 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 509 | function like($field, $match = '', $side = 'both')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 510 | {
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 511 | return $this->_like($field, $match, 'AND ', $side);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 512 | }
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 513 |
|
| 514 | // --------------------------------------------------------------------
|
| 515 |
|
| 516 | /**
|
| 517 | * Not Like
|
| 518 | *
|
| 519 | * Generates a NOT LIKE portion of the query. Separates
|
| 520 | * multiple calls with AND
|
| 521 | *
|
| 522 | * @access public
|
| 523 | * @param mixed
|
| 524 | * @param mixed
|
| 525 | * @return object
|
| 526 | */
|
| 527 | function not_like($field, $match = '', $side = 'both')
|
| 528 | {
|
| 529 | return $this->_like($field, $match, 'AND ', $side, ' NOT');
|
| 530 | }
|
| 531 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 532 | // --------------------------------------------------------------------
|
| 533 |
|
| 534 | /**
|
| 535 | * OR Like
|
| 536 | *
|
| 537 | * Generates a %LIKE% portion of the query. Separates
|
| 538 | * multiple calls with OR
|
| 539 | *
|
| 540 | * @access public
|
| 541 | * @param mixed
|
| 542 | * @param mixed
|
| 543 | * @return object
|
| 544 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 545 | function or_like($field, $match = '', $side = 'both')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 546 | {
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 547 | return $this->_like($field, $match, 'OR ', $side);
|
| 548 | }
|
| 549 |
|
| 550 | // --------------------------------------------------------------------
|
| 551 |
|
| 552 | /**
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 553 | * OR Not Like
|
| 554 | *
|
| 555 | * Generates a NOT LIKE portion of the query. Separates
|
| 556 | * multiple calls with OR
|
| 557 | *
|
| 558 | * @access public
|
| 559 | * @param mixed
|
| 560 | * @param mixed
|
| 561 | * @return object
|
| 562 | */
|
| 563 | function or_not_like($field, $match = '', $side = 'both')
|
| 564 | {
|
| 565 | return $this->_like($field, $match, 'OR ', $side, 'NOT ');
|
| 566 | }
|
| 567 |
|
| 568 | // --------------------------------------------------------------------
|
| 569 |
|
| 570 | /**
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 571 | * orlike() is an alias of or_like()
|
| 572 | * this function is here for backwards compatibility, as
|
| 573 | * orlike() has been deprecated
|
| 574 | */
|
| 575 | function orlike($field, $match = '', $side = 'both')
|
| 576 | {
|
| 577 | return $this->orlike($field, $match, $side);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 578 | }
|
| 579 |
|
| 580 | // --------------------------------------------------------------------
|
| 581 |
|
| 582 | /**
|
| 583 | * Like
|
| 584 | *
|
| 585 | * Called by like() or orlike()
|
| 586 | *
|
| 587 | * @access private
|
| 588 | * @param mixed
|
| 589 | * @param mixed
|
| 590 | * @param string
|
| 591 | * @return object
|
| 592 | */
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 593 | function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 594 | {
|
| 595 | if ( ! is_array($field))
|
| 596 | {
|
| 597 | $field = array($field => $match);
|
| 598 | }
|
| 599 |
|
| 600 | foreach ($field as $k => $v)
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 601 | {
|
| 602 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 603 | $k = $this->_protect_identifiers($k);
|
| 604 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 605 | $prefix = (count($this->ar_like) == 0) ? '' : $type;
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 606 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 607 | $v = $this->escape_str($v);
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 608 |
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 609 | if ($side == 'before')
|
| 610 | {
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 611 | $this->ar_like[] = $prefix." $k $not LIKE '%{$v}'";
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 612 | }
|
| 613 | elseif ($side == 'after')
|
| 614 | {
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 615 | $this->ar_like[] = $prefix." $k $not LIKE '{$v}%'";
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 616 | }
|
| 617 | else
|
| 618 | {
|
Derek Allard | e54e3d2 | 2007-12-19 15:53:44 +0000 | [diff] [blame] | 619 | $this->ar_like[] = $prefix." $k $not LIKE '%{$v}%'";
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 620 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 621 | }
|
| 622 | return $this;
|
| 623 | }
|
| 624 |
|
| 625 | // --------------------------------------------------------------------
|
| 626 |
|
| 627 | /**
|
| 628 | * GROUP BY
|
| 629 | *
|
| 630 | * @access public
|
| 631 | * @param string
|
| 632 | * @return object
|
| 633 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 634 | function group_by($by)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 635 | {
|
| 636 | if (is_string($by))
|
| 637 | {
|
| 638 | $by = explode(',', $by);
|
| 639 | }
|
| 640 |
|
| 641 | foreach ($by as $val)
|
| 642 | {
|
| 643 | $val = trim($val);
|
| 644 |
|
| 645 | if ($val != '')
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 646 | $this->ar_groupby[] = $this->_protect_identifiers($val);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 647 | }
|
| 648 | return $this;
|
| 649 | }
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 650 |
|
| 651 | // --------------------------------------------------------------------
|
| 652 |
|
| 653 | /**
|
| 654 | * groupby() is an alias of group_by()
|
| 655 | * this function is here for backwards compatibility, as
|
| 656 | * groupby() has been deprecated
|
| 657 | */
|
| 658 | function groupby($by)
|
| 659 | {
|
| 660 | return $this->group_by($by);
|
| 661 | }
|
| 662 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 663 | // --------------------------------------------------------------------
|
| 664 |
|
| 665 | /**
|
| 666 | * Sets the HAVING value
|
| 667 | *
|
| 668 | * Separates multiple calls with AND
|
| 669 | *
|
| 670 | * @access public
|
| 671 | * @param string
|
| 672 | * @param string
|
| 673 | * @return object
|
| 674 | */
|
| 675 | function having($key, $value = '')
|
| 676 | {
|
| 677 | return $this->_having($key, $value, 'AND ');
|
| 678 | }
|
| 679 |
|
| 680 | // --------------------------------------------------------------------
|
| 681 |
|
| 682 | /**
|
| 683 | * Sets the OR HAVING value
|
| 684 | *
|
| 685 | * Separates multiple calls with OR
|
| 686 | *
|
| 687 | * @access public
|
| 688 | * @param string
|
| 689 | * @param string
|
| 690 | * @return object
|
| 691 | */
|
| 692 | function orhaving($key, $value = '')
|
| 693 | {
|
| 694 | return $this->_having($key, $value, 'OR ');
|
| 695 | }
|
| 696 |
|
| 697 | // --------------------------------------------------------------------
|
| 698 |
|
| 699 | /**
|
| 700 | * Sets the HAVING values
|
| 701 | *
|
| 702 | * Called by having() or orhaving()
|
| 703 | *
|
| 704 | * @access private
|
| 705 | * @param string
|
| 706 | * @param string
|
| 707 | * @return object
|
| 708 | */
|
| 709 | function _having($key, $value = '', $type = 'AND ')
|
| 710 | {
|
| 711 | if ( ! is_array($key))
|
| 712 | {
|
| 713 | $key = array($key => $value);
|
| 714 | }
|
| 715 |
|
| 716 | foreach ($key as $k => $v)
|
| 717 | {
|
| 718 | $prefix = (count($this->ar_having) == 0) ? '' : $type;
|
| 719 |
|
| 720 | if ($v != '')
|
| 721 | {
|
| 722 | $v = ' '.$this->escape($v);
|
| 723 | }
|
| 724 |
|
| 725 | $this->ar_having[] = $prefix.$k.$v;
|
| 726 | }
|
| 727 | return $this;
|
| 728 | }
|
| 729 |
|
| 730 | // --------------------------------------------------------------------
|
| 731 |
|
| 732 | /**
|
| 733 | * Sets the ORDER BY value
|
| 734 | *
|
| 735 | * @access public
|
| 736 | * @param string
|
| 737 | * @param string direction: asc or desc
|
| 738 | * @return object
|
| 739 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 740 | function order_by($orderby, $direction = '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 741 | {
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 742 | if (strtolower($direction) == 'random')
|
| 743 | {
|
| 744 | $orderby = ''; // Random results want or don't need a field name
|
| 745 | $direction = $this->_random_keyword;
|
| 746 | }
|
| 747 | elseif (trim($direction) != '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 748 | {
|
Derek Allard | 9278249 | 2007-08-10 11:26:01 +0000 | [diff] [blame] | 749 | $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 750 | }
|
| 751 |
|
Derek Allard | 05b3a5d | 2008-01-17 20:25:46 +0000 | [diff] [blame^] | 752 | $this->ar_orderby[] = $this->_protect_identifiers($orderby, TRUE).$direction;
|
| 753 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 754 | return $this;
|
| 755 | }
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 756 |
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 757 | // --------------------------------------------------------------------
|
| 758 |
|
| 759 | /**
|
| 760 | * orderby() is an alias of order_by()
|
| 761 | * this function is here for backwards compatibility, as
|
| 762 | * orderby() has been deprecated
|
| 763 | */
|
| 764 | function orderby($orderby, $direction = '')
|
| 765 | {
|
| 766 | return $this->order_by($orderby, $direction);
|
| 767 | }
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 768 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 769 | // --------------------------------------------------------------------
|
| 770 |
|
| 771 | /**
|
| 772 | * Sets the LIMIT value
|
| 773 | *
|
| 774 | * @access public
|
| 775 | * @param integer the limit value
|
| 776 | * @param integer the offset value
|
| 777 | * @return object
|
| 778 | */
|
| 779 | function limit($value, $offset = '')
|
| 780 | {
|
| 781 | $this->ar_limit = $value;
|
| 782 |
|
| 783 | if ($offset != '')
|
| 784 | $this->ar_offset = $offset;
|
| 785 |
|
| 786 | return $this;
|
| 787 | }
|
| 788 |
|
| 789 | // --------------------------------------------------------------------
|
| 790 |
|
| 791 | /**
|
| 792 | * Sets the OFFSET value
|
| 793 | *
|
| 794 | * @access public
|
| 795 | * @param integer the offset value
|
| 796 | * @return object
|
| 797 | */
|
| 798 | function offset($value)
|
| 799 | {
|
| 800 | $this->ar_offset = $value;
|
| 801 | return $this;
|
| 802 | }
|
| 803 |
|
| 804 | // --------------------------------------------------------------------
|
| 805 |
|
| 806 | /**
|
| 807 | * The "set" function. Allows key/value pairs to be set for inserting or updating
|
| 808 | *
|
| 809 | * @access public
|
| 810 | * @param mixed
|
| 811 | * @param string
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 812 | * @param boolean
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 813 | * @return object
|
| 814 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 815 | function set($key, $value = '', $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 816 | {
|
| 817 | $key = $this->_object_to_array($key);
|
| 818 |
|
| 819 | if ( ! is_array($key))
|
| 820 | {
|
| 821 | $key = array($key => $value);
|
| 822 | }
|
| 823 |
|
| 824 | foreach ($key as $k => $v)
|
| 825 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 826 | if ($escape === FALSE)
|
| 827 | {
|
| 828 | $this->ar_set[$this->_protect_identifiers($k)] = $v;
|
| 829 | }
|
| 830 | else
|
| 831 | {
|
| 832 | $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
|
| 833 | }
|
| 834 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 835 | }
|
| 836 |
|
| 837 | return $this;
|
| 838 | }
|
| 839 |
|
| 840 | // --------------------------------------------------------------------
|
| 841 |
|
| 842 | /**
|
| 843 | * Get
|
| 844 | *
|
| 845 | * Compiles the select statement based on the other functions called
|
| 846 | * and runs the query
|
| 847 | *
|
| 848 | * @access public
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 849 | * @param string the table
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 850 | * @param string the limit clause
|
| 851 | * @param string the offset clause
|
| 852 | * @return object
|
| 853 | */
|
| 854 | function get($table = '', $limit = null, $offset = null)
|
| 855 | {
|
| 856 | if ($table != '')
|
| 857 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 858 | $this->_track_aliases($table);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 859 | $this->from($table);
|
| 860 | }
|
| 861 |
|
| 862 | if ( ! is_null($limit))
|
| 863 | {
|
| 864 | $this->limit($limit, $offset);
|
| 865 | }
|
| 866 |
|
| 867 | $sql = $this->_compile_select();
|
| 868 |
|
| 869 | $result = $this->query($sql);
|
| 870 | $this->_reset_select();
|
| 871 | return $result;
|
| 872 | }
|
| 873 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 874 | /**
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 875 | * "Count All Results" query
|
| 876 | *
|
| 877 | * Generates a platform-specific query string that counts all records
|
| 878 | * returned by an Active Record query.
|
| 879 | *
|
| 880 | * @access public
|
| 881 | * @param string
|
| 882 | * @return string
|
| 883 | */
|
| 884 | function count_all_results($table = '')
|
| 885 | {
|
| 886 | if ($table != '')
|
| 887 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 888 | $this->_track_aliases($table);
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 889 | $this->from($table);
|
| 890 | }
|
| 891 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 892 | $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 893 |
|
| 894 | $query = $this->query($sql);
|
| 895 | $this->_reset_select();
|
| 896 |
|
| 897 | if ($query->num_rows() == 0)
|
| 898 | {
|
| 899 | return '0';
|
| 900 | }
|
| 901 |
|
| 902 | $row = $query->row();
|
| 903 | return $row->numrows;
|
| 904 | }
|
| 905 |
|
| 906 | // --------------------------------------------------------------------
|
| 907 |
|
| 908 | /**
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 909 | * Get_Where
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 910 | *
|
| 911 | * Allows the where clause, limit and offset to be added directly
|
| 912 | *
|
| 913 | * @access public
|
| 914 | * @param string the where clause
|
| 915 | * @param string the limit clause
|
| 916 | * @param string the offset clause
|
| 917 | * @return object
|
| 918 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 919 | function get_where($table = '', $where = null, $limit = null, $offset = null)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 920 | {
|
| 921 | if ($table != '')
|
| 922 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 923 | $this->_track_aliases($table);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 924 | $this->from($table);
|
| 925 | }
|
| 926 |
|
| 927 | if ( ! is_null($where))
|
| 928 | {
|
| 929 | $this->where($where);
|
| 930 | }
|
| 931 |
|
| 932 | if ( ! is_null($limit))
|
| 933 | {
|
| 934 | $this->limit($limit, $offset);
|
| 935 | }
|
| 936 |
|
| 937 | $sql = $this->_compile_select();
|
| 938 |
|
| 939 | $result = $this->query($sql);
|
| 940 | $this->_reset_select();
|
| 941 | return $result;
|
| 942 | }
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 943 |
|
| 944 | // --------------------------------------------------------------------
|
| 945 |
|
| 946 | /**
|
| 947 | * getwhere() is an alias of get_where()
|
| 948 | * this function is here for backwards compatibility, as
|
| 949 | * getwhere() has been deprecated
|
| 950 | */
|
| 951 | function getwhere($table = '', $where = null, $limit = null, $offset = null)
|
| 952 | {
|
| 953 | return $this->get_where($table, $where, $limit, $offset);
|
| 954 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 955 |
|
| 956 | // --------------------------------------------------------------------
|
| 957 |
|
| 958 | /**
|
| 959 | * Insert
|
| 960 | *
|
| 961 | * Compiles an insert string and runs the query
|
| 962 | *
|
| 963 | * @access public
|
| 964 | * @param string the table to retrieve the results from
|
| 965 | * @param array an associative array of insert values
|
| 966 | * @return object
|
| 967 | */
|
| 968 | function insert($table = '', $set = NULL)
|
| 969 | {
|
| 970 | if ( ! is_null($set))
|
| 971 | {
|
| 972 | $this->set($set);
|
| 973 | }
|
| 974 |
|
| 975 | if (count($this->ar_set) == 0)
|
| 976 | {
|
| 977 | if ($this->db_debug)
|
| 978 | {
|
| 979 | return $this->display_error('db_must_use_set');
|
| 980 | }
|
| 981 | return FALSE;
|
| 982 | }
|
| 983 |
|
| 984 | if ($table == '')
|
| 985 | {
|
| 986 | if ( ! isset($this->ar_from[0]))
|
| 987 | {
|
| 988 | if ($this->db_debug)
|
| 989 | {
|
| 990 | return $this->display_error('db_must_set_table');
|
| 991 | }
|
| 992 | return FALSE;
|
| 993 | }
|
| 994 |
|
| 995 | $table = $this->ar_from[0];
|
| 996 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 997 |
|
| 998 | $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] | 999 |
|
| 1000 | $this->_reset_write();
|
| 1001 | return $this->query($sql);
|
| 1002 | }
|
| 1003 |
|
| 1004 | // --------------------------------------------------------------------
|
| 1005 |
|
| 1006 | /**
|
| 1007 | * Update
|
| 1008 | *
|
| 1009 | * Compiles an update string and runs the query
|
| 1010 | *
|
| 1011 | * @access public
|
| 1012 | * @param string the table to retrieve the results from
|
| 1013 | * @param array an associative array of update values
|
| 1014 | * @param mixed the where clause
|
| 1015 | * @return object
|
| 1016 | */
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1017 | function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1018 | {
|
| 1019 | if ( ! is_null($set))
|
| 1020 | {
|
| 1021 | $this->set($set);
|
| 1022 | }
|
| 1023 |
|
| 1024 | if (count($this->ar_set) == 0)
|
| 1025 | {
|
| 1026 | if ($this->db_debug)
|
| 1027 | {
|
| 1028 | return $this->display_error('db_must_use_set');
|
| 1029 | }
|
| 1030 | return FALSE;
|
| 1031 | }
|
| 1032 |
|
| 1033 | if ($table == '')
|
| 1034 | {
|
| 1035 | if ( ! isset($this->ar_from[0]))
|
| 1036 | {
|
| 1037 | if ($this->db_debug)
|
| 1038 | {
|
| 1039 | return $this->display_error('db_must_set_table');
|
| 1040 | }
|
| 1041 | return FALSE;
|
| 1042 | }
|
| 1043 |
|
| 1044 | $table = $this->ar_from[0];
|
| 1045 | }
|
| 1046 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1047 | if ($where != NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1048 | {
|
| 1049 | $this->where($where);
|
| 1050 | }
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 1051 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1052 | if ($limit != NULL)
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 1053 | {
|
| 1054 | $this->limit($limit);
|
| 1055 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1056 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1057 | $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] | 1058 |
|
| 1059 | $this->_reset_write();
|
| 1060 | return $this->query($sql);
|
| 1061 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1062 |
|
| 1063 | // --------------------------------------------------------------------
|
| 1064 |
|
| 1065 | /**
|
| 1066 | * Empty Table
|
| 1067 | *
|
| 1068 | * Compiles a delete string and runs "DELETE FROM table"
|
| 1069 | *
|
| 1070 | * @access public
|
| 1071 | * @param string the table to empty
|
| 1072 | * @return object
|
| 1073 | */
|
| 1074 | function empty_table($table = '')
|
| 1075 | {
|
| 1076 | if ($table == '')
|
| 1077 | {
|
| 1078 | if ( ! isset($this->ar_from[0]))
|
| 1079 | {
|
| 1080 | if ($this->db_debug)
|
| 1081 | {
|
| 1082 | return $this->display_error('db_must_set_table');
|
| 1083 | }
|
| 1084 | return FALSE;
|
| 1085 | }
|
| 1086 |
|
| 1087 | $table = $this->ar_from[0];
|
| 1088 | }
|
| 1089 | else
|
| 1090 | {
|
| 1091 | $table = $this->_protect_identifiers($this->dbprefix.$table);
|
| 1092 | }
|
| 1093 |
|
| 1094 |
|
| 1095 | $sql = $this->_delete($table);
|
| 1096 |
|
| 1097 | $this->_reset_write();
|
| 1098 |
|
| 1099 | return $this->query($sql);
|
| 1100 | }
|
| 1101 |
|
| 1102 | // --------------------------------------------------------------------
|
| 1103 |
|
| 1104 | /**
|
| 1105 | * Truncate
|
| 1106 | *
|
| 1107 | * Compiles a truncate string and runs the query
|
| 1108 | * If the database does not support the truncate() command
|
| 1109 | * This function maps to "DELETE FROM table"
|
| 1110 | *
|
| 1111 | * @access public
|
| 1112 | * @param string the table to truncate
|
| 1113 | * @return object
|
| 1114 | */
|
| 1115 | function truncate($table = '')
|
| 1116 | {
|
| 1117 | if ($table == '')
|
| 1118 | {
|
| 1119 | if ( ! isset($this->ar_from[0]))
|
| 1120 | {
|
| 1121 | if ($this->db_debug)
|
| 1122 | {
|
| 1123 | return $this->display_error('db_must_set_table');
|
| 1124 | }
|
| 1125 | return FALSE;
|
| 1126 | }
|
| 1127 |
|
| 1128 | $table = $this->ar_from[0];
|
| 1129 | }
|
| 1130 | else
|
| 1131 | {
|
| 1132 | $table = $this->_protect_identifiers($this->dbprefix.$table);
|
| 1133 | }
|
| 1134 |
|
| 1135 |
|
| 1136 | $sql = $this->_truncate($table);
|
| 1137 |
|
| 1138 | $this->_reset_write();
|
| 1139 |
|
| 1140 | return $this->query($sql);
|
| 1141 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1142 |
|
| 1143 | // --------------------------------------------------------------------
|
| 1144 |
|
| 1145 | /**
|
| 1146 | * Delete
|
| 1147 | *
|
| 1148 | * Compiles a delete string and runs the query
|
| 1149 | *
|
| 1150 | * @access public
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1151 | * @param mixed the table(s) to delete from. String or array
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1152 | * @param mixed the where clause
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1153 | * @param mixed the limit clause
|
| 1154 | * @param boolean
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1155 | * @return object
|
| 1156 | */
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1157 | function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1158 | {
|
| 1159 | if ($table == '')
|
| 1160 | {
|
| 1161 | if ( ! isset($this->ar_from[0]))
|
| 1162 | {
|
| 1163 | if ($this->db_debug)
|
| 1164 | {
|
| 1165 | return $this->display_error('db_must_set_table');
|
| 1166 | }
|
| 1167 | return FALSE;
|
| 1168 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1169 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1170 | $table = $this->ar_from[0];
|
| 1171 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1172 | elseif (is_array($table))
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1173 | {
|
| 1174 | foreach($table as $single_table)
|
| 1175 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1176 | $this->delete($single_table, $where, $limit, FALSE);
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1177 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1178 |
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1179 | $this->_reset_write();
|
| 1180 | return;
|
| 1181 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1182 | else
|
| 1183 | {
|
| 1184 | $table = $this->_protect_identifiers($this->dbprefix.$table);
|
| 1185 | }
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1186 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1187 | if ($where != '')
|
| 1188 | {
|
| 1189 | $this->where($where);
|
| 1190 | }
|
| 1191 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1192 | if ($limit != NULL)
|
| 1193 | {
|
| 1194 | $this->limit($limit);
|
| 1195 | }
|
| 1196 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1197 | if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1198 | {
|
| 1199 | if ($this->db_debug)
|
| 1200 | {
|
| 1201 | return $this->display_error('db_del_must_use_where');
|
| 1202 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1203 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1204 | return FALSE;
|
| 1205 | }
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1206 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1207 | $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] | 1208 |
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1209 | if ($reset_data)
|
| 1210 | {
|
| 1211 | $this->_reset_write();
|
| 1212 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1213 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1214 | return $this->query($sql);
|
| 1215 | }
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 1216 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1217 | // --------------------------------------------------------------------
|
| 1218 |
|
| 1219 | /**
|
| 1220 | * Use Table - DEPRECATED
|
| 1221 | *
|
| 1222 | * @deprecated use $this->db->from instead
|
| 1223 | */
|
| 1224 | function use_table($table)
|
| 1225 | {
|
| 1226 | return $this->from($table);
|
| 1227 | return $this;
|
| 1228 | }
|
| 1229 |
|
| 1230 | // --------------------------------------------------------------------
|
| 1231 |
|
| 1232 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1233 | * Tests whether the string has an SQL operator
|
| 1234 | *
|
| 1235 | * @access private
|
| 1236 | * @param string
|
| 1237 | * @return bool
|
| 1238 | */
|
| 1239 | function _has_operator($str)
|
| 1240 | {
|
| 1241 | $str = trim($str);
|
| 1242 | if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
|
| 1243 | {
|
| 1244 | return FALSE;
|
| 1245 | }
|
| 1246 |
|
| 1247 | return TRUE;
|
| 1248 | }
|
| 1249 |
|
| 1250 | // --------------------------------------------------------------------
|
| 1251 |
|
| 1252 | /**
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1253 | * Track Aliases
|
| 1254 | *
|
| 1255 | * Used to track SQL statements written with aliased tables.
|
| 1256 | *
|
| 1257 | * @access private
|
| 1258 | * @param string The table to inspect
|
| 1259 | * @return string
|
| 1260 | */
|
| 1261 | function _track_aliases($table)
|
| 1262 | {
|
| 1263 | // if a table alias is used we can recognize it by a space
|
| 1264 | if (strpos($table, " ") !== FALSE)
|
| 1265 | {
|
| 1266 | // if the alias is written with the AS keyowrd, get it out
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1267 | $table = preg_replace('/ AS /i', ' ', $table);
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1268 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1269 | $this->ar_aliased_tables[] = trim(strrchr($table, " "));
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1270 | }
|
| 1271 |
|
| 1272 | return $this->dbprefix.$table;
|
| 1273 | }
|
| 1274 |
|
| 1275 | // --------------------------------------------------------------------
|
| 1276 |
|
| 1277 | /**
|
| 1278 | * Filter Table Aliases
|
| 1279 | *
|
| 1280 | * Intelligently removes database prefixes from aliased tables
|
| 1281 | *
|
| 1282 | * @access private
|
| 1283 | * @param array An array of compiled SQL
|
| 1284 | * @return array Cleaned up statement with aliases accounted for
|
| 1285 | */
|
| 1286 | function _filter_table_aliases($statements)
|
| 1287 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1288 | $statements_without_aliases = array();
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1289 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1290 | foreach ($statements as $k => $v)
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1291 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1292 | foreach ($this->ar_aliased_tables as $table)
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1293 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1294 | $statement = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $v); // makes `table.field`
|
| 1295 | $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] | 1296 | }
|
| 1297 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1298 | $statements[$k] = $statement;
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1299 | }
|
| 1300 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1301 | return $statements;
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1302 | }
|
| 1303 |
|
| 1304 | // --------------------------------------------------------------------
|
| 1305 |
|
| 1306 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1307 | * Compile the SELECT statement
|
| 1308 | *
|
| 1309 | * Generates a query string based on which functions were used.
|
| 1310 | * Should not be called directly. The get() function calls it.
|
| 1311 | *
|
| 1312 | * @access private
|
| 1313 | * @return string
|
| 1314 | */
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1315 | function _compile_select($select_override = FALSE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1316 | {
|
| 1317 | $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
|
| 1318 |
|
| 1319 | $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->ar_select);
|
| 1320 |
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1321 | if ($select_override !== FALSE)
|
| 1322 | {
|
| 1323 | $sql = $select_override;
|
| 1324 | }
|
| 1325 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1326 | if (count($this->ar_from) > 0)
|
| 1327 | {
|
| 1328 | $sql .= "\nFROM ";
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 1329 | $sql .= '(' . implode(', ', $this->ar_from) . ')';
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1330 | }
|
| 1331 |
|
| 1332 | if (count($this->ar_join) > 0)
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1333 | {
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1334 | $sql .= "\n";
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1335 |
|
| 1336 | // special consideration for table aliases
|
| 1337 | if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
|
| 1338 | {
|
| 1339 | $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
|
| 1340 | }
|
| 1341 | else
|
| 1342 | {
|
| 1343 | $sql .= implode("\n", $this->ar_join);
|
| 1344 | }
|
| 1345 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1346 | }
|
| 1347 |
|
| 1348 | if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
|
| 1349 | {
|
| 1350 | $sql .= "\nWHERE ";
|
| 1351 | }
|
| 1352 |
|
| 1353 | $sql .= implode("\n", $this->ar_where);
|
| 1354 |
|
| 1355 | if (count($this->ar_like) > 0)
|
| 1356 | {
|
| 1357 | if (count($this->ar_where) > 0)
|
| 1358 | {
|
| 1359 | $sql .= " AND ";
|
| 1360 | }
|
| 1361 |
|
| 1362 | $sql .= implode("\n", $this->ar_like);
|
| 1363 | }
|
| 1364 |
|
| 1365 | if (count($this->ar_groupby) > 0)
|
| 1366 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1367 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1368 | $sql .= "\nGROUP BY ";
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1369 |
|
| 1370 | // special consideration for table aliases
|
| 1371 | if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
|
| 1372 | {
|
| 1373 | $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
|
| 1374 | }
|
| 1375 | else
|
| 1376 | {
|
| 1377 | $sql .= implode(', ', $this->ar_groupby);
|
| 1378 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1379 | }
|
| 1380 |
|
| 1381 | if (count($this->ar_having) > 0)
|
| 1382 | {
|
| 1383 | $sql .= "\nHAVING ";
|
| 1384 | $sql .= implode("\n", $this->ar_having);
|
| 1385 | }
|
| 1386 |
|
| 1387 | if (count($this->ar_orderby) > 0)
|
| 1388 | {
|
| 1389 | $sql .= "\nORDER BY ";
|
| 1390 | $sql .= implode(', ', $this->ar_orderby);
|
| 1391 |
|
| 1392 | if ($this->ar_order !== FALSE)
|
| 1393 | {
|
| 1394 | $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
|
| 1395 | }
|
| 1396 | }
|
| 1397 |
|
| 1398 | if (is_numeric($this->ar_limit))
|
| 1399 | {
|
| 1400 | $sql .= "\n";
|
| 1401 | $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
|
| 1402 | }
|
| 1403 |
|
| 1404 | return $sql;
|
| 1405 | }
|
| 1406 |
|
| 1407 | // --------------------------------------------------------------------
|
| 1408 |
|
| 1409 | /**
|
| 1410 | * Object to Array
|
| 1411 | *
|
| 1412 | * Takes an object as input and converts the class variables to array key/vals
|
| 1413 | *
|
| 1414 | * @access public
|
| 1415 | * @param object
|
| 1416 | * @return array
|
| 1417 | */
|
| 1418 | function _object_to_array($object)
|
| 1419 | {
|
| 1420 | if ( ! is_object($object))
|
| 1421 | {
|
| 1422 | return $object;
|
| 1423 | }
|
| 1424 |
|
| 1425 | $array = array();
|
| 1426 | foreach (get_object_vars($object) as $key => $val)
|
| 1427 | {
|
Derek Allard | 848b776 | 2007-12-31 16:43:05 +0000 | [diff] [blame] | 1428 | // There are some built in keys we need to ignore for this conversion
|
| 1429 | if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
|
| 1430 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1431 | {
|
| 1432 | $array[$key] = $val;
|
| 1433 | }
|
| 1434 | }
|
| 1435 |
|
| 1436 | return $array;
|
| 1437 | }
|
| 1438 |
|
| 1439 | // --------------------------------------------------------------------
|
| 1440 |
|
| 1441 | /**
|
| 1442 | * Resets the active record values. Called by the get() function
|
| 1443 | *
|
| 1444 | * @access private
|
| 1445 | * @return void
|
| 1446 | */
|
| 1447 | function _reset_select()
|
| 1448 | {
|
| 1449 | $this->ar_select = array();
|
| 1450 | $this->ar_distinct = FALSE;
|
| 1451 | $this->ar_from = array();
|
| 1452 | $this->ar_join = array();
|
| 1453 | $this->ar_where = array();
|
| 1454 | $this->ar_like = array();
|
| 1455 | $this->ar_groupby = array();
|
| 1456 | $this->ar_having = array();
|
| 1457 | $this->ar_limit = FALSE;
|
| 1458 | $this->ar_offset = FALSE;
|
| 1459 | $this->ar_order = FALSE;
|
| 1460 | $this->ar_orderby = array();
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 1461 | $this->ar_wherein = array();
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1462 | $this->ar_aliased_tables = array();
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1463 | }
|
| 1464 |
|
| 1465 | // --------------------------------------------------------------------
|
| 1466 |
|
| 1467 | /**
|
| 1468 | * Resets the active record "write" values.
|
| 1469 | *
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1470 | * Called by the insert() update() and delete() functions
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1471 | *
|
| 1472 | * @access private
|
| 1473 | * @return void
|
| 1474 | */
|
| 1475 | function _reset_write()
|
| 1476 | {
|
| 1477 | $this->ar_set = array();
|
| 1478 | $this->ar_from = array();
|
| 1479 | $this->ar_where = array();
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1480 | $this->ar_like = array();
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 1481 | $this->ar_limit = FALSE;
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 1482 | $this->ar_order = FALSE;
|
| 1483 | $this->ar_orderby = array();
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1484 | }
|
| 1485 |
|
| 1486 | }
|
| 1487 |
|
admin | ac94f38 | 2006-09-24 20:28:12 +0000 | [diff] [blame] | 1488 | ?> |