dao.go 945 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package dao
  2. import (
  3. "context"
  4. "go-common/library/log"
  5. "go-common/app/job/live/xlottery/internal/conf"
  6. "go-common/library/cache/redis"
  7. xsql "go-common/library/database/sql"
  8. )
  9. // Dao dao
  10. type Dao struct {
  11. c *conf.Config
  12. redis *redis.Pool
  13. db *xsql.DB
  14. }
  15. // New init mysql db
  16. func New(c *conf.Config) (dao *Dao) {
  17. dao = &Dao{
  18. c: c,
  19. redis: redis.NewPool(c.Redis.Lottery),
  20. db: xsql.NewMySQL(c.Database.Lottery),
  21. }
  22. return
  23. }
  24. // Close close the resource.
  25. func (d *Dao) Close() {
  26. d.redis.Close()
  27. d.db.Close()
  28. }
  29. // Ping dao ping
  30. func (d *Dao) Ping(ctx context.Context) error {
  31. // TODO: add mc,redis... if you use
  32. return d.db.Ping(ctx)
  33. }
  34. func (d *Dao) execSqlWithBindParams(c context.Context, sql *string, bindParams ...interface{}) (affect int64, err error) {
  35. res, err := d.db.Exec(c, *sql, bindParams...)
  36. if err != nil {
  37. log.Error("db.Exec(%s) error(%v)", *sql, err)
  38. return
  39. }
  40. return res.RowsAffected()
  41. }