reply.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package db
  2. import "git.chuangxin1.com/cx/myth"
  3. // Reply db exec return insert/update/delete
  4. type Reply struct {
  5. OK bool
  6. Err error
  7. LastErr error
  8. ErrCode int
  9. LastID int64
  10. RowsAffected int64
  11. }
  12. // Ok check reply true/false
  13. func (r Reply) Ok() bool {
  14. return r.OK
  15. }
  16. // ReplyOk exec ok
  17. func ReplyOk(rowsAffected, lastID int64) Reply {
  18. var reply Reply
  19. reply.OK = true
  20. reply.ErrCode = 0
  21. reply.LastID = lastID
  22. reply.RowsAffected = rowsAffected
  23. return reply
  24. }
  25. // ReplyFaild exec faild
  26. func ReplyFaild(errCode int, err, errText error) (reply Reply) {
  27. reply.OK = false
  28. reply.ErrCode = errCode
  29. reply.LastID = -1
  30. reply.RowsAffected = -1
  31. reply.Err = err
  32. reply.LastErr = errText
  33. return
  34. }
  35. // ReplyToReplyData db reply to response
  36. func ReplyToReplyData(reply Reply) *myth.ReplyData {
  37. status := myth.ErrOk
  38. if !reply.OK {
  39. switch reply.ErrCode {
  40. case ErrException:
  41. status = myth.ErrException
  42. case ErrExists:
  43. status = myth.ErrDataExists
  44. case ErrNotFound:
  45. status = myth.ErrDataNotFound
  46. case ErrDataNotFound:
  47. status = myth.ErrDataNotFound
  48. case ErrAuthorized:
  49. status = myth.ErrUnAuthorized
  50. case ErrNotConnect:
  51. status = myth.ErrNotFound
  52. case ErrExpired:
  53. status = myth.ErrNotAllowed
  54. case ErrInsufficientBalance:
  55. status = myth.ErrNotAllowed
  56. case ErrCreditLimit:
  57. status = myth.ErrNotAllowed
  58. default:
  59. status = myth.ErrException
  60. }
  61. return myth.ReplyErr(status, reply.LastErr.Error())
  62. }
  63. return myth.ReplyOk()
  64. }