From b207c8440a678331dcc388e928775d52e937d994 Mon Sep 17 00:00:00 2001 From: nikitafrolov Date: Mon, 27 Oct 2025 15:50:46 +0400 Subject: [PATCH] feat: add arguments for From method in UpdateBuilder --- update.go | 4 ++-- update_test.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/update.go b/update.go index eb2a9c4d..aa80bfa0 100644 --- a/update.go +++ b/update.go @@ -244,8 +244,8 @@ func (b UpdateBuilder) SetMap(clauses map[string]interface{}) 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) +func (b UpdateBuilder) From(from string, args ...interface{}) UpdateBuilder { + return builder.Set(b, "From", newPart(from, args...)).(UpdateBuilder) } // FromSelect sets a subquery into the FROM clause of the query. diff --git a/update_test.go b/update_test.go index a2bee1aa..94d094c1 100644 --- a/update_test.go +++ b/update_test.go @@ -89,6 +89,26 @@ func TestUpdateBuilderFrom(t *testing.T) { assert.Equal(t, "UPDATE employees SET sales_count = ? FROM accounts WHERE accounts.name = ?", sql) } +func TestUpdateBuilderParamFrom(t *testing.T) { + fromArgs := []interface{}{ + 1, 5, + 2, 5, + } + sql, _, err := Update("employees"). + Set("sales_count", Expr("c.sales_count")). + From("(VALUES (?, ?), (?, ?)) AS c(id, sales_count)", fromArgs...). + Where("id = c.id"). + ToSql() + assert.NoError(t, err) + + expectedSql := + "UPDATE employees " + + "SET sales_count = c.sales_count " + + "FROM (VALUES (?, ?), (?, ?)) AS c(id, sales_count) " + + "WHERE id = c.id" + assert.Equal(t, expectedSql, sql) +} + func TestUpdateBuilderFromSelect(t *testing.T) { sql, _, err := Update("employees"). Set("sales_count", 100).