dbmethods.go 4.0 KB

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