| 1 | // Code generated by sqlc. DO NOT EDIT. |
| 2 | // versions: |
| 3 | // sqlc v1.31.1 |
| 4 | // source: user_recovery_codes.sql |
| 5 | |
| 6 | package usersdb |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | ) |
| 11 | |
| 12 | const consumeRecoveryCode = `-- name: ConsumeRecoveryCode :execrows |
| 13 | UPDATE user_recovery_codes |
| 14 | SET used_at = now() |
| 15 | WHERE user_id = $1 AND code_hash = $2 AND used_at IS NULL |
| 16 | ` |
| 17 | |
| 18 | type ConsumeRecoveryCodeParams struct { |
| 19 | UserID int64 |
| 20 | CodeHash []byte |
| 21 | } |
| 22 | |
| 23 | // Atomically marks a code as used iff it exists for the user, matches the |
| 24 | // supplied hash, and isn't already used. Rows-affected==1 means accepted; |
| 25 | // 0 means rejected. |
| 26 | func (q *Queries) ConsumeRecoveryCode(ctx context.Context, db DBTX, arg ConsumeRecoveryCodeParams) (int64, error) { |
| 27 | result, err := db.Exec(ctx, consumeRecoveryCode, arg.UserID, arg.CodeHash) |
| 28 | if err != nil { |
| 29 | return 0, err |
| 30 | } |
| 31 | return result.RowsAffected(), nil |
| 32 | } |
| 33 | |
| 34 | const countUnusedRecoveryCodes = `-- name: CountUnusedRecoveryCodes :one |
| 35 | SELECT count(*) FROM user_recovery_codes WHERE user_id = $1 AND used_at IS NULL |
| 36 | ` |
| 37 | |
| 38 | func (q *Queries) CountUnusedRecoveryCodes(ctx context.Context, db DBTX, userID int64) (int64, error) { |
| 39 | row := db.QueryRow(ctx, countUnusedRecoveryCodes, userID) |
| 40 | var count int64 |
| 41 | err := row.Scan(&count) |
| 42 | return count, err |
| 43 | } |
| 44 | |
| 45 | const deleteUserRecoveryCodes = `-- name: DeleteUserRecoveryCodes :exec |
| 46 | DELETE FROM user_recovery_codes WHERE user_id = $1 |
| 47 | ` |
| 48 | |
| 49 | func (q *Queries) DeleteUserRecoveryCodes(ctx context.Context, db DBTX, userID int64) error { |
| 50 | _, err := db.Exec(ctx, deleteUserRecoveryCodes, userID) |
| 51 | return err |
| 52 | } |
| 53 | |
| 54 | const insertRecoveryCode = `-- name: InsertRecoveryCode :exec |
| 55 | |
| 56 | INSERT INTO user_recovery_codes (user_id, code_hash) VALUES ($1, $2) |
| 57 | ` |
| 58 | |
| 59 | type InsertRecoveryCodeParams struct { |
| 60 | UserID int64 |
| 61 | CodeHash []byte |
| 62 | } |
| 63 | |
| 64 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 65 | func (q *Queries) InsertRecoveryCode(ctx context.Context, db DBTX, arg InsertRecoveryCodeParams) error { |
| 66 | _, err := db.Exec(ctx, insertRecoveryCode, arg.UserID, arg.CodeHash) |
| 67 | return err |
| 68 | } |
| 69 |