delete.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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, 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. output, err := util.With("delete").
  22. Parse(template.Delete).
  23. Execute(map[string]interface{}{
  24. "upperStartCamelObject": camel,
  25. "withCache": withCache,
  26. "containsIndexCache": table.ContainsUniqueKey,
  27. "lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).UnTitle(),
  28. "dataType": table.PrimaryKey.DataType,
  29. "keys": strings.Join(keySet.KeysStr(), "\n"),
  30. "originalPrimaryKey": table.PrimaryKey.Name.Source(),
  31. "keyValues": strings.Join(keyVariableSet.KeysStr(), ", "),
  32. })
  33. if err != nil {
  34. return "", err
  35. }
  36. return output.String(), nil
  37. }