models.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/mattn/go-sqlite3"
  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. 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))
  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. // os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
  50. // x, err = xorm.NewEngine("sqlite3", DbCfg.Path)
  51. default:
  52. return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  53. }
  54. if err != nil {
  55. return fmt.Errorf("models.init(fail to conntect database): %v", err)
  56. }
  57. return x.Sync(tables...)
  58. }
  59. func SetEngine() (err error) {
  60. switch DbCfg.Type {
  61. case "mysql":
  62. orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",
  63. DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
  64. case "postgres":
  65. orm, err = xorm.NewEngine("postgres", fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s",
  66. DbCfg.User, DbCfg.Pwd, DbCfg.Name, DbCfg.SslMode))
  67. case "sqlite3":
  68. os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm)
  69. orm, err = xorm.NewEngine("sqlite3", DbCfg.Path)
  70. default:
  71. return fmt.Errorf("Unknown database type: %s", DbCfg.Type)
  72. }
  73. if err != nil {
  74. return fmt.Errorf("models.init(fail to conntect database): %v", err)
  75. }
  76. // WARNNING: for serv command, MUST remove the output to os.stdout,
  77. // so use log file to instead print to stdout.
  78. execDir, _ := base.ExecDir()
  79. logPath := execDir + "/log/xorm.log"
  80. os.MkdirAll(path.Dir(logPath), os.ModePerm)
  81. f, err := os.Create(logPath)
  82. if err != nil {
  83. return fmt.Errorf("models.init(fail to create xorm.log): %v", err)
  84. }
  85. orm.Logger = f
  86. orm.ShowSQL = true
  87. orm.ShowDebug = true
  88. orm.ShowErr = true
  89. return nil
  90. }
  91. func NewEngine() (err error) {
  92. if err = SetEngine(); err != nil {
  93. return err
  94. }
  95. if err = orm.Sync(tables...); err != nil {
  96. return fmt.Errorf("sync database struct error: %v\n", err)
  97. }
  98. return nil
  99. }
  100. type Statistic struct {
  101. Counter struct {
  102. User, PublicKey, Repo, Watch, Action, Access int64
  103. }
  104. }
  105. func GetStatistic() (stats Statistic) {
  106. stats.Counter.User, _ = orm.Count(new(User))
  107. stats.Counter.PublicKey, _ = orm.Count(new(PublicKey))
  108. stats.Counter.Repo, _ = orm.Count(new(Repository))
  109. stats.Counter.Watch, _ = orm.Count(new(Watch))
  110. stats.Counter.Action, _ = orm.Count(new(Action))
  111. stats.Counter.Access, _ = orm.Count(new(Access))
  112. return
  113. }