-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
53 lines (49 loc) · 1.17 KB
/
config.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
package f
import (
gmap "github.com/og/x/map"
"strings"
)
// 通过 DataSourceName 结构体创建 sq.Open(driverName, dataSourceName string) 中的 dataSourceName 字符串
// 因为自己写 root:password@(localhost:3306)/test_gofree 这样的字符串太烦了还容易错
// DataSourceName.User 是个 map[string]stirng 结构
// 用于生成 dataSourceName 中的 ?charset=utf8&parseTime=True&loc=Local 部分
type DataSourceName struct {
DriverName string
User string
Password string
Host string
Port string
DB string
Query map[string]string
}
func (config DataSourceName) GetString() (dataSourceName string) {
configList := []string{
config.User,
":",
config.Password,
"@",
"(",
config.Host,
":",
config.Port,
")",
"/",
config.DB,
"?",
}
if len(config.Query) == 0 {
config.Query = map[string]string{
"charset": "utf8",
"parseTime": "True",
"loc": "Local",
}
}
configList = append(configList)
var queryList []string
for _, key := range gmap.StringStringKeys(config.Query) {
value := config.Query[key]
queryList = append(queryList, key +"="+value)
}
dataSourceName = strings.Join(configList,"") + strings.Join(queryList, "&")
return
}