Skip to content

Commit 68ea74a

Browse files
committed
Allow multiple columns
1 parent 39cfb6a commit 68ea74a

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

src/mako/database/query/Query.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,62 +1162,64 @@ public function descendingVectorDistance(string $column, array|string|Subquery $
11621162
/**
11631163
* Adds a ORDER BY clause where nulls come either first or last.
11641164
*/
1165-
protected function orderByNull(string $column, SortDirection $order = SortDirection::Ascending, bool $nullsLast = true): static
1166-
{
1167-
$this->orderings[] = [
1168-
'type' => 'nullOrdering',
1169-
'column' => $column,
1170-
'order' => $order->value,
1171-
'nullsLast' => $nullsLast,
1172-
];
1165+
protected function orderByNull(array|string $column, SortDirection $order = SortDirection::Ascending, bool $nullsLast = true): static
1166+
{
1167+
foreach ((array) $column as $col) {
1168+
$this->orderings[] = [
1169+
'type' => 'nullOrdering',
1170+
'column' => $col,
1171+
'order' => $order->value,
1172+
'nullsLast' => $nullsLast,
1173+
];
1174+
}
11731175

11741176
return $this;
11751177
}
11761178

11771179
/**
11781180
* Adds a ORDER BY clause where nulls come first.
11791181
*/
1180-
public function orderByNullsFirst(string $column, SortDirection|string $order = SortDirection::Ascending): static
1182+
public function orderByNullsFirst(array|string $column, SortDirection|string $order = SortDirection::Ascending): static
11811183
{
11821184
return $this->orderByNull($column, $order, false);
11831185
}
11841186

11851187
/**
11861188
* Adds a ORDER BY clause where nulls come last.
11871189
*/
1188-
public function orderByNullsLast(string $column, SortDirection|string $order = SortDirection::Ascending): static
1190+
public function orderByNullsLast(array|string $column, SortDirection|string $order = SortDirection::Ascending): static
11891191
{
11901192
return $this->orderByNull($column, $order, true);
11911193
}
11921194

11931195
/**
11941196
* Adds an ascending ORDER BY clause where nulls come first.
11951197
*/
1196-
public function ascendingNullsFirst(string $column): static
1198+
public function ascendingNullsFirst(array|string $column): static
11971199
{
11981200
return $this->orderByNull($column, SortDirection::Ascending, false);
11991201
}
12001202

12011203
/**
12021204
* Adds a descending ORDER BY clause where nulls come first.
12031205
*/
1204-
public function descendingNullsFirst(string $column): static
1206+
public function descendingNullsFirst(array|string $column): static
12051207
{
12061208
return $this->orderByNull($column, SortDirection::Descending, false);
12071209
}
12081210

12091211
/**
12101212
* Adds an ascending ORDER BY clause where nulls come last.
12111213
*/
1212-
public function ascendingNullsLast(string $column): static
1214+
public function ascendingNullsLast(array|string $column): static
12131215
{
12141216
return $this->orderByNull($column, SortDirection::Ascending, true);
12151217
}
12161218

12171219
/**
12181220
* Adds a descending ORDER BY clause where nulls come last.
12191221
*/
1220-
public function descendingNullsLast(string $column): static
1222+
public function descendingNullsLast(array|string $column): static
12211223
{
12221224
return $this->orderByNull($column, SortDirection::Descending, true);
12231225
}

tests/unit/database/query/compilers/BaseCompilerTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,6 +2077,32 @@ public function testOrderByNullsFirst(): void
20772077
$this->assertEquals([], $query['params']);
20782078
}
20792079

2080+
/**
2081+
*
2082+
*/
2083+
public function testOrderByNullsFirstWithMultipleColumns(): void
2084+
{
2085+
$query = $this->getBuilder();
2086+
2087+
$query->orderByNullsFirst(['text1', 'text2']);
2088+
2089+
$query = $query->getCompiler()->select();
2090+
2091+
$this->assertEquals('SELECT * FROM "foobar" ORDER BY ("text1" IS NOT NULL), "text1" ASC, ("text2" IS NOT NULL), "text2" ASC', $query['sql']);
2092+
$this->assertEquals([], $query['params']);
2093+
2094+
//
2095+
2096+
$query = $this->getBuilder();
2097+
2098+
$query->orderByNullsFirst(['text1', 'text2'], SortDirection::Descending);
2099+
2100+
$query = $query->getCompiler()->select();
2101+
2102+
$this->assertEquals('SELECT * FROM "foobar" ORDER BY ("text1" IS NOT NULL), "text1" DESC, ("text2" IS NOT NULL), "text2" DESC', $query['sql']);
2103+
$this->assertEquals([], $query['params']);
2104+
}
2105+
20802106
/**
20812107
*
20822108
*/

0 commit comments

Comments
 (0)