dbmethods.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package common
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "time"
  7. "github.com/pkg/errors"
  8. )
  9. type DBMethods struct {
  10. DB *sql.DB
  11. Debug bool
  12. Driver string
  13. }
  14. func (db *DBMethods) fixQuery(query string) string {
  15. if db.Driver == "mysql" {
  16. return fixQuery(query)
  17. }
  18. return query
  19. }
  20. func (db *DBMethods) Begin(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
  21. if db.Debug {
  22. t := time.Now()
  23. tx, err := db.DB.BeginTx(ctx, opts)
  24. log("[func Begin]", t, err, true, "")
  25. return &Tx{tx, db.Debug, db.Driver, t}, err
  26. }
  27. tx, err := db.DB.BeginTx(ctx, opts)
  28. if err != nil {
  29. return nil, err
  30. }
  31. return &Tx{tx, db.Debug, db.Driver, time.Now()}, err
  32. }
  33. func (db *DBMethods) Close() error {
  34. if db.Debug {
  35. t := time.Now()
  36. err := db.DB.Close()
  37. log("[func Close]", t, err, false, "")
  38. return err
  39. }
  40. return db.DB.Close()
  41. }
  42. func (db *DBMethods) Exec(ctx context.Context, query string, args ...any) (sql.Result, error) {
  43. if db.Debug {
  44. t := time.Now()
  45. res, err := db.DB.ExecContext(ctx, db.fixQuery(query), args...)
  46. log("[func Exec]", t, err, false, db.fixQuery(query), args...)
  47. return res, err
  48. }
  49. return db.DB.ExecContext(ctx, db.fixQuery(query), args...)
  50. }
  51. func (db *DBMethods) Ping(ctx context.Context) error {
  52. if db.Debug {
  53. t := time.Now()
  54. err := db.DB.PingContext(ctx)
  55. log("[func Ping]", t, err, false, "")
  56. return err
  57. }
  58. return db.DB.PingContext(ctx)
  59. }
  60. func (db *DBMethods) Prepare(ctx context.Context, query string) (*sql.Stmt, error) {
  61. if db.Debug {
  62. t := time.Now()
  63. stm, err := db.DB.PrepareContext(ctx, db.fixQuery(query))
  64. log("[func Prepare]", t, err, false, db.fixQuery(query))
  65. return stm, err
  66. }
  67. return db.DB.PrepareContext(ctx, db.fixQuery(query))
  68. }
  69. func (db *DBMethods) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
  70. if db.Debug {
  71. t := time.Now()
  72. rows, err := db.DB.QueryContext(ctx, db.fixQuery(query), args...)
  73. log("[func Query]", t, err, false, db.fixQuery(query), args...)
  74. return rows, err
  75. }
  76. return db.DB.QueryContext(ctx, db.fixQuery(query), args...)
  77. }
  78. func (db *DBMethods) QueryRow(ctx context.Context, query string, args ...any) *sql.Row {
  79. if db.Debug {
  80. t := time.Now()
  81. row := db.DB.QueryRowContext(ctx, db.fixQuery(query), args...)
  82. log("[func QueryRow]", t, nil, false, db.fixQuery(query), args...)
  83. return row
  84. }
  85. return db.DB.QueryRowContext(ctx, db.fixQuery(query), args...)
  86. }
  87. func (db *DBMethods) Transaction(ctx context.Context, queries queryFunc) error {
  88. if queries == nil {
  89. return fmt.Errorf("queries is not set for transaction")
  90. }
  91. tx, err := db.Begin(ctx, nil)
  92. if err != nil {
  93. return err
  94. }
  95. if err := queries(ctx, tx); err != nil {
  96. return errors.Wrap(err, tx.Rollback().Error())
  97. }
  98. return tx.Commit()
  99. }