forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQLMessageDecoder.swift
282 lines (245 loc) · 13.1 KB
/
PostgreSQLMessageDecoder.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import Bits
import Foundation
/// Non-decoder wrapper for `_PostgreSQLMessageDecoder`.
final class PostgreSQLMessageDecoder {
/// Create a new `PostgreSQLMessageDecoder`
init() {}
/// Encodes a `PostgreSQLMessage` to `Data`.
func decode(_ data: Data) throws -> (PostgreSQLMessage, Int)? {
let decoder = _PostgreSQLMessageDecoder(data: data)
guard decoder.data.count >= 1 else {
return nil
}
let type = try decoder.decode(Byte.self)
guard try decoder.verifyLength() else {
return nil
}
let message: PostgreSQLMessage
switch type {
case .E: message = try .error(decoder.decode())
case .N: message = try .notice(decoder.decode())
case .R: message = try .authenticationRequest(decoder.decode())
case .S: message = try .parameterStatus(decoder.decode())
case .K: message = try .backendKeyData(decoder.decode())
case .Z: message = try .readyForQuery(decoder.decode())
case .T: message = try .rowDescription(decoder.decode())
case .D: message = try .dataRow(decoder.decode())
case .C: message = try .close(decoder.decode())
case .one: message = .parseComplete
case .two: message = .bindComplete
case .n: message = .noData
case .t: message = try .parameterDescription(decoder.decode())
default:
let string = String(bytes: [type], encoding: .ascii) ?? "n/a"
throw PostgreSQLError(
identifier: "decoder",
reason: "Unrecognized message type: \(string) (\(type)",
possibleCauses: ["Connected to non-PostgreSQL database"],
suggestedFixes: ["Connect to PostgreSQL database"]
)
}
return (message, decoder.data.count)
}
}
// MARK: Decoder / Single
fileprivate final class _PostgreSQLMessageDecoder: Decoder, SingleValueDecodingContainer {
/// See Decoder.codingPath
var codingPath: [CodingKey]
/// See Decoder.userInfo
var userInfo: [CodingUserInfoKey: Any]
/// The data being decoded.
var data: Data
/// Creates a new internal `_PostgreSQLMessageDecoder`.
init(data: Data) {
self.codingPath = []
self.userInfo = [:]
self.data = data
}
/// Extracts and verifies the data length.
func verifyLength() throws -> Bool {
guard let length = try extractLength() else {
return false
}
guard data.count + MemoryLayout<Int32>.size >= length else {
return false
}
return true
}
/// Extracts an Int32 length, returning `nil`
/// if it doesn't exist.
func extractLength() throws -> Int32? {
guard data.count >= 4 else {
// need length
return nil
}
return try decode(Int32.self)
}
/// See Encoder.singleValueContainer
func singleValueContainer() throws -> SingleValueDecodingContainer {
return self
}
/// See SingleValueDecodingContainer.decode
func decode(_ type: UInt8.Type) throws -> UInt8 {
return self.data.unsafePopFirst()
}
/// See SingleValueDecodingContainer.decode
func decode(_ type: Int16.Type) throws -> Int16 {
return try decode(fixedWidthInteger: Int16.self)
}
/// See SingleValueDecodingContainer.decode
func decode(_ type: Int32.Type) throws -> Int32 {
return try decode(fixedWidthInteger: Int32.self)
}
/// Decodes a fixed width integer.
func decode<B>(fixedWidthInteger type: B.Type) throws -> B where B: FixedWidthInteger {
return data.extract(B.self).bigEndian
}
/// See SingleValueDecodingContainer.decode
func decode(_ type: String.Type) throws -> String {
var bytes: [UInt8] = []
parse: while true {
let byte = self.data.unsafePopFirst()
switch byte {
case 0: break parse // c style strings
default: bytes.append(byte)
}
}
let data = Data(bytes: bytes)
guard let string = String(data: data, encoding: .utf8) else { throw PostgreSQLError(identifier: "decoder", reason: "Unsupported decode type: non-UTF8 string")}
return string
}
/// See SingleValueDecodingContainer.decode
func decode<T>(_ type: T.Type = T.self) throws -> T where T: Decodable {
if T.self == Data.self {
let count = try Int(decode(Int32.self))
switch count {
case 0: return Data() as! T
case 1...:
let sub: Data = data.subdata(in: data.startIndex..<data.index(data.startIndex, offsetBy: count))
data = data.advanced(by: count)
return sub as! T
default: throw PostgreSQLError(identifier: "decoder", reason: "Illegal data row column value count: \(count)")
}
} else {
return try T(from: self)
}
}
/// See SingleValueDecodingContainer.decodeNil
func decodeNil() -> Bool {
guard data.count >= 4 else {
return false
}
/// if Int32 decode == -1, then this should be decoding `Data?.none`
let count = data.withUnsafeBytes { (pointer: UnsafePointer<UInt8>) -> Int32 in
return pointer.withMemoryRebound(to: Int32.self, capacity: 1) { (pointer: UnsafePointer<Int32>) -> Int32 in
return pointer.pointee.bigEndian
}
}
switch count {
case -1:
data = data.advanced(by: MemoryLayout<Int32>.size)
return true
default: return false
}
}
/// See Decoder.container
func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key : CodingKey {
let container = _PostgreSQLMessageKeyedDecoder<Key>(decoder: self)
return KeyedDecodingContainer(container)
}
/// See Decoder.unkeyedContainer
func unkeyedContainer() throws -> UnkeyedDecodingContainer {
return _PostgreSQLMessageUnkeyedDecoder(decoder: self)
}
// Unsupported
func decode(_ type: Bool.Type) throws -> Bool { throw PostgreSQLError(identifier: "decoder", reason: "Unsupported decode type: \(type)") }
func decode(_ type: Int.Type) throws -> Int { throw PostgreSQLError(identifier: "decoder", reason: "Unsupported decode type: \(type)") }
func decode(_ type: Int8.Type) throws -> Int8 { throw PostgreSQLError(identifier: "decoder", reason: "Unsupported decode type: \(type)") }
func decode(_ type: Int64.Type) throws -> Int64 { throw PostgreSQLError(identifier: "decoder", reason: "Unsupported decode type: \(type)") }
func decode(_ type: UInt.Type) throws -> UInt { throw PostgreSQLError(identifier: "decoder", reason: "Unsupported decode type: \(type)") }
func decode(_ type: UInt16.Type) throws -> UInt16 { throw PostgreSQLError(identifier: "decoder", reason: "Unsupported decode type: \(type)") }
func decode(_ type: UInt32.Type) throws -> UInt32 { throw PostgreSQLError(identifier: "decoder", reason: "Unsupported decode type: \(type)") }
func decode(_ type: UInt64.Type) throws -> UInt64 { throw PostgreSQLError(identifier: "decoder", reason: "Unsupported decode type: \(type)") }
func decode(_ type: Float.Type) throws -> Float { throw PostgreSQLError(identifier: "decoder", reason: "Unsupported decode type: \(type)") }
func decode(_ type: Double.Type) throws -> Double { throw PostgreSQLError(identifier: "decoder", reason: "Unsupported decode type: \(type)") }
}
// MARK: Keyed
fileprivate final class _PostgreSQLMessageKeyedDecoder<K>: KeyedDecodingContainerProtocol where K: CodingKey {
typealias Key = K
var codingPath: [CodingKey]
var allKeys: [K]
var decoder: _PostgreSQLMessageDecoder
/// Creates a new internal `_PostgreSQLMessageKeyedDecoder`
init(decoder: _PostgreSQLMessageDecoder) {
self.codingPath = []
self.allKeys = []
self.decoder = decoder
}
// Map decode for key to decoder
func contains(_ key: K) -> Bool { return true }
func decode(_ type: Bool.Type, forKey key: K) throws -> Bool { return try decoder.decode(Bool.self) }
func decode(_ type: Int.Type, forKey key: K) throws -> Int { return try decoder.decode(Int.self) }
func decode(_ type: Int8.Type, forKey key: K) throws -> Int8 { return try decoder.decode(Int8.self) }
func decode(_ type: Int16.Type, forKey key: K) throws -> Int16 { return try decoder.decode(Int16.self) }
func decode(_ type: Int32.Type, forKey key: K) throws -> Int32 { return try decoder.decode(Int32.self) }
func decode(_ type: Int64.Type, forKey key: K) throws -> Int64 { return try decoder.decode(Int64.self) }
func decode(_ type: UInt.Type, forKey key: K) throws -> UInt { return try decoder.decode(UInt.self) }
func decode(_ type: UInt8.Type, forKey key: K) throws -> UInt8 { return try decoder.decode(UInt8.self) }
func decode(_ type: UInt16.Type, forKey key: K) throws -> UInt16 { return try decoder.decode(UInt16.self) }
func decode(_ type: UInt32.Type, forKey key: K) throws -> UInt32 { return try decoder.decode(UInt32.self) }
func decode(_ type: UInt64.Type, forKey key: K) throws -> UInt64 { return try decoder.decode(UInt64.self) }
func decode(_ type: Float.Type, forKey key: K) throws -> Float { return try decoder.decode(Float.self) }
func decode(_ type: Double.Type, forKey key: K) throws -> Double { return try decoder.decode(Double.self) }
func decode(_ type: String.Type, forKey key: K) throws -> String { return try decoder.decode(String.self) }
func decode<T>(_ type: T.Type, forKey key: K) throws -> T where T : Decodable { return try decoder.decode(T.self) }
func superDecoder() throws -> Decoder { return decoder }
func superDecoder(forKey key: K) throws -> Decoder { return decoder }
func decodeNil(forKey key: K) throws -> Bool { return decoder.decodeNil() }
// Unsupported
func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: K) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
let container = _PostgreSQLMessageKeyedDecoder<NestedKey>(decoder: decoder)
return KeyedDecodingContainer(container)
}
func nestedUnkeyedContainer(forKey key: K) throws -> UnkeyedDecodingContainer {
throw PostgreSQLError(identifier: "decoder", reason: "Unsupported decoding type: nested unkeyed container")
}
}
/// MARK: Unkeyed
fileprivate final class _PostgreSQLMessageUnkeyedDecoder: UnkeyedDecodingContainer {
var count: Int?
var isAtEnd: Bool {
return currentIndex == count
}
var currentIndex: Int
var codingPath: [CodingKey]
var decoder: _PostgreSQLMessageDecoder
/// Creates a new internal `_PostgreSQLMessageUnkeyedDecoder`
init(decoder: _PostgreSQLMessageDecoder) {
self.codingPath = []
self.decoder = decoder
self.count = try! Int(decoder.decode(Int16.self))
currentIndex = 0
}
func decode(_ type: Bool.Type) throws -> Bool { currentIndex += 1; return try decoder.decode(Bool.self) }
func decode(_ type: Int.Type) throws -> Int { currentIndex += 1; return try decoder.decode(Int.self) }
func decode(_ type: Int8.Type) throws -> Int8 { currentIndex += 1; return try decoder.decode(Int8.self) }
func decode(_ type: Int16.Type) throws -> Int16 { currentIndex += 1; return try decoder.decode(Int16.self) }
func decode(_ type: Int32.Type) throws -> Int32 { currentIndex += 1; return try decoder.decode(Int32.self) }
func decode(_ type: Int64.Type) throws -> Int64 { currentIndex += 1; return try decoder.decode(Int64.self) }
func decode(_ type: UInt.Type) throws -> UInt { currentIndex += 1; return try decoder.decode(UInt.self) }
func decode(_ type: UInt8.Type) throws -> UInt8 { currentIndex += 1; return try decoder.decode(UInt8.self) }
func decode(_ type: UInt16.Type) throws -> UInt16 { currentIndex += 1; return try decoder.decode(UInt16.self) }
func decode(_ type: UInt32.Type) throws -> UInt32 { currentIndex += 1; return try decoder.decode(UInt32.self) }
func decode(_ type: UInt64.Type) throws -> UInt64 { currentIndex += 1; return try decoder.decode(UInt64.self) }
func decode(_ type: Float.Type) throws -> Float { currentIndex += 1; return try decoder.decode(Float.self) }
func decode(_ type: Double.Type) throws -> Double { currentIndex += 1; return try decoder.decode(Double.self) }
func decode(_ type: String.Type) throws -> String { currentIndex += 1; return try decoder.decode(String.self) }
func decodeNil() throws -> Bool {currentIndex += 1; return decoder.decodeNil() }
func decode<T>(_ type: T.Type) throws -> T where T : Decodable { currentIndex += 1; return try decoder.decode(T.self) }
func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer { currentIndex += 1; return _PostgreSQLMessageUnkeyedDecoder(decoder: decoder) }
func superDecoder() throws -> Decoder { return decoder }
func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
let container = _PostgreSQLMessageKeyedDecoder<NestedKey>(decoder: decoder)
return KeyedDecodingContainer(container)
}
}