Skip to content

Commit c329655

Browse files
committed
fix #171: no need to escape field any more
1 parent 5d9be7f commit c329655

File tree

2 files changed

+64
-106
lines changed

2 files changed

+64
-106
lines changed

cond.go

Lines changed: 31 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@ func NewCond() *Cond {
2525

2626
// Equal is used to construct the expression "field = value".
2727
func (c *Cond) Equal(field string, value interface{}) string {
28-
escaped := Escape(field)
29-
3028
return c.Var(condBuilder{
3129
Builder: func(ctx *argsCompileContext) {
32-
ctx.WriteString(escaped)
30+
ctx.WriteString(field)
3331
ctx.WriteString(" = ")
3432
ctx.WriteValue(value)
3533
},
@@ -48,11 +46,9 @@ func (c *Cond) EQ(field string, value interface{}) string {
4846

4947
// NotEqual is used to construct the expression "field <> value".
5048
func (c *Cond) NotEqual(field string, value interface{}) string {
51-
escaped := Escape(field)
52-
5349
return c.Var(condBuilder{
5450
Builder: func(ctx *argsCompileContext) {
55-
ctx.WriteString(escaped)
51+
ctx.WriteString(field)
5652
ctx.WriteString(" <> ")
5753
ctx.WriteValue(value)
5854
},
@@ -71,11 +67,9 @@ func (c *Cond) NEQ(field string, value interface{}) string {
7167

7268
// GreaterThan is used to construct the expression "field > value".
7369
func (c *Cond) GreaterThan(field string, value interface{}) string {
74-
escaped := Escape(field)
75-
7670
return c.Var(condBuilder{
7771
Builder: func(ctx *argsCompileContext) {
78-
ctx.WriteString(escaped)
72+
ctx.WriteString(field)
7973
ctx.WriteString(" > ")
8074
ctx.WriteValue(value)
8175
},
@@ -94,11 +88,9 @@ func (c *Cond) GT(field string, value interface{}) string {
9488

9589
// GreaterEqualThan is used to construct the expression "field >= value".
9690
func (c *Cond) GreaterEqualThan(field string, value interface{}) string {
97-
escaped := Escape(field)
98-
9991
return c.Var(condBuilder{
10092
Builder: func(ctx *argsCompileContext) {
101-
ctx.WriteString(escaped)
93+
ctx.WriteString(field)
10294
ctx.WriteString(" >= ")
10395
ctx.WriteValue(value)
10496
},
@@ -117,11 +109,9 @@ func (c *Cond) GTE(field string, value interface{}) string {
117109

118110
// LessThan is used to construct the expression "field < value".
119111
func (c *Cond) LessThan(field string, value interface{}) string {
120-
escaped := Escape(field)
121-
122112
return c.Var(condBuilder{
123113
Builder: func(ctx *argsCompileContext) {
124-
ctx.WriteString(escaped)
114+
ctx.WriteString(field)
125115
ctx.WriteString(" < ")
126116
ctx.WriteValue(value)
127117
},
@@ -140,11 +130,9 @@ func (c *Cond) LT(field string, value interface{}) string {
140130

141131
// LessEqualThan is used to construct the expression "field <= value".
142132
func (c *Cond) LessEqualThan(field string, value interface{}) string {
143-
escaped := Escape(field)
144-
145133
return c.Var(condBuilder{
146134
Builder: func(ctx *argsCompileContext) {
147-
ctx.WriteString(escaped)
135+
ctx.WriteString(field)
148136
ctx.WriteString(" <= ")
149137
ctx.WriteValue(value)
150138
},
@@ -163,11 +151,9 @@ func (c *Cond) LTE(field string, value interface{}) string {
163151

164152
// In is used to construct the expression "field IN (value...)".
165153
func (c *Cond) In(field string, values ...interface{}) string {
166-
escaped := Escape(field)
167-
168154
return c.Var(condBuilder{
169155
Builder: func(ctx *argsCompileContext) {
170-
ctx.WriteString(escaped)
156+
ctx.WriteString(field)
171157
ctx.WriteString(" IN (")
172158
ctx.WriteValues(values, ", ")
173159
ctx.WriteString(")")
@@ -177,11 +163,9 @@ func (c *Cond) In(field string, values ...interface{}) string {
177163

178164
// NotIn is used to construct the expression "field NOT IN (value...)".
179165
func (c *Cond) NotIn(field string, values ...interface{}) string {
180-
escaped := Escape(field)
181-
182166
return c.Var(condBuilder{
183167
Builder: func(ctx *argsCompileContext) {
184-
ctx.WriteString(escaped)
168+
ctx.WriteString(field)
185169
ctx.WriteString(" NOT IN (")
186170
ctx.WriteValues(values, ", ")
187171
ctx.WriteString(")")
@@ -191,11 +175,9 @@ func (c *Cond) NotIn(field string, values ...interface{}) string {
191175

192176
// Like is used to construct the expression "field LIKE value".
193177
func (c *Cond) Like(field string, value interface{}) string {
194-
escaped := Escape(field)
195-
196178
return c.Var(condBuilder{
197179
Builder: func(ctx *argsCompileContext) {
198-
ctx.WriteString(escaped)
180+
ctx.WriteString(field)
199181
ctx.WriteString(" LIKE ")
200182
ctx.WriteValue(value)
201183
},
@@ -208,20 +190,18 @@ func (c *Cond) Like(field string, value interface{}) string {
208190
// the ILike method will return "LOWER(field) LIKE LOWER(value)"
209191
// to simulate the behavior of the ILIKE operator.
210192
func (c *Cond) ILike(field string, value interface{}) string {
211-
escaped := Escape(field)
212-
213193
return c.Var(condBuilder{
214194
Builder: func(ctx *argsCompileContext) {
215195
switch ctx.Flavor {
216196
case PostgreSQL, SQLite:
217-
ctx.WriteString(escaped)
197+
ctx.WriteString(field)
218198
ctx.WriteString(" ILIKE ")
219199
ctx.WriteValue(value)
220200

221201
default:
222202
// Use LOWER to simulate ILIKE.
223203
ctx.WriteString("LOWER(")
224-
ctx.WriteString(escaped)
204+
ctx.WriteString(field)
225205
ctx.WriteString(") LIKE LOWER(")
226206
ctx.WriteValue(value)
227207
ctx.WriteString(")")
@@ -232,11 +212,9 @@ func (c *Cond) ILike(field string, value interface{}) string {
232212

233213
// NotLike is used to construct the expression "field NOT LIKE value".
234214
func (c *Cond) NotLike(field string, value interface{}) string {
235-
escaped := Escape(field)
236-
237215
return c.Var(condBuilder{
238216
Builder: func(ctx *argsCompileContext) {
239-
ctx.WriteString(escaped)
217+
ctx.WriteString(field)
240218
ctx.WriteString(" NOT LIKE ")
241219
ctx.WriteValue(value)
242220
},
@@ -249,20 +227,18 @@ func (c *Cond) NotLike(field string, value interface{}) string {
249227
// the NotILike method will return "LOWER(field) NOT LIKE LOWER(value)"
250228
// to simulate the behavior of the ILIKE operator.
251229
func (c *Cond) NotILike(field string, value interface{}) string {
252-
escaped := Escape(field)
253-
254230
return c.Var(condBuilder{
255231
Builder: func(ctx *argsCompileContext) {
256232
switch ctx.Flavor {
257233
case PostgreSQL, SQLite:
258-
ctx.WriteString(escaped)
234+
ctx.WriteString(field)
259235
ctx.WriteString(" NOT ILIKE ")
260236
ctx.WriteValue(value)
261237

262238
default:
263239
// Use LOWER to simulate ILIKE.
264240
ctx.WriteString("LOWER(")
265-
ctx.WriteString(escaped)
241+
ctx.WriteString(field)
266242
ctx.WriteString(") NOT LIKE LOWER(")
267243
ctx.WriteValue(value)
268244
ctx.WriteString(")")
@@ -273,35 +249,29 @@ func (c *Cond) NotILike(field string, value interface{}) string {
273249

274250
// IsNull is used to construct the expression "field IS NULL".
275251
func (c *Cond) IsNull(field string) string {
276-
escaped := Escape(field)
277-
278252
return c.Var(condBuilder{
279253
Builder: func(ctx *argsCompileContext) {
280-
ctx.WriteString(escaped)
254+
ctx.WriteString(field)
281255
ctx.WriteString(" IS NULL")
282256
},
283257
})
284258
}
285259

286260
// IsNotNull is used to construct the expression "field IS NOT NULL".
287261
func (c *Cond) IsNotNull(field string) string {
288-
escaped := Escape(field)
289-
290262
return c.Var(condBuilder{
291263
Builder: func(ctx *argsCompileContext) {
292-
ctx.WriteString(escaped)
264+
ctx.WriteString(field)
293265
ctx.WriteString(" IS NOT NULL")
294266
},
295267
})
296268
}
297269

298270
// Between is used to construct the expression "field BETWEEN lower AND upper".
299271
func (c *Cond) Between(field string, lower, upper interface{}) string {
300-
escaped := Escape(field)
301-
302272
return c.Var(condBuilder{
303273
Builder: func(ctx *argsCompileContext) {
304-
ctx.WriteString(escaped)
274+
ctx.WriteString(field)
305275
ctx.WriteString(" BETWEEN ")
306276
ctx.WriteValue(lower)
307277
ctx.WriteString(" AND ")
@@ -312,11 +282,9 @@ func (c *Cond) Between(field string, lower, upper interface{}) string {
312282

313283
// NotBetween is used to construct the expression "field NOT BETWEEN lower AND upper".
314284
func (c *Cond) NotBetween(field string, lower, upper interface{}) string {
315-
escaped := Escape(field)
316-
317285
return c.Var(condBuilder{
318286
Builder: func(ctx *argsCompileContext) {
319-
ctx.WriteString(escaped)
287+
ctx.WriteString(field)
320288
ctx.WriteString(" NOT BETWEEN ")
321289
ctx.WriteValue(lower)
322290
ctx.WriteString(" AND ")
@@ -398,11 +366,9 @@ func (c *Cond) NotExists(subquery interface{}) string {
398366

399367
// Any is used to construct the expression "field op ANY (value...)".
400368
func (c *Cond) Any(field, op string, values ...interface{}) string {
401-
escaped := Escape(field)
402-
403369
return c.Var(condBuilder{
404370
Builder: func(ctx *argsCompileContext) {
405-
ctx.WriteString(escaped)
371+
ctx.WriteString(field)
406372
ctx.WriteString(" ")
407373
ctx.WriteString(op)
408374
ctx.WriteString(" ANY (")
@@ -414,11 +380,9 @@ func (c *Cond) Any(field, op string, values ...interface{}) string {
414380

415381
// All is used to construct the expression "field op ALL (value...)".
416382
func (c *Cond) All(field, op string, values ...interface{}) string {
417-
escaped := Escape(field)
418-
419383
return c.Var(condBuilder{
420384
Builder: func(ctx *argsCompileContext) {
421-
ctx.WriteString(escaped)
385+
ctx.WriteString(field)
422386
ctx.WriteString(" ")
423387
ctx.WriteString(op)
424388
ctx.WriteString(" ALL (")
@@ -430,11 +394,9 @@ func (c *Cond) All(field, op string, values ...interface{}) string {
430394

431395
// Some is used to construct the expression "field op SOME (value...)".
432396
func (c *Cond) Some(field, op string, values ...interface{}) string {
433-
escaped := Escape(field)
434-
435397
return c.Var(condBuilder{
436398
Builder: func(ctx *argsCompileContext) {
437-
ctx.WriteString(escaped)
399+
ctx.WriteString(field)
438400
ctx.WriteString(" ")
439401
ctx.WriteString(op)
440402
ctx.WriteString(" SOME (")
@@ -451,19 +413,17 @@ func (c *Cond) Some(field, op string, values ...interface{}) string {
451413
// "CASE ... WHEN ... ELSE ... END" expression to simulate the behavior of
452414
// the IS DISTINCT FROM operator.
453415
func (c *Cond) IsDistinctFrom(field string, value interface{}) string {
454-
escaped := Escape(field)
455-
456416
return c.Var(condBuilder{
457417
Builder: func(ctx *argsCompileContext) {
458418
switch ctx.Flavor {
459419
case PostgreSQL, SQLite, SQLServer:
460-
ctx.WriteString(escaped)
420+
ctx.WriteString(field)
461421
ctx.WriteString(" IS DISTINCT FROM ")
462422
ctx.WriteValue(value)
463423

464424
case MySQL:
465425
ctx.WriteString("NOT ")
466-
ctx.WriteString(escaped)
426+
ctx.WriteString(field)
467427
ctx.WriteString(" <=> ")
468428
ctx.WriteValue(value)
469429

@@ -474,15 +434,15 @@ func (c *Cond) IsDistinctFrom(field string, value interface{}) string {
474434
// ELSE 1
475435
// END = 1
476436
ctx.WriteString("CASE WHEN ")
477-
ctx.WriteString(escaped)
437+
ctx.WriteString(field)
478438
ctx.WriteString(" IS NULL AND ")
479439
ctx.WriteValue(value)
480440
ctx.WriteString(" IS NULL THEN 0 WHEN ")
481-
ctx.WriteString(escaped)
441+
ctx.WriteString(field)
482442
ctx.WriteString(" IS NOT NULL AND ")
483443
ctx.WriteValue(value)
484444
ctx.WriteString(" IS NOT NULL AND ")
485-
ctx.WriteString(escaped)
445+
ctx.WriteString(field)
486446
ctx.WriteString(" = ")
487447
ctx.WriteValue(value)
488448
ctx.WriteString(" THEN 0 ELSE 1 END = 1")
@@ -498,18 +458,16 @@ func (c *Cond) IsDistinctFrom(field string, value interface{}) string {
498458
// "CASE ... WHEN ... ELSE ... END" expression to simulate the behavior of
499459
// the IS NOT DISTINCT FROM operator.
500460
func (c *Cond) IsNotDistinctFrom(field string, value interface{}) string {
501-
escaped := Escape(field)
502-
503461
return c.Var(condBuilder{
504462
Builder: func(ctx *argsCompileContext) {
505463
switch ctx.Flavor {
506464
case PostgreSQL, SQLite, SQLServer:
507-
ctx.WriteString(escaped)
465+
ctx.WriteString(field)
508466
ctx.WriteString(" IS NOT DISTINCT FROM ")
509467
ctx.WriteValue(value)
510468

511469
case MySQL:
512-
ctx.WriteString(escaped)
470+
ctx.WriteString(field)
513471
ctx.WriteString(" <=> ")
514472
ctx.WriteValue(value)
515473

@@ -520,15 +478,15 @@ func (c *Cond) IsNotDistinctFrom(field string, value interface{}) string {
520478
// ELSE 0
521479
// END = 1
522480
ctx.WriteString("CASE WHEN ")
523-
ctx.WriteString(escaped)
481+
ctx.WriteString(field)
524482
ctx.WriteString(" IS NULL AND ")
525483
ctx.WriteValue(value)
526484
ctx.WriteString(" IS NULL THEN 1 WHEN ")
527-
ctx.WriteString(escaped)
485+
ctx.WriteString(field)
528486
ctx.WriteString(" IS NOT NULL AND ")
529487
ctx.WriteValue(value)
530488
ctx.WriteString(" IS NOT NULL AND ")
531-
ctx.WriteString(escaped)
489+
ctx.WriteString(field)
532490
ctx.WriteString(" = ")
533491
ctx.WriteValue(value)
534492
ctx.WriteString(" THEN 1 ELSE 0 END = 1")

0 commit comments

Comments
 (0)