Skip to content

Commit

Permalink
[Masterminds#82] Added FROM clause to update builder
Browse files Browse the repository at this point in the history
  • Loading branch information
sivagollapalli committed Dec 5, 2018
1 parent e5bf00f commit e472f58
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type updateData struct {
Limit string
Offset string
Suffixes exprs
From Sqlizer
}

type setClause struct {
Expand Down Expand Up @@ -230,3 +231,9 @@ func (b UpdateBuilder) Offset(offset uint64) UpdateBuilder {
func (b UpdateBuilder) Suffix(sql string, args ...interface{}) UpdateBuilder {
return builder.Append(b, "Suffixes", Expr(sql, args...)).(UpdateBuilder)
}

// From adds FROM clause to the query
// FROM is valid construct in postgresql only.
func (b UpdateBuilder) From(from string) UpdateBuilder {
return builder.Set(b, "From", newPart(from)).(UpdateBuilder)
}
6 changes: 6 additions & 0 deletions update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,9 @@ func TestUpdateBuilderNoRunner(t *testing.T) {
_, err := b.Exec()
assert.Equal(t, RunnerNotSet, err)
}

func TestUpdateBuilderFromClause(t *testing.T) {
sql, _, err := Update("employees").Set("sales_count", 100).From("accounts").Where("accounts.name = ?", "ACME").ToSql()
assert.NoError(t, err)
assert.Equal(t, "UPDATE employees SET sales_count = ? WHERE accounts.name = ?", sql)
}

0 comments on commit e472f58

Please sign in to comment.