Added concurrent sync, added search page. Other small changes

This commit is contained in:
2024-10-04 20:19:40 +02:00
parent fafa044c9b
commit 8fa93d580d
35 changed files with 1901 additions and 89 deletions

View File

@@ -3,11 +3,13 @@ package db
import (
"context"
"fmt"
"github.com/jackc/pgx/v5"
"os"
"github.com/jackc/pgx/v5/pgxpool"
)
var pool *pgx.Conn
var dbpool *pgxpool.Pool
var ctx = context.Background()
func InitDB(host string, port int, user string, password string, dbname string) {
@@ -18,14 +20,14 @@ func InitDB(host string, port int, user string, password string, dbname string)
fmt.Println(psqlInfo)
var err error
pool, err = pgx.Connect(context.Background(), psqlInfo)
dbpool, err = pgxpool.New(ctx, psqlInfo)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
var success string
err = pool.QueryRow(context.Background(), "select 'Successfully connected!'").Scan(&success)
err = dbpool.QueryRow(ctx, "select 'Successfully connected!'").Scan(&success)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
@@ -35,14 +37,11 @@ func InitDB(host string, port int, user string, password string, dbname string)
}
func CloseDb() {
err := pool.Close(context.Background())
if err != nil {
return
}
dbpool.Close()
}
func Testf() {
rows, dbErr := pool.Query(context.Background(), "select game_name from game")
rows, dbErr := dbpool.Query(ctx, "select game_name from game")
if dbErr != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", dbErr)
os.Exit(1)
@@ -58,7 +57,7 @@ func Testf() {
}
func resetGameIdSeq() {
_, err := pool.Query(context.Background(), "SELECT setval('game_id_seq', (SELECT MAX(id) FROM game)+1);")
_, err := dbpool.Query(ctx, "SELECT setval('game_id_seq', (SELECT MAX(id) FROM game)+1);")
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
}