dao.go 706 B

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