dbmethods.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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) error {
  38. if callback == nil {
  39. return fmt.Errorf("callback is not set")
  40. }
  41. rows, err := d.Query(ctx, query)
  42. if err != nil {
  43. return err
  44. }
  45. defer rows.Close()
  46. for rows.Next() {
  47. if err := callback(ctx, rows); err != nil {
  48. return err
  49. }
  50. }
  51. if err := rows.Err(); err != nil {
  52. return err
  53. }
  54. return nil
  55. }
  56. func (d *DBMethods) Exec(ctx context.Context, query string, args ...any) (sql.Result, error) {
  57. start := time.Now()
  58. res, err := d.DB.ExecContext(ctx, d.fixQuery(query), args...)
  59. d.log("Exec", start, err, false, d.fixQuery(query), args...)
  60. return res, err
  61. }
  62. func (d *DBMethods) Ping(ctx context.Context) error {
  63. start := time.Now()
  64. err := d.DB.PingContext(ctx)
  65. d.log("Ping", start, err, false, "")
  66. return err
  67. }
  68. func (d *DBMethods) Prepare(ctx context.Context, query string) (*sql.Stmt, error) {
  69. start := time.Now()
  70. stm, err := d.DB.PrepareContext(ctx, d.fixQuery(query))
  71. d.log("Prepare", start, err, false, d.fixQuery(query))
  72. return stm, err
  73. }
  74. func (d *DBMethods) Query(ctx context.Context, query string, args ...any) (*Rows, error) {
  75. start := time.Now()
  76. rows, err := d.DB.QueryContext(ctx, d.fixQuery(query), args...)
  77. d.log("Query", start, err, false, d.fixQuery(query), args...)
  78. return &Rows{Rows: rows}, err
  79. }
  80. func (d *DBMethods) QueryRow(ctx context.Context, query string, args ...any) *Row {
  81. start := time.Now()
  82. row := d.DB.QueryRowContext(ctx, d.fixQuery(query), args...)
  83. d.log("QueryRow", start, nil, false, d.fixQuery(query), args...)
  84. return &Row{Row: row}
  85. }
  86. func (d *DBMethods) SetConnMaxLifetime(t time.Duration) {
  87. d.DB.SetConnMaxLifetime(t)
  88. }
  89. func (d *DBMethods) SetMaxIdleConns(n int) {
  90. d.DB.SetMaxIdleConns(n)
  91. }
  92. func (d *DBMethods) SetMaxOpenConns(n int) {
  93. d.DB.SetMaxOpenConns(n)
  94. }
  95. func (d *DBMethods) Transaction(ctx context.Context, callback func(ctx context.Context, tx *Tx) error) error {
  96. if callback == nil {
  97. return fmt.Errorf("callback is not set")
  98. }
  99. tx, err := d.Begin(ctx, nil)
  100. if err != nil {
  101. return err
  102. }
  103. if err := callback(ctx, tx); err != nil {
  104. rerr := tx.Rollback()
  105. if rerr != nil {
  106. return fmt.Errorf(
  107. "%v: %v",
  108. rerr,
  109. err,
  110. )
  111. }
  112. return err
  113. }
  114. return tx.Commit()
  115. }