2008-09-23 06:47:11 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* 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
|
2009-02-17 13:54:18 +03:00
|
|
|
* <http://www.doctrine-project.org>.
|
2008-09-23 06:47:11 +04:00
|
|
|
*/
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\ORM\Query;
|
|
|
|
|
2009-03-23 20:39:33 +03:00
|
|
|
use Doctrine\Common\DoctrineException;
|
2009-04-03 15:06:58 +04:00
|
|
|
use Doctrine\ORM\Query;
|
2009-01-22 22:38:10 +03:00
|
|
|
use Doctrine\ORM\Query\AST;
|
|
|
|
use Doctrine\ORM\Query\Exec;
|
|
|
|
|
2008-09-23 06:47:11 +04:00
|
|
|
/**
|
2009-03-23 22:02:16 +03:00
|
|
|
* An LL(*) parser for the context-free grammar of the Doctrine Query Language.
|
2009-01-22 22:38:10 +03:00
|
|
|
* Parses a DQL query, reports any errors in it, and generates an AST.
|
2008-09-23 06:47:11 +04:00
|
|
|
*
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Janne Vanhala <jpvanhal@cc.hut.fi>
|
2009-01-22 22:38:10 +03:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2008-09-23 06:47:11 +04:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
2009-01-19 21:40:12 +03:00
|
|
|
* @link http://www.doctrine-project.org
|
2008-09-23 06:47:11 +04:00
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision$
|
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class Parser
|
2008-09-23 06:47:11 +04:00
|
|
|
{
|
2009-03-21 22:58:52 +03:00
|
|
|
const SCALAR_QUERYCOMPONENT_ALIAS = 'dctrn';
|
|
|
|
|
2009-03-23 20:39:33 +03:00
|
|
|
/** Maps registered string function names to class names. */
|
|
|
|
private static $_STRING_FUNCTIONS = array(
|
|
|
|
'concat' => 'Doctrine\ORM\Query\AST\Functions\ConcatFunction',
|
|
|
|
'substring' => 'Doctrine\ORM\Query\AST\Functions\SubstringFunction',
|
|
|
|
'trim' => 'Doctrine\ORM\Query\AST\Functions\TrimFunction',
|
|
|
|
'lower' => 'Doctrine\ORM\Query\AST\Functions\LowerFunction',
|
|
|
|
'upper' => 'Doctrine\ORM\Query\AST\Functions\UpperFunction'
|
|
|
|
);
|
|
|
|
|
|
|
|
/** Maps registered numeric function names to class names. */
|
|
|
|
private static $_NUMERIC_FUNCTIONS = array(
|
|
|
|
'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'
|
|
|
|
);
|
|
|
|
|
|
|
|
/** Maps registered datetime function names to class names. */
|
|
|
|
private static $_DATETIME_FUNCTIONS = array(
|
|
|
|
'current_date' => 'Doctrine\ORM\Query\AST\Functions\CurrentDateFunction',
|
|
|
|
'current_time' => 'Doctrine\ORM\Query\AST\Functions\CurrentTimeFunction',
|
|
|
|
'current_timestamp' => 'Doctrine\ORM\Query\AST\Functions\CurrentTimestampFunction'
|
|
|
|
);
|
|
|
|
|
2008-09-23 06:47:11 +04:00
|
|
|
/**
|
|
|
|
* The minimum number of tokens read after last detected error before
|
|
|
|
* another error can be reported.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
//const MIN_ERROR_DISTANCE = 2;
|
2008-09-23 06:47:11 +04:00
|
|
|
|
|
|
|
/**
|
2009-01-19 21:40:12 +03:00
|
|
|
* Path expressions that were encountered during parsing of SelectExpressions
|
|
|
|
* and still need to be validated.
|
2008-09-23 06:47:11 +04:00
|
|
|
*
|
2009-01-19 21:40:12 +03:00
|
|
|
* @var array
|
2008-09-23 06:47:11 +04:00
|
|
|
*/
|
2009-03-21 15:49:58 +03:00
|
|
|
private $_deferredPathExpressionStacks = array();
|
2008-09-23 06:47:11 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A scanner object.
|
|
|
|
*
|
|
|
|
* @var Doctrine_ORM_Query_Scanner
|
|
|
|
*/
|
2009-03-20 23:53:14 +03:00
|
|
|
private $_lexer;
|
2008-09-23 06:47:11 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Parser Result object.
|
|
|
|
*
|
|
|
|
* @var Doctrine_ORM_Query_ParserResult
|
|
|
|
*/
|
2009-03-20 23:53:14 +03:00
|
|
|
private $_parserResult;
|
2008-09-23 06:47:11 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The EntityManager.
|
|
|
|
*
|
|
|
|
* @var EnityManager
|
|
|
|
*/
|
2009-03-20 23:53:14 +03:00
|
|
|
private $_em;
|
2008-09-23 06:47:11 +04:00
|
|
|
|
2009-04-03 15:06:58 +04:00
|
|
|
/**
|
|
|
|
* The Query to parse.
|
|
|
|
*
|
|
|
|
* @var Query
|
|
|
|
*/
|
|
|
|
private $_query;
|
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
/**
|
|
|
|
* Map of declared classes in the parsed query.
|
|
|
|
* Maps the declared DQL alias (key) to the class name (value).
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_queryComponents = array();
|
2009-04-03 15:06:58 +04:00
|
|
|
|
2008-09-23 06:47:11 +04:00
|
|
|
/**
|
|
|
|
* Creates a new query parser object.
|
|
|
|
*
|
2009-04-03 15:06:58 +04:00
|
|
|
* @param Query $query The Query to parse.
|
2008-09-23 06:47:11 +04:00
|
|
|
*/
|
2009-04-03 15:06:58 +04:00
|
|
|
public function __construct(Query $query)
|
2008-09-23 06:47:11 +04:00
|
|
|
{
|
2009-04-03 15:06:58 +04:00
|
|
|
$this->_query = $query;
|
2008-09-23 06:47:11 +04:00
|
|
|
$this->_em = $query->getEntityManager();
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->_lexer = new Lexer($query->getDql());
|
2009-04-03 15:06:58 +04:00
|
|
|
$this->_parserResult = new ParserResult;
|
2008-09-23 06:47:11 +04:00
|
|
|
$this->_parserResult->setEntityManager($this->_em);
|
2009-04-03 15:06:58 +04:00
|
|
|
//$this->free(true);
|
2008-09-23 06:47:11 +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.
|
|
|
|
*
|
|
|
|
* @param int|string token type or value
|
|
|
|
* @return bool True, if tokens match; false otherwise.
|
|
|
|
*/
|
|
|
|
public function match($token)
|
|
|
|
{
|
|
|
|
if (is_string($token)) {
|
2009-01-22 22:38:10 +03:00
|
|
|
$isMatch = ($this->_lexer->lookahead['value'] === $token);
|
2008-09-23 06:47:11 +04:00
|
|
|
} else {
|
2009-01-22 22:38:10 +03:00
|
|
|
$isMatch = ($this->_lexer->lookahead['type'] === $token);
|
2008-09-23 06:47:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $isMatch) {
|
2009-03-14 13:30:47 +03:00
|
|
|
$this->syntaxError($this->_lexer->getLiteral($token));
|
2008-09-23 06:47:11 +04:00
|
|
|
}
|
|
|
|
|
2009-03-20 23:53:14 +03:00
|
|
|
$this->_lexer->moveNext();
|
2008-09-23 06:47:11 +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
|
|
|
|
*/
|
|
|
|
public function free($deep = false, $position = 0)
|
|
|
|
{
|
|
|
|
// WARNING! Use this method with care. It resets the scanner!
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->_lexer->resetPosition($position);
|
2008-09-23 06:47:11 +04:00
|
|
|
|
|
|
|
// Deep = true cleans peek and also any previously defined errors
|
|
|
|
if ($deep) {
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->_lexer->resetPeek();
|
|
|
|
//$this->_errors = array();
|
2008-09-23 06:47:11 +04:00
|
|
|
}
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->_lexer->token = null;
|
|
|
|
$this->_lexer->lookahead = null;
|
|
|
|
//$this->_errorDistance = self::MIN_ERROR_DISTANCE;
|
2008-09-23 06:47:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a query string.
|
|
|
|
*/
|
|
|
|
public function parse()
|
|
|
|
{
|
2009-01-19 21:40:12 +03:00
|
|
|
// Parse & build AST
|
|
|
|
$AST = $this->_QueryLanguage();
|
2009-01-21 21:25:05 +03:00
|
|
|
|
2008-09-23 06:47:11 +04:00
|
|
|
// Check for end of string
|
2009-01-22 22:38:10 +03:00
|
|
|
if ($this->_lexer->lookahead !== null) {
|
2009-03-23 22:02:16 +03:00
|
|
|
//var_dump($this->_lexer->lookahead);
|
2008-09-23 06:47:11 +04:00
|
|
|
$this->syntaxError('end of string');
|
|
|
|
}
|
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
// Create SqlWalker who creates the SQL from the AST
|
2009-04-09 22:12:48 +04:00
|
|
|
$sqlWalker = new SqlWalker($this->_query, $this->_parserResult, $this->_queryComponents);
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2009-03-23 22:02:16 +03:00
|
|
|
// Assign an SQL executor to the parser result
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->_parserResult->setSqlExecutor(Exec\AbstractExecutor::create($AST, $sqlWalker));
|
2008-09-23 06:47:11 +04:00
|
|
|
|
|
|
|
return $this->_parserResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-03 15:06:58 +04:00
|
|
|
* Gets the lexer used by the parser.
|
2008-09-23 06:47:11 +04:00
|
|
|
*
|
2009-04-03 15:06:58 +04:00
|
|
|
* @return Doctrine\ORM\Query\Lexer
|
2008-09-23 06:47:11 +04:00
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
public function getLexer()
|
2008-09-23 06:47:11 +04:00
|
|
|
{
|
2009-01-22 22:38:10 +03:00
|
|
|
return $this->_lexer;
|
2008-09-23 06:47:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-03 15:06:58 +04:00
|
|
|
* Gets the ParserResult that is being filled with information during parsing.
|
2008-09-23 06:47:11 +04:00
|
|
|
*
|
2009-04-03 15:06:58 +04:00
|
|
|
* @return Doctrine\ORM\Query\ParserResult
|
2008-09-23 06:47:11 +04:00
|
|
|
*/
|
|
|
|
public function getParserResult()
|
|
|
|
{
|
|
|
|
return $this->_parserResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a new syntax error.
|
|
|
|
*
|
|
|
|
* @param string $expected Optional expected string.
|
|
|
|
* @param array $token Optional token.
|
|
|
|
*/
|
|
|
|
public function syntaxError($expected = '', $token = null)
|
|
|
|
{
|
|
|
|
if ($token === null) {
|
2009-01-22 22:38:10 +03:00
|
|
|
$token = $this->_lexer->lookahead;
|
2008-09-23 06:47:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$message = 'line 0, col ' . (isset($token['position']) ? $token['position'] : '-1') . ': Error: ';
|
|
|
|
|
|
|
|
if ($expected !== '') {
|
|
|
|
$message .= "Expected '$expected', got ";
|
|
|
|
} else {
|
|
|
|
$message .= 'Unexpected ';
|
|
|
|
}
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
if ($this->_lexer->lookahead === null) {
|
2008-09-23 06:47:11 +04:00
|
|
|
$message .= 'end of string.';
|
|
|
|
} else {
|
2009-01-22 22:38:10 +03:00
|
|
|
$message .= "'{$this->_lexer->lookahead['value']}'";
|
2008-09-23 06:47:11 +04:00
|
|
|
}
|
|
|
|
|
2009-04-03 15:06:58 +04:00
|
|
|
throw DoctrineException::updateMe($message);
|
2008-09-23 06:47:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a new semantical error.
|
|
|
|
*
|
|
|
|
* @param string $message Optional message.
|
|
|
|
* @param array $token Optional token.
|
|
|
|
*/
|
|
|
|
public function semanticalError($message = '', $token = null)
|
|
|
|
{
|
|
|
|
if ($token === null) {
|
2009-01-22 22:38:10 +03:00
|
|
|
$token = $this->_lexer->token;
|
2008-09-23 06:47:11 +04:00
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
//TODO: Include $token in $message
|
2009-04-03 15:06:58 +04:00
|
|
|
throw DoctrineException::updateMe($message);
|
2008-09-23 06:47:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs new error entry.
|
|
|
|
*
|
|
|
|
* @param string $message Message to log.
|
|
|
|
* @param array $token Token that it was processing.
|
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
/*protected function _logError($message = '', $token)
|
2008-09-23 06:47:11 +04:00
|
|
|
{
|
|
|
|
if ($this->_errorDistance >= self::MIN_ERROR_DISTANCE) {
|
|
|
|
$message = 'line 0, col ' . $token['position'] . ': ' . $message;
|
|
|
|
$this->_errors[] = $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_errorDistance = 0;
|
2009-01-22 22:38:10 +03:00
|
|
|
}*/
|
2008-09-23 06:47:11 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the EntityManager used by the parser.
|
|
|
|
*
|
|
|
|
* @return EntityManager
|
|
|
|
*/
|
|
|
|
public function getEntityManager()
|
|
|
|
{
|
|
|
|
return $this->_em;
|
2009-03-21 22:58:52 +03:00
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
|
|
|
|
/**
|
2009-03-23 22:02:16 +03:00
|
|
|
* Checks if the next-next (after lookahead) token starts a function.
|
2009-01-19 21:40:12 +03:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private function _isFunction()
|
|
|
|
{
|
2009-01-22 22:38:10 +03:00
|
|
|
$next = $this->_lexer->glimpse();
|
2009-03-21 22:58:52 +03:00
|
|
|
return $next['value'] === '(';
|
2009-01-19 21:40:12 +03: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()
|
|
|
|
{
|
2009-01-22 22:38:10 +03:00
|
|
|
$la = $this->_lexer->lookahead;
|
|
|
|
$next = $this->_lexer->glimpse();
|
2009-03-14 13:30:47 +03:00
|
|
|
return ($la['value'] === '(' && $next['type'] === Lexer::T_SELECT);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* QueryLanguage ::= SelectStatement | UpdateStatement | DeleteStatement
|
|
|
|
*/
|
2009-04-03 15:06:58 +04:00
|
|
|
public function _QueryLanguage()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-03-20 23:53:14 +03:00
|
|
|
$this->_lexer->moveNext();
|
2009-01-22 22:38:10 +03:00
|
|
|
switch ($this->_lexer->lookahead['type']) {
|
2009-03-14 13:30:47 +03:00
|
|
|
case Lexer::T_SELECT:
|
2009-01-19 21:40:12 +03:00
|
|
|
return $this->_SelectStatement();
|
2009-03-14 13:30:47 +03:00
|
|
|
case Lexer::T_UPDATE:
|
2009-01-19 21:40:12 +03:00
|
|
|
return $this->_UpdateStatement();
|
2009-03-14 13:30:47 +03:00
|
|
|
case Lexer::T_DELETE:
|
2009-01-19 21:40:12 +03:00
|
|
|
return $this->_DeleteStatement();
|
|
|
|
default:
|
|
|
|
$this->syntaxError('SELECT, UPDATE or DELETE');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SelectStatement ::= SelectClause FromClause [WhereClause] [GroupByClause] [HavingClause] [OrderByClause]
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _SelectStatement()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-03-21 15:49:58 +03:00
|
|
|
$this->_beginDeferredPathExpressionStack();
|
2009-01-19 21:40:12 +03:00
|
|
|
$selectClause = $this->_SelectClause();
|
|
|
|
$fromClause = $this->_FromClause();
|
2009-03-21 15:49:58 +03:00
|
|
|
$this->_processDeferredPathExpressionStack();
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2009-03-14 13:30:47 +03:00
|
|
|
$whereClause = $this->_lexer->isNextToken(Lexer::T_WHERE) ?
|
2009-01-19 21:40:12 +03:00
|
|
|
$this->_WhereClause() : null;
|
|
|
|
|
2009-03-14 13:30:47 +03:00
|
|
|
$groupByClause = $this->_lexer->isNextToken(Lexer::T_GROUP) ?
|
2009-01-19 21:40:12 +03:00
|
|
|
$this->_GroupByClause() : null;
|
|
|
|
|
2009-03-14 13:30:47 +03:00
|
|
|
$havingClause = $this->_lexer->isNextToken(Lexer::T_HAVING) ?
|
2009-01-19 21:40:12 +03:00
|
|
|
$this->_HavingClause() : null;
|
|
|
|
|
2009-03-14 13:30:47 +03:00
|
|
|
$orderByClause = $this->_lexer->isNextToken(Lexer::T_ORDER) ?
|
2009-01-19 21:40:12 +03:00
|
|
|
$this->_OrderByClause() : null;
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
return new AST\SelectStatement(
|
2009-01-19 21:40:12 +03:00
|
|
|
$selectClause, $fromClause, $whereClause, $groupByClause, $havingClause, $orderByClause
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-03-21 15:49:58 +03:00
|
|
|
/**
|
2009-03-21 22:58:52 +03:00
|
|
|
* Begins a new stack of deferred path expressions.
|
2009-03-21 15:49:58 +03:00
|
|
|
*/
|
|
|
|
private function _beginDeferredPathExpressionStack()
|
|
|
|
{
|
|
|
|
$this->_deferredPathExpressionStacks[] = array();
|
|
|
|
}
|
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
/**
|
2009-03-21 22:58:52 +03:00
|
|
|
* Processes the topmost stack of deferred path expressions.
|
|
|
|
* These will be validated to make sure they are indeed
|
2009-01-19 21:40:12 +03:00
|
|
|
* valid <tt>StateFieldPathExpression</tt>s and additional information
|
|
|
|
* is attached to their AST nodes.
|
|
|
|
*/
|
2009-03-21 15:49:58 +03:00
|
|
|
private function _processDeferredPathExpressionStack()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-03-21 15:49:58 +03:00
|
|
|
$exprStack = array_pop($this->_deferredPathExpressionStacks);
|
2009-04-09 22:12:48 +04:00
|
|
|
$qComps = $this->_queryComponents;
|
2009-03-21 15:49:58 +03:00
|
|
|
foreach ($exprStack as $expr) {
|
2009-01-19 21:40:12 +03:00
|
|
|
$parts = $expr->getParts();
|
|
|
|
$numParts = count($parts);
|
|
|
|
$dqlAlias = $parts[0];
|
|
|
|
if (count($parts) == 2) {
|
|
|
|
$expr->setIsSimpleStateFieldPathExpression(true);
|
|
|
|
if ( ! $qComps[$dqlAlias]['metadata']->hasField($parts[1])) {
|
2009-05-14 14:03:09 +04:00
|
|
|
$this->semanticalError('The class ' . $qComps[$dqlAlias]['metadata']->name
|
2009-03-17 01:12:38 +03:00
|
|
|
. ' has no simple state field named ' . $parts[1]);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$embeddedClassFieldSeen = false;
|
|
|
|
$assocSeen = false;
|
|
|
|
for ($i = 1; $i < $numParts - 1; ++$i) {
|
|
|
|
if ($qComps[$dqlAlias]['metadata']->hasAssociation($parts[$i])) {
|
|
|
|
if ($embeddedClassFieldSeen) {
|
|
|
|
$this->semanticalError('Invalid navigation path.');
|
|
|
|
}
|
|
|
|
// Indirect join
|
|
|
|
$assoc = $qComps[$dqlAlias]['metadata']->getAssociationMapping($parts[$i]);
|
|
|
|
if ( ! $assoc->isOneToOne()) {
|
|
|
|
$this->semanticalError('Single-valued association expected.');
|
|
|
|
}
|
|
|
|
$expr->setIsSingleValuedAssociationPart($parts[$i]);
|
|
|
|
//TODO...
|
|
|
|
$assocSeen = true;
|
|
|
|
} else if ($qComps[$dqlAlias]['metadata']->hasEmbeddedClassField($parts[$i])) {
|
|
|
|
//TODO...
|
|
|
|
$expr->setIsEmbeddedClassPart($parts[$i]);
|
|
|
|
$this->syntaxError();
|
|
|
|
} else {
|
|
|
|
$this->syntaxError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( ! $assocSeen) {
|
|
|
|
$expr->setIsSimpleStateFieldPathExpression(true);
|
|
|
|
} else {
|
|
|
|
$expr->setIsSimpleStateFieldAssociationPathExpression(true);
|
|
|
|
}
|
|
|
|
// Last part MUST be a simple state field
|
|
|
|
if ( ! $qComps[$dqlAlias]['metadata']->hasField($parts[$numParts-1])) {
|
2009-05-14 14:03:09 +04:00
|
|
|
$this->semanticalError('The class ' . $qComps[$dqlAlias]['metadata']->name
|
2009-03-17 01:12:38 +03:00
|
|
|
. ' has no simple state field named ' . $parts[$numParts-1]);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-15 00:19:50 +03:00
|
|
|
/**
|
|
|
|
* UpdateStatement ::= UpdateClause [WhereClause]
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _UpdateStatement()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-03-19 15:43:48 +03:00
|
|
|
$updateStatement = new AST\UpdateStatement($this->_UpdateClause());
|
|
|
|
$updateStatement->setWhereClause(
|
|
|
|
$this->_lexer->isNextToken(Lexer::T_WHERE) ? $this->_WhereClause() : null
|
|
|
|
);
|
|
|
|
return $updateStatement;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UpdateClause ::= "UPDATE" AbstractSchemaName [["AS"] AliasIdentificationVariable] "SET" UpdateItem {"," UpdateItem}*
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _UpdateClause()
|
2009-03-19 15:43:48 +03:00
|
|
|
{
|
|
|
|
$this->match(Lexer::T_UPDATE);
|
|
|
|
$abstractSchemaName = $this->_AbstractSchemaName();
|
|
|
|
$aliasIdentificationVariable = null;
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_AS)) {
|
|
|
|
$this->match(Lexer::T_AS);
|
|
|
|
}
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_IDENTIFIER)) {
|
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
|
|
|
$aliasIdentificationVariable = $this->_lexer->token['value'];
|
2009-03-21 22:58:52 +03:00
|
|
|
} else {
|
|
|
|
$aliasIdentificationVariable = $abstractSchemaName;
|
2009-03-19 15:43:48 +03:00
|
|
|
}
|
|
|
|
$this->match(Lexer::T_SET);
|
|
|
|
$updateItems = array();
|
|
|
|
$updateItems[] = $this->_UpdateItem();
|
|
|
|
while ($this->_lexer->isNextToken(',')) {
|
|
|
|
$this->match(',');
|
|
|
|
$updateItems[] = $this->_UpdateItem();
|
|
|
|
}
|
|
|
|
|
2009-03-21 22:58:52 +03:00
|
|
|
$classMetadata = $this->_em->getClassMetadata($abstractSchemaName);
|
|
|
|
// Building queryComponent
|
|
|
|
$queryComponent = array(
|
|
|
|
'metadata' => $classMetadata,
|
|
|
|
'parent' => null,
|
|
|
|
'relation' => null,
|
|
|
|
'map' => null,
|
|
|
|
'scalar' => null,
|
|
|
|
);
|
2009-04-09 22:12:48 +04:00
|
|
|
$this->_queryComponents[$aliasIdentificationVariable] = $queryComponent;
|
2009-03-19 15:43:48 +03:00
|
|
|
|
|
|
|
$updateClause = new AST\UpdateClause($abstractSchemaName, $updateItems);
|
|
|
|
$updateClause->setAliasIdentificationVariable($aliasIdentificationVariable);
|
|
|
|
|
|
|
|
return $updateClause;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UpdateItem ::= [IdentificationVariable "."] {StateField | SingleValuedAssociationField} "=" NewValue
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _UpdateItem()
|
2009-03-19 15:43:48 +03:00
|
|
|
{
|
|
|
|
$peek = $this->_lexer->glimpse();
|
|
|
|
$identVariable = null;
|
|
|
|
if ($peek['value'] == '.') {
|
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
|
|
|
$identVariable = $this->_lexer->token['value'];
|
|
|
|
$this->match('.');
|
2009-03-21 22:58:52 +03:00
|
|
|
} else {
|
2009-04-09 22:12:48 +04:00
|
|
|
//$identVariable = $this->_parserResult->getDefaultQueryComponentAlias();
|
|
|
|
throw new DoctrineException("Missing alias qualifier.");
|
2009-03-19 15:43:48 +03:00
|
|
|
}
|
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
|
|
|
$field = $this->_lexer->token['value'];
|
|
|
|
$this->match('=');
|
|
|
|
$newValue = $this->_NewValue();
|
|
|
|
|
|
|
|
$updateItem = new AST\UpdateItem($field, $newValue);
|
|
|
|
$updateItem->setIdentificationVariable($identVariable);
|
|
|
|
|
|
|
|
return $updateItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NewValue ::= SimpleArithmeticExpression | StringPrimary | DatetimePrimary | BooleanPrimary |
|
|
|
|
* EnumPrimary | SimpleEntityExpression | "NULL"
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _NewValue()
|
2009-03-19 15:43:48 +03:00
|
|
|
{
|
|
|
|
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']);
|
|
|
|
} else if ($this->_lexer->isNextToken(Lexer::T_STRING)) {
|
|
|
|
//TODO: Can be StringPrimary or EnumPrimary
|
|
|
|
return $this->_StringPrimary();
|
|
|
|
} else {
|
2009-03-21 15:49:58 +03:00
|
|
|
$this->syntaxError('Not yet implemented-1.');
|
2009-03-19 15:43:48 +03:00
|
|
|
//echo "UH OH ...";
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
2009-03-15 00:19:50 +03:00
|
|
|
/**
|
|
|
|
* DeleteStatement ::= DeleteClause [WhereClause]
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _DeleteStatement()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-03-19 15:43:48 +03:00
|
|
|
$deleteStatement = new AST\DeleteStatement($this->_DeleteClause());
|
|
|
|
$deleteStatement->setWhereClause(
|
|
|
|
$this->_lexer->isNextToken(Lexer::T_WHERE) ? $this->_WhereClause() : null
|
|
|
|
);
|
|
|
|
return $deleteStatement;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DeleteClause ::= "DELETE" ["FROM"] AbstractSchemaName [["AS"] AliasIdentificationVariable]
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _DeleteClause()
|
2009-03-19 15:43:48 +03:00
|
|
|
{
|
|
|
|
$this->match(Lexer::T_DELETE);
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_FROM)) {
|
|
|
|
$this->match(Lexer::T_FROM);
|
|
|
|
}
|
|
|
|
$deleteClause = new AST\DeleteClause($this->_AbstractSchemaName());
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_AS)) {
|
|
|
|
$this->match(Lexer::T_AS);
|
|
|
|
}
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_IDENTIFIER)) {
|
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
|
|
|
$deleteClause->setAliasIdentificationVariable($this->_lexer->token['value']);
|
2009-03-21 22:58:52 +03:00
|
|
|
} else {
|
|
|
|
$deleteClause->setAliasIdentificationVariable($deleteClause->getAbstractSchemaName());
|
2009-03-19 15:43:48 +03:00
|
|
|
}
|
2009-03-21 22:58:52 +03:00
|
|
|
|
|
|
|
$classMetadata = $this->_em->getClassMetadata($deleteClause->getAbstractSchemaName());
|
|
|
|
$queryComponent = array(
|
|
|
|
'metadata' => $classMetadata,
|
|
|
|
'parent' => null,
|
|
|
|
'relation' => null,
|
|
|
|
'map' => null,
|
|
|
|
'scalar' => null,
|
|
|
|
);
|
2009-04-09 22:12:48 +04:00
|
|
|
$this->_queryComponents[$deleteClause->getAliasIdentificationVariable()] = $queryComponent;
|
|
|
|
//$this->_parserResult->setDefaultQueryComponentAlias($deleteClause->getAliasIdentificationVariable());
|
|
|
|
//$this->_declaredClasses[$deleteClause->getAliasIdentificationVariable()] = $classMetadata;
|
2009-03-19 15:43:48 +03:00
|
|
|
return $deleteClause;
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SelectClause ::= "SELECT" ["DISTINCT"] SelectExpression {"," SelectExpression}
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _SelectClause()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
|
|
|
$isDistinct = false;
|
2009-03-14 13:30:47 +03:00
|
|
|
$this->match(Lexer::T_SELECT);
|
2009-01-19 21:40:12 +03:00
|
|
|
|
|
|
|
// Inspecting if we are in a DISTINCT query
|
2009-03-14 13:30:47 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_DISTINCT)) {
|
|
|
|
$this->match(Lexer::T_DISTINCT);
|
2009-01-19 21:40:12 +03:00
|
|
|
$isDistinct = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process SelectExpressions (1..N)
|
|
|
|
$selectExpressions = array();
|
|
|
|
$selectExpressions[] = $this->_SelectExpression();
|
2009-01-22 22:38:10 +03:00
|
|
|
while ($this->_lexer->isNextToken(',')) {
|
2009-01-19 21:40:12 +03:00
|
|
|
$this->match(',');
|
|
|
|
$selectExpressions[] = $this->_SelectExpression();
|
|
|
|
}
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
return new AST\SelectClause($selectExpressions, $isDistinct);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FromClause ::= "FROM" IdentificationVariableDeclaration {"," IdentificationVariableDeclaration}
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _FromClause()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-03-14 13:30:47 +03:00
|
|
|
$this->match(Lexer::T_FROM);
|
2009-01-19 21:40:12 +03:00
|
|
|
$identificationVariableDeclarations = array();
|
|
|
|
$identificationVariableDeclarations[] = $this->_IdentificationVariableDeclaration();
|
2009-03-28 23:59:07 +03:00
|
|
|
|
|
|
|
$firstRangeDecl = $identificationVariableDeclarations[0]->getRangeVariableDeclaration();
|
2009-04-09 22:12:48 +04:00
|
|
|
/*if ($firstRangeDecl->getAliasIdentificationVariable()) {
|
2009-03-28 23:59:07 +03:00
|
|
|
$this->_parserResult->setDefaultQueryComponentAlias($firstRangeDecl->getAliasIdentificationVariable());
|
|
|
|
} else {
|
|
|
|
$this->_parserResult->setDefaultQueryComponentAlias($firstRangeDecl->getAbstractSchemaName());
|
2009-04-09 22:12:48 +04:00
|
|
|
}*/
|
2009-03-28 23:59:07 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
while ($this->_lexer->isNextToken(',')) {
|
2009-01-19 21:40:12 +03:00
|
|
|
$this->match(',');
|
|
|
|
$identificationVariableDeclarations[] = $this->_IdentificationVariableDeclaration();
|
|
|
|
}
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
return new AST\FromClause($identificationVariableDeclarations);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SelectExpression ::=
|
|
|
|
* IdentificationVariable | StateFieldPathExpression |
|
2009-03-28 20:10:41 +03:00
|
|
|
* (AggregateExpression | "(" Subselect ")") [["AS"] FieldAliasIdentificationVariable] |
|
|
|
|
* Function
|
2009-01-19 21:40:12 +03:00
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _SelectExpression()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
|
|
|
$expression = null;
|
|
|
|
$fieldIdentificationVariable = null;
|
2009-01-22 22:38:10 +03:00
|
|
|
$peek = $this->_lexer->glimpse();
|
2009-01-19 21:40:12 +03:00
|
|
|
// First we recognize for an IdentificationVariable (DQL class alias)
|
2009-03-28 20:10:41 +03:00
|
|
|
if ($peek['value'] != '.' && $peek['value'] != '(' && $this->_lexer->lookahead['type'] === Lexer::T_IDENTIFIER) {
|
2009-01-19 21:40:12 +03:00
|
|
|
$expression = $this->_IdentificationVariable();
|
|
|
|
} else if (($isFunction = $this->_isFunction()) !== false || $this->_isSubselect()) {
|
2009-03-19 15:43:48 +03:00
|
|
|
if ($isFunction) {
|
2009-03-28 20:10:41 +03:00
|
|
|
if ($this->_isAggregateFunction($this->_lexer->lookahead['type'])) {
|
|
|
|
$expression = $this->_AggregateExpression();
|
|
|
|
} else {
|
|
|
|
$expression = $this->_Function();
|
|
|
|
}
|
2009-03-19 15:43:48 +03:00
|
|
|
} else {
|
|
|
|
$this->match('(');
|
|
|
|
$expression = $this->_Subselect();
|
|
|
|
$this->match(')');
|
|
|
|
}
|
2009-03-14 13:30:47 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_AS)) {
|
|
|
|
$this->match(Lexer::T_AS);
|
2009-03-19 15:43:48 +03:00
|
|
|
}
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_IDENTIFIER)) {
|
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
|
|
|
$fieldIdentificationVariable = $this->_lexer->token['value'];
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
} else {
|
2009-04-03 15:06:58 +04:00
|
|
|
//TODO: If hydration mode is OBJECT throw an exception ("partial object dangerous...")
|
|
|
|
// unless the doctrine.forcePartialLoad query hint is set
|
2009-03-21 15:49:58 +03:00
|
|
|
$expression = $this->_StateFieldPathExpression();
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
return new AST\SelectExpression($expression, $fieldIdentificationVariable);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IdentificationVariable ::= identifier
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _IdentificationVariable()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-03-14 13:30:47 +03:00
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
2009-01-22 22:38:10 +03:00
|
|
|
return $this->_lexer->token['value'];
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IdentificationVariableDeclaration ::= RangeVariableDeclaration [IndexBy] {JoinVariableDeclaration}*
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _IdentificationVariableDeclaration()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
|
|
|
$rangeVariableDeclaration = $this->_RangeVariableDeclaration();
|
2009-03-14 13:30:47 +03:00
|
|
|
$indexBy = $this->_lexer->isNextToken(Lexer::T_INDEX) ?
|
2009-01-19 21:40:12 +03:00
|
|
|
$this->_IndexBy() : null;
|
|
|
|
$joinVariableDeclarations = array();
|
|
|
|
while (
|
2009-03-14 13:30:47 +03:00
|
|
|
$this->_lexer->isNextToken(Lexer::T_LEFT) ||
|
|
|
|
$this->_lexer->isNextToken(Lexer::T_INNER) ||
|
|
|
|
$this->_lexer->isNextToken(Lexer::T_JOIN)
|
2009-01-19 21:40:12 +03:00
|
|
|
) {
|
|
|
|
$joinVariableDeclarations[] = $this->_JoinVariableDeclaration();
|
|
|
|
}
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
return new AST\IdentificationVariableDeclaration(
|
2009-01-19 21:40:12 +03:00
|
|
|
$rangeVariableDeclaration, $indexBy, $joinVariableDeclarations
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RangeVariableDeclaration ::= AbstractSchemaName ["AS"] AliasIdentificationVariable
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _RangeVariableDeclaration()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
|
|
|
$abstractSchemaName = $this->_AbstractSchemaName();
|
|
|
|
|
2009-03-14 13:30:47 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_AS)) {
|
|
|
|
$this->match(Lexer::T_AS);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
$aliasIdentificationVariable = $this->_AliasIdentificationVariable();
|
|
|
|
$classMetadata = $this->_em->getClassMetadata($abstractSchemaName);
|
|
|
|
|
|
|
|
// Building queryComponent
|
|
|
|
$queryComponent = array(
|
|
|
|
'metadata' => $classMetadata,
|
|
|
|
'parent' => null,
|
|
|
|
'relation' => null,
|
|
|
|
'map' => null,
|
|
|
|
'scalar' => null,
|
|
|
|
);
|
2009-04-09 22:12:48 +04:00
|
|
|
$this->_queryComponents[$aliasIdentificationVariable] = $queryComponent;
|
|
|
|
//$this->_declaredClasses[$aliasIdentificationVariable] = $classMetadata;
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
return new AST\RangeVariableDeclaration(
|
2009-01-19 21:40:12 +03:00
|
|
|
$classMetadata, $aliasIdentificationVariable
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AbstractSchemaName ::= identifier
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _AbstractSchemaName()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-03-14 13:30:47 +03:00
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
2009-01-22 22:38:10 +03:00
|
|
|
return $this->_lexer->token['value'];
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AliasIdentificationVariable = identifier
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _AliasIdentificationVariable()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-03-14 13:30:47 +03:00
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
2009-01-22 22:38:10 +03:00
|
|
|
return $this->_lexer->token['value'];
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* JoinVariableDeclaration ::= Join [IndexBy]
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _JoinVariableDeclaration()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
|
|
|
$join = $this->_Join();
|
2009-03-14 13:30:47 +03:00
|
|
|
$indexBy = $this->_lexer->isNextToken(Lexer::T_INDEX) ?
|
2009-01-19 21:40:12 +03:00
|
|
|
$this->_IndexBy() : null;
|
2009-01-22 22:38:10 +03:00
|
|
|
return new AST\JoinVariableDeclaration($join, $indexBy);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Join ::= ["LEFT" ["OUTER"] | "INNER"] "JOIN" JoinAssociationPathExpression
|
|
|
|
* ["AS"] AliasIdentificationVariable [("ON" | "WITH") ConditionalExpression]
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _Join()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
|
|
|
// Check Join type
|
2009-01-22 22:38:10 +03:00
|
|
|
$joinType = AST\Join::JOIN_TYPE_INNER;
|
2009-03-14 13:30:47 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_LEFT)) {
|
|
|
|
$this->match(Lexer::T_LEFT);
|
2009-01-19 21:40:12 +03:00
|
|
|
// Possible LEFT OUTER join
|
2009-03-14 13:30:47 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_OUTER)) {
|
|
|
|
$this->match(Lexer::T_OUTER);
|
2009-01-22 22:38:10 +03:00
|
|
|
$joinType = AST\Join::JOIN_TYPE_LEFTOUTER;
|
2009-01-19 21:40:12 +03:00
|
|
|
} else {
|
2009-01-22 22:38:10 +03:00
|
|
|
$joinType = AST\Join::JOIN_TYPE_LEFT;
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2009-03-14 13:30:47 +03:00
|
|
|
} else if ($this->_lexer->isNextToken(Lexer::T_INNER)) {
|
|
|
|
$this->match(Lexer::T_INNER);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
2009-03-14 13:30:47 +03:00
|
|
|
$this->match(Lexer::T_JOIN);
|
2009-01-19 21:40:12 +03:00
|
|
|
|
|
|
|
$joinPathExpression = $this->_JoinPathExpression();
|
2009-03-14 13:30:47 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_AS)) {
|
|
|
|
$this->match(Lexer::T_AS);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$aliasIdentificationVariable = $this->_AliasIdentificationVariable();
|
|
|
|
|
|
|
|
// Verify that the association exists, if yes update the ParserResult
|
|
|
|
// with the new component.
|
2009-04-09 22:12:48 +04:00
|
|
|
//$parentComp = $this->_parserResult->getQueryComponent($joinPathExpression->getIdentificationVariable());
|
|
|
|
$parentClass = $this->_queryComponents[$joinPathExpression->getIdentificationVariable()]['metadata'];
|
2009-01-19 21:40:12 +03:00
|
|
|
$assocField = $joinPathExpression->getAssociationField();
|
|
|
|
if ( ! $parentClass->hasAssociation($assocField)) {
|
2009-05-14 14:03:09 +04:00
|
|
|
$this->semanticalError("Class " . $parentClass->name .
|
2009-01-19 21:40:12 +03:00
|
|
|
" has no association named '$assocField'.");
|
|
|
|
}
|
|
|
|
$targetClassName = $parentClass->getAssociationMapping($assocField)->getTargetEntityName();
|
|
|
|
|
|
|
|
// Building queryComponent
|
|
|
|
$joinQueryComponent = array(
|
|
|
|
'metadata' => $this->_em->getClassMetadata($targetClassName),
|
|
|
|
'parent' => $joinPathExpression->getIdentificationVariable(),
|
|
|
|
'relation' => $parentClass->getAssociationMapping($assocField),
|
|
|
|
'map' => null,
|
|
|
|
'scalar' => null,
|
|
|
|
);
|
2009-04-09 22:12:48 +04:00
|
|
|
$this->_queryComponents[$aliasIdentificationVariable] = $joinQueryComponent;
|
|
|
|
//$this->_declaredClasses[$aliasIdentificationVariable] = $this->_em->getClassMetadata($targetClassName);
|
2009-01-19 21:40:12 +03:00
|
|
|
|
|
|
|
// Create AST node
|
2009-01-22 22:38:10 +03:00
|
|
|
$join = new AST\Join($joinType, $joinPathExpression, $aliasIdentificationVariable);
|
2009-01-19 21:40:12 +03:00
|
|
|
|
|
|
|
// Check Join where type
|
2009-03-15 00:19:50 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_ON) || $this->_lexer->isNextToken(Lexer::T_WITH)) {
|
2009-03-14 13:30:47 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_ON)) {
|
|
|
|
$this->match(Lexer::T_ON);
|
2009-01-22 22:38:10 +03:00
|
|
|
$join->setWhereType(AST\Join::JOIN_WHERE_ON);
|
2009-01-19 21:40:12 +03:00
|
|
|
} else {
|
2009-03-14 13:30:47 +03:00
|
|
|
$this->match(Lexer::T_WITH);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
$join->setConditionalExpression($this->_ConditionalExpression());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $join;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* JoinPathExpression ::= IdentificationVariable "." (CollectionValuedAssociationField | SingleValuedAssociationField)
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _JoinPathExpression()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
|
|
|
$identificationVariable = $this->_IdentificationVariable();
|
|
|
|
$this->match('.');
|
2009-03-14 13:30:47 +03:00
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
2009-01-22 22:38:10 +03:00
|
|
|
$assocField = $this->_lexer->token['value'];
|
|
|
|
return new AST\JoinPathExpression(
|
2009-01-19 21:40:12 +03:00
|
|
|
$identificationVariable, $assocField
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IndexBy ::= "INDEX" "BY" SimpleStateFieldPathExpression
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _IndexBy()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-03-14 13:30:47 +03:00
|
|
|
$this->match(Lexer::T_INDEX);
|
|
|
|
$this->match(Lexer::T_BY);
|
2009-01-19 21:40:12 +03:00
|
|
|
$pathExp = $this->_SimpleStateFieldPathExpression();
|
|
|
|
// Add the INDEX BY info to the query component
|
2009-04-09 22:12:48 +04:00
|
|
|
$this->_queryComponents[$pathExp->getIdentificationVariable()]['map'] = $pathExp->getSimpleStateField();
|
2009-01-19 21:40:12 +03:00
|
|
|
return $pathExp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SimpleStateFieldPathExpression ::= IdentificationVariable "." StateField
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _SimpleStateFieldPathExpression()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
|
|
|
$identificationVariable = $this->_IdentificationVariable();
|
|
|
|
$this->match('.');
|
2009-03-14 13:30:47 +03:00
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
2009-01-22 22:38:10 +03:00
|
|
|
$simpleStateField = $this->_lexer->token['value'];
|
|
|
|
return new AST\SimpleStateFieldPathExpression($identificationVariable, $simpleStateField);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
/**
|
|
|
|
* StateFieldPathExpression ::= SimpleStateFieldPathExpression | SimpleStateFieldAssociationPathExpression
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _StateFieldPathExpression()
|
2009-01-20 20:07:07 +03:00
|
|
|
{
|
2009-03-21 15:49:58 +03:00
|
|
|
if ( ! empty($this->_deferredPathExpressionStacks)) {
|
|
|
|
$exprStack = array_pop($this->_deferredPathExpressionStacks);
|
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
|
|
|
$parts = array($this->_lexer->token['value']);
|
|
|
|
while ($this->_lexer->isNextToken('.')) {
|
|
|
|
$this->match('.');
|
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
|
|
|
$parts[] = $this->_lexer->token['value'];
|
|
|
|
}
|
|
|
|
$expr = new AST\StateFieldPathExpression($parts);
|
|
|
|
$exprStack[] = $expr;
|
|
|
|
array_push($this->_deferredPathExpressionStacks, $exprStack);
|
|
|
|
return $expr; // EARLY EXIT!
|
|
|
|
}
|
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
$parts = array();
|
|
|
|
$stateFieldSeen = false;
|
|
|
|
$assocSeen = false;
|
|
|
|
|
|
|
|
$identificationVariable = $this->_IdentificationVariable();
|
2009-04-09 22:12:48 +04:00
|
|
|
if ( ! isset($this->_queryComponents[$identificationVariable])) {
|
2009-01-20 20:07:07 +03:00
|
|
|
$this->syntaxError("Identification variable.");
|
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
$qComp = $this->_queryComponents[$identificationVariable];
|
2009-01-20 20:07:07 +03:00
|
|
|
$parts[] = $identificationVariable;
|
|
|
|
|
|
|
|
$class = $qComp['metadata'];
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
if ( ! $this->_lexer->isNextToken('.')) {
|
2009-01-20 20:07:07 +03:00
|
|
|
$this->syntaxError();
|
|
|
|
}
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
while ($this->_lexer->isNextToken('.')) {
|
2009-01-20 20:07:07 +03:00
|
|
|
if ($stateFieldSeen) $this->syntaxError();
|
|
|
|
$this->match('.');
|
|
|
|
$part = $this->_IdentificationVariable();
|
|
|
|
if ($class->hasField($part)) {
|
|
|
|
$stateFieldSeen = true;
|
|
|
|
} else if ($class->hasAssociation($part)) {
|
|
|
|
$assoc = $class->getAssociationMapping($part);
|
|
|
|
$class = $this->_em->getClassMetadata($assoc->getTargetEntityName());
|
|
|
|
$assocSeen = true;
|
|
|
|
} else {
|
2009-05-14 14:03:09 +04:00
|
|
|
$this->semanticalError('The class ' . $class->name .
|
2009-03-17 01:12:38 +03:00
|
|
|
' has no field or association named ' . $part);
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
|
|
|
$parts[] = $part;
|
|
|
|
}
|
|
|
|
|
2009-03-21 15:49:58 +03:00
|
|
|
$pathExpr = new AST\StateFieldPathExpression($parts);
|
2009-01-20 20:07:07 +03:00
|
|
|
|
|
|
|
if ($assocSeen) {
|
|
|
|
$pathExpr->setIsSimpleStateFieldAssociationPathExpression(true);
|
|
|
|
} else {
|
|
|
|
$pathExpr->setIsSimpleStateFieldPathExpression(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pathExpr;
|
|
|
|
}
|
|
|
|
|
2009-03-21 15:49:58 +03:00
|
|
|
/**
|
|
|
|
* NullComparisonExpression ::= (SingleValuedPathExpression | InputParameter) "IS" ["NOT"] "NULL"
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _NullComparisonExpression()
|
2009-03-21 15:49:58 +03:00
|
|
|
{
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_INPUT_PARAMETER)) {
|
|
|
|
$this->match(Lexer::T_INPUT_PARAMETER);
|
|
|
|
$expr = new AST\InputParameter($this->_lexer->token['value']);
|
|
|
|
} else {
|
|
|
|
//TODO: Support SingleValuedAssociationPathExpression
|
|
|
|
$expr = $this->_StateFieldPathExpression();
|
|
|
|
}
|
|
|
|
$nullCompExpr = new AST\NullComparisonExpression($expr);
|
|
|
|
$this->match(Lexer::T_IS);
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_NOT)) {
|
|
|
|
$this->match(Lexer::T_NOT);
|
|
|
|
$nullCompExpr->setNot(true);
|
|
|
|
}
|
|
|
|
$this->match(Lexer::T_NULL);
|
|
|
|
return $nullCompExpr;
|
|
|
|
}
|
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
/**
|
|
|
|
* AggregateExpression ::=
|
|
|
|
* ("AVG" | "MAX" | "MIN" | "SUM") "(" ["DISTINCT"] StateFieldPathExpression ")" |
|
|
|
|
* "COUNT" "(" ["DISTINCT"] (IdentificationVariable | SingleValuedAssociationPathExpression | StateFieldPathExpression) ")"
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _AggregateExpression()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
|
|
|
$isDistinct = false;
|
|
|
|
$functionName = '';
|
2009-03-14 13:30:47 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_COUNT)) {
|
|
|
|
$this->match(Lexer::T_COUNT);
|
2009-01-22 22:38:10 +03:00
|
|
|
$functionName = $this->_lexer->token['value'];
|
2009-01-19 21:40:12 +03:00
|
|
|
$this->match('(');
|
2009-03-14 13:30:47 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_DISTINCT)) {
|
|
|
|
$this->match(Lexer::T_DISTINCT);
|
2009-01-19 21:40:12 +03:00
|
|
|
$isDistinct = true;
|
|
|
|
}
|
|
|
|
// For now we only support a PathExpression here...
|
2009-03-21 15:49:58 +03:00
|
|
|
$pathExp = $this->_StateFieldPathExpression();
|
2009-01-19 21:40:12 +03:00
|
|
|
$this->match(')');
|
2009-03-19 15:43:48 +03: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-01-22 22:38:10 +03:00
|
|
|
$functionName = $this->_lexer->token['value'];
|
2009-01-19 21:40:12 +03:00
|
|
|
$this->match('(');
|
2009-03-19 15:43:48 +03:00
|
|
|
$pathExp = $this->_StateFieldPathExpression();
|
|
|
|
$this->match(')');
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
return new AST\AggregateExpression($functionName, $pathExp, $isDistinct);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GroupByClause ::= "GROUP" "BY" GroupByItem {"," GroupByItem}*
|
|
|
|
* GroupByItem ::= SingleValuedPathExpression
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _GroupByClause()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-03-14 13:30:47 +03:00
|
|
|
$this->match(Lexer::T_GROUP);
|
|
|
|
$this->match(Lexer::T_BY);
|
2009-01-19 21:40:12 +03:00
|
|
|
$groupByItems = array();
|
2009-03-21 15:49:58 +03:00
|
|
|
$groupByItems[] = $this->_StateFieldPathExpression();
|
2009-01-22 22:38:10 +03:00
|
|
|
while ($this->_lexer->isNextToken(',')) {
|
2009-01-19 21:40:12 +03:00
|
|
|
$this->match(',');
|
2009-03-21 15:49:58 +03:00
|
|
|
$groupByItems[] = $this->_StateFieldPathExpression();
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
return new AST\GroupByClause($groupByItems);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
2009-03-19 15:43:48 +03:00
|
|
|
/**
|
|
|
|
* HavingClause ::= "HAVING" ConditionalExpression
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _HavingClause()
|
2009-03-19 15:43:48 +03:00
|
|
|
{
|
|
|
|
$this->match(Lexer::T_HAVING);
|
|
|
|
return new AST\HavingClause($this->_ConditionalExpression());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OrderByClause ::= "ORDER" "BY" OrderByItem {"," OrderByItem}*
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _OrderByClause()
|
2009-03-19 15:43:48 +03:00
|
|
|
{
|
|
|
|
$this->match(Lexer::T_ORDER);
|
|
|
|
$this->match(Lexer::T_BY);
|
|
|
|
$orderByItems = array();
|
|
|
|
$orderByItems[] = $this->_OrderByItem();
|
|
|
|
while ($this->_lexer->isNextToken(',')) {
|
|
|
|
$this->match(',');
|
|
|
|
$orderByItems[] = $this->_OrderByItem();
|
|
|
|
}
|
|
|
|
return new AST\OrderByClause($orderByItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OrderByItem ::= StateFieldPathExpression ["ASC" | "DESC"]
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _OrderByItem()
|
2009-03-19 15:43:48 +03:00
|
|
|
{
|
|
|
|
$item = new AST\OrderByItem($this->_StateFieldPathExpression());
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_ASC)) {
|
|
|
|
$this->match(Lexer::T_ASC);
|
|
|
|
$item->setAsc(true);
|
|
|
|
} else if ($this->_lexer->isNextToken(Lexer::T_DESC)) {
|
|
|
|
$this->match(Lexer::T_DESC);
|
|
|
|
$item->setDesc(true);
|
|
|
|
} else {
|
|
|
|
$item->setDesc(true);
|
|
|
|
}
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
2009-01-19 21:40:12 +03:00
|
|
|
/**
|
|
|
|
* WhereClause ::= "WHERE" ConditionalExpression
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _WhereClause()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-03-14 13:30:47 +03:00
|
|
|
$this->match(Lexer::T_WHERE);
|
2009-01-22 22:38:10 +03:00
|
|
|
return new AST\WhereClause($this->_ConditionalExpression());
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ConditionalExpression ::= ConditionalTerm {"OR" ConditionalTerm}*
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _ConditionalExpression()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
|
|
|
$conditionalTerms = array();
|
|
|
|
$conditionalTerms[] = $this->_ConditionalTerm();
|
2009-03-14 13:30:47 +03:00
|
|
|
while ($this->_lexer->isNextToken(Lexer::T_OR)) {
|
|
|
|
$this->match(Lexer::T_OR);
|
2009-01-19 21:40:12 +03:00
|
|
|
$conditionalTerms[] = $this->_ConditionalTerm();
|
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
return new AST\ConditionalExpression($conditionalTerms);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ConditionalTerm ::= ConditionalFactor {"AND" ConditionalFactor}*
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _ConditionalTerm()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
|
|
|
$conditionalFactors = array();
|
|
|
|
$conditionalFactors[] = $this->_ConditionalFactor();
|
2009-03-14 13:30:47 +03:00
|
|
|
while ($this->_lexer->isNextToken(Lexer::T_AND)) {
|
|
|
|
$this->match(Lexer::T_AND);
|
2009-01-19 21:40:12 +03:00
|
|
|
$conditionalFactors[] = $this->_ConditionalFactor();
|
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
return new AST\ConditionalTerm($conditionalFactors);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ConditionalFactor ::= ["NOT"] ConditionalPrimary
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _ConditionalFactor()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
|
|
|
$not = false;
|
2009-03-14 13:30:47 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_NOT)) {
|
|
|
|
$this->match(Lexer::T_NOT);
|
2009-01-19 21:40:12 +03:00
|
|
|
$not = true;
|
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
return new AST\ConditionalFactor($this->_ConditionalPrimary(), $not);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ConditionalPrimary ::= SimpleConditionalExpression | "(" ConditionalExpression ")"
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _ConditionalPrimary()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-01-22 22:38:10 +03:00
|
|
|
$condPrimary = new AST\ConditionalPrimary;
|
|
|
|
if ($this->_lexer->isNextToken('(')) {
|
2009-01-21 21:25:05 +03:00
|
|
|
$numUnmatched = 1;
|
2009-01-22 22:38:10 +03:00
|
|
|
$peek = $this->_lexer->peek();
|
2009-01-21 21:25:05 +03:00
|
|
|
while ($numUnmatched > 0) {
|
|
|
|
if ($peek['value'] == ')') {
|
|
|
|
--$numUnmatched;
|
|
|
|
} else if ($peek['value'] == '(') {
|
|
|
|
++$numUnmatched;
|
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
$peek = $this->_lexer->peek();
|
2009-01-21 21:25:05 +03:00
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->_lexer->resetPeek();
|
2009-01-21 21:25:05 +03:00
|
|
|
|
|
|
|
//TODO: This is not complete, what about LIKE/BETWEEN/...etc?
|
|
|
|
$comparisonOps = array("=", "<", "<=", "<>", ">", ">=", "!=");
|
|
|
|
|
|
|
|
if (in_array($peek['value'], $comparisonOps)) {
|
|
|
|
$condPrimary->setSimpleConditionalExpression($this->_SimpleConditionalExpression());
|
|
|
|
} else {
|
|
|
|
$this->match('(');
|
|
|
|
$conditionalExpression = $this->_ConditionalExpression();
|
|
|
|
$this->match(')');
|
|
|
|
$condPrimary->setConditionalExpression($conditionalExpression);
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
} else {
|
|
|
|
$condPrimary->setSimpleConditionalExpression($this->_SimpleConditionalExpression());
|
|
|
|
}
|
|
|
|
return $condPrimary;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-20 00:16:46 +03:00
|
|
|
* SimpleConditionalExpression ::=
|
|
|
|
* ComparisonExpression | BetweenExpression | LikeExpression |
|
|
|
|
* InExpression | NullComparisonExpression | ExistsExpression |
|
|
|
|
* EmptyCollectionComparisonExpression | CollectionMemberExpression
|
2009-01-19 21:40:12 +03:00
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _SimpleConditionalExpression()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-03-14 13:30:47 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_NOT)) {
|
2009-01-22 22:38:10 +03:00
|
|
|
$token = $this->_lexer->glimpse();
|
2009-01-20 20:07:07 +03:00
|
|
|
} else {
|
2009-01-22 22:38:10 +03:00
|
|
|
$token = $this->_lexer->lookahead;
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
2009-03-14 13:30:47 +03:00
|
|
|
if ($token['type'] === Lexer::T_EXISTS) {
|
2009-01-19 21:40:12 +03:00
|
|
|
return $this->_ExistsExpression();
|
|
|
|
}
|
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
$stateFieldPathExpr = false;
|
2009-03-14 13:30:47 +03:00
|
|
|
if ($token['type'] === Lexer::T_IDENTIFIER) {
|
2009-01-20 20:07:07 +03:00
|
|
|
// Peek beyond the PathExpression
|
|
|
|
$stateFieldPathExpr = true;
|
2009-01-22 22:38:10 +03:00
|
|
|
$peek = $this->_lexer->peek();
|
2009-01-20 20:07:07 +03:00
|
|
|
while ($peek['value'] === '.') {
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->_lexer->peek();
|
|
|
|
$peek = $this->_lexer->peek();
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
2009-03-15 00:19:50 +03:00
|
|
|
|
|
|
|
// Also peek beyond a NOT if there is one
|
|
|
|
if ($peek['type'] === Lexer::T_NOT) {
|
|
|
|
$peek = $this->_lexer->peek();
|
|
|
|
}
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->_lexer->resetPeek();
|
2009-01-20 20:07:07 +03:00
|
|
|
$token = $peek;
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
if ($stateFieldPathExpr) {
|
|
|
|
switch ($token['type']) {
|
2009-03-14 13:30:47 +03:00
|
|
|
case Lexer::T_BETWEEN:
|
2009-01-20 20:07:07 +03:00
|
|
|
return $this->_BetweenExpression();
|
2009-03-14 13:30:47 +03:00
|
|
|
case Lexer::T_LIKE:
|
2009-01-20 20:07:07 +03:00
|
|
|
return $this->_LikeExpression();
|
2009-03-14 13:30:47 +03:00
|
|
|
case Lexer::T_IN:
|
2009-01-20 20:07:07 +03:00
|
|
|
return $this->_InExpression();
|
2009-03-14 13:30:47 +03:00
|
|
|
case Lexer::T_IS:
|
2009-01-20 20:07:07 +03:00
|
|
|
return $this->_NullComparisonExpression();
|
2009-03-14 13:30:47 +03:00
|
|
|
case Lexer::T_NONE:
|
2009-01-20 20:07:07 +03:00
|
|
|
return $this->_ComparisonExpression();
|
|
|
|
default:
|
|
|
|
$this->syntaxError();
|
|
|
|
}
|
|
|
|
} else {
|
2009-03-19 15:43:48 +03:00
|
|
|
return $this->_ComparisonExpression();
|
|
|
|
/*} else {
|
2009-01-20 20:07:07 +03:00
|
|
|
switch ($token['type']) {
|
2009-03-14 13:30:47 +03:00
|
|
|
case Lexer::T_INTEGER:
|
2009-01-20 20:07:07 +03:00
|
|
|
// IF it turns out its a ComparisonExpression, then it MUST be ArithmeticExpression
|
|
|
|
break;
|
2009-03-14 13:30:47 +03:00
|
|
|
case Lexer::T_STRING:
|
2009-01-20 20:07:07 +03:00
|
|
|
// IF it turns out its a ComparisonExpression, then it MUST be StringExpression
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->syntaxError();
|
2009-03-19 15:43:48 +03:00
|
|
|
}*/
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
/**
|
2009-03-23 20:39:33 +03:00
|
|
|
* ComparisonExpression ::=
|
|
|
|
* ArithmeticExpression ComparisonOperator (QuantifiedExpression | ArithmeticExpression) |
|
|
|
|
* StringExpression ComparisonOperator (StringExpression | QuantifiedExpression) |
|
|
|
|
* BooleanExpression ("=" | "<>" | "!=") (BooleanExpression | QuantifiedExpression) |
|
|
|
|
* EnumExpression ("=" | "<>" | "!=") (EnumExpression | QuantifiedExpression) |
|
|
|
|
* DatetimeExpression ComparisonOperator (DatetimeExpression | QuantifiedExpression) |
|
|
|
|
* EntityExpression ("=" | "<>") (EntityExpression | QuantifiedExpression)
|
2009-01-20 20:07:07 +03:00
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _ComparisonExpression()
|
2009-01-20 20:07:07 +03:00
|
|
|
{
|
2009-03-23 20:39:33 +03:00
|
|
|
$peek = $this->_lexer->glimpse();
|
|
|
|
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_INPUT_PARAMETER)) {
|
|
|
|
if ($this->_isComparisonOperator($peek)) {
|
|
|
|
$this->match(Lexer::T_INPUT_PARAMETER);
|
|
|
|
$leftExpr = new AST\InputParameter($this->_lexer->token['value']);
|
|
|
|
} else {
|
|
|
|
$leftExpr = $this->_ArithmeticExpression();
|
|
|
|
}
|
|
|
|
$operator = $this->_ComparisonOperator();
|
2009-01-20 20:07:07 +03:00
|
|
|
$rightExpr = $this->_ArithmeticExpression();
|
2009-03-23 20:39:33 +03:00
|
|
|
//...
|
|
|
|
}
|
|
|
|
else if ($this->_lexer->isNextToken('(') && $peek['type'] == Lexer::T_SELECT) {
|
|
|
|
$leftExpr = $this->_Subselect();
|
|
|
|
//...
|
|
|
|
}
|
|
|
|
else if ($this->_lexer->isNextToken(Lexer::T_IDENTIFIER) && $peek['value'] == '(') {
|
|
|
|
$peek2 = $this->_peekBeyond(')');
|
|
|
|
if ($this->_isComparisonOperator($peek2)) {
|
|
|
|
if ($this->_isStringFunction($this->_lexer->lookahead['value'])) {
|
|
|
|
$leftExpr = $this->_FunctionsReturningStrings();
|
|
|
|
$operator = $this->_ComparisonOperator();
|
|
|
|
if ($this->_lexer->lookahead['type'] === Lexer::T_ALL ||
|
|
|
|
$this->_lexer->lookahead['type'] === Lexer::T_ANY ||
|
|
|
|
$this->_lexer->lookahead['type'] === Lexer::T_SOME) {
|
|
|
|
$rightExpr = $this->_QuantifiedExpression();
|
|
|
|
} else {
|
|
|
|
$rightExpr = $this->_StringPrimary();
|
|
|
|
}
|
|
|
|
} else if ($this->_isNumericFunction($this->_lexer->lookahead['value'])) {
|
|
|
|
$leftExpr = $this->_FunctionsReturningNumerics();
|
|
|
|
$operator = $this->_ComparisonOperator();
|
|
|
|
if ($this->_lexer->lookahead['type'] === Lexer::T_ALL ||
|
|
|
|
$this->_lexer->lookahead['type'] === Lexer::T_ANY ||
|
|
|
|
$this->_lexer->lookahead['type'] === Lexer::T_SOME) {
|
|
|
|
$rightExpr = $this->_QuantifiedExpression();
|
|
|
|
} else {
|
|
|
|
$rightExpr = $this->_ArithmeticExpression();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$leftExpr = $this->_FunctionsReturningDatetime();
|
|
|
|
$operator = $this->_ComparisonOperator();
|
|
|
|
if ($this->_lexer->lookahead['type'] === Lexer::T_ALL ||
|
|
|
|
$this->_lexer->lookahead['type'] === Lexer::T_ANY ||
|
|
|
|
$this->_lexer->lookahead['type'] === Lexer::T_SOME) {
|
|
|
|
$rightExpr = $this->_QuantifiedExpression();
|
|
|
|
} else {
|
|
|
|
$rightExpr = $this->_DatetimePrimary();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$leftExpr = $this->_ArithmeticExpression();
|
|
|
|
$operator = $this->_ComparisonOperator();
|
|
|
|
if ($this->_lexer->lookahead['type'] === Lexer::T_ALL ||
|
|
|
|
$this->_lexer->lookahead['type'] === Lexer::T_ANY ||
|
|
|
|
$this->_lexer->lookahead['type'] === Lexer::T_SOME) {
|
|
|
|
$rightExpr = $this->_QuantifiedExpression();
|
|
|
|
} else {
|
|
|
|
$rightExpr = $this->_StringExpression();
|
|
|
|
}
|
|
|
|
}
|
2009-03-28 23:59:07 +03:00
|
|
|
} else {
|
2009-03-23 20:39:33 +03:00
|
|
|
$leftExpr = $this->_ArithmeticExpression();
|
|
|
|
$operator = $this->_ComparisonOperator();
|
|
|
|
if ($this->_lexer->lookahead['type'] === Lexer::T_ALL ||
|
|
|
|
$this->_lexer->lookahead['type'] === Lexer::T_ANY ||
|
|
|
|
$this->_lexer->lookahead['type'] === Lexer::T_SOME) {
|
|
|
|
$rightExpr = $this->_QuantifiedExpression();
|
|
|
|
} else {
|
|
|
|
$rightExpr = $this->_ArithmeticExpression();
|
|
|
|
}
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
2009-03-23 20:39:33 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
return new AST\ComparisonExpression($leftExpr, $operator, $rightExpr);
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2009-03-28 23:59:07 +03:00
|
|
|
/**
|
|
|
|
* Function ::= FunctionsReturningStrings | FunctionsReturningNumerics | FunctionsReturningDatetime
|
|
|
|
*/
|
2009-03-28 20:10:41 +03:00
|
|
|
public function _Function()
|
|
|
|
{
|
|
|
|
$funcName = $this->_lexer->lookahead['value'];
|
|
|
|
if ($this->_isStringFunction($funcName)) {
|
|
|
|
return $this->_FunctionsReturningStrings();
|
|
|
|
} else if ($this->_isNumericFunction($funcName)) {
|
|
|
|
return $this->_FunctionsReturningNumerics();
|
|
|
|
} else if ($this->_isDatetimeFunction($funcName)) {
|
|
|
|
return $this->_FunctionsReturningDatetime();
|
|
|
|
} else {
|
|
|
|
$this->syntaxError('Known function.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-28 23:59:07 +03:00
|
|
|
/**
|
|
|
|
* Checks whether the function with the given name is a string function
|
|
|
|
* (a function that returns strings).
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _isStringFunction($funcName)
|
2009-03-23 20:39:33 +03:00
|
|
|
{
|
|
|
|
return isset(self::$_STRING_FUNCTIONS[strtolower($funcName)]);
|
|
|
|
}
|
|
|
|
|
2009-03-28 23:59:07 +03:00
|
|
|
/**
|
|
|
|
* Checks whether the function with the given name is a numeric function
|
|
|
|
* (a function that returns numerics).
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _isNumericFunction($funcName)
|
2009-03-23 20:39:33 +03:00
|
|
|
{
|
|
|
|
return isset(self::$_NUMERIC_FUNCTIONS[strtolower($funcName)]);
|
|
|
|
}
|
|
|
|
|
2009-03-28 23:59:07 +03:00
|
|
|
/**
|
|
|
|
* Checks whether the function with the given name is a datetime function
|
|
|
|
* (a function that returns date/time values).
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _isDatetimeFunction($funcName)
|
2009-03-23 20:39:33 +03:00
|
|
|
{
|
|
|
|
return isset(self::$_DATETIME_FUNCTIONS[strtolower($funcName)]);
|
|
|
|
}
|
|
|
|
|
2009-03-28 23:59:07 +03:00
|
|
|
/**
|
|
|
|
* Peeks beyond the specified token and returns the first token after that one.
|
|
|
|
*/
|
2009-03-23 20:39:33 +03:00
|
|
|
private function _peekBeyond($token)
|
|
|
|
{
|
|
|
|
$peek = $this->_lexer->peek();
|
|
|
|
while ($peek['value'] != $token) {
|
|
|
|
$peek = $this->_lexer->peek();
|
|
|
|
}
|
|
|
|
$peek = $this->_lexer->peek();
|
|
|
|
$this->_lexer->resetPeek();
|
|
|
|
return $peek;
|
|
|
|
}
|
|
|
|
|
2009-03-28 23:59:07 +03:00
|
|
|
/**
|
|
|
|
* Checks whether the given token is a comparison operator.
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _isComparisonOperator($token)
|
2009-03-23 20:39:33 +03:00
|
|
|
{
|
|
|
|
$value = $token['value'];
|
|
|
|
return $value == '=' || $value == '<' || $value == '<=' || $value == '<>' ||
|
|
|
|
$value == '>' || $value == '>=' || $value == '!=';
|
|
|
|
}
|
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
/**
|
|
|
|
* ArithmeticExpression ::= SimpleArithmeticExpression | "(" Subselect ")"
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _ArithmeticExpression()
|
2009-01-20 20:07:07 +03:00
|
|
|
{
|
2009-01-22 22:38:10 +03:00
|
|
|
$expr = new AST\ArithmeticExpression;
|
|
|
|
if ($this->_lexer->lookahead['value'] === '(') {
|
|
|
|
$peek = $this->_lexer->glimpse();
|
2009-03-14 13:30:47 +03:00
|
|
|
if ($peek['type'] === Lexer::T_SELECT) {
|
2009-03-23 20:39:33 +03:00
|
|
|
$this->match('(');
|
2009-01-20 20:07:07 +03:00
|
|
|
$expr->setSubselect($this->_Subselect());
|
2009-03-23 20:39:33 +03:00
|
|
|
$this->match(')');
|
2009-01-20 20:07:07 +03:00
|
|
|
return $expr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$expr->setSimpleArithmeticExpression($this->_SimpleArithmeticExpression());
|
|
|
|
return $expr;
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
/**
|
|
|
|
* SimpleArithmeticExpression ::= ArithmeticTerm {("+" | "-") ArithmeticTerm}*
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _SimpleArithmeticExpression()
|
2009-01-20 20:07:07 +03:00
|
|
|
{
|
|
|
|
$terms = array();
|
|
|
|
$terms[] = $this->_ArithmeticTerm();
|
2009-01-22 22:38:10 +03:00
|
|
|
while ($this->_lexer->lookahead['value'] == '+' || $this->_lexer->lookahead['value'] == '-') {
|
|
|
|
if ($this->_lexer->lookahead['value'] == '+') {
|
2009-01-21 21:25:05 +03:00
|
|
|
$this->match('+');
|
|
|
|
} else {
|
|
|
|
$this->match('-');
|
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
$terms[] = $this->_lexer->token['value'];
|
2009-01-20 20:07:07 +03:00
|
|
|
$terms[] = $this->_ArithmeticTerm();
|
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
return new AST\SimpleArithmeticExpression($terms);
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
/**
|
|
|
|
* ArithmeticTerm ::= ArithmeticFactor {("*" | "/") ArithmeticFactor}*
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _ArithmeticTerm()
|
2009-01-20 20:07:07 +03:00
|
|
|
{
|
|
|
|
$factors = array();
|
|
|
|
$factors[] = $this->_ArithmeticFactor();
|
2009-01-22 22:38:10 +03:00
|
|
|
while ($this->_lexer->lookahead['value'] == '*' || $this->_lexer->lookahead['value'] == '/') {
|
|
|
|
if ($this->_lexer->lookahead['value'] == '*') {
|
2009-01-21 21:25:05 +03:00
|
|
|
$this->match('*');
|
|
|
|
} else {
|
|
|
|
$this->match('/');
|
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
$factors[] = $this->_lexer->token['value'];
|
2009-01-20 20:07:07 +03:00
|
|
|
$factors[] = $this->_ArithmeticFactor();
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
return new AST\ArithmeticTerm($factors);
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
/**
|
|
|
|
* ArithmeticFactor ::= [("+" | "-")] ArithmeticPrimary
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _ArithmeticFactor()
|
2009-01-20 20:07:07 +03:00
|
|
|
{
|
|
|
|
$pSign = $nSign = false;
|
2009-01-22 22:38:10 +03:00
|
|
|
if ($this->_lexer->lookahead['value'] == '+') {
|
2009-01-20 20:07:07 +03:00
|
|
|
$this->match('+');
|
|
|
|
$pSign = true;
|
2009-01-22 22:38:10 +03:00
|
|
|
} else if ($this->_lexer->lookahead['value'] == '-') {
|
2009-01-20 20:07:07 +03:00
|
|
|
$this->match('-');
|
|
|
|
$nSign = true;
|
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
return new AST\ArithmeticFactor($this->_ArithmeticPrimary(), $pSign, $nSign);
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
2009-03-15 00:19:50 +03:00
|
|
|
/**
|
|
|
|
* InExpression ::= StateFieldPathExpression ["NOT"] "IN" "(" (Literal {"," Literal}* | Subselect) ")"
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _InExpression()
|
2009-03-15 00:19:50 +03:00
|
|
|
{
|
2009-03-21 15:49:58 +03:00
|
|
|
$inExpression = new AST\InExpression($this->_StateFieldPathExpression());
|
2009-03-15 00:19:50 +03:00
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_NOT)) {
|
|
|
|
$this->match(Lexer::T_NOT);
|
|
|
|
$inExpression->setNot(true);
|
|
|
|
}
|
|
|
|
$this->match(Lexer::T_IN);
|
|
|
|
$this->match('(');
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_SELECT)) {
|
|
|
|
$inExpression->setSubselect($this->_Subselect());
|
|
|
|
} else {
|
|
|
|
$literals = array();
|
|
|
|
$literals[] = $this->_Literal();
|
|
|
|
while ($this->_lexer->isNextToken(',')) {
|
|
|
|
$this->match(',');
|
|
|
|
$literals[] = $this->_Literal();
|
|
|
|
}
|
2009-03-21 22:58:52 +03:00
|
|
|
$inExpression->setLiterals($literals);
|
2009-03-15 00:19:50 +03:00
|
|
|
}
|
|
|
|
$this->match(')');
|
|
|
|
|
|
|
|
return $inExpression;
|
|
|
|
}
|
|
|
|
|
2009-03-17 01:12:38 +03:00
|
|
|
/**
|
|
|
|
* ExistsExpression ::= ["NOT"] "EXISTS" "(" Subselect ")"
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _ExistsExpression()
|
2009-03-17 01:12:38 +03:00
|
|
|
{
|
|
|
|
$not = false;
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_NOT)) {
|
|
|
|
$this->match(Lexer::T_NOT);
|
|
|
|
$not = true;
|
|
|
|
}
|
|
|
|
$this->match(Lexer::T_EXISTS);
|
|
|
|
$this->match('(');
|
|
|
|
$existsExpression = new AST\ExistsExpression($this->_Subselect());
|
|
|
|
$this->match(')');
|
|
|
|
$existsExpression->setNot($not);
|
|
|
|
return $existsExpression;
|
|
|
|
}
|
|
|
|
|
2009-03-28 20:10:41 +03:00
|
|
|
/**
|
|
|
|
* QuantifiedExpression ::= ("ALL" | "ANY" | "SOME") "(" Subselect ")"
|
|
|
|
*/
|
|
|
|
public function _QuantifiedExpression()
|
|
|
|
{
|
|
|
|
$all = $any = $some = false;
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_ALL)) {
|
|
|
|
$this->match(Lexer::T_ALL);
|
|
|
|
$all = true;
|
|
|
|
} else if ($this->_lexer->isNextToken(Lexer::T_ANY)) {
|
|
|
|
$this->match(Lexer::T_ANY);
|
|
|
|
$any = true;
|
|
|
|
} else if ($this->_lexer->isNextToken(Lexer::T_SOME)) {
|
|
|
|
$this->match(Lexer::T_SOME);
|
|
|
|
$some = true;
|
|
|
|
} else {
|
|
|
|
$this->syntaxError('ALL, ANY or SOME');
|
|
|
|
}
|
|
|
|
$this->match('(');
|
|
|
|
$qExpr = new AST\QuantifiedExpression($this->_Subselect());
|
|
|
|
$this->match(')');
|
|
|
|
$qExpr->setAll($all);
|
|
|
|
$qExpr->setAny($any);
|
|
|
|
$qExpr->setSome($some);
|
|
|
|
return $qExpr;
|
|
|
|
}
|
|
|
|
|
2009-03-17 01:12:38 +03:00
|
|
|
/**
|
|
|
|
* Subselect ::= SimpleSelectClause SubselectFromClause [WhereClause] [GroupByClause] [HavingClause] [OrderByClause]
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _Subselect()
|
2009-03-17 01:12:38 +03:00
|
|
|
{
|
2009-03-21 15:49:58 +03:00
|
|
|
$this->_beginDeferredPathExpressionStack();
|
2009-03-17 01:12:38 +03:00
|
|
|
$subselect = new AST\Subselect($this->_SimpleSelectClause(), $this->_SubselectFromClause());
|
2009-03-21 15:49:58 +03:00
|
|
|
$this->_processDeferredPathExpressionStack();
|
2009-03-17 01:12:38 +03:00
|
|
|
|
|
|
|
$subselect->setWhereClause(
|
|
|
|
$this->_lexer->isNextToken(Lexer::T_WHERE) ? $this->_WhereClause() : null
|
|
|
|
);
|
|
|
|
|
|
|
|
$subselect->setGroupByClause(
|
|
|
|
$this->_lexer->isNextToken(Lexer::T_GROUP) ? $this->_GroupByClause() : null
|
|
|
|
);
|
|
|
|
|
|
|
|
$subselect->setHavingClause(
|
|
|
|
$this->_lexer->isNextToken(Lexer::T_HAVING) ? $this->_HavingClause() : null
|
|
|
|
);
|
|
|
|
|
|
|
|
$subselect->setOrderByClause(
|
|
|
|
$this->_lexer->isNextToken(Lexer::T_ORDER) ? $this->_OrderByClause() : null
|
|
|
|
);
|
|
|
|
|
|
|
|
return $subselect;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SimpleSelectClause ::= "SELECT" ["DISTINCT"] SimpleSelectExpression
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _SimpleSelectClause()
|
2009-03-17 01:12:38 +03:00
|
|
|
{
|
|
|
|
$distinct = false;
|
|
|
|
$this->match(Lexer::T_SELECT);
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_DISTINCT)) {
|
|
|
|
$this->match(Lexer::T_DISTINCT);
|
|
|
|
$distinct = true;
|
|
|
|
}
|
|
|
|
$simpleSelectClause = new AST\SimpleSelectClause($this->_SimpleSelectExpression());
|
|
|
|
$simpleSelectClause->setDistinct($distinct);
|
|
|
|
return $simpleSelectClause;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SubselectFromClause ::= "FROM" SubselectIdentificationVariableDeclaration {"," SubselectIdentificationVariableDeclaration}*
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _SubselectFromClause()
|
2009-03-17 01:12:38 +03:00
|
|
|
{
|
|
|
|
$this->match(Lexer::T_FROM);
|
|
|
|
$identificationVariables = array();
|
|
|
|
$identificationVariables[] = $this->_SubselectIdentificationVariableDeclaration();
|
|
|
|
while ($this->_lexer->isNextToken(',')) {
|
|
|
|
$this->match(',');
|
|
|
|
$identificationVariables[] = $this->_SubselectIdentificationVariableDeclaration();
|
|
|
|
}
|
|
|
|
return new AST\SubselectFromClause($identificationVariables);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SubselectIdentificationVariableDeclaration ::= IdentificationVariableDeclaration | (AssociationPathExpression ["AS"] AliasIdentificationVariable)
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _SubselectIdentificationVariableDeclaration()
|
2009-03-17 01:12:38 +03:00
|
|
|
{
|
|
|
|
$peek = $this->_lexer->glimpse();
|
|
|
|
if ($peek['value'] == '.') {
|
|
|
|
$subselectIdentificationVarDecl = new AST\SubselectIdentificationVariableDeclaration;
|
|
|
|
$subselectIdentificationVarDecl->setAssociationPathExpression($this->_AssociationPathExpression());
|
|
|
|
$this->match(Lexer::T_AS);
|
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
|
|
|
$subselectIdentificationVarDecl->setAliasIdentificationVariable($this->_lexer->token['value']);
|
|
|
|
return $subselectIdentificationVarDecl;
|
|
|
|
} else {
|
|
|
|
return $this->_IdentificationVariableDeclaration();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-03-19 15:43:48 +03:00
|
|
|
* SimpleSelectExpression ::= StateFieldPathExpression | IdentificationVariable | (AggregateExpression [["AS"] FieldAliasIdentificationVariable])
|
2009-03-17 01:12:38 +03:00
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _SimpleSelectExpression()
|
2009-03-17 01:12:38 +03:00
|
|
|
{
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_IDENTIFIER)) {
|
|
|
|
// SingleValuedPathExpression | IdentificationVariable
|
|
|
|
$peek = $this->_lexer->glimpse();
|
|
|
|
if ($peek['value'] == '.') {
|
2009-03-21 15:49:58 +03:00
|
|
|
return new AST\SimpleSelectExpression($this->_StateFieldPathExpression());
|
2009-03-17 01:12:38 +03:00
|
|
|
} else {
|
|
|
|
$this->match($this->_lexer->lookahead['value']);
|
2009-03-19 15:43:48 +03:00
|
|
|
return new AST\SimpleSelectExpression($this->_lexer->token['value']);
|
2009-03-17 01:12:38 +03:00
|
|
|
}
|
|
|
|
} else {
|
2009-03-19 15:43:48 +03:00
|
|
|
$expr = new AST\SimpleSelectExpression($this->_AggregateExpression());
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_AS)) {
|
|
|
|
$this->match(Lexer::T_AS);
|
|
|
|
}
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_IDENTIFIER)) {
|
|
|
|
$this->match(Lexer::T_IDENTIFIER);
|
|
|
|
$expr->setFieldIdentificationVariable($this->_lexer->token['value']);
|
|
|
|
}
|
|
|
|
return $expr;
|
2009-03-17 01:12:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-15 00:19:50 +03:00
|
|
|
/**
|
|
|
|
* Literal ::= string | char | integer | float | boolean | InputParameter
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _Literal()
|
2009-03-15 00:19:50 +03:00
|
|
|
{
|
|
|
|
switch ($this->_lexer->lookahead['type']) {
|
|
|
|
case Lexer::T_INPUT_PARAMETER:
|
|
|
|
$this->match($this->_lexer->lookahead['value']);
|
|
|
|
return new AST\InputParameter($this->_lexer->token['value']);
|
|
|
|
case Lexer::T_STRING:
|
|
|
|
case Lexer::T_INTEGER:
|
|
|
|
case Lexer::T_FLOAT:
|
|
|
|
$this->match($this->_lexer->lookahead['value']);
|
|
|
|
return $this->_lexer->token['value'];
|
|
|
|
default:
|
|
|
|
$this->syntaxError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-19 15:43:48 +03:00
|
|
|
/**
|
|
|
|
* BetweenExpression ::= ArithmeticExpression ["NOT"] "BETWEEN" ArithmeticExpression "AND" ArithmeticExpression
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _BetweenExpression()
|
2009-03-19 15:43:48 +03:00
|
|
|
{
|
|
|
|
$not = false;
|
|
|
|
$arithExpr1 = $this->_ArithmeticExpression();
|
|
|
|
if ($this->_lexer->isNextToken(Lexer::T_NOT)) {
|
|
|
|
$this->match(Lexer::T_NOT);
|
|
|
|
$not = true;
|
|
|
|
}
|
|
|
|
$this->match(Lexer::T_BETWEEN);
|
|
|
|
$arithExpr2 = $this->_ArithmeticExpression();
|
|
|
|
$this->match(Lexer::T_AND);
|
|
|
|
$arithExpr3 = $this->_ArithmeticExpression();
|
|
|
|
|
|
|
|
$betweenExpr = new AST\BetweenExpression($arithExpr1, $arithExpr2, $arithExpr3);
|
|
|
|
$betweenExpr->setNot($not);
|
|
|
|
|
|
|
|
return $betweenExpr;
|
|
|
|
}
|
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
/**
|
|
|
|
* ArithmeticPrimary ::= StateFieldPathExpression | Literal | "(" SimpleArithmeticExpression ")" | Function | AggregateExpression
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _ArithmeticPrimary()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-01-22 22:38:10 +03:00
|
|
|
if ($this->_lexer->lookahead['value'] === '(') {
|
2009-01-21 21:25:05 +03:00
|
|
|
$this->match('(');
|
|
|
|
$expr = $this->_SimpleArithmeticExpression();
|
|
|
|
$this->match(')');
|
|
|
|
return $expr;
|
2009-01-20 20:07:07 +03:00
|
|
|
}
|
2009-03-19 15:43:48 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
switch ($this->_lexer->lookahead['type']) {
|
2009-03-14 13:30:47 +03:00
|
|
|
case Lexer::T_IDENTIFIER:
|
2009-01-22 22:38:10 +03:00
|
|
|
$peek = $this->_lexer->glimpse();
|
|
|
|
if ($peek['value'] == '(') {
|
2009-03-23 20:39:33 +03:00
|
|
|
return $this->_FunctionsReturningNumerics();
|
2009-01-22 22:38:10 +03:00
|
|
|
}
|
2009-01-21 21:25:05 +03:00
|
|
|
return $this->_StateFieldPathExpression();
|
2009-03-14 13:30:47 +03:00
|
|
|
case Lexer::T_INPUT_PARAMETER:
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->match($this->_lexer->lookahead['value']);
|
|
|
|
return new AST\InputParameter($this->_lexer->token['value']);
|
2009-03-14 13:30:47 +03:00
|
|
|
case Lexer::T_STRING:
|
|
|
|
case Lexer::T_INTEGER:
|
|
|
|
case Lexer::T_FLOAT:
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->match($this->_lexer->lookahead['value']);
|
|
|
|
return $this->_lexer->token['value'];
|
2009-01-21 21:25:05 +03:00
|
|
|
default:
|
2009-03-19 15:43:48 +03:00
|
|
|
$peek = $this->_lexer->glimpse();
|
|
|
|
if ($peek['value'] == '(') {
|
|
|
|
if ($this->_isAggregateFunction($this->_lexer->lookahead['type'])) {
|
|
|
|
return $this->_AggregateExpression();
|
|
|
|
}
|
|
|
|
return $this->_FunctionsReturningStrings();
|
|
|
|
} else {
|
|
|
|
$this->syntaxError();
|
|
|
|
}
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
2009-03-23 20:39:33 +03:00
|
|
|
throw DoctrineException::updateMe("Not yet implemented2.");
|
2009-01-20 20:07:07 +03:00
|
|
|
//TODO...
|
2009-01-19 21:40:12 +03:00
|
|
|
}
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
/**
|
|
|
|
* PortableFunctionsReturningStrings ::=
|
|
|
|
* "CONCAT" "(" StringPrimary "," StringPrimary ")" |
|
|
|
|
* "SUBSTRING" "(" StringPrimary "," SimpleArithmeticExpression "," SimpleArithmeticExpression ")" |
|
|
|
|
* "TRIM" "(" [["LEADING" | "TRAILING" | "BOTH"] [char] "FROM"] StringPrimary ")" |
|
|
|
|
* "LOWER" "(" StringPrimary ")" |
|
|
|
|
* "UPPER" "(" StringPrimary ")"
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _FunctionsReturningStrings()
|
2009-01-22 22:38:10 +03:00
|
|
|
{
|
2009-03-23 20:39:33 +03:00
|
|
|
$funcNameLower = strtolower($this->_lexer->lookahead['value']);
|
|
|
|
$funcClass = self::$_STRING_FUNCTIONS[$funcNameLower];
|
|
|
|
$function = new $funcClass($funcNameLower);
|
|
|
|
$function->parse($this);
|
|
|
|
return $function;
|
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
|
2009-03-23 20:39:33 +03:00
|
|
|
/**
|
|
|
|
* PortableFunctionsReturningNumerics ::=
|
|
|
|
* "LENGTH" "(" StringPrimary ")" |
|
|
|
|
* "LOCATE" "(" StringPrimary "," StringPrimary ["," SimpleArithmeticExpression]")" |
|
|
|
|
* "ABS" "(" SimpleArithmeticExpression ")" |
|
|
|
|
* "SQRT" "(" SimpleArithmeticExpression ")" |
|
|
|
|
* "MOD" "(" SimpleArithmeticExpression "," SimpleArithmeticExpression ")" |
|
|
|
|
* "SIZE" "(" CollectionValuedPathExpression ")"
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _FunctionsReturningNumerics()
|
2009-03-23 20:39:33 +03:00
|
|
|
{
|
|
|
|
$funcNameLower = strtolower($this->_lexer->lookahead['value']);
|
|
|
|
$funcClass = self::$_NUMERIC_FUNCTIONS[$funcNameLower];
|
|
|
|
$function = new $funcClass($funcNameLower);
|
|
|
|
$function->parse($this);
|
|
|
|
return $function;
|
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
|
2009-03-23 20:39:33 +03:00
|
|
|
/**
|
|
|
|
* PortableFunctionsReturningDateTime ::= "CURRENT_DATE" | "CURRENT_TIME" | "CURRENT_TIMESTAMP"
|
|
|
|
*/
|
|
|
|
public function _FunctionsReturningDatetime()
|
|
|
|
{
|
|
|
|
$funcNameLower = strtolower($this->_lexer->lookahead['value']);
|
|
|
|
$funcClass = self::$_DATETIME_FUNCTIONS[$funcNameLower];
|
|
|
|
$function = new $funcClass($funcNameLower);
|
|
|
|
$function->parse($this);
|
|
|
|
return $function;
|
2009-01-22 22:38:10 +03:00
|
|
|
}
|
|
|
|
|
2009-03-23 20:39:33 +03:00
|
|
|
/**
|
|
|
|
* Checks whether the given token type indicates an aggregate function.
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _isAggregateFunction($tokenType)
|
2009-01-22 22:38:10 +03:00
|
|
|
{
|
2009-03-23 20:39:33 +03:00
|
|
|
return $tokenType == Lexer::T_AVG || $tokenType == Lexer::T_MIN ||
|
|
|
|
$tokenType == Lexer::T_MAX || $tokenType == Lexer::T_SUM ||
|
|
|
|
$tokenType == Lexer::T_COUNT;
|
2009-01-22 22:38:10 +03:00
|
|
|
}
|
|
|
|
|
2009-01-20 20:07:07 +03:00
|
|
|
/**
|
|
|
|
* ComparisonOperator ::= "=" | "<" | "<=" | "<>" | ">" | ">=" | "!="
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _ComparisonOperator()
|
2009-01-19 21:40:12 +03:00
|
|
|
{
|
2009-01-22 22:38:10 +03:00
|
|
|
switch ($this->_lexer->lookahead['value']) {
|
2009-01-20 20:07:07 +03:00
|
|
|
case '=':
|
|
|
|
$this->match('=');
|
|
|
|
return '=';
|
|
|
|
case '<':
|
|
|
|
$this->match('<');
|
|
|
|
$operator = '<';
|
2009-01-22 22:38:10 +03:00
|
|
|
if ($this->_lexer->isNextToken('=')) {
|
2009-01-20 20:07:07 +03:00
|
|
|
$this->match('=');
|
|
|
|
$operator .= '=';
|
2009-01-22 22:38:10 +03:00
|
|
|
} else if ($this->_lexer->isNextToken('>')) {
|
2009-01-20 20:07:07 +03:00
|
|
|
$this->match('>');
|
|
|
|
$operator .= '>';
|
|
|
|
}
|
|
|
|
return $operator;
|
|
|
|
case '>':
|
|
|
|
$this->match('>');
|
|
|
|
$operator = '>';
|
2009-01-22 22:38:10 +03:00
|
|
|
if ($this->_lexer->isNextToken('=')) {
|
2009-01-20 20:07:07 +03:00
|
|
|
$this->match('=');
|
|
|
|
$operator .= '=';
|
|
|
|
}
|
|
|
|
return $operator;
|
|
|
|
case '!':
|
|
|
|
$this->match('!');
|
|
|
|
$this->match('=');
|
|
|
|
return '<>';
|
|
|
|
default:
|
2009-01-21 21:25:05 +03:00
|
|
|
$this->syntaxError('=, <, <=, <>, >, >=, !=');
|
2009-01-20 20:07:07 +03:00
|
|
|
break;
|
|
|
|
}
|
2008-09-23 06:47:11 +04:00
|
|
|
}
|
2009-01-21 21:25:05 +03:00
|
|
|
|
|
|
|
/**
|
2009-03-21 15:49:58 +03:00
|
|
|
* LikeExpression ::= StringExpression ["NOT"] "LIKE" (string | input_parameter) ["ESCAPE" char]
|
2009-01-21 21:25:05 +03:00
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _LikeExpression()
|
2009-01-21 21:25:05 +03:00
|
|
|
{
|
|
|
|
$stringExpr = $this->_StringExpression();
|
|
|
|
$isNot = false;
|
2009-03-14 13:30:47 +03:00
|
|
|
if ($this->_lexer->lookahead['type'] === Lexer::T_NOT) {
|
|
|
|
$this->match(Lexer::T_NOT);
|
2009-01-21 21:25:05 +03:00
|
|
|
$isNot = true;
|
|
|
|
}
|
2009-03-14 13:30:47 +03:00
|
|
|
$this->match(Lexer::T_LIKE);
|
2009-03-21 15:49:58 +03: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-01-21 21:25:05 +03:00
|
|
|
$escapeChar = null;
|
2009-03-14 13:30:47 +03:00
|
|
|
if ($this->_lexer->lookahead['type'] === Lexer::T_ESCAPE) {
|
|
|
|
$this->match(Lexer::T_ESCAPE);
|
2009-03-19 15:43:48 +03:00
|
|
|
$this->match(Lexer::T_STRING);
|
|
|
|
$escapeChar = $this->_lexer->token['value'];
|
2009-01-21 21:25:05 +03:00
|
|
|
}
|
2009-01-22 22:38:10 +03:00
|
|
|
return new AST\LikeExpression($stringExpr, $stringPattern, $isNot, $escapeChar);
|
2009-01-21 21:25:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* StringExpression ::= StringPrimary | "(" Subselect ")"
|
|
|
|
*/
|
2009-03-23 22:02:16 +03:00
|
|
|
public function _StringExpression()
|
2009-01-21 21:25:05 +03:00
|
|
|
{
|
2009-01-22 22:38:10 +03:00
|
|
|
if ($this->_lexer->lookahead['value'] === '(') {
|
|
|
|
$peek = $this->_lexer->glimpse();
|
2009-03-14 13:30:47 +03:00
|
|
|
if ($peek['type'] === Lexer::T_SELECT) {
|
2009-03-23 20:39:33 +03:00
|
|
|
$this->match('(');
|
|
|
|
$expr = $this->_Subselect();
|
|
|
|
$this->match(')');
|
|
|
|
return $expr;
|
2009-01-21 21:25:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->_StringPrimary();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* StringPrimary ::= StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression
|
|
|
|
*/
|
2009-03-23 20:39:33 +03:00
|
|
|
public function _StringPrimary()
|
2009-01-21 21:25:05 +03:00
|
|
|
{
|
2009-03-14 13:30:47 +03:00
|
|
|
if ($this->_lexer->lookahead['type'] === Lexer::T_IDENTIFIER) {
|
2009-01-22 22:38:10 +03:00
|
|
|
$peek = $this->_lexer->glimpse();
|
2009-01-21 21:25:05 +03:00
|
|
|
if ($peek['value'] == '.') {
|
|
|
|
return $this->_StateFieldPathExpression();
|
|
|
|
} else if ($peek['value'] == '(') {
|
2009-03-28 20:10:41 +03:00
|
|
|
return $this->_FunctionsReturningStrings();
|
2009-01-21 21:25:05 +03:00
|
|
|
} else {
|
|
|
|
$this->syntaxError("'.' or '('");
|
|
|
|
}
|
2009-03-14 13:30:47 +03:00
|
|
|
} else if ($this->_lexer->lookahead['type'] === Lexer::T_STRING) {
|
2009-03-19 15:43:48 +03:00
|
|
|
$this->match(Lexer::T_STRING);
|
|
|
|
return $this->_lexer->token['value'];
|
2009-03-14 13:30:47 +03:00
|
|
|
} else if ($this->_lexer->lookahead['type'] === Lexer::T_INPUT_PARAMETER) {
|
2009-03-19 15:43:48 +03:00
|
|
|
$this->match(Lexer::T_INPUT_PARAMETER);
|
|
|
|
return new AST\InputParameter($this->_lexer->token['value']);
|
2009-03-28 20:10:41 +03:00
|
|
|
} else if ($this->_isAggregateFunction($this->_lexer->lookahead['type'])) {
|
|
|
|
return $this->_AggregateExpression();
|
2009-01-21 21:25:05 +03:00
|
|
|
} else {
|
|
|
|
$this->syntaxError('StateFieldPathExpression | string | InputParameter | FunctionsReturningStrings | AggregateExpression');
|
|
|
|
}
|
|
|
|
}
|
2009-03-23 20:39:33 +03:00
|
|
|
|
2009-03-23 22:02:16 +03:00
|
|
|
/**
|
|
|
|
* Registers a custom function that returns strings.
|
|
|
|
*
|
|
|
|
* @param string $name The function name.
|
|
|
|
* @param string $class The class name of the function implementation.
|
|
|
|
*/
|
2009-03-23 20:39:33 +03:00
|
|
|
public static function registerStringFunction($name, $class)
|
|
|
|
{
|
|
|
|
self::$_STRING_FUNCTIONS[$name] = $class;
|
|
|
|
}
|
|
|
|
|
2009-03-23 22:02:16 +03:00
|
|
|
/**
|
|
|
|
* Registers a custom function that returns numerics.
|
|
|
|
*
|
|
|
|
* @param string $name The function name.
|
|
|
|
* @param string $class The class name of the function implementation.
|
|
|
|
*/
|
2009-03-23 20:39:33 +03:00
|
|
|
public static function registerNumericFunction($name, $class)
|
|
|
|
{
|
|
|
|
self::$_NUMERIC_FUNCTIONS[$name] = $class;
|
|
|
|
}
|
|
|
|
|
2009-03-23 22:02:16 +03:00
|
|
|
/**
|
|
|
|
* Registers a custom function that returns date/time values.
|
|
|
|
*
|
|
|
|
* @param string $name The function name.
|
|
|
|
* @param string $class The class name of the function implementation.
|
|
|
|
*/
|
2009-03-23 20:39:33 +03:00
|
|
|
public static function registerDatetimeFunction($name, $class)
|
|
|
|
{
|
|
|
|
self::$_DATETIME_FUNCTIONS[$name] = $class;
|
|
|
|
}
|
2009-02-20 08:46:20 +03:00
|
|
|
}
|