mysql.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package dao
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "fmt"
  6. "go-common/app/job/main/identify/model"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _newSessionBinByteLen = 16
  12. _getCookieDeletedSQL = "SELECT id,mid,session FROM user_cookie_deleted_%s where id > ? limit ?"
  13. _getTokenDeletedSQL = "SELECT id,mid,token FROM user_token_deleted_%s where id > ? limit ?"
  14. )
  15. // CookieDeleted get cookie deleted
  16. func (d *Dao) CookieDeleted(c context.Context, start, count int64, suffix string) (res []*model.AuthCookie, err error) {
  17. var rows *xsql.Rows
  18. if rows, err = d.authDB.Query(c, fmt.Sprintf(_getCookieDeletedSQL, suffix), start, count); err != nil {
  19. log.Error("fail to get CookieDeleted, dao.authDB.Query(%s) error(%v)", _getCookieDeletedSQL, err)
  20. return
  21. }
  22. defer rows.Close()
  23. for rows.Next() {
  24. var session []byte
  25. a := new(model.AuthCookie)
  26. if err = rows.Scan(&a.ID, &a.Mid, &session); err != nil {
  27. log.Error("row.Scan() error(%v)", err)
  28. res = nil
  29. return
  30. }
  31. a.Session = encodeSession(session)
  32. res = append(res, a)
  33. }
  34. return
  35. }
  36. // TokenDeleted get token deleted
  37. func (d *Dao) TokenDeleted(c context.Context, start, count int64, suffix string) (res []*model.AuthToken, err error) {
  38. var rows *xsql.Rows
  39. if rows, err = d.authDB.Query(c, fmt.Sprintf(_getTokenDeletedSQL, suffix), start, count); err != nil {
  40. log.Error("fail to get TokenDeleted, dao.authDB.Query(%s) error(%v)", _getTokenDeletedSQL, err)
  41. return
  42. }
  43. defer rows.Close()
  44. for rows.Next() {
  45. var token []byte
  46. a := new(model.AuthToken)
  47. if err = rows.Scan(&a.ID, &a.Mid, &token); err != nil {
  48. log.Error("row.Scan() error(%v)", err)
  49. res = nil
  50. return
  51. }
  52. a.Token = hex.EncodeToString(token)
  53. res = append(res, a)
  54. }
  55. return
  56. }
  57. func encodeSession(b []byte) (s string) {
  58. // format new
  59. if len(b) == _newSessionBinByteLen {
  60. return hex.EncodeToString(b)
  61. }
  62. // or format old
  63. return string(b)
  64. }