267 lines
6.0 KiB
Go
267 lines
6.0 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: resources.sql
|
|
|
|
package postgresdb
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
const insertResource = `-- name: InsertResource :one
|
|
INSERT INTO resources (kind, value, version, updated_at)
|
|
VALUES ($1, $2, 1, $3)
|
|
RETURNING kind, value, version, updated_at, org_id
|
|
`
|
|
|
|
type InsertResourceParams struct {
|
|
Kind string
|
|
Value []byte
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
func (q *Queries) InsertResource(ctx context.Context, arg InsertResourceParams) (Resource, error) {
|
|
row := q.db.QueryRow(ctx, insertResource, arg.Kind, arg.Value, arg.UpdatedAt)
|
|
var i Resource
|
|
err := row.Scan(
|
|
&i.Kind,
|
|
&i.Value,
|
|
&i.Version,
|
|
&i.UpdatedAt,
|
|
&i.OrgID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const insertResourceHistory = `-- name: InsertResourceHistory :one
|
|
INSERT INTO resource_history (
|
|
kind, value, version, actor, source, request_id, expected_version,
|
|
current_version, at
|
|
)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
|
RETURNING kind, value, version, actor, source, request_id, expected_version,
|
|
current_version, at
|
|
`
|
|
|
|
type InsertResourceHistoryParams struct {
|
|
Kind string
|
|
Value []byte
|
|
Version int64
|
|
Actor string
|
|
Source string
|
|
RequestID string
|
|
ExpectedVersion int64
|
|
CurrentVersion int64
|
|
At time.Time
|
|
}
|
|
|
|
type InsertResourceHistoryRow struct {
|
|
Kind string
|
|
Value []byte
|
|
Version int64
|
|
Actor string
|
|
Source string
|
|
RequestID string
|
|
ExpectedVersion int64
|
|
CurrentVersion int64
|
|
At time.Time
|
|
}
|
|
|
|
func (q *Queries) InsertResourceHistory(ctx context.Context, arg InsertResourceHistoryParams) (InsertResourceHistoryRow, error) {
|
|
row := q.db.QueryRow(ctx, insertResourceHistory,
|
|
arg.Kind,
|
|
arg.Value,
|
|
arg.Version,
|
|
arg.Actor,
|
|
arg.Source,
|
|
arg.RequestID,
|
|
arg.ExpectedVersion,
|
|
arg.CurrentVersion,
|
|
arg.At,
|
|
)
|
|
var i InsertResourceHistoryRow
|
|
err := row.Scan(
|
|
&i.Kind,
|
|
&i.Value,
|
|
&i.Version,
|
|
&i.Actor,
|
|
&i.Source,
|
|
&i.RequestID,
|
|
&i.ExpectedVersion,
|
|
&i.CurrentVersion,
|
|
&i.At,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const lockResourceWrite = `-- name: LockResourceWrite :exec
|
|
SELECT pg_advisory_xact_lock(hashtextextended($1, 0))
|
|
`
|
|
|
|
// This lock is acquired in its own statement before a resource read. PostgreSQL
|
|
// takes a fresh Read Committed snapshot for the subsequent read, so concurrent
|
|
// optimistic writers see the committed revision rather than both writing v1.
|
|
func (q *Queries) LockResourceWrite(ctx context.Context, hashtextextended string) error {
|
|
_, err := q.db.Exec(ctx, lockResourceWrite, hashtextextended)
|
|
return err
|
|
}
|
|
|
|
const resourceByKind = `-- name: ResourceByKind :one
|
|
SELECT kind, value, version, updated_at, org_id FROM resources WHERE kind = $1
|
|
`
|
|
|
|
func (q *Queries) ResourceByKind(ctx context.Context, kind string) (Resource, error) {
|
|
row := q.db.QueryRow(ctx, resourceByKind, kind)
|
|
var i Resource
|
|
err := row.Scan(
|
|
&i.Kind,
|
|
&i.Value,
|
|
&i.Version,
|
|
&i.UpdatedAt,
|
|
&i.OrgID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const resourceByRequest = `-- name: ResourceByRequest :one
|
|
SELECT kind, value, version, actor, source, request_id, expected_version,
|
|
current_version, at
|
|
FROM resource_history
|
|
WHERE kind = $1 AND request_id = $2
|
|
`
|
|
|
|
type ResourceByRequestParams struct {
|
|
Kind string
|
|
RequestID string
|
|
}
|
|
|
|
type ResourceByRequestRow struct {
|
|
Kind string
|
|
Value []byte
|
|
Version int64
|
|
Actor string
|
|
Source string
|
|
RequestID string
|
|
ExpectedVersion int64
|
|
CurrentVersion int64
|
|
At time.Time
|
|
}
|
|
|
|
func (q *Queries) ResourceByRequest(ctx context.Context, arg ResourceByRequestParams) (ResourceByRequestRow, error) {
|
|
row := q.db.QueryRow(ctx, resourceByRequest, arg.Kind, arg.RequestID)
|
|
var i ResourceByRequestRow
|
|
err := row.Scan(
|
|
&i.Kind,
|
|
&i.Value,
|
|
&i.Version,
|
|
&i.Actor,
|
|
&i.Source,
|
|
&i.RequestID,
|
|
&i.ExpectedVersion,
|
|
&i.CurrentVersion,
|
|
&i.At,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const resourceHistory = `-- name: ResourceHistory :many
|
|
SELECT kind, value, version, actor, source, request_id, expected_version,
|
|
current_version, at
|
|
FROM resource_history
|
|
WHERE kind = $1
|
|
ORDER BY version DESC
|
|
LIMIT NULLIF($2::bigint, 0)
|
|
`
|
|
|
|
type ResourceHistoryParams struct {
|
|
Kind string
|
|
EntryLimit int64
|
|
}
|
|
|
|
type ResourceHistoryRow struct {
|
|
Kind string
|
|
Value []byte
|
|
Version int64
|
|
Actor string
|
|
Source string
|
|
RequestID string
|
|
ExpectedVersion int64
|
|
CurrentVersion int64
|
|
At time.Time
|
|
}
|
|
|
|
func (q *Queries) ResourceHistory(ctx context.Context, arg ResourceHistoryParams) ([]ResourceHistoryRow, error) {
|
|
rows, err := q.db.Query(ctx, resourceHistory, arg.Kind, arg.EntryLimit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ResourceHistoryRow
|
|
for rows.Next() {
|
|
var i ResourceHistoryRow
|
|
if err := rows.Scan(
|
|
&i.Kind,
|
|
&i.Value,
|
|
&i.Version,
|
|
&i.Actor,
|
|
&i.Source,
|
|
&i.RequestID,
|
|
&i.ExpectedVersion,
|
|
&i.CurrentVersion,
|
|
&i.At,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const resourceVersion = `-- name: ResourceVersion :one
|
|
SELECT version FROM resources WHERE kind = $1
|
|
`
|
|
|
|
func (q *Queries) ResourceVersion(ctx context.Context, kind string) (int64, error) {
|
|
row := q.db.QueryRow(ctx, resourceVersion, kind)
|
|
var version int64
|
|
err := row.Scan(&version)
|
|
return version, err
|
|
}
|
|
|
|
const updateResource = `-- name: UpdateResource :one
|
|
UPDATE resources
|
|
SET value = $2, version = version + 1, updated_at = $3
|
|
WHERE kind = $1 AND version = $4
|
|
RETURNING kind, value, version, updated_at, org_id
|
|
`
|
|
|
|
type UpdateResourceParams struct {
|
|
Kind string
|
|
Value []byte
|
|
UpdatedAt time.Time
|
|
Version int64
|
|
}
|
|
|
|
func (q *Queries) UpdateResource(ctx context.Context, arg UpdateResourceParams) (Resource, error) {
|
|
row := q.db.QueryRow(ctx, updateResource,
|
|
arg.Kind,
|
|
arg.Value,
|
|
arg.UpdatedAt,
|
|
arg.Version,
|
|
)
|
|
var i Resource
|
|
err := row.Scan(
|
|
&i.Kind,
|
|
&i.Value,
|
|
&i.Version,
|
|
&i.UpdatedAt,
|
|
&i.OrgID,
|
|
)
|
|
return i, err
|
|
}
|