dao.go 723 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package music
  2. import (
  3. "context"
  4. "go-common/app/admin/main/videoup/conf"
  5. "go-common/library/database/orm"
  6. "github.com/jinzhu/gorm"
  7. )
  8. // Dao struct user of Dao.
  9. type Dao struct {
  10. c *conf.Config
  11. // db
  12. DB *gorm.DB
  13. }
  14. var (
  15. d *Dao
  16. )
  17. // New create a instance of Dao and return.
  18. func New(c *conf.Config) (d *Dao) {
  19. d = &Dao{
  20. // conf
  21. c: c,
  22. // db
  23. DB: orm.NewMySQL(c.ORMArchive),
  24. }
  25. d.initORM()
  26. return
  27. }
  28. func (d *Dao) initORM() {
  29. d.DB.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. }