Skip to content

Commit 735b130

Browse files
committed
text format support for common types
1 parent 7b4595c commit 735b130

28 files changed

+426
-190
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import Foundation
2+
3+
/// Supported `PostgreSQLData` data types.
4+
public enum PostgreSQLData {
5+
case string(String)
6+
case data(Data)
7+
case date(Date)
8+
9+
case int8(Int8)
10+
case int16(Int16)
11+
case int32(Int32)
12+
case int(Int)
13+
14+
case uint8(UInt8)
15+
case uint16(UInt16)
16+
case uint32(UInt32)
17+
case uint(UInt)
18+
19+
case float(Float)
20+
case double(Double)
21+
22+
case null
23+
}
24+
25+
/// MARK: Polymorphic
26+
27+
extension PostgreSQLData {
28+
/// Returns string value, `nil` if not a string.
29+
public var string: String? {
30+
switch self {
31+
case .string(let s): return s
32+
default: return nil
33+
}
34+
}
35+
36+
/// Returns int value, `nil` if not an int.
37+
public var int: Int? {
38+
switch self {
39+
case .int8(let i): return Int(i)
40+
case .int16(let i): return Int(i)
41+
case .int32(let i): return Int(i)
42+
case .int(let i): return i
43+
case .uint8(let ui): return Int(ui)
44+
case .uint16(let ui): return Int(ui)
45+
case .uint32(let ui): return Int(ui)
46+
case .uint(let ui):
47+
guard ui < UInt(Int.max) else { return nil }
48+
return Int(ui)
49+
case .string(let s): return Int(s)
50+
default: return nil
51+
}
52+
}
53+
54+
/// Returns bool value, `nil` if not a bool.
55+
public var bool: Bool? {
56+
if let int = self.int {
57+
switch int {
58+
case 1: return true
59+
case 0: return false
60+
default: return nil
61+
}
62+
} else if let string = self.string {
63+
switch string.lowercased() {
64+
case "t", "true": return true
65+
case "f", "false": return false
66+
default: return nil
67+
}
68+
} else {
69+
return nil
70+
}
71+
}
72+
}
73+
74+
/// MARK: Equatable
75+
76+
extension PostgreSQLData: Equatable {
77+
/// See Equatable.==
78+
public static func ==(lhs: PostgreSQLData, rhs: PostgreSQLData) -> Bool {
79+
switch (lhs, rhs) {
80+
case (.string(let a), .string(let b)): return a == b
81+
case (.data(let a), .data(let b)): return a == b
82+
case (.date(let a), .date(let b)): return a == b
83+
case (.int8(let a), .int8(let b)): return a == b
84+
case (.int16(let a), .int16(let b)): return a == b
85+
case (.int32(let a), .int32(let b)): return a == b
86+
case (.int(let a), .int(let b)): return a == b
87+
case (.uint8(let a), .uint8(let b)): return a == b
88+
case (.uint16(let a), .uint16(let b)): return a == b
89+
case (.uint32(let a), .uint32(let b)): return a == b
90+
case (.uint(let a), .uint(let b)): return a == b
91+
case (.float(let a), .float(let b)): return a == b
92+
case (.double(let a), .double(let b)): return a == b
93+
case (.null, .null): return true
94+
default: return false
95+
}
96+
}
97+
98+
99+
}
100+
101+
/// MARK: Custom String
102+
103+
extension PostgreSQLData: CustomStringConvertible {
104+
/// See CustomStringConvertible.description
105+
public var description: String {
106+
switch self {
107+
case .string(let val): return "\"\(val)\""
108+
case .data(let val): return val.hexDebug
109+
case .date(let val): return val.description
110+
case .int8(let val): return "\(val) (int8)"
111+
case .int16(let val): return "\(val) (int16)"
112+
case .int32(let val): return "\(val) (int32)"
113+
case .int(let val): return "\(val) (int)"
114+
case .uint8(let val): return "\(val) (uint8)"
115+
case .uint16(let val): return "\(val) (uint16)"
116+
case .uint32(let val): return "\(val) (uint32)"
117+
case .uint(let val): return "\(val) (uint)"
118+
case .float(let val): return "\(val) (float)"
119+
case .double(let val): return "\(val) (double)"
120+
case .null: return "null"
121+
}
122+
}
123+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/// The data type's raw object ID.
2+
/// Use `select * from pg_type where oid = <idhere>;` to lookup more information.
3+
enum PostgreSQLDataType: Int32, Codable {
4+
case bool = 16
5+
case bytea = 17
6+
case char = 18
7+
case name = 19
8+
case int8 = 20
9+
case int2 = 21
10+
case int4 = 23
11+
case regproc = 24
12+
case text = 25
13+
case oid = 26
14+
case pg_node_tree = 194
15+
case float4 = 700
16+
case float8 = 701
17+
case _aclitem = 1034
18+
case bpchar = 1042
19+
case varchar = 1043
20+
case date = 1082
21+
case time = 1083
22+
case timestamp = 1114
23+
case numeric = 1700
24+
case void = 2278
25+
}
26+
27+
extension PostgreSQLDataType {
28+
/// Converts the supplied `PostgreSQLData` to the best matching `PostgreSQLDataType`
29+
static func type(forData data: PostgreSQLData) -> PostgreSQLDataType {
30+
switch data {
31+
case .int8: return .char
32+
case .int16: return .int2
33+
case .int32: return .int4
34+
case .int: return .int8
35+
case .uint: return .int8
36+
case .uint8: return .char
37+
case .uint16: return .int2
38+
case .uint32: return .int4
39+
case .null: return .void
40+
case .string: return .varchar
41+
case .double: return .float8
42+
case .float: return .float4
43+
case .data: return .bytea
44+
case .date: return .timestamp
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// The format code being used for the field.
2+
/// Currently will be zero (text) or one (binary).
3+
/// In a RowDescription returned from the statement variant of Describe,
4+
/// the format code is not yet known and will always be zero.
5+
enum PostgreSQLFormatCode: Int16, Codable {
6+
case text = 0
7+
case binary = 1
8+
}

Sources/PostgreSQL/Message/Messages/PostgreSQLDataRow.swift

-104
This file was deleted.

0 commit comments

Comments
 (0)