forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgres+PSQLCompat.swift
124 lines (108 loc) · 4.21 KB
/
Postgres+PSQLCompat.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
import NIOCore
struct PostgresJSONDecoderWrapper: PSQLJSONDecoder {
let downstream: PostgresJSONDecoder
init(_ downstream: PostgresJSONDecoder) {
self.downstream = downstream
}
func decode<T>(_ type: T.Type, from buffer: ByteBuffer) throws -> T where T : Decodable {
var buffer = buffer
let data = buffer.readData(length: buffer.readableBytes)!
return try self.downstream.decode(T.self, from: data)
}
}
struct PostgresJSONEncoderWrapper: PSQLJSONEncoder {
let downstream: PostgresJSONEncoder
init(_ downstream: PostgresJSONEncoder) {
self.downstream = downstream
}
func encode<T>(_ value: T, into buffer: inout ByteBuffer) throws where T : Encodable {
let data = try self.downstream.encode(value)
buffer.writeData(data)
}
}
extension PostgresData: PSQLEncodable {
var psqlType: PSQLDataType {
PSQLDataType(Int32(self.type.rawValue))
}
var psqlFormat: PSQLFormat {
.binary
}
func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) throws {
preconditionFailure("Should never be hit, since `encodeRaw` is implemented.")
}
// encoding
func encodeRaw(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) throws {
switch self.value {
case .none:
byteBuffer.writeInteger(-1, as: Int32.self)
case .some(var input):
byteBuffer.writeInteger(Int32(input.readableBytes))
byteBuffer.writeBuffer(&input)
}
}
}
extension PostgresData: PSQLDecodable {
static func decode(from byteBuffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> PostgresData {
let myBuffer = byteBuffer.readSlice(length: byteBuffer.readableBytes)!
return PostgresData(type: PostgresDataType(UInt32(type.rawValue)), typeModifier: nil, formatCode: .binary, value: myBuffer)
}
}
extension PostgresData: PSQLCodable {}
extension PSQLError {
func toPostgresError() -> Error {
switch self.base {
case .server(let errorMessage):
var fields = [PostgresMessage.Error.Field: String]()
fields.reserveCapacity(errorMessage.fields.count)
errorMessage.fields.forEach { (key, value) in
fields[PostgresMessage.Error.Field(rawValue: key.rawValue)!] = value
}
return PostgresError.server(PostgresMessage.Error(fields: fields))
case .sslUnsupported:
return PostgresError.protocol("Server does not support TLS")
case .failedToAddSSLHandler(underlying: let underlying):
return underlying
case .decoding(let decodingError):
return PostgresError.protocol("Error decoding message: \(decodingError)")
case .unexpectedBackendMessage(let message):
return PostgresError.protocol("Unexpected message: \(message)")
case .unsupportedAuthMechanism(let authScheme):
return PostgresError.protocol("Unsupported auth scheme: \(authScheme)")
case .authMechanismRequiresPassword:
return PostgresError.protocol("Unable to authenticate without password")
case .saslError(underlyingError: let underlying):
return underlying
case .tooManyParameters:
return self
case .connectionQuiescing:
return PostgresError.connectionClosed
case .connectionClosed:
return PostgresError.connectionClosed
case .connectionError(underlying: let underlying):
return underlying
case .casting(let castingError):
return castingError
case .uncleanShutdown:
return PostgresError.protocol("Unexpected connection close")
}
}
}
extension PostgresFormatCode {
init(psqlFormatCode: PSQLFormat) {
switch psqlFormatCode {
case .binary:
self = .binary
case .text:
self = .text
}
}
}
extension Error {
internal var asAppropriatePostgresError: Error {
if let psqlError = self as? PSQLError {
return psqlError.toPostgresError()
} else {
return self
}
}
}