models.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "os"
  8. "path"
  9. _ "github.com/go-sql-driver/mysql"
  10. _ "github.com/lib/pq"
  11. "github.com/lunny/xorm"
  12. "github.com/gogits/gogs/modules/base"
  13. )
  14. var (
  15. orm *xorm.Engine
  16. tables []interface{}
  17. HasEngine bool
  18. DbCfg struct {
  19. Type, Host, Name, User, Pwd, Path, SslMode string
  20. }
  21. UseSQLite3 bool
  22. )
  23. func init() {
  24. tables = append(tables, new(User), new(PublicKey), new(Repository), new(Watch),
  25. new(Action), new(Access), new(Issue), new(Comment), new(Oauth2))
  26. }
  27. func LoadModelsConfig() {
  28. DbCfg.Type = base.Cfg.MustValue("database", "DB_TYPE")
  29. if DbCfg.Type == "sqlite3" {
  30. UseSQLite3 = true
  31. }
  32. DbCfg.Host = base.Cfg.MustValue("database", "HOST")
  33. DbCfg.Name = base.Cfg.MustValue("database", "NAME")
  34. DbCfg.User = base.Cfg.MustValue("database", "USER")
  35. DbCfg.Pwd = base.Cfg.MustValue("database", "PASSWD")
  36. DbCfg.SslMode = base.Cfg.MustValue("database", "SSL_MODE")
  37. DbCfg.Path = base.Cfg.MustValue("database", "PATH", "data/gogs.db")
  38. }
  39. func NewTestEngine(x *xorm.Engine) (err error) {
  40. switch DbCfg.Type {
  41. case "mysql":
  42. x, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
  43. DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
  44. case "postgres":
  45. x, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s",
  46. DbCfg.User, DbCfg.Pwd, DbCfg.Name, DbCfg.SslMode))
  47. case "sqlite3":
  48. os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
  49. x, err = xorm.NewEngine("sqlite3", DbCfg.Path)
  50. default:
  51. return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  52. }
  53. if err != nil {
  54. return fmt.Errorf("models.init(fail to conntect database): %v", err)
  55. }
  56. return x.Sync(tables...)
  57. }
  58. func SetEngine() (err error) {
  59. switch DbCfg.Type {
  60. case "mysql":
  61. orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
  62. DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
  63. case "postgres":
  64. orm, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s",
  65. DbCfg.User, DbCfg.Pwd, DbCfg.Name, DbCfg.SslMode))
  66. case "sqlite3":
  67. os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
  68. orm, err = xorm.NewEngine("sqlite3", DbCfg.Path)
  69. default:
  70. return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  71. }
  72. if err != nil {
  73. return fmt.Errorf("models.init(fail to conntect database): %v", err)
  74. }
  75. // WARNNING: for serv command, MUST remove the output to os.stdout,
  76. // so use log file to instead print to stdout.
  77. execDir, _ := base.ExecDir()
  78. logPath := execDir + "/log/xorm.log"
  79. os.MkdirAll(path.Dir(logPath), os.ModePerm)
  80. f, err := os.Create(logPath)
  81. if err != nil {
  82. return fmt.Errorf("models.init(fail to create xorm.log): %v", err)
  83. }
  84. orm.Logger = f
  85. orm.ShowSQL = true
  86. orm.ShowDebug = true
  87. orm.ShowErr = true
  88. return nil
  89. }
  90. func NewEngine() (err error) {
  91. if err = SetEngine(); err != nil {
  92. return err
  93. }
  94. if err = orm.Sync(tables...); err != nil {
  95. return fmt.Errorf("sync database struct error: %v\n", err)
  96. }
  97. return nil
  98. }
  99. type Statistic struct {
  100. Counter struct {
  101. User, PublicKey, Repo, Watch, Action, Access int64
  102. }
  103. }
  104. func GetStatistic() (stats Statistic) {
  105. stats.Counter.User, _ = orm.Count(new(User))
  106. stats.Counter.PublicKey, _ = orm.Count(new(PublicKey))
  107. stats.Counter.Repo, _ = orm.Count(new(Repository))
  108. stats.Counter.Watch, _ = orm.Count(new(Watch))
  109. stats.Counter.Action, _ = orm.Count(new(Action))
  110. stats.Counter.Access, _ = orm.Count(new(Access))
  111. return
  112. }