forked from minus5/gofreetds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
credentials.go
48 lines (44 loc) · 1.24 KB
/
credentials.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
47
48
package freetds
import (
"strconv"
"strings"
)
type credentials struct {
user, pwd, host, database, mirrorHost, compatibility string
maxPoolSize, lockTimeout int
}
// NewCredentials fills credentials stusct from connection string
func NewCredentials(connStr string) *credentials {
parts := strings.Split(connStr, ";")
crd := &credentials{maxPoolSize: 100}
for _, part := range parts {
kv := strings.SplitN(part, "=", 2)
if len(kv) == 2 {
key := strings.ToLower(strings.Trim(kv[0], " "))
value := kv[1]
switch key {
case "server", "host":
crd.host = value
case "database":
crd.database = value
case "user id", "user_id", "user":
crd.user = value
case "password", "pwd":
crd.pwd = value
case "failover partner", "failover_partner", "mirror", "mirror_host", "mirror host":
crd.mirrorHost = value
case "max pool size", "max_pool_size":
if i, err := strconv.Atoi(value); err == nil {
crd.maxPoolSize = i
}
case "compatibility_mode", "compatibility mode", "compatibility":
crd.compatibility = strings.ToLower(value)
case "lock timeout", "lock_timeout":
if i, err := strconv.Atoi(value); err == nil {
crd.lockTimeout = i
}
}
}
}
return crd
}