SQL/Orm ArtistRepository complete

This commit is contained in:
Deluan
2020-01-12 17:32:06 -05:00
committed by Deluan Quintão
parent 334e8384ce
commit d70af2c39d
9 changed files with 346 additions and 16 deletions

63
persistence/db_sql/sql.go Normal file
View File

@@ -0,0 +1,63 @@
package db_sql
import (
"os"
"path"
"sync"
"github.com/astaxie/beego/orm"
"github.com/cloudsonic/sonic-server/conf"
"github.com/cloudsonic/sonic-server/log"
_ "github.com/mattn/go-sqlite3"
)
var once sync.Once
func Db() orm.Ormer {
once.Do(func() {
err := os.MkdirAll(conf.Sonic.DbPath, 0700)
if err != nil {
panic(err)
}
dbPath := path.Join(conf.Sonic.DbPath, "sqlite.db")
err = initORM(dbPath)
if err != nil {
panic(err)
}
log.Debug("Opening SQLite DB from: " + dbPath)
})
return orm.NewOrm()
}
func WithTx(block func(orm.Ormer) error) error {
o := orm.NewOrm()
err := o.Begin()
if err != nil {
return err
}
err = block(o)
if err != nil {
err2 := o.Rollback()
if err2 != nil {
return err2
}
return err
}
err2 := o.Commit()
if err2 != nil {
return err2
}
return nil
}
func initORM(dbPath string) error {
orm.Debug = true
orm.RegisterModel(new(Artist))
err := orm.RegisterDataBase("default", "sqlite3", dbPath)
if err != nil {
panic(err)
}
return orm.RunSyncdb("default", false, true)
}