| 1 | <?php |
| 2 | /* |
| 3 | * $Id: Expression.php 3212 2007-11-24 18:58:33Z romanb $ |
| 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 | Doctrine::autoload('Doctrine_Connection_Module'); |
| 22 | /** |
| 23 | * Doctrine_Expression |
| 24 | * |
| 25 | * @package Doctrine |
| 26 | * @subpackage Expression |
| 27 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
| 28 | * @link www.phpdoctrine.org |
| 29 | * @since 1.0 |
| 30 | * @version $Revision: 3212 $ |
| 31 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi> |
| 32 | */ |
| 33 | class Doctrine_Expression |
| 34 | { |
| 35 | protected $_expression; |
| 36 | protected $_conn; |
| 37 | protected $_tokenizer; |
| 38 | |
| 39 | /** |
| 40 | * Create an expression |
| 41 | * |
| 42 | * @param string $expr The expression |
| 43 | * @param Doctrine_Connection $conn The connection (optional) |
| 44 | * @return void |
| 45 | */ |
| 46 | public function __construct($expr, $conn = null) |
| 47 | { |
| 48 | $this->_tokenizer = new Doctrine_Query_Tokenizer(); |
| 49 | $this->setExpression($expr); |
| 50 | if ($conn !== null) { |
| 51 | $this->_conn = $conn; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * getConnection |
| 57 | * |
| 58 | * @return Doctrine_Connection The connection |
| 59 | */ |
| 60 | public function getConnection() |
| 61 | { |
| 62 | if ( ! isset($this->_conn)) { |
| 63 | return Doctrine_Manager::connection(); |
| 64 | } |
| 65 | |
| 66 | return $this->_conn; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * setExpression |
| 71 | * |
| 72 | * @param string $clause The expression to set |
| 73 | * @return void |
| 74 | */ |
| 75 | public function setExpression($clause) |
| 76 | { |
| 77 | $this->_expression = $this->parseClause($clause); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * parseExpression |
| 82 | * |
| 83 | * @todo: What does this function do? |
| 84 | * |
| 85 | * @param string $expr The expression to parse |
| 86 | * @return void |
| 87 | */ |
| 88 | public function parseExpression($expr) |
| 89 | { |
| 90 | $pos = strpos($expr, '('); |
| 91 | if ($pos === false) { |
| 92 | return $expr; |
| 93 | } |
| 94 | |
| 95 | // get the name of the function |
| 96 | $name = substr($expr, 0, $pos); |
| 97 | $argStr = substr($expr, ($pos + 1), -1); |
| 98 | |
| 99 | // parse args |
| 100 | foreach ($this->_tokenizer->bracketExplode($argStr, ',') as $arg) { |
| 101 | $args[] = $this->parseClause($arg); |
| 102 | } |
| 103 | |
| 104 | return call_user_func_array(array($this->getConnection()->expression, $name), $args); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * parseClause |
| 109 | * |
| 110 | * @param string $clause The clause |
| 111 | * @return string The parse clause |
| 112 | */ |
| 113 | public function parseClause($clause) |
| 114 | { |
| 115 | $e = $this->_tokenizer->bracketExplode($clause, ' '); |
| 116 | |
| 117 | foreach ($e as $k => $expr) { |
| 118 | $e[$k] = $this->parseExpression($expr); |
| 119 | } |
| 120 | |
| 121 | return implode(' ', $e); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * getSql |
| 126 | * |
| 127 | * @return string The expression |
| 128 | */ |
| 129 | public function getSql() |
| 130 | { |
| 131 | |
| 132 | return $this->_expression; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * __toString |
| 137 | * |
| 138 | * @return void |
| 139 | */ |
| 140 | public function __toString() |
| 141 | { |
| 142 | return $this->getSql(); |
| 143 | } |
| 144 | } |