Closed loganwright closed 7 years ago
My best guess would be add an optional parameter so that it won't break existing APIs
and make it a dictionary so that it won't add too much parameters.
I don't know how to do a pull request that modifies multiple repository, So I'll just dump every thing I've done here hope you could take your time and read it. PostgreSQL.diff
diff --git a/Sources/PostgreSQL/Connection.swift b/Sources/PostgreSQL/Connection.swift
index 0d1f3a7..d95defe 100644
--- a/Sources/PostgreSQL/Connection.swift
+++ b/Sources/PostgreSQL/Connection.swift
@@ -16,12 +16,16 @@ public final class Connection {
return false
}
- public init(host: String = "localhost", port: String = "5432", dbname: String, user: String, password: String) throws {
- self.connection = PQconnectdb("host='\(host)' port='\(port)' dbname='\(dbname)' user='\(user)' password='\(password)'")
- if !self.connected {
- throw DatabaseError.cannotEstablishConnection(error)
- }
- }
+ public init(host: String = "localhost", port: String = "5432", dbname: String, user: String, password:String, otherParams:[String:String] = [:]) throws {
+ var initString = "host='\(host)' port='\(port)' dbname='\(dbname)' user='\(user)' password='\(password)'"
+ for (key,value) in otherParams {
+ initString += " \(key)='\(value)'"
+ }
+ self.connection = PQconnectdb(initString)
+ if !self.connected {
+ throw DatabaseError.cannotEstablishConnection(error)
+ }
+ }
public func reset() throws {
guard self.connected else {
diff --git a/Sources/PostgreSQL/Database.swift b/Sources/PostgreSQL/Database.swift
index a1560dd..2189caf 100644
--- a/Sources/PostgreSQL/Database.swift
+++ b/Sources/PostgreSQL/Database.swift
@@ -20,13 +20,15 @@ public class Database {
private let dbname: String
private let user: String
private let password: String
+ private let otherParams: [String:String]
- public init(host: String = "localhost", port: String = "5432", dbname: String, user: String, password: String) {
+ public init(host: String = "localhost", port: String = "5432", dbname: String, user: String, password: String,otherParams: [String:String] = [:]) {
self.host = host
self.port = port
self.dbname = dbname
self.user = user
self.password = password
+ self.otherParams = otherParams
}
@discardableResult
@@ -110,6 +112,6 @@ public class Database {
}
public func makeConnection() throws -> Connection {
- return try Connection(host: self.host, port: self.port, dbname: self.dbname, user: self.user, password: self.password)
+ return try Connection(host: self.host, port: self.port, dbname: self.dbname, user: self.user, password: self.password,otherParams: self.otherParams)
}
}
FluentPostgreSQL.diff
diff --git a/Sources/FluentPostgreSQL/PostgreSQLDriver.swift b/Sources/FluentPostgreSQL/PostgreSQLDriver.swift
index d86b420..39695e5 100644
--- a/Sources/FluentPostgreSQL/PostgreSQLDriver.swift
+++ b/Sources/FluentPostgreSQL/PostgreSQLDriver.swift
@@ -19,7 +19,8 @@ public class PostgreSQLDriver: Fluent.Driver {
port: Int = 5432,
dbname: String,
user: String,
- password: String
+ password: String,
+ otherParams:[String:String] = [:]
) {
self.database = PostgreSQL.Database(
@@ -27,7 +28,8 @@ public class PostgreSQLDriver: Fluent.Driver {
port: "\(port)",
dbname: dbname,
user: user,
- password: password
+ password: password,
+ otherParams:otherParams
)
}
VaporPostgreSQL.diff
diff --git a/Sources/VaporPostgreSQL/Provider.swift b/Sources/VaporPostgreSQL/Provider.swift
index 9e5e657..a42f8da 100644
--- a/Sources/VaporPostgreSQL/Provider.swift
+++ b/Sources/VaporPostgreSQL/Provider.swift
@@ -2,6 +2,7 @@ import URI
import Vapor
import Fluent
import FluentPostgreSQL
+import Foundation
public typealias PostgreSQLDriver = FluentPostgreSQL.PostgreSQLDriver
@@ -53,13 +54,21 @@ public final class Provider: Vapor.Provider {
let host = postgresql["host"]?.string
let port = postgresql["port"]?.int
-
+ var otherParams:[String:String] = [:]
+ for (key,value) in postgresql{
+ if key == "password" || key == "user" || key == "database" || key == "host" || key == "port" {
+ //do nothing
+ }else{
+ otherParams[key] = value.string
+ }
+ }
try self.init(
host: host ?? "localhost",
port: port ?? 5432,
dbname: dbname,
user: user,
- password: password
+ password: password,
+ otherParams:otherParams
)
}
}
@@ -85,13 +94,22 @@ public final class Provider: Vapor.Provider {
.split(separator: "/")
.map { String($0) }
.joined(separator: "")
+ var otherParams:[String:String] = [:]
+ if let query = uri.query {
+ let blocks = query.components(separatedBy: "&")
+ for block in blocks{
+ let part = block.components(separatedBy: "=")
+ otherParams[part[0]]=part[1]
+ }
+ }
try self.init(
host: uri.host,
port: port ?? 5432,
dbname: dbname,
user: user,
- password: password
+ password: password,
+ otherParams:otherParams
)
}
@@ -108,14 +126,16 @@ public final class Provider: Vapor.Provider {
port: Int = 5432,
dbname: String,
user: String,
- password: String
+ password: String,
+ otherParams:[String:String]
) throws {
let driver = PostgreSQLDriver(
host: host,
port: port,
dbname: dbname,
user: user,
- password: password
+ password: password,
+ otherParams: otherParams
)
self.driver = driver
https://github.com/vapor/postgresql/blob/master/Sources/PostgreSQL/Connection.swift#L20
On some platforms, SSL is required, I think we need to make it a configurable option in the line above.
This will also need to apply to provider.
http://stackoverflow.com/questions/11832361/how-to-setup-spring-heroku-postgres-ssl-datasource/24716055#24716055
Might be nice to have a more flexible query config or something that can support many little options w/o explicit initializer arguments for each