-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
query_merge.go
324 lines (266 loc) · 7.19 KB
/
query_merge.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package bun
import (
"context"
"database/sql"
"errors"
"github.com/uptrace/bun/dialect"
"github.com/uptrace/bun/dialect/feature"
"github.com/uptrace/bun/internal"
"github.com/uptrace/bun/schema"
)
type MergeQuery struct {
baseQuery
returningQuery
using schema.QueryWithArgs
on schema.QueryWithArgs
when []schema.QueryAppender
}
var _ Query = (*MergeQuery)(nil)
func NewMergeQuery(db *DB) *MergeQuery {
q := &MergeQuery{
baseQuery: baseQuery{
db: db,
conn: db.DB,
},
}
if q.db.dialect.Name() != dialect.MSSQL && q.db.dialect.Name() != dialect.PG {
q.err = errors.New("bun: merge not supported for current dialect")
}
return q
}
func (q *MergeQuery) Conn(db IConn) *MergeQuery {
q.setConn(db)
return q
}
func (q *MergeQuery) Model(model interface{}) *MergeQuery {
q.setModel(model)
return q
}
func (q *MergeQuery) Err(err error) *MergeQuery {
q.setErr(err)
return q
}
// Apply calls each function in fns, passing the MergeQuery as an argument.
func (q *MergeQuery) Apply(fns ...func(*MergeQuery) *MergeQuery) *MergeQuery {
for _, fn := range fns {
if fn != nil {
q = fn(q)
}
}
return q
}
func (q *MergeQuery) With(name string, query schema.QueryAppender) *MergeQuery {
q.addWith(name, query, false)
return q
}
func (q *MergeQuery) WithRecursive(name string, query schema.QueryAppender) *MergeQuery {
q.addWith(name, query, true)
return q
}
//------------------------------------------------------------------------------
func (q *MergeQuery) Table(tables ...string) *MergeQuery {
for _, table := range tables {
q.addTable(schema.UnsafeIdent(table))
}
return q
}
func (q *MergeQuery) TableExpr(query string, args ...interface{}) *MergeQuery {
q.addTable(schema.SafeQuery(query, args))
return q
}
func (q *MergeQuery) ModelTableExpr(query string, args ...interface{}) *MergeQuery {
q.modelTableName = schema.SafeQuery(query, args)
return q
}
//------------------------------------------------------------------------------
// Returning adds a RETURNING clause to the query.
//
// To suppress the auto-generated RETURNING clause, use `Returning("NULL")`.
// Only for mssql output, postgres not supported returning in merge query
func (q *MergeQuery) Returning(query string, args ...interface{}) *MergeQuery {
q.addReturning(schema.SafeQuery(query, args))
return q
}
//------------------------------------------------------------------------------
func (q *MergeQuery) Using(s string, args ...interface{}) *MergeQuery {
q.using = schema.SafeQuery(s, args)
return q
}
func (q *MergeQuery) On(s string, args ...interface{}) *MergeQuery {
q.on = schema.SafeQuery(s, args)
return q
}
// WhenInsert for when insert clause.
func (q *MergeQuery) WhenInsert(expr string, fn func(q *InsertQuery) *InsertQuery) *MergeQuery {
sq := NewInsertQuery(q.db)
// apply the model as default into sub query, since appendColumnsValues required
if q.model != nil {
sq = sq.Model(q.model)
}
sq = sq.Apply(fn)
q.when = append(q.when, &whenInsert{expr: expr, query: sq})
return q
}
// WhenUpdate for when update clause.
func (q *MergeQuery) WhenUpdate(expr string, fn func(q *UpdateQuery) *UpdateQuery) *MergeQuery {
sq := NewUpdateQuery(q.db)
// apply the model as default into sub query
if q.model != nil {
sq = sq.Model(q.model)
}
sq = sq.Apply(fn)
q.when = append(q.when, &whenUpdate{expr: expr, query: sq})
return q
}
// WhenDelete for when delete clause.
func (q *MergeQuery) WhenDelete(expr string) *MergeQuery {
q.when = append(q.when, &whenDelete{expr: expr})
return q
}
// When for raw expression clause.
func (q *MergeQuery) When(expr string, args ...interface{}) *MergeQuery {
q.when = append(q.when, schema.SafeQuery(expr, args))
return q
}
//------------------------------------------------------------------------------
func (q *MergeQuery) Operation() string {
return "MERGE"
}
func (q *MergeQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, err error) {
if q.err != nil {
return nil, q.err
}
fmter = formatterWithModel(fmter, q)
b, err = q.appendWith(fmter, b)
if err != nil {
return nil, err
}
b = append(b, "MERGE "...)
if q.db.dialect.Name() == dialect.PG {
b = append(b, "INTO "...)
}
b, err = q.appendFirstTableWithAlias(fmter, b)
if err != nil {
return nil, err
}
b = append(b, " USING "...)
b, err = q.using.AppendQuery(fmter, b)
if err != nil {
return nil, err
}
b = append(b, " ON "...)
b, err = q.on.AppendQuery(fmter, b)
if err != nil {
return nil, err
}
for _, w := range q.when {
b = append(b, " WHEN "...)
b, err = w.AppendQuery(fmter, b)
if err != nil {
return nil, err
}
}
if q.hasFeature(feature.Output) && q.hasReturning() {
b = append(b, " OUTPUT "...)
b, err = q.appendOutput(fmter, b)
if err != nil {
return nil, err
}
}
// A MERGE statement must be terminated by a semi-colon (;).
b = append(b, ";"...)
return b, nil
}
//------------------------------------------------------------------------------
func (q *MergeQuery) Scan(ctx context.Context, dest ...interface{}) error {
_, err := q.scanOrExec(ctx, dest, true)
return err
}
func (q *MergeQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Result, error) {
return q.scanOrExec(ctx, dest, len(dest) > 0)
}
func (q *MergeQuery) scanOrExec(
ctx context.Context, dest []interface{}, hasDest bool,
) (sql.Result, error) {
if q.err != nil {
return nil, q.err
}
// Run append model hooks before generating the query.
if err := q.beforeAppendModel(ctx, q); err != nil {
return nil, err
}
// Generate the query before checking hasReturning.
queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
if err != nil {
return nil, err
}
useScan := hasDest || (q.hasReturning() && q.hasFeature(feature.InsertReturning|feature.Output))
var model Model
if useScan {
var err error
model, err = q.getModel(dest)
if err != nil {
return nil, err
}
}
query := internal.String(queryBytes)
var res sql.Result
if useScan {
res, err = q.scan(ctx, q, query, model, true)
if err != nil {
return nil, err
}
} else {
res, err = q.exec(ctx, q, query)
if err != nil {
return nil, err
}
}
return res, nil
}
func (q *MergeQuery) String() string {
buf, err := q.AppendQuery(q.db.Formatter(), nil)
if err != nil {
panic(err)
}
return string(buf)
}
//------------------------------------------------------------------------------
type whenInsert struct {
expr string
query *InsertQuery
}
func (w *whenInsert) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, err error) {
b = append(b, w.expr...)
if w.query != nil {
b = append(b, " THEN INSERT"...)
b, err = w.query.appendColumnsValues(fmter, b, true)
if err != nil {
return nil, err
}
}
return b, nil
}
type whenUpdate struct {
expr string
query *UpdateQuery
}
func (w *whenUpdate) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, err error) {
b = append(b, w.expr...)
if w.query != nil {
b = append(b, " THEN UPDATE SET "...)
b, err = w.query.appendSet(fmter, b)
if err != nil {
return nil, err
}
}
return b, nil
}
type whenDelete struct {
expr string
}
func (w *whenDelete) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, err error) {
b = append(b, w.expr...)
b = append(b, " THEN DELETE"...)
return b, nil
}