Skip to content

Commit df25fcd

Browse files
authored
Merge branch 'master' into gm
2 parents bc2d03f + 07fe6bd commit df25fcd

File tree

5 files changed

+63
-31
lines changed

5 files changed

+63
-31
lines changed

Sources/PostgreSQL/Connection/PostgreSQLConnection+TCP.swift renamed to Sources/PostgreSQL/Connection/PostgreSQLConnection+Connect.swift

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,24 @@ import NIO
33
import NIOOpenSSL
44

55
extension PostgreSQLConnection {
6-
/// Connects to a Redis server using a TCP socket.
6+
@available(*, deprecated, message: "Use `.connect(to:...)` instead.")
77
public static func connect(
88
hostname: String = "localhost",
99
port: Int = 5432,
1010
transport: PostgreSQLTransportConfig = .cleartext,
1111
on worker: Worker,
1212
onError: @escaping (Error) -> ()
13-
) throws -> Future<PostgreSQLConnection> {
13+
) throws -> Future<PostgreSQLConnection> {
14+
return try connect(to: .tcp(hostname: hostname, port: port), transport: transport, on: worker, onError: onError)
15+
}
16+
17+
/// Connects to a PostgreSQL server using a TCP socket.
18+
public static func connect(
19+
to serverAddress: PostgreSQLDatabaseConfig.ServerAddress = .default,
20+
transport: PostgreSQLTransportConfig = .cleartext,
21+
on worker: Worker,
22+
onError: @escaping (Error) -> ()
23+
) throws -> Future<PostgreSQLConnection> {
1424
let handler = QueueHandler<PostgreSQLMessage, PostgreSQLMessage>(on: worker, onError: onError)
1525
let bootstrap = ClientBootstrap(group: worker.eventLoop)
1626
// Enable SO_REUSEADDR.
@@ -20,8 +30,16 @@ extension PostgreSQLConnection {
2030
channel.pipeline.add(handler: handler)
2131
}
2232
}
23-
24-
return bootstrap.connect(host: hostname, port: port).flatMap { channel in
33+
34+
let connectedBootstrap: Future<Channel>
35+
switch serverAddress {
36+
case let .tcp(hostname, port):
37+
connectedBootstrap = bootstrap.connect(host: hostname, port: port)
38+
case let .unixSocket(socketPath):
39+
connectedBootstrap = bootstrap.connect(unixDomainSocketPath: socketPath)
40+
}
41+
42+
return connectedBootstrap.flatMap { channel in
2543
let connection = PostgreSQLConnection(queue: handler, channel: channel)
2644
if case .tls(let tlsConfiguration) = transport.method {
2745
return connection.addSSLClientHandler(using: tlsConfiguration).transform(to: connection)

Sources/PostgreSQL/Connection/PostgreSQLConnection.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public final class PostgreSQLConnection: DatabaseConnection, BasicWorker {
1212
return channel.eventLoop
1313
}
1414

15-
/// Handles enqueued redis commands and responses.
15+
/// Handles enqueued PostgreSQL commands and responses.
1616
internal let queue: QueueHandler<PostgreSQLMessage, PostgreSQLMessage>
1717

1818
/// The channel
@@ -42,7 +42,7 @@ public final class PostgreSQLConnection: DatabaseConnection, BasicWorker {
4242
/// Handlers to be stored by channel name
4343
internal var notificationHandlers: [String: NotificationHandler] = [:]
4444

45-
/// Creates a new Redis client on the provided data source and sink.
45+
/// Creates a new PostgreSQL client on the provided data source and sink.
4646
init(queue: QueueHandler<PostgreSQLMessage, PostgreSQLMessage>, channel: Channel) {
4747
self.queue = queue
4848
self.channel = channel

Sources/PostgreSQL/Database/PostgreSQLDatabase.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public final class PostgreSQLDatabase: Database, LogSupporting {
1919
public func newConnection(on worker: Worker) -> Future<PostgreSQLConnection> {
2020
let config = self.config
2121
return Future.flatMap(on: worker) {
22-
return try PostgreSQLConnection.connect(hostname: config.hostname, port: config.port, transport: config.transportConfig, on: worker) { error in
22+
return try PostgreSQLConnection.connect(to: config.serverAddress, transport: config.transportConfig, on: worker) { error in
2323
print("[PostgreSQL] \(error)")
2424
}.flatMap(to: PostgreSQLConnection.self) { client in
2525
return client.authenticate(

Sources/PostgreSQL/Database/PostgreSQLDatabaseConfig.swift

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@ public struct PostgreSQLDatabaseConfig {
77
public static func `default`() -> PostgreSQLDatabaseConfig {
88
return .init(hostname: "localhost", port: 5432, username: "postgres")
99
}
10-
11-
/// Destination hostname.
12-
public let hostname: String
13-
14-
/// Destination port.
15-
public let port: Int
10+
11+
/// Specifies how to connect to a PostgreSQL server.
12+
public enum ServerAddress {
13+
/// Connect via TCP using the given hostname and port.
14+
case tcp(hostname: String, port: Int)
15+
/// Connect via a Unix domain socket file.
16+
case unixSocket(path: String)
17+
18+
public static let `default` = ServerAddress.tcp(hostname: "localhost", port: 5432)
19+
public static let socketDefault = ServerAddress.unixSocket(path: "/tmp/.s.PGSQL.5432")
20+
}
21+
22+
/// Which server to connect to.
23+
public let serverAddress: ServerAddress
1624

1725
/// Username to authenticate.
1826
public let username: String
@@ -29,14 +37,17 @@ public struct PostgreSQLDatabaseConfig {
2937
public let transportConfig: PostgreSQLTransportConfig
3038

3139
/// Creates a new `PostgreSQLDatabaseConfig`.
32-
public init(hostname: String, port: Int = 5432, username: String, database: String? = nil, password: String? = nil, transport: PostgreSQLTransportConfig = .cleartext) {
33-
self.hostname = hostname
34-
self.port = port
35-
self.username = username
36-
self.database = database
37-
self.password = password
38-
self.transportConfig = transport
39-
}
40+
public init(serverAddress: ServerAddress, username: String, database: String? = nil, password: String? = nil, transport: PostgreSQLTransportConfig = .cleartext) {
41+
self.serverAddress = serverAddress
42+
self.username = username
43+
self.database = database
44+
self.password = password
45+
self.transportConfig = transport
46+
}
47+
48+
public init(hostname: String, port: Int = 5432, username: String, database: String? = nil, password: String? = nil, transport: PostgreSQLTransportConfig = .cleartext) {
49+
self.init(serverAddress: .tcp(hostname: hostname, port: port), username: username, database: database, password: password, transport: transport)
50+
}
4051

4152
/// Creates a `PostgreSQLDatabaseConfig` frome a connection string.
4253
public init(url urlString: String, transport: PostgreSQLTransportConfig = .cleartext) throws {
@@ -54,8 +65,7 @@ public struct PostgreSQLDatabaseConfig {
5465
source: .capture()
5566
)
5667
}
57-
self.hostname = hostname
58-
self.port = port
68+
self.serverAddress = .tcp(hostname: hostname, port: port)
5969
self.username = username
6070
let database = url.path
6171
if database.hasPrefix("/") {

Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,12 @@ class PostgreSQLConnectionTests: XCTestCase {
433433
func testURLParsing() throws {
434434
let databaseURL = "postgres://username:password@hostname.com:5432/database"
435435
let config = try PostgreSQLDatabaseConfig(url: databaseURL)
436-
XCTAssertEqual(config.hostname, "hostname.com")
437-
XCTAssertEqual(config.port, 5432)
436+
if case let .tcp(hostname, port) = config.serverAddress {
437+
XCTAssertEqual(hostname, "hostname.com")
438+
XCTAssertEqual(port, 5432)
439+
} else {
440+
XCTFail("unexpected server address \(config.serverAddress)")
441+
}
438442
XCTAssertEqual(config.username, "username")
439443
XCTAssertEqual(config.password, "password")
440444
XCTAssertEqual(config.database, "database")
@@ -518,19 +522,19 @@ class PostgreSQLConnectionTests: XCTestCase {
518522
extension PostgreSQLConnection {
519523
/// Creates a test event loop and psql client over ssl.
520524
static func makeTest(transport: PostgreSQLTransportConfig) throws -> PostgreSQLConnection {
521-
#if Xcode
522-
return try _makeTest(hostname: "localhost", password: "vapor_password", port: transport.isTLS ? 5433 : 5432, transport: transport)
525+
#if os(macOS)
526+
return try _makeTest(serverAddress: .tcp(hostname: "localhost", port: transport.isTLS ? 5433 : 5432), password: "vapor_password", transport: transport)
523527
#else
524-
return try _makeTest(hostname: transport.isTLS ? "tls" : "cleartext", password: "vapor_password", transport: transport)
528+
return try _makeTest(serverAddress: .tcp(hostname: transport.isTLS ? "tls" : "cleartext", port: 5432), password: "vapor_password", transport: transport)
525529
#endif
526530
}
527531

528532
/// Creates a test connection.
529-
private static func _makeTest(hostname: String, password: String? = nil, port: Int = 5432, transport: PostgreSQLTransportConfig = .cleartext) throws -> PostgreSQLConnection {
533+
private static func _makeTest(serverAddress: PostgreSQLDatabaseConfig.ServerAddress, password: String? = nil, transport: PostgreSQLTransportConfig = .cleartext) throws -> PostgreSQLConnection {
530534
let group = MultiThreadedEventLoopGroup(numThreads: 1)
531-
let client = try PostgreSQLConnection.connect(hostname: hostname, port: port, transport: transport, on: group) { error in
535+
let client = try PostgreSQLConnection.connect(to: serverAddress, transport: transport, on: group) { error in
532536
XCTFail("\(error)")
533-
}.wait()
537+
}.wait()
534538
_ = try client.authenticate(username: "vapor_username", database: "vapor_database", password: password).wait()
535539
return client
536540
}

0 commit comments

Comments
 (0)