sqlstore_datasource.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. bus.AddHandler("sql", DeleteDataSource)
  12. bus.AddHandler("sql", UpdateDataSource)
  13. }
  14. func GetDataSources(query *m.GetDataSourcesQuery) error {
  15. sess := x.Limit(100, 0).Where("account_id=?", query.AccountId)
  16. query.Result = make([]*m.DataSource, 0)
  17. return sess.Find(&query.Result)
  18. }
  19. func DeleteDataSource(cmd *m.DeleteDataSourceCommand) error {
  20. return inTransaction(func(sess *xorm.Session) error {
  21. var rawSql = "DELETE FROM data_source WHERE id=? and account_id=?"
  22. _, err := sess.Exec(rawSql, cmd.Id, cmd.AccountId)
  23. return err
  24. })
  25. }
  26. func AddDataSource(cmd *m.AddDataSourceCommand) error {
  27. return inTransaction(func(sess *xorm.Session) error {
  28. var err error
  29. ds := m.DataSource{
  30. AccountId: cmd.AccountId,
  31. Name: cmd.Name,
  32. Type: cmd.Type,
  33. Access: cmd.Access,
  34. Url: cmd.Url,
  35. Created: time.Now(),
  36. Updated: time.Now(),
  37. }
  38. _, err = sess.Insert(ds)
  39. return err
  40. })
  41. }
  42. func UpdateDataSource(cmd *m.UpdateDataSourceCommand) error {
  43. return inTransaction(func(sess *xorm.Session) error {
  44. var err error
  45. ds := m.DataSource{
  46. Id: cmd.Id,
  47. AccountId: cmd.AccountId,
  48. Name: cmd.Name,
  49. Type: cmd.Type,
  50. Access: cmd.Access,
  51. Url: cmd.Url,
  52. Updated: time.Now(),
  53. }
  54. _, err = sess.Where("id=? and account_id=?", ds.Id, ds.AccountId).Update(&ds)
  55. return err
  56. })
  57. }