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 2.6KB

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