-
Notifications
You must be signed in to change notification settings - Fork 8
/
delete_query.go
586 lines (513 loc) · 18.2 KB
/
delete_query.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
package sq
import (
"bytes"
"context"
"fmt"
)
// DeleteQuery represents an SQL DELETE query.
type DeleteQuery struct {
Dialect string
// WITH
CTEs []CTE
// DELETE FROM
DeleteTable Table
DeleteTables []Table
// USING
UsingTable Table
JoinTables []JoinTable
// WHERE
WherePredicate Predicate
// ORDER BY
OrderByFields Fields
// LIMIT
LimitRows any
// OFFSET
OffsetRows any
// RETURNING
ReturningFields []Field
}
var _ Query = (*DeleteQuery)(nil)
// WriteSQL implements the SQLWriter interface.
func (q DeleteQuery) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
var err error
// Table Policies
var policies []Predicate
policies, err = appendPolicy(ctx, dialect, policies, q.DeleteTable)
if err != nil {
return fmt.Errorf("DELETE FROM %s Policy: %w", toString(q.Dialect, q.DeleteTable), err)
}
policies, err = appendPolicy(ctx, dialect, policies, q.UsingTable)
if err != nil {
return fmt.Errorf("USING %s Policy: %w", toString(q.Dialect, q.UsingTable), err)
}
for _, joinTable := range q.JoinTables {
policies, err = appendPolicy(ctx, dialect, policies, joinTable.Table)
if err != nil {
return fmt.Errorf("%s %s Policy: %w", joinTable.JoinOperator, joinTable.Table, err)
}
}
if len(policies) > 0 {
if q.WherePredicate != nil {
policies = append(policies, q.WherePredicate)
}
q.WherePredicate = And(policies...)
}
// WITH
if len(q.CTEs) > 0 {
err = writeCTEs(ctx, dialect, buf, args, params, q.CTEs)
if err != nil {
return fmt.Errorf("WITH: %w", err)
}
}
// DELETE FROM
if (dialect == DialectMySQL || dialect == DialectSQLServer) && len(q.DeleteTables) > 0 {
buf.WriteString("DELETE ")
if len(q.DeleteTables) > 1 && dialect != DialectMySQL {
return fmt.Errorf("dialect %q does not support multi-table DELETE", dialect)
}
for i, table := range q.DeleteTables {
if i > 0 {
buf.WriteString(", ")
}
if alias := getAlias(table); alias != "" {
buf.WriteString(alias)
} else {
err = table.WriteSQL(ctx, dialect, buf, args, params)
if err != nil {
return fmt.Errorf("table #%d: %w", i+1, err)
}
}
}
} else {
buf.WriteString("DELETE FROM ")
if q.DeleteTable == nil {
return fmt.Errorf("no table provided to DELETE FROM")
}
err = q.DeleteTable.WriteSQL(ctx, dialect, buf, args, params)
if err != nil {
return fmt.Errorf("DELETE FROM: %w", err)
}
if dialect != DialectSQLServer {
if alias := getAlias(q.DeleteTable); alias != "" {
buf.WriteString(" AS " + QuoteIdentifier(dialect, alias))
}
}
}
if q.UsingTable != nil || len(q.JoinTables) > 0 {
if dialect != DialectPostgres && dialect != DialectMySQL && dialect != DialectSQLServer {
return fmt.Errorf("%s DELETE does not support JOIN", dialect)
}
}
// OUTPUT
if len(q.ReturningFields) > 0 && dialect == DialectSQLServer {
buf.WriteString(" OUTPUT ")
err = writeFieldsWithPrefix(ctx, dialect, buf, args, params, q.ReturningFields, "DELETED", true)
if err != nil {
return err
}
}
// USING/FROM
if q.UsingTable != nil {
switch dialect {
case DialectPostgres:
buf.WriteString(" USING ")
err = q.UsingTable.WriteSQL(ctx, dialect, buf, args, params)
if err != nil {
return fmt.Errorf("USING: %w", err)
}
case DialectMySQL, DialectSQLServer:
buf.WriteString(" FROM ")
err = q.UsingTable.WriteSQL(ctx, dialect, buf, args, params)
if err != nil {
return fmt.Errorf("FROM: %w", err)
}
}
if alias := getAlias(q.UsingTable); alias != "" {
buf.WriteString(" AS " + QuoteIdentifier(dialect, alias))
}
}
// JOIN
if len(q.JoinTables) > 0 {
if q.UsingTable == nil {
return fmt.Errorf("%s can't JOIN without a USING/FROM table", dialect)
}
buf.WriteString(" ")
err = writeJoinTables(ctx, dialect, buf, args, params, q.JoinTables)
if err != nil {
return fmt.Errorf("JOIN: %w", err)
}
}
// WHERE
if q.WherePredicate != nil {
buf.WriteString(" WHERE ")
switch predicate := q.WherePredicate.(type) {
case VariadicPredicate:
predicate.Toplevel = true
err = predicate.WriteSQL(ctx, dialect, buf, args, params)
if err != nil {
return fmt.Errorf("WHERE: %w", err)
}
default:
err = q.WherePredicate.WriteSQL(ctx, dialect, buf, args, params)
if err != nil {
return fmt.Errorf("WHERE: %w", err)
}
}
}
// ORDER BY
if len(q.OrderByFields) > 0 {
if dialect != DialectMySQL {
return fmt.Errorf("%s UPDATE does not support ORDER BY", dialect)
}
buf.WriteString(" ORDER BY ")
err = q.OrderByFields.WriteSQL(ctx, dialect, buf, args, params)
if err != nil {
return fmt.Errorf("ORDER BY: %w", err)
}
}
// LIMIT
if q.LimitRows != nil {
if dialect != DialectMySQL {
return fmt.Errorf("%s UPDATE does not support LIMIT", dialect)
}
buf.WriteString(" LIMIT ")
err = WriteValue(ctx, dialect, buf, args, params, q.LimitRows)
if err != nil {
return fmt.Errorf("LIMIT: %w", err)
}
}
// RETURNING
if len(q.ReturningFields) > 0 && dialect != DialectSQLServer {
if dialect != DialectPostgres && dialect != DialectSQLite && dialect != DialectMySQL {
return fmt.Errorf("%s UPDATE does not support RETURNING", dialect)
}
buf.WriteString(" RETURNING ")
err = writeFields(ctx, dialect, buf, args, params, q.ReturningFields, true)
if err != nil {
return fmt.Errorf("RETURNING: %w", err)
}
}
return nil
}
// DeleteFrom returns a new DeleteQuery.
func DeleteFrom(table Table) DeleteQuery {
return DeleteQuery{DeleteTable: table}
}
// Where appends to the WherePredicate field of the DeleteQuery.
func (q DeleteQuery) Where(predicates ...Predicate) DeleteQuery {
q.WherePredicate = appendPredicates(q.WherePredicate, predicates)
return q
}
// SetFetchableFields implements the Query interface.
func (q DeleteQuery) SetFetchableFields(fields []Field) (query Query, ok bool) {
switch q.Dialect {
case DialectPostgres, DialectSQLite:
if len(q.ReturningFields) == 0 {
q.ReturningFields = fields
return q, true
}
return q, false
default:
return q, false
}
}
// GetFetchableFields returns the fetchable fields of the query.
func (q DeleteQuery) GetFetchableFields() []Field {
switch q.Dialect {
case DialectPostgres, DialectSQLite:
return q.ReturningFields
default:
return nil
}
}
// GetDialect implements the Query interface.
func (q DeleteQuery) GetDialect() string { return q.Dialect }
// SetDialect sets the dialect of the query.
func (q DeleteQuery) SetDialect(dialect string) DeleteQuery {
q.Dialect = dialect
return q
}
// SQLiteDeleteQuery represents an SQLite DELETE query.
type SQLiteDeleteQuery DeleteQuery
var _ Query = (*SQLiteDeleteQuery)(nil)
// WriteSQL implements the SQLWriter interface.
func (q SQLiteDeleteQuery) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
return DeleteQuery(q).WriteSQL(ctx, dialect, buf, args, params)
}
// DeleteFrom returns a new SQLiteDeleteQuery.
func (b sqliteQueryBuilder) DeleteFrom(table Table) SQLiteDeleteQuery {
return SQLiteDeleteQuery{
Dialect: DialectSQLite,
CTEs: b.ctes,
DeleteTable: table,
}
}
// Where appends to the WherePredicate field of the SQLiteDeleteQuery.
func (q SQLiteDeleteQuery) Where(predicates ...Predicate) SQLiteDeleteQuery {
q.WherePredicate = appendPredicates(q.WherePredicate, predicates)
return q
}
// Returning appends fields to the RETURNING clause of the SQLiteDeleteQuery.
func (q SQLiteDeleteQuery) Returning(fields ...Field) SQLiteDeleteQuery {
q.ReturningFields = append(q.ReturningFields, fields...)
return q
}
// SetFetchableFields implements the Query interface.
func (q SQLiteDeleteQuery) SetFetchableFields(fields []Field) (query Query, ok bool) {
return DeleteQuery(q).SetFetchableFields(fields)
}
// GetFetchableFields returns the fetchable fields of the query.
func (q SQLiteDeleteQuery) GetFetchableFields() []Field {
return DeleteQuery(q).GetFetchableFields()
}
// GetDialect implements the Query interface.
func (q SQLiteDeleteQuery) GetDialect() string { return q.Dialect }
// SetDialect sets the dialect of the query.
func (q SQLiteDeleteQuery) SetDialect(dialect string) SQLiteDeleteQuery {
q.Dialect = dialect
return q
}
// PostgresDeleteQuery represents a Postgres DELETE query.
type PostgresDeleteQuery DeleteQuery
var _ Query = (*PostgresDeleteQuery)(nil)
// WriteSQL implements the SQLWriter interface.
func (q PostgresDeleteQuery) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
return DeleteQuery(q).WriteSQL(ctx, dialect, buf, args, params)
}
// DeleteFrom returns a new PostgresDeleteQuery.
func (b postgresQueryBuilder) DeleteFrom(table Table) PostgresDeleteQuery {
return PostgresDeleteQuery{
Dialect: DialectPostgres,
CTEs: b.ctes,
DeleteTable: table,
}
}
// Using sets the UsingTable field of the PostgresDeleteQuery.
func (q PostgresDeleteQuery) Using(table Table) PostgresDeleteQuery {
q.UsingTable = table
return q
}
// Join joins a new Table to the PostgresDeleteQuery.
func (q PostgresDeleteQuery) Join(table Table, predicates ...Predicate) PostgresDeleteQuery {
q.JoinTables = append(q.JoinTables, Join(table, predicates...))
return q
}
// LeftJoin left joins a new Table to the PostgresDeleteQuery.
func (q PostgresDeleteQuery) LeftJoin(table Table, predicates ...Predicate) PostgresDeleteQuery {
q.JoinTables = append(q.JoinTables, LeftJoin(table, predicates...))
return q
}
// FullJoin full joins a new Table to the PostgresDeleteQuery.
func (q PostgresDeleteQuery) FullJoin(table Table, predicates ...Predicate) PostgresDeleteQuery {
q.JoinTables = append(q.JoinTables, FullJoin(table, predicates...))
return q
}
// CrossJoin cross joins a new Table to the PostgresDeleteQuery.
func (q PostgresDeleteQuery) CrossJoin(table Table) PostgresDeleteQuery {
q.JoinTables = append(q.JoinTables, CrossJoin(table))
return q
}
// CustomJoin joins a new Table to the PostgresDeleteQuery with a custom join
// operator.
func (q PostgresDeleteQuery) CustomJoin(joinOperator string, table Table, predicates ...Predicate) PostgresDeleteQuery {
q.JoinTables = append(q.JoinTables, CustomJoin(joinOperator, table, predicates...))
return q
}
// JoinUsing joins a new Table to the PostgresDeleteQuery with the USING operator.
func (q PostgresDeleteQuery) JoinUsing(table Table, fields ...Field) PostgresDeleteQuery {
q.JoinTables = append(q.JoinTables, JoinUsing(table, fields...))
return q
}
// Where appends to the WherePredicate field of the PostgresDeleteQuery.
func (q PostgresDeleteQuery) Where(predicates ...Predicate) PostgresDeleteQuery {
q.WherePredicate = appendPredicates(q.WherePredicate, predicates)
return q
}
// Returning appends fields to the RETURNING clause of the PostgresDeleteQuery.
func (q PostgresDeleteQuery) Returning(fields ...Field) PostgresDeleteQuery {
q.ReturningFields = append(q.ReturningFields, fields...)
return q
}
// SetFetchableFields implements the Query interface.
func (q PostgresDeleteQuery) SetFetchableFields(fields []Field) (query Query, ok bool) {
return DeleteQuery(q).SetFetchableFields(fields)
}
// GetFetchableFields returns the fetchable fields of the query.
func (q PostgresDeleteQuery) GetFetchableFields() []Field {
return DeleteQuery(q).GetFetchableFields()
}
// GetDialect implements the Query interface.
func (q PostgresDeleteQuery) GetDialect() string { return q.Dialect }
// SetDialect sets the dialect of the query.
func (q PostgresDeleteQuery) SetDialect(dialect string) PostgresDeleteQuery {
q.Dialect = dialect
return q
}
// MySQLDeleteQuery represents a MySQL DELETE query.
type MySQLDeleteQuery DeleteQuery
var _ Query = (*MySQLDeleteQuery)(nil)
// WriteSQL implements the SQLWriter interface.
func (q MySQLDeleteQuery) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
return DeleteQuery(q).WriteSQL(ctx, dialect, buf, args, params)
}
// DeleteFrom returns a new MySQLDeleteQuery.
func (b mysqlQueryBuilder) DeleteFrom(table Table) MySQLDeleteQuery {
return MySQLDeleteQuery{
Dialect: DialectMySQL,
CTEs: b.ctes,
DeleteTable: table,
}
}
// Delete returns a new MySQLDeleteQuery.
func (b mysqlQueryBuilder) Delete(tables ...Table) MySQLDeleteQuery {
return MySQLDeleteQuery{
Dialect: DialectMySQL,
CTEs: b.ctes,
DeleteTables: tables,
}
}
// From sets the UsingTable of the MySQLDeleteQuery.
func (q MySQLDeleteQuery) From(table Table) MySQLDeleteQuery {
q.UsingTable = table
return q
}
// Join joins a new Table to the MySQLDeleteQuery.
func (q MySQLDeleteQuery) Join(table Table, predicates ...Predicate) MySQLDeleteQuery {
q.JoinTables = append(q.JoinTables, Join(table, predicates...))
return q
}
// LeftJoin left joins a new Table to the MySQLDeleteQuery.
func (q MySQLDeleteQuery) LeftJoin(table Table, predicates ...Predicate) MySQLDeleteQuery {
q.JoinTables = append(q.JoinTables, LeftJoin(table, predicates...))
return q
}
// FullJoin full joins a new Table to the MySQLDeleteQuery.
func (q MySQLDeleteQuery) FullJoin(table Table, predicates ...Predicate) MySQLDeleteQuery {
q.JoinTables = append(q.JoinTables, FullJoin(table, predicates...))
return q
}
// CrossJoin cross joins a new Table to the MySQLDeleteQuery.
func (q MySQLDeleteQuery) CrossJoin(table Table) MySQLDeleteQuery {
q.JoinTables = append(q.JoinTables, CrossJoin(table))
return q
}
// CustomJoin joins a new Table to the MySQLDeleteQuery with a custom join
// operator.
func (q MySQLDeleteQuery) CustomJoin(joinOperator string, table Table, predicates ...Predicate) MySQLDeleteQuery {
q.JoinTables = append(q.JoinTables, CustomJoin(joinOperator, table, predicates...))
return q
}
// JoinUsing joins a new Table to the MySQLDeleteQuery with the USING operator.
func (q MySQLDeleteQuery) JoinUsing(table Table, fields ...Field) MySQLDeleteQuery {
q.JoinTables = append(q.JoinTables, JoinUsing(table, fields...))
return q
}
// Where appends to the WherePredicate field of the MySQLDeleteQuery.
func (q MySQLDeleteQuery) Where(predicates ...Predicate) MySQLDeleteQuery {
q.WherePredicate = appendPredicates(q.WherePredicate, predicates)
return q
}
// OrderBy sets the OrderByFields field of the MySQLDeleteQuery.
func (q MySQLDeleteQuery) OrderBy(fields ...Field) MySQLDeleteQuery {
q.OrderByFields = append(q.OrderByFields, fields...)
return q
}
// Limit sets the LimitRows field of the MySQLDeleteQuery.
func (q MySQLDeleteQuery) Limit(limit any) MySQLDeleteQuery {
q.LimitRows = limit
return q
}
// Returning appends fields to the RETURNING clause of the MySQLDeleteQuery.
func (q MySQLDeleteQuery) Returning(fields ...Field) MySQLDeleteQuery {
q.ReturningFields = append(q.ReturningFields, fields...)
return q
}
// SetFetchableFields implements the Query interface.
func (q MySQLDeleteQuery) SetFetchableFields(fields []Field) (query Query, ok bool) {
return DeleteQuery(q).SetFetchableFields(fields)
}
// GetFetchableFields returns the fetchable fields of the query.
func (q MySQLDeleteQuery) GetFetchableFields() []Field {
return DeleteQuery(q).GetFetchableFields()
}
// GetDialect implements the Query interface.
func (q MySQLDeleteQuery) GetDialect() string { return q.Dialect }
// SetDialect sets the dialect of the query.
func (q MySQLDeleteQuery) SetDialect(dialect string) MySQLDeleteQuery {
q.Dialect = dialect
return q
}
// SQLServerDeleteQuery represents an SQL Server DELETE query.
type SQLServerDeleteQuery DeleteQuery
var _ Query = (*SQLServerDeleteQuery)(nil)
// WriteSQL implements the SQLWriter interface.
func (q SQLServerDeleteQuery) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
return DeleteQuery(q).WriteSQL(ctx, dialect, buf, args, params)
}
// DeleteFrom returns a new SQLServerDeleteQuery.
func (b sqlserverQueryBuilder) DeleteFrom(table Table) SQLServerDeleteQuery {
return SQLServerDeleteQuery{
Dialect: DialectSQLServer,
CTEs: b.ctes,
DeleteTable: table,
}
}
// Delete returns a new SQLServerDeleteQuery.
func (b sqlserverQueryBuilder) Delete(table Table) SQLServerDeleteQuery {
return SQLServerDeleteQuery{
Dialect: DialectSQLServer,
CTEs: b.ctes,
DeleteTables: []Table{table},
}
}
// From sets the UsingTable of the SQLServerDeleteQuery.
func (q SQLServerDeleteQuery) From(table Table) SQLServerDeleteQuery {
q.UsingTable = table
return q
}
// Join joins a new Table to the SQLServerDeleteQuery.
func (q SQLServerDeleteQuery) Join(table Table, predicates ...Predicate) SQLServerDeleteQuery {
q.JoinTables = append(q.JoinTables, Join(table, predicates...))
return q
}
// LeftJoin left joins a new Table to the SQLServerDeleteQuery.
func (q SQLServerDeleteQuery) LeftJoin(table Table, predicates ...Predicate) SQLServerDeleteQuery {
q.JoinTables = append(q.JoinTables, LeftJoin(table, predicates...))
return q
}
// FullJoin full joins a new Table to the SQLServerDeleteQuery.
func (q SQLServerDeleteQuery) FullJoin(table Table, predicates ...Predicate) SQLServerDeleteQuery {
q.JoinTables = append(q.JoinTables, FullJoin(table, predicates...))
return q
}
// CrossJoin cross joins a new Table to the SQLServerDeleteQuery.
func (q SQLServerDeleteQuery) CrossJoin(table Table) SQLServerDeleteQuery {
q.JoinTables = append(q.JoinTables, CrossJoin(table))
return q
}
// CustomJoin joins a new Table to the SQLServerDeleteQuery with a custom join
// operator.
func (q SQLServerDeleteQuery) CustomJoin(joinOperator string, table Table, predicates ...Predicate) SQLServerDeleteQuery {
q.JoinTables = append(q.JoinTables, CustomJoin(joinOperator, table, predicates...))
return q
}
// Where appends to the WherePredicate field of the SQLServerDeleteQuery.
func (q SQLServerDeleteQuery) Where(predicates ...Predicate) SQLServerDeleteQuery {
q.WherePredicate = appendPredicates(q.WherePredicate, predicates)
return q
}
// SetFetchableFields implements the Query interface.
func (q SQLServerDeleteQuery) SetFetchableFields(fields []Field) (query Query, ok bool) {
return DeleteQuery(q).SetFetchableFields(fields)
}
// GetFetchableFields returns the fetchable fields of the query.
func (q SQLServerDeleteQuery) GetFetchableFields() []Field {
return DeleteQuery(q).GetFetchableFields()
}
// GetDialect implements the Query interface.
func (q SQLServerDeleteQuery) GetDialect() string { return q.Dialect }
// SetDialect sets the dialect of the query.
func (q SQLServerDeleteQuery) SetDialect(dialect string) SQLServerDeleteQuery {
q.Dialect = dialect
return q
}