audit.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package audit
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-feed/conf"
  5. xsql "go-common/library/database/sql"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _getSQL = "SELECT mobi_app,build FROM audit"
  10. )
  11. // Dao is audit dao.
  12. type Dao struct {
  13. db *xsql.DB
  14. audGet *xsql.Stmt
  15. }
  16. // New new a audit dao.
  17. func New(c *conf.Config) (d *Dao) {
  18. d = &Dao{
  19. db: xsql.NewMySQL(c.MySQL.Show),
  20. }
  21. d.audGet = d.db.Prepared(_getSQL)
  22. return
  23. }
  24. // Audits get all audit build.
  25. func (d *Dao) Audits(c context.Context) (res map[string]map[int]struct{}, err error) {
  26. rows, err := d.audGet.Query(c)
  27. if err != nil {
  28. log.Error("query error(%v)", err)
  29. return
  30. }
  31. defer rows.Close()
  32. var (
  33. mobiApp string
  34. build int
  35. )
  36. res = map[string]map[int]struct{}{}
  37. for rows.Next() {
  38. if err = rows.Scan(&mobiApp, &build); err != nil {
  39. log.Error("rows.Scan error(%v)", err)
  40. res = nil
  41. return
  42. }
  43. if plat, ok := res[mobiApp]; ok {
  44. plat[build] = struct{}{}
  45. } else {
  46. res[mobiApp] = map[int]struct{}{
  47. build: struct{}{},
  48. }
  49. }
  50. }
  51. return
  52. }
  53. func (dao *Dao) PingDB(c context.Context) (err error) {
  54. return dao.db.Ping(c)
  55. }
  56. // Close close db resource.
  57. func (dao *Dao) Close() {
  58. if dao.db != nil {
  59. dao.db.Close()
  60. }
  61. }