-
Notifications
You must be signed in to change notification settings - Fork 7
/
update.go
48 lines (40 loc) · 1.28 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package goqux
import (
"errors"
"github.com/doug-martin/goqu/v9"
"github.com/doug-martin/goqu/v9/exp"
)
type UpdateOption func(table exp.IdentifierExpression, s *goqu.UpdateDataset) *goqu.UpdateDataset
func WithUpdateFilters(filters ...goqu.Expression) UpdateOption {
return func(table exp.IdentifierExpression, s *goqu.UpdateDataset) *goqu.UpdateDataset {
return s.Where(filters...)
}
}
func WithUpdateDialect(dialect string) UpdateOption {
return func(table exp.IdentifierExpression, s *goqu.UpdateDataset) *goqu.UpdateDataset {
return s.WithDialect(dialect)
}
}
func WithUpdateReturningAll() UpdateOption {
return func(table exp.IdentifierExpression, s *goqu.UpdateDataset) *goqu.UpdateDataset {
return s.Returning(goqu.Star())
}
}
func WithUpdateSet(value any) UpdateOption {
return func(table exp.IdentifierExpression, s *goqu.UpdateDataset) *goqu.UpdateDataset {
return s.Set(value)
}
}
func BuildUpdate(tableName string, value any, options ...UpdateOption) (string, []any, error) {
table := goqu.T(tableName)
q := goqu.Update(table).WithDialect(defaultDialect)
values := encodeValues(value, skipUpdate, true)
if len(values) == 0 {
return "", nil, errors.New("no values to update")
}
q = q.Set(values)
for _, o := range options {
q = o(table, q)
}
return q.ToSQL()
}