add func withcontext
添加 WithContext() 支持
This commit is contained in:
8
Makefile
8
Makefile
@@ -1,17 +1,17 @@
|
|||||||
all: # 构建
|
all: # 构建
|
||||||
make tar
|
make tar
|
||||||
windows:
|
windows:
|
||||||
CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -o gormt.exe main.go
|
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o gormt.exe main.go
|
||||||
mac:
|
mac:
|
||||||
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build -o gormt main.go
|
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build -o gormt main.go
|
||||||
linux:
|
linux:
|
||||||
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o gormt main.go
|
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o gormt main.go
|
||||||
tar: # 打包
|
tar: # 打包
|
||||||
CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -o gormt.exe main.go
|
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o gormt.exe main.go
|
||||||
tar czvf gormt_windows.zip gormt.exe config.yml
|
tar czvf gormt_windows.zip gormt.exe config.yml
|
||||||
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build -o gormt main.go
|
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build -o gormt main.go
|
||||||
tar czvf gormt_mac.zip gormt config.yml
|
tar czvf gormt_mac.zip gormt config.yml
|
||||||
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o gormt main.go
|
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o gormt main.go
|
||||||
tar czvf gormt_linux.zip gormt config.yml
|
tar czvf gormt_linux.zip gormt config.yml
|
||||||
clear:
|
clear:
|
||||||
test ! -d model/ || rm -rf model/*
|
test ! -d model/ || rm -rf model/*
|
||||||
|
|||||||
@@ -35,7 +35,19 @@ func (obj *_BaseMgr) SetTimeOut(timeout time.Duration) {
|
|||||||
|
|
||||||
// SetCtx set context
|
// SetCtx set context
|
||||||
func (obj *_BaseMgr) SetCtx(c context.Context) {
|
func (obj *_BaseMgr) SetCtx(c context.Context) {
|
||||||
obj.ctx = c
|
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
|
// GetDB get gorm.DB info
|
||||||
@@ -112,14 +124,14 @@ func (obj *_{{$obj.StructName}}Mgr) GetTableName() string {
|
|||||||
|
|
||||||
// Get 获取
|
// Get 获取
|
||||||
func (obj *_{{$obj.StructName}}Mgr) Get() (result {{$obj.StructName}}, err error) {
|
func (obj *_{{$obj.StructName}}Mgr) Get() (result {{$obj.StructName}}, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Find(&result).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&result).Error
|
||||||
{{GenPreloadList $obj.PreloadList false}}
|
{{GenPreloadList $obj.PreloadList false}}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets 获取批量结果
|
// Gets 获取批量结果
|
||||||
func (obj *_{{$obj.StructName}}Mgr) Gets() (results []*{{$obj.StructName}}, err error) {
|
func (obj *_{{$obj.StructName}}Mgr) Gets() (results []*{{$obj.StructName}}, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&results).Error
|
||||||
{{GenPreloadList $obj.PreloadList true}}
|
{{GenPreloadList $obj.PreloadList true}}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -141,7 +153,7 @@ func (obj *_{{$obj.StructName}}Mgr) GetByOption(opts ...Option) (result {{$obj.S
|
|||||||
o.apply(&options)
|
o.apply(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where(options.query).Find(&result).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&result).Error
|
||||||
{{GenPreloadList $obj.PreloadList false}}
|
{{GenPreloadList $obj.PreloadList false}}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -155,7 +167,7 @@ func (obj *_{{$obj.StructName}}Mgr) GetByOptions(opts ...Option) (results []*{{$
|
|||||||
o.apply(&options)
|
o.apply(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where(options.query).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&results).Error
|
||||||
{{GenPreloadList $obj.PreloadList true}}
|
{{GenPreloadList $obj.PreloadList true}}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -164,20 +176,20 @@ func (obj *_{{$obj.StructName}}Mgr) GetByOptions(opts ...Option) (results []*{{$
|
|||||||
{{range $oem := $obj.Em}}
|
{{range $oem := $obj.Em}}
|
||||||
// GetFrom{{$oem.ColStructName}} 通过{{$oem.ColName}}获取内容 {{$oem.Notes}} {{if $oem.IsMulti}}
|
// GetFrom{{$oem.ColStructName}} 通过{{$oem.ColName}}获取内容 {{$oem.Notes}} {{if $oem.IsMulti}}
|
||||||
func (obj *_{{$obj.StructName}}Mgr) GetFrom{{$oem.ColStructName}}({{CapLowercase $oem.ColStructName}} {{$oem.Type}}) (results []*{{$obj.StructName}}, err error) {
|
func (obj *_{{$obj.StructName}}Mgr) GetFrom{{$oem.ColStructName}}({{CapLowercase $oem.ColStructName}} {{$oem.Type}}) (results []*{{$obj.StructName}}, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("{{$oem.ColName}} = ?", {{CapLowercase $oem.ColStructName}}).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("{{$oem.ColName}} = ?", {{CapLowercase $oem.ColStructName}}).Find(&results).Error
|
||||||
{{GenPreloadList $obj.PreloadList true}}
|
{{GenPreloadList $obj.PreloadList true}}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
{{else}}
|
{{else}}
|
||||||
func (obj *_{{$obj.StructName}}Mgr) GetFrom{{$oem.ColStructName}}({{CapLowercase $oem.ColStructName}} {{$oem.Type}}) (result {{$obj.StructName}}, err error) {
|
func (obj *_{{$obj.StructName}}Mgr) GetFrom{{$oem.ColStructName}}({{CapLowercase $oem.ColStructName}} {{$oem.Type}}) (result {{$obj.StructName}}, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("{{$oem.ColName}} = ?", {{CapLowercase $oem.ColStructName}}).Find(&result).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("{{$oem.ColName}} = ?", {{CapLowercase $oem.ColStructName}}).Find(&result).Error
|
||||||
{{GenPreloadList $obj.PreloadList false}}
|
{{GenPreloadList $obj.PreloadList false}}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
{{end}}
|
{{end}}
|
||||||
// GetBatchFrom{{$oem.ColStructName}} 批量唯一主键查找 {{$oem.Notes}}
|
// GetBatchFrom{{$oem.ColStructName}} 批量唯一主键查找 {{$oem.Notes}}
|
||||||
func (obj *_{{$obj.StructName}}Mgr) GetBatchFrom{{$oem.ColStructName}}({{CapLowercase $oem.ColStructName}}s []{{$oem.Type}}) (results []*{{$obj.StructName}}, err error) {
|
func (obj *_{{$obj.StructName}}Mgr) GetBatchFrom{{$oem.ColStructName}}({{CapLowercase $oem.ColStructName}}s []{{$oem.Type}}) (results []*{{$obj.StructName}}, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("{{$oem.ColName}} IN (?)", {{CapLowercase $oem.ColStructName}}s).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("{{$oem.ColName}} IN (?)", {{CapLowercase $oem.ColStructName}}s).Find(&results).Error
|
||||||
{{GenPreloadList $obj.PreloadList true}}
|
{{GenPreloadList $obj.PreloadList true}}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -186,7 +198,7 @@ func (obj *_{{$obj.StructName}}Mgr) GetBatchFrom{{$oem.ColStructName}}({{CapLowe
|
|||||||
{{range $ofm := $obj.Primay}}
|
{{range $ofm := $obj.Primay}}
|
||||||
// {{GenFListIndex $ofm 1}} primay or index 获取唯一内容
|
// {{GenFListIndex $ofm 1}} primay or index 获取唯一内容
|
||||||
func (obj *_{{$obj.StructName}}Mgr) {{GenFListIndex $ofm 1}}({{GenFListIndex $ofm 2}}) (result {{$obj.StructName}}, err error) {
|
func (obj *_{{$obj.StructName}}Mgr) {{GenFListIndex $ofm 1}}({{GenFListIndex $ofm 2}}) (result {{$obj.StructName}}, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("{{GenFListIndex $ofm 3}}", {{GenFListIndex $ofm 4}}).Find(&result).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("{{GenFListIndex $ofm 3}}", {{GenFListIndex $ofm 4}}).Find(&result).Error
|
||||||
{{GenPreloadList $obj.PreloadList false}}
|
{{GenPreloadList $obj.PreloadList false}}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -195,7 +207,7 @@ func (obj *_{{$obj.StructName}}Mgr) GetBatchFrom{{$oem.ColStructName}}({{CapLowe
|
|||||||
{{range $ofm := $obj.Index}}
|
{{range $ofm := $obj.Index}}
|
||||||
// {{GenFListIndex $ofm 1}} 获取多个内容
|
// {{GenFListIndex $ofm 1}} 获取多个内容
|
||||||
func (obj *_{{$obj.StructName}}Mgr) {{GenFListIndex $ofm 1}}({{GenFListIndex $ofm 2}}) (results []*{{$obj.StructName}}, err error) {
|
func (obj *_{{$obj.StructName}}Mgr) {{GenFListIndex $ofm 1}}({{GenFListIndex $ofm 2}}) (results []*{{$obj.StructName}}, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("{{GenFListIndex $ofm 3}}", {{GenFListIndex $ofm 4}}).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("{{GenFListIndex $ofm 3}}", {{GenFListIndex $ofm 4}}).Find(&results).Error
|
||||||
{{GenPreloadList $obj.PreloadList true}}
|
{{GenPreloadList $obj.PreloadList true}}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,19 @@ func (obj *_BaseMgr) SetTimeOut(timeout time.Duration) {
|
|||||||
|
|
||||||
// SetCtx set context
|
// SetCtx set context
|
||||||
func (obj *_BaseMgr) SetCtx(c context.Context) {
|
func (obj *_BaseMgr) SetCtx(c context.Context) {
|
||||||
obj.ctx = c
|
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
|
// GetDB get gorm.DB info
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ func (obj *_AccountMgr) GetTableName() string {
|
|||||||
|
|
||||||
// Get 获取
|
// Get 获取
|
||||||
func (obj *_AccountMgr) Get() (result Account, err error) {
|
func (obj *_AccountMgr) Get() (result Account, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Find(&result).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&result).Error
|
||||||
if err == nil && obj.isRelated {
|
if err == nil && obj.isRelated {
|
||||||
if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { //
|
if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { //
|
||||||
if err != gorm.ErrRecordNotFound { // 非 没找到
|
if err != gorm.ErrRecordNotFound { // 非 没找到
|
||||||
@@ -41,7 +41,7 @@ func (obj *_AccountMgr) Get() (result Account, err error) {
|
|||||||
|
|
||||||
// Gets 获取批量结果
|
// Gets 获取批量结果
|
||||||
func (obj *_AccountMgr) Gets() (results []*Account, err error) {
|
func (obj *_AccountMgr) Gets() (results []*Account, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&results).Error
|
||||||
if err == nil && obj.isRelated {
|
if err == nil && obj.isRelated {
|
||||||
for i := 0; i < len(results); i++ {
|
for i := 0; i < len(results); i++ {
|
||||||
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
||||||
@@ -90,7 +90,7 @@ func (obj *_AccountMgr) GetByOption(opts ...Option) (result Account, err error)
|
|||||||
o.apply(&options)
|
o.apply(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where(options.query).Find(&result).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&result).Error
|
||||||
if err == nil && obj.isRelated {
|
if err == nil && obj.isRelated {
|
||||||
if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { //
|
if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { //
|
||||||
if err != gorm.ErrRecordNotFound { // 非 没找到
|
if err != gorm.ErrRecordNotFound { // 非 没找到
|
||||||
@@ -111,7 +111,7 @@ func (obj *_AccountMgr) GetByOptions(opts ...Option) (results []*Account, err er
|
|||||||
o.apply(&options)
|
o.apply(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where(options.query).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&results).Error
|
||||||
if err == nil && obj.isRelated {
|
if err == nil && obj.isRelated {
|
||||||
for i := 0; i < len(results); i++ {
|
for i := 0; i < len(results); i++ {
|
||||||
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
||||||
@@ -128,7 +128,7 @@ func (obj *_AccountMgr) GetByOptions(opts ...Option) (results []*Account, err er
|
|||||||
|
|
||||||
// GetFromID 通过id获取内容
|
// GetFromID 通过id获取内容
|
||||||
func (obj *_AccountMgr) GetFromID(id int) (result Account, err error) {
|
func (obj *_AccountMgr) GetFromID(id int) (result Account, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("id = ?", id).Find(&result).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("id = ?", id).Find(&result).Error
|
||||||
if err == nil && obj.isRelated {
|
if err == nil && obj.isRelated {
|
||||||
if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { //
|
if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { //
|
||||||
if err != gorm.ErrRecordNotFound { // 非 没找到
|
if err != gorm.ErrRecordNotFound { // 非 没找到
|
||||||
@@ -142,7 +142,7 @@ func (obj *_AccountMgr) GetFromID(id int) (result Account, err error) {
|
|||||||
|
|
||||||
// GetBatchFromID 批量唯一主键查找
|
// GetBatchFromID 批量唯一主键查找
|
||||||
func (obj *_AccountMgr) GetBatchFromID(ids []int) (results []*Account, err error) {
|
func (obj *_AccountMgr) GetBatchFromID(ids []int) (results []*Account, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("id IN (?)", ids).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("id IN (?)", ids).Find(&results).Error
|
||||||
if err == nil && obj.isRelated {
|
if err == nil && obj.isRelated {
|
||||||
for i := 0; i < len(results); i++ {
|
for i := 0; i < len(results); i++ {
|
||||||
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
||||||
@@ -157,7 +157,7 @@ func (obj *_AccountMgr) GetBatchFromID(ids []int) (results []*Account, err error
|
|||||||
|
|
||||||
// GetFromAccountID 通过account_id获取内容
|
// GetFromAccountID 通过account_id获取内容
|
||||||
func (obj *_AccountMgr) GetFromAccountID(accountID int) (results []*Account, err error) {
|
func (obj *_AccountMgr) GetFromAccountID(accountID int) (results []*Account, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("account_id = ?", accountID).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("account_id = ?", accountID).Find(&results).Error
|
||||||
if err == nil && obj.isRelated {
|
if err == nil && obj.isRelated {
|
||||||
for i := 0; i < len(results); i++ {
|
for i := 0; i < len(results); i++ {
|
||||||
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
||||||
@@ -172,7 +172,7 @@ func (obj *_AccountMgr) GetFromAccountID(accountID int) (results []*Account, err
|
|||||||
|
|
||||||
// GetBatchFromAccountID 批量唯一主键查找
|
// GetBatchFromAccountID 批量唯一主键查找
|
||||||
func (obj *_AccountMgr) GetBatchFromAccountID(accountIDs []int) (results []*Account, err error) {
|
func (obj *_AccountMgr) GetBatchFromAccountID(accountIDs []int) (results []*Account, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("account_id IN (?)", accountIDs).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("account_id IN (?)", accountIDs).Find(&results).Error
|
||||||
if err == nil && obj.isRelated {
|
if err == nil && obj.isRelated {
|
||||||
for i := 0; i < len(results); i++ {
|
for i := 0; i < len(results); i++ {
|
||||||
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
||||||
@@ -187,7 +187,7 @@ func (obj *_AccountMgr) GetBatchFromAccountID(accountIDs []int) (results []*Acco
|
|||||||
|
|
||||||
// GetFromUserID 通过user_id获取内容
|
// GetFromUserID 通过user_id获取内容
|
||||||
func (obj *_AccountMgr) GetFromUserID(userID int) (results []*Account, err error) {
|
func (obj *_AccountMgr) GetFromUserID(userID int) (results []*Account, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("user_id = ?", userID).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("user_id = ?", userID).Find(&results).Error
|
||||||
if err == nil && obj.isRelated {
|
if err == nil && obj.isRelated {
|
||||||
for i := 0; i < len(results); i++ {
|
for i := 0; i < len(results); i++ {
|
||||||
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
||||||
@@ -202,7 +202,7 @@ func (obj *_AccountMgr) GetFromUserID(userID int) (results []*Account, err error
|
|||||||
|
|
||||||
// GetBatchFromUserID 批量唯一主键查找
|
// GetBatchFromUserID 批量唯一主键查找
|
||||||
func (obj *_AccountMgr) GetBatchFromUserID(userIDs []int) (results []*Account, err error) {
|
func (obj *_AccountMgr) GetBatchFromUserID(userIDs []int) (results []*Account, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("user_id IN (?)", userIDs).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("user_id IN (?)", userIDs).Find(&results).Error
|
||||||
if err == nil && obj.isRelated {
|
if err == nil && obj.isRelated {
|
||||||
for i := 0; i < len(results); i++ {
|
for i := 0; i < len(results); i++ {
|
||||||
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
||||||
@@ -217,7 +217,7 @@ func (obj *_AccountMgr) GetBatchFromUserID(userIDs []int) (results []*Account, e
|
|||||||
|
|
||||||
// GetFromType 通过type获取内容
|
// GetFromType 通过type获取内容
|
||||||
func (obj *_AccountMgr) GetFromType(_type int) (results []*Account, err error) {
|
func (obj *_AccountMgr) GetFromType(_type int) (results []*Account, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("type = ?", _type).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("type = ?", _type).Find(&results).Error
|
||||||
if err == nil && obj.isRelated {
|
if err == nil && obj.isRelated {
|
||||||
for i := 0; i < len(results); i++ {
|
for i := 0; i < len(results); i++ {
|
||||||
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
||||||
@@ -232,7 +232,7 @@ func (obj *_AccountMgr) GetFromType(_type int) (results []*Account, err error) {
|
|||||||
|
|
||||||
// GetBatchFromType 批量唯一主键查找
|
// GetBatchFromType 批量唯一主键查找
|
||||||
func (obj *_AccountMgr) GetBatchFromType(_types []int) (results []*Account, err error) {
|
func (obj *_AccountMgr) GetBatchFromType(_types []int) (results []*Account, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("type IN (?)", _types).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("type IN (?)", _types).Find(&results).Error
|
||||||
if err == nil && obj.isRelated {
|
if err == nil && obj.isRelated {
|
||||||
for i := 0; i < len(results); i++ {
|
for i := 0; i < len(results); i++ {
|
||||||
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
||||||
@@ -247,7 +247,7 @@ func (obj *_AccountMgr) GetBatchFromType(_types []int) (results []*Account, err
|
|||||||
|
|
||||||
// GetFromName 通过name获取内容
|
// GetFromName 通过name获取内容
|
||||||
func (obj *_AccountMgr) GetFromName(name string) (results []*Account, err error) {
|
func (obj *_AccountMgr) GetFromName(name string) (results []*Account, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("name = ?", name).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("name = ?", name).Find(&results).Error
|
||||||
if err == nil && obj.isRelated {
|
if err == nil && obj.isRelated {
|
||||||
for i := 0; i < len(results); i++ {
|
for i := 0; i < len(results); i++ {
|
||||||
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
||||||
@@ -262,7 +262,7 @@ func (obj *_AccountMgr) GetFromName(name string) (results []*Account, err error)
|
|||||||
|
|
||||||
// GetBatchFromName 批量唯一主键查找
|
// GetBatchFromName 批量唯一主键查找
|
||||||
func (obj *_AccountMgr) GetBatchFromName(names []string) (results []*Account, err error) {
|
func (obj *_AccountMgr) GetBatchFromName(names []string) (results []*Account, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("name IN (?)", names).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("name IN (?)", names).Find(&results).Error
|
||||||
if err == nil && obj.isRelated {
|
if err == nil && obj.isRelated {
|
||||||
for i := 0; i < len(results); i++ {
|
for i := 0; i < len(results); i++ {
|
||||||
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
||||||
@@ -279,7 +279,7 @@ func (obj *_AccountMgr) GetBatchFromName(names []string) (results []*Account, er
|
|||||||
|
|
||||||
// FetchByPrimaryKey primay or index 获取唯一内容
|
// FetchByPrimaryKey primay or index 获取唯一内容
|
||||||
func (obj *_AccountMgr) FetchByPrimaryKey(id int) (result Account, err error) {
|
func (obj *_AccountMgr) FetchByPrimaryKey(id int) (result Account, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("id = ?", id).Find(&result).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("id = ?", id).Find(&result).Error
|
||||||
if err == nil && obj.isRelated {
|
if err == nil && obj.isRelated {
|
||||||
if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { //
|
if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { //
|
||||||
if err != gorm.ErrRecordNotFound { // 非 没找到
|
if err != gorm.ErrRecordNotFound { // 非 没找到
|
||||||
@@ -293,7 +293,7 @@ func (obj *_AccountMgr) FetchByPrimaryKey(id int) (result Account, err error) {
|
|||||||
|
|
||||||
// FetchUniqueIndexByAccount primay or index 获取唯一内容
|
// FetchUniqueIndexByAccount primay or index 获取唯一内容
|
||||||
func (obj *_AccountMgr) FetchUniqueIndexByAccount(accountID int, userID int) (result Account, err error) {
|
func (obj *_AccountMgr) FetchUniqueIndexByAccount(accountID int, userID int) (result Account, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("account_id = ? AND user_id = ?", accountID, userID).Find(&result).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("account_id = ? AND user_id = ?", accountID, userID).Find(&result).Error
|
||||||
if err == nil && obj.isRelated {
|
if err == nil && obj.isRelated {
|
||||||
if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { //
|
if err = obj.New().Table("user").Where("user_id = ?", result.UserID).Find(&result.User).Error; err != nil { //
|
||||||
if err != gorm.ErrRecordNotFound { // 非 没找到
|
if err != gorm.ErrRecordNotFound { // 非 没找到
|
||||||
@@ -307,7 +307,7 @@ func (obj *_AccountMgr) FetchUniqueIndexByAccount(accountID int, userID int) (re
|
|||||||
|
|
||||||
// FetchIndexByTp 获取多个内容
|
// FetchIndexByTp 获取多个内容
|
||||||
func (obj *_AccountMgr) FetchIndexByTp(userID int, _type int) (results []*Account, err error) {
|
func (obj *_AccountMgr) FetchIndexByTp(userID int, _type int) (results []*Account, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("user_id = ? AND type = ?", userID, _type).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("user_id = ? AND type = ?", userID, _type).Find(&results).Error
|
||||||
if err == nil && obj.isRelated {
|
if err == nil && obj.isRelated {
|
||||||
for i := 0; i < len(results); i++ {
|
for i := 0; i < len(results); i++ {
|
||||||
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
if err = obj.New().Table("user").Where("user_id = ?", results[i].UserID).Find(&results[i].User).Error; err != nil { //
|
||||||
|
|||||||
@@ -27,14 +27,14 @@ func (obj *_UserMgr) GetTableName() string {
|
|||||||
|
|
||||||
// Get 获取
|
// Get 获取
|
||||||
func (obj *_UserMgr) Get() (result User, err error) {
|
func (obj *_UserMgr) Get() (result User, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Find(&result).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&result).Error
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets 获取批量结果
|
// Gets 获取批量结果
|
||||||
func (obj *_UserMgr) Gets() (results []*User, err error) {
|
func (obj *_UserMgr) Gets() (results []*User, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&results).Error
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -70,7 +70,7 @@ func (obj *_UserMgr) GetByOption(opts ...Option) (result User, err error) {
|
|||||||
o.apply(&options)
|
o.apply(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where(options.query).Find(&result).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&result).Error
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -84,7 +84,7 @@ func (obj *_UserMgr) GetByOptions(opts ...Option) (results []*User, err error) {
|
|||||||
o.apply(&options)
|
o.apply(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where(options.query).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&results).Error
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -93,56 +93,56 @@ func (obj *_UserMgr) GetByOptions(opts ...Option) (results []*User, err error) {
|
|||||||
|
|
||||||
// GetFromUserID 通过user_id获取内容
|
// GetFromUserID 通过user_id获取内容
|
||||||
func (obj *_UserMgr) GetFromUserID(userID int) (result User, err error) {
|
func (obj *_UserMgr) GetFromUserID(userID int) (result User, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("user_id = ?", userID).Find(&result).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("user_id = ?", userID).Find(&result).Error
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBatchFromUserID 批量唯一主键查找
|
// GetBatchFromUserID 批量唯一主键查找
|
||||||
func (obj *_UserMgr) GetBatchFromUserID(userIDs []int) (results []*User, err error) {
|
func (obj *_UserMgr) GetBatchFromUserID(userIDs []int) (results []*User, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("user_id IN (?)", userIDs).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("user_id IN (?)", userIDs).Find(&results).Error
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetFromName 通过name获取内容
|
// GetFromName 通过name获取内容
|
||||||
func (obj *_UserMgr) GetFromName(name string) (results []*User, err error) {
|
func (obj *_UserMgr) GetFromName(name string) (results []*User, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("name = ?", name).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("name = ?", name).Find(&results).Error
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBatchFromName 批量唯一主键查找
|
// GetBatchFromName 批量唯一主键查找
|
||||||
func (obj *_UserMgr) GetBatchFromName(names []string) (results []*User, err error) {
|
func (obj *_UserMgr) GetBatchFromName(names []string) (results []*User, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("name IN (?)", names).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("name IN (?)", names).Find(&results).Error
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetFromSex 通过sex获取内容
|
// GetFromSex 通过sex获取内容
|
||||||
func (obj *_UserMgr) GetFromSex(sex int) (results []*User, err error) {
|
func (obj *_UserMgr) GetFromSex(sex int) (results []*User, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("sex = ?", sex).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("sex = ?", sex).Find(&results).Error
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBatchFromSex 批量唯一主键查找
|
// GetBatchFromSex 批量唯一主键查找
|
||||||
func (obj *_UserMgr) GetBatchFromSex(sexs []int) (results []*User, err error) {
|
func (obj *_UserMgr) GetBatchFromSex(sexs []int) (results []*User, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("sex IN (?)", sexs).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("sex IN (?)", sexs).Find(&results).Error
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetFromJob 通过job获取内容
|
// GetFromJob 通过job获取内容
|
||||||
func (obj *_UserMgr) GetFromJob(job int) (results []*User, err error) {
|
func (obj *_UserMgr) GetFromJob(job int) (results []*User, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("job = ?", job).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("job = ?", job).Find(&results).Error
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBatchFromJob 批量唯一主键查找
|
// GetBatchFromJob 批量唯一主键查找
|
||||||
func (obj *_UserMgr) GetBatchFromJob(jobs []int) (results []*User, err error) {
|
func (obj *_UserMgr) GetBatchFromJob(jobs []int) (results []*User, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("job IN (?)", jobs).Find(&results).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("job IN (?)", jobs).Find(&results).Error
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -151,7 +151,7 @@ func (obj *_UserMgr) GetBatchFromJob(jobs []int) (results []*User, err error) {
|
|||||||
|
|
||||||
// FetchByPrimaryKey primay or index 获取唯一内容
|
// FetchByPrimaryKey primay or index 获取唯一内容
|
||||||
func (obj *_UserMgr) FetchByPrimaryKey(userID int) (result User, err error) {
|
func (obj *_UserMgr) FetchByPrimaryKey(userID int) (result User, err error) {
|
||||||
err = obj.DB.Table(obj.GetTableName()).Where("user_id = ?", userID).Find(&result).Error
|
err = obj.WithContext(obj.ctx).Table(obj.GetTableName()).Where("user_id = ?", userID).Find(&result).Error
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user