findonebyfield.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package gen
  2. import (
  3. "fmt"
  4. "strings"
  5. "git.i2edu.net/i2/go-zero/tools/goctl/model/sql/template"
  6. "git.i2edu.net/i2/go-zero/tools/goctl/util"
  7. "git.i2edu.net/i2/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 _, key := range table.UniqueCacheKey {
  23. in, paramJoinString, originalFieldString := convertJoin(key)
  24. output, err := t.Execute(map[string]interface{}{
  25. "upperStartCamelObject": camelTableName,
  26. "upperField": key.FieldNameJoin.Camel().With("").Source(),
  27. "in": in,
  28. "withCache": withCache,
  29. "cacheKey": key.KeyExpression,
  30. "cacheKeyVariable": key.KeyLeft,
  31. "lowerStartCamelObject": stringx.From(camelTableName).Untitle(),
  32. "lowerStartCamelField": paramJoinString,
  33. "upperStartCamelPrimaryKey": table.PrimaryKey.Name.ToCamel(),
  34. "originalField": originalFieldString,
  35. })
  36. if err != nil {
  37. return nil, err
  38. }
  39. list = append(list, output.String())
  40. }
  41. text, err = util.LoadTemplate(category, findOneByFieldMethodTemplateFile, template.FindOneByFieldMethod)
  42. if err != nil {
  43. return nil, err
  44. }
  45. t = util.With("findOneByFieldMethod").Parse(text)
  46. var listMethod []string
  47. for _, key := range table.UniqueCacheKey {
  48. var inJoin, paramJoin Join
  49. for _, f := range key.Fields {
  50. param := stringx.From(f.Name.ToCamel()).Untitle()
  51. inJoin = append(inJoin, fmt.Sprintf("%s %s", param, f.DataType))
  52. paramJoin = append(paramJoin, param)
  53. }
  54. var in string
  55. if len(inJoin) > 0 {
  56. in = inJoin.With(", ").Source()
  57. }
  58. output, err := t.Execute(map[string]interface{}{
  59. "upperStartCamelObject": camelTableName,
  60. "upperField": key.FieldNameJoin.Camel().With("").Source(),
  61. "in": in,
  62. })
  63. if err != nil {
  64. return nil, err
  65. }
  66. listMethod = append(listMethod, output.String())
  67. }
  68. if withCache {
  69. text, err := util.LoadTemplate(category, findOneByFieldExtraMethodTemplateFile, template.FindOneByFieldExtraMethod)
  70. if err != nil {
  71. return nil, err
  72. }
  73. out, err := util.With("findOneByFieldExtraMethod").Parse(text).Execute(map[string]interface{}{
  74. "upperStartCamelObject": camelTableName,
  75. "primaryKeyLeft": table.PrimaryCacheKey.VarLeft,
  76. "lowerStartCamelObject": stringx.From(camelTableName).Untitle(),
  77. "originalPrimaryField": wrapWithRawString(table.PrimaryKey.Name.Source()),
  78. })
  79. if err != nil {
  80. return nil, err
  81. }
  82. return &findOneCode{
  83. findOneMethod: strings.Join(list, util.NL),
  84. findOneInterfaceMethod: strings.Join(listMethod, util.NL),
  85. cacheExtra: out.String(),
  86. }, nil
  87. }
  88. return &findOneCode{
  89. findOneMethod: strings.Join(list, util.NL),
  90. findOneInterfaceMethod: strings.Join(listMethod, util.NL),
  91. }, nil
  92. }
  93. func convertJoin(key Key) (in, paramJoinString, originalFieldString string) {
  94. var inJoin, paramJoin, argJoin Join
  95. for _, f := range key.Fields {
  96. param := stringx.From(f.Name.ToCamel()).Untitle()
  97. inJoin = append(inJoin, fmt.Sprintf("%s %s", param, f.DataType))
  98. paramJoin = append(paramJoin, param)
  99. argJoin = append(argJoin, fmt.Sprintf("%s = ?", wrapWithRawString(f.Name.Source())))
  100. }
  101. if len(inJoin) > 0 {
  102. in = inJoin.With(", ").Source()
  103. }
  104. if len(paramJoin) > 0 {
  105. paramJoinString = paramJoin.With(",").Source()
  106. }
  107. if len(argJoin) > 0 {
  108. originalFieldString = argJoin.With(" and ").Source()
  109. }
  110. return in, paramJoinString, originalFieldString
  111. }