Skip to content

Commit 6aab32f

Browse files
committed
Fix double SQL finalization
1 parent 77ef1af commit 6aab32f

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

phpcs.xml.dist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@
269269
<rule ref="SlevomatCodingStandard.Classes.EmptyLinesAroundClassBraces"/>
270270
<rule ref="SlevomatCodingStandard.Classes.TraitUseDeclaration" />
271271
<rule ref="SlevomatCodingStandard.Classes.TraitUseSpacing" />
272-
<rule ref="SlevomatCodingStandard.Classes.DisallowConstructorPropertyPromotion" />
273272
<rule ref="SlevomatCodingStandard.Commenting.DocCommentSpacing">
274273
<properties>
275274
<property name="linesCountBeforeFirstContent" value="0"/>
@@ -305,7 +304,6 @@
305304
<rule ref="SlevomatCodingStandard.Functions.StaticClosure"/>
306305
<rule ref="SlevomatCodingStandard.Functions.UselessParameterDefaultValue"/>
307306
<rule ref="SlevomatCodingStandard.Functions.UnusedInheritedVariablePassedToClosure"/>
308-
<rule ref="SlevomatCodingStandard.Functions.DisallowArrowFunction"/>
309307
<rule ref="SlevomatCodingStandard.Namespaces.UseFromSameNamespace"/>
310308
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses">
311309
<properties>

src/HintDrivenSqlWalker.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
use Doctrine\ORM\Query\AST\UpdateStatement;
5555
use Doctrine\ORM\Query\AST\WhereClause;
5656
use Doctrine\ORM\Query\Exec\PreparedExecutorFinalizer;
57-
use Doctrine\ORM\Query\Exec\SingleSelectSqlFinalizer;
5857
use Doctrine\ORM\Query\Exec\SqlFinalizer;
5958
use Doctrine\ORM\Query\OutputWalker;
6059
use Doctrine\ORM\Query\Parser;
@@ -99,7 +98,7 @@ public function getFinalizer(DeleteStatement|UpdateStatement|SelectStatement $AS
9998
{
10099
switch (true) {
101100
case $AST instanceof SelectStatement:
102-
return new SingleSelectSqlFinalizer($this->walkSelectStatement($AST));
101+
return new NoopSqlFinalizer($this->walkSelectStatement($AST));
103102

104103
case $AST instanceof UpdateStatement:
105104
return new PreparedExecutorFinalizer($this->createUpdateStatementExecutor($AST));

src/NoopSqlFinalizer.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ShipMonk\Doctrine\Walker;
4+
5+
use Doctrine\ORM\Query;
6+
use Doctrine\ORM\Query\Exec\AbstractSqlExecutor;
7+
use Doctrine\ORM\Query\Exec\FinalizedSelectExecutor;
8+
use Doctrine\ORM\Query\Exec\SqlFinalizer;
9+
10+
class NoopSqlFinalizer implements SqlFinalizer
11+
{
12+
13+
public function __construct(readonly private string $sql)
14+
{
15+
}
16+
17+
public function createExecutor(Query $query): AbstractSqlExecutor
18+
{
19+
return new FinalizedSelectExecutor($this->sql);
20+
}
21+
22+
}

tests/HintDrivenSqlWalkerTest.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,19 @@ class HintDrivenSqlWalkerTest extends TestCase
2020
{
2121

2222
/**
23+
* @param callable(EntityManager):Query $queryCallback
2324
* @dataProvider walksProvider
2425
*/
2526
public function testWalker(
26-
string $dql,
27+
callable $queryCallback,
2728
string $handlerClass,
2829
mixed $hintValue,
2930
string $expectedSql,
3031
): void
3132
{
3233
$entityManagerMock = $this->createEntityManagerMock();
3334

34-
$query = new Query($entityManagerMock);
35-
$query->setDQL($dql);
36-
35+
$query = $queryCallback($entityManagerMock);
3736
$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, HintDrivenSqlWalker::class);
3837
$query->setHint($handlerClass, $hintValue);
3938
$producedSql = $query->getSQL();
@@ -42,39 +41,52 @@ public function testWalker(
4241
}
4342

4443
/**
45-
* @return Generator<string, array{string, class-string<HintHandler>, mixed, string}>
44+
* @return Generator<string, array{callable(EntityManager):Query, class-string<HintHandler>, mixed, string}>
4645
*/
4746
public static function walksProvider(): iterable
4847
{
4948
$selectDql = sprintf('SELECT w FROM %s w', DummyEntity::class);
5049

5150
yield 'Lowercase select' => [
52-
$selectDql,
51+
static fn(EntityManager $entityManager): Query => $entityManager->createQuery($selectDql),
5352
LowercaseSelectHintHandler::class,
5453
null,
5554
'select d0_.id AS id_0 FROM dummy_entity d0_',
5655
];
5756

5857
yield 'Comment whole sql - select' => [
59-
$selectDql,
58+
static fn(EntityManager $entityManager): Query => $entityManager->createQuery($selectDql),
6059
CommentWholeSqlHintHandler::class,
6160
'custom comment',
6261
'SELECT d0_.id AS id_0 FROM dummy_entity d0_ -- custom comment',
6362
];
6463

6564
yield 'Comment whole sql - update' => [
66-
sprintf('UPDATE %s w SET w.id = 1', DummyEntity::class),
65+
static fn(EntityManager $entityManager): Query => $entityManager->createQuery(sprintf('UPDATE %s w SET w.id = 1', DummyEntity::class)),
6766
CommentWholeSqlHintHandler::class,
6867
'custom comment',
6968
'UPDATE dummy_entity SET id = 1 -- custom comment',
7069
];
7170

7271
yield 'Comment whole sql - delete' => [
73-
sprintf('DELETE FROM %s w', DummyEntity::class),
72+
static fn(EntityManager $entityManager): Query => $entityManager->createQuery(sprintf('DELETE FROM %s w', DummyEntity::class)),
7473
CommentWholeSqlHintHandler::class,
7574
'custom comment',
7675
'DELETE FROM dummy_entity -- custom comment',
7776
];
77+
78+
yield 'Comment whole sql with LIMIT' => [
79+
static function (EntityManager $entityManager): Query {
80+
return $entityManager->createQueryBuilder()
81+
->select('w')
82+
->from(DummyEntity::class, 'w')
83+
->setMaxResults(1)
84+
->getQuery();
85+
},
86+
CommentWholeSqlHintHandler::class,
87+
'custom comment',
88+
'SELECT d0_.id AS id_0 FROM dummy_entity d0_ LIMIT 1 -- custom comment',
89+
];
7890
}
7991

8092
private function createEntityManagerMock(): EntityManager

0 commit comments

Comments
 (0)