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