Skip to content

Commit

Permalink
Correctly diagnose null field
Browse files Browse the repository at this point in the history
  • Loading branch information
Danappelxx committed Nov 3, 2016
1 parent 05ab2ba commit a3dbab5
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Sources/PostgreSQL/Result.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,18 @@ public class Result: SQL.ResultProtocol {
}

public func data(atRow rowIndex: Int, forFieldIndex fieldIndex: Int) -> Buffer? {
let rowIndex = Int32(rowIndex)
let fieldIndex = Int32(fieldIndex)

let count = PQgetlength(resultPointer, Int32(rowIndex), Int32(fieldIndex))
guard count > 0, let start = PQgetvalue(resultPointer, Int32(rowIndex), Int32(fieldIndex)) else {
guard PQgetisnull(resultPointer, rowIndex, fieldIndex) == 0 else {
return nil
}

let count = PQgetlength(resultPointer, rowIndex, fieldIndex)
guard count > 0, let start = PQgetvalue(resultPointer, rowIndex, fieldIndex) else {
return Buffer()
}

return start.withMemoryRebound(to: UInt8.self, capacity: Int(count)) { start in
return Buffer(Array(UnsafeBufferPointer(start: start, count: Int(count))))
}
Expand Down

0 comments on commit a3dbab5

Please sign in to comment.