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