forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQLData+BinaryFloatingPoint.swift
45 lines (43 loc) · 1.91 KB
/
PostgreSQLData+BinaryFloatingPoint.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
extension BinaryFloatingPoint where Self: LosslessStringConvertible {
/// See `PostgreSQLDataConvertible`.
public static func convertFromPostgreSQLData(_ data: PostgreSQLData) throws -> Self {
switch data.storage {
case .binary(let value):
let f: Self?
switch data.type {
case .char: f = Self(value.as(Int8.self, default: 0).bigEndian)
case .int2: f = Self(value.as(Int16.self, default: 0).bigEndian)
case .int4: f = Self(value.as(Int32.self, default: 0).bigEndian)
case .int8: f = Self(value.as(Int64.self, default: 0).bigEndian)
case .float4: f = Self(Data(value.reversed()).as(Float.self, default: 0))
case .float8: f = Self(Data(value.reversed()).as(Double.self, default: 0))
case .numeric:
let string = try String.convertFromPostgreSQLData(data)
f = Self(string)
default: throw PostgreSQLError.decode(self, from: data)
}
guard let value = f else {
throw PostgreSQLError.decode(self, from: data)
}
return value
case .text(let string):
guard let converted = Self(string) else {
throw PostgreSQLError.decode(self, from: data)
}
return converted
case .null: throw PostgreSQLError.decode(self, from: data)
}
}
/// See `PostgreSQLDataConvertible`.
public func convertToPostgreSQLData() throws -> PostgreSQLData {
let type: PostgreSQLDataFormat
switch Self.bitWidth {
case 32: type = .float4
case 64: type = .float8
default: fatalError("Floating point bit width not supported: \(Self.bitWidth)")
}
return PostgreSQLData(type, binary: .init(Data.of(self).reversed()))
}
}
extension Double: PostgreSQLDataConvertible { }
extension Float: PostgreSQLDataConvertible { }