navidrome/persistence/persistence.go
2020-01-19 20:40:18 -05:00

149 lines
3.1 KiB
Go

package persistence
import (
"reflect"
"strings"
"sync"
"github.com/astaxie/beego/orm"
"github.com/cloudsonic/sonic-server/conf"
"github.com/cloudsonic/sonic-server/log"
"github.com/cloudsonic/sonic-server/model"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
const batchSize = 100
var (
once sync.Once
driver = "sqlite3"
mappedModels map[interface{}]interface{}
)
type SQLStore struct {
orm orm.Ormer
}
func New() model.DataStore {
once.Do(func() {
dbPath := conf.Sonic.DbPath
if dbPath == ":memory:" {
dbPath = "file::memory:?cache=shared"
}
log.Debug("Opening DB from: "+dbPath, "driver", driver)
err := initORM(dbPath)
if err != nil {
panic(err)
}
})
return &SQLStore{}
}
func (db *SQLStore) Album() model.AlbumRepository {
return NewAlbumRepository(db.getOrmer())
}
func (db *SQLStore) Artist() model.ArtistRepository {
return NewArtistRepository(db.getOrmer())
}
func (db *SQLStore) MediaFile() model.MediaFileRepository {
return NewMediaFileRepository(db.getOrmer())
}
func (db *SQLStore) MediaFolder() model.MediaFolderRepository {
return NewMediaFolderRepository(db.getOrmer())
}
func (db *SQLStore) Genre() model.GenreRepository {
return NewGenreRepository(db.getOrmer())
}
func (db *SQLStore) Playlist() model.PlaylistRepository {
return NewPlaylistRepository(db.getOrmer())
}
func (db *SQLStore) Property() model.PropertyRepository {
return NewPropertyRepository(db.getOrmer())
}
func (db *SQLStore) Resource(model interface{}) model.ResourceRepository {
return NewResource(db.getOrmer(), model, mappedModels[model])
}
func (db *SQLStore) WithTx(block func(tx model.DataStore) error) error {
o := orm.NewOrm()
err := o.Begin()
if err != nil {
return err
}
newDb := &SQLStore{orm: o}
err = block(newDb)
if err != nil {
err2 := o.Rollback()
if err2 != nil {
return err2
}
return err
}
err2 := o.Commit()
if err2 != nil {
return err2
}
return nil
}
func (db *SQLStore) getOrmer() orm.Ormer {
if db.orm == nil {
return orm.NewOrm()
}
return db.orm
}
func initORM(dbPath string) error {
verbose := conf.Sonic.LogLevel == "trace"
orm.Debug = verbose
if strings.Contains(dbPath, "postgres") {
driver = "postgres"
}
err := orm.RegisterDataBase("default", driver, dbPath)
if err != nil {
panic(err)
}
return orm.RunSyncdb("default", false, verbose)
}
func collectField(collection interface{}, getValue func(item interface{}) string) []string {
s := reflect.ValueOf(collection)
result := make([]string, s.Len())
for i := 0; i < s.Len(); i++ {
result[i] = getValue(s.Index(i).Interface())
}
return result
}
func registerModel(model interface{}, mappedModel interface{}) {
mappedModels[model] = mappedModel
orm.RegisterModel(mappedModel)
}
func init() {
mappedModels = map[interface{}]interface{}{}
registerModel(new(model.Artist), new(artist))
registerModel(new(model.Album), new(album))
registerModel(new(model.MediaFile), new(mediaFile))
registerModel(new(model.Property), new(property))
registerModel(new(model.Playlist), new(playlist))
registerModel(model.User{}, new(user))
orm.RegisterModel(new(checksum))
orm.RegisterModel(new(search))
}