param.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package param
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/app-resource/conf"
  6. "go-common/app/interface/main/app-resource/model/param"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. // get param key and value
  12. _getAllSQL = "SELECT name,value,plat,build,conditions FROM param WHERE state=0 AND plat!=9"
  13. )
  14. // Dao is a param dao.
  15. type Dao struct {
  16. db *sql.DB
  17. get *sql.Stmt
  18. }
  19. // New new a param dao.
  20. func New(c *conf.Config) (d *Dao) {
  21. d = &Dao{
  22. db: sql.NewMySQL(c.MySQL.Show),
  23. }
  24. // prepare
  25. d.get = d.db.Prepared(_getAllSQL)
  26. return
  27. }
  28. // All get all param
  29. func (d *Dao) All(ctx context.Context) (m map[string][]*param.Param, err error) {
  30. rows, err := d.get.Query(ctx)
  31. if err != nil {
  32. log.Error("d.get error(%v)", err)
  33. return nil, err
  34. }
  35. defer rows.Close()
  36. m = map[string][]*param.Param{}
  37. var _key = "param_%d"
  38. for rows.Next() {
  39. p := &param.Param{}
  40. if err = rows.Scan(&p.Name, &p.Value, &p.Plat, &p.Build, &p.Condition); err != nil {
  41. log.Error("row.Scan error(%v)", err)
  42. return nil, err
  43. }
  44. p.Change()
  45. key := fmt.Sprintf(_key, p.Plat)
  46. m[key] = append(m[key], p)
  47. }
  48. return
  49. }
  50. // Close close memcache resource.
  51. func (d *Dao) Close() {
  52. if d.db != nil {
  53. d.db.Close()
  54. }
  55. }