This commit is contained in:
2025-06-29 18:30:25 +02:00
parent a17541c2e6
commit 13fa688329
7 changed files with 69 additions and 12 deletions

View File

@ -9,6 +9,8 @@ import (
"sync"
"time"
_ "github.com/go-sql-driver/mysql"
log "github.com/sirupsen/logrus"
)
@ -37,18 +39,33 @@ type FlexDXClusterRepository struct {
}
func NewLog4OMContactsRepository(filePath string) *Log4OMContactsRepository {
db, err := sql.Open("sqlite3", filePath)
if err != nil {
Log.Errorf("Cannot open db", err)
}
_, err = db.Exec("PRAGMA journal_mode=WAL")
if err != nil {
panic(err)
if Cfg.Database.MySQL {
db, err := sql.Open("mysql", Cfg.Database.MySQLUser+":"+Cfg.Database.MySQLPassword+"@tcp("+Cfg.Database.MySQLHost+":"+Cfg.Database.MySQLPort+")/"+Cfg.Database.MySQLDbName)
if err != nil {
Log.Errorf("Cannot open db", err)
}
return &Log4OMContactsRepository{
db: db,
Log: Log}
} else if Cfg.Database.SQLite {
db, err := sql.Open("sqlite3", filePath)
if err != nil {
Log.Errorf("Cannot open db", err)
}
_, err = db.Exec("PRAGMA journal_mode=WAL")
if err != nil {
panic(err)
}
return &Log4OMContactsRepository{
db: db,
Log: Log}
}
return &Log4OMContactsRepository{
db: db,
Log: Log}
return nil
}
func NewFlexDXDatabase(filePath string) *FlexDXClusterRepository {
@ -95,6 +112,16 @@ func NewFlexDXDatabase(filePath string) *FlexDXClusterRepository {
}
}
func (r *Log4OMContactsRepository) CountEntries() int {
var contacts int
_ = r.db.QueryRow("SELECT COUNT(*) FROM log").Scan(&contacts)
if err != nil {
log.Error("could not query database", err)
}
return contacts
}
func (r *Log4OMContactsRepository) ListByCountry(countryID string, contactsChan chan []Contact, wg *sync.WaitGroup) {
defer wg.Done()
rows, err := r.db.Query("SELECT callsign, band, mode, dxcc, stationcallsign, country FROM log WHERE dxcc = ?", countryID)