Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Sources/StructuredQueriesCore/SQLite/JSONFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ extension QueryExpression where QueryValue: Codable & QueryBindable & Sendable {
/// ```
///
/// - Parameters:
/// - isDistinct: An boolean to enable the `DISTINCT` clause to apply to the aggregation.
/// - order: An `ORDER BY` clause to apply to the aggregation.
/// - filter: A `FILTER` clause to apply to the aggregation.
/// - Returns: A JSON array aggregate of this expression.
public func jsonGroupArray(
isDistinct: Bool = false,
order: (some QueryExpression)? = Bool?.none,
filter: (some QueryExpression<Bool>)? = Bool?.none
) -> some QueryExpression<[QueryValue].JSONRepresentation> {
AggregateFunction("json_group_array", self, order: order, filter: filter)
AggregateFunction("json_group_array", isDistinct: isDistinct, self, order: order, filter: filter)
}
}

Expand Down Expand Up @@ -85,14 +87,16 @@ extension PrimaryKeyedTableDefinition where QueryValue: Codable & Sendable {
/// }
///
/// - Parameters:
/// - isDistinct: An boolean to enable the `DISTINCT` clause to apply to the aggregation.
/// - order: An `ORDER BY` clause to apply to the aggregation.
/// - filter: A `FILTER` clause to apply to the aggregation.
/// - Returns: A JSON array aggregate of this table.
public func jsonGroupArray(
isDistinct: Bool = false,
order: (some QueryExpression)? = Bool?.none,
filter: (some QueryExpression<Bool>)? = Bool?.none
) -> some QueryExpression<[QueryValue].JSONRepresentation> {
AggregateFunction("json_group_array", jsonObject, order: order, filter: filter)
AggregateFunction("json_group_array", isDistinct: isDistinct, jsonObject, order: order, filter: filter)
}

private var jsonObject: some QueryExpression<QueryValue> {
Expand Down
24 changes: 24 additions & 0 deletions Tests/StructuredQueriesTests/JSONFunctionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ extension SnapshotTests {
}
}

@Test func jsonGroupArrayDisctinct() {
assertQuery(
Reminder.select {
$0.priority.jsonGroupArray(isDistinct: true)
}
) {
"""
SELECT json_group_array(DISTINCT "reminders"."priority")
FROM "reminders"
"""
} results: {
"""
┌────────────────┐
│ [ │
│ [0]: nil, │
│ [1]: .high, │
│ [2]: .low, │
│ [3]: .medium │
│ ] │
└────────────────┘
"""
}
}

@Test func jsonArrayLength() {
assertQuery(
Reminder.select {
Expand Down