- update dorm

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

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

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

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

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

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

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

Loading…
Cancel
Save