diff --git a/.gitignore b/.gitignore index ad97fdd..0df6d18 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .vscode mctl.exe -mctl \ No newline at end of file +mctl +.idea \ No newline at end of file diff --git a/gen/field.go b/gen/field.go index 1b0f3ce..ad296a3 100644 --- a/gen/field.go +++ b/gen/field.go @@ -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 } @@ -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 } diff --git a/gen/findselective.go b/gen/findselective.go index e9afefc..3edfeb6 100644 --- a/gen/findselective.go +++ b/gen/findselective.go @@ -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": "", + "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 { diff --git a/gen/tag.go b/gen/tag.go index f11465d..c5a382d 100644 --- a/gen/tag.go +++ b/gen/tag.go @@ -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 } @@ -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 diff --git a/template/find.go b/template/find.go index f114a41..0fedacf 100644 --- a/template/find.go +++ b/template/find.go @@ -51,8 +51,8 @@ func (r *{{.upperStartCamelObject}}FindResult) One() *{{.upperStartCamelObject}} } ` -var FindSelectiveIfFieldValue = ` - and {{.field}} = #{{print "{" .value print "}"}} +var FindSelectiveIfFieldValue = ` + and {{.field}} {{.mark}} #{{print "{" .value print "}"}} ` diff --git a/template/insert.go b/template/insert.go index 70fca46..55949cd 100644 --- a/template/insert.go +++ b/template/insert.go @@ -14,12 +14,12 @@ func (m *default{{.upperStartCamelObject}}Model) InsertSelective(data *{{.upperS } ` -var InsertIfField = ` +var InsertIfField = ` {{.field}}, ` -var InsertIfValue = ` +var InsertIfValue = ` #{{print "{" .value print "}"}}, ` diff --git a/template/tag.go b/template/tag.go index 7c45eae..5bee7e4 100644 --- a/template/tag.go +++ b/template/tag.go @@ -1,3 +1,3 @@ package template -var Tag = "`field:\"{{.field}}\" json:\"{{.json}}\"`" +var Tag = "`{{if .isDBField}}field:\"{{.field}}\" {{end}}json:\"{{.json}}\"`" diff --git a/template/update.go b/template/update.go index f4e954f..ee30b67 100644 --- a/template/update.go +++ b/template/update.go @@ -16,7 +16,7 @@ func (m *default{{.upperStartCamelObject}}Model) UpdateSelective(data *{{.upperS var UpdateFieldValue = `{{.field}} = #{{print "{" .value print "}"}}` -var UpdateIfFieldValue = ` +var UpdateIfFieldValue = ` {{.field}} = #{{print "{" .value print "}"}}, `