cond_compare.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2016 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package builder
  5. import "fmt"
  6. // WriteMap writes conditions' SQL to Writer, op could be =, <>, >, <, <=, >= and etc.
  7. func WriteMap(w Writer, data map[string]interface{}, op string) error {
  8. var args = make([]interface{}, 0, len(data))
  9. var i = 0
  10. keys := make([]string, 0, len(data))
  11. for k := range data {
  12. keys = append(keys, k)
  13. }
  14. for _, k := range keys {
  15. v := data[k]
  16. switch v.(type) {
  17. case expr:
  18. if _, err := fmt.Fprintf(w, "%s%s(", k, op); err != nil {
  19. return err
  20. }
  21. if err := v.(expr).WriteTo(w); err != nil {
  22. return err
  23. }
  24. if _, err := fmt.Fprintf(w, ")"); err != nil {
  25. return err
  26. }
  27. case *Builder:
  28. if _, err := fmt.Fprintf(w, "%s%s(", k, op); err != nil {
  29. return err
  30. }
  31. if err := v.(*Builder).WriteTo(w); err != nil {
  32. return err
  33. }
  34. if _, err := fmt.Fprintf(w, ")"); err != nil {
  35. return err
  36. }
  37. default:
  38. if _, err := fmt.Fprintf(w, "%s%s?", k, op); err != nil {
  39. return err
  40. }
  41. args = append(args, v)
  42. }
  43. if i != len(data)-1 {
  44. if _, err := fmt.Fprint(w, " AND "); err != nil {
  45. return err
  46. }
  47. }
  48. i = i + 1
  49. }
  50. w.Append(args...)
  51. return nil
  52. }
  53. // Lt defines < condition
  54. type Lt map[string]interface{}
  55. var _ Cond = Lt{}
  56. // WriteTo write SQL to Writer
  57. func (lt Lt) WriteTo(w Writer) error {
  58. return WriteMap(w, lt, "<")
  59. }
  60. // And implements And with other conditions
  61. func (lt Lt) And(conds ...Cond) Cond {
  62. return condAnd{lt, And(conds...)}
  63. }
  64. // Or implements Or with other conditions
  65. func (lt Lt) Or(conds ...Cond) Cond {
  66. return condOr{lt, Or(conds...)}
  67. }
  68. // IsValid tests if this Eq is valid
  69. func (lt Lt) IsValid() bool {
  70. return len(lt) > 0
  71. }
  72. // Lte defines <= condition
  73. type Lte map[string]interface{}
  74. var _ Cond = Lte{}
  75. // WriteTo write SQL to Writer
  76. func (lte Lte) WriteTo(w Writer) error {
  77. return WriteMap(w, lte, "<=")
  78. }
  79. // And implements And with other conditions
  80. func (lte Lte) And(conds ...Cond) Cond {
  81. return And(lte, And(conds...))
  82. }
  83. // Or implements Or with other conditions
  84. func (lte Lte) Or(conds ...Cond) Cond {
  85. return Or(lte, Or(conds...))
  86. }
  87. // IsValid tests if this Eq is valid
  88. func (lte Lte) IsValid() bool {
  89. return len(lte) > 0
  90. }
  91. // Gt defines > condition
  92. type Gt map[string]interface{}
  93. var _ Cond = Gt{}
  94. // WriteTo write SQL to Writer
  95. func (gt Gt) WriteTo(w Writer) error {
  96. return WriteMap(w, gt, ">")
  97. }
  98. // And implements And with other conditions
  99. func (gt Gt) And(conds ...Cond) Cond {
  100. return And(gt, And(conds...))
  101. }
  102. // Or implements Or with other conditions
  103. func (gt Gt) Or(conds ...Cond) Cond {
  104. return Or(gt, Or(conds...))
  105. }
  106. // IsValid tests if this Eq is valid
  107. func (gt Gt) IsValid() bool {
  108. return len(gt) > 0
  109. }
  110. // Gte defines >= condition
  111. type Gte map[string]interface{}
  112. var _ Cond = Gte{}
  113. // WriteTo write SQL to Writer
  114. func (gte Gte) WriteTo(w Writer) error {
  115. return WriteMap(w, gte, ">=")
  116. }
  117. // And implements And with other conditions
  118. func (gte Gte) And(conds ...Cond) Cond {
  119. return And(gte, And(conds...))
  120. }
  121. // Or implements Or with other conditions
  122. func (gte Gte) Or(conds ...Cond) Cond {
  123. return Or(gte, Or(conds...))
  124. }
  125. // IsValid tests if this Eq is valid
  126. func (gte Gte) IsValid() bool {
  127. return len(gte) > 0
  128. }