config.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. MaxLifetime time.Duration
  26. }
  27. // Reply db exec return insert/update/delete
  28. type Reply struct {
  29. OK bool
  30. Err error
  31. LastErr error
  32. ErrCode int
  33. LastID int64
  34. RowsAffected int64
  35. }
  36. // ReplyOk exec ok
  37. func ReplyOk(rowsAffected, lastID int64) Reply {
  38. var reply Reply
  39. reply.OK = true
  40. reply.ErrCode = 0
  41. reply.LastID = lastID
  42. reply.RowsAffected = rowsAffected
  43. return reply
  44. }
  45. // ReplyFaild exec faild
  46. func ReplyFaild(errCode int, err, errText error) (reply Reply) {
  47. reply.OK = false
  48. reply.ErrCode = errCode
  49. reply.LastID = -1
  50. reply.RowsAffected = -1
  51. reply.Err = err
  52. reply.LastErr = errText
  53. return
  54. }
  55. // ReplyToReplyData db reply to response
  56. func ReplyToReplyData(reply Reply) *myth.ReplyData {
  57. status := myth.ErrOk
  58. if !reply.OK {
  59. switch reply.ErrCode {
  60. case ErrException:
  61. status = myth.ErrException
  62. case ErrExists:
  63. status = myth.ErrDataExists
  64. case ErrNotFound:
  65. status = myth.ErrDataNotFound
  66. case ErrDataNotFound:
  67. status = myth.ErrDataNotFound
  68. case ErrAuthorized:
  69. status = myth.ErrUnAuthorized
  70. case ErrNotConnect:
  71. status = myth.ErrNotFound
  72. case ErrExpired:
  73. status = myth.ErrNotAllowed
  74. case ErrInsufficientBalance:
  75. status = myth.ErrNotAllowed
  76. case ErrCreditLimit:
  77. status = myth.ErrNotAllowed
  78. default:
  79. status = myth.ErrException
  80. }
  81. return myth.ReplyErr(status, reply.LastErr.Error())
  82. }
  83. return myth.ReplyOk()
  84. }