From 496a34a4d246f7b3e628e37963d8950e43540733 Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Thu, 12 Aug 2010 00:16:07 -0300 Subject: [PATCH] [DDC-581] Implemented support to SingleValuedPathExpression to InExpression. --- lib/Doctrine/ORM/Query/Parser.php | 4 +- .../ORM/Query/LanguageRecognitionTest.php | 10 ++++ .../ORM/Query/SelectSqlGenerationTest.php | 58 +++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php index 5015394f7..8aa82e9dd 100644 --- a/lib/Doctrine/ORM/Query/Parser.php +++ b/lib/Doctrine/ORM/Query/Parser.php @@ -2421,13 +2421,13 @@ class Parser } /** - * InExpression ::= StateFieldPathExpression ["NOT"] "IN" "(" (InParameter {"," InParameter}* | Subselect) ")" + * InExpression ::= SingleValuedPathExpression ["NOT"] "IN" "(" (InParameter {"," InParameter}* | Subselect) ")" * * @return \Doctrine\ORM\Query\AST\InExpression */ public function InExpression() { - $inExpression = new AST\InExpression($this->StateFieldPathExpression()); + $inExpression = new AST\InExpression($this->SingleValuedPathExpression()); if ($this->_lexer->isNextToken(Lexer::T_NOT)) { $this->match(Lexer::T_NOT); diff --git a/tests/Doctrine/Tests/ORM/Query/LanguageRecognitionTest.php b/tests/Doctrine/Tests/ORM/Query/LanguageRecognitionTest.php index e80e78144..aab94c9dc 100644 --- a/tests/Doctrine/Tests/ORM/Query/LanguageRecognitionTest.php +++ b/tests/Doctrine/Tests/ORM/Query/LanguageRecognitionTest.php @@ -135,6 +135,16 @@ class LanguageRecognitionTest extends \Doctrine\Tests\OrmTestCase $this->assertValidDql('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id NOT IN (1)'); } + public function testInExpressionWithSingleValuedAssociationPathExpression() + { + $this->assertValidDql("SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u WHERE u.avatar IN (?1, ?2)"); + } + + public function testInvalidInExpressionWithCollectionValuedAssociationPathExpression() + { + $this->assertInvalidDql("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.phonenumbers IN (?1, ?2)"); + } + public function testInstanceOfExpressionSupportedInWherePart() { $this->assertValidDql('SELECT u FROM Doctrine\Tests\Models\Company\CompanyPerson u WHERE u INSTANCE OF Doctrine\Tests\Models\Company\CompanyEmployee'); diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index 24be336a3..2e0ff090d 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -15,6 +15,14 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase $this->_em = $this->_getTestEntityManager(); } + /** + * Assert a valid SQL generation. + * + * @param string $dqlToBeTested + * @param string $sqlToBeConfirmed + * @param array $queryHints + * @param array $queryParams + */ public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed, array $queryHints = array(), array $queryParams = array()) { try { @@ -38,6 +46,39 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase } } + /** + * Asser an invalid SQL generation. + * + * @param string $dqlToBeTested + * @param string $expectedException + * @param array $queryHints + * @param array $queryParams + */ + public function assertInvalidSqlGeneration($dqlToBeTested, $expectedException, array $queryHints = array(), array $queryParams = array()) + { + $this->setExpectedException($expectedException); + + $query = $this->_em->createQuery($dqlToBeTested); + + foreach ($queryParams AS $name => $value) { + $query->setParameter($name, $value); + } + + $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true) + ->useQueryCache(false); + + foreach ($queryHints AS $name => $value) { + $query->setHint($name, $value); + } + + $sql = $query->getSql(); + $query->free(); + + // If we reached here, test failed + $this->fail($sql); + } + + public function testSupportsSelectForAllFields() { $this->assertSqlGeneration( @@ -374,6 +415,23 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase ); } + public function testInExpressionWithSingleValuedAssociationPathExpressionInWherePart() + { + $this->assertSqlGeneration( + 'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u WHERE u.avatar IN (?1, ?2)', + 'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ WHERE f0_.avatar_id IN (?, ?)' + ); + } + + public function testInvalidInExpressionWithSingleValuedAssociationPathExpressionOnInverseSide() + { + // We do not support SingleValuedAssociationPathExpression on inverse side + $this->assertInvalidSqlGeneration( + "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.address IN (?1, ?2)", + "Doctrine\ORM\Query\QueryException" + ); + } + public function testSupportsConcatFunctionForMysqlAndPostgresql() { $connMock = $this->_em->getConnection();