初版功能完成
ci / Go checks (ubuntu-latest) (push) Has been cancelled
ci / Go checks (windows-latest) (push) Has been cancelled

This commit is contained in:
qsc
2026-08-29 13:12:17 +08:00
commit 142e5dc7d6
217 changed files with 21313 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
package database
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
)
// GetSetting returns a setting and whether it exists.
func GetSetting(ctx context.Context, db *sql.DB, key string) (string, bool, error) {
var value string
err := db.QueryRowContext(ctx, `SELECT value FROM settings WHERE key = ?`, key).Scan(&value)
if errors.Is(err, sql.ErrNoRows) {
return "", false, nil
}
if err != nil {
return "", false, fmt.Errorf("get setting %q: %w", key, err)
}
return value, true, nil
}
// SetSetting upserts a setting with an RFC3339 UTC timestamp.
func SetSetting(ctx context.Context, db *sql.DB, key, value string) error {
_, err := db.ExecContext(ctx, `
INSERT INTO settings(key, value, updated_at) VALUES(?, ?, ?)
ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at
`, key, value, time.Now().UTC().Format(time.RFC3339Nano))
if err != nil {
return fmt.Errorf("set setting %q: %w", key, err)
}
return nil
}