Skip to content

Commit 8056cf4

Browse files
committed
cleanup
1 parent 21d40a6 commit 8056cf4

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

Sources/StructuredQueriesCore/SQLite/JSONFunctions.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extension QueryExpression where QueryValue: Codable & Sendable {
3939
}
4040

4141
extension PrimaryKeyedTableDefinition where QueryValue: Codable & Sendable {
42-
/// A JSON array repsentation of the aggregation of a table's columns.
42+
/// A JSON array representation of the aggregation of a table's columns.
4343
///
4444
/// Constructs a JSON array of JSON objects with a field for each column of the table. This can be
4545
/// useful for loading many associated values in a single query. For example, to query for every
@@ -84,7 +84,10 @@ extension PrimaryKeyedTableDefinition where QueryValue: Codable & Sendable {
8484
/// }
8585
/// }
8686
///
87-
/// > Note: If the primary key of the row is NULL, then the object is omitted from the array.
87+
/// - Parameters:
88+
/// - order: An `ORDER BY` clause to apply to the aggregation.
89+
/// - filter: A `FILTER` clause to apply to the aggregation.
90+
/// - Returns: A JSON array aggregate of this table.
8891
public func jsonGroupArray(
8992
order: (some QueryExpression)? = Bool?.none,
9093
filter: (some QueryExpression<Bool>)? = Bool?.none
@@ -119,6 +122,8 @@ extension PrimaryKeyedTableDefinition where QueryValue: Codable & Sendable {
119122
let fragment: QueryFragment = Self.allColumns
120123
.map { open($0) }
121124
.joined(separator: ", ")
122-
return SQLQueryExpression("iif(\(self.primaryKey.is(nil)), NULL, json_object(\(fragment)))")
125+
return SQLQueryExpression(
126+
"CASE WHEN \(primaryKey.isNot(nil)) THEN json_object(\(fragment)) END"
127+
)
123128
}
124129
}

Tests/StructuredQueriesTests/JSONFunctionsTests.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,15 @@ extension SnapshotTests {
118118
.limit(2)
119119
) {
120120
"""
121-
SELECT "users"."id", "users"."name" AS "assignedUser", "reminders"."id", "reminders"."assignedUserID", "reminders"."dueDate", "reminders"."isCompleted", "reminders"."isFlagged", "reminders"."notes", "reminders"."priority", "reminders"."remindersListID", "reminders"."title" AS "reminder", json_group_array(iif(("tags"."id" IS NULL), NULL, json_object('id', json_quote("tags"."id"), 'name', json_quote("tags"."name")))) AS "tags"
121+
SELECT "users"."id", "users"."name" AS "assignedUser", "reminders"."id", "reminders"."assignedUserID", "reminders"."dueDate", "reminders"."isCompleted", "reminders"."isFlagged", "reminders"."notes", "reminders"."priority", "reminders"."remindersListID", "reminders"."title" AS "reminder", json_group_array(CASE WHEN ("tags"."id" IS NOT NULL) THEN json_object('id', json_quote("tags"."id"), 'name', json_quote("tags"."name")) END) AS "tags"
122122
FROM "reminders"
123123
LEFT JOIN "reminderTags" ON ("reminders"."id" = "reminderTags"."reminderID")
124124
LEFT JOIN "tags" ON ("reminderTags"."tagID" = "tags"."id")
125125
LEFT JOIN "users" ON ("reminders"."assignedUserID" = "users"."id")
126126
GROUP BY "reminders"."id"
127127
LIMIT 2
128128
"""
129-
}results: {
129+
} results: {
130130
"""
131131
┌──────────────────────────────────────────────┐
132132
│ ReminderRow( │
@@ -200,14 +200,14 @@ extension SnapshotTests {
200200
.limit(1)
201201
) {
202202
"""
203-
SELECT "remindersLists"."id", "remindersLists"."color", "remindersLists"."name" AS "remindersList", json_group_array(iif(("reminders"."id" IS NULL), NULL, json_object('id', json_quote("reminders"."id"), 'assignedUserID', json_quote("reminders"."assignedUserID"), 'dueDate', json_quote("reminders"."dueDate"), 'isCompleted', iif("reminders"."isCompleted" = 0, json('false'), iif("reminders"."isCompleted" = 1, json('true'), NULL)), 'isFlagged', iif("reminders"."isFlagged" = 0, json('false'), iif("reminders"."isFlagged" = 1, json('true'), NULL)), 'notes', json_quote("reminders"."notes"), 'priority', json_quote("reminders"."priority"), 'remindersListID', json_quote("reminders"."remindersListID"), 'title', json_quote("reminders"."title")))) AS "reminders"
203+
SELECT "remindersLists"."id", "remindersLists"."color", "remindersLists"."name" AS "remindersList", json_group_array(CASE WHEN ("reminders"."id" IS NOT NULL) THEN json_object('id', json_quote("reminders"."id"), 'assignedUserID', json_quote("reminders"."assignedUserID"), 'dueDate', json_quote("reminders"."dueDate"), 'isCompleted', json(CASE "reminders"."isCompleted" WHEN 0 THEN 'false' WHEN 1 THEN 'true' END), 'isFlagged', json(CASE "reminders"."isFlagged" WHEN 0 THEN 'false' WHEN 1 THEN 'true' END), 'notes', json_quote("reminders"."notes"), 'priority', json_quote("reminders"."priority"), 'remindersListID', json_quote("reminders"."remindersListID"), 'title', json_quote("reminders"."title")) END) AS "reminders"
204204
FROM "remindersLists"
205205
LEFT JOIN "reminders" ON ("remindersLists"."id" = "reminders"."remindersListID")
206206
WHERE NOT ("reminders"."isCompleted")
207207
GROUP BY "remindersLists"."id"
208208
LIMIT 1
209209
"""
210-
}results: {
210+
} results: {
211211
"""
212212
┌────────────────────────────────────────────────┐
213213
│ RemindersListRow( │
@@ -270,18 +270,18 @@ extension SnapshotTests {
270270

271271
@Test func associationWithJSONField() {
272272
assertQuery(
273-
TaskList
274-
.join(Task.all) { $0.id.eq($1.taskListID) }
275-
.select {
276-
TaskListRow.Columns(taskList: $0, tasks: $1.jsonGroupArray())
277-
}
273+
TaskList
274+
.join(Task.all) { $0.id.eq($1.taskListID) }
275+
.select {
276+
TaskListRow.Columns(taskList: $0, tasks: $1.jsonGroupArray())
277+
}
278278
) {
279279
"""
280-
SELECT "taskLists"."id" AS "taskList", json_group_array(iif(("tasks"."id" IS NULL), NULL, json_object('id', json_quote("tasks"."id"), 'notes', json("tasks"."notes"), 'taskListID', json_quote("tasks"."taskListID")))) AS "tasks"
280+
SELECT "taskLists"."id" AS "taskList", json_group_array(CASE WHEN ("tasks"."id" IS NOT NULL) THEN json_object('id', json_quote("tasks"."id"), 'notes', json("tasks"."notes"), 'taskListID', json_quote("tasks"."taskListID")) END) AS "tasks"
281281
FROM "taskLists"
282282
JOIN "tasks" ON ("taskLists"."id" = "tasks"."taskListID")
283283
"""
284-
}results: {
284+
} results: {
285285
"""
286286
no such table: taskLists
287287
"""

0 commit comments

Comments
 (0)