Skip to content

Commit

Permalink
feat: allow []exp.UpdateExpression as Set values.
Browse files Browse the repository at this point in the history
  • Loading branch information
kvii committed Aug 3, 2023
1 parent 31d438d commit 67da73a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/updating.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,22 @@ Output:
UPDATE "items" SET "address"='111 Test Addr',"name"='Test' []
```

<a name="set-expressions"></a>
**[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' []
```

<a name="from"></a>
**[From / Multi Table](https://godoc.org/github.com/doug-martin/goqu/#UpdateDataset.From)**

Expand Down
4 changes: 4 additions & 0 deletions exp/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions update_dataset_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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() {
Expand Down Expand Up @@ -42,6 +43,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"`
Expand Down
12 changes: 12 additions & 0 deletions update_dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}),
},
)
}

Expand Down

0 comments on commit 67da73a

Please sign in to comment.