-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CTE support to select in QueryBuilder
- Loading branch information
Showing
7 changed files
with
252 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Query; | ||
|
||
/** @internal */ | ||
final class With | ||
{ | ||
/** @param string[] $fields */ | ||
public function __construct( | ||
public readonly string $name, | ||
public readonly string|QueryBuilder $query, | ||
public readonly array $fields = [], | ||
) { | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Query; | ||
|
||
final class WithQuery | ||
{ | ||
/** | ||
* @internal This class should be instantiated only by {@link QueryBuilder}. | ||
* | ||
* @param With[] $withParts | ||
*/ | ||
public function __construct( | ||
private readonly array $withParts, | ||
) { | ||
} | ||
|
||
/** @return With[] */ | ||
public function withParts(): array | ||
{ | ||
return $this->withParts; | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\SQL\Builder; | ||
|
||
use Doctrine\DBAL\Query\With; | ||
use Doctrine\DBAL\Query\WithQuery; | ||
|
||
use function array_map; | ||
use function count; | ||
use function implode; | ||
use function sprintf; | ||
|
||
final class WithSQLBuilder | ||
{ | ||
public function buildSQL(WithQuery $query): string | ||
{ | ||
if (count($query->withParts()) === 0) { | ||
return ''; | ||
} | ||
|
||
$parts = array_map( | ||
static fn (With $part) => sprintf( | ||
'%s%s AS (%s)', | ||
$part->name, | ||
self::fields($part->fields), | ||
$part->query, | ||
), | ||
$query->withParts(), | ||
); | ||
|
||
return 'WITH ' . implode(', ', $parts) . ' '; | ||
} | ||
|
||
/** @param string[] $fields */ | ||
private static function fields(array $fields): string | ||
{ | ||
return count($fields) > 0 ? '(' . implode(', ', $fields) . ')' : ''; | ||
} | ||
} |
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