mysql.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "time"
  6. pushmdl "go-common/app/service/main/push/model"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _delCallbacksSQL = `DELETE FROM push_callbacks where ctime <= ? limit ?`
  12. _reportLastIDSQL = `SELECT MAX(id) from push_reports`
  13. _reportsByRangeSQL = `SELECT id,app_id,platform_id,mid,buvid,device_token,build,time_zone,notify_switch,device_brand,device_model,os_version,extra FROM push_reports WHERE id>? and id<? and dtime=0`
  14. // for 全量推送
  15. _reportsTaskAllByRangeSQL = `SELECT platform_id,device_token,build FROM push_reports WHERE id>? and id<=? and app_id=? and dtime=0 and notify_switch=1`
  16. )
  17. // BeginTx begin transaction.
  18. func (d *Dao) BeginTx(c context.Context) (*xsql.Tx, error) {
  19. return d.db.Begin(c)
  20. }
  21. // DelCallbacks deletes callbacks.
  22. func (d *Dao) DelCallbacks(c context.Context, t time.Time, limit int) (rows int64, err error) {
  23. res, err := d.delCallbacksStmt.Exec(c, t, limit)
  24. if err != nil {
  25. log.Error("d.DelCallbacks(%v) error(%v)", t, err)
  26. PromError("mysql:DelCallbacks")
  27. return
  28. }
  29. rows, err = res.RowsAffected()
  30. return
  31. }
  32. // ReportLastID gets the latest ID of report database record.
  33. func (d *Dao) ReportLastID(c context.Context) (id int64, err error) {
  34. if err = d.reportLastIDStmt.QueryRow(c).Scan(&id); err != nil {
  35. if err == sql.ErrNoRows {
  36. return
  37. }
  38. log.Error("d.ReportLastID() error(%v)", err)
  39. PromError("mysql:ReportLastID")
  40. }
  41. return
  42. }
  43. // ReportsByRange gets reports by id range.
  44. func (d *Dao) ReportsByRange(c context.Context, min, max int64) (rs []*pushmdl.Report, err error) {
  45. rows, err := d.reportsByRangeStmt.Query(c, min, max)
  46. if err != nil {
  47. return
  48. }
  49. defer rows.Close()
  50. for rows.Next() {
  51. r := &pushmdl.Report{}
  52. if err = rows.Scan(&r.ID, &r.APPID, &r.PlatformID, &r.Mid, &r.Buvid, &r.DeviceToken,
  53. &r.Build, &r.TimeZone, &r.NotifySwitch, &r.DeviceBrand, &r.DeviceModel, &r.OSVersion, &r.Extra); err != nil {
  54. log.Error("d.ReportsByRange Scan() error(%v)", err)
  55. PromError("mysql:ReportsByRange")
  56. return
  57. }
  58. rs = append(rs, r)
  59. }
  60. return
  61. }
  62. // ReportsTaskAll gets reports by range
  63. func (d *Dao) ReportsTaskAll(c context.Context, min, max, app int64) (rows *xsql.Rows, err error) {
  64. if rows, err = d.db.Query(c, _reportsTaskAllByRangeSQL, min, max, app); err != nil {
  65. log.Error("ReportsTaskAll load reports start(%d) end(%d) error(%v)", min, max, err)
  66. }
  67. return
  68. }