Skip to content

Commit

Permalink
u-mapper gen: if time field -> add start and end selective
Browse files Browse the repository at this point in the history
  • Loading branch information
chenwenjie committed Feb 3, 2021
1 parent e30d080 commit 5c1f273
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
mctl.exe
mctl
mctl
.idea
44 changes: 41 additions & 3 deletions gen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,45 @@ import (
func genFields(fields []parser.Field) (string, error) {
var list []string
for _, field := range fields {
result, err := genField(field)

if strings.Contains(field.DataType, "NullTime") {

name := "start_" + field.Name.Source()
startField := parser.Field{
Name: stringx.From(name),
DataBaseType: field.DataBaseType,
DataType: field.DataType,
IsPrimaryKey: field.IsPrimaryKey,
IsUniqueKey: field.IsUniqueKey,
Comment: field.Comment,
}

result, err := genField(startField, false)
if err != nil {
return "", err
}

list = append(list, result)

endName := "end_" + field.Name.Source()
endField := parser.Field{
Name: stringx.From(endName),
DataBaseType: field.DataBaseType,
DataType: field.DataType,
IsPrimaryKey: field.IsPrimaryKey,
IsUniqueKey: field.IsUniqueKey,
Comment: field.Comment,
}

endResult, err := genField(endField, false)
if err != nil {
return "", err
}

list = append(list, endResult)
}

result, err := genField(field, true)
if err != nil {
return "", err
}
Expand All @@ -22,8 +60,8 @@ func genFields(fields []parser.Field) (string, error) {
return strings.Join(list, "\n"), nil
}

func genField(field parser.Field) (string, error) {
tag, err := genTag(field.Name.Source(), stringx.From(field.Name.ToCamel()).Untitle())
func genField(field parser.Field, isDBField bool) (string, error) {
tag, err := genTag(field.Name.Source(), stringx.From(field.Name.ToCamel()).Untitle(), isDBField)
if err != nil {
return "", err
}
Expand Down
29 changes: 29 additions & 0 deletions gen/findselective.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,39 @@ func genFindSelective(table Table, withCache bool) (string, string, string, erro
continue
}

if strings.Contains(field.DataType, "NullTime") {
findIfFieldValueOutput, err := util.With("findSelectiveIfFieldValue").
Parse(text).
Execute(map[string]interface{}{
"field": field.Name.Source(),
"mark": ">=",
"value": "Start" + field.Name.ToCamel(),
})
if err != nil {
return "", "", "", err
}

ifValues = append(ifValues, findIfFieldValueOutput.String())

findIfFieldValueOutput, err = util.With("findSelectiveIfFieldValue").
Parse(text).
Execute(map[string]interface{}{
"field": field.Name.Source(),
"mark": "<![CDATA[<=]]>",
"value": "End" + field.Name.ToCamel(),
})
if err != nil {
return "", "", "", err
}

ifValues = append(ifValues, findIfFieldValueOutput.String())
}

findIfFieldValueOutput, err := util.With("findSelectiveIfFieldValue").
Parse(text).
Execute(map[string]interface{}{
"field": field.Name.Source(),
"mark": "=",
"value": field.Name.ToCamel(),
})
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions gen/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/wenj91/mctl/template"
)

func genTag(in string, jsonIn string) (string, error) {
func genTag(in string, jsonIn string, isDBField bool) (string, error) {
if in == "" {
return in, nil
}
Expand All @@ -17,8 +17,9 @@ func genTag(in string, jsonIn string) (string, error) {
output, err := util.With("tag").
Parse(text).
Execute(map[string]interface{}{
"field": in,
"json": jsonIn,
"field": in,
"isDBField": isDBField,
"json": jsonIn,
})
if err != nil {
return "", err
Expand Down
4 changes: 2 additions & 2 deletions template/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func (r *{{.upperStartCamelObject}}FindResult) One() *{{.upperStartCamelObject}}
}
`

var FindSelectiveIfFieldValue = ` <if test="{{.value}} != nil and {{.value}} != ''">
and {{.field}} = #{{print "{" .value print "}"}}
var FindSelectiveIfFieldValue = ` <if test="{{.value}} != nil">
and {{.field}} {{.mark}} #{{print "{" .value print "}"}}
</if>
`

Expand Down
4 changes: 2 additions & 2 deletions template/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func (m *default{{.upperStartCamelObject}}Model) InsertSelective(data *{{.upperS
}
`

var InsertIfField = ` <if test="{{.value}} != nil and {{.value}} != ''">
var InsertIfField = ` <if test="{{.value}} != nil">
{{.field}},
</if>
`

var InsertIfValue = ` <if test="{{.value}} != nil and {{.value}} != ''">
var InsertIfValue = ` <if test="{{.value}} != nil">
#{{print "{" .value print "}"}},
</if>
`
Expand Down
2 changes: 1 addition & 1 deletion template/tag.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package template

var Tag = "`field:\"{{.field}}\" json:\"{{.json}}\"`"
var Tag = "`{{if .isDBField}}field:\"{{.field}}\" {{end}}json:\"{{.json}}\"`"
2 changes: 1 addition & 1 deletion template/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (m *default{{.upperStartCamelObject}}Model) UpdateSelective(data *{{.upperS

var UpdateFieldValue = `{{.field}} = #{{print "{" .value print "}"}}`

var UpdateIfFieldValue = ` <if test="{{.value}} != nil and {{.value}} != ''">
var UpdateIfFieldValue = ` <if test="{{.value}} != nil">
{{.field}} = #{{print "{" .value print "}"}},
</if>
`
Expand Down

0 comments on commit 5c1f273

Please sign in to comment.