/system/application/plugins/doctrine/lib/Doctrine/Expression.php

https://github.com/cawago/ci_campusync_auth · PHP · 158 lines · 56 code · 14 blank · 88 comment · 4 complexity · 442b7734a457f1ee67eb6af79da7d1f0 MD5 · raw file

  1. <?php
  2. /*
  3. * $Id: Expression.php 5798 2009-06-02 15:10:46Z piccoloprincipe $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.phpdoctrine.org>.
  20. */
  21. /**
  22. * Doctrine_Expression memorizes a dql expression that use a db function.
  23. *
  24. * This class manages abstractions of dql expressions like query parts
  25. * that use CONCAT(), MIN(), SUM().
  26. *
  27. * @package Doctrine
  28. * @subpackage Expression
  29. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  30. * @link www.phpdoctrine.org
  31. * @since 1.0
  32. * @version $Revision: 5798 $
  33. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  34. */
  35. class Doctrine_Expression
  36. {
  37. protected $_expression;
  38. protected $_conn;
  39. protected $_tokenizer;
  40. /**
  41. * Creates an expression.
  42. *
  43. * The constructor needs the dql fragment that contains one or more dbms
  44. * functions.
  45. * <code>
  46. * $e = new Doctrine_Expression("CONCAT('some', 'one')");
  47. * </code>
  48. *
  49. * @param string $expr sql fragment
  50. * @param Doctrine_Connection $conn the connection (optional)
  51. */
  52. public function __construct($expr, $conn = null)
  53. {
  54. $this->_tokenizer = new Doctrine_Query_Tokenizer();
  55. $this->setExpression($expr);
  56. if ($conn !== null) {
  57. $this->_conn = $conn;
  58. }
  59. }
  60. /**
  61. * Retrieves the connection associated to this expression at creation,
  62. * or the current connection used if it was not specified.
  63. *
  64. * @return Doctrine_Connection The connection
  65. */
  66. public function getConnection()
  67. {
  68. if ( ! isset($this->_conn)) {
  69. return Doctrine_Manager::connection();
  70. }
  71. return $this->_conn;
  72. }
  73. /**
  74. * Sets the contained expression assuring that it is parsed.
  75. * <code>
  76. * $e->setExpression("CONCAT('some', 'one')");
  77. * </code>
  78. *
  79. * @param string $clause The expression to set
  80. * @return void
  81. */
  82. public function setExpression($clause)
  83. {
  84. $this->_expression = $this->parseClause($clause);
  85. }
  86. /**
  87. * Parses a single expressions and substitutes dql abstract functions
  88. * with their concrete sql counterparts for the given connection.
  89. *
  90. * @param string $expr The expression to parse
  91. * @return string
  92. */
  93. public function parseExpression($expr)
  94. {
  95. $pos = strpos($expr, '(');
  96. $quoted = (substr($expr, 0, 1) === "'" && substr($expr, -1) === "'");
  97. if ($pos === false || $quoted) {
  98. return $expr;
  99. }
  100. // get the name of the function
  101. $name = substr($expr, 0, $pos);
  102. $argStr = substr($expr, ($pos + 1), -1);
  103. // parse args
  104. foreach ($this->_tokenizer->bracketExplode($argStr, ',') as $arg) {
  105. $args[] = $this->parseClause($arg);
  106. }
  107. return call_user_func_array(array($this->getConnection()->expression, $name), $args);
  108. }
  109. /**
  110. * Parses a set of expressions at once.
  111. * @see parseExpression()
  112. *
  113. * @param string $clause The clause. Can be complex and parenthesised.
  114. * @return string The parsed clause.
  115. */
  116. public function parseClause($clause)
  117. {
  118. $e = $this->_tokenizer->bracketExplode($clause, ' ');
  119. foreach ($e as $k => $expr) {
  120. $e[$k] = $this->parseExpression($expr);
  121. }
  122. return implode(' ', $e);
  123. }
  124. /**
  125. * Gets the sql fragment represented.
  126. *
  127. * @return string
  128. */
  129. public function getSql()
  130. {
  131. return $this->_expression;
  132. }
  133. /**
  134. * Magic method.
  135. *
  136. * Returns a string representation of this object. Proxies to @see getSql().
  137. *
  138. * @return string
  139. */
  140. public function __toString()
  141. {
  142. return $this->getSql();
  143. }
  144. }