From a85d1d8d6b25e13500b0895209b6343f0b2bc435 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Mon, 18 Aug 2025 01:18:48 +0800 Subject: [PATCH] Add sqlc --- Makefile | 3 ++- forged/internal/database/queries/.gitignore | 1 + sql/queries/groups.sql | 27 +++++++++++++++++++++++++++ sqlc.yaml | 15 +++++++++++++++ diff --git a/Makefile b/Makefile index c4fd59a41b43a5ca99ef0ee25b70a084ec8d72b0..303467609200eec867c84df1f73bf6ceec92f2c6 100644 --- a/Makefile +++ b/Makefile @@ -13,8 +13,9 @@ CFLAGS = -Wall -Wextra -pedantic -std=c99 -D_GNU_SOURCE all: dist/forged dist/git2d dist/hookc -dist/forged: $(shell git ls-files forged) +dist/forged: $(shell git ls-files forged) $(shell git ls-files sql) mkdir -p dist + sqlc generate CGO_ENABLED=0 go build -o dist/forged -ldflags '-extldflags "-f no-PIC -static"' -tags 'osusergo netgo static_build' ./forged dist/git2d: $(wildcard git2d/*.c) diff --git a/forged/internal/database/queries/.gitignore b/forged/internal/database/queries/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..1307f6de2e662cb8514948d534689a6a3915b0bf --- /dev/null +++ b/forged/internal/database/queries/.gitignore @@ -0,0 +1 @@ +/*.go diff --git a/sql/queries/groups.sql b/sql/queries/groups.sql new file mode 100644 index 0000000000000000000000000000000000000000..07fe5e7fdddf5c7e1319ae59f334a639a4b2855a --- /dev/null +++ b/sql/queries/groups.sql @@ -0,0 +1,27 @@ +-- name: GetGroupIDDescByPath :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 c.id, COALESCE(g.description, '') +FROM group_path_cte c +JOIN groups g ON g.id = c.id +WHERE c.depth = cardinality($1::text[]); diff --git a/sqlc.yaml b/sqlc.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ed5c158a6eb7e45aa57bb9528c649af829f4c63a --- /dev/null +++ b/sqlc.yaml @@ -0,0 +1,15 @@ +version: "2" +sql: + - engine: "postgresql" + schema: "sql/schema.sql" + queries: "sql/queries" + gen: + go: + package: "queries" + out: "forged/internal/database/queries" + sql_package: "pgx/v5" + emit_json_tags: true + emit_db_tags: true + emit_prepared_queries: true + emit_pointers_for_null_types: true + emit_enum_valid_method: true -- 2.48.1