47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package nenefoot
|
|
|
|
import (
|
|
"database/sql"
|
|
"github.com/rs/zerolog/log"
|
|
_ "modernc.org/sqlite"
|
|
)
|
|
|
|
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")
|
|
}
|