Skip to content

Commit

Permalink
Merge pull request #389 from kvii/update
Browse files Browse the repository at this point in the history
feat: allow []exp.UpdateExpression as Set values.
  • Loading branch information
funkyshu authored Dec 14, 2023
2 parents 594a433 + 67da73a commit 58b19cc
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 @@ -324,6 +324,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 @@ -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() {
Expand Down Expand Up @@ -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"`
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 58b19cc

Please sign in to comment.