Skip to content

Commit 66aa574

Browse files
committed
add numeric parsing for binary floating point, fixes vapor#56
1 parent 3b977c0 commit 66aa574

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Sources/PostgreSQL/Data/PostgreSQLData+BinaryFloatingPoint.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ extension BinaryFloatingPoint where Self: LosslessStringConvertible {
1111
case .int8: f = Self(value.as(Int64.self, default: 0).bigEndian)
1212
case .float4: f = Self(Data(value.reversed()).as(Float.self, default: 0))
1313
case .float8: f = Self(Data(value.reversed()).as(Double.self, default: 0))
14+
case .numeric:
15+
let string = try String.convertFromPostgreSQLData(data)
16+
f = Self(string)
1417
default: throw PostgreSQLError.decode(self, from: data)
1518
}
1619
guard let value = f else {

Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,20 @@ class PostgreSQLConnectionTests: XCTestCase {
536536
}
537537
try done.wait()
538538
}
539+
540+
// https://github.com/vapor/postgresql/issues/56
541+
func testSum() throws {
542+
let conn = try PostgreSQLConnection.makeTest(transport: .cleartext)
543+
struct Sum: Decodable {
544+
var sum: Double
545+
}
546+
let rows = try conn.query("SELECT SUM(3.14) as sum", decoding: Sum.self).wait()
547+
switch rows.count {
548+
case 1:
549+
XCTAssertEqual(rows[0].sum, 3.14)
550+
default: XCTFail("invalid row count")
551+
}
552+
}
539553

540554
static var allTests = [
541555
("testVersion", testVersion),

0 commit comments

Comments
 (0)