config.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package db
  2. import (
  3. "time"
  4. "git.chuangxin1.com/cx/myth"
  5. )
  6. // DB error code
  7. const (
  8. ErrOK = 0 // ErrOK ok
  9. ErrException = 1 // ErrException exception 异常
  10. ErrExists = 2 // ErrExists exists 数据存在
  11. ErrNotFound = 3 // ErrNotFound not found 未找到关键数据
  12. ErrAuthorized = 4 // ErrAuthorized authorized 未认证
  13. ErrNotConnect = 5 // ErrNotConnect connect error 数据库内部连接错误
  14. ErrDataNotFound = 6 // data not found 数据未找到
  15. ErrExpired = 7 // expired 已过期
  16. ErrInsufficientBalance = 8 // insufficient balance 余额不足
  17. ErrCreditLimit = 9 // Credit Limit 超额
  18. )
  19. // Config config
  20. type Config struct {
  21. Driver string
  22. DNS string
  23. MaxOpenConns int
  24. MaxIdle int
  25. MaxIdleTime time.Duration
  26. MaxLifeTime time.Duration
  27. }
  28. // Reply db exec return insert/update/delete
  29. type Reply struct {
  30. OK bool
  31. Err error
  32. LastErr error
  33. ErrCode int
  34. LastID int64
  35. RowsAffected int64
  36. }
  37. // ReplyOk exec ok
  38. func ReplyOk(rowsAffected, lastID int64) Reply {
  39. var reply Reply
  40. reply.OK = true
  41. reply.ErrCode = 0
  42. reply.LastID = lastID
  43. reply.RowsAffected = rowsAffected
  44. return reply
  45. }
  46. // ReplyFaild exec faild
  47. func ReplyFaild(errCode int, err, errText error) (reply Reply) {
  48. reply.OK = false
  49. reply.ErrCode = errCode
  50. reply.LastID = -1
  51. reply.RowsAffected = -1
  52. reply.Err = err
  53. reply.LastErr = errText
  54. return
  55. }
  56. // ReplyToReplyData db reply to response
  57. func ReplyToReplyData(reply Reply) *myth.ReplyData {
  58. status := myth.ErrOk
  59. if !reply.OK {
  60. switch reply.ErrCode {
  61. case ErrException:
  62. status = myth.ErrException
  63. case ErrExists:
  64. status = myth.ErrDataExists
  65. case ErrNotFound:
  66. status = myth.ErrDataNotFound
  67. case ErrDataNotFound:
  68. status = myth.ErrDataNotFound
  69. case ErrAuthorized:
  70. status = myth.ErrUnAuthorized
  71. case ErrNotConnect:
  72. status = myth.ErrNotFound
  73. case ErrExpired:
  74. status = myth.ErrNotAllowed
  75. case ErrInsufficientBalance:
  76. status = myth.ErrNotAllowed
  77. case ErrCreditLimit:
  78. status = myth.ErrNotAllowed
  79. default:
  80. status = myth.ErrException
  81. }
  82. return myth.ReplyErr(status, reply.LastErr.Error())
  83. }
  84. return myth.ReplyOk()
  85. }