package db import ( "database/sql" "fmt" mcdsldb "git.wntrmute.dev/kyle/mcdsl/db" ) // Store wraps a SQLite database connection for mc-proxy persistence. type Store struct { db *sql.DB } // Open opens (or creates) the SQLite database at path with WAL mode, // foreign keys, and a busy timeout. The file is created with 0600 // permissions. func Open(path string) (*Store, error) { database, err := mcdsldb.Open(path) if err != nil { return nil, fmt.Errorf("opening database: %w", err) } return &Store{db: database}, nil } // Close closes the database connection. func (s *Store) Close() error { return s.db.Close() } // DB returns the underlying *sql.DB for use with mcdsl functions. func (s *Store) DB() *sql.DB { return s.db } // IsEmpty returns true if the listeners table has no rows. // Used to determine if the database needs seeding from config. func (s *Store) IsEmpty() (bool, error) { var count int err := s.db.QueryRow("SELECT COUNT(*) FROM listeners").Scan(&count) if err != nil { return false, err } return count == 0, nil }