-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate.go
More file actions
175 lines (146 loc) · 5.75 KB
/
Copy pathupdate.go
File metadata and controls
175 lines (146 loc) · 5.75 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package sqx
import (
"context"
"database/sql"
"fmt"
"log"
sq "github.com/stytchauth/squirrel"
)
// UpdateBuilder wraps squirrel.UpdateBuilder and adds syntactic sugar for common usage patterns.
type UpdateBuilder struct {
builder sq.UpdateBuilder
queryable Queryable
ctx context.Context
err error
hasChanges bool
logger Logger
}
// ============================================
// BEGIN: squirrel-UpdateBuilder parity section
// ============================================
// Prefix adds an expression to the beginning of the query
func (b UpdateBuilder) Prefix(sql string, args ...interface{}) UpdateBuilder {
return b.withBuilder(b.builder.Prefix(sql, args...))
}
// PrefixExpr adds an expression to the very beginning of the query
func (b UpdateBuilder) PrefixExpr(expr Sqlizer) UpdateBuilder {
return b.withBuilder(b.builder.PrefixExpr(expr))
}
// JoinClause adds a join clause to the query.
func (b UpdateBuilder) JoinClause(pred interface{}, args ...interface{}) UpdateBuilder {
return b.withBuilder(b.builder.JoinClause(pred, args...))
}
// Join adds a JOIN clause to the query.
func (b UpdateBuilder) Join(join string, rest ...interface{}) UpdateBuilder {
return b.withBuilder(b.builder.Join(join, rest...))
}
// LeftJoin adds a LEFT JOIN clause to the query.
func (b UpdateBuilder) LeftJoin(join string, rest ...interface{}) UpdateBuilder {
return b.withBuilder(b.builder.LeftJoin(join, rest...))
}
// RightJoin adds a RIGHT JOIN clause to the query.
func (b UpdateBuilder) RightJoin(join string, rest ...interface{}) UpdateBuilder {
return b.withBuilder(b.builder.RightJoin(join, rest...))
}
// InnerJoin adds a INNER JOIN clause to the query.
func (b UpdateBuilder) InnerJoin(join string, rest ...interface{}) UpdateBuilder {
return b.withBuilder(b.builder.InnerJoin(join, rest...))
}
// CrossJoin adds a CROSS JOIN clause to the query.
func (b UpdateBuilder) CrossJoin(join string, rest ...interface{}) UpdateBuilder {
return b.withBuilder(b.builder.CrossJoin(join, rest...))
}
// Set adds SET clauses to the query.
func (b UpdateBuilder) Set(column string, value any) UpdateBuilder {
return b.
withBuilder(b.builder.Set(column, value)).
withChanges()
}
// SetMap is a convenience method which calls Set for each key/value pair in clauses.
func (b UpdateBuilder) SetMap(clauses map[string]any, errors ...error) UpdateBuilder {
for _, err := range errors {
if err != nil {
return b.withError(err)
}
}
if len(clauses) == 0 {
return b
}
return b.
withBuilder(b.builder.SetMap(clauses)).
withChanges()
}
// Where adds WHERE expressions to the query.
//
// See SelectBuilder.Where for more information.
func (b UpdateBuilder) Where(pred interface{}, rest ...interface{}) UpdateBuilder {
return b.withBuilder(b.builder.Where(pred, rest...))
}
// OrderBy adds ORDER BY expressions to the query.
func (b UpdateBuilder) OrderBy(orderBys ...string) UpdateBuilder {
return b.withBuilder(b.builder.OrderBy(orderBys...))
}
// Limit sets a LIMIT clause on the query.
func (b UpdateBuilder) Limit(limit uint64) UpdateBuilder {
return b.withBuilder(b.builder.Limit(limit))
}
// Offset sets a OFFSET clause on the query.
func (b UpdateBuilder) Offset(offset uint64) UpdateBuilder {
return b.withBuilder(b.builder.Offset(offset))
}
// Suffix adds an expression to the end of the query
func (b UpdateBuilder) Suffix(sql string, args ...interface{}) UpdateBuilder {
return b.withBuilder(b.builder.Suffix(sql, args...))
}
// SuffixExpr adds an expression to the end of the query
func (b UpdateBuilder) SuffixExpr(expr Sqlizer) UpdateBuilder {
return b.withBuilder(b.builder.SuffixExpr(expr))
}
// ==========================================
// END: squirrel-UpdateBuilder parity section
// ==========================================
// Do executes the UpdateBuilder
func (b UpdateBuilder) Do() error {
_, err := b.DoResult()
return err
}
// DoResult executes the InsertBuilder and also returns the sql.Result for a successful query. This is useful if you
// wish to check the value of the LastInsertId() or RowsAffected() methods since Do() will discard this information.
func (b UpdateBuilder) DoResult() (sql.Result, error) {
if b.err != nil {
return nil, b.err
}
if !b.hasChanges {
log.Println("Skipping write to DB - no updates set")
return EmptyResult{}, nil
}
if b.queryable == nil {
return nil, fmt.Errorf("missing queryable - call SetDefaultQueryable or WithQueryable to set it")
}
return b.builder.RunWith(runShim{b.queryable}).ExecContext(b.ctx)
}
// Debug prints the UpdateBuilder state out to the provided logger
func (b UpdateBuilder) Debug() UpdateBuilder {
debug(b.logger, b.builder)
return b
}
// WithQueryable configures a Queryable for this UpdateBuilder instance
func (b UpdateBuilder) WithQueryable(queryable Queryable) UpdateBuilder {
return UpdateBuilder{builder: b.builder, queryable: queryable, logger: b.logger, ctx: b.ctx, err: b.err, hasChanges: b.hasChanges}
}
// WithLogger configures a Queryable for this UpdateBuilder instance
func (b UpdateBuilder) WithLogger(logger Logger) UpdateBuilder {
return UpdateBuilder{builder: b.builder, queryable: b.queryable, logger: logger, ctx: b.ctx, err: b.err, hasChanges: b.hasChanges}
}
func (b UpdateBuilder) withError(err error) UpdateBuilder {
if b.err != nil {
return b
}
return UpdateBuilder{builder: b.builder, queryable: b.queryable, logger: b.logger, ctx: b.ctx, err: err, hasChanges: b.hasChanges}
}
func (b UpdateBuilder) withBuilder(builder sq.UpdateBuilder) UpdateBuilder {
return UpdateBuilder{builder: builder, queryable: b.queryable, logger: b.logger, ctx: b.ctx, err: b.err, hasChanges: b.hasChanges}
}
func (b UpdateBuilder) withChanges() UpdateBuilder {
return UpdateBuilder{builder: b.builder, queryable: b.queryable, logger: b.logger, ctx: b.ctx, err: b.err, hasChanges: true}
}