package db import ( "time" "git.chuangxin1.com/cx/myth" ) // DB error code const ( ErrOK = 0 // ErrOK ok ErrException = 1 // ErrException exception ErrExists = 2 // ErrExists exists ErrNotFound = 3 // ErrNotFound not found ErrAuthorized = 4 // ErrAuthorized authorized ErrNotConnect = 5 // ErrNotConnect connect error ) // Config config type Config struct { Driver string DNS string MaxOpenConns int MaxIdle int MaxLifetime time.Duration } // Reply db exec return insert/update/delete type Reply struct { OK bool Err error LastErr error ErrCode int LastID int64 RowsAffected int64 } // ReplyOk exec ok func ReplyOk(rowsAffected, lastID int64) Reply { var reply Reply reply.OK = true reply.ErrCode = 0 reply.LastID = lastID reply.RowsAffected = rowsAffected return reply } // ReplyFaild exec faild 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 } // ReplyToReplyData db reply to response 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 ErrAuthorized: status = myth.ErrUnAuthorized case ErrNotConnect: status = myth.ErrNotFound } return myth.ReplyErr(status, reply.LastErr.Error()) } return myth.ReplyOk() }