From e472f58c1f4c55755926965d1476cfae6439a4f4 Mon Sep 17 00:00:00 2001 From: sivagollapalli Date: Wed, 5 Dec 2018 15:44:14 +0530 Subject: [PATCH] [#82] Added FROM clause to update builder --- update.go | 7 +++++++ update_test.go | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/update.go b/update.go index 682906bc..240acca1 100644 --- a/update.go +++ b/update.go @@ -21,6 +21,7 @@ type updateData struct { Limit string Offset string Suffixes exprs + From Sqlizer } type setClause struct { @@ -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) +} diff --git a/update_test.go b/update_test.go index 13795c3d..54b533ad 100644 --- a/update_test.go +++ b/update_test.go @@ -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) +}