1
0
mirror of synced 2025-01-18 06:21:40 +03:00

parse nested new operators

This commit is contained in:
Fabio B. Silva 2012-06-30 19:25:54 -03:00
parent 2b403b7dad
commit 88f04b5ebd
3 changed files with 49 additions and 27 deletions

View File

@ -1680,10 +1680,8 @@ class Parser
return new AST\SimpleSelectExpression($expression);
case ($peek['type'] !== Lexer::T_OPEN_PARENTHESIS):
$expression = $this->IdentificationVariable();
return new AST\SimpleSelectExpression($expression);
case ($this->_lexer->lookahead['type'] === Lexer::T_NEW):
return $this->NewObjectExpression();
case ($this->_isFunction()):
// SUM(u.id) + COUNT(u.id)

View File

@ -1229,28 +1229,7 @@ class SqlWalker implements TreeWalker
break;
case ($expr instanceof AST\NewObjectExpression):
$sqlSelectExpressions = array();
$objIndex = $this->newObjectCounter ++;
foreach ($expr->args as $argIndex => $e) {
$resultAlias = $this->scalarResultCounter++;
$columnAlias = $this->getSQLColumnAlias('sclr') . $resultAlias;
$resultAliasMap = $this->scalarResultAliasMap;
$sqlSelectExpressions[] = $this->walkSimpleSelectExpression($e) . ' AS ' . $columnAlias;
$this->scalarResultAliasMap[$resultAlias] = $columnAlias;
$this->rsm->addScalarResult($columnAlias, $resultAlias, 'string');
$this->rsm->newObjectMappings[$columnAlias] = array(
'className' => $expr->className,
'objIndex' => $objIndex,
'argIndex' => $argIndex
);
}
$sql .= implode(',', $sqlSelectExpressions);
$sql .= $this->walkNewObject($expr);
break;
default:
@ -1419,6 +1398,47 @@ class SqlWalker implements TreeWalker
. $this->walkSimpleSelectExpression($simpleSelectClause->simpleSelectExpression);
}
/**
* @param AST\NewObjectExpression
* @return string The SQL.
*/
public function walkNewObject($newObjectExpression)
{
$sqlSelectExpressions = array();
$objIndex = $this->newObjectCounter ++;
foreach ($newObjectExpression->args as $argIndex => $e) {
$resultAlias = $this->scalarResultCounter++;
$columnAlias = $this->getSQLColumnAlias('sclr') . $resultAlias;
$resultAliasMap = $this->scalarResultAliasMap;
switch (true) {
case $e instanceof AST\NewObjectExpression:
$sqlSelectExpressions[] = $e->dispatch($this);
break;
default:
$sqlSelectExpressions[] = $e->dispatch($this) . ' AS ' . $columnAlias;
break;
}
$this->scalarResultAliasMap[$resultAlias] = $columnAlias;
$this->rsm->addScalarResult($columnAlias, $resultAlias, 'string');
$this->rsm->newObjectMappings[$columnAlias] = array(
'className' => $newObjectExpression->className,
'objIndex' => $objIndex,
'argIndex' => $argIndex
);
}
return implode(',', $sqlSelectExpressions);
}
/**
* Walks down a SimpleSelectExpression AST node, thereby generating the appropriate SQL.
*

View File

@ -129,7 +129,11 @@ class NewOperatorTest extends \Doctrine\Tests\OrmFunctionalTestCase
new Doctrine\Tests\Models\CMS\CmsUserDTO(
u.name,
e.email,
new Doctrine\Tests\Models\CMS\CmsUserDTO(a.country, a.city, a.zip)
new Doctrine\Tests\Models\CMS\CmsUserDTO(
a.country,
a.city,
a.zip
)
)
FROM
Doctrine\Tests\Models\CMS\CmsUser u