update.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package gen
  2. import (
  3. "strings"
  4. "github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
  5. "github.com/tal-tech/go-zero/tools/goctl/util"
  6. "github.com/tal-tech/go-zero/tools/goctl/util/stringx"
  7. )
  8. func genUpdate(table Table, withCache bool) (string, error) {
  9. expressionValues := make([]string, 0)
  10. for _, filed := range table.Fields {
  11. camel := filed.Name.ToCamel()
  12. if camel == "CreateTime" || camel == "UpdateTime" {
  13. continue
  14. }
  15. if filed.IsPrimaryKey {
  16. continue
  17. }
  18. expressionValues = append(expressionValues, "data."+camel)
  19. }
  20. expressionValues = append(expressionValues, "data."+table.PrimaryKey.Name.ToCamel())
  21. camelTableName := table.Name.ToCamel()
  22. text, err := util.LoadTemplate(category, updateTemplateFile, template.Update)
  23. if err != nil {
  24. return "", err
  25. }
  26. output, err := util.With("update").
  27. Parse(text).
  28. Execute(map[string]interface{}{
  29. "withCache": withCache,
  30. "upperStartCamelObject": camelTableName,
  31. "primaryCacheKey": table.CacheKey[table.PrimaryKey.Name.Source()].DataKeyExpression,
  32. "primaryKeyVariable": table.CacheKey[table.PrimaryKey.Name.Source()].Variable,
  33. "lowerStartCamelObject": stringx.From(camelTableName).UnTitle(),
  34. "originalPrimaryKey": table.PrimaryKey.Name.Source(),
  35. "expressionValues": strings.Join(expressionValues, ", "),
  36. })
  37. if err != nil {
  38. return "", nil
  39. }
  40. return output.String(), nil
  41. }