dao.go 978 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package converge
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-card/model/card/operate"
  5. "go-common/app/interface/main/app-feed/conf"
  6. "go-common/library/database/sql"
  7. )
  8. const (
  9. _getSQL = "SELECT id,re_type,re_value,title,cover,content FROM content_card"
  10. )
  11. type Dao struct {
  12. db *sql.DB
  13. cardGet *sql.Stmt
  14. }
  15. func New(c *conf.Config) (d *Dao) {
  16. d = &Dao{
  17. db: sql.NewMySQL(c.MySQL.Manager),
  18. }
  19. // prepare
  20. d.cardGet = d.db.Prepared(_getSQL)
  21. return
  22. }
  23. func (d *Dao) Cards(c context.Context) (cm map[int64]*operate.Converge, err error) {
  24. rows, err := d.cardGet.Query(c)
  25. if err != nil {
  26. return
  27. }
  28. defer rows.Close()
  29. cm = map[int64]*operate.Converge{}
  30. for rows.Next() {
  31. c := &operate.Converge{}
  32. if err = rows.Scan(&c.ID, &c.ReType, &c.ReValue, &c.Title, &c.Cover, &c.Content); err != nil {
  33. return
  34. }
  35. c.Change()
  36. cm[c.ID] = c
  37. }
  38. return
  39. }
  40. // Close close memcache resource.
  41. func (dao *Dao) Close() {
  42. if dao.db != nil {
  43. dao.db.Close()
  44. }
  45. }