sqlstore_dashboards.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package sqlstore
  2. import "github.com/torkelo/grafana-pro/pkg/models"
  3. func SaveDashboard(dash *models.Dashboard) error {
  4. var err error
  5. sess := x.NewSession()
  6. defer sess.Close()
  7. if err = sess.Begin(); err != nil {
  8. return err
  9. }
  10. if dash.Id == 0 {
  11. _, err = sess.Insert(dash)
  12. } else {
  13. _, err = sess.Id(dash.Id).Update(dash)
  14. }
  15. if err != nil {
  16. sess.Rollback()
  17. return err
  18. } else if err = sess.Commit(); err != nil {
  19. return err
  20. }
  21. return nil
  22. }
  23. func GetDashboard(slug string, accountId int64) (*models.Dashboard, error) {
  24. dashboard := models.Dashboard{Slug: slug, AccountId: accountId}
  25. has, err := x.Get(&dashboard)
  26. if err != nil {
  27. return nil, err
  28. } else if has == false {
  29. return nil, models.ErrDashboardNotFound
  30. }
  31. return &dashboard, nil
  32. }
  33. func SearchQuery(query string, accountId int64) ([]*models.SearchResult, error) {
  34. sess := x.Limit(100, 0).Where("account_id=?", accountId)
  35. sess.Table("Dashboard")
  36. results := make([]*models.SearchResult, 0)
  37. err := sess.Find(&results)
  38. return results, err
  39. }
  40. func DeleteDashboard(slug string, accountId int64) error {
  41. sess := x.NewSession()
  42. defer sess.Close()
  43. rawSql := "DELETE FROM Dashboard WHERE account_id=? and slug=?"
  44. _, err := sess.Exec(rawSql, accountId, slug)
  45. return err
  46. }