2022-11-12 16:22:55 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2023-11-23 20:46:54 +00:00
|
|
|
"strings"
|
2022-11-16 00:07:01 +00:00
|
|
|
"th7/data/config"
|
2022-11-12 16:22:55 +00:00
|
|
|
"th7/ports"
|
2022-11-16 00:07:01 +00:00
|
|
|
"time"
|
2022-11-12 16:22:55 +00:00
|
|
|
)
|
|
|
|
|
2022-11-16 00:07:01 +00:00
|
|
|
func NewAdapter(corePort ports.CorePort, cfg config.Config) (ports.DBPort, error) {
|
2022-11-12 16:22:55 +00:00
|
|
|
|
2022-11-16 00:07:01 +00:00
|
|
|
var duration time.Duration
|
|
|
|
var no_logging bool
|
|
|
|
var adapter ports.DBPort
|
|
|
|
var err error
|
|
|
|
|
|
|
|
no_logging = cfg.Board.NoLogging
|
|
|
|
duration = time.Duration(cfg.Board.Logfreq) * time.Second
|
|
|
|
|
2023-11-23 16:55:57 +00:00
|
|
|
// if no DB is specified, or nolog=true then use a dummy db adapter
|
2022-11-16 00:07:01 +00:00
|
|
|
if _, ok := cfg.DB["type"]; !ok || no_logging {
|
2023-11-23 16:55:57 +00:00
|
|
|
adapter, _ = NewDummyAdapter()
|
2022-11-16 00:07:01 +00:00
|
|
|
go startLoggingProcess(adapter, corePort, duration)
|
|
|
|
return adapter, nil
|
2022-11-12 16:22:55 +00:00
|
|
|
}
|
|
|
|
|
2023-11-23 20:46:54 +00:00
|
|
|
db := cfg.DB["type"].(string)
|
2022-11-16 00:07:01 +00:00
|
|
|
|
2023-11-23 20:46:54 +00:00
|
|
|
switch strings.ToLower(db) {
|
2022-11-12 16:22:55 +00:00
|
|
|
case "sqlite3":
|
2023-11-23 16:55:57 +00:00
|
|
|
adapter, err = NewSQLite3Adapter(cfg)
|
2022-11-16 00:07:01 +00:00
|
|
|
|
2023-11-23 20:46:54 +00:00
|
|
|
case "influxdb":
|
|
|
|
adapter, err = NewInfluxDBAdapter(cfg)
|
|
|
|
|
2022-11-16 00:07:01 +00:00
|
|
|
default:
|
|
|
|
return &DummyAdapter{}, errors.New("unknown db type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return &DummyAdapter{}, err
|
2022-11-12 16:22:55 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 00:07:01 +00:00
|
|
|
go startLoggingProcess(adapter, corePort, duration)
|
|
|
|
return adapter, nil
|
2022-11-12 16:22:55 +00:00
|
|
|
}
|