Skip to content
Browse files

Merge pull request #246 from FabioBatSilva/DDC-1557

[DDC 1557] Support for DQL function on subselect
  • Loading branch information...
2 parents 959a686 + ab4482b commit e43897916a001b6fe9cd8df27d6c9c272f41c854 @guilhermeblanco guilhermeblanco committed Dec 29, 2011
Showing with 47 additions and 3 deletions.
  1. +16 −3 lib/Doctrine/ORM/Query/Parser.php
  2. +31 −0 tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php
View
19 lib/Doctrine/ORM/Query/Parser.php
@@ -1981,9 +1981,10 @@ public function SelectExpression()
}
/**
- * SimpleSelectExpression ::=
- * StateFieldPathExpression | IdentificationVariable |
- * ((AggregateExpression | "(" Subselect ")" | ScalarExpression) [["AS"] AliasResultVariable])
+ * SimpleSelectExpression ::= (
+ * StateFieldPathExpression | IdentificationVariable | FunctionDeclaration |
+ * AggregateExpression | "(" Subselect ")" | ScalarExpression
+ * ) [["AS"] AliasResultVariable]
*
* @return \Doctrine\ORM\Query\AST\SimpleSelectExpression
*/
@@ -2004,6 +2005,18 @@ public function SimpleSelectExpression()
return new AST\SimpleSelectExpression($expression);
+ case ($this->_isFunction()):
+ // SUM(u.id) + COUNT(u.id)
+ if ($this->_isMathOperator($this->_peekBeyondClosingParenthesis())) {
+ return new AST\SimpleSelectExpression($this->ScalarExpression());
+ }
+ // COUNT(u.id)
+ if ($this->_isAggregateFunction($this->_lexer->lookahead['type'])) {
+ return new AST\SimpleSelectExpression($this->AggregateExpression());
+ }
+ // IDENTITY(u)
+ return new AST\SimpleSelectExpression($this->FunctionDeclaration());
+
default:
// Do nothing
}
View
31 tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php
@@ -1434,6 +1434,37 @@ public function testParenthesesOnTheLeftHandOfComparison()
);
}
+ /**
+ * @group DDC-1557
+ */
+ public function testSupportsSubSqlFunction()
+ {
+ $this->assertSqlGeneration(
+ 'SELECT u1 FROM Doctrine\Tests\Models\CMS\CmsUser u1 WHERE u1.name IN ( SELECT TRIM(u2.name) FROM Doctrine\Tests\Models\CMS\CmsUser u2 )',
+ 'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.name IN (SELECT TRIM(c1_.name) AS sclr4 FROM cms_users c1_)'
+ );
+ $this->assertSqlGeneration(
+ 'SELECT u1 FROM Doctrine\Tests\Models\CMS\CmsUser u1 WHERE u1.name IN ( SELECT TRIM(u2.name) FROM Doctrine\Tests\Models\CMS\CmsUser u2 WHERE LOWER(u2.name) LIKE \'%fabio%\')',
+ 'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.name IN (SELECT TRIM(c1_.name) AS sclr4 FROM cms_users c1_ WHERE LOWER(c1_.name) LIKE \'%fabio%\')'
+ );
+ $this->assertSqlGeneration(
+ 'SELECT u1 FROM Doctrine\Tests\Models\CMS\CmsUser u1 WHERE u1.email IN ( SELECT TRIM(IDENTITY(u2.email)) FROM Doctrine\Tests\Models\CMS\CmsUser u2 )',
+ 'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.email_id IN (SELECT TRIM(c1_.email_id) AS sclr4 FROM cms_users c1_)'
+ );
+ $this->assertSqlGeneration(
+ 'SELECT u1 FROM Doctrine\Tests\Models\CMS\CmsUser u1 WHERE u1.email IN ( SELECT IDENTITY(u2.email) FROM Doctrine\Tests\Models\CMS\CmsUser u2 )',
+ 'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.email_id IN (SELECT c1_.email_id AS sclr4 FROM cms_users c1_)'
+ );
+ $this->assertSqlGeneration(
+ 'SELECT u1 FROM Doctrine\Tests\Models\CMS\CmsUser u1 WHERE COUNT(u1.id) = ( SELECT SUM(u2.id) FROM Doctrine\Tests\Models\CMS\CmsUser u2 )',
+ 'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE COUNT(c0_.id) = (SELECT SUM(c1_.id) AS dctrn__1 FROM cms_users c1_)'
+ );
+ $this->assertSqlGeneration(
+ 'SELECT u1 FROM Doctrine\Tests\Models\CMS\CmsUser u1 WHERE COUNT(u1.id) <= ( SELECT SUM(u2.id) + COUNT(u2.email) FROM Doctrine\Tests\Models\CMS\CmsUser u2 )',
+ 'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE COUNT(c0_.id) <= (SELECT SUM(c1_.id) + COUNT(c1_.email_id) AS sclr4 FROM cms_users c1_)'
+ );
+ }
+
public function testCustomTypeValueSql()
{
if (DBALType::hasType('negative_to_positive')) {

0 comments on commit e438979

Please sign in to comment.
Something went wrong with that request. Please try again.