Skip to content

Commit 0afdcf8

Browse files
authored
decode null columns, fixes #255 (#256)
1 parent bbd0e70 commit 0afdcf8

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Sources/MySQLKit/MySQLDatabase.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ extension MySQLRow: SQLRow {
105105
}
106106

107107
public func decodeNil(column: String) throws -> Bool {
108-
self.column(column) == nil
108+
guard let data = self.column(column) else {
109+
return true
110+
}
111+
return data.buffer == nil
109112
}
110113

111114
public func decode<D>(column: String, as type: D.Type) throws -> D where D : Decodable {

Tests/MySQLKitTests/MySQLKitTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ class MySQLKitTests: XCTestCase {
88
try self.benchmark.testEnum()
99
}
1010

11+
func testNullDecode() throws {
12+
struct Person: Codable {
13+
let id: Int
14+
let name: String?
15+
}
16+
17+
let rows = try self.db.raw("SELECT 1 as `id`, null as `name`")
18+
.all(decoding: Person.self).wait()
19+
XCTAssertEqual(rows[0].id, 1)
20+
XCTAssertEqual(rows[0].name, nil)
21+
}
22+
1123
var db: SQLDatabase {
1224
self.connection.sql()
1325
}

0 commit comments

Comments
 (0)