diff --git a/docs/updating.md b/docs/updating.md index d22cbf7d..4f86e3a8 100644 --- a/docs/updating.md +++ b/docs/updating.md @@ -324,6 +324,22 @@ Output: UPDATE "items" SET "address"='111 Test Addr',"name"='Test' [] ``` + +**[Set with Expressions](https://godoc.org/github.com/doug-martin/goqu/#UpdateDataset.Set)** + +```go +sql, args, _ := goqu.Update("items").Set([]exp.UpdateExpression{ + goqu.C("name").Set("Test"), + goqu.C("address").Set("111 Test Addr"), +}).ToSQL() +fmt.Println(sql, args) +``` + +Output: +``` +UPDATE "items" SET "name"='Test',"address"='111 Test Addr' [] +``` + **[From / Multi Table](https://godoc.org/github.com/doug-martin/goqu/#UpdateDataset.From)** diff --git a/exp/update.go b/exp/update.go index da9b1987..7db71554 100644 --- a/exp/update.go +++ b/exp/update.go @@ -20,6 +20,10 @@ func set(col IdentifierExpression, val interface{}) UpdateExpression { } func NewUpdateExpressions(update interface{}) (updates []UpdateExpression, err error) { + if us, ok := update.([]UpdateExpression); ok { + updates = append(updates, us...) + return updates, nil + } if u, ok := update.(UpdateExpression); ok { updates = append(updates, u) return updates, nil diff --git a/update_dataset_example_test.go b/update_dataset_example_test.go index 096dfa38..998227c4 100644 --- a/update_dataset_example_test.go +++ b/update_dataset_example_test.go @@ -7,6 +7,7 @@ import ( "github.com/doug-martin/goqu/v9" _ "github.com/doug-martin/goqu/v9/dialect/mysql" + "github.com/doug-martin/goqu/v9/exp" ) func ExampleUpdate_withStruct() { @@ -120,6 +121,17 @@ func ExampleUpdate_withMap() { // UPDATE "items" SET "address"='111 Test Addr',"name"='Test' [] } +func ExampleUpdate_withExpressions() { + sql, args, _ := goqu.Update("items").Set([]exp.UpdateExpression{ + goqu.C("name").Set("Test"), + goqu.C("address").Set("111 Test Addr"), + }).ToSQL() + fmt.Println(sql, args) + + // Output: + // UPDATE "items" SET "name"='Test',"address"='111 Test Addr' [] +} + func ExampleUpdate_withSkipUpdateTag() { type item struct { Address string `db:"address"` diff --git a/update_dataset_test.go b/update_dataset_test.go index 0e5d05ff..c5fac744 100644 --- a/update_dataset_test.go +++ b/update_dataset_test.go @@ -163,6 +163,18 @@ func (uds *updateDatasetSuite) TestSet() { clauses: exp.NewUpdateClauses(). SetTable(goqu.C("items")), }, + updateTestCase{ + ds: bd.Set([]exp.UpdateExpression{ + goqu.C("name").Set("Test"), + goqu.C("address").Set("111 Test Addr"), + }), + clauses: exp.NewUpdateClauses(). + SetTable(goqu.C("items")). + SetSetValues([]exp.UpdateExpression{ + goqu.C("name").Set("Test"), + goqu.C("address").Set("111 Test Addr"), + }), + }, ) }