dao.go 877 B

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