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.

170 lines
5.0 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
  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), new(LoginSource), new(Webhook), new(IssueUser),
  29. new(Milestone), new(Label))
  30. }
  31. func LoadModelsConfig() {
  32. DbCfg.Type = base.Cfg.MustValue("database", "DB_TYPE")
  33. if DbCfg.Type == "sqlite3" {
  34. UseSQLite3 = true
  35. }
  36. DbCfg.Host = base.Cfg.MustValue("database", "HOST")
  37. DbCfg.Name = base.Cfg.MustValue("database", "NAME")
  38. DbCfg.User = base.Cfg.MustValue("database", "USER")
  39. DbCfg.Pwd = base.Cfg.MustValue("database", "PASSWD")
  40. DbCfg.SslMode = base.Cfg.MustValue("database", "SSL_MODE")
  41. DbCfg.Path = base.Cfg.MustValue("database", "PATH", "data/gogs.db")
  42. }
  43. func NewTestEngine(x *xorm.Engine) (err error) {
  44. switch DbCfg.Type {
  45. case "mysql":
  46. x, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
  47. DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
  48. case "postgres":
  49. var host, port = "127.0.0.1", "5432"
  50. fields := strings.Split(DbCfg.Host, ":")
  51. if len(fields) > 0 && len(strings.TrimSpace(fields[0])) > 0 {
  52. host = fields[0]
  53. }
  54. if len(fields) > 1 && len(strings.TrimSpace(fields[1])) > 0 {
  55. port = fields[1]
  56. }
  57. cnnstr := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s",
  58. DbCfg.User, DbCfg.Pwd, host, port, DbCfg.Name, DbCfg.SslMode)
  59. //fmt.Println(cnnstr)
  60. x, err = xorm.NewEngine("postgres", cnnstr)
  61. case "sqlite3":
  62. if !EnableSQLite3 {
  63. return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  64. }
  65. os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
  66. x, err = xorm.NewEngine("sqlite3", DbCfg.Path)
  67. default:
  68. return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  69. }
  70. if err != nil {
  71. return fmt.Errorf("models.init(fail to conntect database): %v", err)
  72. }
  73. return x.Sync(tables...)
  74. }
  75. func SetEngine() (err error) {
  76. switch DbCfg.Type {
  77. case "mysql":
  78. orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
  79. DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
  80. case "postgres":
  81. var host, port = "127.0.0.1", "5432"
  82. fields := strings.Split(DbCfg.Host, ":")
  83. if len(fields) > 0 && len(strings.TrimSpace(fields[0])) > 0 {
  84. host = fields[0]
  85. }
  86. if len(fields) > 1 && len(strings.TrimSpace(fields[1])) > 0 {
  87. port = fields[1]
  88. }
  89. orm, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s",
  90. DbCfg.User, DbCfg.Pwd, host, port, DbCfg.Name, DbCfg.SslMode))
  91. case "sqlite3":
  92. os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
  93. orm, err = xorm.NewEngine("sqlite3", DbCfg.Path)
  94. default:
  95. return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  96. }
  97. if err != nil {
  98. return fmt.Errorf("models.init(fail to conntect database): %v", err)
  99. }
  100. // WARNNING: for serv command, MUST remove the output to os.stdout,
  101. // so use log file to instead print to stdout.
  102. execDir, _ := base.ExecDir()
  103. logPath := execDir + "/log/xorm.log"
  104. os.MkdirAll(path.Dir(logPath), os.ModePerm)
  105. f, err := os.Create(logPath)
  106. if err != nil {
  107. return fmt.Errorf("models.init(fail to create xorm.log): %v", err)
  108. }
  109. orm.Logger = xorm.NewSimpleLogger(f)
  110. orm.ShowSQL = true
  111. orm.ShowDebug = true
  112. orm.ShowErr = true
  113. return nil
  114. }
  115. func NewEngine() (err error) {
  116. if err = SetEngine(); err != nil {
  117. return err
  118. }
  119. if err = orm.Sync(tables...); err != nil {
  120. return fmt.Errorf("sync database struct error: %v\n", err)
  121. }
  122. return nil
  123. }
  124. type Statistic struct {
  125. Counter struct {
  126. User, PublicKey, Repo, Watch, Action, Access,
  127. Issue, Comment, Mirror, Oauth, Release,
  128. LoginSource, Webhook, Milestone 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. stats.Counter.LoginSource, _ = orm.Count(new(LoginSource))
  144. stats.Counter.Webhook, _ = orm.Count(new(Webhook))
  145. stats.Counter.Milestone, _ = orm.Count(new(Milestone))
  146. return
  147. }
  148. // DumpDatabase dumps all data from database to file system.
  149. func DumpDatabase(filePath string) error {
  150. return orm.DumpAllToFile(filePath)
  151. }