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