diff --git a/Sources/StructuredQueriesCore/Documentation.docc/Extensions/QueryExpression.md b/Sources/StructuredQueriesCore/Documentation.docc/Extensions/QueryExpression.md index 70b00679..9fdd11d9 100644 --- a/Sources/StructuredQueriesCore/Documentation.docc/Extensions/QueryExpression.md +++ b/Sources/StructuredQueriesCore/Documentation.docc/Extensions/QueryExpression.md @@ -28,6 +28,7 @@ - ``jsonArrayLength()`` - ``jsonGroupArray(order:filter:)`` +- ``jsonPatch(_:)`` ### Optionality diff --git a/Sources/StructuredQueriesCore/SQLite/JSONFunctions.swift b/Sources/StructuredQueriesCore/SQLite/JSONFunctions.swift index f1f358bc..2371ca16 100644 --- a/Sources/StructuredQueriesCore/SQLite/JSONFunctions.swift +++ b/Sources/StructuredQueriesCore/SQLite/JSONFunctions.swift @@ -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( + _ other: some QueryExpression + ) -> some QueryExpression + where QueryValue == _CodableJSONRepresentation { + QueryFunction("json_patch", self, other) + } + /// Wraps this expression with the `json_array_length` function. /// /// ```swift diff --git a/Tests/StructuredQueriesTests/JSONFunctionsTests.swift b/Tests/StructuredQueriesTests/JSONFunctionsTests.swift index 18e063e6..1af6aad3 100644 --- a/Tests/StructuredQueriesTests/JSONFunctionsTests.swift +++ b/Tests/StructuredQueriesTests/JSONFunctionsTests.swift @@ -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 │ + │ ] │ + └───────────┘ + """ + } + } } }