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

[#82] Added FROM clause to update builder #168

Open
wants to merge 1 commit into
base: v1
Choose a base branch
from
Open
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
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right.

}