db.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package db
  2. import (
  3. "database/sql"
  4. "errors"
  5. "fmt"
  6. "sync"
  7. "time"
  8. // mysql
  9. _ "github.com/go-sql-driver/mysql"
  10. "github.com/jmoiron/sqlx"
  11. )
  12. var (
  13. config Config
  14. db *sqlx.DB
  15. err error
  16. once sync.Once
  17. )
  18. // DB define
  19. type DB struct {
  20. conn *sqlx.DB
  21. tx *sqlx.Tx
  22. }
  23. // SetConfig set
  24. func SetConfig(cfg Config) {
  25. config.Driver = cfg.Driver
  26. config.DNS = cfg.DNS
  27. config.MaxOpenConns = cfg.MaxOpenConns
  28. config.MaxIdle = cfg.MaxIdle
  29. config.MaxLifetime = cfg.MaxLifetime * time.Second
  30. }
  31. // New new DB object
  32. func New() *DB {
  33. return &DB{}
  34. }
  35. // Release free db connect
  36. func Release() {
  37. if db != nil {
  38. db.Close()
  39. }
  40. }
  41. // NewConfig new DB dynamic object
  42. func NewConfig(config Config) (dbx *DB, err error) {
  43. dbx = &DB{}
  44. dbx.conn, err = sqlx.Connect(config.Driver, config.DNS)
  45. if err == nil {
  46. dbx.conn.SetMaxOpenConns(config.MaxOpenConns)
  47. dbx.conn.SetMaxIdleConns(config.MaxIdle)
  48. dbx.conn.SetConnMaxLifetime(config.MaxLifetime)
  49. dbx.conn.Ping()
  50. }
  51. return
  52. }
  53. // ReleaseConfig free db connect
  54. func ReleaseConfig(dbx *DB) {
  55. if dbx.conn != nil {
  56. dbx.conn.Close()
  57. }
  58. }
  59. func connect() (dbx *sqlx.DB, err error) {
  60. once.Do(func() {
  61. db, err = sqlx.Connect(config.Driver, config.DNS)
  62. if err == nil {
  63. db.DB.SetMaxOpenConns(config.MaxOpenConns)
  64. db.DB.SetMaxIdleConns(config.MaxIdle)
  65. db.DB.SetConnMaxLifetime(config.MaxLifetime)
  66. db.Ping()
  67. }
  68. })
  69. dbx = db
  70. return
  71. }
  72. // Connect connect to database
  73. func (d *DB) Connect() (err error) {
  74. if d.conn != nil {
  75. return
  76. }
  77. d.conn, err = connect()
  78. return
  79. }
  80. // Close close database connect
  81. func (d *DB) Close() {
  82. //d.conn.Close()
  83. }
  84. // BeginTrans begin trans
  85. func (d *DB) BeginTrans() (err error) {
  86. d.conn, err = connect()
  87. if err != nil {
  88. return
  89. }
  90. d.tx = d.conn.MustBegin()
  91. return
  92. }
  93. // Commit commit
  94. func (d *DB) Commit() error {
  95. return d.tx.Commit()
  96. }
  97. // Rollback rollback
  98. func (d *DB) Rollback() error {
  99. return d.tx.Rollback()
  100. }
  101. // TransExec trans execute
  102. func (d *DB) TransExec(query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
  103. if rs, err := d.tx.NamedExec(query, args); err == nil {
  104. RowsAffected, _ = rs.RowsAffected()
  105. LastInsertId, _ = rs.LastInsertId()
  106. }
  107. return
  108. }
  109. // TransUpdate trans update
  110. func (d *DB) TransUpdate(query string, args interface{}) (reply Reply) {
  111. var (
  112. err error
  113. rs sql.Result
  114. )
  115. if rs, err = d.tx.NamedExec(query, args); err == nil {
  116. a, _ := rs.RowsAffected()
  117. reply = ReplyOk(a, 0)
  118. } else {
  119. reply = ReplyFaild(ErrException, err, errors.New(`数据执行错误`))
  120. }
  121. return
  122. }
  123. // Rows get rows
  124. func (d *DB) Rows(dest interface{}, query string, args interface{}) error {
  125. err := d.Connect()
  126. if err != nil {
  127. return err
  128. }
  129. defer d.Close()
  130. nstmt, err := d.conn.PrepareNamed(query)
  131. if err != nil {
  132. return err
  133. }
  134. defer nstmt.Close()
  135. err = nstmt.Select(dest, args)
  136. return err
  137. }
  138. // Row get row
  139. func (d *DB) Row(dest interface{}, query string, args interface{}) error {
  140. err := d.Connect()
  141. if err != nil {
  142. return err
  143. }
  144. defer d.Close()
  145. nstmt, err := d.conn.PrepareNamed(query)
  146. if err != nil {
  147. return err
  148. }
  149. defer nstmt.Close()
  150. err = nstmt.Get(dest, args)
  151. return err
  152. }
  153. // InsertReply insert and return DbReply
  154. func (d *DB) InsertReply(query string, args interface{}) (reply Reply) {
  155. var (
  156. err error
  157. rs sql.Result
  158. )
  159. err = d.Connect()
  160. if err != nil {
  161. reply = ReplyFaild(ErrNotConnect, err, errors.New(`数据库连接错误`))
  162. return
  163. }
  164. defer d.Close()
  165. if rs, err = d.conn.NamedExec(query, args); err == nil {
  166. a, _ := rs.RowsAffected()
  167. n, _ := rs.LastInsertId()
  168. reply = ReplyOk(a, n)
  169. } else {
  170. reply = ReplyFaild(ErrException, err, errors.New(`数据执行错误`))
  171. }
  172. return
  173. }
  174. // UpdateReply update/delete and return DbReply
  175. func (d *DB) UpdateReply(query string, args interface{}) (reply Reply) {
  176. var (
  177. err error
  178. rs sql.Result
  179. )
  180. err = d.Connect()
  181. if err != nil {
  182. reply = ReplyFaild(ErrNotConnect, err, errors.New(`数据库连接错误`))
  183. return
  184. }
  185. defer d.Close()
  186. if rs, err = d.conn.NamedExec(query, args); err == nil {
  187. a, _ := rs.RowsAffected()
  188. reply = ReplyOk(a, 0)
  189. } else {
  190. reply = ReplyFaild(ErrException, err, errors.New(`数据执行错误`))
  191. }
  192. return
  193. }
  194. // Insert insert into
  195. func (d *DB) Insert(query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
  196. err = d.Connect()
  197. if err != nil {
  198. return
  199. }
  200. defer d.Close()
  201. if rs, err := d.conn.NamedExec(query, args); err == nil {
  202. LastInsertId, _ = rs.LastInsertId()
  203. RowsAffected, _ = rs.RowsAffected()
  204. }
  205. return
  206. }
  207. // Update update/delete
  208. func (d *DB) Update(query string, args interface{}) (RowsAffected int64, err error) {
  209. err = d.Connect()
  210. if err != nil {
  211. return
  212. }
  213. defer d.Close()
  214. if rs, err := d.conn.NamedExec(query, args); err == nil {
  215. RowsAffected, _ = rs.RowsAffected()
  216. }
  217. return
  218. }
  219. // Limit MySQL limit
  220. func (d *DB) Limit(page, pagesize int) string {
  221. return fmt.Sprintf(" limit %d, %d", (page-1)*pagesize, pagesize)
  222. }