Debian Codegraph @

clause/where.go

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