- update dorm

master
李光春 1 year ago
parent 4bbe48ac00
commit df55706358

@ -30,7 +30,7 @@ type GormClientConfig struct {
// GormClient // GormClient
// https://gorm.io/zh_CN/docs/index.html // https://gorm.io/zh_CN/docs/index.html
type GormClient struct { type GormClient struct {
Db *gorm.DB // 驱动 db *gorm.DB // 驱动
config *GormClientConfig // 配置 config *GormClientConfig // 配置
} }

@ -4,5 +4,5 @@ import "gorm.io/gorm"
// GetDb 获取驱动 // GetDb 获取驱动
func (c *GormClient) GetDb() *gorm.DB { func (c *GormClient) GetDb() *gorm.DB {
return c.Db return c.db
} }

@ -37,7 +37,7 @@ func NewGormMysqlClient(config *GormClientConfig) (*GormClient, error) {
} else { } else {
logLevel = logger.Info logLevel = logger.Info
} }
c.Db, err = gorm.Open(mysql.Open(c.config.Dns), &gorm.Config{ c.db, err = gorm.Open(mysql.Open(c.config.Dns), &gorm.Config{
Logger: logger.New( Logger: logger.New(
writer{}, writer{},
logger.Config{ logger.Config{
@ -52,37 +52,37 @@ func NewGormMysqlClient(config *GormClientConfig) (*GormClient, error) {
}, },
}) })
} else { } else {
c.Db, err = gorm.Open(mysql.Open(c.config.Dns), &gorm.Config{}) c.db, err = gorm.Open(mysql.Open(c.config.Dns), &gorm.Config{})
} }
if err != nil { if err != nil {
return nil, errors.New(fmt.Sprintf("连接失败:%v", err)) return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
} }
sqlDB, err := c.Db.DB() sqlDd, err := c.db.DB()
if err != nil { if err != nil {
return nil, errors.New(fmt.Sprintf("检查连接失败:%v", err)) return nil, errors.New(fmt.Sprintf("检查连接失败:%v", err))
} }
// 设置空闲连接池中连接的最大数量 // 设置空闲连接池中连接的最大数量
if c.config.ConnSetMaxIdle == 0 { if c.config.ConnSetMaxIdle == 0 {
sqlDB.SetMaxIdleConns(10) sqlDd.SetMaxIdleConns(10)
} else { } else {
sqlDB.SetMaxIdleConns(c.config.ConnSetMaxIdle) sqlDd.SetMaxIdleConns(c.config.ConnSetMaxIdle)
} }
// 设置打开数据库连接的最大数量 // 设置打开数据库连接的最大数量
if c.config.ConnSetMaxOpen == 0 { if c.config.ConnSetMaxOpen == 0 {
sqlDB.SetMaxOpenConns(100) sqlDd.SetMaxOpenConns(100)
} else { } else {
sqlDB.SetMaxOpenConns(c.config.ConnSetMaxOpen) sqlDd.SetMaxOpenConns(c.config.ConnSetMaxOpen)
} }
// 设置了连接可复用的最大时间 // 设置了连接可复用的最大时间
if c.config.ConnSetConnMaxLifetime == 0 { if c.config.ConnSetConnMaxLifetime == 0 {
sqlDB.SetConnMaxLifetime(time.Second * 600) sqlDd.SetConnMaxLifetime(time.Second * 600)
} else { } else {
sqlDB.SetConnMaxLifetime(time.Duration(c.config.ConnSetConnMaxLifetime)) sqlDd.SetConnMaxLifetime(time.Duration(c.config.ConnSetConnMaxLifetime))
} }
return c, nil return c, nil

@ -38,7 +38,7 @@ func NewGormPostgresClient(config *GormClientConfig) (*GormClient, error) {
} else { } else {
logLevel = logger.Info logLevel = logger.Info
} }
c.Db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{ c.db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{
Logger: logger.New( Logger: logger.New(
writer{}, writer{},
logger.Config{ logger.Config{
@ -53,37 +53,37 @@ func NewGormPostgresClient(config *GormClientConfig) (*GormClient, error) {
}, },
}) })
} else { } else {
c.Db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{}) c.db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{})
} }
if err != nil { if err != nil {
return nil, errors.New(fmt.Sprintf("连接失败:%v", err)) return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
} }
sqlDB, err := c.Db.DB() sqlDd, err := c.db.DB()
if err != nil { if err != nil {
return nil, errors.New(fmt.Sprintf("检查连接失败:%v", err)) return nil, errors.New(fmt.Sprintf("检查连接失败:%v", err))
} }
// 设置空闲连接池中连接的最大数量 // 设置空闲连接池中连接的最大数量
if c.config.ConnSetMaxIdle == 0 { if c.config.ConnSetMaxIdle == 0 {
sqlDB.SetMaxIdleConns(10) sqlDd.SetMaxIdleConns(10)
} else { } else {
sqlDB.SetMaxIdleConns(c.config.ConnSetMaxIdle) sqlDd.SetMaxIdleConns(c.config.ConnSetMaxIdle)
} }
// 设置打开数据库连接的最大数量 // 设置打开数据库连接的最大数量
if c.config.ConnSetMaxOpen == 0 { if c.config.ConnSetMaxOpen == 0 {
sqlDB.SetMaxOpenConns(100) sqlDd.SetMaxOpenConns(100)
} else { } else {
sqlDB.SetMaxOpenConns(c.config.ConnSetMaxOpen) sqlDd.SetMaxOpenConns(c.config.ConnSetMaxOpen)
} }
// 设置了连接可复用的最大时间 // 设置了连接可复用的最大时间
if c.config.ConnSetConnMaxLifetime == 0 { if c.config.ConnSetConnMaxLifetime == 0 {
sqlDB.SetConnMaxLifetime(time.Hour) sqlDd.SetConnMaxLifetime(time.Hour)
} else { } else {
sqlDB.SetConnMaxLifetime(time.Duration(c.config.ConnSetConnMaxLifetime)) sqlDd.SetConnMaxLifetime(time.Duration(c.config.ConnSetConnMaxLifetime))
} }
return c, nil return c, nil
@ -117,7 +117,7 @@ func NewGormPostgresqlClient(config *GormClientConfig) (*GormClient, error) {
} else { } else {
logLevel = logger.Info logLevel = logger.Info
} }
c.Db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{ c.db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{
Logger: logger.New( Logger: logger.New(
writer{}, writer{},
logger.Config{ logger.Config{
@ -132,37 +132,37 @@ func NewGormPostgresqlClient(config *GormClientConfig) (*GormClient, error) {
}, },
}) })
} else { } else {
c.Db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{}) c.db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{})
} }
if err != nil { if err != nil {
return nil, errors.New(fmt.Sprintf("连接失败:%v", err)) return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
} }
sqlDB, err := c.Db.DB() sqlDd, err := c.db.DB()
if err != nil { if err != nil {
return nil, errors.New(fmt.Sprintf("检查连接失败:%v", err)) return nil, errors.New(fmt.Sprintf("检查连接失败:%v", err))
} }
// 设置空闲连接池中连接的最大数量 // 设置空闲连接池中连接的最大数量
if c.config.ConnSetMaxIdle == 0 { if c.config.ConnSetMaxIdle == 0 {
sqlDB.SetMaxIdleConns(10) sqlDd.SetMaxIdleConns(10)
} else { } else {
sqlDB.SetMaxIdleConns(c.config.ConnSetMaxIdle) sqlDd.SetMaxIdleConns(c.config.ConnSetMaxIdle)
} }
// 设置打开数据库连接的最大数量 // 设置打开数据库连接的最大数量
if c.config.ConnSetMaxOpen == 0 { if c.config.ConnSetMaxOpen == 0 {
sqlDB.SetMaxOpenConns(100) sqlDd.SetMaxOpenConns(100)
} else { } else {
sqlDB.SetMaxOpenConns(c.config.ConnSetMaxOpen) sqlDd.SetMaxOpenConns(c.config.ConnSetMaxOpen)
} }
// 设置了连接可复用的最大时间 // 设置了连接可复用的最大时间
if c.config.ConnSetConnMaxLifetime == 0 { if c.config.ConnSetConnMaxLifetime == 0 {
sqlDB.SetConnMaxLifetime(time.Hour) sqlDd.SetConnMaxLifetime(time.Hour)
} else { } else {
sqlDB.SetConnMaxLifetime(time.Duration(c.config.ConnSetConnMaxLifetime)) sqlDd.SetConnMaxLifetime(time.Duration(c.config.ConnSetConnMaxLifetime))
} }
return c, nil return c, nil

@ -4,15 +4,15 @@ import "gorm.io/gorm"
// Begin 开始事务,不需要创建 Session 对象 // Begin 开始事务,不需要创建 Session 对象
func (c *GormClient) Begin() *gorm.DB { func (c *GormClient) Begin() *gorm.DB {
return c.Db.Begin() return c.db.Begin()
} }
// Rollback 回滚事务 // Rollback 回滚事务
func (c *GormClient) Rollback() *gorm.DB { func (c *GormClient) Rollback() *gorm.DB {
return c.Db.Rollback() return c.db.Rollback()
} }
// Commit 提交事务 // Commit 提交事务
func (c *GormClient) Commit() *gorm.DB { func (c *GormClient) Commit() *gorm.DB {
return c.Db.Commit() return c.db.Commit()
} }

@ -11,7 +11,7 @@ type XormClientSession struct {
// Begin 开始事务,需要创建 Session 对象 // Begin 开始事务,需要创建 Session 对象
//func (c *XormClient) Begin() (*XormClientSession, error) { //func (c *XormClient) Begin() (*XormClientSession, error) {
// session := c.Db.NewSession() // session := c.db.NewSession()
// defer session.Close() // defer session.Close()
// return &session, session.Begin() // return &session, session.Begin()
//} //}

Loading…
Cancel
Save