2008-05-24 22:18:37 +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
|
|
|
|
* <http://www.phpdoctrine.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RangeVariableDeclaration = identifier {"." identifier} [["AS"] IdentificationVariable]
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
|
|
|
* @subpackage Query
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Janne Vanhala <jpvanhal@cc.hut.fi>
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link http://www.phpdoctrine.org
|
2008-06-15 19:56:28 +04:00
|
|
|
* @since 2.0
|
2008-05-24 22:18:37 +04:00
|
|
|
* @version $Revision$
|
|
|
|
*/
|
|
|
|
class Doctrine_Query_Production_RangeVariableDeclaration extends Doctrine_Query_Production
|
|
|
|
{
|
|
|
|
protected $_identifiers = array();
|
|
|
|
|
2008-08-22 00:45:19 +04:00
|
|
|
protected $_queryComponent;
|
|
|
|
|
2008-05-24 22:18:37 +04:00
|
|
|
protected $_identificationVariable;
|
|
|
|
|
|
|
|
|
|
|
|
public function syntax($paramHolder)
|
|
|
|
{
|
|
|
|
// RangeVariableDeclaration = identifier {"." identifier} [["AS"] IdentificationVariable]
|
|
|
|
$this->_parser->match(Doctrine_Query_Token::T_IDENTIFIER);
|
|
|
|
$this->_identifiers[] = $this->_parser->token['value'];
|
|
|
|
|
|
|
|
while ($this->_isNextToken('.')) {
|
|
|
|
$this->_parser->match('.');
|
|
|
|
$this->_parser->match(Doctrine_Query_Token::T_IDENTIFIER);
|
|
|
|
|
|
|
|
$this->_identifiers[] = $this->_parser->token['value'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->_isNextToken(Doctrine_Query_Token::T_AS)) {
|
|
|
|
$this->_parser->match(Doctrine_Query_Token::T_AS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->_isNextToken(Doctrine_Query_Token::T_IDENTIFIER)) {
|
|
|
|
$paramHolder->set('componentName', implode('.', $this->_identifiers));
|
|
|
|
|
|
|
|
// Will return an identifier, with the semantical check already applied
|
|
|
|
$this->_identificationVariable = $this->AST('IdentificationVariable', $paramHolder);
|
|
|
|
|
|
|
|
$paramHolder->remove('componentName');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function semantical($paramHolder)
|
|
|
|
{
|
|
|
|
$parserResult = $this->_parser->getParserResult();
|
|
|
|
$componentName = implode('.', $this->_identifiers);
|
|
|
|
|
|
|
|
if ($parserResult->hasQueryComponent($componentName)) {
|
|
|
|
//echo "Entered in if of hasQueryComponent(".$componentName."): true\n";
|
|
|
|
|
|
|
|
// As long as name != alias, try to bring the queryComponent from name (already processed)
|
|
|
|
$queryComponent = $parserResult->getQueryComponent($componentName);
|
|
|
|
|
|
|
|
// Check if we defined _identificationVariable. We throw semantical error if not
|
|
|
|
if ($this->_identificationVariable === null) {
|
|
|
|
$componentName = $queryComponent['metadata']->getClassName();
|
|
|
|
|
|
|
|
$this->_parser->semanticalError(
|
|
|
|
"Cannot re-declare component '{$componentName}'. Please assign an alias to it."
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//echo "Entered in if hasQueryComponent(".$componentName."), alias ".var_export($this->_identificationVariable, true).": false\n";
|
|
|
|
|
|
|
|
// No queryComponent was found. We will have to build it for the first time
|
|
|
|
if (count($this->_identifiers) > 1) {
|
|
|
|
// We are in a multiple identifier declaration; we are dealing with relations here
|
|
|
|
$this->_semanticalWithMultipleIdentifier();
|
|
|
|
} else {
|
|
|
|
// We are in a single identifier declaration; our identifier is the class name
|
|
|
|
$this->_semanticalWithSingleIdentifier();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function buildSql()
|
|
|
|
{
|
2008-08-22 00:45:19 +04:00
|
|
|
// We need to bring the queryComponent and get things from there.
|
|
|
|
$parserResult = $this->_parser->getParserResult();
|
|
|
|
|
|
|
|
// Retrieving connection
|
|
|
|
$conn = $this->_em->getConnection();
|
|
|
|
|
|
|
|
return $conn->quoteIdentifier($this->_queryComponent['metadata']->getTableName()) . ' '
|
|
|
|
. $conn->quoteIdentifier($parserResult->getTableAliasFromComponentAlias($this->_identificationVariable));
|
2008-05-24 22:18:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function _semanticalWithSingleIdentifier()
|
|
|
|
{
|
|
|
|
$parserResult = $this->_parser->getParserResult();
|
|
|
|
|
|
|
|
// Get the connection for the component
|
2008-06-16 22:31:21 +04:00
|
|
|
$conn = $this->_em->getConnection();
|
2008-05-24 22:18:37 +04:00
|
|
|
$componentName = $this->_identifiers[0];
|
|
|
|
|
|
|
|
// Retrieving ClassMetadata and Mapper
|
|
|
|
try {
|
2008-06-16 22:31:21 +04:00
|
|
|
$classMetadata = $this->_em->getClassMetadata($componentName);
|
2008-05-24 22:18:37 +04:00
|
|
|
|
|
|
|
// Building queryComponent
|
2008-08-22 00:45:19 +04:00
|
|
|
$this->_queryComponent = array(
|
2008-05-24 22:18:37 +04:00
|
|
|
'metadata' => $classMetadata,
|
|
|
|
'parent' => null,
|
|
|
|
'relation' => null,
|
|
|
|
'map' => null,
|
2008-05-27 07:44:08 +04:00
|
|
|
'scalar' => null,
|
2008-05-24 22:18:37 +04:00
|
|
|
);
|
|
|
|
} catch (Doctrine_Exception $e) {
|
|
|
|
//echo "Tried to load class metadata from '".$componentName."': " . $e->getMessage() . "\n";
|
|
|
|
$this->_parser->semanticalError($e->getMessage());
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->_identificationVariable === null) {
|
|
|
|
$this->_identificationVariable = $componentName;
|
|
|
|
}
|
|
|
|
|
|
|
|
//echo "Identification Variable: " .$this->_identificationVariable . "\n";
|
|
|
|
|
2008-06-02 19:00:50 +04:00
|
|
|
$tableAlias = $parserResult->generateTableAlias($classMetadata->getClassName());
|
2008-08-22 00:45:19 +04:00
|
|
|
$parserResult->setQueryComponent($this->_identificationVariable, $this->_queryComponent);
|
2008-05-24 22:18:37 +04:00
|
|
|
$parserResult->setTableAlias($tableAlias, $this->_identificationVariable);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function _semanticalWithMultipleIdentifier()
|
|
|
|
{
|
|
|
|
$parserResult = $this->_parser->getParserResult();
|
|
|
|
|
|
|
|
// Get the connection for the component
|
2008-06-16 22:31:21 +04:00
|
|
|
$conn = $this->_em->getConnection();
|
2008-05-24 22:18:37 +04:00
|
|
|
|
|
|
|
// Retrieve the base component
|
|
|
|
try {
|
2008-08-22 00:45:19 +04:00
|
|
|
$this->_queryComponent = $parserResult->getQueryComponent($this->_identifiers[0]);
|
|
|
|
$classMetadata = $this->_queryComponent['metadata'];
|
2008-05-24 22:18:37 +04:00
|
|
|
$className = $classMetadata->getClassName();
|
|
|
|
$parent = $path = $this->_identifiers[0];
|
|
|
|
} catch (Doctrine_Exception $e) {
|
|
|
|
$this->_parser->semanticalError($e->getMessage());
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We loop into others identifier to build query components
|
|
|
|
for ($i = 1, $l = count($this->_identifiers); $i < $l; $i++) {
|
|
|
|
$relationName = $this->_identifiers[$i];
|
2008-05-27 07:44:08 +04:00
|
|
|
$path .= '.' . $relationName;
|
2008-05-24 22:18:37 +04:00
|
|
|
|
|
|
|
if ($parserResult->hasQueryComponent($path)) {
|
|
|
|
// We already have the query component on hands, get it
|
2008-08-22 00:45:19 +04:00
|
|
|
$this->_queryComponent = $parserResult->getQueryComponent($path);
|
|
|
|
$classMetadata = $this->_queryComponent['metadata'];
|
2008-05-24 22:18:37 +04:00
|
|
|
|
|
|
|
// If we are in our last check and identification variable is null, we throw semantical error
|
|
|
|
if ($i == $l - 1 && $this->_identificationVariable === null) {
|
|
|
|
$componentName = $classMetadata->getClassName();
|
|
|
|
|
|
|
|
$this->_parser->semanticalError(
|
|
|
|
"Cannot re-declare component '{$componentName}' in path '{$path}'. " .
|
|
|
|
"Please assign an alias to it."
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We don't have the query component yet
|
2008-08-16 23:40:59 +04:00
|
|
|
if ( ! $classMetadata->hasAssociation($relationName)) {
|
2008-05-24 22:18:37 +04:00
|
|
|
$className = $classMetadata->getClassName();
|
|
|
|
|
|
|
|
$this->_parser->semanticalError("Relation '{$relationName}' does not exist in component '{$className}'");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-16 23:40:59 +04:00
|
|
|
// Retrieving ClassMetadata
|
2008-05-24 22:18:37 +04:00
|
|
|
try {
|
2008-08-16 23:40:59 +04:00
|
|
|
$relation = $classMetadata->getAssociationMapping($relationName);
|
|
|
|
$targetClassMetadata = $this->_em->getClassMetadata($relation->getTargetEntityName());
|
2008-05-24 22:18:37 +04:00
|
|
|
|
2008-08-22 00:45:19 +04:00
|
|
|
$this->_queryComponent = array(
|
2008-08-16 23:40:59 +04:00
|
|
|
'metadata' => $targetClassMetadata,
|
2008-05-24 22:18:37 +04:00
|
|
|
'parent' => $parent,
|
|
|
|
'relation' => $relation,
|
|
|
|
'map' => null,
|
2008-05-27 07:44:08 +04:00
|
|
|
'scalar' => null,
|
2008-05-24 22:18:37 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
$parent = $path;
|
|
|
|
} catch (Doctrine_Exception $e) {
|
2008-06-08 20:42:55 +04:00
|
|
|
//echo "Tried to load class metadata from '".$relationName."'\n";
|
2008-05-24 22:18:37 +04:00
|
|
|
$this->_parser->semanticalError($e->getMessage());
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->_identificationVariable === null) {
|
|
|
|
$this->_identificationVariable = $path;
|
|
|
|
}
|
|
|
|
|
2008-08-16 23:40:59 +04:00
|
|
|
$tableAlias = $parserResult->generateTableAlias($targetClassMetadata->getClassName());
|
2008-06-02 19:00:50 +04:00
|
|
|
|
2008-08-22 00:45:19 +04:00
|
|
|
$parserResult->setQueryComponent($this->_identificationVariable, $this->_queryComponent);
|
2008-05-24 22:18:37 +04:00
|
|
|
$parserResult->setTableAlias($tableAlias, $this->_identificationVariable);
|
|
|
|
}
|
2008-08-22 00:45:19 +04:00
|
|
|
|
|
|
|
|
2008-06-15 19:56:28 +04:00
|
|
|
/* Getters */
|
|
|
|
public function getIdentifiers()
|
|
|
|
{
|
|
|
|
return $this->_identifiers;
|
|
|
|
}
|
2008-08-22 00:45:19 +04:00
|
|
|
|
|
|
|
|
|
|
|
public function getQueryComponent()
|
|
|
|
{
|
|
|
|
return $this->_queryComponent;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-15 19:56:28 +04:00
|
|
|
public function getIdentificationVariable()
|
|
|
|
{
|
|
|
|
return $this->_identificationVariable;
|
|
|
|
}
|
2008-05-24 22:18:37 +04:00
|
|
|
}
|