models.go 3.4 KB

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