dao.go 859 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package kfc
  2. import (
  3. "context"
  4. "go-common/app/admin/main/activity/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 *gorm.DB
  12. }
  13. // New create a instance of Dao and return.
  14. func New(c *conf.Config) (d *Dao) {
  15. d = &Dao{
  16. c: c,
  17. DB: orm.NewMySQL(c.ORM),
  18. }
  19. d.initORM()
  20. return
  21. }
  22. func (d *Dao) initORM() {
  23. gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
  24. if defaultTableName == "act_matchs" {
  25. return defaultTableName
  26. }
  27. return defaultTableName
  28. }
  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. }