Skip to content

Commit 0ecc452

Browse files
authored
Merge pull request #1112 from benrlewis7450G/UUIDFix
UUID Fix
2 parents 0c03c5d + 09e8424 commit 0ecc452

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

Sources/SQLite/Typed/Coding.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ private class SQLiteEncoder: Encoder {
207207
encoder.setters.append(Expression(key.stringValue) <- data)
208208
} else if let date = value as? Date {
209209
encoder.setters.append(Expression(key.stringValue) <- date.datatypeValue)
210+
} else if let uuid = value as? UUID {
211+
encoder.setters.append(Expression(key.stringValue) <- uuid.datatypeValue)
210212
} else {
211213
let encoded = try JSONEncoder().encode(value)
212214
let string = String(data: encoded, encoding: .utf8)

Tests/SQLiteTests/FoundationTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ class FoundationTests: XCTestCase {
2525
let uuid = UUID.fromDatatypeValue(string)
2626
XCTAssertEqual(UUID(uuidString: "4ABE10C9-FF12-4CD4-90C1-4B429001BAD3"), uuid)
2727
}
28+
2829
}

Tests/SQLiteTests/QueryTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,32 @@ class QueryTests: XCTestCase {
310310
}
311311
#endif
312312

313+
func test_insert_and_search_for_UUID() {
314+
struct Test: Codable {
315+
var uuid: UUID
316+
var string: String
317+
}
318+
let testUUID = UUID()
319+
let testValue = Test(uuid: testUUID, string: "value")
320+
let db = try! Connection(.temporary)
321+
try! db.run(table.create { t in
322+
t.column(uuid)
323+
t.column(string)
324+
}
325+
)
326+
327+
let iQuery = try! table.insert(testValue)
328+
try! db.run(iQuery)
329+
330+
let fQuery = table.filter(uuid == testUUID)
331+
if let result = try! db.pluck(fQuery) {
332+
let testValueReturned = Test(uuid: result[uuid], string: result[string])
333+
XCTAssertEqual(testUUID, testValueReturned.uuid)
334+
} else {
335+
XCTFail("Search for uuid failed")
336+
}
337+
}
338+
313339
func test_upsert_withOnConflict_compilesInsertOrOnConflictExpression() {
314340
assertSQL(
315341
"""

Tests/SQLiteTests/TestHelpers.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ let int64Optional = Expression<Int64?>("int64Optional")
9595
let string = Expression<String>("string")
9696
let stringOptional = Expression<String?>("stringOptional")
9797

98+
let uuid = Expression<UUID>("uuid")
99+
let uuidOptional = Expression<UUID?>("uuidOptional")
100+
98101
func assertSQL(_ expression1: @autoclosure () -> String, _ expression2: @autoclosure () -> Expressible,
99102
file: StaticString = #file, line: UInt = #line) {
100103
XCTAssertEqual(expression1(), expression2().asSQL(), file: file, line: line)

0 commit comments

Comments
 (0)