Progres on move to sql from rethinkdb

This commit is contained in:
Torkel Ödegaard
2014-11-20 12:11:07 +01:00
parent 9b68911d00
commit eb2c078898
13 changed files with 118 additions and 62 deletions
+10 -8
View File
@@ -6,9 +6,11 @@ import (
"path"
"strings"
"github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/setting"
"github.com/go-xorm/xorm"
_ "github.com/mattn/go-sqlite3"
"github.com/torkelo/grafana-pro/pkg/setting"
)
var (
@@ -24,14 +26,14 @@ var (
UseSQLite3 bool
)
type AccountDto struct {
Id int64
Email string `xorm:"UNIQUE NOT NULL"`
Passwd string `xorm:"NOT NULL"`
}
func Init() {
tables = append(tables, new(models.Account), new(models.Dashboard))
func init() {
tables = append(tables, new(AccountDto))
models.CreateAccount = CreateAccount
models.GetAccount = GetAccount
models.GetAccountByLogin = GetAccountByLogin
models.GetDashboard = GetDashboard
models.SaveDashboard = SaveDashboard
}
func LoadModelsConfig() {
+29 -14
View File
@@ -1,8 +1,10 @@
package sqlstore
import "github.com/torkelo/grafana-pro/pkg/log"
import (
"github.com/torkelo/grafana-pro/pkg/models"
)
func SaveAccount() error {
func CreateAccount(account *models.Account) error {
var err error
sess := x.NewSession()
@@ -12,12 +14,7 @@ func SaveAccount() error {
return err
}
u := &AccountDto{
Email: "asdasdas",
Passwd: "MyPassWd",
}
if _, err = sess.Insert(u); err != nil {
if _, err = sess.Insert(account); err != nil {
sess.Rollback()
return err
} else if err = sess.Commit(); err != nil {
@@ -27,14 +24,32 @@ func SaveAccount() error {
return nil
}
func GetAccounts() {
var resp = make([]*AccountDto, 1)
err := x.Find(&resp)
func GetAccount(id int64) (*models.Account, error) {
var err error
account := &models.Account{Id: id}
has, err := x.Get(account)
if err != nil {
log.Error(4, "Error", err)
return nil, err
} else if has == false {
return nil, models.ErrAccountNotFound
}
for _, i := range resp {
log.Info("Item %v", i)
return account, nil
}
func GetAccountByLogin(emailOrLogin string) (*models.Account, error) {
var err error
account := &models.Account{Login: emailOrLogin}
has, err := x.Get(account)
if err != nil {
return nil, err
} else if has == false {
return nil, models.ErrAccountNotFound
}
return account, nil
}
@@ -0,0 +1,38 @@
package sqlstore
import (
"github.com/torkelo/grafana-pro/pkg/models"
)
func SaveDashboard(dash *models.Dashboard) error {
var err error
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
if _, err = sess.Insert(dash); err != nil {
sess.Rollback()
return err
} else if err = sess.Commit(); err != nil {
return err
}
return nil
}
func GetDashboard(slug string, accountId int64) (*models.Dashboard, error) {
dashboard := models.Dashboard{Slug: slug, AccountId: accountId}
has, err := x.Get(&dashboard)
if err != nil {
return nil, err
} else if has == false {
return nil, models.ErrDashboardNotFound
}
return &dashboard, nil
}