many update
This commit is contained in:
84
lib/db.go
84
lib/db.go
@ -16,6 +16,15 @@ import (
|
||||
var DBClient *sql.DB
|
||||
var DBConnector *libsql.Connector
|
||||
|
||||
// Prepared statements
|
||||
var (
|
||||
stmtGetBalance *sql.Stmt
|
||||
stmtUpdateBalance *sql.Stmt
|
||||
stmtGetLeaderboard *sql.Stmt
|
||||
stmtGetUserProfile *sql.Stmt
|
||||
stmtUpdateProfile *sql.Stmt
|
||||
)
|
||||
|
||||
func InitDB() error {
|
||||
// Determine DB path based on /data directory existence
|
||||
var dbPath string
|
||||
@ -31,9 +40,24 @@ func InitDB() error {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Configure connection pool using config values
|
||||
db.SetMaxOpenConns(AppConfig.MaxOpenConns)
|
||||
db.SetMaxIdleConns(AppConfig.MaxIdleConns)
|
||||
db.SetConnMaxLifetime(AppConfig.ConnMaxLifetime)
|
||||
|
||||
// Test the connection
|
||||
if err := db.Ping(); err != nil {
|
||||
return fmt.Errorf("failed to ping database: %w", err)
|
||||
}
|
||||
|
||||
DBClient = db
|
||||
|
||||
return runMigrations()
|
||||
if err := runMigrations(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Prepare frequently used statements
|
||||
return prepareStatements()
|
||||
}
|
||||
|
||||
type Migration struct {
|
||||
@ -140,3 +164,61 @@ func runMigrations() error {
|
||||
log.Println("Database migrations completed successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
func prepareStatements() error {
|
||||
var err error
|
||||
|
||||
// Prepare balance query
|
||||
stmtGetBalance, err = DBClient.Prepare(`
|
||||
SELECT gp.currency_balance
|
||||
FROM guild_profiles gp
|
||||
JOIN users u ON gp.user_id = u.id
|
||||
WHERE u.discord_id = ? AND gp.guild_id = ?`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to prepare balance query: %w", err)
|
||||
}
|
||||
|
||||
// Prepare leaderboard query
|
||||
stmtGetLeaderboard, err = DBClient.Prepare(`
|
||||
SELECT u.username, gp.currency_balance, gp.message_count
|
||||
FROM guild_profiles gp
|
||||
JOIN users u ON gp.user_id = u.id
|
||||
WHERE gp.guild_id = ?
|
||||
ORDER BY gp.currency_balance DESC
|
||||
LIMIT ?`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to prepare leaderboard query: %w", err)
|
||||
}
|
||||
|
||||
// Prepare user profile query
|
||||
stmtGetUserProfile, err = DBClient.Prepare(`
|
||||
SELECT message_count, last_reward_at
|
||||
FROM guild_profiles
|
||||
WHERE user_id = ? AND guild_id = ?`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to prepare user profile query: %w", err)
|
||||
}
|
||||
|
||||
log.Println("Prepared statements initialized successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
// CleanupDB closes all prepared statements
|
||||
func CleanupDB() {
|
||||
if stmtGetBalance != nil {
|
||||
stmtGetBalance.Close()
|
||||
}
|
||||
if stmtUpdateBalance != nil {
|
||||
stmtUpdateBalance.Close()
|
||||
}
|
||||
if stmtGetLeaderboard != nil {
|
||||
stmtGetLeaderboard.Close()
|
||||
}
|
||||
if stmtGetUserProfile != nil {
|
||||
stmtGetUserProfile.Close()
|
||||
}
|
||||
if stmtUpdateProfile != nil {
|
||||
stmtUpdateProfile.Close()
|
||||
}
|
||||
log.Println("Database cleanup completed")
|
||||
}
|
||||
|
Reference in New Issue
Block a user