config.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 ErrAuthorized:
  67. status = myth.ErrUnAuthorized
  68. case ErrNotConnect:
  69. status = myth.ErrNotFound
  70. }
  71. return myth.ReplyErr(status, reply.LastErr.Error())
  72. }
  73. return myth.ReplyOk()
  74. }