Skip to content

Commit dd5b17c

Browse files
authored
Add PostgresQuery (vapor#223)
1 parent f588870 commit dd5b17c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+525
-403
lines changed

Sources/PostgresNIO/Connection/PostgresConnection+Connect.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,11 @@ extension PostgresConnection {
1010
logger: Logger = .init(label: "codes.vapor.postgres"),
1111
on eventLoop: EventLoop
1212
) -> EventLoopFuture<PostgresConnection> {
13-
14-
let coders = PSQLConnection.Configuration.Coders(
15-
jsonEncoder: _defaultJSONEncoder
16-
)
17-
1813
let configuration = PSQLConnection.Configuration(
1914
connection: .resolved(address: socketAddress, serverName: serverHostname),
2015
authentication: nil,
21-
tlsConfiguration: tlsConfiguration,
22-
coders: coders)
16+
tlsConfiguration: tlsConfiguration
17+
)
2318

2419
return PSQLConnection.connect(
2520
configuration: configuration,

Sources/PostgresNIO/Connection/PostgresConnection+Database.swift

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ extension PostgresConnection: PostgresDatabase {
1515

1616
switch command {
1717
case .query(let query, let binds, let onMetadata, let onRow):
18-
resultFuture = self.underlying.query(query, binds, logger: logger).flatMap { stream in
18+
var psqlQuery = PostgresQuery(unsafeSQL: query, binds: .init(capacity: binds.count))
19+
binds.forEach {
20+
// We can bang the try here as encoding PostgresData does not throw. The throw
21+
// is just an option for the protocol.
22+
try! psqlQuery.appendBinding($0, context: .default)
23+
}
24+
25+
resultFuture = self.underlying.query(psqlQuery, logger: logger).flatMap { stream in
1926
let fields = stream.rowDescription.map { column in
2027
PostgresMessage.RowDescription.Field(
2128
name: column.name,
@@ -34,7 +41,14 @@ extension PostgresConnection: PostgresDatabase {
3441
}
3542
}
3643
case .queryAll(let query, let binds, let onResult):
37-
resultFuture = self.underlying.query(query, binds, logger: logger).flatMap { rows in
44+
var psqlQuery = PostgresQuery(unsafeSQL: query, binds: .init(capacity: binds.count))
45+
binds.forEach {
46+
// We can bang the try here as encoding PostgresData does not throw. The throw
47+
// is just an option for the protocol.
48+
try! psqlQuery.appendBinding($0, context: .default)
49+
}
50+
51+
resultFuture = self.underlying.query(psqlQuery, logger: logger).flatMap { rows in
3852
let fields = rows.rowDescription.map { column in
3953
PostgresMessage.RowDescription.Field(
4054
name: column.name,
@@ -65,7 +79,18 @@ extension PostgresConnection: PostgresDatabase {
6579
request.prepared = PreparedQuery(underlying: $0, database: self)
6680
}
6781
case .executePreparedStatement(let preparedQuery, let binds, let onRow):
68-
resultFuture = self.underlying.execute(preparedQuery.underlying, binds, logger: logger).flatMap { rows in
82+
var bindings = PostgresBindings()
83+
binds.forEach { data in
84+
try! bindings.append(data, context: .default)
85+
}
86+
87+
let statement = PSQLExecuteStatement(
88+
name: preparedQuery.underlying.name,
89+
binds: bindings,
90+
rowDescription: preparedQuery.underlying.rowDescription
91+
)
92+
93+
resultFuture = self.underlying.execute(statement, logger: logger).flatMap { rows in
6994
guard let lookupTable = preparedQuery.lookupTable else {
7095
return self.eventLoop.makeSucceededFuture(())
7196
}

Sources/PostgresNIO/Data/PostgresDataType.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public typealias PostgresFormatCode = PostgresFormat
2626

2727
/// The data type's raw object ID.
2828
/// Use `select * from pg_type where oid = <idhere>;` to lookup more information.
29-
public struct PostgresDataType: RawRepresentable, Equatable, CustomStringConvertible {
29+
public struct PostgresDataType: RawRepresentable, Hashable, CustomStringConvertible {
3030
/// `0`
3131
public static let null = PostgresDataType(0)
3232
/// `16`

Sources/PostgresNIO/New/BufferedMessageEncoder.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import NIOCore
22

3-
struct BufferedMessageEncoder<Encoder: MessageToByteEncoder> {
3+
struct BufferedMessageEncoder {
44
private enum State {
55
case flushed
66
case writable
77
}
88

99
private var buffer: ByteBuffer
1010
private var state: State = .writable
11-
private var encoder: Encoder
11+
private var encoder: PSQLFrontendMessageEncoder
1212

13-
init(buffer: ByteBuffer, encoder: Encoder) {
13+
init(buffer: ByteBuffer, encoder: PSQLFrontendMessageEncoder) {
1414
self.buffer = buffer
1515
self.encoder = encoder
1616
}
1717

18-
mutating func encode(_ message: Encoder.OutboundIn) throws {
18+
mutating func encode(_ message: PSQLFrontendMessage) {
1919
switch self.state {
2020
case .flushed:
2121
self.state = .writable
@@ -25,7 +25,7 @@ struct BufferedMessageEncoder<Encoder: MessageToByteEncoder> {
2525
break
2626
}
2727

28-
try self.encoder.encode(data: message, out: &self.buffer)
28+
self.encoder.encode(data: message, out: &self.buffer)
2929
}
3030

3131
mutating func flush() -> ByteBuffer? {

Sources/PostgresNIO/New/Connection State Machine/ConnectionStateMachine.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ struct ConnectionStateMachine {
8484
// Connection Actions
8585

8686
// --- general actions
87-
case sendParseDescribeBindExecuteSync(query: String, binds: [PSQLEncodable])
88-
case sendBindExecuteSync(statementName: String, binds: [PSQLEncodable])
87+
case sendParseDescribeBindExecuteSync(PostgresQuery)
88+
case sendBindExecuteSync(PSQLExecuteStatement)
8989
case failQuery(ExtendedQueryContext, with: PSQLError, cleanupContext: CleanUpContext?)
9090
case succeedQuery(ExtendedQueryContext, columns: [RowDescription.Column])
9191
case succeedQueryNoRowsComming(ExtendedQueryContext, commandTag: String)
@@ -1050,11 +1050,11 @@ extension ConnectionStateMachine {
10501050
}
10511051

10521052
return false
1053-
case .decoding(_):
1053+
case .decoding:
10541054
return true
1055-
case .unexpectedBackendMessage(_):
1055+
case .unexpectedBackendMessage:
10561056
return true
1057-
case .unsupportedAuthMechanism(_):
1057+
case .unsupportedAuthMechanism:
10581058
return true
10591059
case .authMechanismRequiresPassword:
10601060
return true
@@ -1106,10 +1106,10 @@ extension ConnectionStateMachine {
11061106
extension ConnectionStateMachine {
11071107
mutating func modify(with action: ExtendedQueryStateMachine.Action) -> ConnectionStateMachine.ConnectionAction {
11081108
switch action {
1109-
case .sendParseDescribeBindExecuteSync(let query, let binds):
1110-
return .sendParseDescribeBindExecuteSync(query: query, binds: binds)
1111-
case .sendBindExecuteSync(let statementName, let binds):
1112-
return .sendBindExecuteSync(statementName: statementName, binds: binds)
1109+
case .sendParseDescribeBindExecuteSync(let query):
1110+
return .sendParseDescribeBindExecuteSync(query)
1111+
case .sendBindExecuteSync(let executeStatement):
1112+
return .sendBindExecuteSync(executeStatement)
11131113
case .failQuery(let requestContext, with: let error):
11141114
let cleanupContext = self.setErrorAndCreateCleanupContextIfNeeded(error)
11151115
return .failQuery(requestContext, with: error, cleanupContext: cleanupContext)

Sources/PostgresNIO/New/Connection State Machine/ExtendedQueryStateMachine.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ struct ExtendedQueryStateMachine {
2323
}
2424

2525
enum Action {
26-
case sendParseDescribeBindExecuteSync(query: String, binds: [PSQLEncodable])
27-
case sendBindExecuteSync(statementName: String, binds: [PSQLEncodable])
26+
case sendParseDescribeBindExecuteSync(PostgresQuery)
27+
case sendBindExecuteSync(PSQLExecuteStatement)
2828

2929
// --- general actions
3030
case failQuery(ExtendedQueryContext, with: PSQLError)
@@ -56,18 +56,18 @@ struct ExtendedQueryStateMachine {
5656
case .unnamed(let query):
5757
return self.avoidingStateMachineCoW { state -> Action in
5858
state = .parseDescribeBindExecuteSyncSent(queryContext)
59-
return .sendParseDescribeBindExecuteSync(query: query, binds: queryContext.bind)
59+
return .sendParseDescribeBindExecuteSync(query)
6060
}
6161

62-
case .preparedStatement(let name, let rowDescription):
62+
case .preparedStatement(let prepared):
6363
return self.avoidingStateMachineCoW { state -> Action in
64-
switch rowDescription {
64+
switch prepared.rowDescription {
6565
case .some(let rowDescription):
6666
state = .rowDescriptionReceived(queryContext, rowDescription.columns)
6767
case .none:
6868
state = .noDataMessageReceived(queryContext)
6969
}
70-
return .sendBindExecuteSync(statementName: name, binds: queryContext.bind)
70+
return .sendBindExecuteSync(prepared)
7171
}
7272
}
7373
}

Sources/PostgresNIO/New/Data/Array+PSQLCodable.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ extension Array: PSQLEncodable where Element: PSQLArrayElement {
7676
.binary
7777
}
7878

79-
func encode(into buffer: inout ByteBuffer, context: PSQLEncodingContext) throws {
79+
func encode<JSONEncoder: PostgresJSONEncoder>(
80+
into buffer: inout ByteBuffer,
81+
context: PSQLEncodingContext<JSONEncoder>
82+
) throws {
8083
// 0 if empty, 1 if not
8184
buffer.writeInteger(self.isEmpty ? 0 : 1, as: UInt32.self)
8285
// b

Sources/PostgresNIO/New/Data/Bool+PSQLCodable.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ extension Bool: PSQLCodable {
4949
}
5050
}
5151

52-
func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
52+
func encode<JSONEncoder: PostgresJSONEncoder>(
53+
into byteBuffer: inout ByteBuffer,
54+
context: PSQLEncodingContext<JSONEncoder>
55+
) {
5356
byteBuffer.writeInteger(self ? 1 : 0, as: UInt8.self)
5457
}
5558
}

Sources/PostgresNIO/New/Data/Bytes+PSQLCodable.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ extension PSQLEncodable where Self: Sequence, Self.Element == UInt8 {
1111
.binary
1212
}
1313

14-
func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
14+
func encode<JSONEncoder: PostgresJSONEncoder>(
15+
into byteBuffer: inout ByteBuffer,
16+
context: PSQLEncodingContext<JSONEncoder>
17+
) {
1518
byteBuffer.writeBytes(self)
1619
}
1720
}
@@ -25,7 +28,10 @@ extension ByteBuffer: PSQLCodable {
2528
.binary
2629
}
2730

28-
func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
31+
func encode<JSONEncoder: PostgresJSONEncoder>(
32+
into byteBuffer: inout ByteBuffer,
33+
context: PSQLEncodingContext<JSONEncoder>
34+
) {
2935
var copyOfSelf = self // dirty hack
3036
byteBuffer.writeBuffer(&copyOfSelf)
3137
}
@@ -49,7 +55,10 @@ extension Data: PSQLCodable {
4955
.binary
5056
}
5157

52-
func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
58+
func encode<JSONEncoder: PostgresJSONEncoder>(
59+
into byteBuffer: inout ByteBuffer,
60+
context: PSQLEncodingContext<JSONEncoder>
61+
) {
5362
byteBuffer.writeBytes(self)
5463
}
5564

Sources/PostgresNIO/New/Data/Date+PSQLCodable.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ extension Date: PSQLCodable {
3434
}
3535
}
3636

37-
func encode(into buffer: inout ByteBuffer, context: PSQLEncodingContext) {
37+
func encode<JSONEncoder: PostgresJSONEncoder>(
38+
into byteBuffer: inout ByteBuffer,
39+
context: PSQLEncodingContext<JSONEncoder>
40+
) {
3841
let seconds = self.timeIntervalSince(Self._psqlDateStart) * Double(Self._microsecondsPerSecond)
39-
buffer.writeInteger(Int64(seconds))
42+
byteBuffer.writeInteger(Int64(seconds))
4043
}
4144

4245
// MARK: Private Constants

0 commit comments

Comments
 (0)