Skip to content

Commit 26ab8a5

Browse files
authored
Add support for distinct jsonArray (pointfreeco#57)
1 parent e9e4a40 commit 26ab8a5

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

Sources/StructuredQueriesCore/SQLite/JSONFunctions.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ extension QueryExpression where QueryValue: Codable & QueryBindable & Sendable {
2727
/// ```
2828
///
2929
/// - Parameters:
30+
/// - isDistinct: An boolean to enable the `DISTINCT` clause to apply to the aggregation.
3031
/// - order: An `ORDER BY` clause to apply to the aggregation.
3132
/// - filter: A `FILTER` clause to apply to the aggregation.
3233
/// - Returns: A JSON array aggregate of this expression.
3334
public func jsonGroupArray(
35+
isDistinct: Bool = false,
3436
order: (some QueryExpression)? = Bool?.none,
3537
filter: (some QueryExpression<Bool>)? = Bool?.none
3638
) -> some QueryExpression<[QueryValue].JSONRepresentation> {
37-
AggregateFunction("json_group_array", self, order: order, filter: filter)
39+
AggregateFunction("json_group_array", isDistinct: isDistinct, self, order: order, filter: filter)
3840
}
3941
}
4042

@@ -85,14 +87,16 @@ extension PrimaryKeyedTableDefinition where QueryValue: Codable & Sendable {
8587
/// }
8688
///
8789
/// - Parameters:
90+
/// - isDistinct: An boolean to enable the `DISTINCT` clause to apply to the aggregation.
8891
/// - order: An `ORDER BY` clause to apply to the aggregation.
8992
/// - filter: A `FILTER` clause to apply to the aggregation.
9093
/// - Returns: A JSON array aggregate of this table.
9194
public func jsonGroupArray(
95+
isDistinct: Bool = false,
9296
order: (some QueryExpression)? = Bool?.none,
9397
filter: (some QueryExpression<Bool>)? = Bool?.none
9498
) -> some QueryExpression<[QueryValue].JSONRepresentation> {
95-
AggregateFunction("json_group_array", jsonObject, order: order, filter: filter)
99+
AggregateFunction("json_group_array", isDistinct: isDistinct, jsonObject, order: order, filter: filter)
96100
}
97101

98102
private var jsonObject: some QueryExpression<QueryValue> {

Tests/StructuredQueriesTests/JSONFunctionsTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,30 @@ extension SnapshotTests {
4040
}
4141
}
4242

43+
@Test func jsonGroupArrayDisctinct() {
44+
assertQuery(
45+
Reminder.select {
46+
$0.priority.jsonGroupArray(isDistinct: true)
47+
}
48+
) {
49+
"""
50+
SELECT json_group_array(DISTINCT "reminders"."priority")
51+
FROM "reminders"
52+
"""
53+
} results: {
54+
"""
55+
┌────────────────┐
56+
│ [ │
57+
│ [0]: nil, │
58+
│ [1]: .high, │
59+
│ [2]: .low, │
60+
│ [3]: .medium │
61+
│ ] │
62+
└────────────────┘
63+
"""
64+
}
65+
}
66+
4367
@Test func jsonArrayLength() {
4468
assertQuery(
4569
Reminder.select {

0 commit comments

Comments
 (0)