tx.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package sqlx
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. )
  7. type (
  8. beginnable func(*sql.DB) (trans, error)
  9. trans interface {
  10. Session
  11. Commit() error
  12. Rollback() error
  13. }
  14. txSession struct {
  15. *sql.Tx
  16. }
  17. )
  18. // NewSessionFromTx returns a Session with the given sql.Tx.
  19. // Use it with caution, it's provided for other ORM to interact with.
  20. func NewSessionFromTx(tx *sql.Tx) Session {
  21. return txSession{Tx: tx}
  22. }
  23. func (t txSession) Exec(q string, args ...interface{}) (sql.Result, error) {
  24. return t.ExecCtx(context.Background(), q, args...)
  25. }
  26. func (t txSession) ExecCtx(ctx context.Context, q string, args ...interface{}) (sql.Result, error) {
  27. return exec(ctx, t.Tx, q, args...)
  28. }
  29. func (t txSession) Prepare(q string) (StmtSession, error) {
  30. return t.PrepareCtx(context.Background(), q)
  31. }
  32. func (t txSession) PrepareCtx(ctx context.Context, q string) (StmtSession, error) {
  33. stmt, err := t.Tx.PrepareContext(ctx, q)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return statement{
  38. query: q,
  39. stmt: stmt,
  40. }, nil
  41. }
  42. func (t txSession) QueryRow(v interface{}, q string, args ...interface{}) error {
  43. return t.QueryRowCtx(context.Background(), v, q, args...)
  44. }
  45. func (t txSession) QueryRowCtx(ctx context.Context, v interface{}, q string, args ...interface{}) error {
  46. return query(ctx, t.Tx, func(rows *sql.Rows) error {
  47. return unmarshalRow(v, rows, true)
  48. }, q, args...)
  49. }
  50. func (t txSession) QueryRowPartial(v interface{}, q string, args ...interface{}) error {
  51. return t.QueryRowPartialCtx(context.Background(), v, q, args...)
  52. }
  53. func (t txSession) QueryRowPartialCtx(ctx context.Context, v interface{}, q string,
  54. args ...interface{}) error {
  55. return query(ctx, t.Tx, func(rows *sql.Rows) error {
  56. return unmarshalRow(v, rows, false)
  57. }, q, args...)
  58. }
  59. func (t txSession) QueryRows(v interface{}, q string, args ...interface{}) error {
  60. return t.QueryRowsCtx(context.Background(), v, q, args...)
  61. }
  62. func (t txSession) QueryRowsCtx(ctx context.Context, v interface{}, q string, args ...interface{}) error {
  63. return query(ctx, t.Tx, func(rows *sql.Rows) error {
  64. return unmarshalRows(v, rows, true)
  65. }, q, args...)
  66. }
  67. func (t txSession) QueryRowsPartial(v interface{}, q string, args ...interface{}) error {
  68. return t.QueryRowsPartialCtx(context.Background(), v, q, args...)
  69. }
  70. func (t txSession) QueryRowsPartialCtx(ctx context.Context, v interface{}, q string,
  71. args ...interface{}) error {
  72. return query(ctx, t.Tx, func(rows *sql.Rows) error {
  73. return unmarshalRows(v, rows, false)
  74. }, q, args...)
  75. }
  76. func begin(db *sql.DB) (trans, error) {
  77. tx, err := db.Begin()
  78. if err != nil {
  79. return nil, err
  80. }
  81. return txSession{
  82. Tx: tx,
  83. }, nil
  84. }
  85. func transact(ctx context.Context, db *commonSqlConn, b beginnable,
  86. fn func(context.Context, Session) error) (err error) {
  87. conn, err := db.connProv()
  88. if err != nil {
  89. db.onError(err)
  90. return err
  91. }
  92. return transactOnConn(ctx, conn, b, fn)
  93. }
  94. func transactOnConn(ctx context.Context, conn *sql.DB, b beginnable,
  95. fn func(context.Context, Session) error) (err error) {
  96. var tx trans
  97. tx, err = b(conn)
  98. if err != nil {
  99. return
  100. }
  101. defer func() {
  102. if p := recover(); p != nil {
  103. if e := tx.Rollback(); e != nil {
  104. err = fmt.Errorf("recover from %#v, rollback failed: %w", p, e)
  105. } else {
  106. err = fmt.Errorf("recoveer from %#v", p)
  107. }
  108. } else if err != nil {
  109. if e := tx.Rollback(); e != nil {
  110. err = fmt.Errorf("transaction failed: %s, rollback failed: %w", err, e)
  111. }
  112. } else {
  113. err = tx.Commit()
  114. }
  115. }()
  116. return fn(ctx, tx)
  117. }