Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#316 JoinClause with subquery produces wrong dollar placeholders #317

Merged
merged 1 commit into from May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion part.go
Expand Up @@ -19,7 +19,7 @@ func (p part) ToSql() (sql string, args []interface{}, err error) {
case nil:
// no-op
case Sqlizer:
sql, args, err = pred.ToSql()
sql, args, err = nestedToSql(pred)
case string:
sql = pred
args = p.args
Expand Down
16 changes: 16 additions & 0 deletions select_test.go
Expand Up @@ -279,6 +279,22 @@ func TestSelectSubqueryInConjunctionPlaceholderNumbering(t *testing.T) {
assert.Equal(t, []interface{}{1, 2}, args)
}

func TestSelectJoinClausePlaceholderNumbering(t *testing.T) {
subquery := Select("a").Where(Eq{"b": 2}).PlaceholderFormat(Dollar)

sql, args, err := Select("t1.a").
From("t1").
Where(Eq{"a": 1}).
JoinClause(subquery.Prefix("JOIN (").Suffix(") t2 ON (t1.a = t2.a)")).
PlaceholderFormat(Dollar).
ToSql()
assert.NoError(t, err)

expectedSql := "SELECT t1.a FROM t1 JOIN ( SELECT a WHERE b = $1 ) t2 ON (t1.a = t2.a) WHERE a = $2"
assert.Equal(t, expectedSql, sql)
assert.Equal(t, []interface{}{2, 1}, args)
}

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

Expand Down