where.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package clause
  2. import (
  3. "strings"
  4. )
  5. const (
  6. AndWithSpace = " AND "
  7. OrWithSpace = " OR "
  8. )
  9. // Where where clause
  10. type Where struct {
  11. Exprs []Expression
  12. }
  13. // Name where clause name
  14. func (where Where) Name() string {
  15. return "WHERE"
  16. }
  17. // Build build where clause
  18. func (where Where) Build(builder Builder) {
  19. if len(where.Exprs) == 1 {
  20. if andCondition, ok := where.Exprs[0].(AndConditions); ok {
  21. where.Exprs = andCondition.Exprs
  22. }
  23. }
  24. // Switch position if the first query expression is a single Or condition
  25. for idx, expr := range where.Exprs {
  26. if v, ok := expr.(OrConditions); !ok || len(v.Exprs) > 1 {
  27. if idx != 0 {
  28. where.Exprs[0], where.Exprs[idx] = where.Exprs[idx], where.Exprs[0]
  29. }
  30. break
  31. }
  32. }
  33. buildExprs(where.Exprs, builder, AndWithSpace)
  34. }
  35. func buildExprs(exprs []Expression, builder Builder, joinCond string) {
  36. wrapInParentheses := false
  37. for idx, expr := range exprs {
  38. if idx > 0 {
  39. if v, ok := expr.(OrConditions); ok && len(v.Exprs) == 1 {
  40. builder.WriteString(OrWithSpace)
  41. } else {
  42. builder.WriteString(joinCond)
  43. }
  44. }
  45. if len(exprs) > 1 {
  46. switch v := expr.(type) {
  47. case OrConditions:
  48. if len(v.Exprs) == 1 {
  49. if e, ok := v.Exprs[0].(Expr); ok {
  50. sql := strings.ToUpper(e.SQL)
  51. wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace)
  52. }
  53. }
  54. case AndConditions:
  55. if len(v.Exprs) == 1 {
  56. if e, ok := v.Exprs[0].(Expr); ok {
  57. sql := strings.ToUpper(e.SQL)
  58. wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace)
  59. }
  60. }
  61. case Expr:
  62. sql := strings.ToUpper(v.SQL)
  63. wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace)
  64. case NamedExpr:
  65. sql := strings.ToUpper(v.SQL)
  66. wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace)
  67. }
  68. }
  69. if wrapInParentheses {
  70. builder.WriteByte('(')
  71. expr.Build(builder)
  72. builder.WriteByte(')')
  73. wrapInParentheses = false
  74. } else {
  75. expr.Build(builder)
  76. }
  77. }
  78. }
  79. // MergeClause merge where clauses
  80. func (where Where) MergeClause(clause *Clause) {
  81. if w, ok := clause.Expression.(Where); ok {
  82. exprs := make([]Expression, len(w.Exprs)+len(where.Exprs))
  83. copy(exprs, w.Exprs)
  84. copy(exprs[len(w.Exprs):], where.Exprs)
  85. where.Exprs = exprs
  86. }
  87. clause.Expression = where
  88. }
  89. func And(exprs ...Expression) Expression {
  90. if len(exprs) == 0 {
  91. return nil
  92. }
  93. if len(exprs) == 1 {
  94. if _, ok := exprs[0].(OrConditions); !ok {
  95. return exprs[0]
  96. }
  97. }
  98. return AndConditions{Exprs: exprs}
  99. }
  100. type AndConditions struct {
  101. Exprs []Expression
  102. }
  103. func (and AndConditions) Build(builder Builder) {
  104. if len(and.Exprs) > 1 {
  105. builder.WriteByte('(')
  106. buildExprs(and.Exprs, builder, AndWithSpace)
  107. builder.WriteByte(')')
  108. } else {
  109. buildExprs(and.Exprs, builder, AndWithSpace)
  110. }
  111. }
  112. func Or(exprs ...Expression) Expression {
  113. if len(exprs) == 0 {
  114. return nil
  115. }
  116. return OrConditions{Exprs: exprs}
  117. }
  118. type OrConditions struct {
  119. Exprs []Expression
  120. }
  121. func (or OrConditions) Build(builder Builder) {
  122. if len(or.Exprs) > 1 {
  123. builder.WriteByte('(')
  124. buildExprs(or.Exprs, builder, OrWithSpace)
  125. builder.WriteByte(')')
  126. } else {
  127. buildExprs(or.Exprs, builder, OrWithSpace)
  128. }
  129. }
  130. func Not(exprs ...Expression) Expression {
  131. if len(exprs) == 0 {
  132. return nil
  133. }
  134. if len(exprs) == 1 {
  135. if andCondition, ok := exprs[0].(AndConditions); ok {
  136. exprs = andCondition.Exprs
  137. }
  138. }
  139. return NotConditions{Exprs: exprs}
  140. }
  141. type NotConditions struct {
  142. Exprs []Expression
  143. }
  144. func (not NotConditions) Build(builder Builder) {
  145. anyNegationBuilder := false
  146. for _, c := range not.Exprs {
  147. if _, ok := c.(NegationExpressionBuilder); ok {
  148. anyNegationBuilder = true
  149. break
  150. }
  151. }
  152. if anyNegationBuilder {
  153. if len(not.Exprs) > 1 {
  154. builder.WriteByte('(')
  155. }
  156. for idx, c := range not.Exprs {
  157. if idx > 0 {
  158. builder.WriteString(AndWithSpace)
  159. }
  160. if negationBuilder, ok := c.(NegationExpressionBuilder); ok {
  161. negationBuilder.NegationBuild(builder)
  162. } else {
  163. builder.WriteString("NOT ")
  164. e, wrapInParentheses := c.(Expr)
  165. if wrapInParentheses {
  166. sql := strings.ToUpper(e.SQL)
  167. if wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace); wrapInParentheses {
  168. builder.WriteByte('(')
  169. }
  170. }
  171. c.Build(builder)
  172. if wrapInParentheses {
  173. builder.WriteByte(')')
  174. }
  175. }
  176. }
  177. if len(not.Exprs) > 1 {
  178. builder.WriteByte(')')
  179. }
  180. } else {
  181. builder.WriteString("NOT ")
  182. if len(not.Exprs) > 1 {
  183. builder.WriteByte('(')
  184. }
  185. for idx, c := range not.Exprs {
  186. if idx > 0 {
  187. switch c.(type) {
  188. case OrConditions:
  189. builder.WriteString(OrWithSpace)
  190. default:
  191. builder.WriteString(AndWithSpace)
  192. }
  193. }
  194. e, wrapInParentheses := c.(Expr)
  195. if wrapInParentheses {
  196. sql := strings.ToUpper(e.SQL)
  197. if wrapInParentheses = strings.Contains(sql, AndWithSpace) || strings.Contains(sql, OrWithSpace); wrapInParentheses {
  198. builder.WriteByte('(')
  199. }
  200. }
  201. c.Build(builder)
  202. if wrapInParentheses {
  203. builder.WriteByte(')')
  204. }
  205. }
  206. if len(not.Exprs) > 1 {
  207. builder.WriteByte(')')
  208. }
  209. }
  210. }