sorarebuddy/model/batch.go

94 lines
2.1 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.25.0
// source: batch.go
package model
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
)
var (
ErrBatchAlreadyClosed = errors.New("batch already closed")
)
const createOrUpdateCompetitions = `-- name: CreateOrUpdateCompetitions :batchexec
INSERT INTO competitions (
slug,
display_name,
country_slug,
competition_format,
competition_type,
picture_url,
logo_url
)
VALUES ($1, $2, $3, $4, $5, $6, $7) ON CONFLICT (slug)
DO
UPDATE
SET display_name = EXCLUDED.display_name,
competition_format = EXCLUDED.competition_format,
competition_type = EXCLUDED.competition_type,
picture_url = EXCLUDED.picture_url,
logo_url = EXCLUDED.logo_url,
country_slug = EXCLUDED.country_slug
`
type CreateOrUpdateCompetitionsBatchResults struct {
br pgx.BatchResults
tot int
closed bool
}
type CreateOrUpdateCompetitionsParams struct {
Slug string
DisplayName string
CountrySlug string
CompetitionFormat string
CompetitionType string
PictureUrl string
LogoUrl string
}
func (q *Queries) CreateOrUpdateCompetitions(ctx context.Context, arg []CreateOrUpdateCompetitionsParams) *CreateOrUpdateCompetitionsBatchResults {
batch := &pgx.Batch{}
for _, a := range arg {
vals := []interface{}{
a.Slug,
a.DisplayName,
a.CountrySlug,
a.CompetitionFormat,
a.CompetitionType,
a.PictureUrl,
a.LogoUrl,
}
batch.Queue(createOrUpdateCompetitions, vals...)
}
br := q.db.SendBatch(ctx, batch)
return &CreateOrUpdateCompetitionsBatchResults{br, len(arg), false}
}
func (b *CreateOrUpdateCompetitionsBatchResults) Exec(f func(int, error)) {
defer b.br.Close()
for t := 0; t < b.tot; t++ {
if b.closed {
if f != nil {
f(t, ErrBatchAlreadyClosed)
}
continue
}
_, err := b.br.Exec()
if f != nil {
f(t, err)
}
}
}
func (b *CreateOrUpdateCompetitionsBatchResults) Close() error {
b.closed = true
return b.br.Close()
}