stillbox/sql/postgres/queries/share.sql

51 lines
1 KiB
SQL
Raw Normal View History

-- name: GetShare :one
SELECT
2025-02-14 00:19:24 -05:00
s.id,
s.entity_type,
s.entity_id,
s.entity_date,
s.owner,
s.expiration
FROM shares s
WHERE s.id = @id;
-- name: CreateShare :exec
INSERT INTO shares (
id,
entity_type,
entity_id,
2025-01-19 21:51:39 -05:00
entity_date,
owner,
expiration
2025-01-19 21:51:39 -05:00
) VALUES (@id, @entity_type, @entity_id, sqlc.narg('entity_date'), @owner, sqlc.narg('expiration'));
-- name: DeleteShare :exec
DELETE FROM shares WHERE id = @id;
-- name: PruneShares :exec
DELETE FROM shares WHERE expiration < NOW();
2025-02-10 21:29:32 -05:00
-- name: GetSharesP :many
SELECT
2025-02-14 00:19:24 -05:00
sqlc.embed(s),
u.username
2025-02-10 21:29:32 -05:00
FROM shares s
2025-02-14 00:19:24 -05:00
JOIN users u ON (s.owner = u.id)
2025-02-10 21:29:32 -05:00
WHERE
CASE WHEN sqlc.narg('owner')::INTEGER IS NOT NULL THEN
s.owner = @owner ELSE TRUE END
ORDER BY
CASE WHEN @direction::TEXT = 'asc' THEN s.entity_date END ASC,
CASE WHEN @direction::TEXT = 'desc' THEN s.entity_date END DESC
OFFSET sqlc.arg('offset') ROWS
FETCH NEXT sqlc.arg('per_page') ROWS ONLY
;
-- name: GetSharesPCount :one
SELECT COUNT(*)
FROM shares s
WHERE
CASE WHEN sqlc.narg('owner')::INTEGER IS NOT NULL THEN
s.owner = @owner ELSE TRUE END
;