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

290 lines
8.0 KiB
Go
Raw Normal View History

2025-01-23 14:56:20 +00:00
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
// source: job.sql
package repository
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const addJobCanSync = `-- name: AddJobCanSync :exec
INSERT INTO jobCanSync (canSync, jobId) VALUES ($1, $2)
`
type AddJobCanSyncParams struct {
Cansync bool `db:"cansync"`
Jobid pgtype.UUID `db:"jobid"`
}
// AddJobCanSync
//
// INSERT INTO jobCanSync (canSync, jobId) VALUES ($1, $2)
func (q *Queries) AddJobCanSync(ctx context.Context, arg *AddJobCanSyncParams) error {
_, err := q.db.Exec(ctx, addJobCanSync, arg.Cansync, arg.Jobid)
return err
}
2025-01-23 14:56:20 +00:00
const createJob = `-- name: CreateJob :one
INSERT INTO jobs (clientId) VALUES ($1) RETURNING id
`
// CreateJob
//
// INSERT INTO jobs (clientId) VALUES ($1) RETURNING id
func (q *Queries) CreateJob(ctx context.Context, clientid pgtype.UUID) (pgtype.UUID, error) {
row := q.db.QueryRow(ctx, createJob, clientid)
var id pgtype.UUID
err := row.Scan(&id)
return id, err
}
const getJob = `-- name: GetJob :one
SELECT j.id, j.clientId, cs.canSync
FROM jobs as j
JOIN currentJobCanSync as cs on cs.jobId = j.id
WHERE j.id = $1
2025-01-23 14:56:20 +00:00
`
type GetJobRow struct {
ID pgtype.UUID `db:"id"`
Clientid pgtype.UUID `db:"clientid"`
Cansync bool `db:"cansync"`
}
2025-01-23 14:56:20 +00:00
// GetJob
//
// SELECT j.id, j.clientId, cs.canSync
// FROM jobs as j
// JOIN currentJobCanSync as cs on cs.jobId = j.id
// WHERE j.id = $1
func (q *Queries) GetJob(ctx context.Context, id pgtype.UUID) (*GetJobRow, error) {
row := q.db.QueryRow(ctx, getJob, id)
var i GetJobRow
2025-01-23 14:56:20 +00:00
err := row.Scan(&i.ID, &i.Clientid, &i.Cansync)
return &i, err
}
const isJobSynced = `-- name: IsJobSynced :one
WITH
docs AS (
-- Get all documents for this job
SELECT id, jobId FROM documents WHERE jobId = $1
),
2025-02-28 13:11:53 +00:00
doc_clean_entries as (
-- Documents with their current clean entries
SELECT
d.id AS document_id,
d.jobId as job_id,
cte.id AS clean_entry_id
FROM
docs d
LEFT JOIN currentCleanEntries cte ON cte.documentId = d.id
WHERE cte.id is null or cte.id is not null and cte.fail is null
),
doc_text_entries AS (
-- Documents with their current text entries
SELECT
2025-02-28 13:11:53 +00:00
d.document_id,
cte.id AS text_entry_id
FROM
2025-02-28 13:11:53 +00:00
doc_clean_entries d
LEFT JOIN currentTextEntries cte ON cte.cleanEntryId = d.clean_entry_id
),
required_results AS (
-- All required document-query-version combinations
SELECT
2025-02-28 13:11:53 +00:00
d.document_id,
cqdt.queryId,
cqdt.queryVersion,
dte.text_entry_id
FROM
2025-02-28 13:11:53 +00:00
doc_clean_entries d
JOIN collectorQueryDependencyTree cqdt ON cqdt.jobId = d.job_id
JOIN doc_text_entries dte ON dte.document_id = d.document_id
and dte.text_entry_id IS NOT NULL
),
existing_results AS (
-- Valid results that exist
SELECT
rr.document_id AS document_id,
r.queryId,
r.id AS result_id
FROM
results r
JOIN required_results rr ON
r.queryId = rr.queryId AND
r.queryVersion = rr.queryVersion AND
r.textEntryId = rr.text_entry_id
),
missing_results AS (
-- Find missing results
SELECT rr.queryId
FROM required_results rr
LEFT JOIN existing_results er on er.queryId = rr.queryId
WHERE er.result_id is null
),
dependency_check AS (
-- Check for missing dependencies
SELECT DISTINCT er.queryId, er.result_id, qcri.queryId, rd.resultId
FROM existing_results er
JOIN queryCurrentRequiredIds qcri on qcri.requiredQueryId = er.queryId
LEFT JOIN resultDependencies rd on rd.requiredResultId = er.result_id
WHERE rd.resultId is null
)
SELECT (
-- No documents means job is synced
NOT EXISTS (SELECT 1 FROM docs)
OR
-- Documents with no text entries
(NOT EXISTS (SELECT 1 FROM doc_text_entries WHERE text_entry_id IS NULL)
and
-- Documents with missing results
NOT EXISTS (SELECT 1 FROM missing_results)
and
-- Documents with missing dependencies
NOT EXISTS (SELECT 1 FROM dependency_check))
)::bool as is_synced
`
// IsJobSynced
//
// WITH
// docs AS (
// -- Get all documents for this job
// SELECT id, jobId FROM documents WHERE jobId = $1
// ),
2025-02-28 13:11:53 +00:00
// doc_clean_entries as (
// -- Documents with their current clean entries
// SELECT
// d.id AS document_id,
// d.jobId as job_id,
// cte.id AS clean_entry_id
// FROM
// docs d
// LEFT JOIN currentCleanEntries cte ON cte.documentId = d.id
// WHERE cte.id is null or cte.id is not null and cte.fail is null
// ),
// doc_text_entries AS (
// -- Documents with their current text entries
// SELECT
2025-02-28 13:11:53 +00:00
// d.document_id,
// cte.id AS text_entry_id
// FROM
2025-02-28 13:11:53 +00:00
// doc_clean_entries d
// LEFT JOIN currentTextEntries cte ON cte.cleanEntryId = d.clean_entry_id
// ),
// required_results AS (
// -- All required document-query-version combinations
// SELECT
2025-02-28 13:11:53 +00:00
// d.document_id,
// cqdt.queryId,
// cqdt.queryVersion,
// dte.text_entry_id
// FROM
2025-02-28 13:11:53 +00:00
// doc_clean_entries d
// JOIN collectorQueryDependencyTree cqdt ON cqdt.jobId = d.job_id
// JOIN doc_text_entries dte ON dte.document_id = d.document_id
// and dte.text_entry_id IS NOT NULL
// ),
// existing_results AS (
// -- Valid results that exist
// SELECT
// rr.document_id AS document_id,
// r.queryId,
// r.id AS result_id
// FROM
// results r
// JOIN required_results rr ON
// r.queryId = rr.queryId AND
// r.queryVersion = rr.queryVersion AND
// r.textEntryId = rr.text_entry_id
// ),
// missing_results AS (
// -- Find missing results
// SELECT rr.queryId
// FROM required_results rr
// LEFT JOIN existing_results er on er.queryId = rr.queryId
// WHERE er.result_id is null
// ),
// dependency_check AS (
// -- Check for missing dependencies
// SELECT DISTINCT er.queryId, er.result_id, qcri.queryId, rd.resultId
// FROM existing_results er
// JOIN queryCurrentRequiredIds qcri on qcri.requiredQueryId = er.queryId
// LEFT JOIN resultDependencies rd on rd.requiredResultId = er.result_id
// WHERE rd.resultId is null
// )
// SELECT (
// -- No documents means job is synced
// NOT EXISTS (SELECT 1 FROM docs)
//
// OR
//
// -- Documents with no text entries
// (NOT EXISTS (SELECT 1 FROM doc_text_entries WHERE text_entry_id IS NULL)
//
// and
//
// -- Documents with missing results
// NOT EXISTS (SELECT 1 FROM missing_results)
//
// and
//
// -- Documents with missing dependencies
// NOT EXISTS (SELECT 1 FROM dependency_check))
// )::bool as is_synced
func (q *Queries) IsJobSynced(ctx context.Context, dollar_1 pgtype.UUID) (bool, error) {
row := q.db.QueryRow(ctx, isJobSynced, dollar_1)
var is_synced bool
err := row.Scan(&is_synced)
return is_synced, err
}
const listJobDocumentIDsBatch = `-- name: ListJobDocumentIDsBatch :many
SELECT id, totalCount FROM listJobDocumentIDs($1, $2, $3)
`
type ListJobDocumentIDsBatchParams struct {
Jobid pgtype.UUID `db:"jobid"`
Batchsize int32 `db:"batchsize"`
Pageoffset int32 `db:"pageoffset"`
}
type ListJobDocumentIDsBatchRow struct {
ID pgtype.UUID `db:"id"`
Totalcount *int64 `db:"totalcount"`
}
// ListJobDocumentIDsBatch
//
// SELECT id, totalCount FROM listJobDocumentIDs($1, $2, $3)
func (q *Queries) ListJobDocumentIDsBatch(ctx context.Context, arg *ListJobDocumentIDsBatchParams) ([]*ListJobDocumentIDsBatchRow, error) {
rows, err := q.db.Query(ctx, listJobDocumentIDsBatch, arg.Jobid, arg.Batchsize, arg.Pageoffset)
if err != nil {
return nil, err
}
defer rows.Close()
items := []*ListJobDocumentIDsBatchRow{}
for rows.Next() {
var i ListJobDocumentIDsBatchRow
if err := rows.Scan(&i.ID, &i.Totalcount); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}