// Code generated by sqlc. DO NOT EDIT. // versions: // sqlc v1.27.0 // source: client.sql package repository import ( "context" "github.com/jackc/pgx/v5/pgtype" ) const addClientCanSync = `-- name: AddClientCanSync :exec INSERT INTO clientCanSync (canSync, clientId) VALUES ($1, $2) ` type AddClientCanSyncParams struct { Cansync bool `db:"cansync"` Clientid pgtype.UUID `db:"clientid"` } // AddClientCanSync // // INSERT INTO clientCanSync (canSync, clientId) VALUES ($1, $2) func (q *Queries) AddClientCanSync(ctx context.Context, arg *AddClientCanSyncParams) error { _, err := q.db.Exec(ctx, addClientCanSync, arg.Cansync, arg.Clientid) return err } const createClient = `-- name: CreateClient :one INSERT INTO clients (name) VALUES ($1) RETURNING id ` // CreateClient // // INSERT INTO clients (name) VALUES ($1) RETURNING id func (q *Queries) CreateClient(ctx context.Context, name string) (pgtype.UUID, error) { row := q.db.QueryRow(ctx, createClient, name) var id pgtype.UUID err := row.Scan(&id) return id, err } const getClient = `-- name: GetClient :one SELECT c.id, c.name, cs.canSync FROM clients as c JOIN currentClientCanSync as cs on cs.clientId = c.id WHERE c.id = $1 ` type GetClientRow struct { ID pgtype.UUID `db:"id"` Name string `db:"name"` Cansync bool `db:"cansync"` } // GetClient // // SELECT c.id, c.name, cs.canSync // FROM clients as c // JOIN currentClientCanSync as cs on cs.clientId = c.id // WHERE c.id = $1 func (q *Queries) GetClient(ctx context.Context, id pgtype.UUID) (*GetClientRow, error) { row := q.db.QueryRow(ctx, getClient, id) var i GetClientRow err := row.Scan(&i.ID, &i.Name, &i.Cansync) return &i, err } const isClientSynced = `-- name: IsClientSynced :one 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 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 d.document_id, cte.id AS text_entry_id FROM 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 d.document_id, cqdt.queryId, cqdt.queryVersion, dte.text_entry_id FROM doc_clean_entries d JOIN collectorQueryDependencyTree cqdt ON cqdt.clientId = d.client_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 client 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 ` // IsClientSynced // // 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 // 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 // d.document_id, // cte.id AS text_entry_id // FROM // 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 // d.document_id, // cqdt.queryId, // cqdt.queryVersion, // dte.text_entry_id // FROM // doc_clean_entries d // JOIN collectorQueryDependencyTree cqdt ON cqdt.clientId = d.client_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 client 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) IsClientSynced(ctx context.Context, dollar_1 pgtype.UUID) (bool, error) { row := q.db.QueryRow(ctx, isClientSynced, dollar_1) var is_synced bool err := row.Scan(&is_synced) return is_synced, err } const updateClient = `-- name: UpdateClient :exec UPDATE clients SET name = $1 WHERE id = $2 ` type UpdateClientParams struct { Name string `db:"name"` ID pgtype.UUID `db:"id"` } // UpdateClient // // UPDATE clients SET name = $1 WHERE id = $2 func (q *Queries) UpdateClient(ctx context.Context, arg *UpdateClientParams) error { _, err := q.db.Exec(ctx, updateClient, arg.Name, arg.ID) return err }