From 079211f3a5551a7aa53885355ed6a5fc187f42c5 Mon Sep 17 00:00:00 2001 From: aeneasr <3372410+aeneasr@users.noreply.github.com> Date: Wed, 8 Nov 2023 11:25:19 +0100 Subject: [PATCH] chore: synchronize workspaces --- sqlxx/batch/create.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/sqlxx/batch/create.go b/sqlxx/batch/create.go index 8c791d00..5022bda1 100644 --- a/sqlxx/batch/create.go +++ b/sqlxx/batch/create.go @@ -167,9 +167,21 @@ func buildInsertQueryValues[T any](dialect string, mapper *reflectx.Mapper, colu return values, nil } +type createOptions struct { + onConflict string +} + +type option func(*createOptions) + +func OnConflictDoNothing() func(*createOptions) { + return func(o *createOptions) { + o.onConflict = "ON CONFLICT DO NOTHING" + } +} + // Create batch-inserts the given models into the database using a single INSERT statement. // The models are either all created or none. -func Create[T any](ctx context.Context, p *TracerConnection, models []*T) (err error) { +func Create[T any](ctx context.Context, p *TracerConnection, models []*T, opts ...option) (err error) { ctx, span := p.Tracer.Tracer().Start(ctx, "persistence.sql.batch.Create") defer otelx.End(span, &err) @@ -177,6 +189,11 @@ func Create[T any](ctx context.Context, p *TracerConnection, models []*T) (err e return nil } + options := &createOptions{} + for _, opt := range opts { + opt(options) + } + var v T model := pop.NewModel(v, ctx) @@ -199,11 +216,12 @@ func Create[T any](ctx context.Context, p *TracerConnection, models []*T) (err e } query := conn.Dialect.TranslateSQL(fmt.Sprintf( - "INSERT INTO %s (%s) VALUES\n%s\n%s", + "INSERT INTO %s (%s) VALUES\n%s\n%s\n%s", queryArgs.TableName, queryArgs.ColumnsDecl, queryArgs.Placeholders, returningClause, + options.onConflict, )) rows, err := conn.TX.QueryContext(ctx, query, values...)