config.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. )
  15. // Config config
  16. type Config struct {
  17. Driver string
  18. DNS string
  19. MaxOpenConns int
  20. MaxIdle int
  21. MaxLifetime time.Duration
  22. }
  23. // Reply db exec return insert/update/delete
  24. type Reply struct {
  25. OK bool
  26. Err error
  27. LastErr error
  28. ErrCode int
  29. LastID int64
  30. RowsAffected int64
  31. }
  32. // ReplyOk exec ok
  33. func ReplyOk(rowsAffected, lastID int64) Reply {
  34. var reply Reply
  35. reply.OK = true
  36. reply.ErrCode = 0
  37. reply.LastID = lastID
  38. reply.RowsAffected = rowsAffected
  39. return reply
  40. }
  41. // ReplyFaild exec faild
  42. func ReplyFaild(errCode int, err, errText error) (reply Reply) {
  43. reply.OK = false
  44. reply.ErrCode = errCode
  45. reply.LastID = -1
  46. reply.RowsAffected = -1
  47. reply.Err = err
  48. reply.LastErr = errText
  49. return
  50. }
  51. // ReplyToReplyData db reply to response
  52. func ReplyToReplyData(reply Reply) *myth.ReplyData {
  53. status := myth.ErrOk
  54. if !reply.OK {
  55. switch reply.ErrCode {
  56. case ErrException:
  57. status = myth.ErrException
  58. case ErrExists:
  59. status = myth.ErrDataExists
  60. case ErrNotFound:
  61. status = myth.ErrDataNotFound
  62. case ErrAuthorized:
  63. status = myth.ErrUnAuthorized
  64. case ErrNotConnect:
  65. status = myth.ErrNotFound
  66. }
  67. return myth.ReplyErr(status, reply.LastErr.Error())
  68. }
  69. return myth.ReplyOk()
  70. }