Skip to content

Commit

Permalink
add newDB
Browse files Browse the repository at this point in the history
添加newdb
  • Loading branch information
xxjwxc committed Sep 29, 2021
1 parent b379e2a commit 00b9b81
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions data/view/genfunc/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,14 @@ 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{ Context: obj.ctx})
// New new gorm.新gorm,重置条件
func (obj *_BaseMgr) New() {
obj.DB = obj.NewDB()
}
// NewDB new gorm.新gorm
func (obj *_BaseMgr) NewDB() *gorm.DB {
return obj.DB.Session(&gorm.Session{NewDB: true, Context: obj.ctx})
}
type options struct {
Expand Down Expand Up @@ -290,25 +295,25 @@ func (obj *_{{$obj.StructName}}Mgr) GetBatchFrom{{$oem.ColStructName}}({{CapLowe
`
genPreload = `if err == nil && obj.isRelated { {{range $obj := .}}{{if $obj.IsMulti}}
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 = obj.NewDB().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.New().Table("{{$obj.ForeignkeyTableName}}").Where("{{$obj.ForeignkeyCol}} = ?", result.{{$obj.ColStructName}}).Find(&result.{{$obj.ForeignkeyStructName}}).Error; err != nil { // {{$obj.Notes}}
if err = obj.NewDB().Table("{{$obj.ForeignkeyTableName}}").Where("{{$obj.ForeignkeyCol}} = ?", result.{{$obj.ColStructName}}).Find(&result.{{$obj.ForeignkeyStructName}}).Error; err != nil { // {{$obj.Notes}}
if err != gorm.ErrRecordNotFound { // 非 没找到
return
}
}{{end}} {{end}}}
`
genPreloadMulti = `if err == nil && obj.isRelated {
for i := 0; i < len(results); i++ { {{range $obj := .}}{{if $obj.IsMulti}}
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 = obj.NewDB().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.New().Table("{{$obj.ForeignkeyTableName}}").Where("{{$obj.ForeignkeyCol}} = ?", results[i].{{$obj.ColStructName}}).Find(&results[i].{{$obj.ForeignkeyStructName}}).Error; err != nil { // {{$obj.Notes}}
if err = obj.NewDB().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
}
Expand Down

0 comments on commit 00b9b81

Please sign in to comment.