2014-12-16 05:04:08 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2014-12-16 14:05:49 -06:00
|
|
|
"time"
|
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2014-12-16 14:05:49 -06:00
|
|
|
|
|
|
|
"github.com/go-xorm/xorm"
|
2014-12-16 05:04:08 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2014-12-16 14:05:49 -06:00
|
|
|
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)
|
2014-12-19 00:27:25 -06:00
|
|
|
bus.AddHandler("sql", GetDataSourceById)
|
2014-12-18 13:26:06 -06:00
|
|
|
}
|
|
|
|
|
2014-12-19 00:27:25 -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
|
|
|
}
|
|
|
|
|
2014-12-16 14:05:49 -06:00
|
|
|
func GetDataSources(query *m.GetDataSourcesQuery) error {
|
2014-12-19 01:19:12 -06:00
|
|
|
sess := x.Limit(100, 0).Where("account_id=?", query.AccountId).Asc("name")
|
2014-12-16 14:05:49 -06:00
|
|
|
|
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 {
|
2014-12-16 14:05:49 -06:00
|
|
|
|
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
2015-01-09 09:36:23 -06:00
|
|
|
ds := &m.DataSource{
|
2014-12-16 14:05:49 -06:00
|
|
|
AccountId: cmd.AccountId,
|
|
|
|
Name: cmd.Name,
|
|
|
|
Type: cmd.Type,
|
|
|
|
Access: cmd.Access,
|
|
|
|
Url: cmd.Url,
|
2014-12-19 01:19:12 -06:00
|
|
|
User: cmd.User,
|
|
|
|
Password: cmd.Password,
|
|
|
|
Database: cmd.Database,
|
2015-01-09 09:36:23 -06:00
|
|
|
IsDefault: cmd.IsDefault,
|
2014-12-16 14:05:49 -06:00
|
|
|
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,
|
2014-12-19 01:19:12 -06:00
|
|
|
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
|
2014-12-16 14:05:49 -06:00
|
|
|
}
|
|
|
|
|
2015-01-09 09:36:23 -06:00
|
|
|
err = updateIsDefaultFlag(ds, sess)
|
2014-12-16 14:05:49 -06:00
|
|
|
return err
|
|
|
|
})
|
2014-12-16 09:45:07 -06:00
|
|
|
}
|