From 2b20fc0f4f6574f59dae402ccf0ed050c6790b43 Mon Sep 17 00:00:00 2001 From: Gwynne Raskind Date: Mon, 10 Jul 2023 23:29:58 -0500 Subject: [PATCH] Add SQLite support for nested subpath (JSON) expressions (#104) Implement the new SQLDialect.nestedSubpathExpression(in:for:) method for SQLite syntax. --- Package.swift | 2 +- Sources/SQLiteKit/SQLiteDialect.swift | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 0ebb906..dabd6c0 100644 --- a/Package.swift +++ b/Package.swift @@ -14,7 +14,7 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/vapor/sqlite-nio.git", from: "1.2.0"), - .package(url: "https://github.com/vapor/sql-kit.git", from: "3.19.0"), + .package(url: "https://github.com/vapor/sql-kit.git", from: "3.28.0"), .package(url: "https://github.com/vapor/async-kit.git", from: "1.14.0"), ], targets: [ diff --git a/Sources/SQLiteKit/SQLiteDialect.swift b/Sources/SQLiteKit/SQLiteDialect.swift index 411d3b4..5e5a31a 100644 --- a/Sources/SQLiteKit/SQLiteDialect.swift +++ b/Sources/SQLiteKit/SQLiteDialect.swift @@ -34,6 +34,15 @@ public struct SQLiteDialect: SQLDialect { return nil } + public func nestedSubpathExpression(in column: any SQLExpression, for path: [String]) -> (any SQLExpression)? { + guard !path.isEmpty else { return nil } + + return SQLFunction("json_extract", args: [ + column, + SQLLiteral.string("$.\(path.joined(separator: "."))") + ]) + } + public init() { } private func isAtLeastVersion(_ major: Int, _ minor: Int, _ patch: Int) -> Bool {