/service/db.go

https://github.com/88250/pipe · Go · 94 lines · 67 code · 13 blank · 14 comment · 22 complexity · 28a042067f6ea89c90c3639eb9db8c0a MD5 · raw file

  1. // Pipe - A small and beautiful blogging platform written in golang.
  2. // Copyright (c) 2017-present, b3log.org
  3. //
  4. // Pipe is licensed under Mulan PSL v2.
  5. // You can use this software according to the terms and conditions of the Mulan PSL v2.
  6. // You may obtain a copy of Mulan PSL v2 at:
  7. // http://license.coscl.org.cn/MulanPSL2
  8. // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  9. // See the Mulan PSL v2 for more details.
  10. package service
  11. import (
  12. "database/sql"
  13. "os"
  14. "time"
  15. "github.com/88250/gulu"
  16. "github.com/88250/pipe/model"
  17. "github.com/jinzhu/gorm"
  18. _ "github.com/jinzhu/gorm/dialects/mysql"
  19. _ "github.com/jinzhu/gorm/dialects/postgres"
  20. _ "github.com/jinzhu/gorm/dialects/sqlite"
  21. )
  22. // Logger
  23. var logger = gulu.Log.NewLogger(os.Stdout)
  24. var db *gorm.DB
  25. var useSQLite, useMySQL, usePostgres bool
  26. // ConnectDB connects to the database.
  27. func ConnectDB() {
  28. var err error
  29. useSQLite = false
  30. useMySQL = false
  31. usePostgres = false
  32. if "" != model.Conf.SQLite {
  33. db, err = gorm.Open("sqlite3", model.Conf.SQLite)
  34. useSQLite = true
  35. } else if "" != model.Conf.MySQL {
  36. db, err = gorm.Open("mysql", model.Conf.MySQL)
  37. useMySQL = true
  38. } else if "" != model.Conf.Postgres {
  39. db, err = gorm.Open("postgres", model.Conf.Postgres)
  40. usePostgres = true
  41. } else {
  42. logger.Fatal("please specify database")
  43. }
  44. if nil != err {
  45. logger.Fatalf("opens database failed: " + err.Error())
  46. }
  47. if useSQLite {
  48. logger.Debug("used [SQLite] as underlying database")
  49. } else if useMySQL {
  50. logger.Debug("used [MySQL] as underlying database")
  51. } else {
  52. logger.Debug("used [Postgres] as underlying database")
  53. }
  54. if err = db.AutoMigrate(model.Models...).Error; nil != err {
  55. logger.Fatal("auto migrate tables failed: " + err.Error())
  56. }
  57. if err = db.Model(&model.Article{}).AddIndex("idx_b3_pipe_articles_created_at", "created_at").Error; nil != err {
  58. logger.Fatal("adds index failed: " + err.Error())
  59. }
  60. db.DB().SetMaxIdleConns(10)
  61. db.DB().SetMaxOpenConns(50)
  62. db.DB().SetConnMaxLifetime(30 * time.Second)
  63. db.LogMode(model.Conf.ShowSQL)
  64. }
  65. // DisconnectDB disconnects from the database.
  66. func DisconnectDB() {
  67. if err := db.Close(); nil != err {
  68. logger.Errorf("Disconnect from database failed: " + err.Error())
  69. }
  70. }
  71. // DBStat returns database statistics.
  72. func DBStat() sql.DBStats {
  73. return db.DB().Stats()
  74. }
  75. // Database returns the underlying database name.
  76. func Database() string {
  77. if useSQLite {
  78. return "SQLite"
  79. }
  80. return "MySQL"
  81. }