-
Notifications
You must be signed in to change notification settings - Fork 11
/
encoder.go
59 lines (53 loc) · 1.2 KB
/
encoder.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
49
50
51
52
53
54
55
56
57
58
59
package dbconfig
import (
"net/url"
"strings"
)
type Encoder func(*Config) string
var encoders = map[string]Encoder{
`mysql`: func(c *Config) string {
var host string
if strings.HasPrefix(c.Host, `unix:`) {
host = "unix(" + strings.TrimPrefix(c.Host, `unix:`) + ")"
} else {
host = "tcp(" + c.Host
if len(c.Port) > 0 {
host += ":" + c.Port
}
host += ")"
}
dsn := url.QueryEscape(c.User) + ":" + url.QueryEscape(c.Pass) + "@" + host + "/" + c.Name
if len(c.Charset) > 0 {
dsn += "?charset=" + c.Charset
}
return dsn
},
`mymysql`: func(c *Config) string {
//tcp:localhost:3306*gotest/root/root
var host string
if strings.HasPrefix(c.Host, `unix:`) {
host = c.Host
} else {
host = "tcp:" + c.Host
if len(c.Port) > 0 {
host += ":" + c.Port
}
}
dsn := host + "*" + c.Name + "/" + url.QueryEscape(c.User) + "/" + url.QueryEscape(c.Pass)
return dsn
},
}
func EncoderRegister(engine string, encoder Encoder) {
encoders[engine] = encoder
}
func EncoderGet(engine string) Encoder {
encoder, _ := encoders[engine]
return encoder
}
func EncoderUnregister(engine string) bool {
_, ok := encoders[engine]
if ok {
delete(encoders, engine)
}
return ok
}