Files
gormt/data/view/genfunc/model/gen.base.go
谢小军 ef185ecb97 add func withcontext
添加 WithContext() 支持
2020-09-27 16:48:02 +08:00

93 lines
1.7 KiB
Go

package model
import (
"context"
"time"
"gorm.io/gorm"
)
var globalIsRelated bool = true // 全局预加载
// prepare for other
type _BaseMgr struct {
*gorm.DB
ctx context.Context
cancel context.CancelFunc
timeout time.Duration
isRelated bool
}
// SetCtx set context
func (obj *_BaseMgr) SetTimeOut(timeout time.Duration) {
obj.ctx, obj.cancel = context.WithTimeout(context.Background(), timeout)
obj.timeout = timeout
}
// SetCtx set context
func (obj *_BaseMgr) SetCtx(c context.Context) {
if c != nil {
obj.ctx = c
}
}
// Ctx get context
func (obj *_BaseMgr) GetCtx() context.Context {
return obj.ctx
}
// Cancel cancel context
func (obj *_BaseMgr) Cancel(c context.Context) {
obj.cancel()
}
// GetDB get gorm.DB info
func (obj *_BaseMgr) GetDB() *gorm.DB {
return obj.DB
}
// UpdateDB update gorm.DB info
func (obj *_BaseMgr) UpdateDB(db *gorm.DB) {
obj.DB = db
}
// GetIsRelated Query foreign key Association.获取是否查询外键关联(gorm.Related)
func (obj *_BaseMgr) GetIsRelated() bool {
return obj.isRelated
}
// SetIsRelated Query foreign key Association.设置是否查询外键关联(gorm.Related)
func (obj *_BaseMgr) SetIsRelated(b bool) {
obj.isRelated = b
}
// New new gorm.新gorm
func (obj *_BaseMgr) New() *gorm.DB {
return obj.DB.Session(&gorm.Session{WithConditions: false, Context: obj.ctx})
}
type options struct {
query map[string]interface{}
}
// Option overrides behavior of Connect.
type Option interface {
apply(*options)
}
type optionFunc func(*options)
func (f optionFunc) apply(o *options) {
f(o)
}
// OpenRelated 打开全局预加载
func OpenRelated() {
globalIsRelated = true
}
// CloseRelated 关闭全局预加载
func CloseRelated() {
globalIsRelated = true
}