-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathPostgresConnectionSource.swift
41 lines (35 loc) · 1.43 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
import NIOSSL
import Atomics
import AsyncKit
import Logging
import PostgresNIO
import NIOCore
public struct PostgresConnectionSource: ConnectionPoolSource {
public let sqlConfiguration: SQLPostgresConfiguration
private static let idGenerator = ManagedAtomic<Int>(0)
public init(sqlConfiguration: SQLPostgresConfiguration) {
self.sqlConfiguration = sqlConfiguration
}
public func makeConnection(
logger: Logger,
on eventLoop: any EventLoop
) -> EventLoopFuture<PostgresConnection> {
let connectionFuture = PostgresConnection.connect(
on: eventLoop,
configuration: self.sqlConfiguration.coreConfiguration,
id: Self.idGenerator.wrappingIncrementThenLoad(ordering: .relaxed),
logger: logger
)
if let searchPath = self.sqlConfiguration.searchPath {
return connectionFuture.flatMap { conn in
let string = searchPath.map { #""\#($0)""# }.joined(separator: ", ")
return conn.simpleQuery("SET search_path = \(string)").map { _ in conn }
}
.flatMapErrorThrowing { try PostgresDataTranslation.applyPSQLErrorBandaidIfNeeded(for: $0) }
} else {
return connectionFuture
.flatMapErrorThrowing { try PostgresDataTranslation.applyPSQLErrorBandaidIfNeeded(for: $0) }
}
}
}
extension PostgresConnection: ConnectionPoolItem { }