dao.go 737 B

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