Skip to content

Commit

Permalink
feat: add gen.R return execute affectedRows
Browse files Browse the repository at this point in the history
  • Loading branch information
luzhangqiang committed Oct 8, 2021
1 parent 018f61a commit eb1ac5e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
3 changes: 3 additions & 0 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type T interface{}
// M map[string]interface{}
type M map[string]interface{}

// R execute affected raws
type R int64

// NewGenerator create a new generator
func NewGenerator(cfg Config) *Generator {
err := cfg.Revise()
Expand Down
55 changes: 38 additions & 17 deletions internal/check/checkinterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,21 @@ type slice struct {

// InterfaceMethod interface's method
type InterfaceMethod struct {
Doc string
S string
OriginStruct parser.Param
TargetStruct string
MethodName string
Params []parser.Param
Result []parser.Param
ResultData parser.Param
ExecuteResult string
SqlTmplList []string
SqlData []string
SqlString string
GormOption string
Table string // specified by user. if empty, generate it with gorm
InterfaceName string
Package string
Doc string //comment
S string //First letter of
OriginStruct parser.Param // origin struct name
TargetStruct string // generated query struct bane
MethodName string // generated function name
Params []parser.Param // function input params
Result []parser.Param // function output params
ResultData parser.Param // output data
SqlTmplList []string // generated function content
SqlData []string // variable in sql need function input
SqlString string // SQL
GormOption string // gorm execute method Find or Exec or Take
Table string // specified by user. if empty, generate it with gorm
InterfaceName string // origin interface name
Package string // interface package name
}

// HasSqlData has variable or not
Expand All @@ -57,6 +56,24 @@ func (m *InterfaceMethod) GormRunMethodName() string {
return "Take"
}

func (m *InterfaceMethod) ReturnRowsAffected() bool {
for _, res := range m.Result {
if res.Name == "rowsAffected" {
return true
}
}
return false
}

func (m *InterfaceMethod) ReturnError() bool {
for _, res := range m.Result {
if res.IsError() {
return true
}
}
return false
}

// IsRepeatFromDifferentInterface check different interface has same mame method
func (m *InterfaceMethod) IsRepeatFromDifferentInterface(newMethod *InterfaceMethod) bool {
return m.MethodName == newMethod.MethodName && m.InterfaceName != newMethod.InterfaceName && m.TargetStruct == newMethod.TargetStruct
Expand Down Expand Up @@ -157,7 +174,6 @@ func (m *InterfaceMethod) checkResult(result []parser.Param) (err error) {
return fmt.Errorf("query method cannot return more than 1 error value in [%s.%s]", m.InterfaceName, m.MethodName)
}
param.SetName("err")
m.ExecuteResult = "err"
hasError = true
case param.Eq(m.OriginStruct) || param.IsGenT():
if !m.ResultData.IsNull() {
Expand All @@ -170,6 +186,11 @@ func (m *InterfaceMethod) checkResult(result []parser.Param) (err error) {
m.ResultData = param
case param.IsInterface():
return fmt.Errorf("query method can not return interface in [%s.%s]", m.InterfaceName, m.MethodName)
case param.IsGenR():
param.Type = "int64"
param.Package = ""
param.SetName("rowsAffected")
m.GormOption = "Exec"
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
1 change: 0 additions & 1 deletion internal/check/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func CheckInterface(f *parser.InterfaceSet, s *BaseStruct, data []*InterfaceMeth
MethodName: method.MethodName,
Params: method.Params,
Doc: method.Doc,
ExecuteResult: "_",
Table: s.TableName,
InterfaceName: interfaceInfo.Name,
Package: getPackageName(interfaceInfo.Package),
Expand Down
4 changes: 4 additions & 0 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ func (p *Param) IsGenM() bool {
return p.Package == "gen" && p.Type == "M"
}

func (p *Param) IsGenR() bool {
return p.Package == "gen" && p.Type == "R"
}

func (p *Param) IsMap() bool {
return strings.HasPrefix(p.Type, "map[")
}
Expand Down
6 changes: 4 additions & 2 deletions internal/template/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ func ({{.S}} {{.TargetStruct}}Do){{.MethodName}}({{.GetParamInTmpl}})({{.GetResu
{{end}}
{{if .HasNeedNewResult}}result ={{if .ResultData.IsMap}}make{{else}}new{{end}}({{if ne .ResultData.Package ""}}{{.ResultData.Package}}.{{end}}{{.ResultData.Type}}){{end}}
{{.ExecuteResult}} = {{.S}}.UnderlyingDB().{{.GormOption}}(generateSQL{{if .HasSqlData}},params{{end}}){{if not .ResultData.IsNull}}.{{.GormRunMethodName}}({{if .HasGotPoint}}&{{end}}{{.ResultData.Name}}){{end}}.Error
return
{{if or .ReturnRowsAffected .ReturnError}}executeSQL:{{else}}_{{end}}= {{.S}}.UnderlyingDB().{{.GormOption}}(generateSQL{{if .HasSqlData}},params{{end}}){{if not .ResultData.IsNull}}.{{.GormRunMethodName}}({{if .HasGotPoint}}&{{end}}{{.ResultData.Name}}){{end}}
{{if .ReturnRowsAffected}}rowsAffected = executeSQL.RowsAffected
{{end}}{{if .ReturnError}}err = executeSQL.Error
{{end}}return
}
`
Expand Down

0 comments on commit eb1ac5e

Please sign in to comment.