keys.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package gen
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "git.i2edu.net/i2/go-zero/tools/goctl/model/sql/parser"
  7. "git.i2edu.net/i2/go-zero/tools/goctl/util/stringx"
  8. )
  9. // Key describes cache key
  10. type Key struct {
  11. // VarLeft describes the variable of cache key expression which likes cacheUserIdPrefix
  12. VarLeft string
  13. // VarRight describes the value of cache key expression which likes "cache:user:id:"
  14. VarRight string
  15. // VarExpression describes the cache key expression which likes cacheUserIdPrefix = "cache:user:id:"
  16. VarExpression string
  17. // KeyLeft describes the variable of key definition expression which likes userKey
  18. KeyLeft string
  19. // KeyRight describes the value of key definition expression which likes fmt.Sprintf("%s%v", cacheUserPrefix, user)
  20. KeyRight string
  21. // DataKeyRight describes data key likes fmt.Sprintf("%s%v", cacheUserPrefix, data.User)
  22. DataKeyRight string
  23. // KeyExpression describes key expression likes userKey := fmt.Sprintf("%s%v", cacheUserPrefix, user)
  24. KeyExpression string
  25. // DataKeyExpression describes data key expression likes userKey := fmt.Sprintf("%s%v", cacheUserPrefix, data.User)
  26. DataKeyExpression string
  27. // FieldNameJoin describes the filed slice of table
  28. FieldNameJoin Join
  29. // Fields describes the fields of table
  30. Fields []*parser.Field
  31. }
  32. // Join describes an alias of string slice
  33. type Join []string
  34. func genCacheKeys(table parser.Table) (Key, []Key) {
  35. var primaryKey Key
  36. var uniqueKey []Key
  37. primaryKey = genCacheKey(table.Name, []*parser.Field{&table.PrimaryKey.Field})
  38. for _, each := range table.UniqueIndex {
  39. uniqueKey = append(uniqueKey, genCacheKey(table.Name, each))
  40. }
  41. sort.Slice(uniqueKey, func(i, j int) bool {
  42. return uniqueKey[i].VarLeft < uniqueKey[j].VarLeft
  43. })
  44. return primaryKey, uniqueKey
  45. }
  46. func genCacheKey(table stringx.String, in []*parser.Field) Key {
  47. var (
  48. varLeftJoin, varRightJon, fieldNameJoin Join
  49. varLeft, varRight, varExpression string
  50. keyLeftJoin, keyRightJoin, keyRightArgJoin, dataRightJoin Join
  51. keyLeft, keyRight, dataKeyRight, keyExpression, dataKeyExpression string
  52. )
  53. varLeftJoin = append(varLeftJoin, "cache", table.Source())
  54. varRightJon = append(varRightJon, "cache", table.Source())
  55. keyLeftJoin = append(keyLeftJoin, table.Source())
  56. for _, each := range in {
  57. varLeftJoin = append(varLeftJoin, each.Name.Source())
  58. varRightJon = append(varRightJon, each.Name.Source())
  59. keyLeftJoin = append(keyLeftJoin, each.Name.Source())
  60. keyRightJoin = append(keyRightJoin, stringx.From(each.Name.ToCamel()).Untitle())
  61. keyRightArgJoin = append(keyRightArgJoin, "%v")
  62. dataRightJoin = append(dataRightJoin, "data."+each.Name.ToCamel())
  63. fieldNameJoin = append(fieldNameJoin, each.Name.Source())
  64. }
  65. varLeftJoin = append(varLeftJoin, "prefix")
  66. keyLeftJoin = append(keyLeftJoin, "key")
  67. varLeft = varLeftJoin.Camel().With("").Untitle()
  68. varRight = fmt.Sprintf(`"%s"`, varRightJon.Camel().Untitle().With(":").Source()+":")
  69. varExpression = fmt.Sprintf(`%s = %s`, varLeft, varRight)
  70. keyLeft = keyLeftJoin.Camel().With("").Untitle()
  71. keyRight = fmt.Sprintf(`fmt.Sprintf("%s%s", %s, %s)`, "%s", keyRightArgJoin.With(":").Source(), varLeft, keyRightJoin.With(", ").Source())
  72. dataKeyRight = fmt.Sprintf(`fmt.Sprintf("%s%s", %s, %s)`, "%s", keyRightArgJoin.With(":").Source(), varLeft, dataRightJoin.With(", ").Source())
  73. keyExpression = fmt.Sprintf("%s := %s", keyLeft, keyRight)
  74. dataKeyExpression = fmt.Sprintf("%s := %s", keyLeft, dataKeyRight)
  75. return Key{
  76. VarLeft: varLeft,
  77. VarRight: varRight,
  78. VarExpression: varExpression,
  79. KeyLeft: keyLeft,
  80. KeyRight: keyRight,
  81. DataKeyRight: dataKeyRight,
  82. KeyExpression: keyExpression,
  83. DataKeyExpression: dataKeyExpression,
  84. Fields: in,
  85. FieldNameJoin: fieldNameJoin,
  86. }
  87. }
  88. // Title convert items into Title and return
  89. func (j Join) Title() Join {
  90. var join Join
  91. for _, each := range j {
  92. join = append(join, stringx.From(each).Title())
  93. }
  94. return join
  95. }
  96. // Camel convert items into Camel and return
  97. func (j Join) Camel() Join {
  98. var join Join
  99. for _, each := range j {
  100. join = append(join, stringx.From(each).ToCamel())
  101. }
  102. return join
  103. }
  104. // Snake convert items into Snake and return
  105. func (j Join) Snake() Join {
  106. var join Join
  107. for _, each := range j {
  108. join = append(join, stringx.From(each).ToSnake())
  109. }
  110. return join
  111. }
  112. // Untitle converts items into Untitle and return
  113. func (j Join) Untitle() Join {
  114. var join Join
  115. for _, each := range j {
  116. join = append(join, stringx.From(each).Untitle())
  117. }
  118. return join
  119. }
  120. // Upper convert items into Upper and return
  121. func (j Join) Upper() Join {
  122. var join Join
  123. for _, each := range j {
  124. join = append(join, stringx.From(each).Upper())
  125. }
  126. return join
  127. }
  128. // Lower convert items into Lower and return
  129. func (j Join) Lower() Join {
  130. var join Join
  131. for _, each := range j {
  132. join = append(join, stringx.From(each).Lower())
  133. }
  134. return join
  135. }
  136. // With convert items into With and return
  137. func (j Join) With(sep string) stringx.String {
  138. return stringx.From(strings.Join(j, sep))
  139. }