aboutsummaryrefslogtreecommitdiff
path: root/forged/internal/unsorted/database.go
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-04-06 09:26:46 +0800
committerRunxi Yu <me@runxiyu.org>2025-04-06 09:27:53 +0800
commitda1d8f4e7c332c7109427915e6459b10209cedce (patch)
tree280b921be3b51f93d82d916b4eaa89387b7102cc /forged/internal/unsorted/database.go
parentgit2c, git2d: Rename cmd1 and cmd2 descriptively (diff)
downloadforge-0.1.32.tar.gz
forge-0.1.32.tar.zst
forge-0.1.32.zip
Move the Go stuff to ./forged/v0.1.32
Diffstat (limited to 'forged/internal/unsorted/database.go')
-rw-r--r--forged/internal/unsorted/database.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/forged/internal/unsorted/database.go b/forged/internal/unsorted/database.go
new file mode 100644
index 0000000..222b0c4
--- /dev/null
+++ b/forged/internal/unsorted/database.go
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
+
+package unsorted
+
+import (
+ "context"
+
+ "github.com/jackc/pgx/v5"
+)
+
+// TODO: All database handling logic in all request handlers must be revamped.
+// We must ensure that each request has all logic in one transaction (subject
+// to exceptions if appropriate) so they get a consistent view of the database
+// at a single point. A failure to do so may cause things as serious as
+// privilege escalation.
+
+// queryNameDesc is a helper function that executes a query and returns a
+// list of nameDesc results. The query must return two string arguments, i.e. a
+// name and a description.
+func (s *Server) queryNameDesc(ctx context.Context, query string, args ...any) (result []nameDesc, err error) {
+ var rows pgx.Rows
+
+ if rows, err = s.database.Query(ctx, query, args...); err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+
+ for rows.Next() {
+ var name, description string
+ if err = rows.Scan(&name, &description); err != nil {
+ return nil, err
+ }
+ result = append(result, nameDesc{name, description})
+ }
+ return result, rows.Err()
+}
+
+// nameDesc holds a name and a description.
+type nameDesc struct {
+ Name string
+ Description string
+}