341 lines
9.3 KiB
Go
341 lines
9.3 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: needs.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const advanceOrganizationNeed = `-- name: AdvanceOrganizationNeed :one
|
|
WITH advanced AS (
|
|
UPDATE organization_needs
|
|
SET
|
|
current_version = organization_needs.current_version + 1,
|
|
requirements_version = $1,
|
|
updated_at = $2
|
|
WHERE
|
|
organization_needs.organization_id = $3
|
|
AND organization_needs.id = $4
|
|
AND organization_needs.current_version = $5
|
|
AND $6::bigint = organization_needs.current_version + 1
|
|
AND (
|
|
$1::bigint = organization_needs.requirements_version
|
|
OR $1::bigint = organization_needs.current_version + 1
|
|
)
|
|
RETURNING organization_needs.organization_id, organization_needs.id, organization_needs.current_version
|
|
), appended AS (
|
|
INSERT INTO organization_need_versions (
|
|
organization_id,
|
|
need_id,
|
|
version,
|
|
snapshot
|
|
)
|
|
SELECT
|
|
advanced.organization_id,
|
|
advanced.id,
|
|
advanced.current_version,
|
|
$7::jsonb
|
|
FROM advanced
|
|
RETURNING
|
|
organization_need_versions.organization_id,
|
|
organization_need_versions.need_id,
|
|
organization_need_versions.version,
|
|
organization_need_versions.snapshot::text AS snapshot
|
|
)
|
|
SELECT appended.snapshot
|
|
FROM appended
|
|
`
|
|
|
|
type AdvanceOrganizationNeedParams struct {
|
|
RequirementsVersion int64
|
|
UpdatedAt pgtype.Timestamptz
|
|
OrganizationID pgtype.UUID
|
|
NeedID pgtype.UUID
|
|
ExpectedVersion int64
|
|
NextVersion int64
|
|
Snapshot []byte
|
|
}
|
|
|
|
// The conditional parent update is the compare-and-swap. Appending the
|
|
// snapshot is part of the same SQL statement, so a failed insert leaves the
|
|
// parent pointer unchanged even before the surrounding transaction rolls
|
|
// back. No row is returned when the expected version lost a race.
|
|
func (q *Queries) AdvanceOrganizationNeed(ctx context.Context, arg AdvanceOrganizationNeedParams) (string, error) {
|
|
row := q.db.QueryRow(ctx, advanceOrganizationNeed,
|
|
arg.RequirementsVersion,
|
|
arg.UpdatedAt,
|
|
arg.OrganizationID,
|
|
arg.NeedID,
|
|
arg.ExpectedVersion,
|
|
arg.NextVersion,
|
|
arg.Snapshot,
|
|
)
|
|
var snapshot string
|
|
err := row.Scan(&snapshot)
|
|
return snapshot, err
|
|
}
|
|
|
|
const getOrganizationNeedCreationResult = `-- name: GetOrganizationNeedCreationResult :one
|
|
SELECT snapshot::text AS snapshot
|
|
FROM organization_need_creation_results
|
|
WHERE scope_hash = $1
|
|
`
|
|
|
|
// Read only the immutable original response. The current need may have moved
|
|
// to a later version since this result was created.
|
|
func (q *Queries) GetOrganizationNeedCreationResult(ctx context.Context, scopeHash []byte) (string, error) {
|
|
row := q.db.QueryRow(ctx, getOrganizationNeedCreationResult, scopeHash)
|
|
var snapshot string
|
|
err := row.Scan(&snapshot)
|
|
return snapshot, err
|
|
}
|
|
|
|
const insertOrganizationNeed = `-- name: InsertOrganizationNeed :exec
|
|
INSERT INTO organization_needs (
|
|
id,
|
|
organization_id,
|
|
current_version,
|
|
requirements_version,
|
|
created_at,
|
|
updated_at
|
|
) VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6
|
|
)
|
|
`
|
|
|
|
type InsertOrganizationNeedParams struct {
|
|
ID pgtype.UUID
|
|
OrganizationID pgtype.UUID
|
|
CurrentVersion int64
|
|
RequirementsVersion int64
|
|
CreatedAt pgtype.Timestamptz
|
|
UpdatedAt pgtype.Timestamptz
|
|
}
|
|
|
|
// The parent and first immutable snapshot are inserted in one caller-owned
|
|
// transaction. Deferred version references are checked when it commits.
|
|
func (q *Queries) InsertOrganizationNeed(ctx context.Context, arg InsertOrganizationNeedParams) error {
|
|
_, err := q.db.Exec(ctx, insertOrganizationNeed,
|
|
arg.ID,
|
|
arg.OrganizationID,
|
|
arg.CurrentVersion,
|
|
arg.RequirementsVersion,
|
|
arg.CreatedAt,
|
|
arg.UpdatedAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const insertOrganizationNeedVersion = `-- name: InsertOrganizationNeedVersion :exec
|
|
INSERT INTO organization_need_versions (
|
|
organization_id,
|
|
need_id,
|
|
version,
|
|
snapshot
|
|
) VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4::jsonb
|
|
)
|
|
`
|
|
|
|
type InsertOrganizationNeedVersionParams struct {
|
|
OrganizationID pgtype.UUID
|
|
NeedID pgtype.UUID
|
|
Version int64
|
|
Snapshot []byte
|
|
}
|
|
|
|
func (q *Queries) InsertOrganizationNeedVersion(ctx context.Context, arg InsertOrganizationNeedVersionParams) error {
|
|
_, err := q.db.Exec(ctx, insertOrganizationNeedVersion,
|
|
arg.OrganizationID,
|
|
arg.NeedID,
|
|
arg.Version,
|
|
arg.Snapshot,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const listOrganizationNeeds = `-- name: ListOrganizationNeeds :many
|
|
SELECT
|
|
need.organization_id,
|
|
need.id AS need_id,
|
|
need.current_version,
|
|
need.requirements_version,
|
|
need.created_at,
|
|
need.updated_at,
|
|
version.snapshot::text AS snapshot
|
|
FROM organization_needs AS need
|
|
INNER JOIN organization_need_versions AS version
|
|
ON version.organization_id = need.organization_id
|
|
AND version.need_id = need.id
|
|
AND version.version = need.current_version
|
|
WHERE
|
|
need.organization_id = $1
|
|
AND (
|
|
$2::timestamptz IS NULL
|
|
OR (need.created_at, need.id) < (
|
|
$2::timestamptz,
|
|
$3::uuid
|
|
)
|
|
)
|
|
ORDER BY need.created_at DESC, need.id DESC
|
|
LIMIT $4
|
|
`
|
|
|
|
type ListOrganizationNeedsParams struct {
|
|
OrganizationID pgtype.UUID
|
|
AfterCreatedAt pgtype.Timestamptz
|
|
AfterNeedID pgtype.UUID
|
|
RowLimit int32
|
|
}
|
|
|
|
type ListOrganizationNeedsRow struct {
|
|
OrganizationID pgtype.UUID
|
|
NeedID pgtype.UUID
|
|
CurrentVersion int64
|
|
RequirementsVersion int64
|
|
CreatedAt pgtype.Timestamptz
|
|
UpdatedAt pgtype.Timestamptz
|
|
Snapshot string
|
|
}
|
|
|
|
// Read one bounded newest-first page of current needs. Both the organization
|
|
// scope and the tuple cursor are applied before the limit.
|
|
func (q *Queries) ListOrganizationNeeds(ctx context.Context, arg ListOrganizationNeedsParams) ([]ListOrganizationNeedsRow, error) {
|
|
rows, err := q.db.Query(ctx, listOrganizationNeeds,
|
|
arg.OrganizationID,
|
|
arg.AfterCreatedAt,
|
|
arg.AfterNeedID,
|
|
arg.RowLimit,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListOrganizationNeedsRow
|
|
for rows.Next() {
|
|
var i ListOrganizationNeedsRow
|
|
if err := rows.Scan(
|
|
&i.OrganizationID,
|
|
&i.NeedID,
|
|
&i.CurrentVersion,
|
|
&i.RequirementsVersion,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Snapshot,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const readCurrentOrganizationNeed = `-- name: ReadCurrentOrganizationNeed :one
|
|
SELECT
|
|
need.organization_id,
|
|
need.id AS need_id,
|
|
need.current_version,
|
|
need.requirements_version,
|
|
need.created_at,
|
|
need.updated_at,
|
|
version.snapshot::text AS snapshot
|
|
FROM organization_needs AS need
|
|
INNER JOIN organization_need_versions AS version
|
|
ON version.organization_id = need.organization_id
|
|
AND version.need_id = need.id
|
|
AND version.version = need.current_version
|
|
WHERE
|
|
need.organization_id = $1
|
|
AND need.id = $2
|
|
`
|
|
|
|
type ReadCurrentOrganizationNeedParams struct {
|
|
OrganizationID pgtype.UUID
|
|
NeedID pgtype.UUID
|
|
}
|
|
|
|
type ReadCurrentOrganizationNeedRow struct {
|
|
OrganizationID pgtype.UUID
|
|
NeedID pgtype.UUID
|
|
CurrentVersion int64
|
|
RequirementsVersion int64
|
|
CreatedAt pgtype.Timestamptz
|
|
UpdatedAt pgtype.Timestamptz
|
|
Snapshot string
|
|
}
|
|
|
|
// Read one current version with its organization-scoped parent metadata.
|
|
func (q *Queries) ReadCurrentOrganizationNeed(ctx context.Context, arg ReadCurrentOrganizationNeedParams) (ReadCurrentOrganizationNeedRow, error) {
|
|
row := q.db.QueryRow(ctx, readCurrentOrganizationNeed, arg.OrganizationID, arg.NeedID)
|
|
var i ReadCurrentOrganizationNeedRow
|
|
err := row.Scan(
|
|
&i.OrganizationID,
|
|
&i.NeedID,
|
|
&i.CurrentVersion,
|
|
&i.RequirementsVersion,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Snapshot,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const readOrganizationNeedVersion = `-- name: ReadOrganizationNeedVersion :one
|
|
SELECT snapshot::text AS snapshot
|
|
FROM organization_need_versions
|
|
WHERE
|
|
organization_id = $1
|
|
AND need_id = $2
|
|
AND version = $3
|
|
`
|
|
|
|
type ReadOrganizationNeedVersionParams struct {
|
|
OrganizationID pgtype.UUID
|
|
NeedID pgtype.UUID
|
|
Version int64
|
|
}
|
|
|
|
// Read one historical version without exposing another organization's record.
|
|
func (q *Queries) ReadOrganizationNeedVersion(ctx context.Context, arg ReadOrganizationNeedVersionParams) (string, error) {
|
|
row := q.db.QueryRow(ctx, readOrganizationNeedVersion, arg.OrganizationID, arg.NeedID, arg.Version)
|
|
var snapshot string
|
|
err := row.Scan(&snapshot)
|
|
return snapshot, err
|
|
}
|
|
|
|
const saveOrganizationNeedCreationResult = `-- name: SaveOrganizationNeedCreationResult :exec
|
|
INSERT INTO organization_need_creation_results (scope_hash, snapshot)
|
|
VALUES ($1, $2::jsonb)
|
|
ON CONFLICT (scope_hash) DO UPDATE
|
|
SET snapshot = excluded.snapshot
|
|
`
|
|
|
|
type SaveOrganizationNeedCreationResultParams struct {
|
|
ScopeHash []byte
|
|
Snapshot []byte
|
|
}
|
|
|
|
// The generic idempotency scope serializes retries. Replacing the child row
|
|
// is safe only in the transaction that reserves an expired scope for a new
|
|
// create; live retries read and return the stored response without writing.
|
|
func (q *Queries) SaveOrganizationNeedCreationResult(ctx context.Context, arg SaveOrganizationNeedCreationResultParams) error {
|
|
_, err := q.db.Exec(ctx, saveOrganizationNeedCreationResult, arg.ScopeHash, arg.Snapshot)
|
|
return err
|
|
}
|