forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQLMessageEncoder.swift
60 lines (54 loc) · 2.28 KB
/
PostgreSQLMessageEncoder.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
import Async
import Bits
import NIO
import Foundation
final class PostgreSQLMessageEncoder: MessageToByteEncoder {
/// See `MessageToByteEncoder.OutboundIn`
typealias OutboundIn = PostgreSQLMessage
/// Called once there is data to encode. The used `ByteBuffer` is allocated by `allocateOutBuffer`.
///
/// - parameters:
/// - ctx: The `ChannelHandlerContext` which this `ByteToMessageDecoder` belongs to.
/// - data: The data to encode into a `ByteBuffer`.
/// - out: The `ByteBuffer` into which we want to encode.
func encode(ctx: ChannelHandlerContext, data message: PostgreSQLMessage, out buffer: inout ByteBuffer) throws {
VERBOSE("PostgreSQLMessageSerializer.encode(ctx: \(ctx), data: \(message), out: \(buffer))")
switch message {
case .bind: buffer.write(integer: Byte.B)
default: break // no identifier
}
// leave room for size
let messageSizeIndex = buffer.writerIndex
buffer.moveWriterIndex(forwardBy: 4)
let messageStartIndex = buffer.writerIndex
let identifier: Byte?
switch message {
// case .sslSupportRequest(let request):
// identifier = nil
// try request.encode(to: encoder)
// case .startupMessage(let message):
// identifier = nil
// try message.encode(to: encoder)
// case .query(let query):
// identifier = .Q
// try query.encode(to: encoder)
// case .parse(let parseRequest):
// identifier = .P
// try parseRequest.encode(to: encoder)
// case .sync:
// identifier = .S
case .bind(let bind): bind.serialize(into: &buffer)
// case .describe(let describe):
// identifier = .D
// try describe.encode(to: encoder)
// case .execute(let execute):
// identifier = .E
// try execute.encode(to: encoder)
// case .password(let password):
// identifier = .p
// try password.encode(to: encoder)
default: throw PostgreSQLError(identifier: "encoder", reason: "Unsupported encodable type: \(type(of: message))")
}
buffer.set(integer: Int32(buffer.writerIndex - messageStartIndex), at: messageSizeIndex)
}
}