-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
110 lines (89 loc) · 2.46 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package database
import (
"fmt"
"net/url"
"gorm.io/driver/clickhouse"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
)
const (
maxOpenConns = 16384
connMaxLifeTime = 5 // Second
)
// Config mysql config
type Config struct {
Dialect string `mapstructure:"dialect"`
// Database name
Database string `mapstructure:"database"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Encoding string `mapstructure:"encoding"`
gorm.Config
*ConnPool
}
// ConnPool config
type ConnPool struct {
// MaxIdleConns is the maximum number of connections in the idle connection pool
MaxIdleConns int `mapstructure:"max_idle_conns"`
// MaxOpenConns is the maximum number of open connections to the database.
MaxOpenConns int `mapstructure:"max_open_conns"`
// ConnMaxLifeTime is the maximum amount of time a connection may be reused.
ConnMaxLifeTime int `mapstructure:"conn_max_life_time"`
// SetConnMaxIdleTime sets the maximum amount of time a connection may be idle.
ConnMaxIdleTime int `mapstructure:"conn_max_idle_time"`
}
// Dialector return dialector with config
func (config *Config) Dialector() gorm.Dialector {
var dialector gorm.Dialector
if config.Encoding == "" {
config.Encoding = "utf8mb4"
}
switch config.Dialect {
case "mysql":
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&loc=%s&parseTime=true",
config.Username,
config.Password,
config.Host,
config.Port,
config.Database,
config.Encoding,
url.QueryEscape("Local"),
)
dialector = mysql.Open(dsn)
case "postgres":
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai",
config.Host,
config.Username,
config.Password,
config.Database,
config.Port,
)
dialector = postgres.Open(dsn)
case "sqlite":
dialector = sqlite.Open(config.Database)
case "sqlserver":
dsn := fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s",
config.Username,
config.Password,
config.Host,
config.Port,
config.Database,
)
dialector = sqlserver.Open(dsn)
case "clickhouse":
dsn := fmt.Sprintf("tcp://%s:%d?database=%s&username=%s&password=%s&read_timeout=10&write_timeout=20",
config.Host,
config.Port,
config.Database,
config.Username,
config.Password,
)
dialector = clickhouse.Open(dsn)
}
return dialector
}