diff options
author | Runxi Yu <me@runxiyu.org> | 2025-08-12 11:01:07 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-09-13 19:08:22 +0800 |
commit | 5717faed659a9eeb86c528ab56822c42eca1ad3f (patch) | |
tree | 92e6662628a51c03c52300d2fd98173716a82882 /forged/sql/queries | |
parent | Remove forge-specific functions from misc (diff) | |
download | forge-5717faed659a9eeb86c528ab56822c42eca1ad3f.tar.gz forge-5717faed659a9eeb86c528ab56822c42eca1ad3f.tar.zst forge-5717faed659a9eeb86c528ab56822c42eca1ad3f.zip |
Refactor
Diffstat (limited to 'forged/sql/queries')
-rw-r--r-- | forged/sql/queries/groups.sql | 47 | ||||
-rw-r--r-- | forged/sql/queries/login.sql | 8 |
2 files changed, 55 insertions, 0 deletions
diff --git a/forged/sql/queries/groups.sql b/forged/sql/queries/groups.sql new file mode 100644 index 0000000..f067aeb --- /dev/null +++ b/forged/sql/queries/groups.sql @@ -0,0 +1,47 @@ +-- name: GetRootGroups :many +SELECT name, COALESCE(description, '') FROM groups WHERE parent_group IS NULL; + +-- name: GetGroupByPath :one +WITH RECURSIVE group_path_cte AS ( + SELECT + id, + parent_group, + name, + 1 AS depth + FROM groups + WHERE name = ($1::text[])[1] + AND parent_group IS NULL + + UNION ALL + + SELECT + g.id, + g.parent_group, + g.name, + group_path_cte.depth + 1 + FROM groups g + JOIN group_path_cte ON g.parent_group = group_path_cte.id + WHERE g.name = ($1::text[])[group_path_cte.depth + 1] + AND group_path_cte.depth + 1 <= cardinality($1::text[]) +) +SELECT + g.id, + g.name, + g.parent_group, + COALESCE(g.description, '') AS description, + EXISTS ( + SELECT 1 + FROM user_group_roles ugr + WHERE ugr.user_id = $2 + AND ugr.group_id = g.id + ) AS has_role +FROM group_path_cte c +JOIN groups g ON g.id = c.id +WHERE c.depth = cardinality($1::text[]); + + +-- name: GetReposInGroup :many +SELECT name, COALESCE(description, '') FROM repos WHERE group_id = $1; + +-- name: GetSubgroups :many +SELECT name, COALESCE(description, '') FROM groups WHERE parent_group = $1; diff --git a/forged/sql/queries/login.sql b/forged/sql/queries/login.sql new file mode 100644 index 0000000..ffc4026 --- /dev/null +++ b/forged/sql/queries/login.sql @@ -0,0 +1,8 @@ +-- name: GetUserCreds :one +SELECT id, COALESCE(password_hash, '') FROM users WHERE username = $1; + +-- name: InsertSession :exec +INSERT INTO sessions (user_id, token_hash, expires_at) VALUES ($1, $2, $3); + +-- name: GetUserFromSession :one +SELECT user_id, COALESCE(username, '') FROM users u JOIN sessions s ON u.id = s.user_id WHERE s.token_hash = $1; |