config.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. }
  79. return myth.ReplyErr(status, reply.LastErr.Error())
  80. }
  81. return myth.ReplyOk()
  82. }