gorm v2.0
支持gormv2.0
This commit is contained in:
@@ -58,6 +58,11 @@ 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{}
|
||||
}
|
||||
@@ -96,9 +101,8 @@ func {{$obj.StructName}}Mgr(db *gorm.DB) *_{{$obj.StructName}}Mgr {
|
||||
if db == nil {
|
||||
panic(fmt.Errorf("{{$obj.StructName}}Mgr need init by db"))
|
||||
}
|
||||
timeout := 10 * time.Second
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
return &_{{$obj.StructName}}Mgr{_BaseMgr: &_BaseMgr{DB: db.Table("{{$obj.TableName}}"), isRelated: globalIsRelated,ctx:ctx,cancel:cancel,timeout:timeout}}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
return &_{{$obj.StructName}}Mgr{_BaseMgr: &_BaseMgr{DB: db.Table("{{$obj.TableName}}"), isRelated: globalIsRelated,ctx:ctx,cancel:cancel,timeout:-1}}
|
||||
}
|
||||
|
||||
// GetTableName get sql table name.获取数据库名字
|
||||
@@ -199,12 +203,12 @@ func (obj *_{{$obj.StructName}}Mgr) GetBatchFrom{{$oem.ColStructName}}({{CapLowe
|
||||
|
||||
`
|
||||
genPreload = `if err == nil && obj.isRelated { {{range $obj := .}}{{if $obj.IsMulti}}
|
||||
if err = obj.WithContext(obj.ctx).Table("{{$obj.ForeignkeyTableName}}").Where("{{$obj.ForeignkeyCol}} = ?", result.{{$obj.ColStructName}}).Find(&result.{{$obj.ForeignkeyStructName}}List).Error;err != nil { // {{$obj.Notes}}
|
||||
if err = obj.New().Table("{{$obj.ForeignkeyTableName}}").Where("{{$obj.ForeignkeyCol}} = ?", result.{{$obj.ColStructName}}).Find(&result.{{$obj.ForeignkeyStructName}}List).Error;err != nil { // {{$obj.Notes}}
|
||||
if err != gorm.ErrRecordNotFound { // 非 没找到
|
||||
return
|
||||
}
|
||||
} {{else}}
|
||||
if err = obj.WithContext(obj.ctx).Table("{{$obj.ForeignkeyTableName}}").Where("{{$obj.ForeignkeyCol}} = ?", result.{{$obj.ColStructName}}).Find(&result.{{$obj.ForeignkeyStructName}}).Error; err != nil { // {{$obj.Notes}}
|
||||
if err = obj.New().Table("{{$obj.ForeignkeyTableName}}").Where("{{$obj.ForeignkeyCol}} = ?", result.{{$obj.ColStructName}}).Find(&result.{{$obj.ForeignkeyStructName}}).Error; err != nil { // {{$obj.Notes}}
|
||||
if err != gorm.ErrRecordNotFound { // 非 没找到
|
||||
return
|
||||
}
|
||||
@@ -212,12 +216,12 @@ func (obj *_{{$obj.StructName}}Mgr) GetBatchFrom{{$oem.ColStructName}}({{CapLowe
|
||||
`
|
||||
genPreloadMulti = `if err == nil && obj.isRelated {
|
||||
for i := 0; i < len(results); i++ { {{range $obj := .}}{{if $obj.IsMulti}}
|
||||
if err = obj.WithContext(obj.ctx).Table("{{$obj.ForeignkeyTableName}}").Where("{{$obj.ForeignkeyCol}} = ?", results[i].{{$obj.ColStructName}}).Find(&results[i].{{$obj.ForeignkeyStructName}}List).Error;err != nil { // {{$obj.Notes}}
|
||||
if err = obj.New().Table("{{$obj.ForeignkeyTableName}}").Where("{{$obj.ForeignkeyCol}} = ?", results[i].{{$obj.ColStructName}}).Find(&results[i].{{$obj.ForeignkeyStructName}}List).Error;err != nil { // {{$obj.Notes}}
|
||||
if err != gorm.ErrRecordNotFound { // 非 没找到
|
||||
return
|
||||
}
|
||||
} {{else}}
|
||||
if err = obj.WithContext(obj.ctx).Table("{{$obj.ForeignkeyTableName}}").Where("{{$obj.ForeignkeyCol}} = ?", results[i].{{$obj.ColStructName}}).Find(&results[i].{{$obj.ForeignkeyStructName}}).Error; err != nil { // {{$obj.Notes}}
|
||||
if err = obj.New().Table("{{$obj.ForeignkeyTableName}}").Where("{{$obj.ForeignkeyCol}} = ?", results[i].{{$obj.ColStructName}}).Find(&results[i].{{$obj.ForeignkeyStructName}}).Error; err != nil { // {{$obj.Notes}}
|
||||
if err != gorm.ErrRecordNotFound { // 非 没找到
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user