Skip to content

Commit

Permalink
#301 Conjunction produces wrong dollar parameter placeholders (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
strider2038 committed Nov 12, 2021
1 parent 84ae2bc commit def598c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion expr.go
Expand Up @@ -372,7 +372,7 @@ func (c conj) join(sep, defaultExpr string) (sql string, args []interface{}, err
}
var sqlParts []string
for _, sqlizer := range c {
partSQL, partArgs, err := sqlizer.ToSql()
partSQL, partArgs, err := nestedToSql(sqlizer)
if err != nil {
return "", nil, err
}
Expand Down
15 changes: 15 additions & 0 deletions select_test.go
Expand Up @@ -264,6 +264,21 @@ func TestSelectSubqueryPlaceholderNumbering(t *testing.T) {
assert.Equal(t, []interface{}{1, 1, 2}, args)
}

func TestSelectSubqueryInConjunctionPlaceholderNumbering(t *testing.T) {
subquery := Select("a").Where(Eq{"b": 1}).Prefix("EXISTS(").Suffix(")").PlaceholderFormat(Dollar)

sql, args, err := Select("*").
Where(Or{subquery}).
Where("c = ?", 2).
PlaceholderFormat(Dollar).
ToSql()
assert.NoError(t, err)

expectedSql := "SELECT * WHERE (EXISTS( SELECT a WHERE b = $1 )) AND c = $2"
assert.Equal(t, expectedSql, sql)
assert.Equal(t, []interface{}{1, 2}, args)
}

func ExampleSelect() {
Select("id", "created", "first_name").From("users") // ... continue building up your query

Expand Down

0 comments on commit def598c

Please sign in to comment.