diff --git a/tools/goctl/model/sql/example/sql/user.sql b/tools/goctl/model/sql/example/sql/user.sql index 48531dc3..6ad3b71d 100644 --- a/tools/goctl/model/sql/example/sql/user.sql +++ b/tools/goctl/model/sql/example/sql/user.sql @@ -29,6 +29,5 @@ CREATE TABLE `student` ) DEFAULT NULL, `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `update_time` timestamp NULL DEFAULT NULL, - PRIMARY KEY (`type`) USING BTREE, - UNIQUE KEY `class_name_index` (`class`,`name`) + PRIMARY KEY (`type`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; \ No newline at end of file diff --git a/tools/goctl/model/sql/gen/update.go b/tools/goctl/model/sql/gen/update.go index 189eea78..a4cee375 100644 --- a/tools/goctl/model/sql/gen/update.go +++ b/tools/goctl/model/sql/gen/update.go @@ -11,8 +11,13 @@ import ( "github.com/zeromicro/go-zero/tools/goctl/util/stringx" ) -func genUpdate(table Table, withCache, postgreSql bool) (string, string, error) { +func genUpdate(table Table, withCache, postgreSql bool) ( + string, string, error) { expressionValues := make([]string, 0) + var pkg = "data." + if table.ContainsUniqueCacheKey { + pkg = "newData." + } for _, field := range table.Fields { camel := util.SafeString(field.Name.ToCamel()) if camel == "CreateTime" || camel == "UpdateTime" { @@ -23,7 +28,7 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error) continue } - expressionValues = append(expressionValues, "data."+camel) + expressionValues = append(expressionValues, pkg+camel) } keySet := collection.NewSet() @@ -40,9 +45,14 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error) sort.Strings(keyVars) if postgreSql { - expressionValues = append([]string{"data." + table.PrimaryKey.Name.ToCamel()}, expressionValues...) + expressionValues = append( + []string{pkg + table.PrimaryKey.Name.ToCamel()}, + expressionValues..., + ) } else { - expressionValues = append(expressionValues, "data."+table.PrimaryKey.Name.ToCamel()) + expressionValues = append( + expressionValues, pkg+table.PrimaryKey.Name.ToCamel(), + ) } camelTableName := table.Name.ToCamel() text, err := pathx.LoadTemplate(category, updateTemplateFile, template.Update) @@ -50,21 +60,29 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error) return "", "", err } - output, err := util.With("update"). - Parse(text). - Execute(map[string]interface{}{ + output, err := util.With("update").Parse(text).Execute( + map[string]interface{}{ "withCache": withCache, + "containsIndexCache": table.ContainsUniqueCacheKey, "upperStartCamelObject": camelTableName, "keys": strings.Join(keys, "\n"), "keyValues": strings.Join(keyVars, ", "), "primaryCacheKey": table.PrimaryCacheKey.DataKeyExpression, "primaryKeyVariable": table.PrimaryCacheKey.KeyLeft, "lowerStartCamelObject": stringx.From(camelTableName).Untitle(), - "originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql), - "expressionValues": strings.Join(expressionValues, ", "), - "postgreSql": postgreSql, - "data": table, - }) + "upperStartCamelPrimaryKey": util.EscapeGolangKeyword( + stringx.From(table.PrimaryKey.Name.ToCamel()).Title(), + ), + "originalPrimaryKey": wrapWithRawString( + table.PrimaryKey.Name.Source(), postgreSql, + ), + "expressionValues": strings.Join( + expressionValues, ", ", + ), + "postgreSql": postgreSql, + "data": table, + }, + ) if err != nil { return "", "", nil } @@ -75,12 +93,12 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error) return "", "", err } - updateMethodOutput, err := util.With("updateMethod"). - Parse(text). - Execute(map[string]interface{}{ + updateMethodOutput, err := util.With("updateMethod").Parse(text).Execute( + map[string]interface{}{ "upperStartCamelObject": camelTableName, "data": table, - }) + }, + ) if err != nil { return "", "", nil } diff --git a/tools/goctl/model/sql/template/update.go b/tools/goctl/model/sql/template/update.go index 24e172bd..d1fdbca1 100644 --- a/tools/goctl/model/sql/template/update.go +++ b/tools/goctl/model/sql/template/update.go @@ -3,9 +3,14 @@ package template const ( // Update defines a template for generating update codes Update = ` -func (m *default{{.upperStartCamelObject}}Model) Update(ctx context.Context, data *{{.upperStartCamelObject}}) error { - {{if .withCache}}{{.keys}} - _, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { +func (m *default{{.upperStartCamelObject}}Model) Update(ctx context.Context, {{if .containsIndexCache}}newData{{else}}data{{end}} *{{.upperStartCamelObject}}) error { + {{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne(ctx, newData.{{.upperStartCamelPrimaryKey}}) + if err!=nil{ + return err + } + +{{end}} {{.keys}} + _, {{if .containsIndexCache}}err{{else}}err:{{end}}= m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("update %s set %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table, {{.lowerStartCamelObject}}RowsWithPlaceHolder) return conn.ExecCtx(ctx, query, {{.expressionValues}}) }, {{.keyValues}}){{else}}query := fmt.Sprintf("update %s set %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}}", m.table, {{.lowerStartCamelObject}}RowsWithPlaceHolder) @@ -15,5 +20,5 @@ func (m *default{{.upperStartCamelObject}}Model) Update(ctx context.Context, dat ` // UpdateMethod defines an interface method template for generating update codes - UpdateMethod = `Update(ctx context.Context, data *{{.upperStartCamelObject}}) error` + UpdateMethod = `Update(ctx context.Context, newData *{{.upperStartCamelObject}}) error` ) diff --git a/tools/goctl/model/sql/template/vars.go b/tools/goctl/model/sql/template/vars.go index f5309cfa..e0758e77 100644 --- a/tools/goctl/model/sql/template/vars.go +++ b/tools/goctl/model/sql/template/vars.go @@ -3,16 +3,18 @@ package template import "fmt" // Vars defines a template for var block in model -var Vars = fmt.Sprintf(` +var Vars = fmt.Sprintf( + ` var ( {{.lowerStartCamelObject}}FieldNames = builder.RawFieldNames(&{{.upperStartCamelObject}}{}{{if .postgreSql}},true{{end}}) {{.lowerStartCamelObject}}Rows = strings.Join({{.lowerStartCamelObject}}FieldNames, ",") - {{.lowerStartCamelObject}}RowsExpectAutoSet = {{if .postgreSql}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, {{if .autoIncrement}}"{{.originalPrimaryKey}}",{{end}} "%screate_time%s", "%supdate_time%s"), ","){{else}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, {{if .autoIncrement}}"{{.originalPrimaryKey}}",{{end}} "%screate_time%s", "%supdate_time%s"), ","){{end}} - {{.lowerStartCamelObject}}RowsWithPlaceHolder = {{if .postgreSql}}builder.PostgreSqlJoin(stringx.Remove({{.lowerStartCamelObject}}FieldNames, "{{.originalPrimaryKey}}", "%screate_time%s", "%supdate_time%s")){{else}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, "{{.originalPrimaryKey}}", "%screate_time%s", "%supdate_time%s"), "=?,") + "=?"{{end}} + {{.lowerStartCamelObject}}RowsExpectAutoSet = {{if .postgreSql}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, {{if .autoIncrement}}"{{.originalPrimaryKey}}",{{end}} "%screate_time%s", "%supdate_time%s", "%screate_t%s", "%supdate_at%s"), ","){{else}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, {{if .autoIncrement}}"{{.originalPrimaryKey}}",{{end}} "%screate_time%s", "%supdate_time%s", "%screate_at%s", "%supdate_at%s"), ","){{end}} + {{.lowerStartCamelObject}}RowsWithPlaceHolder = {{if .postgreSql}}builder.PostgreSqlJoin(stringx.Remove({{.lowerStartCamelObject}}FieldNames, "{{.originalPrimaryKey}}", "%screate_time%s", "%supdate_time%s", "%screate_at%s", "%supdate_at%s")){{else}}strings.Join(stringx.Remove({{.lowerStartCamelObject}}FieldNames, "{{.originalPrimaryKey}}", "%screate_time%s", "%supdate_time%s", "%screate_at%s", "%supdate_at%s"), "=?,") + "=?"{{end}} {{if .withCache}}{{.cacheKeys}}{{end}} ) -`, "", "", "", "", // postgreSql mode - "`", "`", "`", "`", - "", "", "", "", // postgreSql mode - "`", "`", "`", "`") +`, "", "", "", "", "", "", "", "", // postgreSql mode + "`", "`", "`", "`", "`", "`", "`", "`", + "", "", "", "", "", "", "", "", // postgreSql mode + "`", "`", "`", "`", "`", "`", "`", "`", +)