| 1 | // Code generated by sqlc. DO NOT EDIT. |
| 2 | // versions: |
| 3 | // sqlc v1.31.1 |
| 4 | // source: auth_throttle.sql |
| 5 | |
| 6 | package usersdb |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | |
| 11 | "github.com/jackc/pgx/v5/pgtype" |
| 12 | ) |
| 13 | |
| 14 | const bumpAuthThrottle = `-- name: BumpAuthThrottle :one |
| 15 | |
| 16 | INSERT INTO auth_throttle (scope, identifier, hits, window_started_at) |
| 17 | VALUES ($1, $2, 1, now()) |
| 18 | ON CONFLICT (scope, identifier) DO UPDATE |
| 19 | SET hits = CASE |
| 20 | WHEN auth_throttle.window_started_at < $3 THEN 1 |
| 21 | ELSE auth_throttle.hits + 1 |
| 22 | END, |
| 23 | window_started_at = CASE |
| 24 | WHEN auth_throttle.window_started_at < $3 THEN now() |
| 25 | ELSE auth_throttle.window_started_at |
| 26 | END |
| 27 | RETURNING hits, window_started_at |
| 28 | ` |
| 29 | |
| 30 | type BumpAuthThrottleParams struct { |
| 31 | Scope string |
| 32 | Identifier string |
| 33 | WindowStartedAt pgtype.Timestamptz |
| 34 | } |
| 35 | |
| 36 | type BumpAuthThrottleRow struct { |
| 37 | Hits int32 |
| 38 | WindowStartedAt pgtype.Timestamptz |
| 39 | } |
| 40 | |
| 41 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 42 | // Increments the hit counter for (scope, identifier). When the existing |
| 43 | // window is older than the supplied window-start cutoff, resets to 1 and |
| 44 | // starts a new window. Returns the post-bump (hits, window_started_at). |
| 45 | func (q *Queries) BumpAuthThrottle(ctx context.Context, db DBTX, arg BumpAuthThrottleParams) (BumpAuthThrottleRow, error) { |
| 46 | row := db.QueryRow(ctx, bumpAuthThrottle, arg.Scope, arg.Identifier, arg.WindowStartedAt) |
| 47 | var i BumpAuthThrottleRow |
| 48 | err := row.Scan(&i.Hits, &i.WindowStartedAt) |
| 49 | return i, err |
| 50 | } |
| 51 | |
| 52 | const purgeStaleAuthThrottle = `-- name: PurgeStaleAuthThrottle :exec |
| 53 | DELETE FROM auth_throttle WHERE window_started_at < $1 |
| 54 | ` |
| 55 | |
| 56 | func (q *Queries) PurgeStaleAuthThrottle(ctx context.Context, db DBTX, windowStartedAt pgtype.Timestamptz) error { |
| 57 | _, err := db.Exec(ctx, purgeStaleAuthThrottle, windowStartedAt) |
| 58 | return err |
| 59 | } |
| 60 | |
| 61 | const resetAuthThrottle = `-- name: ResetAuthThrottle :exec |
| 62 | DELETE FROM auth_throttle WHERE scope = $1 AND identifier = $2 |
| 63 | ` |
| 64 | |
| 65 | type ResetAuthThrottleParams struct { |
| 66 | Scope string |
| 67 | Identifier string |
| 68 | } |
| 69 | |
| 70 | func (q *Queries) ResetAuthThrottle(ctx context.Context, db DBTX, arg ResetAuthThrottleParams) error { |
| 71 | _, err := db.Exec(ctx, resetAuthThrottle, arg.Scope, arg.Identifier) |
| 72 | return err |
| 73 | } |
| 74 |