12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package db
- import (
- "time"
- "git.chuangxin1.com/cx/myth"
- )
- const (
- ErrOK = 0
- ErrException = 1
- ErrExists = 2
- ErrNotFound = 3
- ErrAuthorized = 4
- ErrNotConnect = 5
- ErrDataNotFound = 6
- ErrExpired = 7
- ErrInsufficientBalance = 8
- ErrCreditLimit = 9
- )
- type Config struct {
- Driver string
- DNS string
- MaxOpenConns int
- MaxIdle int
- MaxLifetime time.Duration
- }
- type Reply struct {
- OK bool
- Err error
- LastErr error
- ErrCode int
- LastID int64
- RowsAffected int64
- }
- func ReplyOk(rowsAffected, lastID int64) Reply {
- var reply Reply
- reply.OK = true
- reply.ErrCode = 0
- reply.LastID = lastID
- reply.RowsAffected = rowsAffected
- return reply
- }
- func ReplyFaild(errCode int, err, errText error) (reply Reply) {
- reply.OK = false
- reply.ErrCode = errCode
- reply.LastID = -1
- reply.RowsAffected = -1
- reply.Err = err
- reply.LastErr = errText
- return
- }
- func ReplyToReplyData(reply Reply) *myth.ReplyData {
- status := myth.ErrOk
- if !reply.OK {
- switch reply.ErrCode {
- case ErrException:
- status = myth.ErrException
- case ErrExists:
- status = myth.ErrDataExists
- case ErrNotFound:
- status = myth.ErrDataNotFound
- case ErrDataNotFound:
- status = myth.ErrDataNotFound
- case ErrAuthorized:
- status = myth.ErrUnAuthorized
- case ErrNotConnect:
- status = myth.ErrNotFound
- case ErrExpired:
- status = myth.ErrNotAllowed
- case ErrInsufficientBalance:
- status = myth.ErrNotAllowed
- case ErrCreditLimit:
- status = myth.ErrNotAllowed
- default:
- status = myth.ErrException
- }
- return myth.ReplyErr(status, reply.LastErr.Error())
- }
- return myth.ReplyOk()
- }
|