-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
113 lines (96 loc) · 2.15 KB
/
db.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
package gbase
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/jinzhu/gorm"
// mysql
_ "github.com/jinzhu/gorm/dialects/mysql"
log "github.com/sirupsen/logrus"
)
type gormLogger struct{}
func (*gormLogger) Print(values ...interface{}) {
if values[0] == "sql" {
query := fmt.Sprintf("%.2fms", float64(values[2].(time.Duration).Nanoseconds()/1e4)/100.0)
log.WithFields(log.Fields{"sql": values[3], "sql_bind": values[4], "used": query}).Info("[SQL]")
}
}
// singleton
var _db *dBInstance
var once sync.Once
// DB database manager class
type dBInstance struct {
Master *gorm.DB
Slave *gorm.DB
}
// DB returns a new Model without opening database connection
func DB() *dBInstance {
var err error
once.Do(func() {
if _db == nil {
_db = &dBInstance{}
err = _db.init(DBConn)
}
})
if err == nil {
return _db
}
return nil
}
func (me *dBInstance) Close() {
if me.Master != nil {
me.Master.Close()
}
if me.Slave != nil {
me.Slave.Close()
}
}
func (me *dBInstance) init(str string) error {
arr := strings.Split(str, ",")
for _, v := range arr {
if len(v) <= 0 {
continue
}
if err := me.open(strings.TrimSpace(v)); err != nil {
return err
}
}
return nil
}
// Open opens database connection with the settings found in cfg
func (me *dBInstance) open(dbType string) error {
switch dbType {
case "master":
me.Master = me.getConn(os.Getenv("DB_URL_MASTER"))
case "slave":
me.Slave = me.getConn(os.Getenv("DB_URL_SLAVE"))
case "default":
me.Master = me.getConn(os.Getenv("DB_URL"))
default:
return errors.New("db type err")
}
return nil
}
func (me *dBInstance) getConn(url string) *gorm.DB {
conn, err := gorm.Open("mysql", url)
log.Info(url)
if err != nil {
panic("fail open mysql connection")
}
_dbMaxIdleConns, _ := strconv.Atoi(os.Getenv("DB_MaxIdleConns"))
_dbMaxOpenConns, _ := strconv.Atoi(os.Getenv("DB_MaxOpenConns"))
conn.DB().SetMaxIdleConns(_dbMaxIdleConns)
conn.DB().SetMaxOpenConns(_dbMaxOpenConns)
if err = conn.DB().Ping(); err != nil {
panic("ping mysql err")
}
if os.Getenv("DEBUG") == "true" {
conn.LogMode(true)
conn.SetLogger(&gormLogger{})
}
return conn
}