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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

- ``jsonArrayLength()``
- ``jsonGroupArray(order:filter:)``
- ``jsonPatch(_:)``

### Optionality

Expand Down
11 changes: 11 additions & 0 deletions Sources/StructuredQueriesCore/SQLite/JSONFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import Foundation
import StructuredQueriesSupport

extension QueryExpression {
/// Passes this expression and the given one to the `json_patch` function.
///
/// - Parameter other: A JSON object to patch this object with.
/// - Returns: A JSON expression of the result of the invoking the `json_patch` function.
public func jsonPatch<QueryOutput: Codable>(
_ other: some QueryExpression<QueryValue>
) -> some QueryExpression<QueryValue>
where QueryValue == _CodableJSONRepresentation<QueryOutput> {
QueryFunction("json_patch", self, other)
}

/// Wraps this expression with the `json_array_length` function.
///
/// ```swift
Expand Down
21 changes: 21 additions & 0 deletions Tests/StructuredQueriesTests/JSONFunctionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,27 @@ extension SnapshotTests {
"""
}
}

@Test func jsonPatch() {
assertQuery(Values(#bind(["a": 1]).jsonPatch(#bind(["b": 2])))) {
"""
SELECT json_patch('{
"a" : 1
}', '{
"b" : 2
}')
"""
} results: {
"""
┌───────────┐
│ [ │
│ "a": 1, │
│ "b": 2 │
│ ] │
└───────────┘
"""
}
}
}
}

Expand Down