2020-07-29 17:11:41 +08:00
|
|
|
package gen
|
|
|
|
|
|
|
|
import (
|
2022-04-09 00:25:23 +08:00
|
|
|
"sort"
|
2020-07-29 17:11:41 +08:00
|
|
|
"strings"
|
|
|
|
|
2022-01-25 23:15:07 +08:00
|
|
|
"github.com/zeromicro/go-zero/core/collection"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/model/sql/template"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/util"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/util/stringx"
|
2020-07-29 17:11:41 +08:00
|
|
|
)
|
|
|
|
|
2022-05-23 09:12:06 +08:00
|
|
|
func genUpdate(table Table, withCache, postgreSql bool) (
|
|
|
|
string, string, error) {
|
2020-07-29 17:11:41 +08:00
|
|
|
expressionValues := make([]string, 0)
|
2022-05-23 09:12:06 +08:00
|
|
|
var pkg = "data."
|
|
|
|
if table.ContainsUniqueCacheKey {
|
|
|
|
pkg = "newData."
|
|
|
|
}
|
2021-01-13 18:07:31 +08:00
|
|
|
for _, field := range table.Fields {
|
2022-04-11 10:11:40 +08:00
|
|
|
camel := util.SafeString(field.Name.ToCamel())
|
2020-08-19 10:41:19 +08:00
|
|
|
if camel == "CreateTime" || camel == "UpdateTime" {
|
2020-07-29 17:11:41 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-01-14 13:24:24 +08:00
|
|
|
|
2021-03-01 17:29:07 +08:00
|
|
|
if field.Name.Source() == table.PrimaryKey.Name.Source() {
|
2020-08-19 10:41:19 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-01-14 13:24:24 +08:00
|
|
|
|
2022-05-23 09:12:06 +08:00
|
|
|
expressionValues = append(expressionValues, pkg+camel)
|
2020-07-29 17:11:41 +08:00
|
|
|
}
|
2021-01-14 13:24:24 +08:00
|
|
|
|
2021-03-12 17:49:28 +08:00
|
|
|
keySet := collection.NewSet()
|
|
|
|
keyVariableSet := collection.NewSet()
|
|
|
|
keySet.AddStr(table.PrimaryCacheKey.DataKeyExpression)
|
|
|
|
keyVariableSet.AddStr(table.PrimaryCacheKey.KeyLeft)
|
|
|
|
for _, key := range table.UniqueCacheKey {
|
|
|
|
keySet.AddStr(key.DataKeyExpression)
|
|
|
|
keyVariableSet.AddStr(key.KeyLeft)
|
|
|
|
}
|
2022-04-09 00:25:23 +08:00
|
|
|
keys := keySet.KeysStr()
|
|
|
|
sort.Strings(keys)
|
|
|
|
keyVars := keyVariableSet.KeysStr()
|
|
|
|
sort.Strings(keyVars)
|
2021-03-12 17:49:28 +08:00
|
|
|
|
2021-09-05 22:27:59 +08:00
|
|
|
if postgreSql {
|
2022-05-23 09:12:06 +08:00
|
|
|
expressionValues = append(
|
|
|
|
[]string{pkg + table.PrimaryKey.Name.ToCamel()},
|
|
|
|
expressionValues...,
|
|
|
|
)
|
2021-09-05 22:27:59 +08:00
|
|
|
} else {
|
2022-05-23 09:12:06 +08:00
|
|
|
expressionValues = append(
|
|
|
|
expressionValues, pkg+table.PrimaryKey.Name.ToCamel(),
|
|
|
|
)
|
2021-09-05 22:27:59 +08:00
|
|
|
}
|
2020-08-20 10:29:18 +08:00
|
|
|
camelTableName := table.Name.ToCamel()
|
2022-01-03 21:32:40 +08:00
|
|
|
text, err := pathx.LoadTemplate(category, updateTemplateFile, template.Update)
|
2020-10-21 14:59:35 +08:00
|
|
|
if err != nil {
|
2020-11-24 22:36:23 +08:00
|
|
|
return "", "", err
|
2020-10-21 14:59:35 +08:00
|
|
|
}
|
|
|
|
|
2022-05-23 09:12:06 +08:00
|
|
|
output, err := util.With("update").Parse(text).Execute(
|
|
|
|
map[string]interface{}{
|
2020-08-19 10:41:19 +08:00
|
|
|
"withCache": withCache,
|
2022-05-23 09:12:06 +08:00
|
|
|
"containsIndexCache": table.ContainsUniqueCacheKey,
|
2020-08-19 10:41:19 +08:00
|
|
|
"upperStartCamelObject": camelTableName,
|
2022-04-09 00:25:23 +08:00
|
|
|
"keys": strings.Join(keys, "\n"),
|
|
|
|
"keyValues": strings.Join(keyVars, ", "),
|
2021-03-01 17:29:07 +08:00
|
|
|
"primaryCacheKey": table.PrimaryCacheKey.DataKeyExpression,
|
|
|
|
"primaryKeyVariable": table.PrimaryCacheKey.KeyLeft,
|
2020-12-08 23:01:25 +08:00
|
|
|
"lowerStartCamelObject": stringx.From(camelTableName).Untitle(),
|
2022-05-23 09:12:06 +08:00
|
|
|
"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,
|
|
|
|
},
|
|
|
|
)
|
2020-07-29 17:11:41 +08:00
|
|
|
if err != nil {
|
2020-11-24 22:36:23 +08:00
|
|
|
return "", "", nil
|
2020-07-29 17:11:41 +08:00
|
|
|
}
|
2020-10-21 14:59:35 +08:00
|
|
|
|
2020-11-24 22:36:23 +08:00
|
|
|
// update interface method
|
2022-01-03 21:32:40 +08:00
|
|
|
text, err = pathx.LoadTemplate(category, updateMethodTemplateFile, template.UpdateMethod)
|
2020-11-24 22:36:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2022-05-23 09:12:06 +08:00
|
|
|
updateMethodOutput, err := util.With("updateMethod").Parse(text).Execute(
|
|
|
|
map[string]interface{}{
|
2020-11-24 22:36:23 +08:00
|
|
|
"upperStartCamelObject": camelTableName,
|
2022-01-18 10:36:38 +08:00
|
|
|
"data": table,
|
2022-05-23 09:12:06 +08:00
|
|
|
},
|
|
|
|
)
|
2020-11-24 22:36:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return output.String(), updateMethodOutput.String(), nil
|
2020-07-29 17:11:41 +08:00
|
|
|
}
|