2020-07-29 17:11:41 +08:00
|
|
|
package gen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2020-08-19 10:41:19 +08:00
|
|
|
"github.com/tal-tech/go-zero/core/collection"
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
|
2020-10-19 23:13:18 +08:00
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
2022-01-03 21:32:40 +08:00
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
|
2020-08-19 10:41:19 +08:00
|
|
|
"github.com/tal-tech/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
|
|
|
}
|
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,
|
2020-12-08 23:01:25 +08:00
|
|
|
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
|
2020-08-19 10:41:19 +08:00
|
|
|
"dataType": table.PrimaryKey.DataType,
|
|
|
|
"keys": strings.Join(keySet.KeysStr(), "\n"),
|
2021-07-23 11:45:15 +08:00
|
|
|
"originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql),
|
2020-08-19 10:41:19 +08:00
|
|
|
"keyValues": strings.Join(keyVariableSet.KeysStr(), ", "),
|
2021-07-23 11:45:15 +08:00
|
|
|
"postgreSql": postgreSql,
|
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{}{
|
2020-12-08 23:01:25 +08:00
|
|
|
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
|
2020-11-24 22:36:23 +08:00
|
|
|
"dataType": table.PrimaryKey.DataType,
|
|
|
|
})
|
|
|
|
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
|
|
|
}
|