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.

95 lines
2.5 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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() {
  30. var err error
  31. switch DbCfg.Type {
  32. case "mysql":
  33. orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@%s/%s?charset=utf8",
  34. DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name))
  35. case "postgres":
  36. orm, 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. orm, err = xorm.NewEngine("sqlite3", DbCfg.Path)
  41. default:
  42. fmt.Printf("Unknown database type: %s\n", DbCfg.Type)
  43. os.Exit(2)
  44. }
  45. if err != nil {
  46. fmt.Printf("models.init(fail to conntect database): %v\n", err)
  47. os.Exit(2)
  48. }
  49. // WARNNING: for serv command, MUST remove the output to os.stdout,
  50. // so use log file to instead print to stdout.
  51. //x.ShowDebug = true
  52. //orm.ShowErr = true
  53. f, err := os.Create("xorm.log")
  54. if err != nil {
  55. fmt.Printf("models.init(fail to create xorm.log): %v\n", err)
  56. os.Exit(2)
  57. }
  58. orm.Logger = f
  59. orm.ShowSQL = true
  60. }
  61. func NewEngine() {
  62. setEngine()
  63. if err := orm.Sync(new(User), new(PublicKey), new(Repository), new(Watch),
  64. new(Action), new(Access), new(Issue)); err != nil {
  65. fmt.Printf("sync database struct error: %v\n", err)
  66. os.Exit(2)
  67. }
  68. }
  69. type Statistic struct {
  70. Counter struct {
  71. User, PublicKey, Repo, Watch, Action, Access int64
  72. }
  73. }
  74. func GetStatistic() (stats Statistic) {
  75. stats.Counter.User, _ = orm.Count(new(User))
  76. stats.Counter.PublicKey, _ = orm.Count(new(PublicKey))
  77. stats.Counter.Repo, _ = orm.Count(new(Repository))
  78. stats.Counter.Watch, _ = orm.Count(new(Watch))
  79. stats.Counter.Action, _ = orm.Count(new(Action))
  80. stats.Counter.Access, _ = orm.Count(new(Access))
  81. return
  82. }