delete.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package gen
  2. import (
  3. "strings"
  4. "github.com/tal-tech/go-zero/core/collection"
  5. "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
  6. "github.com/tal-tech/go-zero/tools/goctl/util"
  7. "github.com/tal-tech/go-zero/tools/goctl/util/stringx"
  8. )
  9. func genDelete(table Table, withCache bool) (string, string, error) {
  10. keySet := collection.NewSet()
  11. keyVariableSet := collection.NewSet()
  12. for fieldName, key := range table.CacheKey {
  13. if fieldName == table.PrimaryKey.Name.Source() {
  14. keySet.AddStr(key.KeyExpression)
  15. } else {
  16. keySet.AddStr(key.DataKeyExpression)
  17. }
  18. keyVariableSet.AddStr(key.Variable)
  19. }
  20. camel := table.Name.ToCamel()
  21. text, err := util.LoadTemplate(category, deleteTemplateFile, template.Delete)
  22. if err != nil {
  23. return "", "", err
  24. }
  25. output, err := util.With("delete").
  26. Parse(text).
  27. Execute(map[string]interface{}{
  28. "upperStartCamelObject": camel,
  29. "withCache": withCache,
  30. "containsIndexCache": table.ContainsUniqueKey,
  31. "lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
  32. "dataType": table.PrimaryKey.DataType,
  33. "keys": strings.Join(keySet.KeysStr(), "\n"),
  34. "originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source()),
  35. "keyValues": strings.Join(keyVariableSet.KeysStr(), ", "),
  36. })
  37. if err != nil {
  38. return "", "", err
  39. }
  40. // interface method
  41. text, err = util.LoadTemplate(category, deleteMethodTemplateFile, template.DeleteMethod)
  42. if err != nil {
  43. return "", "", err
  44. }
  45. deleteMethodOut, err := util.With("deleteMethod").
  46. Parse(text).
  47. Execute(map[string]interface{}{
  48. "lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
  49. "dataType": table.PrimaryKey.DataType,
  50. })
  51. if err != nil {
  52. return "", "", err
  53. }
  54. return output.String(), deleteMethodOut.String(), nil
  55. }