-- name: CreateNumericEntitlement :one INSERT INTO core.numeric_entitlements (pool_id, resource_key, entitlement_type, resource_limit) VALUES ($1, $2, $3, $4) RETURNING *; -- name: GetNumericEntitlementByPoolAndResource :one SELECT * FROM core.numeric_entitlements WHERE pool_id = $1 AND resource_key = $2; -- name: UpdateNumericEntitlementLimit :one UPDATE core.numeric_entitlements SET resource_limit = $2 WHERE entitlement_id = $1 RETURNING *; -- name: ListNumericEntitlementsByPoolID :many SELECT * FROM core.numeric_entitlements WHERE pool_id = $1 ORDER BY resource_key ASC; -- name: ListNumericEntitlementStateForPools :many -- Read-only per-pool numeric state for a batch of pools, each row carrying the -- usage the limit is read against. The preview folds these without locking. SELECT ne.pool_id, ne.resource_key, ne.resource_limit, COALESCE(u.current_usage, 0)::BIGINT AS current_usage FROM core.numeric_entitlements ne LEFT JOIN core.numeric_entitlement_usage u ON u.entitlement_id = ne.entitlement_id WHERE ne.pool_id = ANY(sqlc.arg(pool_ids)::uuid[]) ORDER BY ne.pool_id ASC, ne.resource_key ASC;