-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtx.go
75 lines (66 loc) · 2.38 KB
/
tx.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
package mssqlx
import (
"context"
"database/sql"
)
// Tx wraps over sql.Tx
type Tx struct {
*sql.Tx
}
// Commit commits the transaction.
func (t *Tx) 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 *Tx) 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 *Tx) 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 *Tx) 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 *Tx) 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
}
// Query executes a query that returns rows, typically a SELECT.
func (t *Tx) 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 *Tx) 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
}