// 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 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 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 }