update.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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/templatex"
  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. output, err := templatex.With("update").
  23. Parse(template.Update).
  24. Execute(map[string]interface{}{
  25. "withCache": withCache,
  26. "upperStartCamelObject": camelTableName,
  27. "primaryCacheKey": table.CacheKey[table.PrimaryKey.Name.Source()].DataKeyExpression,
  28. "primaryKeyVariable": table.CacheKey[table.PrimaryKey.Name.Source()].Variable,
  29. "lowerStartCamelObject": stringx.From(camelTableName).UnTitle(),
  30. "originalPrimaryKey": table.PrimaryKey.Name.Source(),
  31. "expressionValues": strings.Join(expressionValues, ", "),
  32. })
  33. if err != nil {
  34. return "", nil
  35. }
  36. return output.String(), nil
  37. }