Skip to content

Commit 7f5b0ec

Browse files
vranaforrest79
authored andcommitted
Fluent: allow Db\Sql in distinctOn
1 parent 3ed8260 commit 7f5b0ec

File tree

6 files changed

+37
-25
lines changed

6 files changed

+37
-25
lines changed

phpstan.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ parameters:
192192
count: 1
193193

194194
-
195-
message: "#^Offset 'prefix'\\|'suffix' on array\\{select\\: array\\<int\\|string, BackedEnum\\|Forrest79\\\\PhPgSql\\\\Db\\\\Sql\\|Forrest79\\\\PhPgSql\\\\Fluent\\\\Query\\|int\\|string\\>, distinct\\: bool, distinctOn\\: list\\<string\\>, tables\\: array\\<string, array\\{string, string\\}\\>, table\\-types\\: array\\{main\\: string\\|null, from\\: list\\<string\\>, joins\\: list\\<string\\>, using\\: string\\|null\\}, on\\-conditions\\: array\\<string, Forrest79\\\\PhPgSql\\\\Fluent\\\\Complex\\>, lateral\\-tables\\: array\\<string, string\\>, where\\: Forrest79\\\\PhPgSql\\\\Fluent\\\\Complex\\|null, \\.\\.\\.\\} on left side of \\?\\? always exists and is not nullable\\.$#"
195+
message: "#^Offset 'prefix'\\|'suffix' on array\\{select\\: array\\<int\\|string, BackedEnum\\|Forrest79\\\\PhPgSql\\\\Db\\\\Sql\\|Forrest79\\\\PhPgSql\\\\Fluent\\\\Query\\|int\\|string\\>, distinct\\: bool, distinctOn\\: list\\<Forrest79\\\\PhPgSql\\\\Db\\\\Sql\\|Forrest79\\\\PhPgSql\\\\Fluent\\\\Query\\|string\\>, tables\\: array\\<string, array\\{string, string\\}\\>, table\\-types\\: array\\{main\\: string\\|null, from\\: list\\<string\\>, joins\\: list\\<string\\>, using\\: string\\|null\\}, on\\-conditions\\: array\\<string, Forrest79\\\\PhPgSql\\\\Fluent\\\\Complex\\>, lateral\\-tables\\: array\\<string, string\\>, where\\: Forrest79\\\\PhPgSql\\\\Fluent\\\\Complex\\|null, \\.\\.\\.\\} on left side of \\?\\? always exists and is not nullable\\.$#"
196196
path: %rootDir%/../../../src/Fluent/QueryBuilder.php
197197
count: 1
198198

@@ -226,7 +226,7 @@ parameters:
226226
# === PHPStan imperfection ===
227227

228228
-
229-
message: "#^Property Forrest79\\\\PhPgSql\\\\Fluent\\\\Query\\:\\:\\$params \\(array\\{select\\: array\\<int\\|string, BackedEnum\\|Forrest79\\\\PhPgSql\\\\Db\\\\Sql\\|Forrest79\\\\PhPgSql\\\\Fluent\\\\Query\\|int\\|string\\>, distinct\\: bool, distinctOn\\: list\\<string\\>, tables\\: array\\<string, array\\{string, string\\}\\>, table\\-types\\: array\\{main\\: string\\|null, from\\: list\\<string\\>, joins\\: list\\<string\\>, using\\: string\\|null\\}, on\\-conditions\\: array\\<string, Forrest79\\\\PhPgSql\\\\Fluent\\\\Complex\\>, lateral\\-tables\\: array\\<string, string\\>, where\\: Forrest79\\\\PhPgSql\\\\Fluent\\\\Complex\\|null, \\.\\.\\.\\}\\) does not accept .+\\.$#"
229+
message: "#^Property Forrest79\\\\PhPgSql\\\\Fluent\\\\Query\\:\\:\\$params \\(array\\{select\\: array\\<int\\|string, BackedEnum\\|Forrest79\\\\PhPgSql\\\\Db\\\\Sql\\|Forrest79\\\\PhPgSql\\\\Fluent\\\\Query\\|int\\|string\\>, distinct\\: bool, distinctOn\\: list\\<Forrest79\\\\PhPgSql\\\\Db\\\\Sql\\|Forrest79\\\\PhPgSql\\\\Fluent\\\\Query\\|string\\>, tables\\: array\\<string, array\\{string, string\\}\\>, table\\-types\\: array\\{main\\: string\\|null, from\\: list\\<string\\>, joins\\: list\\<string\\>, using\\: string\\|null\\}, on\\-conditions\\: array\\<string, Forrest79\\\\PhPgSql\\\\Fluent\\\\Complex\\>, lateral\\-tables\\: array\\<string, string\\>, where\\: Forrest79\\\\PhPgSql\\\\Fluent\\\\Complex\\|null, \\.\\.\\.\\}\\) does not accept .+\\.$#"
230230
path: src/Fluent/Query.php
231231
count: 7
232232

src/Fluent/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function distinct(): QueryExecute
4040
/**
4141
* @throws Exceptions\QueryException
4242
*/
43-
public function distinctOn(string ...$on): QueryExecute
43+
public function distinctOn(string|Query|Db\Sql ...$on): QueryExecute
4444
{
4545
return $this->createQuery()->distinctOn(...$on);
4646
}

src/Fluent/Query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public function distinct(): static
185185
/**
186186
* @throws Exceptions\QueryException
187187
*/
188-
public function distinctOn(string ...$on): static
188+
public function distinctOn(string|self|Db\Sql ...$on): static
189189
{
190190
$this->resetQuery();
191191

src/Fluent/QueryBuilder.php

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @phpstan-type QueryParams array{
99
* select: array<int|string, string|int|\BackedEnum|Query|Db\Sql>,
1010
* distinct: bool,
11-
* distinctOn: list<string>,
11+
* distinctOn: list<string|Query|Db\Sql>,
1212
* tables: array<string, array{0: string, 1: string}>,
1313
* table-types: array{main: string|NULL, from: list<string>, joins: list<string>, using: string|NULL},
1414
* on-conditions: array<string, Complex>,
@@ -95,7 +95,7 @@ private function createSelect(
9595
): string
9696
{
9797
$selectSql = 'SELECT ' .
98-
$this->getSelectDistinct($queryParams) .
98+
$this->getSelectDistinct($queryParams, $params) .
9999
$this->getSelectColumns($queryParams, $params, $insertSelectColumnNames) .
100100
$this->getFrom($queryParams, $params, $insertSelectColumnNames === NULL) .
101101
$this->getJoins($queryParams, $params) .
@@ -429,9 +429,10 @@ private function createWith(array $queryParams, array &$params): string
429429

430430
/**
431431
* @param array<string, mixed> $queryParams
432+
* @param list<mixed> $params
432433
* @phpstan-param QueryParams $queryParams
433434
*/
434-
private function getSelectDistinct(array $queryParams): string
435+
private function getSelectDistinct(array $queryParams, array &$params): string
435436
{
436437
if (($queryParams[Query::PARAM_DISTINCT] === TRUE) && ($queryParams[Query::PARAM_DISTINCTON] !== [])) {
437438
throw Exceptions\QueryBuilderException::cantCombineDistinctAndDistinctOn();
@@ -440,7 +441,7 @@ private function getSelectDistinct(array $queryParams): string
440441
if ($queryParams[Query::PARAM_DISTINCT] === TRUE) {
441442
return 'DISTINCT ';
442443
} else if ($queryParams[Query::PARAM_DISTINCTON] !== []) {
443-
return 'DISTINCT ON (' . implode(', ', $queryParams[Query::PARAM_DISTINCTON]) . ') ';
444+
return 'DISTINCT ON (' . $this->processElements($queryParams[Query::PARAM_DISTINCTON], $params) . ') ';
444445
}
445446

446447
return '';
@@ -631,19 +632,7 @@ private function getOrderBy(array $queryParams, array &$params): string
631632
return '';
632633
}
633634

634-
$columns = [];
635-
foreach ($orderBy as $value) {
636-
if ($value instanceof Db\Sql) {
637-
$params[] = $value;
638-
$value = '?';
639-
} else if ($value instanceof Query) {
640-
$params[] = $value->createSqlQuery();
641-
$value = '(?)';
642-
}
643-
$columns[] = $value;
644-
}
645-
646-
return ' ORDER BY ' . \implode(', ', $columns);
635+
return ' ORDER BY ' . $this->processElements($orderBy, $params);
647636
}
648637

649638

@@ -882,4 +871,27 @@ private function processComplex(Complex $complex, array &$params): string
882871
return \implode(' ' . $complex->getType() . ' ', $processedConditions);
883872
}
884873

874+
875+
/**
876+
* @param list<string|Query|Db\Sql> $elements
877+
* @param list<mixed> $params
878+
*/
879+
private function processElements(array $elements, array &$params): string
880+
{
881+
$return = [];
882+
883+
foreach ($elements as $element) {
884+
if ($element instanceof Db\Sql) {
885+
$params[] = $element;
886+
$element = '?';
887+
} else if ($element instanceof Query) {
888+
$params[] = $element->createSqlQuery();
889+
$element = '(?)';
890+
}
891+
$return[] = $element;
892+
}
893+
894+
return \implode(', ', $return);
895+
}
896+
885897
}

src/Fluent/Sql.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function select(array $columns): Query;
1919
function distinct(): Query;
2020

2121

22-
function distinctOn(string ...$on): Query;
22+
function distinctOn(string|Query|Db\Sql ...$on): Query;
2323

2424

2525
function from(string|Query|Db\Sql $from, string|NULL $alias = NULL): Query;

tests/Unit/FluentQueryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ public function testSelectDistinctOn(): void
3939
{
4040
$query = $this->query()
4141
->select(['t.column'])
42-
->distinctOn('t.column')
42+
->distinctOn('t.column', Db\Sql\Expression::create('x = ?', 1))
4343
->from('table', 't')
4444
->where('t.column', 100)
4545
->createSqlQuery()
4646
->createQuery();
4747

48-
Tester\Assert::same('SELECT DISTINCT ON (t.column) t.column FROM table AS t WHERE t.column = $1', $query->getSql());
49-
Tester\Assert::same([100], $query->getParams());
48+
Tester\Assert::same('SELECT DISTINCT ON (t.column, x = $1) t.column FROM table AS t WHERE t.column = $2', $query->getSql());
49+
Tester\Assert::same([1, 100], $query->getParams());
5050
}
5151

5252

0 commit comments

Comments
 (0)