sqlstore.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package sqlstore
  2. import (
  3. "fmt"
  4. "os"
  5. "path"
  6. "strings"
  7. "github.com/torkelo/grafana-pro/pkg/models"
  8. "github.com/torkelo/grafana-pro/pkg/setting"
  9. "github.com/go-xorm/xorm"
  10. _ "github.com/mattn/go-sqlite3"
  11. )
  12. var (
  13. x *xorm.Engine
  14. tables []interface{}
  15. HasEngine bool
  16. DbCfg struct {
  17. Type, Host, Name, User, Pwd, Path, SslMode string
  18. }
  19. UseSQLite3 bool
  20. )
  21. func Init() {
  22. tables = append(tables, new(models.Account), new(models.Dashboard), new(models.Collaborator))
  23. models.SaveAccount = SaveAccount
  24. models.GetAccount = GetAccount
  25. models.GetAccountByLogin = GetAccountByLogin
  26. models.GetOtherAccountsFor = GetOtherAccountsFor
  27. models.GetDashboard = GetDashboard
  28. models.SaveDashboard = SaveDashboard
  29. models.SearchQuery = SearchQuery
  30. models.DeleteDashboard = DeleteDashboard
  31. models.GetCollaboratorsForAccount = GetCollaboratorsForAccount
  32. models.AddCollaborator = AddCollaborator
  33. }
  34. func LoadModelsConfig() {
  35. DbCfg.Type = setting.Cfg.MustValue("database", "type")
  36. if DbCfg.Type == "sqlite3" {
  37. UseSQLite3 = true
  38. }
  39. DbCfg.Host = setting.Cfg.MustValue("database", "host")
  40. DbCfg.Name = setting.Cfg.MustValue("database", "name")
  41. DbCfg.User = setting.Cfg.MustValue("database", "user")
  42. if len(DbCfg.Pwd) == 0 {
  43. DbCfg.Pwd = setting.Cfg.MustValue("database", "password")
  44. }
  45. DbCfg.SslMode = setting.Cfg.MustValue("database", "ssl_mode")
  46. DbCfg.Path = setting.Cfg.MustValue("database", "path", "data/grafana.db")
  47. }
  48. func NewEngine() (err error) {
  49. if err = SetEngine(); err != nil {
  50. return err
  51. }
  52. if err = x.Sync2(tables...); err != nil {
  53. return fmt.Errorf("sync database struct error: %v\n", err)
  54. }
  55. return nil
  56. }
  57. func SetEngine() (err error) {
  58. x, err = getEngine()
  59. if err != nil {
  60. return fmt.Errorf("models.init(fail to connect to database): %v", err)
  61. }
  62. logPath := path.Join(setting.LogRootPath, "xorm.log")
  63. os.MkdirAll(path.Dir(logPath), os.ModePerm)
  64. f, err := os.Create(logPath)
  65. if err != nil {
  66. return fmt.Errorf("models.init(fail to create xorm.log): %v", err)
  67. }
  68. x.Logger = xorm.NewSimpleLogger(f)
  69. x.ShowSQL = true
  70. x.ShowInfo = true
  71. x.ShowDebug = true
  72. x.ShowErr = true
  73. x.ShowWarn = true
  74. return nil
  75. }
  76. func getEngine() (*xorm.Engine, error) {
  77. cnnstr := ""
  78. switch DbCfg.Type {
  79. case "mysql":
  80. cnnstr = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
  81. DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name)
  82. case "postgres":
  83. var host, port = "127.0.0.1", "5432"
  84. fields := strings.Split(DbCfg.Host, ":")
  85. if len(fields) > 0 && len(strings.TrimSpace(fields[0])) > 0 {
  86. host = fields[0]
  87. }
  88. if len(fields) > 1 && len(strings.TrimSpace(fields[1])) > 0 {
  89. port = fields[1]
  90. }
  91. cnnstr = fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s",
  92. DbCfg.User, DbCfg.Pwd, host, port, DbCfg.Name, DbCfg.SslMode)
  93. case "sqlite3":
  94. os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
  95. cnnstr = "file:" + DbCfg.Path + "?cache=shared&mode=rwc"
  96. default:
  97. return nil, fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  98. }
  99. return xorm.NewEngine(DbCfg.Type, cnnstr)
  100. }