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)
}

View File

@@ -1,7 +1,6 @@
package db
import (
"context"
"errors"
"fmt"
"music-server/pkg/models"
@@ -13,10 +12,12 @@ import (
func GetGameName(gameId int) string {
var gameName = ""
err := pool.QueryRow(context.Background(),
err := dbpool.QueryRow(ctx,
"SELECT game_name FROM game WHERE id = $1", gameId).Scan(&gameName)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
if compareError.Error() != err.Error() {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
return ""
}
return gameName
@@ -27,11 +28,13 @@ func GetGameById(gameId int) (models.GameData, error) {
var numberOfSongs pgtype.Int4
var gameName, path string
var added, deleted, lastChanged, lastPlayed pgtype.Timestamp
err := pool.QueryRow(context.Background(),
err := dbpool.QueryRow(ctx,
"SELECT id, game_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs "+
"FROM game WHERE id = $1 AND deleted IS NULL ", gameId).Scan(&id, &gameName, &added, &deleted, &lastChanged, &path, &timesPlayed, &lastPlayed, &numberOfSongs)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
if compareError.Error() != err.Error() {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
return models.GameData{}, errors.New("Game not found")
}
return models.GameData{
@@ -48,7 +51,7 @@ func GetGameById(gameId int) (models.GameData, error) {
}
func SetGameDeletionDate() {
_, err := pool.Exec(context.Background(),
_, err := dbpool.Exec(ctx,
"UPDATE game SET deleted=$1 WHERE deleted IS NULL", time.Now())
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
@@ -56,14 +59,14 @@ func SetGameDeletionDate() {
}
func ClearGames() {
_, err := pool.Exec(context.Background(), "DELETE FROM game")
_, err := dbpool.Exec(ctx, "DELETE FROM game")
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
}
}
func UpdateGameName(id int, name string, path string) {
_, err := pool.Exec(context.Background(),
_, err := dbpool.Exec(ctx,
"UPDATE game SET game_name=$1, path=$2, last_changed=$3 WHERE id=$4",
name, path, time.Now(), id)
if err != nil {
@@ -72,7 +75,7 @@ func UpdateGameName(id int, name string, path string) {
}
func RemoveDeletionDate(id int) {
_, err := pool.Exec(context.Background(),
_, err := dbpool.Exec(ctx,
"UPDATE game SET deleted=null WHERE id=$1", id)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
@@ -81,10 +84,12 @@ func RemoveDeletionDate(id int) {
func GetIdByGameName(name string) int {
var gameId = -1
err := pool.QueryRow(context.Background(),
err := dbpool.QueryRow(ctx,
"SELECT id FROM game WHERE game_name = $1", name).Scan(&gameId)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
if compareError.Error() != err.Error() {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
return -1
}
return gameId
@@ -92,17 +97,21 @@ func GetIdByGameName(name string) int {
func InsertGame(name string, path string) int {
gameId := -1
err := pool.QueryRow(context.Background(),
err := dbpool.QueryRow(ctx,
"INSERT INTO game(game_name, path, added) VALUES ($1, $2, $3) RETURNING id",
name, path, time.Now()).Scan(&gameId)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
if compareError.Error() != err.Error() {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
resetGameIdSeq()
err2 := pool.QueryRow(context.Background(),
err2 := dbpool.QueryRow(ctx,
"INSERT INTO game(game_name, path, added) VALUES ($1, $2, $3) RETURNING id",
name, path, time.Now()).Scan(&gameId)
if err2 != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
if compareError.Error() != err2.Error() {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
return -1
}
}
@@ -110,7 +119,7 @@ func InsertGame(name string, path string) int {
}
func InsertGameWithExistingId(id int, name string, path string) {
_, err := pool.Exec(context.Background(),
_, err := dbpool.Exec(ctx,
"INSERT INTO game(id, game_name, path, added) VALUES ($1, $2, $3, $4)",
id, name, path, time.Now())
if err != nil {
@@ -119,12 +128,14 @@ func InsertGameWithExistingId(id int, name string, path string) {
}
func FindAllGames() []models.GameData {
rows, err := pool.Query(context.Background(),
rows, err := dbpool.Query(ctx,
"SELECT id, game_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs "+
"FROM game WHERE deleted IS NULL "+
"ORDER BY game_name")
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
if compareError.Error() != err.Error() {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
}
var gameList []models.GameData
for rows.Next() {
@@ -134,7 +145,9 @@ func FindAllGames() []models.GameData {
var added, deleted, lastChanged, lastPlayed pgtype.Timestamp
err := rows.Scan(&id, &gameName, &added, &deleted, &lastChanged, &path, &timesPlayed, &lastPlayed, &numberOfSongs)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
if compareError.Error() != err.Error() {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
}
gameList = append(gameList, models.GameData{
Id: id,
@@ -152,7 +165,7 @@ func FindAllGames() []models.GameData {
}
func AddGamePlayed(id int) {
_, err := pool.Exec(context.Background(),
_, err := dbpool.Exec(ctx,
"UPDATE game SET times_played=times_played+1, last_played=now() WHERE id=$1", id)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)

View File

@@ -1,21 +1,23 @@
package db
import (
"context"
"errors"
"fmt"
"music-server/pkg/models"
"os"
"strings"
)
var compareError = errors.New("no rows in result set")
func ClearSongs(gameId int) {
if gameId == -1 {
_, err := pool.Exec(context.Background(), "DELETE FROM song")
_, err := dbpool.Exec(ctx, "DELETE FROM song")
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
}
} else {
_, err := pool.Exec(context.Background(), "DELETE FROM song where game_id=$1", gameId)
_, err := dbpool.Exec(ctx, "DELETE FROM song where game_id=$1", gameId)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
}
@@ -23,7 +25,7 @@ func ClearSongs(gameId int) {
}
func AddSong(song models.SongData) {
_, err := pool.Exec(context.Background(),
_, err := dbpool.Exec(ctx,
"INSERT INTO song(game_id, song_name, path, file_name) VALUES ($1, $2, $3, $4)",
song.GameId, song.SongName, song.Path, song.FileName)
if err != nil {
@@ -33,17 +35,18 @@ func AddSong(song models.SongData) {
func CheckSong(songPath string) bool {
var path string
err := pool.QueryRow(context.Background(),
err := dbpool.QueryRow(ctx,
"SELECT path FROM song WHERE path = $1", songPath).Scan(&path)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
if compareError.Error() != err.Error() {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
}
println("CheckSong path", path)
return path != ""
}
func UpdateSong(songName string, fileName string, path string) {
_, err := pool.Exec(context.Background(),
_, err := dbpool.Exec(ctx,
"UPDATE song SET song_name=$1, file_name=$2 WHERE path = $3",
songName, fileName, path)
if err != nil {
@@ -52,10 +55,12 @@ func UpdateSong(songName string, fileName string, path string) {
}
func FindSongsFromGame(id int) []models.SongData {
rows, err := pool.Query(context.Background(),
rows, err := dbpool.Query(ctx,
"SELECT song_name, path, file_name, times_played FROM song WHERE game_id = $1", id)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
if compareError.Error() != err.Error() {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
return nil
}
@@ -68,7 +73,9 @@ func FindSongsFromGame(id int) []models.SongData {
err := rows.Scan(&songName, &path, &fileName, &timesPlayed)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
if compareError.Error() != err.Error() {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
}
songDataList = append(songDataList, models.SongData{
@@ -83,7 +90,7 @@ func FindSongsFromGame(id int) []models.SongData {
}
func AddSongPlayed(id int, name string) {
_, err := pool.Exec(context.Background(),
_, err := dbpool.Exec(ctx,
"UPDATE song SET times_played=times_played+1 WHERE game_id=$1 AND song_name=$2", id, name)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
@@ -91,10 +98,12 @@ func AddSongPlayed(id int, name string) {
}
func FetchAllSongs() []models.SongData {
rows, err := pool.Query(context.Background(),
rows, err := dbpool.Query(ctx,
"SELECT song_name, path FROM song")
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
if compareError.Error() != err.Error() {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
return nil
}
@@ -105,7 +114,9 @@ func FetchAllSongs() []models.SongData {
err := rows.Scan(&songName, &path)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
if compareError.Error() != err.Error() {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
}
songDataList = append(songDataList, models.SongData{
@@ -117,7 +128,7 @@ func FetchAllSongs() []models.SongData {
}
func RemoveBrokenSong(song models.SongData) {
_, err := pool.Exec(context.Background(), "DELETE FROM song where path=$1", song.Path)
_, err := dbpool.Exec(ctx, "DELETE FROM song where path=$1", song.Path)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
}
@@ -130,7 +141,7 @@ func RemoveBrokenSongs(songs []models.SongData) {
}
joined = strings.TrimSuffix(joined, ",")
_, err := pool.Exec(context.Background(), "DELETE FROM song where path in ($1)", joined)
_, err := dbpool.Exec(ctx, "DELETE FROM song where path in ($1)", joined)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
}

View File

@@ -1,7 +1,6 @@
package db
import (
"context"
"fmt"
"music-server/pkg/models"
"os"
@@ -9,7 +8,7 @@ import (
)
func InsertSongInList(song models.SongListData) {
err := pool.QueryRow(context.Background(),
_, err := dbpool.Exec(ctx,
`INSERT INTO song_list (match_date, match_id, song_no, game_name, song_name) VALUES ($1, $2, $3, $4, $5)`,
song.MatchDate, song.MatchId, song.SongNo, song.GameName, song.SongName)
if err != nil {
@@ -18,12 +17,14 @@ func InsertSongInList(song models.SongListData) {
}
func GetSongList(matchId int) []models.SongListData {
rows, err := pool.Query(context.Background(),
rows, err := dbpool.Query(ctx,
"SELECT match_date, match_id, song_no, game_name, song_name "+
"FROM song_list WHERE match_date = $1"+
"ORDER BY song_no DESC", matchId)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
if compareError.Error() != err.Error() {
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
}
}
var songList []models.SongListData
for rows.Next() {