cond_compare.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. for k, v := range data {
  11. switch v.(type) {
  12. case expr:
  13. if _, err := fmt.Fprintf(w, "%s%s(", k, op); err != nil {
  14. return err
  15. }
  16. if err := v.(expr).WriteTo(w); err != nil {
  17. return err
  18. }
  19. if _, err := fmt.Fprintf(w, ")"); err != nil {
  20. return err
  21. }
  22. case *Builder:
  23. if _, err := fmt.Fprintf(w, "%s%s(", k, op); err != nil {
  24. return err
  25. }
  26. if err := v.(*Builder).WriteTo(w); err != nil {
  27. return err
  28. }
  29. if _, err := fmt.Fprintf(w, ")"); err != nil {
  30. return err
  31. }
  32. default:
  33. if _, err := fmt.Fprintf(w, "%s%s?", k, op); err != nil {
  34. return err
  35. }
  36. args = append(args, v)
  37. }
  38. if i != len(data)-1 {
  39. if _, err := fmt.Fprint(w, " AND "); err != nil {
  40. return err
  41. }
  42. }
  43. i = i + 1
  44. }
  45. w.Append(args...)
  46. return nil
  47. }
  48. // Lt defines < condition
  49. type Lt map[string]interface{}
  50. var _ Cond = Lt{}
  51. // WriteTo write SQL to Writer
  52. func (lt Lt) WriteTo(w Writer) error {
  53. return WriteMap(w, lt, "<")
  54. }
  55. // And implements And with other conditions
  56. func (lt Lt) And(conds ...Cond) Cond {
  57. return condAnd{lt, And(conds...)}
  58. }
  59. // Or implements Or with other conditions
  60. func (lt Lt) Or(conds ...Cond) Cond {
  61. return condOr{lt, Or(conds...)}
  62. }
  63. // IsValid tests if this Eq is valid
  64. func (lt Lt) IsValid() bool {
  65. return len(lt) > 0
  66. }
  67. // Lte defines <= condition
  68. type Lte map[string]interface{}
  69. var _ Cond = Lte{}
  70. // WriteTo write SQL to Writer
  71. func (lte Lte) WriteTo(w Writer) error {
  72. return WriteMap(w, lte, "<=")
  73. }
  74. // And implements And with other conditions
  75. func (lte Lte) And(conds ...Cond) Cond {
  76. return And(lte, And(conds...))
  77. }
  78. // Or implements Or with other conditions
  79. func (lte Lte) Or(conds ...Cond) Cond {
  80. return Or(lte, Or(conds...))
  81. }
  82. // IsValid tests if this Eq is valid
  83. func (lte Lte) IsValid() bool {
  84. return len(lte) > 0
  85. }
  86. // Gt defines > condition
  87. type Gt map[string]interface{}
  88. var _ Cond = Gt{}
  89. // WriteTo write SQL to Writer
  90. func (gt Gt) WriteTo(w Writer) error {
  91. return WriteMap(w, gt, ">")
  92. }
  93. // And implements And with other conditions
  94. func (gt Gt) And(conds ...Cond) Cond {
  95. return And(gt, And(conds...))
  96. }
  97. // Or implements Or with other conditions
  98. func (gt Gt) Or(conds ...Cond) Cond {
  99. return Or(gt, Or(conds...))
  100. }
  101. // IsValid tests if this Eq is valid
  102. func (gt Gt) IsValid() bool {
  103. return len(gt) > 0
  104. }
  105. // Gte defines >= condition
  106. type Gte map[string]interface{}
  107. var _ Cond = Gte{}
  108. // WriteTo write SQL to Writer
  109. func (gte Gte) WriteTo(w Writer) error {
  110. return WriteMap(w, gte, ">=")
  111. }
  112. // And implements And with other conditions
  113. func (gte Gte) And(conds ...Cond) Cond {
  114. return And(gte, And(conds...))
  115. }
  116. // Or implements Or with other conditions
  117. func (gte Gte) Or(conds ...Cond) Cond {
  118. return Or(gte, Or(conds...))
  119. }
  120. // IsValid tests if this Eq is valid
  121. func (gte Gte) IsValid() bool {
  122. return len(gte) > 0
  123. }