Skip to content

Commit

Permalink
#20 Don't read digits beyond the digits boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
irar2 committed Jun 15, 2017
1 parent 3a89bc5 commit d6f7060
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
18 changes: 12 additions & 6 deletions Sources/SwiftKueryPostgreSQL/PostgreSQLResultFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public class PostgreSQLResultFetcher: ResultFetcher {

case .numeric:
// Numeric is a sequence of Int16's: number of digits, weight, sign, display scale, numeric digits
// https://www.postgresql.org/message-id/[email protected]
let sign = PostgreSQLResultFetcher.int16NetworkToHost(from: value.advanced(by: 4))
if sign == -16384 { // 0xC000
return "NaN"
Expand All @@ -171,15 +172,20 @@ public class PostgreSQLResultFetcher: ResultFetcher {

if weight >= 0 {
for i in 0 ... weight {
let digitsAsInt16 = PostgreSQLResultFetcher.int16NetworkToHost(from: currentDigitData)
if i == 0 {
result += String(digitsAsInt16)
if currentDigitNumber < numberOfDigits {
let digitsAsInt16 = PostgreSQLResultFetcher.int16NetworkToHost(from: currentDigitData)
if i == 0 {
result += String(digitsAsInt16)
}
else {
result += String(format: "%04d", digitsAsInt16)
}
currentDigitData = currentDigitData.advanced(by: 2)
currentDigitNumber = i + 1
}
else {
result += String(format: "%04d", digitsAsInt16)
result += "0000"
}
currentDigitData = currentDigitData.advanced(by: 2)
currentDigitNumber = i + 1
}
}

Expand Down
29 changes: 25 additions & 4 deletions Tests/SwiftKueryPostgreSQLTests/TestTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,31 @@ class TestTypes: XCTestCase {
XCTAssertEqual(rows![4][3]! as! String, negativeLongNumber, "Wrong value in row 4 column 3")
XCTAssertEqual(rows![4][4]! as! String, "0", "Wrong value in row 4 column 4")

let drop = Raw(query: "DROP TABLE", table: t)
executeQuery(query: drop, connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "DROP TABLE failed")
XCTAssertNil(result.asError, "Error in DELETE: \(result.asError!)")
i = Insert(into: t, values: "grape", "90000.0", "-400000000", "20000.000000000000000", "0.000000000000")
executeQuery(query: i, connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "INSERT failed")
XCTAssertNil(result.asError, "Error in INSERT: \(result.asError!)")

executeQuery(query: s, connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "SELECT failed")
XCTAssertNil(result.asError, "Error in SELECT: \(result.asError!)")
XCTAssertNotNil(rows, "SELECT returned no rows")
XCTAssertEqual(rows!.count, 6, "SELECT returned wrong number of rows")
XCTAssertEqual(rows![5].count, 5, "SELECT returned wrong number of columns")

XCTAssertEqual(rows![5][0]! as! String, "grape")
XCTAssertEqual(rows![5][1]! as! String, "90000")
XCTAssertEqual(rows![5][2]! as! String, "-400000000")
XCTAssertEqual(rows![5][3]! as! String, "20000")
XCTAssertEqual(rows![5][4]! as! String, "0")

let drop = Raw(query: "DROP TABLE", table: t)
executeQuery(query: drop, connection: connection) { result, rows in
XCTAssertEqual(result.success, true, "DROP TABLE failed")
XCTAssertNil(result.asError, "Error in DELETE: \(result.asError!)")

}
}
}
}
}
Expand Down

0 comments on commit d6f7060

Please sign in to comment.