Skip to content

Commit 4ec9db3

Browse files
committed
Fix jsonGroupArray() automatic application of filter when QueryValue is Optional.
1 parent efd928f commit 4ec9db3

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

Sources/StructuredQueriesCore/SQLite/JSONFunctions.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ extension PrimaryKeyedTableDefinition where QueryValue: Codable & Sendable {
112112
}
113113
}
114114

115-
extension PrimaryKeyedTableDefinition {
115+
extension PrimaryKeyedTableDefinition where QueryValue: _OptionalProtocol & Codable & Sendable, QueryValue.Wrapped: Codable & Sendable {
116116
/// A JSON array representation of the aggregation of a table's columns.
117117
///
118118
/// Constructs a JSON array of JSON objects with a field for each column of the table. This can be
@@ -163,12 +163,11 @@ extension PrimaryKeyedTableDefinition {
163163
/// - order: An `ORDER BY` clause to apply to the aggregation.
164164
/// - filter: A `FILTER` clause to apply to the aggregation.
165165
/// - Returns: A JSON array aggregate of this table.
166-
public func jsonGroupArray<Wrapped: Codable & Sendable>(
166+
public func jsonGroupArray(
167167
isDistinct: Bool = false,
168168
order: (some QueryExpression)? = Bool?.none,
169169
filter: (some QueryExpression<Bool>)? = Bool?.none
170-
) -> some QueryExpression<[Wrapped].JSONRepresentation>
171-
where QueryValue == Wrapped? {
170+
) -> some QueryExpression<[QueryValue.Wrapped].JSONRepresentation> {
172171
let filterQueryFragment =
173172
if let filter {
174173
self.primaryKey.isNot(nil).and(filter).queryFragment
@@ -183,6 +182,9 @@ extension PrimaryKeyedTableDefinition {
183182
filter: filterQueryFragment
184183
)
185184
}
185+
}
186+
187+
extension PrimaryKeyedTableDefinition {
186188

187189
fileprivate var jsonObject: some QueryExpression<QueryValue> {
188190
func open<TableColumn: TableColumnExpression>(_ column: TableColumn) -> QueryFragment {

Tests/StructuredQueriesTests/KitchenSinkTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ extension SnapshotTests {
211211
.select { ($0, $1.jsonGroupArray()) }
212212
) {
213213
"""
214-
SELECT "kitchens"."id", json_group_array(CASE WHEN ("kitchenSinks"."id" IS NOT NULL) THEN json_object('id', json_quote("kitchenSinks"."id"), 'kitchenID', json_quote("kitchenSinks"."kitchenID"), 'bool', json(CASE "kitchenSinks"."bool" WHEN 0 THEN 'false' WHEN 1 THEN 'true' END), 'optionalBool', json(CASE "kitchenSinks"."optionalBool" WHEN 0 THEN 'false' WHEN 1 THEN 'true' END), 'string', json_quote("kitchenSinks"."string"), 'optionalString', json_quote("kitchenSinks"."optionalString"), 'int', json_quote("kitchenSinks"."int"), 'optionalInt', json_quote("kitchenSinks"."optionalInt"), 'double', json_quote("kitchenSinks"."double"), 'optionalDouble', json_quote("kitchenSinks"."optionalDouble"), 'rawRepresentable', json_quote("kitchenSinks"."rawRepresentable"), 'optionalRawRepresentable', json_quote("kitchenSinks"."optionalRawRepresentable"), 'iso8601Date', json_quote("kitchenSinks"."iso8601Date"), 'optionalISO8601Date', json_quote("kitchenSinks"."optionalISO8601Date"), 'unixTimeDate', datetime("kitchenSinks"."unixTimeDate", 'unixepoch'), 'optionalUnixTimeDate', datetime("kitchenSinks"."optionalUnixTimeDate", 'unixepoch'), 'julianDayDate', datetime("kitchenSinks"."julianDayDate", 'julianday'), 'optionalJulianDayDate', datetime("kitchenSinks"."optionalJulianDayDate", 'julianday'), 'jsonArray', json("kitchenSinks"."jsonArray"), 'optionalJSONArray', json("kitchenSinks"."optionalJSONArray"), 'jsonArrayOfDates', json("kitchenSinks"."jsonArrayOfDates")) END)
214+
SELECT "kitchens"."id", json_group_array(CASE WHEN ("kitchenSinks"."id" IS NOT NULL) THEN json_object('id', json_quote("kitchenSinks"."id"), 'kitchenID', json_quote("kitchenSinks"."kitchenID"), 'bool', json(CASE "kitchenSinks"."bool" WHEN 0 THEN 'false' WHEN 1 THEN 'true' END), 'optionalBool', json(CASE "kitchenSinks"."optionalBool" WHEN 0 THEN 'false' WHEN 1 THEN 'true' END), 'string', json_quote("kitchenSinks"."string"), 'optionalString', json_quote("kitchenSinks"."optionalString"), 'int', json_quote("kitchenSinks"."int"), 'optionalInt', json_quote("kitchenSinks"."optionalInt"), 'double', json_quote("kitchenSinks"."double"), 'optionalDouble', json_quote("kitchenSinks"."optionalDouble"), 'rawRepresentable', json_quote("kitchenSinks"."rawRepresentable"), 'optionalRawRepresentable', json_quote("kitchenSinks"."optionalRawRepresentable"), 'iso8601Date', json_quote("kitchenSinks"."iso8601Date"), 'optionalISO8601Date', json_quote("kitchenSinks"."optionalISO8601Date"), 'unixTimeDate', datetime("kitchenSinks"."unixTimeDate", 'unixepoch'), 'optionalUnixTimeDate', datetime("kitchenSinks"."optionalUnixTimeDate", 'unixepoch'), 'julianDayDate', datetime("kitchenSinks"."julianDayDate", 'julianday'), 'optionalJulianDayDate', datetime("kitchenSinks"."optionalJulianDayDate", 'julianday'), 'jsonArray', json("kitchenSinks"."jsonArray"), 'optionalJSONArray', json("kitchenSinks"."optionalJSONArray"), 'jsonArrayOfDates', json("kitchenSinks"."jsonArrayOfDates")) END) FILTER (WHERE ("kitchenSinks"."id" IS NOT NULL))
215215
FROM "kitchens"
216216
FULL JOIN "kitchenSinks" ON ("kitchens"."id" IS "kitchenSinks"."kitchenID")
217217
"""

Tests/StructuredQueriesTests/SelectTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ extension SnapshotTests {
10491049
.select { ($0, $1.jsonGroupArray()) }
10501050
) {
10511051
"""
1052-
SELECT "r1s"."id", "r1s"."assignedUserID", "r1s"."dueDate", "r1s"."isCompleted", "r1s"."isFlagged", "r1s"."notes", "r1s"."priority", "r1s"."remindersListID", "r1s"."title", "r1s"."updatedAt", json_group_array(CASE WHEN ("r2s"."id" IS NOT NULL) THEN json_object('id', json_quote("r2s"."id"), 'assignedUserID', json_quote("r2s"."assignedUserID"), 'dueDate', json_quote("r2s"."dueDate"), 'isCompleted', json(CASE "r2s"."isCompleted" WHEN 0 THEN 'false' WHEN 1 THEN 'true' END), 'isFlagged', json(CASE "r2s"."isFlagged" WHEN 0 THEN 'false' WHEN 1 THEN 'true' END), 'notes', json_quote("r2s"."notes"), 'priority', json_quote("r2s"."priority"), 'remindersListID', json_quote("r2s"."remindersListID"), 'title', json_quote("r2s"."title"), 'updatedAt', json_quote("r2s"."updatedAt")) END)
1052+
SELECT "r1s"."id", "r1s"."assignedUserID", "r1s"."dueDate", "r1s"."isCompleted", "r1s"."isFlagged", "r1s"."notes", "r1s"."priority", "r1s"."remindersListID", "r1s"."title", "r1s"."updatedAt", json_group_array(CASE WHEN ("r2s"."id" IS NOT NULL) THEN json_object('id', json_quote("r2s"."id"), 'assignedUserID', json_quote("r2s"."assignedUserID"), 'dueDate', json_quote("r2s"."dueDate"), 'isCompleted', json(CASE "r2s"."isCompleted" WHEN 0 THEN 'false' WHEN 1 THEN 'true' END), 'isFlagged', json(CASE "r2s"."isFlagged" WHEN 0 THEN 'false' WHEN 1 THEN 'true' END), 'notes', json_quote("r2s"."notes"), 'priority', json_quote("r2s"."priority"), 'remindersListID', json_quote("r2s"."remindersListID"), 'title', json_quote("r2s"."title"), 'updatedAt', json_quote("r2s"."updatedAt")) END) FILTER (WHERE ("r2s"."id" IS NOT NULL))
10531053
FROM "reminders" AS "r1s"
10541054
LEFT JOIN "reminders" AS "r2s" ON ("r1s"."id" = "r2s"."id")
10551055
GROUP BY "r1s"."id"

0 commit comments

Comments
 (0)