dbmethods.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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) DeleteRowByID(ctx context.Context, id int64, row any) error {
  38. query := deleteRowByIDString(row)
  39. _, err := d.Exec(ctx, query, id)
  40. return err
  41. }
  42. func (d *DBMethods) Each(ctx context.Context, query string, callback func(ctx context.Context, rows *Rows) error, args ...any) error {
  43. if callback == nil {
  44. return fmt.Errorf("callback is not set")
  45. }
  46. rows, err := d.Query(ctx, query, args...)
  47. if err != nil {
  48. return err
  49. }
  50. defer rows.Close()
  51. for rows.Next() {
  52. select {
  53. case <-ctx.Done():
  54. return ctx.Err()
  55. default:
  56. if err := callback(ctx, rows); err != nil {
  57. return err
  58. }
  59. }
  60. }
  61. if err := rows.Err(); err != nil {
  62. return err
  63. }
  64. return nil
  65. }
  66. func (d *DBMethods) Exec(ctx context.Context, query string, args ...any) (sql.Result, error) {
  67. start := time.Now()
  68. res, err := d.DB.ExecContext(ctx, d.fixQuery(query), args...)
  69. d.log("Exec", start, err, false, d.fixQuery(query), args...)
  70. return res, err
  71. }
  72. func (d *DBMethods) Ping(ctx context.Context) error {
  73. start := time.Now()
  74. err := d.DB.PingContext(ctx)
  75. d.log("Ping", start, err, false, "")
  76. return err
  77. }
  78. func (d *DBMethods) Prepare(ctx context.Context, query string) (*sql.Stmt, error) {
  79. start := time.Now()
  80. stm, err := d.DB.PrepareContext(ctx, d.fixQuery(query))
  81. d.log("Prepare", start, err, false, d.fixQuery(query))
  82. return stm, err
  83. }
  84. func (d *DBMethods) Query(ctx context.Context, query string, args ...any) (*Rows, error) {
  85. start := time.Now()
  86. rows, err := d.DB.QueryContext(ctx, d.fixQuery(query), args...)
  87. d.log("Query", start, err, false, d.fixQuery(query), args...)
  88. return &Rows{Rows: rows}, err
  89. }
  90. func (d *DBMethods) QueryRow(ctx context.Context, query string, args ...any) *Row {
  91. start := time.Now()
  92. row := d.DB.QueryRowContext(ctx, d.fixQuery(query), args...)
  93. d.log("QueryRow", start, nil, false, d.fixQuery(query), args...)
  94. return &Row{Row: row}
  95. }
  96. func (d *DBMethods) QueryRowByID(ctx context.Context, id int64, row any) error {
  97. query := queryRowByIDString(row)
  98. return d.QueryRow(ctx, query, id).Scans(row)
  99. }
  100. func (d *DBMethods) RowExists(ctx context.Context, id int64, row any) bool {
  101. var exists int
  102. query := rowExistsString(row)
  103. if err := d.QueryRow(ctx, query, id).Scan(&exists); err == nil && exists == 1 {
  104. return true
  105. }
  106. return false
  107. }
  108. func (d *DBMethods) SetConnMaxLifetime(t time.Duration) {
  109. start := time.Now()
  110. d.DB.SetConnMaxLifetime(t)
  111. d.log("SetConnMaxLifetime", start, nil, false, "")
  112. }
  113. func (d *DBMethods) SetMaxIdleConns(n int) {
  114. start := time.Now()
  115. d.DB.SetMaxIdleConns(n)
  116. d.log("SetMaxIdleConns", start, nil, false, "")
  117. }
  118. func (d *DBMethods) SetMaxOpenConns(n int) {
  119. start := time.Now()
  120. d.DB.SetMaxOpenConns(n)
  121. d.log("SetMaxOpenConns", start, nil, false, "")
  122. }
  123. func (d *DBMethods) Transaction(ctx context.Context, callback func(ctx context.Context, tx *Tx) error) error {
  124. if callback == nil {
  125. return fmt.Errorf("callback is not set")
  126. }
  127. tx, err := d.Begin(ctx, nil)
  128. if err != nil {
  129. return err
  130. }
  131. if err := callback(ctx, tx); err != nil {
  132. rerr := tx.Rollback()
  133. if rerr != nil {
  134. return fmt.Errorf(
  135. "%v: %v",
  136. rerr,
  137. err,
  138. )
  139. }
  140. return err
  141. }
  142. return tx.Commit()
  143. }