forked from mtusi2026/mtusi2026lab4
lab3
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: idempotency.sql
|
||||
|
||||
package sqlcgen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const claimMessageIdempotency = `-- name: ClaimMessageIdempotency :one
|
||||
INSERT INTO message_idempotency (actor_id, chat_id, key, request_hash, expires_at)
|
||||
VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5
|
||||
)
|
||||
ON CONFLICT (actor_id, chat_id, key) DO UPDATE
|
||||
SET request_hash = EXCLUDED.request_hash,
|
||||
expires_at = EXCLUDED.expires_at,
|
||||
message_id = NULL
|
||||
WHERE message_idempotency.expires_at <= now()
|
||||
RETURNING key
|
||||
`
|
||||
|
||||
type ClaimMessageIdempotencyParams struct {
|
||||
ActorID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
Key string
|
||||
RequestHash []byte
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
// Claims the key for this actor/chat and returns whether this request owns it.
|
||||
// A live record wins: the statement returns nothing and the caller replays the
|
||||
// stored message. An expired record is replaced in place.
|
||||
func (q *Queries) ClaimMessageIdempotency(ctx context.Context, arg ClaimMessageIdempotencyParams) (string, error) {
|
||||
row := q.db.QueryRow(ctx, claimMessageIdempotency,
|
||||
arg.ActorID,
|
||||
arg.ChatID,
|
||||
arg.Key,
|
||||
arg.RequestHash,
|
||||
arg.ExpiresAt,
|
||||
)
|
||||
var key string
|
||||
err := row.Scan(&key)
|
||||
return key, err
|
||||
}
|
||||
|
||||
const completeMessageIdempotency = `-- name: CompleteMessageIdempotency :exec
|
||||
UPDATE message_idempotency
|
||||
SET message_id = $1
|
||||
WHERE actor_id = $2
|
||||
AND chat_id = $3
|
||||
AND key = $4
|
||||
`
|
||||
|
||||
type CompleteMessageIdempotencyParams struct {
|
||||
MessageID pgtype.UUID
|
||||
ActorID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
Key string
|
||||
}
|
||||
|
||||
func (q *Queries) CompleteMessageIdempotency(ctx context.Context, arg CompleteMessageIdempotencyParams) error {
|
||||
_, err := q.db.Exec(ctx, completeMessageIdempotency,
|
||||
arg.MessageID,
|
||||
arg.ActorID,
|
||||
arg.ChatID,
|
||||
arg.Key,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const getMessageByID = `-- name: GetMessageByID :one
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetMessageByID(ctx context.Context, id uuid.UUID) (Message, error) {
|
||||
row := q.db.QueryRow(ctx, getMessageByID, id)
|
||||
var i Message
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.SenderID,
|
||||
&i.Text,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getMessageIdempotency = `-- name: GetMessageIdempotency :one
|
||||
SELECT message_id, request_hash
|
||||
FROM message_idempotency
|
||||
WHERE actor_id = $1
|
||||
AND chat_id = $2
|
||||
AND key = $3
|
||||
FOR UPDATE
|
||||
`
|
||||
|
||||
type GetMessageIdempotencyParams struct {
|
||||
ActorID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
Key string
|
||||
}
|
||||
|
||||
type GetMessageIdempotencyRow struct {
|
||||
MessageID pgtype.UUID
|
||||
RequestHash []byte
|
||||
}
|
||||
|
||||
// Concurrent duplicates block here until the owner commits, then read the
|
||||
// result it stored in the same transaction.
|
||||
func (q *Queries) GetMessageIdempotency(ctx context.Context, arg GetMessageIdempotencyParams) (GetMessageIdempotencyRow, error) {
|
||||
row := q.db.QueryRow(ctx, getMessageIdempotency, arg.ActorID, arg.ChatID, arg.Key)
|
||||
var i GetMessageIdempotencyRow
|
||||
err := row.Scan(&i.MessageID, &i.RequestHash)
|
||||
return i, err
|
||||
}
|
||||
Reference in New Issue
Block a user