delete.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. keySet.AddStr(table.PrimaryCacheKey.KeyExpression)
  13. keyVariableSet.AddStr(table.PrimaryCacheKey.KeyLeft)
  14. for _, key := range table.UniqueCacheKey {
  15. keySet.AddStr(key.DataKeyExpression)
  16. keyVariableSet.AddStr(key.KeyLeft)
  17. }
  18. camel := table.Name.ToCamel()
  19. text, err := util.LoadTemplate(category, deleteTemplateFile, template.Delete)
  20. if err != nil {
  21. return "", "", err
  22. }
  23. output, err := util.With("delete").
  24. Parse(text).
  25. Execute(map[string]interface{}{
  26. "upperStartCamelObject": camel,
  27. "withCache": withCache,
  28. "containsIndexCache": table.ContainsUniqueCacheKey,
  29. "lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
  30. "dataType": table.PrimaryKey.DataType,
  31. "keys": strings.Join(keySet.KeysStr(), "\n"),
  32. "originalPrimaryKey": wrapWithRawString(table.PrimaryKey.Name.Source()),
  33. "keyValues": strings.Join(keyVariableSet.KeysStr(), ", "),
  34. })
  35. if err != nil {
  36. return "", "", err
  37. }
  38. // interface method
  39. text, err = util.LoadTemplate(category, deleteMethodTemplateFile, template.DeleteMethod)
  40. if err != nil {
  41. return "", "", err
  42. }
  43. deleteMethodOut, err := util.With("deleteMethod").
  44. Parse(text).
  45. Execute(map[string]interface{}{
  46. "lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
  47. "dataType": table.PrimaryKey.DataType,
  48. })
  49. if err != nil {
  50. return "", "", err
  51. }
  52. return output.String(), deleteMethodOut.String(), nil
  53. }