sqlstore_datasource.go 899 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package sqlstore
  2. import (
  3. "time"
  4. "github.com/torkelo/grafana-pro/pkg/bus"
  5. m "github.com/torkelo/grafana-pro/pkg/models"
  6. "github.com/go-xorm/xorm"
  7. )
  8. func init() {
  9. bus.AddHandler("sql", GetDataSources)
  10. bus.AddHandler("sql", AddDataSource)
  11. }
  12. func GetDataSources(query *m.GetDataSourcesQuery) error {
  13. sess := x.Limit(100, 0).Where("account_id=?", query.AccountId)
  14. query.Resp = make([]*m.DataSource, 0)
  15. return sess.Find(&query.Resp)
  16. }
  17. func AddDataSource(cmd *m.AddDataSourceCommand) error {
  18. return inTransaction(func(sess *xorm.Session) error {
  19. var err error
  20. ds := m.DataSource{
  21. AccountId: cmd.AccountId,
  22. Name: cmd.Name,
  23. Type: cmd.Type,
  24. Access: cmd.Access,
  25. Url: cmd.Url,
  26. Created: time.Now(),
  27. Updated: time.Now(),
  28. }
  29. if ds.Id == 0 {
  30. _, err = sess.Insert(ds)
  31. } else {
  32. _, err = sess.Id(ds.Id).Update(ds)
  33. }
  34. return err
  35. })
  36. }