bloaterbot/inline/nenefoot/sql.go
watn3y 6375fe5af1 A bunch of Changes
Docker is broken as of now, will fix later
2023-12-29 05:15:23 +01:00

47 lines
1.2 KiB
Go

package nenefoot
import (
"database/sql"
_ "modernc.org/sqlite"
"github.com/rs/zerolog/log"
)
var gpSelectUser *sql.Stmt
var gpSelectChat *sql.Stmt
var gpSet *sql.Stmt
func InitDB() {
const dbPath string = "data/db/nenefoot.db"
log.Info().Str("dbpath", dbPath).Msg("init database")
db, err := sql.Open("sqlite", dbPath)
if err != nil {
log.Panic().Err(err).Msg("failed to open sqlite database")
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS nenefoot (title string, type string, ID string, URL string, UNIQUE(title), UNIQUE(ID), UNIQUE(URL))")
if err != nil {
log.Panic().Err(err).Msg("failed to create table")
}
gpSelectUser, err = db.Prepare("SELECT gaypoints FROM gaypoints WHERE chatid = ? AND userid = ?")
if err != nil {
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.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.Panic().Err(err).Msg("failed to create sql statement")
}
log.Info().Msg("init database: done")
}