dao.go 610 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/admin/main/card/conf"
  5. "go-common/library/database/orm"
  6. "github.com/jinzhu/gorm"
  7. )
  8. // Dao dao
  9. type Dao struct {
  10. c *conf.Config
  11. DB *gorm.DB
  12. }
  13. // New init mysql db
  14. func New(c *conf.Config) (d *Dao) {
  15. d = &Dao{
  16. c: c,
  17. // db
  18. DB: orm.NewMySQL(c.ORM),
  19. }
  20. d.initORM()
  21. return
  22. }
  23. func (d *Dao) initORM() {
  24. d.DB.LogMode(true)
  25. }
  26. // Close close the resource.
  27. func (d *Dao) Close() {
  28. if d.DB != nil {
  29. d.DB.Close()
  30. }
  31. }
  32. // Ping dao ping
  33. func (d *Dao) Ping(c context.Context) (err error) {
  34. if d.DB != nil {
  35. err = d.DB.DB().PingContext(c)
  36. }
  37. return
  38. }