You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tx.go 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright 2019 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package core
  5. import (
  6. "context"
  7. "database/sql"
  8. "xorm.io/xorm/contexts"
  9. )
  10. var (
  11. _ QueryExecuter = &Tx{}
  12. )
  13. // Tx represents a transaction
  14. type Tx struct {
  15. *sql.Tx
  16. db *DB
  17. ctx context.Context
  18. }
  19. func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
  20. hookCtx := contexts.NewContextHook(ctx, "BEGIN TRANSACTION", nil)
  21. ctx, err := db.beforeProcess(hookCtx)
  22. if err != nil {
  23. return nil, err
  24. }
  25. tx, err := db.DB.BeginTx(ctx, opts)
  26. hookCtx.End(ctx, nil, err)
  27. if err := db.afterProcess(hookCtx); err != nil {
  28. return nil, err
  29. }
  30. return &Tx{tx, db, ctx}, nil
  31. }
  32. func (db *DB) Begin() (*Tx, error) {
  33. return db.BeginTx(context.Background(), nil)
  34. }
  35. func (tx *Tx) Commit() error {
  36. hookCtx := contexts.NewContextHook(tx.ctx, "COMMIT", nil)
  37. ctx, err := tx.db.beforeProcess(hookCtx)
  38. if err != nil {
  39. return err
  40. }
  41. err = tx.Tx.Commit()
  42. hookCtx.End(ctx, nil, err)
  43. if err := tx.db.afterProcess(hookCtx); err != nil {
  44. return err
  45. }
  46. return nil
  47. }
  48. func (tx *Tx) Rollback() error {
  49. hookCtx := contexts.NewContextHook(tx.ctx, "ROLLBACK", nil)
  50. ctx, err := tx.db.beforeProcess(hookCtx)
  51. if err != nil {
  52. return err
  53. }
  54. err = tx.Tx.Rollback()
  55. hookCtx.End(ctx, nil, err)
  56. if err := tx.db.afterProcess(hookCtx); err != nil {
  57. return err
  58. }
  59. return nil
  60. }
  61. func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
  62. names := make(map[string]int)
  63. var i int
  64. query = re.ReplaceAllStringFunc(query, func(src string) string {
  65. names[src[1:]] = i
  66. i++
  67. return "?"
  68. })
  69. hookCtx := contexts.NewContextHook(ctx, "PREPARE", nil)
  70. ctx, err := tx.db.beforeProcess(hookCtx)
  71. if err != nil {
  72. return nil, err
  73. }
  74. stmt, err := tx.Tx.PrepareContext(ctx, query)
  75. hookCtx.End(ctx, nil, err)
  76. if err := tx.db.afterProcess(hookCtx); err != nil {
  77. return nil, err
  78. }
  79. return &Stmt{stmt, tx.db, names, query}, nil
  80. }
  81. func (tx *Tx) Prepare(query string) (*Stmt, error) {
  82. return tx.PrepareContext(context.Background(), query)
  83. }
  84. func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt {
  85. stmt.Stmt = tx.Tx.StmtContext(ctx, stmt.Stmt)
  86. return stmt
  87. }
  88. func (tx *Tx) Stmt(stmt *Stmt) *Stmt {
  89. return tx.StmtContext(context.Background(), stmt)
  90. }
  91. func (tx *Tx) ExecMapContext(ctx context.Context, query string, mp interface{}) (sql.Result, error) {
  92. query, args, err := MapToSlice(query, mp)
  93. if err != nil {
  94. return nil, err
  95. }
  96. return tx.ExecContext(ctx, query, args...)
  97. }
  98. func (tx *Tx) ExecMap(query string, mp interface{}) (sql.Result, error) {
  99. return tx.ExecMapContext(context.Background(), query, mp)
  100. }
  101. func (tx *Tx) ExecStructContext(ctx context.Context, query string, st interface{}) (sql.Result, error) {
  102. query, args, err := StructToSlice(query, st)
  103. if err != nil {
  104. return nil, err
  105. }
  106. return tx.ExecContext(ctx, query, args...)
  107. }
  108. func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
  109. hookCtx := contexts.NewContextHook(ctx, query, args)
  110. ctx, err := tx.db.beforeProcess(hookCtx)
  111. if err != nil {
  112. return nil, err
  113. }
  114. res, err := tx.Tx.ExecContext(ctx, query, args...)
  115. hookCtx.End(ctx, res, err)
  116. if err := tx.db.afterProcess(hookCtx); err != nil {
  117. return nil, err
  118. }
  119. return res, err
  120. }
  121. func (tx *Tx) ExecStruct(query string, st interface{}) (sql.Result, error) {
  122. return tx.ExecStructContext(context.Background(), query, st)
  123. }
  124. func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) {
  125. hookCtx := contexts.NewContextHook(ctx, query, args)
  126. ctx, err := tx.db.beforeProcess(hookCtx)
  127. if err != nil {
  128. return nil, err
  129. }
  130. rows, err := tx.Tx.QueryContext(ctx, query, args...)
  131. hookCtx.End(ctx, nil, err)
  132. if err := tx.db.afterProcess(hookCtx); err != nil {
  133. if rows != nil {
  134. rows.Close()
  135. }
  136. return nil, err
  137. }
  138. return &Rows{rows, tx.db}, nil
  139. }
  140. func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) {
  141. return tx.QueryContext(context.Background(), query, args...)
  142. }
  143. func (tx *Tx) QueryMapContext(ctx context.Context, query string, mp interface{}) (*Rows, error) {
  144. query, args, err := MapToSlice(query, mp)
  145. if err != nil {
  146. return nil, err
  147. }
  148. return tx.QueryContext(ctx, query, args...)
  149. }
  150. func (tx *Tx) QueryMap(query string, mp interface{}) (*Rows, error) {
  151. return tx.QueryMapContext(context.Background(), query, mp)
  152. }
  153. func (tx *Tx) QueryStructContext(ctx context.Context, query string, st interface{}) (*Rows, error) {
  154. query, args, err := StructToSlice(query, st)
  155. if err != nil {
  156. return nil, err
  157. }
  158. return tx.QueryContext(ctx, query, args...)
  159. }
  160. func (tx *Tx) QueryStruct(query string, st interface{}) (*Rows, error) {
  161. return tx.QueryStructContext(context.Background(), query, st)
  162. }
  163. func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row {
  164. rows, err := tx.QueryContext(ctx, query, args...)
  165. return &Row{rows, err}
  166. }
  167. func (tx *Tx) QueryRow(query string, args ...interface{}) *Row {
  168. return tx.QueryRowContext(context.Background(), query, args...)
  169. }
  170. func (tx *Tx) QueryRowMapContext(ctx context.Context, query string, mp interface{}) *Row {
  171. query, args, err := MapToSlice(query, mp)
  172. if err != nil {
  173. return &Row{nil, err}
  174. }
  175. return tx.QueryRowContext(ctx, query, args...)
  176. }
  177. func (tx *Tx) QueryRowMap(query string, mp interface{}) *Row {
  178. return tx.QueryRowMapContext(context.Background(), query, mp)
  179. }
  180. func (tx *Tx) QueryRowStructContext(ctx context.Context, query string, st interface{}) *Row {
  181. query, args, err := StructToSlice(query, st)
  182. if err != nil {
  183. return &Row{nil, err}
  184. }
  185. return tx.QueryRowContext(ctx, query, args...)
  186. }
  187. func (tx *Tx) QueryRowStruct(query string, st interface{}) *Row {
  188. return tx.QueryRowStructContext(context.Background(), query, st)
  189. }