Skip to content
This repository was archived by the owner on Jun 14, 2019. It is now read-only.

Commit b0282a4

Browse files
authored
between support Expr and more tests (#28)
1 parent 1ae2cc6 commit b0282a4

File tree

2 files changed

+147
-2
lines changed

2 files changed

+147
-2
lines changed

builder_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ func TestBuilderCond(t *testing.T) {
4343
"e=(SELECT id FROM f WHERE g=?)",
4444
[]interface{}{1},
4545
},
46+
{
47+
Like{"a", "%1"}.And(Like{"b", "%2"}),
48+
"a LIKE ? AND b LIKE ?",
49+
[]interface{}{"%1", "%2"},
50+
},
51+
{
52+
Like{"a", "%1"}.Or(Like{"b", "%2"}),
53+
"a LIKE ? OR b LIKE ?",
54+
[]interface{}{"%1", "%2"},
55+
},
4656
{
4757
Neq{"d": []string{"e", "f"}},
4858
"d NOT IN (?,?)",
@@ -63,6 +73,16 @@ func TestBuilderCond(t *testing.T) {
6373
"d<?",
6474
[]interface{}{3},
6575
},
76+
{
77+
Lt{"d": 3}.And(Lt{"e": 4}),
78+
"d<? AND e<?",
79+
[]interface{}{3, 4},
80+
},
81+
{
82+
Lt{"d": 3}.Or(Lt{"e": 4}),
83+
"d<? OR e<?",
84+
[]interface{}{3, 4},
85+
},
6686
{
6787
Lt{"e": Select("id").From("f").Where(Eq{"g": 1})},
6888
"e<(SELECT id FROM f WHERE g=?)",
@@ -78,6 +98,16 @@ func TestBuilderCond(t *testing.T) {
7898
"d<=?",
7999
[]interface{}{3},
80100
},
101+
{
102+
Lte{"d": 3}.And(Lte{"e": 4}),
103+
"d<=? AND e<=?",
104+
[]interface{}{3, 4},
105+
},
106+
{
107+
Lte{"d": 3}.Or(Lte{"e": 4}),
108+
"d<=? OR e<=?",
109+
[]interface{}{3, 4},
110+
},
81111
{
82112
Lte{"e": Select("id").From("f").Where(Eq{"g": 1})},
83113
"e<=(SELECT id FROM f WHERE g=?)",
@@ -93,6 +123,16 @@ func TestBuilderCond(t *testing.T) {
93123
"d>?",
94124
[]interface{}{3},
95125
},
126+
{
127+
Gt{"d": 3}.And(Gt{"e": 4}),
128+
"d>? AND e>?",
129+
[]interface{}{3, 4},
130+
},
131+
{
132+
Gt{"d": 3}.Or(Gt{"e": 4}),
133+
"d>? OR e>?",
134+
[]interface{}{3, 4},
135+
},
96136
{
97137
Gt{"e": Select("id").From("f").Where(Eq{"g": 1})},
98138
"e>(SELECT id FROM f WHERE g=?)",
@@ -108,6 +148,16 @@ func TestBuilderCond(t *testing.T) {
108148
"d>=?",
109149
[]interface{}{3},
110150
},
151+
{
152+
Gte{"d": 3}.And(Gte{"e": 4}),
153+
"d>=? AND e>=?",
154+
[]interface{}{3, 4},
155+
},
156+
{
157+
Gte{"d": 3}.Or(Gte{"e": 4}),
158+
"d>=? OR e>=?",
159+
[]interface{}{3, 4},
160+
},
111161
{
112162
Gte{"e": Select("id").From("f").Where(Eq{"g": 1})},
113163
"e>=(SELECT id FROM f WHERE g=?)",
@@ -123,11 +173,61 @@ func TestBuilderCond(t *testing.T) {
123173
"d BETWEEN ? AND ?",
124174
[]interface{}{0, 2},
125175
},
176+
{
177+
Between{"d", 0, Expr("CAST('2003-01-01' AS DATE)")},
178+
"d BETWEEN ? AND CAST('2003-01-01' AS DATE)",
179+
[]interface{}{0},
180+
},
181+
{
182+
Between{"d", Expr("CAST('2003-01-01' AS DATE)"), 2},
183+
"d BETWEEN CAST('2003-01-01' AS DATE) AND ?",
184+
[]interface{}{2},
185+
},
186+
{
187+
Between{"d", Expr("CAST('2003-01-01' AS DATE)"), Expr("CAST('2003-01-01' AS DATE)")},
188+
"d BETWEEN CAST('2003-01-01' AS DATE) AND CAST('2003-01-01' AS DATE)",
189+
[]interface{}{},
190+
},
191+
{
192+
Between{"d", 0, 2}.And(Between{"e", 3, 4}),
193+
"d BETWEEN ? AND ? AND e BETWEEN ? AND ?",
194+
[]interface{}{0, 2, 3, 4},
195+
},
196+
{
197+
Between{"d", 0, 2}.Or(Between{"e", 3, 4}),
198+
"d BETWEEN ? AND ? OR e BETWEEN ? AND ?",
199+
[]interface{}{0, 2, 3, 4},
200+
},
126201
{
127202
IsNull{"d"},
128203
"d IS NULL",
129204
[]interface{}{},
130205
},
206+
{
207+
IsNull{"d"}.And(IsNull{"e"}),
208+
"d IS NULL AND e IS NULL",
209+
[]interface{}{},
210+
},
211+
{
212+
IsNull{"d"}.Or(IsNull{"e"}),
213+
"d IS NULL OR e IS NULL",
214+
[]interface{}{},
215+
},
216+
{
217+
NotNull{"d"},
218+
"d IS NOT NULL",
219+
[]interface{}{},
220+
},
221+
{
222+
NotNull{"d"}.And(NotNull{"e"}),
223+
"d IS NOT NULL AND e IS NOT NULL",
224+
[]interface{}{},
225+
},
226+
{
227+
NotNull{"d"}.Or(NotNull{"e"}),
228+
"d IS NOT NULL OR e IS NOT NULL",
229+
[]interface{}{},
230+
},
131231
{
132232
NotIn("a", 1, 2).And(NotIn("b", "c", "d")),
133233
"a NOT IN (?,?) AND b NOT IN (?,?)",
@@ -388,6 +488,26 @@ func TestBuilderCond(t *testing.T) {
388488
"NOT (a=? AND b=?)",
389489
[]interface{}{1, 2},
390490
},
491+
{
492+
Not{Eq{"a": 1}.And(Eq{"b": 2})},
493+
"NOT (a=? AND b=?)",
494+
[]interface{}{1, 2},
495+
},
496+
{
497+
Not{Neq{"a": 1}.And(Neq{"b": 2})},
498+
"NOT (a<>? AND b<>?)",
499+
[]interface{}{1, 2},
500+
},
501+
{
502+
Not{Eq{"a": 1}}.And(Neq{"b": 2}),
503+
"NOT a=? AND b<>?",
504+
[]interface{}{1, 2},
505+
},
506+
{
507+
Not{Eq{"a": 1}}.Or(Neq{"b": 2}),
508+
"NOT a=? OR b<>?",
509+
[]interface{}{1, 2},
510+
},
391511
}
392512

393513
for _, k := range cases {

cond_between.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,35 @@ var _ Cond = Between{}
1717

1818
// WriteTo write data to Writer
1919
func (between Between) WriteTo(w Writer) error {
20-
if _, err := fmt.Fprintf(w, "%s BETWEEN ? AND ?", between.Col); err != nil {
20+
if _, err := fmt.Fprintf(w, "%s BETWEEN ", between.Col); err != nil {
2121
return err
2222
}
23-
w.Append(between.LessVal, between.MoreVal)
23+
if lv, ok := between.LessVal.(expr); ok {
24+
if err := lv.WriteTo(w); err != nil {
25+
return err
26+
}
27+
} else {
28+
if _, err := fmt.Fprint(w, "?"); err != nil {
29+
return err
30+
}
31+
w.Append(between.LessVal)
32+
}
33+
34+
if _, err := fmt.Fprint(w, " AND "); err != nil {
35+
return err
36+
}
37+
38+
if mv, ok := between.MoreVal.(expr); ok {
39+
if err := mv.WriteTo(w); err != nil {
40+
return err
41+
}
42+
} else {
43+
if _, err := fmt.Fprint(w, "?"); err != nil {
44+
return err
45+
}
46+
w.Append(between.MoreVal)
47+
}
48+
2449
return nil
2550
}
2651

0 commit comments

Comments
 (0)