forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresConnectionSource.swift
46 lines (43 loc) · 1.62 KB
/
PostgresConnectionSource.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
public struct PostgresConnectionSource: ConnectionPoolSource {
public let configuration: PostgresConfiguration
public init(configuration: PostgresConfiguration) {
self.configuration = configuration
}
public func makeConnection(
logger: Logger,
on eventLoop: EventLoop
) -> EventLoopFuture<PostgresConnection> {
let address: SocketAddress
do {
address = try self.configuration.address()
} catch {
return eventLoop.makeFailedFuture(error)
}
return PostgresConnection.connect(
to: address,
tlsConfiguration: self.configuration.tlsConfiguration,
serverHostname: self.configuration._hostname,
logger: logger,
on: eventLoop
).flatMap { conn in
return conn.authenticate(
username: self.configuration.username,
database: self.configuration.database,
password: self.configuration.password,
logger: logger
).flatMap {
if let searchPath = self.configuration.searchPath {
let string = searchPath.map { "\"" + $0 + "\"" }.joined(separator: ", ")
return conn.simpleQuery("SET search_path = \(string)")
.map { _ in }
} else {
return eventLoop.makeSucceededFuture(())
}
}.flatMapErrorThrowing { error in
_ = conn.close()
throw error
}.map { conn }
}
}
}
extension PostgresConnection: ConnectionPoolItem { }