forked from huandu/go-sqlbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cond.go
313 lines (266 loc) · 7.92 KB
/
cond.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright 2018 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package sqlbuilder
// Cond provides several helper methods to build conditions.
type Cond struct {
Args *Args
}
// NewCond returns a new Cond.
func NewCond() *Cond {
return &Cond{
Args: &Args{},
}
}
// Equal represents "field = value".
func (c *Cond) Equal(field string, value interface{}) string {
buf := newStringBuilder()
buf.WriteString(Escape(field))
buf.WriteString(" = ")
buf.WriteString(c.Args.Add(value))
return buf.String()
}
// E is an alias of Equal.
func (c *Cond) E(field string, value interface{}) string {
return c.Equal(field, value)
}
// EQ is an alias of Equal.
func (c *Cond) EQ(field string, value interface{}) string {
return c.Equal(field, value)
}
// NotEqual represents "field <> value".
func (c *Cond) NotEqual(field string, value interface{}) string {
buf := newStringBuilder()
buf.WriteString(Escape(field))
buf.WriteString(" <> ")
buf.WriteString(c.Args.Add(value))
return buf.String()
}
// NE is an alias of NotEqual.
func (c *Cond) NE(field string, value interface{}) string {
return c.NotEqual(field, value)
}
// NEQ is an alias of NotEqual.
func (c *Cond) NEQ(field string, value interface{}) string {
return c.NotEqual(field, value)
}
// GreaterThan represents "field > value".
func (c *Cond) GreaterThan(field string, value interface{}) string {
buf := newStringBuilder()
buf.WriteString(Escape(field))
buf.WriteString(" > ")
buf.WriteString(c.Args.Add(value))
return buf.String()
}
// G is an alias of GreaterThan.
func (c *Cond) G(field string, value interface{}) string {
return c.GreaterThan(field, value)
}
// GT is an alias of GreaterThan.
func (c *Cond) GT(field string, value interface{}) string {
return c.GreaterThan(field, value)
}
// GreaterEqualThan represents "field >= value".
func (c *Cond) GreaterEqualThan(field string, value interface{}) string {
buf := newStringBuilder()
buf.WriteString(Escape(field))
buf.WriteString(" >= ")
buf.WriteString(c.Args.Add(value))
return buf.String()
}
// GE is an alias of GreaterEqualThan.
func (c *Cond) GE(field string, value interface{}) string {
return c.GreaterEqualThan(field, value)
}
// GTE is an alias of GreaterEqualThan.
func (c *Cond) GTE(field string, value interface{}) string {
return c.GreaterEqualThan(field, value)
}
// LessThan represents "field < value".
func (c *Cond) LessThan(field string, value interface{}) string {
buf := newStringBuilder()
buf.WriteString(Escape(field))
buf.WriteString(" < ")
buf.WriteString(c.Args.Add(value))
return buf.String()
}
// L is an alias of LessThan.
func (c *Cond) L(field string, value interface{}) string {
return c.LessThan(field, value)
}
// LT is an alias of LessThan.
func (c *Cond) LT(field string, value interface{}) string {
return c.LessThan(field, value)
}
// LessEqualThan represents "field <= value".
func (c *Cond) LessEqualThan(field string, value interface{}) string {
buf := newStringBuilder()
buf.WriteString(Escape(field))
buf.WriteString(" <= ")
buf.WriteString(c.Args.Add(value))
return buf.String()
}
// LE is an alias of LessEqualThan.
func (c *Cond) LE(field string, value interface{}) string {
return c.LessEqualThan(field, value)
}
// LTE is an alias of LessEqualThan.
func (c *Cond) LTE(field string, value interface{}) string {
return c.LessEqualThan(field, value)
}
// In represents "field IN (value...)".
func (c *Cond) In(field string, value ...interface{}) string {
vs := make([]string, 0, len(value))
for _, v := range value {
vs = append(vs, c.Args.Add(v))
}
buf := newStringBuilder()
buf.WriteString(Escape(field))
buf.WriteString(" IN (")
buf.WriteStrings(vs, ", ")
buf.WriteString(")")
return buf.String()
}
// NotIn represents "field NOT IN (value...)".
func (c *Cond) NotIn(field string, value ...interface{}) string {
vs := make([]string, 0, len(value))
for _, v := range value {
vs = append(vs, c.Args.Add(v))
}
buf := newStringBuilder()
buf.WriteString(Escape(field))
buf.WriteString(" NOT IN (")
buf.WriteStrings(vs, ", ")
buf.WriteString(")")
return buf.String()
}
// Like represents "field LIKE value".
func (c *Cond) Like(field string, value interface{}) string {
buf := newStringBuilder()
buf.WriteString(Escape(field))
buf.WriteString(" LIKE ")
buf.WriteString(c.Args.Add(value))
return buf.String()
}
// NotLike represents "field NOT LIKE value".
func (c *Cond) NotLike(field string, value interface{}) string {
buf := newStringBuilder()
buf.WriteString(Escape(field))
buf.WriteString(" NOT LIKE ")
buf.WriteString(c.Args.Add(value))
return buf.String()
}
// IsNull represents "field IS NULL".
func (c *Cond) IsNull(field string) string {
buf := newStringBuilder()
buf.WriteString(Escape(field))
buf.WriteString(" IS NULL")
return buf.String()
}
// IsNotNull represents "field IS NOT NULL".
func (c *Cond) IsNotNull(field string) string {
buf := newStringBuilder()
buf.WriteString(Escape(field))
buf.WriteString(" IS NOT NULL")
return buf.String()
}
// Between represents "field BETWEEN lower AND upper".
func (c *Cond) Between(field string, lower, upper interface{}) string {
buf := newStringBuilder()
buf.WriteString(Escape(field))
buf.WriteString(" BETWEEN ")
buf.WriteString(c.Args.Add(lower))
buf.WriteString(" AND ")
buf.WriteString(c.Args.Add(upper))
return buf.String()
}
// NotBetween represents "field NOT BETWEEN lower AND upper".
func (c *Cond) NotBetween(field string, lower, upper interface{}) string {
buf := newStringBuilder()
buf.WriteString(Escape(field))
buf.WriteString(" NOT BETWEEN ")
buf.WriteString(c.Args.Add(lower))
buf.WriteString(" AND ")
buf.WriteString(c.Args.Add(upper))
return buf.String()
}
// Or represents OR logic like "expr1 OR expr2 OR expr3".
func (c *Cond) Or(orExpr ...string) string {
buf := newStringBuilder()
buf.WriteString("(")
buf.WriteStrings(orExpr, " OR ")
buf.WriteString(")")
return buf.String()
}
// And represents AND logic like "expr1 AND expr2 AND expr3".
func (c *Cond) And(andExpr ...string) string {
buf := newStringBuilder()
buf.WriteString("(")
buf.WriteStrings(andExpr, " AND ")
buf.WriteString(")")
return buf.String()
}
// Exists represents "EXISTS (subquery)".
func (c *Cond) Exists(subquery interface{}) string {
buf := newStringBuilder()
buf.WriteString("EXISTS (")
buf.WriteString(c.Args.Add(subquery))
buf.WriteString(")")
return buf.String()
}
// NotExists represents "NOT EXISTS (subquery)".
func (c *Cond) NotExists(subquery interface{}) string {
buf := newStringBuilder()
buf.WriteString("NOT EXISTS (")
buf.WriteString(c.Args.Add(subquery))
buf.WriteString(")")
return buf.String()
}
// Any represents "field op ANY (value...)".
func (c *Cond) Any(field, op string, value ...interface{}) string {
vs := make([]string, 0, len(value))
for _, v := range value {
vs = append(vs, c.Args.Add(v))
}
buf := newStringBuilder()
buf.WriteString(Escape(field))
buf.WriteString(" ")
buf.WriteString(op)
buf.WriteString(" ANY (")
buf.WriteStrings(vs, ", ")
buf.WriteString(")")
return buf.String()
}
// All represents "field op ALL (value...)".
func (c *Cond) All(field, op string, value ...interface{}) string {
vs := make([]string, 0, len(value))
for _, v := range value {
vs = append(vs, c.Args.Add(v))
}
buf := newStringBuilder()
buf.WriteString(Escape(field))
buf.WriteString(" ")
buf.WriteString(op)
buf.WriteString(" ALL (")
buf.WriteStrings(vs, ", ")
buf.WriteString(")")
return buf.String()
}
// Some represents "field op SOME (value...)".
func (c *Cond) Some(field, op string, value ...interface{}) string {
vs := make([]string, 0, len(value))
for _, v := range value {
vs = append(vs, c.Args.Add(v))
}
buf := newStringBuilder()
buf.WriteString(Escape(field))
buf.WriteString(" ")
buf.WriteString(op)
buf.WriteString(" SOME (")
buf.WriteStrings(vs, ", ")
buf.WriteString(")")
return buf.String()
}
// Var returns a placeholder for value.
func (c *Cond) Var(value interface{}) string {
return c.Args.Add(value)
}