sqlw.go 2.7 KB

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