-
Notifications
You must be signed in to change notification settings - Fork 15
/
txx.go
117 lines (103 loc) · 3.95 KB
/
txx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package mssqlx
import (
"context"
"database/sql"
"github.com/jmoiron/sqlx"
)
// Txx wraps over sqlx.Tx
type Txx struct {
*sqlx.Tx
}
// Commit commits the transaction.
func (t *Txx) Commit() (err error) {
_, err = retryFunc("tx_commit", func() (interface{}, error) {
return nil, t.Tx.Commit()
})
return
}
// Exec executes a query that doesn't return rows. For example: an INSERT and UPDATE.
func (t *Txx) Exec(query string, args ...interface{}) (result sql.Result, err error) {
return t.ExecContext(context.Background(), query, args...)
}
// ExecContext executes a query that doesn't return rows. For example: an INSERT and UPDATE.
func (t *Txx) ExecContext(ctx context.Context, query string, args ...interface{}) (result sql.Result, err error) {
r, err := retryFunc("tx_exec", func() (interface{}, error) {
return t.Tx.ExecContext(ctx, query, args...)
})
if err == nil {
result = r.(sql.Result)
}
return
}
// Prepare creates a prepared statement for use within a transaction.
//
// The returned statement operates within the transaction and will be closed when the transaction has been committed or rolled back.
func (t *Txx) Prepare(query string) (*Stmt, error) {
return t.PrepareContext(context.Background(), query)
}
// PrepareContext creates a prepared statement for use within a transaction.
//
// The returned statement operates within the transaction and will be closed when the transaction has been committed or rolled back.
//
// The provided context will be used for the preparation of the context, not for the execution of the returned statement. The returned statement will run in the transaction context.
func (t *Txx) PrepareContext(ctx context.Context, query string) (result *Stmt, err error) {
r, err := retryFunc("tx_prepare", func() (interface{}, error) {
return t.Tx.PrepareContext(ctx, query)
})
if err == nil {
result = &Stmt{
Stmt: r.(*sql.Stmt),
}
}
return
}
// Preparex creates a prepared statement for use within a transaction.
//
// The returned statement operates within the transaction and will be closed when the transaction has been committed or rolled back.
func (t *Txx) Preparex(query string) (*Stmtx, error) {
return t.PreparexContext(context.Background(), query)
}
// PreparexContext creates a prepared statement for use within a transaction.
//
// The returned statement operates within the transaction and will be closed when the transaction has been committed or rolled back.
//
// The provided context will be used for the preparation of the context, not for the execution of the returned statement. The returned statement will run in the transaction context.
func (t *Txx) PreparexContext(ctx context.Context, query string) (result *Stmtx, err error) {
r, err := retryFunc("tx_prepare", func() (interface{}, error) {
return t.Tx.PreparexContext(ctx, query)
})
if err == nil {
result = &Stmtx{
Stmt: r.(*sqlx.Stmt),
}
}
return
}
// Query executes a query that returns rows, typically a SELECT.
func (t *Txx) Query(query string, args ...interface{}) (*sql.Rows, error) {
return t.QueryContext(context.Background(), query, args...)
}
// QueryContext executes a query that returns rows, typically a SELECT.
func (t *Txx) QueryContext(ctx context.Context, query string, args ...interface{}) (result *sql.Rows, err error) {
r, err := retryFunc("tx_query", func() (interface{}, error) {
return t.Tx.QueryContext(ctx, query)
})
if err == nil {
result = r.(*sql.Rows)
}
return
}
// Queryx executes a query that returns rows, typically a SELECT.
func (t *Txx) Queryx(query string, args ...interface{}) (*sqlx.Rows, error) {
return t.QueryxContext(context.Background(), query, args...)
}
// QueryxContext executes a query that returns rows, typically a SELECT.
func (t *Txx) QueryxContext(ctx context.Context, query string, args ...interface{}) (result *sqlx.Rows, err error) {
r, err := retryFunc("tx_query", func() (interface{}, error) {
return t.Tx.QueryxContext(ctx, query)
})
if err == nil {
result = r.(*sqlx.Rows)
}
return
}