forked from j2gg0s/otsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsn.go
46 lines (40 loc) · 881 Bytes
/
dsn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package otsql
import "strings"
func parseDSN(dsn string) (string, string) {
addr, database := "localhost", ""
if i := strings.Index(dsn, "://"); i > -1 {
dsn = dsn[i+3:]
}
if i := strings.Index(dsn, "/"); i > -1 {
if i > 0 {
if j := strings.Index(dsn[:i], "@"); j > 0 && j+1 < i {
addr = dsn[j+1 : i]
}
}
if i < len(dsn) {
j := strings.Index(dsn[i+1:], "?")
if j > -1 {
j = i + 1 + j
} else {
j = len(dsn)
}
database = dsn[i+1 : j]
}
return addr, database
}
host, port := "", ""
for _, s := range strings.Split(dsn, " ") {
if strings.HasPrefix(s, "dbname=") {
database = s[len("dbname="):]
} else if strings.HasPrefix(s, "host=") {
host = s[len("host="):]
} else if strings.HasPrefix(s, "port=") {
port = s[len("port="):]
}
}
addr = host
if port != "" {
addr += ":" + port
}
return addr, database
}