// Code generated by sqlc. DO NOT EDIT. // versions: // sqlc v1.31.1 // source: api_keys.sql package store import ( "context" "database/sql" ) const addAPIKeySpend = `-- name: AddAPIKeySpend :exec UPDATE api_keys SET spent_micros = spent_micros + ? WHERE id = ? ` type AddAPIKeySpendParams struct { SpentMicros int64 `json:"spent_micros"` ID string `json:"id"` } func (q *Queries) AddAPIKeySpend(ctx context.Context, arg AddAPIKeySpendParams) error { _, err := q.db.ExecContext(ctx, addAPIKeySpend, arg.SpentMicros, arg.ID) return err } const createAPIKey = `-- name: CreateAPIKey :one INSERT INTO api_keys ( id, user_id, key_hash, key_word, key_last4, name, max_budget_micros, created_at ) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING id, user_id, key_hash, key_word, key_last4, name, max_budget_micros, spent_micros, last_used_at, created_at, revoked_at ` type CreateAPIKeyParams struct { ID string `json:"id"` UserID string `json:"user_id"` KeyHash string `json:"key_hash"` KeyWord string `json:"key_word"` KeyLast4 string `json:"key_last4"` Name string `json:"name"` MaxBudgetMicros sql.NullInt64 `json:"max_budget_micros"` CreatedAt int64 `json:"created_at"` } func (q *Queries) CreateAPIKey(ctx context.Context, arg CreateAPIKeyParams) (ApiKey, error) { row := q.db.QueryRowContext(ctx, createAPIKey, arg.ID, arg.UserID, arg.KeyHash, arg.KeyWord, arg.KeyLast4, arg.Name, arg.MaxBudgetMicros, arg.CreatedAt, ) var i ApiKey err := row.Scan( &i.ID, &i.UserID, &i.KeyHash, &i.KeyWord, &i.KeyLast4, &i.Name, &i.MaxBudgetMicros, &i.SpentMicros, &i.LastUsedAt, &i.CreatedAt, &i.RevokedAt, ) return i, err } const getAPIKeyByHash = `-- name: GetAPIKeyByHash :one SELECT id, user_id, key_hash, key_word, key_last4, name, max_budget_micros, spent_micros, last_used_at, created_at, revoked_at FROM api_keys WHERE key_hash = ? AND revoked_at IS NULL ` func (q *Queries) GetAPIKeyByHash(ctx context.Context, keyHash string) (ApiKey, error) { row := q.db.QueryRowContext(ctx, getAPIKeyByHash, keyHash) var i ApiKey err := row.Scan( &i.ID, &i.UserID, &i.KeyHash, &i.KeyWord, &i.KeyLast4, &i.Name, &i.MaxBudgetMicros, &i.SpentMicros, &i.LastUsedAt, &i.CreatedAt, &i.RevokedAt, ) return i, err } const listAPIKeysForUser = `-- name: ListAPIKeysForUser :many SELECT id, user_id, key_hash, key_word, key_last4, name, max_budget_micros, spent_micros, last_used_at, created_at, revoked_at FROM api_keys WHERE user_id = ? ORDER BY created_at DESC ` func (q *Queries) ListAPIKeysForUser(ctx context.Context, userID string) ([]ApiKey, error) { rows, err := q.db.QueryContext(ctx, listAPIKeysForUser, userID) if err != nil { return nil, err } defer rows.Close() items := []ApiKey{} for rows.Next() { var i ApiKey if err := rows.Scan( &i.ID, &i.UserID, &i.KeyHash, &i.KeyWord, &i.KeyLast4, &i.Name, &i.MaxBudgetMicros, &i.SpentMicros, &i.LastUsedAt, &i.CreatedAt, &i.RevokedAt, ); err != nil { return nil, err } items = append(items, i) } if err := rows.Close(); err != nil { return nil, err } if err := rows.Err(); err != nil { return nil, err } return items, nil } const renameAPIKey = `-- name: RenameAPIKey :exec UPDATE api_keys SET name = ? WHERE id = ? AND user_id = ? ` type RenameAPIKeyParams struct { Name string `json:"name"` ID string `json:"id"` UserID string `json:"user_id"` } func (q *Queries) RenameAPIKey(ctx context.Context, arg RenameAPIKeyParams) error { _, err := q.db.ExecContext(ctx, renameAPIKey, arg.Name, arg.ID, arg.UserID) return err } const revokeAPIKey = `-- name: RevokeAPIKey :exec UPDATE api_keys SET revoked_at = ? WHERE id = ? AND user_id = ? AND revoked_at IS NULL ` type RevokeAPIKeyParams struct { RevokedAt sql.NullInt64 `json:"revoked_at"` ID string `json:"id"` UserID string `json:"user_id"` } func (q *Queries) RevokeAPIKey(ctx context.Context, arg RevokeAPIKeyParams) error { _, err := q.db.ExecContext(ctx, revokeAPIKey, arg.RevokedAt, arg.ID, arg.UserID) return err } const touchAPIKey = `-- name: TouchAPIKey :exec UPDATE api_keys SET last_used_at = ? WHERE id = ? ` type TouchAPIKeyParams struct { LastUsedAt sql.NullInt64 `json:"last_used_at"` ID string `json:"id"` } // Debounced caller: only call when last_used_at is older than 60s. func (q *Queries) TouchAPIKey(ctx context.Context, arg TouchAPIKeyParams) error { _, err := q.db.ExecContext(ctx, touchAPIKey, arg.LastUsedAt, arg.ID) return err }