Skip to content

Commit

Permalink
Added paren for select statement in set clause (#263)
Browse files Browse the repository at this point in the history
Co-authored-by: Чащин Валентин Валерьевич <valentin.chashchin@goods.ru>
  • Loading branch information
x-foby and Чащин Валентин Валерьевич committed Nov 27, 2020
1 parent 34e532c commit d1a9a0e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 5 additions & 1 deletion update.go
Expand Up @@ -86,7 +86,11 @@ func (d *updateData) ToSql() (sqlStr string, args []interface{}, err error) {
if err != nil {
return "", nil, err
}
valSql = vsql
if _, ok := vs.(SelectBuilder); ok {
valSql = fmt.Sprintf("(%s)", vsql)
} else {
valSql = vsql
}
args = append(args, vargs...)
} else {
valSql = "?"
Expand Down
4 changes: 3 additions & 1 deletion update_test.go
Expand Up @@ -14,6 +14,7 @@ func TestUpdateBuilderToSql(t *testing.T) {
SetMap(Eq{"c": 2}).
Set("c1", Case("status").When("1", "2").When("2", "1")).
Set("c2", Case().When("a = 2", Expr("?", "foo")).When("a = 3", Expr("?", "bar"))).
Set("c3", Select("a").From("b")).
Where("d = ?", 3).
OrderBy("e").
Limit(4).
Expand All @@ -27,7 +28,8 @@ func TestUpdateBuilderToSql(t *testing.T) {
"WITH prefix AS ? " +
"UPDATE a SET b = ? + 1, c = ?, " +
"c1 = CASE status WHEN 1 THEN 2 WHEN 2 THEN 1 END, " +
"c2 = CASE WHEN a = 2 THEN ? WHEN a = 3 THEN ? END " +
"c2 = CASE WHEN a = 2 THEN ? WHEN a = 3 THEN ? END, " +
"c3 = (SELECT a FROM b) " +
"WHERE d = ? " +
"ORDER BY e LIMIT 4 OFFSET 5 " +
"RETURNING ?"
Expand Down

0 comments on commit d1a9a0e

Please sign in to comment.