audit.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package audit
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-resource/conf"
  5. "go-common/library/database/sql"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _auditSQL = "SELECT mobi_app,build FROM audit"
  10. )
  11. // Dao is notice dao.
  12. type Dao struct {
  13. db *sql.DB
  14. audit *sql.Stmt
  15. }
  16. // New new a notice dao.
  17. func New(c *conf.Config) (d *Dao) {
  18. d = &Dao{
  19. db: sql.NewMySQL(c.MySQL.Show),
  20. }
  21. d.audit = d.db.Prepared(_auditSQL)
  22. return
  23. }
  24. // Audits get all audit build.
  25. func (d *Dao) Audits(ctx context.Context) (res map[string]map[int]struct{}, err error) {
  26. rows, err := d.audit.Query(ctx)
  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. // Close close memcache resource.
  54. func (dao *Dao) Close() {
  55. if dao.db != nil {
  56. dao.db.Close()
  57. }
  58. }