dbmethods.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package common
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "os"
  7. "time"
  8. )
  9. type DBMethods struct {
  10. DB *sql.DB
  11. Debug bool
  12. Driver string
  13. }
  14. func (d *DBMethods) fixQuery(query string) string {
  15. if d.Driver == "mysql" {
  16. return fixQuery(query)
  17. }
  18. return query
  19. }
  20. func (d *DBMethods) log(fname string, start time.Time, err error, tx bool, query string, args ...any) {
  21. if d.Debug {
  22. log(os.Stdout, fname, start, err, tx, query, args...)
  23. }
  24. }
  25. func (d *DBMethods) Begin(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
  26. start := time.Now()
  27. tx, err := d.DB.BeginTx(ctx, opts)
  28. d.log("Begin", start, err, true, "")
  29. return &Tx{tx, d.Debug, d.Driver, start}, err
  30. }
  31. func (d *DBMethods) Close() error {
  32. start := time.Now()
  33. err := d.DB.Close()
  34. d.log("Close", start, err, false, "")
  35. return err
  36. }
  37. func (d *DBMethods) Each(ctx context.Context, query string, callback func(ctx context.Context, rows *Rows) error, args ...any) error {
  38. if callback == nil {
  39. return fmt.Errorf("callback is not set")
  40. }
  41. rows, err := d.Query(ctx, query, args...)
  42. if err != nil {
  43. return err
  44. }
  45. defer rows.Close()
  46. for rows.Next() {
  47. select {
  48. case <-ctx.Done():
  49. return ctx.Err()
  50. default:
  51. if err := callback(ctx, rows); err != nil {
  52. return err
  53. }
  54. }
  55. }
  56. if err := rows.Err(); err != nil {
  57. return err
  58. }
  59. return nil
  60. }
  61. func (d *DBMethods) Exec(ctx context.Context, query string, args ...any) (sql.Result, error) {
  62. start := time.Now()
  63. res, err := d.DB.ExecContext(ctx, d.fixQuery(query), args...)
  64. d.log("Exec", start, err, false, d.fixQuery(query), args...)
  65. return res, err
  66. }
  67. func (d *DBMethods) Ping(ctx context.Context) error {
  68. start := time.Now()
  69. err := d.DB.PingContext(ctx)
  70. d.log("Ping", start, err, false, "")
  71. return err
  72. }
  73. func (d *DBMethods) Prepare(ctx context.Context, query string) (*sql.Stmt, error) {
  74. start := time.Now()
  75. stm, err := d.DB.PrepareContext(ctx, d.fixQuery(query))
  76. d.log("Prepare", start, err, false, d.fixQuery(query))
  77. return stm, err
  78. }
  79. func (d *DBMethods) Query(ctx context.Context, query string, args ...any) (*Rows, error) {
  80. start := time.Now()
  81. rows, err := d.DB.QueryContext(ctx, d.fixQuery(query), args...)
  82. d.log("Query", start, err, false, d.fixQuery(query), args...)
  83. return &Rows{Rows: rows}, err
  84. }
  85. func (d *DBMethods) QueryRow(ctx context.Context, query string, args ...any) *Row {
  86. start := time.Now()
  87. row := d.DB.QueryRowContext(ctx, d.fixQuery(query), args...)
  88. d.log("QueryRow", start, nil, false, d.fixQuery(query), args...)
  89. return &Row{Row: row}
  90. }
  91. func (d *DBMethods) SetConnMaxLifetime(t time.Duration) {
  92. start := time.Now()
  93. d.DB.SetConnMaxLifetime(t)
  94. d.log("SetConnMaxLifetime", start, nil, false, "")
  95. }
  96. func (d *DBMethods) SetMaxIdleConns(n int) {
  97. start := time.Now()
  98. d.DB.SetMaxIdleConns(n)
  99. d.log("SetMaxIdleConns", start, nil, false, "")
  100. }
  101. func (d *DBMethods) SetMaxOpenConns(n int) {
  102. start := time.Now()
  103. d.DB.SetMaxOpenConns(n)
  104. d.log("SetMaxOpenConns", start, nil, false, "")
  105. }
  106. func (d *DBMethods) Transaction(ctx context.Context, callback func(ctx context.Context, tx *Tx) error) error {
  107. if callback == nil {
  108. return fmt.Errorf("callback is not set")
  109. }
  110. tx, err := d.Begin(ctx, nil)
  111. if err != nil {
  112. return err
  113. }
  114. if err := callback(ctx, tx); err != nil {
  115. rerr := tx.Rollback()
  116. if rerr != nil {
  117. return fmt.Errorf(
  118. "%v: %v",
  119. rerr,
  120. err,
  121. )
  122. }
  123. return err
  124. }
  125. return tx.Commit()
  126. }