-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
244 lines (199 loc) · 4.25 KB
/
db.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package model
import (
"context"
"database/sql"
. "github.com/yang-zzhong/go-querybuilder"
)
const DEFAULT = "default"
var (
instances *Db
dbpairs map[string]dbpair
)
type DB interface {
Exec(string, ...interface{}) (sql.Result, error)
Query(string, ...interface{}) (*sql.Rows, error)
}
// tx callback type
type txhandler func(t *sql.Tx) error
type dbpair struct {
modifier Modifier
db *Db
}
type Db struct {
txs []*sql.Tx
*sql.DB
}
func init() {
dbpairs = make(map[string]dbpair)
}
func RegisterDefaultDB(db *sql.DB, m Modifier) {
RegisterDB(db, m, DEFAULT)
}
func RegisterDB(db *sql.DB, m Modifier, name string) {
dbpairs[name] = dbpair{m, &Db{[]*sql.Tx{}, db}}
}
func GetDB(name string) *Db {
if p, ok := dbpairs[name]; ok {
return p.db
}
panic("db config '" + name + "' not found")
}
func GetDefaultDB() *Db {
return GetDB(DEFAULT)
}
func GetModifier(name string) Modifier {
if p, ok := dbpairs[name]; ok {
return p.modifier
}
panic("db config '" + name + "' not found")
}
func GetDefaultModifier() Modifier {
return GetModifier(DEFAULT)
}
func UnregisterDefaultDB() {
UnregisterDB(DEFAULT)
}
func UnregisterDB(name string) {
delete(dbpairs, name)
}
func (db *Db) TxContext(handle txhandler, ctx context.Context, opts *sql.TxOptions) error {
var err error
db.BeginTx(ctx, opts)
if err != nil {
return err
}
defer func() {
if e := recover(); e != nil {
db.Rollback()
panic(e)
}
}()
if err := handle(db.tx()); err != nil {
db.Rollback()
return err
}
if err := db.Commit(); err != nil {
db.Rollback()
return err
}
return nil
}
func (db *Db) Tx(handle txhandler) error {
var err error
db.Begin()
if err != nil {
return err
}
defer func() {
if e := recover(); e != nil {
db.Rollback()
panic(e)
}
}()
if err := handle(db.tx()); err != nil {
db.Rollback()
return err
}
if err := db.Commit(); err != nil {
db.Rollback()
return err
}
return nil
}
func (db *Db) Begin() error {
tx, err := db.DB.Begin()
db.txs = append(db.txs, tx)
return err
}
func (db *Db) BeginTx(ctx context.Context, opts *sql.TxOptions) error {
tx, err := db.DB.BeginTx(ctx, opts)
db.txs = append(db.txs, tx)
return err
}
func (db *Db) Commit() error {
tx := db.tx()
if tx == nil {
panic("please begin before commit")
}
err := tx.Commit()
db.poptx()
return err
}
func (db *Db) Rollback() error {
tx := db.tx()
if tx == nil {
panic("please begin before rollback")
}
err := tx.Rollback()
db.poptx()
return err
}
func (db *Db) Exec(query string, args ...interface{}) (sql.Result, error) {
tx := db.tx()
if tx != nil {
return tx.Exec(query, args...)
}
return db.DB.Exec(query, args...)
}
func (db *Db) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
tx := db.tx()
if tx != nil {
return tx.ExecContext(ctx, query, args...)
}
return db.DB.ExecContext(ctx, query, args...)
}
func (db *Db) Prepare(query string) (*sql.Stmt, error) {
tx := db.tx()
if tx != nil {
return tx.Prepare(query)
}
return db.DB.Prepare(query)
}
func (db *Db) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {
tx := db.tx()
if tx != nil {
return tx.PrepareContext(ctx, query)
}
return db.DB.PrepareContext(ctx, query)
}
func (db *Db) Query(query string, args ...interface{}) (*sql.Rows, error) {
tx := db.tx()
if tx != nil {
return tx.Query(query, args...)
}
return db.DB.Query(query, args...)
}
func (db *Db) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
tx := db.tx()
if tx != nil {
return tx.QueryContext(ctx, query, args...)
}
return db.DB.QueryContext(ctx, query, args...)
}
func (db *Db) QueryRow(query string, args ...interface{}) *sql.Row {
tx := db.tx()
if tx != nil {
return tx.QueryRow(query, args...)
}
return db.DB.QueryRow(query, args...)
}
func (db *Db) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
tx := db.tx()
if tx != nil {
return tx.QueryRowContext(ctx, query, args...)
}
return db.DB.QueryRowContext(ctx, query, args...)
}
func (db *Db) tx() *sql.Tx {
if len(db.txs) == 0 {
return nil
}
return db.txs[len(db.txs)-1]
}
func (db *Db) poptx() {
if len(db.txs) == 1 {
db.txs = []*sql.Tx{}
return
}
db.txs = db.txs[:len(db.txs)-2]
}