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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. EnableSQLite3 bool
  22. UseSQLite3 bool
  23. )
  24. func init() {
  25. tables = append(tables, new(User), new(PublicKey), new(Repository), new(Watch),
  26. new(Action), new(Access), new(Issue), new(Comment), new(Oauth2), new(Follow))
  27. }
  28. func LoadModelsConfig() {
  29. DbCfg.Type = base.Cfg.MustValue("database", "DB_TYPE")
  30. if DbCfg.Type == "sqlite3" {
  31. UseSQLite3 = true
  32. }
  33. DbCfg.Host = base.Cfg.MustValue("database", "HOST")
  34. DbCfg.Name = base.Cfg.MustValue("database", "NAME")
  35. DbCfg.User = base.Cfg.MustValue("database", "USER")
  36. DbCfg.Pwd = base.Cfg.MustValue("database", "PASSWD")
  37. DbCfg.SslMode = base.Cfg.MustValue("database", "SSL_MODE")
  38. DbCfg.Path = base.Cfg.MustValue("database", "PATH", "data/gogs.db")
  39. }
  40. func NewTestEngine(x *xorm.Engine) (err error) {
  41. switch DbCfg.Type {
  42. case "mysql":
  43. x, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
  44. DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
  45. case "postgres":
  46. x, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s",
  47. DbCfg.User, DbCfg.Pwd, DbCfg.Name, DbCfg.SslMode))
  48. case "sqlite3":
  49. if !EnableSQLite3 {
  50. return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  51. }
  52. os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
  53. x, err = xorm.NewEngine("sqlite3", DbCfg.Path)
  54. default:
  55. return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  56. }
  57. if err != nil {
  58. return fmt.Errorf("models.init(fail to conntect database): %v", err)
  59. }
  60. return x.Sync(tables...)
  61. }
  62. func SetEngine() (err error) {
  63. switch DbCfg.Type {
  64. case "mysql":
  65. orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
  66. DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
  67. case "postgres":
  68. orm, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s",
  69. DbCfg.User, DbCfg.Pwd, DbCfg.Name, DbCfg.SslMode))
  70. case "sqlite3":
  71. os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
  72. orm, err = xorm.NewEngine("sqlite3", DbCfg.Path)
  73. default:
  74. return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  75. }
  76. if err != nil {
  77. return fmt.Errorf("models.init(fail to conntect database): %v", err)
  78. }
  79. // WARNNING: for serv command, MUST remove the output to os.stdout,
  80. // so use log file to instead print to stdout.
  81. execDir, _ := base.ExecDir()
  82. logPath := execDir + "/log/xorm.log"
  83. os.MkdirAll(path.Dir(logPath), os.ModePerm)
  84. f, err := os.Create(logPath)
  85. if err != nil {
  86. return fmt.Errorf("models.init(fail to create xorm.log): %v", err)
  87. }
  88. orm.Logger = f
  89. orm.ShowSQL = true
  90. orm.ShowDebug = true
  91. orm.ShowErr = true
  92. return nil
  93. }
  94. func NewEngine() (err error) {
  95. if err = SetEngine(); err != nil {
  96. return err
  97. }
  98. if err = orm.Sync(tables...); err != nil {
  99. return fmt.Errorf("sync database struct error: %v\n", err)
  100. }
  101. return nil
  102. }
  103. type Statistic struct {
  104. Counter struct {
  105. User, PublicKey, Repo, Watch, Action, Access int64
  106. }
  107. }
  108. func GetStatistic() (stats Statistic) {
  109. stats.Counter.User, _ = orm.Count(new(User))
  110. stats.Counter.PublicKey, _ = orm.Count(new(PublicKey))
  111. stats.Counter.Repo, _ = orm.Count(new(Repository))
  112. stats.Counter.Watch, _ = orm.Count(new(Watch))
  113. stats.Counter.Action, _ = orm.Count(new(Action))
  114. stats.Counter.Access, _ = orm.Count(new(Access))
  115. return
  116. }