Skip to content

Commit

Permalink
SQLParserUtils::expandListParameters functionality extracted into new…
Browse files Browse the repository at this point in the history
… 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 doctrine/dbal#4397
  • Loading branch information
vitek-rostislav committed Mar 30, 2022
1 parent 16b6984 commit fce8442
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
27 changes: 27 additions & 0 deletions src/Component/Doctrine/SqlParametersFlattener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Component\Doctrine;

use Doctrine\DBAL\ExpandArrayParameters;
use Doctrine\DBAL\SQL\Parser;

class SqlParametersFlattener
{
/**
* inspired by @see \Doctrine\DBAL\Connection::expandArrayParameters()
*
* @param string $dql
* @param array<string, mixed> $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();
}
}
9 changes: 4 additions & 5 deletions src/Component/Paginator/QueryPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
49 changes: 49 additions & 0 deletions tests/Unit/Component/Doctrine/SqlParametersFlattenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Tests\FrameworkBundle\Unit\Component\Doctrine;

use Iterator;
use PHPUnit\Framework\TestCase;
use Shopsys\FrameworkBundle\Component\Doctrine\SqlParametersFlattener;

class SqlParametersFlattenerTest extends TestCase
{
/**
* @dataProvider expandArrayParametersDataProvider
* @param string $dql
* @param array $parameters
* @param array $expectedFlattenedParameters
*/
public function testExpandArrayParameters(string $dql, array $parameters, array $expectedFlattenedParameters): void
{
$sqlParametersFlattener = new SqlParametersFlattener();
$actualFlattenedParameters = $sqlParametersFlattener::flattenArrayParameters($dql, $parameters);

$this->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'],
];
}
}

0 comments on commit fce8442

Please sign in to comment.