dbmethods.go 2.8 KB

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