Skip to content

Commit 93444ff

Browse files
authored
Merge branch 'nio' into query-pipeline-bug24
2 parents 9c9bb11 + c2b4be0 commit 93444ff

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Sources/PostgreSQL/Database/PostgreSQLDatabaseConfig.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Foundation
12
/// Config options for a `PostgreSQLConnection`
23
public struct PostgreSQLDatabaseConfig {
34
/// Creates a `PostgreSQLDatabaseConfig` with default settings.
@@ -29,4 +30,26 @@ public struct PostgreSQLDatabaseConfig {
2930
self.database = database
3031
self.password = password
3132
}
33+
34+
/// Creates a `PostgreSQLDatabaseConfig` frome a connection string.
35+
public init(url: String) throws {
36+
guard let urL = URL(string: url),
37+
let hostname = urL.host,
38+
let port = urL.port,
39+
let username = urL.user,
40+
let database = URL(string: url)?.path,
41+
database.count > 0
42+
else {
43+
throw PostgreSQLError(identifier: "Bad Connection String",
44+
reason: "Host could not be parsed",
45+
possibleCauses: ["Foundation URL is unable to parse the provided connection string"],
46+
suggestedFixes: ["Check the connection string being passed"],
47+
source: .capture())
48+
}
49+
self.hostname = hostname
50+
self.port = port
51+
self.username = username
52+
self.database = database
53+
self.password = urL.password
54+
}
3255
}

Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import Async
21
import Foundation
32
import XCTest
3+
import NIO
44
import PostgreSQL
55
import Core
66

0 commit comments

Comments
 (0)