PageRenderTime 38ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/system/codeigniter/system/database/DB_active_rec.php

https://bitbucket.org/tdevonshire/hoolux
PHP | 2079 lines | 1079 code | 329 blank | 671 comment | 192 complexity | 36f94f4ab7eee4867919555081583c02 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.2.4 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Active Record Class
  18. *
  19. * This is the platform-independent base Active Record implementation class.
  20. *
  21. * @package CodeIgniter
  22. * @subpackage Drivers
  23. * @category Database
  24. * @author EllisLab Dev Team
  25. * @link http://codeigniter.com/user_guide/database/
  26. */
  27. class CI_DB_active_record extends CI_DB_driver {
  28. var $ar_select = array();
  29. var $ar_distinct = FALSE;
  30. var $ar_from = array();
  31. var $ar_join = array();
  32. var $ar_where = array();
  33. var $ar_like = array();
  34. var $ar_groupby = array();
  35. var $ar_having = array();
  36. var $ar_keys = array();
  37. var $ar_limit = FALSE;
  38. var $ar_offset = FALSE;
  39. var $ar_order = FALSE;
  40. var $ar_orderby = array();
  41. var $ar_set = array();
  42. var $ar_wherein = array();
  43. var $ar_aliased_tables = array();
  44. var $ar_store_array = array();
  45. // Active Record Caching variables
  46. var $ar_caching = FALSE;
  47. var $ar_cache_exists = array();
  48. var $ar_cache_select = array();
  49. var $ar_cache_from = array();
  50. var $ar_cache_join = array();
  51. var $ar_cache_where = array();
  52. var $ar_cache_like = array();
  53. var $ar_cache_groupby = array();
  54. var $ar_cache_having = array();
  55. var $ar_cache_orderby = array();
  56. var $ar_cache_set = array();
  57. var $ar_no_escape = array();
  58. var $ar_cache_no_escape = array();
  59. // --------------------------------------------------------------------
  60. /**
  61. * Select
  62. *
  63. * Generates the SELECT portion of the query
  64. *
  65. * @access public
  66. * @param string
  67. * @return object
  68. */
  69. function select($select = '*', $escape = NULL)
  70. {
  71. if (is_string($select))
  72. {
  73. $select = explode(',', $select);
  74. }
  75. foreach ($select as $val)
  76. {
  77. $val = trim($val);
  78. if ($val != '')
  79. {
  80. $this->ar_select[] = $val;
  81. $this->ar_no_escape[] = $escape;
  82. if ($this->ar_caching === TRUE)
  83. {
  84. $this->ar_cache_select[] = $val;
  85. $this->ar_cache_exists[] = 'select';
  86. $this->ar_cache_no_escape[] = $escape;
  87. }
  88. }
  89. }
  90. return $this;
  91. }
  92. // --------------------------------------------------------------------
  93. /**
  94. * Select Max
  95. *
  96. * Generates a SELECT MAX(field) portion of a query
  97. *
  98. * @access public
  99. * @param string the field
  100. * @param string an alias
  101. * @return object
  102. */
  103. function select_max($select = '', $alias = '')
  104. {
  105. return $this->_max_min_avg_sum($select, $alias, 'MAX');
  106. }
  107. // --------------------------------------------------------------------
  108. /**
  109. * Select Min
  110. *
  111. * Generates a SELECT MIN(field) portion of a query
  112. *
  113. * @access public
  114. * @param string the field
  115. * @param string an alias
  116. * @return object
  117. */
  118. function select_min($select = '', $alias = '')
  119. {
  120. return $this->_max_min_avg_sum($select, $alias, 'MIN');
  121. }
  122. // --------------------------------------------------------------------
  123. /**
  124. * Select Average
  125. *
  126. * Generates a SELECT AVG(field) portion of a query
  127. *
  128. * @access public
  129. * @param string the field
  130. * @param string an alias
  131. * @return object
  132. */
  133. function select_avg($select = '', $alias = '')
  134. {
  135. return $this->_max_min_avg_sum($select, $alias, 'AVG');
  136. }
  137. // --------------------------------------------------------------------
  138. /**
  139. * Select Sum
  140. *
  141. * Generates a SELECT SUM(field) portion of a query
  142. *
  143. * @access public
  144. * @param string the field
  145. * @param string an alias
  146. * @return object
  147. */
  148. function select_sum($select = '', $alias = '')
  149. {
  150. return $this->_max_min_avg_sum($select, $alias, 'SUM');
  151. }
  152. // --------------------------------------------------------------------
  153. /**
  154. * Processing Function for the four functions above:
  155. *
  156. * select_max()
  157. * select_min()
  158. * select_avg()
  159. * select_sum()
  160. *
  161. * @access public
  162. * @param string the field
  163. * @param string an alias
  164. * @return object
  165. */
  166. function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
  167. {
  168. if ( ! is_string($select) OR $select == '')
  169. {
  170. $this->display_error('db_invalid_query');
  171. }
  172. $type = strtoupper($type);
  173. if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
  174. {
  175. show_error('Invalid function type: '.$type);
  176. }
  177. if ($alias == '')
  178. {
  179. $alias = $this->_create_alias_from_table(trim($select));
  180. }
  181. $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$alias;
  182. $this->ar_select[] = $sql;
  183. $this->ar_no_escape[] = NULL;
  184. if ($this->ar_caching === TRUE)
  185. {
  186. $this->ar_cache_select[] = $sql;
  187. $this->ar_cache_exists[] = 'select';
  188. }
  189. return $this;
  190. }
  191. // --------------------------------------------------------------------
  192. /**
  193. * Determines the alias name based on the table
  194. *
  195. * @access private
  196. * @param string
  197. * @return string
  198. */
  199. function _create_alias_from_table($item)
  200. {
  201. if (strpos($item, '.') !== FALSE)
  202. {
  203. return end(explode('.', $item));
  204. }
  205. return $item;
  206. }
  207. // --------------------------------------------------------------------
  208. /**
  209. * DISTINCT
  210. *
  211. * Sets a flag which tells the query string compiler to add DISTINCT
  212. *
  213. * @access public
  214. * @param bool
  215. * @return object
  216. */
  217. function distinct($val = TRUE)
  218. {
  219. $this->ar_distinct = (is_bool($val)) ? $val : TRUE;
  220. return $this;
  221. }
  222. // --------------------------------------------------------------------
  223. /**
  224. * From
  225. *
  226. * Generates the FROM portion of the query
  227. *
  228. * @access public
  229. * @param mixed can be a string or array
  230. * @return object
  231. */
  232. function from($from)
  233. {
  234. foreach ((array)$from as $val)
  235. {
  236. if (strpos($val, ',') !== FALSE)
  237. {
  238. foreach (explode(',', $val) as $v)
  239. {
  240. $v = trim($v);
  241. $this->_track_aliases($v);
  242. $this->ar_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
  243. if ($this->ar_caching === TRUE)
  244. {
  245. $this->ar_cache_from[] = $this->_protect_identifiers($v, TRUE, NULL, FALSE);
  246. $this->ar_cache_exists[] = 'from';
  247. }
  248. }
  249. }
  250. else
  251. {
  252. $val = trim($val);
  253. // Extract any aliases that might exist. We use this information
  254. // in the _protect_identifiers to know whether to add a table prefix
  255. $this->_track_aliases($val);
  256. $this->ar_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
  257. if ($this->ar_caching === TRUE)
  258. {
  259. $this->ar_cache_from[] = $this->_protect_identifiers($val, TRUE, NULL, FALSE);
  260. $this->ar_cache_exists[] = 'from';
  261. }
  262. }
  263. }
  264. return $this;
  265. }
  266. // --------------------------------------------------------------------
  267. /**
  268. * Join
  269. *
  270. * Generates the JOIN portion of the query
  271. *
  272. * @access public
  273. * @param string
  274. * @param string the join condition
  275. * @param string the type of join
  276. * @return object
  277. */
  278. function join($table, $cond, $type = '')
  279. {
  280. if ($type != '')
  281. {
  282. $type = strtoupper(trim($type));
  283. if ( ! in_array($type, array('LEFT', 'RIGHT', 'OUTER', 'INNER', 'LEFT OUTER', 'RIGHT OUTER')))
  284. {
  285. $type = '';
  286. }
  287. else
  288. {
  289. $type .= ' ';
  290. }
  291. }
  292. // Extract any aliases that might exist. We use this information
  293. // in the _protect_identifiers to know whether to add a table prefix
  294. $this->_track_aliases($table);
  295. // Strip apart the condition and protect the identifiers
  296. if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
  297. {
  298. $match[1] = $this->_protect_identifiers($match[1]);
  299. $match[3] = $this->_protect_identifiers($match[3]);
  300. $cond = $match[1].$match[2].$match[3];
  301. }
  302. // Assemble the JOIN statement
  303. $join = $type.'JOIN '.$this->_protect_identifiers($table, TRUE, NULL, FALSE).' ON '.$cond;
  304. $this->ar_join[] = $join;
  305. if ($this->ar_caching === TRUE)
  306. {
  307. $this->ar_cache_join[] = $join;
  308. $this->ar_cache_exists[] = 'join';
  309. }
  310. return $this;
  311. }
  312. // --------------------------------------------------------------------
  313. /**
  314. * Where
  315. *
  316. * Generates the WHERE portion of the query. Separates
  317. * multiple calls with AND
  318. *
  319. * @access public
  320. * @param mixed
  321. * @param mixed
  322. * @return object
  323. */
  324. function where($key, $value = NULL, $escape = TRUE)
  325. {
  326. return $this->_where($key, $value, 'AND ', $escape);
  327. }
  328. // --------------------------------------------------------------------
  329. /**
  330. * OR Where
  331. *
  332. * Generates the WHERE portion of the query. Separates
  333. * multiple calls with OR
  334. *
  335. * @access public
  336. * @param mixed
  337. * @param mixed
  338. * @return object
  339. */
  340. function or_where($key, $value = NULL, $escape = TRUE)
  341. {
  342. return $this->_where($key, $value, 'OR ', $escape);
  343. }
  344. // --------------------------------------------------------------------
  345. /**
  346. * Where
  347. *
  348. * Called by where() or orwhere()
  349. *
  350. * @access private
  351. * @param mixed
  352. * @param mixed
  353. * @param string
  354. * @return object
  355. */
  356. function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
  357. {
  358. if ( ! is_array($key))
  359. {
  360. $key = array($key => $value);
  361. }
  362. // If the escape value was not set will will base it on the global setting
  363. if ( ! is_bool($escape))
  364. {
  365. $escape = $this->_protect_identifiers;
  366. }
  367. foreach ($key as $k => $v)
  368. {
  369. $prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type;
  370. if (is_null($v) && ! $this->_has_operator($k))
  371. {
  372. // value appears not to have been set, assign the test to IS NULL
  373. $k .= ' IS NULL';
  374. }
  375. if ( ! is_null($v))
  376. {
  377. if ($escape === TRUE)
  378. {
  379. $k = $this->_protect_identifiers($k, FALSE, $escape);
  380. $v = ' '.$this->escape($v);
  381. }
  382. if ( ! $this->_has_operator($k))
  383. {
  384. $k .= ' = ';
  385. }
  386. }
  387. else
  388. {
  389. $k = $this->_protect_identifiers($k, FALSE, $escape);
  390. }
  391. $this->ar_where[] = $prefix.$k.$v;
  392. if ($this->ar_caching === TRUE)
  393. {
  394. $this->ar_cache_where[] = $prefix.$k.$v;
  395. $this->ar_cache_exists[] = 'where';
  396. }
  397. }
  398. return $this;
  399. }
  400. // --------------------------------------------------------------------
  401. /**
  402. * Where_in
  403. *
  404. * Generates a WHERE field IN ('item', 'item') SQL query joined with
  405. * AND if appropriate
  406. *
  407. * @access public
  408. * @param string The field to search
  409. * @param array The values searched on
  410. * @return object
  411. */
  412. function where_in($key = NULL, $values = NULL)
  413. {
  414. return $this->_where_in($key, $values);
  415. }
  416. // --------------------------------------------------------------------
  417. /**
  418. * Where_in_or
  419. *
  420. * Generates a WHERE field IN ('item', 'item') SQL query joined with
  421. * OR if appropriate
  422. *
  423. * @access public
  424. * @param string The field to search
  425. * @param array The values searched on
  426. * @return object
  427. */
  428. function or_where_in($key = NULL, $values = NULL)
  429. {
  430. return $this->_where_in($key, $values, FALSE, 'OR ');
  431. }
  432. // --------------------------------------------------------------------
  433. /**
  434. * Where_not_in
  435. *
  436. * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
  437. * with AND if appropriate
  438. *
  439. * @access public
  440. * @param string The field to search
  441. * @param array The values searched on
  442. * @return object
  443. */
  444. function where_not_in($key = NULL, $values = NULL)
  445. {
  446. return $this->_where_in($key, $values, TRUE);
  447. }
  448. // --------------------------------------------------------------------
  449. /**
  450. * Where_not_in_or
  451. *
  452. * Generates a WHERE field NOT IN ('item', 'item') SQL query joined
  453. * with OR if appropriate
  454. *
  455. * @access public
  456. * @param string The field to search
  457. * @param array The values searched on
  458. * @return object
  459. */
  460. function or_where_not_in($key = NULL, $values = NULL)
  461. {
  462. return $this->_where_in($key, $values, TRUE, 'OR ');
  463. }
  464. // --------------------------------------------------------------------
  465. /**
  466. * Where_in
  467. *
  468. * Called by where_in, where_in_or, where_not_in, where_not_in_or
  469. *
  470. * @access public
  471. * @param string The field to search
  472. * @param array The values searched on
  473. * @param boolean If the statement would be IN or NOT IN
  474. * @param string
  475. * @return object
  476. */
  477. function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
  478. {
  479. if ($key === NULL OR $values === NULL)
  480. {
  481. return;
  482. }
  483. if ( ! is_array($values))
  484. {
  485. $values = array($values);
  486. }
  487. $not = ($not) ? ' NOT' : '';
  488. foreach ($values as $value)
  489. {
  490. $this->ar_wherein[] = $this->escape($value);
  491. }
  492. $prefix = (count($this->ar_where) == 0) ? '' : $type;
  493. $where_in = $prefix . $this->_protect_identifiers($key) . $not . " IN (" . implode(", ", $this->ar_wherein) . ") ";
  494. $this->ar_where[] = $where_in;
  495. if ($this->ar_caching === TRUE)
  496. {
  497. $this->ar_cache_where[] = $where_in;
  498. $this->ar_cache_exists[] = 'where';
  499. }
  500. // reset the array for multiple calls
  501. $this->ar_wherein = array();
  502. return $this;
  503. }
  504. // --------------------------------------------------------------------
  505. /**
  506. * Like
  507. *
  508. * Generates a %LIKE% portion of the query. Separates
  509. * multiple calls with AND
  510. *
  511. * @access public
  512. * @param mixed
  513. * @param mixed
  514. * @return object
  515. */
  516. function like($field, $match = '', $side = 'both')
  517. {
  518. return $this->_like($field, $match, 'AND ', $side);
  519. }
  520. // --------------------------------------------------------------------
  521. /**
  522. * Not Like
  523. *
  524. * Generates a NOT LIKE portion of the query. Separates
  525. * multiple calls with AND
  526. *
  527. * @access public
  528. * @param mixed
  529. * @param mixed
  530. * @return object
  531. */
  532. function not_like($field, $match = '', $side = 'both')
  533. {
  534. return $this->_like($field, $match, 'AND ', $side, 'NOT');
  535. }
  536. // --------------------------------------------------------------------
  537. /**
  538. * OR Like
  539. *
  540. * Generates a %LIKE% portion of the query. Separates
  541. * multiple calls with OR
  542. *
  543. * @access public
  544. * @param mixed
  545. * @param mixed
  546. * @return object
  547. */
  548. function or_like($field, $match = '', $side = 'both')
  549. {
  550. return $this->_like($field, $match, 'OR ', $side);
  551. }
  552. // --------------------------------------------------------------------
  553. /**
  554. * OR Not Like
  555. *
  556. * Generates a NOT LIKE portion of the query. Separates
  557. * multiple calls with OR
  558. *
  559. * @access public
  560. * @param mixed
  561. * @param mixed
  562. * @return object
  563. */
  564. function or_not_like($field, $match = '', $side = 'both')
  565. {
  566. return $this->_like($field, $match, 'OR ', $side, 'NOT');
  567. }
  568. // --------------------------------------------------------------------
  569. /**
  570. * Like
  571. *
  572. * Called by like() or orlike()
  573. *
  574. * @access private
  575. * @param mixed
  576. * @param mixed
  577. * @param string
  578. * @return object
  579. */
  580. function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
  581. {
  582. if ( ! is_array($field))
  583. {
  584. $field = array($field => $match);
  585. }
  586. foreach ($field as $k => $v)
  587. {
  588. $k = $this->_protect_identifiers($k);
  589. $prefix = (count($this->ar_like) == 0) ? '' : $type;
  590. $v = $this->escape_like_str($v);
  591. if ($side == 'before')
  592. {
  593. $like_statement = $prefix." $k $not LIKE '%{$v}'";
  594. }
  595. elseif ($side == 'after')
  596. {
  597. $like_statement = $prefix." $k $not LIKE '{$v}%'";
  598. }
  599. else
  600. {
  601. $like_statement = $prefix." $k $not LIKE '%{$v}%'";
  602. }
  603. // some platforms require an escape sequence definition for LIKE wildcards
  604. if ($this->_like_escape_str != '')
  605. {
  606. $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
  607. }
  608. $this->ar_like[] = $like_statement;
  609. if ($this->ar_caching === TRUE)
  610. {
  611. $this->ar_cache_like[] = $like_statement;
  612. $this->ar_cache_exists[] = 'like';
  613. }
  614. }
  615. return $this;
  616. }
  617. // --------------------------------------------------------------------
  618. /**
  619. * GROUP BY
  620. *
  621. * @access public
  622. * @param string
  623. * @return object
  624. */
  625. function group_by($by)
  626. {
  627. if (is_string($by))
  628. {
  629. $by = explode(',', $by);
  630. }
  631. foreach ($by as $val)
  632. {
  633. $val = trim($val);
  634. if ($val != '')
  635. {
  636. $this->ar_groupby[] = $this->_protect_identifiers($val);
  637. if ($this->ar_caching === TRUE)
  638. {
  639. $this->ar_cache_groupby[] = $this->_protect_identifiers($val);
  640. $this->ar_cache_exists[] = 'groupby';
  641. }
  642. }
  643. }
  644. return $this;
  645. }
  646. // --------------------------------------------------------------------
  647. /**
  648. * Sets the HAVING value
  649. *
  650. * Separates multiple calls with AND
  651. *
  652. * @access public
  653. * @param string
  654. * @param string
  655. * @return object
  656. */
  657. function having($key, $value = '', $escape = TRUE)
  658. {
  659. return $this->_having($key, $value, 'AND ', $escape);
  660. }
  661. // --------------------------------------------------------------------
  662. /**
  663. * Sets the OR HAVING value
  664. *
  665. * Separates multiple calls with OR
  666. *
  667. * @access public
  668. * @param string
  669. * @param string
  670. * @return object
  671. */
  672. function or_having($key, $value = '', $escape = TRUE)
  673. {
  674. return $this->_having($key, $value, 'OR ', $escape);
  675. }
  676. // --------------------------------------------------------------------
  677. /**
  678. * Sets the HAVING values
  679. *
  680. * Called by having() or or_having()
  681. *
  682. * @access private
  683. * @param string
  684. * @param string
  685. * @return object
  686. */
  687. function _having($key, $value = '', $type = 'AND ', $escape = TRUE)
  688. {
  689. if ( ! is_array($key))
  690. {
  691. $key = array($key => $value);
  692. }
  693. foreach ($key as $k => $v)
  694. {
  695. $prefix = (count($this->ar_having) == 0) ? '' : $type;
  696. if ($escape === TRUE)
  697. {
  698. $k = $this->_protect_identifiers($k);
  699. }
  700. if ( ! $this->_has_operator($k))
  701. {
  702. $k .= ' = ';
  703. }
  704. if ($v != '')
  705. {
  706. $v = ' '.$this->escape_str($v);
  707. }
  708. $this->ar_having[] = $prefix.$k.$v;
  709. if ($this->ar_caching === TRUE)
  710. {
  711. $this->ar_cache_having[] = $prefix.$k.$v;
  712. $this->ar_cache_exists[] = 'having';
  713. }
  714. }
  715. return $this;
  716. }
  717. // --------------------------------------------------------------------
  718. /**
  719. * Sets the ORDER BY value
  720. *
  721. * @access public
  722. * @param string
  723. * @param string direction: asc or desc
  724. * @param boolean escape: Tells _protect_identifiers() whether
  725. * we are escaping what appear to be escapable values
  726. * @return object
  727. */
  728. function order_by($orderby, $direction = '', $escape = NULL)
  729. {
  730. if (strtolower($direction) == 'random')
  731. {
  732. $orderby = ''; // Random results want or don't need a field name
  733. $direction = $this->_random_keyword;
  734. }
  735. elseif (trim($direction) != '')
  736. {
  737. $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
  738. }
  739. if (strpos($orderby, ',') !== FALSE)
  740. {
  741. $temp = array();
  742. foreach (explode(',', $orderby) as $part)
  743. {
  744. $part = trim($part);
  745. if ( ! in_array($part, $this->ar_aliased_tables))
  746. {
  747. $part = $this->_protect_identifiers(trim($part), FALSE, $escape);
  748. }
  749. $temp[] = $part;
  750. }
  751. $orderby = implode(', ', $temp);
  752. }
  753. else if ($direction != $this->_random_keyword)
  754. {
  755. $orderby = $this->_protect_identifiers($orderby);
  756. }
  757. $orderby_statement = $orderby.$direction;
  758. $this->ar_orderby[] = $orderby_statement;
  759. if ($this->ar_caching === TRUE)
  760. {
  761. $this->ar_cache_orderby[] = $orderby_statement;
  762. $this->ar_cache_exists[] = 'orderby';
  763. }
  764. return $this;
  765. }
  766. // --------------------------------------------------------------------
  767. /**
  768. * Sets the LIMIT value
  769. *
  770. * @access public
  771. * @param integer the limit value
  772. * @param integer the offset value
  773. * @return object
  774. */
  775. function limit($value, $offset = '')
  776. {
  777. $this->ar_limit = $value;
  778. if ($offset != '')
  779. {
  780. $this->ar_offset = $offset;
  781. }
  782. return $this;
  783. }
  784. // --------------------------------------------------------------------
  785. /**
  786. * Sets the OFFSET value
  787. *
  788. * @access public
  789. * @param integer the offset value
  790. * @return object
  791. */
  792. function offset($offset)
  793. {
  794. $this->ar_offset = $offset;
  795. return $this;
  796. }
  797. // --------------------------------------------------------------------
  798. /**
  799. * The "set" function. Allows key/value pairs to be set for inserting or updating
  800. *
  801. * @access public
  802. * @param mixed
  803. * @param string
  804. * @param boolean
  805. * @return object
  806. */
  807. function set($key, $value = '', $escape = TRUE)
  808. {
  809. $key = $this->_object_to_array($key);
  810. if ( ! is_array($key))
  811. {
  812. $key = array($key => $value);
  813. }
  814. foreach ($key as $k => $v)
  815. {
  816. if ($escape === FALSE)
  817. {
  818. $this->ar_set[$this->_protect_identifiers($k)] = $v;
  819. }
  820. else
  821. {
  822. $this->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
  823. }
  824. }
  825. return $this;
  826. }
  827. // --------------------------------------------------------------------
  828. /**
  829. * Get
  830. *
  831. * Compiles the select statement based on the other functions called
  832. * and runs the query
  833. *
  834. * @access public
  835. * @param string the table
  836. * @param string the limit clause
  837. * @param string the offset clause
  838. * @return object
  839. */
  840. function get($table = '', $limit = null, $offset = null)
  841. {
  842. if ($table != '')
  843. {
  844. $this->_track_aliases($table);
  845. $this->from($table);
  846. }
  847. if ( ! is_null($limit))
  848. {
  849. $this->limit($limit, $offset);
  850. }
  851. $sql = $this->_compile_select();
  852. $result = $this->query($sql);
  853. $this->_reset_select();
  854. return $result;
  855. }
  856. // --------------------------------------------------------------------
  857. /**
  858. * "Count All Results" query
  859. *
  860. * Generates a platform-specific query string that counts all records
  861. * returned by an Active Record query.
  862. *
  863. * @access public
  864. * @param string
  865. * @return string
  866. */
  867. function count_all_results($table = '')
  868. {
  869. if ($table != '')
  870. {
  871. $this->_track_aliases($table);
  872. $this->from($table);
  873. }
  874. $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
  875. $query = $this->query($sql);
  876. $this->_reset_select();
  877. if ($query->num_rows() == 0)
  878. {
  879. return 0;
  880. }
  881. $row = $query->row();
  882. return (int) $row->numrows;
  883. }
  884. // --------------------------------------------------------------------
  885. /**
  886. * Get_Where
  887. *
  888. * Allows the where clause, limit and offset to be added directly
  889. *
  890. * @access public
  891. * @param string the where clause
  892. * @param string the limit clause
  893. * @param string the offset clause
  894. * @return object
  895. */
  896. function get_where($table = '', $where = null, $limit = null, $offset = null)
  897. {
  898. if ($table != '')
  899. {
  900. $this->from($table);
  901. }
  902. if ( ! is_null($where))
  903. {
  904. $this->where($where);
  905. }
  906. if ( ! is_null($limit))
  907. {
  908. $this->limit($limit, $offset);
  909. }
  910. $sql = $this->_compile_select();
  911. $result = $this->query($sql);
  912. $this->_reset_select();
  913. return $result;
  914. }
  915. // --------------------------------------------------------------------
  916. /**
  917. * Insert_Batch
  918. *
  919. * Compiles batch insert strings and runs the queries
  920. *
  921. * @access public
  922. * @param string the table to retrieve the results from
  923. * @param array an associative array of insert values
  924. * @return object
  925. */
  926. function insert_batch($table = '', $set = NULL)
  927. {
  928. if ( ! is_null($set))
  929. {
  930. $this->set_insert_batch($set);
  931. }
  932. if (count($this->ar_set) == 0)
  933. {
  934. if ($this->db_debug)
  935. {
  936. //No valid data array. Folds in cases where keys and values did not match up
  937. return $this->display_error('db_must_use_set');
  938. }
  939. return FALSE;
  940. }
  941. if ($table == '')
  942. {
  943. if ( ! isset($this->ar_from[0]))
  944. {
  945. if ($this->db_debug)
  946. {
  947. return $this->display_error('db_must_set_table');
  948. }
  949. return FALSE;
  950. }
  951. $table = $this->ar_from[0];
  952. }
  953. // Batch this baby
  954. for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
  955. {
  956. $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
  957. //echo $sql;
  958. $this->query($sql);
  959. }
  960. $this->_reset_write();
  961. return TRUE;
  962. }
  963. // --------------------------------------------------------------------
  964. /**
  965. * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
  966. *
  967. * @access public
  968. * @param mixed
  969. * @param string
  970. * @param boolean
  971. * @return object
  972. */
  973. function set_insert_batch($key, $value = '', $escape = TRUE)
  974. {
  975. $key = $this->_object_to_array_batch($key);
  976. if ( ! is_array($key))
  977. {
  978. $key = array($key => $value);
  979. }
  980. $keys = array_keys(current($key));
  981. sort($keys);
  982. foreach ($key as $row)
  983. {
  984. if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
  985. {
  986. // batch function above returns an error on an empty array
  987. $this->ar_set[] = array();
  988. return;
  989. }
  990. ksort($row); // puts $row in the same order as our keys
  991. if ($escape === FALSE)
  992. {
  993. $this->ar_set[] = '('.implode(',', $row).')';
  994. }
  995. else
  996. {
  997. $clean = array();
  998. foreach($row as $value)
  999. {
  1000. $clean[] = $this->escape($value);
  1001. }
  1002. $this->ar_set[] = '('.implode(',', $clean).')';
  1003. }
  1004. }
  1005. foreach ($keys as $k)
  1006. {
  1007. $this->ar_keys[] = $this->_protect_identifiers($k);
  1008. }
  1009. return $this;
  1010. }
  1011. // --------------------------------------------------------------------
  1012. /**
  1013. * Insert
  1014. *
  1015. * Compiles an insert string and runs the query
  1016. *
  1017. * @access public
  1018. * @param string the table to retrieve the results from
  1019. * @param array an associative array of insert values
  1020. * @return object
  1021. */
  1022. function insert($table = '', $set = NULL)
  1023. {
  1024. if ( ! is_null($set))
  1025. {
  1026. $this->set($set);
  1027. }
  1028. if (count($this->ar_set) == 0)
  1029. {
  1030. if ($this->db_debug)
  1031. {
  1032. return $this->display_error('db_must_use_set');
  1033. }
  1034. return FALSE;
  1035. }
  1036. if ($table == '')
  1037. {
  1038. if ( ! isset($this->ar_from[0]))
  1039. {
  1040. if ($this->db_debug)
  1041. {
  1042. return $this->display_error('db_must_set_table');
  1043. }
  1044. return FALSE;
  1045. }
  1046. $table = $this->ar_from[0];
  1047. }
  1048. $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
  1049. $this->_reset_write();
  1050. return $this->query($sql);
  1051. }
  1052. function replace($table = '', $set = NULL)
  1053. {
  1054. if ( ! is_null($set))
  1055. {
  1056. $this->set($set);
  1057. }
  1058. if (count($this->ar_set) == 0)
  1059. {
  1060. if ($this->db_debug)
  1061. {
  1062. return $this->display_error('db_must_use_set');
  1063. }
  1064. return FALSE;
  1065. }
  1066. if ($table == '')
  1067. {
  1068. if ( ! isset($this->ar_from[0]))
  1069. {
  1070. if ($this->db_debug)
  1071. {
  1072. return $this->display_error('db_must_set_table');
  1073. }
  1074. return FALSE;
  1075. }
  1076. $table = $this->ar_from[0];
  1077. }
  1078. $sql = $this->_replace($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
  1079. $this->_reset_write();
  1080. return $this->query($sql);
  1081. }
  1082. // --------------------------------------------------------------------
  1083. /**
  1084. * Update
  1085. *
  1086. * Compiles an update string and runs the query
  1087. *
  1088. * @access public
  1089. * @param string the table to retrieve the results from
  1090. * @param array an associative array of update values
  1091. * @param mixed the where clause
  1092. * @return object
  1093. */
  1094. function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
  1095. {
  1096. // Combine any cached components with the current statements
  1097. $this->_merge_cache();
  1098. if ( ! is_null($set))
  1099. {
  1100. $this->set($set);
  1101. }
  1102. if (count($this->ar_set) == 0)
  1103. {
  1104. if ($this->db_debug)
  1105. {
  1106. return $this->display_error('db_must_use_set');
  1107. }
  1108. return FALSE;
  1109. }
  1110. if ($table == '')
  1111. {
  1112. if ( ! isset($this->ar_from[0]))
  1113. {
  1114. if ($this->db_debug)
  1115. {
  1116. return $this->display_error('db_must_set_table');
  1117. }
  1118. return FALSE;
  1119. }
  1120. $table = $this->ar_from[0];
  1121. }
  1122. if ($where != NULL)
  1123. {
  1124. $this->where($where);
  1125. }
  1126. if ($limit != NULL)
  1127. {
  1128. $this->limit($limit);
  1129. }
  1130. $sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
  1131. $this->_reset_write();
  1132. return $this->query($sql);
  1133. }
  1134. // --------------------------------------------------------------------
  1135. /**
  1136. * Update_Batch
  1137. *
  1138. * Compiles an update string and runs the query
  1139. *
  1140. * @access public
  1141. * @param string the table to retrieve the results from
  1142. * @param array an associative array of update values
  1143. * @param string the where key
  1144. * @return object
  1145. */
  1146. function update_batch($table = '', $set = NULL, $index = NULL)
  1147. {
  1148. // Combine any cached components with the current statements
  1149. $this->_merge_cache();
  1150. if (is_null($index))
  1151. {
  1152. if ($this->db_debug)
  1153. {
  1154. return $this->display_error('db_myst_use_index');
  1155. }
  1156. return FALSE;
  1157. }
  1158. if ( ! is_null($set))
  1159. {
  1160. $this->set_update_batch($set, $index);
  1161. }
  1162. if (count($this->ar_set) == 0)
  1163. {
  1164. if ($this->db_debug)
  1165. {
  1166. return $this->display_error('db_must_use_set');
  1167. }
  1168. return FALSE;
  1169. }
  1170. if ($table == '')
  1171. {
  1172. if ( ! isset($this->ar_from[0]))
  1173. {
  1174. if ($this->db_debug)
  1175. {
  1176. return $this->display_error('db_must_set_table');
  1177. }
  1178. return FALSE;
  1179. }
  1180. $table = $this->ar_from[0];
  1181. }
  1182. // Batch this baby
  1183. for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
  1184. {
  1185. $sql = $this->_update_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->ar_set, $i, 100), $this->_protect_identifiers($index), $this->ar_where);
  1186. $this->query($sql);
  1187. }
  1188. $this->_reset_write();
  1189. }
  1190. // --------------------------------------------------------------------
  1191. /**
  1192. * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
  1193. *
  1194. * @access public
  1195. * @param array
  1196. * @param string
  1197. * @param boolean
  1198. * @return object
  1199. */
  1200. function set_update_batch($key, $index = '', $escape = TRUE)
  1201. {
  1202. $key = $this->_object_to_array_batch($key);
  1203. if ( ! is_array($key))
  1204. {
  1205. // @todo error
  1206. }
  1207. foreach ($key as $k => $v)
  1208. {
  1209. $index_set = FALSE;
  1210. $clean = array();
  1211. foreach($v as $k2 => $v2)
  1212. {
  1213. if ($k2 == $index)
  1214. {
  1215. $index_set = TRUE;
  1216. }
  1217. else
  1218. {
  1219. $not[] = $k.'-'.$v;
  1220. }
  1221. if ($escape === FALSE)
  1222. {
  1223. $clean[$this->_protect_identifiers($k2)] = $v2;
  1224. }
  1225. else
  1226. {
  1227. $clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
  1228. }
  1229. }
  1230. if ($index_set == FALSE)
  1231. {
  1232. return $this->display_error('db_batch_missing_index');
  1233. }
  1234. $this->ar_set[] = $clean;
  1235. }
  1236. return $this;
  1237. }
  1238. // --------------------------------------------------------------------
  1239. /**
  1240. * Empty Table
  1241. *
  1242. * Compiles a delete string and runs "DELETE FROM table"
  1243. *
  1244. * @access public
  1245. * @param string the table to empty
  1246. * @return object
  1247. */
  1248. function empty_table($table = '')
  1249. {
  1250. if ($table == '')
  1251. {
  1252. if ( ! isset($this->ar_from[0]))
  1253. {
  1254. if ($this->db_debug)
  1255. {
  1256. return $this->display_error('db_must_set_table');
  1257. }
  1258. return FALSE;
  1259. }
  1260. $table = $this->ar_from[0];
  1261. }
  1262. else
  1263. {
  1264. $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
  1265. }
  1266. $sql = $this->_delete($table);
  1267. $this->_reset_write();
  1268. return $this->query($sql);
  1269. }
  1270. // --------------------------------------------------------------------
  1271. /**
  1272. * Truncate
  1273. *
  1274. * Compiles a truncate string and runs the query
  1275. * If the database does not support the truncate() command
  1276. * This function maps to "DELETE FROM table"
  1277. *
  1278. * @access public
  1279. * @param string the table to truncate
  1280. * @return object
  1281. */
  1282. function truncate($table = '')
  1283. {
  1284. if ($table == '')
  1285. {
  1286. if ( ! isset($this->ar_from[0]))
  1287. {
  1288. if ($this->db_debug)
  1289. {
  1290. return $this->display_error('db_must_set_table');
  1291. }
  1292. return FALSE;
  1293. }
  1294. $table = $this->ar_from[0];
  1295. }
  1296. else
  1297. {
  1298. $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
  1299. }
  1300. $sql = $this->_truncate($table);
  1301. $this->_reset_write();
  1302. return $this->query($sql);
  1303. }
  1304. // --------------------------------------------------------------------
  1305. /**
  1306. * Delete
  1307. *
  1308. * Compiles a delete string and runs the query
  1309. *
  1310. * @access public
  1311. * @param mixed the table(s) to delete from. String or array
  1312. * @param mixed the where clause
  1313. * @param mixed the limit clause
  1314. * @param boolean
  1315. * @return object
  1316. */
  1317. function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
  1318. {
  1319. // Combine any cached components with the current statements
  1320. $this->_merge_cache();
  1321. if ($table == '')
  1322. {
  1323. if ( ! isset($this->ar_from[0]))
  1324. {
  1325. if ($this->db_debug)
  1326. {
  1327. return $this->display_error('db_must_set_table');
  1328. }
  1329. return FALSE;
  1330. }
  1331. $table = $this->ar_from[0];
  1332. }
  1333. elseif (is_array($table))
  1334. {
  1335. foreach($table as $single_table)
  1336. {
  1337. $this->delete($single_table, $where, $limit, FALSE);
  1338. }
  1339. $this->_reset_write();
  1340. return;
  1341. }
  1342. else
  1343. {
  1344. $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
  1345. }
  1346. if ($where != '')
  1347. {
  1348. $this->where($where);
  1349. }
  1350. if ($limit != NULL)
  1351. {
  1352. $this->limit($limit);
  1353. }
  1354. if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0)
  1355. {
  1356. if ($this->db_debug)
  1357. {
  1358. return $this->display_error('db_del_must_use_where');
  1359. }
  1360. return FALSE;
  1361. }
  1362. $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
  1363. if ($reset_data)
  1364. {
  1365. $this->_reset_write();
  1366. }
  1367. return $this->query($sql);
  1368. }
  1369. // --------------------------------------------------------------------
  1370. /**
  1371. * DB Prefix
  1372. *
  1373. * Prepends a database prefix if one exists in configuration
  1374. *
  1375. * @access public
  1376. * @param string the table
  1377. * @return string
  1378. */
  1379. function dbprefix($table = '')
  1380. {
  1381. if ($table == '')
  1382. {
  1383. $this->display_error('db_table_name_required');
  1384. }
  1385. return $this->dbprefix.$table;
  1386. }
  1387. // --------------------------------------------------------------------
  1388. /**
  1389. * Track Aliases
  1390. *
  1391. * Used to track SQL statements written with aliased tables.
  1392. *
  1393. * @access private
  1394. * @param string The table to inspect
  1395. * @return string
  1396. */
  1397. function _track_aliases($table)
  1398. {
  1399. if (is_array($table))
  1400. {
  1401. foreach ($table as $t)
  1402. {
  1403. $this->_track_aliases($t);
  1404. }
  1405. return;
  1406. }
  1407. // Does the string contain a comma? If so, we need to separate
  1408. // the string into discreet statements
  1409. if (strpos($table, ',') !== FALSE)
  1410. {
  1411. return $this->_track_aliases(explode(',', $table));
  1412. }
  1413. // if a table alias is used we can recognize it by a space
  1414. if (strpos($table, " ") !== FALSE)
  1415. {
  1416. // if the alias is written with the AS keyword, remove it
  1417. $table = preg_replace('/ AS /i', ' ', $table);
  1418. // Grab the alias
  1419. $table = trim(strrchr($table, " "));
  1420. // Store the alias, if it doesn't already exist
  1421. if ( ! in_array($table, $this->ar_aliased_tables))
  1422. {
  1423. $this->ar_aliased_tables[] = $table;
  1424. }
  1425. }
  1426. }
  1427. // --------------------------------------------------------------------
  1428. /**
  1429. * Compile the SELECT statement
  1430. *
  1431. * Generates a query string based on which functions were used.
  1432. * Should not be called directly. The get() function calls it.
  1433. *
  1434. * @access private
  1435. * @return string
  1436. */
  1437. function _compile_select($select_override = FALSE)
  1438. {
  1439. // Combine any cached components with the current statements
  1440. $this->_merge_cache();
  1441. // ----------------------------------------------------------------
  1442. // Write the "select" portion of the query
  1443. if ($select_override !== FALSE)
  1444. {
  1445. $sql = $select_override;
  1446. }
  1447. else
  1448. {
  1449. $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
  1450. if (count($this->ar_select) == 0)
  1451. {
  1452. $sql .= '*';
  1453. }
  1454. else
  1455. {
  1456. // Cycle through the "select" portion of the query and prep each column name.
  1457. // The reason we protect identifiers here rather then in the select() function
  1458. // is because until the user calls the from() function we don't know if there are aliases
  1459. foreach ($this->ar_select as $key => $val)
  1460. {
  1461. $this->ar_select[$key] = $this->_protect_identifiers($val, FALSE, $this->ar_no_escape[$key]);
  1462. }
  1463. $sql .= implode(', ', $this->ar_select);
  1464. }
  1465. }
  1466. // ----------------------------------------------------------------
  1467. // Write the "FROM" portion of the query
  1468. if (count($this->ar_from) > 0)
  1469. {
  1470. $sql .= "\nFROM ";
  1471. $sql .= $this->_from_tables($this->ar_from);
  1472. }
  1473. // ----------------------------------------------------------------
  1474. // Write the "JOIN" portion of the query
  1475. if (count($this->ar_join) > 0)
  1476. {
  1477. $sql .= "\n";
  1478. $sql .= implode("\n", $this->ar_join);
  1479. }
  1480. // ----------------------------------------------------------------
  1481. // Write the "WHERE" portion of the query
  1482. if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
  1483. {
  1484. $sql .= "\nWHERE ";
  1485. }
  1486. $sql .= implode("\n", $this->ar_where);
  1487. // ----------------------------------------------------------------
  1488. // Write the "LIKE" portion of the query
  1489. if (count($this->ar_like) > 0)
  1490. {
  1491. if (count($this->ar_where) > 0)
  1492. {
  1493. $sql .= "\nAND ";
  1494. }
  1495. $sql .= implode("\n", $this->ar_like);
  1496. }
  1497. // ----------------------------------------------------------------
  1498. // Write the "GROUP BY" portion of the query
  1499. if (count($this->ar_groupby) > 0)
  1500. {
  1501. $sql .= "\nGROUP BY ";
  1502. $sql .= implode(', ', $this->ar_groupby);
  1503. }
  1504. // ----------------------------------------------------------------
  1505. // Write the "HAVING" portion of the query
  1506. if (count($this->ar_having) > 0)
  1507. {
  1508. $sql .= "\nHAVING ";
  1509. $sql .= implode("\n", $this->ar_having);
  1510. }
  1511. // ----------------------------------------------------------------
  1512. // Write the "ORDER BY" portion of the query
  1513. if (count($this->ar_orderby) > 0)
  1514. {
  1515. $sql .= "\nORDER BY ";
  1516. $sql .= implode(', ', $this->ar_orderby);
  1517. if ($this->ar_order !== FALSE)
  1518. {
  1519. $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
  1520. }
  1521. }
  1522. // ----------------------------------------------------------------
  1523. // Write the "LIMIT" portion of the query
  1524. if (is_numeric($this->ar_limit))
  1525. {
  1526. $sql .= "\n";
  1527. $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
  1528. }
  1529. return $sql;
  1530. }
  1531. // --------------------------------------------------------------------
  1532. /**
  1533. * Object to Array
  1534. *
  1535. * Takes an object as input and converts the class variables to array key/vals
  1536. *
  1537. * @access public
  1538. * @param object
  1539. * @return array
  1540. */
  1541. function _object_to_array($object)
  1542. {
  1543. if ( ! is_object($object))
  1544. {
  1545. return $object;
  1546. }
  1547. $array = array();
  1548. foreach (get_object_vars($object) as $key => $val)
  1549. {
  1550. // There are some built in keys we need to ignore for this conversion
  1551. if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
  1552. {
  1553. $array[$key] = $val;
  1554. }
  1555. }
  1556. return $array;
  1557. }
  1558. // --------------------------------------------------------------------
  1559. /**
  1560. * Object to Array
  1561. *
  1562. * Takes an object as input and converts the class variables to array key/vals
  1563. *
  1564. * @access public
  1565. * @param object
  1566. * @return array
  1567. */
  1568. function _object_to_array_batch($object)
  1569. {
  1570. if ( ! is_object($object))
  1571. {
  1572. return $object;
  1573. }
  1574. $array = array();
  1575. $out = get_object_vars($object);
  1576. $fields = array_keys($out);
  1577. foreach ($fields as $val)
  1578. {
  1579. // There are some built in keys we need to ignore for this conversion
  1580. if ($val != '_parent_name')
  1581. {
  1582. $i = 0;
  1583. foreach ($out[$val] as $data)
  1584. {
  1585. $array[$i][$val] = $data;
  1586. $i++;
  1587. }
  1588. }
  1589. }
  1590. return $array;
  1591. }
  1592. // --------------------------------------------------------------------
  1593. /**
  1594. * Start Cache
  1595. *
  1596. * Starts AR caching
  1597. *
  1598. * @access public
  1599. * @return void
  1600. */
  1601. function start_cache()
  1602. {
  1603. $this->ar_caching = TRUE;
  1604. }
  1605. // --------------------------------------------------------------------
  1606. /**
  1607. * Stop Cache
  1608. *
  1609. * Stops AR caching
  1610. *
  1611. * @access public
  1612. * @return void
  1613. */
  1614. function stop_cache()
  1615. {
  1616. $this->ar_caching = FALSE;
  1617. }
  1618. // --------------------------------------------------------------------
  1619. /**
  1620. * Flush Cache
  1621. *
  1622. * Empties the AR cache
  1623. *
  1624. * @access public
  1625. * @return void
  1626. */
  1627. function flush_cache()
  1628. {
  1629. $this->_reset_run(
  1630. array(
  1631. 'ar_cache_select' => array(),
  1632. 'ar_cache_from' => array(),
  1633. 'ar_cache_join' => array(),
  1634. 'ar_cache_where' => array(),
  1635. 'ar_cache_like' => array(),
  1636. 'ar_cache_groupby' => array(),
  1637. 'ar_cache_having' => array(),
  1638. 'ar_cache_orderby' => array(),
  1639. 'ar_cache_set' => array(),
  1640. 'ar_cache_exists' => array(),
  1641. 'ar_cache_no_escape' => array(),
  1642. )
  1643. );
  1644. }
  1645. // --------------------------------------------------------------------
  1646. /**
  1647. * Merge Cache
  1648. *
  1649. * When called, this function merges any cached AR arrays with
  1650. * locally called ones.
  1651. *
  1652. * @access private
  1653. * @return void
  1654. */
  1655. function _merge_cache()
  1656. {
  1657. if (count($this->ar_cache_exists) == 0)
  1658. {
  1659. return;
  1660. }
  1661. foreach ($this->ar_cache_exists as $val)
  1662. {
  1663. $ar_variable = 'ar_'.$val;
  1664. $ar_cache_var = 'ar_cache_'.$val;
  1665. if (count($this->$ar_cache_var) == 0)
  1666. {
  1667. continue;
  1668. }
  1669. $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
  1670. }
  1671. // If we are "protecting identifiers" we need to examine the "from"
  1672. // portion of the query to determine if there are any aliases
  1673. if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
  1674. {
  1675. $this->_track_aliases($this->ar_from);
  1676. }
  1677. $this->ar_no_escape = $this->ar_cache_no_escape;
  1678. }
  1679. // --------------------------------------------------------------------
  1680. /**
  1681. * Resets the active record values. Called by the get() function
  1682. *
  1683. * @access private
  1684. * @param array An array of fields to reset
  1685. * @return void
  1686. */
  1687. function _reset_run($ar_reset_items)
  1688. {
  1689. foreach ($ar_reset_items as $item => $default_value)
  1690. {
  1691. if ( ! in_array($item, $this->ar_store_array))
  1692. {
  1693. $this->$item = $default_value;
  1694. }
  1695. }
  1696. }
  1697. // --------------------------------------------------------------------
  1698. /**
  1699. * Resets the active record values. Called by the get() function
  1700. *
  1701. * @access private
  1702. * @return void
  1703. */
  1704. function _reset_select()
  1705. {
  1706. $ar_reset_items = array(
  1707. 'ar_select' => array(),
  1708. 'ar_from' => array(),
  1709. 'ar_join' => array(),
  1710. 'ar_where' => array(),
  1711. 'ar_like' => array(),
  1712. 'ar_groupby' => array(),
  1713. 'ar_having' => array(),
  1714. 'ar_orderby' => array(),
  1715. 'ar_wherein' => array(),
  1716. 'ar_aliased_tables' => array(),
  1717. 'ar_no_escape' => array(),
  1718. 'ar_distinct' => FALSE,
  1719. 'ar_limit' => FALSE,
  1720. 'ar_offset' => FALSE,
  1721. 'ar_order' => FALSE,
  1722. );
  1723. $this->_reset_run($ar_reset_items);
  1724. }
  1725. // --------------------------------------------------------------------
  1726. /**
  1727. * Resets the active record "write" values.
  1728. *
  1729. * Called by the insert() update() insert_batch() update_batch() and delete() functions
  1730. *
  1731. * @access private
  1732. * @return void
  1733. */
  1734. function _reset_write()
  1735. {
  1736. $ar_reset_items = array(
  1737. 'ar_set' => array(),
  1738. 'ar_from' => array(),
  1739. 'ar_where' => array(),
  1740. 'ar_like' => array(),
  1741. 'ar_orderby' => array(),
  1742. 'ar_keys' => array(),
  1743. 'ar_limit' => FALSE,
  1744. 'ar_order' => FALSE
  1745. );
  1746. $this->_reset_run($ar_reset_items);
  1747. }
  1748. }
  1749. /* End of file DB_active_rec.php */
  1750. /* Location: ./system/database/DB_active_rec.php */