sqlw.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package sqlw
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql"
  5. "errors"
  6. "time"
  7. "golang-fave/consts"
  8. )
  9. type Rows = sql.Rows
  10. type DB struct {
  11. db *sql.DB
  12. }
  13. var ErrNoRows = sql.ErrNoRows
  14. func Open(driverName, dataSourceName string) (*DB, error) {
  15. db, err := sql.Open(driverName, dataSourceName)
  16. if err != nil {
  17. if consts.ParamDebug {
  18. log("[CM] OPEN", time.Now(), err, true)
  19. }
  20. return nil, err
  21. }
  22. if consts.ParamDebug {
  23. log("[CM] OPEN", time.Now(), err, true)
  24. }
  25. return &DB{db: db}, err
  26. }
  27. func (this *DB) Close() error {
  28. if consts.ParamDebug {
  29. err := this.db.Close()
  30. log("[CM] CLOSE", time.Now(), err, true)
  31. return err
  32. }
  33. return this.db.Close()
  34. }
  35. func (this *DB) Ping() error {
  36. if consts.ParamDebug {
  37. err := this.db.Ping()
  38. log("[CM] PING", time.Now(), err, true)
  39. return err
  40. }
  41. return this.db.Ping()
  42. }
  43. func (this *DB) SetConnMaxLifetime(d time.Duration) {
  44. this.db.SetConnMaxLifetime(d)
  45. }
  46. func (this *DB) SetMaxIdleConns(n int) {
  47. this.db.SetMaxIdleConns(n)
  48. }
  49. func (this *DB) SetMaxOpenConns(n int) {
  50. this.db.SetMaxOpenConns(n)
  51. }
  52. func (this *DB) QueryRow(query string, args ...interface{}) *sql.Row {
  53. if consts.ParamDebug {
  54. s := time.Now()
  55. r := this.db.QueryRow(query, args...)
  56. log(query, s, nil, false)
  57. return r
  58. }
  59. return this.db.QueryRow(query, args...)
  60. }
  61. func (this *DB) Begin() (*Tx, error) {
  62. tx, err := this.db.Begin()
  63. if err != nil {
  64. if consts.ParamDebug {
  65. log("[TX] TRANSACTION START", time.Now(), err, true)
  66. }
  67. return nil, err
  68. }
  69. if consts.ParamDebug {
  70. s := time.Now()
  71. log("[TX] TRANSACTION START", s, err, true)
  72. return &Tx{tx, s}, err
  73. }
  74. return &Tx{tx, time.Now()}, err
  75. }
  76. func (this *DB) Query(query string, args ...interface{}) (*sql.Rows, error) {
  77. if consts.ParamDebug {
  78. s := time.Now()
  79. r, e := this.db.Query(query, args...)
  80. log(query, s, e, false)
  81. return r, e
  82. }
  83. return this.db.Query(query, args...)
  84. }
  85. func (this *DB) Exec(query string, args ...interface{}) (sql.Result, error) {
  86. if consts.ParamDebug {
  87. s := time.Now()
  88. r, e := this.db.Exec(query, args...)
  89. log(query, s, e, false)
  90. return r, e
  91. }
  92. return this.db.Exec(query, args...)
  93. }
  94. func (this *DB) Transaction(queries func(tx *Tx) error) error {
  95. if queries == nil {
  96. return errors.New("queries is not set for transaction")
  97. }
  98. tx, err := this.Begin()
  99. if err != nil {
  100. return err
  101. }
  102. err = queries(tx)
  103. if err != nil {
  104. tx.Rollback()
  105. return err
  106. }
  107. return tx.Commit()
  108. }