2025-01-29 11:52:37 +00:00
|
|
|
-- name: ListQueryRequirementValues :many
|
2025-02-11 15:22:59 +00:00
|
|
|
WITH reqQueries as (
|
2025-02-14 18:43:26 +00:00
|
|
|
SELECT q.id as queryId, av.id as activeVersion, q.type
|
2025-01-29 11:52:37 +00:00
|
|
|
FROM requiredQueries as rq
|
|
|
|
|
JOIN queries as q on q.id = rq.requiredQueryId
|
2025-02-14 18:43:26 +00:00
|
|
|
LEFT JOIN queryCurrentActiveVersions as av on q.id = av.queryId
|
2025-02-11 15:22:59 +00:00
|
|
|
WHERE rq.queryId = @queryId
|
2025-02-14 18:43:26 +00:00
|
|
|
and isInVersion(@version, rq.addedVersion, rq.removedVersion)
|
2025-02-11 15:22:59 +00:00
|
|
|
),
|
2025-02-14 10:56:24 +00:00
|
|
|
docs as (
|
|
|
|
|
SELECT id, jobId
|
|
|
|
|
FROM documents
|
|
|
|
|
WHERE id = @documentId
|
|
|
|
|
),
|
2025-02-11 15:22:59 +00:00
|
|
|
codeVersions as (
|
|
|
|
|
SELECT
|
|
|
|
|
d.id as documentId,
|
|
|
|
|
coalesce(ccv.minCleanVersion, 1) as minCleanVersion,
|
|
|
|
|
coalesce(ccv.minTextVersion, 1) as minTextVersion
|
2025-02-14 10:56:24 +00:00
|
|
|
FROM docs as d
|
2025-02-11 15:22:59 +00:00
|
|
|
LEFT JOIN collectors as c on c.jobId = d.jobId
|
|
|
|
|
LEFT JOIN collectorCodeVersions as ccv on c.id = ccv.collectorId
|
2025-02-14 18:43:26 +00:00
|
|
|
and isInVersion(c.activeVersion, ccv.addedVersion, ccv.removedVersion)
|
2025-02-11 15:22:59 +00:00
|
|
|
LIMIT 1
|
|
|
|
|
),
|
|
|
|
|
latestVersions AS (
|
|
|
|
|
SELECT
|
|
|
|
|
rq.queryId,
|
|
|
|
|
rq.type,
|
|
|
|
|
r.queryVersion,
|
|
|
|
|
r.cleanVersion,
|
|
|
|
|
r.textVersion,
|
|
|
|
|
ROW_NUMBER() OVER (
|
|
|
|
|
PARTITION BY rq.queryId
|
|
|
|
|
ORDER BY r.cleanVersion DESC, r.textVersion DESC
|
|
|
|
|
) as rowNumber
|
|
|
|
|
FROM reqQueries as rq
|
|
|
|
|
LEFT JOIN results as r ON rq.queryId = r.queryId
|
|
|
|
|
and r.documentId = @documentId
|
|
|
|
|
and r.queryVersion = rq.activeVersion
|
2025-02-14 10:56:24 +00:00
|
|
|
JOIN codeVersions as ccv on ccv.documentId = r.documentId
|
2025-02-11 15:22:59 +00:00
|
|
|
and r.cleanVersion >= ccv.minCleanVersion
|
|
|
|
|
and r.textVersion >= ccv.minTextVersion
|
|
|
|
|
)
|
2025-02-14 10:56:24 +00:00
|
|
|
SELECT DISTINCT lv.queryId, lv.type, r.value
|
2025-02-11 15:22:59 +00:00
|
|
|
FROM latestVersions as lv
|
|
|
|
|
JOIN results as r ON r.queryId = lv.queryId
|
|
|
|
|
and r.documentId = @documentId
|
|
|
|
|
and r.queryVersion = lv.queryVersion
|
|
|
|
|
and r.cleanVersion = lv.cleanVersion
|
|
|
|
|
and r.textVersion = lv.textVersion
|
|
|
|
|
and lv.rowNumber = 1;
|
2024-12-20 17:35:33 +00:00
|
|
|
|
2025-02-11 15:22:59 +00:00
|
|
|
-- name: SetResult :exec
|
|
|
|
|
INSERT INTO results (queryId, documentId, value, cleanVersion, textVersion, queryVersion) VALUES ($1, $2, $3, $4, $5, $6);
|
2025-01-29 11:52:37 +00:00
|
|
|
|
|
|
|
|
-- name: GetResultValueWithVersion :one
|
|
|
|
|
SELECT id, value FROM results WHERE queryId = $1 and queryVersion = $2 and documentId = $3 and cleanVersion >= $4 and textVersion >= $5;
|
|
|
|
|
|
2025-02-11 15:22:59 +00:00
|
|
|
-- name: ListUnsyncedNoDepsQueriesByDocId :many
|
|
|
|
|
WITH docs as (
|
|
|
|
|
SELECT id, jobId from documents where id = $1
|
|
|
|
|
),
|
|
|
|
|
unsyncedQueries AS (
|
|
|
|
|
SELECT DISTINCT dt.queryId, dt.requiredIds, r.value, d.jobID, d.id
|
|
|
|
|
from docs as d
|
|
|
|
|
JOIN collectorQueryDependencyTree as dt on d.jobId = dt.jobId
|
|
|
|
|
JOIN fullActiveCollectors as c on c.id = dt.collectorId
|
2025-01-29 11:52:37 +00:00
|
|
|
LEFT JOIN results as r on r.queryId = dt.queryId
|
2025-02-11 15:22:59 +00:00
|
|
|
and r.documentId = d.id
|
2025-01-29 11:52:37 +00:00
|
|
|
and r.queryVersion = dt.queryVersion
|
|
|
|
|
and r.cleanVersion >= c.minCleanVersion and r.textVersion >= c.minTextVersion
|
2025-02-11 15:22:59 +00:00
|
|
|
where r.value is null
|
2025-01-29 11:52:37 +00:00
|
|
|
)
|
2025-02-11 15:22:59 +00:00
|
|
|
SELECT DISTINCT queryId FROM unsyncedQueries as baseuq
|
|
|
|
|
WHERE NOT EXISTS (
|
|
|
|
|
SELECT 1 FROM unsyncedQueries as uq WHERE uq.queryId = any(baseuq.requiredIds)
|
|
|
|
|
);
|
|
|
|
|
|