wp-go/db/db.go

32 lines
739 B
Go
Raw Normal View History

2022-08-27 13:21:05 +00:00
package db
import (
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
2022-11-04 02:38:59 +00:00
"github/fthvgb1/wp-go/config"
2022-08-27 13:21:05 +00:00
)
var Db *sqlx.DB
func InitDb() error {
2022-11-04 02:38:59 +00:00
dsn := config.Conf.Mysql.Dsn.GetDsn()
2022-08-27 13:21:05 +00:00
var err error
Db, err = sqlx.Open("mysql", dsn)
if err != nil {
return err
}
2022-11-04 02:38:59 +00:00
if config.Conf.Mysql.Pool.ConnMaxIdleTime != 0 {
Db.SetConnMaxIdleTime(config.Conf.Mysql.Pool.ConnMaxLifetime)
2022-08-27 13:21:05 +00:00
}
2022-11-04 02:38:59 +00:00
if config.Conf.Mysql.Pool.MaxIdleConn != 0 {
Db.SetMaxIdleConns(config.Conf.Mysql.Pool.MaxIdleConn)
2022-08-27 13:21:05 +00:00
}
2022-11-04 02:38:59 +00:00
if config.Conf.Mysql.Pool.MaxOpenConn != 0 {
Db.SetMaxOpenConns(config.Conf.Mysql.Pool.MaxOpenConn)
2022-08-27 13:21:05 +00:00
}
2022-11-04 02:38:59 +00:00
if config.Conf.Mysql.Pool.ConnMaxLifetime != 0 {
Db.SetConnMaxLifetime(config.Conf.Mysql.Pool.ConnMaxLifetime)
2022-08-27 13:21:05 +00:00
}
return err
}