Skip to content

Commit

Permalink
Add PostgresQuery
Browse files Browse the repository at this point in the history
Postgres compiles

Fix tests
  • Loading branch information
fabianfett committed Feb 19, 2022
1 parent f588870 commit 4a2b050
Show file tree
Hide file tree
Showing 49 changed files with 465 additions and 386 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@ extension PostgresConnection {
logger: Logger = .init(label: "codes.vapor.postgres"),
on eventLoop: EventLoop
) -> EventLoopFuture<PostgresConnection> {

let coders = PSQLConnection.Configuration.Coders(
jsonEncoder: _defaultJSONEncoder
)

let configuration = PSQLConnection.Configuration(
connection: .resolved(address: socketAddress, serverName: serverHostname),
authentication: nil,
tlsConfiguration: tlsConfiguration,
coders: coders)
tlsConfiguration: tlsConfiguration
)

return PSQLConnection.connect(
configuration: configuration,
Expand Down
13 changes: 12 additions & 1 deletion Sources/PostgresNIO/Connection/PostgresConnection+Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,18 @@ extension PostgresConnection: PostgresDatabase {
request.prepared = PreparedQuery(underlying: $0, database: self)
}
case .executePreparedStatement(let preparedQuery, let binds, let onRow):
resultFuture = self.underlying.execute(preparedQuery.underlying, binds, logger: logger).flatMap { rows in
var bindings = PostgresBindings()
binds.forEach { data in
try! bindings._append(data, context: .default)
}

let statement = PSQLExecuteStatement(
name: preparedQuery.underlying.name,
binds: bindings,
rowDescription: preparedQuery.underlying.rowDescription
)

resultFuture = self.underlying.execute(statement, logger: logger).flatMap { rows in
guard let lookupTable = preparedQuery.lookupTable else {
return self.eventLoop.makeSucceededFuture(())
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/PostgresNIO/Data/PostgresDataType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public typealias PostgresFormatCode = PostgresFormat

/// The data type's raw object ID.
/// Use `select * from pg_type where oid = <idhere>;` to lookup more information.
public struct PostgresDataType: RawRepresentable, Equatable, CustomStringConvertible {
public struct PostgresDataType: RawRepresentable, Hashable, CustomStringConvertible {
/// `0`
public static let null = PostgresDataType(0)
/// `16`
Expand Down
10 changes: 5 additions & 5 deletions Sources/PostgresNIO/New/BufferedMessageEncoder.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import NIOCore

struct BufferedMessageEncoder<Encoder: MessageToByteEncoder> {
struct BufferedMessageEncoder {
private enum State {
case flushed
case writable
}

private var buffer: ByteBuffer
private var state: State = .writable
private var encoder: Encoder
private var encoder: PSQLFrontendMessageEncoder

init(buffer: ByteBuffer, encoder: Encoder) {
init(buffer: ByteBuffer, encoder: PSQLFrontendMessageEncoder) {
self.buffer = buffer
self.encoder = encoder
}

mutating func encode(_ message: Encoder.OutboundIn) throws {
mutating func encode(_ message: PSQLFrontendMessage) {
switch self.state {
case .flushed:
self.state = .writable
Expand All @@ -25,7 +25,7 @@ struct BufferedMessageEncoder<Encoder: MessageToByteEncoder> {
break
}

try self.encoder.encode(data: message, out: &self.buffer)
self.encoder.encode(data: message, out: &self.buffer)
}

mutating func flush() -> ByteBuffer? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ struct ConnectionStateMachine {
// Connection Actions

// --- general actions
case sendParseDescribeBindExecuteSync(query: String, binds: [PSQLEncodable])
case sendBindExecuteSync(statementName: String, binds: [PSQLEncodable])
case sendParseDescribeBindExecuteSync(PostgresQuery)
case sendBindExecuteSync(PSQLExecuteStatement)
case failQuery(ExtendedQueryContext, with: PSQLError, cleanupContext: CleanUpContext?)
case succeedQuery(ExtendedQueryContext, columns: [RowDescription.Column])
case succeedQueryNoRowsComming(ExtendedQueryContext, commandTag: String)
Expand Down Expand Up @@ -1050,11 +1050,11 @@ extension ConnectionStateMachine {
}

return false
case .decoding(_):
case .decoding:
return true
case .unexpectedBackendMessage(_):
case .unexpectedBackendMessage:
return true
case .unsupportedAuthMechanism(_):
case .unsupportedAuthMechanism:
return true
case .authMechanismRequiresPassword:
return true
Expand Down Expand Up @@ -1106,10 +1106,10 @@ extension ConnectionStateMachine {
extension ConnectionStateMachine {
mutating func modify(with action: ExtendedQueryStateMachine.Action) -> ConnectionStateMachine.ConnectionAction {
switch action {
case .sendParseDescribeBindExecuteSync(let query, let binds):
return .sendParseDescribeBindExecuteSync(query: query, binds: binds)
case .sendBindExecuteSync(let statementName, let binds):
return .sendBindExecuteSync(statementName: statementName, binds: binds)
case .sendParseDescribeBindExecuteSync(let query):
return .sendParseDescribeBindExecuteSync(query)
case .sendBindExecuteSync(let executeStatement):
return .sendBindExecuteSync(executeStatement)
case .failQuery(let requestContext, with: let error):
let cleanupContext = self.setErrorAndCreateCleanupContextIfNeeded(error)
return .failQuery(requestContext, with: error, cleanupContext: cleanupContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ struct ExtendedQueryStateMachine {
}

enum Action {
case sendParseDescribeBindExecuteSync(query: String, binds: [PSQLEncodable])
case sendBindExecuteSync(statementName: String, binds: [PSQLEncodable])
case sendParseDescribeBindExecuteSync(PostgresQuery)
case sendBindExecuteSync(PSQLExecuteStatement)

// --- general actions
case failQuery(ExtendedQueryContext, with: PSQLError)
Expand Down Expand Up @@ -56,18 +56,18 @@ struct ExtendedQueryStateMachine {
case .unnamed(let query):
return self.avoidingStateMachineCoW { state -> Action in
state = .parseDescribeBindExecuteSyncSent(queryContext)
return .sendParseDescribeBindExecuteSync(query: query, binds: queryContext.bind)
return .sendParseDescribeBindExecuteSync(query)
}

case .preparedStatement(let name, let rowDescription):
case .preparedStatement(let prepared):
return self.avoidingStateMachineCoW { state -> Action in
switch rowDescription {
switch prepared.rowDescription {
case .some(let rowDescription):
state = .rowDescriptionReceived(queryContext, rowDescription.columns)
case .none:
state = .noDataMessageReceived(queryContext)
}
return .sendBindExecuteSync(statementName: name, binds: queryContext.bind)
return .sendBindExecuteSync(prepared)
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/PostgresNIO/New/Data/Array+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ extension Array: PSQLEncodable where Element: PSQLArrayElement {
.binary
}

func encode(into buffer: inout ByteBuffer, context: PSQLEncodingContext) throws {
func encode<JSONEncoder: PostgresJSONEncoder>(
into buffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) throws {
// 0 if empty, 1 if not
buffer.writeInteger(self.isEmpty ? 0 : 1, as: UInt32.self)
// b
Expand Down
5 changes: 4 additions & 1 deletion Sources/PostgresNIO/New/Data/Bool+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ extension Bool: PSQLCodable {
}
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) {
byteBuffer.writeInteger(self ? 1 : 0, as: UInt8.self)
}
}
15 changes: 12 additions & 3 deletions Sources/PostgresNIO/New/Data/Bytes+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ extension PSQLEncodable where Self: Sequence, Self.Element == UInt8 {
.binary
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) {
byteBuffer.writeBytes(self)
}
}
Expand All @@ -25,7 +28,10 @@ extension ByteBuffer: PSQLCodable {
.binary
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) {
var copyOfSelf = self // dirty hack
byteBuffer.writeBuffer(&copyOfSelf)
}
Expand All @@ -49,7 +55,10 @@ extension Data: PSQLCodable {
.binary
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) {
byteBuffer.writeBytes(self)
}

Expand Down
7 changes: 5 additions & 2 deletions Sources/PostgresNIO/New/Data/Date+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ extension Date: PSQLCodable {
}
}

func encode(into buffer: inout ByteBuffer, context: PSQLEncodingContext) {
func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) {
let seconds = self.timeIntervalSince(Self._psqlDateStart) * Double(Self._microsecondsPerSecond)
buffer.writeInteger(Int64(seconds))
byteBuffer.writeInteger(Int64(seconds))
}

// MARK: Private Constants
Expand Down
5 changes: 4 additions & 1 deletion Sources/PostgresNIO/New/Data/Decimal+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ extension Decimal: PSQLCodable {
}
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) {
let numeric = PostgresNumeric(decimal: self)
byteBuffer.writeInteger(numeric.ndigits)
byteBuffer.writeInteger(numeric.weight)
Expand Down
10 changes: 8 additions & 2 deletions Sources/PostgresNIO/New/Data/Float+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ extension Float: PSQLCodable {
}
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) {
byteBuffer.psqlWriteFloat(self)
}
}
Expand Down Expand Up @@ -77,7 +80,10 @@ extension Double: PSQLCodable {
}
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) {
byteBuffer.psqlWriteDouble(self)
}
}
Expand Down
25 changes: 20 additions & 5 deletions Sources/PostgresNIO/New/Data/Int+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ extension UInt8: PSQLCodable {
}
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) {
byteBuffer.writeInteger(self, as: UInt8.self)
}
}
Expand Down Expand Up @@ -64,7 +67,10 @@ extension Int16: PSQLCodable {
}
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) {
byteBuffer.writeInteger(self, as: Int16.self)
}
}
Expand Down Expand Up @@ -105,7 +111,10 @@ extension Int32: PSQLCodable {
}
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) {
byteBuffer.writeInteger(self, as: Int32.self)
}
}
Expand Down Expand Up @@ -151,7 +160,10 @@ extension Int64: PSQLCodable {
}
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) {
byteBuffer.writeInteger(self, as: Int64.self)
}
}
Expand Down Expand Up @@ -204,7 +216,10 @@ extension Int: PSQLCodable {
}
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) {
byteBuffer.writeInteger(self, as: Int.self)
}
}
5 changes: 4 additions & 1 deletion Sources/PostgresNIO/New/Data/JSON+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ extension PSQLCodable where Self: Codable {
}
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) throws {
func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) throws {
byteBuffer.writeInteger(JSONBVersionByte)
try context.jsonEncoder.encode(self, into: &byteBuffer)
}
Expand Down
10 changes: 8 additions & 2 deletions Sources/PostgresNIO/New/Data/Optional+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@ extension Optional: PSQLEncodable where Wrapped: PSQLEncodable {
}
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) throws {
func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) {
preconditionFailure("Should never be hit, since `encodeRaw` is implemented.")
}

func encodeRaw(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) throws {
func encodeRaw<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) throws {
switch self {
case .none:
byteBuffer.writeInteger(-1, as: Int32.self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ extension PSQLCodable where Self: RawRepresentable, RawValue: PSQLCodable {
return selfValue
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) throws {
func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) throws {
try rawValue.encode(into: &byteBuffer, context: context)
}
}
5 changes: 4 additions & 1 deletion Sources/PostgresNIO/New/Data/String+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ extension String: PSQLCodable {
.binary
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) {
byteBuffer.writeString(self)
}

Expand Down
5 changes: 4 additions & 1 deletion Sources/PostgresNIO/New/Data/UUID+PSQLCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ extension UUID: PSQLCodable {
.binary
}

func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
func encode<JSONEncoder: PostgresJSONEncoder>(
into byteBuffer: inout ByteBuffer,
context: PSQLEncodingContext<JSONEncoder>
) {
let uuid = self.uuid
byteBuffer.writeBytes([
uuid.0, uuid.1, uuid.2, uuid.3,
Expand Down
Loading

0 comments on commit 4a2b050

Please sign in to comment.