package sqlitex
|
|
import (
|
"github.com/jinzhu/gorm"
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
"time"
|
)
|
|
type Conf struct {
|
LogMode bool
|
MaxIdleCon int64
|
MaxOpenCon int64
|
ConnMaxLifeTimeSecond int64
|
ConnMaxIdleTimeSecond int64
|
Dsn string
|
Host string
|
}
|
|
var openDb *gorm.DB
|
|
func Init(conf *Conf) error {
|
db, err := gorm.Open("sqlite3", conf.Dsn)
|
if err != nil {
|
return err
|
}
|
sqlDb := db.DB()
|
sqlDb.SetMaxIdleConns(int(conf.MaxIdleCon))
|
sqlDb.SetMaxOpenConns(int(conf.MaxOpenCon))
|
sqlDb.SetConnMaxLifetime(time.Duration(conf.ConnMaxLifeTimeSecond) * time.Second)
|
sqlDb.SetConnMaxIdleTime(time.Duration(conf.ConnMaxIdleTimeSecond) * time.Second)
|
openDb = db
|
db.LogMode(true)
|
return nil
|
}
|
|
func GetDB() *gorm.DB {
|
return openDb
|
}
|