From fce844231dda915998609bc5bd3cf8ba46349432 Mon Sep 17 00:00:00 2001 From: Rostislav Vitek Date: Fri, 25 Mar 2022 23:08:38 +0100 Subject: [PATCH] SQLParserUtils::expandListParameters functionality extracted into new SqlParametersFlattener - the original method is not available anymore (it has been moved to Connection and made private) in the new version of doctrine/dbal - see https://github.com/doctrine/dbal/pull/4397 --- .../Doctrine/SqlParametersFlattener.php | 27 ++++++++++ src/Component/Paginator/QueryPaginator.php | 9 ++-- .../Doctrine/SqlParametersFlattenerTest.php | 49 +++++++++++++++++++ 3 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 src/Component/Doctrine/SqlParametersFlattener.php create mode 100644 tests/Unit/Component/Doctrine/SqlParametersFlattenerTest.php diff --git a/src/Component/Doctrine/SqlParametersFlattener.php b/src/Component/Doctrine/SqlParametersFlattener.php new file mode 100644 index 0000000000..51c9057198 --- /dev/null +++ b/src/Component/Doctrine/SqlParametersFlattener.php @@ -0,0 +1,27 @@ + $parameters + * @return array + */ + public static function flattenArrayParameters(string $dql, array $parameters): array + { + $parser = new Parser(false); + $visitor = new ExpandArrayParameters($parameters, []); + $parser->parse($dql, $visitor); + + return $visitor->getParameters(); + } +} diff --git a/src/Component/Paginator/QueryPaginator.php b/src/Component/Paginator/QueryPaginator.php index 08e50d010a..736fefb809 100644 --- a/src/Component/Paginator/QueryPaginator.php +++ b/src/Component/Paginator/QueryPaginator.php @@ -2,11 +2,11 @@ namespace Shopsys\FrameworkBundle\Component\Paginator; -use Doctrine\DBAL\SQLParserUtils; use Doctrine\ORM\Query; use Doctrine\ORM\Query\ResultSetMapping; use Doctrine\ORM\QueryBuilder; use Shopsys\FrameworkBundle\Component\Doctrine\SortableNullsWalker; +use Shopsys\FrameworkBundle\Component\Doctrine\SqlParametersFlattener; class QueryPaginator implements PaginatorInterface { @@ -99,10 +99,9 @@ protected function getTotalNativeQuery(QueryBuilder $queryBuilder) $parametersAssoc[$parameter->getName()] = $parameter->getValue(); } - list(, $flatenedParameters) = SQLParserUtils::expandListParameters( + $flattenedParameters = SqlParametersFlattener::flattenArrayParameters( $query->getDQL(), - $parametersAssoc, - [] + $parametersAssoc ); // COUNT() returns BIGINT which is hydrated into string on 32-bit architecture @@ -111,6 +110,6 @@ protected function getTotalNativeQuery(QueryBuilder $queryBuilder) $rsm = new ResultSetMapping(); $rsm->addScalarResult('total_count', 'totalCount'); return $em->createNativeQuery($sql, $rsm) - ->setParameters($flatenedParameters); + ->setParameters($flattenedParameters); } } diff --git a/tests/Unit/Component/Doctrine/SqlParametersFlattenerTest.php b/tests/Unit/Component/Doctrine/SqlParametersFlattenerTest.php new file mode 100644 index 0000000000..06e2795ee7 --- /dev/null +++ b/tests/Unit/Component/Doctrine/SqlParametersFlattenerTest.php @@ -0,0 +1,49 @@ +assertSame($expectedFlattenedParameters, $actualFlattenedParameters); + } + + /** + * @return \Iterator + */ + public function expandArrayParametersDataProvider(): Iterator + { + yield [ + 'dql' => 'SELECT a FROM Article WHERE id = :id AND name = :name', + 'parameters' => [ + 'id' => 1, + 'name' => 'name', + ], + 'expectedFlattenedParameters' => [1, 'name'], + ]; + yield [ + 'dql' => 'SELECT a FROM Article WHERE id = :id AND name = :name', + 'parameters' => [ + 'name' => 'name', + 'id' => 1, + ], + 'expectedFlattenedParameters' => [1, 'name'], + ]; + } +}