-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add CTE support to select in QueryBuilder #6621
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
Merged
+417
−1
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6559c32
Add CTE support to select in QueryBuilder
nio-dtp 11280cd
Bind parameters to CTE level
nio-dtp 19b6ad3
Rework naming to use CTE everywhere
nio-dtp aab2338
More tests
nio-dtp 3fdc8d8
Reject any duplicated parameter name
nio-dtp b20f5e6
Rollback bind parameters to CTE level
nio-dtp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Query; | ||
|
||
use function count; | ||
use function sprintf; | ||
|
||
/** @internal */ | ||
final class CommonTableExpression | ||
{ | ||
/** @param string[]|null $columns */ | ||
public function __construct( | ||
public readonly string $name, | ||
public readonly string|QueryBuilder $query, | ||
public readonly ?array $columns, | ||
) { | ||
if ($columns !== null && count($columns) === 0) { | ||
throw new QueryException(sprintf('Columns defined in CTE "%s" should not be an empty array.', $name)); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,13 +18,16 @@ | |
use Doctrine\DBAL\Statement; | ||
use Doctrine\DBAL\Types\Type; | ||
|
||
use function array_filter; | ||
use function array_intersect; | ||
use function array_key_exists; | ||
use function array_keys; | ||
use function array_merge; | ||
use function array_unshift; | ||
use function count; | ||
use function implode; | ||
use function is_object; | ||
use function sprintf; | ||
use function substr; | ||
|
||
/** | ||
|
@@ -160,6 +163,13 @@ class QueryBuilder | |
*/ | ||
private array $unionParts = []; | ||
|
||
/** | ||
* The common table expression parts. | ||
* | ||
* @var CommonTableExpression[] | ||
*/ | ||
private array $commonTableExpressions = []; | ||
|
||
/** | ||
* The query cache profile used for caching results. | ||
*/ | ||
|
@@ -557,6 +567,36 @@ public function addUnion(string|QueryBuilder $part, UnionType $type = UnionType: | |
return $this; | ||
} | ||
|
||
/** | ||
* Add a Common Table Expression to be used for a select query. | ||
* | ||
* <code> | ||
* // WITH cte_name AS (SELECT id AS column1 FROM table_a) | ||
* $qb = $conn->createQueryBuilder() | ||
* ->with('cte_name', 'SELECT id AS column1 FROM table_a'); | ||
* | ||
* // WITH cte_name(column1) AS (SELECT id AS column1 FROM table_a) | ||
* $qb = $conn->createQueryBuilder() | ||
* ->with('cte_name', 'SELECT id AS column1 FROM table_a', ['column1']); | ||
* </code> | ||
* | ||
* @param string $name The name of the CTE | ||
* @param string[]|null $columns The optional columns list to select in the CTE. | ||
* If not provided, the columns are inferred from the CTE. | ||
* | ||
* @return $this This QueryBuilder instance. | ||
* | ||
* @throws QueryException Setting an empty array as columns is not allowed. | ||
*/ | ||
public function with(string $name, string|QueryBuilder $part, ?array $columns = null): self | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe if the outer builder will ignore the parameters bound to the inner one, it shouldn't accept |
||
{ | ||
$this->commonTableExpressions[] = new CommonTableExpression($name, $part, $columns); | ||
|
||
$this->sql = null; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Specifies an item that is to be returned in the query result. | ||
* Replaces any previously specified selections, if any. | ||
|
@@ -1266,7 +1306,15 @@ private function getSQLForSelect(): string | |
throw new QueryException('No SELECT expressions given. Please use select() or addSelect().'); | ||
} | ||
|
||
return $this->connection->getDatabasePlatform() | ||
$databasePlatform = $this->connection->getDatabasePlatform(); | ||
$selectParts = []; | ||
if (count($this->commonTableExpressions) > 0) { | ||
$selectParts[] = $databasePlatform | ||
->createWithSQLBuilder() | ||
->buildSQL(...$this->commonTableExpressions); | ||
} | ||
|
||
$selectParts[] = $databasePlatform | ||
->createSelectSQLBuilder() | ||
->buildSQL( | ||
new SelectQuery( | ||
|
@@ -1281,6 +1329,8 @@ private function getSQLForSelect(): string | |
$this->forUpdate, | ||
), | ||
); | ||
|
||
return implode(' ', $selectParts); | ||
} | ||
|
||
/** | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\SQL\Builder; | ||
|
||
use Doctrine\DBAL\Query\CommonTableExpression; | ||
|
||
use function array_merge; | ||
use function count; | ||
use function implode; | ||
|
||
final class WithSQLBuilder | ||
{ | ||
public function buildSQL(CommonTableExpression $firstExpression, CommonTableExpression ...$otherExpressions): string | ||
{ | ||
$cteParts = []; | ||
|
||
foreach (array_merge([$firstExpression], $otherExpressions) as $part) { | ||
$ctePart = [$part->name]; | ||
if ($part->columns !== null && count($part->columns) > 0) { | ||
$ctePart[] = ' (' . implode(', ', $part->columns) . ')'; | ||
} | ||
|
||
$ctePart[] = ' AS (' . $part->query . ')'; | ||
$cteParts[] = implode('', $ctePart); | ||
} | ||
|
||
return 'WITH ' . implode(', ', $cteParts); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.