8000 Adding ORDER support for UNION queries · thecodingmachine/tdbm@e4346d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit e4346d3

Browse files
mhtghnhomersimpsons
authored andcommitted
Adding ORDER support for UNION queries
1 parent 5c091cd commit e4346d3

File tree

1 file changed

+25
-4
lines changed
Collapse file tree

1 file changed

+25
-4
lines changed

src/QueryFactory/FindObjectsFromRawSqlQueryFactory.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
use Doctrine\DBAL\Platforms\MySqlPlatform;
77
use Doctrine\DBAL\Schema\Schema;
8+
use PHPSQLParser\builders\OrderByBuilder;
9+
use PHPSQLParser\builders\SelectStatementBuilder;
810
use TheCodingMachine\TDBM\TDBMException;
911
use TheCodingMachine\TDBM\TDBMService;
1012
use PHPSQLParser\PHPSQLCreator;
@@ -103,7 +105,7 @@ private function compute(string $sql, ?string $sqlCount): array
103105
* @param mixed[] $parsedSql
104106
* @param null|string $sqlCount
105107
* @return mixed[] An array of 3 elements: [$processedSql, $processedSqlCount, $columnDescriptors]
106-
* @throws \PHPSQLParser\exceptions\UnsupportedFeatureException
108+
* @throws \PHPSQLParser\exceptions\UnsupportedFeatureException|\PHPSQLParser\exceptions\UnableToCreateSQLException
107109
*/
108110
private function processParsedUnionQuery(array $parsedSql, ?string $sqlCount): array
109111
{
@@ -117,9 +119,9 @@ private function processParsedUnionQuery(array $parsedSql, ?string $sqlCount): a
117119

118120
// Let's reparse the returned SQL (not the most efficient way of doing things)
119121
$parser = new PHPSQLParser();
120-
$parsedSql = $parser->parse($selectProcessedSql);
122+
$parsedSelectSql = $parser->parse($selectProcessedSql);
121123

122-
$parsedSqlList[] = $parsedSql;
124+
$parsedSqlList[] = $parsedSelectSql;
123125
}
124126

125127
// Let's rebuild the UNION query
@@ -130,12 +132,31 @@ private function processParsedUnionQuery(array $parsedSql, ?string $sqlCount): a
130132

131133
$generator = new PHPSQLCreator();
132134

133-
$processedSql = $generator->create($query);
135+
// Replaced the default generator by our own to add parenthesis around each SELECT
136+
$processedSql = $this->buildUnion($query);
134137
$processedSqlCount = $generator->create($countQuery);
135138

139+
// Let's add the ORDER BY if any
140+
if (isset($parsedSql['0']['ORDER'])) {
141+
$orderByBuilder = new OrderByBuilder();
142+
$processedSql .= " " . $orderByBuilder->build($parsedSql['0']['ORDER']);
143+
}
144+
136145
return [$processedSql, $sqlCount ?? $processedSqlCount, $columnDescriptors];
137146
}
138147

148+
/**
149+
* @param mixed[] $parsed
150+
*/
151+
private function buildUnion(array $parsed): string
152+
{
153+
$selectBuilder = new SelectStatementBuilder();
154+
155+
return implode(' UNION ', array_map(function ($clause) use ($selectBuilder) {
156+
return '(' . $selectBuilder->build($clause) . ')';
157+
}, $parsed['UNION']));
158+
}
159+
139160
/**
140161
* @param mixed[] $parsedSql
141162
* @param null|string $sqlCount

0 commit comments

Comments
 (0)
0