db.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package toolkit
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "sync"
  6. "time"
  7. //
  8. _ "github.com/go-sql-driver/mysql"
  9. "github.com/jmoiron/sqlx"
  10. )
  11. // DbConfig config
  12. type DbConfig struct {
  13. Driver string
  14. DNS string
  15. MaxOpenConns int
  16. MaxIdle int
  17. MaxLifetime time.Duration
  18. }
  19. var (
  20. config DbConfig
  21. db *sqlx.DB
  22. once sync.Once
  23. )
  24. // DB define
  25. type DB struct {
  26. conn *sqlx.DB
  27. tx *sqlx.Tx
  28. }
  29. // SetDbConfig set
  30. func SetDbConfig(cfg DbConfig) {
  31. config.Driver = cfg.Driver
  32. config.DNS = cfg.DNS
  33. config.MaxOpenConns = cfg.MaxOpenConns
  34. config.MaxIdle = cfg.MaxIdle
  35. config.MaxLifetime = cfg.MaxLifetime * time.Second
  36. }
  37. // NewDB new DB object
  38. func NewDB() *DB {
  39. return &DB{}
  40. }
  41. // FreeDB free db connect
  42. func FreeDB() {
  43. if db != nil {
  44. db.Close()
  45. }
  46. }
  47. // ErrNoRows check norows error
  48. func ErrNoRows(err error) bool {
  49. if err == sql.ErrNoRows {
  50. return true
  51. }
  52. return false
  53. }
  54. func connect() (*sqlx.DB, error) {
  55. if db != nil {
  56. return db, nil
  57. }
  58. var err error
  59. once.Do(func() {
  60. db, err = sqlx.Connect(config.Driver, config.DNS)
  61. if err == nil {
  62. db.DB.SetMaxOpenConns(config.MaxOpenConns)
  63. db.DB.SetMaxIdleConns(config.MaxIdle)
  64. db.DB.SetConnMaxLifetime(config.MaxLifetime)
  65. db.Ping()
  66. }
  67. })
  68. return db, err
  69. }
  70. // Connect connect to database
  71. func (d *DB) Connect() (err error) {
  72. d.conn, err = connect()
  73. return
  74. }
  75. // Close close database connect
  76. func (d *DB) Close() {
  77. //d.conn.Close()
  78. }
  79. // BeginTrans begin trans
  80. func (d *DB) BeginTrans() {
  81. d.tx = d.conn.MustBegin()
  82. }
  83. // Commit commit
  84. func (d *DB) Commit() error {
  85. return d.tx.Commit()
  86. }
  87. // Rollback rollback
  88. func (d *DB) Rollback() error {
  89. return d.tx.Rollback()
  90. }
  91. // TransExec trans execute
  92. func (d *DB) TransExec(query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
  93. if rs, err := d.tx.NamedExec(query, args); err == nil {
  94. RowsAffected, _ = rs.RowsAffected()
  95. LastInsertId, _ = rs.LastInsertId()
  96. }
  97. return
  98. }
  99. // Rows get rows
  100. func (d *DB) Rows(dest interface{}, query string, args interface{}) error {
  101. err := d.Connect()
  102. if err != nil {
  103. return err
  104. }
  105. defer d.Close()
  106. nstmt, err := d.conn.PrepareNamed(query)
  107. if err != nil {
  108. return err
  109. }
  110. defer nstmt.Close()
  111. err = nstmt.Select(dest, args)
  112. return err
  113. }
  114. // Row get row
  115. func (d *DB) Row(dest interface{}, query string, args interface{}) error {
  116. err := d.Connect()
  117. if err != nil {
  118. return err
  119. }
  120. defer d.Close()
  121. nstmt, err := d.conn.PrepareNamed(query)
  122. if err != nil {
  123. return err
  124. }
  125. defer nstmt.Close()
  126. err = nstmt.Get(dest, args)
  127. return err
  128. }
  129. // Insert insert into
  130. func (d *DB) Insert(query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
  131. err = d.Connect()
  132. if err != nil {
  133. return
  134. }
  135. defer d.Close()
  136. if rs, err := d.conn.NamedExec(query, args); err == nil {
  137. LastInsertId, _ = rs.LastInsertId()
  138. RowsAffected, _ = rs.RowsAffected()
  139. }
  140. return
  141. }
  142. // Update update/delete
  143. func (d *DB) Update(query string, args interface{}) (RowsAffected int64, err error) {
  144. err = d.Connect()
  145. if err != nil {
  146. return
  147. }
  148. defer d.Close()
  149. if rs, err := d.conn.NamedExec(query, args); err == nil {
  150. RowsAffected, _ = rs.RowsAffected()
  151. }
  152. return
  153. }
  154. // Limit MySQL limit
  155. func (d *DB) Limit(page, pagesize int) string {
  156. return fmt.Sprintf(" limit %d, %d", (page-1)*pagesize, pagesize)
  157. }