getandlist

This commit is contained in:
Michael McGuinness
2025-01-03 14:08:04 +00:00
parent 788b21594c
commit 483a47e4e0
8 changed files with 129 additions and 39 deletions
+47 -1
View File
@@ -12,7 +12,7 @@ import (
)
const deprecateQuery = `-- name: DeprecateQuery :exec
INSERT INTO queryDeprecations (queryId) VALUES ($1) RETURNING id
INSERT INTO queryDeprecations (queryId) VALUES ($1)
`
func (q *Queries) DeprecateQuery(ctx context.Context, queryid pgtype.UUID) error {
@@ -98,3 +98,49 @@ func (q *Queries) IsQueryDeprecated(ctx context.Context, queryid pgtype.UUID) (b
err := row.Scan(&exists)
return exists, err
}
const listQueries = `-- name: ListQueries :many
SELECT q.id, q.type, q.activeVersion, c.config, ARRAY_AGG(r.requiredQueryId) AS requiredIds
FROM queries AS q
JOIN queryConfigs AS c ON q.id = c.queryId
JOIN requiredQueries AS r ON q.id = r.queryId
WHERE c.addedVersion >= q.activeVersion
and COALESCE(c.removedVersion, q.activeVersion - 1) < q.activeVersion
and r.addedVersion >= q.activeVersion
and COALESCE(r.removedVersion, q.activeVersion - 1) < q.activeVersion
GROUP BY q.id, c.config
`
type ListQueriesRow struct {
ID pgtype.UUID
Type Querytype
Activeversion int32
Config []byte
Requiredids []pgtype.UUID
}
func (q *Queries) ListQueries(ctx context.Context) ([]ListQueriesRow, error) {
rows, err := q.db.Query(ctx, listQueries)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListQueriesRow
for rows.Next() {
var i ListQueriesRow
if err := rows.Scan(
&i.ID,
&i.Type,
&i.Activeversion,
&i.Config,
&i.Requiredids,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}