Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
| 2 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 3 | * CodeIgniter
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
| 8 | * @author Rick Ellis
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Allard | 6838f00 | 2007-10-04 19:29:59 +0000 | [diff] [blame] | 10 | * @license http://www.codeigniter.com/user_guide/license.html
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 11 | * @link http://www.codeigniter.com
|
| 12 | * @since Version 1.0
|
| 13 | * @filesource
|
| 14 | */
|
| 15 |
|
| 16 | // ------------------------------------------------------------------------
|
| 17 |
|
| 18 | /**
|
| 19 | * Active Record Class
|
| 20 | *
|
| 21 | * This is the platform-independent base Active Record implementation class.
|
| 22 | *
|
| 23 | * @package CodeIgniter
|
| 24 | * @subpackage Drivers
|
| 25 | * @category Database
|
| 26 | * @author Rick Ellis
|
| 27 | * @link http://www.codeigniter.com/user_guide/database/
|
| 28 | */
|
| 29 | class CI_DB_active_record extends CI_DB_driver {
|
| 30 |
|
| 31 | var $ar_select = array();
|
| 32 | var $ar_distinct = FALSE;
|
| 33 | var $ar_from = array();
|
| 34 | var $ar_join = array();
|
| 35 | var $ar_where = array();
|
| 36 | var $ar_like = array();
|
| 37 | var $ar_groupby = array();
|
| 38 | var $ar_having = array();
|
| 39 | var $ar_limit = FALSE;
|
| 40 | var $ar_offset = FALSE;
|
| 41 | var $ar_order = FALSE;
|
| 42 | var $ar_orderby = array();
|
| 43 | var $ar_set = array();
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 44 | var $ar_wherein = array();
|
| 45 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 46 |
|
| 47 |
|
| 48 | /**
|
| 49 | * Select
|
| 50 | *
|
| 51 | * Generates the SELECT portion of the query
|
| 52 | *
|
| 53 | * @access public
|
| 54 | * @param string
|
| 55 | * @return object
|
| 56 | */
|
| 57 | function select($select = '*')
|
| 58 | {
|
| 59 | if (is_string($select))
|
| 60 | {
|
| 61 | $select = explode(',', $select);
|
| 62 | }
|
| 63 |
|
| 64 | foreach ($select as $val)
|
| 65 | {
|
| 66 | $val = trim($val);
|
| 67 |
|
| 68 | if ($val != '')
|
| 69 | $this->ar_select[] = $val;
|
| 70 | }
|
| 71 | return $this;
|
| 72 | }
|
| 73 |
|
| 74 | // --------------------------------------------------------------------
|
| 75 |
|
| 76 | /**
|
| 77 | * DISTINCT
|
| 78 | *
|
| 79 | * Sets a flag which tells the query string compiler to add DISTINCT
|
| 80 | *
|
| 81 | * @access public
|
| 82 | * @param bool
|
| 83 | * @return object
|
| 84 | */
|
| 85 | function distinct($val = TRUE)
|
| 86 | {
|
| 87 | $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
|
| 88 | return $this;
|
| 89 | }
|
| 90 |
|
| 91 | // --------------------------------------------------------------------
|
| 92 |
|
| 93 | /**
|
| 94 | * From
|
| 95 | *
|
| 96 | * Generates the FROM portion of the query
|
| 97 | *
|
| 98 | * @access public
|
| 99 | * @param mixed can be a string or array
|
| 100 | * @return object
|
| 101 | */
|
| 102 | function from($from)
|
| 103 | {
|
| 104 | foreach ((array)$from as $val)
|
| 105 | {
|
| 106 | $this->ar_from[] = $this->dbprefix.$val;
|
| 107 | }
|
| 108 | return $this;
|
| 109 | }
|
| 110 |
|
| 111 | // --------------------------------------------------------------------
|
| 112 |
|
| 113 | /**
|
| 114 | * Join
|
| 115 | *
|
| 116 | * Generates the JOIN portion of the query
|
| 117 | *
|
| 118 | * @access public
|
| 119 | * @param string
|
| 120 | * @param string the join condition
|
| 121 | * @param string the type of join
|
| 122 | * @return object
|
| 123 | */
|
| 124 | function join($table, $cond, $type = '')
|
| 125 | {
|
| 126 | if ($type != '')
|
| 127 | {
|
| 128 | $type = strtoupper(trim($type));
|
| 129 |
|
| 130 | if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER'), TRUE))
|
| 131 | {
|
| 132 | $type = '';
|
| 133 | }
|
| 134 | else
|
| 135 | {
|
| 136 | $type .= ' ';
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | if ($this->dbprefix)
|
| 141 | {
|
| 142 | $cond = preg_replace('|([\w\.]+)([\W\s]+)(.+)|', $this->dbprefix . "$1$2" . $this->dbprefix . "$3", $cond);
|
| 143 | }
|
| 144 |
|
| 145 | // If a DB prefix is used we might need to add it to the column names
|
| 146 | if ($this->dbprefix)
|
| 147 | {
|
| 148 | // First we remove any existing prefixes in the condition to avoid duplicates
|
| 149 | $cond = preg_replace('|('.$this->dbprefix.')([\w\.]+)([\W\s]+)|', "$2$3", $cond);
|
| 150 |
|
| 151 | // Next we add the prefixes to the condition
|
| 152 | $cond = preg_replace('|([\w\.]+)([\W\s]+)(.+)|', $this->dbprefix . "$1$2" . $this->dbprefix . "$3", $cond);
|
| 153 | }
|
| 154 |
|
| 155 | $this->ar_join[] = $type.'JOIN '.$this->dbprefix.$table.' ON '.$cond;
|
| 156 | return $this;
|
| 157 | }
|
| 158 |
|
| 159 | // --------------------------------------------------------------------
|
| 160 |
|
| 161 | /**
|
| 162 | * Where
|
| 163 | *
|
| 164 | * Generates the WHERE portion of the query. Separates
|
| 165 | * multiple calls with AND
|
| 166 | *
|
| 167 | * @access public
|
| 168 | * @param mixed
|
| 169 | * @param mixed
|
| 170 | * @return object
|
| 171 | */
|
| 172 | function where($key, $value = NULL)
|
| 173 | {
|
| 174 | return $this->_where($key, $value, 'AND ');
|
| 175 | }
|
| 176 |
|
| 177 | // --------------------------------------------------------------------
|
| 178 |
|
| 179 | /**
|
| 180 | * OR Where
|
| 181 | *
|
| 182 | * Generates the WHERE portion of the query. Separates
|
| 183 | * multiple calls with OR
|
| 184 | *
|
| 185 | * @access public
|
| 186 | * @param mixed
|
| 187 | * @param mixed
|
| 188 | * @return object
|
| 189 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 190 | function or_where($key, $value = NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 191 | {
|
| 192 | return $this->_where($key, $value, 'OR ');
|
| 193 | }
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 194 |
|
| 195 | // --------------------------------------------------------------------
|
| 196 |
|
| 197 | /**
|
| 198 | * orwhere() is an alias of or_where()
|
| 199 | * this function is here for backwards compatibility, as
|
| 200 | * orwhere() has been deprecated
|
| 201 | */
|
| 202 | function orwhere($key, $value = NULL)
|
| 203 | {
|
| 204 | return $this->or_where($key, $value);
|
| 205 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 206 |
|
| 207 | // --------------------------------------------------------------------
|
| 208 |
|
| 209 | /**
|
| 210 | * Where
|
| 211 | *
|
| 212 | * Called by where() or orwhere()
|
| 213 | *
|
| 214 | * @access private
|
| 215 | * @param mixed
|
| 216 | * @param mixed
|
| 217 | * @param string
|
| 218 | * @return object
|
| 219 | */
|
| 220 | function _where($key, $value = NULL, $type = 'AND ')
|
| 221 | {
|
| 222 | if ( ! is_array($key))
|
| 223 | {
|
| 224 | $key = array($key => $value);
|
| 225 | }
|
| 226 |
|
| 227 | foreach ($key as $k => $v)
|
| 228 | {
|
| 229 | $prefix = (count($this->ar_where) == 0) ? '' : $type;
|
| 230 |
|
| 231 | if ( ! is_null($v))
|
| 232 | {
|
| 233 | if ( ! $this->_has_operator($k))
|
| 234 | {
|
| 235 | $k .= ' =';
|
| 236 | }
|
| 237 |
|
| 238 | $v = ' '.$this->escape($v);
|
| 239 | }
|
| 240 |
|
| 241 | $this->ar_where[] = $prefix.$k.$v;
|
| 242 | }
|
| 243 | return $this;
|
| 244 | }
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 245 |
|
| 246 | // --------------------------------------------------------------------
|
| 247 |
|
| 248 | /**
|
| 249 | * Where_in
|
| 250 | *
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 251 | * Generates a WHERE field IN ('item', 'item') SQL query joined with
|
| 252 | * AND if appropriate
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 253 | *
|
| 254 | * @access public
|
| 255 | * @param string The field to search
|
| 256 | * @param array The values searched on
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 257 |
|
| 258 | * @return object
|
| 259 | */
|
| 260 | function where_in($key = NULL, $values = NULL)
|
| 261 | {
|
| 262 | return $this->_where_in($key, $values);
|
| 263 | }
|
| 264 |
|
| 265 | // --------------------------------------------------------------------
|
| 266 |
|
| 267 | /**
|
| 268 | * Where_in_or
|
| 269 | *
|
| 270 | * Generates a WHERE field IN ('item', 'item') SQL query joined with
|
| 271 | * OR if appropriate
|
| 272 | *
|
| 273 | * @access public
|
| 274 | * @param string The field to search
|
| 275 | * @param array The values searched on
|
| 276 |
|
| 277 | * @return object
|
| 278 | */
|
| 279 | function where_in_or($key = NULL, $values = NULL)
|
| 280 | {
|
| 281 | return $this->_where_in($key, $values, FALSE, 'or');
|
| 282 | }
|
| 283 |
|
| 284 | // --------------------------------------------------------------------
|
| 285 |
|
| 286 | /**
|
| 287 | * Where_not_in
|
| 288 | *
|
| 289 | * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
|
| 290 | * with AND if appropriate
|
| 291 | *
|
| 292 | * @access public
|
| 293 | * @param string The field to search
|
| 294 | * @param array The values searched on
|
| 295 |
|
| 296 | * @return object
|
| 297 | */
|
| 298 | function where_not_in($key = NULL, $values = NULL)
|
| 299 | {
|
| 300 | return $this->_where_in($key, $values, TRUE);
|
| 301 | }
|
| 302 |
|
| 303 | // --------------------------------------------------------------------
|
| 304 |
|
| 305 | /**
|
| 306 | * Where_not_in_or
|
| 307 | *
|
| 308 | * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
|
| 309 | * with OR if appropriate
|
| 310 | *
|
| 311 | * @access public
|
| 312 | * @param string The field to search
|
| 313 | * @param array The values searched on
|
| 314 |
|
| 315 | * @return object
|
| 316 | */
|
| 317 | function where_not_in_or($key = NULL, $values = NULL)
|
| 318 | {
|
| 319 | return $this->_where_in($key, $values, FALSE, 'or');
|
| 320 | }
|
| 321 |
|
| 322 | // --------------------------------------------------------------------
|
| 323 |
|
| 324 | /**
|
| 325 | * Where_in
|
| 326 | *
|
| 327 | * Called by where_in, where_in_or, where_not_in, where_not_in_or
|
| 328 | *
|
| 329 | * @access public
|
| 330 | * @param string The field to search
|
| 331 | * @param array The values searched on
|
| 332 | * @param boolean If the statement whould be IN or NOT IN
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 333 | * @param string
|
| 334 | * @return object
|
| 335 | */
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 336 | function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'and')
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 337 | {
|
| 338 | if ($key === NULL || !is_array($values))
|
| 339 | {
|
| 340 | return;
|
| 341 | }
|
| 342 |
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 343 | $not = ($not) ? ' NOT ' : '';
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 344 | $type = (strtolower($type) == 'or') ? ' OR ' : ' AND ';
|
| 345 |
|
| 346 | foreach ($values as $value)
|
| 347 | {
|
| 348 | $this->ar_wherein[] = $this->escape($value);
|
| 349 | }
|
| 350 |
|
| 351 | $prefix = (count($this->ar_where) == 0) ? '' : $type;
|
| 352 |
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 353 | $this->ar_where[] = $prefix.$key.$not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
|
Derek Allard | 80dd702 | 2007-12-18 23:55:06 +0000 | [diff] [blame] | 354 |
|
| 355 | return $this;
|
| 356 | }
|
| 357 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 358 | // --------------------------------------------------------------------
|
| 359 |
|
| 360 | /**
|
| 361 | * Like
|
| 362 | *
|
| 363 | * Generates a %LIKE% portion of the query. Separates
|
| 364 | * multiple calls with AND
|
| 365 | *
|
| 366 | * @access public
|
| 367 | * @param mixed
|
| 368 | * @param mixed
|
| 369 | * @return object
|
| 370 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 371 | function like($field, $match = '', $side = 'both')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 372 | {
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 373 | return $this->_like($field, $match, 'AND ', $side);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 374 | }
|
| 375 |
|
| 376 | // --------------------------------------------------------------------
|
| 377 |
|
| 378 | /**
|
| 379 | * OR Like
|
| 380 | *
|
| 381 | * Generates a %LIKE% portion of the query. Separates
|
| 382 | * multiple calls with OR
|
| 383 | *
|
| 384 | * @access public
|
| 385 | * @param mixed
|
| 386 | * @param mixed
|
| 387 | * @return object
|
| 388 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 389 | function or_like($field, $match = '', $side = 'both')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 390 | {
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 391 | return $this->_like($field, $match, 'OR ', $side);
|
| 392 | }
|
| 393 |
|
| 394 | // --------------------------------------------------------------------
|
| 395 |
|
| 396 | /**
|
| 397 | * orlike() is an alias of or_like()
|
| 398 | * this function is here for backwards compatibility, as
|
| 399 | * orlike() has been deprecated
|
| 400 | */
|
| 401 | function orlike($field, $match = '', $side = 'both')
|
| 402 | {
|
| 403 | return $this->orlike($field, $match, $side);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 404 | }
|
| 405 |
|
| 406 | // --------------------------------------------------------------------
|
| 407 |
|
| 408 | /**
|
| 409 | * Like
|
| 410 | *
|
| 411 | * Called by like() or orlike()
|
| 412 | *
|
| 413 | * @access private
|
| 414 | * @param mixed
|
| 415 | * @param mixed
|
| 416 | * @param string
|
| 417 | * @return object
|
| 418 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 419 | function _like($field, $match = '', $type = 'AND ', $side = 'both')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 420 | {
|
| 421 | if ( ! is_array($field))
|
| 422 | {
|
| 423 | $field = array($field => $match);
|
| 424 | }
|
| 425 |
|
| 426 | foreach ($field as $k => $v)
|
| 427 | {
|
| 428 | $prefix = (count($this->ar_like) == 0) ? '' : $type;
|
| 429 |
|
| 430 | $v = $this->escape_str($v);
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 431 |
|
| 432 | if ($side == 'before')
|
| 433 | {
|
| 434 | $this->ar_like[] = $prefix." $k LIKE '%{$v}'";
|
| 435 | }
|
| 436 | elseif ($side == 'after')
|
| 437 | {
|
| 438 | $this->ar_like[] = $prefix." $k LIKE '{$v}%'";
|
| 439 | }
|
| 440 | else
|
| 441 | {
|
| 442 | $this->ar_like[] = $prefix." $k LIKE '%{$v}%'";
|
| 443 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 444 | }
|
| 445 | return $this;
|
| 446 | }
|
| 447 |
|
| 448 | // --------------------------------------------------------------------
|
| 449 |
|
| 450 | /**
|
| 451 | * GROUP BY
|
| 452 | *
|
| 453 | * @access public
|
| 454 | * @param string
|
| 455 | * @return object
|
| 456 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 457 | function group_by($by)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 458 | {
|
| 459 | if (is_string($by))
|
| 460 | {
|
| 461 | $by = explode(',', $by);
|
| 462 | }
|
| 463 |
|
| 464 | foreach ($by as $val)
|
| 465 | {
|
| 466 | $val = trim($val);
|
| 467 |
|
| 468 | if ($val != '')
|
| 469 | $this->ar_groupby[] = $val;
|
| 470 | }
|
| 471 | return $this;
|
| 472 | }
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 473 |
|
| 474 | // --------------------------------------------------------------------
|
| 475 |
|
| 476 | /**
|
| 477 | * groupby() is an alias of group_by()
|
| 478 | * this function is here for backwards compatibility, as
|
| 479 | * groupby() has been deprecated
|
| 480 | */
|
| 481 | function groupby($by)
|
| 482 | {
|
| 483 | return $this->group_by($by);
|
| 484 | }
|
| 485 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 486 | // --------------------------------------------------------------------
|
| 487 |
|
| 488 | /**
|
| 489 | * Sets the HAVING value
|
| 490 | *
|
| 491 | * Separates multiple calls with AND
|
| 492 | *
|
| 493 | * @access public
|
| 494 | * @param string
|
| 495 | * @param string
|
| 496 | * @return object
|
| 497 | */
|
| 498 | function having($key, $value = '')
|
| 499 | {
|
| 500 | return $this->_having($key, $value, 'AND ');
|
| 501 | }
|
| 502 |
|
| 503 | // --------------------------------------------------------------------
|
| 504 |
|
| 505 | /**
|
| 506 | * Sets the OR HAVING value
|
| 507 | *
|
| 508 | * Separates multiple calls with OR
|
| 509 | *
|
| 510 | * @access public
|
| 511 | * @param string
|
| 512 | * @param string
|
| 513 | * @return object
|
| 514 | */
|
| 515 | function orhaving($key, $value = '')
|
| 516 | {
|
| 517 | return $this->_having($key, $value, 'OR ');
|
| 518 | }
|
| 519 |
|
| 520 | // --------------------------------------------------------------------
|
| 521 |
|
| 522 | /**
|
| 523 | * Sets the HAVING values
|
| 524 | *
|
| 525 | * Called by having() or orhaving()
|
| 526 | *
|
| 527 | * @access private
|
| 528 | * @param string
|
| 529 | * @param string
|
| 530 | * @return object
|
| 531 | */
|
| 532 | function _having($key, $value = '', $type = 'AND ')
|
| 533 | {
|
| 534 | if ( ! is_array($key))
|
| 535 | {
|
| 536 | $key = array($key => $value);
|
| 537 | }
|
| 538 |
|
| 539 | foreach ($key as $k => $v)
|
| 540 | {
|
| 541 | $prefix = (count($this->ar_having) == 0) ? '' : $type;
|
| 542 |
|
| 543 | if ($v != '')
|
| 544 | {
|
| 545 | $v = ' '.$this->escape($v);
|
| 546 | }
|
| 547 |
|
| 548 | $this->ar_having[] = $prefix.$k.$v;
|
| 549 | }
|
| 550 | return $this;
|
| 551 | }
|
| 552 |
|
| 553 | // --------------------------------------------------------------------
|
| 554 |
|
| 555 | /**
|
| 556 | * Sets the ORDER BY value
|
| 557 | *
|
| 558 | * @access public
|
| 559 | * @param string
|
| 560 | * @param string direction: asc or desc
|
| 561 | * @return object
|
| 562 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 563 | function order_by($orderby, $direction = '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 564 | {
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 565 | if (strtolower($direction) == 'random')
|
| 566 | {
|
| 567 | $orderby = ''; // Random results want or don't need a field name
|
| 568 | $direction = $this->_random_keyword;
|
| 569 | }
|
| 570 | elseif (trim($direction) != '')
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 571 | {
|
Derek Allard | 9278249 | 2007-08-10 11:26:01 +0000 | [diff] [blame] | 572 | $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 573 | }
|
| 574 |
|
| 575 | $this->ar_orderby[] = $orderby.$direction;
|
| 576 | return $this;
|
| 577 | }
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 578 |
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 579 | // --------------------------------------------------------------------
|
| 580 |
|
| 581 | /**
|
| 582 | * orderby() is an alias of order_by()
|
| 583 | * this function is here for backwards compatibility, as
|
| 584 | * orderby() has been deprecated
|
| 585 | */
|
| 586 | function orderby($orderby, $direction = '')
|
| 587 | {
|
| 588 | return $this->order_by($orderby, $direction);
|
| 589 | }
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 590 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 591 | // --------------------------------------------------------------------
|
| 592 |
|
| 593 | /**
|
| 594 | * Sets the LIMIT value
|
| 595 | *
|
| 596 | * @access public
|
| 597 | * @param integer the limit value
|
| 598 | * @param integer the offset value
|
| 599 | * @return object
|
| 600 | */
|
| 601 | function limit($value, $offset = '')
|
| 602 | {
|
| 603 | $this->ar_limit = $value;
|
| 604 |
|
| 605 | if ($offset != '')
|
| 606 | $this->ar_offset = $offset;
|
| 607 |
|
| 608 | return $this;
|
| 609 | }
|
| 610 |
|
| 611 | // --------------------------------------------------------------------
|
| 612 |
|
| 613 | /**
|
| 614 | * Sets the OFFSET value
|
| 615 | *
|
| 616 | * @access public
|
| 617 | * @param integer the offset value
|
| 618 | * @return object
|
| 619 | */
|
| 620 | function offset($value)
|
| 621 | {
|
| 622 | $this->ar_offset = $value;
|
| 623 | return $this;
|
| 624 | }
|
| 625 |
|
| 626 | // --------------------------------------------------------------------
|
| 627 |
|
| 628 | /**
|
| 629 | * The "set" function. Allows key/value pairs to be set for inserting or updating
|
| 630 | *
|
| 631 | * @access public
|
| 632 | * @param mixed
|
| 633 | * @param string
|
| 634 | * @return object
|
| 635 | */
|
| 636 | function set($key, $value = '')
|
| 637 | {
|
| 638 | $key = $this->_object_to_array($key);
|
| 639 |
|
| 640 | if ( ! is_array($key))
|
| 641 | {
|
| 642 | $key = array($key => $value);
|
| 643 | }
|
| 644 |
|
| 645 | foreach ($key as $k => $v)
|
| 646 | {
|
| 647 | $this->ar_set[$k] = $this->escape($v);
|
| 648 | }
|
| 649 |
|
| 650 | return $this;
|
| 651 | }
|
| 652 |
|
| 653 | // --------------------------------------------------------------------
|
| 654 |
|
| 655 | /**
|
| 656 | * Get
|
| 657 | *
|
| 658 | * Compiles the select statement based on the other functions called
|
| 659 | * and runs the query
|
| 660 | *
|
| 661 | * @access public
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 662 | * @param string the table
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 663 | * @param string the limit clause
|
| 664 | * @param string the offset clause
|
| 665 | * @return object
|
| 666 | */
|
| 667 | function get($table = '', $limit = null, $offset = null)
|
| 668 | {
|
| 669 | if ($table != '')
|
| 670 | {
|
| 671 | $this->from($table);
|
| 672 | }
|
| 673 |
|
| 674 | if ( ! is_null($limit))
|
| 675 | {
|
| 676 | $this->limit($limit, $offset);
|
| 677 | }
|
| 678 |
|
| 679 | $sql = $this->_compile_select();
|
| 680 |
|
| 681 | $result = $this->query($sql);
|
| 682 | $this->_reset_select();
|
| 683 | return $result;
|
| 684 | }
|
| 685 |
|
| 686 | // --------------------------------------------------------------------
|
| 687 |
|
| 688 | /**
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 689 | * "Count All Results" query
|
| 690 | *
|
| 691 | * Generates a platform-specific query string that counts all records
|
| 692 | * returned by an Active Record query.
|
| 693 | *
|
| 694 | * @access public
|
| 695 | * @param string
|
| 696 | * @return string
|
| 697 | */
|
| 698 | function count_all_results($table = '')
|
| 699 | {
|
| 700 | if ($table != '')
|
| 701 | {
|
| 702 | $this->from($table);
|
| 703 | }
|
| 704 |
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 705 | $sql = $this->_compile_select($this->_count_string);
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 706 |
|
| 707 | $query = $this->query($sql);
|
| 708 | $this->_reset_select();
|
| 709 |
|
| 710 | if ($query->num_rows() == 0)
|
| 711 | {
|
| 712 | return '0';
|
| 713 | }
|
| 714 |
|
| 715 | $row = $query->row();
|
| 716 | return $row->numrows;
|
| 717 | }
|
| 718 |
|
| 719 | // --------------------------------------------------------------------
|
| 720 |
|
| 721 | /**
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 722 | * Get_Where
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 723 | *
|
| 724 | * Allows the where clause, limit and offset to be added directly
|
| 725 | *
|
| 726 | * @access public
|
| 727 | * @param string the where clause
|
| 728 | * @param string the limit clause
|
| 729 | * @param string the offset clause
|
| 730 | * @return object
|
| 731 | */
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 732 | function get_where($table = '', $where = null, $limit = null, $offset = null)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 733 | {
|
| 734 | if ($table != '')
|
| 735 | {
|
| 736 | $this->from($table);
|
| 737 | }
|
| 738 |
|
| 739 | if ( ! is_null($where))
|
| 740 | {
|
| 741 | $this->where($where);
|
| 742 | }
|
| 743 |
|
| 744 | if ( ! is_null($limit))
|
| 745 | {
|
| 746 | $this->limit($limit, $offset);
|
| 747 | }
|
| 748 |
|
| 749 | $sql = $this->_compile_select();
|
| 750 |
|
| 751 | $result = $this->query($sql);
|
| 752 | $this->_reset_select();
|
| 753 | return $result;
|
| 754 | }
|
Derek Allard | 218e2bc | 2007-12-17 21:18:14 +0000 | [diff] [blame] | 755 |
|
| 756 | // --------------------------------------------------------------------
|
| 757 |
|
| 758 | /**
|
| 759 | * getwhere() is an alias of get_where()
|
| 760 | * this function is here for backwards compatibility, as
|
| 761 | * getwhere() has been deprecated
|
| 762 | */
|
| 763 | function getwhere($table = '', $where = null, $limit = null, $offset = null)
|
| 764 | {
|
| 765 | return $this->get_where($table, $where, $limit, $offset);
|
| 766 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 767 |
|
| 768 | // --------------------------------------------------------------------
|
| 769 |
|
| 770 | /**
|
| 771 | * Insert
|
| 772 | *
|
| 773 | * Compiles an insert string and runs the query
|
| 774 | *
|
| 775 | * @access public
|
| 776 | * @param string the table to retrieve the results from
|
| 777 | * @param array an associative array of insert values
|
| 778 | * @return object
|
| 779 | */
|
| 780 | function insert($table = '', $set = NULL)
|
| 781 | {
|
| 782 | if ( ! is_null($set))
|
| 783 | {
|
| 784 | $this->set($set);
|
| 785 | }
|
| 786 |
|
| 787 | if (count($this->ar_set) == 0)
|
| 788 | {
|
| 789 | if ($this->db_debug)
|
| 790 | {
|
| 791 | return $this->display_error('db_must_use_set');
|
| 792 | }
|
| 793 | return FALSE;
|
| 794 | }
|
| 795 |
|
| 796 | if ($table == '')
|
| 797 | {
|
| 798 | if ( ! isset($this->ar_from[0]))
|
| 799 | {
|
| 800 | if ($this->db_debug)
|
| 801 | {
|
| 802 | return $this->display_error('db_must_set_table');
|
| 803 | }
|
| 804 | return FALSE;
|
| 805 | }
|
| 806 |
|
| 807 | $table = $this->ar_from[0];
|
| 808 | }
|
| 809 |
|
| 810 | $sql = $this->_insert($this->dbprefix.$table, array_keys($this->ar_set), array_values($this->ar_set));
|
| 811 |
|
| 812 | $this->_reset_write();
|
| 813 | return $this->query($sql);
|
| 814 | }
|
| 815 |
|
| 816 | // --------------------------------------------------------------------
|
| 817 |
|
| 818 | /**
|
| 819 | * Update
|
| 820 | *
|
| 821 | * Compiles an update string and runs the query
|
| 822 | *
|
| 823 | * @access public
|
| 824 | * @param string the table to retrieve the results from
|
| 825 | * @param array an associative array of update values
|
| 826 | * @param mixed the where clause
|
| 827 | * @return object
|
| 828 | */
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 829 | function update($table = '', $set = NULL, $where = null, $limit = NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 830 | {
|
| 831 | if ( ! is_null($set))
|
| 832 | {
|
| 833 | $this->set($set);
|
| 834 | }
|
| 835 |
|
| 836 | if (count($this->ar_set) == 0)
|
| 837 | {
|
| 838 | if ($this->db_debug)
|
| 839 | {
|
| 840 | return $this->display_error('db_must_use_set');
|
| 841 | }
|
| 842 | return FALSE;
|
| 843 | }
|
| 844 |
|
| 845 | if ($table == '')
|
| 846 | {
|
| 847 | if ( ! isset($this->ar_from[0]))
|
| 848 | {
|
| 849 | if ($this->db_debug)
|
| 850 | {
|
| 851 | return $this->display_error('db_must_set_table');
|
| 852 | }
|
| 853 | return FALSE;
|
| 854 | }
|
| 855 |
|
| 856 | $table = $this->ar_from[0];
|
| 857 | }
|
| 858 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame^] | 859 | if ($where != NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 860 | {
|
| 861 | $this->where($where);
|
| 862 | }
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 863 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame^] | 864 | if ($limit != NULL)
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 865 | {
|
| 866 | $this->limit($limit);
|
| 867 | }
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 868 |
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 869 | $sql = $this->_update($this->dbprefix.$table, $this->ar_set, $this->ar_where, $this->ar_limit);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 870 |
|
| 871 | $this->_reset_write();
|
| 872 | return $this->query($sql);
|
| 873 | }
|
| 874 |
|
| 875 | // --------------------------------------------------------------------
|
| 876 |
|
| 877 | /**
|
| 878 | * Delete
|
| 879 | *
|
| 880 | * Compiles a delete string and runs the query
|
| 881 | *
|
| 882 | * @access public
|
| 883 | * @param string the table to retrieve the results from
|
| 884 | * @param mixed the where clause
|
| 885 | * @return object
|
| 886 | */
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame^] | 887 | function delete($table = '', $where = '', $limit = NULL)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 888 | {
|
| 889 | if ($table == '')
|
| 890 | {
|
| 891 | if ( ! isset($this->ar_from[0]))
|
| 892 | {
|
| 893 | if ($this->db_debug)
|
| 894 | {
|
| 895 | return $this->display_error('db_must_set_table');
|
| 896 | }
|
| 897 | return FALSE;
|
| 898 | }
|
| 899 |
|
| 900 | $table = $this->ar_from[0];
|
| 901 | }
|
| 902 |
|
| 903 | if ($where != '')
|
| 904 | {
|
| 905 | $this->where($where);
|
| 906 | }
|
| 907 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame^] | 908 | if ($limit != NULL)
|
| 909 | {
|
| 910 | $this->limit($limit);
|
| 911 | }
|
| 912 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 913 | if (count($this->ar_where) == 0)
|
| 914 | {
|
| 915 | if ($this->db_debug)
|
| 916 | {
|
| 917 | return $this->display_error('db_del_must_use_where');
|
| 918 | }
|
| 919 | return FALSE;
|
| 920 | }
|
| 921 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame^] | 922 | $sql = $this->_delete($this->dbprefix.$table, $this->ar_where, $this->ar_limit);
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 923 |
|
| 924 | $this->_reset_write();
|
| 925 | return $this->query($sql);
|
| 926 | }
|
| 927 |
|
| 928 | // --------------------------------------------------------------------
|
| 929 |
|
| 930 | /**
|
| 931 | * Use Table - DEPRECATED
|
| 932 | *
|
| 933 | * @deprecated use $this->db->from instead
|
| 934 | */
|
| 935 | function use_table($table)
|
| 936 | {
|
| 937 | return $this->from($table);
|
| 938 | return $this;
|
| 939 | }
|
| 940 |
|
| 941 | // --------------------------------------------------------------------
|
| 942 |
|
| 943 | /**
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 944 | * Tests whether the string has an SQL operator
|
| 945 | *
|
| 946 | * @access private
|
| 947 | * @param string
|
| 948 | * @return bool
|
| 949 | */
|
| 950 | function _has_operator($str)
|
| 951 | {
|
| 952 | $str = trim($str);
|
| 953 | if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
|
| 954 | {
|
| 955 | return FALSE;
|
| 956 | }
|
| 957 |
|
| 958 | return TRUE;
|
| 959 | }
|
| 960 |
|
| 961 | // --------------------------------------------------------------------
|
| 962 |
|
| 963 | /**
|
| 964 | * Compile the SELECT statement
|
| 965 | *
|
| 966 | * Generates a query string based on which functions were used.
|
| 967 | * Should not be called directly. The get() function calls it.
|
| 968 | *
|
| 969 | * @access private
|
| 970 | * @return string
|
| 971 | */
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 972 | function _compile_select($select_override = FALSE)
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 973 | {
|
| 974 | $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
|
| 975 |
|
| 976 | $sql .= (count($this->ar_select) == 0) ? '*' : implode(', ', $this->ar_select);
|
| 977 |
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 978 | if ($select_override !== FALSE)
|
| 979 | {
|
| 980 | $sql = $select_override;
|
| 981 | }
|
| 982 |
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 983 | if (count($this->ar_from) > 0)
|
| 984 | {
|
| 985 | $sql .= "\nFROM ";
|
| 986 | $sql .= implode(', ', $this->ar_from);
|
| 987 | }
|
| 988 |
|
| 989 | if (count($this->ar_join) > 0)
|
| 990 | {
|
| 991 | $sql .= "\n";
|
| 992 | $sql .= implode("\n", $this->ar_join);
|
| 993 | }
|
| 994 |
|
| 995 | if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
|
| 996 | {
|
| 997 | $sql .= "\nWHERE ";
|
| 998 | }
|
| 999 |
|
| 1000 | $sql .= implode("\n", $this->ar_where);
|
| 1001 |
|
| 1002 | if (count($this->ar_like) > 0)
|
| 1003 | {
|
| 1004 | if (count($this->ar_where) > 0)
|
| 1005 | {
|
| 1006 | $sql .= " AND ";
|
| 1007 | }
|
| 1008 |
|
| 1009 | $sql .= implode("\n", $this->ar_like);
|
| 1010 | }
|
| 1011 |
|
| 1012 | if (count($this->ar_groupby) > 0)
|
| 1013 | {
|
| 1014 | $sql .= "\nGROUP BY ";
|
| 1015 | $sql .= implode(', ', $this->ar_groupby);
|
| 1016 | }
|
| 1017 |
|
| 1018 | if (count($this->ar_having) > 0)
|
| 1019 | {
|
| 1020 | $sql .= "\nHAVING ";
|
| 1021 | $sql .= implode("\n", $this->ar_having);
|
| 1022 | }
|
| 1023 |
|
| 1024 | if (count($this->ar_orderby) > 0)
|
| 1025 | {
|
| 1026 | $sql .= "\nORDER BY ";
|
| 1027 | $sql .= implode(', ', $this->ar_orderby);
|
| 1028 |
|
| 1029 | if ($this->ar_order !== FALSE)
|
| 1030 | {
|
| 1031 | $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
|
| 1032 | }
|
| 1033 | }
|
| 1034 |
|
| 1035 | if (is_numeric($this->ar_limit))
|
| 1036 | {
|
| 1037 | $sql .= "\n";
|
| 1038 | $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
|
| 1039 | }
|
| 1040 |
|
| 1041 | return $sql;
|
| 1042 | }
|
| 1043 |
|
| 1044 | // --------------------------------------------------------------------
|
| 1045 |
|
| 1046 | /**
|
| 1047 | * Object to Array
|
| 1048 | *
|
| 1049 | * Takes an object as input and converts the class variables to array key/vals
|
| 1050 | *
|
| 1051 | * @access public
|
| 1052 | * @param object
|
| 1053 | * @return array
|
| 1054 | */
|
| 1055 | function _object_to_array($object)
|
| 1056 | {
|
| 1057 | if ( ! is_object($object))
|
| 1058 | {
|
| 1059 | return $object;
|
| 1060 | }
|
| 1061 |
|
| 1062 | $array = array();
|
| 1063 | foreach (get_object_vars($object) as $key => $val)
|
| 1064 | {
|
| 1065 | if ( ! is_object($val) AND ! is_array($val))
|
| 1066 | {
|
| 1067 | $array[$key] = $val;
|
| 1068 | }
|
| 1069 | }
|
| 1070 |
|
| 1071 | return $array;
|
| 1072 | }
|
| 1073 |
|
| 1074 | // --------------------------------------------------------------------
|
| 1075 |
|
| 1076 | /**
|
| 1077 | * Resets the active record values. Called by the get() function
|
| 1078 | *
|
| 1079 | * @access private
|
| 1080 | * @return void
|
| 1081 | */
|
| 1082 | function _reset_select()
|
| 1083 | {
|
| 1084 | $this->ar_select = array();
|
| 1085 | $this->ar_distinct = FALSE;
|
| 1086 | $this->ar_from = array();
|
| 1087 | $this->ar_join = array();
|
| 1088 | $this->ar_where = array();
|
| 1089 | $this->ar_like = array();
|
| 1090 | $this->ar_groupby = array();
|
| 1091 | $this->ar_having = array();
|
| 1092 | $this->ar_limit = FALSE;
|
| 1093 | $this->ar_offset = FALSE;
|
| 1094 | $this->ar_order = FALSE;
|
| 1095 | $this->ar_orderby = array();
|
Derek Allard | c693551 | 2007-12-19 14:23:19 +0000 | [diff] [blame] | 1096 | $this->ar_wherein = array();
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1097 | }
|
| 1098 |
|
| 1099 | // --------------------------------------------------------------------
|
| 1100 |
|
| 1101 | /**
|
| 1102 | * Resets the active record "write" values.
|
| 1103 | *
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame^] | 1104 | * Called by the insert() update() and delete() functions
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1105 | *
|
| 1106 | * @access private
|
| 1107 | * @return void
|
| 1108 | */
|
| 1109 | function _reset_write()
|
| 1110 | {
|
| 1111 | $this->ar_set = array();
|
| 1112 | $this->ar_from = array();
|
| 1113 | $this->ar_where = array();
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 1114 | $this->ar_limit = FALSE;
|
Derek Allard | 09de185 | 2007-02-14 01:35:56 +0000 | [diff] [blame] | 1115 | }
|
| 1116 |
|
| 1117 | }
|
| 1118 |
|
admin | ac94f38 | 2006-09-24 20:28:12 +0000 | [diff] [blame] | 1119 | ?> |