dao.go 1005 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package blocked
  2. import (
  3. "context"
  4. "go-common/app/admin/main/credit/conf"
  5. "go-common/library/database/orm"
  6. bm "go-common/library/net/http/blademaster"
  7. "github.com/jinzhu/gorm"
  8. )
  9. // Dao struct info of Dao.
  10. type Dao struct {
  11. // mysql
  12. ReadDB *gorm.DB
  13. DB *gorm.DB
  14. // http
  15. client *bm.Client
  16. // conf
  17. c *conf.Config
  18. }
  19. // New new a Dao and return.
  20. func New(c *conf.Config) (d *Dao) {
  21. d = &Dao{
  22. // conf
  23. c: c,
  24. // http client
  25. client: bm.NewClient(c.HTTPClient),
  26. ReadDB: orm.NewMySQL(c.ORM.Read),
  27. DB: orm.NewMySQL(c.ORM.Write),
  28. }
  29. d.initORM()
  30. return
  31. }
  32. func (d *Dao) initORM() {
  33. d.ReadDB.LogMode(true)
  34. d.DB.LogMode(true)
  35. }
  36. // Close close dao.
  37. func (d *Dao) Close() {
  38. if d.ReadDB != nil {
  39. d.ReadDB.Close()
  40. }
  41. if d.DB != nil {
  42. d.DB.Close()
  43. }
  44. }
  45. // Ping check connection of db , mc.
  46. func (d *Dao) Ping(c context.Context) (err error) {
  47. if d.ReadDB != nil {
  48. err = d.ReadDB.DB().PingContext(c)
  49. return
  50. }
  51. if d.DB != nil {
  52. err = d.DB.DB().PingContext(c)
  53. }
  54. return
  55. }