go-zero/tools/goctl/model/sql/gen/delete.go

71 lines
2.2 KiB
Go
Raw Normal View History

2020-07-29 17:11:41 +08:00
package gen
import (
"sort"
2020-07-29 17:11:41 +08:00
"strings"
"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
)
func genDelete(table Table, withCache, postgreSql bool) (string, string, error) {
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
}
keys := keySet.KeysStr()
sort.Strings(keys)
keyVars := keyVariableSet.KeysStr()
sort.Strings(keyVars)
camel := table.Name.ToCamel()
text, err := pathx.LoadTemplate(category, deleteTemplateFile, template.Delete)
if err != nil {
return "", "", err
}
output, err := util.With("delete").
Parse(text).
Execute(map[string]interface{}{
"upperStartCamelObject": camel,
"withCache": withCache,
2021-03-01 17:29:07 +08:00
"containsIndexCache": table.ContainsUniqueCacheKey,
"lowerStartCamelPrimaryKey": util.EscapeGolangKeyword(stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle()),
"dataType": table.PrimaryKey.DataType,
"keys": strings.Join(keys, "\n"),
"originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source(), postgreSql),
"keyValues": strings.Join(keyVars, ", "),
"postgreSql": postgreSql,
"data": table,
})
2020-07-29 17:11:41 +08:00
if err != nil {
return "", "", err
2020-07-29 17:11:41 +08:00
}
// interface method
text, err = pathx.LoadTemplate(category, deleteMethodTemplateFile, template.DeleteMethod)
if err != nil {
return "", "", err
}
deleteMethodOut, err := util.With("deleteMethod").
Parse(text).
Execute(map[string]interface{}{
"lowerStartCamelPrimaryKey": util.EscapeGolangKeyword(stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle()),
"dataType": table.PrimaryKey.DataType,
"data": table,
})
if err != nil {
return "", "", err
}
2021-01-14 13:24:24 +08:00
return output.String(), deleteMethodOut.String(), nil
2020-07-29 17:11:41 +08:00
}