// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.26.0
// source: token.sql

package db

import (
	"context"

	"github.com/jackc/pgx/v5/pgconn"
)

const createToken = `-- name: CreateToken :one
INSERT INTO tokens (
  created_at, category, token_key, value_hash, expires
) VALUES (
  $1, $2, $3, $4, $5
)
ON CONFLICT(category,token_key) DO UPDATE SET created_at = $1, category = $2, token_key = $3, value_hash = $4, expires = $5
RETURNING created_at, category, token_key, value_hash, expires
`

type CreateTokenParams struct {
	CreatedAt int64
	Category  string
	TokenKey  string
	ValueHash []byte
	Expires   int64
}

func (q *Queries) CreateToken(ctx context.Context, arg CreateTokenParams) (Token, error) {
	row := q.db.QueryRow(ctx, createToken,
		arg.CreatedAt,
		arg.Category,
		arg.TokenKey,
		arg.ValueHash,
		arg.Expires,
	)
	var i Token
	err := row.Scan(
		&i.CreatedAt,
		&i.Category,
		&i.TokenKey,
		&i.ValueHash,
		&i.Expires,
	)
	return i, err
}

const deleteToken = `-- name: DeleteToken :execresult
DELETE FROM tokens WHERE (category = $1 AND token_key = $2) OR expires < $3
`

type DeleteTokenParams struct {
	Category string
	TokenKey string
	Now      int64
}

func (q *Queries) DeleteToken(ctx context.Context, arg DeleteTokenParams) (pgconn.CommandTag, error) {
	return q.db.Exec(ctx, deleteToken, arg.Category, arg.TokenKey, arg.Now)
}

const findToken = `-- name: FindToken :one
SELECT created_at, category, token_key, value_hash, expires FROM tokens WHERE category = $1 AND token_key = $2 AND expires > $3
`

type FindTokenParams struct {
	Category string
	TokenKey string
	Now      int64
}

func (q *Queries) FindToken(ctx context.Context, arg FindTokenParams) (Token, error) {
	row := q.db.QueryRow(ctx, findToken, arg.Category, arg.TokenKey, arg.Now)
	var i Token
	err := row.Scan(
		&i.CreatedAt,
		&i.Category,
		&i.TokenKey,
		&i.ValueHash,
		&i.Expires,
	)
	return i, err
}

const findTokenByValue = `-- name: FindTokenByValue :one
SELECT created_at, category, token_key, value_hash, expires FROM tokens WHERE category = $1 AND value_hash = $2 AND expires > $3
`

type FindTokenByValueParams struct {
	Category  string
	ValueHash []byte
	Now       int64
}

func (q *Queries) FindTokenByValue(ctx context.Context, arg FindTokenByValueParams) (Token, error) {
	row := q.db.QueryRow(ctx, findTokenByValue, arg.Category, arg.ValueHash, arg.Now)
	var i Token
	err := row.Scan(
		&i.CreatedAt,
		&i.Category,
		&i.TokenKey,
		&i.ValueHash,
		&i.Expires,
	)
	return i, err
}
