Skip to content

Commit 9258433

Browse files
committed
Merge pull request mauricio#64 from dylex/missinghost
Improve URL parser to allow missing hostname/dbname
2 parents ac8382d + e3bcf1c commit 9258433

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

postgresql-async/src/main/scala/com/github/mauricio/async/db/postgresql/util/ParserURL.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ object ParserURL {
2121

2222
val DEFAULT_PORT = "5432"
2323

24-
private val pgurl1 = """(jdbc:postgresql)://([^:]*|\[.+\])(?::(\d+))?/([^?]+)(?:\?user=(.*)&password=(.*))?""".r
24+
private val pgurl1 = """(jdbc:postgresql):(?://([^/:]*|\[.+\])(?::(\d+))?)?(?:/([^/?]*))?(?:\?user=(.*)&password=(.*))?""".r
2525
private val pgurl2 = """(postgres|postgresql)://(.*):(.*)@(.*):(\d+)/(.*)""".r
2626

2727
def parse(connectionURL: String): Map[String, String] = {
2828
val properties: Map[String, String] = Map()
2929

3030
connectionURL match {
3131
case pgurl1(protocol, server, port, dbname, username, password) => {
32-
var result = properties + (PGHOST -> unwrapIpv6address(server)) + (PGDBNAME -> dbname)
32+
var result = properties
33+
if (server != null) result += (PGHOST -> unwrapIpv6address(server))
34+
if (dbname != null && dbname.nonEmpty) result += (PGDBNAME -> dbname)
3335
if(port != null) result += (PGPORT -> port)
3436
if(username != null) result = (result + (PGUSERNAME -> username) + (PGPASSWORD -> password))
3537
result
@@ -51,4 +53,4 @@ object ParserURL {
5153
} else server
5254
}
5355

54-
}
56+
}

postgresql-async/src/main/scala/com/github/mauricio/async/db/postgresql/util/URLParser.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ object URLParser {
3838
username = properties.get(Username).getOrElse(Default.username),
3939
password = properties.get(Password),
4040
database = properties.get(ParserURL.PGDBNAME),
41-
host = properties(ParserURL.PGHOST),
41+
host = properties.getOrElse(ParserURL.PGHOST, Default.host),
4242
port = port,
4343
charset = charset
4444
)

postgresql-async/src/test/scala/com/github/mauricio/async/db/postgresql/util/URLParserSpec.scala

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,54 @@ class URLParserSpec extends Specification {
124124
configuration.port === 9987
125125
}
126126

127+
"create a connection with a missing hostname" in {
128+
val connectionUri = "jdbc:postgresql:/my_database?user=john&password=doe"
129+
130+
val configuration = URLParser.parse(connectionUri)
131+
132+
configuration.username === "john"
133+
configuration.password === Some("doe")
134+
configuration.database === Some("my_database")
135+
configuration.host === "localhost"
136+
configuration.port === 5432
137+
}
138+
139+
"create a connection with a missing database name" in {
140+
val connectionUri = "jdbc:postgresql://[::1]:9987/?user=john&password=doe"
141+
142+
val configuration = URLParser.parse(connectionUri)
143+
144+
configuration.username === "john"
145+
configuration.password === Some("doe")
146+
configuration.database === None
147+
configuration.host === "::1"
148+
configuration.port === 9987
149+
}
150+
151+
"create a connection with all default fields" in {
152+
val connectionUri = "jdbc:postgresql:"
153+
154+
val configuration = URLParser.parse(connectionUri)
155+
156+
configuration.username === "postgres"
157+
configuration.password === None
158+
configuration.database === None
159+
configuration.host === "localhost"
160+
configuration.port === 5432
161+
}
162+
163+
"create a connection with an empty (invalid) url" in {
164+
val connectionUri = ""
165+
166+
val configuration = URLParser.parse(connectionUri)
167+
168+
configuration.username === "postgres"
169+
configuration.password === None
170+
configuration.database === None
171+
configuration.host === "localhost"
172+
configuration.port === 5432
173+
}
174+
127175
}
128176

129177
}

0 commit comments

Comments
 (0)