2026-09-08 15:36:10 +08:00
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: query.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgconn"
)
2026-09-22 16:41:46 +08:00
const addResourceTag = `-- name: AddResourceTag :execresult
INSERT INTO resource_tags (resource_type, resource_id, tag_id, assigned_by_user_id)
SELECT $1, $2, t.id, $3::uuid
FROM tags t WHERE t.id = $4::uuid AND t.deleted_at IS NULL
`
type AddResourceTagParams struct {
ResourceType string
ResourceID string
ActorID string
TagID string
}
func ( q * Queries ) AddResourceTag ( ctx context . Context , arg AddResourceTagParams ) ( pgconn . CommandTag , error ) {
return q . db . Exec ( ctx , addResourceTag ,
arg . ResourceType ,
arg . ResourceID ,
arg . ActorID ,
arg . TagID ,
)
}
2026-09-09 18:32:10 +08:00
const canUseSystem = `-- name: CanUseSystem :one
SELECT EXISTS (
SELECT 1 FROM users u
WHERE u.id = $1 AND u.status = 'active' AND u.deleted_at IS NULL
AND (NOT EXISTS (
SELECT 1 FROM settings WHERE key = 'billing' AND value->>'charging_mode' = 'remote'
) OR EXISTS (
SELECT 1 FROM user_identities i
WHERE i.user_id = u.id AND i.provider = 'baizhiyun' AND i.deleted_at IS NULL
))
)::boolean
`
func ( q * Queries ) CanUseSystem ( ctx context . Context , id string ) ( bool , error ) {
row := q . db . QueryRow ( ctx , canUseSystem , id )
var column_1 bool
err := row . Scan ( & column_1 )
return column_1 , err
}
2026-10-15 10:54:05 +08:00
const createAllShares = `-- name: CreateAllShares :execresult
INSERT INTO resource_access_grants (resource_type, resource_id, all_users, access_level, granted_by_user_id)
VALUES ($1, $2, true, 'read_only', $3)
ON CONFLICT (resource_type, resource_id) WHERE all_users DO NOTHING
`
type CreateAllSharesParams struct {
ResourceType string
ResourceID string
GrantedByUserID string
}
func ( q * Queries ) CreateAllShares ( ctx context . Context , arg CreateAllSharesParams ) ( pgconn . CommandTag , error ) {
return q . db . Exec ( ctx , createAllShares , arg . ResourceType , arg . ResourceID , arg . GrantedByUserID )
}
2026-09-08 15:36:10 +08:00
const createGrant = `-- name: CreateGrant :execresult
2026-09-08 19:20:12 +08:00
INSERT INTO resource_access_grants (resource_type, resource_id, user_id, group_id, all_users, access_level, usage_requirement,
2026-09-08 15:36:10 +08:00
granted_by_user_id)
VALUES ($1, $2, NULLIF ($3::text,
2026-09-08 19:20:12 +08:00
'')::UUID,NULLIF($4::text,'')::uuid, $5, 'read_only', $6, $7)
2026-09-08 15:36:10 +08:00
`
type CreateGrantParams struct {
ResourceType string
ResourceID string
UserID string
GroupID string
2026-09-08 19:20:12 +08:00
AllUsers bool
2026-09-08 15:36:10 +08:00
UsageRequirement string
GrantedByUserID string
}
func ( q * Queries ) CreateGrant ( ctx context . Context , arg CreateGrantParams ) ( pgconn . CommandTag , error ) {
return q . db . Exec ( ctx , createGrant ,
arg . ResourceType ,
arg . ResourceID ,
arg . UserID ,
arg . GroupID ,
2026-09-08 19:20:12 +08:00
arg . AllUsers ,
2026-09-08 15:36:10 +08:00
arg . UsageRequirement ,
arg . GrantedByUserID ,
)
}
2026-10-08 15:17:25 +08:00
const createGroupShares = `-- name: CreateGroupShares :execresult
INSERT INTO resource_access_grants (resource_type, resource_id, group_id, access_level, granted_by_user_id)
SELECT $1, $2, g.id, 'read_only', $3
FROM groups g
WHERE g.id::text = ANY ($4::text[]) AND g.deleted_at IS NULL
ON CONFLICT (resource_type, resource_id, group_id) WHERE group_id IS NOT NULL DO NOTHING
`
type CreateGroupSharesParams struct {
ResourceType string
ResourceID string
GrantedByUserID string
GroupIds [] string
}
func ( q * Queries ) CreateGroupShares ( ctx context . Context , arg CreateGroupSharesParams ) ( pgconn . CommandTag , error ) {
return q . db . Exec ( ctx , createGroupShares ,
arg . ResourceType ,
arg . ResourceID ,
arg . GrantedByUserID ,
arg . GroupIds ,
)
}
2026-11-16 11:53:46 +08:00
const createKbGrant = `-- name: CreateKbGrant :exec
INSERT INTO resource_access_grants (resource_type, resource_id, user_id, group_id, all_users, access_level, usage_requirement, granted_by_user_id)
VALUES ('kb', $1, NULLIF($2::text,'')::uuid, NULLIF($3::text,'')::uuid,
$4, $5, 'optional', $6::uuid)
`
type CreateKbGrantParams struct {
ResourceID string
UserID string
GroupID string
AllUsers bool
AccessLevel string
Actor string
}
func ( q * Queries ) CreateKbGrant ( ctx context . Context , arg CreateKbGrantParams ) error {
_ , err := q . db . Exec ( ctx , createKbGrant ,
arg . ResourceID ,
arg . UserID ,
arg . GroupID ,
arg . AllUsers ,
arg . AccessLevel ,
arg . Actor ,
)
return err
}
2026-09-08 15:36:10 +08:00
const createShares = `-- name: CreateShares :execresult
INSERT INTO resource_access_grants (resource_type, resource_id, user_id, access_level, granted_by_user_id)
SELECT
$1,
$2,
u.id,
'read_only',
$3
FROM
users u
WHERE
u.id::text = ANY ($4::text[])
ON CONFLICT (resource_type,
resource_id,
user_id)
WHERE
user_id IS NOT NULL
DO NOTHING
`
type CreateSharesParams struct {
ResourceType string
ResourceID string
GrantedByUserID string
UserIds [] string
}
func ( q * Queries ) CreateShares ( ctx context . Context , arg CreateSharesParams ) ( pgconn . CommandTag , error ) {
return q . db . Exec ( ctx , createShares ,
arg . ResourceType ,
arg . ResourceID ,
arg . GrantedByUserID ,
arg . UserIds ,
)
}
const createTag = `-- name: CreateTag :one
INSERT INTO tags (name, created_by_user_id)
VALUES ($1, $2)
RETURNING
jsonb_build_object('id', id, 'name', name)
`
type CreateTagParams struct {
Name string
CreatedByUserID string
}
func ( q * Queries ) CreateTag ( ctx context . Context , arg CreateTagParams ) ([] byte , error ) {
row := q . db . QueryRow ( ctx , createTag , arg . Name , arg . CreatedByUserID )
var jsonb_build_object [] byte
err := row . Scan ( & jsonb_build_object )
return jsonb_build_object , err
}
const deleteGrants = `-- name: DeleteGrants :execresult
DELETE FROM resource_access_grants
WHERE resource_type = $1
AND resource_id = $2
`
type DeleteGrantsParams struct {
ResourceType string
ResourceID string
}
func ( q * Queries ) DeleteGrants ( ctx context . Context , arg DeleteGrantsParams ) ( pgconn . CommandTag , error ) {
return q . db . Exec ( ctx , deleteGrants , arg . ResourceType , arg . ResourceID )
}
2026-11-16 11:53:46 +08:00
const deleteKbGrant = `-- name: DeleteKbGrant :exec
DELETE FROM resource_access_grants WHERE id = $1 AND resource_type = 'kb'
`
func ( q * Queries ) DeleteKbGrant ( ctx context . Context , id string ) error {
_ , err := q . db . Exec ( ctx , deleteKbGrant , id )
return err
}
2026-09-08 15:36:10 +08:00
const deleteTag = `-- name: DeleteTag :one
UPDATE
tags
SET
deleted_at = now(),
updated_at = now()
WHERE
id = $1
AND deleted_at IS NULL
RETURNING
jsonb_build_object('id', id)
`
func ( q * Queries ) DeleteTag ( ctx context . Context , id string ) ([] byte , error ) {
row := q . db . QueryRow ( ctx , deleteTag , id )
var jsonb_build_object [] byte
err := row . Scan ( & jsonb_build_object )
return jsonb_build_object , err
}
2026-10-12 18:59:31 +08:00
const getResourceImport = `-- name: GetResourceImport :one
SELECT publisher, slug, retired_at IS NOT NULL AS retired
FROM resource_import_bindings
WHERE resource_type = $1 AND resource_id = $2
`
type GetResourceImportParams struct {
ResourceType string
ResourceID string
}
type GetResourceImportRow struct {
Publisher string
Slug string
Retired interface {}
}
func ( q * Queries ) GetResourceImport ( ctx context . Context , arg GetResourceImportParams ) ( GetResourceImportRow , error ) {
row := q . db . QueryRow ( ctx , getResourceImport , arg . ResourceType , arg . ResourceID )
var i GetResourceImportRow
err := row . Scan ( & i . Publisher , & i . Slug , & i . Retired )
return i , err
}
2026-09-08 15:36:10 +08:00
const hasAccess = `-- name: HasAccess :one
WITH RECURSIVE user_groups (
group_id
) AS (
SELECT
id
FROM
GROUPS
WHERE
deleted_at IS NULL
AND id IN (
SELECT
group_id
FROM
group_users
WHERE
2026-09-08 19:20:12 +08:00
user_id = $3
2026-09-08 15:36:10 +08:00
AND removed_at IS NULL)
UNION
SELECT
parent.id
FROM
GROUPS g
JOIN user_groups ug ON ug.group_id = g.id
JOIN GROUPS parent ON parent.id = g.parent_id
WHERE
g.deleted_at IS NULL
AND parent.deleted_at IS NULL
)
SELECT
EXISTS (
SELECT
1
FROM
2026-11-26 22:37:49 +08:00
effective_resource_grants rag
2026-09-08 15:36:10 +08:00
WHERE
2026-09-08 19:20:12 +08:00
rag.resource_type = $1
AND rag.resource_id = $2
2026-11-30 17:53:28 +08:00
AND NOT EXISTS (SELECT 1 FROM resource_drafts d JOIN approval_requests a ON a.id=d.approval_id
WHERE d.resource_type=rag.resource_type AND d.resource_id=rag.resource_id AND a.action='create')
2026-09-08 19:20:12 +08:00
AND ((rag.all_users AND EXISTS (
SELECT 1 FROM users u WHERE u.id = $3 AND u.status = 'active' AND u.deleted_at IS NULL
))
OR rag.user_id = $3
2026-09-08 15:36:10 +08:00
OR rag.group_id IN (
SELECT
group_id
FROM
user_groups)))
`
type HasAccessParams struct {
ResourceType string
ResourceID string
2026-09-08 19:20:12 +08:00
UserID * string
2026-09-08 15:36:10 +08:00
}
func ( q * Queries ) HasAccess ( ctx context . Context , arg HasAccessParams ) ( bool , error ) {
2026-09-08 19:20:12 +08:00
row := q . db . QueryRow ( ctx , hasAccess , arg . ResourceType , arg . ResourceID , arg . UserID )
2026-09-08 15:36:10 +08:00
var exists bool
err := row . Scan ( & exists )
return exists , err
}
2026-11-13 13:49:21 +08:00
const hasExpertConnectorAccess = `-- name: HasExpertConnectorAccess :one
WITH RECURSIVE user_groups(group_id) AS (
SELECT g.id FROM groups g JOIN group_users gu ON gu.group_id = g.id
WHERE gu.user_id = $2::uuid AND gu.removed_at IS NULL AND g.deleted_at IS NULL
UNION
SELECT parent.id FROM groups g
JOIN user_groups ug ON ug.group_id = g.id
JOIN groups parent ON parent.id = g.parent_id
WHERE g.deleted_at IS NULL AND parent.deleted_at IS NULL
)
SELECT EXISTS (
SELECT 1 FROM experts e
JOIN expert_connectors x ON x.expert_id = e.id
JOIN connectors d ON d.id = x.connector_id
JOIN resource_access_grants rag ON rag.resource_type = 'expert' AND rag.resource_id = e.id
WHERE d.id = $1::uuid
AND e.ownership_type = 'user' AND e.enabled AND e.deleted_at IS NULL
AND d.ownership_type = 'user' AND d.owner_user_id = e.owner_user_id
AND d.enabled AND d.deleted_at IS NULL
AND (rag.user_id = $2::uuid
OR rag.group_id IN (SELECT group_id FROM user_groups)
OR (rag.all_users AND EXISTS (
SELECT 1 FROM users u
WHERE u.id = $2::uuid AND u.status = 'active' AND u.deleted_at IS NULL
)))
)
`
type HasExpertConnectorAccessParams struct {
ResourceID string
UserID string
}
func ( q * Queries ) HasExpertConnectorAccess ( ctx context . Context , arg HasExpertConnectorAccessParams ) ( bool , error ) {
row := q . db . QueryRow ( ctx , hasExpertConnectorAccess , arg . ResourceID , arg . UserID )
var exists bool
err := row . Scan ( & exists )
return exists , err
}
const hasExpertSkillAccess = `-- name: HasExpertSkillAccess :one
WITH RECURSIVE user_groups(group_id) AS (
SELECT g.id FROM groups g JOIN group_users gu ON gu.group_id = g.id
WHERE gu.user_id = $2::uuid AND gu.removed_at IS NULL AND g.deleted_at IS NULL
UNION
SELECT parent.id FROM groups g
JOIN user_groups ug ON ug.group_id = g.id
JOIN groups parent ON parent.id = g.parent_id
WHERE g.deleted_at IS NULL AND parent.deleted_at IS NULL
)
SELECT EXISTS (
SELECT 1 FROM experts e
JOIN expert_skills x ON x.expert_id = e.id
JOIN skills d ON d.id = x.skill_id
JOIN resource_access_grants rag ON rag.resource_type = 'expert' AND rag.resource_id = e.id
WHERE d.id = $1::uuid
AND e.ownership_type = 'user' AND e.enabled AND e.deleted_at IS NULL
AND d.ownership_type = 'user' AND d.owner_user_id = e.owner_user_id
AND d.enabled AND d.deleted_at IS NULL
AND (rag.user_id = $2::uuid
OR rag.group_id IN (SELECT group_id FROM user_groups)
OR (rag.all_users AND EXISTS (
SELECT 1 FROM users u
WHERE u.id = $2::uuid AND u.status = 'active' AND u.deleted_at IS NULL
)))
)
`
type HasExpertSkillAccessParams struct {
ResourceID string
UserID string
}
func ( q * Queries ) HasExpertSkillAccess ( ctx context . Context , arg HasExpertSkillAccessParams ) ( bool , error ) {
row := q . db . QueryRow ( ctx , hasExpertSkillAccess , arg . ResourceID , arg . UserID )
var exists bool
err := row . Scan ( & exists )
return exists , err
}
2026-09-08 15:36:10 +08:00
const listGrants = `-- name: ListGrants :many
SELECT
2026-11-16 11:53:46 +08:00
jsonb_build_object('id', rag.id, 'access_level', rag.access_level, 'created_at', rag.created_at, 'updated_at', rag.updated_at, 'user_id', rag.user_id, 'group_id', rag.group_id, 'all_users', rag.all_users, 'usage_requirement', rag.usage_requirement,
2026-10-08 15:17:25 +08:00
'user', CASE WHEN u.id IS NOT NULL THEN jsonb_build_object('id', u.id, 'name', u.name, 'email', u.email) END,
2026-10-15 10:54:05 +08:00
'group', CASE
WHEN rag.all_users THEN jsonb_build_object('id', '00000000-0000-0000-0000-000000000000',
2026-11-20 16:33:56 +08:00
'name', 'Monkey AI', 'parent_id', NULL)
2026-10-15 10:54:05 +08:00
WHEN g.id IS NOT NULL THEN jsonb_build_object('id', g.id, 'name', g.name, 'parent_id', COALESCE(g.parent_id::text, '00000000-0000-0000-0000-000000000000')) END)
2026-09-08 15:36:10 +08:00
FROM
2026-09-10 16:37:29 +08:00
resource_access_grants rag
LEFT JOIN users u ON u.id = rag.user_id AND u.deleted_at IS NULL
2026-10-08 15:17:25 +08:00
LEFT JOIN groups g ON g.id = rag.group_id AND g.deleted_at IS NULL
2026-09-08 15:36:10 +08:00
WHERE
2026-09-10 16:37:29 +08:00
rag.resource_type = $1
AND rag.resource_id = $2
2026-09-08 15:36:10 +08:00
ORDER BY
2026-09-10 16:37:29 +08:00
rag.group_id,
rag.user_id
2026-09-08 15:36:10 +08:00
`
type ListGrantsParams struct {
ResourceType string
ResourceID string
}
func ( q * Queries ) ListGrants ( ctx context . Context , arg ListGrantsParams ) ([][] byte , error ) {
rows , err := q . db . Query ( ctx , listGrants , arg . ResourceType , arg . ResourceID )
if err != nil {
return nil , err
}
defer rows . Close ()
items := [][] byte {}
for rows . Next () {
var jsonb_build_object [] byte
if err := rows . Scan ( & jsonb_build_object ); err != nil {
return nil , err
}
items = append ( items , jsonb_build_object )
}
if err := rows . Err (); err != nil {
return nil , err
}
return items , nil
}
const listGroups = `-- name: ListGroups :many
SELECT
jsonb_build_object('id', id, 'parent_id', parent_id, 'name', name)
FROM
GROUPS
WHERE
deleted_at IS NULL
ORDER BY
name,
id
`
func ( q * Queries ) ListGroups ( ctx context . Context ) ([][] byte , error ) {
rows , err := q . db . Query ( ctx , listGroups )
if err != nil {
return nil , err
}
defer rows . Close ()
items := [][] byte {}
for rows . Next () {
var jsonb_build_object [] byte
if err := rows . Scan ( & jsonb_build_object ); err != nil {
return nil , err
}
items = append ( items , jsonb_build_object )
}
if err := rows . Err (); err != nil {
return nil , err
}
return items , nil
}
2026-09-11 15:10:40 +08:00
const listOwners = `-- name: ListOwners :many
SELECT
id, name, email
FROM
users
WHERE
id::text = ANY ($1::text[])
`
type ListOwnersRow struct {
ID string
Name string
Email string
}
func ( q * Queries ) ListOwners ( ctx context . Context , dollar_1 [] string ) ([] ListOwnersRow , error ) {
rows , err := q . db . Query ( ctx , listOwners , dollar_1 )
if err != nil {
return nil , err
}
defer rows . Close ()
items := [] ListOwnersRow {}
for rows . Next () {
var i ListOwnersRow
if err := rows . Scan ( & i . ID , & i . Name , & i . Email ); err != nil {
return nil , err
}
items = append ( items , i )
}
if err := rows . Err (); err != nil {
return nil , err
}
return items , nil
}
2026-10-08 15:17:25 +08:00
const listSharedGroups = `-- name: ListSharedGroups :many
2026-10-15 10:54:05 +08:00
SELECT rag.resource_id,
COALESCE(g.id, '00000000-0000-0000-0000-000000000000'::uuid) AS id,
(CASE WHEN rag.all_users THEN
2026-11-20 16:33:56 +08:00
'Monkey AI'
2026-10-15 10:54:05 +08:00
ELSE g.name END)::text AS name,
COALESCE(g.parent_id, '00000000-0000-0000-0000-000000000000'::uuid) AS parent_id
2026-10-08 15:17:25 +08:00
FROM resource_access_grants rag
2026-10-15 10:54:05 +08:00
LEFT JOIN groups g ON g.id = rag.group_id AND g.deleted_at IS NULL
2026-10-08 15:17:25 +08:00
WHERE rag.resource_type = $1
AND rag.resource_id::text = ANY ($2::text[])
2026-10-15 10:54:05 +08:00
AND (rag.all_users OR g.id IS NOT NULL)
ORDER BY rag.resource_id, COALESCE(g.id, '00000000-0000-0000-0000-000000000000'::uuid)
2026-10-08 15:17:25 +08:00
`
type ListSharedGroupsParams struct {
ResourceType string
ResourceIds [] string
}
type ListSharedGroupsRow struct {
ResourceID string
ID string
Name string
ParentID * string
}
func ( q * Queries ) ListSharedGroups ( ctx context . Context , arg ListSharedGroupsParams ) ([] ListSharedGroupsRow , error ) {
rows , err := q . db . Query ( ctx , listSharedGroups , arg . ResourceType , arg . ResourceIds )
if err != nil {
return nil , err
}
defer rows . Close ()
items := [] ListSharedGroupsRow {}
for rows . Next () {
var i ListSharedGroupsRow
if err := rows . Scan (
& i . ResourceID ,
& i . ID ,
& i . Name ,
& i . ParentID ,
); err != nil {
return nil , err
}
items = append ( items , i )
}
if err := rows . Err (); err != nil {
return nil , err
}
return items , nil
}
2026-09-10 16:37:29 +08:00
const listSharedUsers = `-- name: ListSharedUsers :many
SELECT
rag.resource_id,
u.id,
u.name,
u.email
FROM
resource_access_grants rag
JOIN users u ON u.id = rag.user_id AND u.deleted_at IS NULL
WHERE
rag.resource_type = $1
AND rag.resource_id::text = ANY ($2::text[])
ORDER BY
rag.resource_id,
u.id
`
type ListSharedUsersParams struct {
ResourceType string
ResourceIds [] string
}
type ListSharedUsersRow struct {
ResourceID string
ID string
Name string
Email string
}
func ( q * Queries ) ListSharedUsers ( ctx context . Context , arg ListSharedUsersParams ) ([] ListSharedUsersRow , error ) {
rows , err := q . db . Query ( ctx , listSharedUsers , arg . ResourceType , arg . ResourceIds )
if err != nil {
return nil , err
}
defer rows . Close ()
items := [] ListSharedUsersRow {}
for rows . Next () {
var i ListSharedUsersRow
if err := rows . Scan (
& i . ResourceID ,
& i . ID ,
& i . Name ,
& i . Email ,
); err != nil {
return nil , err
}
items = append ( items , i )
}
if err := rows . Err (); err != nil {
return nil , err
}
return items , nil
}
2026-09-08 15:36:10 +08:00
const listTags = `-- name: ListTags :many
SELECT
jsonb_build_object('id', id, 'name', name)
FROM
tags
WHERE
deleted_at IS NULL
ORDER BY
lower(name),
id
`
func ( q * Queries ) ListTags ( ctx context . Context ) ([][] byte , error ) {
rows , err := q . db . Query ( ctx , listTags )
if err != nil {
return nil , err
}
defer rows . Close ()
items := [][] byte {}
for rows . Next () {
var jsonb_build_object [] byte
if err := rows . Scan ( & jsonb_build_object ); err != nil {
return nil , err
}
items = append ( items , jsonb_build_object )
}
if err := rows . Err (); err != nil {
return nil , err
}
return items , nil
}
const listUsers = `-- name: ListUsers :many
SELECT
jsonb_build_object('id', id, 'name', name, 'email', email)
FROM
users
WHERE
deleted_at IS NULL
AND status = 'active'
ORDER BY
name,
id
`
func ( q * Queries ) ListUsers ( ctx context . Context ) ([][] byte , error ) {
rows , err := q . db . Query ( ctx , listUsers )
if err != nil {
return nil , err
}
defer rows . Close ()
items := [][] byte {}
for rows . Next () {
var jsonb_build_object [] byte
if err := rows . Scan ( & jsonb_build_object ); err != nil {
return nil , err
}
items = append ( items , jsonb_build_object )
}
if err := rows . Err (); err != nil {
return nil , err
}
return items , nil
}
const lockRecipients = `-- name: LockRecipients :many
SELECT
id
FROM
users
WHERE
id::text = ANY ($1::text[])
AND deleted_at IS NULL
AND status = 'active'
ORDER BY
id FOR SHARE
`
func ( q * Queries ) LockRecipients ( ctx context . Context , dollar_1 [] string ) ([] string , error ) {
rows , err := q . db . Query ( ctx , lockRecipients , dollar_1 )
if err != nil {
return nil , err
}
defer rows . Close ()
items := [] string {}
for rows . Next () {
var id string
if err := rows . Scan ( & id ); err != nil {
return nil , err
}
items = append ( items , id )
}
if err := rows . Err (); err != nil {
return nil , err
}
return items , nil
}
2026-10-12 18:59:31 +08:00
const lockResourceImport = `-- name: LockResourceImport :one
SELECT retired_at IS NOT NULL AS retired
FROM resource_import_bindings
WHERE resource_type = $1 AND resource_id = $2
FOR UPDATE
`
type LockResourceImportParams struct {
ResourceType string
ResourceID string
}
func ( q * Queries ) LockResourceImport ( ctx context . Context , arg LockResourceImportParams ) ( interface {}, error ) {
row := q . db . QueryRow ( ctx , lockResourceImport , arg . ResourceType , arg . ResourceID )
var retired interface {}
err := row . Scan ( & retired )
return retired , err
}
2026-10-08 15:17:25 +08:00
const lockShareGroups = `-- name: LockShareGroups :many
SELECT id FROM groups
WHERE id::text = ANY ($1::text[]) AND deleted_at IS NULL
ORDER BY id FOR SHARE
`
func ( q * Queries ) LockShareGroups ( ctx context . Context , groupIds [] string ) ([] string , error ) {
rows , err := q . db . Query ( ctx , lockShareGroups , groupIds )
if err != nil {
return nil , err
}
defer rows . Close ()
items := [] string {}
for rows . Next () {
var id string
if err := rows . Scan ( & id ); err != nil {
return nil , err
}
items = append ( items , id )
}
if err := rows . Err (); err != nil {
return nil , err
}
return items , nil
}
2026-09-22 16:41:46 +08:00
const removeResourceTags = `-- name: RemoveResourceTags :exec
DELETE FROM resource_tags WHERE resource_type = $1 AND resource_id = $2
`
type RemoveResourceTagsParams struct {
ResourceType string
ResourceID string
}
func ( q * Queries ) RemoveResourceTags ( ctx context . Context , arg RemoveResourceTagsParams ) error {
_ , err := q . db . Exec ( ctx , removeResourceTags , arg . ResourceType , arg . ResourceID )
return err
}
const resourceTags = `-- name: ResourceTags :many
SELECT jsonb_build_object('id', t.id, 'name', t.name)
FROM tags t JOIN resource_tags rt ON rt.tag_id = t.id
WHERE rt.resource_type = $1 AND rt.resource_id = $2 AND t.deleted_at IS NULL
ORDER BY lower(t.name), t.id
`
type ResourceTagsParams struct {
ResourceType string
ResourceID string
}
func ( q * Queries ) ResourceTags ( ctx context . Context , arg ResourceTagsParams ) ([][] byte , error ) {
rows , err := q . db . Query ( ctx , resourceTags , arg . ResourceType , arg . ResourceID )
if err != nil {
return nil , err
}
defer rows . Close ()
items := [][] byte {}
for rows . Next () {
var jsonb_build_object [] byte
if err := rows . Scan ( & jsonb_build_object ); err != nil {
return nil , err
}
items = append ( items , jsonb_build_object )
}
if err := rows . Err (); err != nil {
return nil , err
}
return items , nil
}
2026-10-15 10:54:05 +08:00
const revokeAllShares = `-- name: RevokeAllShares :execresult
DELETE FROM resource_access_grants
WHERE resource_type = $1
AND resource_id = $2
AND all_users
`
type RevokeAllSharesParams struct {
ResourceType string
ResourceID string
}
func ( q * Queries ) RevokeAllShares ( ctx context . Context , arg RevokeAllSharesParams ) ( pgconn . CommandTag , error ) {
return q . db . Exec ( ctx , revokeAllShares , arg . ResourceType , arg . ResourceID )
}
2026-10-08 15:17:25 +08:00
const revokeGroupShares = `-- name: RevokeGroupShares :execresult
DELETE FROM resource_access_grants
WHERE resource_type = $1
AND resource_id = $2
AND group_id::text = ANY ($3::text[])
`
type RevokeGroupSharesParams struct {
ResourceType string
ResourceID string
GroupIds [] string
}
func ( q * Queries ) RevokeGroupShares ( ctx context . Context , arg RevokeGroupSharesParams ) ( pgconn . CommandTag , error ) {
return q . db . Exec ( ctx , revokeGroupShares , arg . ResourceType , arg . ResourceID , arg . GroupIds )
}
2026-09-08 15:36:10 +08:00
const revokeShares = `-- name: RevokeShares :execresult
DELETE FROM resource_access_grants
WHERE resource_type = $1
AND resource_id = $2
AND user_id::text = ANY ($3::text[])
`
type RevokeSharesParams struct {
ResourceType string
ResourceID string
UserIds [] string
}
func ( q * Queries ) RevokeShares ( ctx context . Context , arg RevokeSharesParams ) ( pgconn . CommandTag , error ) {
return q . db . Exec ( ctx , revokeShares , arg . ResourceType , arg . ResourceID , arg . UserIds )
}
const subjectExists = `-- name: SubjectExists :one
SELECT
EXISTS (
SELECT
1
FROM
users
WHERE
id = NULLIF ($1::text, '')::uuid
AND deleted_at IS NULL
AND status = 'active'
UNION ALL
SELECT
1
FROM
GROUPS
WHERE
id = NULLIF ($2::text, '')::uuid
AND deleted_at IS NULL)
`
type SubjectExistsParams struct {
UserID string
GroupID string
}
func ( q * Queries ) SubjectExists ( ctx context . Context , arg SubjectExistsParams ) ( bool , error ) {
row := q . db . QueryRow ( ctx , subjectExists , arg . UserID , arg . GroupID )
var exists bool
err := row . Scan ( & exists )
return exists , err
}
2026-11-26 22:37:49 +08:00
const unshareableExpertKnowledge = `-- name: UnshareableExpertKnowledge :many
SELECT DISTINCT jsonb_build_object('id', k.id, 'name', k.name, 'enabled', k.enabled,
'ownership_type', k.ownership_type, 'owner_user_id', k.owner_user_id) AS kb
FROM expert_knowledge_bases x JOIN knowledge_bases k ON k.id = x.kb_id
JOIN experts e ON e.id = x.expert_id
WHERE x.expert_id::text = ANY($1::text[])
AND NOT (k.ownership_type = 'user' AND k.owner_user_id = e.owner_user_id)
ORDER BY kb
`
func ( q * Queries ) UnshareableExpertKnowledge ( ctx context . Context , expertIds [] string ) ([][] byte , error ) {
rows , err := q . db . Query ( ctx , unshareableExpertKnowledge , expertIds )
if err != nil {
return nil , err
}
defer rows . Close ()
items := [][] byte {}
for rows . Next () {
var kb [] byte
if err := rows . Scan ( & kb ); err != nil {
return nil , err
}
items = append ( items , kb )
}
if err := rows . Err (); err != nil {
return nil , err
}
return items , nil
}
2026-11-16 11:53:46 +08:00
const updateKbGrant = `-- name: UpdateKbGrant :exec
UPDATE resource_access_grants SET access_level = $2, updated_at = now()
WHERE id = $1 AND resource_type = 'kb' AND access_level IS DISTINCT FROM $2
`
type UpdateKbGrantParams struct {
ID string
AccessLevel string
}
func ( q * Queries ) UpdateKbGrant ( ctx context . Context , arg UpdateKbGrantParams ) error {
_ , err := q . db . Exec ( ctx , updateKbGrant , arg . ID , arg . AccessLevel )
return err
}
2026-09-08 15:36:10 +08:00
const updateTag = `-- name: UpdateTag :one
UPDATE
tags
SET
name = $2,
updated_at = now()
WHERE
id = $1
AND deleted_at IS NULL
RETURNING
jsonb_build_object('id', id, 'name', name)
`
type UpdateTagParams struct {
ID string
Name string
}
func ( q * Queries ) UpdateTag ( ctx context . Context , arg UpdateTagParams ) ([] byte , error ) {
row := q . db . QueryRow ( ctx , updateTag , arg . ID , arg . Name )
var jsonb_build_object [] byte
err := row . Scan ( & jsonb_build_object )
return jsonb_build_object , err
}