tx.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. ctx, span := startSpan(ctx)
  28. defer span.End()
  29. return exec(ctx, t.Tx, q, args...)
  30. }
  31. func (t txSession) Prepare(q string) (StmtSession, error) {
  32. return t.PrepareCtx(context.Background(), q)
  33. }
  34. func (t txSession) PrepareCtx(ctx context.Context, q string) (StmtSession, error) {
  35. ctx, span := startSpan(ctx)
  36. defer span.End()
  37. stmt, err := t.Tx.PrepareContext(ctx, q)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return statement{
  42. query: q,
  43. stmt: stmt,
  44. }, nil
  45. }
  46. func (t txSession) QueryRow(v interface{}, q string, args ...interface{}) error {
  47. return t.QueryRowCtx(context.Background(), v, q, args...)
  48. }
  49. func (t txSession) QueryRowCtx(ctx context.Context, v interface{}, q string, args ...interface{}) error {
  50. ctx, span := startSpan(ctx)
  51. defer span.End()
  52. return query(ctx, t.Tx, func(rows *sql.Rows) error {
  53. return unmarshalRow(v, rows, true)
  54. }, q, args...)
  55. }
  56. func (t txSession) QueryRowPartial(v interface{}, q string, args ...interface{}) error {
  57. return t.QueryRowPartialCtx(context.Background(), v, q, args...)
  58. }
  59. func (t txSession) QueryRowPartialCtx(ctx context.Context, v interface{}, q string,
  60. args ...interface{}) error {
  61. ctx, span := startSpan(ctx)
  62. defer span.End()
  63. return query(ctx, t.Tx, func(rows *sql.Rows) error {
  64. return unmarshalRow(v, rows, false)
  65. }, q, args...)
  66. }
  67. func (t txSession) QueryRows(v interface{}, q string, args ...interface{}) error {
  68. return t.QueryRowsCtx(context.Background(), v, q, args...)
  69. }
  70. func (t txSession) QueryRowsCtx(ctx context.Context, v interface{}, q string, args ...interface{}) error {
  71. ctx, span := startSpan(ctx)
  72. defer span.End()
  73. return query(ctx, t.Tx, func(rows *sql.Rows) error {
  74. return unmarshalRows(v, rows, true)
  75. }, q, args...)
  76. }
  77. func (t txSession) QueryRowsPartial(v interface{}, q string, args ...interface{}) error {
  78. return t.QueryRowsPartialCtx(context.Background(), v, q, args...)
  79. }
  80. func (t txSession) QueryRowsPartialCtx(ctx context.Context, v interface{}, q string,
  81. args ...interface{}) error {
  82. ctx, span := startSpan(ctx)
  83. defer span.End()
  84. return query(ctx, t.Tx, func(rows *sql.Rows) error {
  85. return unmarshalRows(v, rows, false)
  86. }, q, args...)
  87. }
  88. func begin(db *sql.DB) (trans, error) {
  89. tx, err := db.Begin()
  90. if err != nil {
  91. return nil, err
  92. }
  93. return txSession{
  94. Tx: tx,
  95. }, nil
  96. }
  97. func transact(ctx context.Context, db *commonSqlConn, b beginnable,
  98. fn func(context.Context, Session) error) (err error) {
  99. conn, err := db.connProv()
  100. if err != nil {
  101. db.onError(err)
  102. return err
  103. }
  104. return transactOnConn(ctx, conn, b, fn)
  105. }
  106. func transactOnConn(ctx context.Context, conn *sql.DB, b beginnable,
  107. fn func(context.Context, Session) error) (err error) {
  108. var tx trans
  109. tx, err = b(conn)
  110. if err != nil {
  111. return
  112. }
  113. defer func() {
  114. if p := recover(); p != nil {
  115. if e := tx.Rollback(); e != nil {
  116. err = fmt.Errorf("recover from %#v, rollback failed: %w", p, e)
  117. } else {
  118. err = fmt.Errorf("recoveer from %#v", p)
  119. }
  120. } else if err != nil {
  121. if e := tx.Rollback(); e != nil {
  122. err = fmt.Errorf("transaction failed: %s, rollback failed: %w", err, e)
  123. }
  124. } else {
  125. err = tx.Commit()
  126. }
  127. }()
  128. return fn(ctx, tx)
  129. }