/
tx.go
143 lines (116 loc) · 3.55 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
package exql
import (
"context"
"database/sql"
"fmt"
"github.com/loilo-inc/exql/v2/query"
)
type Tx interface {
Saver
Finder
Mapper
Tx() *sql.Tx
}
type tx struct {
s *saver
f *finder
tx *sql.Tx
}
func newTx(t *sql.Tx) *tx {
return &tx{s: newSaver(t), f: newFinder(t), tx: t}
}
func (t *tx) Insert(modelPtr Model) (sql.Result, error) {
return t.s.Insert(modelPtr)
}
func (t *tx) InsertContext(ctx context.Context, modelPtr Model) (sql.Result, error) {
return t.s.InsertContext(ctx, modelPtr)
}
func (t *tx) Update(table string, set map[string]interface{}, where query.Condition) (sql.Result, error) {
return t.s.Update(table, set, where)
}
func (t *tx) UpdateModel(ptr ModelUpdate, where query.Condition) (sql.Result, error) {
return t.s.UpdateModel(ptr, where)
}
func (t *tx) UpdateContext(ctx context.Context, table string, set map[string]interface{}, where query.Condition) (sql.Result, error) {
return t.s.UpdateContext(ctx, table, set, where)
}
func (t *tx) UpdateModelContext(ctx context.Context, ptr ModelUpdate, where query.Condition) (sql.Result, error) {
return t.s.UpdateModelContext(ctx, ptr, where)
}
func (t *tx) Delete(table string, where query.Condition) (sql.Result, error) {
return t.s.Delete(table, where)
}
func (t *tx) DeleteContext(ctx context.Context, table string, where query.Condition) (sql.Result, error) {
return t.s.DeleteContext(ctx, table, where)
}
func (d *tx) Exec(query query.Query) (sql.Result, error) {
return d.s.Exec(query)
}
func (d *tx) ExecContext(ctx context.Context, query query.Query) (sql.Result, error) {
return d.s.ExecContext(ctx, query)
}
func (d *tx) Query(query query.Query) (*sql.Rows, error) {
return d.s.Query(query)
}
func (d *tx) QueryContext(ctx context.Context, query query.Query) (*sql.Rows, error) {
return d.s.QueryContext(ctx, query)
}
func (d *tx) QueryRow(query query.Query) (*sql.Row, error) {
return d.s.QueryRow(query)
}
func (d *tx) QueryRowContext(ctx context.Context, query query.Query) (*sql.Row, error) {
return d.s.QueryRowContext(ctx, query)
}
// Find implements DB
func (t *tx) Find(q query.Query, destPtrOfStruct any) error {
return t.f.Find(q, destPtrOfStruct)
}
// FindContext implements DB
func (t *tx) FindContext(ctx context.Context, q query.Query, destPtrOfStruct any) error {
return t.f.FindContext(ctx, q, destPtrOfStruct)
}
// FindMany implements DB
func (t *tx) FindMany(q query.Query, destSlicePtrOfStruct any) error {
return t.f.FindMany(q, destSlicePtrOfStruct)
}
// FindManyContext implements DB
func (t *tx) FindManyContext(ctx context.Context, q query.Query, destSlicePtrOfStruct any) error {
return t.f.FindManyContext(ctx, q, destSlicePtrOfStruct)
}
// Deprecated: Use Find or MapRow/MapRows. It will be removed in next version.
func (t *tx) Map(rows *sql.Rows, destPtr any) error {
return MapRow(rows, destPtr)
}
// Deprecated: Use FindContext or MapRow/MapRows. It will be removed in next version.
func (t *tx) MapMany(rows *sql.Rows, destSlicePtr any) error {
return MapRows(rows, destSlicePtr)
}
func (t *tx) Tx() *sql.Tx {
return t.tx
}
func Transaction(db *sql.DB, ctx context.Context, opts *sql.TxOptions, callback func(tx Tx) error) error {
sqlTx, err := db.BeginTx(ctx, opts)
if err != nil {
return err
}
tx := newTx(sqlTx)
var p interface{}
txErr := func() error {
defer func() {
p = recover()
}()
return callback(tx)
}()
if p != nil {
txErr = fmt.Errorf("recovered: %s", p)
}
if txErr != nil {
if err := sqlTx.Rollback(); err != nil {
return err
}
return txErr
} else if err := sqlTx.Commit(); err != nil {
return err
}
return nil
}