dao.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package whitelist
  2. import (
  3. "context"
  4. "go-common/app/interface/main/creative/conf"
  5. "go-common/app/interface/main/creative/model/archive"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. var (
  10. _getAllSQL = "select mid, type from whitelist where state=1 order by type asc, ctime desc"
  11. )
  12. // Dao define
  13. type Dao struct {
  14. db *sql.DB
  15. getAllStmt *sql.Stmt
  16. }
  17. // New init dao
  18. func New(c *conf.Config) (d *Dao) {
  19. d = &Dao{
  20. db: sql.NewMySQL(c.DB.Creative),
  21. }
  22. d.getAllStmt = d.db.Prepared(_getAllSQL)
  23. return
  24. }
  25. // Ping db
  26. func (d *Dao) Ping(c context.Context) (err error) {
  27. return d.db.Ping(c)
  28. }
  29. // Close db
  30. func (d *Dao) Close() (err error) {
  31. return d.db.Close()
  32. }
  33. // List fn
  34. func (d *Dao) List(c context.Context) (wls []*archive.WhiteList, err error) {
  35. rows, err := d.getAllStmt.Query(c)
  36. if err != nil {
  37. log.Error("mysqlDB.Query error(%v)", err)
  38. return
  39. }
  40. defer rows.Close()
  41. for rows.Next() {
  42. wl := &archive.WhiteList{}
  43. if err = rows.Scan(&wl.Mid, &wl.Tp); err != nil {
  44. log.Error("row.Scan error(%v)", err)
  45. return
  46. }
  47. wls = append(wls, wl)
  48. }
  49. return
  50. }