fix: Useless delete cache logic in update (#1923)

* Fix bug: useless delete cache logic in update

* Format code
This commit is contained in:
anqiansong
2022-05-23 09:12:06 +08:00
committed by GitHub
parent ca88b69d24
commit 58787746db
4 changed files with 53 additions and 29 deletions

View File

@@ -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
}