where.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package clause
  2. import (
  3. "strings"
  4. )
  5. // Where where clause
  6. type Where struct {
  7. Exprs []Expression
  8. }
  9. // Name where clause name
  10. func (where Where) Name() string {
  11. return "WHERE"
  12. }
  13. // Build build where clause
  14. func (where Where) Build(builder Builder) {
  15. // Switch position if the first query expression is a single Or condition
  16. for idx, expr := range where.Exprs {
  17. if v, ok := expr.(OrConditions); !ok || len(v.Exprs) > 1 {
  18. if idx != 0 {
  19. where.Exprs[0], where.Exprs[idx] = where.Exprs[idx], where.Exprs[0]
  20. }
  21. break
  22. }
  23. }
  24. buildExprs(where.Exprs, builder, " AND ")
  25. }
  26. func buildExprs(exprs []Expression, builder Builder, joinCond string) {
  27. wrapInParentheses := false
  28. for idx, expr := range exprs {
  29. if idx > 0 {
  30. if v, ok := expr.(OrConditions); ok && len(v.Exprs) == 1 {
  31. builder.WriteString(" OR ")
  32. } else {
  33. builder.WriteString(joinCond)
  34. }
  35. }
  36. if len(exprs) > 1 {
  37. switch v := expr.(type) {
  38. case OrConditions:
  39. if len(v.Exprs) == 1 {
  40. if e, ok := v.Exprs[0].(Expr); ok {
  41. sql := strings.ToLower(e.SQL)
  42. wrapInParentheses = strings.Contains(sql, "and") || strings.Contains(sql, "or")
  43. }
  44. }
  45. case AndConditions:
  46. if len(v.Exprs) == 1 {
  47. if e, ok := v.Exprs[0].(Expr); ok {
  48. sql := strings.ToLower(e.SQL)
  49. wrapInParentheses = strings.Contains(sql, "and") || strings.Contains(sql, "or")
  50. }
  51. }
  52. case Expr:
  53. sql := strings.ToLower(v.SQL)
  54. wrapInParentheses = strings.Contains(sql, "and") || strings.Contains(sql, "or")
  55. }
  56. }
  57. if wrapInParentheses {
  58. builder.WriteString(`(`)
  59. expr.Build(builder)
  60. builder.WriteString(`)`)
  61. wrapInParentheses = false
  62. } else {
  63. expr.Build(builder)
  64. }
  65. }
  66. }
  67. // MergeClause merge where clauses
  68. func (where Where) MergeClause(clause *Clause) {
  69. if w, ok := clause.Expression.(Where); ok {
  70. exprs := make([]Expression, len(w.Exprs)+len(where.Exprs))
  71. copy(exprs, w.Exprs)
  72. copy(exprs[len(w.Exprs):], where.Exprs)
  73. where.Exprs = exprs
  74. }
  75. clause.Expression = where
  76. }
  77. func And(exprs ...Expression) Expression {
  78. if len(exprs) == 0 {
  79. return nil
  80. } else if len(exprs) == 1 {
  81. return exprs[0]
  82. }
  83. return AndConditions{Exprs: exprs}
  84. }
  85. type AndConditions struct {
  86. Exprs []Expression
  87. }
  88. func (and AndConditions) Build(builder Builder) {
  89. if len(and.Exprs) > 1 {
  90. builder.WriteByte('(')
  91. buildExprs(and.Exprs, builder, " AND ")
  92. builder.WriteByte(')')
  93. } else {
  94. buildExprs(and.Exprs, builder, " AND ")
  95. }
  96. }
  97. func Or(exprs ...Expression) Expression {
  98. if len(exprs) == 0 {
  99. return nil
  100. }
  101. return OrConditions{Exprs: exprs}
  102. }
  103. type OrConditions struct {
  104. Exprs []Expression
  105. }
  106. func (or OrConditions) Build(builder Builder) {
  107. if len(or.Exprs) > 1 {
  108. builder.WriteByte('(')
  109. buildExprs(or.Exprs, builder, " OR ")
  110. builder.WriteByte(')')
  111. } else {
  112. buildExprs(or.Exprs, builder, " OR ")
  113. }
  114. }
  115. func Not(exprs ...Expression) Expression {
  116. if len(exprs) == 0 {
  117. return nil
  118. }
  119. return NotConditions{Exprs: exprs}
  120. }
  121. type NotConditions struct {
  122. Exprs []Expression
  123. }
  124. func (not NotConditions) Build(builder Builder) {
  125. if len(not.Exprs) > 1 {
  126. builder.WriteByte('(')
  127. }
  128. for idx, c := range not.Exprs {
  129. if idx > 0 {
  130. builder.WriteString(" AND ")
  131. }
  132. if negationBuilder, ok := c.(NegationExpressionBuilder); ok {
  133. negationBuilder.NegationBuild(builder)
  134. } else {
  135. builder.WriteString("NOT ")
  136. e, wrapInParentheses := c.(Expr)
  137. if wrapInParentheses {
  138. sql := strings.ToLower(e.SQL)
  139. if wrapInParentheses = strings.Contains(sql, "and") || strings.Contains(sql, "or"); wrapInParentheses {
  140. builder.WriteByte('(')
  141. }
  142. }
  143. c.Build(builder)
  144. if wrapInParentheses {
  145. builder.WriteByte(')')
  146. }
  147. }
  148. }
  149. if len(not.Exprs) > 1 {
  150. builder.WriteByte(')')
  151. }
  152. }