dao.go 948 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package block
  2. import (
  3. "context"
  4. "go-common/app/admin/main/member/conf"
  5. "go-common/library/cache/memcache"
  6. xsql "go-common/library/database/sql"
  7. bm "go-common/library/net/http/blademaster"
  8. "github.com/pkg/errors"
  9. )
  10. // Dao .
  11. type Dao struct {
  12. conf *conf.Config
  13. mc *memcache.Pool
  14. db *xsql.DB
  15. httpClient *bm.Client
  16. }
  17. // New init mysql db
  18. func New(conf *conf.Config, client *bm.Client, mc *memcache.Pool, db *xsql.DB) (dao *Dao) {
  19. dao = &Dao{
  20. conf: conf,
  21. mc: mc,
  22. db: db,
  23. httpClient: client,
  24. }
  25. return
  26. }
  27. // BeginTX .
  28. func (d *Dao) BeginTX(c context.Context) (tx *xsql.Tx, err error) {
  29. if tx, err = d.db.Begin(c); err != nil {
  30. err = errors.WithStack(err)
  31. }
  32. return
  33. }
  34. // Close close the resource.
  35. func (d *Dao) Close() {
  36. d.mc.Close()
  37. d.db.Close()
  38. }
  39. // Ping dao ping
  40. func (d *Dao) Ping(c context.Context) (err error) {
  41. if err = d.db.Ping(c); err != nil {
  42. return
  43. }
  44. return
  45. }