2014-12-16 05:04:08 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2014-12-16 14:05:49 -06:00
|
|
|
"time"
|
|
|
|
|
2014-12-16 05:04:08 -06:00
|
|
|
"github.com/torkelo/grafana-pro/pkg/bus"
|
|
|
|
m "github.com/torkelo/grafana-pro/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-16 05:04:08 -06:00
|
|
|
}
|
|
|
|
|
2014-12-16 14:05:49 -06:00
|
|
|
func GetDataSources(query *m.GetDataSourcesQuery) error {
|
|
|
|
sess := x.Limit(100, 0).Where("account_id=?", query.AccountId)
|
|
|
|
|
|
|
|
query.Resp = make([]*m.DataSource, 0)
|
|
|
|
return sess.Find(&query.Resp)
|
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 {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
ds := m.DataSource{
|
|
|
|
AccountId: cmd.AccountId,
|
|
|
|
Name: cmd.Name,
|
|
|
|
Type: cmd.Type,
|
|
|
|
Access: cmd.Access,
|
|
|
|
Url: cmd.Url,
|
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if ds.Id == 0 {
|
|
|
|
_, err = sess.Insert(ds)
|
|
|
|
} else {
|
|
|
|
_, err = sess.Id(ds.Id).Update(ds)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
2014-12-16 09:45:07 -06:00
|
|
|
}
|