/
mysql.go
72 lines (66 loc) · 2.23 KB
/
mysql.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
package plugins
import (
"database/sql"
"fmt"
"runtime"
"time"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type MySQLConfig struct {
ID string `yaml:"id" required:"true"`
Addr string `yaml:"addr" required:"true"`
Database string `yaml:"database" required:"true"`
Username string `yaml:"username" required:"true"`
Password string `yaml:"password" required:"true"`
Charset string `yaml:"charset" default:"utf8"`
ParseTime string `yaml:"parse_time" default:"True"`
Loc string `yaml:"loc" default:"Local"`
MaxIdleConns int `yaml:"max_idle_conns" default:"0"`
MaxOpenConns int `yaml:"max_open_conns" default:"0"`
ConnMaxIdleTime time.Duration `yaml:"conn_max_idle_time" default:"5m"`
ConnMaxLifeTime time.Duration `yaml:"conn_max_life_time" default:"0"`
// automatically start transaction before API be processed
AutoWithTransaction bool `yaml:"auto_with_transaction" default:"false"`
// automatically rollback transaction on errors
AutoRollbackTransaction bool `yaml:"auto_rollback_transaction" default:"false"`
// automatically commit transaction if return success
AutoCommitTransaction bool `yaml:"auto_commit_transaction" default:"true"`
}
type MySQL struct {
Config *MySQLConfig
*gorm.DB
}
func NewMySQL(config MySQLConfig) (plugin *MySQL, err error) {
// set dialect settings
var dsn = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=%s&parseTime=%s&loc=%s",
config.Username,
config.Password,
config.Addr,
config.Database,
config.Charset,
config.ParseTime,
config.Loc)
var gdb *gorm.DB = nil
gdb, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if gdb == nil || err != nil {
return nil, err
}
var db *sql.DB = nil
if db, err = gdb.DB(); db == nil || err != nil {
return nil, err
}
// connection pool settings
if config.MaxIdleConns > 0 {
db.SetMaxIdleConns(config.MaxIdleConns)
} else {
// default connection pool size
db.SetMaxIdleConns(runtime.NumCPU())
}
if config.MaxOpenConns > 0 {
db.SetMaxOpenConns(config.MaxOpenConns)
}
db.SetConnMaxIdleTime(config.ConnMaxIdleTime)
db.SetConnMaxLifetime(config.ConnMaxLifeTime)
return &MySQL{Config: &config, DB: gdb}, err
}