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.

models.go 3.6KB

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