Skip to content
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
0a59105
Changed dependencies to Skelpo forks
calebkleveter Nov 21, 2019
28b0663
Conformed PostgresDataDecoder to PostrgesDecoder and PostgresDataEnco…
calebkleveter Dec 4, 2019
fa56c71
Added JSON encoder/decoder configurations to PostgresConfiguration in…
calebkleveter Dec 4, 2019
e6d4810
Added .encoder and .decoder properties to _ConnectionPoolPostgresData…
calebkleveter Dec 4, 2019
c7dce4b
Pass configuration.encoder and .decoder into PostgresConnection.conne…
calebkleveter Dec 4, 2019
0d88de2
Use PostgresRow.decoder to decode data in PostgresRow.decode(column:a…
calebkleveter Dec 4, 2019
61e59f6
Added .encoder and .decoder properties to _PostgresSQLDatabase
calebkleveter Dec 4, 2019
3f7c581
Use .encoder to encode query binds in _PostgresSQLDatabase.execute(sq…
calebkleveter Dec 4, 2019
a26d07c
Use .source instead of .sourceInfo propety in _ConnectionPoolPostgres…
calebkleveter Dec 4, 2019
887271a
Merged 'upstream/master' into master
calebkleveter Dec 9, 2019
41cd1c7
Use skeplo/master for PostgresNIO until forks are merged
calebkleveter Dec 9, 2019
ed2c6e8
Replaced PostgresRow:SQLRow conformance with .sqlRow(using:) method
calebkleveter Dec 9, 2019
6436a02
Replaced uses of Postgres<Coder> protocols with PostgresData<Coder> t…
calebkleveter Dec 9, 2019
10ba3a2
Use original PostgresNIO library instead of Skelpo fork
calebkleveter Dec 9, 2019
0884bbd
Fixed calls to PostgresConnection.sql(encoder:decoder:) in PostgresKi…
calebkleveter Dec 9, 2019
c2bf44a
Deleted Xcode workspace
calebkleveter Dec 9, 2019
0fa2d80
Keep PostgresRow.sqlRow(using:) method internal
calebkleveter Dec 10, 2019
505570e
Removed PostgresConnection.sql() extension method
calebkleveter Dec 10, 2019
edb4965
Pass in PostgresData coders instead of JSON coders to PostgresConfigu…
calebkleveter Dec 10, 2019
5b83ae2
Removed leftover PostgresData coder declarations in PostgresConnectio…
calebkleveter Dec 10, 2019
f69cdf8
Revert "Keep PostgresRow.sqlRow(using:) method internal"
calebkleveter Dec 10, 2019
55b4524
Use registered JSONEncoder to handle DoJSON case in PostgresDataEncoder
calebkleveter Dec 10, 2019
f7d7bb1
Merged 'upstream/master' into master
calebkleveter Dec 11, 2019
83acdea
Added default value to PostgresDataEncoder.init(json:) and PostgresDa…
calebkleveter Dec 11, 2019
b4b7380
Renamed PostgresRow.sqlRow(using:) method to .sql(using:)
calebkleveter Dec 11, 2019
388dec1
Replace uses of PostgresRow.sqlRow with .sql
calebkleveter Dec 11, 2019
6eb20b2
Revert all changes made for custom JSON
calebkleveter Dec 11, 2019
17381eb
Add default value to PostgresRow.sql method
calebkleveter Dec 11, 2019
5f96b82
Fix PostgresDataDecoder paramater name for .sql call
calebkleveter Dec 11, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Sources/PostgresKit/ConnectionPool+Postgres.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ private struct _ConnectionPoolPostgresDatabase {
}

extension _ConnectionPoolPostgresDatabase: PostgresDatabase {
var eventLoop: EventLoop {
self.pool.eventLoop
}
var eventLoop: EventLoop { self.pool.eventLoop }

func send(_ request: PostgresRequest, logger: Logger) -> EventLoopFuture<Void> {
self.pool.withConnection(logger: logger) {
Expand Down
15 changes: 11 additions & 4 deletions Sources/PostgresKit/PostgresClient+SQL.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import PostgresNIO
import Foundation
import SQLKit

extension PostgresDatabase {
public func sql() -> SQLDatabase {
_PostgresSQLDatabase(database: self)
public func sql(
encoder: PostgresDataEncoder = PostgresDataEncoder(),
decoder: PostgresDataDecoder = PostgresDataDecoder()
) -> SQLDatabase {
_PostgresSQLDatabase(database: self, encoder: encoder, decoder: decoder)
}
}

private struct _PostgresSQLDatabase {
let database: PostgresDatabase
let encoder: PostgresDataEncoder
let decoder: PostgresDataDecoder
}

extension _PostgresSQLDatabase: SQLDatabase {
Expand All @@ -27,9 +34,9 @@ extension _PostgresSQLDatabase: SQLDatabase {
let (sql, binds) = self.serialize(query)
do {
return try self.database.query(sql, binds.map { encodable in
return try PostgresDataEncoder().encode(encodable)
return try self.encoder.encode(encodable)
}) { row in
onRow(row)
onRow(row.sql(using: self.decoder))
}
} catch {
return self.eventLoop.makeFailedFuture(error)
Expand Down
13 changes: 10 additions & 3 deletions Sources/PostgresKit/PostgresConfiguration.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
@_exported import struct Foundation.URL
@_exported import Foundation

public struct PostgresConfiguration {
public let address: () throws -> SocketAddress
public let username: String
public let password: String
public let database: String?
public let tlsConfiguration: TLSConfiguration?


public let encoder: PostgresDataEncoder
public let decoder: PostgresDataDecoder

internal var _hostname: String?

public init?(url: URL) {
Expand Down Expand Up @@ -49,7 +52,9 @@ public struct PostgresConfiguration {
username: String,
password: String,
database: String? = nil,
tlsConfiguration: TLSConfiguration? = nil
tlsConfiguration: TLSConfiguration? = nil,
encoder: PostgresDataEncoder = PostgresDataEncoder(),
decoder: PostgresDataDecoder = PostgresDataDecoder()
) {
self.address = {
return try SocketAddress.makeAddressResolvingHost(hostname, port: port)
Expand All @@ -59,5 +64,7 @@ public struct PostgresConfiguration {
self.password = password
self.tlsConfiguration = tlsConfiguration
self._hostname = hostname
self.encoder = encoder
self.decoder = decoder
}
}
17 changes: 12 additions & 5 deletions Sources/PostgresKit/PostgresDataDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ struct DecoderUnwrapper: Decodable {
}
}

public struct PostgresDataDecoder {
public init() {}
public final class PostgresDataDecoder {
public let jsonDecoder: JSONDecoder

public init(json: JSONDecoder = JSONDecoder()) {
self.jsonDecoder = json
}

public func decode<T>(_ type: T.Type, from data: PostgresData) throws -> T
where T: Decodable
{
if let convertible = T.self as? PostgresDataConvertible.Type {
return convertible.init(postgresData: data)! as! T
} else {
return try T.init(from: _Decoder(data: data))
return try T.init(from: _Decoder(data: data, json: self.jsonDecoder))
}
}

Expand All @@ -30,8 +34,11 @@ public struct PostgresDataDecoder {
}

let data: PostgresData
init(data: PostgresData) {
let json: JSONDecoder

init(data: PostgresData, json: JSONDecoder) {
self.data = data
self.json = json
}

func unkeyedContainer() throws -> UnkeyedDecodingContainer {
Expand All @@ -51,7 +58,7 @@ public struct PostgresDataDecoder {
debugDescription: "Cannot decode JSON from nil value"
))
}
let unwrapper = try JSONDecoder()
let unwrapper = try self.json
.decode(DecoderUnwrapper.self, from: Data(buffer.readableBytesView))
return unwrapper.decoder
}
Expand Down
11 changes: 8 additions & 3 deletions Sources/PostgresKit/PostgresDataEncoder.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import Foundation

public struct PostgresDataEncoder {
public init() { }
public final class PostgresDataEncoder {
public let jsonEncoder: JSONEncoder

public init(json: JSONEncoder = JSONEncoder()) {
self.jsonEncoder = json
}

public func encode(_ value: Encodable) throws -> PostgresData {
if let custom = value as? PostgresDataConvertible {
Expand All @@ -12,7 +16,8 @@ public struct PostgresDataEncoder {
try value.encode(to: encoder)
return encoder.data
} catch is DoJSON {
return try PostgresData(jsonb: Wrapper(value))
let data = try self.jsonEncoder.encode(Wrapper(value))
return PostgresData(jsonb: data)
}
}
}
Expand Down
17 changes: 13 additions & 4 deletions Sources/PostgresKit/PostgresRow+SQL.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
extension PostgresRow: SQLRow {
public func decode<D>(column: String, as type: D.Type) throws -> D where D : Decodable {
guard let data = self.column(column) else {
extension PostgresRow {
public func sql(using decoder: PostgresDataDecoder) -> SQLRow {
return _PostgreSQLRow(row: self, decoder: decoder)
}
}

private struct _PostgreSQLRow: SQLRow {
let row: PostgresRow
let decoder: PostgresDataDecoder

func decode<D>(column: String, as type: D.Type) throws -> D where D : Decodable {
guard let data = self.row.column(column) else {
fatalError()
}
return try PostgresDataDecoder().decode(D.self, from: data)
return try self.decoder.decode(D.self, from: data)
}
}