| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: Expression.php 7490 2010-03-29 19:53:27Z jwage $
|
|
|
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.doctrine-project.org>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Doctrine_Expression memorizes a dql expression that use a db function.
|
|
|
24 |
*
|
|
|
25 |
* This class manages abstractions of dql expressions like query parts
|
|
|
26 |
* that use CONCAT(), MIN(), SUM().
|
|
|
27 |
*
|
|
|
28 |
* @package Doctrine
|
|
|
29 |
* @subpackage Expression
|
|
|
30 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
31 |
* @link www.doctrine-project.org
|
|
|
32 |
* @since 1.0
|
|
|
33 |
* @version $Revision: 7490 $
|
|
|
34 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
35 |
*/
|
|
|
36 |
class Doctrine_Expression
|
|
|
37 |
{
|
|
|
38 |
protected $_expression;
|
|
|
39 |
protected $_conn;
|
|
|
40 |
protected $_tokenizer;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Creates an expression.
|
|
|
44 |
*
|
|
|
45 |
* The constructor needs the dql fragment that contains one or more dbms
|
|
|
46 |
* functions.
|
|
|
47 |
* <code>
|
|
|
48 |
* $e = new Doctrine_Expression("CONCAT('some', 'one')");
|
|
|
49 |
* </code>
|
|
|
50 |
*
|
|
|
51 |
* @param string $expr sql fragment
|
|
|
52 |
* @param Doctrine_Connection $conn the connection (optional)
|
|
|
53 |
*/
|
|
|
54 |
public function __construct($expr, $conn = null)
|
|
|
55 |
{
|
|
|
56 |
if ($conn !== null) {
|
|
|
57 |
$this->_conn = $conn;
|
|
|
58 |
}
|
|
|
59 |
$this->_tokenizer = new Doctrine_Query_Tokenizer();
|
|
|
60 |
$this->setExpression($expr);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Retrieves the connection associated to this expression at creation,
|
|
|
65 |
* or the current connection used if it was not specified.
|
|
|
66 |
*
|
|
|
67 |
* @return Doctrine_Connection The connection
|
|
|
68 |
*/
|
|
|
69 |
public function getConnection()
|
|
|
70 |
{
|
|
|
71 |
if ( ! isset($this->_conn)) {
|
|
|
72 |
return Doctrine_Manager::connection();
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
return $this->_conn;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Sets the contained expression assuring that it is parsed.
|
|
|
80 |
* <code>
|
|
|
81 |
* $e->setExpression("CONCAT('some', 'one')");
|
|
|
82 |
* </code>
|
|
|
83 |
*
|
|
|
84 |
* @param string $clause The expression to set
|
|
|
85 |
* @return void
|
|
|
86 |
*/
|
|
|
87 |
public function setExpression($clause)
|
|
|
88 |
{
|
|
|
89 |
$this->_expression = $this->parseClause($clause);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Parses a single expressions and substitutes dql abstract functions
|
|
|
94 |
* with their concrete sql counterparts for the given connection.
|
|
|
95 |
*
|
|
|
96 |
* @param string $expr The expression to parse
|
|
|
97 |
* @return string
|
|
|
98 |
*/
|
|
|
99 |
public function parseExpression($expr)
|
|
|
100 |
{
|
|
|
101 |
$pos = strpos($expr, '(');
|
|
|
102 |
$quoted = (substr($expr, 0, 1) === "'" && substr($expr, -1) === "'");
|
|
|
103 |
if ($pos === false || $quoted) {
|
|
|
104 |
return $expr;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
// get the name of the function
|
|
|
108 |
$name = substr($expr, 0, $pos);
|
|
|
109 |
$argStr = substr($expr, ($pos + 1), -1);
|
|
|
110 |
|
|
|
111 |
// parse args
|
|
|
112 |
foreach ($this->_tokenizer->bracketExplode($argStr, ',') as $arg) {
|
|
|
113 |
$args[] = $this->parseClause($arg);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
return call_user_func_array(array($this->getConnection()->expression, $name), $args);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Parses a set of expressions at once.
|
|
|
121 |
* @see parseExpression()
|
|
|
122 |
*
|
|
|
123 |
* @param string $clause The clause. Can be complex and parenthesised.
|
|
|
124 |
* @return string The parsed clause.
|
|
|
125 |
*/
|
|
|
126 |
public function parseClause($clause)
|
|
|
127 |
{
|
|
|
128 |
$e = $this->_tokenizer->bracketExplode($clause, ' ');
|
|
|
129 |
|
|
|
130 |
foreach ($e as $k => $expr) {
|
|
|
131 |
$e[$k] = $this->parseExpression($expr);
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
return implode(' ', $e);
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
/**
|
|
|
138 |
* Gets the sql fragment represented.
|
|
|
139 |
*
|
|
|
140 |
* @return string
|
|
|
141 |
*/
|
|
|
142 |
public function getSql()
|
|
|
143 |
{
|
|
|
144 |
return $this->_expression;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* Magic method.
|
|
|
149 |
*
|
|
|
150 |
* Returns a string representation of this object. Proxies to @see getSql().
|
|
|
151 |
*
|
|
|
152 |
* @return string
|
|
|
153 |
*/
|
|
|
154 |
public function __toString()
|
|
|
155 |
{
|
|
|
156 |
return $this->getSql();
|
|
|
157 |
}
|
|
|
158 |
}
|