added proper logging to most of the code

This commit is contained in:
watn3y 2023-09-07 03:24:31 +02:00
parent 65c6020e49
commit 7d4c9c5ab9
21 changed files with 218 additions and 283 deletions

View file

@ -3,8 +3,7 @@ package gaypoints
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"log"
"strconv"
"github.com/rs/zerolog/log"
)
var gpSelectUser *sql.Stmt
@ -16,7 +15,8 @@ var gpSet *sql.Stmt
func sqlGetAllGP(chatid int64) (gaypoints []gaypointShortDetails) {
rows, err := gpSelectChat.Query(chatid)
if err != nil {
log.Panicf("Failed to query gaypoints: %v\n", err)
log.Error().Err(err).Msg("failed to execute SQL to get get gaypoints for chat")
return
}
var c int64
@ -29,10 +29,11 @@ func sqlGetAllGP(chatid int64) (gaypoints []gaypointShortDetails) {
gaypoints: b,
})
default:
log.Panicf("Failed to query gaypoints: %v\n", err)
log.Error().Err(err).Msg("failed to parse SQL to get get gaypoints for chat")
return
}
log.Println("[gaypoints.sql] Got gaypoints for " + strconv.FormatInt(c, 10) + " in chat " + strconv.FormatInt(chatid, 10) + ". Value is " + strconv.FormatInt(b, 10))
log.Debug().Msg("executed SQL to get gaypoints for chat")
}
return gaypoints
@ -48,50 +49,53 @@ func sqlGetGP(chatid int64, userid int64) (gaypoints int64) {
case nil:
break
default:
log.Panicf("Failed to query gaypoints: %v\n", err)
log.Error().Err(err).Msg("failed to execute SQL to get get gaypoints for user")
return
}
log.Println("[gaypoints.sql] Got gaypoints for " + strconv.FormatInt(userid, 10) + " in chat " + strconv.FormatInt(chatid, 10) + ". Value is " + strconv.FormatInt(gaypoints, 10))
log.Debug().Msg("executed SQL to get gaypoints for user")
return gaypoints
}
func sqlSetGP(chatid int64, userid int64, gaypoints int64) {
_, err := gpSet.Exec(chatid, userid, gaypoints)
if err != nil {
log.Panicf("Failed to set gaypoints: %v\n", err)
log.Error().Err(err).Msg("failed to execute SQL to get set gaypoints for user")
return
}
log.Println("[gaypoints.sql] Set gaypoints for " + strconv.FormatInt(userid, 10) + " in chat " + strconv.FormatInt(chatid, 10) + " to " + strconv.FormatInt(gaypoints, 10))
log.Debug().Msg("executed SQL to set gaypoints for user")
}
func InitDB() {
const dbPath string = "./bloater.db"
log.Info().Str("dbpath", dbPath).Msg("init database")
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
log.Panicf("Failed to open sqlite database: %v\n", err)
log.Panic().Err(err).Msg("failed to open sqlite database")
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS gaypoints (chatid bigint, userid bigint,gaypoints bigint,UNIQUE(chatid,userid))")
if err != nil {
log.Panicf("Failed to create table: %v\n", err)
log.Panic().Err(err).Msg("failed to create table")
}
log.Println("[gaypoints.sql] Created sqlite table in database at " + dbPath)
gpSelectUser, err = db.Prepare("SELECT gaypoints FROM gaypoints WHERE chatid = ? AND userid = ?")
if err != nil {
log.Panicf("Failed to prepare sql select: %v\n", err)
log.Panic().Err(err).Msg("failed to create sql statement")
}
gpSelectChat, err = db.Prepare("SELECT userid,gaypoints FROM gaypoints WHERE chatid = ? ORDER BY gaypoints DESC")
if err != nil {
log.Panicf("Failed to prepare sql select: %v\n", err)
log.Panic().Err(err).Msg("failed to create sql statement")
}
gpSet, err = db.Prepare("INSERT OR REPLACE INTO gaypoints (chatid, userid, gaypoints) values (?,?,?)")
if err != nil {
log.Panicf("Failed to prepare sql insert: %v\n", err)
log.Panic().Err(err).Msg("failed to create sql statement")
}
log.Info().Msg("init database: done")
}