forked from doctrine/dbal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request doctrine#6621 from nio-dtp/cte-support
Add CTE support to select in QueryBuilder
- Loading branch information
Showing
9 changed files
with
417 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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.