Skip to content

Commit

Permalink
Bind parameters to CTE level
Browse files Browse the repository at this point in the history
  • Loading branch information
nio-dtp committed Dec 6, 2024
1 parent 6559c32 commit 11280cd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
41 changes: 39 additions & 2 deletions src/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,51 @@ public function fetchFirstColumn(): array
*/
public function executeQuery(): Result
{
[$params, $types] = $this->boundParameters();

return $this->connection->executeQuery(
$this->getSQL(),
$this->params,
$this->types,
$params,
$types,
$this->resultCacheProfile,
);
}

/**
* Retrieve parameters and types bound to all queries (optional CTEs and main query).
*
* @return array{
* list<mixed>|array<string, mixed>,
* WrapperParameterTypeArray,
* } The parameters and types bound to the CTE queries merged with those bound to the main query.
*/
private function boundParameters(): array
{
if (count($this->withParts) === 0) {
return [$this->params, $this->types];
}

$cteParams = $cteParamsTypes = [];

foreach ($this->withParts as $withPart) {
if (! $withPart->query instanceof self) {
continue;

Check warning on line 340 in src/Query/QueryBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/Query/QueryBuilder.php#L340

Added line #L340 was not covered by tests
}

$cteParams = array_merge($cteParams, $withPart->query->params);
$cteParamsTypes = array_merge($cteParamsTypes, $withPart->query->types);
}

if (count($cteParams) === 0) {
return [$this->params, $this->types];
}

return [
array_merge($cteParams, $this->params),
array_merge($cteParamsTypes, $this->types),
];

Check warning on line 354 in src/Query/QueryBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/Query/QueryBuilder.php#L351-L354

Added lines #L351 - L354 were not covered by tests
}

/**
* Executes an SQL statement and returns the number of affected rows.
*
Expand Down
17 changes: 10 additions & 7 deletions tests/Functional/Query/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,17 +350,17 @@ public function testSelectWithCTENamedParameter(): void
$cteQueryBuilder = $this->connection->createQueryBuilder();
$cteQueryBuilder->select('id AS virtual_id')
->from('for_update')
->where('virtual_id = :id');
->where('virtual_id = :id')
->setParameter('id', 1);

$qb->with('cte_a', $cteQueryBuilder, ['virtual_id'])
->select('virtual_id')
->from('cte_a')
->setParameter('id', 1);
->from('cte_a');

self::assertSame($expectedRows, $qb->executeQuery()->fetchAllAssociative());
}

public function testSelectWithCTEPositionalParameter(): void
public function testSelectWithCTEPositionalParametersBindForEachQuery(): void
{
if (! $this->platformSupportsCTEs()) {
self::markTestSkipped('The database platform does not support CTE.');
Expand All @@ -376,19 +376,22 @@ public function testSelectWithCTEPositionalParameter(): void
$cteQueryBuilder1 = $this->connection->createQueryBuilder();
$cteQueryBuilder1->select('id AS virtual_id')
->from('for_update')
->where($qb->expr()->eq('virtual_id', '?'));
->where($cteQueryBuilder1->expr()->eq('id', '?'))
->setParameter(0, 1, ParameterType::INTEGER);

$cteQueryBuilder2 = $this->connection->createQueryBuilder();
$cteQueryBuilder2->select('id AS virtual_id')
->from('for_update')
->where($qb->expr()->in('id', '?'));
->where($cteQueryBuilder2->expr()->in('id', ':id'))
->setParameter('id', [1, 2], ArrayParameterType::INTEGER);

$qb->with('cte_a', $cteQueryBuilder1, ['virtual_id'])
->with('cte_b', $cteQueryBuilder2, ['virtual_id'])
->select('a.virtual_id')
->from('cte_a', 'a')
->join('a', 'cte_b', 'b', 'a.virtual_id = b.virtual_id')
->setParameters([1, [1, 2]], [ParameterType::INTEGER, ArrayParameterType::INTEGER]);
->where($qb->expr()->eq('a.virtual_id', '?'))
->setParameter(0, 1, ParameterType::INTEGER);

self::assertSame($expectedRows, $qb->executeQuery()->fetchAllAssociative());
}
Expand Down

0 comments on commit 11280cd

Please sign in to comment.