-
Notifications
You must be signed in to change notification settings - Fork 39
/
connection.go
143 lines (114 loc) · 2.56 KB
/
connection.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
package gosql
import (
"database/sql"
"errors"
"fmt"
"log"
"strings"
"time"
"github.com/jmoiron/sqlx"
)
// defaultLink set database default link name
var defaultLink = "default"
// If database fatal exit
var FatalExit = true
var dbService = make(map[string]*sqlx.DB, 0)
// DB gets the specified database engine,
// or the default DB if no name is specified.
func Sqlx(name ...string) *sqlx.DB {
dbName := defaultLink
if name != nil {
dbName = name[0]
}
engine, ok := dbService[dbName]
if !ok {
panic(fmt.Sprintf("[db] the database link `%s` is not configured", dbName))
}
return engine
}
// List gets the list of database engines
func List() map[string]*sqlx.DB {
return dbService
}
type Options struct {
maxOpenConns int
maxIdleConns int
maxLifetime int
}
type Option func(*Options)
func WithMaxOpenConns(i int) Option {
return func(options *Options) {
options.maxOpenConns = i
}
}
func WithMaxIdleConns(i int) Option {
return func(options *Options) {
options.maxIdleConns = i
}
}
func WithMaxLifetimes(i int) Option {
return func(options *Options) {
options.maxLifetime = i
}
}
// Open gosql.DB with sqlx
func Open(driver, dbSource string, opts ...Option) (*DB, error) {
var options Options
for _, opt := range opts {
opt(&options)
}
db, err := sqlx.Connect(driver, dbSource)
if err != nil {
return nil, err
}
if options.maxOpenConns > 0 {
db.SetMaxOpenConns(options.maxOpenConns)
}
if options.maxIdleConns > 0 {
db.SetMaxIdleConns(options.maxIdleConns)
}
if options.maxLifetime > 0 {
db.SetConnMaxLifetime(time.Duration(options.maxLifetime) * time.Second)
}
return &DB{database: db}, nil
}
// OpenWithDB open gosql.DB with sql.DB
func OpenWithDB(driver string, db *sql.DB) *DB {
return &DB{database: sqlx.NewDb(db, driver)}
}
// Connect database
func Connect(configs map[string]*Config) (err error) {
var errs []string
defer func() {
if len(errs) > 0 {
err = errors.New("[db] " + strings.Join(errs, "\n"))
if FatalExit {
log.Fatal(err)
}
}
}()
for key, conf := range configs {
if !conf.Enable {
continue
}
sess, err := sqlx.Connect(conf.Driver, conf.Dsn)
if err != nil {
errs = append(errs, err.Error())
continue
}
log.Println("[db] connect:" + key)
if conf.ShowSql {
logger.SetLogging(true)
}
sess.SetMaxOpenConns(conf.MaxOpenConns)
sess.SetMaxIdleConns(conf.MaxIdleConns)
if conf.MaxLifetime > 0 {
sess.SetConnMaxLifetime(time.Duration(conf.MaxLifetime) * time.Second)
}
if db, ok := dbService[key]; ok {
_ = db.Close()
}
dbService[key] = sess
}
return
}