forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresQuery.swift
152 lines (128 loc) · 4.49 KB
/
PostgresQuery.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
/// A Postgres SQL query, that can be executed on a Postgres server. Contains the raw sql string and bindings.
public struct PostgresQuery: Hashable {
/// The query string
public var sql: String
/// The query binds
public var binds: PostgresBindings
public init(unsafeSQL sql: String, binds: PostgresBindings = PostgresBindings()) {
self.sql = sql
self.binds = binds
}
}
extension PostgresQuery: ExpressibleByStringInterpolation {
public init(stringInterpolation: StringInterpolation) {
self.sql = stringInterpolation.sql
self.binds = stringInterpolation.binds
}
public init(stringLiteral value: String) {
self.sql = value
self.binds = PostgresBindings()
}
}
extension PostgresQuery {
public struct StringInterpolation: StringInterpolationProtocol {
public typealias StringLiteralType = String
@usableFromInline
var sql: String
@usableFromInline
var binds: PostgresBindings
public init(literalCapacity: Int, interpolationCount: Int) {
self.sql = ""
self.binds = PostgresBindings(capacity: interpolationCount)
}
public mutating func appendLiteral(_ literal: String) {
self.sql.append(contentsOf: literal)
}
@inlinable
public mutating func appendInterpolation<Value: PostgresEncodable>(_ value: Value) throws {
try self.binds.append(value, context: .default)
self.sql.append(contentsOf: "$\(self.binds.count)")
}
@inlinable
public mutating func appendInterpolation<Value: PostgresEncodable>(_ value: Optional<Value>) throws {
switch value {
case .none:
self.binds.appendNull()
case .some(let value):
try self.binds.append(value, context: .default)
}
self.sql.append(contentsOf: "$\(self.binds.count)")
}
@inlinable
public mutating func appendInterpolation<Value: PostgresEncodable, JSONEncoder: PostgresJSONEncoder>(
_ value: Value,
context: PostgresEncodingContext<JSONEncoder>
) throws {
try self.binds.append(value, context: context)
self.sql.append(contentsOf: "$\(self.binds.count)")
}
@inlinable
public mutating func appendInterpolation(unescaped interpolated: String) {
self.sql.append(contentsOf: interpolated)
}
}
}
struct PSQLExecuteStatement {
/// The statements name
var name: String
/// The binds
var binds: PostgresBindings
var rowDescription: RowDescription?
}
public struct PostgresBindings: Hashable {
@usableFromInline
struct Metadata: Hashable {
@usableFromInline
var dataType: PostgresDataType
@usableFromInline
var format: PostgresFormat
@inlinable
init(dataType: PostgresDataType, format: PostgresFormat) {
self.dataType = dataType
self.format = format
}
@inlinable
init<Value: PostgresEncodable>(value: Value) {
self.init(dataType: Value.psqlType, format: Value.psqlFormat)
}
}
@usableFromInline
var metadata: [Metadata]
@usableFromInline
var bytes: ByteBuffer
public var count: Int {
self.metadata.count
}
public init() {
self.metadata = []
self.bytes = ByteBuffer()
}
public init(capacity: Int) {
self.metadata = []
self.metadata.reserveCapacity(capacity)
self.bytes = ByteBuffer()
self.bytes.reserveCapacity(128 * capacity)
}
public mutating func appendNull() {
self.bytes.writeInteger(-1, as: Int32.self)
self.metadata.append(.init(dataType: .null, format: .binary))
}
@inlinable
public mutating func append<Value: PostgresEncodable, JSONEncoder: PostgresJSONEncoder>(
_ value: Value,
context: PostgresEncodingContext<JSONEncoder>
) throws {
try value.encodeRaw(into: &self.bytes, context: context)
self.metadata.append(.init(value: value))
}
mutating func append(_ postgresData: PostgresData) {
switch postgresData.value {
case .none:
self.bytes.writeInteger(-1, as: Int32.self)
case .some(var input):
self.bytes.writeInteger(Int32(input.readableBytes))
self.bytes.writeBuffer(&input)
}
self.metadata.append(.init(dataType: postgresData.type, format: .binary))
}
}