Skip to content

Commit 7dc02da

Browse files
committed
Make a method
1 parent 66e8c57 commit 7dc02da

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

Sources/StructuredQueriesCore/Documentation.docc/Extensions/QueryExpression.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
2929
- ``jsonArrayLength()``
3030
- ``jsonGroupArray(order:filter:)``
31+
- ``jsonObject()``
3132
- ``jsonPatch(_:)``
3233
3334
### Optionality

Sources/StructuredQueriesCore/SQLite/JSONFunctions.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ extension PrimaryKeyedTableDefinition where QueryValue: Codable & Sendable {
115115
AggregateFunction(
116116
"json_group_array",
117117
isDistinct: isDistinct,
118-
[jsonObject.queryFragment],
118+
[jsonObject().queryFragment],
119119
order: order?.queryFragment,
120120
filter: filter?.queryFragment
121121
)
@@ -188,15 +188,18 @@ extension PrimaryKeyedTableDefinition where QueryValue: _OptionalProtocol & Coda
188188
return AggregateFunction(
189189
"json_group_array",
190190
isDistinct: isDistinct,
191-
[QueryValue.columns.jsonObject.queryFragment],
191+
[QueryValue.columns.jsonObject().queryFragment],
192192
order: order?.queryFragment,
193193
filter: filterQueryFragment
194194
)
195195
}
196196
}
197197

198198
extension TableDefinition where QueryValue: Codable & Sendable {
199-
public var jsonObject: some QueryExpression<_CodableJSONRepresentation<QueryValue>> {
199+
/// A JSON representation of a table's columns.
200+
///
201+
/// Useful for referencing a table row in a larger JSON selection.
202+
public func jsonObject() -> some QueryExpression<_CodableJSONRepresentation<QueryValue>> {
200203
func open<TableColumn: TableColumnExpression>(_ column: TableColumn) -> QueryFragment {
201204
typealias Value = TableColumn.QueryValue._Optionalized.Wrapped
202205

@@ -244,7 +247,10 @@ extension TableDefinition where QueryValue: Codable & Sendable {
244247
}
245248

246249
extension Optional.TableColumns where QueryValue: Codable & Sendable {
247-
public var jsonObject: some QueryExpression<_CodableJSONRepresentation<Wrapped>?> {
248-
Case().when(rowid.isNot(nil), then: Wrapped.columns.jsonObject)
250+
/// A JSON representation of a table's columns.
251+
///
252+
/// Useful for referencing a table row in a larger JSON selection.
253+
public func jsonObject() -> some QueryExpression<_CodableJSONRepresentation<Wrapped>?> {
254+
Case().when(rowid.isNot(nil), then: Wrapped.columns.jsonObject())
249255
}
250256
}

Tests/StructuredQueriesTests/JSONFunctionsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ extension SnapshotTests {
344344
assertQuery(
345345
Reminder
346346
.join(RemindersList.all) { $0.remindersListID.eq($1.id) }
347-
.select { ($0, $1.jsonObject) }
347+
.select { ($0, $1.jsonObject()) }
348348
) {
349349
"""
350350
SELECT "reminders"."id", "reminders"."assignedUserID", "reminders"."dueDate", "reminders"."isCompleted", "reminders"."isFlagged", "reminders"."notes", "reminders"."priority", "reminders"."remindersListID", "reminders"."title", "reminders"."updatedAt", json_object('id', json_quote("remindersLists"."id"), 'color', json_quote("remindersLists"."color"), 'title', json_quote("remindersLists"."title"), 'position', json_quote("remindersLists"."position"))

0 commit comments

Comments
 (0)