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

63 lines
1.8 KiB
SQL
Raw Normal View History

-- name: CreateClient :exec
INSERT INTO clients (clientId, name) VALUES ($1, $2);
2025-01-21 18:24:14 +00:00
-- name: GetClient :one
SELECT * FROM fullClients WHERE clientId = $1;
-- name: ListClients :many
-- Returns all clients ordered by name
SELECT * FROM fullClients ORDER BY name;
2025-01-21 18:24:14 +00:00
-- name: UpdateClient :exec
UPDATE clients SET name = $1 WHERE clientId = $2;
-- name: AddClientCanSync :exec
INSERT INTO clientCanSync (canSync, clientId) VALUES ($1, $2);
-- name: IsClientSynced :one
-- Query functionality has been removed. A client is considered synced when
-- all documents have completed text extraction (or failed with explicit failure).
-- See remove_query_plan.md for details.
WITH
docs AS (
-- Get all documents for this client
SELECT id, clientId FROM documents WHERE clientId = $1
),
doc_clean_entries as (
-- Documents with their current clean entries
SELECT
d.id AS document_id,
d.clientId as client_id,
cte.id AS clean_entry_id,
cte.fail as clean_fail
FROM
docs d
LEFT JOIN currentCleanEntries cte ON cte.documentId = d.id
),
doc_text_entries AS (
-- Documents with their current text entries
SELECT
d.document_id,
cte.id AS text_entry_id,
d.clean_entry_id,
d.clean_fail,
d.client_id
FROM
doc_clean_entries d
LEFT JOIN currentTextEntries cte ON cte.cleanId = d.clean_entry_id
)
SELECT (
-- No documents means client is synced
NOT EXISTS (SELECT 1 FROM docs)
OR
-- All documents have completed processing (clean entry exists and either
-- text entry exists or clean failed)
NOT EXISTS (
SELECT 1 FROM doc_text_entries
where clean_entry_id is null or
(clean_fail is null and text_entry_id is null)
)
)::bool as is_synced;