Skip to content

Commit

Permalink
Support storing json in text columns (#87)
Browse files Browse the repository at this point in the history
* add support for storing json in text columns

* newlines
  • Loading branch information
tanner0101 authored Jul 16, 2020
1 parent 442968e commit 86947d5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Sources/SQLiteKit/SQLiteDataDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ public struct SQLiteDataDecoder {

func jsonDecoder() throws -> Decoder {
let data: Data
if case .blob(let buffer) = self.data {
switch self.data {
case .blob(let buffer):
data = Data(buffer.readableBytesView)
} else {
case .text(let string):
data = Data(string.utf8)
default:
data = .init()
}
return try JSONDecoder()
Expand Down
12 changes: 12 additions & 0 deletions Tests/SQLiteKitTests/SQLiteKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ class SQLiteKitTests: XCTestCase {
XCTAssertEqual(res[0].column("foreign_keys"), .integer(1))
}

func testJSONStringColumn() throws {
_ = try self.connection.query("CREATE TABLE foo (bar TEXT)").wait()
_ = try self.connection.query(#"INSERT INTO foo (bar) VALUES ('{"baz": "qux"}')"#).wait()
let rows = try self.connection.query("SELECT * FROM foo").wait()

struct Bar: Codable {
var baz: String
}
let bar = try SQLiteDataDecoder().decode(Bar.self, from: rows[0].column("bar")!)
XCTAssertEqual(bar.baz, "qux")
}

func testMultipleInMemoryDatabases() throws {
let a = SQLiteConnectionSource(
configuration: .init(storage: .memory, enableForeignKeys: true),
Expand Down

0 comments on commit 86947d5

Please sign in to comment.