Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UUID Fix #1112

Merged
merged 4 commits into from
Feb 22, 2022
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
2 changes: 2 additions & 0 deletions Sources/SQLite/Typed/Coding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ private class SQLiteEncoder: Encoder {
encoder.setters.append(Expression(key.stringValue) <- data)
} else if let date = value as? Date {
encoder.setters.append(Expression(key.stringValue) <- date.datatypeValue)
} else if let uuid = value as? UUID {
encoder.setters.append(Expression(key.stringValue) <- uuid.datatypeValue)
} else {
let encoded = try JSONEncoder().encode(value)
let string = String(data: encoded, encoding: .utf8)
Expand Down
1 change: 1 addition & 0 deletions Tests/SQLiteTests/FoundationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ class FoundationTests: XCTestCase {
let uuid = UUID.fromDatatypeValue(string)
XCTAssertEqual(UUID(uuidString: "4ABE10C9-FF12-4CD4-90C1-4B429001BAD3"), uuid)
}

}
26 changes: 26 additions & 0 deletions Tests/SQLiteTests/QueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,32 @@ class QueryTests: XCTestCase {
}
#endif

func test_insert_and_search_for_UUID() {
struct Test: Codable {
var uuid: UUID
var string: String
}
let testUUID = UUID()
let testValue = Test(uuid: testUUID, string: "value")
let db = try! Connection(.temporary)
try! db.run(table.create { t in
t.column(uuid)
t.column(string)
}
)

let iQuery = try! table.insert(testValue)
try! db.run(iQuery)

let fQuery = table.filter(uuid == testUUID)
if let result = try! db.pluck(fQuery) {
let testValueReturned = Test(uuid: result[uuid], string: result[string])
XCTAssertEqual(testUUID, testValueReturned.uuid)
} else {
XCTFail("Search for uuid failed")
}
}

func test_upsert_withOnConflict_compilesInsertOrOnConflictExpression() {
assertSQL(
"""
Expand Down
3 changes: 3 additions & 0 deletions Tests/SQLiteTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ let int64Optional = Expression<Int64?>("int64Optional")
let string = Expression<String>("string")
let stringOptional = Expression<String?>("stringOptional")

let uuid = Expression<UUID>("uuid")
let uuidOptional = Expression<UUID?>("uuidOptional")

func assertSQL(_ expression1: @autoclosure () -> String, _ expression2: @autoclosure () -> Expressible,
file: StaticString = #file, line: UInt = #line) {
XCTAssertEqual(expression1(), expression2().asSQL(), file: file, line: line)
Expand Down