2009-06-02 22:05:26 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Doctrine\ORM\Query;
|
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
use Doctrine\ORM\Query;
|
2010-08-09 15:13:21 +04:00
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
2009-06-02 22:05:26 +04:00
|
|
|
|
|
|
|
/**
|
2010-02-20 00:28:17 +03:00
|
|
|
* An LL(*) recursive-descent parser for the context-free grammar of the Doctrine Query Language.
|
2009-06-02 22:05:26 +04:00
|
|
|
* Parses a DQL query, reports any errors in it, and generates an AST.
|
|
|
|
*
|
2009-08-07 01:42:07 +04:00
|
|
|
* @since 2.0
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
* @author Janne Vanhala <jpvanhal@cc.hut.fi>
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
|
|
|
class Parser
|
|
|
|
{
|
2010-03-29 17:20:41 +04:00
|
|
|
/** READ-ONLY: Maps BUILT-IN string function names to AST class names. */
|
2009-06-02 22:05:26 +04:00
|
|
|
private static $_STRING_FUNCTIONS = array(
|
2009-11-04 00:42:58 +03:00
|
|
|
'concat' => 'Doctrine\ORM\Query\AST\Functions\ConcatFunction',
|
2009-06-02 22:05:26 +04:00
|
|
|
'substring' => 'Doctrine\ORM\Query\AST\Functions\SubstringFunction',
|
2009-11-04 00:42:58 +03:00
|
|
|
'trim' => 'Doctrine\ORM\Query\AST\Functions\TrimFunction',
|
|
|
|
'lower' => 'Doctrine\ORM\Query\AST\Functions\LowerFunction',
|
2011-09-08 09:10:48 +04:00
|
|
|
'upper' => 'Doctrine\ORM\Query\AST\Functions\UpperFunction',
|
|
|
|
'identity' => 'Doctrine\ORM\Query\AST\Functions\IdentityFunction',
|
2009-07-17 02:03:35 +04:00
|
|
|
);
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
/** READ-ONLY: Maps BUILT-IN numeric function names to AST class names. */
|
2009-06-02 22:05:26 +04:00
|
|
|
private static $_NUMERIC_FUNCTIONS = array(
|
2011-03-27 14:18:47 +04:00
|
|
|
'length' => 'Doctrine\ORM\Query\AST\Functions\LengthFunction',
|
|
|
|
'locate' => 'Doctrine\ORM\Query\AST\Functions\LocateFunction',
|
|
|
|
'abs' => 'Doctrine\ORM\Query\AST\Functions\AbsFunction',
|
|
|
|
'sqrt' => 'Doctrine\ORM\Query\AST\Functions\SqrtFunction',
|
|
|
|
'mod' => 'Doctrine\ORM\Query\AST\Functions\ModFunction',
|
|
|
|
'size' => 'Doctrine\ORM\Query\AST\Functions\SizeFunction',
|
|
|
|
'date_diff' => 'Doctrine\ORM\Query\AST\Functions\DateDiffFunction',
|
2009-06-02 22:05:26 +04:00
|
|
|
);
|
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
/** READ-ONLY: Maps BUILT-IN datetime function names to AST class names. */
|
2009-06-02 22:05:26 +04:00
|
|
|
private static $_DATETIME_FUNCTIONS = array(
|
2009-11-04 00:42:58 +03:00
|
|
|
'current_date' => 'Doctrine\ORM\Query\AST\Functions\CurrentDateFunction',
|
|
|
|
'current_time' => 'Doctrine\ORM\Query\AST\Functions\CurrentTimeFunction',
|
2011-03-27 14:18:47 +04:00
|
|
|
'current_timestamp' => 'Doctrine\ORM\Query\AST\Functions\CurrentTimestampFunction',
|
|
|
|
'date_add' => 'Doctrine\ORM\Query\AST\Functions\DateAddFunction',
|
|
|
|
'date_sub' => 'Doctrine\ORM\Query\AST\Functions\DateSubFunction',
|
2009-06-02 22:05:26 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2009-12-27 06:26:15 +03:00
|
|
|
* Expressions that were encountered during parsing of identifiers and expressions
|
2009-06-02 22:05:26 +04:00
|
|
|
* and still need to be validated.
|
|
|
|
*/
|
2010-02-20 00:28:17 +03:00
|
|
|
private $_deferredIdentificationVariables = array();
|
|
|
|
private $_deferredPartialObjectExpressions = array();
|
|
|
|
private $_deferredPathExpressions = array();
|
|
|
|
private $_deferredResultVariables = array();
|
2009-06-02 22:05:26 +04:00
|
|
|
|
|
|
|
/**
|
2009-06-14 21:34:28 +04:00
|
|
|
* The lexer.
|
2009-06-02 22:05:26 +04:00
|
|
|
*
|
|
|
|
* @var Doctrine\ORM\Query\Lexer
|
|
|
|
*/
|
|
|
|
private $_lexer;
|
|
|
|
|
|
|
|
/**
|
2009-06-14 21:34:28 +04:00
|
|
|
* The parser result.
|
2009-06-02 22:05:26 +04:00
|
|
|
*
|
|
|
|
* @var Doctrine\ORM\Query\ParserResult
|
|
|
|
*/
|
|
|
|
private $_parserResult;
|
2009-06-14 21:34:28 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
|
|
|
* The EntityManager.
|
|
|
|
*
|
|
|
|
* @var EnityManager
|
|
|
|
*/
|
|
|
|
private $_em;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
|
|
|
* The Query to parse.
|
|
|
|
*
|
|
|
|
* @var Query
|
|
|
|
*/
|
|
|
|
private $_query;
|
|
|
|
|
|
|
|
/**
|
2009-06-14 21:34:28 +04:00
|
|
|
* Map of declared query components in the parsed query.
|
2009-06-02 22:05:26 +04:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_queryComponents = array();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-04 23:48:40 +04:00
|
|
|
/**
|
|
|
|
* Keeps the nesting level of defined ResultVariables
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
private $_nestingLevel = 0;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-16 07:22:08 +04:00
|
|
|
/**
|
2009-08-13 14:13:06 +04:00
|
|
|
* Any additional custom tree walkers that modify the AST.
|
2009-07-16 07:22:08 +04:00
|
|
|
*
|
2009-08-13 14:13:06 +04:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_customTreeWalkers = array();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-13 14:13:06 +04:00
|
|
|
/**
|
|
|
|
* The custom last tree walker, if any, that is responsible for producing the output.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-07-22 07:46:05 +04:00
|
|
|
* @var TreeWalker
|
2009-07-16 07:22:08 +04:00
|
|
|
*/
|
2009-08-13 14:13:06 +04:00
|
|
|
private $_customOutputWalker;
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2010-11-11 23:12:09 +03:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_identVariableExpressions = array();
|
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
|
|
|
* Creates a new query parser object.
|
|
|
|
*
|
|
|
|
* @param Query $query The Query to parse.
|
|
|
|
*/
|
|
|
|
public function __construct(Query $query)
|
|
|
|
{
|
|
|
|
$this->_query = $query;
|
|
|
|
$this->_em = $query->getEntityManager();
|
|
|
|
$this->_lexer = new Lexer($query->getDql());
|
2009-07-21 09:22:22 +04:00
|
|
|
$this->_parserResult = new ParserResult();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
/**
|
2009-08-13 14:13:06 +04:00
|
|
|
* Sets a custom tree walker that produces output.
|
|
|
|
* This tree walker will be run last over the AST, after any other walkers.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-08-13 14:13:06 +04:00
|
|
|
* @param string $className
|
2009-07-22 07:46:05 +04:00
|
|
|
*/
|
2009-08-13 14:13:06 +04:00
|
|
|
public function setCustomOutputTreeWalker($className)
|
2009-07-22 07:46:05 +04:00
|
|
|
{
|
2009-08-13 14:13:06 +04:00
|
|
|
$this->_customOutputWalker = $className;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-13 14:13:06 +04:00
|
|
|
/**
|
|
|
|
* Adds a custom tree walker for modifying the AST.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-08-13 14:13:06 +04:00
|
|
|
* @param string $className
|
|
|
|
*/
|
|
|
|
public function addCustomTreeWalker($className)
|
|
|
|
{
|
|
|
|
$this->_customTreeWalkers[] = $className;
|
2009-07-22 07:46:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the lexer used by the parser.
|
|
|
|
*
|
|
|
|
* @return Doctrine\ORM\Query\Lexer
|
|
|
|
*/
|
|
|
|
public function getLexer()
|
|
|
|
{
|
|
|
|
return $this->_lexer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the ParserResult that is being filled with information during parsing.
|
|
|
|
*
|
|
|
|
* @return Doctrine\ORM\Query\ParserResult
|
|
|
|
*/
|
|
|
|
public function getParserResult()
|
|
|
|
{
|
|
|
|
return $this->_parserResult;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
/**
|
|
|
|
* Gets the EntityManager used by the parser.
|
|
|
|
*
|
|
|
|
* @return EntityManager
|
|
|
|
*/
|
|
|
|
public function getEntityManager()
|
|
|
|
{
|
|
|
|
return $this->_em;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-03-16 22:21:59 +03:00
|
|
|
/**
|
|
|
|
* Parse and build AST for the given Query.
|
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\SelectStatement |
|
|
|
|
* \Doctrine\ORM\Query\AST\UpdateStatement |
|
|
|
|
* \Doctrine\ORM\Query\AST\DeleteStatement
|
|
|
|
*/
|
|
|
|
public function getAST()
|
|
|
|
{
|
|
|
|
// Parse & build AST
|
|
|
|
$AST = $this->QueryLanguage();
|
|
|
|
|
|
|
|
// Process any deferred validations of some nodes in the AST.
|
|
|
|
// This also allows post-processing of the AST for modification purposes.
|
|
|
|
$this->_processDeferredIdentificationVariables();
|
2010-04-23 07:51:32 +04:00
|
|
|
|
2010-03-16 22:21:59 +03:00
|
|
|
if ($this->_deferredPartialObjectExpressions) {
|
|
|
|
$this->_processDeferredPartialObjectExpressions();
|
|
|
|
}
|
2010-04-23 07:51:32 +04:00
|
|
|
|
2010-03-16 22:21:59 +03:00
|
|
|
if ($this->_deferredPathExpressions) {
|
|
|
|
$this->_processDeferredPathExpressions($AST);
|
|
|
|
}
|
2010-04-23 07:51:32 +04:00
|
|
|
|
2010-03-16 22:21:59 +03:00
|
|
|
if ($this->_deferredResultVariables) {
|
|
|
|
$this->_processDeferredResultVariables();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $AST;
|
|
|
|
}
|
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
|
|
|
* Attempts to match the given token with the current lookahead token.
|
|
|
|
*
|
|
|
|
* If they match, updates the lookahead token; otherwise raises a syntax
|
|
|
|
* error.
|
|
|
|
*
|
2011-04-01 21:54:12 +04:00
|
|
|
* @param int token type
|
2010-02-20 00:28:17 +03:00
|
|
|
* @return void
|
|
|
|
* @throws QueryException If the tokens dont match.
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
|
|
|
public function match($token)
|
|
|
|
{
|
2010-05-19 00:19:08 +04:00
|
|
|
// short-circuit on first condition, usually types match
|
|
|
|
if ($this->_lexer->lookahead['type'] !== $token &&
|
|
|
|
$token !== Lexer::T_IDENTIFIER &&
|
|
|
|
$this->_lexer->lookahead['type'] <= Lexer::T_IDENTIFIER
|
|
|
|
) {
|
2009-06-02 22:05:26 +04:00
|
|
|
$this->syntaxError($this->_lexer->getLiteral($token));
|
2009-08-08 01:47:21 +04:00
|
|
|
}
|
2009-06-02 22:05:26 +04:00
|
|
|
|
|
|
|
$this->_lexer->moveNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-06-14 21:34:28 +04:00
|
|
|
* Free this parser enabling it to be reused
|
|
|
|
*
|
|
|
|
* @param boolean $deep Whether to clean peek and reset errors
|
|
|
|
* @param integer $position Position to reset
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
|
|
|
public function free($deep = false, $position = 0)
|
|
|
|
{
|
|
|
|
// WARNING! Use this method with care. It resets the scanner!
|
|
|
|
$this->_lexer->resetPosition($position);
|
|
|
|
|
|
|
|
// Deep = true cleans peek and also any previously defined errors
|
|
|
|
if ($deep) {
|
|
|
|
$this->_lexer->resetPeek();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_lexer->token = null;
|
|
|
|
$this->_lexer->lookahead = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a query string.
|
2009-06-14 21:34:28 +04:00
|
|
|
*
|
2009-06-02 22:05:26 +04:00
|
|
|
* @return ParserResult
|
|
|
|
*/
|
|
|
|
public function parse()
|
|
|
|
{
|
2010-03-16 22:21:59 +03:00
|
|
|
$AST = $this->getAST();
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2010-12-28 12:17:33 +03:00
|
|
|
$this->fixIdentificationVariableOrder($AST);
|
|
|
|
$this->assertSelectEntityRootAliasRequirement();
|
2010-12-09 01:42:02 +03:00
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
if (($customWalkers = $this->_query->getHint(Query::HINT_CUSTOM_TREE_WALKERS)) !== false) {
|
2009-08-13 14:13:06 +04:00
|
|
|
$this->_customTreeWalkers = $customWalkers;
|
|
|
|
}
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
if (($customOutputWalker = $this->_query->getHint(Query::HINT_CUSTOM_OUTPUT_WALKER)) !== false) {
|
2010-03-06 12:52:48 +03:00
|
|
|
$this->_customOutputWalker = $customOutputWalker;
|
|
|
|
}
|
|
|
|
|
2009-08-13 14:13:06 +04:00
|
|
|
// Run any custom tree walkers over the AST
|
|
|
|
if ($this->_customTreeWalkers) {
|
|
|
|
$treeWalkerChain = new TreeWalkerChain($this->_query, $this->_parserResult, $this->_queryComponents);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-13 14:13:06 +04:00
|
|
|
foreach ($this->_customTreeWalkers as $walker) {
|
|
|
|
$treeWalkerChain->addTreeWalker($walker);
|
2009-08-11 14:51:38 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-13 14:13:06 +04:00
|
|
|
if ($AST instanceof AST\SelectStatement) {
|
|
|
|
$treeWalkerChain->walkSelectStatement($AST);
|
|
|
|
} else if ($AST instanceof AST\UpdateStatement) {
|
|
|
|
$treeWalkerChain->walkUpdateStatement($AST);
|
|
|
|
} else {
|
|
|
|
$treeWalkerChain->walkDeleteStatement($AST);
|
|
|
|
}
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-13 14:13:06 +04:00
|
|
|
if ($this->_customOutputWalker) {
|
|
|
|
$outputWalker = new $this->_customOutputWalker(
|
|
|
|
$this->_query, $this->_parserResult, $this->_queryComponents
|
|
|
|
);
|
2009-08-11 14:51:38 +04:00
|
|
|
} else {
|
2009-08-13 14:13:06 +04:00
|
|
|
$outputWalker = new SqlWalker(
|
2009-08-11 14:51:38 +04:00
|
|
|
$this->_query, $this->_parserResult, $this->_queryComponents
|
|
|
|
);
|
2009-08-13 14:13:06 +04:00
|
|
|
}
|
2009-06-02 22:05:26 +04:00
|
|
|
|
|
|
|
// Assign an SQL executor to the parser result
|
2009-08-13 14:13:06 +04:00
|
|
|
$this->_parserResult->setSqlExecutor($outputWalker->getExecutor($AST));
|
2009-06-02 22:05:26 +04:00
|
|
|
|
|
|
|
return $this->_parserResult;
|
|
|
|
}
|
2010-12-28 12:17:33 +03:00
|
|
|
|
|
|
|
private function assertSelectEntityRootAliasRequirement()
|
|
|
|
{
|
|
|
|
if ( count($this->_identVariableExpressions) > 0) {
|
|
|
|
$foundRootEntity = false;
|
|
|
|
foreach ($this->_identVariableExpressions AS $dqlAlias => $expr) {
|
|
|
|
if (isset($this->_queryComponents[$dqlAlias]) && $this->_queryComponents[$dqlAlias]['parent'] === null) {
|
|
|
|
$foundRootEntity = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$foundRootEntity) {
|
|
|
|
$this->semanticalError('Cannot select entity through identification variables without choosing at least one root entity alias.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fix order of identification variables.
|
|
|
|
*
|
|
|
|
* They have to appear in the select clause in the same order as the
|
|
|
|
* declarations (from ... x join ... y join ... z ...) appear in the query
|
|
|
|
* as the hydration process relies on that order for proper operation.
|
|
|
|
*
|
|
|
|
* @param AST\SelectStatement|AST\DeleteStatement|AST\UpdateStatement $AST
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function fixIdentificationVariableOrder($AST)
|
|
|
|
{
|
|
|
|
if ( count($this->_identVariableExpressions) > 1) {
|
|
|
|
foreach ($this->_queryComponents as $dqlAlias => $qComp) {
|
|
|
|
if (isset($this->_identVariableExpressions[$dqlAlias])) {
|
|
|
|
$expr = $this->_identVariableExpressions[$dqlAlias];
|
|
|
|
$key = array_search($expr, $AST->selectClause->selectExpressions);
|
|
|
|
unset($AST->selectClause->selectExpressions[$key]);
|
|
|
|
$AST->selectClause->selectExpressions[] = $expr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
|
|
|
* Generates a new syntax error.
|
|
|
|
*
|
2010-02-20 00:28:17 +03:00
|
|
|
* @param string $expected Expected string.
|
|
|
|
* @param array $token Got token.
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @throws \Doctrine\ORM\Query\QueryException
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
|
|
|
public function syntaxError($expected = '', $token = null)
|
|
|
|
{
|
|
|
|
if ($token === null) {
|
|
|
|
$token = $this->_lexer->lookahead;
|
|
|
|
}
|
|
|
|
|
2009-07-21 09:22:22 +04:00
|
|
|
$tokenPos = (isset($token['position'])) ? $token['position'] : '-1';
|
|
|
|
$message = "line 0, col {$tokenPos}: Error: ";
|
2009-06-02 22:05:26 +04:00
|
|
|
|
|
|
|
if ($expected !== '') {
|
2009-11-04 04:52:40 +03:00
|
|
|
$message .= "Expected {$expected}, got ";
|
2009-06-02 22:05:26 +04:00
|
|
|
} else {
|
|
|
|
$message .= 'Unexpected ';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->_lexer->lookahead === null) {
|
|
|
|
$message .= 'end of string.';
|
|
|
|
} else {
|
2009-08-08 01:47:21 +04:00
|
|
|
$message .= "'{$token['value']}'";
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
2010-03-15 20:19:00 +03:00
|
|
|
throw QueryException::syntaxError($message);
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a new semantical error.
|
|
|
|
*
|
|
|
|
* @param string $message Optional message.
|
|
|
|
* @param array $token Optional token.
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @throws \Doctrine\ORM\Query\QueryException
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
|
|
|
public function semanticalError($message = '', $token = null)
|
|
|
|
{
|
|
|
|
if ($token === null) {
|
2009-07-16 07:22:08 +04:00
|
|
|
$token = $this->_lexer->lookahead;
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-05 23:00:16 +04:00
|
|
|
// Minimum exposed chars ahead of token
|
|
|
|
$distance = 12;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-16 07:22:08 +04:00
|
|
|
// Find a position of a final word to display in error string
|
|
|
|
$dql = $this->_query->getDql();
|
2009-08-05 23:00:16 +04:00
|
|
|
$length = strlen($dql);
|
|
|
|
$pos = $token['position'] + $distance;
|
|
|
|
$pos = strpos($dql, ' ', ($length > $pos) ? $pos : $length);
|
|
|
|
$length = ($pos !== false) ? $pos - $token['position'] : $distance;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-16 07:22:08 +04:00
|
|
|
// Building informative message
|
2009-08-05 23:00:16 +04:00
|
|
|
$message = 'line 0, col ' . (
|
|
|
|
(isset($token['position']) && $token['position'] > 0) ? $token['position'] : '-1'
|
|
|
|
) . " near '" . substr($dql, $token['position'], $length) . "': Error: " . $message;
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-21 09:22:22 +04:00
|
|
|
throw \Doctrine\ORM\Query\QueryException::semanticalError($message);
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
2009-07-21 09:22:22 +04:00
|
|
|
* Peeks beyond the specified token and returns the first token after that one.
|
2009-07-31 08:03:01 +04:00
|
|
|
*
|
|
|
|
* @param array $token
|
|
|
|
* @return array
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-21 09:22:22 +04:00
|
|
|
private function _peekBeyond($token)
|
|
|
|
{
|
|
|
|
$peek = $this->_lexer->peek();
|
|
|
|
|
|
|
|
while ($peek['value'] != $token) {
|
|
|
|
$peek = $this->_lexer->peek();
|
|
|
|
}
|
|
|
|
|
|
|
|
$peek = $this->_lexer->peek();
|
|
|
|
$this->_lexer->resetPeek();
|
2009-06-14 21:34:28 +04:00
|
|
|
|
2009-07-21 09:22:22 +04:00
|
|
|
return $peek;
|
|
|
|
}
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2010-08-12 06:12:44 +04:00
|
|
|
/**
|
|
|
|
* Peek beyond the matched closing parenthesis and return the first token after that one.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function _peekBeyondClosingParenthesis()
|
|
|
|
{
|
|
|
|
$token = $this->_lexer->peek();
|
|
|
|
$numUnmatched = 1;
|
|
|
|
|
|
|
|
while ($numUnmatched > 0 && $token !== null) {
|
|
|
|
if ($token['value'] == ')') {
|
|
|
|
--$numUnmatched;
|
|
|
|
} else if ($token['value'] == '(') {
|
|
|
|
++$numUnmatched;
|
|
|
|
}
|
|
|
|
|
|
|
|
$token = $this->_lexer->peek();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_lexer->resetPeek();
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the given token indicates a mathematical operator.
|
|
|
|
*
|
2010-08-24 19:17:58 +04:00
|
|
|
* @return boolean TRUE if the token is a mathematical operator, FALSE otherwise.
|
2010-08-12 06:12:44 +04:00
|
|
|
*/
|
|
|
|
private function _isMathOperator($token)
|
|
|
|
{
|
2010-08-24 19:17:58 +04:00
|
|
|
return in_array($token['value'], array("+", "-", "/", "*"));
|
2010-08-12 06:12:44 +04:00
|
|
|
}
|
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
2009-07-21 09:22:22 +04:00
|
|
|
* Checks if the next-next (after lookahead) token starts a function.
|
2009-06-02 22:05:26 +04:00
|
|
|
*
|
2009-07-21 09:22:22 +04:00
|
|
|
* @return boolean TRUE if the next-next tokens start a function, FALSE otherwise.
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-21 09:22:22 +04:00
|
|
|
private function _isFunction()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2010-02-20 21:27:05 +03:00
|
|
|
$peek = $this->_lexer->peek();
|
2009-07-21 09:22:22 +04:00
|
|
|
$nextpeek = $this->_lexer->peek();
|
|
|
|
$this->_lexer->resetPeek();
|
2010-02-20 21:27:05 +03:00
|
|
|
|
2009-07-21 09:22:22 +04:00
|
|
|
// We deny the COUNT(SELECT * FROM User u) here. COUNT won't be considered a function
|
|
|
|
return ($peek['value'] === '(' && $nextpeek['type'] !== Lexer::T_SELECT);
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
2009-07-21 09:22:22 +04:00
|
|
|
* Checks whether the given token type indicates an aggregate function.
|
2009-06-02 22:05:26 +04:00
|
|
|
*
|
2009-07-21 09:22:22 +04:00
|
|
|
* @return boolean TRUE if the token type is an aggregate function, FALSE otherwise.
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-21 09:22:22 +04:00
|
|
|
private function _isAggregateFunction($tokenType)
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-07-21 09:22:22 +04:00
|
|
|
return $tokenType == Lexer::T_AVG || $tokenType == Lexer::T_MIN ||
|
|
|
|
$tokenType == Lexer::T_MAX || $tokenType == Lexer::T_SUM ||
|
|
|
|
$tokenType == Lexer::T_COUNT;
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
/**
|
|
|
|
* Checks whether the current lookahead token of the lexer has the type
|
|
|
|
* T_ALL, T_ANY or T_SOME.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private function _isNextAllAnySome()
|
|
|
|
{
|
|
|
|
return $this->_lexer->lookahead['type'] === Lexer::T_ALL ||
|
|
|
|
$this->_lexer->lookahead['type'] === Lexer::T_ANY ||
|
|
|
|
$this->_lexer->lookahead['type'] === Lexer::T_SOME;
|
|
|
|
}
|
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
|
|
|
* Checks whether the next 2 tokens start a subselect.
|
|
|
|
*
|
|
|
|
* @return boolean TRUE if the next 2 tokens start a subselect, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
private function _isSubselect()
|
|
|
|
{
|
|
|
|
$la = $this->_lexer->lookahead;
|
|
|
|
$next = $this->_lexer->glimpse();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
return ($la['value'] === '(' && $next['type'] === Lexer::T_SELECT);
|
|
|
|
}
|
2009-07-21 09:22:22 +04:00
|
|
|
|
2009-12-27 06:26:15 +03:00
|
|
|
/**
|
2010-08-12 06:12:44 +04:00
|
|
|
* Validates that the given <tt>IdentificationVariable</tt> is semantically correct.
|
2009-12-27 06:26:15 +03:00
|
|
|
* It must exist in query components list.
|
|
|
|
*
|
2010-02-20 00:28:17 +03:00
|
|
|
* @return void
|
2009-12-27 06:26:15 +03:00
|
|
|
*/
|
2010-02-20 00:28:17 +03:00
|
|
|
private function _processDeferredIdentificationVariables()
|
2009-12-27 06:26:15 +03:00
|
|
|
{
|
2010-02-20 00:28:17 +03:00
|
|
|
foreach ($this->_deferredIdentificationVariables as $deferredItem) {
|
|
|
|
$identVariable = $deferredItem['expression'];
|
|
|
|
|
|
|
|
// Check if IdentificationVariable exists in queryComponents
|
|
|
|
if ( ! isset($this->_queryComponents[$identVariable])) {
|
|
|
|
$this->semanticalError(
|
2010-04-23 07:51:32 +04:00
|
|
|
"'$identVariable' is not defined.", $deferredItem['token']
|
2010-02-20 00:28:17 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$qComp = $this->_queryComponents[$identVariable];
|
|
|
|
|
|
|
|
// Check if queryComponent points to an AbstractSchemaName or a ResultVariable
|
|
|
|
if ( ! isset($qComp['metadata'])) {
|
|
|
|
$this->semanticalError(
|
2010-04-23 07:51:32 +04:00
|
|
|
"'$identVariable' does not point to a Class.", $deferredItem['token']
|
2010-02-20 00:28:17 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate if identification variable nesting level is lower or equal than the current one
|
|
|
|
if ($qComp['nestingLevel'] > $deferredItem['nestingLevel']) {
|
|
|
|
$this->semanticalError(
|
2010-04-23 07:51:32 +04:00
|
|
|
"'$identVariable' is used outside the scope of its declaration.", $deferredItem['token']
|
2010-02-20 00:28:17 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-12 06:12:44 +04:00
|
|
|
/**
|
|
|
|
* Validates that the given <tt>PartialObjectExpression</tt> is semantically correct.
|
|
|
|
* It must exist in query components list.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-02-20 00:28:17 +03:00
|
|
|
private function _processDeferredPartialObjectExpressions()
|
|
|
|
{
|
|
|
|
foreach ($this->_deferredPartialObjectExpressions as $deferredItem) {
|
|
|
|
$expr = $deferredItem['expression'];
|
|
|
|
$class = $this->_queryComponents[$expr->identificationVariable]['metadata'];
|
2010-07-23 08:55:33 +04:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
foreach ($expr->partialFieldSet as $field) {
|
|
|
|
if ( ! isset($class->fieldMappings[$field])) {
|
|
|
|
$this->semanticalError(
|
2010-04-23 07:51:32 +04:00
|
|
|
"There is no mapped field named '$field' on class " . $class->name . ".",
|
|
|
|
$deferredItem['token']
|
2010-02-20 00:28:17 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2010-07-23 08:55:33 +04:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
if (array_intersect($class->identifier, $expr->partialFieldSet) != $class->identifier) {
|
|
|
|
$this->semanticalError(
|
2010-04-23 07:51:32 +04:00
|
|
|
"The partial field selection of class " . $class->name . " must contain the identifier.",
|
|
|
|
$deferredItem['token']
|
2010-02-20 00:28:17 +03:00
|
|
|
);
|
|
|
|
}
|
2009-07-21 09:22:22 +04:00
|
|
|
}
|
2009-12-27 06:26:15 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-12-27 06:26:15 +03:00
|
|
|
/**
|
2010-08-12 06:12:44 +04:00
|
|
|
* Validates that the given <tt>ResultVariable</tt> is semantically correct.
|
2009-12-27 06:26:15 +03:00
|
|
|
* It must exist in query components list.
|
|
|
|
*
|
2010-02-20 00:28:17 +03:00
|
|
|
* @return void
|
2009-12-27 06:26:15 +03:00
|
|
|
*/
|
2010-02-20 00:28:17 +03:00
|
|
|
private function _processDeferredResultVariables()
|
2009-12-27 06:26:15 +03:00
|
|
|
{
|
2010-02-20 00:28:17 +03:00
|
|
|
foreach ($this->_deferredResultVariables as $deferredItem) {
|
|
|
|
$resultVariable = $deferredItem['expression'];
|
|
|
|
|
|
|
|
// Check if ResultVariable exists in queryComponents
|
|
|
|
if ( ! isset($this->_queryComponents[$resultVariable])) {
|
|
|
|
$this->semanticalError(
|
2010-04-23 07:51:32 +04:00
|
|
|
"'$resultVariable' is not defined.", $deferredItem['token']
|
2010-02-20 00:28:17 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$qComp = $this->_queryComponents[$resultVariable];
|
|
|
|
|
|
|
|
// Check if queryComponent points to an AbstractSchemaName or a ResultVariable
|
|
|
|
if ( ! isset($qComp['resultVariable'])) {
|
|
|
|
$this->semanticalError(
|
2010-04-23 07:51:32 +04:00
|
|
|
"'$identVariable' does not point to a ResultVariable.", $deferredItem['token']
|
2010-02-20 00:28:17 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate if identification variable nesting level is lower or equal than the current one
|
|
|
|
if ($qComp['nestingLevel'] > $deferredItem['nestingLevel']) {
|
|
|
|
$this->semanticalError(
|
2010-04-23 07:51:32 +04:00
|
|
|
"'$resultVariable' is used outside the scope of its declaration.", $deferredItem['token']
|
2010-02-20 00:28:17 +03:00
|
|
|
);
|
|
|
|
}
|
2009-12-27 06:26:15 +03:00
|
|
|
}
|
2009-07-21 09:22:22 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-07-21 09:22:22 +04:00
|
|
|
/**
|
2010-08-12 06:12:44 +04:00
|
|
|
* Validates that the given <tt>PathExpression</tt> is semantically correct for grammar rules:
|
2009-07-21 09:22:22 +04:00
|
|
|
*
|
|
|
|
* AssociationPathExpression ::= CollectionValuedPathExpression | SingleValuedAssociationPathExpression
|
|
|
|
* SingleValuedPathExpression ::= StateFieldPathExpression | SingleValuedAssociationPathExpression
|
2010-07-23 08:55:33 +04:00
|
|
|
* StateFieldPathExpression ::= IdentificationVariable "." StateField
|
|
|
|
* SingleValuedAssociationPathExpression ::= IdentificationVariable "." SingleValuedAssociationField
|
|
|
|
* CollectionValuedPathExpression ::= IdentificationVariable "." CollectionValuedAssociationField
|
2009-07-21 09:22:22 +04:00
|
|
|
*
|
2010-01-01 01:48:51 +03:00
|
|
|
* @param array $deferredItem
|
|
|
|
* @param mixed $AST
|
2009-07-21 09:22:22 +04:00
|
|
|
*/
|
2010-02-20 00:28:17 +03:00
|
|
|
private function _processDeferredPathExpressions($AST)
|
2009-07-21 09:22:22 +04:00
|
|
|
{
|
2010-02-20 00:28:17 +03:00
|
|
|
foreach ($this->_deferredPathExpressions as $deferredItem) {
|
|
|
|
$pathExpression = $deferredItem['expression'];
|
|
|
|
|
|
|
|
$qComp = $this->_queryComponents[$pathExpression->identificationVariable];
|
2010-07-23 08:55:33 +04:00
|
|
|
$class = $qComp['metadata'];
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2010-07-23 08:55:33 +04:00
|
|
|
if (($field = $pathExpression->field) === null) {
|
|
|
|
$field = $pathExpression->field = $class->identifier[0];
|
2010-04-15 05:03:29 +04:00
|
|
|
}
|
|
|
|
|
2010-07-23 08:55:33 +04:00
|
|
|
// Check if field or association exists
|
|
|
|
if ( ! isset($class->associationMappings[$field]) && ! isset($class->fieldMappings[$field])) {
|
|
|
|
$this->semanticalError(
|
|
|
|
'Class ' . $class->name . ' has no field or association named ' . $field,
|
|
|
|
$deferredItem['token']
|
|
|
|
);
|
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2010-07-23 08:55:33 +04:00
|
|
|
if (isset($class->fieldMappings[$field])) {
|
|
|
|
$fieldType = AST\PathExpression::TYPE_STATE_FIELD;
|
|
|
|
} else {
|
|
|
|
$assoc = $class->associationMappings[$field];
|
2010-08-09 15:13:21 +04:00
|
|
|
$class = $this->_em->getClassMetadata($assoc['targetEntity']);
|
2010-01-01 01:48:51 +03:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($assoc['type'] & ClassMetadata::TO_ONE) {
|
2010-07-23 08:55:33 +04:00
|
|
|
$fieldType = AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION;
|
2010-02-20 00:28:17 +03:00
|
|
|
} else {
|
2010-07-23 08:55:33 +04:00
|
|
|
$fieldType = AST\PathExpression::TYPE_COLLECTION_VALUED_ASSOCIATION;
|
2010-01-01 01:48:51 +03:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate if PathExpression is one of the expected types
|
|
|
|
$expectedType = $pathExpression->expectedType;
|
|
|
|
|
|
|
|
if ( ! ($expectedType & $fieldType)) {
|
|
|
|
// We need to recognize which was expected type(s)
|
|
|
|
$expectedStringTypes = array();
|
|
|
|
|
|
|
|
// Validate state field type
|
|
|
|
if ($expectedType & AST\PathExpression::TYPE_STATE_FIELD) {
|
|
|
|
$expectedStringTypes[] = 'StateFieldPathExpression';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate single valued association (*-to-one)
|
|
|
|
if ($expectedType & AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION) {
|
|
|
|
$expectedStringTypes[] = 'SingleValuedAssociationField';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate single valued association (*-to-many)
|
|
|
|
if ($expectedType & AST\PathExpression::TYPE_COLLECTION_VALUED_ASSOCIATION) {
|
|
|
|
$expectedStringTypes[] = 'CollectionValuedAssociationField';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the error message
|
|
|
|
$semanticalError = 'Invalid PathExpression. ';
|
|
|
|
|
|
|
|
if (count($expectedStringTypes) == 1) {
|
|
|
|
$semanticalError .= 'Must be a ' . $expectedStringTypes[0] . '.';
|
2010-01-01 01:48:51 +03:00
|
|
|
} else {
|
2010-02-20 00:28:17 +03:00
|
|
|
$semanticalError .= implode(' or ', $expectedStringTypes) . ' expected.';
|
2010-01-01 01:48:51 +03:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
|
|
|
$this->semanticalError($semanticalError, $deferredItem['token']);
|
2009-07-21 09:22:22 +04:00
|
|
|
}
|
2010-07-23 08:55:33 +04:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
// We need to force the type in PathExpression
|
|
|
|
$pathExpression->type = $fieldType;
|
2009-07-30 08:40:04 +04:00
|
|
|
}
|
2009-07-21 09:22:22 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
|
|
|
* QueryLanguage ::= SelectStatement | UpdateStatement | DeleteStatement
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
2010-03-05 19:35:00 +03:00
|
|
|
* @return \Doctrine\ORM\Query\AST\SelectStatement |
|
|
|
|
* \Doctrine\ORM\Query\AST\UpdateStatement |
|
2009-08-02 03:40:11 +04:00
|
|
|
* \Doctrine\ORM\Query\AST\DeleteStatement
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function QueryLanguage()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
|
|
|
$this->_lexer->moveNext();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
switch ($this->_lexer->lookahead['type']) {
|
|
|
|
case Lexer::T_SELECT:
|
2010-02-20 00:28:17 +03:00
|
|
|
$statement = $this->SelectStatement();
|
|
|
|
break;
|
2009-06-02 22:05:26 +04:00
|
|
|
case Lexer::T_UPDATE:
|
2010-02-20 00:28:17 +03:00
|
|
|
$statement = $this->UpdateStatement();
|
|
|
|
break;
|
2009-06-02 22:05:26 +04:00
|
|
|
case Lexer::T_DELETE:
|
2010-02-20 00:28:17 +03:00
|
|
|
$statement = $this->DeleteStatement();
|
|
|
|
break;
|
2009-06-02 22:05:26 +04:00
|
|
|
default:
|
|
|
|
$this->syntaxError('SELECT, UPDATE or DELETE');
|
2009-07-17 02:03:35 +04:00
|
|
|
break;
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2010-01-01 01:48:51 +03:00
|
|
|
// Check for end of string
|
|
|
|
if ($this->_lexer->lookahead !== null) {
|
|
|
|
$this->syntaxError('end of string');
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
return $statement;
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SelectStatement ::= SelectClause FromClause [WhereClause] [GroupByClause] [HavingClause] [OrderByClause]
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\SelectStatement
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function SelectStatement()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$selectStatement = new AST\SelectStatement($this->SelectClause(), $this->FromClause());
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$selectStatement->whereClause = $this->_lexer->isNextToken(Lexer::T_WHERE)
|
2009-07-16 03:20:11 +04:00
|
|
|
? $this->WhereClause() : null;
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$selectStatement->groupByClause = $this->_lexer->isNextToken(Lexer::T_GROUP)
|
2009-07-16 03:20:11 +04:00
|
|
|
? $this->GroupByClause() : null;
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$selectStatement->havingClause = $this->_lexer->isNextToken(Lexer::T_HAVING)
|
2009-07-16 03:20:11 +04:00
|
|
|
? $this->HavingClause() : null;
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$selectStatement->orderByClause = $this->_lexer->isNextToken(Lexer::T_ORDER)
|
2009-07-16 03:20:11 +04:00
|
|
|
? $this->OrderByClause() : null;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
return $selectStatement;
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-21 09:22:22 +04:00
|
|
|
* UpdateStatement ::= UpdateClause [WhereClause]
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\UpdateStatement
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-21 09:22:22 +04:00
|
|
|
public function UpdateStatement()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-07-21 09:22:22 +04:00
|
|
|
$updateStatement = new AST\UpdateStatement($this->UpdateClause());
|
2010-03-05 19:35:00 +03:00
|
|
|
$updateStatement->whereClause = $this->_lexer->isNextToken(Lexer::T_WHERE)
|
2010-02-20 00:28:17 +03:00
|
|
|
? $this->WhereClause() : null;
|
2009-07-21 09:22:22 +04:00
|
|
|
|
|
|
|
return $updateStatement;
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-07-21 09:22:22 +04:00
|
|
|
/**
|
|
|
|
* DeleteStatement ::= DeleteClause [WhereClause]
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\DeleteStatement
|
2009-07-21 09:22:22 +04:00
|
|
|
*/
|
|
|
|
public function DeleteStatement()
|
|
|
|
{
|
|
|
|
$deleteStatement = new AST\DeleteStatement($this->DeleteClause());
|
2010-03-05 19:35:00 +03:00
|
|
|
$deleteStatement->whereClause = $this->_lexer->isNextToken(Lexer::T_WHERE)
|
2010-02-20 00:28:17 +03:00
|
|
|
? $this->WhereClause() : null;
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2009-07-21 09:22:22 +04:00
|
|
|
return $deleteStatement;
|
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
2009-07-21 09:22:22 +04:00
|
|
|
* IdentificationVariable ::= identifier
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return string
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-21 09:22:22 +04:00
|
|
|
public function IdentificationVariable()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-07-21 09:22:22 +04:00
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-12-27 06:26:15 +03:00
|
|
|
$identVariable = $this->_lexer->token['value'];
|
2010-02-20 00:28:17 +03:00
|
|
|
|
|
|
|
$this->_deferredIdentificationVariables[] = array(
|
2009-12-27 06:26:15 +03:00
|
|
|
'expression' => $identVariable,
|
|
|
|
'nestingLevel' => $this->_nestingLevel,
|
|
|
|
'token' => $this->_lexer->token,
|
|
|
|
);
|
|
|
|
|
|
|
|
return $identVariable;
|
2009-07-21 09:22:22 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-07-21 09:22:22 +04:00
|
|
|
/**
|
|
|
|
* AliasIdentificationVariable = identifier
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return string
|
2009-07-21 09:22:22 +04:00
|
|
|
*/
|
|
|
|
public function AliasIdentificationVariable()
|
|
|
|
{
|
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-12-27 06:26:15 +03:00
|
|
|
$aliasIdentVariable = $this->_lexer->token['value'];
|
2010-01-01 01:48:51 +03:00
|
|
|
$exists = isset($this->_queryComponents[$aliasIdentVariable]);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-01-01 01:48:51 +03:00
|
|
|
if ($exists) {
|
|
|
|
$this->semanticalError(
|
|
|
|
"'$aliasIdentVariable' is already defined.", $this->_lexer->token
|
|
|
|
);
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-12-27 06:26:15 +03:00
|
|
|
return $aliasIdentVariable;
|
2009-07-21 09:22:22 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-07-21 09:22:22 +04:00
|
|
|
/**
|
|
|
|
* AbstractSchemaName ::= identifier
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return string
|
2009-07-21 09:22:22 +04:00
|
|
|
*/
|
|
|
|
public function AbstractSchemaName()
|
|
|
|
{
|
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-08-08 17:01:03 +04:00
|
|
|
$schemaName = ltrim($this->_lexer->token['value'], '\\');
|
2010-03-03 04:30:00 +03:00
|
|
|
|
|
|
|
if (strrpos($schemaName, ':') !== false) {
|
|
|
|
list($namespaceAlias, $simpleClassName) = explode(':', $schemaName);
|
|
|
|
$schemaName = $this->_em->getConfiguration()->getEntityNamespace($namespaceAlias) . '\\' . $simpleClassName;
|
2010-02-26 21:11:53 +03:00
|
|
|
}
|
2010-03-03 04:30:00 +03:00
|
|
|
|
2010-01-01 01:48:51 +03:00
|
|
|
$exists = class_exists($schemaName, true);
|
2010-03-03 04:30:00 +03:00
|
|
|
|
2010-01-01 01:48:51 +03:00
|
|
|
if ( ! $exists) {
|
|
|
|
$this->semanticalError("Class '$schemaName' is not defined.", $this->_lexer->token);
|
|
|
|
}
|
2009-12-27 06:26:15 +03:00
|
|
|
|
|
|
|
return $schemaName;
|
2009-07-21 09:22:22 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2010-01-01 01:48:51 +03:00
|
|
|
/**
|
|
|
|
* AliasResultVariable ::= identifier
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function AliasResultVariable()
|
|
|
|
{
|
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-01-01 01:48:51 +03:00
|
|
|
$resultVariable = $this->_lexer->token['value'];
|
|
|
|
$exists = isset($this->_queryComponents[$resultVariable]);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-01-01 01:48:51 +03:00
|
|
|
if ($exists) {
|
|
|
|
$this->semanticalError(
|
|
|
|
"'$resultVariable' is already defined.", $this->_lexer->token
|
|
|
|
);
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-01-01 01:48:51 +03:00
|
|
|
return $resultVariable;
|
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-07-21 09:22:22 +04:00
|
|
|
/**
|
|
|
|
* ResultVariable ::= identifier
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return string
|
2009-07-21 09:22:22 +04:00
|
|
|
*/
|
|
|
|
public function ResultVariable()
|
|
|
|
{
|
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-12-27 06:26:15 +03:00
|
|
|
$resultVariable = $this->_lexer->token['value'];
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-12-27 06:26:15 +03:00
|
|
|
// Defer ResultVariable validation
|
2010-02-20 00:28:17 +03:00
|
|
|
$this->_deferredResultVariables[] = array(
|
|
|
|
'expression' => $resultVariable,
|
|
|
|
'nestingLevel' => $this->_nestingLevel,
|
|
|
|
'token' => $this->_lexer->token,
|
2009-12-27 06:26:15 +03:00
|
|
|
);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-12-27 06:26:15 +03:00
|
|
|
return $resultVariable;
|
2009-07-21 09:22:22 +04:00
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-21 09:22:22 +04:00
|
|
|
/**
|
2009-07-30 08:40:04 +04:00
|
|
|
* JoinAssociationPathExpression ::= IdentificationVariable "." (CollectionValuedAssociationField | SingleValuedAssociationField)
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\JoinAssociationPathExpression
|
2009-07-21 09:22:22 +04:00
|
|
|
*/
|
2009-07-30 08:40:04 +04:00
|
|
|
public function JoinAssociationPathExpression()
|
2009-07-21 09:22:22 +04:00
|
|
|
{
|
2009-08-05 23:00:16 +04:00
|
|
|
$token = $this->_lexer->lookahead;
|
2009-08-07 01:42:07 +04:00
|
|
|
$identVariable = $this->IdentificationVariable();
|
2010-07-23 08:55:33 +04:00
|
|
|
|
2011-02-05 13:42:10 +03:00
|
|
|
if (!isset($this->_queryComponents[$identVariable])) {
|
|
|
|
$this->semanticalError('Identification Variable ' . $identVariable .' used in join path expression but was not defined before.');
|
|
|
|
}
|
|
|
|
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_DOT);
|
2010-05-19 00:19:08 +04:00
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
2010-07-23 08:55:33 +04:00
|
|
|
|
2009-07-30 08:40:04 +04:00
|
|
|
$field = $this->_lexer->token['value'];
|
2010-02-20 21:27:05 +03:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
// Validate association field
|
|
|
|
$qComp = $this->_queryComponents[$identVariable];
|
|
|
|
$class = $qComp['metadata'];
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
if ( ! isset($class->associationMappings[$field])) {
|
|
|
|
$this->semanticalError('Class ' . $class->name . ' has no association named ' . $field);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new AST\JoinAssociationPathExpression($identVariable, $field);
|
2010-03-05 19:35:00 +03:00
|
|
|
}
|
2009-07-21 09:22:22 +04:00
|
|
|
|
|
|
|
/**
|
2010-03-05 19:35:00 +03:00
|
|
|
* Parses an arbitrary path expression and defers semantical validation
|
2009-07-30 08:40:04 +04:00
|
|
|
* based on expected types.
|
2009-07-21 09:22:22 +04:00
|
|
|
*
|
2010-07-23 08:55:33 +04:00
|
|
|
* PathExpression ::= IdentificationVariable "." identifier
|
2009-07-21 09:22:22 +04:00
|
|
|
*
|
2009-12-27 06:26:15 +03:00
|
|
|
* @param integer $expectedTypes
|
2009-08-02 03:40:11 +04:00
|
|
|
* @return \Doctrine\ORM\Query\AST\PathExpression
|
2009-07-21 09:22:22 +04:00
|
|
|
*/
|
2009-12-27 06:26:15 +03:00
|
|
|
public function PathExpression($expectedTypes)
|
2009-07-21 09:22:22 +04:00
|
|
|
{
|
2009-08-05 23:00:16 +04:00
|
|
|
$token = $this->_lexer->lookahead;
|
2009-08-07 01:42:07 +04:00
|
|
|
$identVariable = $this->IdentificationVariable();
|
2010-07-23 08:55:33 +04:00
|
|
|
$field = null;
|
2009-07-21 09:22:22 +04:00
|
|
|
|
2010-07-23 08:55:33 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_DOT)) {
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_DOT);
|
2009-07-21 09:22:22 +04:00
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-07-23 08:55:33 +04:00
|
|
|
$field = $this->_lexer->token['value'];
|
2010-04-14 07:04:44 +04:00
|
|
|
}
|
2010-07-23 08:55:33 +04:00
|
|
|
|
2009-07-30 08:40:04 +04:00
|
|
|
// Creating AST node
|
2010-07-23 08:55:33 +04:00
|
|
|
$pathExpr = new AST\PathExpression($expectedTypes, $identVariable, $field);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-30 08:40:04 +04:00
|
|
|
// Defer PathExpression validation if requested to be defered
|
2010-02-20 00:28:17 +03:00
|
|
|
$this->_deferredPathExpressions[] = array(
|
|
|
|
'expression' => $pathExpr,
|
|
|
|
'nestingLevel' => $this->_nestingLevel,
|
|
|
|
'token' => $this->_lexer->token,
|
2009-12-27 06:26:15 +03:00
|
|
|
);
|
|
|
|
|
2009-07-30 08:40:04 +04:00
|
|
|
return $pathExpr;
|
2009-07-21 09:22:22 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-07-21 09:22:22 +04:00
|
|
|
/**
|
|
|
|
* AssociationPathExpression ::= CollectionValuedPathExpression | SingleValuedAssociationPathExpression
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\PathExpression
|
2009-07-21 09:22:22 +04:00
|
|
|
*/
|
|
|
|
public function AssociationPathExpression()
|
|
|
|
{
|
2009-07-30 08:40:04 +04:00
|
|
|
return $this->PathExpression(
|
|
|
|
AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION |
|
|
|
|
AST\PathExpression::TYPE_COLLECTION_VALUED_ASSOCIATION
|
|
|
|
);
|
2009-07-21 09:22:22 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-07-21 09:22:22 +04:00
|
|
|
/**
|
|
|
|
* SingleValuedPathExpression ::= StateFieldPathExpression | SingleValuedAssociationPathExpression
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\PathExpression
|
2009-07-21 09:22:22 +04:00
|
|
|
*/
|
|
|
|
public function SingleValuedPathExpression()
|
|
|
|
{
|
2009-07-30 08:40:04 +04:00
|
|
|
return $this->PathExpression(
|
|
|
|
AST\PathExpression::TYPE_STATE_FIELD |
|
|
|
|
AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION
|
|
|
|
);
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-07-21 09:22:22 +04:00
|
|
|
/**
|
2010-07-23 08:55:33 +04:00
|
|
|
* StateFieldPathExpression ::= IdentificationVariable "." StateField
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\PathExpression
|
2009-07-21 09:22:22 +04:00
|
|
|
*/
|
|
|
|
public function StateFieldPathExpression()
|
|
|
|
{
|
2009-07-30 08:40:04 +04:00
|
|
|
return $this->PathExpression(AST\PathExpression::TYPE_STATE_FIELD);
|
2009-07-21 09:22:22 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
2010-07-23 08:55:33 +04:00
|
|
|
* SingleValuedAssociationPathExpression ::= IdentificationVariable "." SingleValuedAssociationField
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\PathExpression
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-21 09:22:22 +04:00
|
|
|
public function SingleValuedAssociationPathExpression()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-07-30 08:40:04 +04:00
|
|
|
return $this->PathExpression(AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION);
|
2009-07-21 09:22:22 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-07-21 09:22:22 +04:00
|
|
|
/**
|
2010-07-23 08:55:33 +04:00
|
|
|
* CollectionValuedPathExpression ::= IdentificationVariable "." CollectionValuedAssociationField
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\PathExpression
|
2009-07-21 09:22:22 +04:00
|
|
|
*/
|
|
|
|
public function CollectionValuedPathExpression()
|
|
|
|
{
|
2009-07-30 08:40:04 +04:00
|
|
|
return $this->PathExpression(AST\PathExpression::TYPE_COLLECTION_VALUED_ASSOCIATION);
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
/**
|
|
|
|
* SelectClause ::= "SELECT" ["DISTINCT"] SelectExpression {"," SelectExpression}
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\SelectClause
|
2009-07-22 07:46:05 +04:00
|
|
|
*/
|
|
|
|
public function SelectClause()
|
|
|
|
{
|
|
|
|
$isDistinct = false;
|
|
|
|
$this->match(Lexer::T_SELECT);
|
|
|
|
|
|
|
|
// Check for DISTINCT
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_DISTINCT)) {
|
|
|
|
$this->match(Lexer::T_DISTINCT);
|
|
|
|
$isDistinct = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process SelectExpressions (1..N)
|
|
|
|
$selectExpressions = array();
|
|
|
|
$selectExpressions[] = $this->SelectExpression();
|
|
|
|
|
2009-11-04 00:42:58 +03:00
|
|
|
while ($this->_lexer->isNextToken(Lexer::T_COMMA)) {
|
|
|
|
$this->match(Lexer::T_COMMA);
|
2009-07-22 07:46:05 +04:00
|
|
|
$selectExpressions[] = $this->SelectExpression();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new AST\SelectClause($selectExpressions, $isDistinct);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SimpleSelectClause ::= "SELECT" ["DISTINCT"] SimpleSelectExpression
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\SimpleSelectClause
|
2009-07-22 07:46:05 +04:00
|
|
|
*/
|
|
|
|
public function SimpleSelectClause()
|
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$isDistinct = false;
|
2009-07-22 07:46:05 +04:00
|
|
|
$this->match(Lexer::T_SELECT);
|
|
|
|
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_DISTINCT)) {
|
|
|
|
$this->match(Lexer::T_DISTINCT);
|
2009-08-07 01:42:07 +04:00
|
|
|
$isDistinct = true;
|
2009-07-22 07:46:05 +04:00
|
|
|
}
|
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
return new AST\SimpleSelectClause($this->SimpleSelectExpression(), $isDistinct);
|
2009-07-22 07:46:05 +04:00
|
|
|
}
|
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
2009-12-27 06:26:15 +03:00
|
|
|
* UpdateClause ::= "UPDATE" AbstractSchemaName ["AS"] AliasIdentificationVariable "SET" UpdateItem {"," UpdateItem}*
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\UpdateClause
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function UpdateClause()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
|
|
|
$this->match(Lexer::T_UPDATE);
|
2009-08-05 23:00:16 +04:00
|
|
|
$token = $this->_lexer->lookahead;
|
2009-06-14 21:34:28 +04:00
|
|
|
$abstractSchemaName = $this->AbstractSchemaName();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_AS)) {
|
|
|
|
$this->match(Lexer::T_AS);
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-12-27 06:26:15 +03:00
|
|
|
$aliasIdentificationVariable = $this->AliasIdentificationVariable();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-05 23:00:16 +04:00
|
|
|
$class = $this->_em->getClassMetadata($abstractSchemaName);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
// Building queryComponent
|
|
|
|
$queryComponent = array(
|
2009-08-05 23:00:16 +04:00
|
|
|
'metadata' => $class,
|
2009-08-05 07:56:21 +04:00
|
|
|
'parent' => null,
|
|
|
|
'relation' => null,
|
|
|
|
'map' => null,
|
|
|
|
'nestingLevel' => $this->_nestingLevel,
|
2009-08-05 23:00:16 +04:00
|
|
|
'token' => $token,
|
2009-06-02 22:05:26 +04:00
|
|
|
);
|
|
|
|
$this->_queryComponents[$aliasIdentificationVariable] = $queryComponent;
|
|
|
|
|
2009-08-05 23:00:16 +04:00
|
|
|
$this->match(Lexer::T_SET);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-05 23:00:16 +04:00
|
|
|
$updateItems = array();
|
|
|
|
$updateItems[] = $this->UpdateItem();
|
|
|
|
|
2009-11-04 00:42:58 +03:00
|
|
|
while ($this->_lexer->isNextToken(Lexer::T_COMMA)) {
|
|
|
|
$this->match(Lexer::T_COMMA);
|
2009-08-05 23:00:16 +04:00
|
|
|
$updateItems[] = $this->UpdateItem();
|
|
|
|
}
|
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
$updateClause = new AST\UpdateClause($abstractSchemaName, $updateItems);
|
2009-08-07 01:42:07 +04:00
|
|
|
$updateClause->aliasIdentificationVariable = $aliasIdentificationVariable;
|
2009-06-02 22:05:26 +04:00
|
|
|
|
|
|
|
return $updateClause;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-27 06:26:15 +03:00
|
|
|
* DeleteClause ::= "DELETE" ["FROM"] AbstractSchemaName ["AS"] AliasIdentificationVariable
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\DeleteClause
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function DeleteClause()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
|
|
|
$this->match(Lexer::T_DELETE);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_FROM)) {
|
|
|
|
$this->match(Lexer::T_FROM);
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-08-05 23:00:16 +04:00
|
|
|
$token = $this->_lexer->lookahead;
|
2009-06-14 21:34:28 +04:00
|
|
|
$deleteClause = new AST\DeleteClause($this->AbstractSchemaName());
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_AS)) {
|
|
|
|
$this->match(Lexer::T_AS);
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-12-27 06:26:15 +03:00
|
|
|
$aliasIdentificationVariable = $this->AliasIdentificationVariable();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$deleteClause->aliasIdentificationVariable = $aliasIdentificationVariable;
|
|
|
|
$class = $this->_em->getClassMetadata($deleteClause->abstractSchemaName);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-05 23:00:16 +04:00
|
|
|
// Building queryComponent
|
2009-06-02 22:05:26 +04:00
|
|
|
$queryComponent = array(
|
2009-08-05 23:00:16 +04:00
|
|
|
'metadata' => $class,
|
2009-08-05 07:56:21 +04:00
|
|
|
'parent' => null,
|
|
|
|
'relation' => null,
|
|
|
|
'map' => null,
|
|
|
|
'nestingLevel' => $this->_nestingLevel,
|
2009-08-05 23:00:16 +04:00
|
|
|
'token' => $token,
|
2009-06-02 22:05:26 +04:00
|
|
|
);
|
2009-08-05 23:00:16 +04:00
|
|
|
$this->_queryComponents[$aliasIdentificationVariable] = $queryComponent;
|
2009-06-02 22:05:26 +04:00
|
|
|
|
|
|
|
return $deleteClause;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-22 07:46:05 +04:00
|
|
|
* FromClause ::= "FROM" IdentificationVariableDeclaration {"," IdentificationVariableDeclaration}*
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\FromClause
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function FromClause()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-07-22 07:46:05 +04:00
|
|
|
$this->match(Lexer::T_FROM);
|
|
|
|
$identificationVariableDeclarations = array();
|
|
|
|
$identificationVariableDeclarations[] = $this->IdentificationVariableDeclaration();
|
|
|
|
|
2009-11-04 00:42:58 +03:00
|
|
|
while ($this->_lexer->isNextToken(Lexer::T_COMMA)) {
|
|
|
|
$this->match(Lexer::T_COMMA);
|
2009-07-22 07:46:05 +04:00
|
|
|
$identificationVariableDeclarations[] = $this->IdentificationVariableDeclaration();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
return new AST\FromClause($identificationVariableDeclarations);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SubselectFromClause ::= "FROM" SubselectIdentificationVariableDeclaration {"," SubselectIdentificationVariableDeclaration}*
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\SubselectFromClause
|
2009-07-22 07:46:05 +04:00
|
|
|
*/
|
|
|
|
public function SubselectFromClause()
|
|
|
|
{
|
|
|
|
$this->match(Lexer::T_FROM);
|
|
|
|
$identificationVariables = array();
|
|
|
|
$identificationVariables[] = $this->SubselectIdentificationVariableDeclaration();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-11-04 00:42:58 +03:00
|
|
|
while ($this->_lexer->isNextToken(Lexer::T_COMMA)) {
|
|
|
|
$this->match(Lexer::T_COMMA);
|
2009-07-22 07:46:05 +04:00
|
|
|
$identificationVariables[] = $this->SubselectIdentificationVariableDeclaration();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
return new AST\SubselectFromClause($identificationVariables);
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-22 07:46:05 +04:00
|
|
|
* WhereClause ::= "WHERE" ConditionalExpression
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\WhereClause
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function WhereClause()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-07-22 07:46:05 +04:00
|
|
|
$this->match(Lexer::T_WHERE);
|
|
|
|
|
|
|
|
return new AST\WhereClause($this->ConditionalExpression());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HavingClause ::= "HAVING" ConditionalExpression
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\HavingClause
|
2009-07-22 07:46:05 +04:00
|
|
|
*/
|
|
|
|
public function HavingClause()
|
|
|
|
{
|
|
|
|
$this->match(Lexer::T_HAVING);
|
|
|
|
|
|
|
|
return new AST\HavingClause($this->ConditionalExpression());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GroupByClause ::= "GROUP" "BY" GroupByItem {"," GroupByItem}*
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\GroupByClause
|
2009-07-22 07:46:05 +04:00
|
|
|
*/
|
|
|
|
public function GroupByClause()
|
|
|
|
{
|
|
|
|
$this->match(Lexer::T_GROUP);
|
|
|
|
$this->match(Lexer::T_BY);
|
|
|
|
|
|
|
|
$groupByItems = array($this->GroupByItem());
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2009-11-04 00:42:58 +03:00
|
|
|
while ($this->_lexer->isNextToken(Lexer::T_COMMA)) {
|
|
|
|
$this->match(Lexer::T_COMMA);
|
2009-07-22 07:46:05 +04:00
|
|
|
$groupByItems[] = $this->GroupByItem();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
return new AST\GroupByClause($groupByItems);
|
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
/**
|
|
|
|
* OrderByClause ::= "ORDER" "BY" OrderByItem {"," OrderByItem}*
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\OrderByClause
|
2009-07-22 07:46:05 +04:00
|
|
|
*/
|
|
|
|
public function OrderByClause()
|
|
|
|
{
|
|
|
|
$this->match(Lexer::T_ORDER);
|
|
|
|
$this->match(Lexer::T_BY);
|
|
|
|
|
|
|
|
$orderByItems = array();
|
|
|
|
$orderByItems[] = $this->OrderByItem();
|
|
|
|
|
2009-11-04 00:42:58 +03:00
|
|
|
while ($this->_lexer->isNextToken(Lexer::T_COMMA)) {
|
|
|
|
$this->match(Lexer::T_COMMA);
|
2009-07-22 07:46:05 +04:00
|
|
|
$orderByItems[] = $this->OrderByItem();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new AST\OrderByClause($orderByItems);
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-22 07:46:05 +04:00
|
|
|
* Subselect ::= SimpleSelectClause SubselectFromClause [WhereClause] [GroupByClause] [HavingClause] [OrderByClause]
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\Subselect
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function Subselect()
|
|
|
|
{
|
2009-08-04 23:48:40 +04:00
|
|
|
// Increase query nesting level
|
|
|
|
$this->_nestingLevel++;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
$subselect = new AST\Subselect($this->SimpleSelectClause(), $this->SubselectFromClause());
|
2010-03-05 19:35:00 +03:00
|
|
|
|
|
|
|
$subselect->whereClause = $this->_lexer->isNextToken(Lexer::T_WHERE)
|
2009-08-07 01:42:07 +04:00
|
|
|
? $this->WhereClause() : null;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
|
|
|
$subselect->groupByClause = $this->_lexer->isNextToken(Lexer::T_GROUP)
|
2009-08-07 01:42:07 +04:00
|
|
|
? $this->GroupByClause() : null;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
|
|
|
$subselect->havingClause = $this->_lexer->isNextToken(Lexer::T_HAVING)
|
2009-08-07 01:42:07 +04:00
|
|
|
? $this->HavingClause() : null;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
|
|
|
$subselect->orderByClause = $this->_lexer->isNextToken(Lexer::T_ORDER)
|
2009-08-07 01:42:07 +04:00
|
|
|
? $this->OrderByClause() : null;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-04 23:48:40 +04:00
|
|
|
// Decrease query nesting level
|
|
|
|
$this->_nestingLevel--;
|
2009-07-22 07:46:05 +04:00
|
|
|
|
|
|
|
return $subselect;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-30 08:30:02 +04:00
|
|
|
* UpdateItem ::= SingleValuedPathExpression "=" NewValue
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\UpdateItem
|
2009-07-22 07:46:05 +04:00
|
|
|
*/
|
|
|
|
public function UpdateItem()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2010-07-30 08:30:02 +04:00
|
|
|
$pathExpr = $this->SingleValuedPathExpression();
|
2010-07-23 08:55:33 +04:00
|
|
|
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_EQUALS);
|
2010-07-23 08:55:33 +04:00
|
|
|
|
2010-07-08 19:30:39 +04:00
|
|
|
$updateItem = new AST\UpdateItem($pathExpr, $this->NewValue());
|
2009-07-22 07:46:05 +04:00
|
|
|
|
|
|
|
return $updateItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GroupByItem ::= IdentificationVariable | SingleValuedPathExpression
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return string | \Doctrine\ORM\Query\AST\PathExpression
|
2009-07-22 07:46:05 +04:00
|
|
|
*/
|
|
|
|
public function GroupByItem()
|
|
|
|
{
|
2009-07-31 08:03:01 +04:00
|
|
|
// We need to check if we are in a IdentificationVariable or SingleValuedPathExpression
|
|
|
|
$glimpse = $this->_lexer->glimpse();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-07-23 08:55:33 +04:00
|
|
|
if ($glimpse['type'] != Lexer::T_DOT) {
|
2009-08-05 23:00:16 +04:00
|
|
|
$token = $this->_lexer->lookahead;
|
2009-08-07 01:42:07 +04:00
|
|
|
$identVariable = $this->IdentificationVariable();
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2011-03-20 16:07:33 +03:00
|
|
|
if (!isset($this->_queryComponents[$identVariable])) {
|
|
|
|
$this->semanticalError('Cannot group by undefined identification variable.');
|
|
|
|
}
|
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
return $identVariable;
|
2009-07-31 08:03:01 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
return $this->SingleValuedPathExpression();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-08-14 07:46:02 +04:00
|
|
|
* OrderByItem ::= (ResultVariable | SingleValuedPathExpression) ["ASC" | "DESC"]
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\OrderByItem
|
2009-07-22 07:46:05 +04:00
|
|
|
*/
|
|
|
|
public function OrderByItem()
|
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$type = 'ASC';
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
// We need to check if we are in a ResultVariable or StateFieldPathExpression
|
|
|
|
$glimpse = $this->_lexer->glimpse();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-07-23 08:55:33 +04:00
|
|
|
if ($glimpse['type'] != Lexer::T_DOT) {
|
2009-08-05 23:00:16 +04:00
|
|
|
$token = $this->_lexer->lookahead;
|
2009-09-11 23:50:48 +04:00
|
|
|
$expr = $this->ResultVariable();
|
2009-06-02 22:05:26 +04:00
|
|
|
} else {
|
2011-08-14 07:46:02 +04:00
|
|
|
$expr = $this->SingleValuedPathExpression();
|
2009-07-22 07:46:05 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
$item = new AST\OrderByItem($expr);
|
2009-07-16 07:22:08 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_ASC)) {
|
|
|
|
$this->match(Lexer::T_ASC);
|
2009-08-07 01:42:07 +04:00
|
|
|
} else if ($this->_lexer->isNextToken(Lexer::T_DESC)) {
|
2009-07-22 07:46:05 +04:00
|
|
|
$this->match(Lexer::T_DESC);
|
2009-08-07 01:42:07 +04:00
|
|
|
$type = 'DESC';
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$item->type = $type;
|
2009-07-22 07:46:05 +04:00
|
|
|
return $item;
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
/**
|
|
|
|
* NewValue ::= SimpleArithmeticExpression | StringPrimary | DatetimePrimary | BooleanPrimary |
|
|
|
|
* EnumPrimary | SimpleEntityExpression | "NULL"
|
|
|
|
*
|
|
|
|
* NOTE: Since it is not possible to correctly recognize individual types, here is the full
|
|
|
|
* grammar that needs to be supported:
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-07-22 07:46:05 +04:00
|
|
|
* NewValue ::= SimpleArithmeticExpression | "NULL"
|
|
|
|
*
|
|
|
|
* SimpleArithmeticExpression covers all *Primary grammar rules and also SimplEntityExpression
|
|
|
|
*/
|
|
|
|
public function NewValue()
|
|
|
|
{
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_NULL)) {
|
|
|
|
$this->match(Lexer::T_NULL);
|
|
|
|
return null;
|
|
|
|
} else if ($this->_lexer->isNextToken(Lexer::T_INPUT_PARAMETER)) {
|
|
|
|
$this->match(Lexer::T_INPUT_PARAMETER);
|
|
|
|
return new AST\InputParameter($this->_lexer->token['value']);
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
return $this->SimpleArithmeticExpression();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IdentificationVariableDeclaration ::= RangeVariableDeclaration [IndexBy] {JoinVariableDeclaration}*
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\IdentificationVariableDeclaration
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function IdentificationVariableDeclaration()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-06-14 21:34:28 +04:00
|
|
|
$rangeVariableDeclaration = $this->RangeVariableDeclaration();
|
|
|
|
$indexBy = $this->_lexer->isNextToken(Lexer::T_INDEX) ? $this->IndexBy() : null;
|
2009-06-02 22:05:26 +04:00
|
|
|
$joinVariableDeclarations = array();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
while (
|
2009-07-16 03:20:11 +04:00
|
|
|
$this->_lexer->isNextToken(Lexer::T_LEFT) ||
|
|
|
|
$this->_lexer->isNextToken(Lexer::T_INNER) ||
|
|
|
|
$this->_lexer->isNextToken(Lexer::T_JOIN)
|
2009-06-02 22:05:26 +04:00
|
|
|
) {
|
2009-06-14 21:34:28 +04:00
|
|
|
$joinVariableDeclarations[] = $this->JoinVariableDeclaration();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return new AST\IdentificationVariableDeclaration(
|
2009-07-16 03:20:11 +04:00
|
|
|
$rangeVariableDeclaration, $indexBy, $joinVariableDeclarations
|
2009-06-02 22:05:26 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
/**
|
|
|
|
* SubselectIdentificationVariableDeclaration ::= IdentificationVariableDeclaration | (AssociationPathExpression ["AS"] AliasIdentificationVariable)
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\SubselectIdentificationVariableDeclaration |
|
|
|
|
* \Doctrine\ORM\Query\AST\IdentificationVariableDeclaration
|
2009-07-22 07:46:05 +04:00
|
|
|
*/
|
|
|
|
public function SubselectIdentificationVariableDeclaration()
|
|
|
|
{
|
2010-07-23 08:55:33 +04:00
|
|
|
$glimpse = $this->_lexer->glimpse();
|
2009-07-22 07:46:05 +04:00
|
|
|
|
2010-07-23 08:55:33 +04:00
|
|
|
/* NOT YET IMPLEMENTED!
|
|
|
|
|
|
|
|
if ($glimpse['type'] == Lexer::T_DOT) {
|
2009-08-07 01:42:07 +04:00
|
|
|
$subselectIdVarDecl = new AST\SubselectIdentificationVariableDeclaration();
|
|
|
|
$subselectIdVarDecl->associationPathExpression = $this->AssociationPathExpression();
|
2009-07-22 07:46:05 +04:00
|
|
|
$this->match(Lexer::T_AS);
|
2009-08-07 01:42:07 +04:00
|
|
|
$subselectIdVarDecl->aliasIdentificationVariable = $this->AliasIdentificationVariable();
|
2009-07-22 07:46:05 +04:00
|
|
|
|
2009-07-31 08:03:01 +04:00
|
|
|
return $subselectIdVarDecl;
|
2009-07-22 07:46:05 +04:00
|
|
|
}
|
2010-07-23 08:55:33 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
|
|
|
|
return $this->IdentificationVariableDeclaration();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* JoinVariableDeclaration ::= Join [IndexBy]
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\JoinVariableDeclaration
|
2009-07-22 07:46:05 +04:00
|
|
|
*/
|
|
|
|
public function JoinVariableDeclaration()
|
|
|
|
{
|
|
|
|
$join = $this->Join();
|
|
|
|
$indexBy = $this->_lexer->isNextToken(Lexer::T_INDEX)
|
2010-02-20 00:28:17 +03:00
|
|
|
? $this->IndexBy() : null;
|
2009-07-22 07:46:05 +04:00
|
|
|
|
|
|
|
return new AST\JoinVariableDeclaration($join, $indexBy);
|
|
|
|
}
|
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
|
|
|
* RangeVariableDeclaration ::= AbstractSchemaName ["AS"] AliasIdentificationVariable
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
2010-02-20 00:28:17 +03:00
|
|
|
* @return Doctrine\ORM\Query\AST\RangeVariableDeclaration
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function RangeVariableDeclaration()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-06-14 21:34:28 +04:00
|
|
|
$abstractSchemaName = $this->AbstractSchemaName();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_AS)) {
|
|
|
|
$this->match(Lexer::T_AS);
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-08-05 23:00:16 +04:00
|
|
|
$token = $this->_lexer->lookahead;
|
2009-06-14 21:34:28 +04:00
|
|
|
$aliasIdentificationVariable = $this->AliasIdentificationVariable();
|
2009-06-02 22:05:26 +04:00
|
|
|
$classMetadata = $this->_em->getClassMetadata($abstractSchemaName);
|
|
|
|
|
|
|
|
// Building queryComponent
|
|
|
|
$queryComponent = array(
|
2009-08-05 07:56:21 +04:00
|
|
|
'metadata' => $classMetadata,
|
|
|
|
'parent' => null,
|
|
|
|
'relation' => null,
|
|
|
|
'map' => null,
|
|
|
|
'nestingLevel' => $this->_nestingLevel,
|
2010-02-20 00:28:17 +03:00
|
|
|
'token' => $token
|
2009-07-21 09:22:22 +04:00
|
|
|
);
|
|
|
|
$this->_queryComponents[$aliasIdentificationVariable] = $queryComponent;
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-08-13 06:17:27 +04:00
|
|
|
return new AST\RangeVariableDeclaration($abstractSchemaName, $aliasIdentificationVariable);
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
/**
|
|
|
|
* PartialObjectExpression ::= "PARTIAL" IdentificationVariable "." PartialFieldSet
|
|
|
|
* PartialFieldSet ::= "{" SimpleStateField {"," SimpleStateField}* "}"
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2010-02-20 00:28:17 +03:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function PartialObjectExpression()
|
|
|
|
{
|
2010-03-05 19:35:00 +03:00
|
|
|
$this->match(Lexer::T_PARTIAL);
|
2010-02-20 00:28:17 +03:00
|
|
|
|
|
|
|
$partialFieldSet = array();
|
|
|
|
|
|
|
|
$identificationVariable = $this->IdentificationVariable();
|
|
|
|
$this->match(Lexer::T_DOT);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
$this->match(Lexer::T_OPEN_CURLY_BRACE);
|
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
|
|
|
$partialFieldSet[] = $this->_lexer->token['value'];
|
2010-07-23 08:55:33 +04:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
while ($this->_lexer->isNextToken(Lexer::T_COMMA)) {
|
|
|
|
$this->match(Lexer::T_COMMA);
|
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
|
|
|
$partialFieldSet[] = $this->_lexer->token['value'];
|
|
|
|
}
|
2010-07-23 08:55:33 +04:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
$this->match(Lexer::T_CLOSE_CURLY_BRACE);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
$partialObjectExpression = new AST\PartialObjectExpression($identificationVariable, $partialFieldSet);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
// Defer PartialObjectExpression validation
|
|
|
|
$this->_deferredPartialObjectExpressions[] = array(
|
|
|
|
'expression' => $partialObjectExpression,
|
|
|
|
'nestingLevel' => $this->_nestingLevel,
|
|
|
|
'token' => $this->_lexer->token,
|
|
|
|
);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
return $partialObjectExpression;
|
|
|
|
}
|
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
2009-07-30 08:40:04 +04:00
|
|
|
* Join ::= ["LEFT" ["OUTER"] | "INNER"] "JOIN" JoinAssociationPathExpression
|
2010-02-20 00:28:17 +03:00
|
|
|
* ["AS"] AliasIdentificationVariable ["WITH" ConditionalExpression]
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
2010-02-20 00:28:17 +03:00
|
|
|
* @return Doctrine\ORM\Query\AST\Join
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function Join()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
|
|
|
// Check Join type
|
|
|
|
$joinType = AST\Join::JOIN_TYPE_INNER;
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_LEFT)) {
|
|
|
|
$this->match(Lexer::T_LEFT);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
// Possible LEFT OUTER join
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_OUTER)) {
|
|
|
|
$this->match(Lexer::T_OUTER);
|
|
|
|
$joinType = AST\Join::JOIN_TYPE_LEFTOUTER;
|
|
|
|
} else {
|
|
|
|
$joinType = AST\Join::JOIN_TYPE_LEFT;
|
|
|
|
}
|
|
|
|
} else if ($this->_lexer->isNextToken(Lexer::T_INNER)) {
|
|
|
|
$this->match(Lexer::T_INNER);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->match(Lexer::T_JOIN);
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-07-30 08:40:04 +04:00
|
|
|
$joinPathExpression = $this->JoinAssociationPathExpression();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_AS)) {
|
|
|
|
$this->match(Lexer::T_AS);
|
|
|
|
}
|
|
|
|
|
2009-08-05 23:00:16 +04:00
|
|
|
$token = $this->_lexer->lookahead;
|
2009-06-14 21:34:28 +04:00
|
|
|
$aliasIdentificationVariable = $this->AliasIdentificationVariable();
|
2009-06-02 22:05:26 +04:00
|
|
|
|
|
|
|
// Verify that the association exists.
|
2009-08-07 01:42:07 +04:00
|
|
|
$parentClass = $this->_queryComponents[$joinPathExpression->identificationVariable]['metadata'];
|
|
|
|
$assocField = $joinPathExpression->associationField;
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
if ( ! $parentClass->hasAssociation($assocField)) {
|
2009-07-16 03:20:11 +04:00
|
|
|
$this->semanticalError(
|
|
|
|
"Class " . $parentClass->name . " has no association named '$assocField'."
|
|
|
|
);
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
$targetClassName = $parentClass->associationMappings[$assocField]['targetEntity'];
|
2009-06-02 22:05:26 +04:00
|
|
|
|
|
|
|
// Building queryComponent
|
|
|
|
$joinQueryComponent = array(
|
2009-08-05 07:56:21 +04:00
|
|
|
'metadata' => $this->_em->getClassMetadata($targetClassName),
|
2009-08-07 01:42:07 +04:00
|
|
|
'parent' => $joinPathExpression->identificationVariable,
|
2009-08-05 07:56:21 +04:00
|
|
|
'relation' => $parentClass->getAssociationMapping($assocField),
|
|
|
|
'map' => null,
|
|
|
|
'nestingLevel' => $this->_nestingLevel,
|
2010-02-20 00:28:17 +03:00
|
|
|
'token' => $token
|
2009-06-02 22:05:26 +04:00
|
|
|
);
|
|
|
|
$this->_queryComponents[$aliasIdentificationVariable] = $joinQueryComponent;
|
|
|
|
|
|
|
|
// Create AST node
|
|
|
|
$join = new AST\Join($joinType, $joinPathExpression, $aliasIdentificationVariable);
|
|
|
|
|
|
|
|
// Check for ad-hoc Join conditions
|
2010-02-20 00:28:17 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_WITH)) {
|
|
|
|
$this->match(Lexer::T_WITH);
|
2009-08-07 01:42:07 +04:00
|
|
|
$join->conditionalExpression = $this->ConditionalExpression();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $join;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-23 08:55:33 +04:00
|
|
|
* IndexBy ::= "INDEX" "BY" StateFieldPathExpression
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
2010-02-20 00:28:17 +03:00
|
|
|
* @return Doctrine\ORM\Query\AST\IndexBy
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function IndexBy()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
|
|
|
$this->match(Lexer::T_INDEX);
|
|
|
|
$this->match(Lexer::T_BY);
|
2010-07-23 08:55:33 +04:00
|
|
|
$pathExpr = $this->StateFieldPathExpression();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
// Add the INDEX BY info to the query component
|
2010-07-23 08:55:33 +04:00
|
|
|
$this->_queryComponents[$pathExpr->identificationVariable]['map'] = $pathExpr->field;
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-07-23 08:55:33 +04:00
|
|
|
return new AST\IndexBy($pathExpr);
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
/**
|
|
|
|
* ScalarExpression ::= SimpleArithmeticExpression | StringPrimary | DateTimePrimary |
|
|
|
|
* StateFieldPathExpression | BooleanPrimary | CaseExpression |
|
2011-08-08 09:09:25 +04:00
|
|
|
* InstanceOfExpression
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2010-02-20 00:28:17 +03:00
|
|
|
* @return mixed One of the possible expressions or subexpressions.
|
|
|
|
*/
|
|
|
|
public function ScalarExpression()
|
|
|
|
{
|
|
|
|
$lookahead = $this->_lexer->lookahead['type'];
|
|
|
|
if ($lookahead === Lexer::T_IDENTIFIER) {
|
|
|
|
$this->_lexer->peek(); // lookahead => '.'
|
|
|
|
$this->_lexer->peek(); // lookahead => token after '.'
|
|
|
|
$peek = $this->_lexer->peek(); // lookahead => token after the token after the '.'
|
|
|
|
$this->_lexer->resetPeek();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-08-12 06:12:44 +04:00
|
|
|
if ($this->_isMathOperator($peek)) {
|
2010-02-20 00:28:17 +03:00
|
|
|
return $this->SimpleArithmeticExpression();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->StateFieldPathExpression();
|
|
|
|
} else if ($lookahead == Lexer::T_INTEGER || $lookahead == Lexer::T_FLOAT) {
|
|
|
|
return $this->SimpleArithmeticExpression();
|
2011-06-17 23:16:22 +04:00
|
|
|
} else if ($lookahead == Lexer::T_CASE || $lookahead == Lexer::T_COALESCE || $lookahead == Lexer::T_NULLIF) {
|
|
|
|
// Since NULLIF and COALESCE can be identified as a function,
|
|
|
|
// we need to check if before check for FunctionDeclaration
|
|
|
|
return $this->CaseExpression();
|
2011-03-27 16:04:53 +04:00
|
|
|
} else if ($this->_isFunction() || $this->_isAggregateFunction($this->_lexer->lookahead['type'])) {
|
2010-08-12 06:12:44 +04:00
|
|
|
// We may be in an ArithmeticExpression (find the matching ")" and inspect for Math operator)
|
|
|
|
$this->_lexer->peek(); // "("
|
|
|
|
$peek = $this->_peekBeyondClosingParenthesis();
|
|
|
|
|
|
|
|
if ($this->_isMathOperator($peek)) {
|
|
|
|
return $this->SimpleArithmeticExpression();
|
|
|
|
}
|
2011-03-27 16:04:53 +04:00
|
|
|
|
|
|
|
if ($this->_isAggregateFunction($this->_lexer->lookahead['type'])) {
|
|
|
|
return $this->AggregateExpression();
|
|
|
|
}
|
2011-08-08 09:09:25 +04:00
|
|
|
|
|
|
|
return $this->FunctionDeclaration();
|
2010-02-20 00:28:17 +03:00
|
|
|
} else if ($lookahead == Lexer::T_STRING) {
|
|
|
|
return $this->StringPrimary();
|
|
|
|
} else if ($lookahead == Lexer::T_INPUT_PARAMETER) {
|
|
|
|
return $this->InputParameter();
|
|
|
|
} else if ($lookahead == Lexer::T_TRUE || $lookahead == Lexer::T_FALSE) {
|
|
|
|
$this->match($lookahead);
|
|
|
|
return new AST\Literal(AST\Literal::BOOLEAN, $this->_lexer->token['value']);
|
|
|
|
} else {
|
|
|
|
$this->syntaxError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-08 09:09:25 +04:00
|
|
|
/**
|
|
|
|
* CaseExpression ::= GeneralCaseExpression | SimpleCaseExpression | CoalesceExpression | NullifExpression
|
|
|
|
* GeneralCaseExpression ::= "CASE" WhenClause {WhenClause}* "ELSE" ScalarExpression "END"
|
|
|
|
* WhenClause ::= "WHEN" ConditionalExpression "THEN" ScalarExpression
|
|
|
|
* SimpleCaseExpression ::= "CASE" CaseOperand SimpleWhenClause {SimpleWhenClause}* "ELSE" ScalarExpression "END"
|
|
|
|
* CaseOperand ::= StateFieldPathExpression | TypeDiscriminator
|
|
|
|
* SimpleWhenClause ::= "WHEN" ScalarExpression "THEN" ScalarExpression
|
|
|
|
* CoalesceExpression ::= "COALESCE" "(" ScalarExpression {"," ScalarExpression}* ")"
|
|
|
|
* NullifExpression ::= "NULLIF" "(" ScalarExpression "," ScalarExpression ")"
|
|
|
|
*
|
|
|
|
* @return mixed One of the possible expressions or subexpressions.
|
|
|
|
*/
|
2010-02-20 00:28:17 +03:00
|
|
|
public function CaseExpression()
|
|
|
|
{
|
2011-06-17 23:16:22 +04:00
|
|
|
$lookahead = $this->_lexer->lookahead['type'];
|
|
|
|
|
|
|
|
switch ($lookahead) {
|
|
|
|
case Lexer::T_NULLIF:
|
|
|
|
return $this->NullIfExpression();
|
|
|
|
|
|
|
|
case Lexer::T_COALESCE:
|
|
|
|
return $this->CoalesceExpression();
|
2011-08-08 09:09:25 +04:00
|
|
|
|
|
|
|
case Lexer::T_CASE:
|
2011-09-22 00:30:45 +04:00
|
|
|
$this->_lexer->resetPeek();
|
2011-08-08 09:09:25 +04:00
|
|
|
$peek = $this->_lexer->peek();
|
|
|
|
|
|
|
|
return ($peek['type'] === Lexer::T_WHEN)
|
|
|
|
? $this->GeneralCaseExpression()
|
|
|
|
: $this->SimpleCaseExpression();
|
2011-06-17 23:16:22 +04:00
|
|
|
|
|
|
|
default:
|
2011-08-08 09:09:25 +04:00
|
|
|
// Do nothing
|
|
|
|
break;
|
2011-06-17 23:16:22 +04:00
|
|
|
}
|
2011-08-08 09:09:25 +04:00
|
|
|
|
|
|
|
$this->syntaxError();
|
2011-06-17 23:16:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CoalesceExpression ::= "COALESCE" "(" ScalarExpression {"," ScalarExpression}* ")"
|
|
|
|
*
|
|
|
|
* @return Doctrine\ORM\Query\AST\CoalesceExpression
|
|
|
|
*/
|
|
|
|
public function CoalesceExpression()
|
|
|
|
{
|
|
|
|
$this->match(Lexer::T_COALESCE);
|
|
|
|
$this->match(Lexer::T_OPEN_PARENTHESIS);
|
|
|
|
|
|
|
|
// Process ScalarExpressions (1..N)
|
|
|
|
$scalarExpressions = array();
|
|
|
|
$scalarExpressions[] = $this->ScalarExpression();
|
|
|
|
|
|
|
|
while ($this->_lexer->isNextToken(Lexer::T_COMMA)) {
|
|
|
|
$this->match(Lexer::T_COMMA);
|
|
|
|
$scalarExpressions[] = $this->ScalarExpression();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->match(Lexer::T_CLOSE_PARENTHESIS);
|
|
|
|
|
|
|
|
return new AST\CoalesceExpression($scalarExpressions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NullIfExpression ::= "NULLIF" "(" ScalarExpression "," ScalarExpression ")"
|
|
|
|
*
|
2011-08-08 09:09:25 +04:00
|
|
|
* @return Doctrine\ORM\Query\AST\NullIfExpression
|
2011-06-17 23:16:22 +04:00
|
|
|
*/
|
|
|
|
public function NullIfExpression()
|
|
|
|
{
|
|
|
|
$this->match(Lexer::T_NULLIF);
|
|
|
|
$this->match(Lexer::T_OPEN_PARENTHESIS);
|
|
|
|
|
|
|
|
$firstExpression = $this->ScalarExpression();
|
|
|
|
$this->match(Lexer::T_COMMA);
|
|
|
|
$secondExpression = $this->ScalarExpression();
|
|
|
|
|
|
|
|
$this->match(Lexer::T_CLOSE_PARENTHESIS);
|
|
|
|
|
|
|
|
return new AST\NullIfExpression($firstExpression, $secondExpression);
|
2010-02-20 00:28:17 +03:00
|
|
|
}
|
2011-08-08 09:09:25 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GeneralCaseExpression ::= "CASE" WhenClause {WhenClause}* "ELSE" ScalarExpression "END"
|
|
|
|
*
|
|
|
|
* @return Doctrine\ORM\Query\AST\GeneralExpression
|
|
|
|
*/
|
|
|
|
public function GeneralCaseExpression()
|
|
|
|
{
|
|
|
|
$this->match(Lexer::T_CASE);
|
|
|
|
|
|
|
|
// Process WhenClause (1..N)
|
|
|
|
$whenClauses = array();
|
|
|
|
|
|
|
|
do {
|
|
|
|
$whenClauses[] = $this->WhenClause();
|
|
|
|
} while ($this->_lexer->isNextToken(Lexer::T_WHEN));
|
|
|
|
|
|
|
|
$this->match(Lexer::T_ELSE);
|
|
|
|
$scalarExpression = $this->ScalarExpression();
|
|
|
|
$this->match(Lexer::T_END);
|
|
|
|
|
|
|
|
return new AST\GeneralCaseExpression($whenClauses, $scalarExpression);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SimpleCaseExpression ::= "CASE" CaseOperand SimpleWhenClause {SimpleWhenClause}* "ELSE" ScalarExpression "END"
|
|
|
|
* CaseOperand ::= StateFieldPathExpression | TypeDiscriminator
|
|
|
|
*/
|
|
|
|
public function SimpleCaseExpression()
|
|
|
|
{
|
|
|
|
$this->match(Lexer::T_CASE);
|
|
|
|
$caseOperand = $this->StateFieldPathExpression();
|
|
|
|
|
|
|
|
// Process SimpleWhenClause (1..N)
|
|
|
|
$simpleWhenClauses = array();
|
|
|
|
|
|
|
|
do {
|
|
|
|
$simpleWhenClauses[] = $this->SimpleWhenClause();
|
|
|
|
} while ($this->_lexer->isNextToken(Lexer::T_WHEN));
|
|
|
|
|
|
|
|
$this->match(Lexer::T_ELSE);
|
|
|
|
$scalarExpression = $this->ScalarExpression();
|
|
|
|
$this->match(Lexer::T_END);
|
|
|
|
|
|
|
|
return new AST\SimpleCaseExpression($caseOperand, $simpleWhenClauses, $scalarExpression);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WhenClause ::= "WHEN" ConditionalExpression "THEN" ScalarExpression
|
|
|
|
*
|
|
|
|
* @return Doctrine\ORM\Query\AST\WhenExpression
|
|
|
|
*/
|
|
|
|
public function WhenClause()
|
|
|
|
{
|
|
|
|
$this->match(Lexer::T_WHEN);
|
|
|
|
$conditionalExpression = $this->ConditionalExpression();
|
|
|
|
$this->match(Lexer::T_THEN);
|
|
|
|
|
|
|
|
return new AST\WhenClause($conditionalExpression, $this->ScalarExpression());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SimpleWhenClause ::= "WHEN" ScalarExpression "THEN" ScalarExpression
|
|
|
|
*
|
|
|
|
* @return Doctrine\ORM\Query\AST\SimpleWhenExpression
|
|
|
|
*/
|
|
|
|
public function SimpleWhenClause()
|
|
|
|
{
|
|
|
|
$this->match(Lexer::T_WHEN);
|
|
|
|
$conditionalExpression = $this->ScalarExpression();
|
|
|
|
$this->match(Lexer::T_THEN);
|
|
|
|
|
|
|
|
return new AST\SimpleWhenClause($conditionalExpression, $this->ScalarExpression());
|
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-07-16 08:08:14 +04:00
|
|
|
/**
|
2009-07-22 07:46:05 +04:00
|
|
|
* SelectExpression ::=
|
|
|
|
* IdentificationVariable | StateFieldPathExpression |
|
2010-02-20 00:28:17 +03:00
|
|
|
* (AggregateExpression | "(" Subselect ")" | ScalarExpression) [["AS"] AliasResultVariable]
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
2010-02-20 00:28:17 +03:00
|
|
|
* @return Doctrine\ORM\Query\AST\SelectExpression
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function SelectExpression()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-07-22 07:46:05 +04:00
|
|
|
$expression = null;
|
2010-11-11 23:12:09 +03:00
|
|
|
$identVariable = null;
|
2009-07-22 07:46:05 +04:00
|
|
|
$fieldAliasIdentificationVariable = null;
|
|
|
|
$peek = $this->_lexer->glimpse();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
$supportsAlias = true;
|
2010-07-15 17:52:42 +04:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
if ($peek['value'] != '(' && $this->_lexer->lookahead['type'] === Lexer::T_IDENTIFIER) {
|
|
|
|
if ($peek['value'] == '.') {
|
|
|
|
// ScalarExpression
|
|
|
|
$expression = $this->ScalarExpression();
|
2009-06-02 22:05:26 +04:00
|
|
|
} else {
|
2010-02-20 00:28:17 +03:00
|
|
|
$supportsAlias = false;
|
2010-11-11 23:12:09 +03:00
|
|
|
$expression = $identVariable = $this->IdentificationVariable();
|
2010-02-20 00:28:17 +03:00
|
|
|
}
|
|
|
|
} else if ($this->_lexer->lookahead['value'] == '(') {
|
|
|
|
if ($peek['type'] == Lexer::T_SELECT) {
|
|
|
|
// Subselect
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_OPEN_PARENTHESIS);
|
2009-07-22 07:46:05 +04:00
|
|
|
$expression = $this->Subselect();
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_CLOSE_PARENTHESIS);
|
2010-02-20 00:28:17 +03:00
|
|
|
} else {
|
|
|
|
// Shortcut: ScalarExpression => SimpleArithmeticExpression
|
|
|
|
$expression = $this->SimpleArithmeticExpression();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
} else if ($this->_isFunction()) {
|
2010-08-12 06:12:44 +04:00
|
|
|
$this->_lexer->peek(); // "("
|
2011-06-17 23:16:22 +04:00
|
|
|
|
|
|
|
$lookaheadType = $this->_lexer->lookahead['type'];
|
|
|
|
$beyond = $this->_peekBeyondClosingParenthesis();
|
|
|
|
|
2010-08-12 06:12:44 +04:00
|
|
|
if ($this->_isMathOperator($beyond)) {
|
|
|
|
$expression = $this->ScalarExpression();
|
|
|
|
} else if ($this->_isAggregateFunction($this->_lexer->lookahead['type'])) {
|
2010-02-20 00:28:17 +03:00
|
|
|
$expression = $this->AggregateExpression();
|
2011-08-08 09:09:25 +04:00
|
|
|
} else if (in_array($lookaheadType, array(Lexer::T_COALESCE, Lexer::T_NULLIF))) {
|
2011-06-17 23:16:22 +04:00
|
|
|
$expression = $this->CaseExpression();
|
2010-02-20 00:28:17 +03:00
|
|
|
} else {
|
2010-03-05 19:35:00 +03:00
|
|
|
// Shortcut: ScalarExpression => Function
|
2010-02-20 00:28:17 +03:00
|
|
|
$expression = $this->FunctionDeclaration();
|
|
|
|
}
|
|
|
|
} else if ($this->_lexer->lookahead['type'] == Lexer::T_PARTIAL) {
|
|
|
|
$supportsAlias = false;
|
|
|
|
$expression = $this->PartialObjectExpression();
|
2010-11-11 23:12:09 +03:00
|
|
|
$identVariable = $expression->identificationVariable;
|
2010-02-20 00:28:17 +03:00
|
|
|
} else if ($this->_lexer->lookahead['type'] == Lexer::T_INTEGER ||
|
2011-03-27 23:10:50 +04:00
|
|
|
$this->_lexer->lookahead['type'] == Lexer::T_FLOAT ||
|
|
|
|
$this->_lexer->lookahead['type'] == Lexer::T_STRING) {
|
2010-02-20 00:28:17 +03:00
|
|
|
// Shortcut: ScalarExpression => SimpleArithmeticExpression
|
|
|
|
$expression = $this->SimpleArithmeticExpression();
|
2011-08-08 09:09:25 +04:00
|
|
|
} else if ($this->_lexer->lookahead['type'] == Lexer::T_CASE) {
|
|
|
|
$expression = $this->CaseExpression();
|
2010-02-20 00:28:17 +03:00
|
|
|
} else {
|
2011-08-08 09:09:25 +04:00
|
|
|
$this->syntaxError(
|
|
|
|
'IdentificationVariable | StateFieldPathExpression | AggregateExpression | "(" Subselect ")" | ScalarExpression',
|
|
|
|
$this->_lexer->lookahead
|
|
|
|
);
|
2010-02-20 00:28:17 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-02-20 00:28:17 +03:00
|
|
|
if ($supportsAlias) {
|
2009-07-22 07:46:05 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_AS)) {
|
|
|
|
$this->match(Lexer::T_AS);
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_IDENTIFIER)) {
|
2009-08-05 23:00:16 +04:00
|
|
|
$token = $this->_lexer->lookahead;
|
2010-01-01 01:48:51 +03:00
|
|
|
$fieldAliasIdentificationVariable = $this->AliasResultVariable();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-01-01 01:48:51 +03:00
|
|
|
// Include AliasResultVariable in query components.
|
2009-09-11 23:50:48 +04:00
|
|
|
$this->_queryComponents[$fieldAliasIdentificationVariable] = array(
|
2009-12-27 06:26:15 +03:00
|
|
|
'resultVariable' => $expression,
|
2009-08-04 23:48:40 +04:00
|
|
|
'nestingLevel' => $this->_nestingLevel,
|
2009-08-05 23:00:16 +04:00
|
|
|
'token' => $token,
|
2009-07-31 08:03:01 +04:00
|
|
|
);
|
2009-07-22 07:46:05 +04:00
|
|
|
}
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-11-11 23:12:09 +03:00
|
|
|
$expr = new AST\SelectExpression($expression, $fieldAliasIdentificationVariable);
|
|
|
|
if (!$supportsAlias) {
|
|
|
|
$this->_identVariableExpressions[$identVariable] = $expr;
|
|
|
|
}
|
|
|
|
return $expr;
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-12 06:12:44 +04:00
|
|
|
* SimpleSelectExpression ::=
|
|
|
|
* StateFieldPathExpression | IdentificationVariable |
|
|
|
|
* ((AggregateExpression | "(" Subselect ")" | ScalarExpression) [["AS"] AliasResultVariable])
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\SimpleSelectExpression
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function SimpleSelectExpression()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2010-08-12 06:12:44 +04:00
|
|
|
$peek = $this->_lexer->glimpse();
|
|
|
|
|
|
|
|
if ($peek['value'] != '(' && $this->_lexer->lookahead['type'] === Lexer::T_IDENTIFIER) {
|
2009-07-22 07:46:05 +04:00
|
|
|
// SingleValuedPathExpression | IdentificationVariable
|
2010-08-12 06:12:44 +04:00
|
|
|
if ($peek['value'] == '.') {
|
|
|
|
$expression = $this->StateFieldPathExpression();
|
|
|
|
} else {
|
|
|
|
$expression = $this->IdentificationVariable();
|
|
|
|
}
|
2009-07-22 07:46:05 +04:00
|
|
|
|
2010-08-12 06:12:44 +04:00
|
|
|
return new AST\SimpleSelectExpression($expression);
|
|
|
|
} else if ($this->_lexer->lookahead['value'] == '(') {
|
|
|
|
if ($peek['type'] == Lexer::T_SELECT) {
|
|
|
|
// Subselect
|
|
|
|
$this->match(Lexer::T_OPEN_PARENTHESIS);
|
|
|
|
$expression = $this->Subselect();
|
|
|
|
$this->match(Lexer::T_CLOSE_PARENTHESIS);
|
|
|
|
} else {
|
|
|
|
// Shortcut: ScalarExpression => SimpleArithmeticExpression
|
|
|
|
$expression = $this->SimpleArithmeticExpression();
|
2009-07-22 07:46:05 +04:00
|
|
|
}
|
|
|
|
|
2010-08-12 06:12:44 +04:00
|
|
|
return new AST\SimpleSelectExpression($expression);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_lexer->peek();
|
2009-07-22 07:46:05 +04:00
|
|
|
|
2011-03-27 16:04:53 +04:00
|
|
|
$expression = $this->ScalarExpression();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-08-12 06:12:44 +04:00
|
|
|
$expr = new AST\SimpleSelectExpression($expression);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-08-02 03:40:11 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_AS)) {
|
|
|
|
$this->match(Lexer::T_AS);
|
|
|
|
}
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2009-08-02 03:40:11 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_IDENTIFIER)) {
|
2009-08-05 23:00:16 +04:00
|
|
|
$token = $this->_lexer->lookahead;
|
2010-01-01 01:48:51 +03:00
|
|
|
$resultVariable = $this->AliasResultVariable();
|
2009-08-07 01:42:07 +04:00
|
|
|
$expr->fieldIdentificationVariable = $resultVariable;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-01-01 01:48:51 +03:00
|
|
|
// Include AliasResultVariable in query components.
|
2009-08-02 03:40:11 +04:00
|
|
|
$this->_queryComponents[$resultVariable] = array(
|
2009-08-04 23:48:40 +04:00
|
|
|
'resultvariable' => $expr,
|
|
|
|
'nestingLevel' => $this->_nestingLevel,
|
2009-08-05 23:00:16 +04:00
|
|
|
'token' => $token,
|
2009-08-02 03:40:11 +04:00
|
|
|
);
|
2009-07-22 07:46:05 +04:00
|
|
|
}
|
2009-08-02 03:40:11 +04:00
|
|
|
|
|
|
|
return $expr;
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ConditionalExpression ::= ConditionalTerm {"OR" ConditionalTerm}*
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\ConditionalExpression
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function ConditionalExpression()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
|
|
|
$conditionalTerms = array();
|
2009-06-14 21:34:28 +04:00
|
|
|
$conditionalTerms[] = $this->ConditionalTerm();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
while ($this->_lexer->isNextToken(Lexer::T_OR)) {
|
|
|
|
$this->match(Lexer::T_OR);
|
2009-06-14 21:34:28 +04:00
|
|
|
$conditionalTerms[] = $this->ConditionalTerm();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
// Phase 1 AST optimization: Prevent AST\ConditionalExpression
|
|
|
|
// if only one AST\ConditionalTerm is defined
|
|
|
|
if (count($conditionalTerms) == 1) {
|
|
|
|
return $conditionalTerms[0];
|
|
|
|
}
|
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
return new AST\ConditionalExpression($conditionalTerms);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ConditionalTerm ::= ConditionalFactor {"AND" ConditionalFactor}*
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\ConditionalTerm
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function ConditionalTerm()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
|
|
|
$conditionalFactors = array();
|
2009-06-14 21:34:28 +04:00
|
|
|
$conditionalFactors[] = $this->ConditionalFactor();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
while ($this->_lexer->isNextToken(Lexer::T_AND)) {
|
|
|
|
$this->match(Lexer::T_AND);
|
2009-06-14 21:34:28 +04:00
|
|
|
$conditionalFactors[] = $this->ConditionalFactor();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
// Phase 1 AST optimization: Prevent AST\ConditionalTerm
|
|
|
|
// if only one AST\ConditionalFactor is defined
|
|
|
|
if (count($conditionalFactors) == 1) {
|
|
|
|
return $conditionalFactors[0];
|
|
|
|
}
|
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
return new AST\ConditionalTerm($conditionalFactors);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ConditionalFactor ::= ["NOT"] ConditionalPrimary
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\ConditionalFactor
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function ConditionalFactor()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
|
|
|
$not = false;
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_NOT)) {
|
|
|
|
$this->match(Lexer::T_NOT);
|
|
|
|
$not = true;
|
|
|
|
}
|
2010-04-30 05:15:36 +04:00
|
|
|
|
|
|
|
$conditionalPrimary = $this->ConditionalPrimary();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
// Phase 1 AST optimization: Prevent AST\ConditionalFactor
|
|
|
|
// if only one AST\ConditionalPrimary is defined
|
|
|
|
if ( ! $not) {
|
|
|
|
return $conditionalPrimary;
|
|
|
|
}
|
|
|
|
|
|
|
|
$conditionalFactor = new AST\ConditionalFactor($conditionalPrimary);
|
|
|
|
$conditionalFactor->not = $not;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
return $conditionalFactor;
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ConditionalPrimary ::= SimpleConditionalExpression | "(" ConditionalExpression ")"
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
2009-08-17 21:58:16 +04:00
|
|
|
* @return Doctrine\ORM\Query\AST\ConditionalPrimary
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function ConditionalPrimary()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
|
|
|
$condPrimary = new AST\ConditionalPrimary;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-11-04 04:52:40 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_OPEN_PARENTHESIS)) {
|
2009-08-16 20:36:00 +04:00
|
|
|
// Peek beyond the matching closing paranthesis ')'
|
2010-02-20 21:27:05 +03:00
|
|
|
$peek = $this->_peekBeyondClosingParenthesis();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-16 20:36:00 +04:00
|
|
|
if (in_array($peek['value'], array("=", "<", "<=", "<>", ">", ">=", "!=")) ||
|
|
|
|
$peek['type'] === Lexer::T_NOT ||
|
|
|
|
$peek['type'] === Lexer::T_BETWEEN ||
|
|
|
|
$peek['type'] === Lexer::T_LIKE ||
|
|
|
|
$peek['type'] === Lexer::T_IN ||
|
|
|
|
$peek['type'] === Lexer::T_IS ||
|
|
|
|
$peek['type'] === Lexer::T_EXISTS) {
|
2009-08-07 01:42:07 +04:00
|
|
|
$condPrimary->simpleConditionalExpression = $this->SimpleConditionalExpression();
|
2009-06-02 22:05:26 +04:00
|
|
|
} else {
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_OPEN_PARENTHESIS);
|
2009-08-16 21:42:04 +04:00
|
|
|
$condPrimary->conditionalExpression = $this->ConditionalExpression();
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_CLOSE_PARENTHESIS);
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
} else {
|
2009-08-07 01:42:07 +04:00
|
|
|
$condPrimary->simpleConditionalExpression = $this->SimpleConditionalExpression();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
return $condPrimary;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SimpleConditionalExpression ::=
|
|
|
|
* ComparisonExpression | BetweenExpression | LikeExpression |
|
|
|
|
* InExpression | NullComparisonExpression | ExistsExpression |
|
2010-07-30 08:30:02 +04:00
|
|
|
* EmptyCollectionComparisonExpression | CollectionMemberExpression |
|
|
|
|
* InstanceOfExpression
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function SimpleConditionalExpression()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_NOT)) {
|
|
|
|
$token = $this->_lexer->glimpse();
|
|
|
|
} else {
|
|
|
|
$token = $this->_lexer->lookahead;
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
if ($token['type'] === Lexer::T_EXISTS) {
|
2009-06-14 21:34:28 +04:00
|
|
|
return $this->ExistsExpression();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
2010-02-20 21:27:05 +03:00
|
|
|
$peek = $this->_lexer->glimpse();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
if ($token['type'] === Lexer::T_IDENTIFIER || $token['type'] === Lexer::T_INPUT_PARAMETER) {
|
2010-02-20 21:27:05 +03:00
|
|
|
if ($peek['value'] == '(') {
|
|
|
|
// Peek beyond the matching closing paranthesis ')'
|
2009-06-02 22:05:26 +04:00
|
|
|
$this->_lexer->peek();
|
2010-02-20 21:27:05 +03:00
|
|
|
$token = $this->_peekBeyondClosingParenthesis();
|
|
|
|
} else {
|
|
|
|
// Peek beyond the PathExpression (or InputParameter)
|
2009-06-02 22:05:26 +04:00
|
|
|
$peek = $this->_lexer->peek();
|
|
|
|
|
2010-02-20 21:27:05 +03:00
|
|
|
while ($peek['value'] === '.') {
|
|
|
|
$this->_lexer->peek();
|
|
|
|
$peek = $this->_lexer->peek();
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-02-20 21:27:05 +03:00
|
|
|
// Also peek beyond a NOT if there is one
|
|
|
|
if ($peek['type'] === Lexer::T_NOT) {
|
|
|
|
$peek = $this->_lexer->peek();
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-02-20 21:27:05 +03:00
|
|
|
$token = $peek;
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-02-20 21:27:05 +03:00
|
|
|
// We need to go even further in case of IS (differenciate between NULL and EMPTY)
|
|
|
|
$lookahead = $this->_lexer->peek();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-02-20 21:27:05 +03:00
|
|
|
// Also peek beyond a NOT if there is one
|
|
|
|
if ($lookahead['type'] === Lexer::T_NOT) {
|
|
|
|
$lookahead = $this->_lexer->peek();
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-02-20 21:27:05 +03:00
|
|
|
$this->_lexer->resetPeek();
|
|
|
|
}
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-02-20 21:27:05 +03:00
|
|
|
switch ($token['type']) {
|
|
|
|
case Lexer::T_BETWEEN:
|
|
|
|
return $this->BetweenExpression();
|
|
|
|
case Lexer::T_LIKE:
|
|
|
|
return $this->LikeExpression();
|
|
|
|
case Lexer::T_IN:
|
|
|
|
return $this->InExpression();
|
2010-07-30 08:30:02 +04:00
|
|
|
case Lexer::T_INSTANCE:
|
|
|
|
return $this->InstanceOfExpression();
|
2010-02-20 21:27:05 +03:00
|
|
|
case Lexer::T_IS:
|
|
|
|
if ($lookahead['type'] == Lexer::T_NULL) {
|
|
|
|
return $this->NullComparisonExpression();
|
|
|
|
}
|
|
|
|
return $this->EmptyCollectionComparisonExpression();
|
|
|
|
case Lexer::T_MEMBER:
|
|
|
|
return $this->CollectionMemberExpression();
|
|
|
|
default:
|
|
|
|
return $this->ComparisonExpression();
|
|
|
|
}
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-05 01:41:53 +04:00
|
|
|
/**
|
|
|
|
* EmptyCollectionComparisonExpression ::= CollectionValuedPathExpression "IS" ["NOT"] "EMPTY"
|
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\EmptyCollectionComparisonExpression
|
|
|
|
*/
|
|
|
|
public function EmptyCollectionComparisonExpression()
|
|
|
|
{
|
|
|
|
$emptyColletionCompExpr = new AST\EmptyCollectionComparisonExpression(
|
|
|
|
$this->CollectionValuedPathExpression()
|
|
|
|
);
|
|
|
|
$this->match(Lexer::T_IS);
|
|
|
|
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_NOT)) {
|
|
|
|
$this->match(Lexer::T_NOT);
|
2009-08-07 01:42:07 +04:00
|
|
|
$emptyColletionCompExpr->not = true;
|
2009-08-05 01:41:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->match(Lexer::T_EMPTY);
|
|
|
|
|
|
|
|
return $emptyColletionCompExpr;
|
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
2009-06-14 21:34:28 +04:00
|
|
|
* CollectionMemberExpression ::= EntityExpression ["NOT"] "MEMBER" ["OF"] CollectionValuedPathExpression
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-06-14 21:34:28 +04:00
|
|
|
* EntityExpression ::= SingleValuedAssociationPathExpression | SimpleEntityExpression
|
|
|
|
* SimpleEntityExpression ::= IdentificationVariable | InputParameter
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-08-02 03:40:11 +04:00
|
|
|
* @return \Doctrine\ORM\Query\AST\CollectionMemberExpression
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function CollectionMemberExpression()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$not = false;
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
$entityExpr = $this->EntityExpression();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-16 08:08:14 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_NOT)) {
|
2009-08-07 01:42:07 +04:00
|
|
|
$not = true;
|
2009-07-16 08:08:14 +04:00
|
|
|
$this->match(Lexer::T_NOT);
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-16 08:08:14 +04:00
|
|
|
$this->match(Lexer::T_MEMBER);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-16 08:08:14 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_OF)) {
|
|
|
|
$this->match(Lexer::T_OF);
|
2009-06-14 21:34:28 +04:00
|
|
|
}
|
2009-07-16 08:08:14 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$collMemberExpr = new AST\CollectionMemberExpression(
|
|
|
|
$entityExpr, $this->CollectionValuedPathExpression()
|
2009-07-16 08:08:14 +04:00
|
|
|
);
|
2009-08-07 01:42:07 +04:00
|
|
|
$collMemberExpr->not = $not;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
return $collMemberExpr;
|
2009-06-14 21:34:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-08-04 08:11:43 +04:00
|
|
|
* Literal ::= string | char | integer | float | boolean
|
2009-06-14 21:34:28 +04:00
|
|
|
*
|
2009-08-04 08:11:43 +04:00
|
|
|
* @return string
|
2009-06-14 21:34:28 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function Literal()
|
2009-06-14 21:34:28 +04:00
|
|
|
{
|
2009-07-22 07:46:05 +04:00
|
|
|
switch ($this->_lexer->lookahead['type']) {
|
|
|
|
case Lexer::T_STRING:
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_STRING);
|
2009-10-13 14:48:46 +04:00
|
|
|
return new AST\Literal(AST\Literal::STRING, $this->_lexer->token['value']);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
case Lexer::T_INTEGER:
|
|
|
|
case Lexer::T_FLOAT:
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(
|
|
|
|
$this->_lexer->isNextToken(Lexer::T_INTEGER) ? Lexer::T_INTEGER : Lexer::T_FLOAT
|
|
|
|
);
|
2009-10-13 14:48:46 +04:00
|
|
|
return new AST\Literal(AST\Literal::NUMERIC, $this->_lexer->token['value']);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-10-13 14:48:46 +04:00
|
|
|
case Lexer::T_TRUE:
|
|
|
|
case Lexer::T_FALSE:
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(
|
|
|
|
$this->_lexer->isNextToken(Lexer::T_TRUE) ? Lexer::T_TRUE : Lexer::T_FALSE
|
|
|
|
);
|
2009-10-13 14:48:46 +04:00
|
|
|
return new AST\Literal(AST\Literal::BOOLEAN, $this->_lexer->token['value']);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
default:
|
|
|
|
$this->syntaxError('Literal');
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-08-04 08:11:43 +04:00
|
|
|
/**
|
|
|
|
* InParameter ::= Literal | InputParameter
|
|
|
|
*
|
|
|
|
* @return string | \Doctrine\ORM\Query\AST\InputParameter
|
|
|
|
*/
|
|
|
|
public function InParameter()
|
|
|
|
{
|
|
|
|
if ($this->_lexer->lookahead['type'] == Lexer::T_INPUT_PARAMETER) {
|
|
|
|
return $this->InputParameter();
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-04 08:11:43 +04:00
|
|
|
return $this->Literal();
|
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-08-04 08:11:43 +04:00
|
|
|
/**
|
|
|
|
* InputParameter ::= PositionalParameter | NamedParameter
|
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\InputParameter
|
|
|
|
*/
|
|
|
|
public function InputParameter()
|
|
|
|
{
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_INPUT_PARAMETER);
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2009-08-04 08:11:43 +04:00
|
|
|
return new AST\InputParameter($this->_lexer->token['value']);
|
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
|
|
|
* ArithmeticExpression ::= SimpleArithmeticExpression | "(" Subselect ")"
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\ArithmeticExpression
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function ArithmeticExpression()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
|
|
|
$expr = new AST\ArithmeticExpression;
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-11-04 00:42:58 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_OPEN_PARENTHESIS)) {
|
2009-06-02 22:05:26 +04:00
|
|
|
$peek = $this->_lexer->glimpse();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
if ($peek['type'] === Lexer::T_SELECT) {
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_OPEN_PARENTHESIS);
|
2009-08-06 19:48:41 +04:00
|
|
|
$expr->subselect = $this->Subselect();
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_CLOSE_PARENTHESIS);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
return $expr;
|
|
|
|
}
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-08-06 19:48:41 +04:00
|
|
|
$expr->simpleArithmeticExpression = $this->SimpleArithmeticExpression();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
return $expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SimpleArithmeticExpression ::= ArithmeticTerm {("+" | "-") ArithmeticTerm}*
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\SimpleArithmeticExpression
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function SimpleArithmeticExpression()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
|
|
|
$terms = array();
|
2009-06-14 21:34:28 +04:00
|
|
|
$terms[] = $this->ArithmeticTerm();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-11-04 00:42:58 +03:00
|
|
|
while (($isPlus = $this->_lexer->isNextToken(Lexer::T_PLUS)) || $this->_lexer->isNextToken(Lexer::T_MINUS)) {
|
|
|
|
$this->match(($isPlus) ? Lexer::T_PLUS : Lexer::T_MINUS);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
$terms[] = $this->_lexer->token['value'];
|
2009-06-14 21:34:28 +04:00
|
|
|
$terms[] = $this->ArithmeticTerm();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
// Phase 1 AST optimization: Prevent AST\SimpleArithmeticExpression
|
|
|
|
// if only one AST\ArithmeticTerm is defined
|
|
|
|
if (count($terms) == 1) {
|
|
|
|
return $terms[0];
|
|
|
|
}
|
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
return new AST\SimpleArithmeticExpression($terms);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ArithmeticTerm ::= ArithmeticFactor {("*" | "/") ArithmeticFactor}*
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\ArithmeticTerm
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function ArithmeticTerm()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
|
|
|
$factors = array();
|
2009-06-14 21:34:28 +04:00
|
|
|
$factors[] = $this->ArithmeticFactor();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-11-04 00:42:58 +03:00
|
|
|
while (($isMult = $this->_lexer->isNextToken(Lexer::T_MULTIPLY)) || $this->_lexer->isNextToken(Lexer::T_DIVIDE)) {
|
|
|
|
$this->match(($isMult) ? Lexer::T_MULTIPLY : Lexer::T_DIVIDE);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
$factors[] = $this->_lexer->token['value'];
|
2009-06-14 21:34:28 +04:00
|
|
|
$factors[] = $this->ArithmeticFactor();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
// Phase 1 AST optimization: Prevent AST\ArithmeticTerm
|
|
|
|
// if only one AST\ArithmeticFactor is defined
|
|
|
|
if (count($factors) == 1) {
|
|
|
|
return $factors[0];
|
|
|
|
}
|
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
return new AST\ArithmeticTerm($factors);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ArithmeticFactor ::= [("+" | "-")] ArithmeticPrimary
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\ArithmeticFactor
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function ArithmeticFactor()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2010-04-30 05:15:36 +04:00
|
|
|
$sign = null;
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
if (($isPlus = $this->_lexer->isNextToken(Lexer::T_PLUS)) || $this->_lexer->isNextToken(Lexer::T_MINUS)) {
|
|
|
|
$this->match(($isPlus) ? Lexer::T_PLUS : Lexer::T_MINUS);
|
|
|
|
$sign = $isPlus;
|
|
|
|
}
|
|
|
|
|
|
|
|
$primary = $this->ArithmeticPrimary();
|
|
|
|
|
|
|
|
// Phase 1 AST optimization: Prevent AST\ArithmeticFactor
|
|
|
|
// if only one AST\ArithmeticPrimary is defined
|
|
|
|
if ($sign === null) {
|
|
|
|
return $primary;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-04-30 05:15:36 +04:00
|
|
|
return new AST\ArithmeticFactor($primary, $sign);
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-22 07:46:05 +04:00
|
|
|
* ArithmeticPrimary ::= SingleValuedPathExpression | Literal | "(" SimpleArithmeticExpression ")"
|
|
|
|
* | FunctionsReturningNumerics | AggregateExpression | FunctionsReturningStrings
|
2011-09-05 10:16:01 +04:00
|
|
|
* | FunctionsReturningDatetime | IdentificationVariable | ResultVariable | CaseExpression
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function ArithmeticPrimary()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-11-04 00:42:58 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_OPEN_PARENTHESIS)) {
|
|
|
|
$this->match(Lexer::T_OPEN_PARENTHESIS);
|
2009-07-22 07:46:05 +04:00
|
|
|
$expr = $this->SimpleArithmeticExpression();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_CLOSE_PARENTHESIS);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
return $expr;
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
switch ($this->_lexer->lookahead['type']) {
|
2011-08-08 09:09:25 +04:00
|
|
|
case Lexer::T_COALESCE:
|
|
|
|
case Lexer::T_NULLIF:
|
|
|
|
case Lexer::T_CASE:
|
|
|
|
return $this->CaseExpression();
|
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
case Lexer::T_IDENTIFIER:
|
|
|
|
$peek = $this->_lexer->glimpse();
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
if ($peek['value'] == '(') {
|
|
|
|
return $this->FunctionDeclaration();
|
|
|
|
}
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
if ($peek['value'] == '.') {
|
|
|
|
return $this->SingleValuedPathExpression();
|
|
|
|
}
|
2011-09-05 10:16:01 +04:00
|
|
|
|
|
|
|
if (isset($this->_queryComponents[$this->_lexer->lookahead['value']]['resultVariable'])) {
|
|
|
|
return $this->ResultVariable();
|
|
|
|
}
|
|
|
|
|
2010-07-23 08:55:33 +04:00
|
|
|
return $this->StateFieldPathExpression();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
case Lexer::T_INPUT_PARAMETER:
|
2009-08-04 08:11:43 +04:00
|
|
|
return $this->InputParameter();
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
default:
|
|
|
|
$peek = $this->_lexer->glimpse();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
if ($peek['value'] == '(') {
|
|
|
|
if ($this->_isAggregateFunction($this->_lexer->lookahead['type'])) {
|
|
|
|
return $this->AggregateExpression();
|
|
|
|
}
|
2009-06-14 21:34:28 +04:00
|
|
|
|
2009-08-05 19:47:41 +04:00
|
|
|
return $this->FunctionDeclaration();
|
2009-10-13 14:48:46 +04:00
|
|
|
} else {
|
|
|
|
return $this->Literal();
|
2009-07-22 07:46:05 +04:00
|
|
|
}
|
|
|
|
}
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
2009-07-22 07:46:05 +04:00
|
|
|
* StringExpression ::= StringPrimary | "(" Subselect ")"
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\StringPrimary |
|
|
|
|
* \Doctrine]ORM\Query\AST\Subselect
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function StringExpression()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-11-04 00:42:58 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_OPEN_PARENTHESIS)) {
|
2009-07-22 07:46:05 +04:00
|
|
|
$peek = $this->_lexer->glimpse();
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
if ($peek['type'] === Lexer::T_SELECT) {
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_OPEN_PARENTHESIS);
|
2009-07-22 07:46:05 +04:00
|
|
|
$expr = $this->Subselect();
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_CLOSE_PARENTHESIS);
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
return $expr;
|
|
|
|
}
|
|
|
|
}
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
return $this->StringPrimary();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-08-08 09:09:25 +04:00
|
|
|
* StringPrimary ::= StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression | CaseExpression
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function StringPrimary()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-11-04 00:42:58 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_IDENTIFIER)) {
|
2009-07-22 07:46:05 +04:00
|
|
|
$peek = $this->_lexer->glimpse();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
if ($peek['value'] == '.') {
|
|
|
|
return $this->StateFieldPathExpression();
|
|
|
|
} else if ($peek['value'] == '(') {
|
2011-05-01 13:01:30 +04:00
|
|
|
// do NOT directly go to FunctionsReturningString() because it doesnt check for custom functions.
|
|
|
|
return $this->FunctionDeclaration();
|
2009-07-22 07:46:05 +04:00
|
|
|
} else {
|
|
|
|
$this->syntaxError("'.' or '('");
|
|
|
|
}
|
2009-11-04 00:42:58 +03:00
|
|
|
} else if ($this->_lexer->isNextToken(Lexer::T_STRING)) {
|
2009-07-22 07:46:05 +04:00
|
|
|
$this->match(Lexer::T_STRING);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
return $this->_lexer->token['value'];
|
2009-11-04 00:42:58 +03:00
|
|
|
} else if ($this->_lexer->isNextToken(Lexer::T_INPUT_PARAMETER)) {
|
2009-08-04 08:11:43 +04:00
|
|
|
return $this->InputParameter();
|
2009-07-22 07:46:05 +04:00
|
|
|
} else if ($this->_isAggregateFunction($this->_lexer->lookahead['type'])) {
|
|
|
|
return $this->AggregateExpression();
|
2011-08-08 09:09:25 +04:00
|
|
|
} else if (in_array($this->_lexer->lookahead['type'], array(Lexer::T_CASE, Lexer::T_COALESCE, Lexer::T_NULLIF))) {
|
|
|
|
return $this->CaseExpression();
|
2009-07-22 07:46:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->syntaxError('StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression');
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-22 07:46:05 +04:00
|
|
|
* EntityExpression ::= SingleValuedAssociationPathExpression | SimpleEntityExpression
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\SingleValuedAssociationPathExpression |
|
|
|
|
* \Doctrine\ORM\Query\AST\SimpleEntityExpression
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function EntityExpression()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-07-22 07:46:05 +04:00
|
|
|
$glimpse = $this->_lexer->glimpse();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_IDENTIFIER) && $glimpse['value'] === '.') {
|
|
|
|
return $this->SingleValuedAssociationPathExpression();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
return $this->SimpleEntityExpression();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
2009-07-22 07:46:05 +04:00
|
|
|
* SimpleEntityExpression ::= IdentificationVariable | InputParameter
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return string | \Doctrine\ORM\Query\AST\InputParameter
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function SimpleEntityExpression()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-07-22 07:46:05 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_INPUT_PARAMETER)) {
|
2009-08-04 08:11:43 +04:00
|
|
|
return $this->InputParameter();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
return $this->IdentificationVariable();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-22 07:46:05 +04:00
|
|
|
* AggregateExpression ::=
|
|
|
|
* ("AVG" | "MAX" | "MIN" | "SUM") "(" ["DISTINCT"] StateFieldPathExpression ")" |
|
|
|
|
* "COUNT" "(" ["DISTINCT"] (IdentificationVariable | SingleValuedPathExpression) ")"
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\AggregateExpression
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function AggregateExpression()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-07-22 07:46:05 +04:00
|
|
|
$isDistinct = false;
|
|
|
|
$functionName = '';
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_COUNT)) {
|
|
|
|
$this->match(Lexer::T_COUNT);
|
|
|
|
$functionName = $this->_lexer->token['value'];
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_OPEN_PARENTHESIS);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_DISTINCT)) {
|
|
|
|
$this->match(Lexer::T_DISTINCT);
|
|
|
|
$isDistinct = true;
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
$pathExp = $this->SingleValuedPathExpression();
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_CLOSE_PARENTHESIS);
|
2009-07-22 07:46:05 +04:00
|
|
|
} else {
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_AVG)) {
|
|
|
|
$this->match(Lexer::T_AVG);
|
|
|
|
} else if ($this->_lexer->isNextToken(Lexer::T_MAX)) {
|
|
|
|
$this->match(Lexer::T_MAX);
|
|
|
|
} else if ($this->_lexer->isNextToken(Lexer::T_MIN)) {
|
|
|
|
$this->match(Lexer::T_MIN);
|
|
|
|
} else if ($this->_lexer->isNextToken(Lexer::T_SUM)) {
|
|
|
|
$this->match(Lexer::T_SUM);
|
|
|
|
} else {
|
|
|
|
$this->syntaxError('One of: MAX, MIN, AVG, SUM, COUNT');
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
$functionName = $this->_lexer->token['value'];
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_OPEN_PARENTHESIS);
|
2011-02-20 07:44:05 +03:00
|
|
|
$pathExp = $this->SimpleArithmeticExpression();
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_CLOSE_PARENTHESIS);
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2009-07-22 07:46:05 +04:00
|
|
|
|
|
|
|
return new AST\AggregateExpression($functionName, $pathExp, $isDistinct);
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-22 07:46:05 +04:00
|
|
|
* QuantifiedExpression ::= ("ALL" | "ANY" | "SOME") "(" Subselect ")"
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\QuantifiedExpression
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function QuantifiedExpression()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-08-07 01:42:07 +04:00
|
|
|
$type = '';
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_ALL)) {
|
|
|
|
$this->match(Lexer::T_ALL);
|
2009-08-07 01:42:07 +04:00
|
|
|
$type = 'ALL';
|
2009-07-22 07:46:05 +04:00
|
|
|
} else if ($this->_lexer->isNextToken(Lexer::T_ANY)) {
|
|
|
|
$this->match(Lexer::T_ANY);
|
2009-08-07 01:42:07 +04:00
|
|
|
$type = 'ANY';
|
2009-07-22 07:46:05 +04:00
|
|
|
} else if ($this->_lexer->isNextToken(Lexer::T_SOME)) {
|
|
|
|
$this->match(Lexer::T_SOME);
|
2009-08-07 01:42:07 +04:00
|
|
|
$type = 'SOME';
|
2009-07-22 07:46:05 +04:00
|
|
|
} else {
|
|
|
|
$this->syntaxError('ALL, ANY or SOME');
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_OPEN_PARENTHESIS);
|
2009-07-22 07:46:05 +04:00
|
|
|
$qExpr = new AST\QuantifiedExpression($this->Subselect());
|
2009-08-07 01:42:07 +04:00
|
|
|
$qExpr->type = $type;
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_CLOSE_PARENTHESIS);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
return $qExpr;
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BetweenExpression ::= ArithmeticExpression ["NOT"] "BETWEEN" ArithmeticExpression "AND" ArithmeticExpression
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\BetweenExpression
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function BetweenExpression()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
|
|
|
$not = false;
|
2009-06-14 21:34:28 +04:00
|
|
|
$arithExpr1 = $this->ArithmeticExpression();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_NOT)) {
|
|
|
|
$this->match(Lexer::T_NOT);
|
|
|
|
$not = true;
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
$this->match(Lexer::T_BETWEEN);
|
2009-06-14 21:34:28 +04:00
|
|
|
$arithExpr2 = $this->ArithmeticExpression();
|
2009-06-02 22:05:26 +04:00
|
|
|
$this->match(Lexer::T_AND);
|
2009-06-14 21:34:28 +04:00
|
|
|
$arithExpr3 = $this->ArithmeticExpression();
|
2009-06-02 22:05:26 +04:00
|
|
|
|
|
|
|
$betweenExpr = new AST\BetweenExpression($arithExpr1, $arithExpr2, $arithExpr3);
|
2009-08-07 01:42:07 +04:00
|
|
|
$betweenExpr->not = $not;
|
2009-06-02 22:05:26 +04:00
|
|
|
|
|
|
|
return $betweenExpr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-22 07:46:05 +04:00
|
|
|
* ComparisonExpression ::= ArithmeticExpression ComparisonOperator ( QuantifiedExpression | ArithmeticExpression )
|
|
|
|
*
|
2009-08-02 03:40:11 +04:00
|
|
|
* @return \Doctrine\ORM\Query\AST\ComparisonExpression
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function ComparisonExpression()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-07-22 07:46:05 +04:00
|
|
|
$peek = $this->_lexer->glimpse();
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
$leftExpr = $this->ArithmeticExpression();
|
|
|
|
$operator = $this->ComparisonOperator();
|
|
|
|
|
|
|
|
if ($this->_isNextAllAnySome()) {
|
|
|
|
$rightExpr = $this->QuantifiedExpression();
|
|
|
|
} else {
|
|
|
|
$rightExpr = $this->ArithmeticExpression();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
return new AST\ComparisonExpression($leftExpr, $operator, $rightExpr);
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
/**
|
2010-08-12 07:16:07 +04:00
|
|
|
* InExpression ::= SingleValuedPathExpression ["NOT"] "IN" "(" (InParameter {"," InParameter}* | Subselect) ")"
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\InExpression
|
2009-07-22 07:46:05 +04:00
|
|
|
*/
|
|
|
|
public function InExpression()
|
|
|
|
{
|
2010-08-12 07:16:07 +04:00
|
|
|
$inExpression = new AST\InExpression($this->SingleValuedPathExpression());
|
2009-07-22 07:46:05 +04:00
|
|
|
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_NOT)) {
|
|
|
|
$this->match(Lexer::T_NOT);
|
2009-08-07 01:42:07 +04:00
|
|
|
$inExpression->not = true;
|
2009-07-22 07:46:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->match(Lexer::T_IN);
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_OPEN_PARENTHESIS);
|
2009-07-22 07:46:05 +04:00
|
|
|
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_SELECT)) {
|
2009-08-07 01:42:07 +04:00
|
|
|
$inExpression->subselect = $this->Subselect();
|
2009-07-22 07:46:05 +04:00
|
|
|
} else {
|
|
|
|
$literals = array();
|
2009-08-04 08:11:43 +04:00
|
|
|
$literals[] = $this->InParameter();
|
2009-07-22 07:46:05 +04:00
|
|
|
|
2009-11-04 00:42:58 +03:00
|
|
|
while ($this->_lexer->isNextToken(Lexer::T_COMMA)) {
|
|
|
|
$this->match(Lexer::T_COMMA);
|
2009-08-04 08:11:43 +04:00
|
|
|
$literals[] = $this->InParameter();
|
2009-07-22 07:46:05 +04:00
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$inExpression->literals = $literals;
|
2009-07-22 07:46:05 +04:00
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_CLOSE_PARENTHESIS);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
return $inExpression;
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-07-30 08:30:02 +04:00
|
|
|
/**
|
2011-08-15 08:53:56 +04:00
|
|
|
* InstanceOfExpression ::= IdentificationVariable ["NOT"] "INSTANCE" ["OF"] (InstanceOfParameter | "(" InstanceOfParameter {"," InstanceOfParameter}* ")")
|
2010-07-30 08:30:02 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\InstanceOfExpression
|
|
|
|
*/
|
|
|
|
public function InstanceOfExpression()
|
|
|
|
{
|
|
|
|
$instanceOfExpression = new AST\InstanceOfExpression($this->IdentificationVariable());
|
|
|
|
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_NOT)) {
|
|
|
|
$this->match(Lexer::T_NOT);
|
|
|
|
$instanceOfExpression->not = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->match(Lexer::T_INSTANCE);
|
2011-08-15 08:53:56 +04:00
|
|
|
$this->match(Lexer::T_OF);
|
|
|
|
|
|
|
|
$exprValues = array();
|
|
|
|
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_OPEN_PARENTHESIS)) {
|
|
|
|
$this->match(Lexer::T_OPEN_PARENTHESIS);
|
|
|
|
|
|
|
|
$exprValues[] = $this->InstanceOfParameter();
|
2010-07-30 08:30:02 +04:00
|
|
|
|
2011-08-15 08:53:56 +04:00
|
|
|
while ($this->_lexer->isNextToken(Lexer::T_COMMA)) {
|
|
|
|
$this->match(Lexer::T_COMMA);
|
|
|
|
|
|
|
|
$exprValues[] = $this->InstanceOfParameter();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->match(Lexer::T_CLOSE_PARENTHESIS);
|
|
|
|
|
|
|
|
$instanceOfExpression->value = $exprValues;
|
|
|
|
|
|
|
|
return $instanceOfExpression;
|
2010-07-30 08:30:02 +04:00
|
|
|
}
|
|
|
|
|
2011-08-15 08:53:56 +04:00
|
|
|
$exprValues[] = $this->InstanceOfParameter();
|
|
|
|
|
|
|
|
$instanceOfExpression->value = $exprValues;
|
|
|
|
|
|
|
|
return $instanceOfExpression;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* InstanceOfParameter ::= AbstractSchemaName | InputParameter
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function InstanceOfParameter()
|
|
|
|
{
|
2010-07-30 08:30:02 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_INPUT_PARAMETER)) {
|
|
|
|
$this->match(Lexer::T_INPUT_PARAMETER);
|
2011-08-15 08:53:56 +04:00
|
|
|
|
|
|
|
return new AST\InputParameter($this->_lexer->token['value']);
|
2010-07-30 08:30:02 +04:00
|
|
|
}
|
|
|
|
|
2011-08-15 08:53:56 +04:00
|
|
|
return $this->AliasIdentificationVariable();
|
2010-07-30 08:30:02 +04:00
|
|
|
}
|
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
/**
|
|
|
|
* LikeExpression ::= StringExpression ["NOT"] "LIKE" (string | input_parameter) ["ESCAPE" char]
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\LikeExpression
|
2009-07-22 07:46:05 +04:00
|
|
|
*/
|
|
|
|
public function LikeExpression()
|
|
|
|
{
|
|
|
|
$stringExpr = $this->StringExpression();
|
2009-08-07 01:42:07 +04:00
|
|
|
$not = false;
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-11-04 00:42:58 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_NOT)) {
|
2009-07-22 07:46:05 +04:00
|
|
|
$this->match(Lexer::T_NOT);
|
2009-08-07 01:42:07 +04:00
|
|
|
$not = true;
|
2009-07-22 07:46:05 +04:00
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
$this->match(Lexer::T_LIKE);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_INPUT_PARAMETER)) {
|
|
|
|
$this->match(Lexer::T_INPUT_PARAMETER);
|
|
|
|
$stringPattern = new AST\InputParameter($this->_lexer->token['value']);
|
|
|
|
} else {
|
|
|
|
$this->match(Lexer::T_STRING);
|
|
|
|
$stringPattern = $this->_lexer->token['value'];
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
$escapeChar = null;
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
if ($this->_lexer->lookahead['type'] === Lexer::T_ESCAPE) {
|
|
|
|
$this->match(Lexer::T_ESCAPE);
|
|
|
|
$this->match(Lexer::T_STRING);
|
|
|
|
$escapeChar = $this->_lexer->token['value'];
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2009-07-22 07:46:05 +04:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
$likeExpr = new AST\LikeExpression($stringExpr, $stringPattern, $escapeChar);
|
|
|
|
$likeExpr->not = $not;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-07 01:42:07 +04:00
|
|
|
return $likeExpr;
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-22 07:46:05 +04:00
|
|
|
* NullComparisonExpression ::= (SingleValuedPathExpression | InputParameter) "IS" ["NOT"] "NULL"
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\NullComparisonExpression
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function NullComparisonExpression()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-07-22 07:46:05 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_INPUT_PARAMETER)) {
|
|
|
|
$this->match(Lexer::T_INPUT_PARAMETER);
|
|
|
|
$expr = new AST\InputParameter($this->_lexer->token['value']);
|
|
|
|
} else {
|
|
|
|
$expr = $this->SingleValuedPathExpression();
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
$nullCompExpr = new AST\NullComparisonExpression($expr);
|
|
|
|
$this->match(Lexer::T_IS);
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_NOT)) {
|
|
|
|
$this->match(Lexer::T_NOT);
|
2009-08-07 01:42:07 +04:00
|
|
|
$nullCompExpr->not = true;
|
2009-07-22 07:46:05 +04:00
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
$this->match(Lexer::T_NULL);
|
|
|
|
|
|
|
|
return $nullCompExpr;
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-22 07:46:05 +04:00
|
|
|
* ExistsExpression ::= ["NOT"] "EXISTS" "(" Subselect ")"
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return \Doctrine\ORM\Query\AST\ExistsExpression
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function ExistsExpression()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-07-22 07:46:05 +04:00
|
|
|
$not = false;
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_NOT)) {
|
|
|
|
$this->match(Lexer::T_NOT);
|
|
|
|
$not = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->match(Lexer::T_EXISTS);
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_OPEN_PARENTHESIS);
|
2009-07-22 07:46:05 +04:00
|
|
|
$existsExpression = new AST\ExistsExpression($this->Subselect());
|
2009-08-07 01:42:07 +04:00
|
|
|
$existsExpression->not = $not;
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_CLOSE_PARENTHESIS);
|
2009-07-22 07:46:05 +04:00
|
|
|
|
|
|
|
return $existsExpression;
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ComparisonOperator ::= "=" | "<" | "<=" | "<>" | ">" | ">=" | "!="
|
2009-08-02 03:40:11 +04:00
|
|
|
*
|
|
|
|
* @return string
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-06-14 21:34:28 +04:00
|
|
|
public function ComparisonOperator()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
|
|
|
switch ($this->_lexer->lookahead['value']) {
|
|
|
|
case '=':
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_EQUALS);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
return '=';
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
case '<':
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_LOWER_THAN);
|
2009-06-02 22:05:26 +04:00
|
|
|
$operator = '<';
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-11-04 00:42:58 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_EQUALS)) {
|
|
|
|
$this->match(Lexer::T_EQUALS);
|
2009-06-02 22:05:26 +04:00
|
|
|
$operator .= '=';
|
2009-11-04 00:42:58 +03:00
|
|
|
} else if ($this->_lexer->isNextToken(Lexer::T_GREATER_THAN)) {
|
|
|
|
$this->match(Lexer::T_GREATER_THAN);
|
2009-06-02 22:05:26 +04:00
|
|
|
$operator .= '>';
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
return $operator;
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
case '>':
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_GREATER_THAN);
|
2009-06-02 22:05:26 +04:00
|
|
|
$operator = '>';
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-11-04 00:42:58 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_EQUALS)) {
|
|
|
|
$this->match(Lexer::T_EQUALS);
|
2009-06-02 22:05:26 +04:00
|
|
|
$operator .= '=';
|
|
|
|
}
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
return $operator;
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
case '!':
|
2009-11-04 00:42:58 +03:00
|
|
|
$this->match(Lexer::T_NEGATE);
|
|
|
|
$this->match(Lexer::T_EQUALS);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
return '<>';
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
default:
|
|
|
|
$this->syntaxError('=, <, <=, <>, >, >=, !=');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-22 07:46:05 +04:00
|
|
|
* FunctionDeclaration ::= FunctionsReturningStrings | FunctionsReturningNumerics | FunctionsReturningDatetime
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function FunctionDeclaration()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2010-03-16 17:01:54 +03:00
|
|
|
$token = $this->_lexer->lookahead;
|
|
|
|
$funcName = strtolower($token['value']);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2010-03-15 20:19:00 +03:00
|
|
|
// Check for built-in functions first!
|
|
|
|
if (isset(self::$_STRING_FUNCTIONS[$funcName])) {
|
|
|
|
return $this->FunctionsReturningStrings();
|
|
|
|
} else if (isset(self::$_NUMERIC_FUNCTIONS[$funcName])) {
|
|
|
|
return $this->FunctionsReturningNumerics();
|
|
|
|
} else if (isset(self::$_DATETIME_FUNCTIONS[$funcName])) {
|
|
|
|
return $this->FunctionsReturningDatetime();
|
|
|
|
}
|
2010-03-16 17:01:54 +03:00
|
|
|
|
2010-03-15 20:19:00 +03:00
|
|
|
// Check for custom functions afterwards
|
|
|
|
$config = $this->_em->getConfiguration();
|
2010-03-16 17:01:54 +03:00
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
if ($config->getCustomStringFunction($funcName) !== null) {
|
|
|
|
return $this->CustomFunctionsReturningStrings();
|
|
|
|
} else if ($config->getCustomNumericFunction($funcName) !== null) {
|
|
|
|
return $this->CustomFunctionsReturningNumerics();
|
|
|
|
} else if ($config->getCustomDatetimeFunction($funcName) !== null) {
|
|
|
|
return $this->CustomFunctionsReturningDatetime();
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-03-16 17:01:54 +03:00
|
|
|
$this->syntaxError('known function', $token);
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-07-22 07:46:05 +04:00
|
|
|
* FunctionsReturningNumerics ::=
|
|
|
|
* "LENGTH" "(" StringPrimary ")" |
|
|
|
|
* "LOCATE" "(" StringPrimary "," StringPrimary ["," SimpleArithmeticExpression]")" |
|
|
|
|
* "ABS" "(" SimpleArithmeticExpression ")" |
|
|
|
|
* "SQRT" "(" SimpleArithmeticExpression ")" |
|
|
|
|
* "MOD" "(" SimpleArithmeticExpression "," SimpleArithmeticExpression ")" |
|
|
|
|
* "SIZE" "(" CollectionValuedPathExpression ")"
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function FunctionsReturningNumerics()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-07-22 07:46:05 +04:00
|
|
|
$funcNameLower = strtolower($this->_lexer->lookahead['value']);
|
|
|
|
$funcClass = self::$_NUMERIC_FUNCTIONS[$funcNameLower];
|
|
|
|
$function = new $funcClass($funcNameLower);
|
|
|
|
$function->parse($this);
|
2009-07-16 03:20:11 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
return $function;
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
public function CustomFunctionsReturningNumerics()
|
|
|
|
{
|
2010-04-15 22:14:03 +04:00
|
|
|
$funcName = strtolower($this->_lexer->lookahead['value']);
|
|
|
|
// getCustomNumericFunction is case-insensitive
|
|
|
|
$funcClass = $this->_em->getConfiguration()->getCustomNumericFunction($funcName);
|
|
|
|
$function = new $funcClass($funcName);
|
2010-03-29 17:20:41 +04:00
|
|
|
$function->parse($this);
|
|
|
|
|
|
|
|
return $function;
|
|
|
|
}
|
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
2009-07-22 07:46:05 +04:00
|
|
|
* FunctionsReturningDateTime ::= "CURRENT_DATE" | "CURRENT_TIME" | "CURRENT_TIMESTAMP"
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function FunctionsReturningDatetime()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-07-22 07:46:05 +04:00
|
|
|
$funcNameLower = strtolower($this->_lexer->lookahead['value']);
|
|
|
|
$funcClass = self::$_DATETIME_FUNCTIONS[$funcNameLower];
|
|
|
|
$function = new $funcClass($funcNameLower);
|
|
|
|
$function->parse($this);
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
return $function;
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2010-02-20 00:28:17 +03:00
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
public function CustomFunctionsReturningDatetime()
|
|
|
|
{
|
2010-04-15 22:14:03 +04:00
|
|
|
$funcName = $this->_lexer->lookahead['value'];
|
|
|
|
// getCustomDatetimeFunction is case-insensitive
|
|
|
|
$funcClass = $this->_em->getConfiguration()->getCustomDatetimeFunction($funcName);
|
|
|
|
$function = new $funcClass($funcName);
|
2010-03-29 17:20:41 +04:00
|
|
|
$function->parse($this);
|
|
|
|
|
|
|
|
return $function;
|
|
|
|
}
|
|
|
|
|
2009-06-02 22:05:26 +04:00
|
|
|
/**
|
2009-07-22 07:46:05 +04:00
|
|
|
* FunctionsReturningStrings ::=
|
|
|
|
* "CONCAT" "(" StringPrimary "," StringPrimary ")" |
|
|
|
|
* "SUBSTRING" "(" StringPrimary "," SimpleArithmeticExpression "," SimpleArithmeticExpression ")" |
|
|
|
|
* "TRIM" "(" [["LEADING" | "TRAILING" | "BOTH"] [char] "FROM"] StringPrimary ")" |
|
|
|
|
* "LOWER" "(" StringPrimary ")" |
|
|
|
|
* "UPPER" "(" StringPrimary ")"
|
2009-06-02 22:05:26 +04:00
|
|
|
*/
|
2009-07-22 07:46:05 +04:00
|
|
|
public function FunctionsReturningStrings()
|
2009-06-02 22:05:26 +04:00
|
|
|
{
|
2009-07-22 07:46:05 +04:00
|
|
|
$funcNameLower = strtolower($this->_lexer->lookahead['value']);
|
|
|
|
$funcClass = self::$_STRING_FUNCTIONS[$funcNameLower];
|
2010-03-29 17:20:41 +04:00
|
|
|
$function = new $funcClass($funcNameLower);
|
|
|
|
$function->parse($this);
|
|
|
|
|
|
|
|
return $function;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function CustomFunctionsReturningStrings()
|
|
|
|
{
|
2010-04-15 22:14:03 +04:00
|
|
|
$funcName = $this->_lexer->lookahead['value'];
|
|
|
|
// getCustomStringFunction is case-insensitive
|
|
|
|
$funcClass = $this->_em->getConfiguration()->getCustomStringFunction($funcName);
|
|
|
|
$function = new $funcClass($funcName);
|
2009-07-22 07:46:05 +04:00
|
|
|
$function->parse($this);
|
2009-06-02 22:05:26 +04:00
|
|
|
|
2009-07-22 07:46:05 +04:00
|
|
|
return $function;
|
2009-06-02 22:05:26 +04:00
|
|
|
}
|
2009-07-20 16:05:19 +04:00
|
|
|
}
|