-
Notifications
You must be signed in to change notification settings - Fork 373
/
db.go
55 lines (45 loc) · 1.28 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
// Copyright 2019 syncd Author. All Rights Reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package syncd
import (
"fmt"
"time"
"errors"
"github.com/jinzhu/gorm"
_ "github.com/go-sql-driver/mysql"
)
type DB struct {
DbHandler *gorm.DB
cfg *DbConfig
}
func NewDatabase(cfg *DbConfig) *DB {
return &DB{
cfg: cfg,
}
}
func (db *DB) Open() error {
c, err := gorm.Open("mysql", db.parseConnConfig())
if err != nil {
return errors.New(fmt.Sprintf("mysql connect failed, %s", err.Error()))
}
c.LogMode(false)
c.DB().SetMaxIdleConns(db.cfg.MaxIdleConns)
c.DB().SetMaxOpenConns(db.cfg.MaxOpenConns)
c.DB().SetConnMaxLifetime(time.Second * time.Duration(db.cfg.ConnMaxLifeTime))
db.DbHandler = c
return nil
}
func (db *DB) Close() {
db.DbHandler.Close()
}
func (db *DB) parseConnConfig() string {
var connHost string
if db.cfg.Unix != "" {
connHost = fmt.Sprintf("unix(%s)", db.cfg.Unix)
} else {
connHost = fmt.Sprintf("tcp(%s:%d)", db.cfg.Host, db.cfg.Port)
}
s := fmt.Sprintf("%s:%s@%s/%s?charset=%s&parseTime=True&loc=Local", db.cfg.User, db.cfg.Pass, connHost, db.cfg.DbName, db.cfg.Charset)
return s
}