83 lines
2.6 KiB
SQL
83 lines
2.6 KiB
SQL
-- name: CreateAttachment :one
|
|
INSERT INTO attachments (
|
|
id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256, status
|
|
)
|
|
VALUES (
|
|
sqlc.arg(id),
|
|
sqlc.arg(chat_id),
|
|
sqlc.arg(owner_id),
|
|
sqlc.arg(object_key),
|
|
sqlc.arg(filename),
|
|
sqlc.arg(content_type),
|
|
sqlc.arg(size_bytes),
|
|
sqlc.arg(sha256),
|
|
'pending'
|
|
)
|
|
RETURNING id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256,
|
|
status, error_code, created_at, updated_at;
|
|
|
|
-- name: MarkAttachmentReady :one
|
|
UPDATE attachments
|
|
SET status = 'ready',
|
|
object_key = sqlc.arg(object_key),
|
|
updated_at = now()
|
|
WHERE id = sqlc.arg(id)
|
|
AND status = 'pending'
|
|
RETURNING id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256,
|
|
status, error_code, created_at, updated_at;
|
|
|
|
-- name: GetAttachmentForMember :one
|
|
SELECT a.id, a.chat_id, a.owner_id, a.object_key, a.filename, a.content_type,
|
|
a.size_bytes, a.sha256, a.status, a.error_code, a.created_at, a.updated_at
|
|
FROM attachments a
|
|
JOIN chat_members cm
|
|
ON cm.chat_id = a.chat_id
|
|
AND cm.user_id = sqlc.arg(user_id)
|
|
WHERE a.id = sqlc.arg(id);
|
|
|
|
-- name: GetAttachmentForOwner :one
|
|
SELECT a.id, a.chat_id, a.owner_id, a.object_key, a.filename, a.content_type,
|
|
a.size_bytes, a.sha256, a.status, a.error_code, a.created_at, a.updated_at
|
|
FROM attachments a
|
|
JOIN chat_members cm
|
|
ON cm.chat_id = a.chat_id
|
|
AND cm.user_id = a.owner_id
|
|
WHERE a.id = sqlc.arg(id)
|
|
AND a.owner_id = sqlc.arg(owner_id);
|
|
|
|
-- name: ListAttachmentStatesForChat :many
|
|
SELECT id, status
|
|
FROM attachments
|
|
WHERE chat_id = sqlc.arg(chat_id)
|
|
AND id = ANY(sqlc.arg(ids)::uuid[]);
|
|
|
|
-- name: AttachToMessage :exec
|
|
INSERT INTO message_attachments (message_id, attachment_id, position)
|
|
VALUES (sqlc.arg(message_id), sqlc.arg(attachment_id), sqlc.arg(position));
|
|
|
|
-- name: ListMessageAttachments :many
|
|
SELECT message_id, attachment_id
|
|
FROM message_attachments
|
|
WHERE message_id = ANY(sqlc.arg(message_ids)::uuid[])
|
|
ORDER BY message_id, position;
|
|
|
|
-- name: DeletePendingAttachment :exec
|
|
DELETE FROM attachments
|
|
WHERE id = sqlc.arg(id)
|
|
AND status = 'pending';
|
|
|
|
-- name: ListStalePendingAttachments :many
|
|
SELECT id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256,
|
|
status, error_code, created_at, updated_at
|
|
FROM attachments
|
|
WHERE status = 'pending'
|
|
AND created_at < sqlc.arg(created_before)
|
|
ORDER BY created_at
|
|
LIMIT sqlc.arg(row_limit);
|
|
|
|
-- name: ListAttachmentKeys :many
|
|
SELECT id, object_key, status, created_at
|
|
FROM attachments
|
|
ORDER BY id
|
|
LIMIT sqlc.arg(row_limit) OFFSET sqlc.arg(row_offset);
|