-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for nested subpath (JSON) expressions (#169)
* Add a new SQLDialect method for generating nested subpath expressions (JSON paths) and a SQLNestedSubpathExpression expression for actually using it.
- Loading branch information
Showing
6 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/// Represents a "nested subpath" expression. At this time, this always represents a key path leading to a | ||
/// specific value in a JSON object. | ||
public struct SQLNestedSubpathExpression: SQLExpression { | ||
public var column: any SQLExpression | ||
public var path: [String] | ||
|
||
public init(column: any SQLExpression, path: [String]) { | ||
assert(!path.isEmpty) | ||
|
||
self.column = column | ||
self.path = path | ||
} | ||
|
||
public init(column: String, path: [String]) { | ||
self.init(column: SQLIdentifier(column), path: path) | ||
} | ||
|
||
public func serialize(to serializer: inout SQLSerializer) { | ||
serializer.dialect.nestedSubpathExpression(in: self.column, for: self.path)?.serialize(to: &serializer) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import SQLKit | ||
import XCTest | ||
|
||
extension SQLBenchmarker { | ||
public func testJSONPaths() throws { | ||
try self.runTest { | ||
try $0.drop(table: "planet_metadata") | ||
.ifExists() | ||
.run().wait() | ||
try $0.create(table: "planet_metadata") | ||
.column("id", type: .bigint, .primaryKey(autoIncrement: $0.dialect.supportsAutoIncrement)) | ||
.column("metadata", type: .custom(SQLRaw($0.dialect.name == "postgresql" ? "jsonb" : "json"))) | ||
.run().wait() | ||
|
||
// insert | ||
try $0.insert(into: "planet_metadata") | ||
.columns("id", "metadata") | ||
.values(SQLLiteral.default, SQLLiteral.string(#"{"a":{"b":{"c":[1,2,3]}}}"#)) | ||
.run().wait() | ||
|
||
// try to extract fields | ||
let objectARows = try $0.select().column(SQLNestedSubpathExpression(column: "metadata", path: ["a"]), as: "data").from("planet_metadata").all().wait() | ||
let objectARow = try XCTUnwrap(objectARows.first) | ||
let objectARaw = try objectARow.decode(column: "data", as: String.self) | ||
let objectA = try JSONDecoder().decode([String: [String: [Int]]].self, from: objectARaw.data(using: .utf8)!) | ||
|
||
XCTAssertEqual(objectARows.count, 1) | ||
XCTAssertEqual(objectA, ["b": ["c": [1, 2 ,3]]]) | ||
|
||
let objectBRows = try $0.select().column(SQLNestedSubpathExpression(column: "metadata", path: ["a", "b"]), as: "data").from("planet_metadata").all().wait() | ||
let objectBRow = try XCTUnwrap(objectBRows.first) | ||
let objectBRaw = try objectBRow.decode(column: "data", as: String.self) | ||
let objectB = try JSONDecoder().decode([String: [Int]].self, from: objectBRaw.data(using: .utf8)!) | ||
|
||
XCTAssertEqual(objectBRows.count, 1) | ||
XCTAssertEqual(objectB, ["c": [1, 2, 3]]) | ||
|
||
let objectCRows = try $0.select().column(SQLNestedSubpathExpression(column: "metadata", path: ["a", "b", "c"]), as: "data").from("planet_metadata").all().wait() | ||
let objectCRow = try XCTUnwrap(objectCRows.first) | ||
let objectCRaw = try objectCRow.decode(column: "data", as: String.self) | ||
let objectC = try JSONDecoder().decode([Int].self, from: objectCRaw.data(using: .utf8)!) | ||
|
||
XCTAssertEqual(objectCRows.count, 1) | ||
XCTAssertEqual(objectC, [1, 2, 3]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters