Skip to content

Commit

Permalink
u-update model
Browse files Browse the repository at this point in the history
  • Loading branch information
chenwenjie committed Jan 15, 2021
1 parent 0ffaab7 commit 6710a1e
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 21 deletions.
32 changes: 27 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ mctl model 为go-zero生成github.com/wenj91/gobatis模板代码工具,主要
* 生成代码示例

```go
package model
import (
"encoding/json"
"time"
Expand All @@ -45,13 +43,17 @@ mctl model 为go-zero生成github.com/wenj91/gobatis模板代码工具,主要
)
type (
TestUserInfoFindResult struct {
testUserInfos []*TestUserInfo
}
TestUserInfoModel interface {
WithConn(conn gobatis.GoBatis) TestUserInfoModel
Insert(data *TestUserInfo) (id int64, affected int64, err error)
InsertSelective(data *TestUserInfo) (id int64, affected int64, err error)
FindOne(id int64) (*TestUserInfo, error)
FindOneByNanosecond(nanosecond int64) (*TestUserInfo, error)
FindSelective(data *TestUserInfo) ([]*TestUserInfo, error)
FindSelective(data *TestUserInfo) (*TestUserInfoFindResult, error)
Update(data *TestUserInfo) (affected int64, err error)
UpdateSelective(data *TestUserInfo) (affected int64, err error)
Delete(id int64) (affected int64, err error)
Expand Down Expand Up @@ -83,6 +85,24 @@ mctl model 为go-zero生成github.com/wenj91/gobatis模板代码工具,主要
return str
}
func newTestUserInfoFindResult(testUserInfos []*TestUserInfo) *TestUserInfoFindResult {
return &TestUserInfoFindResult{
testUserInfos: testUserInfos,
}
}
func (r *TestUserInfoFindResult) List() []*TestUserInfo {
return r.testUserInfos
}
func (r *TestUserInfoFindResult) One() *TestUserInfo {
if len(r.testUserInfos) == 0 {
return nil
}
return r.testUserInfos[0]
}
func NewTestUserInfoModel(conn gobatis.GoBatis) TestUserInfoModel {
return &defaultTestUserInfoModel{
conn: conn,
Expand Down Expand Up @@ -127,10 +147,12 @@ mctl model 为go-zero生成github.com/wenj91/gobatis模板代码工具,主要
return resp, err
}
func (m *defaultTestUserInfoModel) FindSelective(data *TestUserInfo) ([]*TestUserInfo, error) {
func (m *defaultTestUserInfoModel) FindSelective(data *TestUserInfo) (*TestUserInfoFindResult, error) {
resp := make([]*TestUserInfo, 0)
err := m.conn.Select(m.method("findSelective"), data)(&resp)
return resp, err
return &TestUserInfoFindResult{
testUserInfos: resp,
}, err
}
func (m *defaultTestUserInfoModel) Update(data *TestUserInfo) (affected int64, err error) {
Expand Down
33 changes: 33 additions & 0 deletions gen/findselectiveresult.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package gen

import (
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
"github.com/wenj91/mctl/template"
)

func genFindSelectiveResult(table Table, withCache bool) (string, error) {
camel := table.Name.ToCamel()
text, err := util.LoadTemplate(category, findSelectiveResultMethodTemplateFile, template.FindSelectiveResultMethod)
if err != nil {
return "", err
}

output, err := util.With("findSelectiveResultMethod").
Parse(text).
Execute(map[string]interface{}{
"withCache": withCache,
"upperStartCamelObject": camel,
"lowerStartCamelObject": stringx.From(camel).Untitle(),
"originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source()),
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
"dataType": table.PrimaryKey.DataType,
"cacheKey": table.CacheKey[table.PrimaryKey.Name.Source()].KeyExpression,
"cacheKeyVariable": table.CacheKey[table.PrimaryKey.Name.Source()].Variable,
})
if err != nil {
return "", err
}

return output.String(), nil
}
32 changes: 19 additions & 13 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, st
return "", "", err
}

findSelectiveResultCode, err := genFindSelectiveResult(table, withCache)
if err != nil {
return "", "", err
}

withConnCode, withConnMethodCode, err := genWithConn(table)
if err != nil {
return "", "", err
Expand Down Expand Up @@ -339,19 +344,20 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, st
}

output, err := t.Execute(map[string]interface{}{
"pkg": g.pkg,
"imports": importsCode,
"vars": varsCode,
"types": typesCode,
"new": newCode,
"toString": toStringCode,
"method": methodCode,
"withConn": withConnCode,
"insert": strings.Join(insertCodes, "\n"),
"find": strings.Join(findCode, "\n"),
"update": strings.Join(updateCodes, "\n"),
"delete": deleteCode,
"extraMethod": ret.cacheExtra,
"pkg": g.pkg,
"imports": importsCode,
"vars": varsCode,
"types": typesCode,
"new": newCode,
"toString": toStringCode,
"findSelectiveResultCode": findSelectiveResultCode,
"method": methodCode,
"withConn": withConnCode,
"insert": strings.Join(insertCodes, "\n"),
"find": strings.Join(findCode, "\n"),
"update": strings.Join(updateCodes, "\n"),
"delete": deleteCode,
"extraMethod": ret.cacheExtra,
})
if err != nil {
return "", "", err
Expand Down
2 changes: 2 additions & 0 deletions gen/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
findSelectiveMapperTemplateFile = "mapper-find-selective.tpl"
findSelectiveMethodTemplateFile = "interface-find-selective.tpl"
findSelectiveIfFieldValuesTemplateFile = "if-field-value-find-selective.tpl"
findSelectiveResultMethodTemplateFile = "result-find-selective.tpl"
importsTemplateFile = "import.tpl"
importsWithNoCacheTemplateFile = "import-no-cache.tpl"
insertTemplateFile = "insert.tpl"
Expand Down Expand Up @@ -63,6 +64,7 @@ var templates = map[string]string{
findSelectiveMapperTemplateFile: template.FindSelectiveMapper,
findSelectiveMethodTemplateFile: template.FindSelectiveMethod,
findSelectiveIfFieldValuesTemplateFile: template.FindSelectiveIfFieldValue,
findSelectiveResultMethodTemplateFile: template.FindSelectiveResultMethod,
fieldTemplateFile: template.Field,
findOneTemplateFile: template.FindOne,
findOneMethodTemplateFile: template.FindOneMethod,
Expand Down
2 changes: 2 additions & 0 deletions gen/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gen

import (
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
"github.com/wenj91/mctl/template"
)

Expand All @@ -22,6 +23,7 @@ func genTypes(table Table, methods string, withCache bool) (string, error) {
Execute(map[string]interface{}{
"withCache": withCache,
"method": methods,
"lowerStartCamelObject": stringx.From(table.Name.ToCamel()).Untitle(),
"upperStartCamelObject": table.Name.ToCamel(),
"fields": fieldsString,
})
Expand Down
28 changes: 25 additions & 3 deletions template/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,32 @@ var FindOneMapper = `
`

var FindSelective = `
func (m *default{{.upperStartCamelObject}}Model) FindSelective(data *{{.upperStartCamelObject}}) ([]*{{.upperStartCamelObject}}, error) {
func (m *default{{.upperStartCamelObject}}Model) FindSelective(data *{{.upperStartCamelObject}}) (*{{.upperStartCamelObject}}FindResult, error) {
resp := make([]*{{.upperStartCamelObject}}, 0)
err := m.conn.Select(m.method("findSelective"), data)(&resp)
return resp, err
return &{{.upperStartCamelObject}}FindResult{
{{.lowerStartCamelObject}}s: resp,
}, err
}
`

var FindSelectiveResultMethod = `
func new{{.upperStartCamelObject}}FindResult({{.lowerStartCamelObject}}s []*{{.upperStartCamelObject}}) *{{.upperStartCamelObject}}FindResult {
return &{{.upperStartCamelObject}}FindResult{
{{.lowerStartCamelObject}}s: {{.lowerStartCamelObject}}s,
}
}
func (r *{{.upperStartCamelObject}}FindResult) List() []*{{.upperStartCamelObject}} {
return r.{{.lowerStartCamelObject}}s
}
func (r *{{.upperStartCamelObject}}FindResult) One() *{{.upperStartCamelObject}} {
if len(r.{{.lowerStartCamelObject}}s) == 0 {
return nil
}
return r.{{.lowerStartCamelObject}}s[0]
}
`

Expand Down Expand Up @@ -78,4 +100,4 @@ func (m *default{{.upperStartCamelObject}}Model) queryPrimary(conn sqlx.SqlConn,

var FindOneMethod = `FindOne({{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error) `
var FindOneByFieldMethod = `FindOneBy{{.upperField}}({{.in}}) (*{{.upperStartCamelObject}}, error) `
var FindSelectiveMethod = `FindSelective(data *{{.upperStartCamelObject}}) ([]*{{.upperStartCamelObject}}, error) `
var FindSelectiveMethod = `FindSelective(data *{{.upperStartCamelObject}}) (*{{.upperStartCamelObject}}FindResult, error) `
1 change: 1 addition & 0 deletions template/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var Model = `package {{.pkg}}
{{.imports}}
{{.types}}
{{.toString}}
{{.findSelectiveResultCode}}
{{.new}}
{{.method}}
{{.withConn}}
Expand Down
4 changes: 4 additions & 0 deletions template/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package template

var Types = `
type (
{{.upperStartCamelObject}}FindResult struct {
{{.lowerStartCamelObject}}s []*{{.upperStartCamelObject}}
}
{{.upperStartCamelObject}}Model interface{
{{.method}}
}
Expand Down

0 comments on commit 6710a1e

Please sign in to comment.