condition.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package search
  2. import "strings"
  3. type Condition interface {
  4. SetWhere(k string, v []interface{})
  5. SetOr(k string, v []interface{})
  6. SetOrContains(k string, v []interface{})
  7. SetOrder(k string)
  8. SetJoinOn(t, on string) Condition
  9. }
  10. type GormCondition struct {
  11. GormPublic
  12. Join []*GormJoin
  13. }
  14. type GormPublic struct {
  15. Where map[string][]interface{}
  16. Order []string
  17. Or map[string][]interface{}
  18. OrContains map[string][]interface{}
  19. }
  20. type GormJoin struct {
  21. Type string
  22. JoinOn string
  23. GormPublic
  24. }
  25. func (e *GormJoin) SetJoinOn(t, on string) Condition {
  26. return nil
  27. }
  28. func (e *GormPublic) SetWhere(k string, v []interface{}) {
  29. if e.Where == nil {
  30. e.Where = make(map[string][]interface{})
  31. }
  32. e.Where[k] = v
  33. }
  34. func (e *GormPublic) SetOr(k string, v []interface{}) {
  35. if e.Or == nil {
  36. e.Or = make(map[string][]interface{})
  37. }
  38. e.Or[k] = v
  39. }
  40. func (e *GormPublic) SetOrContains(k string, v []interface{}) {
  41. if e.OrContains == nil {
  42. e.OrContains = make(map[string][]interface{})
  43. }
  44. e.OrContains[k] = v
  45. }
  46. func (e *GormPublic) SetOrder(k string) {
  47. if e.Order == nil {
  48. e.Order = make([]string, 0)
  49. }
  50. e.Order = append(e.Order, k)
  51. }
  52. func (e *GormCondition) SetJoinOn(t, on string) Condition {
  53. if e.Join == nil {
  54. e.Join = make([]*GormJoin, 0)
  55. }
  56. join := &GormJoin{
  57. Type: t,
  58. JoinOn: on,
  59. GormPublic: GormPublic{},
  60. }
  61. e.Join = append(e.Join, join)
  62. return join
  63. }
  64. type resolveSearchTag struct {
  65. Type string
  66. Column string
  67. Table string
  68. On []string
  69. Join string
  70. }
  71. // makeTag 解析search的tag标签
  72. func makeTag(tag string) *resolveSearchTag {
  73. r := &resolveSearchTag{}
  74. tags := strings.Split(tag, ";")
  75. var ts []string
  76. for _, t := range tags {
  77. ts = strings.Split(t, ":")
  78. if len(ts) == 0 {
  79. continue
  80. }
  81. switch ts[0] {
  82. case "type":
  83. if len(ts) > 1 {
  84. r.Type = ts[1]
  85. }
  86. case "column":
  87. if len(ts) > 1 {
  88. r.Column = ts[1]
  89. }
  90. case "table":
  91. if len(ts) > 1 {
  92. r.Table = ts[1]
  93. }
  94. case "on":
  95. if len(ts) > 1 {
  96. r.On = ts[1:]
  97. }
  98. case "join":
  99. if len(ts) > 1 {
  100. r.Join = ts[1]
  101. }
  102. }
  103. }
  104. return r
  105. }