1
0
mirror of synced 2025-01-06 00:57:10 +03:00

Implemented HIDDEN support in DQL. Fixes DDC-1363.

This commit is contained in:
Guilherme Blanco 2011-10-03 02:07:07 -03:00
parent 8efae0b232
commit ebe933810e
6 changed files with 110 additions and 46 deletions

View File

@ -23,7 +23,7 @@ namespace Doctrine\ORM\Query\AST;
/** /**
* SelectExpression ::= IdentificationVariable ["." "*"] | StateFieldPathExpression | * SelectExpression ::= IdentificationVariable ["." "*"] | StateFieldPathExpression |
* (AggregateExpression | "(" Subselect ")") [["AS"] FieldAliasIdentificationVariable] * (AggregateExpression | "(" Subselect ")") [["AS"] ["HIDDEN"] FieldAliasIdentificationVariable]
* *
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org * @link www.doctrine-project.org
@ -36,11 +36,13 @@ namespace Doctrine\ORM\Query\AST;
class SelectExpression extends Node class SelectExpression extends Node
{ {
public $expression; public $expression;
public $hiddenAliasResultVariable;
public $fieldIdentificationVariable; public $fieldIdentificationVariable;
public function __construct($expression, $fieldIdentificationVariable) public function __construct($expression, $hiddenAliasResultVariable, $fieldIdentificationVariable)
{ {
$this->expression = $expression; $this->expression = $expression;
$this->hiddenAliasResultVariable = $hiddenAliasResultVariable;
$this->fieldIdentificationVariable = $fieldIdentificationVariable; $this->fieldIdentificationVariable = $fieldIdentificationVariable;
} }

View File

@ -76,39 +76,40 @@ class Lexer extends \Doctrine\Common\Lexer
const T_FROM = 122; const T_FROM = 122;
const T_GROUP = 123; const T_GROUP = 123;
const T_HAVING = 124; const T_HAVING = 124;
const T_IN = 125; const T_HIDDEN = 125;
const T_INDEX = 126; const T_IN = 126;
const T_INNER = 127; const T_INDEX = 127;
const T_INSTANCE = 128; const T_INNER = 128;
const T_IS = 129; const T_INSTANCE = 129;
const T_JOIN = 130; const T_IS = 130;
const T_LEADING = 131; const T_JOIN = 131;
const T_LEFT = 132; const T_LEADING = 132;
const T_LIKE = 133; const T_LEFT = 133;
const T_MAX = 134; const T_LIKE = 134;
const T_MEMBER = 135; const T_MAX = 135;
const T_MIN = 136; const T_MEMBER = 136;
const T_NOT = 137; const T_MIN = 137;
const T_NULL = 138; const T_NOT = 138;
const T_NULLIF = 139; const T_NULL = 139;
const T_OF = 140; const T_NULLIF = 140;
const T_OR = 141; const T_OF = 141;
const T_ORDER = 142; const T_OR = 142;
const T_OUTER = 143; const T_ORDER = 143;
const T_SELECT = 144; const T_OUTER = 144;
const T_SET = 145; const T_SELECT = 145;
const T_SIZE = 146; const T_SET = 146;
const T_SOME = 147; const T_SIZE = 147;
const T_SUM = 148; const T_SOME = 148;
const T_THEN = 149; const T_SUM = 149;
const T_TRAILING = 150; const T_THEN = 150;
const T_TRUE = 151; const T_TRAILING = 151;
const T_UPDATE = 152; const T_TRUE = 152;
const T_WHEN = 153; const T_UPDATE = 153;
const T_WHERE = 154; const T_WHEN = 154;
const T_WITH = 155; const T_WHERE = 155;
const T_PARTIAL = 156; const T_WITH = 156;
const T_MOD = 157; const T_PARTIAL = 157;
const T_MOD = 158;
/** /**
* Creates a new query scanner object. * Creates a new query scanner object.

View File

@ -1831,7 +1831,7 @@ class Parser
/** /**
* SelectExpression ::= * SelectExpression ::=
* IdentificationVariable | StateFieldPathExpression | * IdentificationVariable | StateFieldPathExpression |
* (AggregateExpression | "(" Subselect ")" | ScalarExpression) [["AS"] AliasResultVariable] * (AggregateExpression | "(" Subselect ")" | ScalarExpression) [["AS"] ["HIDDEN"] AliasResultVariable]
* *
* @return Doctrine\ORM\Query\AST\SelectExpression * @return Doctrine\ORM\Query\AST\SelectExpression
*/ */
@ -1839,6 +1839,7 @@ class Parser
{ {
$expression = null; $expression = null;
$identVariable = null; $identVariable = null;
$hiddenAliasResultVariable = false;
$fieldAliasIdentificationVariable = null; $fieldAliasIdentificationVariable = null;
$peek = $this->_lexer->glimpse(); $peek = $this->_lexer->glimpse();
@ -1901,6 +1902,12 @@ class Parser
$this->match(Lexer::T_AS); $this->match(Lexer::T_AS);
} }
if ($this->_lexer->isNextToken(Lexer::T_HIDDEN)) {
$this->match(Lexer::T_HIDDEN);
$hiddenAliasResultVariable = true;
}
if ($this->_lexer->isNextToken(Lexer::T_IDENTIFIER)) { if ($this->_lexer->isNextToken(Lexer::T_IDENTIFIER)) {
$token = $this->_lexer->lookahead; $token = $this->_lexer->lookahead;
$fieldAliasIdentificationVariable = $this->AliasResultVariable(); $fieldAliasIdentificationVariable = $this->AliasResultVariable();
@ -1914,10 +1921,12 @@ class Parser
} }
} }
$expr = new AST\SelectExpression($expression, $fieldAliasIdentificationVariable); $expr = new AST\SelectExpression($expression, $hiddenAliasResultVariable, $fieldAliasIdentificationVariable);
if ( ! $supportsAlias) { if ( ! $supportsAlias) {
$this->_identVariableExpressions[$identVariable] = $expr; $this->_identVariableExpressions[$identVariable] = $expr;
} }
return $expr; return $expr;
} }

View File

@ -977,6 +977,7 @@ class SqlWalker implements TreeWalker
{ {
$sql = ''; $sql = '';
$expr = $selectExpression->expression; $expr = $selectExpression->expression;
$hidden = $selectExpression->hiddenAliasResultVariable;
if ($expr instanceof AST\PathExpression) { if ($expr instanceof AST\PathExpression) {
if ($expr->type !== AST\PathExpression::TYPE_STATE_FIELD) { if ($expr->type !== AST\PathExpression::TYPE_STATE_FIELD) {
@ -1006,7 +1007,10 @@ class SqlWalker implements TreeWalker
$columnAlias = $this->getSQLColumnAlias($columnName); $columnAlias = $this->getSQLColumnAlias($columnName);
$sql .= $sqlTableAlias . '.' . $columnName . ' AS ' . $columnAlias; $sql .= $sqlTableAlias . '.' . $columnName . ' AS ' . $columnAlias;
$columnAlias = $this->_platform->getSQLResultCasing($columnAlias); $columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
if ( ! $hidden) {
$this->_rsm->addScalarResult($columnAlias, $resultAlias); $this->_rsm->addScalarResult($columnAlias, $resultAlias);
}
} else if ($expr instanceof AST\AggregateExpression) { } else if ($expr instanceof AST\AggregateExpression) {
if ( ! $selectExpression->fieldIdentificationVariable) { if ( ! $selectExpression->fieldIdentificationVariable) {
$resultAlias = $this->_scalarResultCounter++; $resultAlias = $this->_scalarResultCounter++;
@ -1019,7 +1023,10 @@ class SqlWalker implements TreeWalker
$this->_scalarResultAliasMap[$resultAlias] = $columnAlias; $this->_scalarResultAliasMap[$resultAlias] = $columnAlias;
$columnAlias = $this->_platform->getSQLResultCasing($columnAlias); $columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
if ( ! $hidden) {
$this->_rsm->addScalarResult($columnAlias, $resultAlias); $this->_rsm->addScalarResult($columnAlias, $resultAlias);
}
} else if ($expr instanceof AST\Subselect) { } else if ($expr instanceof AST\Subselect) {
if ( ! $selectExpression->fieldIdentificationVariable) { if ( ! $selectExpression->fieldIdentificationVariable) {
$resultAlias = $this->_scalarResultCounter++; $resultAlias = $this->_scalarResultCounter++;
@ -1032,7 +1039,10 @@ class SqlWalker implements TreeWalker
$this->_scalarResultAliasMap[$resultAlias] = $columnAlias; $this->_scalarResultAliasMap[$resultAlias] = $columnAlias;
$columnAlias = $this->_platform->getSQLResultCasing($columnAlias); $columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
if ( ! $hidden) {
$this->_rsm->addScalarResult($columnAlias, $resultAlias); $this->_rsm->addScalarResult($columnAlias, $resultAlias);
}
} else if ($expr instanceof AST\Functions\FunctionNode) { } else if ($expr instanceof AST\Functions\FunctionNode) {
if ( ! $selectExpression->fieldIdentificationVariable) { if ( ! $selectExpression->fieldIdentificationVariable) {
$resultAlias = $this->_scalarResultCounter++; $resultAlias = $this->_scalarResultCounter++;
@ -1045,7 +1055,10 @@ class SqlWalker implements TreeWalker
$this->_scalarResultAliasMap[$resultAlias] = $columnAlias; $this->_scalarResultAliasMap[$resultAlias] = $columnAlias;
$columnAlias = $this->_platform->getSQLResultCasing($columnAlias); $columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
if ( ! $hidden) {
$this->_rsm->addScalarResult($columnAlias, $resultAlias); $this->_rsm->addScalarResult($columnAlias, $resultAlias);
}
} else if ( } else if (
$expr instanceof AST\SimpleArithmeticExpression || $expr instanceof AST\SimpleArithmeticExpression ||
$expr instanceof AST\ArithmeticTerm || $expr instanceof AST\ArithmeticTerm ||
@ -1070,7 +1083,10 @@ class SqlWalker implements TreeWalker
$this->_scalarResultAliasMap[$resultAlias] = $columnAlias; $this->_scalarResultAliasMap[$resultAlias] = $columnAlias;
$columnAlias = $this->_platform->getSQLResultCasing($columnAlias); $columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
if ( ! $hidden) {
$this->_rsm->addScalarResult($columnAlias, $resultAlias); $this->_rsm->addScalarResult($columnAlias, $resultAlias);
}
} else if ( } else if (
$expr instanceof AST\NullIfExpression || $expr instanceof AST\NullIfExpression ||
$expr instanceof AST\CoalesceExpression || $expr instanceof AST\CoalesceExpression ||
@ -1090,7 +1106,10 @@ class SqlWalker implements TreeWalker
$this->_scalarResultAliasMap[$resultAlias] = $columnAlias; $this->_scalarResultAliasMap[$resultAlias] = $columnAlias;
$columnAlias = $this->_platform->getSQLResultCasing($columnAlias); $columnAlias = $this->_platform->getSQLResultCasing($columnAlias);
if ( ! $hidden) {
$this->_rsm->addScalarResult($columnAlias, $resultAlias); $this->_rsm->addScalarResult($columnAlias, $resultAlias);
}
} else { } else {
// IdentificationVariable or PartialObjectExpression // IdentificationVariable or PartialObjectExpression
if ($expr instanceof AST\PartialObjectExpression) { if ($expr instanceof AST\PartialObjectExpression) {

View File

@ -399,14 +399,17 @@ public function <methodName>()
} }
$collections = array(); $collections = array();
foreach ($metadata->associationMappings AS $mapping) { foreach ($metadata->associationMappings AS $mapping) {
if ($mapping['type'] & ClassMetadataInfo::TO_MANY) { if ($mapping['type'] & ClassMetadataInfo::TO_MANY) {
$collections[] = '$this->'.$mapping['fieldName'].' = new \Doctrine\Common\Collections\ArrayCollection();'; $collections[] = '$this->'.$mapping['fieldName'].' = new \Doctrine\Common\Collections\ArrayCollection();';
} }
} }
if ($collections) { if ($collections) {
return $this->_prefixCodeWithSpaces(str_replace("<collections>", implode("\n", $collections), self::$_constructorMethodTemplate)); return $this->_prefixCodeWithSpaces(str_replace("<collections>", implode("\n", $collections), self::$_constructorMethodTemplate));
} }
return ''; return '';
} }

View File

@ -533,4 +533,34 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertEquals(2, count($users)); $this->assertEquals(2, count($users));
} }
public function testQueryWithHiddenAsSelectExpression()
{
$userA = new CmsUser;
$userA->name = 'Benjamin';
$userA->username = 'beberlei';
$userA->status = 'developer';
$this->_em->persist($userA);
$userB = new CmsUser;
$userB->name = 'Roman';
$userB->username = 'romanb';
$userB->status = 'developer';
$this->_em->persist($userB);
$userC = new CmsUser;
$userC->name = 'Jonathan';
$userC->username = 'jwage';
$userC->status = 'developer';
$this->_em->persist($userC);
$this->_em->flush();
$this->_em->clear();
$query = $this->_em->createQuery("SELECT u, (SELECT COUNT(u2.id) FROM Doctrine\Tests\Models\CMS\CmsUser u2) AS HIDDEN total FROM Doctrine\Tests\Models\CMS\CmsUser u");
$users = $query->execute();
$this->assertEquals(3, count($users));
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $users[0]);
}
} }