Skip to content

Commit

Permalink
chore: synchronize workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Nov 8, 2023
1 parent 363e49a commit 079211f
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions sqlxx/batch/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,33 @@ 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)

if len(models) == 0 {
return nil
}

options := &createOptions{}
for _, opt := range opts {
opt(options)
}

var v T
model := pop.NewModel(v, ctx)

Expand All @@ -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...)
Expand Down

0 comments on commit 079211f

Please sign in to comment.