PageRenderTime 61ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/system/database/DB_active_rec.php

https://bitbucket.org/naando_araujo/pagseguro
PHP | 2073 lines | 1076 code | 328 blank | 669 comment | 193 complexity | 41300a5c5818a634902710e165ed278c 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.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, 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 ExpressionEngine 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. // --------------------------------------------------------------------
  58. /**
  59. * Select
  60. *
  61. * Generates the SELECT portion of the query
  62. *
  63. * @access public
  64. * @param string
  65. * @return object
  66. */
  67. function select($select = '*', $escape = NULL)
  68. {
  69. // Set the global value if this was sepecified
  70. if (is_bool($escape))
  71. {
  72. $this->_protect_identifiers = $escape;
  73. }
  74. if (is_string($select))
  75. {
  76. $select = explode(',', $select);
  77. }
  78. foreach ($select as $val)
  79. {
  80. $val = trim($val);
  81. if ($val != '')
  82. {
  83. $this->ar_select[] = $val;
  84. if ($this->ar_caching === TRUE)
  85. {
  86. $this->ar_cache_select[] = $val;
  87. $this->ar_cache_exists[] = 'select';
  88. }
  89. }
  90. }
  91. return $this;
  92. }
  93. // --------------------------------------------------------------------
  94. /**
  95. * Select Max
  96. *
  97. * Generates a SELECT MAX(field) portion of a query
  98. *
  99. * @access public
  100. * @param string the field
  101. * @param string an alias
  102. * @return object
  103. */
  104. function select_max($select = '', $alias = '')
  105. {
  106. return $this->_max_min_avg_sum($select, $alias, 'MAX');
  107. }
  108. // --------------------------------------------------------------------
  109. /**
  110. * Select Min
  111. *
  112. * Generates a SELECT MIN(field) portion of a query
  113. *
  114. * @access public
  115. * @param string the field
  116. * @param string an alias
  117. * @return object
  118. */
  119. function select_min($select = '', $alias = '')
  120. {
  121. return $this->_max_min_avg_sum($select, $alias, 'MIN');
  122. }
  123. // --------------------------------------------------------------------
  124. /**
  125. * Select Average
  126. *
  127. * Generates a SELECT AVG(field) portion of a query
  128. *
  129. * @access public
  130. * @param string the field
  131. * @param string an alias
  132. * @return object
  133. */
  134. function select_avg($select = '', $alias = '')
  135. {
  136. return $this->_max_min_avg_sum($select, $alias, 'AVG');
  137. }
  138. // --------------------------------------------------------------------
  139. /**
  140. * Select Sum
  141. *
  142. * Generates a SELECT SUM(field) portion of a query
  143. *
  144. * @access public
  145. * @param string the field
  146. * @param string an alias
  147. * @return object
  148. */
  149. function select_sum($select = '', $alias = '')
  150. {
  151. return $this->_max_min_avg_sum($select, $alias, 'SUM');
  152. }
  153. // --------------------------------------------------------------------
  154. /**
  155. * Processing Function for the four functions above:
  156. *
  157. * select_max()
  158. * select_min()
  159. * select_avg()
  160. * select_sum()
  161. *
  162. * @access public
  163. * @param string the field
  164. * @param string an alias
  165. * @return object
  166. */
  167. function _max_min_avg_sum($select = '', $alias = '', $type = 'MAX')
  168. {
  169. if ( ! is_string($select) OR $select == '')
  170. {
  171. $this->display_error('db_invalid_query');
  172. }
  173. $type = strtoupper($type);
  174. if ( ! in_array($type, array('MAX', 'MIN', 'AVG', 'SUM')))
  175. {
  176. show_error('Invalid function type: '.$type);
  177. }
  178. if ($alias == '')
  179. {
  180. $alias = $this->_create_alias_from_table(trim($select));
  181. }
  182. $sql = $type.'('.$this->_protect_identifiers(trim($select)).') AS '.$alias;
  183. $this->ar_select[] = $sql;
  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. * @return object
  725. */
  726. function order_by($orderby, $direction = '')
  727. {
  728. if (strtolower($direction) == 'random')
  729. {
  730. $orderby = ''; // Random results want or don't need a field name
  731. $direction = $this->_random_keyword;
  732. }
  733. elseif (trim($direction) != '')
  734. {
  735. $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
  736. }
  737. if (strpos($orderby, ',') !== FALSE)
  738. {
  739. $temp = array();
  740. foreach (explode(',', $orderby) as $part)
  741. {
  742. $part = trim($part);
  743. if ( ! in_array($part, $this->ar_aliased_tables))
  744. {
  745. $part = $this->_protect_identifiers(trim($part));
  746. }
  747. $temp[] = $part;
  748. }
  749. $orderby = implode(', ', $temp);
  750. }
  751. else if ($direction != $this->_random_keyword)
  752. {
  753. $orderby = $this->_protect_identifiers($orderby);
  754. }
  755. $orderby_statement = $orderby.$direction;
  756. $this->ar_orderby[] = $orderby_statement;
  757. if ($this->ar_caching === TRUE)
  758. {
  759. $this->ar_cache_orderby[] = $orderby_statement;
  760. $this->ar_cache_exists[] = 'orderby';
  761. }
  762. return $this;
  763. }
  764. // --------------------------------------------------------------------
  765. /**
  766. * Sets the LIMIT value
  767. *
  768. * @access public
  769. * @param integer the limit value
  770. * @param integer the offset value
  771. * @return object
  772. */
  773. function limit($value, $offset = '')
  774. {
  775. $this->ar_limit = $value;
  776. if ($offset != '')
  777. {
  778. $this->ar_offset = $offset;
  779. }
  780. return $this;
  781. }
  782. // --------------------------------------------------------------------
  783. /**
  784. * Sets the OFFSET value
  785. *
  786. * @access public
  787. * @param integer the offset value
  788. * @return object
  789. */
  790. function offset($offset)
  791. {
  792. $this->ar_offset = $offset;
  793. return $this;
  794. }
  795. // --------------------------------------------------------------------
  796. /**
  797. * The "set" function. Allows key/value pairs to be set for inserting or updating
  798. *
  799. * @access public
  800. * @param mixed
  801. * @param string
  802. * @param boolean
  803. * @return object
  804. */
  805. function set($key, $value = '', $escape = TRUE)
  806. {
  807. $key = $this->_object_to_array($key);
  808. if ( ! is_array($key))
  809. {
  810. $key = array($key => $value);
  811. }
  812. foreach ($key as $k => $v)
  813. {
  814. if ($escape === FALSE)
  815. {
  816. $this->ar_set[$this->_protect_identifiers($k)] = $v;
  817. }
  818. else
  819. {
  820. $this->ar_set[$this->_protect_identifiers($k, FALSE, TRUE)] = $this->escape($v);
  821. }
  822. }
  823. return $this;
  824. }
  825. // --------------------------------------------------------------------
  826. /**
  827. * Get
  828. *
  829. * Compiles the select statement based on the other functions called
  830. * and runs the query
  831. *
  832. * @access public
  833. * @param string the table
  834. * @param string the limit clause
  835. * @param string the offset clause
  836. * @return object
  837. */
  838. function get($table = '', $limit = null, $offset = null)
  839. {
  840. if ($table != '')
  841. {
  842. $this->_track_aliases($table);
  843. $this->from($table);
  844. }
  845. if ( ! is_null($limit))
  846. {
  847. $this->limit($limit, $offset);
  848. }
  849. $sql = $this->_compile_select();
  850. $result = $this->query($sql);
  851. $this->_reset_select();
  852. return $result;
  853. }
  854. /**
  855. * "Count All Results" query
  856. *
  857. * Generates a platform-specific query string that counts all records
  858. * returned by an Active Record query.
  859. *
  860. * @access public
  861. * @param string
  862. * @return string
  863. */
  864. function count_all_results($table = '')
  865. {
  866. if ($table != '')
  867. {
  868. $this->_track_aliases($table);
  869. $this->from($table);
  870. }
  871. $sql = $this->_compile_select($this->_count_string . $this->_protect_identifiers('numrows'));
  872. $query = $this->query($sql);
  873. $this->_reset_select();
  874. if ($query->num_rows() == 0)
  875. {
  876. return 0;
  877. }
  878. $row = $query->row();
  879. return (int) $row->numrows;
  880. }
  881. // --------------------------------------------------------------------
  882. /**
  883. * Get_Where
  884. *
  885. * Allows the where clause, limit and offset to be added directly
  886. *
  887. * @access public
  888. * @param string the where clause
  889. * @param string the limit clause
  890. * @param string the offset clause
  891. * @return object
  892. */
  893. function get_where($table = '', $where = null, $limit = null, $offset = null)
  894. {
  895. if ($table != '')
  896. {
  897. $this->from($table);
  898. }
  899. if ( ! is_null($where))
  900. {
  901. $this->where($where);
  902. }
  903. if ( ! is_null($limit))
  904. {
  905. $this->limit($limit, $offset);
  906. }
  907. $sql = $this->_compile_select();
  908. $result = $this->query($sql);
  909. $this->_reset_select();
  910. return $result;
  911. }
  912. // --------------------------------------------------------------------
  913. /**
  914. * Insert_Batch
  915. *
  916. * Compiles batch insert strings and runs the queries
  917. *
  918. * @access public
  919. * @param string the table to retrieve the results from
  920. * @param array an associative array of insert values
  921. * @return object
  922. */
  923. function insert_batch($table = '', $set = NULL)
  924. {
  925. if ( ! is_null($set))
  926. {
  927. $this->set_insert_batch($set);
  928. }
  929. if (count($this->ar_set) == 0)
  930. {
  931. if ($this->db_debug)
  932. {
  933. //No valid data array. Folds in cases where keys and values did not match up
  934. return $this->display_error('db_must_use_set');
  935. }
  936. return FALSE;
  937. }
  938. if ($table == '')
  939. {
  940. if ( ! isset($this->ar_from[0]))
  941. {
  942. if ($this->db_debug)
  943. {
  944. return $this->display_error('db_must_set_table');
  945. }
  946. return FALSE;
  947. }
  948. $table = $this->ar_from[0];
  949. }
  950. // Batch this baby
  951. for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
  952. {
  953. $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
  954. //echo $sql;
  955. $this->query($sql);
  956. }
  957. $this->_reset_write();
  958. return TRUE;
  959. }
  960. // --------------------------------------------------------------------
  961. /**
  962. * The "set_insert_batch" function. Allows key/value pairs to be set for batch inserts
  963. *
  964. * @access public
  965. * @param mixed
  966. * @param string
  967. * @param boolean
  968. * @return object
  969. */
  970. function set_insert_batch($key, $value = '', $escape = TRUE)
  971. {
  972. $key = $this->_object_to_array_batch($key);
  973. if ( ! is_array($key))
  974. {
  975. $key = array($key => $value);
  976. }
  977. $keys = array_keys(current($key));
  978. sort($keys);
  979. foreach ($key as $row)
  980. {
  981. if (count(array_diff($keys, array_keys($row))) > 0 OR count(array_diff(array_keys($row), $keys)) > 0)
  982. {
  983. // batch function above returns an error on an empty array
  984. $this->ar_set[] = array();
  985. return;
  986. }
  987. ksort($row); // puts $row in the same order as our keys
  988. if ($escape === FALSE)
  989. {
  990. $this->ar_set[] = '('.implode(',', $row).')';
  991. }
  992. else
  993. {
  994. $clean = array();
  995. foreach ($row as $value)
  996. {
  997. $clean[] = $this->escape($value);
  998. }
  999. $this->ar_set[] = '('.implode(',', $clean).')';
  1000. }
  1001. }
  1002. foreach ($keys as $k)
  1003. {
  1004. $this->ar_keys[] = $this->_protect_identifiers($k);
  1005. }
  1006. return $this;
  1007. }
  1008. // --------------------------------------------------------------------
  1009. /**
  1010. * Insert
  1011. *
  1012. * Compiles an insert string and runs the query
  1013. *
  1014. * @access public
  1015. * @param string the table to retrieve the results from
  1016. * @param array an associative array of insert values
  1017. * @return object
  1018. */
  1019. function insert($table = '', $set = NULL)
  1020. {
  1021. if ( ! is_null($set))
  1022. {
  1023. $this->set($set);
  1024. }
  1025. if (count($this->ar_set) == 0)
  1026. {
  1027. if ($this->db_debug)
  1028. {
  1029. return $this->display_error('db_must_use_set');
  1030. }
  1031. return FALSE;
  1032. }
  1033. if ($table == '')
  1034. {
  1035. if ( ! isset($this->ar_from[0]))
  1036. {
  1037. if ($this->db_debug)
  1038. {
  1039. return $this->display_error('db_must_set_table');
  1040. }
  1041. return FALSE;
  1042. }
  1043. $table = $this->ar_from[0];
  1044. }
  1045. $sql = $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
  1046. $this->_reset_write();
  1047. return $this->query($sql);
  1048. }
  1049. function replace($table = '', $set = NULL)
  1050. {
  1051. if ( ! is_null($set))
  1052. {
  1053. $this->set($set);
  1054. }
  1055. if (count($this->ar_set) == 0)
  1056. {
  1057. if ($this->db_debug)
  1058. {
  1059. return $this->display_error('db_must_use_set');
  1060. }
  1061. return FALSE;
  1062. }
  1063. if ($table == '')
  1064. {
  1065. if ( ! isset($this->ar_from[0]))
  1066. {
  1067. if ($this->db_debug)
  1068. {
  1069. return $this->display_error('db_must_set_table');
  1070. }
  1071. return FALSE;
  1072. }
  1073. $table = $this->ar_from[0];
  1074. }
  1075. $sql = $this->_replace($this->_protect_identifiers($table, TRUE, NULL, FALSE), array_keys($this->ar_set), array_values($this->ar_set));
  1076. $this->_reset_write();
  1077. return $this->query($sql);
  1078. }
  1079. // --------------------------------------------------------------------
  1080. /**
  1081. * Update
  1082. *
  1083. * Compiles an update string and runs the query
  1084. *
  1085. * @access public
  1086. * @param string the table to retrieve the results from
  1087. * @param array an associative array of update values
  1088. * @param mixed the where clause
  1089. * @return object
  1090. */
  1091. function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
  1092. {
  1093. // Combine any cached components with the current statements
  1094. $this->_merge_cache();
  1095. if ( ! is_null($set))
  1096. {
  1097. $this->set($set);
  1098. }
  1099. if (count($this->ar_set) == 0)
  1100. {
  1101. if ($this->db_debug)
  1102. {
  1103. return $this->display_error('db_must_use_set');
  1104. }
  1105. return FALSE;
  1106. }
  1107. if ($table == '')
  1108. {
  1109. if ( ! isset($this->ar_from[0]))
  1110. {
  1111. if ($this->db_debug)
  1112. {
  1113. return $this->display_error('db_must_set_table');
  1114. }
  1115. return FALSE;
  1116. }
  1117. $table = $this->ar_from[0];
  1118. }
  1119. if ($where != NULL)
  1120. {
  1121. $this->where($where);
  1122. }
  1123. if ($limit != NULL)
  1124. {
  1125. $this->limit($limit);
  1126. }
  1127. $sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
  1128. $this->_reset_write();
  1129. return $this->query($sql);
  1130. }
  1131. // --------------------------------------------------------------------
  1132. /**
  1133. * Update_Batch
  1134. *
  1135. * Compiles an update string and runs the query
  1136. *
  1137. * @access public
  1138. * @param string the table to retrieve the results from
  1139. * @param array an associative array of update values
  1140. * @param string the where key
  1141. * @return object
  1142. */
  1143. function update_batch($table = '', $set = NULL, $index = NULL)
  1144. {
  1145. // Combine any cached components with the current statements
  1146. $this->_merge_cache();
  1147. if (is_null($index))
  1148. {
  1149. if ($this->db_debug)
  1150. {
  1151. return $this->display_error('db_myst_use_index');
  1152. }
  1153. return FALSE;
  1154. }
  1155. if ( ! is_null($set))
  1156. {
  1157. $this->set_update_batch($set, $index);
  1158. }
  1159. if (count($this->ar_set) == 0)
  1160. {
  1161. if ($this->db_debug)
  1162. {
  1163. return $this->display_error('db_must_use_set');
  1164. }
  1165. return FALSE;
  1166. }
  1167. if ($table == '')
  1168. {
  1169. if ( ! isset($this->ar_from[0]))
  1170. {
  1171. if ($this->db_debug)
  1172. {
  1173. return $this->display_error('db_must_set_table');
  1174. }
  1175. return FALSE;
  1176. }
  1177. $table = $this->ar_from[0];
  1178. }
  1179. // Batch this baby
  1180. for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
  1181. {
  1182. $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);
  1183. $this->query($sql);
  1184. }
  1185. $this->_reset_write();
  1186. }
  1187. // --------------------------------------------------------------------
  1188. /**
  1189. * The "set_update_batch" function. Allows key/value pairs to be set for batch updating
  1190. *
  1191. * @access public
  1192. * @param array
  1193. * @param string
  1194. * @param boolean
  1195. * @return object
  1196. */
  1197. function set_update_batch($key, $index = '', $escape = TRUE)
  1198. {
  1199. $key = $this->_object_to_array_batch($key);
  1200. if ( ! is_array($key))
  1201. {
  1202. // @todo error
  1203. }
  1204. foreach ($key as $k => $v)
  1205. {
  1206. $index_set = FALSE;
  1207. $clean = array();
  1208. foreach ($v as $k2 => $v2)
  1209. {
  1210. if ($k2 == $index)
  1211. {
  1212. $index_set = TRUE;
  1213. }
  1214. else
  1215. {
  1216. $not[] = $k.'-'.$v;
  1217. }
  1218. if ($escape === FALSE)
  1219. {
  1220. $clean[$this->_protect_identifiers($k2)] = $v2;
  1221. }
  1222. else
  1223. {
  1224. $clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
  1225. }
  1226. }
  1227. if ($index_set == FALSE)
  1228. {
  1229. return $this->display_error('db_batch_missing_index');
  1230. }
  1231. $this->ar_set[] = $clean;
  1232. }
  1233. return $this;
  1234. }
  1235. // --------------------------------------------------------------------
  1236. /**
  1237. * Empty Table
  1238. *
  1239. * Compiles a delete string and runs "DELETE FROM table"
  1240. *
  1241. * @access public
  1242. * @param string the table to empty
  1243. * @return object
  1244. */
  1245. function empty_table($table = '')
  1246. {
  1247. if ($table == '')
  1248. {
  1249. if ( ! isset($this->ar_from[0]))
  1250. {
  1251. if ($this->db_debug)
  1252. {
  1253. return $this->display_error('db_must_set_table');
  1254. }
  1255. return FALSE;
  1256. }
  1257. $table = $this->ar_from[0];
  1258. }
  1259. else
  1260. {
  1261. $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
  1262. }
  1263. $sql = $this->_delete($table);
  1264. $this->_reset_write();
  1265. return $this->query($sql);
  1266. }
  1267. // --------------------------------------------------------------------
  1268. /**
  1269. * Truncate
  1270. *
  1271. * Compiles a truncate string and runs the query
  1272. * If the database does not support the truncate() command
  1273. * This function maps to "DELETE FROM table"
  1274. *
  1275. * @access public
  1276. * @param string the table to truncate
  1277. * @return object
  1278. */
  1279. function truncate($table = '')
  1280. {
  1281. if ($table == '')
  1282. {
  1283. if ( ! isset($this->ar_from[0]))
  1284. {
  1285. if ($this->db_debug)
  1286. {
  1287. return $this->display_error('db_must_set_table');
  1288. }
  1289. return FALSE;
  1290. }
  1291. $table = $this->ar_from[0];
  1292. }
  1293. else
  1294. {
  1295. $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
  1296. }
  1297. $sql = $this->_truncate($table);
  1298. $this->_reset_write();
  1299. return $this->query($sql);
  1300. }
  1301. // --------------------------------------------------------------------
  1302. /**
  1303. * Delete
  1304. *
  1305. * Compiles a delete string and runs the query
  1306. *
  1307. * @access public
  1308. * @param mixed the table(s) to delete from. String or array
  1309. * @param mixed the where clause
  1310. * @param mixed the limit clause
  1311. * @param boolean
  1312. * @return object
  1313. */
  1314. function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
  1315. {
  1316. // Combine any cached components with the current statements
  1317. $this->_merge_cache();
  1318. if ($table == '')
  1319. {
  1320. if ( ! isset($this->ar_from[0]))
  1321. {
  1322. if ($this->db_debug)
  1323. {
  1324. return $this->display_error('db_must_set_table');
  1325. }
  1326. return FALSE;
  1327. }
  1328. $table = $this->ar_from[0];
  1329. }
  1330. elseif (is_array($table))
  1331. {
  1332. foreach ($table as $single_table)
  1333. {
  1334. $this->delete($single_table, $where, $limit, FALSE);
  1335. }
  1336. $this->_reset_write();
  1337. return;
  1338. }
  1339. else
  1340. {
  1341. $table = $this->_protect_identifiers($table, TRUE, NULL, FALSE);
  1342. }
  1343. if ($where != '')
  1344. {
  1345. $this->where($where);
  1346. }
  1347. if ($limit != NULL)
  1348. {
  1349. $this->limit($limit);
  1350. }
  1351. if (count($this->ar_where) == 0 && count($this->ar_wherein) == 0 && count($this->ar_like) == 0)
  1352. {
  1353. if ($this->db_debug)
  1354. {
  1355. return $this->display_error('db_del_must_use_where');
  1356. }
  1357. return FALSE;
  1358. }
  1359. $sql = $this->_delete($table, $this->ar_where, $this->ar_like, $this->ar_limit);
  1360. if ($reset_data)
  1361. {
  1362. $this->_reset_write();
  1363. }
  1364. return $this->query($sql);
  1365. }
  1366. // --------------------------------------------------------------------
  1367. /**
  1368. * DB Prefix
  1369. *
  1370. * Prepends a database prefix if one exists in configuration
  1371. *
  1372. * @access public
  1373. * @param string the table
  1374. * @return string
  1375. */
  1376. function dbprefix($table = '')
  1377. {
  1378. if ($table == '')
  1379. {
  1380. $this->display_error('db_table_name_required');
  1381. }
  1382. return $this->dbprefix.$table;
  1383. }
  1384. // --------------------------------------------------------------------
  1385. /**
  1386. * Track Aliases
  1387. *
  1388. * Used to track SQL statements written with aliased tables.
  1389. *
  1390. * @access private
  1391. * @param string The table to inspect
  1392. * @return string
  1393. */
  1394. function _track_aliases($table)
  1395. {
  1396. if (is_array($table))
  1397. {
  1398. foreach ($table as $t)
  1399. {
  1400. $this->_track_aliases($t);
  1401. }
  1402. return;
  1403. }
  1404. // Does the string contain a comma? If so, we need to separate
  1405. // the string into discreet statements
  1406. if (strpos($table, ',') !== FALSE)
  1407. {
  1408. return $this->_track_aliases(explode(',', $table));
  1409. }
  1410. // if a table alias is used we can recognize it by a space
  1411. if (strpos($table, " ") !== FALSE)
  1412. {
  1413. // if the alias is written with the AS keyword, remove it
  1414. $table = preg_replace('/ AS /i', ' ', $table);
  1415. // Grab the alias
  1416. $table = trim(strrchr($table, " "));
  1417. // Store the alias, if it doesn't already exist
  1418. if ( ! in_array($table, $this->ar_aliased_tables))
  1419. {
  1420. $this->ar_aliased_tables[] = $table;
  1421. }
  1422. }
  1423. }
  1424. // --------------------------------------------------------------------
  1425. /**
  1426. * Compile the SELECT statement
  1427. *
  1428. * Generates a query string based on which functions were used.
  1429. * Should not be called directly. The get() function calls it.
  1430. *
  1431. * @access private
  1432. * @return string
  1433. */
  1434. function _compile_select($select_override = FALSE)
  1435. {
  1436. // Combine any cached components with the current statements
  1437. $this->_merge_cache();
  1438. // ----------------------------------------------------------------
  1439. // Write the "select" portion of the query
  1440. if ($select_override !== FALSE)
  1441. {
  1442. $sql = $select_override;
  1443. }
  1444. else
  1445. {
  1446. $sql = ( ! $this->ar_distinct) ? 'SELECT ' : 'SELECT DISTINCT ';
  1447. if (count($this->ar_select) == 0)
  1448. {
  1449. $sql .= '*';
  1450. }
  1451. else
  1452. {
  1453. // Cycle through the "select" portion of the query and prep each column name.
  1454. // The reason we protect identifiers here rather then in the select() function
  1455. // is because until the user calls the from() function we don't know if there are aliases
  1456. foreach ($this->ar_select as $key => $val)
  1457. {
  1458. $this->ar_select[$key] = $this->_protect_identifiers($val);
  1459. }
  1460. $sql .= implode(', ', $this->ar_select);
  1461. }
  1462. }
  1463. // ----------------------------------------------------------------
  1464. // Write the "FROM" portion of the query
  1465. if (count($this->ar_from) > 0)
  1466. {
  1467. $sql .= "\nFROM ";
  1468. $sql .= $this->_from_tables($this->ar_from);
  1469. }
  1470. // ----------------------------------------------------------------
  1471. // Write the "JOIN" portion of the query
  1472. if (count($this->ar_join) > 0)
  1473. {
  1474. $sql .= "\n";
  1475. $sql .= implode("\n", $this->ar_join);
  1476. }
  1477. // ----------------------------------------------------------------
  1478. // Write the "WHERE" portion of the query
  1479. if (count($this->ar_where) > 0 OR count($this->ar_like) > 0)
  1480. {
  1481. $sql .= "\n";
  1482. $sql .= "WHERE ";
  1483. }
  1484. $sql .= implode("\n", $this->ar_where);
  1485. // ----------------------------------------------------------------
  1486. // Write the "LIKE" portion of the query
  1487. if (count($this->ar_like) > 0)
  1488. {
  1489. if (count($this->ar_where) > 0)
  1490. {
  1491. $sql .= "\nAND ";
  1492. }
  1493. $sql .= implode("\n", $this->ar_like);
  1494. }
  1495. // ----------------------------------------------------------------
  1496. // Write the "GROUP BY" portion of the query
  1497. if (count($this->ar_groupby) > 0)
  1498. {
  1499. $sql .= "\nGROUP BY ";
  1500. $sql .= implode(', ', $this->ar_groupby);
  1501. }
  1502. // ----------------------------------------------------------------
  1503. // Write the "HAVING" portion of the query
  1504. if (count($this->ar_having) > 0)
  1505. {
  1506. $sql .= "\nHAVING ";
  1507. $sql .= implode("\n", $this->ar_having);
  1508. }
  1509. // ----------------------------------------------------------------
  1510. // Write the "ORDER BY" portion of the query
  1511. if (count($this->ar_orderby) > 0)
  1512. {
  1513. $sql .= "\nORDER BY ";
  1514. $sql .= implode(', ', $this->ar_orderby);
  1515. if ($this->ar_order !== FALSE)
  1516. {
  1517. $sql .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
  1518. }
  1519. }
  1520. // ----------------------------------------------------------------
  1521. // Write the "LIMIT" portion of the query
  1522. if (is_numeric($this->ar_limit))
  1523. {
  1524. $sql .= "\n";
  1525. $sql = $this->_limit($sql, $this->ar_limit, $this->ar_offset);
  1526. }
  1527. return $sql;
  1528. }
  1529. // --------------------------------------------------------------------
  1530. /**
  1531. * Object to Array
  1532. *
  1533. * Takes an object as input and converts the class variables to array key/vals
  1534. *
  1535. * @access public
  1536. * @param object
  1537. * @return array
  1538. */
  1539. function _object_to_array($object)
  1540. {
  1541. if ( ! is_object($object))
  1542. {
  1543. return $object;
  1544. }
  1545. $array = array();
  1546. foreach (get_object_vars($object) as $key => $val)
  1547. {
  1548. // There are some built in keys we need to ignore for this conversion
  1549. if ( ! is_object($val) && ! is_array($val) && $key != '_parent_name')
  1550. {
  1551. $array[$key] = $val;
  1552. }
  1553. }
  1554. return $array;
  1555. }
  1556. // --------------------------------------------------------------------
  1557. /**
  1558. * Object to Array
  1559. *
  1560. * Takes an object as input and converts the class variables to array key/vals
  1561. *
  1562. * @access public
  1563. * @param object
  1564. * @return array
  1565. */
  1566. function _object_to_array_batch($object)
  1567. {
  1568. if ( ! is_object($object))
  1569. {
  1570. return $object;
  1571. }
  1572. $array = array();
  1573. $out = get_object_vars($object);
  1574. $fields = array_keys($out);
  1575. foreach ($fields as $val)
  1576. {
  1577. // There are some built in keys we need to ignore for this conversion
  1578. if ($val != '_parent_name')
  1579. {
  1580. $i = 0;
  1581. foreach ($out[$val] as $data)
  1582. {
  1583. $array[$i][$val] = $data;
  1584. $i++;
  1585. }
  1586. }
  1587. }
  1588. return $array;
  1589. }
  1590. // --------------------------------------------------------------------
  1591. /**
  1592. * Start Cache
  1593. *
  1594. * Starts AR caching
  1595. *
  1596. * @access public
  1597. * @return void
  1598. */
  1599. function start_cache()
  1600. {
  1601. $this->ar_caching = TRUE;
  1602. }
  1603. // --------------------------------------------------------------------
  1604. /**
  1605. * Stop Cache
  1606. *
  1607. * Stops AR caching
  1608. *
  1609. * @access public
  1610. * @return void
  1611. */
  1612. function stop_cache()
  1613. {
  1614. $this->ar_caching = FALSE;
  1615. }
  1616. // --------------------------------------------------------------------
  1617. /**
  1618. * Flush Cache
  1619. *
  1620. * Empties the AR cache
  1621. *
  1622. * @access public
  1623. * @return void
  1624. */
  1625. function flush_cache()
  1626. {
  1627. $this->_reset_run(
  1628. array(
  1629. 'ar_cache_select' => array(),
  1630. 'ar_cache_from' => array(),
  1631. 'ar_cache_join' => array(),
  1632. 'ar_cache_where' => array(),
  1633. 'ar_cache_like' => array(),
  1634. 'ar_cache_groupby' => array(),
  1635. 'ar_cache_having' => array(),
  1636. 'ar_cache_orderby' => array(),
  1637. 'ar_cache_set' => array(),
  1638. 'ar_cache_exists' => array()
  1639. )
  1640. );
  1641. }
  1642. // --------------------------------------------------------------------
  1643. /**
  1644. * Merge Cache
  1645. *
  1646. * When called, this function merges any cached AR arrays with
  1647. * locally called ones.
  1648. *
  1649. * @access private
  1650. * @return void
  1651. */
  1652. function _merge_cache()
  1653. {
  1654. if (count($this->ar_cache_exists) == 0)
  1655. {
  1656. return;
  1657. }
  1658. foreach ($this->ar_cache_exists as $val)
  1659. {
  1660. $ar_variable = 'ar_'.$val;
  1661. $ar_cache_var = 'ar_cache_'.$val;
  1662. if (count($this->$ar_cache_var) == 0)
  1663. {
  1664. continue;
  1665. }
  1666. $this->$ar_variable = array_unique(array_merge($this->$ar_cache_var, $this->$ar_variable));
  1667. }
  1668. // If we are "protecting identifiers" we need to examine the "from"
  1669. // portion of the query to determine if there are any aliases
  1670. if ($this->_protect_identifiers === TRUE AND count($this->ar_cache_from) > 0)
  1671. {
  1672. $this->_track_aliases($this->ar_from);
  1673. }
  1674. }
  1675. // --------------------------------------------------------------------
  1676. /**
  1677. * Resets the active record values. Called by the get() function
  1678. *
  1679. * @access private
  1680. * @param array An array of fields to reset
  1681. * @return void
  1682. */
  1683. function _reset_run($ar_reset_items)
  1684. {
  1685. foreach ($ar_reset_items as $item => $default_value)
  1686. {
  1687. if ( ! in_array($item, $this->ar_store_array))
  1688. {
  1689. $this->$item = $default_value;
  1690. }
  1691. }
  1692. }
  1693. // --------------------------------------------------------------------
  1694. /**
  1695. * Resets the active record values. Called by the get() function
  1696. *
  1697. * @access private
  1698. * @return void
  1699. */
  1700. function _reset_select()
  1701. {
  1702. $ar_reset_items = array(
  1703. 'ar_select' => array(),
  1704. 'ar_from' => array(),
  1705. 'ar_join' => array(),
  1706. 'ar_where' => array(),
  1707. 'ar_like' => array(),
  1708. 'ar_groupby' => array(),
  1709. 'ar_having' => array(),
  1710. 'ar_orderby' => array(),
  1711. 'ar_wherein' => array(),
  1712. 'ar_aliased_tables' => array(),
  1713. 'ar_distinct' => FALSE,
  1714. 'ar_limit' => FALSE,
  1715. 'ar_offset' => FALSE,
  1716. 'ar_order' => FALSE,
  1717. );
  1718. $this->_reset_run($ar_reset_items);
  1719. }
  1720. // --------------------------------------------------------------------
  1721. /**
  1722. * Resets the active record "write" values.
  1723. *
  1724. * Called by the insert() update() insert_batch() update_batch() and delete() functions
  1725. *
  1726. * @access private
  1727. * @return void
  1728. */
  1729. function _reset_write()
  1730. {
  1731. $ar_reset_items = array(
  1732. 'ar_set' => array(),
  1733. 'ar_from' => array(),
  1734. 'ar_where' => array(),
  1735. 'ar_like' => array(),
  1736. 'ar_orderby' => array(),
  1737. 'ar_keys' => array(),
  1738. 'ar_limit' => FALSE,
  1739. 'ar_order' => FALSE
  1740. );
  1741. $this->_reset_run($ar_reset_items);
  1742. }
  1743. }
  1744. /* End of file DB_active_rec.php */
  1745. /* Location: ./system/database/DB_active_rec.php */