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
|
|
|
)
|
|
|
|
|
2021-07-23 11:45:15 +08:00
|
|
|
func genDelete(table Table, withCache, postgreSql bool) (string, string, error) {
|
2020-08-19 10:41:19 +08:00
|
|
|
keySet := collection.NewSet()
|
|
|
|
keyVariableSet := collection.NewSet()
|
2021-03-01 17:29:07 +08:00
|
|
|
keySet.AddStr(table.PrimaryCacheKey.KeyExpression)
|
|
|
|
keyVariableSet.AddStr(table.PrimaryCacheKey.KeyLeft)
|
|
|
|
for _, key := range table.UniqueCacheKey {
|
|
|
|
keySet.AddStr(key.DataKeyExpression)
|
|
|
|
keyVariableSet.AddStr(key.KeyLeft)
|
2020-07-29 17:11:41 +08:00
|
|
|
}
|
2022-04-09 00:25:23 +08:00
|
|
|
keys := keySet.KeysStr()
|
|
|
|
sort.Strings(keys)
|
|
|
|
keyVars := keyVariableSet.KeysStr()
|
|
|
|
sort.Strings(keyVars)
|
2020-10-11 21:55:44 +08:00
|
|
|
|
2020-08-20 10:29:18 +08:00
|
|
|
camel := table.Name.ToCamel()
|
2022-01-03 21:32:40 +08:00
|
|
|
text, err := pathx.LoadTemplate(category, deleteTemplateFile, template.Delete)
|
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
|
|
|
}
|
|
|
|
|
2020-10-19 23:13:18 +08:00
|
|
|
output, err := util.With("delete").
|
2020-10-21 14:59:35 +08:00
|
|
|
Parse(text).
|
2020-08-19 10:41:19 +08:00
|
|
|
Execute(map[string]interface{}{
|
|
|
|
"upperStartCamelObject": camel,
|
|
|
|
"withCache": withCache,
|
2021-03-01 17:29:07 +08:00
|
|
|
"containsIndexCache": table.ContainsUniqueCacheKey,
|
2022-04-07 10:40:21 +08:00
|
|
|
"lowerStartCamelPrimaryKey": util.EscapeGolangKeyword(stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle()),
|
2020-08-19 10:41:19 +08:00
|
|
|
"dataType": table.PrimaryKey.DataType,
|
2022-04-09 00:25:23 +08:00
|
|
|
"keys": strings.Join(keys, "\n"),
|
2021-07-23 11:45:15 +08:00
|
|
|
"originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql),
|
2022-04-09 00:25:23 +08:00
|
|
|
"keyValues": strings.Join(keyVars, ", "),
|
2021-07-23 11:45:15 +08:00
|
|
|
"postgreSql": postgreSql,
|
2022-01-18 10:36:38 +08:00
|
|
|
"data": table,
|
2020-08-19 10:41:19 +08:00
|
|
|
})
|
2020-07-29 17:11:41 +08:00
|
|
|
if err != nil {
|
2020-11-24 22:36:23 +08:00
|
|
|
return "", "", err
|
2020-07-29 17:11:41 +08:00
|
|
|
}
|
2020-11-18 15:32:53 +08:00
|
|
|
|
2020-11-24 22:36:23 +08:00
|
|
|
// interface method
|
2022-01-03 21:32:40 +08:00
|
|
|
text, err = pathx.LoadTemplate(category, deleteMethodTemplateFile, template.DeleteMethod)
|
2020-11-24 22:36:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteMethodOut, err := util.With("deleteMethod").
|
|
|
|
Parse(text).
|
|
|
|
Execute(map[string]interface{}{
|
2022-04-07 10:40:21 +08:00
|
|
|
"lowerStartCamelPrimaryKey": util.EscapeGolangKeyword(stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle()),
|
2020-11-24 22:36:23 +08:00
|
|
|
"dataType": table.PrimaryKey.DataType,
|
2022-01-18 10:36:38 +08:00
|
|
|
"data": table,
|
2020-11-24 22:36:23 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2021-01-14 13:24:24 +08:00
|
|
|
|
2020-11-24 22:36:23 +08:00
|
|
|
return output.String(), deleteMethodOut.String(), nil
|
2020-07-29 17:11:41 +08:00
|
|
|
}
|