You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.6 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. "strings"
  10. _ "github.com/go-sql-driver/mysql"
  11. "github.com/go-xorm/xorm"
  12. _ "github.com/lib/pq"
  13. "github.com/gogits/gogs/modules/base"
  14. )
  15. var (
  16. orm *xorm.Engine
  17. tables []interface{}
  18. HasEngine bool
  19. DbCfg struct {
  20. Type, Host, Name, User, Pwd, Path, SslMode string
  21. }
  22. EnableSQLite3 bool
  23. UseSQLite3 bool
  24. )
  25. func init() {
  26. tables = append(tables, new(User), new(PublicKey), new(Repository), new(Watch),
  27. new(Action), new(Access), new(Issue), new(Comment), new(Oauth2), new(Follow),
  28. new(Mirror), new(Release))
  29. }
  30. func LoadModelsConfig() {
  31. DbCfg.Type = base.Cfg.MustValue("database", "DB_TYPE")
  32. if DbCfg.Type == "sqlite3" {
  33. UseSQLite3 = true
  34. }
  35. DbCfg.Host = base.Cfg.MustValue("database", "HOST")
  36. DbCfg.Name = base.Cfg.MustValue("database", "NAME")
  37. DbCfg.User = base.Cfg.MustValue("database", "USER")
  38. DbCfg.Pwd = base.Cfg.MustValue("database", "PASSWD")
  39. DbCfg.SslMode = base.Cfg.MustValue("database", "SSL_MODE")
  40. DbCfg.Path = base.Cfg.MustValue("database", "PATH", "data/gogs.db")
  41. }
  42. func NewTestEngine(x *xorm.Engine) (err error) {
  43. switch DbCfg.Type {
  44. case "mysql":
  45. x, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
  46. DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
  47. case "postgres":
  48. var host, port = "127.0.0.1", "5432"
  49. fields := strings.Split(DbCfg.Host, ":")
  50. if len(fields) > 0 && len(strings.TrimSpace(fields[0])) > 0 {
  51. host = fields[0]
  52. }
  53. if len(fields) > 1 && len(strings.TrimSpace(fields[1])) > 0 {
  54. port = fields[1]
  55. }
  56. cnnstr := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s",
  57. DbCfg.User, DbCfg.Pwd, host, port, DbCfg.Name, DbCfg.SslMode)
  58. //fmt.Println(cnnstr)
  59. x, err = xorm.NewEngine("postgres", cnnstr)
  60. case "sqlite3":
  61. if !EnableSQLite3 {
  62. return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  63. }
  64. os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
  65. x, err = xorm.NewEngine("sqlite3", DbCfg.Path)
  66. default:
  67. return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  68. }
  69. if err != nil {
  70. return fmt.Errorf("models.init(fail to conntect database): %v", err)
  71. }
  72. return x.Sync(tables...)
  73. }
  74. func SetEngine() (err error) {
  75. switch DbCfg.Type {
  76. case "mysql":
  77. orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
  78. DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
  79. case "postgres":
  80. var host, port = "127.0.0.1", "5432"
  81. fields := strings.Split(DbCfg.Host, ":")
  82. if len(fields) > 0 && len(strings.TrimSpace(fields[0])) > 0 {
  83. host = fields[0]
  84. }
  85. if len(fields) > 1 && len(strings.TrimSpace(fields[1])) > 0 {
  86. port = fields[1]
  87. }
  88. orm, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s",
  89. DbCfg.User, DbCfg.Pwd, host, port, DbCfg.Name, DbCfg.SslMode))
  90. case "sqlite3":
  91. os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
  92. orm, err = xorm.NewEngine("sqlite3", DbCfg.Path)
  93. default:
  94. return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  95. }
  96. if err != nil {
  97. return fmt.Errorf("models.init(fail to conntect database): %v", err)
  98. }
  99. // WARNNING: for serv command, MUST remove the output to os.stdout,
  100. // so use log file to instead print to stdout.
  101. execDir, _ := base.ExecDir()
  102. logPath := execDir + "/log/xorm.log"
  103. os.MkdirAll(path.Dir(logPath), os.ModePerm)
  104. f, err := os.Create(logPath)
  105. if err != nil {
  106. return fmt.Errorf("models.init(fail to create xorm.log): %v", err)
  107. }
  108. orm.Logger = xorm.NewSimpleLogger(f)
  109. orm.ShowSQL = true
  110. orm.ShowDebug = true
  111. orm.ShowErr = true
  112. return nil
  113. }
  114. func NewEngine() (err error) {
  115. if err = SetEngine(); err != nil {
  116. return err
  117. }
  118. if err = orm.Sync(tables...); err != nil {
  119. return fmt.Errorf("sync database struct error: %v\n", err)
  120. }
  121. return nil
  122. }
  123. type Statistic struct {
  124. Counter struct {
  125. User, PublicKey, Repo,
  126. Watch, Action, Access,
  127. Issue, Comment,
  128. Mirror, Oauth, Release int64
  129. }
  130. }
  131. func GetStatistic() (stats Statistic) {
  132. stats.Counter.User, _ = orm.Count(new(User))
  133. stats.Counter.PublicKey, _ = orm.Count(new(PublicKey))
  134. stats.Counter.Repo, _ = orm.Count(new(Repository))
  135. stats.Counter.Watch, _ = orm.Count(new(Watch))
  136. stats.Counter.Action, _ = orm.Count(new(Action))
  137. stats.Counter.Access, _ = orm.Count(new(Access))
  138. stats.Counter.Issue, _ = orm.Count(new(Issue))
  139. stats.Counter.Comment, _ = orm.Count(new(Comment))
  140. stats.Counter.Mirror, _ = orm.Count(new(Mirror))
  141. stats.Counter.Oauth, _ = orm.Count(new(Oauth2))
  142. stats.Counter.Release, _ = orm.Count(new(Release))
  143. return
  144. }