sqlw.go 2.5 KB

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