From b5b1b73c60b58969490f554ffee7dc3d3926121c Mon Sep 17 00:00:00 2001 From: Artem Stepin Date: Sun, 28 May 2017 11:18:57 +0200 Subject: [PATCH 1/6] allow arithmetic expressions within IN operator --- docs/en/reference/dql-doctrine-query-language.rst | 2 +- lib/Doctrine/ORM/Query/Parser.php | 4 ++-- lib/Doctrine/ORM/Query/SqlWalker.php | 2 +- .../Tests/ORM/Query/SelectSqlGenerationTest.php | 11 +++++++++++ 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/docs/en/reference/dql-doctrine-query-language.rst b/docs/en/reference/dql-doctrine-query-language.rst index 375d20f6b2b..a94d5a5b958 100644 --- a/docs/en/reference/dql-doctrine-query-language.rst +++ b/docs/en/reference/dql-doctrine-query-language.rst @@ -1712,7 +1712,7 @@ Literal Values .. code-block:: php Literal ::= string | char | integer | float | boolean - InParameter ::= Literal | InputParameter + InParameter ::= ArithmeticExpression | InputParameter Input Parameter ~~~~~~~~~~~~~~~ diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php index 294cda48c1d..04125ab2a3d 100644 --- a/lib/Doctrine/ORM/Query/Parser.php +++ b/lib/Doctrine/ORM/Query/Parser.php @@ -2759,7 +2759,7 @@ public function Literal() } /** - * InParameter ::= Literal | InputParameter + * InParameter ::= ArithmeticExpression | InputParameter * * @return AST\InputParameter|AST\Literal */ @@ -2769,7 +2769,7 @@ public function InParameter() return $this->InputParameter(); } - return $this->Literal(); + return $this->ArithmeticExpression(); } /** diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 2a1139181cb..d896f857285 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -2106,7 +2106,7 @@ public function walkInParameter($inParam) { return $inParam instanceof AST\InputParameter ? $this->walkInputParameter($inParam) - : $this->walkLiteral($inParam); + : $this->walkArithmeticExpression($inParam); } /** diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index 710a03accbf..5b09335c280 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -643,6 +643,17 @@ public function testInvalidInExpressionWithSingleValuedAssociationPathExpression ); } + public function testCustomFunctionWithinInExpression() : void + { + $this->entityManager->getConfiguration()->addCustomStringFunction('FOO', MyAbsFunction::class); + $this->assertSqlGeneration( + "SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u WHERE u.username IN (FOO('Lo'), 'Lo', :name)", + <<<'SQL' +SELECT f0_.id AS id_0, f0_.username AS username_1 FROM forum_users f0_ WHERE f0_.username IN (ABS('Lo'), 'Lo', ?) +SQL + ); + } + public function testSupportsConcatFunctionForMysqlAndPostgresql(): void { $connMock = $this->entityManager->getConnection(); From 9fe65d15ed8cfa8b05e7af525a5b8afdb5440e4b Mon Sep 17 00:00:00 2001 From: David ALLIX Date: Sun, 12 Dec 2021 16:13:57 +0100 Subject: [PATCH 2/6] Review fixes --- lib/Doctrine/ORM/Query/Parser.php | 2 +- tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php index 04125ab2a3d..302769fb553 100644 --- a/lib/Doctrine/ORM/Query/Parser.php +++ b/lib/Doctrine/ORM/Query/Parser.php @@ -2761,7 +2761,7 @@ public function Literal() /** * InParameter ::= ArithmeticExpression | InputParameter * - * @return AST\InputParameter|AST\Literal + * @return AST\InputParameter|AST\ArithmeticExpression */ public function InParameter() { diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index 5b09335c280..04ec464f5b8 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -650,6 +650,12 @@ public function testCustomFunctionWithinInExpression() : void "SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u WHERE u.username IN (FOO('Lo'), 'Lo', :name)", <<<'SQL' SELECT f0_.id AS id_0, f0_.username AS username_1 FROM forum_users f0_ WHERE f0_.username IN (ABS('Lo'), 'Lo', ?) +SQL + ); + $this->assertSqlGeneration( + "SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u WHERE u.id IN (1+1)", + <<<'SQL' +SELECT f0_.id AS id_0, f0_.username AS username_1 FROM forum_users f0_ WHERE f0_.id IN (1+1) SQL ); } From 3c91ace58cc51604ba5b92fa729777cf5589d1d4 Mon Sep 17 00:00:00 2001 From: David ALLIX Date: Sun, 12 Dec 2021 20:08:51 +0100 Subject: [PATCH 3/6] Fixes --- tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index 04ec464f5b8..dfca3d6a163 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -653,9 +653,9 @@ public function testCustomFunctionWithinInExpression() : void SQL ); $this->assertSqlGeneration( - "SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u WHERE u.id IN (1+1)", + 'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u WHERE u.id IN (1+1)', <<<'SQL' -SELECT f0_.id AS id_0, f0_.username AS username_1 FROM forum_users f0_ WHERE f0_.id IN (1+1) +SELECT f0_.id AS id_0, f0_.username AS username_1 FROM forum_users f0_ WHERE f0_.id IN (1 + 1) SQL ); } From e1bda972976062e13fea86b83c995b3922358bb7 Mon Sep 17 00:00:00 2001 From: David ALLIX Date: Tue, 14 Dec 2021 22:35:45 +0100 Subject: [PATCH 4/6] Add functional tests --- .../ORM/Functional/QueryDqlFunctionTest.php | 33 +++++++++++++++++++ .../ORM/Query/SelectSqlGenerationTest.php | 13 +++----- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php b/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php index 42a6a8b67ab..ca77a02a68f 100644 --- a/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php @@ -421,6 +421,39 @@ public function testBitAndComparison(): void self::assertEquals($result[3][0]['salary'] / 100000 & 2, $result[3]['salary_bit_and']); } + public function testInArithmeticExpression1(): void + { + $dql = <<_em->createQuery($dql)->getArrayResult(); + + self::assertCount(2, $result); + self::assertEquals('Roman B.', $result[0]['name']); + self::assertEquals('Benjamin E.', $result[1]['name']); + } + + public function testInArithmeticExpression2(): void + { + $this->_em->getConfiguration()->addCustomStringFunction('FOO', static function ($funcName) { + return new NoOp($funcName); // See Doctrine/Tests/ORM/Functional/CustomFunctionsTest + }); + + $dql = <<_em->createQuery($dql)->getArrayResult(); + + self::assertCount(1, $result); + self::assertEquals('Jonathan W.', $result[0]['name']); + } + protected function generateFixture(): void { $manager1 = new CompanyManager(); diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index dfca3d6a163..efc1cd94c18 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -643,20 +643,17 @@ public function testInvalidInExpressionWithSingleValuedAssociationPathExpression ); } - public function testCustomFunctionWithinInExpression() : void + public function testInExpressionWithArithmeticExpression(): void { $this->entityManager->getConfiguration()->addCustomStringFunction('FOO', MyAbsFunction::class); + $this->assertSqlGeneration( "SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u WHERE u.username IN (FOO('Lo'), 'Lo', :name)", - <<<'SQL' -SELECT f0_.id AS id_0, f0_.username AS username_1 FROM forum_users f0_ WHERE f0_.username IN (ABS('Lo'), 'Lo', ?) -SQL + "SELECT f0_.id AS id_0, f0_.username AS username_1 FROM forum_users f0_ WHERE f0_.username IN (ABS('Lo'), 'Lo', ?)" ); $this->assertSqlGeneration( - 'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u WHERE u.id IN (1+1)', - <<<'SQL' -SELECT f0_.id AS id_0, f0_.username AS username_1 FROM forum_users f0_ WHERE f0_.id IN (1 + 1) -SQL + 'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u WHERE u.id IN (1 + 1)', + 'SELECT f0_.id AS id_0, f0_.username AS username_1 FROM forum_users f0_ WHERE f0_.id IN (1 + 1)' ); } From 44b153acbd9bdd53df793bf7f5e5d5cddb66d7f4 Mon Sep 17 00:00:00 2001 From: David ALLIX Date: Tue, 21 Dec 2021 14:15:37 +0100 Subject: [PATCH 5/6] Fix try --- .../ORM/Functional/QueryDqlFunctionTest.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php b/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php index ca77a02a68f..a4825025eea 100644 --- a/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php @@ -423,27 +423,27 @@ public function testBitAndComparison(): void public function testInArithmeticExpression1(): void { - $dql = <<_em->createQuery($dql)->getArrayResult(); self::assertCount(2, $result); - self::assertEquals('Roman B.', $result[0]['name']); - self::assertEquals('Benjamin E.', $result[1]['name']); + self::assertEquals('Roman B.', $result[0]['m_name']); + self::assertEquals('Benjamin E.', $result[1]['m_name']); } public function testInArithmeticExpression2(): void { $this->_em->getConfiguration()->addCustomStringFunction('FOO', static function ($funcName) { - return new NoOp($funcName); // See Doctrine/Tests/ORM/Functional/CustomFunctionsTest + return new NoOp($funcName); // See Doctrine/Tests/ORM/Functional/CustomFunctionsTest }); - $dql = <<_em->createQuery($dql)->getArrayResult(); self::assertCount(1, $result); - self::assertEquals('Jonathan W.', $result[0]['name']); + self::assertEquals('Jonathan W.', $result[0]['m_name']); } protected function generateFixture(): void From 54df211fefe9a3427713fd685ac6e1312e6848d0 Mon Sep 17 00:00:00 2001 From: David ALLIX Date: Tue, 21 Dec 2021 14:35:13 +0100 Subject: [PATCH 6/6] Fix try --- tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php b/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php index a4825025eea..a9df227ba40 100644 --- a/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php @@ -427,7 +427,7 @@ public function testInArithmeticExpression1(): void SELECT m, m.name AS m_name FROM Doctrine\Tests\Models\Company\CompanyManager m WHERE m.salary IN (800000 / 8, 100000 * 2) - SQL; +SQL; $result = $this->_em->createQuery($dql)->getArrayResult(); @@ -446,7 +446,7 @@ public function testInArithmeticExpression2(): void SELECT m, m.name AS m_name FROM Doctrine\Tests\Models\Company\CompanyManager m WHERE m.department IN (FOO('Administration')) - SQL; +SQL; $result = $this->_em->createQuery($dql)->getArrayResult();