Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CTE support to select in QueryBuilder #6621

Open
wants to merge 6 commits into
base: 4.3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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.
nio-dtp marked this conversation as resolved.
Show resolved Hide resolved
*/
private function boundParameters(): array
nio-dtp marked this conversation as resolved.
Show resolved Hide resolved
{
if (count($this->withParts) === 0) {
return [$this->params, $this->types];
}
morozov marked this conversation as resolved.
Show resolved Hide resolved

$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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code coverage complains about this check not beeing covered.

I'd say additional tests using plain sql strings as parts instead of QueryBuilder instances should be added anyway, and would cover this line here two (guessing).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a test with a sql string as cte query

}

$cteParams = array_merge($cteParams, $withPart->query->params);
nio-dtp marked this conversation as resolved.
Show resolved Hide resolved
$cteParamsTypes = array_merge($cteParamsTypes, $withPart->query->types);
nio-dtp marked this conversation as resolved.
Show resolved Hide resolved
}

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();
morozov marked this conversation as resolved.
Show resolved Hide resolved
$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
Loading