Skip to content

Commit d73f1a7

Browse files
committed
PostgresDatabase protocol cleanup
1 parent 1d39d75 commit d73f1a7

File tree

5 files changed

+86
-75
lines changed

5 files changed

+86
-75
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ let package = Package(
1010
dependencies: [
1111
.package(url: "https://github.com/vapor/nio-postgres.git", .branch("master")),
1212
.package(url: "https://github.com/vapor/sql.git", .branch("master")),
13-
.package(url: "https://github.com/vapor/database-kit.git", .branch("master")),
13+
.package(url: "https://github.com/vapor/nio-kit.git", .branch("master")),
1414
],
1515
targets: [
16-
.target(name: "PostgresKit", dependencies: ["DatabaseKit", "NIOPostgres", "SQLKit"]),
16+
.target(name: "PostgresKit", dependencies: ["NIOKit", "NIOPostgres", "SQLKit"]),
1717
.testTarget(name: "PostgresKitTests", dependencies: ["PostgresKit", "SQLKitBenchmark"]),
1818
.target(name: "PostgresKitPerformance", dependencies: ["PostgresKit"]),
1919
]

Sources/PostgresKit/Exports.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
@_exported import NIOPostgres
2-
@_exported import DatabaseKit
2+
@_exported import NIOKit
3+
@_exported import SQLKit

Sources/PostgresKit/PostgresDatabase.swift

Lines changed: 76 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,69 @@
1-
import DatabaseKit
2-
import Foundation
3-
import NIO
4-
import NIOPostgres
5-
import NIOOpenSSL
6-
import SQLKit
1+
@_exported import struct Foundation.URL
72

8-
public final class PostgresDatabase: Database {
9-
public struct Config {
10-
public let address: () throws -> SocketAddress
11-
public let username: String
12-
public let password: String
13-
public let database: String?
14-
public let tlsConfig: TLSConfiguration?
15-
16-
public init?(url: URL) {
17-
guard url.scheme == "postgres" else {
18-
return nil
19-
}
20-
guard let username = url.user else {
21-
return nil
22-
}
23-
guard let password = url.password else {
24-
return nil
25-
}
26-
guard let hostname = url.host else {
27-
return nil
28-
}
29-
guard let port = url.port else {
30-
return nil
31-
}
32-
33-
let tlsConfig: TLSConfiguration?
34-
if url.query == "ssl=true" {
35-
tlsConfig = TLSConfiguration.forClient(certificateVerification: .none)
36-
} else {
37-
tlsConfig = nil
38-
}
39-
40-
self.init(
41-
hostname: hostname,
42-
port: port,
43-
username: username,
44-
password: password,
45-
database: url.databaseName,
46-
tlsConfig: tlsConfig
47-
)
3+
public struct PostgresConfig {
4+
public let address: () throws -> SocketAddress
5+
public let username: String
6+
public let password: String
7+
public let database: String?
8+
public let tlsConfig: TLSConfiguration?
9+
10+
public init?(url: URL) {
11+
guard url.scheme == "postgres" else {
12+
return nil
13+
}
14+
guard let username = url.user else {
15+
return nil
16+
}
17+
guard let password = url.password else {
18+
return nil
19+
}
20+
guard let hostname = url.host else {
21+
return nil
22+
}
23+
guard let port = url.port else {
24+
return nil
4825
}
4926

50-
public init(
51-
hostname: String,
52-
port: Int = 5432,
53-
username: String,
54-
password: String,
55-
database: String? = nil,
56-
tlsConfig: TLSConfiguration? = nil
57-
) {
58-
self.address = {
59-
return try SocketAddress.makeAddressResolvingHost(hostname, port: port)
60-
}
61-
self.username = username
62-
self.database = database
63-
self.password = password
64-
self.tlsConfig = tlsConfig
27+
let tlsConfig: TLSConfiguration?
28+
if url.query == "ssl=true" {
29+
tlsConfig = TLSConfiguration.forClient(certificateVerification: .none)
30+
} else {
31+
tlsConfig = nil
6532
}
33+
34+
self.init(
35+
hostname: hostname,
36+
port: port,
37+
username: username,
38+
password: password,
39+
database: url.path.split(separator: "/").last.flatMap(String.init),
40+
tlsConfig: tlsConfig
41+
)
6642
}
6743

44+
public init(
45+
hostname: String,
46+
port: Int = 5432,
47+
username: String,
48+
password: String,
49+
database: String? = nil,
50+
tlsConfig: TLSConfiguration? = nil
51+
) {
52+
self.address = {
53+
return try SocketAddress.makeAddressResolvingHost(hostname, port: port)
54+
}
55+
self.username = username
56+
self.database = database
57+
self.password = password
58+
self.tlsConfig = tlsConfig
59+
}
60+
}
61+
62+
public struct PostgresConnectionSource: ConnectionPoolSource {
6863
public var eventLoop: EventLoop
69-
public let config: Config
64+
public let config: PostgresConfig
7065

71-
public init(config: Config, on eventLoop: EventLoop) {
66+
public init(config: PostgresConfig, on eventLoop: EventLoop) {
7267
self.config = config
7368
self.eventLoop = eventLoop
7469
}
@@ -102,7 +97,7 @@ public final class PostgresDatabase: Database {
10297
}
10398
}
10499

105-
extension PostgresConnection: DatabaseConnection {
100+
extension PostgresConnection: ConnectionPoolItem {
106101
public var isClosed: Bool {
107102
#warning("implement is closed")
108103
return false
@@ -164,7 +159,8 @@ struct PostgresDialect: SQLDialect {
164159
}
165160
}
166161

167-
extension PostgresConnection: SQLDatabase {
162+
extension PostgresConnection: SQLDatabase { }
163+
extension SQLDatabase where Self: PostgresDatabase {
168164
public func sqlQuery(_ query: SQLExpression, _ onRow: @escaping (SQLRow) throws -> ()) -> EventLoopFuture<Void> {
169165
var serializer = SQLSerializer(dialect: PostgresDialect())
170166
query.serialize(to: &serializer)
@@ -176,10 +172,21 @@ extension PostgresConnection: SQLDatabase {
176172
}
177173
}
178174

179-
extension PostgresDatabase: SQLDatabase {
175+
#warning("TODO: move to NIOPostgres?")
176+
extension ConnectionPool: PostgresDatabase where Source.Connection: PostgresDatabase {
177+
public var eventLoop: EventLoop {
178+
return self.source.eventLoop
179+
}
180+
181+
public func send(_ request: PostgresRequestHandler) -> EventLoopFuture<Void> {
182+
return self.withConnection { $0.send(request) }
183+
}
184+
}
185+
186+
#warning("TODO: move to SQLKit?")
187+
extension ConnectionPool: SQLDatabase where Source.Connection: SQLDatabase {
180188
public func sqlQuery(_ query: SQLExpression, _ onRow: @escaping (SQLRow) throws -> ()) -> EventLoopFuture<Void> {
181-
return self.makeConnection().flatMap { conn in
182-
return conn.sqlQuery(query, onRow)
183-
}
189+
return self.withConnection { $0.sqlQuery(query, onRow) }
184190
}
185191
}
192+

Sources/PostgresKitPerformance/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import NIO
22
import PostgresKit
33

44
let eventLoop = MultiThreadedEventLoopGroup(numberOfThreads: 1).next()
5-
let db = PostgresDatabase(config: .init(hostname: "localhost", username: "vapor_username", password: "vapor_password", database: "vapor_database"), on: eventLoop)
5+
let db = PostgresConnectionSource(config: .init(hostname: "localhost", username: "vapor_username", password: "vapor_password", database: "vapor_database"), on: eventLoop)
66
let conn = try db.makeConnection().wait()
77

88
_ = try conn.simpleQuery("SELECT * FROM generate_series(1, 100000) num").wait()

Tests/PostgresKitTests/PostgresKitTests.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ class PostgresKitTests: XCTestCase {
2525
}
2626

2727
func testPerformance() throws {
28-
let db = PostgresDatabase(config: .init(hostname: "localhost", username: "vapor_username", password: "vapor_password", database: "vapor_database"), on: self.eventLoop)
29-
let pool = db.makeConnectionPool(config: .init(maxConnections: 12))
28+
let db = PostgresConnectionSource(
29+
config: .init(hostname: "localhost", username: "vapor_username", password: "vapor_password", database: "vapor_database"),
30+
on: self.eventLoop
31+
)
32+
let pool = ConnectionPool(config: .init(maxConnections: 12), source: db)
3033
self.measure {
3134
for i in 1...100 {
3235
_ = try! pool.withConnection { conn in

0 commit comments

Comments
 (0)