Skip to content

Commit 7c6df26

Browse files
authored
Add WithUpdateReturning function to specify columns to return (#28)
* Add WithUpdateReturning function to specify columns to return * fix lint
1 parent 57dd00c commit 7c6df26

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

.golangci.yml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
run:
22
tests: true
3-
skip-dirs:
3+
4+
issues:
5+
exclude-dirs:
46
- bin
57
- docs
68
- example
9+
exclude-rules:
10+
# Exclude some linters from running on tests files.
11+
- path: _test\.go
12+
linters:
13+
- dupl
14+
- unused
715

816
linters-settings:
917
errcheck:
10-
ignore: fmt:.*,[rR]ead|[wW]rite|[cC]lose,io:Copy
18+
exclude-functions:
19+
- fmt:.*
20+
- "[rR]ead|[wW]rite|[cC]lose"
21+
- io:Copy
1122

1223
linters:
1324
disable-all: true
@@ -26,11 +37,3 @@ linters:
2637
- prealloc
2738
- unconvert
2839
- unused
29-
30-
issues:
31-
exclude-rules:
32-
# Exclude some linters from running on tests files.
33-
- path: _test\.go
34-
linters:
35-
- dupl
36-
- unused

update.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ func WithUpdateReturningAll() UpdateOption {
2727
}
2828
}
2929

30+
func WithUpdateReturning(columns ...string) UpdateOption {
31+
return func(table exp.IdentifierExpression, s *goqu.UpdateDataset) *goqu.UpdateDataset {
32+
cols := make([]any, 0, len(columns))
33+
for _, c := range columns {
34+
cols = append(cols, table.Col(c))
35+
}
36+
return s.Returning(cols...)
37+
}
38+
}
39+
3040
func WithUpdateSet(value any) UpdateOption {
3141
return func(table exp.IdentifierExpression, s *goqu.UpdateDataset) *goqu.UpdateDataset {
3242
return s.Set(value)

update_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ func TestBuildUpdate(t *testing.T) {
5353
expectedQuery: `UPDATE "update_models" SET "int_field"=$1 RETURNING *`,
5454
expectedArgs: []interface{}{int64(5)},
5555
},
56+
{
57+
name: "update_with_specific_returning",
58+
dst: updateModel{IntField: 5},
59+
options: []goqux.UpdateOption{goqux.WithUpdateReturning("int_field", "another_col_name")},
60+
expectedQuery: `UPDATE "update_models" SET "int_field"=$1 RETURNING "update_models"."int_field", "update_models"."another_col_name"`,
61+
expectedArgs: []interface{}{int64(5)},
62+
},
5663
{
5764
name: "update_with_zero_values",
5865
dst: updateModel{IntField: 0},

0 commit comments

Comments
 (0)