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 | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 752 | $this->ar_orderby[] = $this->_protect_identifiers($orderby).$direction;
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 753 | return $this;
|
| 754 | }
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 755 |
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 756 | // --------------------------------------------------------------------
|
| 757 |
|
| 758 | /**
|
| 759 | * orderby() is an alias of order_by()
|
| 760 | * this function is here for backwards compatibility, as
|
| 761 | * orderby() has been deprecated
|
| 762 | */
|
| 763 | function orderby($orderby, $direction = '')
|
| 764 | {
|
| 765 | return $this->order_by($orderby, $direction);
|
| 766 | }
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 767 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 768 | // --------------------------------------------------------------------
|
| 769 |
|
| 770 | /**
|
| 771 | * Sets the LIMIT value
|
| 772 | *
|
| 773 | * @access public
|
| 774 | * @param integer the limit value
|
| 775 | * @param integer the offset value
|
| 776 | * @return object
|
| 777 | */
|
| 778 | function limit($value, $offset = '')
|
| 779 | {
|
| 780 | $this->ar_limit = $value;
|
| 781 |
|
| 782 | if ($offset != '')
|
| 783 | $this->ar_offset = $offset;
|
| 784 |
|
| 785 | return $this;
|
| 786 | }
|
| 787 |
|
| 788 | // --------------------------------------------------------------------
|
| 789 |
|
| 790 | /**
|
| 791 | * Sets the OFFSET value
|
| 792 | *
|
| 793 | * @access public
|
| 794 | * @param integer the offset value
|
| 795 | * @return object
|
| 796 | */
|
| 797 | function offset($value)
|
| 798 | {
|
| 799 | $this->ar_offset = $value;
|
| 800 | return $this;
|
| 801 | }
|
| 802 |
|
| 803 | // --------------------------------------------------------------------
|
| 804 |
|
| 805 | /**
|
| 806 | * The "set" function. Allows key/value pairs to be set for inserting or updating
|
| 807 | *
|
| 808 | * @access public
|
| 809 | * @param mixed
|
| 810 | * @param string
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 811 | * @param boolean
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 812 | * @return object
|
| 813 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 814 | function set($key, $value = '', $escape = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 815 | {
|
| 816 | $key = $this->_object_to_array($key);
|
| 817 |
|
| 818 | if ( ! is_array($key))
|
| 819 | {
|
| 820 | $key = array($key => $value);
|
| 821 | }
|
| 822 |
|
| 823 | foreach ($key as $k => $v)
|
| 824 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 825 | if ($escape === FALSE)
|
| 826 | {
|
| 827 | $this->ar_set[$this->_protect_identifiers($k)] = $v;
|
| 828 | }
|
| 829 | else
|
| 830 | {
|
| 831 | $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
|
| 832 | }
|
| 833 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 834 | }
|
| 835 |
|
| 836 | return $this;
|
| 837 | }
|
| 838 |
|
| 839 | // --------------------------------------------------------------------
|
| 840 |
|
| 841 | /**
|
| 842 | * Get
|
| 843 | *
|
| 844 | * Compiles the select statement based on the other functions called
|
| 845 | * and runs the query
|
| 846 | *
|
| 847 | * @access public
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 848 | * @param string the table
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 849 | * @param string the limit clause
|
| 850 | * @param string the offset clause
|
| 851 | * @return object
|
| 852 | */
|
| 853 | function get($table = '', $limit = null, $offset = null)
|
| 854 | {
|
| 855 | if ($table != '')
|
| 856 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 857 | $this->_track_aliases($table);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 858 | $this->from($table);
|
| 859 | }
|
| 860 |
|
| 861 | if ( ! is_null($limit))
|
| 862 | {
|
| 863 | $this->limit($limit, $offset);
|
| 864 | }
|
| 865 |
|
| 866 | $sql = $this->_compile_select();
|
| 867 |
|
| 868 | $result = $this->query($sql);
|
| 869 | $this->_reset_select();
|
| 870 | return $result;
|
| 871 | }
|
| 872 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 873 | /**
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 874 | * "Count All Results" query
|
| 875 | *
|
| 876 | * Generates a platform-specific query string that counts all records
|
| 877 | * returned by an Active Record query.
|
| 878 | *
|
| 879 | * @access public
|
| 880 | * @param string
|
| 881 | * @return string
|
| 882 | */
|
| 883 | function count_all_results($table = '')
|
| 884 | {
|
| 885 | if ($table != '')
|
| 886 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 887 | $this->_track_aliases($table);
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 888 | $this->from($table);
|
| 889 | }
|
| 890 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 891 | $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 892 |
|
| 893 | $query = $this->query($sql);
|
| 894 | $this->_reset_select();
|
| 895 |
|
| 896 | if ($query->num_rows() == 0)
|
| 897 | {
|
| 898 | return '0';
|
| 899 | }
|
| 900 |
|
| 901 | $row = $query->row();
|
| 902 | return $row->numrows;
|
| 903 | }
|
| 904 |
|
| 905 | // --------------------------------------------------------------------
|
| 906 |
|
| 907 | /**
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 908 | * Get_Where
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 909 | *
|
| 910 | * Allows the where clause, limit and offset to be added directly
|
| 911 | *
|
| 912 | * @access public
|
| 913 | * @param string the where clause
|
| 914 | * @param string the limit clause
|
| 915 | * @param string the offset clause
|
| 916 | * @return object
|
| 917 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 918 | function get_where($table = '', $where = null, $limit = null, $offset = null)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 919 | {
|
| 920 | if ($table != '')
|
| 921 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 922 | $this->_track_aliases($table);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 923 | $this->from($table);
|
| 924 | }
|
| 925 |
|
| 926 | if ( ! is_null($where))
|
| 927 | {
|
| 928 | $this->where($where);
|
| 929 | }
|
| 930 |
|
| 931 | if ( ! is_null($limit))
|
| 932 | {
|
| 933 | $this->limit($limit, $offset);
|
| 934 | }
|
| 935 |
|
| 936 | $sql = $this->_compile_select();
|
| 937 |
|
| 938 | $result = $this->query($sql);
|
| 939 | $this->_reset_select();
|
| 940 | return $result;
|
| 941 | }
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 942 |
|
| 943 | // --------------------------------------------------------------------
|
| 944 |
|
| 945 | /**
|
| 946 | * getwhere() is an alias of get_where()
|
| 947 | * this function is here for backwards compatibility, as
|
| 948 | * getwhere() has been deprecated
|
| 949 | */
|
| 950 | function getwhere($table = '', $where = null, $limit = null, $offset = null)
|
| 951 | {
|
| 952 | return $this->get_where($table, $where, $limit, $offset);
|
| 953 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 954 |
|
| 955 | // --------------------------------------------------------------------
|
| 956 |
|
| 957 | /**
|
| 958 | * Insert
|
| 959 | *
|
| 960 | * Compiles an insert string and runs the query
|
| 961 | *
|
| 962 | * @access public
|
| 963 | * @param string the table to retrieve the results from
|
| 964 | * @param array an associative array of insert values
|
| 965 | * @return object
|
| 966 | */
|
| 967 | function insert($table = '', $set = NULL)
|
| 968 | {
|
| 969 | if ( ! is_null($set))
|
| 970 | {
|
| 971 | $this->set($set);
|
| 972 | }
|
| 973 |
|
| 974 | if (count($this->ar_set) == 0)
|
| 975 | {
|
| 976 | if ($this->db_debug)
|
| 977 | {
|
| 978 | return $this->display_error('db_must_use_set');
|
| 979 | }
|
| 980 | return FALSE;
|
| 981 | }
|
| 982 |
|
| 983 | if ($table == '')
|
| 984 | {
|
| 985 | if ( ! isset($this->ar_from[0]))
|
| 986 | {
|
| 987 | if ($this->db_debug)
|
| 988 | {
|
| 989 | return $this->display_error('db_must_set_table');
|
| 990 | }
|
| 991 | return FALSE;
|
| 992 | }
|
| 993 |
|
| 994 | $table = $this->ar_from[0];
|
| 995 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 996 |
|
| 997 | $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] | 998 |
|
| 999 | $this->_reset_write();
|
| 1000 | return $this->query($sql);
|
| 1001 | }
|
| 1002 |
|
| 1003 | // --------------------------------------------------------------------
|
| 1004 |
|
| 1005 | /**
|
| 1006 | * Update
|
| 1007 | *
|
| 1008 | * Compiles an update string and runs the query
|
| 1009 | *
|
| 1010 | * @access public
|
| 1011 | * @param string the table to retrieve the results from
|
| 1012 | * @param array an associative array of update values
|
| 1013 | * @param mixed the where clause
|
| 1014 | * @return object
|
| 1015 | */
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1016 | function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1017 | {
|
| 1018 | if ( ! is_null($set))
|
| 1019 | {
|
| 1020 | $this->set($set);
|
| 1021 | }
|
| 1022 |
|
| 1023 | if (count($this->ar_set) == 0)
|
| 1024 | {
|
| 1025 | if ($this->db_debug)
|
| 1026 | {
|
| 1027 | return $this->display_error('db_must_use_set');
|
| 1028 | }
|
| 1029 | return FALSE;
|
| 1030 | }
|
| 1031 |
|
| 1032 | if ($table == '')
|
| 1033 | {
|
| 1034 | if ( ! isset($this->ar_from[0]))
|
| 1035 | {
|
| 1036 | if ($this->db_debug)
|
| 1037 | {
|
| 1038 | return $this->display_error('db_must_set_table');
|
| 1039 | }
|
| 1040 | return FALSE;
|
| 1041 | }
|
| 1042 |
|
| 1043 | $table = $this->ar_from[0];
|
| 1044 | }
|
| 1045 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1046 | if ($where != NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1047 | {
|
| 1048 | $this->where($where);
|
| 1049 | }
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 1050 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1051 | if ($limit != NULL)
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 1052 | {
|
| 1053 | $this->limit($limit);
|
| 1054 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1055 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1056 | $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] | 1057 |
|
| 1058 | $this->_reset_write();
|
| 1059 | return $this->query($sql);
|
| 1060 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1061 |
|
| 1062 | // --------------------------------------------------------------------
|
| 1063 |
|
| 1064 | /**
|
| 1065 | * Empty Table
|
| 1066 | *
|
| 1067 | * Compiles a delete string and runs "DELETE FROM table"
|
| 1068 | *
|
| 1069 | * @access public
|
| 1070 | * @param string the table to empty
|
| 1071 | * @return object
|
| 1072 | */
|
| 1073 | function empty_table($table = '')
|
| 1074 | {
|
| 1075 | if ($table == '')
|
| 1076 | {
|
| 1077 | if ( ! isset($this->ar_from[0]))
|
| 1078 | {
|
| 1079 | if ($this->db_debug)
|
| 1080 | {
|
| 1081 | return $this->display_error('db_must_set_table');
|
| 1082 | }
|
| 1083 | return FALSE;
|
| 1084 | }
|
| 1085 |
|
| 1086 | $table = $this->ar_from[0];
|
| 1087 | }
|
| 1088 | else
|
| 1089 | {
|
| 1090 | $table = $this->_protect_identifiers($this->dbprefix.$table);
|
| 1091 | }
|
| 1092 |
|
| 1093 |
|
| 1094 | $sql = $this->_delete($table);
|
| 1095 |
|
| 1096 | $this->_reset_write();
|
| 1097 |
|
| 1098 | return $this->query($sql);
|
| 1099 | }
|
| 1100 |
|
| 1101 | // --------------------------------------------------------------------
|
| 1102 |
|
| 1103 | /**
|
| 1104 | * Truncate
|
| 1105 | *
|
| 1106 | * Compiles a truncate string and runs the query
|
| 1107 | * If the database does not support the truncate() command
|
| 1108 | * This function maps to "DELETE FROM table"
|
| 1109 | *
|
| 1110 | * @access public
|
| 1111 | * @param string the table to truncate
|
| 1112 | * @return object
|
| 1113 | */
|
| 1114 | function truncate($table = '')
|
| 1115 | {
|
| 1116 | if ($table == '')
|
| 1117 | {
|
| 1118 | if ( ! isset($this->ar_from[0]))
|
| 1119 | {
|
| 1120 | if ($this->db_debug)
|
| 1121 | {
|
| 1122 | return $this->display_error('db_must_set_table');
|
| 1123 | }
|
| 1124 | return FALSE;
|
| 1125 | }
|
| 1126 |
|
| 1127 | $table = $this->ar_from[0];
|
| 1128 | }
|
| 1129 | else
|
| 1130 | {
|
| 1131 | $table = $this->_protect_identifiers($this->dbprefix.$table);
|
| 1132 | }
|
| 1133 |
|
| 1134 |
|
| 1135 | $sql = $this->_truncate($table);
|
| 1136 |
|
| 1137 | $this->_reset_write();
|
| 1138 |
|
| 1139 | return $this->query($sql);
|
| 1140 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1141 |
|
| 1142 | // --------------------------------------------------------------------
|
| 1143 |
|
| 1144 | /**
|
| 1145 | * Delete
|
| 1146 | *
|
| 1147 | * Compiles a delete string and runs the query
|
| 1148 | *
|
| 1149 | * @access public
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1150 | * @param mixed the table(s) to delete from. String or array
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1151 | * @param mixed the where clause
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1152 | * @param mixed the limit clause
|
| 1153 | * @param boolean
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1154 | * @return object
|
| 1155 | */
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1156 | function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1157 | {
|
| 1158 | if ($table == '')
|
| 1159 | {
|
| 1160 | if ( ! isset($this->ar_from[0]))
|
| 1161 | {
|
| 1162 | if ($this->db_debug)
|
| 1163 | {
|
| 1164 | return $this->display_error('db_must_set_table');
|
| 1165 | }
|
| 1166 | return FALSE;
|
| 1167 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1168 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1169 | $table = $this->ar_from[0];
|
| 1170 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1171 | elseif (is_array($table))
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1172 | {
|
| 1173 | foreach($table as $single_table)
|
| 1174 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1175 | $this->delete($single_table, $where, $limit, FALSE);
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1176 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1177 |
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1178 | $this->_reset_write();
|
| 1179 | return;
|
| 1180 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1181 | else
|
| 1182 | {
|
| 1183 | $table = $this->_protect_identifiers($this->dbprefix.$table);
|
| 1184 | }
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1185 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1186 | if ($where != '')
|
| 1187 | {
|
| 1188 | $this->where($where);
|
| 1189 | }
|
| 1190 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1191 | if ($limit != NULL)
|
| 1192 | {
|
| 1193 | $this->limit($limit);
|
| 1194 | }
|
| 1195 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1196 | if (count($this->ar_where) == 0 && count($this->ar_like) == 0)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1197 | {
|
| 1198 | if ($this->db_debug)
|
| 1199 | {
|
| 1200 | return $this->display_error('db_del_must_use_where');
|
| 1201 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1202 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1203 | return FALSE;
|
| 1204 | }
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1205 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1206 | $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] | 1207 |
|
Derek Allard | 41f60d4 | 2007-12-20 20:09:22 +0000 | [diff] [blame] | 1208 | if ($reset_data)
|
| 1209 | {
|
| 1210 | $this->_reset_write();
|
| 1211 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1212 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1213 | return $this->query($sql);
|
| 1214 | }
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 1215 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1216 | // --------------------------------------------------------------------
|
| 1217 |
|
| 1218 | /**
|
| 1219 | * Use Table - DEPRECATED
|
| 1220 | *
|
| 1221 | * @deprecated use $this->db->from instead
|
| 1222 | */
|
| 1223 | function use_table($table)
|
| 1224 | {
|
| 1225 | return $this->from($table);
|
| 1226 | return $this;
|
| 1227 | }
|
| 1228 |
|
| 1229 | // --------------------------------------------------------------------
|
| 1230 |
|
| 1231 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1232 | * Tests whether the string has an SQL operator
|
| 1233 | *
|
| 1234 | * @access private
|
| 1235 | * @param string
|
| 1236 | * @return bool
|
| 1237 | */
|
| 1238 | function _has_operator($str)
|
| 1239 | {
|
| 1240 | $str = trim($str);
|
| 1241 | if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
|
| 1242 | {
|
| 1243 | return FALSE;
|
| 1244 | }
|
| 1245 |
|
| 1246 | return TRUE;
|
| 1247 | }
|
| 1248 |
|
| 1249 | // --------------------------------------------------------------------
|
| 1250 |
|
| 1251 | /**
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1252 | * Track Aliases
|
| 1253 | *
|
| 1254 | * Used to track SQL statements written with aliased tables.
|
| 1255 | *
|
| 1256 | * @access private
|
| 1257 | * @param string The table to inspect
|
| 1258 | * @return string
|
| 1259 | */
|
| 1260 | function _track_aliases($table)
|
| 1261 | {
|
| 1262 | // if a table alias is used we can recognize it by a space
|
| 1263 | if (strpos($table, " ") !== FALSE)
|
| 1264 | {
|
| 1265 | // if the alias is written with the AS keyowrd, get it out
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1266 | $table = preg_replace('/ AS /i', ' ', $table);
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1267 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1268 | $this->ar_aliased_tables[] = trim(strrchr($table, " "));
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1269 | }
|
| 1270 |
|
| 1271 | return $this->dbprefix.$table;
|
| 1272 | }
|
| 1273 |
|
| 1274 | // --------------------------------------------------------------------
|
| 1275 |
|
| 1276 | /**
|
| 1277 | * Filter Table Aliases
|
| 1278 | *
|
| 1279 | * Intelligently removes database prefixes from aliased tables
|
| 1280 | *
|
| 1281 | * @access private
|
| 1282 | * @param array An array of compiled SQL
|
| 1283 | * @return array Cleaned up statement with aliases accounted for
|
| 1284 | */
|
| 1285 | function _filter_table_aliases($statements)
|
| 1286 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1287 | $statements_without_aliases = array();
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1288 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1289 | foreach ($statements as $k => $v)
|
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 ($this->ar_aliased_tables as $table)
|
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 | $statement = preg_replace('/(\w+\.\w+)/', $this->_protect_identifiers('$0'), $v); // makes `table.field`
|
| 1294 | $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] | 1295 | }
|
| 1296 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1297 | $statements[$k] = $statement;
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1298 | }
|
| 1299 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1300 | return $statements;
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1301 | }
|
| 1302 |
|
| 1303 | // --------------------------------------------------------------------
|
| 1304 |
|
| 1305 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1306 | * Compile the SELECT statement
|
| 1307 | *
|
| 1308 | * Generates a query string based on which functions were used.
|
| 1309 | * Should not be called directly. The get() function calls it.
|
| 1310 | *
|
| 1311 | * @access private
|
| 1312 | * @return string
|
| 1313 | */
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1314 | function _compile_select($select_override = FALSE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1315 | {
|
| 1316 | $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
|
| 1317 |
|
| 1318 | $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->ar_select);
|
| 1319 |
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 1320 | if ($select_override !== FALSE)
|
| 1321 | {
|
| 1322 | $sql = $select_override;
|
| 1323 | }
|
| 1324 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1325 | if (count($this->ar_from) > 0)
|
| 1326 | {
|
| 1327 | $sql .= "\nFROM ";
|
Derek Allard | 15ddc9d | 2007-12-20 13:54:39 +0000 | [diff] [blame] | 1328 | $sql .= '(' . implode(', ', $this->ar_from) . ')';
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1329 | }
|
| 1330 |
|
| 1331 | if (count($this->ar_join) > 0)
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1332 | {
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1333 | $sql .= "\n";
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1334 |
|
| 1335 | // special consideration for table aliases
|
| 1336 | if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
|
| 1337 | {
|
| 1338 | $sql .= implode("\n", $this->_filter_table_aliases($this->ar_join));
|
| 1339 | }
|
| 1340 | else
|
| 1341 | {
|
| 1342 | $sql .= implode("\n", $this->ar_join);
|
| 1343 | }
|
| 1344 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1345 | }
|
| 1346 |
|
| 1347 | if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
|
| 1348 | {
|
| 1349 | $sql .= "\nWHERE ";
|
| 1350 | }
|
| 1351 |
|
| 1352 | $sql .= implode("\n", $this->ar_where);
|
| 1353 |
|
| 1354 | if (count($this->ar_like) > 0)
|
| 1355 | {
|
| 1356 | if (count($this->ar_where) > 0)
|
| 1357 | {
|
| 1358 | $sql .= " AND ";
|
| 1359 | }
|
| 1360 |
|
| 1361 | $sql .= implode("\n", $this->ar_like);
|
| 1362 | }
|
| 1363 |
|
| 1364 | if (count($this->ar_groupby) > 0)
|
| 1365 | {
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1366 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1367 | $sql .= "\nGROUP BY ";
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1368 |
|
| 1369 | // special consideration for table aliases
|
| 1370 | if (count($this->ar_aliased_tables) > 0 && $this->dbprefix)
|
| 1371 | {
|
| 1372 | $sql .= implode(", ", $this->_filter_table_aliases($this->ar_groupby));
|
| 1373 | }
|
| 1374 | else
|
| 1375 | {
|
| 1376 | $sql .= implode(', ', $this->ar_groupby);
|
| 1377 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1378 | }
|
| 1379 |
|
| 1380 | if (count($this->ar_having) > 0)
|
| 1381 | {
|
| 1382 | $sql .= "\nHAVING ";
|
| 1383 | $sql .= implode("\n", $this->ar_having);
|
| 1384 | }
|
| 1385 |
|
| 1386 | if (count($this->ar_orderby) > 0)
|
| 1387 | {
|
| 1388 | $sql .= "\nORDER BY ";
|
| 1389 | $sql .= implode(', ', $this->ar_orderby);
|
| 1390 |
|
| 1391 | if ($this->ar_order !== FALSE)
|
| 1392 | {
|
| 1393 | $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
|
| 1394 | }
|
| 1395 | }
|
| 1396 |
|
| 1397 | if (is_numeric($this->ar_limit))
|
| 1398 | {
|
| 1399 | $sql .= "\n";
|
| 1400 | $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
|
| 1401 | }
|
| 1402 |
|
| 1403 | return $sql;
|
| 1404 | }
|
| 1405 |
|
| 1406 | // --------------------------------------------------------------------
|
| 1407 |
|
| 1408 | /**
|
| 1409 | * Object to Array
|
| 1410 | *
|
| 1411 | * Takes an object as input and converts the class variables to array key/vals
|
| 1412 | *
|
| 1413 | * @access public
|
| 1414 | * @param object
|
| 1415 | * @return array
|
| 1416 | */
|
| 1417 | function _object_to_array($object)
|
| 1418 | {
|
| 1419 | if ( ! is_object($object))
|
| 1420 | {
|
| 1421 | return $object;
|
| 1422 | }
|
| 1423 |
|
| 1424 | $array = array();
|
| 1425 | foreach (get_object_vars($object) as $key => $val)
|
| 1426 | {
|
Derek Allard | 848b776 | 2007-12-31 16:43:05 +0000 | [diff] [blame] | 1427 | // There are some built in keys we need to ignore for this conversion
|
| 1428 | if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name' && $key != '_ci_scaffolding' && $key != '_ci_scaff_table')
|
| 1429 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1430 | {
|
| 1431 | $array[$key] = $val;
|
| 1432 | }
|
| 1433 | }
|
| 1434 |
|
| 1435 | return $array;
|
| 1436 | }
|
| 1437 |
|
| 1438 | // --------------------------------------------------------------------
|
| 1439 |
|
| 1440 | /**
|
| 1441 | * Resets the active record values. Called by the get() function
|
| 1442 | *
|
| 1443 | * @access private
|
| 1444 | * @return void
|
| 1445 | */
|
| 1446 | function _reset_select()
|
| 1447 | {
|
| 1448 | $this->ar_select = array();
|
| 1449 | $this->ar_distinct = FALSE;
|
| 1450 | $this->ar_from = array();
|
| 1451 | $this->ar_join = array();
|
| 1452 | $this->ar_where = array();
|
| 1453 | $this->ar_like = array();
|
| 1454 | $this->ar_groupby = array();
|
| 1455 | $this->ar_having = array();
|
| 1456 | $this->ar_limit = FALSE;
|
| 1457 | $this->ar_offset = FALSE;
|
| 1458 | $this->ar_order = FALSE;
|
| 1459 | $this->ar_orderby = array();
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 1460 | $this->ar_wherein = array();
|
Derek Allard | 5e12894 | 2007-12-28 21:33:03 +0000 | [diff] [blame] | 1461 | $this->ar_aliased_tables = array();
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1462 | }
|
| 1463 |
|
| 1464 | // --------------------------------------------------------------------
|
| 1465 |
|
| 1466 | /**
|
| 1467 | * Resets the active record "write" values.
|
| 1468 | *
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 1469 | * Called by the insert() update() and delete() functions
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1470 | *
|
| 1471 | * @access private
|
| 1472 | * @return void
|
| 1473 | */
|
| 1474 | function _reset_write()
|
| 1475 | {
|
| 1476 | $this->ar_set = array();
|
| 1477 | $this->ar_from = array();
|
| 1478 | $this->ar_where = array();
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1479 | $this->ar_like = array();
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 1480 | $this->ar_limit = FALSE;
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame^] | 1481 | $this->ar_order = FALSE;
|
| 1482 | $this->ar_orderby = array();
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1483 | }
|
| 1484 |
|
| 1485 | }
|
| 1486 |
|
admin | ac94f38 | 2006-09-24 20:28:12 +0000 | [diff] [blame] | 1487 | ?> |