Go · 795 bytes Raw Blame History
1 // SPDX-License-Identifier: AGPL-3.0-or-later
2
3 package search
4
5 import "fmt"
6
7 func repoOwnerJoin(repoAlias, userAlias, orgAlias string) string {
8 return fmt.Sprintf(
9 "LEFT JOIN users %s ON %s.id = %s.owner_user_id LEFT JOIN orgs %s ON %s.id = %s.owner_org_id",
10 userAlias, userAlias, repoAlias, orgAlias, orgAlias, repoAlias,
11 )
12 }
13
14 func repoOwnerNameExpr(userAlias, orgAlias string) string {
15 return fmt.Sprintf("coalesce(%s.username, %s.slug)", userAlias, orgAlias)
16 }
17
18 func repoFilterByOwnerName(repoAlias string, ownerPos, namePos int) string {
19 return fmt.Sprintf(
20 " AND %s.id = (SELECT r2.id FROM repos r2 %s "+
21 "WHERE %s = $%d AND r2.name = $%d AND r2.deleted_at IS NULL)",
22 repoAlias,
23 repoOwnerJoin("r2", "u2", "o2"),
24 repoOwnerNameExpr("u2", "o2"),
25 ownerPos,
26 namePos,
27 )
28 }
29