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

models.go 3.7 kB

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