From a7f3af8328b031a6f006eef1062554bf9f57e1df Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Thu, 8 Sep 2011 02:10:48 -0300 Subject: [PATCH] Added IDENTITY DQL Function. Fixes DDC-1339. --- .../Query/AST/Functions/IdentityFunction.php | 68 +++++++++++++++++++ lib/Doctrine/ORM/Query/Parser.php | 3 +- .../ORM/Query/SelectSqlGenerationTest.php | 22 ++++++ 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 lib/Doctrine/ORM/Query/AST/Functions/IdentityFunction.php diff --git a/lib/Doctrine/ORM/Query/AST/Functions/IdentityFunction.php b/lib/Doctrine/ORM/Query/AST/Functions/IdentityFunction.php new file mode 100644 index 000000000..10c7982c5 --- /dev/null +++ b/lib/Doctrine/ORM/Query/AST/Functions/IdentityFunction.php @@ -0,0 +1,68 @@ +. + */ + +namespace Doctrine\ORM\Query\AST\Functions; + +use Doctrine\ORM\Query\Lexer; + +/** + * "IDENTITY" "(" SingleValuedAssociationPathExpression ")" + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.org + * @since 2.2 + * @author Guilherme Blanco + * @author Benjamin Eberlei + */ +class IdentityFunction extends FunctionNode +{ + public $pathExpression; + + /** + * @override + */ + public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) + { + $platform = $sqlWalker->getConnection()->getDatabasePlatform(); + $dqlAlias = $this->pathExpression->identificationVariable; + $assocField = $this->pathExpression->field; + + $qComp = $sqlWalker->getQueryComponent($dqlAlias); + $class = $qComp['metadata']; + $assoc = $class->associationMappings[$assocField]; + + $tableAlias = $sqlWalker->getSQLTableAlias($class->getTableName(), $dqlAlias); + + return $tableAlias . '.' . reset($assoc['targetToSourceKeyColumns']);; + } + + /** + * @override + */ + public function parse(\Doctrine\ORM\Query\Parser $parser) + { + $parser->match(Lexer::T_IDENTIFIER); + $parser->match(Lexer::T_OPEN_PARENTHESIS); + + $this->pathExpression = $parser->SingleValuedAssociationPathExpression(); + + $parser->match(Lexer::T_CLOSE_PARENTHESIS); + } +} + diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php index a8ddfd879..5083bf5af 100644 --- a/lib/Doctrine/ORM/Query/Parser.php +++ b/lib/Doctrine/ORM/Query/Parser.php @@ -40,7 +40,8 @@ class Parser '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' + 'upper' => 'Doctrine\ORM\Query\AST\Functions\UpperFunction', + 'identity' => 'Doctrine\ORM\Query\AST\Functions\IdentityFunction', ); /** READ-ONLY: Maps BUILT-IN numeric function names to AST class names. */ diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index 63a885497..1846653e6 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -1041,6 +1041,28 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase "SELECT c0_.id AS id0, c0_.name AS name1 FROM cms_groups c0_ WHERE c0_.id = CASE c0_.name WHEN admin THEN 1 WHEN moderator THEN 2 ELSE 3 END" ); } + + /** + * @group DDC-1339 + */ + public function testIdentityFunctionInSelectClause() + { + $this->assertSqlGeneration( + "SELECT IDENTITY(u.email) as email_id FROM Doctrine\Tests\Models\CMS\CmsUser u", + "SELECT c0_.email_id AS sclr0 FROM cms_users c0_" + ); + } + + /** + * @group DDC-1339 + */ + public function testIdentityFunctionDoesNotAcceptStateField() + { + $this->assertInvalidSqlGeneration( + "SELECT IDENTITY(u.name) as name FROM Doctrine\Tests\Models\CMS\CmsUser u", + "Doctrine\ORM\Query\QueryException" + ); + } }