-
Notifications
You must be signed in to change notification settings - Fork 4
/
sqlxdb.go
219 lines (202 loc) · 7.04 KB
/
sqlxdb.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// sqlx 创建 db 对象的函数封装
package goutils
import (
"sync"
"time"
"github.com/jmoiron/sqlx"
"github.com/spf13/viper"
)
// NewSqlxSQLite3 返回 sqlx sqlite3 连接实例
func NewSqlxSQLite3(conf DBConfig) (*sqlx.DB, error) {
dsn, err := conf.SQLite3DSN()
if err != nil {
return nil, err
}
sqlxSqlite3, err := sqlx.Open("sqlite3", dsn)
if err != nil {
return nil, err
}
sqlxSqlite3.SetMaxIdleConns(conf.MaxIdleConns)
sqlxSqlite3.SetMaxOpenConns(conf.MaxOpenConns)
sqlxSqlite3.SetConnMaxLifetime(time.Duration(conf.ConnMaxLifeMinutes) * time.Minute)
return sqlxSqlite3, nil
}
// NewSqlxMySQL 返回 sqlx mysql 连接实例
func NewSqlxMySQL(conf DBConfig) (*sqlx.DB, error) {
dsn, err := conf.MySQLDSN()
if err != nil {
return nil, err
}
sqlxMysql, err := sqlx.Open("mysql", dsn)
if err != nil {
return nil, err
}
sqlxMysql.SetMaxIdleConns(conf.MaxIdleConns)
sqlxMysql.SetMaxOpenConns(conf.MaxOpenConns)
sqlxMysql.SetConnMaxLifetime(time.Duration(conf.ConnMaxLifeMinutes) * time.Minute)
return sqlxMysql, nil
}
// NewSqlxPostgres 返回 sqlx postgresql 连接实例
func NewSqlxPostgres(conf DBConfig) (*sqlx.DB, error) {
dsn, err := conf.PostgresDSN()
if err != nil {
return nil, err
}
sqlxPostgres, err := sqlx.Open("postgres", dsn)
if err != nil {
return nil, err
}
sqlxPostgres.SetMaxIdleConns(conf.MaxIdleConns)
sqlxPostgres.SetMaxOpenConns(conf.MaxOpenConns)
sqlxPostgres.SetConnMaxLifetime(time.Duration(conf.ConnMaxLifeMinutes) * time.Minute)
return sqlxPostgres, nil
}
// NewSqlxMsSQL 返回 sqlx sqlserver 连接实例
func NewSqlxMsSQL(conf DBConfig) (*sqlx.DB, error) {
dsn, err := conf.SqlserverDSN()
if err != nil {
return nil, err
}
sqlxMssql, err := sqlx.Open("mssql", dsn)
if err != nil {
return nil, err
}
sqlxMssql.SetMaxIdleConns(conf.MaxIdleConns)
sqlxMssql.SetMaxOpenConns(conf.MaxOpenConns)
sqlxMssql.SetConnMaxLifetime(time.Duration(conf.ConnMaxLifeMinutes) * time.Minute)
return sqlxMssql, nil
}
// SqlxInstances 以 sync.Map 保存 sqlx db 相关信息
// key 为小写的数据库驱动名称, value 为实例名为 key , 具体的 db 对象为 value 的 sync.Map
// 形如: {"mysql": {"localhost": db}, "postgres": {"localhost": db}}
var SqlxInstances sync.Map
// SqlxMySQL 根据 viper 中配置的实例名称返回 sqlx 连接 mysql 的实例
func SqlxMySQL(which string) (*sqlx.DB, error) {
mysqls, loaded := SqlxInstances.LoadOrStore("mysql", new(sync.Map))
if loaded {
if db, loaded := mysqls.(*sync.Map).Load(which); loaded {
return db.(*sqlx.DB), nil
}
}
// mysql 不存在 或 存在时 which 不存在 则新建 mysql db 实例存放到 map 中
// 注意:这里依赖 viper ,必须在外部先对 viper 配置进行加载
prefix := "mysql." + which
conf := DBConfig{
Host: viper.GetString(prefix + ".host"),
Port: viper.GetInt(prefix + ".port"),
Username: viper.GetString(prefix + ".username"),
Password: viper.GetString(prefix + ".password"),
DBName: viper.GetString(prefix + ".dbname"),
LogMode: viper.GetBool(prefix + ".log_mode"),
MaxIdleConns: viper.GetInt(prefix + ".max_idle_conns"),
MaxOpenConns: viper.GetInt(prefix + ".max_open_conns"),
ConnMaxLifeMinutes: viper.GetInt(prefix + ".conn_max_life_minutes"),
ConnTimeout: viper.GetInt(prefix + ".conn_timeout"),
ReadTimeout: viper.GetInt(prefix + ".read_timeout"),
WriteTimeout: viper.GetInt(prefix + ".write_timeout"),
}
db, err := NewSqlxMySQL(conf)
if err != nil {
return nil, err
}
mysqls.(*sync.Map).Store(which, db)
SqlxInstances.Store("mysql", mysqls)
return db, nil
}
// SqlxSQLite3 根据 viper 中配置的实例名称返回 sqlite3 实例
func SqlxSQLite3(which string) (*sqlx.DB, error) {
sqlite3s, loaded := SqlxInstances.LoadOrStore("sqlite3", new(sync.Map))
if loaded {
if db, loaded := sqlite3s.(*sync.Map).Load(which); loaded {
return db.(*sqlx.DB), nil
}
}
prefix := "sqlite3." + which
conf := DBConfig{
DBName: viper.GetString(prefix + ".dbname"),
LogMode: viper.GetBool(prefix + ".log_mode"),
MaxIdleConns: viper.GetInt(prefix + ".max_idle_conns"),
MaxOpenConns: viper.GetInt(prefix + ".max_open_conns"),
ConnMaxLifeMinutes: viper.GetInt(prefix + ".conn_max_life_minutes"),
}
db, err := NewSqlxSQLite3(conf)
if err != nil {
return nil, err
}
sqlite3s.(*sync.Map).Store(which, db)
SqlxInstances.Store("sqlite3", sqlite3s)
return db, nil
}
// SqlxPostgres 根据 viper 中配置的实例名称返回 pg 实例
func SqlxPostgres(which string) (*sqlx.DB, error) {
pgs, loaded := SqlxInstances.LoadOrStore("postgres", new(sync.Map))
if loaded {
if db, loaded := pgs.(*sync.Map).Load(which); loaded {
return db.(*sqlx.DB), nil
}
}
prefix := "postgres." + which
conf := DBConfig{
Host: viper.GetString(prefix + ".host"),
Port: viper.GetInt(prefix + ".port"),
Username: viper.GetString(prefix + ".username"),
Password: viper.GetString(prefix + ".password"),
DBName: viper.GetString(prefix + ".dbname"),
LogMode: viper.GetBool(prefix + ".log_mode"),
MaxIdleConns: viper.GetInt(prefix + ".max_idle_conns"),
MaxOpenConns: viper.GetInt(prefix + ".max_open_conns"),
ConnMaxLifeMinutes: viper.GetInt(prefix + ".conn_max_life_minutes"),
DisableSSL: viper.GetBool(prefix + ".disable_ssl"),
}
db, err := NewSqlxPostgres(conf)
if err != nil {
return nil, err
}
pgs.(*sync.Map).Store(which, db)
SqlxInstances.Store("postgres", pgs)
return db, nil
}
// SqlxMsSQL 根据 viper 中配置的实例名称返回 sqlserver 实例
func SqlxMsSQL(which string) (*sqlx.DB, error) {
mssqls, loaded := SqlxInstances.LoadOrStore("mssql", new(sync.Map))
if loaded {
if db, loaded := mssqls.(*sync.Map).Load(which); loaded {
return db.(*sqlx.DB), nil
}
}
// mysql 不存在 或 存在时 which 不存在 则新建 mysql db 实例存放到 map 中
prefix := "mssql." + which
conf := DBConfig{
Host: viper.GetString(prefix + ".host"),
Port: viper.GetInt(prefix + ".port"),
Username: viper.GetString(prefix + ".username"),
Password: viper.GetString(prefix + ".password"),
DBName: viper.GetString(prefix + ".dbname"),
LogMode: viper.GetBool(prefix + ".log_mode"),
MaxIdleConns: viper.GetInt(prefix + ".max_idle_conns"),
MaxOpenConns: viper.GetInt(prefix + ".max_open_conns"),
ConnMaxLifeMinutes: viper.GetInt(prefix + ".conn_max_life_minutes"),
}
db, err := NewSqlxMsSQL(conf)
if err != nil {
return nil, err
}
mssqls.(*sync.Map).Store(which, db)
SqlxInstances.Store("mssql", mssqls)
return db, nil
}
// CloseSqlxInstances 关闭全部的 Sqlx 连接并重置 SqlxInstances
func CloseSqlxInstances() {
SqlxInstances.Range(func(ik, iv interface{}) bool {
if m, ok := iv.(*sync.Map); ok {
m.Range(func(k, v interface{}) bool {
if db, ok := v.(*sqlx.DB); ok {
db.Close()
}
return true
})
}
return true
})
SqlxInstances = sync.Map{}
}