findonebyfield.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package gen
  2. import (
  3. "fmt"
  4. "strings"
  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. type findOneCode struct {
  10. findOneMethod string
  11. findOneInterfaceMethod string
  12. cacheExtra string
  13. }
  14. func genFindOneByField(table Table, withCache bool) (*findOneCode, error) {
  15. text, err := util.LoadTemplate(category, findOneByFieldTemplateFile, template.FindOneByField)
  16. if err != nil {
  17. return nil, err
  18. }
  19. t := util.With("findOneByField").Parse(text)
  20. var list []string
  21. camelTableName := table.Name.ToCamel()
  22. for _, field := range table.Fields {
  23. if field.IsPrimaryKey || !field.IsUniqueKey {
  24. continue
  25. }
  26. camelFieldName := field.Name.ToCamel()
  27. output, err := t.Execute(map[string]interface{}{
  28. "upperStartCamelObject": camelTableName,
  29. "upperField": camelFieldName,
  30. "in": fmt.Sprintf("%s %s", stringx.From(camelFieldName).Untitle(), field.DataType),
  31. "withCache": withCache,
  32. "cacheKey": table.CacheKey[field.Name.Source()].KeyExpression,
  33. "cacheKeyVariable": table.CacheKey[field.Name.Source()].Variable,
  34. "lowerStartCamelObject": stringx.From(camelTableName).Untitle(),
  35. "lowerStartCamelField": stringx.From(camelFieldName).Untitle(),
  36. "upperStartCamelPrimaryKey": table.PrimaryKey.Name.ToCamel(),
  37. "originalField": wrapWithRawString(field.Name.Source()),
  38. })
  39. if err != nil {
  40. return nil, err
  41. }
  42. list = append(list, output.String())
  43. }
  44. text, err = util.LoadTemplate(category, findOneByFieldMethodTemplateFile, template.FindOneByFieldMethod)
  45. if err != nil {
  46. return nil, err
  47. }
  48. t = util.With("findOneByFieldMethod").Parse(text)
  49. var listMethod []string
  50. for _, field := range table.Fields {
  51. if field.IsPrimaryKey || !field.IsUniqueKey {
  52. continue
  53. }
  54. camelFieldName := field.Name.ToCamel()
  55. output, err := t.Execute(map[string]interface{}{
  56. "upperStartCamelObject": camelTableName,
  57. "upperField": camelFieldName,
  58. "in": fmt.Sprintf("%s %s", stringx.From(camelFieldName).Untitle(), field.DataType),
  59. })
  60. if err != nil {
  61. return nil, err
  62. }
  63. listMethod = append(listMethod, output.String())
  64. }
  65. if withCache {
  66. text, err := util.LoadTemplate(category, findOneByFieldExtraMethodTemplateFile, template.FindOneByFieldExtraMethod)
  67. if err != nil {
  68. return nil, err
  69. }
  70. out, err := util.With("findOneByFieldExtraMethod").Parse(text).Execute(map[string]interface{}{
  71. "upperStartCamelObject": camelTableName,
  72. "primaryKeyLeft": table.CacheKey[table.PrimaryKey.Name.Source()].Left,
  73. "lowerStartCamelObject": stringx.From(camelTableName).Untitle(),
  74. "originalPrimaryField": wrapWithRawString(table.PrimaryKey.Name.Source()),
  75. })
  76. if err != nil {
  77. return nil, err
  78. }
  79. return &findOneCode{
  80. findOneMethod: strings.Join(list, util.NL),
  81. findOneInterfaceMethod: strings.Join(listMethod, util.NL),
  82. cacheExtra: out.String(),
  83. }, nil
  84. }
  85. return &findOneCode{
  86. findOneMethod: strings.Join(list, util.NL),
  87. findOneInterfaceMethod: strings.Join(listMethod, util.NL),
  88. }, nil
  89. }