-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtx.go
157 lines (126 loc) · 2.93 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package sqle
import (
"context"
"database/sql"
)
type Tx struct {
*sql.Tx
noCopy //nolint
stmts map[string]*sql.Stmt
}
func (tx *Tx) prepareStmt(ctx context.Context, query string) (*sql.Stmt, error) {
if tx.stmts == nil {
tx.stmts = make(map[string]*sql.Stmt)
}
s, ok := tx.stmts[query]
if ok {
return s, nil
}
s, err := tx.Tx.PrepareContext(ctx, query)
if err != nil {
return nil, err
}
tx.stmts[query] = s
return s, nil
}
func (tx *Tx) closeStmts() {
for _, stmt := range tx.stmts {
stmt.Close()
}
}
func (tx *Tx) Query(query string, args ...any) (*Rows, error) {
return tx.QueryContext(context.Background(), query, args...)
}
func (tx *Tx) QueryBuilder(ctx context.Context, b *Builder) (*Rows, error) {
query, args, err := b.Build()
if err != nil {
return nil, err
}
return tx.QueryContext(ctx, query, args...)
}
func (tx *Tx) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error) {
if len(args) > 0 {
stmt, err := tx.prepareStmt(ctx, query)
if err != nil {
return nil, err
}
rows, err := stmt.QueryContext(ctx, args...)
if err != nil {
return nil, err
}
return &Rows{Rows: rows, query: query}, nil
}
rows, err := tx.Tx.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
return &Rows{Rows: rows, query: query}, nil
}
func (tx *Tx) QueryRow(query string, args ...any) *Row {
return tx.QueryRowContext(context.Background(), query, args...)
}
func (tx *Tx) QueryRowBuilder(ctx context.Context, b *Builder) *Row {
query, args, err := b.Build()
if err != nil {
return &Row{
err: err,
}
}
return tx.QueryRowContext(ctx, query, args...)
}
func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...any) *Row {
if len(args) > 0 {
stmt, err := tx.prepareStmt(ctx, query)
if err != nil {
return &Row{
err: err,
query: query,
}
}
rows, err := stmt.QueryContext(ctx, args...)
if err != nil {
return &Row{
err: err,
query: query,
}
}
return &Row{
rows: rows,
query: query,
}
}
rows, err := tx.Tx.QueryContext(ctx, query, args...)
return &Row{
rows: rows,
err: err,
query: query,
}
}
func (tx *Tx) Exec(query string, args ...any) (sql.Result, error) {
return tx.ExecContext(context.Background(), query, args...)
}
func (tx *Tx) ExecBuilder(ctx context.Context, b *Builder) (sql.Result, error) {
query, args, err := b.Build()
if err != nil {
return nil, err
}
return tx.ExecContext(ctx, query, args...)
}
func (tx *Tx) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
if len(args) > 0 {
stmt, err := tx.prepareStmt(ctx, query)
if err != nil {
return nil, err
}
return stmt.ExecContext(ctx, args...)
}
return tx.Tx.ExecContext(context.Background(), query, args...)
}
func (tx *Tx) Rollback() error {
defer tx.closeStmts()
return tx.Tx.Rollback()
}
func (tx *Tx) Commit() error {
defer tx.closeStmts()
return tx.Tx.Commit()
}