forked from elgris/sqrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.go
219 lines (180 loc) · 4.91 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package sqrl
import (
"bytes"
"context"
"database/sql"
"errors"
"strconv"
"strings"
)
type setClause struct {
column string
value interface{}
}
// Builder
// UpdateBuilder builds SQL UPDATE statements.
type UpdateBuilder struct {
StatementBuilderType
prefixes []expr
table string
setClauses map[string]interface{}
whereParts []sqlWriter
orderBys []string
limit uint64
limitValid bool
offset uint64
offsetValid bool
suffixes []expr
}
// NewUpdateBuilder creates new instance of UpdateBuilder
func NewUpdateBuilder(b StatementBuilderType) *UpdateBuilder {
return &UpdateBuilder{StatementBuilderType: b}
}
// RunWith sets a Runner (like database/sql.DB) to be used with e.g. Exec.
func (b *UpdateBuilder) RunWith(runner BaseRunner) *UpdateBuilder {
b.runWith = runner
return b
}
// Exec builds and Execs the query with the Runner set by RunWith.
func (b *UpdateBuilder) Exec() (sql.Result, error) {
return b.ExecContext(context.Background())
}
// ExecContext builds and Execs the query with the Runner set by RunWith using given context.
func (b *UpdateBuilder) ExecContext(ctx context.Context) (sql.Result, error) {
if b.runWith == nil {
return nil, ErrRunnerNotSet
}
return ExecWithContext(ctx, b.runWith, b)
}
// PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the
// query.
func (b *UpdateBuilder) PlaceholderFormat(f PlaceholderFormat) *UpdateBuilder {
b.placeholderFormat = f
return b
}
// ToSql builds the query into a SQL string and bound args.
func (b *UpdateBuilder) ToSQL() (sqlStr string, args []interface{}, err error) {
if len(b.table) == 0 {
err = errors.New("update statements must specify a table")
return
}
if len(b.setClauses) == 0 {
err = errors.New("update statements must have at least one Set clause")
return
}
sql := &bytes.Buffer{}
if len(b.prefixes) > 0 {
args, _ = appendExpressionsToSQL(sql, b.prefixes, " ", args)
sql.WriteString(" ")
}
sql.WriteString("UPDATE ")
sql.WriteString(b.table)
sql.WriteString(" SET ")
i := 0
for column, value := range b.setClauses {
if i > 0 {
sql.WriteString(", ")
}
sql.WriteString(column + " = ")
switch typedVal := value.(type) {
case sqlWriter:
var valArgs []interface{}
valArgs, err = typedVal.toSQL(sql)
if err != nil {
return
}
if len(valArgs) != 0 {
args = append(args, valArgs...)
}
default:
sql.WriteString("?")
args = append(args, typedVal)
}
i++
}
if len(b.whereParts) > 0 {
sql.WriteString(" WHERE ")
args, err = appendToSQL(b.whereParts, sql, " AND ", args)
if err != nil {
return
}
}
if len(b.orderBys) > 0 {
sql.WriteString(" ORDER BY ")
sql.WriteString(strings.Join(b.orderBys, ", "))
}
if b.limitValid {
sql.WriteString(" LIMIT ")
sql.WriteString(strconv.FormatUint(b.limit, 10))
}
if b.offsetValid {
sql.WriteString(" OFFSET ")
sql.WriteString(strconv.FormatUint(b.offset, 10))
}
if len(b.suffixes) > 0 {
sql.WriteString(" ")
args, _ = appendExpressionsToSQL(sql, b.suffixes, " ", args)
}
sqlStr, err = b.placeholderFormat.ReplacePlaceholders(sql.String())
return
}
// SQL methods
// Prefix adds an expression to the beginning of the query
func (b *UpdateBuilder) Prefix(sql string, args ...interface{}) *UpdateBuilder {
b.prefixes = append(b.prefixes, Expr(sql, args...))
return b
}
// Table sets the table to be updateb.
func (b *UpdateBuilder) Table(table string) *UpdateBuilder {
b.table = table
return b
}
// Set adds SET clauses to the query.
func (b *UpdateBuilder) Set(column string, value interface{}) *UpdateBuilder {
if b.setClauses == nil {
b.setClauses = map[string]interface{}{column: value}
} else {
b.setClauses[column] = value
}
return b
}
// SetMap is a convenience method which calls .Set for each key/value pair in clauses.
func (b *UpdateBuilder) SetMap(clauses map[string]interface{}) *UpdateBuilder {
if b.setClauses == nil {
b.setClauses = clauses
} else {
for k, v := range clauses {
b.setClauses[k] = v
}
}
return b
}
// Where adds WHERE expressions to the query.
//
// See SelectBuilder.Where for more information.
func (b *UpdateBuilder) Where(pred interface{}, args ...interface{}) *UpdateBuilder {
b.whereParts = append(b.whereParts, newWherePart(pred, args...))
return b
}
// OrderBy adds ORDER BY expressions to the query.
func (b *UpdateBuilder) OrderBy(orderBys ...string) *UpdateBuilder {
b.orderBys = append(b.orderBys, orderBys...)
return b
}
// Limit sets a LIMIT clause on the query.
func (b *UpdateBuilder) Limit(limit uint64) *UpdateBuilder {
b.limit = limit
b.limitValid = true
return b
}
// Offset sets a OFFSET clause on the query.
func (b *UpdateBuilder) Offset(offset uint64) *UpdateBuilder {
b.offset = offset
b.offsetValid = true
return b
}
// Suffix adds an expression to the end of the query
func (b *UpdateBuilder) Suffix(sql string, args ...interface{}) *UpdateBuilder {
b.suffixes = append(b.suffixes, Expr(sql, args...))
return b
}