Skip to content

Commit

Permalink
Bugfix for chained Where() (doug-martin#24)
Browse files Browse the repository at this point in the history
* Added test for chained Where() calls with expression overwriting

* Fixed bug with chained Where with expression overwriting
  • Loading branch information
Emreu authored and doug-martin committed Sep 2, 2016
1 parent a96f4ed commit 510bcea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
25 changes: 25 additions & 0 deletions dataset_select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,31 @@ func (me *datasetTest) TestWhere() {
assert.Equal(t, sql, `SELECT * FROM "test" WHERE ("a" IN (SELECT "id" FROM "test2"))`)
}

func (me *datasetTest) TestWhereChain() {
t := me.T()
ds1 := From("test").Where(
I("x").Eq(0),
I("y").Eq(1),
)

ds2 := ds1.Where(
I("z").Eq(2),
)

a := ds2.Where(
I("a").Eq("A"),
)
b := ds2.Where(
I("b").Eq("B"),
)
sql, _, err := a.ToSql()
assert.NoError(t, err)
assert.Equal(t, sql, `SELECT * FROM "test" WHERE (("x" = 0) AND ("y" = 1) AND ("z" = 2) AND ("a" = 'A'))`)
sql, _, err = b.ToSql()
assert.NoError(t, err)
assert.Equal(t, sql, `SELECT * FROM "test" WHERE (("x" = 0) AND ("y" = 1) AND ("z" = 2) AND ("b" = 'B'))`)
}

func (me *datasetTest) TestClearWhere() {
t := me.T()
ds1 := From("test")
Expand Down
3 changes: 2 additions & 1 deletion expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ func (me expressionList) Expressions() []Expression {
func (me expressionList) Append(expressions ...Expression) ExpressionList {
ret := new(expressionList)
ret.operator = me.operator
exps := me.expressions
exps := make([]Expression, len(me.expressions))
copy(exps, me.expressions)
for _, exp := range expressions {
exps = append(exps, exp)
}
Expand Down

0 comments on commit 510bcea

Please sign in to comment.