Files
query-orchestration/internal/database/repository/result.sql.go
T

276 lines
8.7 KiB
Go
Raw Normal View History

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
// source: result.sql
package repository
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const getResultValueWithVersion = `-- name: GetResultValueWithVersion :one
SELECT id, value FROM results WHERE queryId = $1 and queryVersion = $2 and documentId = $3 and cleanVersion >= $4 and textVersion >= $5
2024-12-20 17:35:33 +00:00
`
type GetResultValueWithVersionParams struct {
Queryid pgtype.UUID `db:"queryid"`
Queryversion int32 `db:"queryversion"`
Documentid pgtype.UUID `db:"documentid"`
Cleanversion int32 `db:"cleanversion"`
Textversion int32 `db:"textversion"`
}
type GetResultValueWithVersionRow struct {
ID pgtype.UUID `db:"id"`
Value string `db:"value"`
}
// GetResultValueWithVersion
//
// SELECT id, value FROM results WHERE queryId = $1 and queryVersion = $2 and documentId = $3 and cleanVersion >= $4 and textVersion >= $5
func (q *Queries) GetResultValueWithVersion(ctx context.Context, arg *GetResultValueWithVersionParams) (*GetResultValueWithVersionRow, error) {
row := q.db.QueryRow(ctx, getResultValueWithVersion,
arg.Queryid,
arg.Queryversion,
arg.Documentid,
arg.Cleanversion,
arg.Textversion,
)
var i GetResultValueWithVersionRow
err := row.Scan(&i.ID, &i.Value)
return &i, err
}
const listQueryRequirementValues = `-- name: ListQueryRequirementValues :many
WITH reqQueries as (
SELECT q.id as queryId, q.activeVersion, q.type
FROM requiredQueries as rq
JOIN queries as q on q.id = rq.requiredQueryId
WHERE rq.queryId = $2
and $3 >= rq.addedVersion
and (rq.removedVersion is null or $3 < rq.removedVersion)
),
docs as (
SELECT id, jobId
FROM documents
WHERE id = $1
),
codeVersions as (
SELECT
d.id as documentId,
coalesce(ccv.minCleanVersion, 1) as minCleanVersion,
coalesce(ccv.minTextVersion, 1) as minTextVersion
FROM docs as d
LEFT JOIN collectors as c on c.jobId = d.jobId
LEFT JOIN collectorCodeVersions as ccv on c.id = ccv.collectorId
and c.activeVersion >= ccv.addedVersion
and c.activeVersion < COALESCE(ccv.removedVersion, c.activeVersion)
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 = $1
and r.queryVersion = rq.activeVersion
JOIN codeVersions as ccv on ccv.documentId = r.documentId
and r.cleanVersion >= ccv.minCleanVersion
and r.textVersion >= ccv.minTextVersion
)
SELECT DISTINCT lv.queryId, lv.type, r.value
FROM latestVersions as lv
JOIN results as r ON r.queryId = lv.queryId
and r.documentId = $1
and r.queryVersion = lv.queryVersion
and r.cleanVersion = lv.cleanVersion
and r.textVersion = lv.textVersion
and lv.rowNumber = 1
`
type ListQueryRequirementValuesParams struct {
Documentid pgtype.UUID `db:"documentid"`
Queryid pgtype.UUID `db:"queryid"`
Version int32 `db:"version"`
}
type ListQueryRequirementValuesRow struct {
Queryid pgtype.UUID `db:"queryid"`
Type Querytype `db:"type"`
Value string `db:"value"`
2024-12-20 17:35:33 +00:00
}
// ListQueryRequirementValues
//
// WITH reqQueries as (
// SELECT q.id as queryId, q.activeVersion, q.type
// FROM requiredQueries as rq
// JOIN queries as q on q.id = rq.requiredQueryId
// WHERE rq.queryId = $2
// and $3 >= rq.addedVersion
// and (rq.removedVersion is null or $3 < rq.removedVersion)
// ),
// docs as (
// SELECT id, jobId
// FROM documents
// WHERE id = $1
// ),
// codeVersions as (
// SELECT
// d.id as documentId,
// coalesce(ccv.minCleanVersion, 1) as minCleanVersion,
// coalesce(ccv.minTextVersion, 1) as minTextVersion
// FROM docs as d
// LEFT JOIN collectors as c on c.jobId = d.jobId
// LEFT JOIN collectorCodeVersions as ccv on c.id = ccv.collectorId
// and c.activeVersion >= ccv.addedVersion
// and c.activeVersion < COALESCE(ccv.removedVersion, c.activeVersion)
// 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 = $1
// and r.queryVersion = rq.activeVersion
// JOIN codeVersions as ccv on ccv.documentId = r.documentId
// and r.cleanVersion >= ccv.minCleanVersion
// and r.textVersion >= ccv.minTextVersion
// )
// SELECT DISTINCT lv.queryId, lv.type, r.value
// FROM latestVersions as lv
// JOIN results as r ON r.queryId = lv.queryId
// and r.documentId = $1
// and r.queryVersion = lv.queryVersion
// and r.cleanVersion = lv.cleanVersion
// and r.textVersion = lv.textVersion
// and lv.rowNumber = 1
func (q *Queries) ListQueryRequirementValues(ctx context.Context, arg *ListQueryRequirementValuesParams) ([]*ListQueryRequirementValuesRow, error) {
rows, err := q.db.Query(ctx, listQueryRequirementValues, arg.Documentid, arg.Queryid, arg.Version)
2024-12-20 17:35:33 +00:00
if err != nil {
return nil, err
}
defer rows.Close()
items := []*ListQueryRequirementValuesRow{}
2024-12-20 17:35:33 +00:00
for rows.Next() {
var i ListQueryRequirementValuesRow
if err := rows.Scan(&i.Queryid, &i.Type, &i.Value); err != nil {
2024-12-20 17:35:33 +00:00
return nil, err
}
items = append(items, &i)
2024-12-20 17:35:33 +00:00
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listUnsyncedNoDepsQueriesByDocId = `-- 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
LEFT JOIN results as r on r.queryId = dt.queryId
and r.documentId = d.id
and r.queryVersion = dt.queryVersion
and r.cleanVersion >= c.minCleanVersion and r.textVersion >= c.minTextVersion
where r.value is null
)
SELECT DISTINCT queryId FROM unsyncedQueries as baseuq
WHERE NOT EXISTS (
SELECT 1 FROM unsyncedQueries as uq WHERE uq.queryId = any(baseuq.requiredIds)
)
`
// ListUnsyncedNoDepsQueriesByDocId
//
// 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
// LEFT JOIN results as r on r.queryId = dt.queryId
// and r.documentId = d.id
// and r.queryVersion = dt.queryVersion
// and r.cleanVersion >= c.minCleanVersion and r.textVersion >= c.minTextVersion
// where r.value is null
// )
// SELECT DISTINCT queryId FROM unsyncedQueries as baseuq
// WHERE NOT EXISTS (
// SELECT 1 FROM unsyncedQueries as uq WHERE uq.queryId = any(baseuq.requiredIds)
// )
func (q *Queries) ListUnsyncedNoDepsQueriesByDocId(ctx context.Context, dollar_1 pgtype.UUID) ([]pgtype.UUID, error) {
rows, err := q.db.Query(ctx, listUnsyncedNoDepsQueriesByDocId, dollar_1)
if err != nil {
return nil, err
}
defer rows.Close()
items := []pgtype.UUID{}
for rows.Next() {
var queryid pgtype.UUID
if err := rows.Scan(&queryid); err != nil {
return nil, err
}
items = append(items, queryid)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
2024-12-20 17:35:33 +00:00
const setResult = `-- name: SetResult :exec
INSERT INTO results (queryId, documentId, value, cleanVersion, textVersion, queryVersion) VALUES ($1, $2, $3, $4, $5, $6)
2024-12-20 17:35:33 +00:00
`
type SetResultParams struct {
Queryid pgtype.UUID `db:"queryid"`
Documentid pgtype.UUID `db:"documentid"`
Value string `db:"value"`
Cleanversion int32 `db:"cleanversion"`
Textversion int32 `db:"textversion"`
Queryversion int32 `db:"queryversion"`
2024-12-20 17:35:33 +00:00
}
// SetResult
//
// INSERT INTO results (queryId, documentId, value, cleanVersion, textVersion, queryVersion) VALUES ($1, $2, $3, $4, $5, $6)
func (q *Queries) SetResult(ctx context.Context, arg *SetResultParams) error {
_, err := q.db.Exec(ctx, setResult,
2024-12-20 17:35:33 +00:00
arg.Queryid,
arg.Documentid,
arg.Value,
arg.Cleanversion,
arg.Textversion,
arg.Queryversion,
)
return err
2024-12-20 17:35:33 +00:00
}