Files
query-orchestration/internal/database/queries/text.sql
T

69 lines
2.1 KiB
SQL
Raw Normal View History

-- name: IsDocumentTextExtracted :one
SELECT EXISTS(
SELECT 1 FROM currentTextEntries WHERE documentId = @documentId
);
-- name: GetDocumentTextExtractionByHash :one
SELECT id FROM currentTextEntries WHERE cleanId = @cleanEntryId and hash = @hash;
2025-04-08 12:44:09 +00:00
-- name: GetTextractOutputCurrentPart :one
WITH client as (
select clientId from clients where clientId = @clientId
2025-04-08 12:44:09 +00:00
),
parts as (
SELECT extract.part
2025-04-08 12:44:09 +00:00
FROM client as c
JOIN documents as docs on docs.clientId = c.clientId
2025-04-08 12:44:09 +00:00
JOIN documentCleans as clean on docs.id = clean.documentId
JOIN documentTextExtractions as extract
on extract.cleanId = clean.id
WHERE date(extract.createdAt) = date(@queryDate)
2025-04-08 12:44:09 +00:00
),
max_part as (
SELECT COALESCE(max(part), 0) as max_part_num FROM parts
)
SELECT
COALESCE(p.part, 0)::unsignedsmallint as part,
COUNT(p.part) as count
FROM parts p
RIGHT JOIN max_part mp ON p.part = mp.max_part_num
GROUP BY p.part;
-- name: GetTextOutCurrentPart :one
WITH client as (
select clientId from clients where clientId = @clientId
2025-04-08 12:44:09 +00:00
),
parts as (
SELECT extract.part
2025-04-08 12:44:09 +00:00
FROM client as c
JOIN documents as docs on docs.clientId = c.clientId
2025-04-08 12:44:09 +00:00
JOIN documentCleans as clean on docs.id = clean.documentId
JOIN documentTextExtractions extract ON extract.cleanId = clean.id
WHERE date(extract.createdAt) = date(@queryDate)
2025-04-08 12:44:09 +00:00
),
max_part as (
SELECT COALESCE(max(part), 0) as max_part_num FROM parts
)
SELECT
COALESCE(p.part, 0)::unsignedsmallint as part,
COUNT(p.part) as count
FROM parts p
RIGHT JOIN max_part mp ON p.part = mp.max_part_num
GROUP BY p.part;
-- name: AddDocumentText :one
2025-04-08 12:44:09 +00:00
INSERT INTO documentTextExtractions
(cleanId, bucket, key, hash, createdAt, part)
VALUES ($1, $2, $3, $4, $5, $6)
2025-04-08 12:44:09 +00:00
returning id;
-- name: AddDocumentTextEntry :exec
2025-04-08 12:44:09 +00:00
INSERT INTO documentTextExtractionEntries
(textId, version)
VALUES ($1, $2);
-- name: GetTextEntryByDocId :one
SELECT id, documentId, bucket, key, hash, cleanId, extractionVersion
FROM currentTextEntries
WHERE documentId = @documentId;