diff --git a/tools/goctl/model/sql/gen/delete.go b/tools/goctl/model/sql/gen/delete.go index 47e9fb31..9f401fb4 100644 --- a/tools/goctl/model/sql/gen/delete.go +++ b/tools/goctl/model/sql/gen/delete.go @@ -38,6 +38,7 @@ func genDelete(table Table, withCache, postgreSql bool) (string, string, error) "originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql), "keyValues": strings.Join(keyVariableSet.KeysStr(), ", "), "postgreSql": postgreSql, + "data": table, }) if err != nil { return "", "", err @@ -54,6 +55,7 @@ func genDelete(table Table, withCache, postgreSql bool) (string, string, error) Execute(map[string]interface{}{ "lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(), "dataType": table.PrimaryKey.DataType, + "data": table, }) if err != nil { return "", "", err diff --git a/tools/goctl/model/sql/gen/field.go b/tools/goctl/model/sql/gen/field.go index bca2f17d..b4397dbe 100644 --- a/tools/goctl/model/sql/gen/field.go +++ b/tools/goctl/model/sql/gen/field.go @@ -9,11 +9,11 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) -func genFields(fields []*parser.Field) (string, error) { +func genFields(table Table, fields []*parser.Field) (string, error) { var list []string for _, field := range fields { - result, err := genField(field) + result, err := genField(table, field) if err != nil { return "", err } @@ -24,8 +24,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.NameOriginal) +func genField(table Table, field *parser.Field) (string, error) { + tag, err := genTag(table, field.NameOriginal) if err != nil { return "", err } @@ -43,6 +43,7 @@ func genField(field *parser.Field) (string, error) { "tag": tag, "hasComment": field.Comment != "", "comment": field.Comment, + "data": table, }) if err != nil { return "", err diff --git a/tools/goctl/model/sql/gen/findone.go b/tools/goctl/model/sql/gen/findone.go index a5acd62a..a4a0d0cd 100644 --- a/tools/goctl/model/sql/gen/findone.go +++ b/tools/goctl/model/sql/gen/findone.go @@ -26,6 +26,7 @@ func genFindOne(table Table, withCache, postgreSql bool) (string, string, error) "cacheKey": table.PrimaryCacheKey.KeyExpression, "cacheKeyVariable": table.PrimaryCacheKey.KeyLeft, "postgreSql": postgreSql, + "data": table, }) if err != nil { return "", "", err @@ -42,6 +43,7 @@ func genFindOne(table Table, withCache, postgreSql bool) (string, string, error) "upperStartCamelObject": camel, "lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(), "dataType": table.PrimaryKey.DataType, + "data": table, }) if err != nil { return "", "", err diff --git a/tools/goctl/model/sql/gen/findonebyfield.go b/tools/goctl/model/sql/gen/findonebyfield.go index 35373d9a..029eb498 100644 --- a/tools/goctl/model/sql/gen/findonebyfield.go +++ b/tools/goctl/model/sql/gen/findonebyfield.go @@ -40,6 +40,7 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e "upperStartCamelPrimaryKey": table.PrimaryKey.Name.ToCamel(), "originalField": originalFieldString, "postgreSql": postgreSql, + "data": table, }) if err != nil { return nil, err @@ -71,6 +72,7 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e "upperStartCamelObject": camelTableName, "upperField": key.FieldNameJoin.Camel().With("").Source(), "in": in, + "data": table, }) if err != nil { return nil, err @@ -91,6 +93,7 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e "lowerStartCamelObject": stringx.From(camelTableName).Untitle(), "originalPrimaryField": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql), "postgreSql": postgreSql, + "data": table, }) if err != nil { return nil, err diff --git a/tools/goctl/model/sql/gen/gen.go b/tools/goctl/model/sql/gen/gen.go index 8f2b50b6..940d48e4 100644 --- a/tools/goctl/model/sql/gen/gen.go +++ b/tools/goctl/model/sql/gen/gen.go @@ -217,17 +217,17 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er primaryKey, uniqueKey := genCacheKeys(in) - importsCode, err := genImports(withCache, in.ContainsTime()) - if err != nil { - return "", err - } - var table Table table.Table = in table.PrimaryCacheKey = primaryKey table.UniqueCacheKey = uniqueKey table.ContainsUniqueCacheKey = len(uniqueKey) > 0 + importsCode, err := genImports(withCache, in.ContainsTime(), table) + if err != nil { + return "", err + } + varsCode, err := genVars(table, withCache, g.isPostgreSql) if err != nil { return "", err @@ -284,7 +284,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er cacheExtra: ret.cacheExtra, } - output, err := g.executeModel(code) + output, err := g.executeModel(table, code) if err != nil { return "", err } @@ -292,7 +292,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er return output.String(), nil } -func (g *defaultGenerator) executeModel(code *code) (*bytes.Buffer, error) { +func (g *defaultGenerator) executeModel(table Table, code *code) (*bytes.Buffer, error) { text, err := pathx.LoadTemplate(category, modelTemplateFile, template.Model) if err != nil { return nil, err @@ -311,6 +311,7 @@ func (g *defaultGenerator) executeModel(code *code) (*bytes.Buffer, error) { "update": code.updateCode, "delete": code.deleteCode, "extraMethod": code.cacheExtra, + "data": table, }) if err != nil { return nil, err diff --git a/tools/goctl/model/sql/gen/imports.go b/tools/goctl/model/sql/gen/imports.go index a89cb21b..a458147a 100644 --- a/tools/goctl/model/sql/gen/imports.go +++ b/tools/goctl/model/sql/gen/imports.go @@ -6,7 +6,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) -func genImports(withCache, timeImport bool) (string, error) { +func genImports(withCache, timeImport bool, table Table) (string, error) { if withCache { text, err := pathx.LoadTemplate(category, importsTemplateFile, template.Imports) if err != nil { @@ -15,6 +15,7 @@ func genImports(withCache, timeImport bool) (string, error) { buffer, err := util.With("import").Parse(text).Execute(map[string]interface{}{ "time": timeImport, + "data": table, }) if err != nil { return "", err @@ -30,6 +31,7 @@ func genImports(withCache, timeImport bool) (string, error) { buffer, err := util.With("import").Parse(text).Execute(map[string]interface{}{ "time": timeImport, + "data": table, }) if err != nil { return "", err diff --git a/tools/goctl/model/sql/gen/insert.go b/tools/goctl/model/sql/gen/insert.go index c2bd2cf0..a8ecee43 100644 --- a/tools/goctl/model/sql/gen/insert.go +++ b/tools/goctl/model/sql/gen/insert.go @@ -62,6 +62,7 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error) "expressionValues": strings.Join(expressionValues, ", "), "keys": strings.Join(keySet.KeysStr(), "\n"), "keyValues": strings.Join(keyVariableSet.KeysStr(), ", "), + "data": table, }) if err != nil { return "", "", err @@ -75,6 +76,7 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error) insertMethodOutput, err := util.With("insertMethod").Parse(text).Execute(map[string]interface{}{ "upperStartCamelObject": camel, + "data": table, }) if err != nil { return "", "", err diff --git a/tools/goctl/model/sql/gen/new.go b/tools/goctl/model/sql/gen/new.go index fec03df3..fb58812f 100644 --- a/tools/goctl/model/sql/gen/new.go +++ b/tools/goctl/model/sql/gen/new.go @@ -25,6 +25,7 @@ func genNew(table Table, withCache, postgreSql bool) (string, error) { "table": t, "withCache": withCache, "upperStartCamelObject": table.Name.ToCamel(), + "data": table, }) if err != nil { return "", err diff --git a/tools/goctl/model/sql/gen/tag.go b/tools/goctl/model/sql/gen/tag.go index 612089ac..247b140b 100644 --- a/tools/goctl/model/sql/gen/tag.go +++ b/tools/goctl/model/sql/gen/tag.go @@ -6,7 +6,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) -func genTag(in string) (string, error) { +func genTag(table Table, in string) (string, error) { if in == "" { return in, nil } @@ -18,6 +18,7 @@ func genTag(in string) (string, error) { output, err := util.With("tag").Parse(text).Execute(map[string]interface{}{ "field": in, + "data": table, }) if err != nil { return "", err diff --git a/tools/goctl/model/sql/gen/types.go b/tools/goctl/model/sql/gen/types.go index 57d3a37f..c9f5363e 100644 --- a/tools/goctl/model/sql/gen/types.go +++ b/tools/goctl/model/sql/gen/types.go @@ -8,7 +8,7 @@ import ( func genTypes(table Table, methods string, withCache bool) (string, error) { fields := table.Fields - fieldsString, err := genFields(fields) + fieldsString, err := genFields(table, fields) if err != nil { return "", err } @@ -25,6 +25,7 @@ func genTypes(table Table, methods string, withCache bool) (string, error) { "method": methods, "upperStartCamelObject": table.Name.ToCamel(), "fields": fieldsString, + "data": table, }) if err != nil { return "", err diff --git a/tools/goctl/model/sql/gen/update.go b/tools/goctl/model/sql/gen/update.go index de2c6230..041c41c1 100644 --- a/tools/goctl/model/sql/gen/update.go +++ b/tools/goctl/model/sql/gen/update.go @@ -58,6 +58,7 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error) "originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql), "expressionValues": strings.Join(expressionValues, ", "), "postgreSql": postgreSql, + "data": table, }) if err != nil { return "", "", nil @@ -73,6 +74,7 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error) Parse(text). Execute(map[string]interface{}{ "upperStartCamelObject": camelTableName, + "data": table, }) if err != nil { return "", "", nil diff --git a/tools/goctl/model/sql/gen/vars.go b/tools/goctl/model/sql/gen/vars.go index 58deff06..8688da22 100644 --- a/tools/goctl/model/sql/gen/vars.go +++ b/tools/goctl/model/sql/gen/vars.go @@ -31,6 +31,7 @@ func genVars(table Table, withCache, postgreSql bool) (string, error) { "originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql), "withCache": withCache, "postgreSql": postgreSql, + "data": table, }) if err != nil { return "", err