/
update_builder.go
109 lines (93 loc) · 2.89 KB
/
update_builder.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package db
import (
"context"
"database/sql"
"reflect"
"github.com/diamcircle/go/support/errors"
)
// Exec executes the query that has been previously configured on the
// UpdateBuilder.
func (ub *UpdateBuilder) Exec(ctx context.Context) (sql.Result, error) {
r, err := ub.Table.Session.Exec(ctx, ub.sql)
if err != nil {
return nil, errors.Wrap(err, "select failed")
}
return r, nil
}
// Limit is a passthrough call to the squirrel. See
// https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Limit
func (ub *UpdateBuilder) Limit(limit uint64) *UpdateBuilder {
ub.sql = ub.sql.Limit(limit)
return ub
}
// Offset is a passthrough call to the squirrel. See
// https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Offset
func (ub *UpdateBuilder) Offset(offset uint64) *UpdateBuilder {
ub.sql = ub.sql.Offset(offset)
return ub
}
// OrderBy is a passthrough call to the squirrel. See
// https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.OrderBy
func (ub *UpdateBuilder) OrderBy(
orderBys ...string,
) *UpdateBuilder {
ub.sql = ub.sql.OrderBy(orderBys...)
return ub
}
// Prefix is a passthrough call to the squirrel. See
// https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Prefix
func (ub *UpdateBuilder) Prefix(
sql string,
args ...interface{},
) *UpdateBuilder {
ub.sql = ub.sql.Prefix(sql, args...)
return ub
}
// Set is a passthrough call to the squirrel. See
// https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Set
func (ub *UpdateBuilder) Set(column string, value interface{}) *UpdateBuilder {
ub.sql = ub.sql.Set(column, value)
return ub
}
// SetMap is a passthrough call to the squirrel. See
// https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.SetMap
func (ub *UpdateBuilder) SetMap(clauses map[string]interface{}) *UpdateBuilder {
ub.sql = ub.sql.SetMap(clauses)
return ub
}
// SetStruct is using `db` tag on the struct and updates the query with struct
// values for each field (except `ignored` fields).
func (ub *UpdateBuilder) SetStruct(s interface{}, ignored []string) *UpdateBuilder {
ignoredMap := map[string]bool{}
for _, ig := range ignored {
ignoredMap[ig] = true
}
cols := ColumnsForStruct(s)
row := reflect.ValueOf(s)
rvals := mapper.FieldsByName(row, cols)
for i, col := range cols {
if ignoredMap[col] {
continue
}
ub.Set(col, rvals[i].Interface())
}
return ub
}
// Suffix is a passthrough call to the squirrel. See
// https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Suffix
func (ub *UpdateBuilder) Suffix(
sql string,
args ...interface{},
) *UpdateBuilder {
ub.sql = ub.sql.Suffix(sql, args...)
return ub
}
// Where is a passthrough call to the squirrel. See
// https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Where
func (ub *UpdateBuilder) Where(
pred interface{},
args ...interface{},
) *UpdateBuilder {
ub.sql = ub.sql.Where(pred, args...)
return ub
}