txw.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package sqlw
  2. import (
  3. "context"
  4. "database/sql"
  5. _ "github.com/go-sql-driver/mysql"
  6. "time"
  7. "golang-fave/engine/consts"
  8. )
  9. type Tx struct {
  10. tx *sql.Tx
  11. s time.Time
  12. }
  13. func (this *Tx) Commit() error {
  14. if consts.ParamDebug {
  15. err := this.tx.Commit()
  16. log("[TX] TRANSACTION END (Commit)", this.s, err, true)
  17. return err
  18. }
  19. return this.tx.Commit()
  20. }
  21. func (this *Tx) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
  22. if consts.ParamDebug {
  23. s := time.Now()
  24. r, e := this.tx.ExecContext(ctx, query, args...)
  25. log("[TX] "+query, s, e, true)
  26. return r, e
  27. }
  28. return this.tx.ExecContext(ctx, query, args...)
  29. }
  30. func (this *Tx) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
  31. if consts.ParamDebug {
  32. s := time.Now()
  33. r, e := this.tx.QueryContext(ctx, query, args...)
  34. log("[TX] "+query, s, e, true)
  35. return r, e
  36. }
  37. return this.tx.QueryContext(ctx, query, args...)
  38. }
  39. func (this *Tx) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row {
  40. if consts.ParamDebug {
  41. s := time.Now()
  42. r := this.tx.QueryRowContext(ctx, query, args...)
  43. log("[TX] "+query, s, nil, true)
  44. return r
  45. }
  46. return this.tx.QueryRowContext(ctx, query, args...)
  47. }
  48. func (this *Tx) Rollback() error {
  49. if consts.ParamDebug {
  50. err := this.tx.Rollback()
  51. log("[TX] TRANSACTION END (Rollback)", this.s, nil, true)
  52. return err
  53. }
  54. return this.tx.Rollback()
  55. }