-
Notifications
You must be signed in to change notification settings - Fork 4
/
twowaysql.go
197 lines (154 loc) · 4.64 KB
/
twowaysql.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Package twowaysql provides an implementation of 2WaySQL.
package twowaysql
import (
"context"
"database/sql"
"fmt"
"github.com/jmoiron/sqlx"
)
// Twowaysql is a struct for issuing 2WaySQL query
type Twowaysql struct {
db *sqlx.DB
}
// New returns instance of Twowaysql
func New(db *sqlx.DB) *Twowaysql {
return &Twowaysql{
db: db,
}
}
// Select is a thin wrapper around db.Select in the sqlx package.
// params takes a tagged struct. The tags format must be `twowaysql:"tag_name"`.
// dest takes a pointer to a slice of a struct. The struct tag format must be `db:"tag_name"`.
func (t *Twowaysql) Select(ctx context.Context, dest interface{}, query string, params interface{}) error {
eval, bindParams, err := Eval(query, params)
if err != nil {
return err
}
q := t.db.Rebind(eval)
if destMap, ok := dest.(*[]map[string]interface{}); ok {
rows, err := t.db.QueryxContext(ctx, q, bindParams...)
if err != nil {
return err
}
return convertResultToMap(destMap, rows)
}
return t.db.SelectContext(ctx, dest, q, bindParams...)
}
// Exec is a thin wrapper around db.Exec in the sqlx package.
// params takes a tagged struct. The tags format must be `twowaysql:"tag_name"`.
func (t *Twowaysql) Exec(ctx context.Context, query string, params interface{}) (sql.Result, error) {
eval, bindParams, err := Eval(query, params)
if err != nil {
return nil, err
}
q := t.db.Rebind(eval)
return t.db.ExecContext(ctx, q, bindParams...)
}
// Begin is a thin wrapper around db.BeginTxx in the sqlx package.
func (t *Twowaysql) Begin(ctx context.Context) (*TwowaysqlTx, error) {
tx, err := t.db.BeginTxx(ctx, nil)
if err != nil {
return nil, err
}
return &TwowaysqlTx{tx: tx}, nil
}
// Close is a thin wrapper around db.Close in the sqlx package.
func (t *Twowaysql) Close() error {
if err := t.db.Close(); err != nil {
return fmt.Errorf("close db: %w", err)
}
return nil
}
// DB returns `*sqlx.DB`
func (t *Twowaysql) DB() *sqlx.DB {
return t.db
}
// Transaction starts a transaction as a block.
// arguments function is return error will rollback, otherwise to commit.
func (t *Twowaysql) Transaction(ctx context.Context, fn func(tx *TwowaysqlTx) error) error {
tx, err := t.Begin(ctx)
if err != nil {
return err
}
defer func() {
if p := recover(); p != nil {
if rerr := tx.Rollback(); rerr != nil {
panic(fmt.Sprintf("panic occured %v and failed rollback %v", p, rerr))
}
panic(p)
}
}()
if err := fn(tx); err != nil {
if rerr := tx.Rollback(); rerr != nil {
return fmt.Errorf("failed rollback %v: %w", rerr, err)
}
return err
}
if err := tx.Commit(); err != nil {
return err
}
return nil
}
// TwowaysqlTx is a structure for issuing 2WaySQL queries within a transaction.
type TwowaysqlTx struct {
tx *sqlx.Tx
}
// Commit is a thin wrapper around tx.Commit in the sqlx package.
func (t *TwowaysqlTx) Commit() error {
if err := t.tx.Commit(); err != nil {
return err
}
return nil
}
// Rollback is a thin wrapper around tx.Rollback in the sqlx package.
func (t *TwowaysqlTx) Rollback() error {
if err := t.tx.Rollback(); err != nil {
return err
}
return nil
}
// Select is a thin wrapper around db.Select in the sqlx package.
// params takes a tagged struct. The tags format must be `twowaysql:"tag_name"`.
// dest takes a pointer to a slice of a struct. The struct tag format must be `db:"tag_name"`.
// It is an equivalent implementation of Twowaysql.Select
func (t *TwowaysqlTx) Select(ctx context.Context, dest interface{}, query string, params interface{}) error {
eval, bindParams, err := Eval(query, params)
if err != nil {
return err
}
q := t.tx.Rebind(eval)
if destMap, ok := dest.(*[]map[string]interface{}); ok {
rows, err := t.tx.QueryxContext(ctx, q, bindParams...)
if err != nil {
return err
}
return convertResultToMap(destMap, rows)
}
return t.tx.SelectContext(ctx, dest, q, bindParams...)
}
// Exec is a thin wrapper around db.Exec in the sqlx package.
// params takes a tagged struct. The tags format must be `twowaysql:"tag_name"`.
// It is an equivalent implementation of Twowaysql.Exec
func (t *TwowaysqlTx) Exec(ctx context.Context, query string, params interface{}) (sql.Result, error) {
eval, bindParams, err := Eval(query, params)
if err != nil {
return nil, err
}
q := t.tx.Rebind(eval)
return t.tx.ExecContext(ctx, q, bindParams...)
}
func convertResultToMap(dest *[]map[string]interface{}, rows *sqlx.Rows) error {
defer rows.Close()
for rows.Next() {
row := map[string]interface{}{}
if err := rows.MapScan(row); err != nil {
return err
}
*dest = append(*dest, row)
}
return nil
}
// Tx returns `*sqlx.Tx`
func (t *TwowaysqlTx) Tx() *sqlx.Tx {
return t.tx
}