Skip to content

Commit a7d9ad4

Browse files
committed
Bug fix: missing parentheses if expr cond include or condition
Add expr cond test
1 parent a4a881a commit a7d9ad4

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

builder_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,12 @@ func TestSubquery(t *testing.T) {
217217
assert.EqualValues(t, "SELECT a, b FROM table_a WHERE id=? AND b_id=(SELECT id FROM table_b WHERE b=?)", sql)
218218
assert.EqualValues(t, []interface{}{23, "a"}, args)
219219
}
220+
221+
// https://github.com/go-xorm/xorm/issues/820
222+
func TestExprCond(t *testing.T) {
223+
b := Select("id").From("table1").Where(expr{sql: "a=? OR b=?", args: []interface{}{1, 2}}).Where(Or(Eq{"c": 3}, Eq{"d": 4}))
224+
sql, args, err := b.ToSQL()
225+
assert.NoError(t, err)
226+
assert.EqualValues(t, "SELECT id FROM table1 WHERE (a=? OR b=?) AND (c=? OR d=?)", sql)
227+
assert.EqualValues(t, []interface{}{1, 2, 3, 4}, args)
228+
}

cond_and.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ func And(conds ...Cond) Cond {
2525
func (and condAnd) WriteTo(w Writer) error {
2626
for i, cond := range and {
2727
_, isOr := cond.(condOr)
28-
if isOr {
28+
_, isExpr := cond.(expr)
29+
wrap := isOr || isExpr
30+
if wrap {
2931
fmt.Fprint(w, "(")
3032
}
3133

@@ -34,7 +36,7 @@ func (and condAnd) WriteTo(w Writer) error {
3436
return err
3537
}
3638

37-
if isOr {
39+
if wrap {
3840
fmt.Fprint(w, ")")
3941
}
4042

0 commit comments

Comments
 (0)