forked from mtusi2026/mtusi2026lab3
79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: idempotency.sql
|
|
|
|
package queries
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const replayMessage = `-- name: ReplayMessage :one
|
|
SELECT m.id,m.chat_id,m.sender_id,m.text,m.created_at,i.payload_hash
|
|
FROM idempotency i JOIN messages m ON m.id=i.message_id
|
|
WHERE i.user_id=$1 AND i.chat_id=$2 AND i.key=$3
|
|
`
|
|
|
|
type ReplayMessageParams struct {
|
|
UserID uuid.UUID
|
|
ChatID uuid.UUID
|
|
Key string
|
|
}
|
|
|
|
type ReplayMessageRow struct {
|
|
ID uuid.UUID
|
|
ChatID uuid.UUID
|
|
SenderID uuid.UUID
|
|
Text string
|
|
CreatedAt time.Time
|
|
PayloadHash string
|
|
}
|
|
|
|
func (q *Queries) ReplayMessage(ctx context.Context, arg ReplayMessageParams) (ReplayMessageRow, error) {
|
|
row := q.db.QueryRow(ctx, replayMessage, arg.UserID, arg.ChatID, arg.Key)
|
|
var i ReplayMessageRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ChatID,
|
|
&i.SenderID,
|
|
&i.Text,
|
|
&i.CreatedAt,
|
|
&i.PayloadHash,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const reserveKey = `-- name: ReserveKey :one
|
|
INSERT INTO idempotency(user_id,chat_id,key,payload_hash,message_id,expires_at)
|
|
VALUES($1,$2,$3,$4,$5,clock_timestamp()+interval '24 hours')
|
|
ON CONFLICT(user_id,chat_id,key) DO UPDATE SET payload_hash=excluded.payload_hash,
|
|
message_id=excluded.message_id,expires_at=excluded.expires_at
|
|
WHERE idempotency.expires_at<=clock_timestamp()
|
|
RETURNING message_id
|
|
`
|
|
|
|
type ReserveKeyParams struct {
|
|
UserID uuid.UUID
|
|
ChatID uuid.UUID
|
|
Key string
|
|
PayloadHash string
|
|
MessageID uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) ReserveKey(ctx context.Context, arg ReserveKeyParams) (uuid.UUID, error) {
|
|
row := q.db.QueryRow(ctx, reserveKey,
|
|
arg.UserID,
|
|
arg.ChatID,
|
|
arg.Key,
|
|
arg.PayloadHash,
|
|
arg.MessageID,
|
|
)
|
|
var message_id uuid.UUID
|
|
err := row.Scan(&message_id)
|
|
return message_id, err
|
|
}
|