1
0

BaseStatistics -> SqlStatistics.

This commit is contained in:
Alexander Andreev 2024-12-31 00:24:29 +04:00
parent e092a34055
commit 714a3790ee
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34
2 changed files with 14 additions and 14 deletions

View File

@ -31,7 +31,7 @@ var (
) )
type SQLiteStatistics struct { type SQLiteStatistics struct {
statistics.BaseStatistics statistics.SqlStatistics
} }
func New(path string) (statistics.Statistics, error) { func New(path string) (statistics.Statistics, error) {
@ -41,7 +41,7 @@ func New(path string) (statistics.Statistics, error) {
} }
stats := &SQLiteStatistics{ stats := &SQLiteStatistics{
BaseStatistics: statistics.BaseStatistics{ SqlStatistics: statistics.SqlStatistics{
Db: db, DbDateFormat: dbDateFormat}} Db: db, DbDateFormat: dbDateFormat}}
db.Exec("PRAGMA foreign_keys = ON;") db.Exec("PRAGMA foreign_keys = ON;")
@ -52,31 +52,31 @@ func New(path string) (statistics.Statistics, error) {
statistics.ErrPrepareStmt{Name: "initial schema"}.Error()) statistics.ErrPrepareStmt{Name: "initial schema"}.Error())
} }
stats.BaseStatistics.StmtSongAdd, err = db.Prepare(querySongAdd) stats.SqlStatistics.StmtSongAdd, err = db.Prepare(querySongAdd)
if err != nil { if err != nil {
return nil, errors.Wrap(err, return nil, errors.Wrap(err,
statistics.ErrPrepareStmt{Name: "song_add"}.Error()) statistics.ErrPrepareStmt{Name: "song_add"}.Error())
} }
stats.BaseStatistics.StmtHistoryAdd, err = db.Prepare(queryHistoryAdd) stats.SqlStatistics.StmtHistoryAdd, err = db.Prepare(queryHistoryAdd)
if err != nil { if err != nil {
return nil, errors.Wrap(err, return nil, errors.Wrap(err,
statistics.ErrPrepareStmt{Name: "history_add"}.Error()) statistics.ErrPrepareStmt{Name: "history_add"}.Error())
} }
stats.BaseStatistics.StmtLastNSongs, err = db.Prepare(queryLastNSongs) stats.SqlStatistics.StmtLastNSongs, err = db.Prepare(queryLastNSongs)
if err != nil { if err != nil {
return nil, errors.Wrap(err, return nil, errors.Wrap(err,
statistics.ErrPrepareStmt{Name: "last N songs"}.Error()) statistics.ErrPrepareStmt{Name: "last N songs"}.Error())
} }
stats.BaseStatistics.StmtMostPopularSongs, err = db.Prepare(queryMostPopularSongs) stats.SqlStatistics.StmtMostPopularSongs, err = db.Prepare(queryMostPopularSongs)
if err != nil { if err != nil {
return nil, errors.Wrap(err, return nil, errors.Wrap(err,
statistics.ErrPrepareStmt{Name: "most popular song"}.Error()) statistics.ErrPrepareStmt{Name: "most popular song"}.Error())
} }
stats.BaseStatistics.StmtMostSimultaneousListeners, err = db.Prepare(queryMostSimultaneousListeners) stats.SqlStatistics.StmtMostSimultaneousListeners, err = db.Prepare(queryMostSimultaneousListeners)
if err != nil { if err != nil {
return nil, errors.Wrap(err, return nil, errors.Wrap(err,
statistics.ErrPrepareStmt{Name: "most simultaneous listeners"}.Error()) statistics.ErrPrepareStmt{Name: "most simultaneous listeners"}.Error())
@ -86,5 +86,5 @@ func New(path string) (statistics.Statistics, error) {
} }
func (s *SQLiteStatistics) Close() error { func (s *SQLiteStatistics) Close() error {
return s.BaseStatistics.Close() return s.SqlStatistics.Close()
} }

View File

@ -30,7 +30,7 @@ func (e ErrPrepareStmt) Error() string {
var ErrNoSong = errors.New("no song was passed (a struct is nil or empty)") var ErrNoSong = errors.New("no song was passed (a struct is nil or empty)")
var ErrSongNotAdded = errors.New("song was not added") var ErrSongNotAdded = errors.New("song was not added")
type BaseStatistics struct { type SqlStatistics struct {
Db *sql.DB Db *sql.DB
DbDateFormat string DbDateFormat string
@ -42,7 +42,7 @@ type BaseStatistics struct {
StmtMostSimultaneousListeners *sql.Stmt StmtMostSimultaneousListeners *sql.Stmt
} }
func (s *BaseStatistics) Add(song *radio.Song) error { func (s *SqlStatistics) Add(song *radio.Song) error {
if song == nil || song.Artist == "" || song.Title == "" { if song == nil || song.Artist == "" || song.Title == "" {
return ErrNoSong return ErrNoSong
} }
@ -74,7 +74,7 @@ func (s *BaseStatistics) Add(song *radio.Song) error {
return nil return nil
} }
func (s *BaseStatistics) LastNSongs(n int64) ([]radio.Song, error) { func (s *SqlStatistics) LastNSongs(n int64) ([]radio.Song, error) {
if n == 0 { if n == 0 {
return nil, nil return nil, nil
} }
@ -121,7 +121,7 @@ func (s *BaseStatistics) LastNSongs(n int64) ([]radio.Song, error) {
return lst, nil return lst, nil
} }
func (s *BaseStatistics) MostNPopularSongs(n int64) ([]radio.Song, error) { func (s *SqlStatistics) MostNPopularSongs(n int64) ([]radio.Song, error) {
if n == 0 { if n == 0 {
return nil, nil return nil, nil
} }
@ -129,10 +129,10 @@ func (s *BaseStatistics) MostNPopularSongs(n int64) ([]radio.Song, error) {
return nil, nil return nil, nil
} }
func (s *BaseStatistics) MostSimultaneousListeners() (radio.Song, error) { func (s *SqlStatistics) MostSimultaneousListeners() (radio.Song, error) {
return radio.Song{}, nil return radio.Song{}, nil
} }
func (s *BaseStatistics) Close() error { func (s *SqlStatistics) Close() error {
return s.Db.Close() return s.Db.Close()
} }