dao.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/job/main/passport-user-compare/conf"
  5. "go-common/library/database/sql"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. )
  9. const (
  10. safeQuestionSegment = 100
  11. )
  12. // Dao dao
  13. type Dao struct {
  14. c *conf.Config
  15. httpClient *bm.Client
  16. originDB *sql.DB
  17. userDB *sql.DB
  18. secretDB *sql.DB
  19. }
  20. // New new dao.
  21. func New(c *conf.Config) (d *Dao) {
  22. d = &Dao{
  23. c: c,
  24. originDB: sql.NewMySQL(c.DB.Origin),
  25. userDB: sql.NewMySQL(c.DB.User),
  26. secretDB: sql.NewMySQL(c.DB.Secret),
  27. httpClient: bm.NewClient(c.HTTPClient),
  28. }
  29. return
  30. }
  31. // Ping check dao ok.
  32. func (d *Dao) Ping(c context.Context) (err error) {
  33. if err = d.originDB.Ping(c); err != nil {
  34. log.Info("dao.originDB.Ping() error(%v)", err)
  35. }
  36. if err = d.userDB.Ping(c); err != nil {
  37. log.Info("dao.newDB.Ping() error(%v)", err)
  38. }
  39. if err = d.secretDB.Ping(c); err != nil {
  40. log.Info("dao.secretDB.Ping() error(%v)", err)
  41. }
  42. return
  43. }
  44. // Close close connections.
  45. func (d *Dao) Close() (err error) {
  46. if d.originDB != nil {
  47. d.originDB.Close()
  48. }
  49. if d.userDB != nil {
  50. d.userDB.Close()
  51. }
  52. if d.secretDB != nil {
  53. d.secretDB.Close()
  54. }
  55. return
  56. }