Daniel Ponte
1664c26e14
Reviewed-on: #69 Co-authored-by: Daniel Ponte <amigan@gmail.com> Co-committed-by: Daniel Ponte <amigan@gmail.com>
211 lines
4.5 KiB
Go
211 lines
4.5 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.27.0
|
|
// source: batch.go
|
|
|
|
package database
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"dynatron.me/x/stillbox/internal/jsontypes"
|
|
"dynatron.me/x/stillbox/pkg/alerting/rules"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
var (
|
|
ErrBatchAlreadyClosed = errors.New("batch already closed")
|
|
)
|
|
|
|
const storeTGVersion = `-- name: StoreTGVersion :batchexec
|
|
INSERT INTO talkgroup_versions(time, created_by,
|
|
system_id,
|
|
tgid,
|
|
name,
|
|
alpha_tag,
|
|
tg_group,
|
|
frequency,
|
|
metadata,
|
|
tags,
|
|
alert,
|
|
alert_config,
|
|
weight,
|
|
learned
|
|
) SELECT NOW(), $1,
|
|
tg.system_id,
|
|
tg.tgid,
|
|
tg.name,
|
|
tg.alpha_tag,
|
|
tg.tg_group,
|
|
tg.frequency,
|
|
tg.metadata,
|
|
tg.tags,
|
|
tg.alert,
|
|
tg.alert_config,
|
|
tg.weight,
|
|
tg.learned
|
|
FROM talkgroups tg WHERE tg.system_id = $2 AND tg.tgid = $3
|
|
`
|
|
|
|
type StoreTGVersionBatchResults struct {
|
|
br pgx.BatchResults
|
|
tot int
|
|
closed bool
|
|
}
|
|
|
|
type StoreTGVersionParams struct {
|
|
Submitter *int32 `json:"submitter"`
|
|
SystemID int32 `json:"system_id"`
|
|
TGID int32 `json:"tgid"`
|
|
}
|
|
|
|
func (q *Queries) StoreTGVersion(ctx context.Context, arg []StoreTGVersionParams) *StoreTGVersionBatchResults {
|
|
batch := &pgx.Batch{}
|
|
for _, a := range arg {
|
|
vals := []interface{}{
|
|
a.Submitter,
|
|
a.SystemID,
|
|
a.TGID,
|
|
}
|
|
batch.Queue(storeTGVersion, vals...)
|
|
}
|
|
br := q.db.SendBatch(ctx, batch)
|
|
return &StoreTGVersionBatchResults{br, len(arg), false}
|
|
}
|
|
|
|
func (b *StoreTGVersionBatchResults) 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 *StoreTGVersionBatchResults) Close() error {
|
|
b.closed = true
|
|
return b.br.Close()
|
|
}
|
|
|
|
const upsertTalkgroup = `-- name: UpsertTalkgroup :batchone
|
|
INSERT INTO talkgroups AS tg (
|
|
system_id, tgid, name, alpha_tag, tg_group, frequency, metadata, tags, alert, alert_config, weight, learned
|
|
) VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7,
|
|
COALESCE($8, '{}'::text[])::text[],
|
|
COALESCE($9, TRUE),
|
|
$10,
|
|
COALESCE($11, 1.0)::numeric,
|
|
COALESCE($12, FALSE)::boolean
|
|
)
|
|
ON CONFLICT (system_id, tgid) DO UPDATE
|
|
SET
|
|
name = COALESCE($3, tg.name),
|
|
alpha_tag = COALESCE($4, tg.alpha_tag),
|
|
tg_group = COALESCE($5, tg.tg_group),
|
|
frequency = COALESCE($6, tg.frequency),
|
|
metadata = COALESCE($7, tg.metadata),
|
|
tags = COALESCE($8, tg.tags),
|
|
alert = COALESCE($9, tg.alert),
|
|
alert_config = COALESCE($10, tg.alert_config),
|
|
weight = COALESCE($11, tg.weight),
|
|
learned = COALESCE($12, tg.learned)
|
|
RETURNING id, system_id, tgid, name, alpha_tag, tg_group, frequency, metadata, tags, alert, alert_config, weight, learned, ignored
|
|
`
|
|
|
|
type UpsertTalkgroupBatchResults struct {
|
|
br pgx.BatchResults
|
|
tot int
|
|
closed bool
|
|
}
|
|
|
|
type UpsertTalkgroupParams struct {
|
|
SystemID int32 `json:"system_id"`
|
|
TGID int32 `json:"tgid"`
|
|
Name *string `json:"name"`
|
|
AlphaTag *string `json:"alpha_tag"`
|
|
TGGroup *string `json:"tg_group"`
|
|
Frequency *int32 `json:"frequency"`
|
|
Metadata jsontypes.Metadata `json:"metadata"`
|
|
Tags []string `json:"tags"`
|
|
Alert interface{} `json:"alert"`
|
|
AlertConfig rules.AlertRules `json:"alert_config"`
|
|
Weight pgtype.Numeric `json:"weight"`
|
|
Learned *bool `json:"learned"`
|
|
}
|
|
|
|
func (q *Queries) UpsertTalkgroup(ctx context.Context, arg []UpsertTalkgroupParams) *UpsertTalkgroupBatchResults {
|
|
batch := &pgx.Batch{}
|
|
for _, a := range arg {
|
|
vals := []interface{}{
|
|
a.SystemID,
|
|
a.TGID,
|
|
a.Name,
|
|
a.AlphaTag,
|
|
a.TGGroup,
|
|
a.Frequency,
|
|
a.Metadata,
|
|
a.Tags,
|
|
a.Alert,
|
|
a.AlertConfig,
|
|
a.Weight,
|
|
a.Learned,
|
|
}
|
|
batch.Queue(upsertTalkgroup, vals...)
|
|
}
|
|
br := q.db.SendBatch(ctx, batch)
|
|
return &UpsertTalkgroupBatchResults{br, len(arg), false}
|
|
}
|
|
|
|
func (b *UpsertTalkgroupBatchResults) QueryRow(f func(int, Talkgroup, error)) {
|
|
defer b.br.Close()
|
|
for t := 0; t < b.tot; t++ {
|
|
var i Talkgroup
|
|
if b.closed {
|
|
if f != nil {
|
|
f(t, i, ErrBatchAlreadyClosed)
|
|
}
|
|
continue
|
|
}
|
|
row := b.br.QueryRow()
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.SystemID,
|
|
&i.TGID,
|
|
&i.Name,
|
|
&i.AlphaTag,
|
|
&i.TGGroup,
|
|
&i.Frequency,
|
|
&i.Metadata,
|
|
&i.Tags,
|
|
&i.Alert,
|
|
&i.AlertConfig,
|
|
&i.Weight,
|
|
&i.Learned,
|
|
&i.Ignored,
|
|
)
|
|
if f != nil {
|
|
f(t, i, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (b *UpsertTalkgroupBatchResults) Close() error {
|
|
b.closed = true
|
|
return b.br.Close()
|
|
}
|