Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add result type: gorm.DB #1057

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion internal/generate/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (m *InterfaceMethod) IsRepeatFromSameInterface(newMethod *InterfaceMethod)
return m.MethodName == newMethod.MethodName && m.InterfaceName == newMethod.InterfaceName && m.TargetStruct == newMethod.TargetStruct
}

//GetParamInTmpl return param list
// GetParamInTmpl return param list
func (m *InterfaceMethod) GetParamInTmpl() string {
return paramToString(m.Params)
}
Expand Down Expand Up @@ -253,6 +253,9 @@ func (m *InterfaceMethod) checkResult(result []parser.Param) (err error) {
param.SetName("rows")
m.GormOption = "Raw"
param.IsPointer = true
case param.IsGormDB():
param.SetName("executeSQL")
m.ResultData = param
default:
if !m.ResultData.IsNull() {
return fmt.Errorf("query method cannot return more than 1 data value in [%s.%s]", m.InterfaceName, m.MethodName)
Expand Down
5 changes: 5 additions & 0 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ func (p *Param) IsSQLRows() bool {
return (p.Package == "sql" && p.Type == "Rows") || (p.Package == "gen" && p.Type == "SQLRows")
}

// IsGormDB ...
func (p *Param) IsGormDB() bool {
return p.Package == "gorm" && p.Type == "DB"
}

// SetName ...
func (p *Param) SetName(name string) {
p.Name = name
Expand Down
4 changes: 2 additions & 2 deletions internal/template/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func ({{.S}} {{.TargetStruct}}Do){{.FuncSign}}{
result,{{if .ReturnError}}err{{else}}_{{end}} = stmt.ConnPool.ExecContext(stmt.Context,generateSQL.String(){{if .HasSQLData}},params...{{end}}) // ignore_security_alert
{{else if .ReturnSQLRow}}row = {{.S}}.UnderlyingDB().Raw(generateSQL.String(){{if .HasSQLData}},params...{{end}}).Row() // ignore_security_alert
{{else if .ReturnSQLRows}}rows,{{if .ReturnError}}err{{else}}_{{end}} = {{.S}}.UnderlyingDB().Raw(generateSQL.String(){{if .HasSQLData}},params...{{end}}).Rows() // ignore_security_alert
{{else}}var executeSQL *gorm.DB
executeSQL = {{.S}}.UnderlyingDB().{{.GormOption}}(generateSQL.String(){{if .HasSQLData}},params...{{end}}){{if not .ResultData.IsNull}}.{{.GormRunMethodName}}({{if .HasGotPoint}}&{{end}}{{.ResultData.Name}}){{end}} // ignore_security_alert
{{else}}{{if not .ResultData.IsGormDB}}var executeSQL *gorm.DB{{end}}
executeSQL = {{if and (.ResultData.IsGormDB) (not .ResultData.IsPointer)}}*{{end}}{{.S}}.UnderlyingDB().{{.GormOption}}(generateSQL.String(){{if .HasSQLData}},params...{{end}}){{if and (not .ResultData.IsNull) (not .ResultData.IsGormDB)}}.{{.GormRunMethodName}}({{if .HasGotPoint}}&{{end}}{{.ResultData.Name}}){{end}} // ignore_security_alert
{{if .ReturnRowsAffected}}rowsAffected = executeSQL.RowsAffected
{{end}}{{if .ReturnError}}err = executeSQL.Error
{{end}}{{if .ReturnNothing}}_ = executeSQL
Expand Down