plugin.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package plugin
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-resource/conf"
  5. "go-common/app/interface/main/app-resource/model/plugin"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _getSQL = "SELECT `name`,`package`,`policy`,`ver_code`,`ver_name`,`size`,`md5`,`url`,`enable`,`force`,`clear`,`min_build`,`max_build`,`base_code`,`base_name`,`desc`,`coverage` FROM plugin WHERE `enable`=1 AND `state`=0"
  11. )
  12. type Dao struct {
  13. db *sql.DB
  14. pluginGet *sql.Stmt
  15. }
  16. func New(c *conf.Config) (d *Dao) {
  17. d = &Dao{
  18. db: sql.NewMySQL(c.MySQL.Show),
  19. }
  20. // prepare
  21. d.pluginGet = d.db.Prepared(_getSQL)
  22. return
  23. }
  24. func (d *Dao) All(c context.Context) (psm map[string][]*plugin.Plugin, err error) {
  25. rows, err := d.pluginGet.Query(c)
  26. if err != nil {
  27. log.Error("query error(%v)", err)
  28. return nil, err
  29. }
  30. defer rows.Close()
  31. psm = map[string][]*plugin.Plugin{}
  32. for rows.Next() {
  33. p := &plugin.Plugin{}
  34. if err = rows.Scan(&p.Name, &p.Package, &p.Policy, &p.VerCode, &p.VerName, &p.Size, &p.MD5, &p.URL, &p.Enable, &p.Force, &p.Clear, &p.MinBuild, &p.MaxBuild, &p.BaseCode, &p.BaseName, &p.Desc, &p.Coverage); err != nil {
  35. log.Error("row.Scan error(%v)", err)
  36. return nil, err
  37. }
  38. if p.MaxBuild != 0 && p.MaxBuild < p.MinBuild {
  39. continue
  40. }
  41. psm[p.Name] = append(psm[p.Name], p)
  42. }
  43. return psm, err
  44. }
  45. // Close close memcache resource.
  46. func (dao *Dao) Close() {
  47. if dao.db != nil {
  48. dao.db.Close()
  49. }
  50. }
  51. func (dao *Dao) PingDB(c context.Context) (err error) {
  52. return dao.db.Ping(c)
  53. }