• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bokwoon95 / sq / 4698610118

pending completion
4698610118

push

github

bokwoon
add tentative RETURNING support for MySQL

10 of 10 new or added lines in 2 files covered. (100.0%)

4683 of 5966 relevant lines covered (78.49%)

38.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.29
/insert_query.go
1
package sq
2

3
import (
4
        "bytes"
5
        "context"
6
        "fmt"
7
)
8

9
// InsertQuery represents an SQL INSERT query.
10
type InsertQuery struct {
11
        Dialect      string
12
        ColumnMapper func(*Column)
13
        // WITH
14
        CTEs []CTE
15
        // INSERT INTO
16
        InsertIgnore  bool
17
        InsertTable   Table
18
        InsertColumns []Field
19
        // VALUES
20
        RowValues []RowValue
21
        RowAlias  string
22
        // SELECT
23
        SelectQuery Query
24
        // ON CONFLICT
25
        Conflict ConflictClause
26
        // RETURNING
27
        ReturningFields []Field
28
}
29

30
var _ Query = (*InsertQuery)(nil)
31

32
// WriteSQL implements the SQLWriter interface.
33
func (q InsertQuery) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) (err error) {
47✔
34
        if q.ColumnMapper != nil {
74✔
35
                col := &Column{
27✔
36
                        dialect:  q.Dialect,
27✔
37
                        isUpdate: false,
27✔
38
                }
27✔
39
                defer recoverPanic(&err)
27✔
40
                q.ColumnMapper(col)
27✔
41
                if err != nil {
27✔
42
                        return err
×
43
                }
×
44
                q.InsertColumns, q.RowValues = col.insertColumns, col.rowValues
26✔
45
        }
46
        // WITH
47
        if len(q.CTEs) > 0 {
61✔
48
                if dialect == DialectMySQL {
16✔
49
                        return fmt.Errorf("mysql does not support CTEs with INSERT")
1✔
50
                }
1✔
51
                err = writeCTEs(ctx, dialect, buf, args, params, q.CTEs)
14✔
52
                if err != nil {
15✔
53
                        return fmt.Errorf("WITH: %w", err)
1✔
54
                }
1✔
55
        }
56
        // INSERT INTO
57
        if q.InsertIgnore {
46✔
58
                if dialect != DialectMySQL {
3✔
59
                        return fmt.Errorf("%s does not support INSERT IGNORE", dialect)
1✔
60
                }
1✔
61
                buf.WriteString("INSERT IGNORE INTO ")
1✔
62
        } else {
42✔
63
                buf.WriteString("INSERT INTO ")
42✔
64
        }
42✔
65
        if q.InsertTable == nil {
44✔
66
                return fmt.Errorf("no table provided to INSERT")
1✔
67
        }
1✔
68
        err = q.InsertTable.WriteSQL(ctx, dialect, buf, args, params)
42✔
69
        if err != nil {
43✔
70
                return fmt.Errorf("INSERT INTO: %w", err)
1✔
71
        }
1✔
72
        if alias := getAlias(q.InsertTable); alias != "" {
55✔
73
                if dialect == DialectMySQL || dialect == DialectSQLServer {
15✔
74
                        return fmt.Errorf("%s does not allow an alias for the INSERT table", dialect)
1✔
75
                }
1✔
76
                buf.WriteString(" AS " + QuoteIdentifier(dialect, alias))
13✔
77
        }
78
        // Columns
79
        if len(q.InsertColumns) > 0 {
78✔
80
                buf.WriteString(" (")
38✔
81
                err = writeFieldsWithPrefix(ctx, dialect, buf, args, params, q.InsertColumns, "", false)
38✔
82
                if err != nil {
39✔
83
                        return fmt.Errorf("INSERT INTO: %w", err)
1✔
84
                }
1✔
85
                buf.WriteString(")")
37✔
86
        }
87
        // OUTPUT
88
        if len(q.ReturningFields) > 0 && dialect == DialectSQLServer {
39✔
89
                buf.WriteString(" OUTPUT ")
×
90
                for i, field := range q.ReturningFields {
×
91
                        if i > 0 {
×
92
                                buf.WriteString(", ")
×
93
                        }
×
94
                        err = WriteValue(ctx, dialect, buf, args, params, withPrefix(field, "INSERTED"))
×
95
                        if err != nil {
×
96
                                return err
×
97
                        }
×
98
                        if alias := getAlias(field); alias != "" {
×
99
                                buf.WriteString(" AS " + QuoteIdentifier(dialect, alias))
×
100
                        }
×
101
                }
102
        }
103
        // VALUES
104
        if len(q.RowValues) > 0 {
70✔
105
                buf.WriteString(" VALUES ")
31✔
106
                err = RowValues(q.RowValues).WriteSQL(ctx, dialect, buf, args, params)
31✔
107
                if err != nil {
32✔
108
                        return fmt.Errorf("VALUES: %w", err)
1✔
109
                }
1✔
110
                if q.RowAlias != "" {
32✔
111
                        if dialect != DialectMySQL {
3✔
112
                                return fmt.Errorf("%s does not support row aliases", dialect)
1✔
113
                        }
1✔
114
                        buf.WriteString(" AS " + q.RowAlias)
1✔
115
                }
116
        } else if q.SelectQuery != nil { // SELECT
14✔
117
                buf.WriteString(" ")
6✔
118
                err = q.SelectQuery.WriteSQL(ctx, dialect, buf, args, params)
6✔
119
                if err != nil {
7✔
120
                        return fmt.Errorf("SELECT: %w", err)
1✔
121
                }
1✔
122
        } else {
2✔
123
                return fmt.Errorf("InsertQuery missing RowValues and SelectQuery (either one is required)")
2✔
124
        }
2✔
125
        // ON CONFLICT
126
        err = q.Conflict.WriteSQL(ctx, dialect, buf, args, params)
34✔
127
        if err != nil {
41✔
128
                return err
7✔
129
        }
7✔
130
        // RETURNING
131
        if len(q.ReturningFields) > 0 && dialect != DialectSQLServer {
31✔
132
                if dialect != DialectPostgres && dialect != DialectSQLite && dialect != DialectMySQL {
4✔
133
                        return fmt.Errorf("%s INSERT does not support RETURNING", dialect)
×
134
                }
×
135
                buf.WriteString(" RETURNING ")
4✔
136
                err = writeFields(ctx, dialect, buf, args, params, q.ReturningFields, true)
4✔
137
                if err != nil {
5✔
138
                        return fmt.Errorf("RETURNING: %w", err)
1✔
139
                }
1✔
140
        }
141
        return nil
26✔
142
}
143

144
// InsertInto creates a new InsertQuery.
145
func InsertInto(table Table) InsertQuery {
4✔
146
        return InsertQuery{InsertTable: table}
4✔
147
}
4✔
148

149
// Columns sets the InsertColumns field of the InsertQuery.
150
func (q InsertQuery) Columns(fields ...Field) InsertQuery {
×
151
        q.InsertColumns = fields
×
152
        return q
×
153
}
×
154

155
// Values sets the RowValues field of the InsertQuery.
156
func (q InsertQuery) Values(values ...any) InsertQuery {
×
157
        q.RowValues = append(q.RowValues, values)
×
158
        return q
×
159
}
×
160

161
// ColumnValues sets the ColumnMapper field of the InsertQuery.
162
func (q InsertQuery) ColumnValues(colmapper func(*Column)) InsertQuery {
4✔
163
        q.ColumnMapper = colmapper
4✔
164
        return q
4✔
165
}
4✔
166

167
// Select sets the SelectQuery field of the InsertQuery.
168
func (q InsertQuery) Select(query Query) InsertQuery {
×
169
        q.SelectQuery = query
×
170
        return q
×
171
}
×
172

173
// ConflictClause represents an SQL conflict clause e.g. ON CONFLICT DO
174
// NOTHING/DO UPDATE or ON DUPLICATE KEY UPDATE.
175
type ConflictClause struct {
176
        ConstraintName      string
177
        Fields              []Field
178
        Predicate           Predicate
179
        DoNothing           bool
180
        Resolution          []Assignment
181
        ResolutionPredicate Predicate
182
}
183

184
// WriteSQL implements the SQLWriter interface.
185
func (c ConflictClause) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
34✔
186
        var err error
34✔
187
        if c.ConstraintName == "" && len(c.Fields) == 0 && len(c.Resolution) == 0 && !c.DoNothing {
55✔
188
                return nil
21✔
189
        }
21✔
190
        if dialect != DialectSQLite && dialect != DialectPostgres && dialect != DialectMySQL {
13✔
191
                return nil
×
192
        }
×
193
        if dialect == DialectMySQL {
16✔
194
                if len(c.Resolution) > 0 {
6✔
195
                        buf.WriteString(" ON DUPLICATE KEY UPDATE ")
3✔
196
                        err = Assignments(c.Resolution).WriteSQL(ctx, dialect, buf, args, params)
3✔
197
                        if err != nil {
4✔
198
                                return fmt.Errorf("ON DUPLICATE KEY UPDATE: %w", err)
1✔
199
                        }
1✔
200
                }
201
                return nil
2✔
202
        }
203
        buf.WriteString(" ON CONFLICT")
10✔
204
        if c.ConstraintName != "" {
11✔
205
                buf.WriteString(" ON CONSTRAINT " + QuoteIdentifier(dialect, c.ConstraintName))
1✔
206
        } else if len(c.Fields) > 0 {
19✔
207
                buf.WriteString(" (")
9✔
208
                err = writeFieldsWithPrefix(ctx, dialect, buf, args, params, c.Fields, "", false)
9✔
209
                if err != nil {
10✔
210
                        return fmt.Errorf("ON CONFLICT: %w", err)
1✔
211
                }
1✔
212
                buf.WriteString(")")
8✔
213
                if c.Predicate != nil {
12✔
214
                        buf.WriteString(" WHERE ")
4✔
215
                        switch predicate := c.Predicate.(type) {
4✔
216
                        case VariadicPredicate:
3✔
217
                                predicate.Toplevel = true
3✔
218
                                err = predicate.WriteSQL(ctx, dialect, buf, args, params)
3✔
219
                                if err != nil {
4✔
220
                                        return fmt.Errorf("ON CONFLICT ... WHERE: %w", err)
1✔
221
                                }
1✔
222
                        default:
1✔
223
                                err = c.Predicate.WriteSQL(ctx, dialect, buf, args, params)
1✔
224
                                if err != nil {
2✔
225
                                        return fmt.Errorf("ON CONFLICT ... WHERE: %w", err)
1✔
226
                                }
1✔
227
                        }
228
                }
229
        }
230
        if len(c.Resolution) == 0 || c.DoNothing {
9✔
231
                buf.WriteString(" DO NOTHING")
2✔
232
                return nil
2✔
233
        }
2✔
234
        buf.WriteString(" DO UPDATE SET ")
5✔
235
        err = Assignments(c.Resolution).WriteSQL(ctx, dialect, buf, args, params)
5✔
236
        if err != nil {
7✔
237
                return fmt.Errorf("DO UPDATE SET: %w", err)
2✔
238
        }
2✔
239
        if c.ResolutionPredicate != nil {
6✔
240
                buf.WriteString(" WHERE ")
3✔
241
                switch predicate := c.ResolutionPredicate.(type) {
3✔
242
                case VariadicPredicate:
2✔
243
                        predicate.Toplevel = true
2✔
244
                        err = predicate.WriteSQL(ctx, dialect, buf, args, params)
2✔
245
                        if err != nil {
2✔
246
                                return fmt.Errorf("DO UPDATE SET ... WHERE: %w", err)
×
247
                        }
×
248
                default:
1✔
249
                        err = c.ResolutionPredicate.WriteSQL(ctx, dialect, buf, args, params)
1✔
250
                        if err != nil {
2✔
251
                                return fmt.Errorf("DO UPDATE SET ... WHERE: %w", err)
1✔
252
                        }
1✔
253
                }
254
        }
255
        return nil
2✔
256
}
257

258
// SetFetchableFields implements the Query interface.
259
func (q InsertQuery) SetFetchableFields(fields []Field) (query Query, ok bool) {
4✔
260
        switch q.Dialect {
4✔
261
        case DialectPostgres, DialectSQLite:
2✔
262
                q.ReturningFields = fields
2✔
263
                return q, true
2✔
264
        default:
2✔
265
                return q, false
2✔
266
        }
267
}
268

269
// GetFetchableFields returns the fetchable fields of the query.
270
func (q InsertQuery) GetFetchableFields() []Field {
4✔
271
        switch q.Dialect {
4✔
272
        case DialectPostgres, DialectSQLite:
2✔
273
                return q.ReturningFields
2✔
274
        default:
2✔
275
                return nil
2✔
276
        }
277
}
278

279
// GetDialect implements the Query interface.
280
func (q InsertQuery) GetDialect() string { return q.Dialect }
26✔
281

282
// SetDialect sets the dialect of the query.
283
func (q InsertQuery) SetDialect(dialect string) InsertQuery {
4✔
284
        q.Dialect = dialect
4✔
285
        return q
4✔
286
}
4✔
287

288
// SQLiteInsertQuery represents an SQLite INSERT query.
289
type SQLiteInsertQuery InsertQuery
290

291
var _ Query = (*SQLiteInsertQuery)(nil)
292

293
// WriteSQL implements the SQLWriter interface.
294
func (q SQLiteInsertQuery) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
8✔
295
        return InsertQuery(q).WriteSQL(ctx, dialect, buf, args, params)
8✔
296
}
8✔
297

298
// InsertInto creates a new SQLiteInsertQuery.
299
func (b sqliteQueryBuilder) InsertInto(table Table) SQLiteInsertQuery {
9✔
300
        return SQLiteInsertQuery{
9✔
301
                Dialect:     DialectSQLite,
9✔
302
                CTEs:        b.ctes,
9✔
303
                InsertTable: table,
9✔
304
        }
9✔
305
}
9✔
306

307
// Columns sets the InsertColumns field of the SQLiteInsertQuery.
308
func (q SQLiteInsertQuery) Columns(fields ...Field) SQLiteInsertQuery {
4✔
309
        q.InsertColumns = fields
4✔
310
        return q
4✔
311
}
4✔
312

313
// Values sets the RowValues field of the SQLiteInsertQuery.
314
func (q SQLiteInsertQuery) Values(values ...any) SQLiteInsertQuery {
6✔
315
        q.RowValues = append(q.RowValues, values)
6✔
316
        return q
6✔
317
}
6✔
318

319
// ColumnValues sets the ColumnMapper field of the SQLiteInsertQuery.
320
func (q SQLiteInsertQuery) ColumnValues(colmapper func(*Column)) SQLiteInsertQuery {
4✔
321
        q.ColumnMapper = colmapper
4✔
322
        return q
4✔
323
}
4✔
324

325
// Select sets the SelectQuery field of the SQLiteInsertQuery.
326
func (q SQLiteInsertQuery) Select(query Query) SQLiteInsertQuery {
1✔
327
        q.SelectQuery = query
1✔
328
        return q
1✔
329
}
1✔
330

331
type sqliteInsertConflict struct{ q *SQLiteInsertQuery }
332

333
// OnConflict starts the ON CONFLICT clause of the SQLiteInsertQuery.
334
func (q SQLiteInsertQuery) OnConflict(fields ...Field) sqliteInsertConflict {
2✔
335
        q.Conflict.Fields = fields
2✔
336
        return sqliteInsertConflict{q: &q}
2✔
337
}
2✔
338

339
// Where adds predicates to the ON CONFLICT clause of the SQLiteInsertQuery.
340
func (c sqliteInsertConflict) Where(predicates ...Predicate) sqliteInsertConflict {
1✔
341
        c.q.Conflict.Predicate = appendPredicates(c.q.Conflict.Predicate, predicates)
1✔
342
        return c
1✔
343
}
1✔
344

345
// DoNothing resolves the ON CONFLICT clause of the SQLiteInsertQuery with DO
346
// NOTHING.
347
func (c sqliteInsertConflict) DoNothing() SQLiteInsertQuery {
1✔
348
        c.q.Conflict.DoNothing = true
1✔
349
        return *c.q
1✔
350
}
1✔
351

352
// DoUpdateSet resolves the ON CONFLICT CLAUSE of the SQLiteInsertQuery with DO UPDATE SET.
353
func (c sqliteInsertConflict) DoUpdateSet(assignments ...Assignment) SQLiteInsertQuery {
1✔
354
        c.q.Conflict.Resolution = assignments
1✔
355
        return *c.q
1✔
356
}
1✔
357

358
// Where adds predicates to the DO UPDATE SET clause of the SQLiteInsertQuery.
359
func (q SQLiteInsertQuery) Where(predicates ...Predicate) SQLiteInsertQuery {
1✔
360
        q.Conflict.ResolutionPredicate = appendPredicates(q.Conflict.ResolutionPredicate, predicates)
1✔
361
        return q
1✔
362
}
1✔
363

364
// Returning adds fields to the RETURNING clause of the SQLiteInsertQuery.
365
func (q SQLiteInsertQuery) Returning(fields ...Field) SQLiteInsertQuery {
2✔
366
        q.ReturningFields = append(q.ReturningFields, fields...)
2✔
367
        return q
2✔
368
}
2✔
369

370
// SetFetchableFields implements the Query interface.
371
func (q SQLiteInsertQuery) SetFetchableFields(fields []Field) (query Query, ok bool) {
1✔
372
        return InsertQuery(q).SetFetchableFields(fields)
1✔
373
}
1✔
374

375
// GetFetchableFields returns the fetchable fields of the query.
376
func (q SQLiteInsertQuery) GetFetchableFields() []Field {
1✔
377
        return InsertQuery(q).GetFetchableFields()
1✔
378
}
1✔
379

380
// GetDialect implements the Query interface.
381
func (q SQLiteInsertQuery) GetDialect() string { return q.Dialect }
9✔
382

383
// SetDialect returns the dialect of the query.
384
func (q SQLiteInsertQuery) SetDialect(dialect string) SQLiteInsertQuery {
2✔
385
        q.Dialect = dialect
2✔
386
        return q
2✔
387
}
2✔
388

389
// PostgresInsertQuery represents a Postgres INSERT query.
390
type PostgresInsertQuery InsertQuery
391

392
var _ Query = (*PostgresInsertQuery)(nil)
393

394
// WriteSQL implements the SQLWriter interface.
395
func (q PostgresInsertQuery) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
5✔
396
        return InsertQuery(q).WriteSQL(ctx, dialect, buf, args, params)
5✔
397
}
5✔
398

399
// InsertInto creates a new PostgresInsertQuery.
400
func (b postgresQueryBuilder) InsertInto(table Table) PostgresInsertQuery {
6✔
401
        return PostgresInsertQuery{
6✔
402
                Dialect:     DialectPostgres,
6✔
403
                CTEs:        b.ctes,
6✔
404
                InsertTable: table,
6✔
405
        }
6✔
406
}
6✔
407

408
// Columns sets the InsertColumns field of the PostgresInsertQuery.
409
func (q PostgresInsertQuery) Columns(fields ...Field) PostgresInsertQuery {
4✔
410
        q.InsertColumns = fields
4✔
411
        return q
4✔
412
}
4✔
413

414
// Values sets the RowValues field of the PostgresInsertQuery.
415
func (q PostgresInsertQuery) Values(values ...any) PostgresInsertQuery {
6✔
416
        q.RowValues = append(q.RowValues, values)
6✔
417
        return q
6✔
418
}
6✔
419

420
// ColumnValues sets the ColumnMapper field of the PostgresInsertQuery.
421
func (q PostgresInsertQuery) ColumnValues(colmapper func(*Column)) PostgresInsertQuery {
1✔
422
        q.ColumnMapper = colmapper
1✔
423
        return q
1✔
424
}
1✔
425

426
// Select sets the SelectQuery field of the PostgresInsertQuery.
427
func (q PostgresInsertQuery) Select(query Query) PostgresInsertQuery {
1✔
428
        q.SelectQuery = query
1✔
429
        return q
1✔
430
}
1✔
431

432
type postgresInsertConflict struct{ q *PostgresInsertQuery }
433

434
// OnConflict starts the ON CONFLICT clause of the PostgresInsertQuery.
435
func (q PostgresInsertQuery) OnConflict(fields ...Field) postgresInsertConflict {
1✔
436
        q.Conflict.Fields = fields
1✔
437
        return postgresInsertConflict{q: &q}
1✔
438
}
1✔
439

440
// OnConflict starts the ON CONFLICT clause of the PostgresInsertQuery.
441
func (q PostgresInsertQuery) OnConflictOnConstraint(constraintName string) postgresInsertConflict {
1✔
442
        q.Conflict.ConstraintName = constraintName
1✔
443
        return postgresInsertConflict{q: &q}
1✔
444
}
1✔
445

446
// Where adds predicates to the ON CONFLICT clause of the PostgresInsertQuery.
447
func (c postgresInsertConflict) Where(predicates ...Predicate) postgresInsertConflict {
1✔
448
        c.q.Conflict.Predicate = appendPredicates(c.q.Conflict.Predicate, predicates)
1✔
449
        return c
1✔
450
}
1✔
451

452
// DoNothing resolves the ON CONFLICT clause of the PostgresInsertQuery with DO
453
// NOTHING.
454
func (c postgresInsertConflict) DoNothing() PostgresInsertQuery {
1✔
455
        c.q.Conflict.DoNothing = true
1✔
456
        return *c.q
1✔
457
}
1✔
458

459
// DoUpdateSet resolves the ON CONFLICT CLAUSE of the PostgresInsertQuery with DO UPDATE SET.
460
func (c postgresInsertConflict) DoUpdateSet(assignments ...Assignment) PostgresInsertQuery {
1✔
461
        c.q.Conflict.Resolution = assignments
1✔
462
        return *c.q
1✔
463
}
1✔
464

465
// Where adds predicates to the DO UPDATE SET clause of the PostgresInsertQuery.
466
func (q PostgresInsertQuery) Where(predicates ...Predicate) PostgresInsertQuery {
1✔
467
        q.Conflict.ResolutionPredicate = appendPredicates(q.Conflict.ResolutionPredicate, predicates)
1✔
468
        return q
1✔
469
}
1✔
470

471
// Returning adds fields to the RETURNING clause of the PostgresInsertQuery.
472
func (q PostgresInsertQuery) Returning(fields ...Field) PostgresInsertQuery {
2✔
473
        q.ReturningFields = append(q.ReturningFields, fields...)
2✔
474
        return q
2✔
475
}
2✔
476

477
// SetFetchableFields implements the Query interface.
478
func (q PostgresInsertQuery) SetFetchableFields(fields []Field) (query Query, ok bool) {
1✔
479
        return InsertQuery(q).SetFetchableFields(fields)
1✔
480
}
1✔
481

482
// GetFetchableFields returns the fetchable fields of the query.
483
func (q PostgresInsertQuery) GetFetchableFields() []Field {
1✔
484
        return InsertQuery(q).GetFetchableFields()
1✔
485
}
1✔
486

487
// GetDialect implements the Query interface.
488
func (q PostgresInsertQuery) GetDialect() string { return q.Dialect }
6✔
489

490
// SetDialect returns the dialect of the query.
491
func (q PostgresInsertQuery) SetDialect(dialect string) PostgresInsertQuery {
2✔
492
        q.Dialect = dialect
2✔
493
        return q
2✔
494
}
2✔
495

496
// MySQLInsertQuery represents a MySQL INSERT query.
497
type MySQLInsertQuery InsertQuery
498

499
var _ Query = (*MySQLInsertQuery)(nil)
500

501
// WriteSQL implements the SQLWriter interface.
502
func (q MySQLInsertQuery) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
6✔
503
        return InsertQuery(q).WriteSQL(ctx, dialect, buf, args, params)
6✔
504
}
6✔
505

506
// InsertInto creates a new MySQLInsertQuery.
507
func (b mysqlQueryBuilder) InsertInto(table Table) MySQLInsertQuery {
6✔
508
        return MySQLInsertQuery{
6✔
509
                Dialect:     DialectMySQL,
6✔
510
                CTEs:        b.ctes,
6✔
511
                InsertTable: table,
6✔
512
        }
6✔
513
}
6✔
514

515
// InsertInto creates a new MySQLInsertQuery.
516
func (b mysqlQueryBuilder) InsertIgnoreInto(table Table) MySQLInsertQuery {
1✔
517
        return MySQLInsertQuery{
1✔
518
                Dialect:      DialectMySQL,
1✔
519
                CTEs:         b.ctes,
1✔
520
                InsertTable:  table,
1✔
521
                InsertIgnore: true,
1✔
522
        }
1✔
523
}
1✔
524

525
// Columns sets the InsertColumns field of the MySQLInsertQuery.
526
func (q MySQLInsertQuery) Columns(fields ...Field) MySQLInsertQuery {
5✔
527
        q.InsertColumns = fields
5✔
528
        return q
5✔
529
}
5✔
530

531
// Values sets the RowValues field of the MySQLInsertQuery.
532
func (q MySQLInsertQuery) Values(values ...any) MySQLInsertQuery {
6✔
533
        q.RowValues = append(q.RowValues, values)
6✔
534
        return q
6✔
535
}
6✔
536

537
// As sets the RowAlias field of the MySQLInsertQuery.
538
func (q MySQLInsertQuery) As(rowAlias string) MySQLInsertQuery {
1✔
539
        q.RowAlias = rowAlias
1✔
540
        return q
1✔
541
}
1✔
542

543
// ColumnValues sets the ColumnMapper field of the MySQLInsertQuery.
544
func (q MySQLInsertQuery) ColumnValues(colmapper func(*Column)) MySQLInsertQuery {
1✔
545
        q.ColumnMapper = colmapper
1✔
546
        return q
1✔
547
}
1✔
548

549
// Select sets the SelectQuery field of the MySQLInsertQuery.
550
func (q MySQLInsertQuery) Select(query Query) MySQLInsertQuery {
2✔
551
        q.SelectQuery = query
2✔
552
        return q
2✔
553
}
2✔
554

555
// OnDuplicateKeyUpdate sets the ON DUPLICATE KEY UPDATE clause of the
556
// MySQLInsertQuery.
557
func (q MySQLInsertQuery) OnDuplicateKeyUpdate(assignments ...Assignment) MySQLInsertQuery {
2✔
558
        q.Conflict.Resolution = assignments
2✔
559
        return q
2✔
560
}
2✔
561

562
// Returning adds fields to the RETURNING clause of the MySQLInsertQuery.
563
func (q MySQLInsertQuery) Returning(fields ...Field) MySQLInsertQuery {
1✔
564
        q.ReturningFields = append(q.ReturningFields, fields...)
1✔
565
        return q
1✔
566
}
1✔
567

568
// SetFetchableFields implements the Query interface.
569
func (q MySQLInsertQuery) SetFetchableFields(fields []Field) (query Query, ok bool) {
1✔
570
        return InsertQuery(q).SetFetchableFields(fields)
1✔
571
}
1✔
572

573
// GetFetchableFields returns the fetchable fields of the query.
574
func (q MySQLInsertQuery) GetFetchableFields() []Field {
1✔
575
        return InsertQuery(q).GetFetchableFields()
1✔
576
}
1✔
577

578
// GetDialect implements the Query interface.
579
func (q MySQLInsertQuery) GetDialect() string { return q.Dialect }
7✔
580

581
// SetDialect returns the dialect of the query.
582
func (q MySQLInsertQuery) SetDialect(dialect string) MySQLInsertQuery {
2✔
583
        q.Dialect = dialect
2✔
584
        return q
2✔
585
}
2✔
586

587
// SQLServerInsertQuery represents an SQL Server INSERT query.
588
type SQLServerInsertQuery InsertQuery
589

590
var _ Query = (*SQLServerInsertQuery)(nil)
591

592
// WriteSQL implements the SQLWriter interface.
593
func (q SQLServerInsertQuery) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
3✔
594
        return InsertQuery(q).WriteSQL(ctx, dialect, buf, args, params)
3✔
595
}
3✔
596

597
// InsertInto creates a new SQLServerInsertQuery.
598
func (b sqlserverQueryBuilder) InsertInto(table Table) SQLServerInsertQuery {
4✔
599
        return SQLServerInsertQuery{
4✔
600
                Dialect:     DialectSQLServer,
4✔
601
                CTEs:        b.ctes,
4✔
602
                InsertTable: table,
4✔
603
        }
4✔
604
}
4✔
605

606
// Columns sets the InsertColumns field of the SQLServerInsertQuery.
607
func (q SQLServerInsertQuery) Columns(fields ...Field) SQLServerInsertQuery {
2✔
608
        q.InsertColumns = fields
2✔
609
        return q
2✔
610
}
2✔
611

612
// Values sets the RowValues field of the SQLServerInsertQuery.
613
func (q SQLServerInsertQuery) Values(values ...any) SQLServerInsertQuery {
2✔
614
        q.RowValues = append(q.RowValues, values)
2✔
615
        return q
2✔
616
}
2✔
617

618
// ColumnValues sets the ColumnMapper field of the SQLServerInsertQuery.
619
func (q SQLServerInsertQuery) ColumnValues(colmapper func(*Column)) SQLServerInsertQuery {
1✔
620
        q.ColumnMapper = colmapper
1✔
621
        return q
1✔
622
}
1✔
623

624
// Select sets the SelectQuery field of the SQLServerInsertQuery.
625
func (q SQLServerInsertQuery) Select(query Query) SQLServerInsertQuery {
1✔
626
        q.SelectQuery = query
1✔
627
        return q
1✔
628
}
1✔
629

630
// SetFetchableFields implements the Query interface.
631
func (q SQLServerInsertQuery) SetFetchableFields(fields []Field) (query Query, ok bool) {
1✔
632
        return InsertQuery(q).SetFetchableFields(fields)
1✔
633
}
1✔
634

635
// GetFetchableFields returns the fetchable fields of the query.
636
func (q SQLServerInsertQuery) GetFetchableFields() []Field {
1✔
637
        return InsertQuery(q).GetFetchableFields()
1✔
638
}
1✔
639

640
// GetDialect implements the Query interface.
641
func (q SQLServerInsertQuery) GetDialect() string { return q.Dialect }
4✔
642

643
// SetDialect returns the dialect of the query.
644
func (q SQLServerInsertQuery) SetDialect(dialect string) SQLServerInsertQuery {
2✔
645
        q.Dialect = dialect
2✔
646
        return q
2✔
647
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc