grafana/pkg/services/sqlstore/datasource.go

113 lines
2.6 KiB
Go
Raw Normal View History

2014-12-16 05:04:08 -06:00
package sqlstore
import (
"time"
2015-02-05 03:37:13 -06:00
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
"github.com/go-xorm/xorm"
2014-12-16 05:04:08 -06:00
)
func init() {
bus.AddHandler("sql", GetDataSources)
2014-12-16 09:45:07 -06:00
bus.AddHandler("sql", AddDataSource)
2014-12-17 10:32:22 -06:00
bus.AddHandler("sql", DeleteDataSource)
bus.AddHandler("sql", UpdateDataSource)
bus.AddHandler("sql", GetDataSourceById)
2014-12-18 13:26:06 -06:00
}
func GetDataSourceById(query *m.GetDataSourceByIdQuery) error {
sess := x.Limit(100, 0).Where("account_id=? AND id=?", query.AccountId, query.Id)
2014-12-18 13:26:06 -06:00
has, err := sess.Get(&query.Result)
if !has {
return m.ErrDataSourceNotFound
}
return err
2014-12-16 05:04:08 -06:00
}
func GetDataSources(query *m.GetDataSourcesQuery) error {
sess := x.Limit(100, 0).Where("account_id=?", query.AccountId).Asc("name")
2014-12-17 10:32:22 -06:00
query.Result = make([]*m.DataSource, 0)
return sess.Find(&query.Result)
}
func DeleteDataSource(cmd *m.DeleteDataSourceCommand) error {
return inTransaction(func(sess *xorm.Session) error {
var rawSql = "DELETE FROM data_source WHERE id=? and account_id=?"
_, err := sess.Exec(rawSql, cmd.Id, cmd.AccountId)
return err
})
2014-12-16 05:04:08 -06:00
}
2014-12-16 09:45:07 -06:00
func AddDataSource(cmd *m.AddDataSourceCommand) error {
return inTransaction(func(sess *xorm.Session) error {
2015-01-09 09:36:23 -06:00
ds := &m.DataSource{
AccountId: cmd.AccountId,
Name: cmd.Name,
Type: cmd.Type,
Access: cmd.Access,
Url: cmd.Url,
User: cmd.User,
Password: cmd.Password,
Database: cmd.Database,
2015-01-09 09:36:23 -06:00
IsDefault: cmd.IsDefault,
Created: time.Now(),
Updated: time.Now(),
}
2015-01-09 09:36:23 -06:00
if _, err := sess.Insert(ds); err != nil {
return err
}
if err := updateIsDefaultFlag(ds, sess); err != nil {
return err
}
2014-12-17 10:32:22 -06:00
2015-01-09 09:36:23 -06:00
cmd.Result = ds
return nil
2014-12-17 10:32:22 -06:00
})
}
2015-01-09 09:36:23 -06:00
func updateIsDefaultFlag(ds *m.DataSource, sess *xorm.Session) error {
// Handle is default flag
if ds.IsDefault {
rawSql := "UPDATE data_source SET is_default = 0 WHERE account_id=? AND id <> ?"
if _, err := sess.Exec(rawSql, ds.AccountId, ds.Id); err != nil {
return err
}
}
return nil
}
2014-12-17 10:32:22 -06:00
func UpdateDataSource(cmd *m.UpdateDataSourceCommand) error {
return inTransaction(func(sess *xorm.Session) error {
2015-01-09 09:36:23 -06:00
ds := &m.DataSource{
2014-12-17 10:32:22 -06:00
Id: cmd.Id,
AccountId: cmd.AccountId,
Name: cmd.Name,
Type: cmd.Type,
Access: cmd.Access,
Url: cmd.Url,
User: cmd.User,
Password: cmd.Password,
Database: cmd.Database,
2014-12-17 10:32:22 -06:00
Updated: time.Now(),
2015-01-09 09:36:23 -06:00
IsDefault: cmd.IsDefault,
}
sess.UseBool("is_default")
_, err := sess.Where("id=? and account_id=?", ds.Id, ds.AccountId).Update(ds)
if err != nil {
return err
}
2015-01-09 09:36:23 -06:00
err = updateIsDefaultFlag(ds, sess)
return err
})
2014-12-16 09:45:07 -06:00
}