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