Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow []exp.UpdateExpression as Set values. #389

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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