forked from mtusi2026/mtusi2026lab5
feat: port lab4 implementation
This commit is contained in:
@@ -0,0 +1,365 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: attachments.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const createAttachment = `-- name: CreateAttachment :one
|
||||
INSERT INTO attachments (
|
||||
id,
|
||||
chat_id,
|
||||
owner_id,
|
||||
s3_key,
|
||||
filename,
|
||||
content_type,
|
||||
size_bytes,
|
||||
sha256,
|
||||
error_code
|
||||
)
|
||||
SELECT
|
||||
$1::uuid,
|
||||
$2::uuid,
|
||||
$3::uuid,
|
||||
$4::text,
|
||||
$5::text,
|
||||
$6::attachment_content_type,
|
||||
$7::int,
|
||||
$8::text,
|
||||
NULLIF($9::text, '')
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM chat_members
|
||||
WHERE chat_id = $2
|
||||
AND user_id = $3
|
||||
)
|
||||
RETURNING id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at
|
||||
`
|
||||
|
||||
type CreateAttachmentParams struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
S3Key string
|
||||
Filename string
|
||||
ContentType AttachmentContentType
|
||||
SizeBytes int
|
||||
Sha256 string
|
||||
ErrorCode string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateAttachment(ctx context.Context, arg CreateAttachmentParams) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, createAttachment,
|
||||
arg.ID,
|
||||
arg.ChatID,
|
||||
arg.OwnerID,
|
||||
arg.S3Key,
|
||||
arg.Filename,
|
||||
arg.ContentType,
|
||||
arg.SizeBytes,
|
||||
arg.Sha256,
|
||||
arg.ErrorCode,
|
||||
)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.S3Key,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const createMessageAttachment = `-- name: CreateMessageAttachment :exec
|
||||
INSERT INTO message_attachments (
|
||||
message_id,
|
||||
attachment_id,
|
||||
position
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3
|
||||
)
|
||||
`
|
||||
|
||||
type CreateMessageAttachmentParams struct {
|
||||
MessageID uuid.UUID
|
||||
AttachmentID uuid.UUID
|
||||
Position int
|
||||
}
|
||||
|
||||
func (q *Queries) CreateMessageAttachment(ctx context.Context, arg CreateMessageAttachmentParams) error {
|
||||
_, err := q.db.Exec(ctx, createMessageAttachment, arg.MessageID, arg.AttachmentID, arg.Position)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteQueuedAttachment = `-- name: DeleteQueuedAttachment :execrows
|
||||
DELETE FROM attachments
|
||||
WHERE id = $1::uuid AND status = 'queued'
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteQueuedAttachment(ctx context.Context, id uuid.UUID) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, deleteQueuedAttachment, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const getAttachmentByID = `-- name: GetAttachmentByID :one
|
||||
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at FROM attachments
|
||||
WHERE id = $1::uuid
|
||||
`
|
||||
|
||||
func (q *Queries) GetAttachmentByID(ctx context.Context, dollar_1 uuid.UUID) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, getAttachmentByID, dollar_1)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.S3Key,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listAttachmentsByIDs = `-- name: ListAttachmentsByIDs :many
|
||||
SELECT status
|
||||
FROM attachments
|
||||
WHERE chat_id = $1::uuid
|
||||
AND id = ANY($2::uuid[])
|
||||
`
|
||||
|
||||
type ListAttachmentsByIDsParams struct {
|
||||
ChatID uuid.UUID
|
||||
Ids []uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) ListAttachmentsByIDs(ctx context.Context, arg ListAttachmentsByIDsParams) ([]AttachmentStatus, error) {
|
||||
rows, err := q.db.Query(ctx, listAttachmentsByIDs, arg.ChatID, arg.Ids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []AttachmentStatus{}
|
||||
for rows.Next() {
|
||||
var status AttachmentStatus
|
||||
if err := rows.Scan(&status); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, status)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listAttachmentsForReconcile = `-- name: ListAttachmentsForReconcile :many
|
||||
SELECT
|
||||
a.id,
|
||||
a.s3_key,
|
||||
a.status,
|
||||
a.size_bytes,
|
||||
a.created_at,
|
||||
EXISTS (
|
||||
SELECT 1 FROM message_attachments ma
|
||||
WHERE ma.attachment_id = a.id
|
||||
) AS referenced
|
||||
FROM attachments a
|
||||
`
|
||||
|
||||
type ListAttachmentsForReconcileRow struct {
|
||||
ID uuid.UUID
|
||||
S3Key string
|
||||
Status AttachmentStatus
|
||||
SizeBytes int
|
||||
CreatedAt time.Time
|
||||
Referenced bool
|
||||
}
|
||||
|
||||
func (q *Queries) ListAttachmentsForReconcile(ctx context.Context) ([]ListAttachmentsForReconcileRow, error) {
|
||||
rows, err := q.db.Query(ctx, listAttachmentsForReconcile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListAttachmentsForReconcileRow{}
|
||||
for rows.Next() {
|
||||
var i ListAttachmentsForReconcileRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.S3Key,
|
||||
&i.Status,
|
||||
&i.SizeBytes,
|
||||
&i.CreatedAt,
|
||||
&i.Referenced,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listMessageAttachmentIDs = `-- name: ListMessageAttachmentIDs :many
|
||||
SELECT attachment_id
|
||||
FROM message_attachments
|
||||
WHERE message_id = $1::uuid
|
||||
ORDER BY position
|
||||
`
|
||||
|
||||
func (q *Queries) ListMessageAttachmentIDs(ctx context.Context, messageID uuid.UUID) ([]uuid.UUID, error) {
|
||||
rows, err := q.db.Query(ctx, listMessageAttachmentIDs, messageID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []uuid.UUID{}
|
||||
for rows.Next() {
|
||||
var attachment_id uuid.UUID
|
||||
if err := rows.Scan(&attachment_id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, attachment_id)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listMessageAttachmentsByMessageIDs = `-- name: ListMessageAttachmentsByMessageIDs :many
|
||||
SELECT message_id, attachment_id
|
||||
FROM message_attachments
|
||||
WHERE message_id = ANY($1::uuid[])
|
||||
ORDER BY message_id, position
|
||||
`
|
||||
|
||||
type ListMessageAttachmentsByMessageIDsRow struct {
|
||||
MessageID uuid.UUID
|
||||
AttachmentID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) ListMessageAttachmentsByMessageIDs(ctx context.Context, messageIds []uuid.UUID) ([]ListMessageAttachmentsByMessageIDsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listMessageAttachmentsByMessageIDs, messageIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListMessageAttachmentsByMessageIDsRow{}
|
||||
for rows.Next() {
|
||||
var i ListMessageAttachmentsByMessageIDsRow
|
||||
if err := rows.Scan(&i.MessageID, &i.AttachmentID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listStaleAttachmentUploads = `-- name: ListStaleAttachmentUploads :many
|
||||
SELECT id, s3_key FROM attachments
|
||||
WHERE status = 'queued' AND created_at < $1::timestamptz
|
||||
`
|
||||
|
||||
type ListStaleAttachmentUploadsRow struct {
|
||||
ID uuid.UUID
|
||||
S3Key string
|
||||
}
|
||||
|
||||
func (q *Queries) ListStaleAttachmentUploads(ctx context.Context, cutoff time.Time) ([]ListStaleAttachmentUploadsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listStaleAttachmentUploads, cutoff)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListStaleAttachmentUploadsRow{}
|
||||
for rows.Next() {
|
||||
var i ListStaleAttachmentUploadsRow
|
||||
if err := rows.Scan(&i.ID, &i.S3Key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const markAttachmentFailedIfReady = `-- name: MarkAttachmentFailedIfReady :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'failed',
|
||||
error_code = $1::text,
|
||||
updated_at = now()
|
||||
WHERE id = $2::uuid AND status = 'ready'
|
||||
`
|
||||
|
||||
type MarkAttachmentFailedIfReadyParams struct {
|
||||
ErrorCode string
|
||||
ID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) MarkAttachmentFailedIfReady(ctx context.Context, arg MarkAttachmentFailedIfReadyParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, markAttachmentFailedIfReady, arg.ErrorCode, arg.ID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const markAttachmentReady = `-- name: MarkAttachmentReady :one
|
||||
UPDATE attachments
|
||||
SET status = 'ready', updated_at = now()
|
||||
WHERE id = $1::uuid
|
||||
RETURNING id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at
|
||||
`
|
||||
|
||||
func (q *Queries) MarkAttachmentReady(ctx context.Context, dollar_1 uuid.UUID) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, markAttachmentReady, dollar_1)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.S3Key,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
Reference in New Issue
Block a user