Files
lincodex/backend/internal/adapter/secondary/postgres/sqlc/company_quota.sql.go
T

446 lines
14 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: company_quota.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const countCompanyCardViewQuotaHoldingWindow = `-- name: CountCompanyCardViewQuotaHoldingWindow :one
SELECT
count(*)::bigint AS usage_count,
min(usage.last_seen_at) AS earliest_at
FROM company_card_view_quota_usage AS usage
INNER JOIN organizations AS organization ON organization.id = usage.organization_id
WHERE
organization.holding_id = $1
AND organization.verification_level = 'e0'
AND usage.last_seen_at > $2
`
type CountCompanyCardViewQuotaHoldingWindowParams struct {
HoldingID pgtype.UUID
WindowStart pgtype.Timestamptz
}
type CountCompanyCardViewQuotaHoldingWindowRow struct {
UsageCount int64
EarliestAt interface{}
}
// Counts the distinct-card usage of the E0 pool of a holding, with the same
// rule as CountCompanyInvitationQuotaHoldingWindow. A view is still counted
// per organization: the same candidate seen by two organizations of one
// holding is two charges.
func (q *Queries) CountCompanyCardViewQuotaHoldingWindow(ctx context.Context, arg CountCompanyCardViewQuotaHoldingWindowParams) (CountCompanyCardViewQuotaHoldingWindowRow, error) {
row := q.db.QueryRow(ctx, countCompanyCardViewQuotaHoldingWindow, arg.HoldingID, arg.WindowStart)
var i CountCompanyCardViewQuotaHoldingWindowRow
err := row.Scan(&i.UsageCount, &i.EarliestAt)
return i, err
}
const countCompanyCardViewQuotaWindow = `-- name: CountCompanyCardViewQuotaWindow :one
SELECT
count(*)::bigint AS usage_count,
min(last_seen_at) AS earliest_at
FROM company_card_view_quota_usage
WHERE
organization_id = $1
AND last_seen_at > $2
`
type CountCompanyCardViewQuotaWindowParams struct {
OrganizationID pgtype.UUID
WindowStart pgtype.Timestamptz
}
type CountCompanyCardViewQuotaWindowRow struct {
UsageCount int64
EarliestAt interface{}
}
func (q *Queries) CountCompanyCardViewQuotaWindow(ctx context.Context, arg CountCompanyCardViewQuotaWindowParams) (CountCompanyCardViewQuotaWindowRow, error) {
row := q.db.QueryRow(ctx, countCompanyCardViewQuotaWindow, arg.OrganizationID, arg.WindowStart)
var i CountCompanyCardViewQuotaWindowRow
err := row.Scan(&i.UsageCount, &i.EarliestAt)
return i, err
}
const countCompanyInvitationQuotaHoldingWindow = `-- name: CountCompanyInvitationQuotaHoldingWindow :one
SELECT
count(*)::bigint AS usage_count,
min(usage.reserved_at) AS earliest_at
FROM company_invitation_quota_usage AS usage
INNER JOIN organizations AS organization ON organization.id = usage.organization_id
WHERE
organization.holding_id = $1
AND organization.verification_level = 'e0'
AND usage.reserved_at > $2
`
type CountCompanyInvitationQuotaHoldingWindowParams struct {
HoldingID pgtype.UUID
WindowStart pgtype.Timestamptz
}
type CountCompanyInvitationQuotaHoldingWindowRow struct {
UsageCount int64
EarliestAt interface{}
}
// Counts the invitation usage of the E0 pool of a holding: the usage of every
// organization of the holding whose trust level is E0 when the count runs. Each
// row stays attributed to the organization that consumed it.
func (q *Queries) CountCompanyInvitationQuotaHoldingWindow(ctx context.Context, arg CountCompanyInvitationQuotaHoldingWindowParams) (CountCompanyInvitationQuotaHoldingWindowRow, error) {
row := q.db.QueryRow(ctx, countCompanyInvitationQuotaHoldingWindow, arg.HoldingID, arg.WindowStart)
var i CountCompanyInvitationQuotaHoldingWindowRow
err := row.Scan(&i.UsageCount, &i.EarliestAt)
return i, err
}
const countCompanyInvitationQuotaWindow = `-- name: CountCompanyInvitationQuotaWindow :one
SELECT
count(*)::bigint AS usage_count,
min(reserved_at) AS earliest_at
FROM company_invitation_quota_usage
WHERE
organization_id = $1
AND reserved_at > $2
`
type CountCompanyInvitationQuotaWindowParams struct {
OrganizationID pgtype.UUID
WindowStart pgtype.Timestamptz
}
type CountCompanyInvitationQuotaWindowRow struct {
UsageCount int64
EarliestAt interface{}
}
func (q *Queries) CountCompanyInvitationQuotaWindow(ctx context.Context, arg CountCompanyInvitationQuotaWindowParams) (CountCompanyInvitationQuotaWindowRow, error) {
row := q.db.QueryRow(ctx, countCompanyInvitationQuotaWindow, arg.OrganizationID, arg.WindowStart)
var i CountCompanyInvitationQuotaWindowRow
err := row.Scan(&i.UsageCount, &i.EarliestAt)
return i, err
}
const deleteExpiredCompanyCardViewQuotaUsage = `-- name: DeleteExpiredCompanyCardViewQuotaUsage :execrows
WITH expired AS (
SELECT quota_usage.organization_id, quota_usage.candidate_id
FROM company_card_view_quota_usage AS quota_usage
WHERE
quota_usage.organization_id = $1
AND quota_usage.last_seen_at <= $2
ORDER BY quota_usage.last_seen_at, quota_usage.candidate_id
LIMIT $3::integer
)
DELETE FROM company_card_view_quota_usage AS usage
USING expired
WHERE
usage.organization_id = expired.organization_id
AND usage.candidate_id = expired.candidate_id
`
type DeleteExpiredCompanyCardViewQuotaUsageParams struct {
OrganizationID pgtype.UUID
WindowStart pgtype.Timestamptz
RowLimit int32
}
func (q *Queries) DeleteExpiredCompanyCardViewQuotaUsage(ctx context.Context, arg DeleteExpiredCompanyCardViewQuotaUsageParams) (int64, error) {
result, err := q.db.Exec(ctx, deleteExpiredCompanyCardViewQuotaUsage, arg.OrganizationID, arg.WindowStart, arg.RowLimit)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const findCompanyCardViewQuotaUsage = `-- name: FindCompanyCardViewQuotaUsage :one
SELECT last_seen_at
FROM company_card_view_quota_usage
WHERE
organization_id = $1
AND candidate_id = $2
FOR UPDATE
`
type FindCompanyCardViewQuotaUsageParams struct {
OrganizationID pgtype.UUID
CandidateID pgtype.UUID
}
func (q *Queries) FindCompanyCardViewQuotaUsage(ctx context.Context, arg FindCompanyCardViewQuotaUsageParams) (pgtype.Timestamptz, error) {
row := q.db.QueryRow(ctx, findCompanyCardViewQuotaUsage, arg.OrganizationID, arg.CandidateID)
var last_seen_at pgtype.Timestamptz
err := row.Scan(&last_seen_at)
return last_seen_at, err
}
const findCompanyInvitationQuotaUsage = `-- name: FindCompanyInvitationQuotaUsage :one
SELECT invitation_id
FROM company_invitation_quota_usage
WHERE
organization_id = $1
AND invitation_id = $2
FOR UPDATE
`
type FindCompanyInvitationQuotaUsageParams struct {
OrganizationID pgtype.UUID
InvitationID pgtype.UUID
}
func (q *Queries) FindCompanyInvitationQuotaUsage(ctx context.Context, arg FindCompanyInvitationQuotaUsageParams) (pgtype.UUID, error) {
row := q.db.QueryRow(ctx, findCompanyInvitationQuotaUsage, arg.OrganizationID, arg.InvitationID)
var invitation_id pgtype.UUID
err := row.Scan(&invitation_id)
return invitation_id, err
}
const insertCompanyInvitationQuotaUsage = `-- name: InsertCompanyInvitationQuotaUsage :exec
INSERT INTO company_invitation_quota_usage (
organization_id,
invitation_id,
reserved_at,
policy_version_id,
verification_level,
verification_basis,
verification_changed_at,
quota_tier
) VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8
)
`
type InsertCompanyInvitationQuotaUsageParams struct {
OrganizationID pgtype.UUID
InvitationID pgtype.UUID
ReservedAt pgtype.Timestamptz
PolicyVersionID pgtype.UUID
VerificationLevel string
VerificationBasis string
VerificationChangedAt pgtype.Timestamptz
QuotaTier string
}
func (q *Queries) InsertCompanyInvitationQuotaUsage(ctx context.Context, arg InsertCompanyInvitationQuotaUsageParams) error {
_, err := q.db.Exec(ctx, insertCompanyInvitationQuotaUsage,
arg.OrganizationID,
arg.InvitationID,
arg.ReservedAt,
arg.PolicyVersionID,
arg.VerificationLevel,
arg.VerificationBasis,
arg.VerificationChangedAt,
arg.QuotaTier,
)
return err
}
const lockCompanyQuotaHolding = `-- name: LockCompanyQuotaHolding :one
SELECT id
FROM holdings
WHERE id = $1
FOR NO KEY UPDATE
`
// Takes the pool lock of an E0 holding. It follows the organization lock, so
// every writer locks an organization first and its holding second. Two
// reservations of one holding queue here, and the second one counts the
// usage that the first one committed. The lock does not conflict with the
// FOR KEY SHARE that organizations.holding_id takes when an organization joins
// the holding.
func (q *Queries) LockCompanyQuotaHolding(ctx context.Context, holdingID pgtype.UUID) (pgtype.UUID, error) {
row := q.db.QueryRow(ctx, lockCompanyQuotaHolding, holdingID)
var id pgtype.UUID
err := row.Scan(&id)
return id, err
}
const lockCompanyQuotaOrganization = `-- name: LockCompanyQuotaOrganization :one
SELECT
id,
verification_level,
verification_basis,
verification_changed_at,
holding_id
FROM organizations
WHERE id = $1
FOR UPDATE
`
type LockCompanyQuotaOrganizationRow struct {
ID pgtype.UUID
VerificationLevel string
VerificationBasis string
VerificationChangedAt pgtype.Timestamptz
HoldingID pgtype.UUID
}
func (q *Queries) LockCompanyQuotaOrganization(ctx context.Context, organizationID pgtype.UUID) (LockCompanyQuotaOrganizationRow, error) {
row := q.db.QueryRow(ctx, lockCompanyQuotaOrganization, organizationID)
var i LockCompanyQuotaOrganizationRow
err := row.Scan(
&i.ID,
&i.VerificationLevel,
&i.VerificationBasis,
&i.VerificationChangedAt,
&i.HoldingID,
)
return i, err
}
const lockCompanyQuotaOrganizationFirst = `-- name: LockCompanyQuotaOrganizationFirst :one
SELECT
id,
verification_level,
holding_id
FROM organizations
WHERE id = $1
FOR NO KEY UPDATE
`
type LockCompanyQuotaOrganizationFirstRow struct {
ID pgtype.UUID
VerificationLevel string
HoldingID pgtype.UUID
}
// Writers lock the organization first, then, for an E0 organization, the holding
// whose pool it shares, then invitation/card-view state, and finally usage rows.
// The organization lock serializes quota decisions for the tenant, the holding
// lock serializes the E0 pool across the organizations of one holding, and
// together they make the subsequent count and insert atomic.
// Takes the organization lock ahead of every share lock of a reader that will
// reserve usage later. FOR NO KEY UPDATE conflicts with itself and with FOR
// SHARE, so readers of one organization queue up instead of deadlocking on a
// share lock that each then tries to raise, but it does not conflict with the
// FOR KEY SHARE that a foreign key takes, such as the hidden-organization
// insert of a candidate, which holds the candidate's row and then needs this
// one. The reservation itself still takes FOR UPDATE inside the same
// transaction. The result carries the holding, whose pool an E0 organization
// shares; the caller locks that holding right after this lock.
func (q *Queries) LockCompanyQuotaOrganizationFirst(ctx context.Context, organizationID pgtype.UUID) (LockCompanyQuotaOrganizationFirstRow, error) {
row := q.db.QueryRow(ctx, lockCompanyQuotaOrganizationFirst, organizationID)
var i LockCompanyQuotaOrganizationFirstRow
err := row.Scan(&i.ID, &i.VerificationLevel, &i.HoldingID)
return i, err
}
const refreshCompanyCardViewQuotaUsage = `-- name: RefreshCompanyCardViewQuotaUsage :execrows
UPDATE company_card_view_quota_usage
SET
last_seen_at = greatest(last_seen_at, $1),
policy_version_id = $2,
verification_level = $3,
verification_basis = $4,
verification_changed_at = $5,
quota_tier = $6
WHERE
organization_id = $7
AND candidate_id = $8
AND last_seen_at > $9
`
type RefreshCompanyCardViewQuotaUsageParams struct {
LastSeenAt pgtype.Timestamptz
PolicyVersionID pgtype.UUID
VerificationLevel string
VerificationBasis string
VerificationChangedAt pgtype.Timestamptz
QuotaTier string
OrganizationID pgtype.UUID
CandidateID pgtype.UUID
WindowStart pgtype.Timestamptz
}
func (q *Queries) RefreshCompanyCardViewQuotaUsage(ctx context.Context, arg RefreshCompanyCardViewQuotaUsageParams) (int64, error) {
result, err := q.db.Exec(ctx, refreshCompanyCardViewQuotaUsage,
arg.LastSeenAt,
arg.PolicyVersionID,
arg.VerificationLevel,
arg.VerificationBasis,
arg.VerificationChangedAt,
arg.QuotaTier,
arg.OrganizationID,
arg.CandidateID,
arg.WindowStart,
)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const saveCompanyCardViewQuotaUsage = `-- name: SaveCompanyCardViewQuotaUsage :exec
INSERT INTO company_card_view_quota_usage (
organization_id,
candidate_id,
last_seen_at,
policy_version_id,
verification_level,
verification_basis,
verification_changed_at,
quota_tier
) VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8
)
ON CONFLICT (organization_id, candidate_id)
DO UPDATE SET
last_seen_at = greatest(
company_card_view_quota_usage.last_seen_at,
EXCLUDED.last_seen_at
),
policy_version_id = EXCLUDED.policy_version_id,
verification_level = EXCLUDED.verification_level,
verification_basis = EXCLUDED.verification_basis,
verification_changed_at = EXCLUDED.verification_changed_at,
quota_tier = EXCLUDED.quota_tier
`
type SaveCompanyCardViewQuotaUsageParams struct {
OrganizationID pgtype.UUID
CandidateID pgtype.UUID
LastSeenAt pgtype.Timestamptz
PolicyVersionID pgtype.UUID
VerificationLevel string
VerificationBasis string
VerificationChangedAt pgtype.Timestamptz
QuotaTier string
}
func (q *Queries) SaveCompanyCardViewQuotaUsage(ctx context.Context, arg SaveCompanyCardViewQuotaUsageParams) error {
_, err := q.db.Exec(ctx, saveCompanyCardViewQuotaUsage,
arg.OrganizationID,
arg.CandidateID,
arg.LastSeenAt,
arg.PolicyVersionID,
arg.VerificationLevel,
arg.VerificationBasis,
arg.VerificationChangedAt,
arg.QuotaTier,
)
return err
}