update.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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, string, error) {
  9. expressionValues := make([]string, 0)
  10. for _, field := range table.Fields {
  11. camel := field.Name.ToCamel()
  12. if camel == "CreateTime" || camel == "UpdateTime" {
  13. continue
  14. }
  15. if field.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": wrapWithRawString(table.PrimaryKey.Name.Source()),
  35. "expressionValues": strings.Join(expressionValues, ", "),
  36. })
  37. if err != nil {
  38. return "", "", nil
  39. }
  40. // update interface method
  41. text, err = util.LoadTemplate(category, updateMethodTemplateFile, template.UpdateMethod)
  42. if err != nil {
  43. return "", "", err
  44. }
  45. updateMethodOutput, err := util.With("updateMethod").
  46. Parse(text).
  47. Execute(map[string]interface{}{
  48. "upperStartCamelObject": camelTableName,
  49. })
  50. if err != nil {
  51. return "", "", nil
  52. }
  53. return output.String(), updateMethodOutput.String(), nil
  54. }