internal/store/scope.go

784b5dfad3f6ed718ada2a43910225c220abb310
gitbay/internal/store/scope.go history · blame · raw

33 lines · 996 bytes

 1package store
 2
 3import (
 4	"errors"
 5	"strings"
 6)
 7
 8// ErrOrgScoped is returned when a repository-level write names a label or
 9// milestone its org holds; the org commands manage those.
10var ErrOrgScoped = errors.New("held by the org")
11
12// scopeClause selects the label or milestone rows a repository sees: its
13// own, and its org's when an org owns it. alias is the table alias in the
14// query.
15func scopeClause(alias string, repo Repo) (string, []any) {
16	if repo.OwnerKind == "org" {
17		return "(" + alias + ".repo_id = ? OR " + alias + ".org_id = ?)", []any{repo.ID, repo.OwnerID}
18	}
19	return alias + ".repo_id = ?", []any{repo.ID}
20}
21
22// inClause renders ids as a parenthesised placeholder list. An empty set
23// yields (NULL), which matches nothing.
24func inClause(ids []int64) (string, []any) {
25	if len(ids) == 0 {
26		return "(NULL)", nil
27	}
28	args := make([]any, len(ids))
29	for i, id := range ids {
30		args[i] = id
31	}
32	return "(" + strings.TrimSuffix(strings.Repeat("?,", len(ids)), ",") + ")", args
33}