forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresData+Int.swift
205 lines (178 loc) · 5.03 KB
/
PostgresData+Int.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
extension PostgresData {
public init(int value: Int) {
self.init(fwi: value)
}
public init(int8 value: Int8) {
self.init(fwi: value)
}
public init(int16 value: Int16) {
self.init(fwi: value)
}
public init(int32 value: Int32) {
self.init(fwi: value)
}
public init(int64 value: Int64) {
self.init(fwi: value)
}
public init(uint value: UInt) {
self.init(fwi: value)
}
public init(uint8 value: UInt8) {
self.init(fwi: value)
}
public init(uint16 value: UInt16) {
self.init(fwi: value)
}
public init(uint32 value: UInt32) {
self.init(fwi: value)
}
public init(uint64 value: UInt64) {
self.init(fwi: value)
}
public var int: Int? {
return fwi()
}
public var int8: Int8? {
return fwi()
}
public var int16: Int16? {
return fwi()
}
public var int32: Int32? {
return fwi()
}
public var int64: Int64? {
return fwi()
}
public var uint: UInt? {
return fwi()
}
public var uint8: UInt8? {
return fwi()
}
public var uint16: UInt16? {
return fwi()
}
public var uint32: UInt32? {
return fwi()
}
public var uint64: UInt64? {
return fwi()
}
}
private extension PostgresData {
init<I>(fwi: I) where I: FixedWidthInteger {
let capacity: Int
let type: PostgresDataType
switch I.bitWidth {
case 8:
capacity = 1
type = .char
case 16:
capacity = 2
type = .int2
case 32:
capacity = 3
type = .int4
case 64:
capacity = 4
type = .int8
default:
fatalError("Cannot encode \(I.self) to PostgresData")
}
var buffer = ByteBufferAllocator().buffer(capacity: capacity)
buffer.writeInteger(fwi)
self.init(type: type, formatCode: .binary, value: buffer)
}
func fwi<I>(_ type: I.Type = I.self) -> I?
where I: FixedWidthInteger
{
guard var value = self.value else {
return nil
}
switch self.formatCode {
case .binary:
switch self.type {
case .char, .bpchar:
guard value.readableBytes == 1 else {
return nil
}
guard let uint8 = value.getInteger(at: value.readerIndex, as: UInt8.self) else {
return nil
}
return I(uint8)
case .int2:
assert(value.readableBytes == 2)
guard let int16 = value.readInteger(as: Int16.self) else {
return nil
}
return I(int16)
case .int4, .regproc:
assert(value.readableBytes == 4)
guard let int32 = value.getInteger(at: value.readerIndex, as: Int32.self) else {
return nil
}
return I(int32)
case .oid:
assert(value.readableBytes == 4)
guard let uint32 = value.getInteger(at: value.readerIndex, as: UInt32.self) else {
return nil
}
return I(uint32)
case .int8:
assert(value.readableBytes == 8)
guard let int64 = value.getInteger(at: value.readerIndex, as: Int64.self) else {
return nil
}
return I(int64)
default:
return nil
}
case .text:
guard let string = self.string else {
return nil
}
return I(string)
}
}
}
extension FixedWidthInteger {
public static var postgresDataType: PostgresDataType {
switch self.bitWidth {
case 8:
return .char
case 16:
return .int2
case 32:
return .int4
case 64:
return .int8
default:
fatalError("\(self.bitWidth) not supported")
}
}
public var postgresData: PostgresData? {
return .init(fwi: self)
}
public init?(postgresData: PostgresData) {
guard let fwi = postgresData.fwi(Self.self) else {
return nil
}
self = fwi
}
}
extension Int: PostgresDataConvertible { }
extension Int8: PostgresDataConvertible { }
extension Int16: PostgresDataConvertible { }
extension Int32: PostgresDataConvertible { }
extension Int64: PostgresDataConvertible { }
extension UInt: PostgresDataConvertible { }
extension UInt8: PostgresDataConvertible { }
extension UInt16: PostgresDataConvertible { }
extension UInt32: PostgresDataConvertible { }
extension UInt64: PostgresDataConvertible { }
extension PostgresData: ExpressibleByIntegerLiteral {
public init(integerLiteral value: Int) {
self.init(int: value)
}
}