forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgres+PSQLCompat.swift
102 lines (92 loc) · 3.45 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
import NIOCore
extension PostgresData: PSQLEncodable {
var psqlType: PostgresDataType {
self.type
}
var psqlFormat: PostgresFormat {
.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<JSONDecoder: PostgresJSONDecoder>(
from buffer: inout ByteBuffer,
type: PostgresDataType,
format: PostgresFormat,
context: PostgresDecodingContext<JSONDecoder>
) throws -> Self {
let myBuffer = buffer.readSlice(length: buffer.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 PostgresFormat {
init(psqlFormatCode: PostgresFormat) {
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
}
}
}