Skip to content

Commit

Permalink
Fix numeric with leading zero fractional (#68)
Browse files Browse the repository at this point in the history
* Create failing test

Numeric numbers with a fractional part with a leading zero (eg: 10.08) will be decoded from the database without the leading zero (ie: 10.80).

This is a serious issue which results in corrupted numeric values.

* Add padding of zeros to fractional portion of numericals
  • Loading branch information
dmonagle authored and vzsg committed May 22, 2018
1 parent 4988607 commit 9721627
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Sources/PostgreSQL/Data/PostgreSQLData+String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ extension String: PostgreSQLDataConvertible {
if offset < metadata.weight.bigEndian + 1 {
integer += string
} else {
fractional += string
// Leading zeros matter with fractional
fractional += fractional.count == 0 ? String(repeating: "0", count: 4 - string.count) + string : string
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class PostgreSQLConnectionTests: XCTestCase {
params += Int16(1) // smallint
params += Int32(2) // integer
params += Int64(3) // bigint
params += String("123456789.123456789") // decimal
params += String("123456789.0123456789") // decimal
params += Double(5) // numeric
params += Float(6) // real
params += Double(7) // double
Expand All @@ -238,7 +238,7 @@ class PostgreSQLConnectionTests: XCTestCase {
try XCTAssertEqual(row.firstValue(forColumn: "smallint")?.decode(Int16.self), 1)
try XCTAssertEqual(row.firstValue(forColumn: "integer")?.decode(Int32.self), 2)
try XCTAssertEqual(row.firstValue(forColumn: "bigint")?.decode(Int64.self), 3)
try XCTAssertEqual(row.firstValue(forColumn: "decimal")?.decode(String.self), "123456789.123456789")
try XCTAssertEqual(row.firstValue(forColumn: "decimal")?.decode(String.self), "123456789.0123456789")
try XCTAssertEqual(row.firstValue(forColumn: "real")?.decode(Float.self), 6)
try XCTAssertEqual(row.firstValue(forColumn: "double")?.decode(Double.self), 7)
try XCTAssertEqual(row.firstValue(forColumn: "varchar")?.decode(String.self), "8")
Expand Down

0 comments on commit 9721627

Please sign in to comment.