Skip to content

Commit 7a50dfd

Browse files
author
Shaun Hubbard
committed
added connection string parsing for PostgreSQLDatabaseConfig
1 parent 9f90100 commit 7a50dfd

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Sources/PostgreSQL/Database/PostgreSQLDatabaseConfig.swift

Lines changed: 22 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,25 @@ public struct PostgreSQLDatabaseConfig {
2930
self.database = database
3031
self.password = password
3132
}
33+
34+
public init(connectionString: String) throws {
35+
guard let url = URL(string: connectionString),
36+
let hostname = url.host,
37+
let port = url.port,
38+
let username = url.user,
39+
let database = URL(string: connectionString)?.path,
40+
database.count > 0
41+
else {
42+
throw PostgreSQLError(identifier: "Bad Connection String",
43+
reason: "Host could not be parsed",
44+
possibleCauses: ["Foundation URL is unable to parse the provided connection string"],
45+
suggestedFixes: ["Check the connection string being passed"],
46+
source: .capture())
47+
}
48+
self.hostname = hostname
49+
self.port = port
50+
self.username = username
51+
self.database = database
52+
self.password = url.password
53+
}
3254
}

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

66
class PostgreSQLConnectionTests: XCTestCase {

0 commit comments

Comments
 (0)